Skip to content

Commit abde6aa

Browse files
author
easonLiangWorldedtech
committed
fix(api): preserve APICallError statusCode as .status; drop superseded local-copy test blocks (Zoo-Code-Org#797)
- handleProviderError now falls back to the AI SDK APICallError statusCode so a real 429 reaches Task.backoffAndAnnounce's .status === 429 check (addressed maintainer review) - openai-compatible.spec.ts 429 test uses a real APICallError instead of a fabricated {status: 429} - removed two local-map-copy test blocks superseded by the real-Task integration tests (duplicate-tool-use-ids compound-key block; Task.streaming-tool-calls local-copy block) - PR description: corrected the Novita/Moonshot/Poe inheritance claim and documented the first-call-wins dedup rule
1 parent 50114a7 commit abde6aa

4 files changed

Lines changed: 27 additions & 284 deletions

File tree

‎src/api/providers/__tests__/openai-compatible.spec.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { Anthropic } from "@anthropic-ai/sdk"
2828
import { OpenAICompatibleHandler, OpenAICompatibleConfig } from "../openai-compatible"
2929
import type { ApiHandlerOptions } from "../../../shared/api"
3030
import type { ApiStreamChunk } from "../../../api/transform/stream"
31-
import type { LanguageModel } from "ai"
31+
import { APICallError, type LanguageModel } from "ai"
3232

3333
/** Error shape produced when the upstream API responds with a 4xx/5xx status. */
3434
type StatusedError = Error & { status: number }
@@ -224,9 +224,16 @@ describe("OpenAICompatibleHandler", () => {
224224
expect(toolChunks[2]).toMatchObject({ type: "tool_call_end", id: "tc_1" })
225225
})
226226

227-
// Test 1: createMessage() with mock 429 response → verify thrown error has .status === 429 and provider name in message
227+
// Test 1: createMessage() with a real AI SDK APICallError 429 (the AI SDK
228+
// exposes the HTTP status as statusCode, not status) → verify the wrapped error
229+
// surfaces .status === 429 and the provider name in its message.
228230
it("should throw error with .status 429 when API returns 429", async () => {
229-
const rateLimitError = Object.assign(new Error("Rate limited"), { status: 429 })
231+
const rateLimitError = new APICallError({
232+
message: "Rate limited",
233+
statusCode: 429,
234+
url: "https://test.invalid/v1/chat/completions",
235+
requestBodyValues: {},
236+
})
230237

231238
mockStreamText.mockReturnValue({
232239
// eslint-disable-next-line require-yield

‎src/api/providers/utils/error-handler.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function handleProviderError(
5656
name: error.name,
5757
stack: error.stack,
5858
status: anyErr.status,
59+
statusCode: anyErr.statusCode,
5960
})
6061

6162
let wrapped: Error
@@ -74,9 +75,12 @@ export function handleProviderError(
7475

7576
// Preserve HTTP status and structured details for retry/backoff + UI
7677
// These fields are used by Task.backoffAndAnnounce() and ChatRow/ErrorRow
77-
// to provide status-aware error messages and handling
78-
if (anyErr.status !== undefined) {
79-
;(wrapped as any).status = anyErr.status
78+
// to provide status-aware error messages and handling. AI SDK errors (e.g.,
79+
// APICallError) expose the HTTP status as `statusCode` instead of `status`,
80+
// so fall back to it: a real 429 must reach Task.backoffAndAnnounce's .status check.
81+
const preservedStatus = anyErr.status !== undefined ? anyErr.status : anyErr.statusCode
82+
if (preservedStatus !== undefined) {
83+
;(wrapped as any).status = preservedStatus
8084
}
8185
if (anyErr.errorDetails !== undefined) {
8286
;(wrapped as any).errorDetails = anyErr.errorDetails
@@ -96,10 +100,17 @@ export function handleProviderError(
96100
console.error(`[${providerName}] Non-Error exception:`, error)
97101
const wrapped = new Error(`${providerName} ${messagePrefix} error: ${String(error)}`)
98102

99-
// Also try to preserve status for non-Error exceptions (e.g., plain objects with status)
103+
// Also try to preserve status for non-Error exceptions (e.g., plain objects with
104+
// status or statusCode, matching the AI SDK APICallError shape)
100105
const anyErr = error as any
101-
if (typeof anyErr?.status === "number") {
102-
;(wrapped as any).status = anyErr.status
106+
const plainStatus =
107+
typeof anyErr?.status === "number"
108+
? anyErr.status
109+
: typeof anyErr?.statusCode === "number"
110+
? anyErr.statusCode
111+
: undefined
112+
if (plainStatus !== undefined) {
113+
;(wrapped as any).status = plainStatus
103114
}
104115

105116
return wrapped

‎src/core/task/__tests__/Task.streaming-tool-calls.spec.ts‎

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -797,147 +797,6 @@ describe("Task - Streaming Tool Call Handling", () => {
797797
})
798798
})
799799

800-
describe("Task.ts compound key dedup and streaming paths", () => {
801-
// Test-local structural stand-in for the Task.assistantMessageContent entries
802-
// this block exercises: partial tool_use objects and parser results.
803-
type AssistantContentEntry = {
804-
type?: string
805-
id?: string
806-
name?: string
807-
params?: Partial<Record<ToolParamName, string>>
808-
partial?: boolean
809-
}
810-
811-
it("should build and use compound streaming keys via makeStreamingKey", () => {
812-
const key = NativeToolCallParser.makeStreamingKey("toolu_compound123", "read_file")
813-
expect(key).toBe("toolu_compound123::read_file")
814-
815-
NativeToolCallParser.startStreamingToolCall("toolu_compound123", "read_file", scope)
816-
expect(NativeToolCallParser.getStreamingToolName(key, scope)).toBe("read_file")
817-
})
818-
819-
it("should ignore duplicate tool_call_start for same compound key", () => {
820-
const streamingToolCallIndices = new Map<string, number>()
821-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
822-
const events = [
823-
{ type: "tool_call_start", id: "toolu_dup_compound", name: "read_file" as const },
824-
{ type: "tool_call_start", id: "toolu_dup_compound", name: "read_file" as const },
825-
]
826-
const assistantMessageContent: AssistantContentEntry[] = []
827-
828-
for (const event of events) {
829-
const dedupKey = `${event.id}::${event.name}`
830-
if (streamingToolCallIndices.has(dedupKey)) {
831-
console.warn(
832-
`[Task#test] Ignoring duplicate tool_call_start for ID: ${event.id} (tool: ${event.name})`,
833-
)
834-
continue
835-
}
836-
streamingToolCallIndices.set(dedupKey, assistantMessageContent.length)
837-
assistantMessageContent.push({ type: "tool_use", id: event.id, name: event.name, partial: true })
838-
}
839-
840-
expect(assistantMessageContent).toHaveLength(1)
841-
expect(streamingToolCallIndices.has("toolu_dup_compound::read_file")).toBe(true)
842-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("[Task#test]"))
843-
warnSpy.mockRestore()
844-
})
845-
846-
it("should use compound key for delta events via getStreamingToolCallById", () => {
847-
const streamingToolCallIndices = new Map<string, number>()
848-
const assistantMessageContent: AssistantContentEntry[] = []
849-
850-
NativeToolCallParser.startStreamingToolCall("toolu_delta_compound", "read_file", scope)
851-
const dedupKey = NativeToolCallParser.makeStreamingKey("toolu_delta_compound", "read_file")
852-
streamingToolCallIndices.set(dedupKey, 0)
853-
assistantMessageContent.push({
854-
type: "tool_use",
855-
id: "toolu_delta_compound",
856-
name: "read_file",
857-
params: {},
858-
partial: true,
859-
})
860-
861-
const partialToolUse = NativeToolCallParser.processStreamingChunk(dedupKey, '{"path":"', scope)
862-
expect(partialToolUse).not.toBeNull()
863-
864-
const existingEntry = NativeToolCallParser.getStreamingToolCallById("toolu_delta_compound", scope)
865-
expect(existingEntry).not.toBeNull()
866-
const resolvedKey = existingEntry
867-
? NativeToolCallParser.makeStreamingKey(existingEntry.id, existingEntry.name)
868-
: undefined
869-
const updatedToolUse = NativeToolCallParser.processStreamingChunk(resolvedKey!, '"test.ts"}', scope)
870-
expect(updatedToolUse).not.toBeNull()
871-
const name = NativeToolCallParser.getStreamingToolName(resolvedKey!, scope)
872-
const toolUseIndex = streamingToolCallIndices.get(`${existingEntry!.id}::${name}`)
873-
expect(toolUseIndex).toBe(0)
874-
if (!updatedToolUse) {
875-
throw new Error("Expected processStreamingChunk to return a tool use")
876-
}
877-
assistantMessageContent[toolUseIndex!] = updatedToolUse
878-
879-
expect(assistantMessageContent).toHaveLength(1)
880-
expect(assistantMessageContent[0]?.type).toBe("tool_use")
881-
expect(assistantMessageContent[0]?.name).toBe("read_file")
882-
})
883-
884-
it("should finalize and clean up tracking using compound key on tool_call_end", () => {
885-
const streamingToolCallIndices = new Map<string, number>()
886-
const assistantMessageContent: AssistantContentEntry[] = []
887-
const dedupKey = NativeToolCallParser.makeStreamingKey("toolu_cleanup123", "read_file")
888-
889-
NativeToolCallParser.startStreamingToolCall("toolu_cleanup123", "read_file", scope)
890-
streamingToolCallIndices.set(dedupKey, 0)
891-
assistantMessageContent.push({ type: "tool_use", id: "toolu_cleanup123", name: "read_file", partial: true })
892-
NativeToolCallParser.processStreamingChunk(dedupKey, '{"path":"test.ts"}', scope)
893-
894-
const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(dedupKey, scope)
895-
expect(finalToolUse).not.toBeNull()
896-
if (!finalToolUse) {
897-
throw new Error("Expected finalizeStreamingToolCall to return a tool use")
898-
}
899-
assistantMessageContent[0] = finalToolUse
900-
streamingToolCallIndices.delete(dedupKey)
901-
902-
expect(assistantMessageContent).toHaveLength(1)
903-
expect(assistantMessageContent[0]?.type).toBe("tool_use")
904-
expect(assistantMessageContent[0]?.name).toBe("read_file")
905-
expect(assistantMessageContent[0]?.partial).toBe(false)
906-
expect(
907-
streamingToolCallIndices.has(NativeToolCallParser.makeStreamingKey("toolu_cleanup123", "read_file")),
908-
).toBe(false)
909-
})
910-
911-
it("should handle malformed JSON finalization with compound key cleanup", () => {
912-
const streamingToolCallIndices = new Map<string, number>()
913-
const assistantMessageContent: AssistantContentEntry[] = []
914-
const dedupKey = NativeToolCallParser.makeStreamingKey("toolu_malformed123", "read_file")
915-
916-
NativeToolCallParser.startStreamingToolCall("toolu_malformed123", "read_file", scope)
917-
streamingToolCallIndices.set(dedupKey, 0)
918-
assistantMessageContent.push({
919-
type: "tool_use",
920-
id: "toolu_malformed123",
921-
name: "read_file",
922-
partial: true,
923-
})
924-
NativeToolCallParser.processStreamingChunk(dedupKey, "{invalid json", scope)
925-
926-
const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(dedupKey, scope)
927-
expect(finalToolUse).toBeNull()
928-
assistantMessageContent[0].partial = false
929-
streamingToolCallIndices.delete(dedupKey)
930-
931-
expect(assistantMessageContent).toHaveLength(1)
932-
expect(assistantMessageContent[0]?.type).toBe("tool_use")
933-
expect(assistantMessageContent[0]?.name).toBe("read_file")
934-
expect(assistantMessageContent[0]?.partial).toBe(false)
935-
expect(
936-
streamingToolCallIndices.has(NativeToolCallParser.makeStreamingKey("toolu_malformed123", "read_file")),
937-
).toBe(false)
938-
})
939-
})
940-
941800
describe("real Task streaming integration (attemptApiRequest)", () => {
942801
// Structural access to the private Task state these tests assert against.
943802
type TaskStreamingTestAccess = {

‎src/core/task/__tests__/duplicate-tool-use-ids.spec.ts‎

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -259,138 +259,4 @@ describe("Duplicate tool_use ID Prevention", () => {
259259
expect(ids).toEqual(uniqueIds) // All IDs are unique
260260
})
261261
})
262-
263-
describe("Compound key deduplication (id + name)", () => {
264-
/**
265-
* Tests the compound key fix: when two different tools share the same toolCallId,
266-
* they should both be processed because their (id, name) keys differ.
267-
*
268-
* The streamingToolCallIndices map now uses `${event.id}::${event.name}` as the key
269-
* instead of just `event.id`, allowing different tools with the same call ID to coexist.
270-
*/
271-
272-
it("should allow two different tools with same toolCallId but different names", () => {
273-
const streamingToolCallIndices = new Map<string, number>()
274-
const processedEvents: Array<{ id: string; name: string }> = []
275-
276-
const processToolCallStart = (id: string, name: string): boolean => {
277-
// Compound key deduplication (new behavior)
278-
const dedupKey = `${id}::${name}`
279-
if (streamingToolCallIndices.has(dedupKey)) {
280-
return false // Skipped as duplicate
281-
}
282-
streamingToolCallIndices.set(dedupKey, processedEvents.length)
283-
processedEvents.push({ id, name })
284-
return true
285-
}
286-
287-
// First tool with shared ID
288-
expect(processToolCallStart("toolu_123", "read_file")).toBe(true)
289-
expect(processedEvents).toEqual([{ id: "toolu_123", name: "read_file" }])
290-
291-
// Second DIFFERENT tool with same ID should succeed (not dropped)
292-
expect(processToolCallStart("toolu_123", "write_to_file")).toBe(true)
293-
expect(processedEvents).toEqual([
294-
{ id: "toolu_123", name: "read_file" },
295-
{ id: "toolu_123", name: "write_to_file" },
296-
])
297-
298-
// True duplicate (same ID AND same name) should be rejected
299-
expect(processToolCallStart("toolu_123", "read_file")).toBe(false)
300-
expect(processedEvents).toHaveLength(2) // No change
301-
})
302-
303-
it("should reject true duplicates but allow different tools with same callId", () => {
304-
const streamingToolCallIndices = new Map<string, number>()
305-
const processedEvents: Array<{ id: string; name: string }> = []
306-
307-
const processToolCallStart = (id: string, name: string): boolean => {
308-
const dedupKey = `${id}::${name}`
309-
if (streamingToolCallIndices.has(dedupKey)) {
310-
return false
311-
}
312-
streamingToolCallIndices.set(dedupKey, processedEvents.length)
313-
processedEvents.push({ id, name })
314-
return true
315-
}
316-
317-
// First call: read_file with toolu_abc
318-
expect(processToolCallStart("toolu_abc", "read_file")).toBe(true)
319-
320-
// Stream retry of the SAME event should be rejected
321-
expect(processToolCallStart("toolu_abc", "read_file")).toBe(false)
322-
323-
// Different tool with same callId should succeed
324-
expect(processToolCallStart("toolu_abc", "get_status")).toBe(true)
325-
326-
// Retry of get_status should also be rejected
327-
expect(processToolCallStart("toolu_abc", "get_status")).toBe(false)
328-
329-
// Both tools processed
330-
expect(processedEvents).toHaveLength(2)
331-
expect(processedEvents[0].name).toBe("read_file")
332-
expect(processedEvents[1].name).toBe("get_status")
333-
})
334-
335-
it("should track correct indices for compound key lookups", () => {
336-
const streamingToolCallIndices = new Map<string, number>()
337-
let currentIndex = 0
338-
339-
const processToolCallStart = (id: string, name: string): number | null => {
340-
const dedupKey = `${id}::${name}`
341-
if (streamingToolCallIndices.has(dedupKey)) {
342-
return null
343-
}
344-
const index = currentIndex
345-
streamingToolCallIndices.set(dedupKey, index)
346-
currentIndex++
347-
return index
348-
}
349-
350-
expect(processToolCallStart("toolu_1", "tool_a")).toBe(0)
351-
expect(processToolCallStart("toolu_1", "tool_b")).toBe(1)
352-
expect(processToolCallStart("toolu_2", "tool_a")).toBe(2)
353-
354-
// Verify compound key lookups return correct indices
355-
expect(streamingToolCallIndices.get("toolu_1::tool_a")).toBe(0)
356-
expect(streamingToolCallIndices.get("toolu_1::tool_b")).toBe(1)
357-
expect(streamingToolCallIndices.get("toolu_2::tool_a")).toBe(2)
358-
359-
// Old single-key lookup would return wrong index or conflict
360-
// With compound keys, each (id, name) pair has its own entry
361-
expect(streamingToolCallIndices.has("toolu_1")).toBe(false) // No bare key
362-
expect(streamingToolCallIndices.has("toolu_2")).toBe(false)
363-
})
364-
365-
it("should clean up after tool_call_end using compound key", () => {
366-
const streamingToolCallIndices = new Map<string, number>()
367-
368-
const addTool = (id: string, name: string): void => {
369-
const dedupKey = `${id}::${name}`
370-
streamingToolCallIndices.set(dedupKey, streamingToolCallIndices.size)
371-
}
372-
373-
const removeTool = (id: string, name: string): boolean => {
374-
const dedupKey = `${id}::${name}`
375-
return streamingToolCallIndices.delete(dedupKey)
376-
}
377-
378-
addTool("toolu_1", "read_file")
379-
addTool("toolu_1", "write_to_file")
380-
381-
expect(streamingToolCallIndices.size).toBe(2)
382-
expect(streamingToolCallIndices.has("toolu_1::read_file")).toBe(true)
383-
expect(streamingToolCallIndices.has("toolu_1::write_to_file")).toBe(true)
384-
385-
// Finalize first tool - should only remove that specific entry
386-
expect(removeTool("toolu_1", "read_file")).toBe(true)
387-
expect(streamingToolCallIndices.size).toBe(1)
388-
expect(streamingToolCallIndices.has("toolu_1::read_file")).toBe(false)
389-
expect(streamingToolCallIndices.has("toolu_1::write_to_file")).toBe(true)
390-
391-
// Finalize second tool
392-
expect(removeTool("toolu_1", "write_to_file")).toBe(true)
393-
expect(streamingToolCallIndices.size).toBe(0)
394-
})
395-
})
396262
})

0 commit comments

Comments
 (0)