From bf01fd549d34e2d59429eeae2d207b4f8893d267 Mon Sep 17 00:00:00 2001 From: yuez Date: Tue, 27 Oct 2026 18:43:05 +0800 Subject: [PATCH] fix(web): rebuild the session dot matrix from pi2.nvim The Chat session list dot did not follow the plugin it mirrors (:PiSessions in pi2.nvim, lua/pi/ui/sessions.lua). Four things were wrong: - priority: attention outranked error and exited; the plugin order is exited > error > attention > done > busy > compacting > idle. - interrupted wore the attention amber, the same colour as "waiting for you"; it is a dead process, so it takes the solid exited red (dot--dead). - exited and dormant were collapsed into the hollow grey ring; a dead parent is the solid red one, the hollow ring is the dormant sub-session. - sub-sessions reused the parent matrix. They now follow child_dot_hl: a failed child is steady red while you are looking at it and blinking red while you are not, busy/compacting blink at the busy rate, and a child never shows green (the plugin forces done=false there). The current-tab marker (PiSessionsListCurrent) was missing entirely: the row the console has open now carries dot--current, steady on idle and blinking on busy, and never on top of a notification colour - same guard as refresh_current_markers. It is the browser analogue of the focused nvim tab, so no wire change is needed. Also drops the unreachable sessionDotClass() dead code, whose second mapping was the reason the divergence went unnoticed, and consolidates the three subagent dot tables into subagentDot(). Unread notifications now expire like the plugin's: done/error are unread markers, not states (on_agent_end only raises done for a tab that is not current, clear_flags wipes both on TabEnter). The store gained markSeen(), called when the routed session carries done/error, so a session you open stops being green or red. The canonical reducer and fixtures/ are untouched: this is a client-side view concern. --- web/src/components/ui/status-dot.tsx | 55 ++------------ web/src/features/chat/session-state.ts | 100 ++++++++++++++++++++++--- web/src/features/chat/session-tree.tsx | 13 +++- web/src/pages/chat.tsx | 12 ++- web/src/stores/sessions.ts | 39 +++++++++- 5 files changed, 153 insertions(+), 66 deletions(-) diff --git a/web/src/components/ui/status-dot.tsx b/web/src/components/ui/status-dot.tsx index c843fc4..05b766c 100644 --- a/web/src/components/ui/status-dot.tsx +++ b/web/src/components/ui/status-dot.tsx @@ -1,7 +1,7 @@ import { cn } from "@/lib/utils"; /** Status-dot vocabulary from the design's `:PiSessions` state matrix. */ -export type DotStatus = "busy" | "compacting" | "idle" | "ok" | "err" | "attention" | "exited"; +export type DotStatus = "busy" | "compacting" | "idle" | "ok" | "err" | "attention" | "exited" | "dead"; export const DOT_CLASS: Record = { busy: "dot--busy", @@ -11,6 +11,7 @@ export const DOT_CLASS: Record = { err: "dot--err", attention: "dot--attention", exited: "dot--exited", + dead: "dot--dead", }; export type ServerStatus = "online" | "degraded" | "offline"; @@ -24,62 +25,22 @@ export function serverDotClass(status: ServerStatus | string | null | undefined) return DOT_CLASS.idle; } -export type SubagentStatus = "active" | "completed" | "failed" | "interrupted" | "dormant"; - -export function subagentDotClass(status: SubagentStatus | string): string { - switch (status) { - case "active": - return DOT_CLASS.busy; - case "completed": - return DOT_CLASS.ok; - case "failed": - return DOT_CLASS.err; - case "interrupted": - return DOT_CLASS.attention; - case "dormant": - return DOT_CLASS.exited; - default: - return DOT_CLASS.idle; - } -} - -export type SessionStatus = "starting" | "busy" | "compacting" | "idle" | "exited" | "interrupted" | "attention" | "done" | "error"; - -export function sessionDotClass(status: SessionStatus | string): string { - switch (status) { - case "busy": - case "starting": - return DOT_CLASS.busy; - case "compacting": - return DOT_CLASS.compacting; - case "interrupted": - case "attention": - return DOT_CLASS.attention; - case "done": - return DOT_CLASS.ok; - case "error": - return DOT_CLASS.err; - case "exited": - return DOT_CLASS.exited; - case "idle": - return DOT_CLASS.idle; - default: - return DOT_CLASS.idle; - } -} - export interface StatusDotProps { status: DotStatus; title?: string; className?: string; + /** Pin the dot solid (no blink) — nvim's "focused child error is steady red". */ + steady?: boolean; + /** "You are here" overlay (nvim `PiSessionsListCurrent`, agent blue). */ + current?: boolean; /** Decorative by default; pass a title to expose the state to assistive tech. */ "aria-hidden"?: boolean; } -export function StatusDot({ status, title, className, ...rest }: StatusDotProps) { +export function StatusDot({ status, title, className, steady, current, ...rest }: StatusDotProps) { return ( 0; } -/** `:PiSessions` status matrix → base.css dot class. */ -export function sessionDot(state: SessionState): DotStatus { - if (attentionPending(state)) return "attention"; - if (state.status === "busy" || state.status === "starting") return "busy"; - if (state.status === "compacting") return "compacting"; - if (state.status === "interrupted") return "attention"; - if (state.status === "exited") return "exited"; - if (state.flags.error) return "err"; - if (state.flags.done) return "ok"; - return "idle"; +/** + * A dot render decision: the base status plus the two overlay modifiers. + * `steady` pins the dot solid, `current` paints the "you are here" overlay. + */ +export interface DotView { + status: DotStatus; + steady?: boolean; + current?: boolean; +} + +/** + * The current-tab marker: `:PiSessions` paints the dot of the session its tab is + * showing in agent blue, steady while idle and blinking while busy, and never on + * top of a notification colour (`dot_hl` outranks it — see + * `refresh_current_markers`). The browser's analogue is the row the user has + * open. A focused `compacting` session blinks at the busy rate, not half speed. + */ +function withCurrent(view: DotView, state: SessionState, viewed: boolean): DotView { + if (!viewed) return view; + if (state.flags.error || state.flags.done || attentionPending(state)) return view; + if (state.status === "exited" || state.status === "interrupted") return view; + return { current: true, status: state.status === "compacting" ? "busy" : view.status }; +} + +/** + * `:PiSessions` parent row matrix (`dot_hl`, priority order): exited > error > + * attention > done > busy > compacting > idle. `interrupted` is the wire's + * "the process died badly" — it shares the exited red with `exited`, never the + * attention amber (`sessionStatusKey` still labels it "interrupted"). + */ +function parentDot(state: SessionState): DotView { + if (state.status === "exited") return { status: "dead" }; + if (state.flags.error) return { status: "err" }; + if (attentionPending(state)) return { status: "attention" }; + if (state.flags.done) return { status: "ok" }; + if (state.status === "busy") return { status: "busy" }; + if (state.status === "compacting") return { status: "compacting" }; + if (state.status === "interrupted") return { status: "dead" }; + return { status: "idle" }; +} + +/** `:PiSessions` → dot for a parent (depth 0) session row. */ +export function sessionDot(state: SessionState, opts: { viewed?: boolean } = {}): DotView { + return withCurrent(parentDot(state), state, opts.viewed === true); +} + +/** + * `:PiSessions` sub-session matrix (`child_dot_hl`) — deliberately not the + * parent's: an exited/dormant child is the hollow `◌` (never the exited red), a + * failed child is steady red while you are looking at it and blinking red while + * you are not, and `busy`/`compacting` share the busy amber at the busy rate. + * A child never shows `ok` or `attention`: the plugin forces `done = false` and + * `attention = 0` there, surfacing both in the parent chat instead. + */ +function childDot(state: SessionState, viewed: boolean): DotView { + if (state.status === "exited") return { status: "exited" }; + if (state.status === "interrupted") return { status: "dead" }; + if (state.flags.error) return { status: "err", steady: viewed }; + if (state.status === "busy" || state.status === "compacting") return { status: "busy" }; + return { status: "idle" }; +} + +/** `:PiSessions` → dot for a sub-session (depth > 0) row. */ +export function childSessionDot(state: SessionState, opts: { viewed?: boolean } = {}): DotView { + const viewed = opts.viewed === true; + return withCurrent(childDot(state, viewed), state, viewed); +} + +/** + * `:PiSessions` → dot for a manifest-only subagent row. A `completed` subagent is + * the hollow dot, not a green one: the plugin shows a finished child as dormant + * `◌` and keeps green for "a turn finished while you were elsewhere" on parents. + */ +export function subagentDot( + subagent: SessionState["subagents"][number], + opts: { viewed?: boolean } = {}, +): DotView { + switch (subagent.status) { + case "active": + return { status: "busy" }; + case "completed": + case "dormant": + return { status: "exited" }; + case "failed": + return { status: "err", steady: opts.viewed === true }; + case "interrupted": + return { status: "dead" }; + default: + return { status: "idle" }; + } } /** i18n key under `state.*` for the current session status. */ diff --git a/web/src/features/chat/session-tree.tsx b/web/src/features/chat/session-tree.tsx index 2ba0f8c..959d83f 100644 --- a/web/src/features/chat/session-tree.tsx +++ b/web/src/features/chat/session-tree.tsx @@ -4,10 +4,10 @@ import { useTranslation } from "react-i18next"; import { Icon } from "@/components/icons"; import { Empty } from "@/components/ui/empty"; -import { StatusDot, subagentDotClass } from "@/components/ui/status-dot"; +import { StatusDot } from "@/components/ui/status-dot"; import { cn } from "@/lib/utils"; import { SessionRowMenu } from "./session-row-menu"; -import { flattenSubagents, sessionDot } from "./session-state"; +import { childSessionDot, flattenSubagents, sessionDot, subagentDot } from "./session-state"; import { sessionTitle, subagentTitle } from "./session-title"; import { SessionTitleSpinner } from "./session-title-spinner"; import { @@ -178,6 +178,10 @@ export function SessionTree({ function renderSessionRow(node: SessionTreeNode) { const session = node.session; const selected = session.sessionId === selectedId; + const dot = + node.depth > 0 + ? childSessionDot(session, { viewed: selected }) + : sessionDot(session, { viewed: selected }); const menuOpen = rowMenu?.session.sessionId === session.sessionId; // A row is never labelled with its id (pi2.nvim shows `(unnamed)`). const title = sessionTitle(session, titlePhrases); @@ -209,7 +213,7 @@ export function SessionTree({ aria-current={selected ? "true" : undefined} onClick={() => onSelectSession(session.sessionId)} > - + {title.spinner ? : null} onSelectSubagent(session.sessionId, subagent.id)} > -