fix: make session resume atomic across restarts
This commit is contained in:
@@ -68,9 +68,14 @@ export class PiProcessManager {
|
||||
cwd: this.cfg.harnessDir,
|
||||
env,
|
||||
});
|
||||
// Log stderr for debugging (was silently drained)
|
||||
child.stderr.on("data", (d: Buffer) => console.error(`[pi:${sessionId}] stderr:`, d.toString().trim()));
|
||||
return child;
|
||||
try {
|
||||
// Log stderr for debugging (was silently drained)
|
||||
child.stderr.on("data", (d: Buffer) => console.error(`[pi:${sessionId}] stderr:`, d.toString().trim()));
|
||||
return child;
|
||||
} catch (error) {
|
||||
try { child.kill(); } catch { /* preserve the initialization error */ }
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
count(): number { return this.runtimes.size; }
|
||||
@@ -91,31 +96,39 @@ export class PiProcessManager {
|
||||
const author = o.author ?? "dev@local";
|
||||
const provider = canonicalPiProvider(o.provider ?? this.cfg.defaults.provider);
|
||||
const child = this.spawnFn(sessionId, author, provider);
|
||||
const rpc = new RpcClient(child);
|
||||
const bridge = new SessionBridge(rpc);
|
||||
const rt: SessionRuntime = { rpc, bridge, child };
|
||||
bridge.beginTurn();
|
||||
this.runtimes.set(sessionId, rt);
|
||||
// Identity-checked: a stale child's exit must not evict a newer runtime.
|
||||
// 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) => {
|
||||
console.error(`[pi:${sessionId}] exited code=${code ?? "?"} mapped=${this.runtimes.get(sessionId) === rt}`);
|
||||
if (this.runtimes.get(sessionId) === rt) {
|
||||
rt.bridge.markFailed();
|
||||
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: "session_failed" });
|
||||
rt.bridge.emitClientEvent({ type: "system_event", event: "agent_end" });
|
||||
}
|
||||
});
|
||||
let rt: SessionRuntime | undefined;
|
||||
try {
|
||||
const rpc = new RpcClient(child);
|
||||
const bridge = new SessionBridge(rpc);
|
||||
const runtime: SessionRuntime = { rpc, bridge, child };
|
||||
rt = runtime;
|
||||
bridge.beginTurn();
|
||||
this.runtimes.set(sessionId, runtime);
|
||||
// Identity-checked: a stale child's exit must not evict a newer runtime.
|
||||
// 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) => {
|
||||
console.error(`[pi:${sessionId}] exited code=${code ?? "?"} mapped=${this.runtimes.get(sessionId) === runtime}`);
|
||||
if (this.runtimes.get(sessionId) === runtime) {
|
||||
runtime.bridge.markFailed();
|
||||
this.runtimes.delete(sessionId);
|
||||
runtime.bridge.emitClientEvent({
|
||||
type: "info",
|
||||
level: "error",
|
||||
text: `Pi process exited unexpectedly (code ${code ?? "?"})`,
|
||||
});
|
||||
runtime.bridge.emitClientEvent({ type: "system_event", event: "session_failed" });
|
||||
runtime.bridge.emitClientEvent({ type: "system_event", event: "agent_end" });
|
||||
}
|
||||
});
|
||||
|
||||
return rt;
|
||||
return runtime;
|
||||
} catch (error) {
|
||||
if (rt && this.runtimes.get(sessionId) === rt) this.runtimes.delete(sessionId);
|
||||
try { child.kill(); } catch { /* preserve the initialization error */ }
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/** Configure model and thinking. Safe to run alongside deterministic retrieval. */
|
||||
|
||||
Reference in New Issue
Block a user