Decompose run-swarm.sh along its 8 visible seams #2

Closed
opened 2026-08-19 22:05:56 +00:00 by ben · 1 comment
Owner

Follow-up from #571 (Matou/ourcloud) — the factory-core extraction

The 2026-08-15 factory-reengineering survey named run-swarm.sh (598 lines at survey time, 22 distinct responsibilities, 12 exit points hand-maintaining SWARM_EXIT_REASON) as the patch magnet, with natural seams already visible: preflight → identity → schedule → provision → execute → verify → land → report — six of eight stages already have a library.

#571 moved run-swarm.sh into this repo as-is (vendored, tested, unchanged behavior) rather than attempting the decomposition in the same pass — the topology move (push→pull) was the architecturally load-bearing change; splitting the orchestrator is a separate, lower-risk refactor that can proceed independently now that it lives in one place instead of being forked across product repos.

What's needed

Extract the two stages that don't yet have a library (of the eight named above), and reduce the file to a thin sequence of calls into per-stage libraries, each independently unit-tested — mirroring the pattern heal.sh/heal-lib.sh and claim-next-task.sh/claim-lib.sh already establish in this repo. Full .sandcastle/tests/*.sh suite (including nix-store-test.sh/debounce-test.sh/run-swarm-env-guard-test.sh, which pin byte-for-byte copies of specific run-swarm.sh blocks in isolation) must stay green or be updated in lockstep — those three tests are themselves evidence of exactly the kind of logic that should move into a library.

## Follow-up from #571 (Matou/ourcloud) — the factory-core extraction The 2026-08-15 factory-reengineering survey named `run-swarm.sh` (598 lines at survey time, 22 distinct responsibilities, 12 exit points hand-maintaining `SWARM_EXIT_REASON`) as the patch magnet, with natural seams already visible: preflight → identity → schedule → provision → execute → verify → land → report — six of eight stages already have a library. #571 moved `run-swarm.sh` into this repo as-is (vendored, tested, unchanged behavior) rather than attempting the decomposition in the same pass — the topology move (push→pull) was the architecturally load-bearing change; splitting the orchestrator is a separate, lower-risk refactor that can proceed independently now that it lives in one place instead of being forked across product repos. ## What's needed Extract the two stages that don't yet have a library (of the eight named above), and reduce the file to a thin sequence of calls into per-stage libraries, each independently unit-tested — mirroring the pattern `heal.sh`/`heal-lib.sh` and `claim-next-task.sh`/`claim-lib.sh` already establish in this repo. Full `.sandcastle/tests/*.sh` suite (including `nix-store-test.sh`/`debounce-test.sh`/`run-swarm-env-guard-test.sh`, which pin byte-for-byte copies of specific run-swarm.sh blocks in isolation) must stay green or be updated in lockstep — those three tests are themselves evidence of exactly the kind of logic that should move into a library.

Close report — #2 decomposed, landed as d456cf2

Ruled by agent under ADR 0174 — veto anytime. Every change here is a two-way door (one revertible commit) proven by existing and new offline tests; no one-way door was touched, so nothing routes to ready-for-human.

What landed

run-swarm.sh: 846 lines → 268 lines (122 non-comment), now a sequence that reads as its own stage list.

The two seams the ticket named as having no library:

new lib owns
schedule-lib.sh drive-reservation yield, janitor re-arm, ready set, debounce coalescing, per-run model
provision-lib.sh .env materialize + allowlist guard, secret files, pnpm-store warm check, mount dirs, install/build-image, persistent nix-store seed

Four more, so the file is genuinely thin and not merely shorter. The libs already sitting at those four seams are primitives (limit/cancel, protected-paths/fence/runlog, sweep) — they own detectors and predicates, not the stage. Leaving the stage logic inline would have left ~250 lines of orchestrator with no independent test:

new lib owns
preflight-lib.sh self-tests / policy / model gates — all fail CLOSED
execute-lib.sh the pnpm run sandcastle loop: cancel→auth→limit classification order, #510 one-shot failover, hourly notice dedupe
verify-lib.sh birth-watcher lifecycle, #435 green wedge, #445 protected-path boundary
report-lib.sh post-run sweep, summary assembly, D5 self-rearm

