Skip to content

fix: safely recover teardown scratch content - #4935

Open
jasonlgong wants to merge 8 commits into
kunchenguid:mainfrom
jasonlgong:fm/fm-ship-4784
Open

jasonlgong wants to merge 8 commits into
kunchenguid:mainfrom
jasonlgong:fm/fm-ship-4784

Conversation

@jasonlgong

@jasonlgong jasonlgong commented Sep 19, 2026 •

Copy link
Copy Markdown

Summary

  • Leftover scratch/ or .scratch/ content can prevent a returned Treehouse pool slot from being leased again, eventually blocking new task spawns.
  • Teardown moves ordinary untracked and ignored scratch into a durable data/<id>/scratch-recovery-* directory before returning the slot.
  • Empty scratch directories are removed.
  • When scratch contains clean tracked files alongside untracked output, only the untracked and ignored files move; tracked files stay in place.
  • An unborn HEAD with only untracked scratch uses the same recovery path.
  • Teardown refuses when scratch contains nested, bare, or linked repository data, or tracked or staged changes, including with --force.
  • Retaining uncertain content avoids deleting work based only on the name of a scratch directory.

Validation

  • The published head 75613db1d091d704aad350619a51dab4bab4beef has a no-mistakes attestation recording completed review, test, document, and lint steps.
  • Scratch scenario output records relocation, real pool re-lease, repository refusal, tracked-change refusal, and recovery retention across return retry.
  • Endpoint safety output records endpoint and slot ownership checks.
  • The broader teardown suite reported an unrelated Herdr missing-adapter preflight failure; the scratch scenarios passed.
  • Re-validated on current main (39f4c2a): rebases cleanly; scratch relocation and repository refusal re-proved.

Limitations

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed (3) ✅
  • 🚨 bin/fm-teardown.sh:1767 - tracked_scratch_changes() treats every nonzero result from git diff --quiet HEAD -- &lt;scratch&gt; other than 1 as an unborn HEAD, then accepts the path when the cached diff is clean. A worktree with a broken/unresolvable existing HEAD and modified tracked scratch can therefore move only untracked files and proceed to reset/delete the modified tracked content. Only an actually unborn HEAD should use the fallback; other Git inspection errors must refuse teardown.

🔧 Fix applied.
1 error still open:

  • 🚨 bin/fm-teardown.sh:1779 - show-ref --verify only confirms that the symbolic ref exists; it does not prove that the ref's object is resolvable. If HEAD points to an existing branch ref containing an invalid object ID, git diff HEAD returns an error, ref_exists is still 0, and the code falls back to checking only the index. With clean staged content and modified tracked scratch, teardown therefore proceeds and can delete the tracked modification instead of refusing. Validate the target object (for example with git rev-parse --verify HEAD) before treating this as an unborn HEAD.

🔧 Fix applied.
1 error still open:

  • 🚨 bin/fm-teardown.sh:1727 - The early return skips Git inspection whenever both scratch paths are absent. Deleting the only tracked scratch/file removes scratch/, so teardown proceeds and later reset/return discards the tracked deletion instead of refusing. Inspect Git changes for scratch and .scratch even when the directories are absent.

🔧 Fix applied.
✅ Re-checked - no issues remain.

⚠️ **Test** - 1 warning
  • ⚠️ tests/fm-teardown.test.sh:4000 - The broader teardown script also reported an unrelated Herdr preflight failure for the missing-adapter case; all change-relevant scratch scenarios passed.
  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Mixed tracked placeholder and untracked scratch is torn down; untracked output moves to durable recovery while tracked content remains ✅ pass live teardown-scratch-pass-lines.log
Unborn HEAD with only untracked scratch takes the move path and preserves both scratch trees in recovery ✅ pass live teardown-scratch-pass-lines.log
Broken or unresolvable HEAD with modified tracked scratch refuses teardown and preserves the task record and content ✅ pass live teardown-scratch-pass-lines.log
Deleted tracked scratch file refuses teardown instead of silently restoring or discarding the deletion ✅ pass live teardown-scratch-pass-lines.log
Nested, bare, and linked repositories refuse teardown, including the force path ✅ pass live teardown-scratch-pass-lines.log
Non-git and malformed endpoint fixtures preserve their no-git/no-mutation safety contract ✅ pass live teardown-endpoint-safety.log
  • bash tests/fm-teardown.test.sh
  • bash tests/fm-teardown-endpoint-safety.test.sh
  • Focused scratch scenarios captured in teardown-scratch-pass-lines.log
  • Endpoint safety output captured in teardown-endpoint-safety.log
  • git status --short
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Replace destructive teardown scratch cleanup with durable retention plus an
explicit refusal for repository-bearing or tracked scratch. Ordinary and
ignored untracked scratch is moved into data/<id>/scratch-recovery-*/ (empty
dirs rmdir'd) so the slot re-leases; any nested/bare/linked git repo or
tracked/staged scratch change causes teardown to refuse with the exact path
and preserve the work and task record in place, including under --force.

This is a documented safe partial: it does not auto-recover slots that hold a
real repository or genuine tracked changes.

Refs kunchenguid#4784.
…sing

Two fail-closed over-triggers in the scratch retention path refused slots that
held no committed work at risk, stranding them for manual cleanup:

- Mixed tracked + untracked scratch (e.g. a committed scratch/.gitkeep plus
  untracked worker output) refused the whole slot. Now only the untracked and
  ignored subset is relocated to recovery; tracked content is left in place for
  Treehouse to reset, and the slot re-leases.
- An unborn HEAD (a repo with zero commits) made git diff HEAD error, which was
  read as tracked scratch changes and refused. The check now distinguishes an
  unresolvable HEAD from a real diff and still preserves staged content, so an
  unborn HEAD with only untracked scratch takes the ordinary move path.

Repository-bearing scratch (nested/bare/linked) and genuine tracked/staged
changes are still preserved-not-deleted, including under --force; this stays a
documented safe partial that does not auto-recover repo-bearing slots.

Refs kunchenguid#4784.
@jasonlgong jasonlgong changed the title fix: safely recover teardown scratch content feat(bin): preserve scratch data during worktree teardown Sep 23, 2026
@jasonlgong jasonlgong changed the title feat(bin): preserve scratch data during worktree teardown fix: safely recover teardown scratch content Sep 23, 2026
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