Skip to content

fix(codex): stop the managed config mirror from reordering the file on every pass - #44467

Merged
brennanb2025 merged 6 commits into
mainfrom
brennanb2025/codex-hook-pass-fixed-point
Nov 19, 2026
Merged

brennanb2025 merged 6 commits into
mainfrom
brennanb2025/codex-hook-pass-fixed-point

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Every Codex launch runs a hook-preparation pass on the Electron main thread: approvals, settings mirror, hook trust, status. With a 330 KB managed config.toml (~1,900 worktrees), that pass costs a few hundred ms of CPU. A reuse cache is supposed to skip it when nothing changed, but it only stores a pass that wrote nothing.

When the managed home's daemon socket path is too long for sun_path, the mirror adds a [features] daemon_auto_start = false guard. That's true for typical macOS user-data paths and long Linux paths. The mirror re-added the guard at the end of the file on every pass. Project trust grants and hook approvals append their tables after it. So after any grant or approval:

  • the next pass moved blocks around and republished the whole config;
  • that pass wasn't cached, so the launch after it paid a second full pass.

The legacy shared-home refresh had the same ordering. Separately, stripCodexDaemonOverride copied every line kept so far at each table header, which is quadratic in the number of tables. The bug is in stable (v1.4.263).

Fix

  • Guard the system settings before the managed home's own tables (projects, hook approvals, bookkeeping) are merged on. This applies to both the managed mirror and the legacy shared-home refresh. The guard keeps one position, tables appended later stay in place, and a pass after a grant writes nothing, so the next launch reuses it.
  • Strip a leading BOM before guarding, so a BOM-led inline features = {…} is still recognized and doesn't get a second [features] table.
  • Measure the socket path once per mirror and pass the decision to pure helpers.
  • Remove the quadratic copy in the guard strip.
  • Existing configs settle with one rewrite. The guard still never reaches ~/.codex, because promotion strips it.

Short managed-home paths, which is the usual Windows case, never get the guard, so this PR doesn't change their pass. Moving the pass's CPU off the main thread is the follow-up.

Measurements

Linux QA machine, PR head a78ff44 against main 8bc232f, two runs each. The setup is a 332 KB system config and the real installForLaunchPrep; the burst is 5 launches, each after a project-trust grant to both configs; the next launch has no change.

Home path 5-launch burst, CPU config rewrites in burst next launch
long (guard on) main 1,834 / 1,887 ms 5 full pass, 377 / 389 ms
long (guard on) this PR 1,229 / 1,255 ms 0 cached, 1.3 / 1.4 ms
short (guard off) main 1,026 / 1,181 ms 0 cached
short (guard off) this PR 1,077 / 1,121 ms 0 cached

Test

codex-config-mirror-daemon-guard.test.ts uses the real guard with a long home path.

  • After tables are appended, another mirror pass leaves the config byte-identical. Cases: plain, the user's own [features] table, CRLF, each with a mirrored user hook approval.
  • A BOM-led inline features table stays a single valid definition.
  • The legacy refresh leaves a settled managed layout unchanged.

Each case fails without its fix, except the own-[features] control. Tests that switch the guard off now mock the socket-path measurement.

