Skip to content

fix(cli): open the migration target through a held descriptor, and say what that narrows - #1178

Merged
cameri merged 3 commits into
rewrite/mainfrom
factory/031-held-descriptor
Nov 19, 2026
Merged

cameri merged 3 commits into
rewrite/mainfrom
factory/031-held-descriptor

Conversation

@chappie-daemon

@chappie-daemon chappie-daemon commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

nostream-cli migrate pg-to-lmdb now opens its target through a held descriptor instead of its path.

Two windows, two different outcomes, and the difference is the point:

  • The window cli: open the migration target through a held descriptor, not its path #1162 describes is closed. verify used to stat the path after the open, so a target renamed during the open and restored before the check compared the original inode against itself. The descriptor is now the handle, and a rename lands in the pinned inode: closed, with a swap test per arm.
  • A narrower window inside heed is not closed. heed canonicalises the path and then opens the canonical path, so the descriptor stops being fd-relative for that pair. This change narrows it and says so everywhere; closing it needs a second unsafe/ffi site, which CLAUDE.md:81-84 forbids.

Fixes #1162. (On this repository Closes is inert against rewrite/main: the issue needs closing by hand after the merge.)

The window

TargetPin pins the target directory — it holds a File and fstats it — but base passed the path to LmdbBackend::open and verify re-statted the path afterwards. Rename the target during the open and restore it before the check, and the check compares the original inode against itself: the store lands in the replacement and the command reports success.

Now the descriptor is the handle: TargetPin::root() returns /proc/self/fd/<n> where /proc/self/fd is a directory (runtime probe, no cfg(target_os) in production code), else the path unchanged. open_target, both verifying arms and the manifest read/write/sync all resolve through it. The --verify arm and the completed-manifest arm never pinned at all before this change; they do now.

What it does not close — and the tree says so

heed canonicalises the path (heed-0.20.5/src/env.rs:239) and then opens the canonical path (:305), so the fd spelling stops being fd-relative at the syscall that matters. This narrows the window to that pair; it does not close it, and closing it needs a second unsafe/ffi site, which CLAUDE.md:81-84 forbids. Measured on a name-swapping thread, 300 rounds: 21 pinned, 0 in the wrong directory, 279 refused.

Every claim in the tree, the ADR and the changeset says narrows. Nothing says it closes.

The class this job found in its own spec

The mechanism changed the vocabulary of every error path, and the spec's requirement 8 named the right rule while enumerating the wrong sites. The ux lens caught it by running the binary — a store that would not open printed:

Migration failed: failed to open the lmdb environment at /proc/self/fd/9/lmdb: MDB_INVALID: File is not an LMDB file

and now prints:

Migration failed: failed to open the lmdb environment at /tmp/…/target/lmdb: MDB_INVALID: File is not an LMDB file

operator_message(message, root, target) is applied at all 12 CliError::Migrate sites in the manifest functions and at both openers, and the test a_store_that_will_not_open_names_the_target_not_the_descriptor pins it — it fails on the pre-fix tree with the line above.

Evidence

  • The swap tests, one per verifying arm: pin, rename the target through its parent, put a store-without-manifest at the original name, then run the arm. Pinned reads the moved store (exit 0, Verified 9 tables); bare reads the replacement (No manifest at …, exit 1). Inverting either arm fails only its own test.
  • The symlink check still holds through the descriptor: a planted symlink at /proc/self/fd/N/migration-manifest.json.tmp is refused EEXIST by create_new, the victim untouched, mode 0600, the rename and dir-fsync landing in the pinned directory. That control had only ever been exercised on the path arm.
  • No third window: nothing drops the pin before use, a closed spelling answers ENOENT rather than silently falling back to the name, and the handle is CLOEXEC.
  • cargo test --workspace --all-features, both clippy passes, --no-default-features, and the release build are green; migrate_cli 45 (base 42).

The one limit a future reader needs in plain words

  • The arm tests prove the seam is called, not that it is called in the right place; the ordering is held by the adjacent lines and AC 6's read.
  • Making that ordering testable needs an instrumented seam — recorded as out of scope rather than solved.
  • A later change that moves after_pin() below the open keeps every test green.

Known limits

  • The copy arm's open has no test. run probes the source before create_target, so without a reachable Postgres it never reaches its pin; that one call is guarded by the AC 5/6 greps and the compiler. pin.verify at migrate.rs:139 bounds what an attacker gains there today.
  • AC 5's call-form count became a pin.root() count (5) when root() stopped taking an argument — a weaker check, recorded.
  • --verify now needs read permission on the target where base needed only search (pinning opens the directory). Accepted and recorded in the ADR: a migration target is writable by definition, and the refusal names the operator's path.
  • macOS has no /proc: the runtime probe falls back to today's path behaviour, and verify remains the check there.

Cost

$2.16 against $1.25 expected — the operator accepted ~$1.6 before the last two passes; the excess above that is a second approver read after a record escalation. What the money bought: recon, a two-pass spec red-team, the build plus two rework passes (one of them the operator's test for the --verify arm, one the round-1 operator-message defect), review round 1 with four lenses and a two-lens closing round. The overrun bought the message fix and two lens notes, not scope.

@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6ef635c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 19, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bdeffc31-40b1-4fc2-a259-ae05339d2cc0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge within its stated limits.

Summary

The migration command now uses a held directory handle for store opens and manifest access where procfs is available, narrowing the directory-swap window.

  • Migration store and manifest access follow the held target directory.

Acknowledged limits from chappie-daemon: heed still resolves an absolute path before opening, so the race is narrowed rather than closed; macOS keeps path-based behavior; verification now needs directory read permission. The copy arm lacks a full command test because it requires reachable Postgres. The hook tests check presence, not ordering, and the root-call count is a weaker check; stronger instrumentation is explicitly out of scope.

Reviews (1) · Last reviewed commit: "fix(cli): name the operator's target in ..." · Reviewed by Greptile

@cameri
cameri merged commit d16f28a into rewrite/main Nov 19, 2026
19 checks passed
@cameri
cameri deleted the factory/031-held-descriptor branch November 19, 2026 01:08
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.

2 participants