fix(coding-agent): cancel btw post-prompt work

This commit is contained in:
KamijoToma
2026-06-18 03:10:09 +08:00
parent b051bcf627
commit da29a25e54
2 changed files with 43 additions and 0 deletions
@@ -11192,6 +11192,17 @@ export class AgentSession {
}
}
await this.#cancelPostPromptTasks();
if (
this.isBashRunning ||
this.isEvalRunning ||
this.isCompacting ||
this.isGeneratingHandoff ||
this.isRetrying
) {
throw new Error("Cannot branch /btw while session maintenance or user work is still running");
}
this.#pendingNextTurnMessages = [];
this.#scheduledHiddenNextTurnGeneration = undefined;
this.agent.replaceQueues([], []);
@@ -252,6 +252,38 @@ describe("AgentSession.branchFromBtw", () => {
);
});
it("cancels post-prompt work after branch hooks before switching sessions", async () => {
const hookRelease = Promise.withResolvers<void>();
const extensionRunner = {
hasHandlers: vi.fn((eventType: string) => eventType === "session_before_branch"),
emit: vi.fn(async () => {
await hookRelease.promise;
return undefined;
}),
} as unknown as ExtensionRunner;
const activeSession = await createSession({ extensionRunner });
activeSession.sessionManager.appendMessage({ role: "user", content: "seed", timestamp: Date.now() });
await activeSession.sessionManager.flush();
activeSession.queueDeferredMessage({
role: "custom",
customType: "test-hidden-message",
content: "hidden",
display: false,
timestamp: Date.now(),
});
expect(activeSession.hasPostPromptWork).toBe(true);
const branchPromise = activeSession.branchFromBtw("question", createBtwAssistant());
await Promise.resolve();
expect(activeSession.hasPostPromptWork).toBe(true);
hookRelease.resolve();
const result = await branchPromise;
expect(result.cancelled).toBe(false);
expect(activeSession.hasPostPromptWork).toBe(false);
});
it("throws for in-memory sessions", async () => {
const activeSession = await createSession({ persisted: false });
activeSession.sessionManager.appendMessage({ role: "user", content: "seed", timestamp: Date.now() });