The best way to control Claude Code, Codex and more from any computer or phone
fix(provider-acp): a cancel no longer overtakes the prompt it cancels (#30846)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Navid Nadali committed
Nov 15, 2026 at 00:48 UTC
5f29d87c9b2a12e6ce3b7d5d0a68aaf13db0a471
3 files changed
+274
-4
apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts
+233
@@ -15,13 +15,16 @@ import * as Exit from "effect/Exit";
15
import * as Fiber from "effect/Fiber";
16
import * as Scope from "effect/Scope";
17
import * as Result from "effect/Result";
18
+import * as Scheduler from "effect/Scheduler";
19
import * as Stream from "effect/Stream";
20
+import * as TestClock from "effect/testing/TestClock";
21
import { ChildProcessSpawner } from "effect/process";
22
import { describe, expect } from "vite-plus/test";
23
24
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
25
import { AgentScope } from "@t3tools/shared/AgentScope";
26
import * as EffectAcpErrors from "effect-acp/errors";
27
+import type * as EffectAcpProtocol from "effect-acp/protocol";
28
29
const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
30
const mockAgentPath = NodePath.join(__dirname, "../../../scripts/acp-mock-agent.ts");
@@ -44,6 +47,136 @@ const setProcessEnv = (name: string, value: string) => {
47
};
48
};
49
50
+const isPromptStarted = (event: AcpSessionRuntime.AcpSessionRequestLogEvent) =>
51
+ event.method === "session/prompt" && event.status === "started";
52
+
53
+const isOutgoing = (tag: string) => (event: EffectAcpProtocol.AcpProtocolLogEvent) =>
54
+ event.direction === "outgoing" &&
55
+ event.stage === "decoded" &&
56
+ typeof event.payload === "object" &&
57
+ event.payload !== null &&
58
+ Reflect.get(event.payload, "tag") === tag;
59
+
60
+// The methods the mock agent read, in order, keeping only the given ones.
61
+const readReceived = (requestLogPath: string, methods: ReadonlyArray<string>) =>
62
+ NodeFS.readFileSync(requestLogPath, "utf8")
63
+ .split("\n")
64
+ .filter((line) => line.length > 0)
65
+ .map((line) => (JSON.parse(line) as { readonly method?: string }).method)
66
+ .filter((method) => method !== undefined && methods.includes(method));
67
+
68
+// Prompts, then cancels while the prompt is held on its way out: in its started request record,
69
+// in its outgoing protocol log record, or by its fiber yielding every few operations. The cancel
70
+// comes on dispatch or from inside the held record. Returns the prompt's exit
71
+// and the order in which the agent read the prompt, the cancel and the request that ends it.
72
+const promptThenCancel = (input: {
73
+ readonly cancelBehavior: "interrupt" | "wait-for-prompt";
74
+ readonly holdPrompt: {
75
+ readonly in: "request-log" | "protocol-log" | "scheduler";
76
+ readonly cancelOn: "dispatch" | "log";
77
+ };
78
+}) =>
79
+ Effect.gen(function* () {
80
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-"));
81
+ yield* Effect.addFinalizer(() =>
82
+ Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true })),
83
+ );
84
+ const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
85
+ const { holdPrompt } = input;
86
+ const promptHeld = yield* Deferred.make<void>();
87
+ const promptReleased = yield* Deferred.make<void>();
88
+ yield* Effect.addFinalizer(() => Deferred.succeed(promptReleased, undefined));
89
+ const testScope = yield* Scope.Scope;
90
+ const runtimeMade = yield* Deferred.make<AcpSessionRuntime.AcpSessionRuntime["Service"]>();
91
+ const cancelledFromLog = yield* Deferred.make<Fiber.Fiber<void, EffectAcpErrors.AcpError>>();
92
+ const hold = (
93
+ holdPrompt.cancelOn === "log"
94
+ ? Deferred.await(runtimeMade).pipe(
95
+ Effect.flatMap((made) =>
96
+ made.cancel.pipe(Effect.forkIn(testScope, { startImmediately: true })),
97
+ ),
98
+ Effect.flatMap((fiber) => Deferred.succeed(cancelledFromLog, fiber)),
99
+ )
100
+ : Effect.void
101
+ ).pipe(
102
+ Effect.andThen(Deferred.succeed(promptHeld, undefined)),
103
+ Effect.andThen(Deferred.await(promptReleased)),
104
+ );
105
+ const runtime = yield* AcpSessionRuntime.make({
106
+ ...mockRuntimeOptions,
107
+ spawn: {
108
+ ...mockRuntimeOptions.spawn,
109
+ env: {
110
+ T3_ACP_COMPLETE_FIRST_PROMPT_ON_CANCEL: "1",
111
+ T3_ACP_REQUEST_LOG_PATH: requestLogPath,
112
+ },
113
+ },
114
+ cancelBehavior: input.cancelBehavior,
115
+ interruptPromptOnCancel: false,
116
+ protocolLogging: {
117
+ logOutgoing: holdPrompt.in === "protocol-log",
118
+ logger: (event) => (isOutgoing("session/prompt")(event) ? hold : Effect.void),
119
+ },
120
+ requestLogger: (event) =>
121
+ holdPrompt.in === "request-log" && isPromptStarted(event) ? hold : Effect.void,
122
+ });
123
+ yield* Deferred.succeed(runtimeMade, runtime);
124
+ const cancelReceived = yield* Deferred.make<void>();
125
+ yield* runtime.getEvents().pipe(
126
+ Stream.runForEach((event) => {
127
+ if (event._tag === "EventStreamBarrier") {
128
+ return Deferred.succeed(event.acknowledge, undefined);
129
+ }
130
+ if (event._tag === "ThoughtDelta" && event.text === "native-cancel-received") {
131
+ return Deferred.succeed(cancelReceived, undefined);
132
+ }
133
+ return Effect.void;
134
+ }),
135
+ Effect.forkChild,
136
+ );
137
+ yield* runtime.start();
138
+ const dispatched = yield* Deferred.make<void>();
139
+ const prompt = yield* runtime
140
+ .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched })
141
+ .pipe(
142
+ holdPrompt.in === "scheduler"
143
+ ? Effect.provideService(Scheduler.MaxOpsBeforeYield, 3)
144
+ : (effect) => effect,
145
+ Effect.forkChild,
146
+ );
147
+ // A round trip: whatever was queued before it reaches the agent first.
148
+ const roundTrip = runtime.request("_test/environment", {});
149
+ let cancel: Fiber.Fiber<void, EffectAcpErrors.AcpError>;
150
+ if (holdPrompt.in === "scheduler") {
151
+ yield* Deferred.await(dispatched);
152
+ cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true }));
153
+ } else {
154
+ yield* Deferred.await(promptHeld);
155
+ if (holdPrompt.cancelOn === "dispatch") {
156
+ cancel = yield* Deferred.await(dispatched).pipe(
157
+ Effect.andThen(runtime.cancel),
158
+ Effect.forkChild({ startImmediately: true }),
159
+ );
160
+ } else {
161
+ cancel = yield* Deferred.await(cancelledFromLog);
162
+ }
163
+ yield* roundTrip;
164
+ yield* Deferred.succeed(promptReleased, undefined);
165
+ }
166
+ yield* Deferred.await(cancelReceived);
167
+ yield* runtime.request("_test/finish-cancel", {});
168
+ // The agent answers it only after reading every earlier line.
169
+ yield* roundTrip;
170
+ const received = readReceived(requestLogPath, [
171
+ "session/prompt",
172
+ "session/cancel",
173
+ "_test/finish-cancel",
174
+ ]);
175
+ const promptExit = yield* Fiber.await(prompt);
176
+ yield* Fiber.join(cancel);
177
+ return { received, promptExit };
178
+ }).pipe(Effect.scoped);
179
+
180
describe("AcpSessionRuntime", () => {
181
it.effect.each(["session/new", "session/resume"] as const)(
182
"buffers root metadata while %s startup is still pending",
@@ -1239,4 +1372,104 @@ describe("AcpSessionRuntime", () => {
1372
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
1373
);
1374
});
1375
+
1376
+ it.effect.each(
1377
+ (["interrupt", "wait-for-prompt"] as const).flatMap((cancelBehavior) =>
1378
+ (
1379
+ [
1380
+ { in: "request-log", cancelOn: "dispatch" },
1381
+ { in: "protocol-log", cancelOn: "log" },
1382
+ { in: "scheduler", cancelOn: "dispatch" },
1383
+ ] as const
1384
+ ).map((holdPrompt) => ({ cancelBehavior, holdPrompt })),
1385
+ ),
1386
+ )(
1387
+ "sends session/prompt before a cancel issued while the prompt is held in the $holdPrompt.in ($cancelBehavior, cancel on $holdPrompt.cancelOn)",
1388
+ ({ cancelBehavior, holdPrompt }) =>
1389
+ Effect.gen(function* () {
1390
+ const run = yield* promptThenCancel({ cancelBehavior, holdPrompt });
1391
+ expect(run.received).toEqual(["session/prompt", "session/cancel", "_test/finish-cancel"]);
1392
+ expect(run.promptExit).toEqual(
1393
+ Exit.succeed({ stopReason: "cancelled", _meta: { nativeCancel: true } }),
1394
+ );
1395
+ }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1396
+ );
1397
+
1398
+ it.effect("sends a cancel after the cancel timeout when its prompt never goes out", () =>
1399
+ Effect.gen(function* () {
1400
+ const runtime = yield* AcpSessionRuntime.make({
1401
+ ...mockRuntimeOptions,
1402
+ interruptPromptOnCancel: false,
1403
+ cancelTimeout: "5 seconds",
1404
+ requestLogger: (event) => (isPromptStarted(event) ? Effect.never : Effect.void),
1405
+ });
1406
+ yield* runtime.start();
1407
+ const dispatched = yield* Deferred.make<void>();
1408
+ yield* runtime
1409
+ .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched })
1410
+ .pipe(Effect.forkChild);
1411
+ yield* Deferred.await(dispatched);
1412
+ const cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true }));
1413
+ yield* TestClock.adjust("5 seconds");
1414
+ yield* Fiber.join(cancel);
1415
+ }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1416
+ );
1417
+
1418
+ it.effect(
1419
+ "sends a cancel before the next prompt when the prompt it waited for never went out",
1420
+ () =>
1421
+ Effect.gen(function* () {
1422
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-"));
1423
+ yield* Effect.addFinalizer(() =>
1424
+ Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true })),
1425
+ );
1426
+ const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
1427
+ let promptsStarted = 0;
1428
+ const runtime = yield* AcpSessionRuntime.make({
1429
+ ...mockRuntimeOptions,
1430
+ spawn: { ...mockRuntimeOptions.spawn, env: { T3_ACP_REQUEST_LOG_PATH: requestLogPath } },
1431
+ interruptPromptOnCancel: false,
1432
+ // The cancel spends a test-clock second in its outgoing record, the window in which the
1433
+ // next prompt could overtake it.
1434
+ protocolLogging: {
1435
+ logOutgoing: true,
1436
+ logger: (event) =>
1437
+ isOutgoing("session/cancel")(event) ? Effect.sleep("1 second") : Effect.void,
1438
+ },
1439
+ requestLogger: (event) =>
1440
+ isPromptStarted(event) && promptsStarted++ === 0 ? Effect.never : Effect.void,
1441
+ });
1442
+ yield* runtime.getEvents().pipe(
1443
+ Stream.runForEach((event) =>
1444
+ event._tag === "EventStreamBarrier"
1445
+ ? Deferred.succeed(event.acknowledge, undefined)
1446
+ : Effect.void,
1447
+ ),
1448
+ Effect.forkChild,
1449
+ );
1450
+ yield* runtime.start();
1451
+ const dispatched = yield* Deferred.make<void>();
1452
+ const first = yield* runtime
1453
+ .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched })
1454
+ .pipe(Effect.forkChild);
1455
+ yield* Deferred.await(dispatched);
1456
+ const cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true }));
1457
+ const next = yield* runtime
1458
+ .prompt({ prompt: [{ type: "text", text: "next" }] })
1459
+ .pipe(Effect.forkChild);
1460
+ // The first prompt ends before it is queued, which releases the cancel's wait.
1461
+ yield* Effect.sleep("1 second").pipe(
1462
+ Effect.andThen(Fiber.interrupt(first)),
1463
+ Effect.forkChild,
1464
+ );
1465
+ yield* TestClock.adjust("1 second");
1466
+ yield* TestClock.adjust("1 second");
1467
+ yield* Fiber.join(cancel);
1468
+ yield* Fiber.join(next);
1469
+ expect(readReceived(requestLogPath, ["session/prompt", "session/cancel"])).toEqual([
1470
+ "session/cancel",
1471
+ "session/prompt",
1472
+ ]);
1473
+ }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
1474
+ );
1475
});
packages/effect-acp/src/protocol.ts
+14
@@ -1,6 +1,7 @@
1
import type * as AcpCompat from "./compat.ts";
2
import * as Cause from "effect/Cause";
3
import * as Clock from "effect/Clock";
4
+import * as Context from "effect/Context";
5
import * as Effect from "effect/Effect";
6
import * as Deferred from "effect/Deferred";
7
import * as Exit from "effect/Exit";
@@ -115,6 +116,15 @@ export interface AcpPatchedProtocolOptions {
116
};
117
}
118
119
+/**
120
+ * Provided to a request call, settles once that request is queued for writing. Messages queued
121
+ * later are written after it; the call itself returns only with the reply.
122
+ */
123
+export const AcpOutgoingQueued = Context.Reference<Deferred.Deferred<void> | undefined>(
124
+ "effect-acp/protocol/AcpOutgoingQueued",
125
+ { defaultValue: () => undefined },
126
+);
127
+
128
export interface AcpPatchedProtocol {
129
readonly clientProtocol: RpcClient.Protocol["Service"];
130
readonly serverProtocol: RpcServer.Protocol["Service"];
@@ -438,6 +448,10 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
448
}).pipe(Effect.andThen(Deferred.fail(acknowledgement, error)), Effect.asVoid),
449
),
450
);
451
+ const queued = yield* AcpOutgoingQueued;
452
+ if (queued !== undefined) {
453
+ yield* Deferred.succeed(queued, undefined);
454
+ }
455
if (acknowledgement !== undefined) {
456
yield* Deferred.await(acknowledgement);
457
}
packages/provider-acp/src/server/AcpSessionRuntime.ts
+27
-4
@@ -27,7 +27,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
27
import * as EffectAcpClient from "effect-acp/client";
28
import * as EffectAcpErrors from "effect-acp/errors";
29
import type * as EffectAcpSchema from "effect-acp/compat";
30
-import type * as EffectAcpProtocol from "effect-acp/protocol";
30
+import * as EffectAcpProtocol from "effect-acp/protocol";
31
import { resolveSpawnCommand } from "@t3tools/shared/shell";
32
import { AgentScope, AgentScopeThreadId, RAISE_OOM_SCORE_LINE } from "@t3tools/shared/AgentScope";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -1245,8 +1245,14 @@ interface EnsureActiveAssistantSegmentResult {
1245
interface AcpActivePrompt {
1246
readonly fiber: Fiber.Fiber<EffectAcpSchema.PromptResponse, EffectAcpErrors.AcpError>;
1247
readonly completed: Deferred.Deferred<void>;
1248
+ /** Settles once the prompt is queued to the agent, or its call has ended without that. */
1249
+ readonly queued: Deferred.Deferred<void>;
1250
}
1251
1252
+// A fiber stopped before its call began never settles queued, so its end counts too.
1253
+const awaitPromptQueued = (prompt: AcpActivePrompt) =>
1254
+ Effect.raceFirst(Deferred.await(prompt.queued), Fiber.await(prompt.fiber).pipe(Effect.asVoid));
1255
+
1256
export const make = Effect.fn("AcpSessionRuntime.make")(function* (
1257
options: AcpSessionRuntimeOptions,
1258
): Effect.fn.Return<
@@ -2403,8 +2409,16 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
2409
sessionId: started.sessionId,
2410
...(options.cancelMeta === undefined ? {} : { _meta: options.cancelMeta }),
2411
} satisfies EffectAcpSchema.CancelNotification;
2412
+ // An agent drops a cancel that reaches it before the prompt it targets, so the cancel waits
2413
+ // until the prompt is queued, for at most the cancel timeout.
2414
+ const promptQueued = Option.isSome(activePrompt)
2415
+ ? awaitPromptQueued(activePrompt.value).pipe(
2416
+ Effect.timeoutOption(options.cancelTimeout ?? defaultCancelTimeout),
2417
+ )
2418
+ : Effect.void;
2419
if (options.cancelBehavior !== "wait-for-prompt") {
2420
if (options.interruptPromptOnCancel === false) {
2421
+ yield* promptQueued;
2422
return yield* acp.agent.cancel(cancelPayload);
2423
}
2424
if (Option.isSome(activePrompt)) {
@@ -2415,6 +2429,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
2429
return;
2430
}
2431
2432
+ yield* promptQueued;
2433
yield* acp.agent.cancel(cancelPayload);
2434
if (Option.isNone(activePrompt)) {
2435
return;
@@ -2649,12 +2664,18 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
2664
...payload,
2665
} satisfies EffectAcpSchema.PromptRequest;
2666
const completed = yield* Deferred.make<void>();
2667
+ const queued = yield* Deferred.make<void>();
2668
const fiber = yield* runLoggedRequest(
2669
"session/prompt",
2670
requestPayload,
2655
- acp.agent.prompt(requestPayload),
2671
+ acp.agent
2672
+ .prompt(requestPayload)
2673
+ .pipe(
2674
+ Effect.provideService(EffectAcpProtocol.AcpOutgoingQueued, queued),
2675
+ Effect.ensuring(Deferred.succeed(queued, undefined)),
2676
+ ),
2677
).pipe(Effect.forkIn(runtimeScope));
2657
- const active = { fiber, completed } satisfies AcpActivePrompt;
2678
+ const active = { fiber, completed, queued } satisfies AcpActivePrompt;
2679
yield* Ref.set(activePromptRef, Option.some(active));
2680
if (promptOptions?.dispatched) {
2681
yield* Deferred.succeed(promptOptions.dispatched, undefined);
@@ -2711,8 +2732,10 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
2732
: joinPrompt(outcome.fiber),
2733
),
2734
),
2735
+ // A cancel that waits for its prompt holds the dispatch permit, so a next prompt cannot reach
2736
+ // the agent ahead of it and take the cancel meant for the prompt before.
2737
cancel:
2715
- options.cancelBehavior === "wait-for-prompt"
2738
+ options.cancelBehavior === "wait-for-prompt" || options.interruptPromptOnCancel === false
2739
? promptDispatchSemaphore.withPermit(cancel)
2740
: cancel,
2741
...(options.ownDetachedProcessGroup === true ? { terminateProcessGroup } : {}),