Repository navigation
feat(sandbox-client, mocactl): the flight recorder joins a session's calls to their Execs - #985
Conversation
…calls to their Execs
A session's tool calls could not be joined to the Execs they ran: the
client never kept an Exec's req_id, the flight recorder had no id of
its own, and commands other than `sbx mcp` recorded no Exec at all.
Format 2 of the flight recorder is additive, and asks nothing of the
agent, runner or proxy:
- ExecResult.reqIds: the distinct req_ids an Exec was sent under (a
token refusal retries the same one, a stale_mode refusal a fresh
one). ExecOptions.onAttempt reports each attempt's req_id, start,
ms, exit and outcome; ExecOptions.callId is sent as
`x-moca-call-id` gRPC metadata ([A-Za-z0-9._:-]{1,64}).
- `sbx mcp` writes a `session` line after MCP initialize: an
`mcp_session` id the server mints, `sbx`, the handshake's clientInfo
as sent (capped), mocactl's version and the optional
MOCACTL_SBX_RUN_ID. Each `call` line gains `n` (the server's own
counter), `rpc_id`, `exec[]` and `meta_tool_use_id` (only if the
client sends one in `_meta`); a `session_end` line ends it, best
effort. Each Exec carries `<mcp_session>:<n>` as its call id.
- Every other command writes one `exec` line per attempt (`sbx run`,
push, pull, …) with `invocation` (`--record`'s id when that is on)
and `<invocation>:<k>` as the call id.
- `sbx doctor` counts call lines only.
Never the command, its output or a token. The SBX0 spec's flight
recorder section and the CLI reference describe the format.
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
sandbox-relay keeps its own copy of the rule (it doesn't depend on sandbox-client), so both sides assert the same literal. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
clawgenti
left a comment
There was a problem hiding this comment.
Summary
The format is additive in the way it claims, and the join keys line up end to end: a call line's exec[].req_id is the id the relay records and wid resolves to on the worker, and the call id each Exec carries (<mcp_session>:<n>, <invocation>:<k>) matches what #981's relay writes as call_id. The per-attempt observer is wired so a record failure cannot fail the agent's command (observe catches), the recorder is diagnostics-only on both sides, and the --record invocation id is reused rather than minted twice, so the two records of one command join. sbx mcp correctly opens its connection without execRecord, so its Execs appear only on the call line (the new test asserts no separate exec lines).
Verified at 3d891f5, in this sandbox:
npx tsc --noEmitclean inpackages/sandbox-clientandpackages/mocactl.packages/sandbox-client: 284 passed, 2 skipped (286).packages/mocactl: 2219 passed, 7 skipped, 1 failed —test/sbx-up.test.ts"rm reports a file system error as a message naming the file". It chmods.claude/to0o500and expectsEACCES; running as root the chmod denies nothing,rmsucceeds, and the teardownchmodthen hitsENOENT. It fails the same way onmain, and neither the test nor its code is touched here.- Two nits/suggestions inline.
Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (packages/sandbox-client exec-conn and MCP server, packages/mocactl sbx call record/exec/MCP doctor), specs and CLI docs
Agent/IDE config (.claude/.vscode): none
Commits: 2, all signed off: yes
CI status: passing (k8s-kind-e2e, lint, microvm-gates, proto, shellcheck, trivy-scan)
Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).
| mcp_session: mcpSession, | ||
| ...(o.sandboxId ? { sbx: o.sandboxId } : {}), | ||
| n, | ||
| rpc_id: extra.requestId, |
There was a problem hiding this comment.
suggestion — rpc_id is recorded exactly as the client sent it and is the one client-chosen string in the format-2 line with no cap. clientInfo values are cut at CLIENT_INFO_MAX (128) and so is meta_tool_use_id, but a tools/call whose JSON-RPC id is a long string writes that whole string into the flight recorder (NDJSON stays well-formed — JSON.stringify escapes it — so this is size/noise, not corruption). MCP allows a string id, and nothing else in the line bounds it.
Suggested fix: cap the string form the same way as the neighbours, e.g. rpc_id: typeof extra.requestId === 'string' ? extra.requestId.slice(0, CLIENT_INFO_MAX) : extra.requestId, or say in the spec why a large id is kept whole.
| recordCall(r.stateDir, sandboxId, { | ||
| v: 2, | ||
| event: 'exec', | ||
| ts: new Date(a.start).toISOString(), |
There was a problem hiding this comment.
nit — on this line ts and start are the same instant twice (new Date(a.start).toISOString() for both, and a.start comes from the client's own start). On a call line the two differ (the call's start vs the attempt's), so a reader can reasonably expect a distinction on an exec line too.
Suggested fix: either drop start from the exec line (the base line's ts already carries it) or note in the spec that the two are equal here.
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Review — flight recorder format 2
Approving. The format is additive and the join story is internally consistent across #981/#983/#985: a client exec line's req_id matches the relay's sandbox_exec.req_id, its call_id matches the relay's call_id, and the worker join goes through the relay's wid.
Verified: CALL_ID_HEADER/CALL_ID_RE are the literals #981 pins, and the client validates the id before prepare(), so a malformed id sends nothing; a token refusal reuses the req_id while a stale_mode remint gets a fresh one, with reqIds distinct/ordered and onAttempt firing for every attempt that reached the wire; the recorder writes no command text, output or token, caps each clientInfo string at 128, and only takes a tool-use id from _meta; sbx mcp writes session + numbered call lines + a best-effort session_end, and sbx doctor still counts call lines only.
Two non-blocking notes inline. CI 18/18, both commits DCO-signed. Author MEMBER (maintainer); no .claude//.vscode/ changes; no injection content in the PR.
| * How one attempt of an Exec ended, as a client record says it: `ok` (the command ran and exited), | ||
| * `timeout` (the worker timed it out), `refused_before_start` (a token or `stale_mode` refusal, | ||
| * retried under a fresh req_id), `refused`, `cancelled`, an ExecFailureKind, or `error` (anything | ||
| * else, the token source's own failure included). |
There was a problem hiding this comment.
Suggestion: This says error covers "the token source's own failure included", but such a failure never reaches onAttempt: send() awaits prepare() (which asks the token source and can throw, e.g. expiring or an ApiError) before the try that wraps once() and calls observe(). A failed mint therefore produces no client attempt record at all, so it cannot appear as error. Either observe the pre-send failure (if a record of "attempted but never sent" is wanted) or narrow this parenthetical to the failures once() can throw.
| } | ||
|
|
||
| /** An attempt's outcome from what it ended with (see ExecAttemptOutcome). */ | ||
| function outcomeOf(err: unknown): ExecAttemptOutcome { |
There was a problem hiding this comment.
Nit: This new module-level outcomeOf(err: unknown) is shadowed ~540 lines below by the pre-existing local const outcomeOf = (message: string | undefined) inside once() (line 1040). Same name, different parameter type and return shape, in one file — easy to edit the wrong one later. Consider naming this attemptOutcomeOf.
Upstream rossoctl#972, rossoctl#977, rossoctl#978, rossoctl#979, rossoctl#981, rossoctl#983, rossoctl#985, rossoctl#988 and rossoctl#990 (44 commits past 49cce28). No conflict. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
…flight-recorder lines Follow-up to #985. Each Exec attempt (ExecAttempt, via onAttempt) now carries outBytes and errBytes (the output it received: stdout as the relay's sandbox_exec.out_bytes counts it, then stderr; counts only), grpcStatus (the status the call ended with when not OK) and wireCode (the relay's or worker's wire text's code before its first ':', bounded by WIRE_CODE_RE; never the text after it). The flight recorder's format-2 attempt fields gain out_bytes, err_bytes, grpc_status and wire_code (null when absent), on both `sbx mcp`'s call lines (exec[]) and the exec lines of every other command, through one execEntry(). A failed attempt's byte counts and status are kept too, so a trace can tell a refusal before the start from a command that produced output and then failed. Six tests fail before and pass after; sandbox-client 288 and mocactl pass. The SBX0 spec section and the generated cli-sbx.md name the new fields. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
Fixes #984
What
The flight recorder's format 2: additive, asking nothing of the agent, runner or proxy.
ExecResult.reqIdslists the distinct req_ids an Exec was sent under. A token refusal retries the same one; astale_moderefusal retries under a fresh one.ExecOptions.onAttemptreports each attempt'sreq_id, start, ms, exit and outcome.ExecOptions.callIdis sent asx-moca-call-idgRPC metadata.CALL_ID_RE = /^[A-Za-z0-9._:-]{1,64}$/is exported; a malformed id is a RangeError and nothing is sent.sbx mcp:sessionline after MCPinitialize: anmcp_sessionid the server mints,sbx, the handshake'sclientInfoas sent (each string capped at 128), mocactl's version, and the optionalMOCACTL_SBX_RUN_ID.callline addsn(the server's own counter),rpc_id,exec[], andmeta_tool_use_id(recorded only if the client sends one in_meta).session_endline.<mcp_session>:<n>as its call id.sbx run, push, pull, …) writes oneexecline per attempt, withinvocation(--record's id when that's on). Each Exec carries<invocation>:<k>as its call id.sbx doctorcounts call lines only.Never the command, its output or a token. The SBX0 spec's flight-recorder section and the CLI reference describe the format.
Why
Without it, a session's tool calls can only be joined to the relay's and worker's records by time windows (see the issue). With it:
exec[].req_idmatches the relay'ssandbox_exec.req_id;call_id(feat(sandbox-relay): the sandbox_exec audit line carries wid and call_id, and harness Execs get one #981);wid(feat(sandbox-relay): the sandbox_exec audit line carries wid and call_id, and harness Execs get one #981) and its timing (feat(vmpool): the always-on vmpool: exec line carries start_ms and dur_ms #983).Tests
CALL_ID_HEADERandCALL_ID_RE.sourceto the literals sandbox-relay uses.Assisted-By: Claude Code