Files Added Deleted Net
Test 10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​120 $\color{#cf222e}{\Huge{\mathbf{−}}}$​9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​111
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​131 $\color{#cf222e}{\Huge{\mathbf{−}}}$​70 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​61

…n every pass

When the managed Codex home's daemon socket path is too long (typical
on macOS), Orca adds a [features] daemon_auto_start guard to the
managed config.toml. The mirror re-added that table at the very end on
every pass, while project trust grants and hook approvals append their
tables after it, so each launch-preparation pass after a change moved
blocks around and republished the 330 KB config. A pass that writes is
never cached, so the next launch ran the whole hook preparation again.

The guard is now applied to the settings part before the managed home's
own tables are joined on, so it keeps one position and appended tables
stay where they are: a pass after a trust grant now writes nothing and
the next launch reuses it. Existing configs settle with one rewrite.
… the same way, measuring the socket path once
@brennanb2025

brennanb2025 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Perf scan 3: PR #44467 at 9000f7c

I compared origin/main (merge base a7e6362, the same as scan 2) with head 9000f7c and graded the full git diff origin/main...HEAD (15 files, +251/−79).

Findings

None. No P0 or P1.

What changed since scan 2 (a78ff44, PASS)

git diff a78ff44e1a9..9000f7ccad1 --stat shows one file and one line: config/main-blocking-helper-baseline.txt lowers its count for src/main/codex/configuration/shared-server/codex-daemon-socket-path-guard.ts from 2 to 1. No runtime source changed and the merge base is the same, so scan 2's evidence still applies as written:

  • trigger, population and cost for each changed function
  • reproduced timings: head strip 2.8 ms → 23.4 ms at 10× input, base 4.7 ms → 455 ms; guarded pre-merge upsert 2.0 ms at 190 KB, 21.6 ms at 1.9 MB
  • the write fixed point (1 whole-file rewrite per pass → 0 at steady state)
  • the six-question answers

None of it is repeated here.

Is the new baseline number right?

  • The file still makes exactly one blocking call (realpathSync.native, line 32), the same as base. The PR removed the wrapper applyCodexDaemonSocketGuard, which called the blocking helper codexDaemonSocketPathExceedsLimit. The rename to applyCodexDaemonOverride(config, homePath, socketPathExceedsLimit) makes callers pass the result in instead. That removes one helper reference, so 2 → 1 matches the source.
  • Tightening the count is safe. Lowering the number only makes the ratchet stricter, and nothing on the runtime path changes.
  • I ran the check. node config/scripts/static-analysis/ratchets/runtime-lifecycle/check-main-blocking-helper-ratchet.mjs at this head printed main-blocking-helper ratchet OK — 6537 grandfathered blocking helper reference(s). and exited 0. The worktree stayed clean.

Regression classes touched

The dispositions are the same as scan 2, since no runtime code changed:

  • Whole-file rewrite per change: fixed.
  • Hot-path rescans: the quadratic strip is fixed.
  • Update published when nothing changed: disproved.
  • Re-reading unchanged input: disproved.
  • Per-entity state outliving the entity: no new code.
  • Eager startup imports: disproved.
  • Sync fs on Electron main: disproved. realpathSync stays at 1 per mirrored home per pass. The baseline now records one fewer helper reference, and no *Sync call was added.

Six questions

These carry over from scan 2 unchanged; the code they graded is byte-identical at this head:

  • Q1–Q5: reviewed with evidence.
  • Q6: not applicable, because the mirror is a synchronous one-shot pass.

Evidence

  • Diff check (this head): confirmed that runtime source is unchanged since a78ff44.
  • Ratchet run (this head, macOS): passed, as above.
  • Carried over from scan 2: reproduced micro-benchmarks, 17/17 vitest passes, and the author's Linux QA numbers.

Residual

These carry over from scan 2:

  • The fix for the quadratic strip has no deterministic regression test. It removes a problem that predates this PR, so it is a suggested follow-up, not a blocker.
  • No packaged-build timing on Windows or WSL.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Status: ready

Problem: with a ~330 KB Codex config (~1,900 worktrees), after every project-trust grant or hook approval the hook-preparation pass on the main thread rewrote the managed config.toml. It moved the daemon-socket guard back to the end of the file, so the launch-prep cache missed and the next launch paid a second full pass. This affects homes whose daemon socket path exceeds sun_path, which is typical on macOS and on long Linux paths.

User-facing change: Codex launches after a trust grant no longer rewrite the managed config, and the launch after them reuses the cached pass. Launch bursts on long-path homes use about 33% less main-thread CPU. Config contents are unchanged, except that the guard's [features] block now sits after the user's settings instead of at the end of the file.

Fixed during review:

  • A BOM-led inline features config could get a second [features] table (P1).
  • The legacy shared-home refresh still reordered the file (P2).
  • Blocking-helper ratchet accounting after the split.
  • Removed an unused wrapper and its inert test mocks.

Deferred: short-path homes (usual on Windows) don't get the guard, so their pass is unchanged. Moving the pass off the main thread is the follow-up PR.

Verified:

  • Benchmark (below), the new mirror tests (each fails without its fix), and 11 related test files.
  • remote-check PASS on 9000f7c.
  • Perf scan PASS (comment above).
  • Fresh Codex and Claude reviewers clean on the final commit.

Benchmark: head=a78ff44e1a9eb59dcc4914d278c4ff9c99f40cc2 gate=PASS host=omarchy (Linux QA machine) corpus=332 KB config, 5 grant launches then 1 unchanged launch, 2 runs per arm, base=8bc232f5ed8 files=src/main/codex/configuration/settings/codex-config-mirror.ts,src/main/codex/configuration/settings/codex-config-mirror-source.ts,src/main/codex/configuration/settings/codex-config-runtime-owned-values.ts,src/main/codex/configuration/shared-server/codex-daemon-socket-path-guard.ts listeners=none (direct installForLaunchPrep) | guard on: burst 1834/1887 → 1229/1255 ms, rewrites 5 → 0, next launch full pass 377/389 ms → cached 1.3/1.4 ms | guard off: 1026/1181 → 1077/1121 ms, unchanged. Only commit after a78ff44 is 9000f7c (baseline file only).

@brennanb2025
brennanb2025 marked this pull request as ready for review November 19, 2026 06:14
@coderabbitai

coderabbitai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Config mirroring now uses dedicated helpers to resolve and prepare the source config, and to guard it before merging. Managed and legacy mirroring measure whether the runtime home’s daemon socket path exceeds its limit and pass that result to runtime-owned config application. The daemon override API accepts the measured result. Tests cover repeated mirroring with long runtime paths, several TOML formats, and updated guard mocks.

Merge Risk: Minimal · up to 9000f

This change stops the managed Codex config from being reordered and rewritten on every launch when the daemon socket path is long, so launch preparation can be cached. Short-path behavior is unchanged. No concrete merge-blocking issue was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the managed Codex config mirror stops reordering the config file on every pass. The change moves the daemon socket guard ahead of the managed home's appended tables, s…
Description check Passed The description follows the repository template closely. It covers the problem, the fix, measurements, and tests. It has the ELI5, Risk, Testing, and Linked Issue sections in the template form. The Li…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the managed Codex config mirror stops reordering the config file on every pass. The change moves the daemon socket guard ahead of the managed home's appended tables, so repeated passes leave the file unchanged. The title uses the conventional fix(codex) prefix and fits the changeset.

Full details: Description check

Explanation

The description follows the repository template closely. It covers the problem, the fix, measurements, and tests. It has the ELI5, Risk, Testing, and Linked Issue sections in the template form. The Linked Issue section is still the unfilled template text, "Fixes #", with no issue number. The ELI5, Visual Proof, Risk level, AI Disclosure, Review, and Checklist sections are also not filled in. The "What Changed" and "Why" sections are partly covered under "Fix" and "Problem", but they do not use the template headings. The description is mostly complete, so the check passes.

Resolution

Optional: add an issue number under Linked Issue (the template requires one for outside contributors), add an ELI5 section, set the Risk level, and mark the Testing and Checklist items.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ca3a9b83-2195-4d0e-8a15-2ffce5d25b60
Commits

Reviewing files that changed from the base of the PR and between a7e6362 and 9000f7c.

Files selected for processing: 15

  • config/main-blocking-helper-baseline.txt
  • src/main/codex-accounts/runtime-home/credentials/runtime-home-managed-auth-recovery.test.ts
  • src/main/codex-accounts/runtime-home/credentials/runtime-home-mirror-login-copy.test.ts
  • src/main/codex-accounts/runtime-home/credentials/runtime-home-retained-auth-provenance.test.ts
  • src/main/codex-accounts/runtime-home/legacy-host/legacy-shared-config-compatibility.test.ts
  • src/main/codex-accounts/runtime-home/routing/runtime-home-real-home-lane-routing.test.ts
  • src/main/codex/configuration/settings/codex-config-mirror-daemon-guard.test.ts
  • src/main/codex/configuration/settings/codex-config-mirror-hook-trust-carry.test.ts
  • src/main/codex/configuration/settings/codex-config-mirror-source.ts
  • src/main/codex/configuration/settings/codex-config-mirror.test.ts
  • src/main/codex/configuration/settings/codex-config-mirror.ts
  • src/main/codex/configuration/settings/codex-config-runtime-owned-values.ts
  • src/main/codex/configuration/settings/config-features-settings-promotion.test.ts
  • src/main/codex/configuration/settings/config-settings-promotion.test.ts
  • src/main/codex/configuration/shared-server/codex-daemon-socket-path-guard.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Benchmark rerun on the exact final head, which only changed a baseline file after the earlier run. Same harness: a 332 KB config, a burst of 5 grant launches, then 1 unchanged launch; 2 runs per arm, on a Linux QA machine under shared load. The direct installForLaunchPrep pass mounts no listeners.

Arm main (9c28817) this PR
Long-path home (socket guard on): burst 3159 / 3628 ms, 5 config rewrites 1775 / 1593 ms, 0 rewrites
Long-path home: next unchanged launch full pass, 664 / 598 ms cached, 2.8 / 2.2 ms
Short-path home (guard off): burst 1602 / 2599 ms 1310 / 1473 ms

The machine was busier than in the first run, so absolute times are higher for both arms. The relative result is unchanged.

Benchmark: head=9000f7ccad1bfc107b5b3a7e95a40e0042c8eda6 gate=PASS host=omarchy corpus=332 KB config, 5 grant launches then 1 unchanged launch, 2 runs per arm, base=9c28817280b6 files=src/main/codex/configuration/settings/codex-config-mirror.ts,src/main/codex/configuration/settings/codex-config-mirror-source.ts,src/main/codex/configuration/settings/codex-config-runtime-owned-values.ts,src/main/codex/configuration/shared-server/codex-daemon-socket-path-guard.ts listeners=none (direct installForLaunchPrep) | guard on: burst 3159/3628 → 1775/1593 ms, rewrites 5 → 0, next launch full pass 664/598 ms → cached 2.8/2.2 ms | guard off: 1602/2599 → 1310/1473 ms

@brennanb2025
brennanb2025 merged commit e076c68 into main Nov 19, 2026
159 of 165 checks passed
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.

1 participant