Skip to content

fix(save-user-section): atomic temp+rename write (profile Phase 2 hardening) - #66

Merged
cryptolir merged 2 commits into
mainfrom
feat/save-user-section-atomic-write
Jun 16, 2026
Merged

cryptolir merged 2 commits into
mainfrom
feat/save-user-section-atomic-write

Conversation

@cryptolir

Copy link
Copy Markdown
Owner

Phase 2 hardening (per the adversarial review on the per-user-profile plan #64). The agent writer did fs.readFile then fs.writeFile with no atomicity and no lock, while the dashboard side uses flock+temp+rename CAS. A concurrent reader (the dashboard section reader) or the new app_profile seed's CAS read could observe a half-written file → torn markers → the reader fails closed with a 500. This writes to a temp file in the same dir then rename(2) over the target (atomic on one filesystem), so readers only ever see a complete file.

Follow-up (tracked): unify the lock domain so the agent writer and the dashboard CAS serialise (the agent writer should also flock the same <file>.lock), closing the remaining lost-update race. Atomic rename alone fixes the corruption/torn-read case; the lost-update self-heals (the seed is idempotent + best-effort; the agent re-writes its block next turn).

Gate: vitest save-user-section 13/13; file oxfmt-clean. (Reaches the live life agent on the next gateway image roll — Phase 3.)

🤖 Generated with Claude Code

The agent writer did fs.readFile then fs.writeFile with no atomicity and no
lock, so a concurrent reader (the dashboard section reader) or the app_profile
seed's CAS read could observe a half-written file -> torn markers -> the reader
fails closed with a 500. Write to a temp file in the same dir then rename(2)
over the target (atomic on one filesystem). Flagged by the Phase 2 adversarial
review. Unifying the lock domain with the dashboard flock CAS so the two
writers serialise is a tracked follow-up. vitest 13/13.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

@codex review — small Phase 2 hardening: the openclaw agent writer (save_user_section) now writes via temp+rename instead of fs.writeFile, so a concurrent reader / the new dashboard seed CAS read never observes a half-written file (torn markers -> reader 500). Please confirm the rename is atomic + the temp cleanup on failure is sound. Follow-up (noted in the PR): unify the lock domain with the dashboard flock so the two writers serialise. vitest 13/13.

@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: 8711b73c2f

ℹ️ 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/agents/tools/save-user-section.ts Outdated
usersDir,
`.${appUserId}.md.tmp.${crypto.randomBytes(6).toString("hex")}`,
);
await fs.writeFile(tmpPath, next, "utf-8");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clean up temp files when the write fails

When fs.writeFile(tmpPath, ...) fails after creating/truncating the temp file (for example ENOSPC/EIO or a short partial write), execution never reaches the rename catch, so .user.md.tmp.* files are left behind in users/. Repeated failed saves can accumulate partial temp files and further consume the workspace disk, so the write and rename should share a cleanup path (e.g. try around both steps, skipping rm only after a successful rename).

Useful? React with 👍 / 👎.

@cryptolir cryptolir left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Follow-up review finding for PR #66:

  1. [P2] Temp files are not cleaned up if writeFile fails.
    The cleanup only wraps fs.rename. If fs.writeFile(tmpPath, next, "utf-8") creates/truncates the temp file and then fails (ENOSPC/EIO/partial write), the temp file remains in users/. Please wrap both writeFile and rename in one cleanup path, skipping rm only after rename succeeds.

Everything else looks reasonable. Temp+rename fixes torn reads; the remaining lost-update race is accurately called out as a follow-up for shared locking.

… review 4510329796)

Wrap both fs.writeFile and fs.rename in one try so the temp file is removed if
writeFile creates/truncates it and then fails (ENOSPC/EIO); only a successful
rename leaves no temp behind. vitest 13/13.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

Fixed in the latest push: both fs.writeFile and fs.rename are now wrapped in one try, so the temp file is cleaned up if writeFile creates/truncates it and then fails (ENOSPC/EIO) — only a successful rename leaves no temp behind. vitest 13/13.

@cryptolir
cryptolir merged commit 67bed18 into main Jun 16, 2026
16 of 20 checks passed
@cryptolir
cryptolir deleted the feat/save-user-section-atomic-write branch June 16, 2026 22:11
cryptolir added a commit that referenced this pull request Jun 18, 2026
…oad_skill #74) (#75)

Record the load_skill roll: #74 merged, gateway v2026.06.18.1 (sourceSha
09a99e4) pinned to life only — ships the full per-user stack to prod (writer
#65/#66 + injection #68 + first-turn #71 + load_skill #74). Bump the
INFRASTRUCTURE life-ahead tag to v2026.06.18.1 and note the US-host disk cleanup
(97% to 67%, removed 8 unused/unreferenced gateway tags).

Co-authored-by: Liran Peretz <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
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