Skip to content

feat(sandbox-client, mocactl): the flight recorder joins a session's calls to their Execs - #985

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
webchang:pr/flight-recorder-v2
Nov 23, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
webchang:pr/flight-recorder-v2

Conversation

@webchang

Copy link
Copy Markdown
Member

Fixes #984

What

The flight recorder's format 2: additive, asking nothing of the agent, runner or proxy.

  • sandbox-client:
    • ExecResult.reqIds lists the distinct req_ids an Exec was sent under. A token refusal retries the same one; a stale_mode refusal retries under 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. CALL_ID_RE = /^[A-Za-z0-9._:-]{1,64}$/ is exported; a malformed id is a RangeError and nothing is sent.
  • sbx mcp:
    • A session line after MCP initialize: an mcp_session id the server mints, sbx, the handshake's clientInfo as sent (each string capped at 128), mocactl's version, and the optional MOCACTL_SBX_RUN_ID.
    • Each call line adds n (the server's own counter), rpc_id, exec[], and meta_tool_use_id (recorded only if the client sends one in _meta).
    • A best-effort session_end line.
    • Each Exec carries <mcp_session>:<n> as its call id.
  • Every other command (sbx run, push, pull, …) writes one exec line per attempt, with invocation (--record's id when that's on). Each Exec carries <invocation>:<k> as its 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.

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:

Tests

  • 17 tests fail on main's code and pass with the change: 11 in sandbox-client and 6 in mocactl, including two existing exact-shape tests updated for the new fields.
  • Full suites: sandbox-client 286, mocactl 2224 (4 skipped). Both typecheck.
  • A test pins CALL_ID_HEADER and CALL_ID_RE.source to the literals sandbox-relay uses.

Assisted-By: Claude Code

…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]>
@webchang
webchang requested a review from pdettori as a code owner November 22, 2026 06:22
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 23, 2026

@clawgenti clawgenti 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.

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 --noEmit clean in packages/sandbox-client and packages/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/ to 0o500 and expects EACCES; running as root the chmod denies nothing, rm succeeds, and the teardown chmod then hits ENOENT. It fails the same way on main, 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,

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 — 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(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 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 — 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).

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: 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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@pdettori
pdettori merged commit 4ed95d5 into rossoctl:main Nov 23, 2026
18 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 23, 2026
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]>
pdettori pushed a commit that referenced this pull request Nov 24, 2026
…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]>
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.

mocactl: a session's tool calls can't be joined to the Execs they ran

4 participants