Skip to content

fix(remote-worker,docs/api): retry a command refused before it started, then exit 75 - #729

Merged
pdettori merged 12 commits into
rossoctl:mainfrom
webchang:fix/sbx-retry-refused-exec
Nov 12, 2026
Merged

pdettori merged 12 commits into
rossoctl:mainfrom
webchang:fix/sbx-retry-refused-exec

Conversation

@webchang

@webchang webchang commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Refs #728. Stacked on #727 (the static Go client, #726): this branch contains #727's commits, and its own change is the last two commits: fix(remote-worker,docs/api): retry a command refused before it started, then exit 75 and fix(docs/api): sbx.py builds one Sandbox per call, not one per retry. Once #727 merges, only those two remain.

What it does: the contract in #728, in the Go client (remote-worker/cmd/sbx) and its Python twin (docs/api/examples/sbx.py, sandbox_v1_client.py). When the sandbox refuses a command at capacity before any output chunk arrives, the client sends it again:

  • Backoff: about 1, 2, 4, 8, then 8 s, with jitter, until SBX_BUSY_RETRY_S (default 30 s; 0 keeps today's exit 125) is spent.
  • New request id for each attempt.
  • Out of retries: exit 75. Interrupted while waiting: 130.
  • Never retried: a refusal after output, and every other failure.
  • SBX_LOG keeps {t, op, ms, exit} and adds attempt, refused, reason and busy_wait_ms.
  • Docs: docs/api/sandbox-v1-direct-client.md documents the setting and the exit code.

Tests:

  • go test -race ./cmd/sbx passes: the refusal classes; a refusal then success with new request ids; the budget spent → 75; no retry after a chunk, for machinery errors, for other RESOURCE_EXHAUSTED, or for a refused token; SBX_BUSY_RETRY_S=0 keeps 125; cancel during backoff → 130; env parsing.
  • The Python examples' offline tests, run as CI runs them (grpcio 1.84.0): 51 tests, OK.

mocactl's run gets the same contract in a separate PR.

Assisted-By: Claude Code

…pace

The Go twin of docs/api/examples/sbx.py, for a coding agent whose shell may run only `sbx run` and
`sbx put`, in an image with no Python or grpcio: one static binary can be bind-mounted into any
container. The same environment (SBX_ADDR, SBX_SANDBOX_ID, SBX_WORKSPACE_KEY, SBX_TOKEN[_FILE],
SBX_TIMEOUT, SBX_LOG), the same exit codes (the command's own; 124 timeout; 125 sandbox failure; 137
for a signal), the same 8 MiB truncation notice, the same SBX_LOG line and PULL script, and req_id
below 2^53. No push: the workspace is seeded on the server side. Ctrl-C/TERM aborts the Exec it
started. Tests drive the core against a scripted SandboxExecClient. Inert unless invoked.

Signed-off-by: Rong Chang <[email protected]>
…_FILE)

sbx dialed plaintext only, so it could reach a local relay but not a TLS exec endpoint. With
SBX_TLS=1 it dials over TLS. The server is verified against the system roots plus SBX_CA_FILE's CA
when given (a sandbox token grant's execCa, for a self-issued certificate): the extra CA is added to
the roots, never replacing them, as sandbox-client's caBundle does. SBX_CA_FILE without SBX_TLS, a
bad SBX_TLS value and a file with no PEM are refused at start. Without SBX_TLS, nothing changes.

Tests (tls_test.go) run a real sandbox/v1 server over TLS on loopback with a self-issued CA:
- with the CA, the command runs through the client's own execOnce;
- without it, the dial is refused, never falling back to plaintext;
- a plaintext dial cannot reach the TLS server.
A deliberate break (the CA ignored) was caught.

Signed-off-by: Rong Chang <[email protected]>
…_ADDR)

Each sbx call is its own process, so each one looked up the exec endpoint's host again; behind a slow
resolver that cost about 20 s per command. With SBX_DIAL_ADDR the caller resolves once and sbx
connects to that ip:port, while TLS still verifies, and :authority still names, SBX_ADDR's host.
Unset, nothing changes.

Tests: a certificate issued to a name that does not resolve, reached by IP (passes); the same with
another name (refused); the setting's parsing.

Signed-off-by: Rong Chang <[email protected]>

@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 retry contract from #728 is implemented carefully, and it matches between the Go and Python clients:

  • only refusals that arrive before any Chunk are retried, each attempt with a new req_id;
  • cur follows the attempt that is in flight, so a signal aborts the right Exec;
  • the budget is enforced, with exit 75 when it runs out and 130 when interrupted;
  • SBX_BUSY_RETRY_S=0 keeps exit 125.

go vet, gofmt and go test -race ./cmd/sbx pass on head. So do the Python example tests, run as CI runs them (grpcio 1.84.0): 51 tests, OK. No must-fix issues. The two #727 suggestions (signal exit code, timeout detection) are still present in outcome() on this head, confirmed with throwaway probes that were deleted afterwards. There is also one nit on the Python retry loop.

Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (remote-worker/cmd/sbx), Python (docs/api/examples), Docs, tests
Agent/IDE config (.claude/.vscode): none
Commits: 7 commits (6 from #727, which this PR is stacked on), all signed-off: yes
CI status: passing

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

Comment thread remote-worker/cmd/sbx/sbx.go Outdated
}
code := int(r.exitCode)
if code < 0 {
code = 128 + 9

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 is the same finding as on #727: the signal exit is hard-coded to 137, but sbx.py returns 128 - exit_code. A probe on this head with end(-15) returned 137 here and 143 from sbx.py. Suggested fix: code = 128 - code.

Comment thread remote-worker/cmd/sbx/sbx.go Outdated
func outcome(r result, err error, stdout, stderr io.Writer) int {
if err != nil {
msg := err.Error()
if strings.Contains(msg, "timeout:") {

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 is the same finding as on #727, now in outcome(). strings.Contains(msg, "timeout:") turns a machinery error such as vsock dial timeout: 30s into 124 "command timed out" (probe on this head), where sbx.py fully matches ^timeout:\d+$. A real timeout also drops any partial stdout, which sbx.py writes. Suggested fix: classify the timeout in execOnce from the in-stream ExecError, and keep the buffered output.

Comment thread docs/api/examples/sbx.py Outdated
waited, attempt, attempt_start = 0.0, 1, started
while True:
try:
r = sandbox().exec(command, stdin=stdin, timeout_s=int(os.environ.get("SBX_TIMEOUT", "600")))

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 — sandbox() runs inside the retry loop, so every retry builds a new Sandbox (a new channel) and a new TokenSource, which reruns SBX_TOKEN_CMD (mocactl sbx token …) up to about 6 times in the default 30 s budget. The new req_id per attempt comes from exec(), not from the new Sandbox, so calling sandbox() once before the loop keeps that behaviour and avoids the extra mints and connections.

A timeout is the worker's in-stream ExecError whose whole message is timeout:<n>, as sbx.py's
TIMEOUT_MESSAGE.fullmatch checks it, and the output so far is written before exit 124 ("command
timed out after N s"). Any other error, including a machinery message that mentions a timeout
("vsock dial timeout: 30s"), is a sandbox failure, 125. A command killed by signal n exits 128 + n
(143 for SIGTERM), not always 137. The usage text states the two differences from sbx.py: TLS only
with SBX_TLS=1, and no SBX_TOKEN_CMD. From review on rossoctl#727.

Signed-off-by: Rong Chang <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 12, 2026
The retry loop called sandbox() on every attempt, so each retry opened a new channel and a new
TokenSource, which reran SBX_TOKEN_CMD. A retry needs only a new req_id, which Sandbox.exec draws per
call, so the Sandbox is built once (still inside the try, so a failed token command is reported as
before). From review on rossoctl#729.

Signed-off-by: Rong Chang <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
@webchang
webchang force-pushed the fix/sbx-retry-refused-exec branch from f850479 to 2343b38 Compare November 12, 2026 13:44
@webchang

Copy link
Copy Markdown
Member Author

Thanks for the review. All three points are addressed. #729 is rebased onto #727's fix, so its own change is now the last two commits:

  • Signal exits and timeouts in outcome(): the same rules as feat(remote-worker): sbx, a static Go client for one sandbox/v1 workspace #727's fix (3f162ec), carried into the retry code in 8551094. A timeout is only an in-stream timeout:<n>, with the partial output kept; every other error is 125; a signal gives 128 + n. A timeout is still never retried, since it isn't a refusal before start.
  • sbx.py (nit): the retry loop builds one Sandbox per call, not one per attempt (2343b38), so a retry doesn't reopen the channel or rerun SBX_TOKEN_CMD. A new req_id per attempt still comes from Sandbox.exec.

go vet and go test -race ./cmd/sbx pass, and the Python examples' tests pass as CI runs them (grpcio 1.84.0): 51 OK.

Assisted-By: Claude Code

@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

This is a re-review after the rebase onto the updated #727 and the new commit 2343b38. All three earlier findings are resolved:

  • outcome() inherits #727's fixes: the in-stream timeout classification (partial output kept) and 128 + n signal exits.
  • sbx.py now builds one Sandbox before the retry loop, so a retry reuses its channel and TokenSource.

The refusal classes match the worker's wire text (vmpool.RefuseMemoryBudget/RefuseMaxRuns, session/loop.go's busy: queue full) and sandbox-client's TEMPLATE_BUSY. relay_busy is limited to RESOURCE_EXHAUSTED sandbox busy. A refusal after a Chunk is never retried, in both clients. On head, go vet, gofmt and go test -race ./cmd/sbx pass. So do the Python example tests, run as CI runs them (grpcio 1.84.0): 51 tests, OK. No must-fix issues. One nit: a stale doc comment.

Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (remote-worker/cmd/sbx), Python (docs/api/examples), Docs, tests
Agent/IDE config (.claude/.vscode): none
Commits: 9 commits (7 from #727, which this PR is stacked on), all signed-off: yes
CI status: pending (most checks still running; DCO, docs, shellcheck, trivy, hadolint, dependency-review pass)

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

Comment thread remote-worker/cmd/sbx/sbx.go Outdated

// callID runs one command and turns its outcome into the process's exit code, writing output and
// the SBX_LOG line the way sbx.py does: 124 for a timeout, 125 for any other sandbox failure,
// 137 for a command killed by a signal (a negative exit code), else the command's own code.

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 — callID's doc comment still says "137 for a command killed by a signal" and does not mention 75 or 130. outcome() now returns 128 + n, and call adds exit 75 (refused at capacity until the budget ran out) and 130 (interrupted while waiting). Suggested fix: point the comment at call/outcome and the exit-code constants, so that it cannot drift again.

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

Summary

This PR is stacked on #727. Its base is now exactly #727's head 3f162ec, so I reviewed only 8551094 (the rebased retry commit) and 2343b38. The range-diff against our earlier f850479 shows that the only changes are #727's timeout and signal fixes, carried into outcome(). The retry logic itself is unchanged. 2343b38 builds one Sandbox before the retry loop, inside the try, so a failed token command is still reported as before, and a retry still gets a new req_id from exec(). This fixes clawgenti's Sandbox-per-retry nit. I found no must-fix issues. I add one nit and carry the #727 token-lifetime suggestion, which still applies to execOnce. clawgenti's open nit about the stale callID doc comment (137) still applies and is not repeated here.

Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (remote-worker/cmd/sbx), Python (docs/api/examples), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 9 commits (7 from #727 + 2), all signed-off: yes
CI status: mostly passing; check and deploy-scripts-bash32 still pending

Comment thread remote-worker/cmd/sbx/sbx.go Outdated
// callID runs one command and turns its outcome into the process's exit code, writing output and
// the SBX_LOG line the way sbx.py does: 124 for a timeout, 125 for any other sandbox failure,
// 137 for a command killed by a signal (a negative exit code), else the command's own code.
func callID(ctx context.Context, c pb.SandboxExecClient, cfg config, id uint64, op, command string, stdin []byte, stdout, stderr io.Writer, now func() time.Time) int {

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: main now calls call directly, so the only caller of callID is sbx_test.go:83. It is a test-only wrapper in production code. Moving it into sbx_test.go would also remove the stale doc comment that clawgenti flagged.

ReqId: id,
Command: command,
Stdin: stdin,
TimeoutS: cfg.timeoutS,

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 (carried from our #727; execOnce is unchanged here): timeout_s is sent as SBX_TIMEOUT (default 600) with no cap. sbx.py/Sandbox.exec caps it at the token's lifetime minus 60 s and prints sbx: timeout capped at …, and sandbox-client does the same (exec-conn.ts TOKEN_MARGIN_S). This client reads a static token from SBX_TOKEN/SBX_TOKEN_FILE and never refreshes it. With the control plane's default SH_SANDBOX_TOKEN_TTL_SECONDS=900, SBX_TIMEOUT=1200 outlives the token, and so does a 600 s command started on a token more than 5 min old. The relay then cancels the Exec (DEADLINE_EXCEEDED "sandbox token expired during the Exec"), and sbx reports a generic 125. Suggested fix: when the token is a readable JWT, cap timeout_s at exp - now - 60 as sbx.py does, and say so on stderr. At minimum, name the token-expiry case in the 125 message and add it to the usage text's list of differences from sbx.py.

…ng digits

configFrom parsed SBX_TIMEOUT with fmt.Sscanf("%d"), which stops at the first
non-digit: "1e3" became a 1 s timeout and "10s" a 10 s one, silently. Parse the
whole value with strconv.ParseUint (base 10, 32 bits) and refuse anything else,
or 0, with an error naming the variable; the client exits 2 for it, as for its
other bad configuration.

Signed-off-by: Rong Chang <[email protected]>
sbx.py's Sandbox.exec cuts timeout_s to the token's lifetime less 60 s, and
to what the token has left less 60 s (never below 1 s), and says so on
stderr; otherwise a long SBX_TIMEOUT outlives the token and the relay
cancels the Exec at its exp. The Go client reads a static token and never
refreshes it, so it needs the same rule.

When the token is a JWT, its iat and exp are read from the payload without
verifying (the relay verifies; the token is never printed) and the timeout
sent is capped by sbx.py's rule, with sbx.py's line:
"sbx: timeout capped at N s, the sandbox token's lifetime minus 60 s".
A token that is not a JWT, or has no exp, is not capped. A timed-out
command reports the capped timeout.

A DEADLINE_EXCEEDED that names the token expiring (or has no message: the
client sets no deadline of its own) now exits 125 with sbx.py's text, "...:
the command outlived its token and was cancelled; give it a shorter
timeout". The usage text says both.

Signed-off-by: Rong Chang <[email protected]>
…d, then exit 75

A worker at its memory budget, its run limit or its queue limit refuses an Exec before anything runs
("memory-budget: …", "max-runs: …", "busy: queue full"), and the relay answers RESOURCE_EXHAUSTED
"sandbox busy" the same way. The static sbx client exited 125 for these, the same as a machinery
failure, so a caller could not tell "nothing ran" from "something may have".

The Go sbx (remote-worker/cmd/sbx) and its Python twin (docs/api/examples/sbx.py,
sandbox_v1_client.py) now send the command again when the sandbox refused it at capacity before any
Chunk arrived, as sandbox-client's TEMPLATE_BUSY and busy rule does. Backoff about 1, 2, 4, 8, then 8 s
with jitter, until SBX_BUSY_RETRY_S seconds of waiting are spent (default 30; 0 means no retries and the
old exit 125). Each retry is a new req_id: the refused one never ran, and the worker dedups by req_id.
Out of retries: exit 75 (EX_TEMPFAIL), nothing ran; interrupted while waiting: 130. A refusal after any
output, and every other failure, is never retried. A signal aborts the Exec in flight.

SBX_LOG keeps {t, op, ms, exit} and adds: attempt (every record); refused and reason (a refused
attempt's class and its sanitized text, cut to 300 characters); busy_wait_ms (the call's final record).

Tests: Go (the refusal classes; refused then ok with new req_ids; the budget spent -> 75 with
busy_wait_ms; no retry after a chunk, for machinery, for other RESOURCE_EXHAUSTED, for a refused token;
SBX_BUSY_RETRY_S=0 is the old 125; cancel during backoff -> 130; env parsing), -race clean; Python.

Signed-off-by: Rong Chang <[email protected]>
The retry loop called sandbox() on every attempt, so each retry opened a new channel and a new
TokenSource, which reran SBX_TOKEN_CMD. A retry needs only a new req_id, which Sandbox.exec draws per
call, so the Sandbox is built once (still inside the try, so a failed token command is reported as
before). From review on rossoctl#729.

Signed-off-by: Rong Chang <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
…real exit codes

Since the busy retry, only the tests call callID (main calls call), so it moves
into sbx_test.go, and its stale comment, which said 137 for any command killed
by a signal, goes with it. The comments on call and outcome now give the exit
codes the code returns: the command's own, 128 + n for signal n, 124 timed
out, 125 any other failure, 75 refused at capacity until SBX_BUSY_RETRY_S ran
out, 130 interrupted while waiting to retry.

A test checks that each attempt caps its own timeout to the token: a retry
starts later, with less of the token left.

Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang force-pushed the fix/sbx-retry-refused-exec branch from 2343b38 to 486788c Compare November 12, 2026 16:51
@webchang

Copy link
Copy Markdown
Member Author

Thanks for both reviews. This branch is rebased onto #727's new head (75216ec), plus one new commit:

  • The token-lifetime cap now covers the retry path too: each attempt in call() caps its own timeout when it starts, so a retry started later gets a smaller cap. outcome() prints the capped line. TestEachAttemptCapsItsTimeout covers a refusal and then a success, where the timeouts sent are 240 and then 239.
  • callID is test-only now (486788c): it moved into sbx_test.go, which also removes its stale "137" doc comment. The comments on call and outcome now state the real exit codes: the command's own code, 128 + n for signal n, 124 for a timeout, 125 for any other failure, 75 for a refusal at capacity after SBX_BUSY_RETRY_S, and 130 if interrupted while waiting.
  • The rebase changed only what feat(remote-worker): sbx, a static Go client for one sandbox/v1 workspace #727's two new commits brought in. The retry logic is unchanged.

gofmt, go vet and go test -race ./cmd/sbx pass, and so do the Python example tests as CI runs them (grpcio 1.84.0: 51 tests, OK).

Assisted-By: Claude Code

@pdettori
pdettori merged commit 3299cc9 into rossoctl:main Nov 12, 2026
17 checks passed
pdettori pushed a commit that referenced this pull request Nov 13, 2026
…exit 75

The relay's in-flight cap ("sandbox busy") and a template worker's admission refusals
("memory-budget:", "max-runs:", "busy: queue full") refuse an Exec before its command starts.
`mocactl sbx run` and `sbx put` reported that as an ordinary sandbox failure, exit 125, so a
caller could not tell "nothing ran, try again" from "it failed partway".

This is mocactl's half of the retry contract stated in #728 (the Go client's half is #729):

- Only an ExecMachineryError of kind `busy` or `template_busy` that did not run
  (`mayHaveRun` false) is retried. A machinery failure, a token expiry, or any failure after the
  command may have started is never retried, so the command still runs at most once.
- The command is sent again after a backoff of 1, 2, 4, 8 s (then 8 s each) with +/-25% jitter,
  with one stderr note per retry ("nothing ran; retrying in N s (k/n)").
- MOCACTL_SBX_BUSY_RETRIES sets the number of retries: default 4, at most 20, 0 for none;
  anything that is not a whole number is the default.
- Refused on every try: exit 75 (EX_TEMPFAIL). Interrupted while waiting: 130. 124 and 125 keep
  their meanings.

`sbx mcp` is unchanged: its MCP server already reports a busy sandbox to the model. The usage
text, the user guide and the SBX0 design's exit codes describe the new exit.

The waits count against the sandbox token's lifetime: each retry's timeout is re-capped to what
the token has left (its lifetime less the 60 s margin, less the time waited), and with nothing left
the command is not sent again (exit 75).

Refs #728

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit to webchang/moca that referenced this pull request Nov 16, 2026
Conflicts: the sbx Go client, its tests, build-snapshot.sh and test_sandbox_v1_client.py take
upstream's reviewed versions, a superset of webchang's (rossoctl#727, rossoctl#729, rossoctl#757, rossoctl#761, the build-snapshot
PRs). packages/sandbox-relay/src/main.ts keeps webchang's per-client tokens beside upstream's
set-mode tokens, and direct clients' Execs are normalized to per-exec like the harness's.
sbx.py keeps the per-client-token environment line.

Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 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.

4 participants