fix(bridge): forward Pi agent_end so the spinner stops at workflow completion

The FE derived 'working' purely as activeSession && !pendingWidget, so the
final workflow turn — the only one that ends without a follow-up gate —
left the spinner on forever (observed live: 21592s after F8 approve).

- SessionBridge maps Pi's agent_end -> SSE system_event {event: agent_end}
- PiProcessManager notifies the client (info error + synthetic agent_end)
  when the child dies unexpectedly; expected teardowns stay silent
- sessionStore tracks agentActive (on: user entry/text_delta/ui_request,
  off: agent_end); AppShell working now requires it; resume sets it
  optimistically

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 10:14:43 +02:00
co-authored by Claude Fable 5
parent b59b57c4e6
commit 2410f01b34
7 changed files with 115 additions and 7 deletions
+17 -3
View File
@@ -15,12 +15,17 @@ interface SessionState {
lastSystemEvent: StreamEvent | null;
currentPhase: string | null;
phaseError: string | null;
// True while a Pi turn is in flight. Turned off by the backend-forwarded `agent_end`
// system event — the only end-of-turn signal on the final workflow step, where no
// follow-up gate arrives to release the spinner.
agentActive: boolean;
applyEvent: (e: StreamEvent) => void;
clearPending: () => void;
resetSession: () => void;
setPhase: (phase: string | null) => void;
pushToast: (toast: { level: string; text: string }) => void;
setLastUserEntry: (e: { kind: "input" | "choice"; text: string }) => void;
setAgentActive: (v: boolean) => void;
}
const empty = {
@@ -32,6 +37,7 @@ const empty = {
lastSystemEvent: null,
currentPhase: null as string | null,
phaseError: null as string | null,
agentActive: false,
};
export const useSessionStore = create<SessionState>((set) => ({
@@ -41,6 +47,8 @@ export const useSessionStore = create<SessionState>((set) => ({
if (e.type === "ui_request")
return {
pendingWidget: e.ui_request,
// The turn is blocked on the gate, not ended: keep the agent marked active.
agentActive: true,
currentPhase: e.ui_request.phase
? e.ui_request.phase.split("_")[0]
: st.currentPhase,
@@ -52,19 +60,25 @@ export const useSessionStore = create<SessionState>((set) => ({
const last = t.at(-1);
if (last && !st.pendingWidget) t[t.length - 1] = { role: "assistant", text: last.text + e.text };
else t.push({ role: "assistant", text: e.text });
return { transcript: t };
// Streamed text means the turn is alive (also covers reattaching mid-turn).
return { transcript: t, agentActive: true };
}
if (e.type === "info") {
const stepMessages = [...st.stepMessages, { level: e.level ?? "info", text: e.text }];
// An error during the active phase marks that phase red until the next gate.
return e.level === "error" ? { stepMessages, phaseError: st.currentPhase } : { stepMessages };
}
if (e.type === "system_event") return { lastSystemEvent: e };
if (e.type === "system_event")
return e.event === "agent_end"
? { lastSystemEvent: e, agentActive: false }
: { lastSystemEvent: e };
return {};
}),
clearPending: () => set({ pendingWidget: null }),
resetSession: () => set({ ...empty }),
setPhase: (phase) => set({ currentPhase: phase }),
pushToast: (toast) => set((st) => ({ toasts: [...st.toasts, toast] })),
setLastUserEntry: (e) => set({ lastUserEntry: e, stepMessages: [] }),
// A user entry (question, gate choice, steer) hands the ball back to the harness.
setLastUserEntry: (e) => set({ lastUserEntry: e, stepMessages: [], agentActive: true }),
setAgentActive: (v) => set({ agentActive: v }),
}));