Skip to content

Commit eb8b52d

Browse files
committed
fix(fx): preserve real tool failures during Stop
1 parent fc336db commit eb8b52d

9 files changed

Lines changed: 194 additions & 7 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,27 @@ const program = Effect.gen(function* () {
12901290
status: "in_progress",
12911291
},
12921292
});
1293+
yield* agent.client.sessionUpdate({
1294+
sessionId: requestedSessionId,
1295+
update: {
1296+
sessionUpdate: "tool_call_update",
1297+
toolCallId: "native-failed-tool",
1298+
title: "Failing command",
1299+
kind: "execute",
1300+
status: "in_progress",
1301+
},
1302+
});
12931303
yield* Deferred.await(nativeCancelRequested);
12941304
yield* Deferred.await(nativeCancelRelease);
1305+
yield* agent.client.sessionUpdate({
1306+
sessionId: requestedSessionId,
1307+
update: {
1308+
sessionUpdate: "tool_call_update",
1309+
toolCallId: "native-failed-tool",
1310+
status: "failed",
1311+
content: [{ type: "content", content: { type: "text", text: "Permission denied" } }],
1312+
},
1313+
});
12951314
yield* agent.client.sessionUpdate({
12961315
sessionId: requestedSessionId,
12971316
update: {

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5332,6 +5332,11 @@ describe("AcpAdapterV2", () => {
53325332
flavor: {
53335333
driver: ACP_TEST_DRIVER,
53345334
capabilities: AcpProviderCapabilitiesV2,
5335+
isCancelledToolCall: Schema.is(
5336+
Schema.Struct({
5337+
update: Schema.Struct({ toolCallId: Schema.Literal("native-cancel-tool") }),
5338+
}),
5339+
),
53355340
makeRuntime: makeMockRuntime({
53365341
childProcessSpawner,
53375342
mockAgentPath: yield* path.fromFileUrl(
@@ -5423,6 +5428,19 @@ describe("AcpAdapterV2", () => {
54235428
.at(-1);
54245429
assert.equal(command?.status, "interrupted");
54255430
assert.include(command?.type === "command_execution" ? command.output : "", "exit 130");
5431+
const failure = untilTerminal
5432+
.flatMap((event) =>
5433+
event.type === "turn_item.updated" &&
5434+
event.turnItem.nativeItemRef?.nativeId === "native-failed-tool"
5435+
? [event.turnItem]
5436+
: [],
5437+
)
5438+
.at(-1);
5439+
assert.equal(failure?.status, "failed");
5440+
assert.include(
5441+
failure?.type === "command_execution" ? failure.output : "",
5442+
"Permission denied",
5443+
);
54265444
yield* runtime.startTurn(
54275445
makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: 2 }),
54285446
);

‎packages/effect-acp/src/protocol.test.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
9292
const notifications =
9393
yield* Deferred.make<ReadonlyArray<AcpProtocol.AcpIncomingNotification>>();
9494
yield* transport.incoming.pipe(
95-
Stream.take(2),
95+
Stream.take(3),
9696
Stream.runCollect,
9797
Effect.flatMap((notificationChunk) => Deferred.succeed(notifications, notificationChunk)),
9898
Effect.forkScoped,
@@ -133,18 +133,48 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
133133
},
134134
});
135135

136+
const commandResult = {
137+
exit_code: null,
138+
signal: null,
139+
timed_out: false,
140+
termination_indeterminate: true,
141+
};
142+
const cancelledToolFrame = encoder.encode(
143+
`${encodeUnknownJsonString({
144+
jsonrpc: "2.0",
145+
method: "session/update",
146+
params: {
147+
sessionId: "session-1",
148+
update: {
149+
sessionUpdate: "tool_call_update",
150+
toolCallId: "cancelled-tool",
151+
status: "failed",
152+
command_result: commandResult,
153+
},
154+
},
155+
})}\n`,
156+
);
136157
// Frames split across reads, with one read ending one frame and starting the next.
137-
const wire = new Uint8Array([...updateFrame, ...completionFrame]);
158+
const wire = new Uint8Array([...updateFrame, ...completionFrame, ...cancelledToolFrame]);
138159
const splits = [10, updateFrame.length + 10, wire.length];
139160
let start = 0;
140161
for (const end of splits) {
141162
yield* Queue.offer(input, wire.subarray(start, end));
142163
start = end;
143164
}
144165

145-
const [update, completion] = yield* Deferred.await(notifications);
166+
const [update, completion, cancelledTool] = yield* Deferred.await(notifications);
146167
assert.equal(update?._tag, "SessionUpdate");
147168
assert.equal(completion?._tag, "ElicitationComplete");
169+
assert.deepEqual(cancelledTool?.params, {
170+
sessionId: "session-1",
171+
update: {
172+
sessionUpdate: "tool_call_update",
173+
toolCallId: "cancelled-tool",
174+
status: "failed",
175+
command_result: commandResult,
176+
},
177+
});
148178
}),
149179
);
150180

‎packages/effect-acp/src/protocol.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ const decodeSessionUpdate = Schema.decodeUnknownEffect(
153153
_meta: AcpSchema.UpdateSessionNotification.fields._meta,
154154
}),
155155
}),
156+
// fx reports command cancellation outside ACP's standard tool fields.
157+
Schema.Struct({
158+
...AcpSchemaV1.SessionNotification.fields,
159+
update: Schema.Struct({
160+
...AcpSchemaV1.ToolCallUpdate.fields,
161+
sessionUpdate: Schema.Literals(["tool_call", "tool_call_update"]),
162+
command_result: Schema.Json,
163+
}),
164+
}),
156165
AcpSchema.UpdateSessionNotification,
157166
AcpSchemaV1.SessionNotification,
158167
]),

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ export interface AcpAdapterV2Flavor {
402402
) => AcpAdapterV2SubagentUpdate | undefined;
403403
readonly isProvisionalSubagentTool?: (toolCall: AcpToolCallState) => boolean;
404404
readonly normalizeToolCall?: (toolCall: AcpToolCallState) => AcpToolCallState;
405+
readonly isCancelledToolCall?: (payload: unknown) => boolean;
405406
readonly extractProposedPlanMarkdown?: (toolCall: AcpToolCallState) => string | undefined;
406407
readonly extractBackgroundTaskId?: (toolCall: AcpToolCallState) => string | undefined;
407408
/** Distinguishes commands from monitors when lifecycle notifications omit their kind. */
@@ -3511,6 +3512,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
35113512
context: ActiveAcpTurn,
35123513
incoming: AcpToolCallState,
35133514
projectedStatus?: ProjectedToolStatus,
3515+
rawPayload?: unknown,
35143516
) => Effect.Effect<void> = () => Effect.void;
35153517

