factory design: cancel a live run — new AbortSignal kill-path design + TUI wiring (#607 follow-up) #612

Closed
opened 2026-08-20 03:15:28 +00:00 by matou-build-agent · 2 comments

Context

#607 ("the factory TUI — fleet/live-run/incident/ask screens + engage side")
named cancelling a live run as "the one genuinely new mechanism" in its
engage-side scope, and its own Suggested-next-slice section called it out to
split into its own ticket "given the new-mechanism design work it needs
before any TUI wiring." #607 closed on its delivered read-side slice (a Run
tab); the rest of #607's engage scope (answer-asks, arm/park/prioritise,
re-arm) moved to a sibling follow-up ticket. This ticket is that carve-out.

Why this is different from the rest of engage

Every other engage action is composition of an existing protocol the factory
already runs unattended: label writes via the tracker's /labels
sub-endpoint, rearm_dispatch, the ask-human.sh thread protocol. Cancel-a-
run has no existing protocol to compose:

  • RunOptions.signal (AbortSignal) exists in the Sandcastle runner's
    options shape today but is unused — nothing ever passes or triggers it.
  • A bash-level SIGTERM on the host process cannot interrupt an
    in-flight agent turn inside the sandboxed container the way it can a plain
    shell script — run-swarm.sh's own EXIT/SIGTERM/SIGINT traps write a
    verdict on the orchestrator's death, not proof the in-flight Claude Code
    turn inside the container stopped cleanly.
  • There is no rollback/established pattern for this in the codebase yet
    (contrast: droplet vhosts/units and org-secret rotation, which ADR 0174
    explicitly names as having one).

Scope — design pass first, then TUI wiring

  1. Design where the signal actually connects — main.mts's Sandcastle
    invocation, the Docker container lifecycle, or both. Read main.mts,
    run-swarm.sh's trap handling, and close-report-lib.sh's gates before
    proposing a mechanism.
  2. Decide what a partial/aborted run leaves behind — swarm.db's
    runs/attempts/processes rows (an aborted run must not read as a
    silent success, and must not leave a wedge the janitor sweep can't
    recognise), and the runlog.
  3. Decide whether a run mid-close-report can be safely killed — close-
    report's envelope-then-post sequence may have a window where killing
    mid-write is worse than letting it finish; the design should say so
    explicitly rather than leave it implicit.
  4. Only then: wire a Cancel action into the TUI's Run tab
    (.sandcastle/tui/app.py), gated the same two-way-door way as the rest of
    engage (ADR 0174 ruling posted to whichever ticket lands it).

This is real new-mechanism design work, not a slice-sized TUI addition —
expect it to need its own design note (an ADR, or at minimum a ## Design
section on this ticket) before code, the same bar #574's data-layer work and
#573's original scope ruling both cite for "materially bigger and riskier."

Ruled by agent under ADR 0174 (scope split, revertible/provable, no
one-way-door — filing itself is reversible; the actual kill-path mechanism
this ticket describes is NOT pre-authorized by this ruling and needs its own
design-time ruling when picked up): filed ready-for-session to match
#573/#607's own label — this is factory/dev tooling requiring the session's
host standing, not Ben.

