|
| 1 | +# 120 — #2108 phase 1: log the concrete native-main gate reason |
| 2 | + |
| 3 | +Work-phase `wp2108`. One PABCD cycle. Phase 2 (retryable boot-time `unknown`) |
| 4 | +is explicitly NOT in this cycle. |
| 5 | + |
| 6 | +**Revision note.** The first draft of this doc proposed a response header |
| 7 | +`x-ocx-native-main-gate-reason` alongside the log line. An adversarial audit |
| 8 | +returned `fail` and it was correct; the header is removed. What follows is the |
| 9 | +design that survived, with the refutation recorded because it is the reason the |
| 10 | +scope is what it is. |
| 11 | + |
| 12 | +## The decision that gated this cycle |
| 13 | + |
| 14 | +`040_2108_windows_reboot_gate.md` said: check PR #2101 before starting, because |
| 15 | +it edits both files phase 1 touches. |
| 16 | + |
| 17 | +**#2101 is still OPEN** (`REVIEW_REQUIRED`, head `ingw/fix-daybreak-account-entitlement`, |
| 18 | +1397 lines, last touched 2026-08-19T12:00:56Z). The collision is real, so the |
| 19 | +only question is whether it is avoidable. |
| 20 | + |
| 21 | +Its hunks in `src/codex/auth-context.ts` cover, in old-file lines: 23-28, |
| 22 | +237-242, 260-267, 273-278, 302-308, 312-318, 325-330. Its hunks in |
| 23 | +`src/server/index.ts` cover 58, 894, 906, 940, 957, 971, 1038. |
| 24 | + |
| 25 | +**`auth-context.ts:115-131` is clear** — nearest hunk edges are 28 and 237. |
| 26 | +This cycle changes nothing else, in any file. |
| 27 | + |
| 28 | +That is what makes this an implement rather than a defer. All three throw sites |
| 29 | +sit in or beside #2101's hunks: `:313` (inside 312-318), `:319` (one line past |
| 30 | +that hunk, still inside its rebase blast radius), and `:326` (inside 325-330). |
| 31 | +A design that had to edit any of them would have been a defer. |
| 32 | + |
| 33 | +## What ships |
| 34 | + |
| 35 | +One file: `src/codex/auth-context.ts`, lines 115-131 only. No other production |
| 36 | +file, and no signature any other file calls. |
| 37 | + |
| 38 | +### The reason is captured and logged in the constructor |
| 39 | + |
| 40 | +`CodexMainProfileDrainingError` has a zero-arg constructor today. It gains: |
| 41 | + |
| 42 | +- a `readonly reason?: NativeMainStartupBlockReason`, read from |
| 43 | + `nativeMainStartupGateSnapshot()` **in the constructor**; |
| 44 | +- one `console.warn` naming that reason, deduped per distinct reason. |
| 45 | + |
| 46 | +Capturing in the constructor is the whole design. It is the only moment where |
| 47 | +the reason is both in scope and true — every catch site has already lost it, and |
| 48 | +re-reading the gate later can observe a different value. |
| 49 | + |
| 50 | +### Why there is no header |
| 51 | + |
| 52 | +The first draft wanted a header so the reason would be machine-readable. Three |
| 53 | +findings killed it, and each one independently: |
| 54 | + |
| 55 | +**It cannot reach the caller without editing five files.** |
| 56 | +`codexMainProfileDrainingResponse()` is called with zero arguments at |
| 57 | +`core.ts:1121`, `compact.ts:390`, `search.ts:126`, `live.ts:542`, |
| 58 | +`images.ts:441`. Threading a reason means editing all five — and `core.ts` and |
| 59 | +`compact.ts` are both edited by #2101. The collision we set out to avoid comes |
| 60 | +straight back. |
| 61 | + |
| 62 | +**Having the response re-read the gate is worse, not cheaper.** It looks like a |
| 63 | +one-file fix, but `nativeMainStartupGateSnapshot()` returns fresh state each |
| 64 | +call and `completeNativeMainRecovery()` can flip it to `ready` between the throw |
| 65 | +and the catch. The header would then be blank or wrong exactly when recovery is |
| 66 | +racing — which is the scenario #2108 is about. |
| 67 | + |
| 68 | +**It would not reach the Claude surface anyway.** `claude-messages.ts:823-829` |
| 69 | +builds a new `Response` with a hand-written header object carrying only |
| 70 | +`Content-Type` and `Retry-After`; every other header is dropped. The WebSocket |
| 71 | +fence path exposes no response headers at all. |
| 72 | + |
| 73 | +So the log line is not a consolation prize. It is the only surface that reaches |
| 74 | +every path this fence can fire on. |
| 75 | + |
| 76 | +### Why not the message, and why not `/api/logs` |
| 77 | + |
| 78 | +Both are closed off, which is worth stating because they are the obvious first |
| 79 | +guesses. |
| 80 | + |
| 81 | +**The message must not change.** `claude-messages.ts:818-820` identifies this |
| 82 | +response by `message === CODEX_MAIN_PROFILE_MAINTENANCE_MESSAGE`. Break that |
| 83 | +match and `isTransientUpstreamStatus(503)` takes over, remapping the fence to |
| 84 | +Anthropic 529 — a local maintenance state would start telling Claude Code to |
| 85 | +back off and retry an upstream that was never involved. |
| 86 | + |
| 87 | +**`/api/logs` cannot carry it either.** `upstreamError` is populated by reading |
| 88 | +the response body back and taking `json.error.message` and nothing else |
| 89 | +(`request-log.ts:755-759`, via `relay.ts:434-442`). Headers are already gone. |
| 90 | +`error.code` is likewise filtered: `requestLogErrorCode` pins 503 to |
| 91 | +`server_is_overloaded` and refuses to forward arbitrary codes by design |
| 92 | +(`request-log.ts:508-540`, comment at :514). |
| 93 | + |
| 94 | +That leaves stdout — which is where the reporter was reading anyway. Their |
| 95 | +excerpt interleaves `[17:25:23] opencodex service wrapper start` with the 503 |
| 96 | +lines, so a `console.warn` lands in the stream they already quoted. |
| 97 | + |
| 98 | +### Dedup, and the site that has no reason |
| 99 | + |
| 100 | +Deduped per distinct reason: the reporter's log shows three 503s in eleven |
| 101 | +seconds and a real client retries harder than that. A per-request line buries |
| 102 | +the signal it exists to produce. Dedup state is module-level, so the module |
| 103 | +exports a reset for tests — otherwise the second test in a Bun process observes |
| 104 | +nothing and the assertion passes vacuously. |
| 105 | + |
| 106 | +The third throw site, `:326`, fires when `claimMainProfile()` refuses. That is |
| 107 | +the **turn-drain** fence (`lifecycle.ts:180`), not the startup gate — line 318 |
| 108 | +has already established `nativeMainTrafficBlocked` is false there, so the |
| 109 | +snapshot reads `ready` and carries no reason. |
| 110 | + |
| 111 | +That site must stay silent. Constructor capture gets this right by |
| 112 | +construction: a reason is recorded only when the snapshot is `blocked`. A 503 |
| 113 | +with no reason therefore means "not the startup fence", which is itself a |
| 114 | +distinction #2108 needs — the reporter could not make it. |
| 115 | + |
| 116 | +Only `reason` is emitted. The snapshot also carries `homeId`, which is derived |
| 117 | +from a profile directory path and has no business in a log line. |
| 118 | + |
| 119 | +## Why logging and not the mechanism |
| 120 | + |
| 121 | +The trigger is still unidentified. `040` names two candidates that settle to |
| 122 | +different reasons: ACL fail-closed gives `owner-unavailable` |
| 123 | +(`native-profile-startup.ts:138-139`), probe fail-closed gives |
| 124 | +`ownership-unknown` (`:311`). One log line separates them. |
| 125 | + |
| 126 | +**No test anywhere asserts `owner-unavailable`.** It is produced at |
| 127 | +`native-profile-startup.ts:139` and asserted nowhere in the suite — the |
| 128 | +candidate branch we most need named in the field is the one with no coverage. |
| 129 | + |
| 130 | +## Test plan |
| 131 | + |
| 132 | +Drive the real gate, never a module mock — the existing tests already do this |
| 133 | +(`tests/codex-auth-context.test.ts:250-254` via |
| 134 | +`initializeNativeMainStartupGate({ probeRecoveryState: () => "manual" })`, |
| 135 | +`tests/native-profile-startup.test.ts:291-292` via |
| 136 | +`blockNativeMainStartupForUnownedServiceHome`). Capture the log with a local |
| 137 | +`spyOn(console, "warn")`, the repo idiom (`app-owned-memory.test.ts:237`). |
| 138 | + |
| 139 | +1. `ownership-unknown` fence → `err.reason` is `ownership-unknown` **and the |
| 140 | + warn fires carrying it**. The log assertion is the deliverable; without it |
| 141 | + this cycle ships something unobserved. |
| 142 | +2. `foreign-ownership` fence → same, different value. Proves the value is read |
| 143 | + rather than hardcoded. |
| 144 | +3. Second construction under the same reason → no second warn. Pins the dedup. |
| 145 | +4. No fence active → no reason, no warn. Pins the `:326` turn-drain case. |
| 146 | +5. Message, status, `Retry-After` unchanged. This is the claude-messages guard. |
| 147 | + Worth knowing it is weaker than it sounds: grepping |
| 148 | + `tests/claude-messages-endpoint.test.ts` for `nativeMainFence`, `529`, or the |
| 149 | + message constant returns nothing, so the 503→529 remap has no direct |
| 150 | + regression test of its own. |
| 151 | + |
| 152 | +**Red-drive is mandatory.** Lesson 1 of this session: the first #1527 attempt |
| 153 | +patched a helper that was not on the failure path and all ten tests still |
| 154 | +passed. Each assertion must be seen failing before the fix. |
| 155 | + |
| 156 | +## Verification |
| 157 | + |
| 158 | +``` |
| 159 | +bun test tests/codex-auth-context.test.ts tests/native-profile-startup.test.ts tests/native-profile-drain-server.test.ts |
| 160 | +bun x tsc --noEmit |
| 161 | +bun run privacy:scan |
| 162 | +``` |
| 163 | + |
| 164 | +`privacy:scan` is not ceremony here — this cycle adds a log statement, which is |
| 165 | +the exact thing that scan exists to police. |
| 166 | + |
| 167 | +CI is ignored this cycle by standing instruction while the merge train churns; |
| 168 | +local green is the judgment surface. |
| 169 | + |
| 170 | +## Stack position |
| 171 | + |
| 172 | +Stacks on `fix/cursor-abort-teardown` (#2118), targeting it. Retarget to `dev` |
| 173 | +after the parents land. |
| 174 | + |
0 commit comments