Skip to content

fix(core): cascade session interrupt into subagent tree - #67302

Open
aifeifei798 wants to merge 1 commit into
anomalyco:devfrom
aifeifei798:interrupt-cascade
Open

aifeifei798 wants to merge 1 commit into
anomalyco:devfrom
aifeifei798:interrupt-cascade

Conversation

@aifeifei798

@aifeifei798 aifeifei798 commented Nov 30, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #66398

Type of change

  • Bug fix
  • New feature (the interrupt_cascade flag)

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, and
jobs.cancelSession — which does match them through matchesSession — is only reachable from Session.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}/interrupt all left background work behind.

Session.interrupt now 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 are
    cancelled.
  • execution.interrupt(child.id) plus execution.awaitIdle(child.id) for each child, which is what releases the
    child's claim; cancelling the Job alone leaves a running turn in flight.
  • Descendants settle first, so a cancelled child's report lands in the parent's inbox while the parent is still
    active, then the parent is interrupted.
  • The walk is capped at MaxStopDepth = 4 and per-child failures are discarded, so a deep or broken tree cannot
    wedge the interrupt.

Everything stays inside Session.interrupt, so the server handler, the TUI and the REST endpoint all cascade with
no protocol or interrupt-parameter change.

The new default behavior is gated behind the new interrupt_cascade registry key in
packages/schema/src/config/flag.ts, read as the interrupted Session's own location configures it (same shape as
SessionBlob.enabledFor, so a location that cannot boot or load its config falls back to off instead of failing
the caller). It defaults to on in local and dev builds and off in releases. InterruptOptions.cascade overrides it
per call.

How did you verify your code works?

packages/core/test/session-interrupt-cascade.test.ts — 4 cases, 17 assertions, over a real SessionExecution
with a hanging SessionRunner:

  • parent → child → grandchild plus an unrelated session: after interrupt(parent, { cascade: true }) every Session
    in the tree is inactive, every execution claim is released, listSuspended() no longer reports them, and both
    runningBackground and pendingBackground are empty; the unrelated session keeps running.
  • cascade: false leaves the child running and claimed.
  • After a cascade, SessionRestart.resumeSuspendedSessions on a fresh graph over the same database resumes nothing.
  • A cancelled background subagent leaves a "Subagent cancelled" synthetic notice in the parent's inbox.

Also ran bun run check (lint plus typecheck across all packages, clean) and bun test in packages/core (the
one failing test there, Worktree > requires force to remove a dirty git worktree, reproduces identically at base
9268e29823, unrelated to this change).

Screenshots / recordings

No UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Related: #36423, #60107, #63875

Before

After

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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") } },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: interrupting a session does not stop its subagent tasks, and they resume on every service restart

1 participant