Skip to content

test(tests): scope snapshot interrupt cleanup assertion to snapshot-owned temp files - #39

Closed
sKeLe100 wants to merge 2 commits into
mainfrom
fm/snapshot-interrupt-test-tmpdir
Closed

sKeLe100 wants to merge 2 commits into
mainfrom
fm/snapshot-interrupt-test-tmpdir

Conversation

@sKeLe100

Copy link
Copy Markdown
Owner

Intent

Fix overbroad assertion in test_interrupted_snapshot_leaves_no_temp_files that required TMPDIR to be entirely empty after an interrupted fm-fleet-snapshot.sh run. On hosts lacking timeout/perl, the fm-timeout-lib.sh bash fallback creates transient temp files directly in TMPDIR (fm-bash-timeout-command.) that may survive an interrupt. Narrowed the assertion to check only for snapshot-owned temp files (fm-fleet-snapshot. directory and its contents). Added test_bash_timeout_fallback_cleanliness to exercise the bash fallback path when timeout/perl are unavailable.

What Changed

  • test_interrupted_snapshot_leaves_no_temp_files no longer requires TMPDIR to be entirely empty after an interrupted fm-fleet-snapshot.sh run; it now asserts specifically that no fm-fleet-snapshot.* directory or files under it survive, so unrelated residue from the fm-timeout-lib.sh bash fallback no longer fails the test.
  • Added test_bash_timeout_fallback_cleanliness, which runs a snapshot over a 500-entry synthetic backlog and asserts no fm-bash-timeout-command.* files remain in TMPDIR; it passes as a skip when timeout, gtimeout, or perl is available since the bash fallback path is not exercised there.
  • Registered the new test in the suite's run list and expanded the comments documenting why the interrupt assertion is scoped the way it is.

Risk Assessment

✅ Low: Test-only change that correctly narrows an overbroad assertion without weakening its regression coverage; the added test is inert on most hosts but harmless.

Testing

Ran the targeted suite tests/fm-fleet-snapshot-view.test.sh — all cases pass; on this host (timeout and perl both present) the new bash-fallback case reports its skip branch, as the author designed. Because a green skip proves nothing on its own, I additionally reproduced the scenario manually end-to-end: forcing the fm-timeout-lib.sh bash mechanism and interrupting a bounded call leaves fm-bash-timeout-command.* directly in TMPDIR, which the previous "TMPDIR entirely empty" assertion would have reported as a snapshot cleanup bug, while the narrowed snapshot-owned assertion correctly passes. Interrupting a real fm-fleet-snapshot.sh --json run after its fm-fleet-snapshot.* tmpdir was observed shows the directory is fully cleaned, and a planted-residue negative control confirms the narrowed assertion still fails on genuine snapshot leftovers, so it was not weakened into a no-op. No UI surface is involved, so the evidence is a CLI transcript rather than a screenshot. Worktree left clean; evidence files written only to the evidence directory.

Evidence: Interrupt / bash-fallback reproduction transcript (old vs new assertion + negative control)

Source: Interrupt / bash-fallback reproduction transcript (old vs new assertion + negative control)

== Part A: bash timeout fallback interrupted mid-flight == mechanism: bash TMPDIR after interrupt: $TMPDIR/fm-bash-timeout-command.YpVOyC OLD (TMPDIR entirely empty): FAIL - 'interrupted snapshot left temp files behind:' $TMPDIR/fm-bash-timeout-command.YpVOyC NEW (snapshot-owned only): PASS - no fm-fleet-snapshot.* residue == Part B: real fm-fleet-snapshot.sh interrupted mid-run == snapshot tmpdir observed while running: 1 TMPDIR after interrupt: NEW (snapshot-owned only): PASS - no fm-fleet-snapshot.* residue == Part C: negative control - plant fm-fleet-snapshot.* residue == NEW (snapshot-owned only): FAIL - left directory behind: $TMPDIR/fm-fleet-snapshot.CONTROL

== Part A: bash timeout fallback interrupted mid-flight ==
  mechanism: bash
  TMPDIR after interrupt:
    $TMPDIR/fm-bash-timeout-command.YpVOyC
  OLD (TMPDIR entirely empty): FAIL - 'interrupted snapshot left temp files behind:'
    $TMPDIR/fm-bash-timeout-command.YpVOyC
  NEW (snapshot-owned only): PASS - no fm-fleet-snapshot.* residue

== Part B: real fm-fleet-snapshot.sh interrupted mid-run ==
  snapshot tmpdir observed while running: 1
  TMPDIR after interrupt:
  NEW (snapshot-owned only): PASS - no fm-fleet-snapshot.* residue

== Part C: negative control - plant fm-fleet-snapshot.* residue ==
  NEW (snapshot-owned only): FAIL - left directory behind: $TMPDIR/fm-fleet-snapshot.CONTROL
~/.no-mistakes/worktrees/4936102b4bcf/01M1CD5Y7HDX4YPHF5JFPY95Z3/bin/fm-timeout-lib.sh: line 55: /tmp/tmp.zk8YfvrCwl/tmpA/fm-bash-timeout-command.YpVOyC: No such file or directory
~/.no-mistakes/worktrees/4936102b4bcf/01M1CD5Y7HDX4YPHF5JFPY95Z3/bin/fm-timeout-lib.sh: line 62: /tmp/tmp.zk8YfvrCwl/tmpA/fm-bash-timeout-command.YpVOyC.deadline: No such file or directory
Evidence: Reproduction script used to produce the transcript

Source: Reproduction script used to produce the transcript

#!/usr/bin/env bash
# Manual e2e reproduction for the narrowed interrupt-cleanup assertion.
#
# Part A: interrupt a bounded call running through the fm-timeout-lib.sh bash
#         fallback (the mechanism used on hosts with no timeout/gtimeout/perl)
#         and show the fm-bash-timeout-command.* residue it leaves in TMPDIR -
#         exactly what the old "TMPDIR must be entirely empty" assertion
#         flagged as a snapshot cleanup bug.
# Part B: interrupt a real fm-fleet-snapshot.sh run and show its own
#         fm-fleet-snapshot.* tmpdir IS cleaned up.
# Part C: negative control - plant real snapshot residue, new assertion fires.
set -u
ROOT=$1
old_assert() { # <tmpdir>
  local l; l=$(find "$1" -mindepth 1 2>/dev/null)
  if [ -z "$l" ]; then echo "  OLD (TMPDIR entirely empty): PASS"
  else echo "  OLD (TMPDIR entirely empty): FAIL - 'interrupted snapshot left temp files behind:'"; printf '%s\n' "$l" | sed "s|$1|\$TMPDIR|" | sed 's/^/    /'; fi
}
new_assert() { # <tmpdir>
  local d f; d=$(find "$1" -mindepth 1 -maxdepth 1 -name 'fm-fleet-snapshot.*' -type d 2>/dev/null)
  f=$(find "$1" -mindepth 2 -name 'fm-fleet-snapshot.*' -o -path '*/fm-fleet-snapshot.*/*' 2>/dev/null)
  if [ -n "$d" ]; then echo "  NEW (snapshot-owned only): FAIL - left directory behind: ${d/#$1/\$TMPDIR}"
  elif [ -n "$f" ]; then echo "  NEW (snapshot-owned only): FAIL - left temp files behind: ${f/#$1/\$TMPDIR}"
  else echo "  NEW (snapshot-owned only): PASS - no fm-fleet-snapshot.* residue"; fi
}

work=$(mktemp -d); home=$work/home
mkdir -p "$home/state" "$home/data" "$home/projects" "$home/config"

echo "== Part A: bash timeout fallback interrupted mid-flight =="
tmpA=$work/tmpA; mkdir -p "$tmpA"
FM_TIMEOUT_MECHANISM_OVERRIDE=bash TMPDIR="$tmpA" bash -c '
  . '"$ROOT"'/bin/fm-timeout-lib.sh
  echo "  mechanism: $(fm_timeout_mechanism)"
  fm_run_timed 30 sleep 10' &
pid=$!
for _ in $(seq 1 400); do
  [ -n "$(find "$tmpA" -name 'fm-bash-timeout-command.*' -print -quit 2>/dev/null)" ] && break
  sleep 0.02
done
kill -TERM "$pid" 2>/dev/null; wait "$pid" 2>/dev/null
echo "  TMPDIR after interrupt:"; find "$tmpA" -mindepth 1 | sed "s|$tmpA|\$TMPDIR|" | sed 's/^/    /'
old_assert "$tmpA"; new_assert "$tmpA"

