Skip to content

core: interrupted subagent dispatch loses the child session ID #67948

Backlog

Description

@OpeOginni

Description

If the server dies while dispatching a subagent, the child session and its queued prompt can survive without a link from the parent's tool call. On the parent's next model request, the tool error says Tool execution interrupted: subagent without the child session ID. The parent can't use that result to continue the existing child.

The interrupted tool result should include the original child session ID.

Plugins

Built-in subagent tool. No third-party plugins needed.

OpenCode version

Reproduced from source on dev at f1b673ca82.

Steps to reproduce

  1. In a parent session, ask the agent to delegate a small task to a subagent.
  2. Stop the server after the child's prompt is saved but before the background job is recorded. To hit this window reliably in a local test, take a SQLite snapshot at entry to Job.start for that subagent and start a fresh server from the snapshot.
  3. Confirm the snapshot has the child session and its queued prompt, with no background job for that child.
  4. After restart, ask the parent what happened to the delegated task. Inspect the subagent tool result sent with the next model request. It says Tool execution interrupted: subagent and omits the child ID.

Screenshot

Operating System

macOS arm64.

Terminal

Not required. Reproduced through the core integration test harness with a scripted model.

Activity

  1. OpeOginni commented on Dec 3, 2026

    @OpeOginni
    ContributorAuthor

    The existing restart fixes work once a subagent has a durable job record. I verified that an already-running child resumed and reported back once. This issue is about the earlier dispatch window: the child and its prompt are saved, but there is no job record yet and the interrupted parent tool result loses the child ID.

  2. added
    area/core
    type/bug
    version/v2
    on Dec 3, 2026
  3. opencodejr commented on Dec 3, 2026

    @opencodejr
    Contributor

    Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.

    This still happens on the latest dev (c8bf9d5), and I reproduced it on 2.4.3 with opencode-drive's simulated model.

    Setup:

    • A project plugin's prompt hook holds the child's prompt admission, so the child session exists but no job has started.
    • I froze the server and snapshotted its SQLite database, as you describe, then stopped it, restored the snapshot and relaunched.
    • I then asked the parent what happened.

    After the restore, the parent's next model request carried Tool execution interrupted: subagent with no child ID, even though the child session had been created. With a normal shutdown instead, 2.4.3 does include the ID (Tool execution interrupted (sessionID: ses_…)), so only a hard crash loses it. My stop point is slightly earlier than yours (the child's prompt wasn't saved yet), but the missing link is the same. The script and recording are below.

    Likely cause (not confirmed): the child ID is only reported as in-memory tool progress, which isn't saved. After a crash, settleStaleToolCalls in packages/core/src/session/runner/llm.ts (lines 438–446) finds no sessionID in the saved metadata. Your open PR #52296 addresses this; I haven't tested it.

    https://anoma.ly/team/github-bot/media/8c1c68cd6c852c0389303c997150bee4333d25206ba62cf03e6ca961043e064d.mp4

    Reproduction script (drive.ts)
    // Repro for #67948: server dies after the subagent child session is created but before its
    // background job is recorded. A project plugin's `prompt` hook parks the child's prompt
    // admission (marker file), the script snapshots the database there (like a hard crash),
    // stops the server, restores the snapshot, relaunches it, and logs the
    // parent's next model request to show the settled `subagent` tool error.
    import { Effect, Stream } from "effect"
    import { appendFileSync, copyFileSync, existsSync, readdirSync, readFileSync, readlinkSync, rmSync } from "node:fs"
    import { defineScript, Llm } from "opencode-drive"
    
    const MARK = "/workspace/repro/child-prompt.marker"
    const LOG = "/workspace/repro/requests.jsonl"
    rmSync(MARK, { force: true })
    rmSync(LOG, { force: true })
    
    const plugin = `
    import { existsSync, writeFileSync } from "node:fs"
    export default {
      id: "park-child-prompt",
      async setup(ctx) {
        await ctx.session.hook("prompt", async (event) => {
          if (!event.prompt.text.startsWith("You are a subagent spawned")) return
          if (existsSync(${JSON.stringify(MARK)})) return
          writeFileSync(${JSON.stringify(MARK)}, event.sessionID + " " + process.pid)
          await new Promise(() => {}) // park here until the server is killed
        })
      },
    }
    `
    
    // Run: OPENCODE_DRIVE_DB=restart.sqlite opencode-drive start --name r67948 --script ./drive.ts [--dev /path/to/opencode-checkout]
    // The parent's model requests are written to requests.jsonl; look at the `subagent` tool result after the restart.
    export default defineScript({
      launch: "manual",
      project: { git: true, files: { ".opencode/plugins/park/index.js": plugin } },
      run: ({ server, tuis, llm }) =>
        Effect.gen(function* () {
          yield* llm.serve((request, index) => {
            appendFileSync(LOG, JSON.stringify({ index, request }) + "\n")
            if (index === 0)
              return Stream.make(
                Llm.toolCall({ index: 0, id: "call_sub", name: "subagent",
                  input: { agent: "general", description: "Small task", prompt: "Say hi" } }),
                Llm.finish("tool-calls"),
              )
            return Stream.make(Llm.text(`Response ${index}`))
          })
          yield* server.launch()
          const tui = yield* tuis.launch("main", { recording: true })
          yield* tui.ui.submit("Delegate a small task to a subagent")
          for (let i = 0; i < 300 && !existsSync(MARK); i++) yield* Effect.sleep("100 millis")
          if (!existsSync(MARK)) return yield* Effect.die(new Error("child prompt hook never reached"))
          yield* tui.ui.screenshot("before-kill")
          // Hard crash, as in the report: freeze the server at this point, snapshot its SQLite
          // database (with WAL), let it stop, then restore the snapshot before relaunching.
          const pid = Number(readFileSync(MARK, "utf8").split(" ")[1])
          const db = readdirSync(`/proc/${pid}/fd`).map((fd) => { try { return readlinkSync(`/proc/${pid}/fd/${fd}`) } catch { return "" } })
            .find((f) => f.endsWith("restart.sqlite"))!
          process.kill(pid, "SIGSTOP")
          const files = [db, db + "-wal", db + "-shm"].filter((f) => existsSync(f))
          for (const f of files) copyFileSync(f, f + ".snap")
          process.kill(pid, "SIGCONT")
          yield* server.kill()
          for (const f of [db + "-wal", db + "-shm"]) rmSync(f, { force: true })
          for (const f of files) copyFileSync(f + ".snap", f)
          appendFileSync(LOG, JSON.stringify({ snapshot: db, files }) + "\n")
          yield* Effect.sleep("1 second")
          yield* server.launch()
          yield* Effect.sleep("3 seconds")
          yield* tui.ui.submit("What happened to the delegated task?")
          yield* tui.ui.waitFor("Response")
          yield* Effect.sleep("1 second")
          yield* tui.ui.screenshot("after-restart")
        }),
    })

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH.

  4. added
    status/repro-simulated
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/coreplatform/macosstatus/repro-simulatedtype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions