Skip to content

fix(core): prune session directories left without stopped.json - #435

Merged
ElbertePlinio merged 4 commits into
mainfrom
fix/414-prune-empty-session-dir
Oct 29, 2026
Merged

ElbertePlinio merged 4 commits into
mainfrom
fix/414-prune-empty-session-dir

Conversation

@ElbertePlinio

Copy link
Copy Markdown
Member

session prune removed a retained session directory in three steps: the logs, then stopped.json, then the directory. A crash between the stopped.json unlink and rmdir left an empty directory that later prunes skipped forever, because they read the teardown time from stopped.json. A first permit or lease caller that stalled past teardown and prune could also rebuild the directory with only coordination files and a new identity marker. After #317 such a directory also keeps the session's input-closed and takeover-identity markers.

Prune now has a second path for a session directory without a valid stopped.json. It removes the directory only when there is no record and no session lock in any sessions root, the directory's own mtime is older than the one-day marker horizon, and every entry is on an allowlist and passes its check:

  • Permit and lease records and their staging copies: regular files, not links, older than a day, read with O_NOFOLLOW, whose owner process is gone (start ticks count when present, so a reused pid counts as gone).
  • Write temps: regular files older than a day whose writer pid is not alive.
  • permits/: a real directory older than a day whose entries all pass the checks above.

Anything else keeps the directory: an unknown name, another subdirectory, a symlink, a young file, or a live owner. Prune checks the record and lock again and that the directory is still the inode it opened, unlinks each file bound to the identity it checked, restores the directory times after its own unlinks, and removes directories with rmdir, which refuses a directory that gained an entry. The #317 rules then remove the identity and input-closed markers in the same pass. A stalled caller that resumes after the removal fails closed and delivers no input.

Known limit: a hard kill of prune between an unlink and the time restore leaves the directory with a fresh mtime, so the next prune finishes it one day later. The state is safe and lets no input through.

Rollback: revert this PR. It adds no stored format; directories it would have removed simply stay, as before.

Closes #414

Requirement Verified by Scenario and edge cases Result Evidence
Prune removes an empty directory with no record, lock or stopped.json after the marker horizon Unit tests, CLI QA Crash between the stopped.json unlink and rmdir after a real teardown with markers; a young directory; a writer that adds an entry before rmdir Kept while young and late input fails; removed with its markers once old, id reusable; ENOTEMPTY handled session-retention.test.ts, 414/qa/
Test the crash point between the stopped.json unlink and rmdir Unit test that fails rmdir once Real teardown state Next prune finishes after the horizon 414/round2-before-main-source.log (fails on main)
Also prune a coordination-only directory and its identity left by a stalled first caller Unit tests building the state through the takeover code Stalled withAgentPermit caller; unknown file next to coordination files, including near-miss names; young permit; live owner of a permit, staged permit, lease or temp writer; a prune stopped midway Removed only when all entries are stale with gone owners; every other case keeps the directory; the stopped prune is finished by the next one session-retention.test.ts, 414/round2-before-first-fix-source.log
Fail closed for a stalled caller that resumes after removal Unit test Caller holds its new directories while prune removes them AgentPermitUnavailableError, nothing delivered or recreated session-retention.test.ts
Each check guards something Mutations No time restore, owner always gone, temp pid ignored, no file age check Each fails the test that guards it 414/mutation-*.log

QA ran through the CLI with a temporary PICKFORGE_HOME: a destroyed session left in the crash state was kept while young and pruned with both markers once backdated, and a directory holding an unknown notes.txt was kept.

Council review, 2 rounds:

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

Sol's two round 1 blockers, removal by name racing a same-user rename, and the identity check then unlink racing a replaced lease, were disputed and resolved in round 2: rmdir removes only an empty directory, leases and permits publish by exclusive link, so no legitimate writer's entry is lost, and a same-user process could delete these files directly. Sol's should, the one-day delay after a hard kill, is the accepted limit above, which Fable also rated a nit. Fable's should, a shorter stall that leaves permits/ in a retained directory with stopped.json, predates this change and is #434. Fable and Kimi had no open items for round 2.

Follow-ups: #434 (a short stall during teardown leaves permits/ in a retained directory), #431 (staging files from a crash in publishCoordinationIdentity).

Checks on 591c6fa (before the merge of main, which brought only the test fixes from #430): oxlint, typecheck, build; full vitest coverage run with the desktop, window, wait, browser and viewer requirements on (191 files, 3438 passed, 4 skipped); PICKFORGE_REQUIRE_XVFB=1 cargo test --workspace --locked (328 passed). PickCheck 0.4.0 against main is clean.

A prune that stopped after unlinking stopped.json and before rmdir left an
empty directory that later prunes skipped forever, together with the
session markers it kept. Prune now removes such a directory once its mtime
is past the marker horizon and no record or session lock exists. rmdir
refuses a directory that gained an entry, so this path never deletes data.

Refs #414
…led caller

A first permit or lease caller that stalls past teardown and prune rebuilds
the session directory without stopped.json, with permits/ and a new
takeover identity. Prune skipped it forever, and the identity with it.

Prune now removes a directory without stopped.json when no record or lock
exists, the directory and every entry are older than the marker horizon,
and every entry is an allowlisted permit or lease file whose owner or
writer is gone. Any other entry keeps the directory. After its own unlinks,
prune restores each directory's times, so a prune that stops midway leaves
a state the next prune finishes. The identity then follows the marker rules.

Closes #414
@ElbertePlinio
ElbertePlinio merged commit ed86f1b into main Oct 29, 2026
10 checks passed
@ElbertePlinio
ElbertePlinio deleted the fix/414-prune-empty-session-dir branch October 29, 2026 14: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.

session prune: a crash before rmdir leaves an empty session directory forever

1 participant