diff --git a/src/features/background-agent/manager.test.ts b/src/features/background-agent/manager.test.ts index d93246447..e83054d5a 100644 --- a/src/features/background-agent/manager.test.ts +++ b/src/features/background-agent/manager.test.ts @@ -15,6 +15,7 @@ afterAll(() => { mock.restore() }) import { getSessionPromptParams, clearSessionPromptParams } from "../../shared/session-prompt-params-state" import { tmpdir } from "node:os" import type { PluginInput } from "@opencode-ai/plugin" +import { _resetForTesting as resetClaudeCodeSessionState, subagentSessions } from "../claude-code-session-state" import type { BackgroundTask, ResumeInput } from "./types" import { MIN_IDLE_TIME_MS } from "./constants" import { BackgroundManager } from "./manager" @@ -213,6 +214,10 @@ function getCompletionTimers(manager: BackgroundManager): Map> }).completionTimers } +function getRootDescendantCounts(manager: BackgroundManager): Map { + return (manager as unknown as { rootDescendantCounts: Map }).rootDescendantCounts +} + function getQueuesByKey( manager: BackgroundManager ): Map> { @@ -2585,6 +2590,7 @@ describe("BackgroundManager - Non-blocking Queue Integration", () => { test("should keep task cancelled when cancelled during tmux callback before running state is assigned", async () => { // given + resetClaudeCodeSessionState() const originalTmuxEnvironment = process.env.TMUX process.env.TMUX = "test-session" @@ -2674,7 +2680,10 @@ describe("BackgroundManager - Non-blocking Queue Integration", () => { expect(promptAsyncSessionIDs).not.toContain(createdSessionID) expect(abortCalls).toEqual([createdSessionID]) expect(getConcurrencyManager(manager).getCount("test-agent")).toBe(0) + expect(getRootDescendantCounts(manager).has("parent-session")).toBe(false) + expect(subagentSessions.has(createdSessionID)).toBe(false) } finally { + resetClaudeCodeSessionState() if (originalTmuxEnvironment === undefined) { delete process.env.TMUX } else { diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts index aa5979cf4..743d7769a 100644 --- a/src/features/background-agent/manager.ts +++ b/src/features/background-agent/manager.ts @@ -493,6 +493,10 @@ export class BackgroundManager { if (this.tasks.get(task.id)?.status === "cancelled") { await this.abortSessionWithLogging(sessionID, "cancelled during tmux setup") + subagentSessions.delete(sessionID) + if (task.rootSessionID) { + this.unregisterRootDescendant(task.rootSessionID) + } this.concurrencyManager.release(concurrencyKey) return }