Skip to content

feat(mocactl): get --wait and spawn --max-concurrent (#381) - #918

Merged
cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:feat/381-get-wait-and-cap
Nov 19, 2026
Merged

cwiklik merged 2 commits into
rossoctl:mainfrom
cwiklik:feat/381-get-wait-and-cap

Conversation

@cwiklik

@cwiklik cwiklik commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the two items of #381 that were genuinely missing — mocactl get --wait and
mocactl spawn --max-concurrent — and documents the two that turned out to already exist.
#381's scope drifted since it was written in October at 24c6f5e: spawn, subagents, get
(#496) and the workload commands (#520, #552/#675) have all shipped, and #495 closed on
2026-11-16 confirming the CLI converged on mocactl.

mocactl get SESSIONID --wait [--timeout S] blocks until the result is collectable
instead of reading once. This answers @evaline-ju's 2026-10-26 comment on #381: a coding
agent's collect turn needs one blocking call with the result inline, which is the opposite
of watch's batch shape.

  • Polls every 2 s against a 300 s default budget, checked before each sleep so the wait never
    overruns --timeout.
  • Re-mints the session token mid-wait. SH_SESSION_TOKEN_TTL_SECONDS defaults to 300 s —
    exactly the default budget — so a single mint up front would expire mid-wait.
  • Stops early when the ledger says no result is coming (the child is gone past the grace
    window, or the row is already terminal), reporting unknown with the reason rather than
    waiting out the clock.
  • Output on timeout is byte-identical to today's not-finished get (status: "queued",
    exit 1). Only the genuinely-new unknown case adds a status value.

mocactl spawn --max-concurrent N (default 4) refuses a dispatch before a session, a
worker or even a round trip exists. The count is liveness-gated rather than a bare
non-terminal count: a stale running/unknown row — exactly what an unreconciled crash
leaves behind — would otherwise hold a slot for good and wedge spawn permanently.

Already existed, so documented rather than built

  • Item 9, honest running/unknown states. Shipped in transport/leaf-turn.ts
    (mapHonestState) and wired into subagents. The only gap was that get never consulted
    them; --wait fixes that as a side effect.
  • Item 7, follow-up. mocactl run "prompt" --session ID already is one.
    SessionManager.resume is nothing but mintSessionToken(sessionId), so a spawn-created
    session authorizes on the identical path get uses. The issue's premise — that a follow-up
    would clobber the collected result — is false: writeResult has exactly one caller, the
    /v1/runs handler, so /v1/turn writes no leaf result record.

Deliberately not here

  • Item 5, batch watch. design: one CLI for delegating subagent tasks to Moca #495's open question 3 — what it prints so hundreds of tasks stay
    readable in Claude Code's Monitor tool — is still unanswered, and subagents --tag T
    already returns the snapshot a Monitor poll consumes. This is the one place the issue's
    stated scope is narrowed; happy to put it back if you'd rather settle the output question
    here.
  • Item 6, approve/reject. Unreachable input: gates are only produced by the converge
    leaf kind (harness/src/run-leaf.ts:1042) and mocactl spawn accepts only
    --kind prompt|solve, so there is nothing to approve. To be tracked in its own issue, whose
    real prerequisite is spawn --kind converge.

Notes for review:

  • The cap counts one machine's ledger for one user. The server's own pool is the real bound
    (a 503 saturated, already retried in runSpawnWorker), so N machines can still
    oversubscribe it. Marked with a shortcut: comment.
  • The 2 s / 300 s timing and the token re-mint are proven by fake-clock tests, not against a
    live control plane.

Checklist

  • Commits are signed off (git commit -s).
  • make lint and the tests for the touched packages pass. subagent-get and
    subagent-spawn are 47 passed; make typecheck is clean across all 13 packages;
    make lint is clean with hadolint-docker skipped, which needs a Docker daemon not
    running locally. The full mocactl suite has 8 failures in app, overlay-* and
    sbx-* which reproduce unchanged on a clean baseline of this branch's merge-base — and
    the individual test names differ between runs, so they are flaky as well as
    pre-existing. Nothing under subagent-*, cli-* or mocactl-reference fails in either
    run.
  • Docs: the new flags are in docs/harness/reference/cli-mocactl.md, including the
    regenerated mocactl --help block and a new "Following up on a subagent" section.
  • Docs: make check-links passes (236 files, 0 broken links).
  • A new spec or ADR is registered — n/a, no new spec or ADR.

Assisted-By: Claude Code

Two of rossoctl#381's remaining items. The rest of its scope either shipped
already or has no reachable input:

- `get SESSIONID --wait [--timeout S]` blocks until the result is
  collectable, answering @evaline-ju's point that a coding agent's
  collect turn needs one blocking call with the result inline — the
  opposite of a batch shape. Polls every 2 s to a 300 s default budget
  it never overruns, re-minting the session token mid-wait because
  SH_SESSION_TOKEN_TTL_SECONDS defaults to the same 300 s. Stops early
  on a ledger row that can no longer produce a result, reporting
  `unknown` rather than waiting out the clock.
- `spawn --max-concurrent N` (default 4) refuses a dispatch before any
  session, worker or round trip exists. The count is liveness-gated, so
  a stale `running` row from an unreconciled crash cannot hold a slot
  and wedge `spawn`.

Item 9's honest running/unknown states were already shipped in
transport/leaf-turn.ts and wired into `subagents`; `--wait` is what
finally makes `get` consult them. Item 7's `follow-up` is already
`run "prompt" --session ID`, now documented as such. Batch `watch` is
deliberately not built: rossoctl#495's open question 3 (what it prints so
hundreds of tasks stay readable) is still unanswered, and
`subagents --tag T` already returns the snapshot a Monitor poll
consumes. `approve`/`reject` are unreachable until `spawn` accepts
`--kind converge`; follow-up to be filed.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik requested a review from pdettori as a code owner November 19, 2026 14:32
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 19, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — feat(mocactl): get --wait and spawn --max-concurrent (#381)

Both features do what the description says; the details that usually go wrong here are right.

get --wait

  • The timeout cannot overrun: the rt.now() + WAIT_POLL_MS > deadline check is before the sleep, and the timeout path returns 'queued' into the same cmdGet branch that already printed status: "queued" and exited 1, so the "byte-identical" claim holds by construction (and the test pins it).
  • The token handling is correct for the right reason: SessionToken.expiresAt is epoch seconds (handlers.ts:1233 is iat + sessionTokenTtlSeconds), and the refresh test is rt.now() / 1000 >= token.expiresAt - 30 — the unit conversion is right and the 30 s margin covers a poll plus a slow mint. Re-minting per iteration when the mint is stale is cheap.
  • The early stop is consistent with subagents: noResultComing reuses mapHonestState('queued', {alive:false, observedAt}, now), so get --wait and cmdSubagents cannot disagree on when a dead child becomes unknown; the terminal-row case needs no grace at all, which is right because the ledger is the authority there.
  • --timeout without --wait is refused rather than ignored, and --wait/--timeout are refused on other commands — the #419 precedent, consistently applied.
  • A foreign sessionId (no ledger row) simply polls to the deadline: correct, and documented in the comment.

spawn --max-concurrent

  • The gate runs after the local argument checks and before the workload preflight, and requireReady only inspects rt — so "before a session, a worker or a round trip" is literally true.
  • --max-concurrent is validated to 1–9999 (so 0 cannot wedge spawn), defaults to 4, and is refused outside spawn.
  • The count is liveness-gated exactly as the comment says: non-terminal and a live childPid; a row whose child is gone (or that never got a pid) holds nothing, so the "unreconciled crash wedges spawn" failure mode is closed. isAlive is the same kill(pid, 0) helper the ledger already uses for state mapping.

Tests/docs: six --wait cases (poll, single read without the flag, timeout shape, dead child, terminal row, live child) and four cap cases (refuse, allow, stale rows, default), plus the docs section with exit codes and the unknown vs queued distinction plus the follow-up note. CI is 18/18 green.

One suggestion inline about Ctrl-C during --wait, and a body nit: the get tests mint with expiresAt: 999, which is always past, so every poll re-mints and the "reuse a still-valid token" branch is never exercised — a second seam with a far expiresAt would cover it.

Author: cwiklik (MEMBER — maintainer). Agent/IDE config: none. Commits: 1, signed off.

}
// Checked before sleeping so the wait never overruns the budget.
if (rt.now() + WAIT_POLL_MS > deadline) return 'queued';
await rt.sleep(WAIT_POLL_MS, opts.signal);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Ctrl-C during --wait does not end the wait cleanly. sleep resolves early on abort (it is setTimeout + an abort listener, no rejection), so the loop goes round once more: mintSessionToken (which takes no signal) then readLeafState with an already-aborted signal, which the transport wraps as a network error. The user sees an error line and exit 1 instead of the documented 130. workloads.ts has the pattern for this — check signal?.aborted and return 130 — and here it would be cheapest right after the sleep (or at the top of the loop), before the mint.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and right on both halves — core/time.ts:3,8 resolves on abort rather than rejecting, so the loop went round for a mint plus a doomed read. Fixed in 2b1223e.

I took your pointer to workloads.ts and checked in both places it does: after the sleep, and inside the catch. The second one matters for the same reason as the first — an interrupt can land mid-read, not just in the poll gap, and --wait now holds that window open for up to 300 s. Both return 130 bare with nothing printed, matching cmdWorkloadGet at workloads.ts:321; the user is the one who stopped it.

One thing worth recording: without the post-sleep check the new test doesn't just fail, it hangs. The aborted iteration goes on polling a still-queued session, so the real pre-fix symptom on a session that never finishes is worse than an error line — it's a --wait that ignores Ctrl-C until the budget runs out.

Also documented, since it was only true by accident before: the subagent keeps running after the interrupt, so a later get still collects it.

`core/time.ts`'s `sleep` resolves on abort rather than rejecting, so an
interrupt during the poll gap left the loop to go round once more:
`mintSessionToken` (which takes no signal), then `readLeafState` with an
already-aborted signal, which the transport wraps as a network error. The
user got an error line and exit 1 where 130 was documented.

Checked in the two places `workloads.ts` checks: after the sleep, and in
the catch, so an interrupt mid-read is also 130 rather than the wrapped
error. Both return bare, printing nothing — the user is the one who
stopped it, matching `cmdWorkloadGet`.

Without the post-sleep check the new test does not merely fail, it hangs:
the aborted iteration polls a still-queued session forever. The subagent
keeps running either way, so a later `get` still collects it; the
reference now says so.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@cwiklik
cwiklik merged commit 175a09a into rossoctl:main Nov 19, 2026
18 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants