Skip to content

Commit 336badc

Browse files
committed
fix(fx): retain errors across mixed tool content
1 parent 30e993f commit 336badc

2 files changed

Lines changed: 68 additions & 17 deletions

File tree

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

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ describe("fx permission prompts", () => {
3333
});
3434

3535
describe("fx cancellation failures", () => {
36+
const diff = { type: "diff", path: "notes.txt", oldText: "old", newText: "new" };
37+
const terminal = { type: "terminal", terminalId: "fx-terminal" };
38+
3639
it.each([
3740
{ content: ["Cancelled"], cancelled: true },
3841
{ content: ["Cancelled."], cancelled: true },
3942
{ content: ['{"error":{"details":{"error":"Cancelled"}}}'], cancelled: true },
4043
{ content: ['{"error":{"details":{"error":"Permission denied"}}}'], cancelled: false },
4144
{ content: ["Command failed"], cancelled: false },
4245
{ content: [""], cancelled: false },
46+
{ content: ["Cancelled"], followingItems: [diff], cancelled: true },
47+
{ content: ["Cancelled"], precedingItems: [diff], cancelled: false },
4348
{
4449
content: ['{"error":{"details":{"error":"Permission denied"}}}', "Cancelled"],
4550
cancelled: false,
@@ -52,14 +57,23 @@ describe("fx cancellation failures", () => {
5257
content: ["Cancelled", '{"error":{"details":{"error":"Permission denied"}}}'],
5358
cancelled: true,
5459
},
55-
])("recognizes $content as cancellation: $cancelled", ({ content, cancelled }) => {
56-
assert.equal(
57-
fxToolCallWasCancelled({
58-
update: { content: content.map((text) => ({ content: { text } })) },
59-
}),
60-
cancelled,
61-
);
62-
});
60+
])(
61+
"recognizes $content as cancellation: $cancelled",
62+
({ content, cancelled, precedingItems = [], followingItems = [] }) => {
63+
assert.equal(
64+
fxToolCallWasCancelled({
65+
update: {
66+
content: [
67+
...precedingItems,
68+
...content.map((text) => ({ content: { text } })),
69+
...followingItems,
70+
],
71+
},
72+
}),
73+
cancelled,
74+
);
75+
},
76+
);
6377

6478
it.each([
6579
{
@@ -80,6 +94,34 @@ describe("fx cancellation failures", () => {
8094
followingContent: ["Cancelled"],
8195
cancelled: false,
8296
},
97+
{
98+
exit_code: null,
99+
signal: null,
100+
timed_out: false,
101+
termination_indeterminate: true,
102+
content: '{"error":{"message":"Permission denied","details":{"error":"Permission denied"}}}',
103+
followingItems: [terminal],
104+
cancelled: false,
105+
},
106+
{
107+
exit_code: null,
108+
signal: null,
109+
timed_out: false,
110+
termination_indeterminate: true,
111+
content: '{"error":{"type":"tool_execution_failed","message":"clipped",',
112+
followingItems: [diff],
113+
cancelled: false,
114+
},
115+
{
116+
exit_code: null,
117+
signal: null,
118+
timed_out: false,
119+
termination_indeterminate: true,
120+
content: "Still processing",
121+
followingContent: ['{"error":{"message":"Permission denied"}}'],
122+
followingItems: [diff],
123+
cancelled: false,
124+
},
83125
{
84126
exit_code: 1,
85127
signal: null,
@@ -98,12 +140,15 @@ describe("fx cancellation failures", () => {
98140
},
99141
])(
100142
"recognizes a command result with exit $exit_code, timeout $timed_out",
101-
({ cancelled, content, followingContent = [], ...commandResult }) => {
143+
({ cancelled, content, followingContent = [], followingItems = [], ...commandResult }) => {
102144
assert.equal(
103145
fxToolCallWasCancelled({
104146
update: {
105147
command_result: commandResult,
106-
content: [content, ...followingContent].map((text) => ({ content: { text } })),
148+
content: [
149+
...[content, ...followingContent].map((text) => ({ content: { text } })),
150+
...followingItems,
151+
],
107152
},
108153
}),
109154
cancelled,

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ const isCancelledFxCommand = Schema.is(
7171
}),
7272
}),
7373
);
74-
const decodeFxToolText = Schema.decodeUnknownOption(
74+
const decodeFxToolUpdate = Schema.decodeUnknownOption(
7575
Schema.Struct({
76-
update: Schema.Struct({
77-
content: Schema.Array(Schema.Struct({ content: Schema.Struct({ text: Schema.String }) })),
78-
}),
76+
update: Schema.Struct({ content: Schema.Array(Schema.Unknown) }),
7977
}),
8078
);
79+
const decodeFxContentText = Schema.decodeUnknownOption(
80+
Schema.Struct({ content: Schema.Struct({ text: Schema.String }) }),
81+
);
8182
const decodeFxCancelledError = Schema.decodeUnknownOption(
8283
Schema.fromJsonString(
8384
Schema.Struct({
@@ -87,9 +88,11 @@ const decodeFxCancelledError = Schema.decodeUnknownOption(
8788
);
8889

8990
export function fxToolCallWasCancelled(payload: unknown): boolean {
90-
const update = decodeFxToolText(payload);
91+
const update = decodeFxToolUpdate(payload);
9192
const texts = Option.isSome(update)
92-
? update.value.update.content.map(({ content }) => content.text)
93+
? update.value.update.content.map(
94+
(item) => Option.getOrUndefined(decodeFxContentText(item))?.content.text,
95+
)
9396
: [];
9497
const firstText = texts[0];
9598
if (
@@ -99,7 +102,10 @@ export function fxToolCallWasCancelled(payload: unknown): boolean {
99102
return true;
100103
}
101104
// fx also uses indeterminate status for real failures, whose error JSON can be clipped.
102-
return isCancelledFxCommand(payload) && texts.every((text) => !/^\s*\{\s*"error"\s*:/.test(text));
105+
return (
106+
isCancelledFxCommand(payload) &&
107+
texts.every((text) => text === undefined || !/^\s*\{\s*"error"\s*:/.test(text))
108+
);
103109
}
104110

105111
/**

0 commit comments

Comments
 (0)