diff --git a/CHANGELOG.md b/CHANGELOG.md index 38f3830..a91fb89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **Session status icons in the sidebar** — each session in the host tree (and the phone home + list) shows its state in the gap left of its name, under the host icon: a turning arc while it + works or starts, a ring while idle, an alert in amber when it needs approval and in red on an + error, and a faint dashed ring when it is offline or stopped. The arc holds still for people who + ask for reduced motion. + +### Fixed + +- **A resume no longer resets a session's age** — `updated_at`, the "last activity" age in the + sidebar and in `fleet_status`, moves only on the session's own activity (turns, items, messages, + approvals, errors). The registration, status, policy and queue events a node sends when it + restarts, resumes or reconnects a session leave it alone, so a resumed session that has said + nothing new keeps the age of its last response. + ## [0.1.7] — 2026-10-28 ### Added diff --git a/internal/coord/server.go b/internal/coord/server.go index a4f2bc6..3816ef7 100644 --- a/internal/coord/server.go +++ b/internal/coord/server.go @@ -685,6 +685,20 @@ func (s *Server) route(ev model.Event) { } } +// touches reports whether an event is the session doing something, which +// moves its updated_at (the age the UI and fleet_status show). Bookkeeping a +// node sends when it starts, resumes or reconnects a session (registration, +// status, policy, queue counts) does not: a resumed session that has said +// nothing new keeps the age of its last response. +func touches(kind string) bool { + switch kind { + case model.KindSessionRegistered, model.KindSessionStatus, model.KindSessionPolicy, + model.KindSessionQueue, model.KindSessionSettled, model.KindUsage: + return false + } + return true +} + // project updates the session record from one event, and the fleet's // daily token total from usage events. An event at or below the record's // ProjectedSeq is already in it. @@ -720,7 +734,9 @@ func (s *Server) project(ev model.Event) { } before, applied = *sess, true sess.ProjectedSeq = ev.Seq - sess.UpdatedAt = ev.TS + if touches(ev.Kind) { + sess.UpdatedAt = ev.TS + } switch ev.Kind { case model.KindSessionRegistered: var p model.SessionRegistered diff --git a/internal/coord/touches_test.go b/internal/coord/touches_test.go new file mode 100644 index 0000000..d1d106c --- /dev/null +++ b/internal/coord/touches_test.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package coord + +import ( + "testing" + "time" + + "github.com/jm2/switchworks/internal/model" +) + +// A resume (registration, status and queue events with nothing new from +// the agent) keeps the session's age; its next turn moves it. +func TestResumeKeepsUpdatedAt(t *testing.T) { + s := testServer(t, Guards{}) + old := time.Now().Add(-3 * time.Hour).Truncate(time.Second) + sess := addSession(t, s, "devbox", "sw", func(x *model.Session) { x.UpdatedAt = old }) + now := time.Now() + s.project(model.Event{SessionID: sess.ID, Seq: 1, Kind: model.KindSessionRegistered, TS: now, + Payload: model.Marshal(model.SessionRegistered{Model: "m"})}) + s.project(model.Event{SessionID: sess.ID, Seq: 2, Kind: model.KindSessionStatus, TS: now, + Payload: model.Marshal(model.SessionStatus{Status: model.StatusIdle})}) + s.project(model.Event{SessionID: sess.ID, Seq: 3, Kind: model.KindSessionQueue, TS: now, + Payload: model.Marshal(model.SessionQueue{Queued: 0})}) + cur, _ := s.store.Session(sess.ID) + if !cur.UpdatedAt.Equal(old) { + t.Fatalf("a resume moved updated_at from %v to %v", old, cur.UpdatedAt) + } + if cur.Model != "m" || cur.ProjectedSeq != 3 { + t.Fatalf("the resume events were not projected: model %q seq %d", cur.Model, cur.ProjectedSeq) + } + s.project(model.Event{SessionID: sess.ID, Seq: 4, Kind: model.KindTurnStarted, TS: now}) + cur, _ = s.store.Session(sess.ID) + if !cur.UpdatedAt.Equal(now) { + t.Fatalf("a turn did not move updated_at: %v", cur.UpdatedAt) + } +} diff --git a/internal/web/assets/app.css b/internal/web/assets/app.css index 2b23a95..656870a 100644 --- a/internal/web/assets/app.css +++ b/internal/web/assets/app.css @@ -139,7 +139,7 @@ body.resizing { cursor: col-resize; user-select: none; } .host-row.off { color: var(--faint); } .host-row .icon-btn { width: 30px; height: 30px; } .sess-row { - display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 39px; border-radius: 10px; color: var(--ink); + position: relative; display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 39px; border-radius: 10px; color: var(--ink); min-height: 34px; } .sess-row:hover { background: var(--hover); text-decoration: none; } @@ -149,6 +149,16 @@ body.resizing { cursor: col-resize; user-select: none; } .sess-row .queued { min-width: 20px; padding: 0 6px; border-radius: 10px; border: 1px solid var(--line); color: var(--muted); font-size: 12px; line-height: 18px; text-align: center; font-variant-numeric: tabular-nums; } .sess-row .queued.many { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); } .sess-row.stopped .name { color: var(--faint); } +/* session status, in the gap under the host icon */ +.sess-state { position: absolute; left: 14px; top: 50%; width: 14px; height: 14px; margin-top: -7px; display: flex; color: var(--faint); } +.sess-state svg.i { width: 14px; height: 14px; stroke-width: 2; } +.sess-state.active, .sess-state.starting { color: var(--accent); } +.sess-state.active svg.i, .sess-state.starting svg.i { animation: spin 1.1s linear infinite; } +.sess-state.waiting_approval { color: var(--warn); } +.sess-state.error { color: var(--bad); } +.sess-state.stopped, .sess-state.gone, .sess-state.offline { opacity: .6; } +.sess-state.stopped svg.i, .sess-state.gone svg.i, .sess-state.offline svg.i { stroke-dasharray: 2.5 3; } +@keyframes spin { to { transform: rotate(360deg); } } .none { padding: 4px 12px 8px 39px; color: var(--faint); font-size: 14px; } /* status mark: a quiet dot, coloured only when it needs attention or is working */ .mark { width: 7px; height: 7px; border-radius: 50%; flex: none; background: transparent; } @@ -221,7 +231,7 @@ h2 { font-size: 15px; font-weight: 500; color: var(--muted); margin: 26px 0 10px .attention .mark { width: 8px; height: 8px; } .home .host-row { padding-left: 4px; font-size: 16px; } .home .sess-row { padding-left: 33px; align-items: flex-start; flex-wrap: wrap; row-gap: 0; } -.home .sess-row .mark { margin-top: 9px; } +.home .sess-row .sess-state { left: 6px; top: 17px; } .home .sess-row .age { margin-top: 1px; } .home .sess-row .preview { flex-basis: 100%; padding-left: 15px; color: var(--muted); font-size: 14px; @@ -524,4 +534,5 @@ body.login { display: grid; place-items: center; min-height: 100vh; padding: 16p @media (prefers-reduced-motion: reduce) { .side { transition: none; } .flash { animation: none; } + .sess-state svg.i { animation: none !important; } } diff --git a/internal/web/assets/app.js b/internal/web/assets/app.js index 59e6429..28fda87 100644 --- a/internal/web/assets/app.js +++ b/internal/web/assets/app.js @@ -373,6 +373,8 @@ const ICONS = { auto: 'M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18zM12 3v18', reply: 'M10 8L5 12l5 4M5 12h9a5 5 0 0 1 5 5v1', image: 'M4 5h16v14H4zM4 16l5-5 4 4 3-3 4 4M15.5 9.5h.01', + ring: 'M12 5a7 7 0 1 0 0 14 7 7 0 0 0 0-14z', + spin: 'M12 5a7 7 0 1 1-7 7', }; function icon(name, cls) { @@ -386,6 +388,15 @@ function icon(name, cls) { return svg; } +// A session row's status, drawn in the gap left of its name: a turning arc +// while it works, a ring while idle, an alert when it needs someone. +const stateIcon = {active: 'spin', starting: 'spin', waiting_approval: 'alert', error: 'alert'}; +function sessState(s) { + const label = statusLabel[s.status] || s.status; + return h('span', {class: 'sess-state ' + s.status, role: 'img', 'aria-label': label, title: label}, + icon(stateIcon[s.status] || 'ring')); +} + function iconBtn(name, label, onclick, attrs) { return h('button', Object.assign({type: 'button', class: 'icon-btn', 'aria-label': label, title: label, onclick}, attrs || {}), icon(name)); } @@ -459,7 +470,7 @@ function renderSide() { icon('host'), h('span', {class: 'name', text: host.id, title: host.id}), host.connected ? iconLink('plus', `New session on ${host.id}`, spawnHref(host.id)) : null), list.length ? list.map((s) => h('a', {class: 'sess-row' + (s.id === current.session ? ' on' : ''), href: '#/s/' + s.id}, - h('span', {class: 'mark ' + s.status, title: statusLabel[s.status] || s.status}), + sessState(s), h('span', {class: 'name', text: s.name, title: s.name}), s.queued ? h('span', {class: 'queued' + (s.queued >= 20 ? ' many' : ''), title: `${s.queued} queued for the next turn`, text: s.queued}) : null, h('span', {class: 'age', text: ago(s.updated_at)}))) @@ -659,7 +670,7 @@ function fleetView() { : `offline, last seen ${ago(host.last_seen)} ago`}), host.connected ? iconLink('plus', `New session on ${host.id}`, spawnHref(host.id)) : null), list.length ? list.map((s) => h('a', {class: 'sess-row' + (liveSession(s) ? '' : ' stopped'), href: '#/s/' + s.id}, - h('span', {class: 'mark ' + s.status, title: statusLabel[s.status] || s.status}), + sessState(s), h('span', {class: 'name', text: s.name, title: s.name}), s.queued ? h('span', {class: 'queued' + (s.queued >= 20 ? ' many' : ''), title: `${s.queued} queued for the next turn`, text: s.queued}) : null, h('span', {class: 'age', text: ago(s.updated_at)}),