The land seam's 45-line push/rescue ladder moved into landing-lib.sh (landing_stage, landing_push_main_with_rescue); the identity seam's three-line dance became identity_apply in identity-lib.sh. Both already had libraries — the residue just belonged in them.

The three tests the ticket flagged

those three tests are themselves evidence of exactly the kind of logic that should move into a library

Confirmed, and acted on. debounce-test.sh, nix-store-test.sh and run-swarm-env-guard-test.sh each held a byte-for-byte copy of a run-swarm block — kept only because the surrounding script needed pnpm/docker/a live tracker to reach it. All three now drive the real library functions with their scenarios unchanged, as do run-swarm-cold-store-test.sh, run-swarm-landing-test.sh and run-swarm-reconcile-summary-test.sh. Six copies gone.

Two latent bugs the seam tests surfaced (both fixed here)

  1. LANDING=pr reconcile died on a run with no #NN in any commit subject. The touched-set scrape's grep exits 1, and under the caller's set -o pipefail that non-zero rippled out of the command substitution and killed the whole reconcile stage — after the work was committed. Exactly #21's class. Guarded with || true.
  2. swarm-identity-test.sh's "run-swarm.sh must source swarm-identity.sh" guard had no teeth. It was satisfied by an accidental substring — the regenerate-command string inside the call-site guard, which this refactor moved to identity-lib.sh. It would have gone on passing with the source dropped entirely. Now pinned to the actual source line.

One behaviour the refactor had to deliberately preserve

With docker absent, no birth watcher runs, so the #435 wedge check must be skipped — not read "zero births" as a wedge, which would red every run on such a host. Carried across as VERIFY_WATCHING and covered by a test case.

Proof

  • Full offline suite green, 60 test files, before and after the rebase onto #34: for t in tests/*.sh onboarding/tests/*.sh; do … bash "$t"; done
  • New: schedule-lib-test.sh (13 groups), provision-lib-test.sh (11), execute-lib-test.sh (12), verify-lib-test.sh (9), report-lib-test.sh (8), preflight-lib-test.sh (8), run-swarm-stages-test.sh (8). Extended: identity-lib-test.sh +3, run-swarm-landing-test.sh +1.
  • run-swarm-stages-test.sh is the anti-regrowth ratchet: a real whole-script offline run to the empty-ready-set exit (exercising all 15 sourced libs, the identity contract, the drive-yield gate, trap arming, the verdict, both preflight gates, the janitor, and the EXIT trap's runlog line), plus stage-order and file-size assertions. Verified red when a seam call is deleted.
  • execute-lib-test.sh verified red when the classifier's auth/limit order is swapped.
  • vendor-manifest-coverage-test.sh PASS; .sandcastle/check-harness-drift.sh OK.

Residue (not claimed done)

Root-only, per GOTCHAS 4: the factory's own .sandcastle/ self-copy still runs the pre-#2 run-swarm.sh until a human bumps the pin with bash onboarding/onboard.sh vendor .sandcastle d456cf2. Matou/idss and Matou/matou-app pick it up at their next FACTORY_REF bump. The live acceptance — a real swarm run through the decomposed orchestrator — is owed to that re-vendor.

## Close report — #2 decomposed, landed as `d456cf2` Ruled by agent under ADR 0174 — veto anytime. Every change here is a two-way door (one revertible commit) proven by existing and new offline tests; no one-way door was touched, so nothing routes to `ready-for-human`. ### What landed `run-swarm.sh`: **846 lines → 268 lines (122 non-comment)**, now a sequence that reads as its own stage list. **The two seams the ticket named as having no library:** | new lib | owns | |---|---| | `schedule-lib.sh` | drive-reservation yield, janitor re-arm, ready set, debounce coalescing, per-run model | | `provision-lib.sh` | `.env` materialize + allowlist guard, secret files, pnpm-store warm check, mount dirs, install/build-image, persistent nix-store seed | **Four more, so the file is genuinely *thin* and not merely shorter.** The libs already sitting at those four seams are *primitives* (`limit`/`cancel`, `protected-paths`/`fence`/`runlog`, `sweep`) — they own detectors and predicates, not the stage. Leaving the stage logic inline would have left ~250 lines of orchestrator with no independent test: | new lib | owns | |---|---| | `preflight-lib.sh` | self-tests / policy / model gates — all fail CLOSED | | `execute-lib.sh` | the `pnpm run sandcastle` loop: cancel→auth→limit classification order, #510 one-shot failover, hourly notice dedupe | | `verify-lib.sh` | birth-watcher lifecycle, #435 green wedge, #445 protected-path boundary | | `report-lib.sh` | post-run sweep, summary assembly, D5 self-rearm | The **land** seam's 45-line push/rescue ladder moved into `landing-lib.sh` (`landing_stage`, `landing_push_main_with_rescue`); the **identity** seam's three-line dance became `identity_apply` in `identity-lib.sh`. Both already had libraries — the residue just belonged in them. ### The three tests the ticket flagged > those three tests are themselves evidence of exactly the kind of logic that should move into a library Confirmed, and acted on. `debounce-test.sh`, `nix-store-test.sh` and `run-swarm-env-guard-test.sh` each held a **byte-for-byte copy** of a run-swarm block — kept only because the surrounding script needed pnpm/docker/a live tracker to reach it. All three now drive the real library functions with their scenarios unchanged, as do `run-swarm-cold-store-test.sh`, `run-swarm-landing-test.sh` and `run-swarm-reconcile-summary-test.sh`. Six copies gone. ### Two latent bugs the seam tests surfaced (both fixed here) 1. **`LANDING=pr` reconcile died on a run with no `#NN` in any commit subject.** The touched-set scrape's `grep` exits 1, and under the caller's `set -o pipefail` that non-zero rippled out of the command substitution and killed the whole reconcile stage — *after* the work was committed. Exactly #21's class. Guarded with `|| true`. 2. **`swarm-identity-test.sh`'s "run-swarm.sh must source swarm-identity.sh" guard had no teeth.** It was satisfied by an accidental substring — the regenerate-command string inside the call-site guard, which this refactor moved to `identity-lib.sh`. It would have gone on passing with the source dropped entirely. Now pinned to the actual source line. ### One behaviour the refactor had to deliberately preserve With docker absent, no birth watcher runs, so the #435 wedge check must be **skipped** — not read "zero births" as a wedge, which would red every run on such a host. Carried across as `VERIFY_WATCHING` and covered by a test case. ### Proof - Full offline suite green, **60 test files**, before and after the rebase onto #34: `for t in tests/*.sh onboarding/tests/*.sh; do … bash "$t"; done` - New: `schedule-lib-test.sh` (13 groups), `provision-lib-test.sh` (11), `execute-lib-test.sh` (12), `verify-lib-test.sh` (9), `report-lib-test.sh` (8), `preflight-lib-test.sh` (8), `run-swarm-stages-test.sh` (8). Extended: `identity-lib-test.sh` +3, `run-swarm-landing-test.sh` +1. - `run-swarm-stages-test.sh` is the anti-regrowth ratchet: a **real whole-script offline run** to the empty-ready-set exit (exercising all 15 sourced libs, the identity contract, the drive-yield gate, trap arming, the verdict, both preflight gates, the janitor, and the EXIT trap's runlog line), plus stage-order and file-size assertions. Verified red when a seam call is deleted. - `execute-lib-test.sh` verified red when the classifier's auth/limit order is swapped. - `vendor-manifest-coverage-test.sh` PASS; `.sandcastle/check-harness-drift.sh` OK. ### Residue (not claimed done) Root-only, per GOTCHAS 4: the factory's own `.sandcastle/` self-copy still runs the pre-#2 `run-swarm.sh` until a human bumps the pin with `bash onboarding/onboard.sh vendor .sandcastle d456cf2`. `Matou/idss` and `Matou/matou-app` pick it up at their next `FACTORY_REF` bump. The live acceptance — a real swarm run through the decomposed orchestrator — is owed to that re-vendor.
matou-build-agent 2026-08-23 15:09:04 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/dev-factory#2
No description provided.