## Context #607 ("the factory TUI — fleet/live-run/incident/ask screens + engage side") named cancelling a live run as "the one genuinely new mechanism" in its engage-side scope, and its own Suggested-next-slice section called it out to split into its own ticket "given the new-mechanism design work it needs before any TUI wiring." #607 closed on its delivered read-side slice (a Run tab); the rest of #607's engage scope (answer-asks, arm/park/prioritise, re-arm) moved to a sibling follow-up ticket. This ticket is that carve-out. ## Why this is different from the rest of engage Every other engage action is composition of an existing protocol the factory already runs unattended: label writes via the tracker's `/labels` sub-endpoint, `rearm_dispatch`, the `ask-human.sh` thread protocol. Cancel-a- run has no existing protocol to compose: - `RunOptions.signal` (`AbortSignal`) exists in the Sandcastle runner's options shape today but is **unused** — nothing ever passes or triggers it. - A bash-level `SIGTERM` on the host process **cannot** interrupt an in-flight agent turn inside the sandboxed container the way it can a plain shell script — `run-swarm.sh`'s own EXIT/SIGTERM/SIGINT traps write a verdict on the *orchestrator's* death, not proof the in-flight Claude Code turn inside the container stopped cleanly. - There is no rollback/established pattern for this in the codebase yet (contrast: droplet vhosts/units and org-secret rotation, which ADR 0174 explicitly names as having one). ## Scope — design pass first, then TUI wiring 1. **Design where the signal actually connects** — `main.mts`'s Sandcastle invocation, the Docker container lifecycle, or both. Read `main.mts`, `run-swarm.sh`'s trap handling, and `close-report-lib.sh`'s gates before proposing a mechanism. 2. **Decide what a partial/aborted run leaves behind** — `swarm.db`'s `runs`/`attempts`/`processes` rows (an aborted run must not read as a silent success, and must not leave a wedge the janitor sweep can't recognise), and the runlog. 3. **Decide whether a run mid-close-report can be safely killed** — close- report's envelope-then-post sequence may have a window where killing mid-write is worse than letting it finish; the design should say so explicitly rather than leave it implicit. 4. **Only then**: wire a Cancel action into the TUI's Run tab (`.sandcastle/tui/app.py`), gated the same two-way-door way as the rest of engage (ADR 0174 ruling posted to whichever ticket lands it). This is real new-mechanism design work, not a slice-sized TUI addition — expect it to need its own design note (an ADR, or at minimum a `## Design` section on this ticket) before code, the same bar #574's data-layer work and #573's original scope ruling both cite for "materially bigger and riskier." Ruled by agent under ADR 0174 (scope split, revertible/provable, no one-way-door — filing itself is reversible; the actual kill-path mechanism this ticket describes is NOT pre-authorized by this ruling and needs its own design-time ruling when picked up): filed `ready-for-session` to match #573/#607's own label — this is factory/dev tooling requiring the session's host standing, not Ben.
Owner

This was generated by AI during triage.

Triage

Category: enhancement. State: ready-for-session.

The issue body already carries a ruling ("Ruled by agent under ADR 0174 ...
filed ready-for-session") from filing time, but the label was never
actually applied via the /labels sub-endpoint (only the body text stated
it) — closing that gap now.

Confirming the ruling holds: checked .sandcastle/tui/app.py for any
existing Cancel/AbortSignal wiring — none found, matching the ticket's claim
that RunOptions.signal is unused. No .out-of-scope/ entry matches this
request; not already implemented. ready-for-session (not ready-for-agent)
is correct per docs/agents/triage-labels.md: this is factory/dev tooling
needing the session's host standing, matching the #573/#607 precedent —
not a swarm-worker-pickable task, and not one of Ben's one-way doors
(no personal credentials, security-posture widening, member-facing trust
accept, data destruction, or non-routine spend involved in the design pass
itself).

Note per the ticket's own scope: the design of the kill-path mechanism
itself is explicitly not pre-authorized by this triage ruling — the
ticket says so, and that stands. This ruling only concerns the ticket's
routing (category + state), which is a two-way door (a relabel).

> *This was generated by AI during triage.* ## Triage Category: `enhancement`. State: `ready-for-session`. The issue body already carries a ruling ("Ruled by agent under ADR 0174 ... filed `ready-for-session`") from filing time, but the label was never actually applied via the `/labels` sub-endpoint (only the body text stated it) — closing that gap now. **Confirming the ruling holds:** checked `.sandcastle/tui/app.py` for any existing Cancel/AbortSignal wiring — none found, matching the ticket's claim that `RunOptions.signal` is unused. No `.out-of-scope/` entry matches this request; not already implemented. `ready-for-session` (not `ready-for-agent`) is correct per `docs/agents/triage-labels.md`: this is factory/dev tooling needing the session's host standing, matching the `#573`/`#607` precedent — not a swarm-worker-pickable task, and not one of Ben's one-way doors (no personal credentials, security-posture widening, member-facing trust accept, data destruction, or non-routine spend involved in the design pass itself). Note per the ticket's own scope: the *design* of the kill-path mechanism itself is explicitly **not** pre-authorized by this triage ruling — the ticket says so, and that stands. This ruling only concerns the ticket's routing (category + state), which is a two-way door (a relabel).
Author
Member

Design

Full design write-up: docs/adr/0186-a-cancelled-live-run-is-a-log-marker-run-swarm-detects-not-a-hard-kill-guarantee.md.

Summary of the four questions this ticket asked:

  1. Where the signal connects — main.mts holds an AbortController, wired into Sandcastle's own (previously unused) RunOptions.signal. It polls a host-local marker file ($HOME/swarm/state/cancel-request/<run_id>, the same home swarm.db uses) that a separate process — the TUI — writes. A marker file, not new IPC: it follows limit-lib.sh's existing marker-file convention (CLAUDE_LIMIT_MARKER/CLAUDE_ACTIVE_MARKER).
  2. What an aborted run leaves behind — nothing new needed writing: the existing kills-finalise EXIT-trap invariant (swarmdb_run_end, called on every exit path) already closes the runs row (verdict=cancelled:operator, distinct from completed) and every open attempts row correctly. The Forgejo-side claim (not a swarm.db row) is what the janitor sweep actually watches, and cancellation makes run-swarm.sh exit promptly like any other path, so the existing sweep re-arms the ticket with no new code. Named, accepted gap: an aborted run loses the swarm.db mirror of any iteration that fully succeeded before the cancel landed (record-run-result.sh only runs on run()'s resolved path) — architecturally fine per swarm-db.py's own "this is a MIRROR, losing it loses nothing authoritative" posture, since the real Forgejo close is unaffected.
  3. Mid-close-report kill safety — not guaranteed, but provably not unsafe: close-report.sh's own comment-then-close ordering (post the envelope in every outcome, close only after) means there is no window where a kill can produce a falsely-closed ticket. No code change needed to close-report-lib.sh/close-report.sh.
  4. TUI wiring — the Run tab gets a two-keypress (c to arm, c again within 5s to confirm) Cancel action, deliberately not a modal (cheaper to build/test, and the marker stays deletable up to main.mts's next ~2s poll — a real undo window).

Ruling (ADR 0174)

This is a two-way door: revertible (a commit — marker file, main.mts/run-swarm.sh wiring, TUI action — no schema migration, no tracker-state mutation by the mechanism itself) and provable (26 new offline tests across both repos, all green; check-harness-drift.sh green against the real remote). Not on the one-way-door list: no personal credential, no security-posture widening, no public trust-surface decision, and no data destroyed — nothing this mechanism can kill is the durable record of anything (the Forgejo close, if any, already landed before a kill could matter; swarm.db is an explicitly-documented mirror).

Ruled by agent under ADR 0174 — veto anytime.

What landed

  • Matou/dev-factory@9324ef1a650656ac80a0143078d440afaec4fe0c — cancel-lib.sh (new) + tests/cancel-lib-test.sh (12 groups), main.mts (AbortController + marker poll), run-swarm.sh (swarm_cancel_hit retry-loop branch). Full dev-factory suite green (41/41).
  • Matou/ourcloud@a4772a58088e4a5b586cc1315f5a4e1e775ad6d0 — design (docs/adr/0186-*.md), FACTORY_REF bump 0e536e4→9324ef1, re-vendor.
  • Matou/ourcloud@c0da88a3e69bd89466a156332a1d38cd2f62e983 — TUI wiring: .sandcastle/tui/app.py's Run tab Cancel action, data/cancel.py (new), data/swarmdb.py's running_run. 14 new offline tests, full TUI suite green (65/65).

Closing — design done, mechanism landed and vendored, TUI wired, all tests green.

## Design Full design write-up: [`docs/adr/0186-a-cancelled-live-run-is-a-log-marker-run-swarm-detects-not-a-hard-kill-guarantee.md`](https://git.matou.nz/Matou/ourcloud/src/commit/c0da88a3e69bd89466a156332a1d38cd2f62e983/docs/adr/0186-a-cancelled-live-run-is-a-log-marker-run-swarm-detects-not-a-hard-kill-guarantee.md). Summary of the four questions this ticket asked: 1. **Where the signal connects** — `main.mts` holds an `AbortController`, wired into Sandcastle's own (previously unused) `RunOptions.signal`. It polls a host-local marker file (`$HOME/swarm/state/cancel-request/<run_id>`, the same home `swarm.db` uses) that a separate process — the TUI — writes. A marker file, not new IPC: it follows `limit-lib.sh`'s existing marker-file convention (`CLAUDE_LIMIT_MARKER`/`CLAUDE_ACTIVE_MARKER`). 2. **What an aborted run leaves behind** — nothing new needed writing: the existing kills-finalise EXIT-trap invariant (`swarmdb_run_end`, called on every exit path) already closes the `runs` row (`verdict=cancelled:operator`, distinct from `completed`) and every open `attempts` row correctly. The Forgejo-side claim (not a swarm.db row) is what the janitor sweep actually watches, and cancellation makes `run-swarm.sh` exit promptly like any other path, so the existing sweep re-arms the ticket with no new code. Named, accepted gap: an aborted run loses the swarm.db mirror of any iteration that fully succeeded before the cancel landed (`record-run-result.sh` only runs on `run()`'s resolved path) — architecturally fine per `swarm-db.py`'s own "this is a MIRROR, losing it loses nothing authoritative" posture, since the real Forgejo close is unaffected. 3. **Mid-close-report kill safety** — not guaranteed, but provably not unsafe: `close-report.sh`'s own comment-then-close ordering (post the envelope in every outcome, close only after) means there is no window where a kill can produce a falsely-closed ticket. No code change needed to `close-report-lib.sh`/`close-report.sh`. 4. **TUI wiring** — the Run tab gets a two-keypress (`c` to arm, `c` again within 5s to confirm) Cancel action, deliberately not a modal (cheaper to build/test, and the marker stays deletable up to `main.mts`'s next ~2s poll — a real undo window). ## Ruling (ADR 0174) This is a two-way door: revertible (a commit — marker file, `main.mts`/`run-swarm.sh` wiring, TUI action — no schema migration, no tracker-state mutation by the mechanism itself) and provable (26 new offline tests across both repos, all green; `check-harness-drift.sh` green against the real remote). Not on the one-way-door list: no personal credential, no security-posture widening, no public trust-surface decision, and no data destroyed — nothing this mechanism can kill is the durable record of anything (the Forgejo close, if any, already landed before a kill could matter; `swarm.db` is an explicitly-documented mirror). **Ruled by agent under ADR 0174 — veto anytime.** ## What landed - `Matou/dev-factory@9324ef1a650656ac80a0143078d440afaec4fe0c` — `cancel-lib.sh` (new) + `tests/cancel-lib-test.sh` (12 groups), `main.mts` (AbortController + marker poll), `run-swarm.sh` (`swarm_cancel_hit` retry-loop branch). Full dev-factory suite green (41/41). - `Matou/ourcloud@a4772a58088e4a5b586cc1315f5a4e1e775ad6d0` — design (`docs/adr/0186-*.md`), `FACTORY_REF` bump `0e536e4`→`9324ef1`, re-vendor. - `Matou/ourcloud@c0da88a3e69bd89466a156332a1d38cd2f62e983` — TUI wiring: `.sandcastle/tui/app.py`'s Run tab Cancel action, `data/cancel.py` (new), `data/swarmdb.py`'s `running_run`. 14 new offline tests, full TUI suite green (65/65). Closing — design done, mechanism landed and vendored, TUI wired, all tests green.
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/idss#612
No description provided.