Skip to content

fix(core): remove an owned file only when the removed entry is the checked one - #485

Merged
ElbertePlinio merged 6 commits into
mainfrom
fix/472-owned-unlink-quarantine
Oct 30, 2026
Merged

ElbertePlinio merged 6 commits into
mainfrom
fix/472-owned-unlink-quarantine

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

DirHandle.unlinkOwnedFile checked an entry with lstat and then unlinked it by name. A file that replaced the entry between those two syscalls was removed. Session prune, evidence export and screenshots use this helper, and teardown cleaned lease staging files by name with no check at all.

Now the entry first moves to a removal name that only this call knows, with a random UUID in it. It is checked there and unlinked there. A replacement made before the move goes back under its name with a link, which never replaces anything. A replacement made after the move stays where it is. Prune and teardown put back entries that a stopped remover left under a removal name. When the original name was taken again, they treat the entry as the file it was. Teardown's staging cleanup uses the same removal. The errors now name a removal: "File to remove is gone", "File to remove was replaced, so it was kept", or "...the replacement was kept as ".

Closes #472

Rollback: revert the merge commit. An entry that this version left under a removal name then reads as unknown data, so the older prune keeps its directory and nothing is lost.

Requirement Verification Scenario and edge cases Result Evidence
Remove an owned file only when the removed entry is the checked one, or keep a replacement that arrives at any point Unit tests in dir-handle and session-retention Replacement before the move, at the last step, and after the move with the name taken again; prune, teardown and DirHandle Replacement kept in every case; the last-step tests fail on main, where the replacement is removed before-new-tests.log, core-r3.log
Recover entries left under a removal name by a crash Unit tests, CLI QA Remover gone, remover in this process stopped, remover still live, stopped.json moved, name taken again, nested removal, two concurrent restores, sessions root and permits/ Put back or pruned as the file it was; a live remover's entry is kept until it ends r2-before.log, qa/af1793d/j2 to j5
Same check for the staging cleanup in retainSessionLogs Unit tests Staging file replaced at the last step, replaced after its check, left under a removal name, left after its name was taken again Replacement kept; replaced-after-check fails teardown so a retry can finish; abandoned staging removed r3-before.log, core-r3.log
An error message that fits each caller Unit tests Gone, replaced and put back, replaced and kept under the removal name Each message names a removal, not an atomic write core-r3.log
Test a replacement at the last step, before and after Same tests against main's source DirHandle, prune log and marker, teardown staging Fail on main, pass here before-new-tests.log
Prune that stops midway lets the next one finish Unit tests Stop at stopped.json's removal; entry arriving before stopped.json Next prune restores the teardown time and finishes; the marker stays while another entry remains core-r3.log
session prune through the built CLI QA journeys in an isolated PICKFORGE_HOME Plain, abandoned log, abandoned marker, live remover then gone, name taken again, unknown data, young session Pruned or kept as expected, exit 0 qa/af1793d/
File names near the 255-byte limit Unit tests against af1793d and after A 244-byte name in DirHandle; a 255-byte retained log in prune Removed; both failed with ENAMETOOLONG before the fix r4-before.log, core-r4.log
Full core suite, lint, typecheck, PickCheck, secrets vitest over packages/core, the CLI session-prune and screenshot tests; oxlint; tsc --noEmit; PickCheck; gitleaks At 82150a1, then merged with main 1339 of 1339; clean core-r4.log, typecheck-r4.log, pickcheck-r4.json, gitleaks-r4.log

Known limits: a name too long to keep in its removal name is removed the same way, but a crash midway leaves an entry that prune keeps as unknown data. A directory that replaces a log during prune stays under its removal name, and its session directory is kept, as main keeps any stray subdirectory. A log written into a retained directory after prune's last listing, and before stopped.json goes, leaves that directory without stopped.json; teardown confirms the session's processes are gone first, so no writer exists then.

Evidence paths are under ~/.pickforge/investigations/472/ on the author's machine.

Council review, 3 rounds and a recheck:

Delegate Round 1 Round 2 Round 3 Recheck of 82150a1
GPT-6.1 Sol 5/10 5/10 8/10 9/10
Fable 5.1 8/10 9/10 9/10 not run
Kimi K3.1 Pro not run not run not run not run

The Kimi seat did not run: its OpenCode Go weekly usage limit was reached. Declined, with both other seats accepting the answers: a log written after prune's last listing (no writer exists after teardown), a restore that returns false when the name frees up at that moment (the next prune restores it), a directory left under a removal name (kept as main keeps stray subdirectories), and a stray marker removal in the sessions root whose name was taken again (one file, no directory kept).

…ecked one

unlinkOwnedFile now moves the entry to a removal name that only its call uses, checks it there and unlinks it there. A replacement made before the move goes back under its name, and one made after the move stays where it is. Prune and teardown put back entries that a stopped remover left under a removal name. The teardown staging cleanup uses the same check, and the errors name a removal instead of an atomic write.
A removal name that already exists is skipped, so a process that reuses a stopped remover's pid cannot move onto an entry kept there. Directories stay under their removal name, because putting one back could replace an empty directory. A restore that a concurrent restore finished first succeeds. An abandoned removal that cannot go back counts as the retained or coordination file it was, and prune keeps stopped.json while an entry that arrived meanwhile remains.
A removal name now ends in a random UUID, so the move cannot land on an entry that a stopped remover left or that a restore puts back under a removal name. Teardown also removes a staging file that a stopped teardown left under a removal name after its original name was taken again.
A removal name holds the original name only when the result fits in 255 bytes. A longer name is left out, so the file is still removed, and a crash midway leaves an entry that prune keeps as unknown data.
@ElbertePlinio
ElbertePlinio merged commit 9412c22 into main Oct 30, 2026
10 checks passed
@ElbertePlinio
ElbertePlinio deleted the fix/472-owned-unlink-quarantine branch October 30, 2026 16:07
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.

core: owned-file removal still unlinks by name after its identity check

1 participant