Skip to content

Commit a54b7e4

Browse files
committed
fix(session): narrow unknown-finish retry and discard failed-attempt parts (#121)
1 parent d6f17d2 commit a54b7e4

5 files changed

Lines changed: 101 additions & 17 deletions

File tree

‎EVENTS.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ When the parsed `aictrl run --format json` handler starts, the CLI emits newline
1313

1414
`invocationID` is present on every event from `run --format json`. `sessionID` is present only after a real session has been created; invocation events never fabricate one.
1515

16-
The schema is versioned via `invocation_start.schemaVersion` and `session_start.schemaVersion`. This document describes **schema version `"1"`**. Consumers should pin to this version and treat unknown fields as forward-compatible additions.
16+
The schema is versioned via `invocation_start.schemaVersion` and `session_start.schemaVersion`. This document describes **schema version `"1"`**. Consumers should pin to this version and treat unknown fields and event types as forward-compatible additions. The `retry` event is an additive event type, so `SCHEMA_VERSION` remains `"1"`.
1717

1818
The invocation envelope covers accepted `run --format json` executions from the first line of the parsed handler through validation, bootstrap, session creation, and execution. Argument-parser failures, other commands, and process-global uncaught exceptions or unhandled rejections are outside this contract.
1919

@@ -176,6 +176,27 @@ Emitted immediately before `session_complete` when the session terminates abnorm
176176

177177
## Message Events
178178

179+
### `retry`
180+
181+
Emitted when a primary-session model attempt fails with a retryable error and the CLI schedules another attempt. This includes a thought-only `unknown` finish with no tool call or non-empty text. It is not emitted after retries are exhausted or for an `unknown` finish that includes text or a tool call.
182+
183+
```json
184+
{
185+
"type": "retry",
186+
"timestamp": 1741500000000,
187+
"schemaVersion": "1",
188+
"invocationID": "7d142250-8bdc-43df-99af-efa252db62a7",
189+
"sessionID": "session_01abc...",
190+
"attempt": 1,
191+
"reason": "Provider ended stream without finishReason or a tool call.",
192+
"next": 1741500001000
193+
}
194+
```
195+
196+
- `attempt` (number, **required**) — retry number, starting at 1.
197+
- `reason` (string, **required**) — human-readable reason for retrying.
198+
- `next` (number, **required**) — scheduled next-attempt time in Unix milliseconds.
199+
179200
### `message_complete`
180201

181202
Emitted when a primary-session assistant message finishes (one per LLM turn). Child-session assistant messages are

‎packages/cli/src/provider/transform.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ export namespace ProviderTransform {
171171
return msgs
172172
}
173173

174+
function dropTrailingNonToolAssistant(msgs: ModelMessage[]): ModelMessage[] {
175+
// Gemini rejects requests ending on a model turn. This applies only to
176+
// @ai-sdk/google and @ai-sdk/google-vertex; even a trailing text answer
177+
// must go, because the MAX_STEPS assistant prefill would still cause a 400.
178+
// Unlike MessageV2's history filter, this also removes text-bearing turns.
179+
const user = msgs.some((msg) => msg.role === "user")
180+
if (!user) return msgs
181+
const last = msgs.at(-1)
182+
if (last?.role !== "assistant") return msgs
183+
if (Array.isArray(last.content) && last.content.some((part) => part.type === "tool-call")) return msgs
184+
return dropTrailingNonToolAssistant(msgs.slice(0, -1))
185+
}
186+
174187
function applyCaching(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
175188
const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
176189
const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
@@ -252,12 +265,9 @@ export namespace ProviderTransform {
252265
export function message(msgs: ModelMessage[], model: Provider.Model, options: Record<string, unknown>) {
253266
msgs = unsupportedParts(msgs, model)
254267
msgs = normalizeMessages(msgs, model, options)
255-
if (model.api.npm === "@ai-sdk/google" || model.api.npm === "@ai-sdk/google-vertex") {
256-
while (msgs.at(-1)?.role === "assistant") {
257-
const last = msgs.at(-1)
258-
if (last && Array.isArray(last.content) && last.content.some((part) => part.type === "tool-call")) break
259-
msgs = msgs.slice(0, -1)
260-
}
268+
const key = sdkKey(model.api.npm)
269+
if (key === "google") {
270+
msgs = dropTrailingNonToolAssistant(msgs)
261271
}
262272
if (
263273
(model.providerID === "anthropic" ||
@@ -272,7 +282,6 @@ export namespace ProviderTransform {
272282
}
273283

274284
// Remap providerOptions keys from stored providerID to expected SDK key
275-
const key = sdkKey(model.api.npm)
276285
if (key && key !== model.providerID && model.api.npm !== "@ai-sdk/azure") {
277286
const remap = (opts: Record<string, any> | undefined) => {
278287
if (!opts) return opts

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ export namespace SessionProcessor {
4848
needsCompaction = false
4949
const shouldBreak = (await Config.get()).experimental?.continue_loop_on_deny !== true
5050
while (true) {
51+
const parts = new Set<string>()
52+
const texts: MessageV2.TextPart[] = []
53+
let failed = false
5154
try {
5255
let currentText: MessageV2.TextPart | undefined
5356
let reasoningMap: Record<string, MessageV2.ReasoningPart> = {}
54-
let toolcall = false
57+
let sawToolCall = false
5558
const stream = await LLM.stream(streamInput)
5659

5760
for await (const value of stream.fullStream) {
@@ -77,6 +80,7 @@ export namespace SessionProcessor {
7780
metadata: value.providerMetadata,
7881
}
7982
reasoningMap[value.id] = reasoningPart
83+
parts.add(reasoningPart.id)
8084
await Session.updatePart(reasoningPart)
8185
break
8286

@@ -111,7 +115,7 @@ export namespace SessionProcessor {
111115
break
112116

113117
case "tool-input-start":
114-
toolcall = true
118+
sawToolCall = true
115119
const part = await Session.updatePart({
116120
id: toolcalls[value.id]?.id ?? Identifier.ascending("part"),
117121
messageID: input.assistantMessage.id,
@@ -135,7 +139,7 @@ export namespace SessionProcessor {
135139
break
136140

137141
case "tool-call": {
138-
toolcall = true
142+
sawToolCall = true
139143
const match = toolcalls[value.toolCallId]
140144
if (match) {
141145
const part = await Session.updatePart({
@@ -236,8 +240,10 @@ export namespace SessionProcessor {
236240

237241
case "start-step":
238242
snapshot = await Snapshot.track()
243+
const start = Identifier.ascending("part")
244+
parts.add(start)
239245
await Session.updatePart({
240-
id: Identifier.ascending("part"),
246+
id: start,
241247
messageID: input.assistantMessage.id,
242248
sessionID: input.sessionID,
243249
snapshot,
@@ -273,8 +279,10 @@ export namespace SessionProcessor {
273279
metadata: { finishReason: value.finishReason },
274280
}).toObject()
275281
}
282+
const finish = Identifier.ascending("part")
283+
parts.add(finish)
276284
await Session.updatePart({
277-
id: Identifier.ascending("part"),
285+
id: finish,
278286
reason: value.finishReason,
279287
snapshot: await Snapshot.track(),
280288
messageID: input.assistantMessage.id,
@@ -284,12 +292,13 @@ export namespace SessionProcessor {
284292
cost: usage.cost,
285293
})
286294
await Session.updateMessage(input.assistantMessage)
287-
if (value.finishReason === "unknown" && !toolcall) {
295+
if (value.finishReason === "unknown" && !sawToolCall && !texts.some((part) => part.text.trim())) {
296+
failed = true
288297
throw new MessageV2.APIError({
289298
message: "Provider ended stream without finishReason or a tool call.",
290299
isRetryable: true,
291300
metadata: { finishReason: "unknown" },
292-
}).toObject()
301+
})
293302
}
294303
if (input.assistantMessage.error) {
295304
await Bus.publish(Session.Event.Error, {
@@ -335,6 +344,8 @@ export namespace SessionProcessor {
335344
},
336345
metadata: value.providerMetadata,
337346
}
347+
parts.add(currentText.id)
348+
texts.push(currentText)
338349
await Session.updatePart(currentText)
339350
break
340351

@@ -415,6 +426,15 @@ export namespace SessionProcessor {
415426
error: input.assistantMessage.error,
416427
})
417428
} else {
429+
if (failed) {
430+
for (const partID of parts) {
431+
await Session.removePart({
432+
sessionID: input.sessionID,
433+
messageID: input.assistantMessage.id,
434+
partID,
435+
})
436+
}
437+
}
418438
const delay = SessionRetry.delay(attempt, error.name === "APIError" ? error : undefined)
419439
SessionStatus.set(input.sessionID, {
420440
type: "retry",

‎packages/cli/test/provider/transform.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ describe("ProviderTransform.message - Gemini trailing assistant", () => {
3333
test("other providers keep trailing assistant turns", () => {
3434
expect(ProviderTransform.message([user, thought], model("@ai-sdk/openai"), {})).toEqual([user, thought])
3535
})
36+
37+
test.each(["@ai-sdk/google", "@ai-sdk/google-vertex"])(
38+
"%s keeps all-assistant history when no user turn remains",
39+
(npm) => {
40+
expect(ProviderTransform.message([thought, { role: "assistant", content: "final" }], model(npm), {})).toEqual([
41+
thought,
42+
{ role: "assistant", content: "final" },
43+
])
44+
},
45+
)
3646
})
3747

3848
const OUTPUT_TOKEN_MAX = 32000

‎packages/cli/test/session/processor-unknown-finish.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { SessionRetry } from "../../src/session/retry"
1111
import { tmpdir } from "../fixture/fixture"
1212

1313
describe("processor unknown finish", () => {
14-
test.each(["recover", "exhaust", "tool-call"] as const)("%s", async (scenario) => {
14+
test.each(["recover", "exhaust", "tool-call", "text"] as const)("%s", async (scenario) => {
1515
await using tmp = await tmpdir({
1616
git: true,
1717
config: {
@@ -70,8 +70,19 @@ describe("processor unknown finish", () => {
7070
fullStream: (async function* () {
7171
yield { type: "start-step" }
7272
yield { type: "reasoning-start", id: "thought" }
73-
yield { type: "reasoning-delta", id: "thought", text: "Thinking" }
73+
yield {
74+
type: "reasoning-delta",
75+
id: "thought",
76+
text: scenario === "recover" && requests.length === 2 ? "Successful thought" : "Discarded thought",
77+
}
7478
yield { type: "reasoning-end", id: "thought" }
79+
if (scenario === "recover" || scenario === "text") {
80+
yield { type: "text-start" }
81+
if (scenario === "text" || requests.length === 2) {
82+
yield { type: "text-delta", text: "Answer" }
83+
}
84+
yield { type: "text-end" }
85+
}
7586
if (scenario === "tool-call") {
7687
yield { type: "tool-input-start", id: "call", toolName: "read" }
7788
yield { type: "tool-call", toolCallId: "call", toolName: "read", input: {} }
@@ -112,6 +123,19 @@ describe("processor unknown finish", () => {
112123
} else {
113124
expect(message.error).toBeUndefined()
114125
}
126+
if (scenario === "recover") {
127+
const parts = await MessageV2.parts(message.id)
128+
expect(parts.map((part) => part.type)).toEqual(["step-start", "reasoning", "text", "step-finish"])
129+
expect(parts.find((part) => part.type === "reasoning")?.text).toBe("Successful thought")
130+
expect(parts.find((part) => part.type === "text")?.text).toBe("Answer")
131+
expect(parts.find((part) => part.type === "step-finish")?.reason).toBe("stop")
132+
const history = MessageV2.toModelMessages(
133+
await MessageV2.filterCompacted(MessageV2.stream(session.id)),
134+
model,
135+
)
136+
expect(JSON.stringify(history)).toContain("Successful thought")
137+
expect(JSON.stringify(history)).not.toContain("Discarded thought")
138+
}
115139
} finally {
116140
stream.mockRestore()
117141
sleep.mockRestore()

0 commit comments

Comments
 (0)