fix(agents): preserve foreground-only MCP cancellation

Opt the app and CLI Stop endpoint into canceling idle session background work. Keep normal agent-run cancellation scoped to an in-flight run.
This commit is contained in:
Mohamed Boudra
2026-11-20 12:53:20 +01:00
parent d30e159c0c
commit aed906aaba
3 changed files with 15 additions and 1 deletions
@@ -102,8 +102,13 @@ async function requestAgentRunCancellation(
export async function cancelAgentRunCommand(
dependencies: Pick<AgentLifecycleCommandDependencies, "agentManager" | "logger">,
agentId: string,
options?: { includeBackgroundWork?: boolean },
): Promise<CancelAgentRunResult> {
const result = await requestAgentRunCancellation(dependencies, agentId, true);
const result = await requestAgentRunCancellation(
dependencies,
agentId,
options?.includeBackgroundWork,
);
if (result.cancellation.status === "refused") {
dependencies.logger.warn(
{ agentId },
@@ -2947,9 +2947,17 @@ test.each(["steer", "stop", "interrupt", "refused follow-up"] as const)(
await expect.poll(() => calls).toEqual(["Start two commands", "session/cancel"]);
expect(manager.getAgent(agent.id)?.lifecycle).toBe("idle");
} else {
const foregroundOnly = await cancelAgentRunCommand(
{ agentManager: manager, logger },
agent.id,
);
expect(foregroundOnly.cancelled).toBe(false);
expect(calls).toEqual(["Start two commands"]);
const { cancelled } = await cancelAgentRunCommand(
{ agentManager: manager, logger },
agent.id,
{ includeBackgroundWork: true },
);
expect(cancelled).toBe(true);
await expect.poll(() => calls).toEqual(["Start two commands", "session/cancel"]);
+1
View File
@@ -5334,6 +5334,7 @@ export class Session {
await cancelAgentRunCommand(
{ agentManager: this.agentManager, logger: this.sessionLogger },
agentId,
{ includeBackgroundWork: true },
);
if (requestId) {
const agent = this.agentManager.getAgent(agentId);