fix: buffer "start" events so auth retry does not leak phantom messages#2
Open
dnouri wants to merge 1 commit into
Open
fix: buffer "start" events so auth retry does not leak phantom messages#2dnouri wants to merge 1 commit into
dnouri wants to merge 1 commit into
Conversation
streamAnthropic (the default protocol path) emits a synthetic "start" event synchronously, before the for-await loop that actually drives the HTTP request. If the server returns 401, the loop throws and the catch block emits "error". Event sequence: start → error. Our retry logic gated the refresh on !pushedAny, but pushedAny was set to true as soon as the "start" event was pushed. So when the "error" event arrived, the guard was already false and the speculative OAuth refresh was skipped, letting the raw 401 leak to the user. This happens frequently because Kimi tokens are short-lived. The framework only refreshes when the local expires timestamp is reached, but servers routinely invalidate tokens a few minutes early. During that invalidation window the framework hands out a dead token, the request 401s, and the retry guard fails to catch it. The simple fix is to drop the !pushedAny guard so the retry fires on any first-error. But that leaks the synthetic "start" event into the session history — the consumer (agent-loop.ts) pushes a new empty assistant message on every start. On retry that leaves a dangling phantom message in the TUI and in the persisted session. Instead, we buffer "start" events and only flush them once we see a non-error event that proves the stream is alive. If we do retry, the buffer is discarded and no phantom message escapes. The buffer is at most one event and adds no perceptible overhead.
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="index.ts">
<violation number="1" location="index.ts:872">
P2: Thrown upstream auth failures bypass token-refresh retry and can leak buffered `start` events, reintroducing phantom messages.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
|
||
| // First non-start, non-retry event: flush buffered prefix, then | ||
| // stream normally. | ||
| for (const e of prefixBuffer) filtered.push(e); |
There was a problem hiding this comment.
P2: Thrown upstream auth failures bypass token-refresh retry and can leak buffered start events, reintroducing phantom messages.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At index.ts, line 872:
<comment>Thrown upstream auth failures bypass token-refresh retry and can leak buffered `start` events, reintroducing phantom messages.</comment>
<file context>
@@ -852,16 +860,25 @@ function streamSimpleKimi(
+
+ // First non-start, non-retry event: flush buffered prefix, then
+ // stream normally.
+ for (const e of prefixBuffer) filtered.push(e);
+ prefixBuffer = [];
filtered.push(event);
</file context>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Users see frequent 401 authentication errors even though the extension already has speculative OAuth token refresh logic:
Root cause
streamAnthropic(the default protocol path) emits a syntheticstartevent before the HTTP request begins. If the server returns 401, the stream emitserror. Event sequence:start→error.The retry logic was gated on
!pushedAny && attempt === 0 && event.type === "error". ButpushedAnywas set totrueas soon as thestartevent was pushed. When theerrorevent arrived, the guard was alreadyfalseand the refresh was skipped.This happens frequently because Kimi tokens are short-lived.
pi-coding-agentonly refreshes when the localexpirestimestamp is reached, but servers routinely invalidate tokens a few minutes early. During that invalidation window the framework hands out a dead token, the request 401s, and the retry guard fails to catch it.Why not just drop
!pushedAny?Removing the guard would fix the 401 leak, but it would let the synthetic
startevent escape into the session. The consumer (agent-loop.ts) creates a new empty assistant message on everystart. On retry that leaves a dangling phantom message in the TUI and persisted session.Fix
Buffer
startevents and only flush them once we see a non-error event that proves the stream is alive. If we retry, the buffer is discarded and no phantom message escapes. The buffer is at most one event.What changed
pushedAnywith aprefixBuffer: AssistantMessageEvent[]startevents are buffered instead of forwarded immediatelyerrorwithattempt === 0: refresh OAuth token and retry, discarding the bufferSummary by cubic
Buffers synthetic "start" events so the first 401 triggers an auth refresh and retry without leaking empty assistant messages into the UI or session. This reduces spurious 401s and removes phantom messages during retries.
pushedAnywith aprefixBuffer; flush the buffer on normal end or exceptions.Written for commit 22982d0. Summary will update on new commits.