-
Notifications
You must be signed in to change notification settings - Fork 271
[Fix] Billed API requests return no response when streams fail silently or end at max_tokens #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zoomote
wants to merge
14
commits into
main
Choose a base branch
from
fix/silent-retry-stop-reason-thinking-signature-1up978exiqscg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e7c1d58
[Fix] Billed requests produce no response from silent mid-stream retr…
roomote 9dd0ec6
[Fix] Retry restore duplicates the user turn in persisted API history
roomote 8153b05
test: cover retry and thinking guard boundaries
roomote 29258f1
test: model API retry persistence and replay signed thinking
roomote 6e2ad59
Merge remote-tracking branch 'origin/main' into fix/silent-retry-stop…
roomote b741bc4
fix(task): enforce retry approval and persistence boundaries
roomote debe1ae
test(task): cover persistence outcomes directly
roomote b52d713
test(task): cover retry cleanup branches
roomote 18c96e6
test(task): close mutation coverage gaps
roomote 007fcb9
test(task): verify approved retry cancellation
roomote 262aeb6
fix(task): reset retry budget after approval
roomote 77738b6
fix(task): require approval after tool execution
roomote 22b6eef
test(task): document defensive retry invariants
roomote 5534128
fix(task): stop retry after restore failure
roomote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| type StopReason = "none" | "max_tokens" | ||
| type Phase = "requesting" | "waiting" | "restoring" | "confirming" | "terminal" | ||
|
|
||
| interface State { | ||
| attempt: number | ||
| phase: Phase | ||
| visibleRetries: number | ||
| messageId: string | ||
| timestamp: number | ||
| stopReason: StopReason | ||
| turnPresent: boolean | ||
| autoApprovalEnabled: boolean | ||
| } | ||
|
|
||
| interface Transition { | ||
| name: string | ||
| next: State | ||
| } | ||
|
|
||
| const MAX_RETRIES = 3 | ||
| const initial: State = { | ||
| attempt: 0, | ||
| phase: "requesting", | ||
| visibleRetries: 0, | ||
| messageId: "logical-user-turn", | ||
| timestamp: 1, | ||
| stopReason: "none", | ||
| turnPresent: true, | ||
| autoApprovalEnabled: true, | ||
| } | ||
|
|
||
| function transitions(state: State): Transition[] { | ||
| if (state.phase === "terminal") return [] | ||
| if (state.phase === "waiting") { | ||
| return [{ name: "finish-visible-delay", next: { ...state, phase: "restoring" } }] | ||
| } | ||
| if (state.phase === "restoring") { | ||
| return [{ name: "restore-original-user-turn", next: { ...state, phase: "requesting", turnPresent: true } }] | ||
| } | ||
| if (state.phase === "confirming") { | ||
| return [ | ||
| { name: "decline-retry", next: { ...state, phase: "terminal", turnPresent: true } }, | ||
| { | ||
| name: "confirm-retry", | ||
| next: { | ||
| ...state, | ||
| attempt: state.attempt >= MAX_RETRIES ? 0 : state.attempt + 1, | ||
| visibleRetries: state.visibleRetries + 1, | ||
| phase: "waiting", | ||
| turnPresent: false, | ||
| }, | ||
| }, | ||
| ] | ||
| } | ||
| if (state.stopReason === "max_tokens") { | ||
| return [{ name: "surface-terminal-stop", next: { ...state, phase: "terminal" } }] | ||
| } | ||
| if (state.attempt >= MAX_RETRIES) { | ||
| return [{ name: "exhaust-automatic-retries", next: { ...state, phase: "confirming", turnPresent: false } }] | ||
| } | ||
| if (!state.autoApprovalEnabled) { | ||
| return [{ name: "require-explicit-approval", next: { ...state, phase: "confirming", turnPresent: false } }] | ||
| } | ||
| return [ | ||
| { | ||
| name: "retry-visible", | ||
| next: { | ||
| ...state, | ||
| attempt: state.attempt + 1, | ||
| visibleRetries: state.visibleRetries + 1, | ||
| phase: "waiting", | ||
| turnPresent: false, | ||
| }, | ||
| }, | ||
| { | ||
| name: "receive-max-tokens-empty", | ||
| next: { ...state, stopReason: "max_tokens" }, | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
| const queue: Array<{ state: State; depth: number }> = [ | ||
| { state: initial, depth: 0 }, | ||
| { state: { ...initial, autoApprovalEnabled: false }, depth: 0 }, | ||
| ] | ||
| const seen = new Set<string>() | ||
| const landmarks = new Set<string>() | ||
|
|
||
| function preservesLogicalTurnIdentity(restored: Pick<State, "messageId" | "timestamp">): boolean { | ||
| return restored.messageId === initial.messageId && restored.timestamp === initial.timestamp | ||
| } | ||
|
|
||
| if (!preservesLogicalTurnIdentity({ messageId: initial.messageId, timestamp: initial.timestamp })) { | ||
| throw new Error("original logical user-turn identity was rejected") | ||
| } | ||
| if (preservesLogicalTurnIdentity({ messageId: "reconstructed-turn", timestamp: initial.timestamp + 1 })) { | ||
| throw new Error("accidentally reconstructed logical user turn was accepted") | ||
| } | ||
| landmarks.add("reconstruction-rejected") | ||
|
|
||
| while (queue.length > 0) { | ||
| const current = queue.shift()! | ||
| const key = JSON.stringify(current.state) | ||
| if (seen.has(key)) continue | ||
| seen.add(key) | ||
|
|
||
| const state = current.state | ||
| if (state.attempt > MAX_RETRIES) throw new Error("automatic retry bound exceeded") | ||
| if (state.visibleRetries < state.attempt) throw new Error("retry occurred without a visible announcement") | ||
| if (state.messageId !== initial.messageId || state.timestamp !== initial.timestamp) { | ||
| throw new Error("logical user-turn identity changed across retry/restoration") | ||
| } | ||
| if (state.phase === "terminal" && !state.turnPresent) throw new Error("logical user turn was not restored") | ||
| if (!state.autoApprovalEnabled && state.phase === "waiting" && state.visibleRetries === 0) { | ||
| throw new Error("retry bypassed explicit approval") | ||
| } | ||
| if (state.stopReason === "max_tokens" && state.phase === "waiting") { | ||
| throw new Error("terminal max_tokens response silently re-entered retry") | ||
| } | ||
|
|
||
| if (state.phase === "confirming" && state.attempt === MAX_RETRIES) landmarks.add("bounded-exhaustion") | ||
| if (state.stopReason === "max_tokens" && state.phase === "terminal") landmarks.add("terminal-max-tokens") | ||
| if (state.visibleRetries === MAX_RETRIES) landmarks.add("all-retries-visible") | ||
| if (state.phase === "requesting" && state.attempt > 0 && state.turnPresent) { | ||
| landmarks.add("automatic-turn-restored") | ||
| } | ||
| if (!state.autoApprovalEnabled && state.phase === "confirming" && state.attempt === 0) { | ||
| landmarks.add("manual-approval-boundary") | ||
| } | ||
| if (current.depth >= 10) continue | ||
| for (const transition of transitions(state)) queue.push({ state: transition.next, depth: current.depth + 1 }) | ||
| } | ||
|
|
||
| for (const landmark of [ | ||
| "bounded-exhaustion", | ||
| "terminal-max-tokens", | ||
| "all-retries-visible", | ||
| "manual-approval-boundary", | ||
| "reconstruction-rejected", | ||
| "automatic-turn-restored", | ||
| ]) { | ||
| if (!landmarks.has(landmark)) throw new Error(`semantic landmark unreachable: ${landmark}`) | ||
| } | ||
|
|
||
| console.log(`API retry/persistence model check passed (${seen.size} states)`) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.