Skip to content

core: foreground subagent interrupted by parent shutdown is recorded as a user stop #61413

Closed

Description

@YMoose

Describe the bug

When owner teardown (shutdown) interrupts a parent session while it is blocked waiting on a foreground subagent (jobs.block in packages/core/src/tool/plugin/subagent.ts), the Effect.onInterrupt cleanup calls sessions.interrupt(child.id) with no parent context.

Because SessionExecution.interrupt unconditionally passes "user" to coordinator.interrupt(sessionID, "user"), the parent session records reason: "shutdown" (preserving its time_suspended execution claim for restart recovery and omitting the idle marker), whereas the foreground child session records reason: "user", releases its execution claim (time_suspended = null), sets idle_outcome = "interrupted", and appends an idle context message as if a user had manually stopped it.

Steps to reproduce

  1. Start a parent session that invokes a foreground subagent tool call.
  2. While the child subagent execution is in flight, interrupt the parent execution fiber without calling the user-stop API (simulating owner/shutdown teardown).
  3. Inspect the emitted SessionEvent.Execution.Interrupted events and session rows for both parent and child:
    • Parent: reason === "shutdown", time_suspended !== null, idle_outcome === null
    • Child: reason === "user", time_suspended === null, idle_outcome === "interrupted" (expected "shutdown" to match the parent teardown)

Expected behavior

When a foreground subagent is interrupted as part of cascading its parent's interruption, it should inherit the active parent's recorded interruption reason (falling back to "shutdown" when unlabelled, matching terminal()), while ordinary user stops continue to report "user".

Activity

  1. opencodejr commented on Nov 3, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I reproduced this on the latest dev (6d1c444) with a core unit test.

    What the test does:

    • A parent session's turn starts a child session and waits on it.
    • Like the foreground path in subagent.ts, the parent calls sessions.interrupt(child) when it is interrupted.
    • The test then closes the execution owner scope, as a graceful shutdown does, without any user stop.

    What happens:

    • Parent: Execution.Interrupted has reason shutdown, time_suspended is kept and idle_outcome is null, as intended.
    • Child: reason is user, time_suspended is cleared and idle_outcome is interrupted, so restart recovery won't pick it up.

    The cause is that SessionExecution.interrupt always passes "user" to the coordinator. That overwrites the reason for a child that shutdown has already interrupted. The script below reproduces it; run it from an opencode checkout after bun install. Your PR #61414 proposes a fix.

    Reproduction script (repro-61413.sh)
    #!/usr/bin/env bash
    # Reproduces anomalyco/opencode#61413 as a core unit test.
    # Run from an opencode checkout (dev) after `bun install`: REPO=/path/to/opencode bash repro-61413.sh
    # It reuses the helpers in packages/core/test/session-execution.test.ts (seedSessions, buildExecution)
    # and adds one test: a parent drain starts a foreground child and, like subagent.ts, calls
    # sessions.interrupt(child) in Effect.onInterrupt. The owner scope is then closed (shutdown teardown).
    set -euo pipefail
    REPO="${REPO:-/workspace/repo}"
    cd "$REPO/packages/core"
    T=test/repro-61413.test.ts
    cp test/session-execution.test.ts "$T"
    trap 'rm -f "$T"' EXIT
    cat >> "$T" <<'TEST'
    describe("repro #61413", () => {
      it.effect("foreground child interrupted by parent shutdown teardown", () =>
        Effect.gen(function* () {
          const database = yield* Database.Service
          const bus = yield* Bus.Service
          const parent = Session.ID.make("ses_repro_parent")
          const child = Session.ID.make("ses_repro_child")
          yield* seedSessions(database, [parent])
          yield* seedSessions(database, [child], { parent_id: parent })
          const events: { sessionID: string; reason: string }[] = []
          yield* bus.project(SessionEvent.Execution.Interrupted, (event) =>
            Effect.sync(() => void events.push({ sessionID: event.data.sessionID, reason: event.data.reason })),
          )
          const childRunning = yield* Deferred.make<void>()
          const parentBlocked = yield* Deferred.make<void>()
          const scope = yield* Scope.make()
          yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void))
          let execution!: SessionExecution.Interface
          const context = yield* buildExecution(scope, ({ sessionID }) =>
            sessionID === child
              ? Deferred.succeed(childRunning, undefined).pipe(
                  Effect.andThen(Effect.never),
                  // A real child turn has async cleanup (stream/tool teardown) when interrupted.
                )
              : Effect.gen(function* () {
                  // Mirrors subagent.ts: start child, block on it, and on interrupt call sessions.interrupt(child.id)
                  yield* execution.resume(child).pipe(Effect.forkDetach)
                  yield* Deferred.await(childRunning)
                  yield* Deferred.succeed(parentBlocked, undefined)
                  yield* Effect.never.pipe(Effect.onInterrupt(() => execution.interrupt(child).pipe(Effect.tap((r) => Effect.sync(() => console.log("parent onInterrupt -> interrupt(child) =", r))), Effect.asVoid)))
                }),
          )
          execution = Context.get(context, SessionExecution.Service)
          yield* execution.resume(parent).pipe(Effect.forkIn(scope))
          yield* Deferred.await(parentBlocked)
          yield* Effect.promise(() => new Promise<void>((r) => setTimeout(r, 20)))
          // Owner teardown (shutdown): close the scope without any user stop.
          yield* Scope.close(scope, Exit.void)
          yield* Effect.promise(() => new Promise<void>((r) => setTimeout(r, 100)))
          const rows = yield* database.db
            .select({ id: SessionTable.id, suspended: SessionTable.time_suspended, idle: SessionTable.idle_outcome })
            .from(SessionTable)
            .all()
            .pipe(Effect.orDie)
          console.log("EVENTS", JSON.stringify(events))
          console.log("ROWS", JSON.stringify(rows.map((r) => ({ id: r.id, suspended: r.suspended !== null, idle: r.idle }))))
          expect(events.find((e) => e.sessionID === child)?.reason).toBe("shutdown")
        }),
      )
    })
    TEST
    bun test "$T" -t "repro #61413"

    Run it: bash repro-61413.sh

  2. YMoose commented on Nov 22, 2026

    @YMoose
    Author

    The upstream subagent lifetime refactor (#63382, #63223, #63392) removed the foreground subagent path: children now run as background Jobs, parent teardown no longer cascades an interruption into them (only an explicit caller cancel does), and a server shutdown leaves them resumable by design. The mismatch described here should no longer be reproducible on current dev.

  3. neriousy commented on Nov 23, 2026

    @neriousy
    Member

    The foreground subagent interrupt path this describes was removed in #63382, #63223 and #63392, so this no longer applies. If you still see this on the latest version, comment here and we'll reopen.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions