diff --git a/cmd/agentop/README.md b/cmd/agentop/README.md index f09aca3ec..59f7126e3 100644 --- a/cmd/agentop/README.md +++ b/cmd/agentop/README.md @@ -40,11 +40,13 @@ install updates itself. The title bar shows one of these notices. When more than one applies, it shows the first: -1. `· updated to vX.Y.Z, restart agentop` -2. `· update to vX.Y.Z failed, see agentop doctor` -3. `· vX.Y.Z available`: only when vX.Y.Z is newer than the installed release or +1. `· vX.Y.Z installed, quit and reopen agentop`, in green. The proxy already runs + vX.Y.Z. Only the open agentop still runs the old binary, so you do not need + `agentop service restart`. +2. `· update to vX.Y.Z failed, see agentop doctor`, in red. +3. `· vX.Y.Z available`, in green: only when vX.Y.Z is newer than the installed release or release candidate. A `dev` or `main` build shows no notice. -4. `· no update check since `: service installs only, after 72 hours with no check. +4. `· no update check since `, in amber: service installs only, after 72 hours with no check. ## Run diff --git a/cmd/agentop/tui/app.go b/cmd/agentop/tui/app.go index 3bf16b538..e0a126b0c 100644 --- a/cmd/agentop/tui/app.go +++ b/cmd/agentop/tui/app.go @@ -485,6 +485,8 @@ type model struct { serviceInstall bool // updateNote is the update notice after the build on the title bar. updateNote string + // updateInk is the color of updateNote. + updateInk lipgloss.Style // lastHarvest is when the most recent harvest was STARTED. Read by TWO callers: the sessions // pane's backoff gate, which asks whether untitledBackoff has elapsed since then, and the // timeline pane's gate for unlabeled subagents (timelineLabelHarvestEvery). The picker @@ -2612,7 +2614,7 @@ func (m *model) paneView() string { // yet there is no box to draw, and the body shows as it is. func (m *model) pickerView(title, body, footer string) string { if m.width == 0 { - return lipgloss.JoinVertical(lipgloss.Left, renderTitle(title), body, footer) + return lipgloss.JoinVertical(lipgloss.Left, m.titleView(title), body, footer) } frameH := m.bodyHeight if m.searching && m.searchPane != targetHelp { @@ -2621,7 +2623,7 @@ func (m *model) pickerView(title, body, footer string) string { if m.bodyHeight == 0 { frameH = lipgloss.Height(body) // layout() has not run, so there is no reservation to fill } - rows := []string{renderTitle(title), boxTop("", m.width)} + rows := []string{m.titleView(title), boxTop("", m.width)} rows = append(rows, boxFrame(body, m.width, frameH)...) rows = append(rows, boxBottom(m.width), footer) return lipgloss.JoinVertical(lipgloss.Left, rows...) @@ -2758,7 +2760,7 @@ func (m *model) paneContent() (title, body string) { // framePane is a data pane's screen: the title, the spend band and its drawer, the body in the // box at exactly the rows layout() reserved, and the footer on the last row. func (m *model) framePane(title, body string) string { - header := renderTitle(title) + header := m.titleView(title) // The band sits directly under the title because that is the whole requirement: spend // read BEFORE the data rather than navigated to. // diff --git a/cmd/agentop/tui/styles.go b/cmd/agentop/tui/styles.go index d5acd72a7..481dfc53b 100644 --- a/cmd/agentop/tui/styles.go +++ b/cmd/agentop/tui/styles.go @@ -283,7 +283,11 @@ var titleMarkRe = regexp.MustCompile(` · |https?://\S+|\([^()]*\)`) // renderTitle styles a pane title without changing its text: a leading "agentop" in // accent bold, " · " dim, URLs and (…) groups muted, the rest bold. -func renderTitle(title string) string { +func renderTitle(title string) string { return renderTitleInk(title, "", styleNone) } + +// renderTitleInk is renderTitle with the first copy of mark in bold ink. An empty +// mark, or one that is not in the title, leaves the title as renderTitle styles it. +func renderTitleInk(title, mark string, ink lipgloss.Style) string { if !colorOn() { return styleTitle.Render(title) } @@ -293,12 +297,23 @@ func renderTitle(title string) string { b.WriteString(styleAccentBold.Render("agentop")) rest = r } + if i := strings.Index(rest, mark); mark != "" && i >= 0 { + writeTitleSegments(&b, rest[:i]) + b.WriteString(ink.Bold(true).Render(mark)) + rest = rest[i+len(mark):] + } + writeTitleSegments(&b, rest) + return b.String() +} + +// writeTitleSegments writes s with " · " dim, URLs and (…) groups muted, the rest bold. +func writeTitleSegments(b *strings.Builder, s string) { last := 0 - for _, loc := range titleMarkRe.FindAllStringIndex(rest, -1) { + for _, loc := range titleMarkRe.FindAllStringIndex(s, -1) { if loc[0] > last { - b.WriteString(styleBold.Render(rest[last:loc[0]])) + b.WriteString(styleBold.Render(s[last:loc[0]])) } - m := rest[loc[0]:loc[1]] + m := s[loc[0]:loc[1]] if m == " · " { b.WriteString(styleDim.Render(m)) } else { @@ -306,10 +321,9 @@ func renderTitle(title string) string { } last = loc[1] } - if last < len(rest) { - b.WriteString(styleBold.Render(rest[last:])) + if last < len(s) { + b.WriteString(styleBold.Render(s[last:])) } - return b.String() } // colorGroup is the meaning a cell's value carries, which picks its ink. diff --git a/cmd/agentop/tui/update_notice.go b/cmd/agentop/tui/update_notice.go index 6092519be..2b7d16ab7 100644 --- a/cmd/agentop/tui/update_notice.go +++ b/cmd/agentop/tui/update_notice.go @@ -1,29 +1,34 @@ package tui import ( + "strings" "time" + "github.com/charmbracelet/lipgloss" + "github.com/rossoctl/cortex/cmd/agentop/updatestatus" ) -// updateSuffix is the update notice after the build on the title bar, or "". +// updateSuffix is the update notice after the build on the title bar, or "", and +// its ink: green for news, red for a failure, amber for a missed check. // The first rule that matches wins. -func updateSuffix(st updatestatus.Status, version string, serviceInstall bool, now time.Time) string { +func updateSuffix(st updatestatus.Status, version string, serviceInstall bool, now time.Time) (string, lipgloss.Style) { switch { case st.Installed != version && updatestatus.IsRelease(st.Installed) && (updatestatus.IsRelease(version) || updatestatus.IsRC(version)): - return "· updated to " + st.Installed + ", restart agentop" + // Not "restart agentop": that reads as agentop service restart, which this does not need. + return "· " + st.Installed + " installed, quit and reopen agentop", styleOK case st.Note != "": - return "· " + st.Note + return "· " + st.Note, styleOK case st.Result == updatestatus.ResultFailed: - return "· update to " + st.Latest + " failed, see agentop doctor" + return "· update to " + st.Latest + " failed, see agentop doctor", styleError // Only a newer release: a dev, main or RC build is often ahead of Latest. case updatestatus.Newer(st.Latest, st.Installed): - return "· " + st.Latest + " available" + return "· " + st.Latest + " available", styleOK case serviceInstall && now.Sub(st.CheckedAt) > 72*time.Hour: - return "· no update check since " + st.CheckedAt.Format("2006-01-02") + return "· no update check since " + st.CheckedAt.Format("2006-01-02"), styleWarn } - return "" + return "", styleNone } // readUpdateNotice sets m.updateNote from m.updateFile. A missing or bad file @@ -37,5 +42,10 @@ func (m *model) readUpdateNotice() { if err != nil { return } - m.updateNote = updateSuffix(st, m.version, m.serviceInstall, time.Now()) + m.updateNote, m.updateInk = updateSuffix(st, m.version, m.serviceInstall, time.Now()) +} + +// titleView is renderTitle with the update notice in its own ink, so it stands out. +func (m *model) titleView(title string) string { + return renderTitleInk(title, strings.TrimPrefix(m.updateNote, "· "), m.updateInk) } diff --git a/cmd/agentop/tui/update_notice_test.go b/cmd/agentop/tui/update_notice_test.go index 9ed9b88fd..7d40ece5d 100644 --- a/cmd/agentop/tui/update_notice_test.go +++ b/cmd/agentop/tui/update_notice_test.go @@ -3,9 +3,14 @@ package tui import ( "os" "path/filepath" + "strings" "testing" "time" + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" + "github.com/muesli/termenv" + "github.com/rossoctl/cortex/cmd/agentop/updatestatus" ) @@ -20,7 +25,7 @@ func TestUpdateSuffix(t *testing.T) { want string }{ {"updated", updatestatus.Status{Installed: "v0.9.6", Latest: "v0.9.6", Result: updatestatus.ResultUpdated, CheckedAt: noticeNow}, - "v0.9.5", true, "· updated to v0.9.6, restart agentop"}, + "v0.9.5", true, "· v0.9.6 installed, quit and reopen agentop"}, {"failed", updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.6", Result: updatestatus.ResultFailed, CheckedAt: noticeNow}, "v0.9.5", true, "· update to v0.9.6 failed, see agentop doctor"}, // A cpex replacement keeps the version, so the only sign is the note. @@ -40,12 +45,12 @@ func TestUpdateSuffix(t *testing.T) { {"rc release out", updatestatus.Status{Installed: "v0.10.0-rc.1", Latest: "v0.10.0", Result: updatestatus.ResultWaiting, CheckedAt: noticeNow}, "v0.10.0-rc.1", true, "· v0.10.0 available"}, {"rc updated", updatestatus.Status{Installed: "v0.10.0", Latest: "v0.10.0", Result: updatestatus.ResultUpdated, CheckedAt: noticeNow}, - "v0.10.0-rc.1", true, "· updated to v0.10.0, restart agentop"}, + "v0.10.0-rc.1", true, "· v0.10.0 installed, quit and reopen agentop"}, // Latest moved back: the update installs it, with no "available" before. {"withdrawn", updatestatus.Status{Installed: "v0.10.0", Latest: "v0.9.5", Result: updatestatus.ResultWaiting, CheckedAt: noticeNow}, "v0.10.0", true, ""}, } { - if got := updateSuffix(tc.st, tc.version, tc.service, noticeNow); got != tc.want { + if got, _ := updateSuffix(tc.st, tc.version, tc.service, noticeNow); got != tc.want { t.Errorf("%s: updateSuffix = %q, want %q", tc.name, got, tc.want) } } @@ -53,21 +58,21 @@ func TestUpdateSuffix(t *testing.T) { func TestUpdateSuffixIgnoresEmptyLatest(t *testing.T) { st := updatestatus.Status{Installed: "v0.9.5", Result: updatestatus.ResultUpdated, CheckedAt: noticeNow} - if got := updateSuffix(st, "v0.9.5", true, noticeNow); got != "" { + if got, _ := updateSuffix(st, "v0.9.5", true, noticeNow); got != "" { t.Errorf("updateSuffix with empty latest = %q, want empty", got) } } func TestUpdateSuffixDevBuild(t *testing.T) { st := updatestatus.Status{Installed: "v0.9.6", Latest: "v0.9.6", Result: updatestatus.ResultCurrent, CheckedAt: noticeNow} - if got := updateSuffix(st, "dev-abc", true, noticeNow); got != "" { + if got, _ := updateSuffix(st, "dev-abc", true, noticeNow); got != "" { t.Errorf("updateSuffix on a dev build = %q, want empty", got) } } func TestUpdateSuffixStaleNeedsService(t *testing.T) { st := updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.5", Result: updatestatus.ResultCurrent, CheckedAt: noticeNow.Add(-73 * time.Hour)} - if got := updateSuffix(st, "v0.9.5", false, noticeNow); got != "" { + if got, _ := updateSuffix(st, "v0.9.5", false, noticeNow); got != "" { t.Errorf("updateSuffix with no service = %q, want empty", got) } } @@ -103,3 +108,51 @@ func TestUpdateNoticeRereadOnRefreshTick(t *testing.T) { t.Errorf("updateNote after a tick = %q, want %q", got, want) } } + +// Green for news, red for a failure, amber for a missed check. +func TestUpdateSuffixInk(t *testing.T) { + // With no color, every style renders "x" alike and the test cannot fail. + withProfile(t, termenv.TrueColor) + for _, tc := range []struct { + name string + st updatestatus.Status + version string + want lipgloss.Style + }{ + {"installed", updatestatus.Status{Installed: "v0.9.6", Latest: "v0.9.6", Result: updatestatus.ResultUpdated, CheckedAt: noticeNow}, "v0.9.5", styleOK}, + {"note", updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.5", Result: updatestatus.ResultUpdated, Note: "n", CheckedAt: noticeNow}, "v0.9.5", styleOK}, + {"failed", updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.6", Result: updatestatus.ResultFailed, CheckedAt: noticeNow}, "v0.9.5", styleError}, + {"available", updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.6", Result: updatestatus.ResultWaiting, CheckedAt: noticeNow}, "v0.9.5", styleOK}, + {"stale", updatestatus.Status{Installed: "v0.9.5", Latest: "v0.9.5", Result: updatestatus.ResultCurrent, CheckedAt: noticeNow.Add(-73 * time.Hour)}, "v0.9.5", styleWarn}, + } { + _, ink := updateSuffix(tc.st, tc.version, true, noticeNow) + if got, want := ink.Render("x"), tc.want.Render("x"); got != want { + t.Errorf("%s: ink renders %q, want %q", tc.name, got, want) + } + } +} + +// The title keeps its text, and only the notice takes the notice's ink. +func TestTitleViewInksTheNotice(t *testing.T) { + path := filepath.Join(t.TempDir(), updatestatus.FileName) + writeNoticeFile(t, path, "v0.9.6") + m := &model{version: "v0.9.5", updateFile: path} + m.readUpdateNotice() + withProfile(t, termenv.TrueColor) + in := m.appName() + " · http://127.0.0.1:47601" + got := m.titleView(in) + if s := ansi.Strip(got); s != in { + t.Fatalf("stripped = %q, want %q", s, in) + } + if want := styleOK.Bold(true).Render("v0.9.6 available"); !strings.Contains(got, want) { + t.Errorf("notice not green bold in %q", got) + } + if !strings.Contains(got, styleMuted.Render("http://127.0.0.1:47601")) { + t.Errorf("URL lost its style in %q", got) + } + // With no notice, titleView is renderTitle. + m.updateNote, m.updateInk = "", styleNone + if got, want := m.titleView(in), renderTitle(in); got != want { + t.Errorf("titleView with no notice = %q, want %q", got, want) + } +}