Skip to content

[Bug] A request-owned Codex bearer gets no Pool affinity key, so the account changes on almost every request #6998

Closed as not planned

Description

@vibewriter-hand

Target repository: lidge-jun/opencodex. Version measured: 2.83.0 (installed tree). The same rule is
present on the dev branch.

Summary

When Codex Desktop runs with ChatGPT sign-in, it sends its own authorization bearer on every
request. OCX classifies such a request as a "request-owned main credential". poolStateEligible()
then returns false for it, so the request gets no conversation key, no affinity binding is read or
written, and the account-selection ring moves on every request. One conversation therefore changes
account on almost every turn, and each change drops the carried continuation state.

Before the switch to ChatGPT sign-in the same proxy kept one account per conversation ("reused" rows
dominated every hour). After the switch, "reused" went to zero and the account changed on almost every
consecutive pair of the same conversation.

Environment

  • OCX 2.83.0 (npm global install).
  • Codex CLI 0.162.1 with ChatGPT sign-in; the client sends session-id, thread-id,
    x-codex-window-id, authorization and chatgpt-account-id on the WebSocket upgrade and on the
    POST.
  • Pool config: accountPoolStrategy=round-robin, accountPoolStickyLimit=1, three GPT accounts.

Reproduction

The account ring is reproducible without live traffic, by driving the real resolver with Codex-style
headers and a bearer:

  • Call resolveCodexAuthContext for ONE conversation, 12 requests, with a caller bearer and the
    Codex headers.
  • Result: the selected account cycles main, pool-a, pool-b, main, pool-a, pool-b, ... (a new pick on
    every request).
  • Call it again without a bearer: the same conversation stays on one account.

The in-process account order matches the live usage rows exactly.

Observed (measured)

  • 183 GPT rows after the switch: 124 new_bind, 0 reused, 59 rows without an affinity value.
  • 163 of 176 consecutive same-conversation pairs changed account.
  • "reused" rows before the switch: about 250 to 1,800 per hour. After the switch: 0 in every 10-minute
    bucket.
  • Each account change logs "dropped continuation state after a Codex pool account change", so the next
    request resends the full context.

Cause (file:line, 2.83.0)

# Fact Location
1 A caller-supplied forwardable Codex bearer makes the request a request-owned main credential. src/server/responses/core-auth.ts:186-193, src/server/auth-cors.ts:564-569
2 poolStateEligible() returns false for such a request, so affinityKey is undefined and no lineage is recorded. src/codex/auth-context.ts:196-201, :1196-1204
3 resolveCodexAccountForThreadDetailed(null, ...) runs; the strategy pick binds nothing because the thread id is null. src/codex/auth-context.ts:1320-1328, src/codex/routing.ts:1121-1130, src/codex/routing/selection.ts:477-496 (line 494: if (commitAffinity && threadId))
4 The usage row still reports new_bind/healthy when the thread id is null, so the log looks like "bound every time". src/codex/routing/thread-affinity.ts:123-132
5 Each account change drops the carried state. src/server/responses/account-change-state.ts:264

The docblock at src/codex/auth-context.ts:238-246 and the comment at :1193-1195 state the design
rule: a request-owned main bearer must never read or write a binding. There is no sticky fallback for
that credential, so the ring is the only behavior available to it.

Expected

A request that carries a caller bearer and belongs to a conversation should keep one Pool account for
that conversation, the same way a request without a bearer does. Only an exact account selector should
stay outside Pool state.

Minimal fix

Give a request-owned main bearer a conversation key, and never leave a binding on main. The diff below
is applied on top of the installed 2.83.0 file. It changes one code expression and corrects two
comments and one docblock.

--- a/src/codex/auth-context.ts
+++ b/src/codex/auth-context.ts
@@ -51,6 +51,7 @@
 import { releaseTransientProbe } from "../routing/probe-lease";
+import { deleteThreadAffinitiesForAccount } from "./routing/thread-affinity";
 import {
   codexConversationIdentity,
   recordCodexThreadLineage,
@@ -192,12 +193,46 @@
-/** The one expression both the resolution below and any preview must agree on. */
+/**
+ * A request-owned main bearer (Codex Desktop with ChatGPT sign-in sends one on every request) still
+ * belongs to a conversation, so it gets a conversation key and may bind to a stored Pool account.
+ * Only an exact account selector (including the Reserve pin) stays outside Pool state. The binding
+ * is never kept on main itself: see releaseRequestOwnedMainBinding.
+ */
 function poolStateEligible(
   fixedAccountId: string | undefined,
-  requestScopedMainCredential: boolean,
+  _requestScopedMainCredential: boolean,
 ): boolean {
-  return fixedAccountId === undefined && !requestScopedMainCredential;
+  return fixedAccountId === undefined;
+}
+
+/** A request-owned main context has no account id, so no outcome recorder sees a 429 on it and
+ *  nothing could release a binding to main. Drop the binding the pick just made. */
+function releaseRequestOwnedMainBinding(affinityKey: string | undefined): void {
+  if (affinityKey !== undefined) deleteThreadAffinitiesForAccount(affinityKey, MAIN_CODEX_ACCOUNT_ID);
+}
+
+/** A conversation with a request-owned bearer never had a key before, so it never met the
+ *  idle-expiry error. The expired binding is already deleted, so a second pick is the fresh
+ *  continuation that conversation always got. Other credentials keep the error. */
+function resolveThreadAllowingFreshPick(
+  requestOwnedBearer: boolean,
+  ...args: Parameters<typeof resolveCodexAccountForThreadDetailed>
+): ReturnType<typeof resolveCodexAccountForThreadDetailed> {
+  const first = resolveCodexAccountForThreadDetailed(...args);
+  return first.status === "expired" && requestOwnedBearer
+    ? resolveCodexAccountForThreadDetailed(...args)
+    : first;
 }

Two call sites change with it:

@@ -1317,7 +1352,8 @@
-      : resolveCodexAccountForThreadDetailed(
+      : resolveThreadAllowingFreshPick(
+          requestScopedMainCredential,
           affinityKey ?? null,
@@ -1346,6 +1382,7 @@
     if (selected === MAIN_CODEX_ACCOUNT_ID && requestScopedMainCredential) {
       releaseTransientProbeGrant();
+      releaseRequestOwnedMainBinding(affinityKey);
       return await resolveCallerOwnedMainContext();
     }

Result: a conversation binds to one Pool account and keeps it; main serves first turns only, because a
pick that lands on main drops that key's main entries; a new conversation takes the next account in
the ring. No change to weights, thresholds, strategy or stickyLimit.

Risk notes

  • R1. One long conversation stays on one account, so that account drains faster while the others idle.
    Subagents of one session share the key (cohort key by session-id), so a large fan-out lands on one
    account. This is the intended cache-friendly behavior; relief comes from new conversations and the
    429 release.
  • R2. A request-owned main has no outcome recorder (recordCodexUpstreamOutcome returns when the
    account id is null), so a 429 on main that is not yet visible in main's usage data would not release
    the binding. The patch removes the main binding on every main pick, so there is no main binding to
    strand; a real 429 on main was not exercised.
  • R3. The patch reverses the safety rule in the :1193-1195 comment. Anchored patches fail closed if
    upstream changes these lines.
  • R4. A request-owned bearer keeps a conversation key, but main never joins the binding: a pick that
    lands on main deletes the main entries of that key.
  • R5. No credential is stored by the change. "main" still means the request's own bearer.

Impact

Every GPT turn in a ChatGPT-signed-in desktop session changes account and resends the full context.
The cache read share stays high, so the cost is the dropped continuation state and the full-body
resend, not the cache reads. A conversation can also meet the 24-hour idle-expiry error as HTTP 409
"start a new session", which the same conversation never got before.

Activity

  1. github-actions commented on Oct 12, 2026

    @github-actions
    Contributor

    Issue closed: use an issue template

    Thanks for taking the time to submit this issue. It was closed automatically because it was not opened with a recognized issue template (Bug report, Feature request, Documentation, or Provider compatibility). Freeform and API-opened issues cannot skip this check.

    • This looks like a bug report but it does not use the Bug report template headings (for example Description/Log entry instead of Summary).

    Please edit the issue to add:

    • Use the Bug report template, or edit this issue to include: Client or integration, Summary, Reproduction, Version, and Operating system.
    • Retitling with [Bug]: and applying the bug label alone is not enough without those section headings filled in.

    See the Contributing guide for details.

    Once the report passes the automated checks, it will be reopened automatically unless a maintainer has changed its state.

  2. Ingwannu commented on Oct 12, 2026

    @Ingwannu
    Owner

    Confirmed the current dev source path at 3e58c2ce3e: poolStateEligible excludes request-owned bearers, resolution passes a null affinity key, and the round-robin pick consequently creates no binding. affinityAfterRelease(null, undefined) can still report new_bind, so that log label is not proof of a stored binding. This is source confirmation, not an independent live account measurement.

    The proposed change reverses an explicit ownership invariant, not just a missing sticky option. In particular, deleting a just-created main binding means a new conversation may move off caller-owned main on its next turn; it does not make main sticky. Also, the public eligibility preview, lineage/cohort derivation, selected credential generation, 429/account-change scrub and idle-expiry policy must remain consistent. I would not hotpatch the installed runtime with the supplied diff.

    @lidge-jun please decide whether request-owned requests may own an affinity binding only after selection of a stored Pool credential, while caller-owned main remains outside file-main/durable ownership. A narrow fix should have offline resolver regressions for repeated same-thread caller bearers, main-first selection, different caller identities with the same client headers, token/selection replacement, main and stored-account 429, exact/Reserve pins, lineage siblings, expiry, and preview/final-resolution agreement. No credential content should enter affinity state. The issue was auto-closed by the template gate (comment 6126248387), not fixed. Please restore the required Bug report headings so the gate can reconsider it; the technical contract decision remains unresolved. No manual reopening/waiver was performed; no production config, auth state, provider call or scan changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions