#835

ECW sandbox list: filtered-out rows use up the page, so pages come back short or empty with x-next-token set

Openmonsterlagopened 1mo ago

Problem

GET /v2/sandboxes (ECW) asks the database for limit + 1 rows, sets the next-page cursor from the last kept row, and only then drops rows it will not return:

  • rows whose state maps to no E2B state (stopped, failed, quarantined): build_listed_sandbox returns None through translate_lifecycle_state;
  • rows that do not match the state= query filter, which runs in Rust after the fetch. Only the metadata filter is pushed into SQL.

The response can therefore hold fewer than limit items, or none at all, while x-next-token is still set. The paging decision counts rows the caller never sees.

Rows with lifecycle deleted (tombstones that tombstone_retention.rs keeps for 7 days) or cleanup_required are not excluded either. The default arm of translate_lifecycle_state maps them to running, so from reading the code they appear to be listed as running sandboxes.

Why it matters

E2B callers treat one page as up to limit matching sandboxes. In an org with many stopped or failed sandboxes, or for state=paused in an org that is mostly running sandboxes, Sandbox.list(...).nextItems() can return [] with hasNext true. Code that reads one page, or stops on the first empty page, misses sandboxes that exist, the same kind of silent miss as #577. Each call can also fetch up to 1000 rows and then discard them all.

Evidence

  • crates/libs/api/src/routes/e2b/lifecycle.rs:415-422: list_sandboxes fetches limit + 1 rows (via fetch_scoped_sandbox_page -> scoped_sandbox_page, controllers/sandbox/org.rs:101), pops the sentinel, and takes rows.last() as the cursor before any filtering.
  • crates/libs/api/src/routes/e2b/lifecycle.rs:428-434: a row is skipped when build_listed_sandbox returns None or the state filter does not contain the item's state.
  • crates/libs/api/src/routes/e2b/lifecycle.rs:442-446: x-next-token is set from that cursor whether or not any row was kept.
  • crates/libs/api/src/routes/e2b/translate.rs:893-898 (called from build_listed_sandbox, translate.rs:685-692): stopped, failed and quarantined map to None, and every other value maps to running.
  • crates/libs/api/src/controllers/sandbox/org.rs:104-112 (and the federated arm, 128-150): the SQL filters on owner, cursor and metadata, not on lifecycle.
  • The native list already avoids this for tombstones: crates/libs/api/src/routes/public/sandboxes.rs:1073-1081 adds s.lifecycle <> 'deleted' because deleted rows "must not consume a list page's lookahead". The ECW list has no such predicate.

Reproduction (not run, because it needs Postgres): create limit sandboxes and stop them through the native API (POST /v1/sandboxes/{id}:stop; ECW kill deletes the row instead). Make sure one running sandbox was created before them. Then call GET /v2/sandboxes?limit=<limit>. The first page should be empty and carry x-next-token, and the running sandbox should appear only on the second page.

Proposed approach

Push both filters into scoped_sandbox_page, so that limit + 1 counts only rows the response can return and the existing cursor logic stays correct:

  • Always exclude stopped, failed, quarantined, deleted and cleanup_required.
  • Add the requested E2B states as a predicate on LOWER(TRIM(s.lifecycle)) that mirrors translate_lifecycle_state. paused means paused or suspended. running means every other non-terminal value, so it needs NOT IN rather than = ANY.

The SQL can use sandboxes.lifecycle because that is what the Rust filter reads: cached_sandbox_response passes no live info, so status falls back to sandbox.lifecycle. A Postgres-backed list test could cover a page of limit terminal rows followed by a live one.

Severity

Medium (self-assessed): the call succeeds with incomplete results, so callers that find or clean up sandboxes can silently miss them. There is no security impact.

This issue was drafted by an AI agent (Claude) for @monsterlag.

Activity

1
NancyKwongPerplexity1mo ago

Thanks for opening this issue! A SPACE maintainer will take a look.

CONTRIBUTING.md explains how we review outside contributions.