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.
This commit is contained in:
yuez committed 2026-10-27 18:43:13 +08:00
1 parent 81e7f96a64
commit bf01fd549d
5 files changed
+153 -66

No files matched your search

+8 -47
View File
@@ -1,7 +1,7 @@
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
/** Status-dot vocabulary from the design's `:PiSessions` state matrix. */ /** 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<DotStatus, string> = { export const DOT_CLASS: Record<DotStatus, string> = {
busy: "dot--busy", busy: "dot--busy",
@@ -11,6 +11,7 @@ export const DOT_CLASS: Record<DotStatus, string> = {
err: "dot--err", err: "dot--err",
attention: "dot--attention", attention: "dot--attention",
exited: "dot--exited", exited: "dot--exited",
dead: "dot--dead",
}; };
export type ServerStatus = "online" | "degraded" | "offline"; export type ServerStatus = "online" | "degraded" | "offline";
@@ -24,62 +25,22 @@ export function serverDotClass(status: ServerStatus | string | null | undefined)
return DOT_CLASS.idle; 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 { export interface StatusDotProps {
status: DotStatus; status: DotStatus;
title?: string; title?: string;
className?: 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. */ /** Decorative by default; pass a title to expose the state to assistive tech. */
"aria-hidden"?: boolean; "aria-hidden"?: boolean;
} }
export function StatusDot({ status, title, className, ...rest }: StatusDotProps) { export function StatusDot({ status, title, className, steady, current, ...rest }: StatusDotProps) {
return ( return (
<span <span
className={cn("dot", DOT_CLASS[status], className)} className={cn("dot", DOT_CLASS[status], steady && "dot--steady", current && "dot--current", className)}
title={title} title={title}
aria-label={title} aria-label={title}
aria-hidden={title ? undefined : true} aria-hidden={title ? undefined : true}
+90 -10
View File
@@ -446,16 +446,96 @@ function attentionPending(state: SessionState): boolean {
return state.flags.attention || state.attention.length > 0; return state.flags.attention || state.attention.length > 0;
} }
/** `:PiSessions` status matrix → base.css dot class. */ /**
export function sessionDot(state: SessionState): DotStatus { * A dot render decision: the base status plus the two overlay modifiers.
if (attentionPending(state)) return "attention"; * `steady` pins the dot solid, `current` paints the "you are here" overlay.
if (state.status === "busy" || state.status === "starting") return "busy"; */
if (state.status === "compacting") return "compacting"; export interface DotView {
if (state.status === "interrupted") return "attention"; status: DotStatus;
if (state.status === "exited") return "exited"; steady?: boolean;
if (state.flags.error) return "err"; current?: boolean;
if (state.flags.done) return "ok"; }
return "idle";
/**
* 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. */ /** i18n key under `state.*` for the current session status. */
+9 -4
View File
@@ -4,10 +4,10 @@ import { useTranslation } from "react-i18next";
import { Icon } from "@/components/icons"; import { Icon } from "@/components/icons";
import { Empty } from "@/components/ui/empty"; 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 { cn } from "@/lib/utils";
import { SessionRowMenu } from "./session-row-menu"; 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 { sessionTitle, subagentTitle } from "./session-title";
import { SessionTitleSpinner } from "./session-title-spinner"; import { SessionTitleSpinner } from "./session-title-spinner";
import { import {
@@ -178,6 +178,10 @@ export function SessionTree({
function renderSessionRow(node: SessionTreeNode) { function renderSessionRow(node: SessionTreeNode) {
const session = node.session; const session = node.session;
const selected = session.sessionId === selectedId; 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; const menuOpen = rowMenu?.session.sessionId === session.sessionId;
// A row is never labelled with its id (pi2.nvim shows `(unnamed)`). // A row is never labelled with its id (pi2.nvim shows `(unnamed)`).
const title = sessionTitle(session, titlePhrases); const title = sessionTitle(session, titlePhrases);
@@ -209,7 +213,7 @@ export function SessionTree({
aria-current={selected ? "true" : undefined} aria-current={selected ? "true" : undefined}
onClick={() => onSelectSession(session.sessionId)} onClick={() => onSelectSession(session.sessionId)}
> >
<StatusDot status={sessionDot(session)} /> <StatusDot status={dot.status} steady={dot.steady} current={dot.current} />
{title.spinner ? <SessionTitleSpinner /> : null} {title.spinner ? <SessionTitleSpinner /> : null}
<span <span
className={cn( className={cn(
@@ -244,6 +248,7 @@ export function SessionTree({
function renderSubagentRow(node: SubagentTreeNode) { function renderSubagentRow(node: SubagentTreeNode) {
const { session, subagent } = node; const { session, subagent } = node;
const viewing = subagent.id === activeSubagentId; const viewing = subagent.id === activeSubagentId;
const dot = subagentDot(subagent, { viewed: viewing });
// Sub-session rows resolve name → task prompt (40 chars) → `sub-session`; // Sub-session rows resolve name → task prompt (40 chars) → `sub-session`;
// the id stays an identity, never a label. // the id stays an identity, never a label.
const title = subagentTitle(subagent, titlePhrases); const title = subagentTitle(subagent, titlePhrases);
@@ -275,7 +280,7 @@ export function SessionTree({
data-sub-id={subagent.id} data-sub-id={subagent.id}
onClick={() => onSelectSubagent(session.sessionId, subagent.id)} onClick={() => onSelectSubagent(session.sessionId, subagent.id)}
> >
<span className={cn("dot", subagentDotClass(subagent.status))} aria-hidden="true" /> <StatusDot status={dot.status} steady={dot.steady} />
<span <span
className={cn( className={cn(
"t-name truncate", "t-name truncate",
+11 -1
View File
@@ -116,6 +116,16 @@ function ChatExperience({ sessionId }: { sessionId?: string }) {
sessions, sessions,
sessionsQuery.loaded, sessionsQuery.loaded,
); );
// `:PiSessions` TabEnter: the routed session is in view, so its unread
// notifications are consumed — the plugin clears `done`/`error` when the user
// enters the tab and only raises `done` for a session that is not current.
const viewedSessionId = active?.sessionId;
const viewedDone = active?.flags.done;
const viewedError = active?.flags.error;
useEffect(() => {
if (!viewedSessionId || (!viewedDone && !viewedError)) return;
useSessionsStore.getState().markSeen(viewedSessionId);
}, [viewedSessionId, viewedDone, viewedError]);
const history = useSessionHistory( const history = useSessionHistory(
active?.sessionId, active?.sessionId,
active?.entries ?? NO_ENTRIES, active?.entries ?? NO_ENTRIES,
@@ -535,7 +545,7 @@ function ChatExperience({ sessionId }: { sessionId?: string }) {
) : null} ) : null}
<div className="stream-title"> <div className="stream-title">
{active ? ( {active ? (
<StatusDot status={sessionDot(active)} title={t(`state.${sessionStatusKey(active)}`)} /> <StatusDot status={sessionDot(active).status} title={t(`state.${sessionStatusKey(active)}`)} />
) : null} ) : null}
<h2 className="stream-name truncate" tabIndex={-1} ref={streamNameRef}> <h2 className="stream-name truncate" tabIndex={-1} ref={streamNameRef}>
{active ? activeTitle?.text : (workspace?.name ?? t("nav.chat"))} {active ? activeTitle?.text : (workspace?.name ?? t("nav.chat"))}
+35 -4
View File
@@ -15,6 +15,14 @@ export interface SessionsStore {
renameSessionId: (from: string, to: string) => void; renameSessionId: (from: string, to: string) => void;
removeSession: (sessionId: string) => void; removeSession: (sessionId: string) => void;
selectSession: (sessionId: string | null) => void; selectSession: (sessionId: string | null) => void;
/**
* `:PiSessions` TabEnter: the user is now looking at this session, so its
* unread notifications (`done` / `error` / a stale `attention` flag) are
* consumed. `:PiSessions` clears both on TabEnter and only sets `done` when
* the session is not the current tab; the browser has no tabs, so owning a
* row's notifications means opening it.
*/
markSeen: (sessionId: string) => void;
listFor: (workspaceId: string) => SessionState[]; listFor: (workspaceId: string) => SessionState[];
getSession: (sessionId: string) => SessionState | undefined; getSession: (sessionId: string) => SessionState | undefined;
setStatus: (status: "idle" | "loading" | "error", error?: string | null) => void; setStatus: (status: "idle" | "loading" | "error", error?: string | null) => void;
@@ -46,16 +54,19 @@ export const useSessionsStore = create<SessionsStore>((set, get) => ({
return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: [...list, session] } }; return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: [...list, session] } };
} }
// Tree metadata is list metadata: a live update (`pi2:session`, // Tree metadata is list metadata: a live update (`pi2:session`,
// reducer events, a rename) carries no `parentId`/`visible`/`task` and // reducer events, a rename) carries no `parentId`/`visible`/`task`/
// must not orphan the row. Explicit `undefined` is the only // `tab`/`createdAt`/`lastActiveAt` and must not orphan the row.
// "not carried" marker — an explicit `parentId: null` means root and // Explicit `undefined` is the only "not carried" marker — an explicit
// wins, which `??` would invert. // `parentId: null` means root and wins, which `??` would invert.
const previous = list[index] as SessionState; const previous = list[index] as SessionState;
const merged: SessionState = { const merged: SessionState = {
...session, ...session,
parentId: session.parentId !== undefined ? session.parentId : previous.parentId, parentId: session.parentId !== undefined ? session.parentId : previous.parentId,
visible: session.visible !== undefined ? session.visible : previous.visible, visible: session.visible !== undefined ? session.visible : previous.visible,
task: session.task !== undefined ? session.task : previous.task, task: session.task !== undefined ? session.task : previous.task,
tab: session.tab !== undefined ? session.tab : previous.tab,
createdAt: session.createdAt !== undefined ? session.createdAt : previous.createdAt,
lastActiveAt: session.lastActiveAt !== undefined ? session.lastActiveAt : previous.lastActiveAt,
}; };
const next = list.map((s, i) => (i === index ? merged : s)); const next = list.map((s, i) => (i === index ? merged : s));
return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: next } }; return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: next } };
@@ -117,6 +128,26 @@ export const useSessionsStore = create<SessionsStore>((set, get) => ({
selectSession: (selectedSessionId) => set({ selectedSessionId }), selectSession: (selectedSessionId) => set({ selectedSessionId }),
markSeen: (sessionId) =>
set((state) => {
let changed = false;
const byWorkspace: Record<string, SessionState[]> = {};
for (const [workspaceId, list] of Object.entries(state.byWorkspace)) {
const next = list.map((session) => {
if (session.sessionId !== sessionId) return session;
const attention = session.attention.length > 0;
if (!session.flags.done && !session.flags.error && session.flags.attention === attention) {
return session;
}
changed = true;
return { ...session, flags: { attention, done: false, error: false } };
});
byWorkspace[workspaceId] = next;
}
// Nothing to consume: keep the state object so zustand skips the render.
return changed ? { byWorkspace } : state;
}),
listFor: (workspaceId) => get().byWorkspace[workspaceId] ?? [], listFor: (workspaceId) => get().byWorkspace[workspaceId] ?? [],
getSession: (sessionId) => { getSession: (sessionId) => {