Repository navigation
feat(mocactl): add watch to stream subagent state changes - #929
Merged
Merged
Conversation
`mocactl watch [--tag T] [--json]` is `subagents` as a stream: it reports every matching ledger row once, then re-reads the ledger every 5 s and prints a line only when a row's state or detail changes. It exits 0 once every watched row is `done`, `failed`, `paused` or `unknown` — `unknown` counts as finished so a dead child or a failed read ends the watch visibly instead of polling forever. Ctrl-C returns 130. With `--json` the output is NDJSON, one event per line, so A4 (rossoctl#383) can tail it from a background monitor. The per-row backend routing, honest-state mapping and ledger write that `subagents` did inline are extracted into `sweepStates`, so there stays exactly one place that decides sync vs. async turn reads. Part of rossoctl#381 (scope item 5). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
ingpaolodettori-dev
requested changes
Nov 19, 2026
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Review — feat(mocactl): add watch to stream subagent state changes
The shape is right: watch is subagents as a stream, the shared sweepStates is a faithful extraction (identical to cmdSubagents' old loop, line for line), and the first-sweep / change-only / ledger-re-read semantics match what the docs say. Two must-fix items inline, both on the new loop.
What checks out
isFinalForWatch=isTerminalState(done/failed/paused) orunknown, so the documented exit set is exactly the code's (leaf-turn.ts:58).- The first sweep prints every row (empty
printedmap), later sweeps print only onstate/detailchanges, and aspawn --tag Tafter the start joins because the ledger is re-read each tick — all covered by the new tests. - Async rows route through
readTurnStateand never mint a session token; sync rows keep the ledger-authoritative-past-terminal rule;--tagfiltering, the 5 s interval, theno subagentsearly return,--tag's widening in the flag gate (and its usage text), and the NDJSON fields (label?,atin ms) all match the reference pages. - Tests: 10 new cases, including the two-
queued-reads-one-line case, a spawn mid-watch, the async route, and the already-final batch;check18/18 green.
Inline must-fix
subagent.ts:632— the change-detection key uses a literal NUL byte in the source instead of an escape; the file is now binary togrep/rg.subagent.ts:630— a Ctrl-C that lands during a sweep is turned intounknownrows and exit 0, not 130, contradicting the documented contract (and only the between-polls abort is tested).
Author: cwiklik (MEMBER — maintainer). One commit, signed off.
| const records = ledger.readAll().filter(match); | ||
| const views = await sweepStates(rt, { ledger, transport, turns, records, signal: opts.signal }); | ||
| for (const { record, view } of views) { | ||
| const line = view.state + ' |
What
mocactl watch [--tag T] [--json]— the last piece of #381 (scope item 5), and what A4(#383, the Claude Code plugin's background monitor) needs to stop shelling out to
subagentson a timer.watchissubagentsas a stream:still learns the current state;
get --wait's 2 s) and prints a line onlywhen a row's state or detail changes — two
queuedreads in a row produce onerunningline;done,failed,pausedorunknown.unknowncounts as finished deliberately: otherwise a dead child or a failed read would leave
watchpolling forever. The line is printed, so the operator sees it;no subagentson stderr, exit 0.With
--jsonthe output is NDJSON, one event per line(
{"sessionId","tag","label"?,"kind","state","detail","at"}), usingsubagents --json'sfield names plus
at.No
--timeoutor--interval: every row is already bounded by its child or the server'sreaper, and
timeoutwraps the command.Refactor
The per-row work
cmdSubagentsdid inline — route sync leaf vs. async turn read, map thehonest state, persist a change back to the ledger — is extracted into
sweepStates.watchcalls it in a loop, so there stays exactly one place that decides which backend arow is read through.
Verification
packages/mocactlsubagent + CLI + reference suites: 156/156 pass, including 9 newsubagent-watchtests (baseline-only exit, dedup,unknownterminating, NDJSON shape,--tag, late spawn, async row read through the turn routes without minting a sessiontoken, Ctrl-C).
make typecheck,make check-links,make fmtclean.make lintpasses excepthadolint-docker, which cannot reach a local Docker daemon.piped reader are exercised only through the fake clock.
Refs #381, #378
Assisted-By: Claude Code