Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(daemon): bound the SDK session stop with a force deadline

Claudette0eaa7ee202c1
diff --git a/src/daemon/src/runtime/sdkManagedSession.test.ts b/src/daemon/src/runtime/sdkManagedSession.test.ts
index a269dc3a7..068eec836 100644
--- a/src/daemon/src/runtime/sdkManagedSession.test.ts
+++ b/src/daemon/src/runtime/sdkManagedSession.test.ts
@@ -191,6 +191,76 @@ describe("SdkManagedSession", () => {
expect(exited).toHaveBeenCalledTimes(1);
});
+ // Batch S (plans/daemon-fsm-desync.md): SDK has no SIGKILL backstop, so a
+ // vendor SDK whose `inner.stop()` never settles would otherwise pin the
+ // adapter — `emitExit` sits behind an await that never returns and the FSM
+ // is stuck "stopping" forever. `stop()` bounds the disposal wait; past the
+ // deadline it forces "exit" and abandons the inner session.
+ it("stop() forces \"exit\" within the deadline when inner.stop() hangs forever", async () => {
+ vi.useFakeTimers();
+ try {
+ const { driver } = fakeSdkDriver();
+ const adapter = new SdkManagedSession(driver, baseCtx(), fakeDeps());
+ const exited = vi.fn();
+ adapter.on("exit", exited);
+ await adapter.start({ text: "hello" });
+
+ const inner = (adapter as unknown as { inner: SdkRuntimeSession }).inner;
+ // Never settles — the wedged-dispose case the bound exists for.
+ vi.spyOn(inner, "stop").mockReturnValueOnce(new Promise<void>(() => { }));
+
+ const stopPromise = adapter.stop({ forceAfterMs: 2000 });
+ // Before the deadline: still pending, no exit yet.
+ await vi.advanceTimersByTimeAsync(1999);
+ expect(exited).not.toHaveBeenCalled();
+ // Deadline elapses → forced exit, stop() resolves.
+ await vi.advanceTimersByTimeAsync(1);
+ await expect(stopPromise).resolves.toBeUndefined();
+ expect(exited).toHaveBeenCalledTimes(1);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("stop() best-effort aborts the inner session on the forced (timeout) path", async () => {
+ vi.useFakeTimers();
+ try {
+ const { driver } = fakeSdkDriver();
+ const adapter = new SdkManagedSession(driver, baseCtx(), fakeDeps());
+ await adapter.start({ text: "hello" });
+
+ const inner = (adapter as unknown as { inner: SdkRuntimeSession }).inner;
+ vi.spyOn(inner, "stop").mockReturnValueOnce(new Promise<void>(() => { }));
+ const abort = vi.fn();
+ (inner as unknown as { abort?: () => void }).abort = abort;
+
+ const stopPromise = adapter.stop({ forceAfterMs: 2000 });
+ await vi.advanceTimersByTimeAsync(2000);
+ await stopPromise;
+ expect(abort).toHaveBeenCalledTimes(1);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("stop() that disposes cleanly before the deadline does NOT wait out the timer", async () => {
+ vi.useFakeTimers();
+ try {
+ const { driver } = fakeSdkDriver();
+ const adapter = new SdkManagedSession(driver, baseCtx(), fakeDeps());
+ const exited = vi.fn();
+ adapter.on("exit", exited);
+ await adapter.start({ text: "hello" });
+
+ // inner.stop() resolves immediately — the real fakeSdkDriver handle's
+ // dispose is synchronous, so no timer advance is needed for exit.
+ await adapter.stop({ forceAfterMs: 60_000 });
+ expect(exited).toHaveBeenCalledTimes(1);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
// Regression test: `managerRuntime.ts::doSpawn` registers the session in
// its map (and a `stop`/`terminate_stalled` effect can look it up and call
// `.stop()`) BEFORE `start()`'s `driver.createSession()` has resolved. If
diff --git a/src/daemon/src/runtime/sdkManagedSession.ts b/src/daemon/src/runtime/sdkManagedSession.ts
index 7688acc2f..4394ccd77 100644
--- a/src/daemon/src/runtime/sdkManagedSession.ts
+++ b/src/daemon/src/runtime/sdkManagedSession.ts
@@ -33,6 +33,7 @@
import { EventEmitter } from "events";
import type { Driver, LaunchContext, SdkDriverDeps } from "../types.js";
import type { SdkRuntimeSession } from "./sdkRuntimeSession.js";
+import { SESSION_STOP_GRACE_MS } from "./killTree.js";
type SdkCapableDriver = Driver & { createSession: NonNullable<Driver["createSession"]> };
@@ -118,16 +119,62 @@ export class SdkManagedSession {
* or the manager's exit listener never runs: the session is never removed
* from `AgentProcessManager`'s map and the FSM never leaves "stopping".
*
+ * BOUNDED FORCE (plans/daemon-fsm-desync.md batch S): a child process has a
+ * physical backstop — `killProcessTree` escalates to SIGKILL, which an
+ * unresponsive process cannot survive, so its `exit` is guaranteed. An
+ * in-process SDK has no such backstop: if `inner.stop()` never settles (a
+ * vendor SDK wedged mid-dispose), the `finally` below never runs and the FSM
+ * is stuck `stopping` forever — the same "recovery never completes" failure
+ * this whole plan attacks, one layer down. So we bound the wait: `inner.stop()`
+ * races a `forceAfterMs` deadline (default `SESSION_STOP_GRACE_MS`, matching
+ * the child grace the manager already passes), and whichever settles first,
+ * `emitExit()` fires. This is SDK's SIGKILL-equivalent: a GUARANTEED exit.
+ * On the timeout path the inner session is abandoned (best-effort `abort`) —
+ * we stop awaiting it, so a wedged dispose can't pin the agent.
+ *
* `stopRequested` is set synchronously (before awaiting `starting`) so a
* `start()` that's still mid-flight sees it as soon as `driver.createSession()`
* resolves and skips firing the first turn (see `start()`).
*/
- async stop(): Promise<void> {
+ async stop(opts?: { reason?: string; forceAfterMs?: number }): Promise<void> {
this.stopRequested = true;
+ const forceAfterMs = opts?.forceAfterMs ?? SESSION_STOP_GRACE_MS;
this.stopping = (async () => {
try {
if (this.starting) await this.starting.catch(() => { });
- await this.inner?.stop();
+ const inner = this.inner;
+ if (!inner) return;
+ // Race disposal against the force deadline. `inner.stop()` may reject
+ // promptly (surfaced to the caller — see the dispose-rejects test) or
+ // hang indefinitely (the wedged-SDK case this bound exists for). A
+ // timeout WIN means we abandon the inner session rather than await it.
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const forced = new Promise<"forced">((resolve) => {
+ timer = setTimeout(() => resolve("forced"), forceAfterMs);
+ timer.unref?.();
+ });
+ const stopped = inner.stop().then(() => "stopped" as const);
+ // If the deadline wins the race, `stopped` becomes the loser and its
+ // eventual rejection (a wedged dispose that later throws) would be
+ // unhandled — attach a no-op catch so it's marked handled. This does
+ // NOT swallow a PROMPT rejection from the race itself: `Promise.race`
+ // still sees `stopped` reject first in that case and rethrows it, so
+ // `stop()` rejects (dispose-rejects test) while `emitExit` still fires.
+ stopped.catch(() => { });
+ try {
+ const outcome = await Promise.race([stopped, forced]);
+ if (outcome === "forced") {
+ // Wedged dispose — abandon the inner session so it can't pin us.
+ // Best-effort abort; ignore anything it throws.
+ try {
+ (inner as unknown as { abort?: () => void }).abort?.();
+ } catch {
+ // no-op: abort is best-effort on a session we're already dropping
+ }
+ }
+ } finally {
+ if (timer) clearTimeout(timer);
+ }
} finally {
this.emitExit();
}