Skip to content

Commit dfc6143

Browse files
committed
fix(core): reject JSON arrays in the failed-tool input fallback
The object guard `value && typeof value === "object"` also admits arrays (`typeof [] === "object"`), so streamed text like `[1,2,3]` would be stored as the error input and sent to the provider as a non-object `tool_use.input`, which providers reject (anomalyco#56127). Add `!Array.isArray(value)` so only plain objects are kept; arrays fall back to `{}` like any other non-object JSON. Adds a regression test covering a JSON-array input.
1 parent 3c5c65d commit dfc6143

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/core/src/session/message-updater.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ export function update(adapter: Adapter, event: SessionEvent.DurableEvent) {
377377
? Option.match(decodeToolInput(match.state.input), {
378378
onNone: () => ({}),
379379
onSome: (value) =>
380-
value && typeof value === "object" ? (value as Record<string, unknown>) : {},
380+
value && typeof value === "object" && !Array.isArray(value)
381+
? (value as Record<string, unknown>)
382+
: {},
381383
})
382384
: match.state.input,
383385
...(event.data.content === undefined ? {} : { content: event.data.content }),

‎packages/core/test/session-projector.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,43 @@ describe("SessionProjector", () => {
18761876
}),
18771877
)
18781878

1879+
it.effect("falls back to empty input when streamed arguments are a JSON array", () =>
1880+
Effect.gen(function* () {
1881+
const db = yield* seedSession()
1882+
const assistantMessageID = SessionMessage.ID.make("msg_failed_array_input")
1883+
yield* db.insert(SessionMessageTable).values(assistantRow(assistantMessageID, 0)).run().pipe(Effect.orDie)
1884+
const service = yield* Bus.Service
1885+
yield* service.publish(SessionEvent.Tool.Input.Started, {
1886+
sessionID,
1887+
assistantMessageID,
1888+
id: "call_list",
1889+
name: "list",
1890+
})
1891+
yield* service.publish(SessionEvent.Tool.Input.Ended, {
1892+
sessionID,
1893+
assistantMessageID,
1894+
id: "call_list",
1895+
text: "[1,2,3]",
1896+
})
1897+
yield* service.publish(SessionEvent.Tool.Failed, {
1898+
sessionID,
1899+
assistantMessageID,
1900+
id: "call_list",
1901+
error: { type: "aborted", message: "Tool execution interrupted" },
1902+
executed: false,
1903+
})
1904+
const row = yield* db
1905+
.select()
1906+
.from(SessionMessageTable)
1907+
.where(eq(SessionMessageTable.id, assistantMessageID))
1908+
.get()
1909+
.pipe(Effect.orDie)
1910+
expect(row?.data).toMatchObject({
1911+
content: [{ id: "call_list", state: { status: "error", input: {} } }],
1912+
})
1913+
}),
1914+
)
1915+
18791916
it.effect("preserves already-decoded tool input when a running call fails", () =>
18801917
Effect.gen(function* () {
18811918
const db = yield* seedSession()

0 commit comments

Comments
 (0)