fix(background-agent): clean child session-agent state on pre-start abort and normalize stored agent
Two adjacent gaps cubic flagged on the previous diff: 1. spawner.startTask stored input.agent (potentially prefixed with sort marker and ZWSP) in setSessionAgent, but the prompt body used the stripped/normalized form. The session-agent registry therefore did not match what promptAsync actually dispatched. Capture the normalized agent once at the top of startTask and use it for setSessionAgent plus the launch log lines. 2. manager.startTask wrote setSessionAgent(sessionID, input.agent) before the cancelled and stale-attempt cleanup branches, but those branches only cleared subagentSessions and the delegated bootstrap. The session->agent mapping survived as orphan state after an aborted launch. Call clearSessionAgent inside both early-return paths so nothing remains tied to a session we just aborted. Adds focused tests for both: spawner persistence parity with promptAsync and manager cancellation cleanup leaving getSessionAgent undefined.
This commit is contained in:
@@ -7391,6 +7391,67 @@ describe("BackgroundManager attempt lifecycle bindings", () => {
|
||||
manager.shutdown()
|
||||
})
|
||||
|
||||
test("startTask clears child session agent state when task is cancelled before launch binding", async () => {
|
||||
//#given
|
||||
resetClaudeCodeSessionState()
|
||||
const sessionID = "session-cancelled-prelaunch"
|
||||
const client = {
|
||||
session: {
|
||||
get: async () => ({ data: { directory: "/test/dir" } }),
|
||||
create: async () => ({ data: { id: sessionID } }),
|
||||
promptAsync: async () => ({}),
|
||||
abort: async () => ({}),
|
||||
},
|
||||
}
|
||||
const manager = new BackgroundManager({ pluginContext: createPluginInput(client) })
|
||||
const task: BackgroundTask = {
|
||||
id: "task-cancel-prelaunch",
|
||||
status: "pending",
|
||||
queuedAt: new Date(),
|
||||
description: "cancel before bind",
|
||||
prompt: "continue",
|
||||
agent: "sisyphus-junior",
|
||||
parentSessionId: "parent-session",
|
||||
parentMessageId: "parent-message",
|
||||
model: { providerID: "anthropic", modelID: "claude-haiku-4.5" },
|
||||
attempts: [
|
||||
{
|
||||
attemptId: "attempt-1",
|
||||
attemptNumber: 1,
|
||||
providerId: "anthropic",
|
||||
modelId: "claude-haiku-4.5",
|
||||
status: "pending",
|
||||
},
|
||||
],
|
||||
currentAttemptID: "attempt-1",
|
||||
attemptCount: 1,
|
||||
}
|
||||
const input: import("./types").LaunchInput = {
|
||||
description: task.description,
|
||||
prompt: task.prompt,
|
||||
agent: task.agent,
|
||||
parentSessionId: task.parentSessionId,
|
||||
parentMessageId: task.parentMessageId,
|
||||
model: task.model,
|
||||
onSessionCreated: async () => {
|
||||
// simulate parent flipping task to cancelled between create and bind
|
||||
task.status = "cancelled"
|
||||
const internal = cast<{ tasks: Map<string, BackgroundTask> }>(manager)
|
||||
internal.tasks.set(task.id, task)
|
||||
},
|
||||
}
|
||||
|
||||
//#when
|
||||
await (cast<{
|
||||
startTask: (item: { task: BackgroundTask; input: import("./types").LaunchInput; attemptID: string }) => Promise<void>
|
||||
}>(manager)).startTask({ task, input, attemptID: "attempt-1" })
|
||||
|
||||
//#then
|
||||
expect(getSessionAgent(sessionID)).toBeUndefined()
|
||||
|
||||
manager.shutdown()
|
||||
})
|
||||
|
||||
test("historical attempt session IDs resolve to the task while stale session.error events leave the current attempt unchanged", async () => {
|
||||
//#given
|
||||
const manager = createBackgroundManager()
|
||||
|
||||
Reference in New Issue
Block a user