@devries-cameron20 / t3code Mirror · read-only / commits / 5dd8cde083
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

fix(server): idle ACP child output no longer leaks into root history (#37556)

Julius Marminge committed Nov 16, 2026 at 00:46 UTC 5dd8cde0831113058f2af74a699499c0eeb3b31b
2 files changed +47 -14
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+12
@@ -3405,6 +3405,13 @@ describe("AcpAdapterV2", () => {
3405 yield* replay("agent_message_chunk", "a2", "done");
3406 yield* replay("user_message_chunk", "u2", "\n");
3407 yield* replay("user_message_chunk", "u2", "\tone");
3408 + yield* sessionUpdateHandler({
3409 + sessionId: "mock-child-session",
3410 + update: {
3411 + sessionUpdate: "agent_message_chunk",
3412 + content: { type: "text", text: "Child private work" },
3413 + },
3414 + });
3415 const second = yield* runtime.readThreadSnapshot({ providerThread });
3416
3417 const texts = (snapshot: typeof first) =>
@@ -4696,6 +4703,11 @@ describe("AcpAdapterV2", () => {
4703 const startupMethods = yield* pollProtocolMethods(protocolEvents);
4704 assert.include(startupMethods, "session/resume");
4705 assert.notInclude(startupMethods, "session/new");
4706 + const loaded = yield* runtime.readThreadSnapshot({ providerThread });
4707 + assert.deepEqual(
4708 + loaded.messages.map((message) => `${message.role}:${message.text}`),
4709 + ["user:replay"],
4710 + );
4711
4712 yield* runtime.resumeThread({ providerThread, modelSelection, runtimePolicy });
4713 assert.notInclude(yield* pollProtocolMethods(protocolEvents), "session/resume");
packages/provider-acp/src/server/adapter.ts
+35 -14
@@ -2063,6 +2063,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2063 : flavor.extractSubagentUpdate?.(tool);
2064 let turnAttached = yield* Deferred.make<boolean>();
2065 const activeSessionId = yield* Ref.make<string | null>(null);
2066 + // session/load replays history before activeSessionId names the loaded session.
2067 + const loadingSessionId = yield* Ref.make<string | null>(null);
2068 const contextUsageBySessionId = yield* Ref.make(
2069 new Map<string, ThreadTokenUsageSnapshot>(),
2070 );
@@ -3935,7 +3937,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3937 }
3938 return true;
3939 }
3938 - if (notification.sessionId === root) return false;
3940 + if (
3941 + notification.sessionId === root ||
3942 + notification.sessionId === (yield* Ref.get(loadingSessionId))
3943 + )
3944 + return false;
3945 if (yield* Ref.get(stoppedRunQuarantine)) return true;
3946 const update = notification.update;
3947 if (update.sessionUpdate === "state_update") {
@@ -5524,6 +5530,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5530 if (wakeOutcome.stopProcessing) {
5531 return;
5532 }
5533 + if (
5534 + notification.sessionId !==
5535 + ((yield* Ref.get(loadingSessionId)) ?? (yield* Ref.get(activeSessionId)))
5536 + )
5537 + return;
5538 if (
5539 update.sessionUpdate === "user_message_chunk" ||
5540 update.sessionUpdate === "agent_message_chunk" ||
@@ -7289,12 +7300,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7300 threadId: ThreadId | null,
7301 resumeSessionId?: string,
7302 ) {
7303 + if (resumeSessionId !== undefined) yield* Ref.set(loadingSessionId, resumeSessionId);
7304 const startup = Effect.gen(function* () {
7305 yield* spawnAcpRuntime(threadId, resumeSessionId);
7306 yield* wireAcpRuntimeHandlers(runtime, yield* Ref.get(runtimeCallbackGeneration));
7307 return yield* runtime.start();
7308 });
7297 - return yield* flavor.withRuntimeStartup?.(startup) ?? startup;
7309 + return yield* (flavor.withRuntimeStartup?.(startup) ?? startup).pipe(
7310 + Effect.onError(() => Ref.set(loadingSessionId, null)),
7311 + );
7312 });
7313
7314 const restartAcpRuntime = Effect.fnUntraced(function* (threadId: ThreadId | null) {
@@ -7537,6 +7551,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7551 return yield* startAcpRuntime(input.threadId);
7552 });
7553 yield* Ref.set(activeSessionId, started.sessionId);
7554 + yield* Ref.set(loadingSessionId, null);
7555 yield* setActiveSessionSetup(started);
7556 rememberTerminalEnvironment(started.sessionId, yield* readMcpContext(input.threadId));
7557 yield* runtime.drainEvents;
@@ -7568,14 +7583,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7583 const mcpContext = yield* readMcpContext(threadId);
7584 const activationOptions = acpMcpActivation(mcpContext);
7585 prepareTerminalEnvironment(mcpContext, sessionId);
7571 - const activated = canLoadSession
7572 - ? yield* runtime.loadSession(sessionId, activationOptions)
7573 - : canResumeSession
7574 - ? yield* runtime.resumeSession(sessionId, activationOptions)
7575 - : yield* new ProviderAdapter.ProviderAdapterProtocolError({
7576 - driver,
7577 - detail: `ACP driver cannot load or resume session ${sessionId}`,
7578 - });
7586 + yield* Ref.set(loadingSessionId, sessionId);
7587 + const activated = yield* Effect.gen(function* () {
7588 + return canLoadSession
7589 + ? yield* runtime.loadSession(sessionId, activationOptions)
7590 + : canResumeSession
7591 + ? yield* runtime.resumeSession(sessionId, activationOptions)
7592 + : yield* new ProviderAdapter.ProviderAdapterProtocolError({
7593 + driver,
7594 + detail: `ACP driver cannot load or resume session ${sessionId}`,
7595 + });
7596 + }).pipe(Effect.onError(() => Ref.set(loadingSessionId, null)));
7597 rememberTerminalEnvironment(activated.sessionId, yield* readMcpContext(threadId));
7598 return activated;
7599 });
@@ -8517,6 +8535,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8535 if (needsSessionActivation) {
8536 const activated = yield* activateSession(requestedSessionId, turnInput.threadId);
8537 yield* Ref.set(activeSessionId, activated.sessionId);
8538 + yield* Ref.set(loadingSessionId, null);
8539 yield* setActiveSessionSetup(activated);
8540 yield* configureSession(activated, turnInput.modelSelection, turnInput.runtimePolicy);
8541 yield* Ref.set(activeSelection, turnInput.modelSelection);
@@ -9237,6 +9256,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9256 threadInput.providerThread.appThreadId,
9257 ).pipe(Effect.tapError(() => restorePreviousItemIdentity));
9258 yield* Ref.set(activeSessionId, activated.sessionId);
9259 + yield* Ref.set(loadingSessionId, null);
9260 yield* setActiveSessionSetup(activated);
9261 const nextSelection = threadInput.modelSelection ?? input.modelSelection;
9262 const nextRuntimePolicy = threadInput.runtimePolicy ?? input.runtimePolicy;
@@ -9749,15 +9769,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
9769 snapshotInput.providerThread.appThreadId,
9770 );
9771 prepareTerminalEnvironment(mcpContext, sessionId);
9752 - const activated = yield* runtime.loadSession(
9753 - sessionId,
9754 - acpMcpActivation(mcpContext),
9755 - );
9772 + yield* Ref.set(loadingSessionId, sessionId);
9773 + const activated = yield* runtime
9774 + .loadSession(sessionId, acpMcpActivation(mcpContext))
9775 + .pipe(Effect.onError(() => Ref.set(loadingSessionId, null)));
9776 rememberTerminalEnvironment(
9777 activated.sessionId,
9778 yield* readMcpContext(snapshotInput.providerThread.appThreadId),
9779 );
9780 yield* Ref.set(activeSessionId, activated.sessionId);
9781 + yield* Ref.set(loadingSessionId, null);
9782 yield* setActiveSessionSetup(activated);
9783 yield* Ref.set(activeSelection, null);
9784 yield* Ref.set(activeInteractionMode, null);