Repository navigation
fix: 🐛 mocactl subagents reporting long-running leaves as failed - #518
Conversation
Signed-off-by: Evaline Ju <[email protected]>
Signed-off-by: Evaline Ju <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
The ledger part of the fix looks right. I checked it against the server: sync POST /v1/runs (handleRunLeafParsed) doesn't abort when the client disconnects, and it writeResults the record before responding. So a worker whose fetch was cut off can safely leave the row non-terminal, and subagents will reconcile it to the real outcome. The tests for server rejection vs. network error, and the end-to-end reconcile test, are good.
must-fix: MOCACTL_RUN_TIMEOUT_MS cannot raise the 5-minute ceiling it's documented to override. An AbortSignal can only shorten undici's dispatcher headersTimeout. On Node 26 I reproduced a cutoff at 300.85 s with UND_ERR_HEADERS_TIMEOUT despite AbortSignal.timeout(600000). Details and options are inline.
suggestion: connect-phase errors (ECONNREFUSED/DNS) now end up as permanently-unknown rows instead of failed. Details inline.
nits:
- The
runSpawnWorkerdocstring (subagent.ts:268, "Every exit path with a sessionId writes a terminal ledger line") is now stale. The network-error path deliberately writesrunning. parseIntaccepts30m→ 30 ms (inline).
CI is green, both commits are signed off, and nothing changed under .claude/ or .vscode/.
| let res: Response; | ||
| try { | ||
| // Operators whose leaves exceed undici's default `headersTimeout` (5 min) set | ||
| // `MOCACTL_RUN_TIMEOUT_MS` to widen it; otherwise the worker's catch reconciles via |
There was a problem hiding this comment.
must-fix — AbortSignal.timeout(n) can only make the request give up earlier. It cannot make it wait longer than undici's dispatcher headersTimeout (300 s), which applies no matter what signal you pass. I checked this on Node 26.10: a server that never sends headers, plus fetch(url, { signal: AbortSignal.timeout(600000) }), still rejects after 300.85 s with TypeError: fetch failed (cause UND_ERR_HEADERS_TIMEOUT). So the MOCACTL_RUN_TIMEOUT_MS=1800000 example in the PR body still cuts off at 5 min, and the comment here ("set MOCACTL_RUN_TIMEOUT_MS to widen it") plus the README/configuration.md wording are wrong.
Options:
- Actually widen it: pass a
dispatcher: new Agent({ headersTimeout: n, bodyTimeout: n })fromundici. That's a new dependency, which esbuild bundles. Then keep the signal as an extra ceiling. - Drop the knob. The reconcile path in
subagent.tsalready handles the cutoff, which is the actual fix for 🐛 mocactl subagents shows a misleading failed: fetch failed for any leaf that runs longer than 5 minutes #515. - Keep it but document it as shorten-only (values above 300000 have no effect).
The test at transport-leaf-turn.test.ts:135 only checks that a signal is passed, so it can't catch this.
| ledger.updateState(payload.sessionId, 'failed', { detail: message, now: rt.now() }); | ||
| } else { | ||
| ledger.updateState(payload.sessionId, 'running', { | ||
| detail: `client disconnected (${message}); reconciling`, |
There was a problem hiding this comment.
suggestion — This branch now also catches connect-phase failures (ECONNREFUSED, DNS ENOTFOUND, TLS errors). In those cases the server never received the run. /runs/status never 404s (a missing record is 200 {status:'queued'}), so these rows go running → after the 60 s grace unknown: child exited and no result recorded, and stay non-terminal forever. The next subagents poll overwrites the original error text. Before this PR they showed failed: ….
The same thing affects genuine long leaves: from disconnect+60 s until the leaf finishes, the row reads "no result recorded" even though the leaf is running. The sync /v1/runs path writes no in-progress record.
Suggestion: networkError drops err.cause. Keep it, e.g. ApiError.cause / a causeCode, and treat only UND_ERR_HEADERS_TIMEOUT / UND_ERR_BODY_TIMEOUT / TimeoutError / socket-reset-after-send as "reconcile". Leave connect-phase errors terminal failed. Optionally keep the "client disconnected" hint in the unknown detail, so the operator knows the leaf may still finish.
| function runTimeoutSignal(): AbortSignal | undefined { | ||
| const raw = process.env.MOCACTL_RUN_TIMEOUT_MS; | ||
| if (raw === undefined) return undefined; | ||
| const n = parseInt(raw, 10); |
There was a problem hiding this comment.
nit — parseInt('30m', 10) → 30 and parseInt('1e6', 10) → 1, so this accepts trailing garbage and turns it into a silent 30 ms / 1 ms timeout. The docs say "Unset or non-numeric values are ignored". Number(raw) + Number.isInteger would match that.
Signed-off-by: Evaline Ju <[email protected]>
|
Thanks — all three were real. Fixup pushed: 1. Knob dropped. You're right that 2. Worker now discriminates on undici's cause code — only reconciles after-send cases; connect-phase (ECONNREFUSED, DNS, TLS, connect-timeout) stays terminal. The probe also caught 3. Moot once the knob was gone. Verified end-to-end against a hanging server on Node 22 and 24. |
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Thanks, both prior points are resolved. I checked against the code: MOCACTL_RUN_TIMEOUT_MS is gone everywhere, and the worker now reconciles only on undici's after-send timeouts (UND_ERR_HEADERS_TIMEOUT/UND_ERR_BODY_TIMEOUT, which is what the 300 s cutoff actually surfaces as). Connect-phase and ambiguous socket errors stay terminal. Approving.
Two non-blocking nits:
- (inline) Connect-phase rows will read only
failed: fetch failed, because the cause code isn't in the message. - The
runSpawnWorkerdocstring (subagent.ts:284, "Every exit path with a sessionId writes a terminal ledger line") is still stale. The after-send path deliberately writesrunning. That line isn't in a diff hunk, so I'm mentioning it here instead of inline.
| typeof (cause as { code: unknown }).code === 'string' | ||
| ? (cause as { code: string }).code | ||
| : undefined; | ||
| return new ApiError(source, 0, 'network_error', message, undefined, causeCode); |
There was a problem hiding this comment.
nit — undici's top-level message is always fetch failed, so a connect-phase row now reads failed: fetch failed and the operator can't see why. The ECONNREFUSED test passes only because its fake message is 'connect ECONNREFUSED'. Consider folding the code into the message when present, e.g. causeCode ? ${message} (${causeCode}) : message. Then the terminal detail (and the reconcile hint) shows fetch failed (ECONNREFUSED).
Signed-off-by: Evaline Ju <[email protected]>
|
Addressed both nits in the latest commit: networkError folds the cause code into the message, so a connect-phase row now reads fetch failed (ECONNREFUSED) instead of bare fetch failed; the runSpawnWorker docstring was also updated to reflect the deliberate non-terminal after-send path |
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Thanks, both nits are addressed: networkError now puts undici's cause code in the message (fetch failed (ECONNREFUSED)), the test uses the real undici error shape, and the runSpawnWorker docstring matches the after-send running path. No new issues in a9b3673. LGTM.
Summary
A spawned leaf whose turn exceeded undici's default 5-minute headersTimeout would make the worker's fetch throw, and the worker then wrote a terminal failed: fetch failed to the subagents ledger — even though the server-side run was still happening and often succeeded; because mocactl subagents doesn't re-poll terminal rows, the misreport stuck. The fix keeps the row non-terminal and lets subagents reconcile from
/runs/statusonly when the disconnect happened after the request reached the server; connect-phase failures stay terminal.Fixes: #515
Assisted-By: Claude (Anthropic AI) [email protected]
Checklist
git commit -s).make lintand the tests for the touched packages pass.docs/page, with the right maturity label (seedocs/CONTRIBUTING-DOCS.md).make check-linkspasses if any Markdown changed.docs/specs/README.mdordocs/adrs/README.md. - N/A for bugfix