Skip to content

feat(mocactl,sandbox-client): SBX0 idle lifetime - the activity hint and --lifetime (#770) - #842

Merged
pdettori merged 17 commits into
mainfrom
feat/sbx-idle-lifetime-clients
Nov 17, 2026
Merged

pdettori merged 17 commits into
mainfrom
feat/sbx-idle-lifetime-clients

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

Plan 2 of 3 for #770 (spec: docs/specs/2026-11-16-sbx-idle-lifetime-design.md). Stacked on #837; review only the commits after 6efafbc6 (the merge commit c3565119 only brings in #837's review fixes).

The clients side of idle lifetimes:

  • @moca/sandbox-client
    • Each token ask says whether it is activity. Exec, process calls and owner lease ops (reset, set_mode, sandbox_deleted) ask with activity. The lease keepalive and the lease status ask without it.
    • The connection adopts each grant's sandboxExpiresAt, so a session that outlives the sandbox's original deadline keeps running. Without the field, behaviour is as today.
    • The MCP server's "expired on" text uses the latest deadline.
  • mocactl token source
    • The API client sends {"activity":false} only for a no-activity mint, and no body otherwise (older control planes).
    • A command never runs on a token from a no-activity mint. An activity ask re-mints if needed, and never rides a no-activity mint in flight.
    • An aborted ask starts no mint.
  • Mints that run no command: sbx get (status, set-mode re-send, its denied retry), the sbx mcp lease keepalive, and sbx env mint with activity:false. sbx token and --renew-every keep counting, because a direct client's Execs run on them; the docs say so.
  • --lifetime DURATION on create and up (36h, 14d, 30m, or seconds).
    • The control plane enforces the range (1 h to the deployment maximum).
    • up warns and carries on when it reuses a sandbox.
    • Both commands say so when an older control plane did not take the flag.
  • Output: create, up, doctor, get, ls (and ls --all, with aligned columns) show after 7 d without use when the record carries idleLifetimeS. Nothing extra is shown otherwise. doctor's <24 h warning says any command extends the sandbox.
  • Docs:
    • User guide: the lifetime limits are rewritten. They cover idle time, what extends a sandbox, --lifetime, the §7 margins (25 d / 2 d), an older mcp driving a long-lifetime sandbox, and token pumps.
    • Direct-client doc: the hint and the token-pump caveat.
    • Spec: the doctor line is corrected.

Test evidence

  • packages/mocactl: 2065 passed in the full run. Two failures: app.test.tsx (the TUI baseline failure on the base) and one sbx-templates test that times out under full-suite load and passes alone (94/94). sbx-up, sbx-push and sbx-templates each pass alone. A focused rerun of contract, sbx-mode, sbx-exec, sbx-cli and sbx-lifetime at head gives 289 passed. tsc and prettier are clean.
  • packages/sandbox-client: 260 passed. pi-schema-contract.test.ts doesn't load locally (pi-fork unbuilt), as on the base.
  • Each task got a spec and quality review, and the whole branch got a final review. Its findings are fixed in the last eight commits.

Rollout

Ship with #837 and plan 3 (deploy). With #837 alone, idle lifetimes stay off. Plan 3's setup.sh turns them on together with the workers' longer cleanup timer. Users should upgrade mocactl before using --lifetime above the default, because an older sbx mcp keepalive counts as use.

Part of #770.

Assisted-By: Claude Code

…; the sandbox end follows each grant (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…y token; sandboxExpiresAt (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…o feat/sbx-idle-lifetime-clients

Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
…user guide (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…ent (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…he MCP server follows the deadline (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
leaseControl takes opts.activity (default: every op but status); sendSetMode
passes it to the call and to its denied re-mint; get passes false.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
… number (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…ndbox (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026
Base automatically changed from feat/sbx-idle-lifetime-cp to main November 17, 2026 01:50

@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 is plan 2 of #770 (clients). Every mint that runs no command now sends {"activity":false}: the sbx mcp keepalive and its relay-refusal re-mint, lease status, all of sbx get's mints (including the set_mode re-send and the denied re-mint), and sbx env. That closes the keepalive-counts-as-activity gap from #837's review. Exec, process calls, reset, create/mode, token, doctor, rm, up and push still count as activity. The base branch's control plane reads the same field: parseActivity(...activity) in sandbox-handlers.ts:301, used for /token and /mode-token, and it is in the openapi. The client sends a body only for activity === false, so older control planes are unaffected.

I also checked these, and they look right:

  • sandboxExpiresAt adoption
  • the "did not take --lifetime" path
  • the lifetime suffixes in ls/get/doctor
  • the rewritten user-guide §7
  • sanitizeRemote on remote text

I have three suggestions. None of them block.

Author: pdettori (MEMBER, maintainer)
Areas reviewed: TypeScript (mocactl, sandbox-client), Docs, Tests
Agent/IDE config (.claude/.vscode): none
Commits: 14 commits (one is a merge of the base branch), all signed-off: yes
CI status: passing (DCO, check, k8s-kind-e2e, which is all that runs for a PR based on a non-main branch)

Comment thread packages/mocactl/src/sbx/cli.ts Outdated
worker's)
worker's);
--lifetime: the sandbox ends after DURATION (36h,
14d, 30m or seconds) without use (default: the

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: The 30m example can never succeed. The control plane's floor is MIN_SANDBOX_LIFETIME_S = 3600 (base branch sandbox-config.ts:109), and parseLifetime (sandbox-handlers.ts:205-217) refuses anything lower with 400 invalid_request. The same example appears at cli.ts:289 and docs/sbx/user-guide.md:91, and the guide never mentions the 1 h minimum. I'd change the example (e.g. 90m) and say "at least 1 h". You could also refuse values under 3600 on the client with a clear message, as parseIdleTimeout does for its range.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 86536f0. The examples now use 90m, and the usage text and user guide say "at least 1 h, at most the deployment's maximum". The client still checks syntax only, so the control plane remains the one place that enforces the range.

Comment thread packages/mocactl/src/sbx/doctor.ts Outdated
fix:
'pull its work before then (mocactl sbx pull --apply); for a fresh sandbox, ' +
`mocactl sbx rm, then ${UP}`,
`it ends after ${validLifetime(s.idleLifetimeS) ? lifetimeText(s.idleLifetimeS) : 'its lifetime'} ` +

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 fix text is also printed when the record has no idleLifetimeS (the 'its lifetime' fallback). On a control plane from before idle lifetimes, the lifetime is absolute, so "any command extends it" is wrong there. The old advice (pull, then sbx rm + up) is the right one in that case. Suggest keeping the old text when !validLifetime(s.idleLifetimeS). (#837 with SH_SANDBOX_MAX_LIFETIME_SECONDS unset also reports idleLifetimeS without sliding, but plan 3 closes that.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 989c711. When the record has no valid idleLifetimeS (a control plane from before idle lifetimes), doctor gives the old advice: pull its work, then mocactl sbx rm and mocactl sbx up. It says "any command extends it" only when the lifetime is valid. Both cases are tested.

// A no-activity mint in flight is not joined by an activity ask: wait it out, then mint.
while (pending && activity && !pending.activity) await pending.p.catch(() => undefined);
// An ask aborted during that wait starts nothing, and publishes no mint for others to join.
signal?.throwIfAborted();

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: With mintShared now async and calling throwIfAborted(), an ask whose signal is already aborted always yields a rejected inner promise. mint wraps it in untilAborted(...), and for an aborted signal that returns Promise.reject(signal.reason) straight away (:196) without ever observing the inner promise. The inner rejection is therefore unhandled, and under Node's default --unhandled-rejections=throw it can kill a long-lived sbx mcp. It can happen through remint(signal) → t.force(signal) from refreshMode(extra.signal) on an already-cancelled tool call. Main had a milder form of this (a pending fetch with an aborted signal); throwIfAborted makes it certain. A fix is to check signal?.aborted in mint before calling mintShared, or to add p.catch(() => {}) in untilAborted. A test with a pre-aborted force(signal) would pin it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ed8cbd9. mint now returns Promise.reject(signal.reason) for an already-aborted signal without calling mintShared, so no mint starts. untilAborted also attaches a no-op catch to the inner promise on its early return, as a defence for any future caller. A new test calls force(aborted) and first(aborted) and asserts the abort reason, that no mintSandboxToken call is made, and that no unhandledRejection fires.

@pdettori
pdettori marked this pull request as ready for review November 17, 2026 02:03
…ection (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…te (#770)

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori merged commit 05adbe1 into main Nov 17, 2026
17 checks passed
@pdettori
pdettori deleted the feat/sbx-idle-lifetime-clients branch November 17, 2026 02:23
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox
idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837,
rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824),
egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer
(rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers
(rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors
(rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a
sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821).

Conflicts (one):

- deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843)
  rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most
  SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and
  SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had
  added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking
  upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA
  row after SBX_PROFILE, then re-padding the table with prettier. The
  fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly.

Merged without markers, checked: setup.sh keeps the fork's
SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays
in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS;
both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional
SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional
SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases
beside upstream's. The fork's RUNBOOK, README and template-onboarding
docs say nothing about sandbox lifetime, refusal bursts, login errors or
the create rollback, so none needed a correction; GUIDE.md is unchanged
and GUIDE.pdf is not regenerated.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
pdettori added a commit that referenced this pull request Nov 17, 2026
… caveat (#770)

The idle-lifetime spec moves to v0.2, implemented in #837, #842 and #843 and
shipping in v0.11.0, and records what the build decided (sliding only once the
maximum is configured, doctor and sbx env, SBX_MAX_LIFETIME_DAYS=off, a
lowered maximum keeping the workers' idle). The registry row reads
implemented. The deploy README says to keep deploy/sbx/.generated/workers
until a lowered maximum's old value has passed: the kept idle lives only in
those bundles.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[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.

2 participants