35163518
const markAwaitingBackgroundHydration =
@@ -3563,6 +3565,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
35633565
context: ActiveAcpTurn,
35643566
incoming: AcpToolCallState,
35653567
projectedStatus?: ProjectedToolStatus,
3568+
rawPayload?: unknown,
35663569
) {
35673570
if (context.assistant.current?.sourceMessageId == null)
35683571
yield* closeTextStream(context, "assistant");
@@ -3603,11 +3606,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
36033606
? toolCall
36043607
: { ...toolCall, backgroundCommandTaskId },
36053608
);
3606-
// fx 0.0.15 fails the tools a Stop cut off; they read as interrupted, like ones left open.
3609+
// A failure racing Stop needs the provider's cancellation signal before relabeling it.
36073610
const previousStatus = previous === undefined ? undefined : toolStatus(previous.status);
36083611
if (
36093612
context.interrupted &&
36103613
toolCall.status === "failed" &&
3614+
flavor.isCancelledToolCall?.(rawPayload) === true &&
36113615
(previousStatus === "pending" ||
36123616
previousStatus === "running" ||
36133617
previousStatus === "waiting")
@@ -5275,7 +5279,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
52755279
const parsed = parseSessionUpdateEvent(notification);
52765280
for (const event of parsed.events) {
52775281
if (event._tag === "ToolCallUpdated") {
5278-
yield* emitTool(context, event.toolCall);
5282+
yield* emitTool(context, event.toolCall, undefined, event.rawPayload);
52795283
} else if (event._tag === "PlanUpdated") {
52805284
yield* emitPlan(context, event.payload);
52815285
} else if (event._tag === "NoticeReceived") {

‎packages/provider-fx/src/server/acpSupport.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type * as EffectAcpSchema from "effect-acp/compat";
33

44
import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/ProviderAdapter";
55

6-
import { fxPermissionDisposition } from "./acpSupport.ts";
6+
import { fxPermissionDisposition, fxToolCallWasCancelled } from "./acpSupport.ts";
77

88
const editRequest = {
99
sessionId: "fx-session-01",
@@ -31,3 +31,65 @@ describe("fx permission prompts", () => {
3131
);
3232
});
3333
});
34+
35+
describe("fx cancellation failures", () => {
36+
it.each([
37+
{ content: "Cancelled", cancelled: true },
38+
{ content: "Cancelled.", cancelled: true },
39+
{ content: '{"error":{"details":{"error":"Cancelled"}}}', cancelled: true },
40+
{ content: '{"error":{"details":{"error":"Permission denied"}}}', cancelled: false },
41+
{ content: "Command failed", cancelled: false },
42+
{ content: "", cancelled: false },
43+
])("recognizes $content as cancellation: $cancelled", ({ content, cancelled }) => {
44+
assert.equal(
45+
fxToolCallWasCancelled({ update: { content: [{ content: { text: content } }] } }),
46+
cancelled,
47+
);
48+
});
49+
50+
it.each([
51+
{
52+
exit_code: null,
53+
signal: null,
54+
timed_out: false,
55+
termination_indeterminate: true,
56+
content: "",
57+
cancelled: true,
58+
},
59+
{
60+
exit_code: null,
61+
signal: null,
62+
timed_out: false,
63+
termination_indeterminate: true,
64+
content:
65+
'{"error":{"type":"tool_execution_failed","message":"Command started, but its final process status could not be confirmed",',
66+
cancelled: false,
67+
},
68+
{
69+
exit_code: 1,
70+
signal: null,
71+
timed_out: false,
72+
termination_indeterminate: false,
73+
content: "",
74+
cancelled: false,
75+
},
76+
{
77+
exit_code: null,
78+
signal: null,
79+
timed_out: true,
80+
termination_indeterminate: true,
81+
content: "",
82+
cancelled: false,
83+
},
84+
])(
85+
"recognizes a command result with exit $exit_code, timeout $timed_out",
86+
({ cancelled, content, ...commandResult }) => {
87+
assert.equal(
88+
fxToolCallWasCancelled({
89+
update: { command_result: commandResult, content: [{ content: { text: content } }] },
90+
}),
91+
cancelled,
92+
);
93+
},
94+
);
95+
});

‎packages/provider-fx/src/server/acpSupport.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
1010
import type * as Crypto from "effect/Crypto";
1111
import * as Effect from "effect/Effect";
1212
import * as Layer from "effect/Layer";
13+
import * as Option from "effect/Option";
14+
import * as Schema from "effect/Schema";
1315
import type * as Scope from "effect/Scope";
1416
import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1517
import * as EffectAcpErrors from "effect-acp/errors";
@@ -57,6 +59,47 @@ export const FX_SUPPORTED_RUNTIME_MODES = [
5759
/** fx 0.0.13 answers a cancelled prompt with `stopReason: cancelled`. */
5860
export const FX_ACP_CANCEL_BEHAVIOR = "wait-for-prompt" as const;
5961

62+
const isCancelledFxCommand = Schema.is(
63+
Schema.Struct({
64+
update: Schema.Struct({
65+
command_result: Schema.Struct({
66+
exit_code: Schema.Null,
67+
signal: Schema.Null,
68+
timed_out: Schema.Literal(false),
69+
termination_indeterminate: Schema.Literal(true),
70+
}),
71+
}),
72+
}),
73+
);
74+
const decodeFxToolText = Schema.decodeUnknownOption(
75+
Schema.Struct({
76+
update: Schema.Struct({
77+
content: Schema.Array(Schema.Struct({ content: Schema.Struct({ text: Schema.String }) })),
78+
}),
79+
}),
80+
);
81+
const decodeFxCancelledError = Schema.decodeUnknownOption(
82+
Schema.fromJsonString(
83+
Schema.Struct({
84+
error: Schema.Struct({ details: Schema.Struct({ error: Schema.Literal("Cancelled") }) }),
85+
}),
86+
),
87+
);
88+
89+
export function fxToolCallWasCancelled(payload: unknown): boolean {
90+
const update = decodeFxToolText(payload);
91+
const texts = Option.isSome(update)
92+
? update.value.update.content.map(({ content }) => content.text)
93+
: [];
94+
if (
95+
texts.some((text) => /^Cancelled\.?$/.test(text) || Option.isSome(decodeFxCancelledError(text)))
96+
) {
97+
return true;
98+
}
99+
// fx also uses indeterminate status for real failures, whose error JSON can be clipped.
100+
return isCancelledFxCommand(payload) && texts.every((text) => !/^\s*\{\s*"error"\s*:/.test(text));
101+
}
102+
60103
/**
61104
* Supervised runs fx's `ask` mode and Auto its automatic review. An explicit
62105
* approval or sandbox policy must not launch automatic review, which would

‎packages/provider-fx/src/server/adapter.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
applyFxModelSelection,
2121
FX_DRIVER_KIND,
2222
fxPermissionDisposition,
23+
fxToolCallWasCancelled,
2324
fxSessionModeForPolicy,
2425
makeFxAcpRuntime,
2526
withFxSessionMode,
@@ -202,6 +203,7 @@ function makeFxAcpAdapterFlavor(options: FxAdapterV2Options): AcpAdapterV2Flavor
202203
sessionModeForPolicy: fxSessionModeForPolicy,
203204
permissionDisposition: fxPermissionDisposition,
204205
extractSubagentUpdate: extractFxSubagentUpdate,
206+
isCancelledToolCall: fxToolCallWasCancelled,
205207
...(options.assertComplete === undefined ? {} : { assertComplete: options.assertComplete }),
206208
};
207209
}

‎packages/provider-fx/src/server/maintenance.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @effect-diagnostics nodeBuiltinImport:off
1+
// @effect-diagnostics nodeBuiltinImport:off -- fx's macOS upgrade gate needs the host kernel release.
22
import * as NodeOS from "node:os";
33
import * as Effect from "effect/Effect";
44
import * as HttpClient from "effect/http/HttpClient";

0 commit comments

Comments
 (0)