Skip to content

infra: atomically replace sync JSON writes#60589

Merged
gumadeiras merged 6 commits intomainfrom
codex/issue-54295-item-1
Apr 4, 2026
Merged

infra: atomically replace sync JSON writes#60589
gumadeiras merged 6 commits intomainfrom
codex/issue-54295-item-1

Conversation

@gumadeiras
Copy link
Copy Markdown
Member

Summary

  • replace sync JSON writes with sibling-temp atomic rename semantics
  • remove the pre-read existsSync check so reads rely on direct read/parse fallback
  • add regression coverage for the temp-path replacement flow

Verification

  • pnpm format src/infra/json-file.ts src/infra/json-file.test.ts
  • direct concurrent writer/reader script against src/infra/json-file.ts (parseFailures: 0 across 835 reads)
  • pnpm test -- src/infra/json-file.test.ts hung in this environment, so I did not count it as passing

Part of #54295.

Copilot AI review requested due to automatic review settings April 3, 2026 23:30
@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Apr 3, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 3, 2026

Greptile Summary

This PR replaces synchronous direct writes in src/infra/json-file.ts with an atomic sibling-temp-file + renameSync pattern, and removes the existsSync TOCTOU race in loadJsonFile in favor of a pure try/catch. A regression test verifying the rename path is included.

The implementation is correct. The only noteworthy finding is a redundant chmodSync(pathname, 0o600) after renameSync — POSIX rename preserves source-file permissions, so tmpPath's 0o600 mode transfers automatically, making the post-rename chmod a no-op. It is harmless but can be removed.

Confidence Score: 5/5

Safe to merge; the atomic write semantics are correct and the only finding is a redundant chmod that has no runtime impact.

All findings are P2 (style/cleanup). The core correctness property — atomic replace via same-directory renameSync — is implemented correctly. No data-integrity or reliability risk introduced.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/infra/json-file.ts
Line: 29-33

Comment:
**Redundant `chmodSync` after rename**

On POSIX, `renameSync` atomically replaces the destination and carries over the source file's permissions — `tmpPath` already has `0o600` set both via the `mode` option in `writeFileSync` and the preceding `chmodSync(tmpPath, 0o600)`. The post-rename `chmodSync(pathname, 0o600)` is therefore a no-op on every platform that supports `chmod`, and a no-op on Windows too (where chmod is ignored). Consider removing it to simplify the flow.

```suggestion
    fs.renameSync(tmpPath, pathname);
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "infra: atomically replace sync JSON writ..." | Re-trigger Greptile

Comment thread src/infra/json-file.ts Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the infra JSON file helpers to reduce TOCTOU risk and prevent readers from observing partially-written JSON by switching saves to a temp-file + rename flow, and simplifying loads by removing pre-read existence checks.

Changes:

  • Replace direct JSON writes with sibling temp-file writes followed by renameSync replacement.
  • Remove existsSync pre-check from JSON reads and rely on readFileSync/JSON.parse failure fallback.
  • Add a regression test asserting the temp-path → destination rename behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/infra/json-file.ts Implements atomic(ish) JSON saves via temp file + rename and simplifies loadJsonFile by removing existsSync pre-check.
src/infra/json-file.test.ts Adds coverage for the temp-file replacement flow using a renameSync spy.

Comment thread src/infra/json-file.test.ts Outdated
Comment thread src/infra/json-file.test.ts Outdated
Copy link
Copy Markdown

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

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: 507e40ad90

ℹ️ 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 thread src/infra/json-file.ts
@gumadeiras gumadeiras self-assigned this Apr 3, 2026
Copy link
Copy Markdown

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

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: b7a5995c8b

ℹ️ 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 thread src/infra/json-file.ts Outdated
Copy link
Copy Markdown

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

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: dd3072f322

ℹ️ 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 thread src/infra/json-file.ts Outdated
@gumadeiras gumadeiras force-pushed the codex/issue-54295-item-1 branch from 60ee5ec to cb8ed77 Compare April 4, 2026 00:21
@gumadeiras gumadeiras merged commit 300fb36 into main Apr 4, 2026
8 checks passed
@gumadeiras
Copy link
Copy Markdown
Member Author

Merged via squash.

Thanks @gumadeiras!

@gumadeiras gumadeiras deleted the codex/issue-54295-item-1 branch April 4, 2026 00:21
KimGLee pushed a commit to KimGLee/openclaw that referenced this pull request Apr 4, 2026
Merged via squash.

Prepared head SHA: cb8ed77
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
Merged via squash.

Prepared head SHA: cb8ed77
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
Merged via squash.

Prepared head SHA: cb8ed77
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
Merged via squash.

Prepared head SHA: cb8ed77
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants