Skip to content

Keep rollback prompt writes inside the prompts directory - #150

Merged
tonibergholm-codento merged 7 commits into
mainfrom
harden-prompt-writeback
Nov 20, 2026
Merged

tonibergholm-codento merged 7 commits into
mainfrom
harden-prompt-writeback

Conversation

@tonibergholm-codento

Copy link
Copy Markdown
Collaborator

Rollback restores prompt files into <repo>/prompts. It only checked whether the final file was a symlink, so a symlinked directory inside prompts/, or prompts/ itself being a symlink, sent writes and the orphan sweep outside that directory. The temp file name was derived from the prompt name, so entries such as a.tmp and a.md overwrote each other's contents. A tree that write-back would refuse also failed only after reverse migrations and memory restore had already run.

Now:

  • Before any migration, memory restore or file write, and on dry runs too, rollback checks the whole target prompt tree. It checks every entry name, rejects an entry that is both a file and the directory of another entry (compared by path component, case-insensitively), and refuses a symlinked prompts/ or ancestor directory.
  • The same checks run again at write time.
  • The orphan sweep never descends through a symlinked directory.
  • Temp files get their own names (.agentic-tmp-<pid>-<n>), are created exclusively, and skip names that are taken.

Out of scope: a concurrent directory swap between check and write would need an openat-style walk and a new daemon dependency (an ADR). It only matters when another uid can write prompts/, which the separate state-directory change makes the daemon refuse. Names that differ only by Unicode normalization on APFS remain the existing TM-008 gap.

Proof: new unit tests cover symlinked roots and subdirectories, sibling temp names, planted temp files and symlinks, the retry limit, and file/directory conflicts. Two integration tests show that a rejected tree leaves the schema, memory rows and branch tip unchanged. They fail without the fix. cargo fmt and cargo test --workspace pass.

Reviewed with sol-loop: 4 rounds with gpt-6.2-sol. Sol verdict: one finding declined (Unicode normalization on APFS, pre-existing TM-008); all others resolved.

Created with Claude Opus 5.5 and Claude Sonnet 5.5 in Claude Code.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dd647762-9a99-4707-a004-ffb4eaa6b7b9
  • 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T07:51:45.417148Z 1b10e37 New commits
🔒 Security Review ✅ Completed 2026-11-20T08:00:22.618368Z 1b10e37 New commits
ℹ️ 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.

@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: b9dd24e2c5

ℹ️ 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".

Rollback write-back only checked the leaf of each prompt path for a symlink, so a symlinked prompts directory or subdirectory could redirect writes and the orphan sweep outside the prompts root. It now refuses to run when prompts or any existing parent directory below the repo is a symlink, checked before and after creating directories, and the sweep refuses a symlinked root.

The temp file used for atomic replacement is now created exclusively with a unique name that tree entries cannot use, so entries such as a.tmp and a.txt no longer collide and a planted link is never followed.
A symlinked prompts directory or an invalid tree entry name used to be rejected only after reverse migrations and memory restore had committed, leaving a torn rollback. Rollback now validates the target prompts tree and checks for symlinked directories first, including on dry runs, and keeps the write-time checks.

The temp file now has a short name independent of the prompt name. It is created exclusively, retried on a name clash up to 16 times, and removed only after the exclusive open succeeded, so a pre-existing file is never deleted. The reserved temp-name restriction on tree entries is dropped so rollback to existing commits keeps working.
A tree with both a and a/b passed the preflight and failed halfway
through the write, after the database had changed. A temp name equal to
the destination would also expose a partial write, so skip it.
Raw string comparison let p//a and a case-folded name slip past p/a/b.
Temp files used for atomic prompt replacement lived next to the destination, so a committed tree that named every candidate temp file could make the write fail after the database had already changed. They are now created exclusively in the state directory's tmp folder, which no prompt entry can name, and renamed into place. A rename across filesystems fails with an error saying the state directory and prompts must share a filesystem.

The write-back comment now describes the residual race for a same-uid worker sharing the volume and why it is accepted.
A symlinked or loosened .agentic/tmp is refused or tightened, and a
prompts path on another filesystem than the temp directory now fails in
preflight instead of at rename, after the database changed.
The pairwise scan was quadratic in the number of tree entries, which a
socket client controls. Sorted component paths place every extension of
a key right after it, so comparing neighbours is enough.
@tonibergholm-codento
tonibergholm-codento merged commit 0dc670e into main Nov 20, 2026
14 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