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
+8
View File
@@ -34,6 +34,11 @@ export class SessionBridge {
this.fan({ type: "text_delta", text: m.text ?? "" });
} else if (m.type === "system_event") {
this.fan(m as ClientEvent);
} else if (m.type === "agent_end") {
// Fine turno di Pi (pi-agent-core agent-loop): e' l'unico segnale che il turno e'
// concluso. Senza inoltrarlo, il FE resta "working" per sempre quando il turno
// finisce senza un gate successivo (ultimo step del workflow).
this.fan({ type: "system_event", event: "agent_end" });
}
// altri method nativi (setStatus/setWidget) e altri eventi Pi ignorati in MVP
});
@@ -43,6 +48,9 @@ export class SessionBridge {
onClientEvent(cb: (e: ClientEvent) => void): void { this.cbs.add(cb); }
/** Eventi generati dal backend stesso (es. exit inatteso del child Pi), non da Pi. */
emitClientEvent(e: ClientEvent): void { this.fan(e); }
respond(uiResponse: object & { id: string }): void {
// Correla sull'id RPC di Pi; `value` porta l'uiResponse (con l'id del descriptor) cosi'
// il check interno del gate (resp.id === descriptor.id) regge.
+14 -1
View File
@@ -70,7 +70,20 @@ export class PiProcessManager {
const rt: SessionRuntime = { rpc, bridge, child };
this.runtimes.set(sessionId, rt);
// Identity-checked: a stale child's exit must not evict a newer runtime.
child.on("exit", () => { if (this.runtimes.get(sessionId) === rt) this.runtimes.delete(sessionId); });
// Expected teardowns (teardown()/respawn) delete the runtime from the map BEFORE the
// exit event fires, so reaching this branch with `rt` still mapped means the child
// died on its own: tell the client, or the UI spins forever waiting for a turn end.
child.on("exit", (code) => {
if (this.runtimes.get(sessionId) === rt) {
this.runtimes.delete(sessionId);
rt.bridge.emitClientEvent({
type: "info",
level: "error",
text: `Pi process exited unexpectedly (code ${code ?? "?"})`,
});
rt.bridge.emitClientEvent({ type: "system_event", event: "agent_end" });
}
});
const provider = o.provider ?? this.cfg.defaults.provider;
const model = o.model ?? this.cfg.defaults.model;
+27
View File
@@ -80,6 +80,33 @@ function recordingChild() {
return ch;
}
test("un exit INATTESO del child notifica il client (info error + agent_end)", async () => {
const cfg = loadConfig({});
const child = recordingChild();
const mgr = new PiProcessManager(cfg, { spawnFn: () => child as any });
const rt = await mgr.spawnFor("crash-id", {});
const seen: any[] = [];
rt.bridge.onClientEvent((e) => seen.push(e));
child.emit("exit", 137);
expect(seen).toEqual([
{ type: "info", level: "error", text: expect.stringContaining("137") },
{ type: "system_event", event: "agent_end" },
]);
expect(mgr.count()).toBe(0);
});
test("l'exit dopo teardown NON emette eventi al client (uscita attesa)", async () => {
const cfg = loadConfig({});
const child = recordingChild();
const mgr = new PiProcessManager(cfg, { spawnFn: () => child as any });
const rt = await mgr.spawnFor("stop-id", {});
const seen: any[] = [];
rt.bridge.onClientEvent((e) => seen.push(e));
mgr.teardown("stop-id");
child.emit("exit", 0);
expect(seen).toEqual([]);
});
test("spawnFor resume mode sends /riprendi-sessione <id>", async () => {
const cfg = loadConfig({}); // no provider/model/thinking -> no rpc.request handshakes
const child = recordingChild();
+11
View File
@@ -54,6 +54,17 @@ test("respond correla sull'id RPC di Pi (non sull'id del descriptor) e azzera il
expect(b.pendingWidget()).toBeNull();
});
test("agent_end di Pi diventa un system_event agent_end per il FE", () => {
const { rpc, fire } = fakeRpc();
const b = new SessionBridge(rpc);
const seen: any[] = [];
b.onClientEvent((e) => seen.push(e));
// Pi emette agent_end alla fine di ogni prompt (pi-agent-core agent-loop); e' il solo
// segnale di fine turno: senza mapparlo il FE non puo' mai uscire dallo stato "working".
fire({ type: "agent_end", messages: [] });
expect(seen).toEqual([{ type: "system_event", event: "agent_end" }]);
});
test("steer invia un comando steer", () => {
const { rpc, sent } = fakeRpc();
new SessionBridge(rpc).steer("considera solo il 2024");