Skip to content

fix(identity): salt derived session ids per process so a restart re-mints them - #160

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
cheerc:fix/identity-restart-session-salt
Oct 29, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
cheerc:fix/identity-restart-session-salt

Conversation

@cheerc

@cheerc cheerc commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CanonicalSessionID derived ses_… from the server key and the conversation seed alone, so every process on the same UTC day forwarded the byte-identical x-opencode-session. The free tier keeps charging a session, so once it 429'd, restarting the gateway after an egress change replayed the same id and failed again — measured as a ~0.7 s 429 with in: 0.

This mixes a 16-byte nonce, minted once per process, into the re-encoding hash. A running gateway still derives one stable session per conversation (affinity, sticky proxy, prompt cache keep working); a restart re-mints the ids. Official client-minted ses_… bypass the salt and stay verbatim, as before.

Scope and deviations

  • internal/identity/request.go: salt synthesized sessions with bootNonce.
  • internal/identity/request_test.go: new tests for per-process stability, restart change, and official-id passthrough.
  • README.md / README.zh-CN.md: document the restart behaviour.
  • README.vi.md and README.it.md are not updated (translation); the owner may want to sync them.
  • Probes and the Playground bench still pin a session within one run; Availability probes and the Playground bench both pin a deterministic session, so both 429 while live traffic succeeds #156 remains open and unaffected.
  • This does not change the quota model or routing, only the identity a restart presents.

Verification

  • go test ./internal/identity → ok opencode2api/internal/identity

  • go vet ./... → clean

  • go build -o /tmp/o2a-check ./cmd/opencode2api → success

  • Two separate runs of the patched build with the same fixed seed:

    SESSION=ses_e8b9c8673ffe5hbviAZ04EyKRc
    SESSION=ses_ee4179eeeffe0z6sBV1zoHVEBv
    

    Within one process the same seed returns one id; both ids satisfy the free tier's ses_ shape (descending time part, counter 1, …ffe).

  • CI: all 5 checks pass on 4bc4fff (Go 1.24.x and stable, ubuntu and windows, plus formatting/WebUI syntax; run 40837307779).

  • Field check (single deployment, reported by the reporter, not a controlled run): after changing IP and restarting on this patch, the same conversation reached upstream instead of 429. The exit changed at the same time, so Derived session ids survive a restart, so a burned ses_… keeps 429ing after the egress changes #159 asks for the fixed-exit A/B to confirm which key the limiter uses.

Risks

  • Derived sessions lose upstream prompt-cache affinity on each restart. That is the point of the change, restarts are rare, and official sessions are untouched.
  • Derived ids used by availability probes and the quota check also rotate per restart, while staying stable within a run.
  • Reverting is one line: drop bootNonce from the hash in CanonicalSessionID.

Closes #159. refs #158.

CanonicalSessionID re-encodes a conversation signal from the server key and
the seed alone, so the upstream `x-opencode-session` was identical in every
process on the same UTC day. The free tier keeps charging that session, so
after a 429 a gateway restart no longer changed anything upstream: the same
`ses_…` walked into the new exit's bucket and 429'd again.

Mix a 16-byte nonce, minted once per process, into the hash. A running
gateway still derives one stable session per conversation, while a restart
re-mints the ids and starts a fresh upstream identity. Official client
sessions are still forwarded verbatim.

Refs jasonxu114514#159, jasonxu114514#158
@cheerc

cheerc commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Keeping this open as identity hygiene, with the framing corrected: it is not the fix for the 429s reported in #159.

Measured today on one fixed exit: the session from the original report (ses_e8722fbfaffe5rgVd0mpvuAo3Z) returns 200, so the session was not what refused those requests. The reproducible trigger was client tool names — a Claude Code body declaring both ReportFindings and ScheduleWakeup 429s deterministically and passes once either name is renamed; fixed separately in #161.

This PR is still worth having for the reason it was written: CanonicalSessionID derives one id per (scope, signal, UTC day), so a process that restarts after an egress change presents the same upstream session again. Re-minting per process is the cheapest way to leave a session the upstream has charged, and per-process stability keeps affinity and prompt cache intact. The salt mixes into the hash only — it cannot change the id's shape or its timestamp, so it cannot introduce the session-shape 429.

If it lands, the claim to put in the commit and README is "a restart presents a new session for a conversation", not "this fixes the free-tier 429".

@jasonxu114514
jasonxu114514 merged commit 9946e66 into jasonxu114514:main Oct 29, 2026
5 checks passed
jasonxu114514 added a commit that referenced this pull request Oct 29, 2026
The repository ships without test files; strips the tests that arrived
with #160, #161 and #162 after their squash merges.
@cheerc
cheerc deleted the fix/identity-restart-session-salt branch October 29, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Derived session ids survive a restart, so a burned ses_… keeps 429ing after the egress changes

2 participants