Skip to content

fix(codex): read the CLI update job ledger again when a concurrent write replaced it - #8712

Merged
lidge-jun merged 2 commits into
devfrom
fix/dev-red7-cli-update-read-race
Nov 13, 2026
Merged

lidge-jun merged 2 commits into
devfrom
fix/dev-red7-cli-update-read-race

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Summary

test 1/5 of dev batch 43496185962 (Linux, self-hosted) failed tests/codex-integration/codex-cli-update-apply.test.ts "published Node launcher -> isolated management plan, lost-response apply retry, and CLI status" with "management API treated an unowned running record as stopped". The case starts an old owner's job in the test process without awaiting it and immediately asks the separate management server for its status; the expected answer is ownerState: unknown. The message also covers a non-OK response, and that is what happened here.

The POSIX job ledger (src/codex/cli-update-job.ts) is rewritten atomically (a private temp file renamed over jobs.json) while readers take no lock. readStore opens the file, checks it, then checks that the path still names that file. A rename landing in between unlinks the file the reader holds (nlink 0) and makes the path name a newer inode; both were refused as unsafe_store, so GET /api/codex/cli-update/jobs/:id answered 503 job_state_unavailable for a healthy job. In this test the owner's own running transition is the concurrent write. The same race reaches the dashboard and ocx system codex-cli-update status whenever they read during a job transition.

A read now starts over when the file it opened was replaced, for at most five attempts. Each attempt applies every existing check; a hard-linked (nlink > 1) or symlinked store is still refused immediately, and a store still being replaced at the last attempt is refused as before. The Windows ledger is a separate store and is unchanged.

Verdict on the batch failure: a product race, timing-dependent but not a test flake. No commit between the last batch where this case passed and this one touched the job ledger.

