Skip to content

SBX0: count sandbox TTL as idle time, not from creation #770

Closed

Description

@pdettori

Problem

ADR-0042 gives each SBX0 sandbox a fixed absolute lifetime: 7 days from creation, with no extension. Using the sandbox does not reset the clock.

Concretely, expiresAt is computed once at create (packages/control-plane/src/sandbox-handlers.ts:168, now + cfg.lifetimeS) from SH_SANDBOX_LIFETIME_SECONDS, default 7 days (packages/control-plane/src/sandbox-config.ts:158). Expiry is computed, never scheduled (packages/control-plane/src/sandbox-store.ts:33-44), and minting a token past expiresAt refuses with sandbox_terminated (sandbox-token.ts), so at the deadline the record simply reads ready -> terminated (expired) and access stops.

That is the right behavior for an abandoned sandbox, and it is what bounds disk and worker capacity. It is the wrong behavior for an actively used one: a user who works in a sandbox daily for a week loses the workspace at the 7-day mark even though the sandbox was never idle. They lose work they were actively using.

Desired

Count the lifetime as idle time, not time since creation: expiresAt = lastActivityAt + lifetime. Activity pushes the deadline forward; the sandbox expires only after lifetime with no activity.

Open questions

  1. Maximum lifetime cap. ADR-0042's no-silent-reset invariants include "the worker's idle reclaim is set longer than any sandbox can live." A pure sliding TTL makes lifetime unbounded, so that invariant no longer holds and a long-lived sandbox can hit the worker's idle reclaim. Options:
    • (a) Sliding idle TTL with no absolute cap, and change/disable the worker's idle reclaim for sandboxes — breaks the current invariant; probably not wanted.
    • (b) Sliding idle TTL plus a hard max age (e.g. 30 or 90 days from creation), with expiry reporting idle vs max_age. Recommended.
    • (c) Keep the absolute lifetime but make it longer — does not fix the reported problem.
  2. What counts as activity. Token mint (covers both mocactl sbx exec and the MCP tool server) and every successful Exec are the natural candidates. Read-only list/get should probably not count, or a dashboard poll keeps a sandbox alive forever. Token mint is the cheapest single choke point and covers all clients.
  3. ADR/spec change. ADR-0042 says "fixed absolute lifetime" and "the control plane owns the lifetime." This needs an ADR amendment (or a superseding ADR) and a spec §5.2 update before implementation.
  4. Existing records. Records created before the change carry an expiresAt computed at create. If expiry only ever moves forward, no migration is needed — sliding takes effect from the next activity.

Implementation notes

  • Handler: createSandbox sets the deadline (sandbox-handlers.ts:168). Sliding needs an update path on activity; mintSandboxToken already reads the record and returns expiresAt, so it is the natural place.
  • Store: both backends must extend the deadline atomically. RedisSandboxStore keeps expiresAt in the record hash, as the score in the live ZSET, and derives the name key's PXAT from it (packages/control-plane/src/sandbox-redis.ts), so extending means HSET + ZADD + PXAT in one script while preserving the same now >= expiresAt refusal semantics under concurrency. The in-memory SandboxStore is the contract; test/helpers/sandbox-store-contract.ts should gain the extend case and run against both.
  • Tokens: minted with notAfter: rec.expiresAt, so a moved-forward deadline just raises the cap.
  • Retention/janitor: termination and retention should still count from the terminal terminatedAt, not from the last activity.
  • Config: SH_SANDBOX_LIFETIME_SECONDS changes meaning from absolute to idle lifetime; add SH_SANDBOX_MAX_LIFETIME_SECONDS if option (b) is taken.

Acceptance criteria (draft)

  • A sandbox with activity inside each lifetime window does not expire.
  • A sandbox idle for lifetime reads terminated/expired and stops minting tokens.
  • If a max age is adopted, the sandbox expires at max age regardless of activity, with a distinguishable reason.
  • Contract tests cover extend against both stores; the Redis path is one atomic script.
  • API responses' expiresAt reflects the extended deadline.
  • ADR-0042 amended and the spec updated.

References

Activity

  1. added
    enhancement
    on Nov 12, 2026
  2. added theissue type on Nov 12, 2026
  3. 23 remaining items

  4. pdettori commented on Nov 17, 2026

    @pdettori
    MemberAuthor

    Implemented, and shipping in v0.11.0. It will be tested as part of the v0.11.0 release testing.

    SBX0 rollout (part of v0.11.0): run deploy/sbx/setup.sh with SBX_ACK_HOSTS=1. That raises the worker idle from 216 h to 768 h and turns idle lifetimes on. Reinstall every host's workers the same day, then check that deploy/sbx/status.sh shows no worker idle warning. If the hosts can't be reinstalled that day, use SBX_MAX_LIFETIME_DAYS=off. Cohort users should upgrade mocactl before using --lifetime above the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions