@@ -26,15 +26,300 @@ not required to replace a placeholder before this phase works. |
26 | 26 | All current-code citations and diff context below were rechecked on 2026-08-04 at |
27 | 27 | `7bde9e0c977721fc0b9d8617c85ff17de7c07658`. |
28 | 28 |
|
| 29 | +## Round 6 — three findings that came from running the code, not reading it |
| 30 | + |
| 31 | +Rechecked on 2026-08-05 at `86e5d677b`, with executed probes rather than citation. |
| 32 | +All three change what B must build. |
| 33 | + |
| 34 | +### F1 — the coordinator refuses to open on every routed install (blocking) |
| 35 | + |
| 36 | +`openCodexCoordinatorTransaction` initializes a missing row only after |
| 37 | +`assertInitialStateCanBeCreated()` proves the record is not legacy and |
| 38 | +`classifyNativeRoutedResidue()` returns `clean` |
| 39 | +(`src/codex/transition-state.ts:263-303`). A routed `config.toml` — which is |
| 40 | +exactly what every user with the proxy applied has — is residue. Executed against |
| 41 | +a temp home containing a routed `model_provider = "opencodex"` block: |
| 42 | + |
| 43 | +```text |
| 44 | +REFUSED: CodexCoordinatorLegacyAmbiguousError |
| 45 | + | A missing coordinator row cannot be initialized while native Codex routing residue exists. |
| 46 | +``` |
| 47 | + |
| 48 | +The same probe against a clean home opens and returns |
| 49 | +`{"nativeBefore":0,"nativeAfter":1,...}`. |
| 50 | + |
| 51 | +So the plan's instruction to "place WP9's fixed catalog/native commit under the new |
| 52 | +lock" is, as written, a **regression for every existing routed installation**: a |
| 53 | +catalog refresh that succeeds today would begin refusing. The compatibility-adoption |
| 54 | +path that `005_contract.md:705-800` designed for this is **not implemented** — |
| 55 | +`withCodexCompatibilityNativeHandoff` and `adoption-pending` have zero occurrences |
| 56 | +in `src/`. |
| 57 | + |
| 58 | +WP11 therefore ships the lock **without rewiring `convergence.ts` to require N**. |
| 59 | +`commitCodexCatalogCandidate` keeps `K -> C` (`src/codex/convergence.ts:393-406`), |
| 60 | +which is already correct and already cross-process safe. `convergence.ts` moves |
| 61 | +under N in WP12, together with the admission pipeline and the adoption path that |
| 62 | +makes opening N legal on a routed home. Landing the lock and its rewiring in one |
| 63 | +phase would mean landing a refusal for the current user base to satisfy a document. |
| 64 | + |
| 65 | +That is not a scope dodge: WP11's own accept criteria (C5/C6/C7/C18) are about |
| 66 | +acquisition, identity, and namespace. None of them requires the catalog seam to be |
| 67 | +the first caller. What WP11 must NOT do is ship a module with no caller — defect #10 |
| 68 | +in this unit was exactly that — so the deliverable includes the real |
| 69 | +`convergeCodexNativeUnderLock` entry consumed by a production route, gated to the |
| 70 | +homes where N can legally open, plus the falsifiable test that a routed home refuses |
| 71 | +with a typed reason instead of throwing. |
| 72 | + |
| 73 | +#### The direction asymmetry — real, but it does NOT supply a caller |
| 74 | + |
| 75 | +"N cannot open" is not uniform, and the difference decides which production path |
| 76 | +WP11 can legally serve. Probed both directions against real temp homes: |
| 77 | + |
| 78 | +| Operation | Home state when N is taken | Result | |
| 79 | +|---|---|---| |
| 80 | +| **apply** (route Codex at the proxy) | clean — not yet routed | `OPEN OK`, expectation `{nativeBefore:0, nativeAfter:1}` | |
| 81 | +| **restore** (unroute back to native) | already routed | `REFUSED` — legacy-ambiguous | |
| 82 | + |
| 83 | +A first apply on an unrouted home is the one state the strict initializer accepts, |
| 84 | +because the residue it refuses is the routing this operation has not performed yet. |
| 85 | + |
| 86 | +**That is where I claimed a production caller, and review round 6 proved the claim |
| 87 | +false.** The paragraph that stood here named `injectCodexConfig` |
| 88 | +(`src/codex/inject.ts:487`) as a caller WP11 could serve. Three facts kill it: |
| 89 | + |
| 90 | +1. Production reaches `injectCodexConfig` **directly** from `src/codex/sync.ts:58,110` |
| 91 | + and `src/cli/init.ts:197`. None of them goes through `convergence.ts`, which is |
| 92 | + the only module this phase's write scope was allowed to modify. Adding an entry |
| 93 | + point to `convergence.ts` therefore adds an export nothing calls. |
| 94 | +2. `injectCodexConfig` cannot become the synchronous commit callback as it stands. |
| 95 | + It journals (`src/codex/inject.ts:530`), writes native files |
| 96 | + (`:601`), and then **awaits** the history job (`:614`). The synchronous native |
| 97 | + section has to be split from post-N history dispatch before any of it can sit |
| 98 | + under N. |
| 99 | +3. There is no runtime producer of a full `AdmissionSnapshot` at all. It exists only |
| 100 | + as an interface (`src/codex/convergence-types.ts:495`); the sole thing production |
| 101 | + builds today is `CatalogAdmissionSnapshot`. WP11's own API requires the former. |
| 102 | + |
| 103 | +So the narrowing as written *was* a dodge, and its own falsification test — "if the |
| 104 | +apply path cannot legally take N either, fold WP11 into WP12" — has now fired. The |
| 105 | +resolution is recorded below rather than argued away. |
| 106 | + |
| 107 | +#### Resolution: WP11 becomes mechanism-only, and its caller moves to WP12 |
| 108 | + |
| 109 | +WP11 ships `src/codex/codex-write-lock.ts` and its tests, and **nothing in |
| 110 | +`convergence.ts`**. It is explicitly a mechanism phase whose consumer is WP12, in the |
| 111 | +same way WP8b shipped the contract before anything consumed it. This is not the same |
| 112 | +failure as defect #10 (a coordinator no production caller ever initialized) provided |
| 113 | +two conditions hold, and B must satisfy both: |
| 114 | + |
| 115 | +- The goalplan records WP12 as the phase that supplies the admission producer, the |
| 116 | + apply/restore split in `inject.ts`, and the first real call edge. WP11 is not |
| 117 | + closable as "done" in the sense of "in production"; it is closable as "the |
| 118 | + mechanism is correct and proven by real two-process tests". |
| 119 | +- WP11's tests drive the **production module**, not a copy, through real child |
| 120 | + processes — which its test plan already requires — so the mechanism is executed |
| 121 | + even before a production caller exists. |
| 122 | + |
| 123 | +If WP12 does not land the caller, the honest state of this unit is that the lock is |
| 124 | +unused, and the PR body must say exactly that instead of implying the substrate is |
| 125 | +live. That sentence is a deliverable of WP14, not a footnote. |
| 126 | + |
| 127 | +### F2 — the initializer's residue guard reads the AMBIENT home, not the locked one |
| 128 | + |
| 129 | +Absence-as-guarantee #14. `classifyNativeRoutedResidue()` resolves its own home |
| 130 | +through `getCodexHome()` (`src/codex/native-residue.ts:524`), which re-reads |
| 131 | +`process.env.CODEX_HOME` (`src/codex/paths.ts:32-35`). `readIntegrationRecord()` |
| 132 | +resolves its path the same ambient way. But `resolveCodexCoordinatorDatabasePath` |
| 133 | +is keyed by the **caller-supplied canonical home**. Executed with ambient |
| 134 | +`CODEX_HOME` pointing at a clean directory and the explicit target home routed: |
| 135 | + |
| 136 | +```text |
| 137 | +ambient CODEX_HOME = <tmp>/clean |
| 138 | +explicit home = <tmp>/routed |
| 139 | +OPENED OK for a ROUTED explicit home -> residue check used the AMBIENT home |
| 140 | +``` |
| 141 | + |
| 142 | +The guard passed by inspecting a directory that is not the one being locked. Every |
| 143 | +existing caller happens to pass the ambient home, so the defect is latent today and |
| 144 | +becomes live the moment WP11 accepts an explicit `codexHome` — which its API does. |
| 145 | + |
| 146 | +Consequence for WP11: `CodexWriteLockOptions.codexHome` may not be forwarded to a |
| 147 | +coordinator whose safety guard reads a different home. WP11 refuses with |
| 148 | +`authority_not_proven` when the canonical target home is not identical to the |
| 149 | +ambient `getCodexHome()` result, and a test drives the mismatch. Making the |
| 150 | +guard home-parameterized is WP12's job (it owns admission); WP11 must not silently |
| 151 | +accept a home whose residue was never checked. |
| 152 | + |
| 153 | +**The obvious version of that remedy is itself a TOCTOU**, and it was caught by |
| 154 | +probing rather than by reading. `getCodexHome()` re-resolves `process.env.CODEX_HOME` |
| 155 | +on every call (`src/codex/paths.ts:32-35`), so two calls inside one operation can |
| 156 | +return two different directories: |
| 157 | + |
| 158 | +```text |
| 159 | +same call, two answers: true | a -> b |
| 160 | +``` |
| 161 | + |
| 162 | +A comparison that calls `getCodexHome()` once to validate and lets the coordinator |
| 163 | +call it again to check residue proves nothing: the second read is a fresh read. So |
| 164 | +the check is not "compare the two", it is **resolve the ambient home exactly once, |
| 165 | +canonicalize it, use that single value for both the comparison and the lock target, |
| 166 | +and refuse if the caller supplied anything else**. WP11 never re-reads the ambient |
| 167 | +home after that point, and the residue guard's own later read is covered only |
| 168 | +because it is bounded by N — a second process that changes the environment cannot |
| 169 | +change ours, and our own code does not mutate `CODEX_HOME` mid-operation. |
| 170 | + |
| 171 | +That last clause is a claim, not an assumption, so it needs a guard rather than a |
| 172 | +comment: the B phase adds a test that fails if any production module under `src/` |
| 173 | +assigns to `process.env.CODEX_HOME`. Today `rg -n "env.CODEX_HOME\s*=" src` finds |
| 174 | +nothing, and an absence that nothing enforces is precisely the defect this unit has |
| 175 | +now hit fourteen times. |
| 176 | + |
| 177 | +**And the grep proves the claim false, which is why it was run.** Production code |
| 178 | +does assign `process.env.CODEX_HOME`, in two places: |
| 179 | + |
| 180 | +- `src/codex/history-worker.ts:158` — WP10 added this in THIS session. The Worker |
| 181 | + receives the parent's home in its run message and installs it before doing any |
| 182 | + history work. |
| 183 | +- `src/storage/policy-worker.ts:34` — the same bootstrap shape, older. |
| 184 | + |
| 185 | +Both are Worker entry bootstraps: they set the variable once, at thread start, |
| 186 | +before that thread resolves any path, so neither mutates the home of a thread that |
| 187 | +is mid-operation. That makes the invariant WP11 needs narrower and checkable: |
| 188 | +**no assignment to `process.env.CODEX_HOME` outside a Worker bootstrap**, i.e. none |
| 189 | +on a thread that could be holding N. The B-phase guard asserts exactly that, with |
| 190 | +the two known bootstraps as named exceptions, so a third assignment added on a |
| 191 | +request path fails the test instead of silently invalidating the once-resolved home. |
| 192 | + |
| 193 | +Writing this down mattered more than the guard does. The paragraph above originally |
| 194 | +asserted the grep was empty; running it produced two hits, one of them added by this |
| 195 | +very session. That is instance #15 in miniature — the absence was asserted from |
| 196 | +memory of the design instead of from the tree — and it is the reason every claim in |
| 197 | +this phase gets executed rather than recalled. |
| 198 | + |
| 199 | +#### Two more holes review found in this same remedy |
| 200 | + |
| 201 | +The canonicalize-once rule above is necessary and still not sufficient. |
| 202 | + |
| 203 | +**A symlinked default home refuses itself.** With no `CODEX_HOME` set, |
| 204 | +`getCodexHome()` returns `defaultCodexHome()` **without** `realpath` |
| 205 | +(`src/codex/paths.ts:23`), while WP11's target is `realpathSync.native`-canonical. On |
| 206 | +a machine where `~` or `~/.codex` is a symlink, the two strings differ and the lock |
| 207 | +refuses a home that is in fact the same directory. The comparison must canonicalize |
| 208 | +**both** sides before comparing, never the target alone. |
| 209 | + |
| 210 | +**The comparison must be adjacent to the open.** Acquisition retries across `await` |
| 211 | +boundaries. A comparison performed before the retry loop and an |
| 212 | +`openCodexCoordinatorTransaction` performed after it are separated by suspension |
| 213 | +points, so the guard re-reads the environment in between. The canonical ambient home |
| 214 | +is resolved once, and the equality check is re-asserted **immediately before** the |
| 215 | +synchronous open with no `await` between them. |
| 216 | + |
| 217 | +**And one citation in the original F2 text was simply wrong.** It said |
| 218 | +`readIntegrationRecord()` resolves its path from the ambient CODEX_HOME. It does not: |
| 219 | +its path comes from `getConfigDir()` (`src/codex/integration-record.ts:28`), which is |
| 220 | +`OPENCODEX_HOME`, a different variable. Only `classifyNativeRoutedResidue()` reads |
| 221 | +the ambient Codex home. The defect is real and the mechanism was misdescribed; the |
| 222 | +narrower true statement is what B implements against. |
| 223 | + |
| 224 | +### F4 — absence-as-guarantee #15: ACL success is cached by pathname, not identity |
| 225 | + |
| 226 | +This one is a live defect in shipped code, not only in the plan. |
| 227 | +`hardenStableLockFile` delegates to `hardenSecretPathAsync` |
| 228 | +(`src/codex/native-main-lock-file.ts:127`), which returns success purely because the |
| 229 | +**pathname** is in a module-level `Set<string>` (`src/lib/windows-secret-acl.ts:36,461`). |
| 230 | +Nothing in that cache is bound to the file's identity. Review's executed probe |
| 231 | +hardened a path, unlinked it, recreated it at the same name, and re-hardened: |
| 232 | + |
| 233 | +```text |
| 234 | +{"firstCalls":3,"totalCalls":3,"replacementWasRechecked":false} |
| 235 | +``` |
| 236 | + |
| 237 | +The replacement file received **zero** ACL calls. The plan's "validate the DB, refuse |
| 238 | +substitution" section inherits this: it treats "no path change observed" as proof |
| 239 | +that the cached ACL still describes the current inode. It does not, and on Windows |
| 240 | +that means a substituted coordinator database can be adopted with the previous |
| 241 | +file's hardening credited to it. |
| 242 | + |
| 243 | +Remedy for B: bind the ACL success cache to stable file identity, or invalidate the |
| 244 | +entry when the stable descriptor's last reference closes so the next acquisition |
| 245 | +revalidates. The regression test must be release → replace → **reacquire**; |
| 246 | +substituting the file during a single held acquisition does not reach the cache and |
| 247 | +would pass with the fix removed. |
| 248 | + |
| 249 | +### F5 — deadline exhaustion must not be a permanent refusal |
| 250 | + |
| 251 | +The result taxonomy carries `busy/deadline` as retryable, but the acquisition section |
| 252 | +classifies ACL failure — including timeout — as a non-retryable refusal, and says only |
| 253 | +SQLite busy retries. A caller-supplied short remaining deadline can time ACL work out |
| 254 | +without proving anything unsafe; that is exhaustion, not unsafe authority. Worse, the |
| 255 | +current ACL code rethrows a sanitized untyped `Error` |
| 256 | +(`src/lib/windows-secret-acl.ts:484`), discarding the `ETIMEDOUT` discriminator the |
| 257 | +classification would need. |
| 258 | + |
| 259 | +B preserves a typed timeout discriminator through sanitization, maps outer-budget |
| 260 | +exhaustion to `busy/deadline`, and reserves non-retryable refusal for verified |
| 261 | +ACL/ownership/path failures. |
| 262 | + |
| 263 | +### F6 — citation corrections |
| 264 | + |
| 265 | +Verified accurate: `native-main-lock-file.ts:35-55,74-131`, |
| 266 | +`native-main-owner.ts:75-91`, `home.ts:135-146`, `paths.ts:6-24`. |
| 267 | + |
| 268 | +Corrected: |
| 269 | + |
| 270 | +| Cited | Problem | Use instead | |
| 271 | +|---|---|---| |
| 272 | +| `src/config.ts:1853-1859` | those lines are config-generation reads, not the conditional-rename statement | `src/config.ts:1949` | |
| 273 | +| `src/config.ts:1767-1818` | stops before callback execution, commit, rollback, and close | `src/config.ts:1779-1839` | |
| 274 | +| `windows-secret-acl.ts:217-328,404-494` | misses `HardenOptions`/deadline clamping and the exported async entry | add `:45` and `:512` | |
| 275 | + |
| 276 | +### The type block must not redeclare the capability |
| 277 | + |
| 278 | +The public-contract fence below prints its own `unique symbol` brand and its own |
| 279 | +`CodexCoordinatorTransaction`. Both already exist: |
| 280 | +`src/codex/convergence-types.ts:331` owns the public interface and |
| 281 | +`src/codex/transition-state.ts:148` owns the private brand. The fences compile in |
| 282 | +isolation — which is exactly why the fence check did not catch this — but an |
| 283 | +integration compile assigning `openCodexCoordinatorTransaction(...).capability` to |
| 284 | +the plan's local type fails: |
| 285 | + |
| 286 | +```text |
| 287 | +TS2741: Property '[codexCoordinatorTransactionBrand]' is missing in type |
| 288 | +'convergence-types.CodexCoordinatorTransaction' |
| 289 | +but required in type 'plan.CodexCoordinatorTransaction'. |
| 290 | +``` |
| 291 | + |
| 292 | +The implementation **imports** `CodexCoordinatorTransaction` from |
| 293 | +`./convergence-types` and declares no local brand. The fence below is retained as the |
| 294 | +historical shape; where it declares the brand and interface, read the import. |
| 295 | + |
| 296 | +### F3 — `journal_mode` and reentrancy needed no new mechanism |
| 297 | + |
| 298 | +A pinned-Bun probe shows `bun:sqlite` opens in `delete` mode by default and leaves |
| 299 | +no `-wal`/`-shm` sidecar after a committed `BEGIN IMMEDIATE`, so the plan's |
| 300 | +"forces rollback journal mode" is a verification, not a conversion. And a second |
| 301 | +`openCodexCoordinatorTransaction` on a held path in the SAME process already fails |
| 302 | +with `SQLiteError: database is locked`, because `busy_timeout = 0` is set before |
| 303 | +`BEGIN IMMEDIATE` (`src/codex/transition-state.ts:416`). |
| 304 | + |
| 305 | +`AsyncLocalStorage` reentrancy detection therefore is not what prevents a |
| 306 | +same-process deadlock — SQLite already does. It exists to turn an |
| 307 | +indistinguishable `busy` into a typed `refused/reentrant`, which is a diagnosis |
| 308 | +improvement, not an exclusion mechanism. The test must assert the typed reason, |
| 309 | +not "does not hang", or it proves nothing SQLite was not already doing. |
| 310 | + |
29 | 311 | ## IN / OUT |
30 | 312 |
|
31 | 313 | IN: |
32 | 314 |
|
33 | 315 | - `src/codex/codex-write-lock.ts` (NEW) — exact contract module name; canonical |
34 | 316 | target identity, effective-user namespace, finite async acquisition, synchronous |
35 | 317 | coordinated commit, release, and typed lock mechanics. |
36 | | -- `src/codex/convergence.ts` (MODIFY) — place WP9's fixed catalog/native commit |
37 | | - under the new lock and pass the contract `AdmissionSnapshot`/`CommitExpectation`. |
| 318 | +- `src/codex/convergence.ts` (MODIFY, **narrowed by F1**) — add |
| 319 | + `convergeCodexNativeUnderLock`, the production entry that takes N and publishes a |
| 320 | + transition. It does NOT move the existing catalog commit under N: that seam keeps |
| 321 | + `K -> C` until WP12 supplies admission and the adoption path, because N refuses to |
| 322 | + open on a routed home today and rewiring now would break every applied install. |
38 | 323 | - `src/codex/transition-state.ts` (MODIFY through its public owner API) — lend |
39 | 324 | WP11 a narrow opaque capability backed by the already-open coordinator |
40 | 325 | transaction; this module remains the sole native-generation/transition-row owner. |
@@ -514,6 +799,15 @@ syncs, restores, or ensures the proxy; port 10100 is untouched. |
514 | 799 | and USERPROFILE prove one lock for one user/home. |
515 | 800 | - Config generation, authoritative admission re-read, native/provenance writes, |
516 | 801 | and the conditional transition-row update share N->C; C releases before N commits. |
| 802 | +- **F1** — a routed CODEX_HOME returns typed `refused/lock_unavailable` carrying the |
| 803 | + coordinator's legacy-ambiguous reason rather than throwing, and the existing |
| 804 | + catalog commit path still succeeds on a routed home because it was NOT moved |
| 805 | + under N in this phase. |
| 806 | +- **F2** — an explicit `codexHome` that differs from the ambient `getCodexHome()` |
| 807 | + returns `refused/authority_not_proven` and creates no namespace, because the |
| 808 | + coordinator's residue guard would otherwise have inspected a different directory. |
| 809 | +- **F3** — same-process reentrancy returns `refused/reentrant`, distinguishable from |
| 810 | + the `busy` that SQLite alone would have produced. |
517 | 811 | - `transition-state.ts` alone owns native generation/txId/history scheduling; JSON |
518 | 812 | owns none of them, and WP11 never opens a second coordinator connection in C. |
519 | 813 | - Lock edges are N->C and short fail-fast H->N only; stale history jobs are rejected |
|