GeezerVison is showing github.com — feat(tui): richer /resume session browser (#1047) · Twigpine/zero@2f53117
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/tui/command_center.go +29-10Lines changed: 29 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "strconv" | 9 | 9 | "strings" | 10 | 10 | "time" | | 11 | + "unicode" | 11 | 12 |
| 12 | 13 | tea "charm.land/bubbletea/v2" | 13 | 14 |
| @@ -365,17 +366,30 @@ func (defaultModelSwitchCompactionPolicy) BeforeModelSwitch(request modelSwitchC | 365 | 366 |
| 366 | 367 | var modelSwitchCompactionGuard modelSwitchCompactionPolicy = defaultModelSwitchCompactionPolicy{} | 367 | 368 |
| 368 | | -// sanitizeCardField strips the card protocol's separator bytes from | 369 | | -// user-controlled values (titles can legally contain anything --session-title | 370 | | -// was given), so a hostile or accidental \x1f / newline cannot shift fields | 371 | | -// or leak control characters into the transcript. | | 369 | +// sanitizeCardField strips the card protocol's separator byte and every | | 370 | +// control rune from user-controlled values (titles can legally contain | | 371 | +// anything --session-title was given, and a directory name can legally | | 372 | +// contain an ESC byte on Unix). Line separators and tabs become spaces so | | 373 | +// words stay apart; the remaining controls — ESC/CSI/OSC initiators, BEL, BS, | | 374 | +// VT, NEL, NUL — are dropped outright, since any of them can repaint the | | 375 | +// terminal or shift cells when the value lands in a rendered row. | 372 | 376 | func sanitizeCardField(value string) string { | 373 | | - value = strings.ReplaceAll(value, sessionsCardFieldSep, " ") | 374 | | - value = strings.ReplaceAll(value, "\n", " ") | 375 | | - return strings.ReplaceAll(value, "\x00", "") | | 377 | + var out strings.Builder | | 378 | + for _, r := range value { | | 379 | + switch { | | 380 | + case r == '\x1f' || r == '\n' || r == '\r' || r == '\t': | | 381 | + out.WriteRune(' ') | | 382 | + case unicode.IsControl(r): | | 383 | + default: | | 384 | + out.WriteRune(r) | | 385 | + } | | 386 | + } | | 387 | + return out.String() | 376 | 388 | } | 377 | 389 |
| 378 | | -// relativeAge renders an RFC3339 timestamp as a short age ("2h ago"); "" | | 390 | +// relativeAge renders an RFC3339 timestamp as a short age ("2h ago"), falling | | 391 | +// back to the month/day this year and the bare date for sessions older than a | | 392 | +// month, so deep history reads as a calendar date rather than "400d ago"; "" | 379 | 393 | // when the timestamp does not parse, so the card simply omits it. | 380 | 394 | func relativeAge(timestamp string, now time.Time) string { | 381 | 395 | parsed, err := time.Parse(time.RFC3339, strings.TrimSpace(timestamp)) | @@ -385,14 +399,19 @@ func relativeAge(timestamp string, now time.Time) string { | 385 | 399 | age := now.Sub(parsed) | 386 | 400 | switch { | 387 | 401 | case age < time.Minute: | 388 | | - return "just now" | | 402 | + return "now" | 389 | 403 | case age < time.Hour: | 390 | 404 | return fmt.Sprintf("%dm ago", int(age.Minutes())) | 391 | 405 | case age < 24*time.Hour: | 392 | 406 | return fmt.Sprintf("%dh ago", int(age.Hours())) | 393 | | - default: | | 407 | + case age <= 30*24*time.Hour: | 394 | 408 | return fmt.Sprintf("%dd ago", int(age.Hours()/24)) | 395 | 409 | } | | 410 | + parsed, now = parsed.Local(), now.Local() | | 411 | + if parsed.Year() == now.Year() { | | 412 | + return parsed.Format("Jan _2") | | 413 | + } | | 414 | + return parsed.Format("2006-01-02") | 396 | 415 | } | 397 | 416 |
| 398 | 417 | // handleModelCommand applies a model switch against the ACTIVE provider (the | |
Collapse file Copy file name to clipboardExpand all lines: internal/tui/model_test.go +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1087,16 +1087,16 @@ func TestResumeCommandListsRecentSessions(t *testing.T) { | 1087 | 1087 | } | 1088 | 1088 |
| 1089 | 1089 | func TestSessionPickerLabelAlignsTitles(t *testing.T) { | 1090 | | - today := sessionPickerLabel("20:47:50", "Today title") | 1091 | | - older := sessionPickerLabel("Jul 24 10:47", "Older title") | | 1090 | + recent := sessionPickerLabel("2h ago", "Recent title") | | 1091 | + older := sessionPickerLabel("2006-01-02", "Older title") | 1092 | 1092 |
| 1093 | | - todayColumn := strings.Index(today, "Today title") | | 1093 | + recentColumn := strings.Index(recent, "Recent title") | 1094 | 1094 | olderColumn := strings.Index(older, "Older title") | 1095 | | - if todayColumn < 0 || olderColumn < 0 { | 1096 | | - t.Fatalf("sessionPickerLabel omitted a title: today=%q older=%q", today, older) | | 1095 | + if recentColumn < 0 || olderColumn < 0 { | | 1096 | + t.Fatalf("sessionPickerLabel omitted a title: recent=%q older=%q", recent, older) | 1097 | 1097 | } | 1098 | | - if todayColumn != olderColumn { | 1099 | | - t.Fatalf("title columns differ: today=%d (%q), older=%d (%q)", todayColumn, today, olderColumn, older) | | 1098 | + if recentColumn != olderColumn { | | 1099 | + t.Fatalf("title columns differ: recent=%d (%q), older=%d (%q)", recentColumn, recent, olderColumn, older) | 1100 | 1100 | } | 1101 | 1101 | } | 1102 | 1102 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/tui/mouse.go +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -513,10 +513,11 @@ func (m *model) selectGenericPickerAtMouse(msg tea.MouseMsg) (mouseSelectionTarg | 513 | 513 | return mouseSelectionTarget{}, false | 514 | 514 | } | 515 | 515 | } | 516 | | - maxVisible := minInt(pickerOverlayMaxVisible, len(m.picker.items)) | | 516 | + maxVisible := pickerMaxVisible(m.picker, width) | 517 | 517 | selected := clampInt(m.picker.selected, 0, len(m.picker.items)-1) | 518 | 518 | start := selectableListStart(len(m.picker.items), maxVisible, selected) | 519 | 519 | visible := m.picker.items[start : start+maxVisible] | | 520 | + details := pickerShowsDetails(m.picker, width) | 520 | 521 | // y=0 titled top border, y=1 search line, y=2 separator; rows begin at y=3. | 521 | 522 | line := 3 | 522 | 523 | lastGroup := "" | @@ -528,12 +529,18 @@ func (m *model) selectGenericPickerAtMouse(msg tea.MouseMsg) (mouseSelectionTarg | 528 | 529 | line++ | 529 | 530 | lastGroup = item.Group | 530 | 531 | } | 531 | | - if hit.y == line { | | 532 | + // A rendered detail line belongs to its item: a click anywhere on the | | 533 | + // two-line block selects the same row. | | 534 | + rowLines := 1 | | 535 | + if details && item.Detail != "" { | | 536 | + rowLines = 2 | | 537 | + } | | 538 | + if hit.y >= line && hit.y < line+rowLines { | 532 | 539 | index := start + offset | 533 | 540 | m.picker.selected = index | 534 | 541 | return mouseSelectionTarget{Scope: "picker", Kind: int(m.picker.kind), Value: item.Value, Index: index}, true | 535 | 542 | } | 536 | | - line++ | | 543 | + line += rowLines | 537 | 544 | } | 538 | 545 | return mouseSelectionTarget{}, false | 539 | 546 | } | |
Collapse file Copy file name to clipboardExpand all lines: internal/tui/picker.go +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,12 +34,15 @@ const ( | 34 | 34 | // pickerItem is one selectable row: Label is shown, Value is passed to the | 35 | 35 | // underlying command handler when chosen. Meta is the optional right-aligned | 36 | 36 | // readout (ctx window · capabilities); the dot flags mark provider locality | 37 | | -// for model rows (accent = remote, blue = local). | | 37 | +// for model rows (accent = remote, blue = local). Detail is an optional faint | | 38 | +// second line under the row (project · model · size for sessions), rendered | | 39 | +// only on medium-and-wider tiers where the extra line reads cleanly. | 38 | 40 | type pickerItem struct { | 39 | 41 | Group string | 40 | 42 | Label string | 41 | 43 | Value string | 42 | 44 | Meta string | | 45 | + Detail string | 43 | 46 | Provider string // display tag (catalog id / locality) | 44 | 47 | // OwnerProvider is the saved provider profile name a model belongs to, so the | 45 | 48 | // /model picker can switch providers when a model from a non-active provider is | @@ -167,7 +170,7 @@ func (p *commandPicker) applyQuery() { | 167 | 170 | // the joined haystack, and a fuzzy subsequence is the last-resort match. | 168 | 171 | func scorePickerItem(item pickerItem, query string) (int, bool) { | 169 | 172 | label := strings.ToLower(item.Label) | 170 | | - hay := strings.ToLower(strings.Join([]string{item.Group, item.Label, item.Value, item.Meta}, " ")) | | 173 | + hay := strings.ToLower(strings.Join([]string{item.Group, item.Label, item.Value, item.Meta, item.Detail}, " ")) | 171 | 174 | switch { | 172 | 175 | case label == query: | 173 | 176 | return 0, true | |
Collapse file Copy file name to clipboardExpand all lines: internal/tui/rendering_lime_test.go +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | "strings" | 7 | 7 | "testing" | 8 | 8 | "time" | | 9 | + "unicode" | 9 | 10 |
| 10 | 11 | "charm.land/lipgloss/v2" | 11 | 12 | "github.com/charmbracelet/colorprofile" | @@ -1973,8 +1974,27 @@ func TestPermissionCollapseIsRunScoped(t *testing.T) { | 1973 | 1974 | } | 1974 | 1975 |
| 1975 | 1976 | func TestSessionsCardFieldsAreSanitized(t *testing.T) { | 1976 | | - if got := sanitizeCardField("evil\x1ftitle\nwith\x00bytes"); strings.ContainsAny(got, "\x1f\n\x00") { | 1977 | | - t.Fatalf("sanitizeCardField left separator bytes: %q", got) | | 1977 | + inputs := []string{ | | 1978 | + "evil\x1ftitle", | | 1979 | + "line\nbreak", | | 1980 | + "line\rbreak", | | 1981 | + "nul\x00byte", | | 1982 | + "esc\x1b[31mred", | | 1983 | + "csi\x1b[2J\x1b[H", | | 1984 | + "osc\x1b]0;pwned\a", | | 1985 | + "bell\aring", | | 1986 | + "back\bspace", | | 1987 | + "tab\there", | | 1988 | + "vert\vtab", | | 1989 | + "nel\u0085here", | | 1990 | + } | | 1991 | + for _, input := range inputs { | | 1992 | + got := sanitizeCardField(input) | | 1993 | + for _, r := range got { | | 1994 | + if unicode.IsControl(r) { | | 1995 | + t.Fatalf("sanitizeCardField(%q) left control rune %q in %q", input, r, got) | | 1996 | + } | | 1997 | + } | 1978 | 1998 | } | 1979 | 1999 | } | 1980 | 2000 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/tui/session.go +45-27Lines changed: 45 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | "path/filepath" | 9 | 9 | "runtime" | 10 | 10 | "strings" | 11 | | - "time" | 12 | 11 |
| 13 | 12 | "github.com/Gitlawb/zero/internal/agent" | 14 | 13 | "github.com/Gitlawb/zero/internal/execution" | @@ -360,28 +359,10 @@ func (m model) formatResumeSummary(session sessions.Metadata, eventCount int) st | 360 | 359 | }) | 361 | 360 | } | 362 | 361 |
| 363 | | -// sessionWhen formats a session's RFC3339 timestamp for the picker: a precise | 364 | | -// clock time (with seconds) for today so same-minute sessions stay distinct, the | 365 | | -// month/day and time earlier this year, else the date. Empty on a parse error. | 366 | | -func sessionWhen(timestamp string, now time.Time) string { | 367 | | - parsed, err := time.Parse(time.RFC3339, strings.TrimSpace(timestamp)) | 368 | | - if err != nil { | 369 | | - return "" | 370 | | - } | 371 | | - parsed, now = parsed.Local(), now.Local() | 372 | | - switch { | 373 | | - case parsed.Year() == now.Year() && parsed.YearDay() == now.YearDay(): | 374 | | - return parsed.Format("15:04:05") | 375 | | - case parsed.Year() == now.Year(): | 376 | | - return parsed.Format("Jan _2 15:04") | 377 | | - default: | 378 | | - return parsed.Format("2006-01-02") | 379 | | - } | 380 | | -} | 381 | | - | 382 | 362 | // newSessionPicker builds the interactive /resume picker (mirrors /model & /provider): | 383 | | -// one row per resumable session — title (Label) + id and relative age (Meta). Returns | 384 | | -// nil when there are no resumable sessions so the caller falls back to the text path. | | 363 | +// one row per resumable session — age + title (Label), session id (Value), and a | | 364 | +// project/model/size line (Detail). Returns nil when there are no resumable | | 365 | +// sessions so the caller falls back to the text path. | 385 | 366 | func (m model) newSessionPicker() *commandPicker { | 386 | 367 | if m.sessionStore == nil { | 387 | 368 | return nil | @@ -414,13 +395,17 @@ func (m model) newSessionPicker() *commandPicker { | 414 | 395 | // Lead with a fixed-width timestamp so titles form one scannable column. | 415 | 396 | // The raw id remains the selection/search value but stays out of the row: | 416 | 397 | // rendering it consumed half the picker and truncated the useful title. | 417 | | - label := displayValue(meta.Title, "untitled") | 418 | | - if when := sessionWhen(meta.UpdatedAt, now); when != "" { | | 398 | + // Session metadata is persisted, user-controlled text — a title from | | 399 | + // --session-title or /rename can legally contain newlines, which would | | 400 | + // break the picker's fixed row geometry. | | 401 | + label := displayValue(sanitizeCardField(meta.Title), "untitled") | | 402 | + if when := relativeAge(meta.UpdatedAt, now); when != "" { | 419 | 403 | label = sessionPickerLabel(when, label) | 420 | 404 | } | 421 | 405 | items = append(items, pickerItem{ | 422 | | - Label: label, | 423 | | - Value: meta.SessionID, | | 406 | + Label: label, | | 407 | + Value: meta.SessionID, | | 408 | + Detail: m.sessionPickerDetail(meta), | 424 | 409 | }) | 425 | 410 | } | 426 | 411 | if len(items) == 0 { | @@ -435,12 +420,45 @@ func (m model) newSessionPicker() *commandPicker { | 435 | 420 | } | 436 | 421 | } | 437 | 422 |
| 438 | | -const sessionPickerTimeWidth = len("Jan 02 15:04") | | 423 | +const sessionPickerTimeWidth = len("2006-01-02") | 439 | 424 |
| 440 | 425 | func sessionPickerLabel(when, title string) string { | 441 | 426 | return fmt.Sprintf("%-*s %s", sessionPickerTimeWidth, when, title) | 442 | 427 | } | 443 | 428 |
| | 429 | +// sessionPickerDetail composes the faint second line under a /resume row: | | 430 | +// the session's project directory (~/-contracted), the model it ran on, and a | | 431 | +// short status chip when the session is not a plain mainline run (forks, | | 432 | +// tagged sessions such as btw side-chats). Missing fields are simply omitted — | | 433 | +// the picker is workspace-scoped, so the project column is a confirmation, | | 434 | +// not a disambiguator. | | 435 | +func (m model) sessionPickerDetail(meta sessions.Metadata) string { | | 436 | + parts := make([]string, 0, 3) | | 437 | + if project := displayPath(m.cwd, meta.Cwd); project != "" { | | 438 | + parts = append(parts, project) | | 439 | + } | | 440 | + if modelID := strings.TrimSpace(meta.ModelID); modelID != "" { | | 441 | + parts = append(parts, modelID) | | 442 | + } | | 443 | + if status := sessionPickerStatus(meta); status != "" { | | 444 | + parts = append(parts, status) | | 445 | + } | | 446 | + return sanitizeCardField(strings.Join(parts, " · ")) | | 447 | +} | | 448 | + | | 449 | +// sessionPickerStatus is the concise status chip for a /resume row: an | | 450 | +// explicit tag when the session carries one, otherwise a marker for forked | | 451 | +// sessions. Plain mainline sessions get none — their shape is the default. | | 452 | +func sessionPickerStatus(meta sessions.Metadata) string { | | 453 | + if tag := strings.TrimSpace(meta.Tag); tag != "" { | | 454 | + return tag | | 455 | + } | | 456 | + if meta.SessionKind == sessions.SessionKindFork { | | 457 | + return "fork" | | 458 | + } | | 459 | + return "" | | 460 | +} | | 461 | + | 444 | 462 | // sessionHasResumableContent reports whether a session has anything worth | 445 | 463 | // resuming: a tool call/result, or a non-user message with real content (not the | 446 | 464 | // no-output guardrail stop). Empty/failed runs return false and are hidden from | |
|