Repository navigation
fix(gemini): repair streams that end with an unframed final event #2836
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| ---- MODULE GeminiStreamEnd ---- | ||
| \* Source: the SDK stream iterator that GeminiHandler.createMessage consumes | ||
| \* (models.generateContentStream). Sink: the createMessage completion decision | ||
| \* after the consumption loop. | ||
| \* Failure: a stream that ends while its final event is still unframed raised | ||
| \* a hard provider error, so delivered events were lost behind 'Incomplete | ||
| \* JSON segment at the end' (issue 2682), and a first-attempt end with no | ||
| \* delivered event failed instead of retrying the clean request. | ||
| \* Invariants: UnframedEndCompletes, AbortSurfaces, OtherErrorsSurface, | ||
| \* ZeroDeliveryRetried, SigNotCarried. | ||
| \* Expect Raw to fail UnframedEndCompletes, RetryOff to fail ZeroDeliveryRetried, | ||
| \* AbortMasked to fail AbortSurfaces, SwallowAll to fail OtherErrorsSurface and | ||
| \* SigLeak to fail SigNotCarried. | ||
| \* Result: fixed passes in 94 states; Raw (33), RetryOff (14), AbortMasked | ||
| \* (19), SwallowAll (22) and SigLeak (81) each fail exactly the invariant | ||
| \* named above. Each sabotage run takes less than 1 second; the whole gate | ||
| \* runs in seconds. | ||
| \* Findings: no model failure beyond the pinned defects. A RetryOff run first | ||
| \* passed vacuously because the invariant read the RetryZero constant; the | ||
| \* invariant is now an unconditional unreachability claim. A probe run confirmed | ||
| \* the new ordering: Retry, then Abort on attempt 1, settles to abortError, | ||
| \* so AbortSurfaces checks cancellation after a retry (thread 4801273323). | ||
| \* False alarms: none. The retry restarts the settlement, so a second unframed | ||
| \* end still surfaces; OtherErrorsSurface stays unconditional on purpose. | ||
| \* The provider test replays every invariant sentence on the real handler. | ||
| \* Review: 1 covered, 2 added, 0 gap (PR 2836 CodeRabbit round 1). | ||
| \* Added: thread 4801273323 - the Abort action makes cancellation reachable | ||
| \* while outcome is "none" on any attempt, so AbortSurfaces checks an abort | ||
| \* after Retry advanced attempts to 1 as well. | ||
| \* Added: thread 4801273350 - sig, sigAt and RetryClearsSig model the per | ||
| \* attempt thought-signature reset; SigNotCarried fails when the retry keeps | ||
| \* a discarded attempt's signature. | ||
| \* Covered: thread 4801273340 - the delivered flag abstracts every delivery | ||
| \* kind; the handler tests cover reasoning, tool-call and fallback text. | ||
| \* Cost: about 30 minutes of agent work; each TLC run takes less than 1 second. | ||
| \* The SDK settlement names are oracles; the delivered event values and the | ||
| \* signature strings are outside this model (Task consumes them). | ||
| EXTENDS Naturals | ||
|
|
||
| CONSTANTS TolerateUnframed, AbortFirst, TolerateAnyError, RetryZero, RetryClearsSig | ||
|
|
||
| VARIABLES settlement, aborted, delivered, attempts, outcome, sig, sigAt | ||
| vars == <<settlement, aborted, delivered, attempts, outcome, sig, sigAt>> | ||
|
|
||
| Settlements == {"clean", "unframed", "otherError"} | ||
| Outcomes == {"completed", "providerError", "abortError", "none"} | ||
| SigAttempts == {0, 1, "unset"} | ||
|
|
||
| \* The sink decision is a pure policy over how the iterator settled, whether | ||
| \* the request was aborted, whether an event was delivered, and the attempt. | ||
| Surface(settle, ab, del, att) == | ||
| \* An abort check that runs first never lets tolerance mask the abort. | ||
| IF AbortFirst /\ ab THEN "abortError" | ||
| \* A tolerance broad enough to cover every error is the SwallowAll defect. | ||
| ELSE IF TolerateAnyError /\ settle # "clean" THEN "completed" | ||
| \* A tolerance applied before the abort check masks it (AbortMasked defect). | ||
| ELSE IF ~AbortFirst /\ TolerateUnframed /\ settle = "unframed" THEN "completed" | ||
| ELSE IF ab THEN "abortError" | ||
| ELSE IF settle = "clean" THEN "completed" | ||
| \* The fix: an unframed end still delivered every complete event. | ||
| ELSE IF settle = "unframed" /\ TolerateUnframed /\ del THEN "completed" | ||
| ELSE "providerError" | ||
|
|
||
| \* A zero-delivery unframed end on the first attempt retries the request: | ||
| \* nothing was yielded, so a redo is clean. | ||
| CanRetry == RetryZero /\ settlement = "unframed" /\ ~aborted /\ ~delivered /\ attempts = 0 | ||
|
|
||
| Init == /\ settlement \in Settlements | ||
| /\ aborted \in BOOLEAN | ||
| /\ delivered = FALSE | ||
| /\ attempts = 0 | ||
| /\ outcome = "none" | ||
| /\ sig = FALSE | ||
| /\ sigAt = "unset" | ||
|
|
||
| Deliver == /\ outcome = "none" /\ ~delivered | ||
| /\ delivered' = TRUE | ||
| /\ UNCHANGED <<settlement, aborted, attempts, outcome, sig, sigAt>> | ||
|
|
||
| \* A signature-only part stores a signature without delivering an event. A | ||
| \* part that both signs and delivers is Deliver then Sign in some order; the | ||
| \* invariant reads both the same way. | ||
| Sign == /\ outcome = "none" | ||
| /\ sig' = TRUE | ||
| /\ sigAt' = attempts | ||
| /\ UNCHANGED <<settlement, aborted, delivered, attempts, outcome>> | ||
|
|
||
| Settle == /\ outcome = "none" /\ ~CanRetry | ||
| /\ outcome' = Surface(settlement, aborted, delivered, attempts) | ||
| /\ UNCHANGED <<settlement, aborted, delivered, attempts, sig, sigAt>> | ||
|
|
||
| \* Cancellation fires while a request is in flight, on any attempt: it stays | ||
| \* reachable after Retry advanced attempts to 1 (review 4801273323). | ||
| Abort == /\ outcome = "none" | ||
| /\ aborted' = TRUE | ||
| /\ UNCHANGED <<settlement, delivered, attempts, outcome, sig, sigAt>> | ||
|
|
||
| \* The retry must drop attempt state: the delivered counter and the stored | ||
| \* signature both belong to the discarded attempt (review 4801273350). | ||
| Retry == /\ CanRetry /\ outcome = "none" | ||
| /\ attempts' = 1 | ||
| /\ settlement' \in Settlements | ||
| /\ delivered' = FALSE | ||
| /\ sig' = IF RetryClearsSig THEN FALSE ELSE sig | ||
| /\ sigAt' = IF RetryClearsSig THEN "unset" ELSE sigAt | ||
| /\ UNCHANGED <<aborted, outcome>> | ||
|
|
||
| \* A settled outcome is terminal; the stuttering step keeps TLC's deadlock | ||
| \* check meaningful for states that are stuck before settling. | ||
| Termination == outcome # "none" /\ UNCHANGED vars | ||
|
|
||
| Next == Deliver \/ Settle \/ Retry \/ Sign \/ Abort \/ Termination | ||
|
|
||
| Spec == Init /\ [][Next]_vars | ||
|
|
||
| UnframedEndCompletes == | ||
| (outcome # "none" /\ settlement = "unframed" /\ ~aborted /\ delivered) => | ||
| outcome = "completed" | ||
|
|
||
| AbortSurfaces == (outcome # "none" /\ aborted) => outcome = "abortError" | ||
|
|
||
| OtherErrorsSurface == | ||
| (outcome # "none" /\ settlement = "otherError" /\ ~aborted) => | ||
| outcome = "providerError" | ||
|
|
||
| \* A settled zero-delivery unframed end on the first attempt must be | ||
| \* unreachable: the policy retries it instead of failing it. The claim is | ||
| \* unconditional, so switching RetryZero off in a sabotage run violates it. | ||
| ZeroDeliveryRetried == | ||
| ~(outcome # "none" /\ settlement = "unframed" /\ ~aborted /\ | ||
| ~delivered /\ attempts = 0) | ||
|
|
||
| \* A settled completion never uses a signature captured on a discarded | ||
| \* attempt: the retry must reset the stored value (review 4801273350). | ||
| SigNotCarried == ~(outcome = "completed" /\ sig /\ sigAt # attempts) | ||
|
|
||
| ==== | ||
Uh oh!
There was an error while loading. Please reload this page.