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:
YeonGyu-Kim
2026-05-17 00:30:50 +09:00
parent cc97a023cc
commit d3318617d0
4 changed files with 129 additions and 5 deletions
+3 -1
View File
@@ -28,7 +28,7 @@ import { SessionCategoryRegistry } from "../../shared/session-category-registry"
import { applySessionPromptParams } from "../../shared/session-prompt-params-helpers"
import { setSessionTools } from "../../shared/session-tools-store"
import { isInsideTmux } from "../../shared/tmux"
import { setSessionAgent, subagentSessions, updateSessionAgent } from "../claude-code-session-state"
import { clearSessionAgent, setSessionAgent, subagentSessions, updateSessionAgent } from "../claude-code-session-state"
import { MESSAGE_STORAGE } from "../hook-message-injector"
import { getTaskToastManager } from "../task-toast-manager"
import { abortWithTimeout } from "./abort-with-timeout"
@@ -762,6 +762,7 @@ export class BackgroundManager {
if (this.tasks.get(task.id)?.status === "cancelled") {
clearDelegatedChildSessionBootstrap(sessionID)
clearSessionAgent(sessionID)
await this.abortSessionWithLogging(sessionID, "cancelled during launch setup")
subagentSessions.delete(sessionID)
if (task.rootSessionId) {
@@ -774,6 +775,7 @@ export class BackgroundManager {
const boundAttempt = bindAttemptSession(task, attemptID, sessionID, input.model)
if (!boundAttempt) {
clearDelegatedChildSessionBootstrap(sessionID)
clearSessionAgent(sessionID)
await this.abortSessionWithLogging(sessionID, "stale attempt binding cleanup")
subagentSessions.delete(sessionID)
if (task.rootSessionId) {