Skip to content

fix(bin): stop a finished task's Docker containers at teardown - #16

Merged
demy00 merged 2 commits into
mainfrom
feature/fm-teardown-stop-stacks
Oct 22, 2026
Merged

demy00 merged 2 commits into
mainfrom
feature/fm-teardown-stop-stacks

Conversation

@demy00

@demy00 demy00 commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Intent

The captain's laptop nearly died under load (load average around 50). Among the causes were Docker stacks that workers started and that kept running after their tasks were finished and cleaned up: a demo stack started 2026-10-15 from a demo worker's worktree (compose project odshow3-app/-cp/-dbs, config under ~/.treehouse/owndivision-demo-/2/), a no-mistakes end-to-end stack from 2026-10-15 (project nm01m4, config under ~/.owndivision-demo-9 and /tmp/nm01M4), a Keycloak container from a no-mistakes run worktree (odkc-nm-k1, mounts under ~/.no-mistakes/worktrees///), a local-CI Postgres (localci-be-pg), and an example-databases stack from a torn-down worktree (project owndivision-example-dbs, config under ~/.treehouse/owndivision-example-dbs-/1/). Firstmate stopped them by hand.

The captain's words: "I think these were spin-up by workers. And if we don't need them, we are closing the workers, we are closing their stack. Why is that? I think we should probably have a look at this and fix it."

What Changed

  • Added bin/fm-task-containers.sh (list/stop). It finds the running Docker containers a task provably owns by checking Docker's own records: the compose working dir, the compose config-file labels, and bind-mount sources. A container counts only if one of those paths is in the task worktree, the task temp root, or a no-mistakes run worktree on the worktree's branch. For a reused pool slot, containers started before this task took the slot don't count. It only runs docker stop (never rm, never removes volumes). Containers it can't attribute are left running. Missing or unreachable Docker is reported, not treated as a failure.
  • bin/fm-teardown.sh now calls it before returning the worktree, for every non-secondmate task, as a best-effort step that never blocks teardown. For a pool slot it passes the worktree only when this task holds the owner claim, with the claim's time as the start bound; it always passes the task temp root.
  • Ship and scout briefs (bin/fm-brief.sh) gain a rule: start Docker stacks from compose files and bind mounts inside the worktree, and stop every container you started before reporting done. Docs (architecture.md, scripts.md) and tests cover the new script, the teardown wiring and the brief text.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The change only runs docker stop, never rm or volume removal, and it acts only on containers proved by Docker-recorded paths, with a pool-slot time bound. It is best effort in teardown, both round-1 fixes (parent-shell cache and tolerating partial inspect output) are correctly in place, and behavior tests cover the cases.

Testing

I ran four targeted suites against the fake Docker: fm-task-containers, fm-teardown, fm-teardown-endpoint-safety and fm-brief. All passed, including the regression tests for the run-lookup cache and the container that exits between ps and inspect. Then I ran the script against the live Docker engine (29.5.3). list named the three containers the task owns (two from the worktree compose stack, one bind-mounted from the temp root) and changed nothing. stop stopped only those three; the compose stack outside the task and the container with only a named volume kept running. The stopped containers remain as exited (stop only, no rm, no volume removal), and a second stop was a no-op. The full transcript is saved as evidence. After capture I stopped the two control containers by hand. My attempt to remove the test containers and volume was denied, so they remain as stopped, harmless leftovers.

Evidence: Real-Docker transcript: list and stop pick out only the task's containers

Source: Real-Docker transcript: list and stop pick out only the task's containers

$ # Simulated worker stack: compose project 'fmtce2e-task' in the task worktree
$ docker compose -p fmtce2e-task -f $WT/compose.yaml up -d
 Container fmtce2e-task-db-1 Starting 
 Container fmtce2e-task-db-1 Started 
 Container fmtce2e-task-web-1 Started 
$ # Plain docker run with a bind mount from the task temp root
$ docker run -d --name fmtce2e-tmpbind -v $TT:/data busybox:1.36 sleep 3600
started
$ # Unrelated stacks that must NOT be touched: compose outside the task, named-volume-only run
 Container fmtce2e-other-other-1 Started 
started fmtce2e-namedvol

$ docker ps --filter name=fmtce2e --format '{{.Names}} {{.Status}}'
fmtce2e-namedvol Up Less than a second
fmtce2e-other-other-1 Up Less than a second
fmtce2e-task-db-1 Up Less than a second
fmtce2e-task-web-1 Up Less than a second
fmtce2e-tmpbind Up Less than a second

$ bin/fm-task-containers.sh list --task e2e1 --worktree $WT --tasktmp $TT
would stop Docker container fmtce2e-tmpbind (877611fd9ba9) for e2e1: bind mount $R/tasktmp is in the task temp dir
would stop Docker container fmtce2e-task-web-1 (ae05107e1584) for e2e1: compose working dir $R/worktree is in the task worktree
would stop Docker container fmtce2e-task-db-1 (1ec1065b02c5) for e2e1: compose working dir $R/worktree is in the task worktree
exit=0

$ bin/fm-task-containers.sh stop --task e2e1 --worktree $WT --tasktmp $TT
stopped Docker container fmtce2e-tmpbind (877611fd9ba9) for e2e1: bind mount $R/tasktmp is in the task temp dir
stopped Docker container fmtce2e-task-web-1 (ae05107e1584) for e2e1: compose working dir $R/worktree is in the task worktree
stopped Docker container fmtce2e-task-db-1 (1ec1065b02c5) for e2e1: compose working dir $R/worktree is in the task worktree
exit=0

$ docker ps -a --filter name=fmtce2e --format '{{.Names}} {{.State}}'
fmtce2e-db-1 exited
fmtce2e-namedvol running
fmtce2e-other-other-1 running
fmtce2e-task-db-1 exited
fmtce2e-task-web-1 exited
fmtce2e-tmpbind exited
fmtce2e-tmpmount exited
fmtce2e-unrelated exited

$ # second stop is a no-op
exit=0

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-task-containers.sh:137 - The no-mistakes run proof matches any run of this repo whose branch equals the worktree's current branch, with no slot-time bound. If teardown ever runs on a worktree checked out on a shared branch, it would treat every no-mistakes run on that branch as the task's. Example: a scout makes no branch, so its slot could sit on main, and then the captain's own gated runs on main would match. Containers mounted under those run worktrees would then be stopped. Stop is non-destructive and I could not prove that a scout slot is ever on a named default branch rather than a detached HEAD, so this is informational. A cheap guard would be to leave NM_WORKTREES empty when BRANCH is the repo's default branch (origin/HEAD).
✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-task-containers.test.sh (covers the parent-shell no-mistakes cache (one lookup per run), a container that exits between docker ps and docker inspect, the reused-slot time bound, symlinked /tmp paths, Docker missing or unreachable, a container that will not stop)
  • bash tests/fm-teardown.test.sh (includes 'teardown stops the Docker containers started from the task's worktree and temp root, and only those' and 'teardown finishes and reports it when Docker is unreachable')
  • bash tests/fm-teardown-endpoint-safety.test.sh (includes 'worktree containers count only from the task's own slot claim onward')
  • bash tests/fm-brief.test.sh (crewmate briefs tell workers to start stacks from their worktree and stop them before reporting done)
  • Real-Docker run: started compose project fmtce2e-task from a temp worktree, a docker run bind-mounted from a temp task root, plus controls (a compose project outside the task and a container with only a named volume), then ran bin/fm-task-containers.sh list and stop --task e2e1 --worktree $WT --tasktmp $TT, checked states with docker ps -a, and repeated the stop to show it does nothing the second time
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Docker keeps a stack running after the worker that started it exits, and
teardown returned the worktree without looking at containers, so finished
tasks' demo, end-to-end, and identity-provider stacks kept loading the
machine.

bin/fm-task-containers.sh proves ownership from what Docker records for
each container - the compose working dir and config files labels and bind
mount sources - against the task's worktree (bounded by the slot claim time
for a reused pool slot), its temp root, and the no-mistakes run worktrees of
this repo whose run is on the task branch. Teardown stops (never removes)
only those, reports each, and carries on when Docker is missing or down.
Ship and scout briefs now tell workers to start stacks from their worktree
and stop them before reporting done.

Also isolates the endpoint-safety suite's tmux servers from the operator's
tmux.conf, whose automatic-rename broke its window-name lookups.
@demy00
demy00 merged commit e1b3a90 into main Oct 22, 2026
19 checks passed
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