Verification

  • Reproduced with two processes on macOS (a writer renaming a copy of a real ledger over jobs.json in a loop, a reader calling readCodexCliUpdateJob): with no writer, 13,463 of 13,463 reads succeeded. With ~10k replacements/s, 2,575 of 28,104 reads failed with unsafe_store before the fix and 0 of 27,125 failed after it.
  • New tests/codex-integration/codex-cli-update-job-read-race.test.ts (POSIX store only) uses a test hook that readStore calls after the open and after the descriptor checks, and replaces the ledger at exactly those points: replaced once at either point, the read starts over and returns the job (two attempts); replaced during every attempt, it is refused after five; a hard-linked ledger is refused after one. With the attempt bound set back to one (the previous behaviour) the first three cases fail and the hard-link case still passes. (A first version raced a writer process; review pointed out it could pass without a collision, so it was replaced.)
  • bun run typecheck: exit 0. Layout guards: 29 pass / 0 fail. bun run privacy:scan and bun run structure:check: passed.
  • codex-cli-update-apply.test.ts and three codex-cli-update-plan.test.ts cases fail on this machine on unchanged origin/dev as well (they need a real Node/npm toolchain that this host's node shim does not provide). Their result here comes from this PR's Ubuntu CI.
  • NOT RUN: the full suite locally.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Summary by CodeRabbit

  • Bug Fixes
    • Improved the reliability of Codex CLI update status when its saved information changes during a read.
    • Invalid or repeatedly changing saved information is now rejected instead of being used, helping prevent inconsistent update status.

…ite replaced it

The POSIX job ledger is replaced atomically (private temp file renamed over
jobs.json) and readers take no lock. A rename landing after a reader opened the
file unlinks the file it holds (nlink 0) and makes the path name a newer inode, and
both were refused as unsafe_store. The management status route then answered 503
job_state_unavailable for a healthy job, which is how dev batch 43496185962
failed test 1/5: a restarted management server read an old owner's job while the
owner recorded it as running.

A read now starts over when the file it opened was replaced, up to five
attempts; every attempt applies all checks, a hard-linked or symlinked store is
still refused at once, and a store still being replaced at the last attempt is
refused as before. Two processes on macOS: 2,575 of 28,104 reads failed under
~10k replacements/s before, 0 of 27,125 after. The new regression test failed
with 45-75 refusals per second before and passed 20 of 20 runs after.
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 13, 2026 20:38
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T20:40:28.183162Z 29c37c0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 13, 2026
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

readStore now retries reads when the ledger is replaced or unlinked during identity checks. It rejects unsafe files, including hard links and symlinks, and returns unsafe_store if replacement persists across five attempts. POSIX integration tests cover these cases.

Changes: Ledger read race

Layer / File(s) Summary
Ledger read retries and race coverage
src/codex/cli-update-job.ts, tests/codex-integration/codex-cli-update-job-read-race.test.ts, scripts/test-layout/layout-codex-collision.json, tests/fixtures/test-layout-expected.json
readStore checks file and path identity, retries up to five times after a detected replacement, and rejects unsafe files. POSIX tests cover replacement after opening and descriptor checks, persistent replacement, and hard-link rejection. The test is registered in both layout mappings.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant RaceTest
  participant readStore
  participant LedgerWriter
  RaceTest->>readStore: Read the admitted job
  readStore->>RaceTest: Invoke the opened or descriptor-checked test hook
  RaceTest->>LedgerWriter: Replace the ledger atomically
  readStore->>readStore: Recheck path identity and retry when replacement is detected
  readStore-->>RaceTest: Return the job or reject with unsafe_store
Loading

Suggested reviewers: luvs01

Merge Risk: Minimal · up to b2401

This change stops healthy update jobs from being reported unavailable when the ledger is replaced during a read, while still refusing unsafe stores. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix (retrying the CLI update job ledger read after a concurrent atomic replacement) and matches the changeset: readStore in src/codex/cli-update-job.ts retries up to five times whe…
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 fix (retrying the CLI update job ledger read after a concurrent atomic replacement) and matches the changeset: readStore in src/codex/cli-update-job.ts retries up to five times when the file is replaced after opening, and the new read-race regression test covers that path. It is specific and does not describe unrelated work. It is 84 characters, a bit long, but still clear.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Commits

Reviewing files that changed from the base of the PR and between 0d3b1fa and b2401d2.

Files selected for processing: 4

  • scripts/test-layout/layout-codex-collision.json
  • src/codex/cli-update-job.ts
  • tests/codex-integration/codex-cli-update-job-read-race.test.ts
  • tests/fixtures/test-layout-expected.json

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 29c37c0b40

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +131 to +133
}
const bytes = Buffer.alloc(CODEX_CLI_JOB_STORE_MAX_BYTES + 1);
const length = readSync(fd, bytes, 0, bytes.length, 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update the owning architecture document for ledger read retries

This changes the POSIX ledger's acceptance behavior for replaced files, but the commit does not update structure/codex-cli-update.md, which structure/INDEX.md explicitly identifies as the owner of src/codex/cli-update-job.ts. src/AGENTS.md requires the owning documentation to be updated in the same change. Document the lock-free reader's five-attempt replacement retry, immediate refusal of unsafe files, and refusal on exhaustion so the maintained ledger contract includes this new distinction.

AGENTS.md reference: src/AGENTS.md:L11-L11

Useful? React with 👍 / 👎.

… a racing writer

Review of #8712: the regression test raced a writer process against reads, so it
could pass without a collision and fail on the bounded refusal the fix keeps.
readStore now calls a test hook after the open and after the descriptor checks,
and the test replaces the ledger at exactly those points: once (read again and
succeed, two attempts), on every attempt (refused after five), and a hard-linked
ledger (refused after one). With the attempt bound set back to one, which is the
previous behaviour, the first three fail and the hard-link case still passes.
@lidge-jun

Copy link
Copy Markdown
Owner Author

Maintainer judgment: integrating into dev under the MAINTAINERS.md maintainer-integration exception, without a second maintainer approval. This is maintainer integration, not a self-approval.

Exact head b2401d2:

  • PR CI: all 33 checks on this head passed or were skipped by policy. The Ubuntu shards ran the four new ledger-race cases and the batch's failing case ("published Node launcher -> isolated management plan, lost-response apply retry, and CLI status", 8,413 ms), all passing.
  • Final review (gpt-6.1-terra, high), round 2 on this exact head by a fresh reviewer: PASS, no findings. The test hook is null by default and exercises both refusal paths; reads stay on the validated descriptor; fds close through finally; with the attempt bound at 1 (old behaviour) three cases fail and the hard-link case passes. Round 1 (head 29c37c0) FAIL on a racing, nondeterministic regression test, fixed in b2401d2 with the seam-based cases.
  • Reproduced before the fix: two processes, 2,575 of 28,104 reads refused as unsafe_store under ~10k replacements/s; 0 of 27,125 after.
  • NOT RUN: full suite locally (codex-cli-update-apply needs a real Node/npm toolchain absent on the author's host; covered by CI above).

Store-safety change reviewed with the security lens in both rounds: symlink, hard-link, mode, uid and size checks unchanged and immediate; retries bounded. No outstanding maintainer change requests.

@lidge-jun
lidge-jun merged commit 28680d5 into dev Nov 13, 2026
41 checks passed
@lidge-jun
lidge-jun deleted the fix/dev-red7-cli-update-read-race branch November 13, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant