@devries-cameron20 / t3code Mirror · read-only / commits / 0351dbb092
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, 14:45 UTC

fix(acp): refused and limit-stopped turns no longer show as completed (#28797)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 14, 2026 at 08:28 UTC 0351dbb09204c464bc556c2cfa1b48ceab0d3b21
4 files changed +109 -17
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+1 -6
@@ -2456,12 +2456,7 @@ describe("AcpAdapterV2", () => {
2456 },
2457 stopReason === "error",
2458 );
2459 - const expectedStatus =
2460 - stopReason === "error"
2461 - ? "failed"
2462 - : stopReason === "cancelled"
2463 - ? "cancelled"
2464 - : "completed";
2459 + const expectedStatus = stopReason === "cancelled" ? "cancelled" : "failed";
2460 yield* reportNextPromptStopped(stopReason);
2461 assertReinjectedInstructions(yield* runTurn("Start work.", expectedStatus), "Start work.");
2462 assertReinjectedInstructions(
apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts
+59
@@ -2755,6 +2755,65 @@ describe("AcpRegistryAdapterV2", () => {
2755 }).pipe(Effect.provide(layerTest)),
2756 );
2757
2758 + it.effect.each([
2759 + { stopReason: "refusal", childUpdate: "state_update" },
2760 + { stopReason: "max_tokens", childUpdate: "subagent_update" },
2761 + ])(
2762 + "fails Claude's turn and child and closes its running tool when they stop with $stopReason via $childUpdate",
2763 + ({ stopReason, childUpdate }) =>
2764 + Effect.gen(function* () {
2765 + const { update, idle, prompt } = claudeFrames;
2766 + const session = yield* openClaudeReplaySession([
2767 + ...claudeFrames.open,
2768 + ...prompt(1),
2769 + ...claudeChild,
2770 + update(CLAUDE_ROOT_SESSION, {
2771 + sessionUpdate: "tool_call_update",
2772 + toolCallId: "root-tool",
2773 + name: "Bash",
2774 + title: "Read package",
2775 + kind: "execute",
2776 + status: "in_progress",
2777 + rawInput: { command: "cat package.json" },
2778 + }),
2779 + update("child-1", { sessionUpdate: "state_update", state: "running" }),
2780 + childUpdate === "state_update"
2781 + ? update("child-1", { sessionUpdate: "state_update", state: "idle", stopReason })
2782 + : update(CLAUDE_ROOT_SESSION, {
2783 + sessionUpdate: "subagent_update",
2784 + sessionId: "child-1",
2785 + state: { state: "idle", stopReason },
2786 + }),
2787 + idle(stopReason),
2788 + ]);
2789 + yield* session.runtime.startTurn(session.turnInput(1));
2790 + const events = yield* session.until((event) => event.type === "turn.terminal");
2791 + const terminal = events.at(-1)!;
2792 + if (terminal.type !== "turn.terminal") throw new Error("Missing root terminal");
2793 + assert.equal(terminal.status, "failed");
2794 + assert.equal(terminal.failure?.code, stopReason);
2795 + const child = events.findLast((event) => event.type === "subagent.updated");
2796 + if (child?.type !== "subagent.updated") throw new Error("Missing child update");
2797 + assert.equal(child.subagent.status, "failed");
2798 + assert.isNotNull(child.subagent.result, "the child shows why it stopped");
2799 + assert.isFalse(
2800 + events.some(
2801 + (event) =>
2802 + event.type === "message.updated" &&
2803 + event.message.threadId === child.subagent.childThreadId &&
2804 + event.message.role === "assistant",
2805 + ),
2806 + "the child's conversation has no reply it didn't send",
2807 + );
2808 + const tool = events.findLast(
2809 + (event) =>
2810 + event.type === "turn_item.updated" && event.turnItem.type === "command_execution",
2811 + );
2812 + if (tool?.type !== "turn_item.updated") throw new Error("Missing tool update");
2813 + assert.equal(tool.turnItem.status, "failed");
2814 + }).pipe(Effect.provide(layerTest), Effect.scoped),
2815 + );
2816 +
2817 it.effect("keeps Claude activity after a refused Stop and closes it on retry", () =>
2818 Effect.gen(function* () {
2819 const gate = makeProviderReplayGate(["close-response", "queued-close-response"]);
packages/provider-acp-registry/src/server/claudeAcp.ts
+7 -5
@@ -2,9 +2,10 @@ import * as Option from "effect/Option";
2 import * as Schema from "effect/Schema";
3 import type * as AcpSchema from "effect-acp/compat";
4 import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel";
5 -import type {
6 - AcpAdapterV2SubagentUpdate,
7 - AcpSubagentSessionEvent,
5 +import {
6 + acpUnfinishedStopMessage,
7 + type AcpAdapterV2SubagentUpdate,
8 + type AcpSubagentSessionEvent,
9 } from "@t3tools/provider-acp/server/adapter";
10
11 const decodeAgentInput = Schema.decodeUnknownOption(
@@ -86,6 +87,7 @@ export function extractClaudeSubagentSessionEvent(
87 const child = decodeChildState(update.raw);
88 if (Option.isNone(child)) return;
89 const { sessionId, state } = child.value;
90 + const unfinishedMessage = acpUnfinishedStopMessage(state.stopReason);
91 return {
92 type: "state",
93 childSessionId: sessionId,
@@ -93,12 +95,12 @@ export function extractClaudeSubagentSessionEvent(
95 status:
96 state.state === "running" || state.state === "requires_action"
97 ? "running"
96 - : state.stopReason === "error"
98 + : state.stopReason === "error" || unfinishedMessage !== undefined
99 ? "failed"
100 : state.stopReason === "cancelled" || state.state === "unknown"
101 ? "cancelled"
102 : "completed",
101 - result: state.error?.message ?? null,
103 + result: state.error?.message ?? unfinishedMessage ?? null,
104 };
105 }
106 if (update.originalSessionUpdate === "session_message") {
packages/provider-acp/src/server/adapter.ts
+42 -6
@@ -1021,6 +1021,22 @@ function makeProviderThread(input: {
1021 };
1022 }
1023
1024 +/** Stop reasons where the agent ended without finishing. ACP asks clients to show refusals. */
1025 +export function acpUnfinishedStopMessage(
1026 + stopReason: EffectAcpSchema.StopReason | null | undefined,
1027 +): string | undefined {
1028 + switch (stopReason) {
1029 + case "refusal":
1030 + return "The agent refused this request. The agent's next turn won't include this message or its reply.";
1031 + case "max_tokens":
1032 + return "The agent stopped because its reply reached the output token limit.";
1033 + case "max_turn_requests":
1034 + return "The agent stopped because it reached its request limit for this turn.";
1035 + default:
1036 + return undefined;
1037 + }
1038 +}
1039 +
1040 function appendToolContentText(content: unknown, text: string): unknown[] {
1041 const next: unknown[] = Array.isArray(content) ? [...content] : [];
1042 const last = unknownRecord(next.at(-1));
@@ -3441,8 +3457,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3457 );
3458 }
3459
3460 + // A failed child's result explains the failure; it is not the child's reply.
3461 if (
3462 taskStatus !== "running" &&
3463 + reported.status !== "failed" &&
3464 subagent.assistantText.length === 0 &&
3465 update.result !== null
3466 ) {
@@ -3812,16 +3830,18 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3830 const update = notification.update;
3831 if (update.sessionUpdate === "state_update") {
3832 const child = nativeChild(notification.sessionId);
3833 + const unfinishedMessage =
3834 + update.state === "idle" ? acpUnfinishedStopMessage(update.stopReason) : undefined;
3835 setNativeChildState(
3836 child,
3837 update.state !== "idle"
3838 ? "running"
3819 - : update.stopReason === "error"
3839 + : update.stopReason === "error" || unfinishedMessage !== undefined
3840 ? "failed"
3841 : update.stopReason === "cancelled"
3842 ? "cancelled"
3843 : "completed",
3824 - update.state === "idle" ? (update.error?.message ?? null) : null,
3844 + update.state === "idle" ? (update.error?.message ?? unfinishedMessage ?? null) : null,
3845 );
3846 }
3847 if (context !== null) yield* syncNativeChildren(context);
@@ -5027,6 +5047,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5047 class: "provider_error",
5048 });
5049
5050 + const stopReasonFailure = (
5051 + stopReason: EffectAcpSchema.StopReason | null | undefined,
5052 + error: EffectAcpSchema.PromptResponse["error"],
5053 + ) => {
5054 + if (stopReason === "error") return idleErrorFailure(error);
5055 + const message = acpUnfinishedStopMessage(stopReason);
5056 + return message === undefined
5057 + ? undefined
5058 + : makeProviderFailure({ message, code: stopReason, class: "provider_error" });
5059 + };
5060 +
5061 const settleIdleWork = Effect.fnUntraced(function* (
5062 context: ActiveAcpTurn,
5063 ): Effect.fn.Return<void> {
@@ -5043,7 +5074,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5074 if (work.stopReason === "cancelled" && pendingStopCloseTurn === context) {
5075 context.interrupted = true;
5076 }
5046 - const failure = work.stopReason === "error" ? idleErrorFailure(work.error) : undefined;
5077 + const failure = context.interrupted
5078 + ? undefined
5079 + : stopReasonFailure(work.stopReason, work.error);
5080 + if (failure !== undefined) yield* terminalizeOpenForegroundTools(context, "failed");
5081 yield* finalizeTurn(
5082 context,
5083 failure !== undefined
@@ -8671,8 +8705,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8705 });
8706 }
8707 if (context.finalized || followsAgentWorkState) return;
8674 - // ACP v2 reports a failed prompt as an idle `error`, not an RPC error.
8675 - const idleError = result.stopReason === "error" && !context.interrupted;
8708 + // ACP v2 reports a failed prompt through its idle stop reason, not an RPC error.
8709 const hiddenFailure =
8710 checkPromptOutcome === undefined ||
8711 context.interrupted ||
@@ -8684,7 +8717,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8717 yield* Ref.set(runtimeRestartRequired, true);
8718 }
8719 const failure =
8687 - hiddenFailure ?? (idleError ? idleErrorFailure(result.error) : undefined);
8720 + hiddenFailure ??
8721 + (context.interrupted
8722 + ? undefined
8723 + : stopReasonFailure(result.stopReason, result.error));
8724 const status =
8725 failure !== undefined
8726 ? "failed"