Skip to content

Use exclusive staging and safe lock opens for account files - #1849

Merged
mergify[bot] merged 1 commit into
mainfrom
lean178/1543-safe-account-files
Nov 26, 2026
Merged

mergify[bot] merged 1 commit into
mainfrom
lean178/1543-safe-account-files

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Use exclusive staging and safe lock opens for account files

Context and goal

  • Context: The optional account helper locks local account state and stages updates before publication.
  • Problem: A redirected lock or staging collision can reach an unrelated file.
  • Goal: Preserve existing locks, reject symlink locks, and create private staging files exclusively.

Closes #1543.

Acceptance

  • Existing regular lock contents remain intact and symlink lock paths fail.
  • Updates use exclusively created temporary files beside their destination. Existing colliding files and symlink targets stay intact.
  • Cover both paths with synthetic files in the existing account tests.

Validation

Recorded for 4cd3faa32247

  • uv run --group dev ruff check src tests: exit 0
  • uv run --group dev tach check: exit 0
  • uv run --python 3.14 python scripts/check_architecture_docs.py: exit 0
  • scripts/size_budget.sh: exit 0
  • uv run --group dev pytest -q: exit 0

Review

  • primary, claude, 4cd3faa, approve

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Architecture-Review: unchanged; the account helper retains its state format and lock order.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics 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.

loopzero primary review by claude on 4cd3faa32247cbcec55eb23a980f4cd245ffe28d, 24.6s: approve (0 critical, 0 important, 2 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread tests/test_ai_accounts.py
@pytest.mark.parametrize("symlink", [False, True])
def test_account_staging_preserves_colliding_path(helper, tmp_path, symlink):
path = helper.STATE / "status.json"
staged = path.with_name(f"{path.name}.{helper.os.getpid()}.{helper.threading.get_ident()}.tmp")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Staging test only checks the old temp-file name, which the new code never uses

The test puts a regular file or symlink at the old status.json.<pid>.<tid>.tmp name. write_private now stages through tempfile.mkstemp(prefix='.status.json.'), so it never touches that path. The test would pass even if the new staging were not exclusive. It works as a regression guard against the old naming, but it does not show that a collision with the actual staging name leaves the existing file intact. Smallest fix: monkeypatch helper.tempfile._get_candidate_names (or mkstemp's name sequence) so the first candidate matches a pre-created file or symlink. Then assert that the update still lands and the collided target still reads "preserve". Alternatively, assert that no .status.json.* residue remains after the call.

with (STATE / name).open("a") as lock:
fd = os.open(STATE / name, os.O_RDWR | os.O_CREAT | os.O_NOFOLLOW | os.O_NONBLOCK, 0o600)
with os.fdopen(fd, "rb") as lock:
if not stat.S_ISREG(os.fstat(lock.fileno()).st_mode):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Non-regular lock rejection path is untested

The new S_ISREG check, together with O_NONBLOCK, guards against a FIFO or device at the lock path. No test covers it. Add a short case that creates STATE/.lock with os.mkfifo and expects ValueError from helper._locked('.lock'). That way a later refactor can't quietly drop O_NONBLOCK and let a FIFO hang the open.

@eowca
eowca marked this pull request as ready for review November 26, 2026 11:47
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Merged into main as 4a9d34d at 11:55 UTC

5 min in the queue, 3 min of it CI.

So far

  • ✅ Entered the queue Thu 26 Nov, 11:49 · rule main · triggered by @eowca with the @mergifyio queue command
  • ✅ Passed · draft #1856
  • ✅ Merged 11:55 · 4a9d34d · squash
3 merge conditions met
  • github-review-approved · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = checks
    • check-neutral = checks
    • check-skipped = checks
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = Loop-zero Eligibility
    • check-neutral = Loop-zero Eligibility
    • check-skipped = Loop-zero Eligibility

Times in UTC · dashboard · how the merge queue works

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

No description provided.

@mergify
mergify Bot merged commit 4a9d34d into main Nov 26, 2026
20 checks passed
@mergify mergify Bot removed the queued label Nov 26, 2026
@eowca
eowca deleted the lean178/1543-safe-account-files branch November 26, 2026 11:55
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.

Avoid truncating files reached through account-state locks and staging paths

1 participant