Repository navigation
fix(core): cascade session interrupt into subagent tree - #67302
aifeifei798 wants to merge 1 commit into
Conversation
Interrupting a Session stopped only that Session: its running subagents kept their execution claims, so a task the user stopped resumed on the next server start. Session.interrupt now walks the tree below it, interrupting each descendant and cancelling the background jobs keyed by any Session in the tree, before stopping the Session itself. Descendants settle first so their cancellation reports land while the parent is still active. Guarded by the new interrupt_cascade flag (on by default in local and dev builds), read as the interrupted Session's own location configures it, and overridable per call through InterruptOptions.cascade.
There was a problem hiding this comment.
Thanks for picking this up. Interrupting now does stop the subagent tree, but in my check the parent doesn't stay stopped, so the interrupt doesn't do what the user asked.
How I checked: I drove the TUI from source on both branches. The parent launches a real subagent, and both the parent and the child hang in controlled shell calls. Then I pressed Esc twice. On the base, only the parent stops and the subagent keeps running. With this PR the child is interrupted too, but the child's "General cancelled" report then wakes the parent, which starts a new turn and runs its next tool call within seconds (see the screenshots). It happened in both runs. A user who presses stop ends up with the agent working again.
Public API: adds the experimental flag key interrupt_cascade to the config schema and the generated client type experimental.interrupt_cascade?: boolean. It also adds an optional cascade to Session.interrupt's options inside core; the HTTP API and SDK methods are unchanged. All of this is backwards compatible. With the flag on, the existing interrupt paths change behaviour: the TUI stop, POST session interrupt, plugin session.interrupt and the Code Mode interrupt tool.
The new turn after interrupt is the main issue. The other concerns are inline: the tree walk duplicates the Code Mode interrupt tool from #65718, the depth cap of 4 leaves deeper subagents running, and the test should check that the parent stays idle.
| interrupt: Effect.fn("Session.interrupt")(function* (sessionID, options) { | ||
| const session = yield* store.get(sessionID) | ||
| const cascade = options?.cascade ?? (session ? yield* locationFlag(session, "interrupt_cascade") : false) | ||
| if (cascade) yield* stopTree(sessionID, 0) |
There was a problem hiding this comment.
Stopping the children while the parent is still running makes each child's cancellation report land in the parent's inbox, and that wakes the parent again after its own interrupt. In a TUI run (parent → subagent → both in long shell calls, then Esc Esc), the parent started a new turn and issued its next tool call a few seconds after the interrupt. The parent can also start a new subagent between stopTree and its own interrupt, and that one escapes the cascade. SessionInterruptTool in tool/plugin/session-interrupt.ts (#65718) avoids both problems: it cancels jobs, interrupts, then cancels jobs again, so nothing finishes in between and no completion notice wakes anyone. Could the cascade follow that order, interrupting the session first and then stopping the tree so that nothing wakes it?
| * Descendants settle before the Session itself is interrupted, so their cancellation reports land while | ||
| * the parent is still active. | ||
| */ | ||
| const stopTree: (sessionID: SessionSchema.ID, depth: number) => Effect.Effect<void> = Effect.fn( |
There was a problem hiding this comment.
This is a second version of the tree walk in tool/plugin/session-interrupt.ts, but with a different order and a depth cap. Now that Session.interrupt cascades whenever the flag is on, that tool's own recursion walks the tree a second time. Please keep a single implementation: either make it the job of Session.interrupt({ cascade: true }) and have the tool call it, or share one helper.
With MaxStopDepth = 4, a subagent at depth 5 or more is silently left running with its claim held, so it still resumes after a restart. That's the bug this PR is meant to fix. The tree is finite (parent_id chains), and the existing interrupt tool walks it without a limit. Can the cap go, perhaps with a visited set if cycles are a worry?
| yield* sessions.interrupt(parent, { cascade: true }) | ||
|
|
||
| expect(yield* SessionInbox.list(database.db, parent)).toMatchObject([ | ||
| { type: "synthetic", payload: { text: expect.stringContaining("Subagent cancelled") } }, |
There was a problem hiding this comment.
This asserts that the cancellation notice lands in the parent's inbox, which is the notice that restarts the parent after it's interrupted. Please assert instead that the parent stays idle after a cascading interrupt, with no new drain and no wake-up. The file is also about 345 lines for these checks. Could it shrink to the core case (tree stopped, claims released, parent stays stopped) plus the no-resume-after-restart case?
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |


Issue for this PR
Closes #66398
Type of change
interrupt_cascadeflag)What does this PR do?
Interrupting a Session stopped only that Session. Its subagents kept running: their Job rows are keyed by the
child session id, so the parent's settled hook (
jobs.cancel(sessionID)) never matched them, andjobs.cancelSession— which does match them throughmatchesSession— is only reachable fromSession.remove.A running child also kept its write-ahead execution claim, so every service restart relaunched it. Ctrl+C in the
TUI, the web app's stop and
POST /api/session/{id}/interruptall left background work behind.Session.interruptnow walks the tree below the interrupted Session before stopping the Session itself:jobs.cancelSession(sessionID)at each level, so subagent Job rows keyed by the parent or any descendant arecancelled.
execution.interrupt(child.id)plusexecution.awaitIdle(child.id)for each child, which is what releases thechild's claim; cancelling the Job alone leaves a running turn in flight.
active, then the parent is interrupted.
MaxStopDepth = 4and per-child failures are discarded, so a deep or broken tree cannotwedge the interrupt.
Everything stays inside
Session.interrupt, so the server handler, the TUI and the REST endpoint all cascade withno protocol or interrupt-parameter change.
The new default behavior is gated behind the new
interrupt_cascaderegistry key inpackages/schema/src/config/flag.ts, read as the interrupted Session's own location configures it (same shape asSessionBlob.enabledFor, so a location that cannot boot or load its config falls back to off instead of failingthe caller). It defaults to on in local and dev builds and off in releases.
InterruptOptions.cascadeoverrides itper call.
How did you verify your code works?
packages/core/test/session-interrupt-cascade.test.ts— 4 cases, 17 assertions, over a realSessionExecutionwith a hanging
SessionRunner:interrupt(parent, { cascade: true })every Sessionin the tree is inactive, every execution claim is released,
listSuspended()no longer reports them, and bothrunningBackgroundandpendingBackgroundare empty; the unrelated session keeps running.cascade: falseleaves the child running and claimed.SessionRestart.resumeSuspendedSessionson a fresh graph over the same database resumes nothing.Also ran
bun run check(lint plus typecheck across all packages, clean) andbun testinpackages/core(theone failing test there,
Worktree > requires force to remove a dirty git worktree, reproduces identically at base9268e29823, unrelated to this change).Screenshots / recordings
No UI change.
Checklist