Skip to content

Commit 00f0cd3

Browse files
committed
fix(cli): preserve error classification and await failure publication
1 parent bc7f75f commit 00f0cd3

6 files changed

Lines changed: 162 additions & 8 deletions

File tree

‎EVENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ A provider can finish an HTTP stream normally while reporting a failed model tur
246246
| Thrown provider error | Existing retry/error handling; unrecoverable failures emit the failure lifecycle. |
247247
| Cancellation / stream timeout | Existing cancellation and timeout lifecycle; not reclassified as a provider finish error. |
248248

249+
With pinned Google SDK 2.0.54, `IMAGE_SAFETY`, `RECITATION`, `SAFETY`, `BLOCKLIST`, `PROHIBITED_CONTENT`, and `SPII` map to `content-filter`; `MALFORMED_FUNCTION_CALL` maps to `error`. `OTHER` and `FINISH_REASON_UNSPECIFIED` map to `other`, while `LANGUAGE` maps to `unknown`. These last mappings retain the behavior above; they do not prove successful task completion. Consumers can distinguish `other` from `stop` using `message_complete.finish`.
250+
249251
Errors are attributed to the originating session. A child error alone does not change the primary session's exit status if the primary agent handles it successfully.
250252

251253
### `text`

‎packages/cli/src/cli/cmd/run.errors.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function classifySessionError(err: unknown): ClassifiedSessionError {
3030
if (/heap out of memory|ENOMEM/i.test(message)) {
3131
return { reason: "oom", message }
3232
}
33-
if (name === "APIError" || (status && status >= 500 && status < 600)) {
33+
if ((name === "APIError" && status === undefined) || (status && status >= 500 && status < 600)) {
3434
return { reason: "provider", code: status ? String(status) : undefined, message }
3535
}
3636
return { reason: "unknown", code: status ? String(status) : undefined, message }

‎packages/cli/src/session/processor.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export namespace SessionProcessor {
393393
input.assistantMessage.error = new NamedError.Unknown({
394394
message: `Max retry attempts (${SessionRetry.MAX_RETRY_ATTEMPTS}) reached: ${retry}`,
395395
}).toObject()
396-
Bus.publish(Session.Event.Error, {
396+
await Bus.publish(Session.Event.Error, {
397397
sessionID: input.assistantMessage.sessionID,
398398
error: input.assistantMessage.error,
399399
})
@@ -410,7 +410,7 @@ export namespace SessionProcessor {
410410
continue
411411
}
412412
input.assistantMessage.error = error
413-
Bus.publish(Session.Event.Error, {
413+
await Bus.publish(Session.Event.Error, {
414414
sessionID: input.assistantMessage.sessionID,
415415
error: input.assistantMessage.error,
416416
})

‎packages/cli/test/cli/classify-session-error.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ describe("classifySessionError (#63)", () => {
6969
[401, "Invalid API key", "auth"],
7070
[401, "Unauthenticated", "auth"],
7171
[403, "Permission denied", "auth"],
72+
[400, "Invalid request", "unknown"],
73+
[402, "Payment required", "unknown"],
74+
[404, "Not found", "unknown"],
7275
[undefined, "Stream timeout", "timeout"],
73-
] as const)("APIError preserves specific classifications (%s, %s)", (statusCode, message, reason) => {
76+
] as const)("APIError preserves specific classifications (%s, %s) → %s", (statusCode, message, reason) => {
7477
const res = classifySessionError({ name: "APIError", data: { statusCode, message, isRetryable: false } })
7578
expect(res.reason).toBe(reason)
7679
expect(res.code).toBe(statusCode ? String(statusCode) : undefined)

‎packages/cli/test/cli/run-provider-finish.test.ts‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import path from "path"
3+
import { createGoogleGenerativeAI } from "@ai-sdk/google"
34
import { tmpdir } from "../fixture/fixture"
45

56
const entry = path.resolve(import.meta.dir, "../../src/index.ts")
@@ -12,13 +13,16 @@ describe("headless provider finish reasons (#108)", () => {
1213
["RECITATION", "content-filter", 1, false, false],
1314
["BLOCKLIST", "content-filter", 1, false, false],
1415
["SPII", "content-filter", 1, false, false],
16+
["PROHIBITED_CONTENT", "content-filter", 1, false, false],
17+
["FINISH_REASON_UNSPECIFIED", "other", 0, false, false],
1518
["OTHER", "other", 0, false, false],
1619
["STOP", "stop", 0, false, false],
1720
["STOP", "stop", 0, true, false],
1821
["MAX_TOKENS", "length", 0, false, false],
1922
] as const)(
2023
"normal Gemini stream ending %s",
2124
async (reason, finish, code, tool, partial) => {
25+
await using tmp = await tmpdir()
2226
let calls = 0
2327
const server = Bun.serve({
2428
port: 0,
@@ -68,8 +72,9 @@ describe("headless provider finish reasons (#108)", () => {
6872
})
6973
},
7074
})
71-
await using tmp = await tmpdir({
72-
config: {
75+
await Bun.write(
76+
path.join(tmp.path, "aictrl.json"),
77+
JSON.stringify({
7378
provider: {
7479
fixture: {
7580
npm: "@ai-sdk/google",
@@ -78,8 +83,8 @@ describe("headless provider finish reasons (#108)", () => {
7883
},
7984
},
8085
agent: { title: { disable: true } },
81-
},
82-
})
86+
}),
87+
)
8388
const proc = Bun.spawn(
8489
[
8590
"bun",
@@ -166,3 +171,46 @@ describe("headless provider finish reasons (#108)", () => {
166171
20000,
167172
)
168173
})
174+
175+
describe("pinned Google adapter finish mappings", () => {
176+
test.each([
177+
["STOP", "stop"],
178+
["MAX_TOKENS", "length"],
179+
["IMAGE_SAFETY", "content-filter"],
180+
["RECITATION", "content-filter"],
181+
["SAFETY", "content-filter"],
182+
["BLOCKLIST", "content-filter"],
183+
["PROHIBITED_CONTENT", "content-filter"],
184+
["SPII", "content-filter"],
185+
["MALFORMED_FUNCTION_CALL", "error"],
186+
["OTHER", "other"],
187+
["FINISH_REASON_UNSPECIFIED", "other"],
188+
["LANGUAGE", "unknown"],
189+
])("%s → %s", async (raw, normalized) => {
190+
const provider = createGoogleGenerativeAI({
191+
apiKey: "fixture",
192+
fetch: Object.assign(
193+
async () =>
194+
new Response(
195+
`data: ${JSON.stringify({
196+
candidates: [{ index: 0, content: { role: "model", parts: [] }, finishReason: raw }],
197+
usageMetadata: { promptTokenCount: 7, candidatesTokenCount: 3, totalTokenCount: 10 },
198+
})}\n\n`,
199+
{ headers: { "content-type": "text/event-stream" } },
200+
),
201+
{ preconnect: globalThis.fetch.preconnect },
202+
),
203+
})
204+
const response = await provider("gemini-fixture").doStream({
205+
prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }],
206+
})
207+
const reader = response.stream.getReader()
208+
let finish: string | undefined
209+
while (true) {
210+
const { done, value } = await reader.read()
211+
if (done) break
212+
if (value.type === "finish") finish = value.finishReason
213+
}
214+
expect(finish).toBe(normalized)
215+
})
216+
})
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { describe, expect, spyOn, test } from "bun:test"
2+
import { APICallError } from "ai"
3+
import { Agent } from "../../src/agent/agent"
4+
import { Bus } from "../../src/bus"
5+
import { Identifier } from "../../src/id/id"
6+
import { Instance } from "../../src/project/instance"
7+
import { Provider } from "../../src/provider/provider"
8+
import { Session } from "../../src/session"
9+
import { LLM } from "../../src/session/llm"
10+
import { MessageV2 } from "../../src/session/message-v2"
11+
import { SessionProcessor } from "../../src/session/processor"
12+
import { SessionRetry } from "../../src/session/retry"
13+
import { tmpdir } from "../fixture/fixture"
14+
15+
describe("processor terminal error publication", () => {
16+
test.each([false, true])("waits for subscribers before returning (retryable=%s)", async (retryable) => {
17+
await using tmp = await tmpdir({
18+
config: {
19+
provider: {
20+
fixture: {
21+
npm: "@ai-sdk/google",
22+
options: { apiKey: "fixture" },
23+
models: { "gemini-fixture": { name: "fixture", limit: { context: 100000, output: 1000 } } },
24+
},
25+
},
26+
},
27+
})
28+
await Instance.provide({
29+
directory: tmp.path,
30+
fn: async () => {
31+
const session = await Session.create({})
32+
const model = await Provider.getModel("fixture", "gemini-fixture")
33+
const agent = await Agent.get("build")
34+
const abort = new AbortController().signal
35+
const user: MessageV2.User = {
36+
id: Identifier.ascending("message"),
37+
sessionID: session.id,
38+
role: "user",
39+
time: { created: Date.now() },
40+
agent: agent.name,
41+
model: { providerID: model.providerID, modelID: model.id },
42+
}
43+
const message: MessageV2.Assistant = {
44+
id: Identifier.ascending("message"),
45+
sessionID: session.id,
46+
parentID: user.id,
47+
role: "assistant",
48+
time: { created: Date.now() },
49+
agent: agent.name,
50+
mode: agent.name,
51+
modelID: model.id,
52+
providerID: model.providerID,
53+
path: { cwd: tmp.path, root: tmp.path },
54+
cost: 0,
55+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
56+
}
57+
await Session.updateMessage(user)
58+
await Session.updateMessage(message)
59+
const entered = Promise.withResolvers<void>()
60+
const release = Promise.withResolvers<void>()
61+
const unsub = Bus.subscribe(Session.Event.Error, async () => {
62+
entered.resolve()
63+
await release.promise
64+
})
65+
const stream = spyOn(LLM, "stream").mockRejectedValue(
66+
new APICallError({
67+
url: "https://fixture.invalid",
68+
requestBodyValues: {},
69+
message: "fixture provider error",
70+
statusCode: retryable ? 500 : 400,
71+
isRetryable: retryable,
72+
}),
73+
)
74+
const sleep = spyOn(SessionRetry, "sleep").mockResolvedValue()
75+
let finished = false
76+
const processing = SessionProcessor.create({ assistantMessage: message, sessionID: session.id, model, abort })
77+
.process({ user, sessionID: session.id, model, agent, abort, system: [], messages: [], tools: {} })
78+
.then((result) => {
79+
finished = true
80+
return result
81+
})
82+
try {
83+
await entered.promise
84+
// Give the processor time to return if it accidentally fire-and-forgets the subscriber.
85+
await Bun.sleep(30)
86+
expect(finished).toBe(false)
87+
release.resolve()
88+
await processing
89+
expect(message.error).toBeDefined()
90+
expect(stream).toHaveBeenCalledTimes(retryable ? SessionRetry.MAX_RETRY_ATTEMPTS + 1 : 1)
91+
} finally {
92+
release.resolve()
93+
await processing
94+
stream.mockRestore()
95+
sleep.mockRestore()
96+
unsub()
97+
}
98+
},
99+
})
100+
})
101+
})

0 commit comments

Comments
 (0)