/agent-traffic-control:worktree-historical-test-replay-missing-dirsfrom agent-traffic-control
Fix `pytest exit 4` ("file or directory not found") when running a test command in a git worktree checked out at an OLD commit. Use when: (1) you're doing historical replay (incident replay, mutation testing, git bisect with tests) and the suite errors out in 1-2 seconds with no test execution, (2) the test command works fine at HEAD but fails at pre-fix SHAs, (3) `pytest scripts/overnight/tests/ <analytics_pkg>/tests/ --tb=no -q` returns "ERROR: file or directory not found: scripts/overnight/tests/" at an old SHA, (4) you're building tooling that runs the project's canonical test command across multiple historical SHAs. The fix is NOT to add the missing dir back — it didn't exist then. The fix is to filter the test-dir list at each SHA to only those that exist in that commit's tree, then invoke pytest with the filtered list. If none survive, classify the SHA as "unrunnable" rather than failing the whole replay.
Trending skills
7-day visitors to each skill page
Ships in agent-traffic-control. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You're replaying historical bugs by checking out each pre-fix SHA in a temp worktree and running the project's canonical test command. At HEAD the command works (e.g. 159 tests collect). At older SHAs you get:
ERROR: file or directory not found: scripts/overnight/tests/
no tests ran in 0.01s
Exit code 4, duration 1-2 seconds. Pytest aborted before collection because one of the test directories you passed didn't exist yet at that commit.
This silently corrupts a historical-replay audit: you can't tell apart "the suite passed and missed the bug" from "the suite never ran." Both look like pytest failures from the outside.
pytest scripts/overnight/tests/ <analytics_pkg>/tests/ ...USAGE_ERROR) in <5 seconds with no test executionBefore invoking the test command at each SHA, filter the test directory list to only those that exist in the worktree at that SHA:
# Inside the worktree at the target SHA:
TEST_DIRS=()
for d in scripts/overnight/tests <analytics_pkg>/tests <analytics_pkg>/cloudrun/<dashboard_app>/tests; do
[ -d "$d" ] && TEST_DIRS+=("$d")
done
if [ ${#TEST_DIRS[@]} -eq 0 ]; then
# No test dirs existed at this SHA — classify as 'unrunnable', skip.
echo "STATUS=unrunnable_no_test_dirs_at_sha"
exit 0
fi
PYTHONPATH=. pytest "${TEST_DIRS[@]}" --tb=no -q
In Python:
test_dirs_at_head = ["scripts/overnight/tests", "<analytics_pkg>/tests"]
existing = [d for d in test_dirs_at_head if (worktree_path / d).is_dir()]
if not existing:
return {"status": "unrunnable", "reason": "no test dirs existed at this SHA"}
subprocess.run(["pytest", *existing, "--tb=no", "-q"], cwd=worktree_path)
Or query git directly without checking out:
git -C "$REPO" ls-tree -d --name-only "$SHA" -- "$DIR"
# exit 0 if dir exists at that SHA, prints the dir name
# exit 0 with no output if dir doesn't exist
Pytest treats explicit path arguments as "you promised these exist". When one is missing it aborts with exit 4 (USAGE_ERROR) BEFORE collection — so not a single test runs, even from the dirs that DO exist. The exit code is the same as a pytest CLI usage mistake, which makes the failure mode ambiguous: it looks like "the test command is wrong" rather than "the test layout was different at that SHA".
Subdirs that didn't exist yet at older SHAs are common in real projects:
tests/ dirtests/ from root into services/foo/tests/# At HEAD — all dirs exist, command works:
$ pytest scripts/overnight/tests/ <analytics_pkg>/tests/ --collect-only -q
159 tests collected
# At an old SHA — one dir missing:
$ git -C $WORKTREE ls-tree -d --name-only HEAD -- scripts/overnight/tests <analytics_pkg>/tests
<analytics_pkg>/tests
# (scripts/overnight/tests is absent)
# After filtering:
$ pytest <analytics_pkg>/tests/ --collect-only -q
58 tests collected # ran successfully against the dirs that existed
The filtered run produces a real pass/fail signal. The unfiltered run produces an ambiguous "exit 4" that looks like a script bug.
pytest tests/test_specific.py fails the same way if that file was
added later. Less common, but check.pytest with no path argument (relying on
testpaths in pyproject.toml), this trap is partially avoided —
pytest will collect whatever exists. But you lose control over which
dirs run, and pyproject.toml itself may not exist at older SHAs, so
fall back to explicit + filtered paths for replay tooling.git worktree add -f --detach <SHA> is the right pattern for historical
replay — --detach avoids creating a useless branch you have to clean
up, -f overrides the safety check if the worktree path was reused.git worktree remove -f in a trap on EXIT, even if the test run
failed, or you accumulate dead worktrees.From test-effectiveness-auditor v1.0 (a separately published plugin, not one of this repo's skills):
# Initial naive command — fails on the project's older SHAs because
# scripts/overnight/tests/ was added in a later session:
pytest scripts/overnight/tests/ <analytics_pkg>/tests/ --tb=no -q
# → exit 4 in 2s, status="some_failed", but no tests actually ran
# After filtering test dirs to those existing at pre-fix SHA 72580148dd:
pytest <analytics_pkg>/tests/ --tb=no -q
# → 58 passed in 1.32s — real signal: bug not caught by then-existing tests
This was the difference between recording 4 incidents as "all unrunnable (broken script)" and recording 3 of 4 as "ran cleanly, all gap_testable (tests missed the bug)" — the latter is the actual finding.
--detach -f for replay toolingWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub wan-huiyan/agent-traffic-control --plugin agent-traffic-control118 skills in this plugin
/agent-refusal-with-evidence-beats-literal-complianceA dispatched agent that does exactly what you asked can ship a defect you would
have stopped yourself, because it can see something at the worksite that you
could not see when you wrote the brief. Use when: (1) you are writing a brief
that prescribes a specific fix, threshold, wording or value rather than the
outcome you want; (2) an agent reports that it did not do what you asked and
you are deciding whether that is insubordination or judgement; (3) an agent
asserts "this is fine" or "not needed" with no measurement behind it; (4) you
are reviewing overnight output and want to know which decisions were checked
and which were assumed. Two halves, and both are needed: give the agent
explicit permission to satisfy the INTENT of an instruction while refusing its
letter, and REQUIRE the evidence with the refusal — what it built or measured,
what that showed, what it did instead. Evidence is the whole difference between
a refusal you can bank and one you have to re-litigate. Four agents correctly
declined an instruction in a single run; one built the suggested fix, measured
that it turned a silent safe refusal into a permanently red pipeline, and
reverted it unpushed.
/auto-merge-rearms-while-agent-live-kill-then-disarm-verifyDisarming auto-merge on a PR whose agent is still running does not hold it —
the agent re-arms within minutes and the PR merges, because arming is a step in
the agent's brief and disarming is not an instruction to the agent at all. Use
when: (1) you want to STOP a pull request from merging that an agent opened and
armed, for review, for a rethink, or because it is landing into a queue you are
trying to drain; (2) a PR you disarmed merged anyway; (3) you are telling agents
which PRs are on hold and want the hold to be enforced rather than announced;
(4) you are about to run `gh pr merge --disable-auto` and have not decided what
to do about the agent. The order is KILL THE AGENT FIRST, THEN DISARM, THEN
VERIFY the disarm by reading `autoMergeRequest` back — a disarm you did not read
back is not a hold. The stronger form needs no ordering at all: convert the PR
to a draft with `gh pr ready --undo`. A draft cannot merge whatever any agent
does to it, so the hold survives an agent you missed, an agent that restarts,
and an agent you did not know about.
/batch-member-cites-a-path-only-a-non-member-addsYou wrote something correct that the default reader cannot reach: a batch member citing a
path only a NON-member adds, or a rule, ruling or ledger sitting on an unlanded branch.
/batch-merge-subject-is-not-evidence-a-member-landedYou are about to close, delete the branch of, or mark as landed a pull
request because a batch, roll-up or merge-train commit on the target branch
names it in the subject line, and because the member's own commits all
predate that merge by wall clock. Neither of those is evidence. A batch
branch is assembled at some moment BEFORE it merges, so anything the member
pushed after assembly is outside it, and the subject records INTENT at
assembly time rather than CONTENT at merge time. Use before closing a
member, when an ancestry check against a squashed batch says no, or when a
member's branch still shows commits ahead of the target. Compare file
content against the target branch instead, expecting exactly one legitimate
difference: the shared aggregate file every member appends to.
/check-a-stale-branch-is-not-undoing-newer-main-workA branch built on an out-of-date main is about to merge, or has merged, and may undo work
that landed since. Use when a green, conflict-free PR deletes files you never touched,
newer values or a whole redesign vanish after a sibling PR merged, rows of a shared ledger
(tracker, registry, CHANGELOG) disappear or revert, or when `git diff main...HEAD` or a
reviewer says your small PR touches dozens of files you did not change.
/ci-leg-skipping-moves-minutes-it-does-not-remove-themYou are about to propose, price or approve a CI cost saving that works by
making a job or matrix leg SKIP on pull requests — a path filter, a safe
list, a changed-files gate, an exemption list. Check first whether the
pipeline runs a complement or backstop on merge, on push, or nightly:
many do, and then the legs a pull request skipped are exactly the legs some
later run executes, so a branch that goes green once and merges bills the
same total either way and the wall clock does not move at all. Use when
pricing a filter change, when a percentage saving is quoted with no basis,
or when choosing between filtering legs and batching pull requests. The
question that separates money from bookkeeping is whether the change
removes RUNS or merely moves LEGS.