Skip to content

Commit bb6a6fb

Browse files
authored
Merge pull request #2998 from lidge-jun/codex/shared-transient-budget-continuation
fix(responses): keep the terminal-guard continuation on the shared transient budget
2 parents 9cd4e4b + 3998bb4 commit bb6a6fb

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎docs-site/src/content/docs/guides/codex-app-models.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ them by ignoring `visibility`. See [Codex Desktop native-allowlist compatibility
105105
for the command, disable-key semantics, and safety constraints.
106106

107107
## Integration path
108+
109+
`ocx init`, `ocx start`, and `ocx sync` wire the shared Codex config and catalog into the proxy; see
110+
[Codex Integration](/guides/codex-integration/) for config injection, catalog sync, shims, WebSocket
111+
fallback, and restore mechanics.
112+
108113
## Native quota fallback limitation
109114

110115
When the Codex app exhausts its native five-hour quota it can switch to a reserve
@@ -137,11 +142,6 @@ mode is active; if the client rewrites or refuses it before the request leaves,
137142
setting changes that. Treat the explicit-selection route as worth trying rather than a confirmed
138143
workaround.
139144

140-
141-
`ocx init`, `ocx start`, and `ocx sync` wire the shared Codex config and catalog into the proxy; see
142-
[Codex Integration](/guides/codex-integration/) for config injection, catalog sync, shims, WebSocket
143-
fallback, and restore mechanics.
144-
145145
## Why routed models show up
146146

147147
Codex's model picker expects Codex-shaped catalog entries. opencodex builds routed entries by cloning

‎src/server/responses/core.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6144,7 +6144,15 @@ async function handleResponsesInner(
61446144
{
61456145
abortSignal: upstream.signal,
61466146
label: safeHostLabel(builtContinuationRequest.url),
6147-
...(continuationTransientPolicy ? { attempts: continuationTransientPolicy.attempts } : {}),
6147+
// Same request-scoped budget as the initial send and the 429/rotation refetches:
6148+
// a terminal-guard continuation is another leg of ONE request, so handing it a
6149+
// fresh `attempts` would let one request exceed the configured total-send ceiling.
6150+
...(continuationTransientPolicy
6151+
? {
6152+
attempts: remainingTransientSendBudget(continuationTransientPolicy.attempts),
6153+
onSendsConsumed: noteTransientSends,
6154+
}
6155+
: {}),
61486156
},
61496157
);
61506158
} finally {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { readFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
5+
const source = (relative: string): string =>
6+
readFileSync(join(import.meta.dir, "..", "src", ...relative.split("/")), "utf8");
7+
8+
/**
9+
* `transientRetryOn5xx.attempts` is ONE request-wide total-send budget, not a per-leg
10+
* allowance. A Responses request can reach upstream on several legs — the initial send, a
11+
* 429/account-rotation refetch, and the terminal-guard continuation — and each leg calls
12+
* `fetchWithTransientRetry` separately. The budget only holds if every leg draws from the
13+
* shared request-scoped counter.
14+
*
15+
* The continuation leg shipped on the raw policy value instead, so a request that reached it
16+
* received a fresh full `attempts` allowance: with `attempts: 3` an initial send that had
17+
* already spent its budget could still emit three more upstream sends. Runtime coverage in
18+
* `tests/upstream-transient-retry.test.ts` proves the helper reports and honors a remainder;
19+
* it cannot prove that every call site asks for one, because a site that forgets simply
20+
* passes a larger number. This asserts the wiring at the source, which is the only place the
21+
* omission is visible.
22+
*/
23+
describe("transient send budget stays request-scoped", () => {
24+
test("every transient-retry call site draws from the shared counter", () => {
25+
const core = source("server/responses/core.ts");
26+
27+
// One owner per request, declared before any leg can send.
28+
expect(core.match(/let transientSendsUsed = 0;/g)).toHaveLength(1);
29+
expect(core.match(/const remainingTransientSendBudget = \(budget: number\): number =>/g)).toHaveLength(1);
30+
31+
// Initial send, 429/rotation refetch, and terminal-guard continuation: three legs, three
32+
// reports into the same counter.
33+
expect(core.match(/onSendsConsumed: noteTransientSends/g)).toHaveLength(3);
34+
35+
// The refetch and continuation legs must ask for the REMAINDER. Only the initial send may
36+
// pass a policy value directly, because nothing has been spent yet.
37+
expect(core.match(/attempts: remainingTransientSendBudget\(/g)).toHaveLength(2);
38+
expect(core).toContain("attempts: remainingTransientSendBudget(refetchTransientPolicy.attempts)");
39+
expect(core).toContain("attempts: remainingTransientSendBudget(continuationTransientPolicy.attempts)");
40+
41+
// The regressed shape: a leg handing itself a fresh full budget.
42+
expect(core).not.toContain("attempts: continuationTransientPolicy.attempts }");
43+
expect(core).not.toContain("attempts: refetchTransientPolicy.attempts }");
44+
});
45+
46+
test("the helper still exposes the seam those call sites depend on", () => {
47+
const retry = source("lib/upstream-retry.ts");
48+
expect(retry).toContain("onSendsConsumed?: (sends: number) => void;");
49+
// Reported in `finally` so every exit path — return, throw, abort — feeds the counter.
50+
expect(retry).toMatch(/} finally \{\n\s*opts\.onSendsConsumed\?\.\(sent\);/);
51+
});
52+
});

0 commit comments

Comments
 (0)