@devries-cameron20 / t3code-1 / commits / c6969f8f73
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(acp): a resumed message keeps the text streamed before it (#33604)

Ibrahim Elkamali committed Nov 12, 2026 at 02:13 UTC c6969f8f73ff56932da847beec13767763605acb
2 files changed +223 -35
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+176
@@ -2270,6 +2270,182 @@ describe("AcpAdapterV2", () => {
2270 }).pipe(Effect.provide(layerTest), Effect.scoped),
2271 );
2272
2273 + const textChunk = (
2274 + sessionUpdate: "agent_message_chunk" | "agent_thought_chunk",
2275 + messageId: string | undefined,
2276 + text: string,
2277 + ) => ({
2278 + sessionUpdate,
2279 + ...(messageId === undefined ? {} : { messageId }),
2280 + content: { type: "text" as const, text },
2281 + });
2282 + const answer = (messageId: string | undefined, text: string) =>
2283 + textChunk("agent_message_chunk", messageId, text);
2284 + const thought = (messageId: string | undefined, text: string) =>
2285 + textChunk("agent_thought_chunk", messageId, text);
2286 + const agentMessage = (messageId: string, text: string) => ({
2287 + sessionUpdate: "agent_message" as const,
2288 + messageId,
2289 + content: [{ type: "text" as const, text }],
2290 + });
2291 + type ScriptedTextStep = EffectAcpSchema.SessionUpdate | { readonly advance: Duration.Input };
2292 +
2293 + const runScriptedTextTurn = Effect.fnUntraced(function* (steps: ReadonlyArray<ScriptedTextStep>) {
2294 + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
2295 + let handler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
2296 + const instanceId = ProviderInstanceId.make("acp-named-text-resume");
2297 + const path = yield* Path.Path;
2298 + const adapter = yield* makeAcpAdapterV2({
2299 + instanceId,
2300 + selfInvocation: yield* resolveSelfInvocation(),
2301 + flavor: {
2302 + driver: ACP_TEST_DRIVER,
2303 + capabilities: AcpProviderCapabilitiesV2,
2304 + makeRuntime: makeMockRuntime({
2305 + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
2306 + mockAgentPath: yield* path.fromFileUrl(
2307 + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
2308 + ),
2309 + wrapRuntime: (runtime) => ({
2310 + ...runtime,
2311 + handleSessionUpdate: (next) =>
2312 + Effect.sync(() => {
2313 + handler = next;
2314 + }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
2315 + prompt: () =>
2316 + Effect.gen(function* () {
2317 + assert.isDefined(handler);
2318 + for (const step of steps) {
2319 + if ("advance" in step) yield* TestClock.adjust(step.advance);
2320 + else yield* handler!({ sessionId: "mock-session-1", update: step });
2321 + }
2322 + return { stopReason: "end_turn" as const };
2323 + }),
2324 + }),
2325 + }),
2326 + },
2327 + });
2328 + const threadId = ThreadId.make("thread-acp-named-text-resume");
2329 + const modelSelection = { instanceId, model: "default" };
2330 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2331 + runtimeMode: "full-access",
2332 + interactionMode: "default",
2333 + cwd: process.cwd(),
2334 + });
2335 + const runtime = yield* adapter.openSession({
2336 + threadId,
2337 + providerSessionId: ProviderSessionId.make("session-acp-named-text-resume"),
2338 + modelSelection,
2339 + runtimePolicy,
2340 + });
2341 + const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
2342 + yield* runtime.startTurn(
2343 + makeTurnInput({
2344 + threadId,
2345 + providerThread,
2346 + instanceId,
2347 + runtimePolicy,
2348 + now: yield* DateTime.now,
2349 + ordinal: 1,
2350 + }),
2351 + );
2352 + return yield* runtime.events.pipe(
2353 + Stream.takeUntil((event) => event.type === "turn.terminal"),
2354 + Stream.runCollect,
2355 + );
2356 + });
2357 +
2358 + it.effect.each([
2359 + {
2360 + label: "a thought with its own id interrupts a named assistant message",
2361 + steps: [
2362 + answer("answer", "First. "),
2363 + { advance: "1 second" },
2364 + thought("thought", "Thinking"),
2365 + answer("answer", "Second."),
2366 + ],
2367 + expected: [
2368 + ["assistant_message", "First. Second."],
2369 + ["reasoning", "Thinking"],
2370 + ],
2371 + },
2372 + {
2373 + label: "named assistant messages interleave",
2374 + steps: [answer("a", "A1 "), answer("b", "B1"), answer("a", "A2")],
2375 + expected: [
2376 + ["assistant_message", "A1 A2"],
2377 + ["assistant_message", "B1"],
2378 + ],
2379 + },
2380 + {
2381 + label: "a resumed message continues from authoritative text",
2382 + steps: [
2383 + answer("answer", "stale"),
2384 + thought("thought", "Thinking"),
2385 + agentMessage("answer", "fresh "),
2386 + thought("thought", " more"),
2387 + answer("answer", "tail"),
2388 + ],
2389 + expected: [
2390 + ["assistant_message", "fresh tail"],
2391 + ["reasoning", "Thinking more"],
2392 + ],
2393 + },
2394 + ] satisfies ReadonlyArray<{
2395 + readonly label: string;
2396 + readonly steps: ReadonlyArray<ScriptedTextStep>;
2397 + readonly expected: ReadonlyArray<readonly [string, string]>;
2398 + }>)("resumes ACP text segments when $label", ({ steps, expected }) =>
2399 + Effect.gen(function* () {
2400 + const events = yield* runScriptedTextTurn(steps);
2401 + const history = new Map<string, Array<OrchestrationV2TurnItem>>();
2402 + for (const event of events) {
2403 + if (event.type !== "turn_item.updated") continue;
2404 + const item = event.turnItem;
2405 + if (item.nativeItemRef?.nativeId?.includes(":turn:") !== true) continue;
2406 + history.set(item.id, [...(history.get(item.id) ?? []), item]);
2407 + }
2408 + const texts = [...history.values()].flatMap((snapshots) => {
2409 + const last = snapshots.at(-1);
2410 + return last !== undefined && "text" in last ? [[last.type, last.text]] : [];
2411 + });
2412 + assert.deepEqual(texts, expected);
2413 + for (const snapshots of history.values()) {
2414 + assert.equal(new Set(snapshots.map((item) => item.ordinal)).size, 1);
2415 + assert.equal(new Set(snapshots.map((item) => DateTime.formatIso(item.startedAt!))).size, 1);
2416 + assert.equal(snapshots.at(-1)?.status, "completed");
2417 + }
2418 + }).pipe(Effect.provide(layerTest), Effect.scoped),
2419 + );
2420 +
2421 + it.effect("ignores a text flush scheduled before an ACP segment resumed", () =>
2422 + Effect.gen(function* () {
2423 + const events = yield* runScriptedTextTurn([
2424 + answer("answer", "A"),
2425 + answer("answer", "B"),
2426 + thought("thought", "Thinking"),
2427 + { advance: "30 millis" },
2428 + answer("answer", "C"),
2429 + answer("answer", "D"),
2430 + { advance: "20 millis" },
2431 + answer("answer", "E"),
2432 + { advance: "30 millis" },
2433 + ]);
2434 + const snapshots = events.flatMap((event) =>
2435 + event.type === "message.updated" && event.message.role === "assistant"
2436 + ? [[event.message.text, event.message.streaming]]
2437 + : [],
2438 + );
2439 + assert.deepEqual(snapshots, [
2440 + ["A", true],
2441 + ["AB", false],
2442 + ["ABC", true],
2443 + ["ABCDE", true],
2444 + ["ABCDE", false],
2445 + ]);
2446 + }).pipe(Effect.provide(layerTest), Effect.scoped),
2447 + );
2448 +
2449 it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
2450 Effect.gen(function* () {
2451 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
packages/provider-acp/src/server/adapter.ts
+47 -35
@@ -1344,6 +1344,7 @@ interface ActiveTextSegment {
1344 interface ActiveTextStream {
1345 current: ActiveTextSegment | null;
1346 nextSegment: number;
1347 + readonly closedNamedSegments: Map<string, ActiveTextSegment>;
1348 }
1349
1350 interface AcpNativeBuildConfiguration {
@@ -2864,8 +2865,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2865 kind: "user" | "assistant" | "reasoning",
2866 ) {
2867 const stream = textStreamFor(context, kind);
2867 - if (stream.current === null) return;
2868 + const segment = stream.current;
2869 + if (segment === null) return;
2870 yield* emitTextSegment(context, kind, true);
2871 + if (segment.sourceMessageId !== null) {
2872 + stream.closedNamedSegments.set(segment.sourceMessageId, segment);
2873 + }
2874 stream.current = null;
2875 });
2876
@@ -2875,6 +2880,34 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2880 yield* closeTextStream(context, "assistant");
2881 });
2882
2883 + const openTextSegment = Effect.fnUntraced(function* (
2884 + context: ActiveAcpTurn,
2885 + kind: "user" | "assistant" | "reasoning",
2886 + sourceMessageId: string | null,
2887 + ) {
2888 + const stream = textStreamFor(context, kind);
2889 + const closed =
2890 + sourceMessageId === null ? undefined : stream.closedNamedSegments.get(sourceMessageId);
2891 + // A new object, so a flush scheduled before the close stays a no-op.
2892 + const segment: ActiveTextSegment =
2893 + closed === undefined
2894 + ? {
2895 + nativeItemId:
2896 + sourceMessageId === null
2897 + ? `${context.nativeTurnId}:${kind}:${stream.nextSegment}`
2898 + : `${context.nativeTurnId}:${kind}:message:${sourceMessageId}`,
2899 + startedAt: yield* DateTime.now,
2900 + sourceMessageId,
2901 + text: "",
2902 + emitted: false,
2903 + dirty: false,
2904 + }
2905 + : { ...closed, emitted: false, dirty: false };
2906 + if (closed === undefined) stream.nextSegment += 1;
2907 + stream.current = segment;
2908 + return segment;
2909 + });
2910 +
2911 const appendText = Effect.fnUntraced(function* (
2912 context: ActiveAcpTurn,
2913 kind: "user" | "assistant" | "reasoning",
@@ -2895,25 +2928,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2928 ) {
2929 yield* closeTextStream(context, kind);
2930 }
2898 - if (stream.current === null) {
2899 - const now = yield* DateTime.now;
2900 - stream.current = {
2901 - nativeItemId:
2902 - sourceMessageId === null
2903 - ? `${context.nativeTurnId}:${kind}:${stream.nextSegment}`
2904 - : `${context.nativeTurnId}:${kind}:message:${sourceMessageId}`,
2905 - startedAt: now,
2906 - sourceMessageId,
2907 - text: "",
2908 - emitted: false,
2909 - dirty: false,
2910 - };
2911 - stream.nextSegment += 1;
2912 - } else if (stream.current.sourceMessageId === null && sourceMessageId !== null) {
2931 + let segment = stream.current;
2932 + if (segment === null) {
2933 + segment = yield* openTextSegment(context, kind, sourceMessageId);
2934 + } else if (segment.sourceMessageId === null && sourceMessageId !== null) {
2935 // Agents may omit the first messageId; retain projected identity until the first later id.
2914 - stream.current.sourceMessageId = sourceMessageId;
2936 + segment.sourceMessageId = sourceMessageId;
2937 }
2916 - stream.current.text += text;
2938 + segment.text += text;
2939 yield* publishTextSegment(context, kind);
2940 });
2941
@@ -2926,22 +2948,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2948 for (const other of ["user", "reasoning", "assistant"] as const) {
2949 if (other !== kind) yield* closeTextStream(context, other);
2950 }
2929 - const stream = textStreamFor(context, kind);
2930 - if (stream.current?.sourceMessageId !== messageId) {
2951 + let segment = textStreamFor(context, kind).current;
2952 + if (segment?.sourceMessageId !== messageId) {
2953 yield* closeTextStream(context, kind);
2932 - const now = yield* DateTime.now;
2933 - stream.current = {
2934 - nativeItemId: `${context.nativeTurnId}:${kind}:message:${messageId}`,
2935 - startedAt: now,
2936 - sourceMessageId: messageId,
2937 - text,
2938 - emitted: false,
2939 - dirty: false,
2940 - };
2941 - stream.nextSegment += 1;
2942 - } else {
2943 - stream.current.text = text;
2954 + segment = yield* openTextSegment(context, kind, messageId);
2955 }
2956 + segment.text = text;
2957 yield* publishTextSegment(context, kind);
2958 });
2959
@@ -8192,9 +8204,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8204 (yield* Ref.get(activeSessionSetup))?.initializeResult.protocolVersion === 2
8205 ? promptParts?.prompt
8206 : undefined,
8195 - user: { current: null, nextSegment: 0 },
8196 - assistant: { current: null, nextSegment: 0 },
8197 - reasoning: { current: null, nextSegment: 0 },
8207 + user: { current: null, nextSegment: 0, closedNamedSegments: new Map() },
8208 + assistant: { current: null, nextSegment: 0, closedNamedSegments: new Map() },
8209 + reasoning: { current: null, nextSegment: 0, closedNamedSegments: new Map() },
8210 contextUsage: rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null,
8211 nativeMetadata: initialNativeMetadata,
8212 tools: new Map(),