Skip to content
This repository was archived by the owner on Oct 29, 2026. It is now read-only.

fix: keep the session affinity chose on the first attempt - #11

Closed
dgqyushen wants to merge 1 commit into
tkhs101:mainfrom
dgqyushen:fix/session-affinity-first-attempt
Closed

dgqyushen wants to merge 1 commit into
tkhs101:mainfrom
dgqyushen:fix/session-affinity-first-attempt

Conversation

@dgqyushen

Copy link
Copy Markdown
Contributor

What this fixes

OPENCODE_ZEN_FREE_SESSION_AFFINITY=1 is documented to keep one session per
conversation. It does not: every request goes out under a freshly minted
session, so two turns of one conversation answer two different session ids.

The cause is the order of two statements in attempt():

const attempt = async () => {
  attempts += 1;                                    // counter moves first
  ...
  headers: attemptHeaders(),                        // then reads it

attemptHeaders distinguishes the first attempt from a retry with
attempts === 0, but the counter has already moved by the time the headers are
built. The branch never matches, so:

  • the id sessionID() chose is discarded on every request, and
  • a retry re-mints a session that was never spent, which is the opposite of
    what the retry is for.

The fix builds the headers before the counter moves and sends that value:

const headers = attemptHeaders();
attempts += 1;

Five lines, no behaviour change outside the attempt counter's ordering.

Test

index.test.mjs gains one case that pins the contract: with affinity on, two
turns of one conversation share a session id, the first attempt of a retried
request still opens under it, and the retry itself does not.

On upstream main (b6e828e) the new case fails:

AssertionError: two turns of one conversation share the session affinity keeps for it
+ actual   'ses_e86fb3e31ffesaQSCqt8ZEr1av'
- expected 'ses_e86fb3e29ffeyA5TQQtEqRpTtE'

With the fix, the full suite passes under the declared bun@1.4.3:

 823 pass
 0 fail
Ran 823 tests across 15 files.

Integration check

Deployed as a local patched build of 0.5.11 into a magpie gateway, where the
plugin owns opencode-zen-free. Real requests through the gateway:

NONSTREAM status=200 provider=opencode-zen-free model=deepseek-v4.1-flash-free
          finish=stop content='ok'
STREAM    status=200 provider=opencode-zen-free finish=stop content='1\n2\n3'

Both a streaming and a non-streaming turn were served.

Scope

This is a fix for the affinity regression only. It does not touch the gate,
fingerprint.json, the project-id policy, or the 429 retry's own semantics.

Unrelated to this PR, and not claimed as fixed by it: a gateway running 0.5.9
was answering 429 on this route. 0.5.9 has no attempt counter at all, so it
cannot carry this regression.

`attemptHeaders` tells the first attempt from a retry by `attempts`,
but `attempt()` incremented the counter before building the headers. The
branch therefore never matched, so every request went out under a freshly
minted session and `OPENCODE_ZEN_FREE_SESSION_AFFINITY=1` had no effect:
two turns of one conversation answered two session ids. A retry also
re-minted a session that had never been spent.

Build the headers before the counter moves and send that value.
@tkhs101

tkhs101 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Verified locally and merged (rebased onto main as 41a2833 to keep history linear).

The diagnosis is exact: attempts += 1 ran before attemptHeaders(), so the attempts === 0 branch never matched and the id sessionID() chose was discarded on every request — affinity silently never engaged. Invisible in the default mode (both sides are fresh ids there), which is how it survived.

Both directions verified: the new test fails against main's provider.mjs and passes with the fix; full suite on the rebased state matches the main baseline (806 pass, the 8 fails are pre-existing local-Bun is not defined capture cases).

Side note: this also invalidates the session-pinned arm of some quota experiments I ran yesterday — the pinned session never went out because of this very bug. The project-id arm was capture-verified and stands. Thanks for the sharp catch and the well-built test.

@tkhs101

tkhs101 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Merged as 41a2833 (rebased).

@tkhs101 tkhs101 closed this Oct 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants