GeezerVison is showing github.com — fix(acp): refused and limit-stopped turns no longer show as completed… · pingdotgg/t3code@0351dbb
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2/Adapters provider-acp-registry/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +1-6Lines changed: 1 addition & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2456,12 +2456,7 @@ describe("AcpAdapterV2", () => { | 2456 | 2456 | }, | 2457 | 2457 | stopReason === "error", | 2458 | 2458 | ); | 2459 | | - const expectedStatus = | 2460 | | - stopReason === "error" | 2461 | | - ? "failed" | 2462 | | - : stopReason === "cancelled" | 2463 | | - ? "cancelled" | 2464 | | - : "completed"; | | 2459 | + const expectedStatus = stopReason === "cancelled" ? "cancelled" : "failed"; | 2465 | 2460 | yield* reportNextPromptStopped(stopReason); | 2466 | 2461 | assertReinjectedInstructions(yield* runTurn("Start work.", expectedStatus), "Start work."); | 2467 | 2462 | assertReinjectedInstructions( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2755,6 +2755,65 @@ describe("AcpRegistryAdapterV2", () => { | 2755 | 2755 | }).pipe(Effect.provide(layerTest)), | 2756 | 2756 | ); | 2757 | 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 | + | 2758 | 2817 | it.effect("keeps Claude activity after a refused Stop and closes it on retry", () => | 2759 | 2818 | Effect.gen(function* () { | 2760 | 2819 | const gate = makeProviderReplayGate(["close-response", "queued-close-response"]); | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-acp-registry/src/server/claudeAcp.ts +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,9 +2,10 @@ import * as Option from "effect/Option"; | 2 | 2 | import * as Schema from "effect/Schema"; | 3 | 3 | import type * as AcpSchema from "effect-acp/compat"; | 4 | 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, | 8 | 9 | } from "@t3tools/provider-acp/server/adapter"; | 9 | 10 |
| 10 | 11 | const decodeAgentInput = Schema.decodeUnknownOption( | @@ -86,19 +87,20 @@ export function extractClaudeSubagentSessionEvent( | 86 | 87 | const child = decodeChildState(update.raw); | 87 | 88 | if (Option.isNone(child)) return; | 88 | 89 | const { sessionId, state } = child.value; | | 90 | + const unfinishedMessage = acpUnfinishedStopMessage(state.stopReason); | 89 | 91 | return { | 90 | 92 | type: "state", | 91 | 93 | childSessionId: sessionId, | 92 | 94 | parentSessionId: notification.sessionId, | 93 | 95 | status: | 94 | 96 | state.state === "running" || state.state === "requires_action" | 95 | 97 | ? "running" | 96 | | - : state.stopReason === "error" | | 98 | + : state.stopReason === "error" || unfinishedMessage !== undefined | 97 | 99 | ? "failed" | 98 | 100 | : state.stopReason === "cancelled" || state.state === "unknown" | 99 | 101 | ? "cancelled" | 100 | 102 | : "completed", | 101 | | - result: state.error?.message ?? null, | | 103 | + result: state.error?.message ?? unfinishedMessage ?? null, | 102 | 104 | }; | 103 | 105 | } | 104 | 106 | if (update.originalSessionUpdate === "session_message") { | |
Collapse file Copy file name to clipboardExpand all lines: packages/provider-acp/src/server/adapter.ts +42-6Lines changed: 42 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1021,6 +1021,22 @@ function makeProviderThread(input: { | 1021 | 1021 | }; | 1022 | 1022 | } | 1023 | 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 | + | 1024 | 1040 | function appendToolContentText(content: unknown, text: string): unknown[] { | 1025 | 1041 | const next: unknown[] = Array.isArray(content) ? [...content] : []; | 1026 | 1042 | const last = unknownRecord(next.at(-1)); | @@ -3441,8 +3457,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 3441 | 3457 | ); | 3442 | 3458 | } | 3443 | 3459 |
| | 3460 | + // A failed child's result explains the failure; it is not the child's reply. | 3444 | 3461 | if ( | 3445 | 3462 | taskStatus !== "running" && | | 3463 | + reported.status !== "failed" && | 3446 | 3464 | subagent.assistantText.length === 0 && | 3447 | 3465 | update.result !== null | 3448 | 3466 | ) { | @@ -3812,16 +3830,18 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 3812 | 3830 | const update = notification.update; | 3813 | 3831 | if (update.sessionUpdate === "state_update") { | 3814 | 3832 | const child = nativeChild(notification.sessionId); | | 3833 | + const unfinishedMessage = | | 3834 | + update.state === "idle" ? acpUnfinishedStopMessage(update.stopReason) : undefined; | 3815 | 3835 | setNativeChildState( | 3816 | 3836 | child, | 3817 | 3837 | update.state !== "idle" | 3818 | 3838 | ? "running" | 3819 | | - : update.stopReason === "error" | | 3839 | + : update.stopReason === "error" || unfinishedMessage !== undefined | 3820 | 3840 | ? "failed" | 3821 | 3841 | : update.stopReason === "cancelled" | 3822 | 3842 | ? "cancelled" | 3823 | 3843 | : "completed", | 3824 | | - update.state === "idle" ? (update.error?.message ?? null) : null, | | 3844 | + update.state === "idle" ? (update.error?.message ?? unfinishedMessage ?? null) : null, | 3825 | 3845 | ); | 3826 | 3846 | } | 3827 | 3847 | if (context !== null) yield* syncNativeChildren(context); | @@ -5027,6 +5047,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 5027 | 5047 | class: "provider_error", | 5028 | 5048 | }); | 5029 | 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 | + | 5030 | 5061 | const settleIdleWork = Effect.fnUntraced(function* ( | 5031 | 5062 | context: ActiveAcpTurn, | 5032 | 5063 | ): Effect.fn.Return<void> { | @@ -5043,7 +5074,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 5043 | 5074 | if (work.stopReason === "cancelled" && pendingStopCloseTurn === context) { | 5044 | 5075 | context.interrupted = true; | 5045 | 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"); | 5047 | 5081 | yield* finalizeTurn( | 5048 | 5082 | context, | 5049 | 5083 | failure !== undefined | @@ -8671,8 +8705,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 8671 | 8705 | }); | 8672 | 8706 | } | 8673 | 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. | 8676 | 8709 | const hiddenFailure = | 8677 | 8710 | checkPromptOutcome === undefined || | 8678 | 8711 | context.interrupted || | @@ -8684,7 +8717,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | 8684 | 8717 | yield* Ref.set(runtimeRestartRequired, true); | 8685 | 8718 | } | 8686 | 8719 | const failure = | 8687 | | - hiddenFailure ?? (idleError ? idleErrorFailure(result.error) : undefined); | | 8720 | + hiddenFailure ?? | | 8721 | + (context.interrupted | | 8722 | + ? undefined | | 8723 | + : stopReasonFailure(result.stopReason, result.error)); | 8688 | 8724 | const status = | 8689 | 8725 | failure !== undefined | 8690 | 8726 | ? "failed" | |
|