echo
echo "== Part B: real fm-fleet-snapshot.sh interrupted mid-run =="
tmpB=$work/tmpB; mkdir -p "$tmpB"
{ printf '## In flight\n\n## Queued\n\n## Done\n'
  for i in $(seq 1 3000); do printf -- '- [x] slow-task-%04d - Synthetic backlog record %04d keeping the snapshot busy long enough to interrupt https://github.com/kunchenguid/firstmate/pull/%d (repo: alpha) (kind: ship) (merged 2026-08-01)\n' "$i" "$i" "$i"; done
} > "$home/data/backlog.md"
TMPDIR="$tmpB" FM_HOME="$home" "$ROOT/bin/fm-fleet-snapshot.sh" --json >/dev/null 2>&1 &
pid=$!; seen=0
for _ in $(seq 1 2000); do
  if [ -n "$(find "$tmpB" -mindepth 1 -name 'fm-fleet-snapshot.*' -print -quit 2>/dev/null)" ]; then seen=1; break; fi
  kill -0 "$pid" 2>/dev/null || break
done
echo "  snapshot tmpdir observed while running: $seen"
kill -TERM "$pid" 2>/dev/null; wait "$pid" 2>/dev/null
echo "  TMPDIR after interrupt:"; find "$tmpB" -mindepth 1 | sed "s|$tmpB|\$TMPDIR|" | sed 's/^/    /'
new_assert "$tmpB"

echo
echo "== Part C: negative control - plant fm-fleet-snapshot.* residue =="
mkdir -p "$tmpB/fm-fleet-snapshot.CONTROL"; : > "$tmpB/fm-fleet-snapshot.CONTROL/backlog.json"
new_assert "$tmpB"
rm -rf "$work"

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 2 issues (1 warning, 1 info)
  • ⚠️ tests/fm-fleet-snapshot-view.test.sh:597 - test_bash_timeout_fallback_cleanliness skips (and reports pass) whenever timeout, gtimeout, or perl is on PATH, which is true on virtually every CI host and dev machine, so the new coverage never executes. fm-timeout-lib.sh:31 already supports FM_TIMEOUT_MECHANISM_OVERRIDE=bash, which forces fm_run_bash_timeout deterministically; exporting it alongside TMPDIR/FM_HOME on the $SNAPSHOT invocation would exercise the fallback everywhere and remove the skip branch. As written the test also never asserts that the fallback ran, so even on a host without timeout/perl a green result proves nothing.
  • ℹ️ tests/fm-fleet-snapshot-view.test.sh:580 - The snapshot_files find is unreachable: with -mindepth 2, both branches (-name 'fm-fleet-snapshot.' and -path '/fm-fleet-snapshot./') can only match paths beneath a surviving fm-fleet-snapshot.* directory, and the snapshot_dir check on the preceding line already fails in that case. Drop it, or restate it as a maxdepth-1 check for stray snapshot-named files directly in TMPDIR.
✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-fleet-snapshot-view.test.sh (whole file, includes test_interrupted_snapshot_leaves_no_temp_files and the new test_bash_timeout_fallback_cleanliness)
  • Manual repro: interrupted fm_run_timed with FM_TIMEOUT_MECHANISM_OVERRIDE=bash and applied the old vs new assertion to the same TMPDIR
  • Manual repro: SIGTERM'd a real bin/fm-fleet-snapshot.sh --json run mid-flight and inspected TMPDIR
  • Negative control: planted fm-fleet-snapshot.CONTROL/ residue and re-ran the narrowed assertion
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…les only

The test_interrupted_snapshot_leaves_no_temp_files assertion was overbroad:
it required TMPDIR to be entirely empty after an interrupted snapshot. On
hosts lacking timeout or perl (used internally by fm-fleet-snapshot.sh),
the fm-timeout-lib.sh bash fallback creates transient temp files directly
in TMPDIR (pattern fm-bash-timeout-command.*) that may survive an
interrupt — these are cleaned up by the timeout wrapper itself but are not
snapshot cleanup residue.

Narrow the assertion to check only for snapshot-owned temp files
(fm-fleet-snapshot.* directory and its contents), which is the real
invariant. Add a complementary test that exercises the bash fallback path
when timeout/perl are unavailable.
… fallback path

- The snapshot_files find was unreachable at -mindepth 2: both branches
  could only match paths beneath fm-fleet-snapshot.* which the preceding
  snapshot_dir check already fails on. Restated as a maxdepth-1 check for
  stray snapshot-named files in TMPDIR.

- test_bash_timeout_fallback_cleanliness used a conditional skip whenever
  timeout/perl are on PATH, which is true on virtually every CI host.
  Replaced with FM_TIMEOUT_MECHANISM_OVERRIDE=bash to force the bash
  fallback deterministically so the test exercises the path everywhere.
@sKeLe100

Copy link
Copy Markdown
Owner Author

Closed per fork PR classification: superseded or abandoned; branch kept.

@sKeLe100 sKeLe100 closed this Nov 28, 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