An open-source, faster Devin alternative
TypeScript
design: revise sandbox tags and usage after fresh-eyes review
Igor Zalutskiaf36a40dbfd0
diff --git a/.agents/design/sandbox-tags-and-usage.md b/.agents/design/sandbox-tags-and-usage.mdindex e62f91b3..97d3207c 100644--- a/.agents/design/sandbox-tags-and-usage.md+++ b/.agents/design/sandbox-tags-and-usage.md@@ -1,6 +1,6 @@# Sandbox tags and usage visibility-API-only surface for attributing sandbox spend to the customer's own+API-only surface for attributing sandbox spend to customer-definedgroupings (team, env, customer, etc.) and drilling down to individualsandboxes. GB-seconds, not dollars — Stripe stays the pricing sourceof truth.@@ -8,122 +8,178 @@ of truth.## Why: today's billing is org-rollup only`GetOrgUsage` aggregates `sandbox_scale_events` for the whole org and-reports totals per memory tier to Stripe. There is no way for a customer-to see which sandboxes or which of their groupings drove the bill.-Dashboards, chargebacks, and "why is spend up this month" investigations-are all impossible without this surface.+reports totals per memory tier to Stripe. There is no way for a+customer to see which sandboxes or which of their groupings drove+the bill. Dashboards, chargebacks, and "why is spend up this month"+investigations are all impossible without this surface.Dashboard/CLI come later. v1 is API only.-## Tags reuse existing `metadata` JSONB — no new field+## Tags live in a new table, not on session metadata++First instinct was to reuse `sandbox_sessions.metadata JSONB` — a+user-settable column that is persisted but never queried. On closer+inspection the semantics don't line up: **a sandbox owns many+`sandbox_sessions` rows** (one per start/resume), and each captures+the metadata passed to THAT create call. The column is "per-session+metadata at creation", not "sandbox-level tags." Reusing it would+silently require `ORDER BY started_at DESC LIMIT 1` on every tag+query to pick a winner, and PATCHing it would overwrite an+otherwise-immutable create-time snapshot. Wrong shape.++Instead:++```sql+CREATE TABLE sandbox_tags (+ org_id UUID NOT NULL,+ sandbox_id TEXT NOT NULL,+ key TEXT NOT NULL,+ value TEXT NOT NULL,+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (sandbox_id, key)+);+CREATE INDEX ON sandbox_tags (org_id, key, value);+```-`sandbox_sessions.metadata JSONB` has been user-settable since migration-001, via `POST /sandboxes` body and `oc sandbox create --metadata k=v`.-Audit of the codebase: the field is persisted and round-tripped on read,-but no handler filters, groups, or otherwise acts on it. It's-write-only decoration today.+Row-per-tag keeps grouping SQL clean (`LEFT JOIN sandbox_tags t ON+t.sandbox_id = e.sandbox_id AND t.key = 'team' GROUP BY t.value`),+makes tag-count limits trivial to enforce, and cleanly separates+"current tags" (mutable) from session metadata (historical snapshot).-We rescope metadata as tags rather than adding a parallel `Tags` field.+`sandbox_sessions.metadata` is left intact — still captured at create+time, still unread, still round-trips. No SDK surface for it changes.-- No SDK break — existing `--metadata` flag and `SandboxConfig.Metadata`- keep working and now carry real semantics.-- No semantic overlap users have to disambiguate ("when do I use tags vs- metadata?").-- The only risk is retroactive bucketing of free-form data users may- have stuffed in metadata. Communicated in release notes; users can- clear it via the new PATCH.+## Attribution is live: retagging rewrites history-Implications:+Queries join live `sandbox_tags`. A retag changes all historical+attribution for that sandbox going forward. Fine for ops and+"where is the money going this month"; hazardous for chargebacks.-- `PATCH /sandboxes/{id}` is new — metadata is create-only today.-- Add GIN index on `sandbox_sessions.metadata jsonb_path_ops`.-- Current tags attribute all historical spend. Retagging re-buckets.- Simpler than snapshotting tags onto every scale event; revisit only- if stable historical attribution becomes a real ask.+v1 does not snapshot tags onto scale events. Mitigation in the+response: every sandbox-level payload carries `tagsLastUpdatedAt`+(= max `updated_at` across the sandbox's tags) so dashboards can+annotate "tags edited on …" and stakeholders don't mistake+retagging for spend movement.-## Unit is GB-seconds (memory + disk overage)+If stable historical attribution ever becomes required (chargeback+automation, invoice reconciliation), the upgrade path is a+`sandbox_tag_changes` audit table or inline snapshotting onto scale+events. Out of scope for v1.-Stripe owns pricing — rates vary by plan, trial, grandfathering. A-server-computed dollar figure would be misleading and would silently-drift from the invoice. We surface the physical quantities:+## Unit is GB-seconds — identical math to the Stripe pipeline-- `memoryGbSeconds = memory_mb/1024 × duration_s`, summed over- `sandbox_scale_events` in the window.-- `diskOverageGbSeconds = max(0, disk_mb-20480)/1024 × duration_s`.- Matches what's actually billed (`DiskOverageGBSeconds` in- `internal/billing/pricing.go`), not raw disk — raw disk doesn't tie- to anything.-- Active events count, capped at `now()` — running sandboxes show live- accrual, which is what users expect from a spend view.-- CPU not exposed: deterministic from memory (1 vCPU per 4GB), so- memory GB-seconds already captures it.+Stripe owns pricing — rates vary by plan, trial, grandfathering.+A server-computed dollar figure would drift from the invoice. We+surface the physical quantities the invoice is computed from, using+**bit-for-bit identical math to `GetOrgUsage` and+`DiskOverageGBSeconds`:**-## One aggregator, one discovery endpoint, one drilldown, one PATCH+- `memoryGbSeconds = SUM(memory_mb/1024 × EXTRACT(EPOCH FROM duration))`+- `diskOverageGbSeconds = SUM(max(0, disk_mb-20480)/1024 × duration)`+- Active events (`ended_at IS NULL`) use `COALESCE(ended_at, now())`+ — running sandboxes show live accrual.+- Float math throughout (no rounding). Matches the existing Stripe+ pipeline so per-tag sums always reconcile to the org total.+- CPU not exposed: deterministic from memory tier (1 vCPU per 4GB).-```-GET /usage # aggregator, dimensioned-GET /tags # tag-key discovery-GET /sandboxes/{id}/usage # single-sandbox drilldown-PATCH /sandboxes/{id} # tag updates (body: metadata)-```+A reconciliation test (see Implementation notes) asserts+`Σ by-sandbox = Σ by-tag + untagged = GetOrgUsage(org)` within float+epsilon. If `GetOrgUsage` math ever changes, this code changes in+lockstep.++## Data freshness: fresh on clean shutdown, reconciled otherwise-### Design middle: dimensions as data, not routes+Scale events close on two paths:-We explicitly considered two poles before landing here:+- **Clean transitions** via `UpdateSandboxSessionStatus` — immediate.+- **Worker heartbeat reconciliation** via `ReconcileWorkerSessions`+ — closes zombie rows on worker crash or sandbox death.-- **Narrow endpoints** (`/usage/by-sandbox`, `/usage/by-tag`) — baked in- one dimension per route. Adding `status`, `template`, `region` later- means new routes. `/by-tag` privileges tags over other dimensions.-- **Composable query DSL** (`POST /usage/query` with- `{metrics, groupBy, filter, orderBy}`) — ELK/Prometheus/Cost Explorer- shape. Maximum flexibility, but reinvents a query engine for a- problem that doesn't yet have multi-dim demand.+So "fresh to the minute" holds for clean shutdowns; worst case is+reconciliation-lagged (sandbox dies, usage keeps accruing until the+next heartbeat sweep closes the row). Call it out on the endpoint+reference.++## Surface++```+GET /usage # aggregator (dimensions as data)+GET /tags # org-wide tag-key discovery+GET /sandboxes/{id}/usage # per-sandbox drilldown+GET /sandboxes/{id}/tags # read current tags+PUT /sandboxes/{id}/tags # full-replace tag set+```-The middle: **one REST aggregator with `groupBy` and `filter[]` as-query params**. Dimensions are strings from a small known set-(`sandbox`, `tag:<key>`, future `status`, `template`, `region`). Adding-a dimension later costs one string, not a new route. No query body, no-filter tree, no nested aggregations — the shape of Stripe's list-endpoints or GitHub's search.+Plus an additive change: `GET /sandboxes` and `GET /sandboxes/{id}`+responses gain `tags` and `tagsLastUpdatedAt`.### `GET /usage` — aggregator+Dimensions are data, not URL segments. Adding `status`, `template`,+`region` later costs one string in `groupBy`, not a new route. One+dimension at a time in v1 — multi-dim extends via comma separation+(`groupBy=tag:team,tag:env`) additively.++Alternatives considered: narrow per-dimension routes+(`/usage/by-sandbox`, `/usage/by-tag`) privilege tags in URLs and+grow a route per future dimension; a full composable DSL+(`POST /usage/query` with filter trees, nested aggs) reinvents a+query engine before there's multi-dim demand.+Query parameters:| Param | Values | Notes ||---|---|---|-| `groupBy` | `sandbox`, `tag:<key>` | Required. One dimension. |+| `groupBy` | `sandbox`, `tag:<key>` | Required. || `filter[<dim>]` | any | Repeatable, AND-ed. `filter[tag:team]=` (empty) = "key absent". |-| `from`, `to` | ISO8601 | Defaults to last 30 days. |-| `sort` | `-memoryGbSeconds` (default), `-diskOverageGbSeconds`, `sandboxCount` | |+| `from`, `to` | ISO8601 | Default: last 30 days. Max window: 90 days. |+| `sort` | `-memoryGbSeconds` (default), `-diskOverageGbSeconds` | Secondary sort by `sandboxId` / `tagValue` for cursor determinism. || `limit`, `cursor` | default 50, max 500 | Opaque cursor. |-Response shape when `groupBy=tag:team`:+Response, `groupBy=tag:team`:```json{- "from": "2026-03-23T00:00:00Z",- "to": "2026-04-22T00:00:00Z",+ "from": "...", "to": "...","groupBy": "tag:team",- "total": { "memoryGbSeconds": 19000, "diskOverageGbSeconds": 340 },+ "total": { "memoryGbSeconds": 19000, "diskOverageGbSeconds": 340 },+ "untagged": { "memoryGbSeconds": 1000, "diskOverageGbSeconds": 20,+ "sandboxCount": 2 },"items": [- { "tag:team": "payments", "memoryGbSeconds": 8000,- "diskOverageGbSeconds": 120, "sandboxCount": 12 },- { "tag:team": "growth", "memoryGbSeconds": 4000,- "diskOverageGbSeconds": 50, "sandboxCount": 5 },- { "tag:team": null, "memoryGbSeconds": 1000,- "diskOverageGbSeconds": 20, "sandboxCount": 2 }+ { "tagKey": "team", "tagValue": "payments",+ "memoryGbSeconds": 8000, "diskOverageGbSeconds": 120,+ "sandboxCount": 12 },+ { "tagKey": "team", "tagValue": "growth",+ "memoryGbSeconds": 4000, "diskOverageGbSeconds": 50,+ "sandboxCount": 5 }],"nextCursor": null}```-The `null` row is the untagged bucket. Sum of groups + null row = total-— dashboards can reconcile.+`untagged` is a sibling field, not a null-valued item — typed SDKs+don't null-check items. `Σ items + untagged = total` by construction.-When `groupBy=sandbox`, items include `sandboxId`, `alias`, `status`-(`running|hibernated|stopped|destroyed`), `tags` (the metadata map), and-the metrics.+Response, `groupBy=sandbox`:++```json+{+ "from": "...", "to": "...",+ "groupBy": "sandbox",+ "total": { "memoryGbSeconds": 20000, "diskOverageGbSeconds": 360 },+ "items": [+ { "sandboxId": "sbx_abc", "alias": "my-agent",+ "status": "running",+ "tags": { "env": "prod", "team": "payments" },+ "tagsLastUpdatedAt": "2026-04-19T14:02:00Z",+ "memoryGbSeconds": 8000, "diskOverageGbSeconds": 120 }+ ],+ "nextCursor": null+}+```++No `sandboxCount` per row (always 1). `status` values reconcile with+the existing state machine: `running | hibernated | stopped | error`.### `GET /tags` — discovery@@ -134,75 +190,98 @@ the metrics.] }```-Powers "Group by ___" selectors. Backed by-`SELECT jsonb_object_keys(metadata), COUNT(...) FROM sandbox_sessions-WHERE org_id = $1 GROUP BY 1`, org-scoped.+Backed by `SELECT key, COUNT(DISTINCT sandbox_id),+COUNT(DISTINCT value) FROM sandbox_tags WHERE org_id = $1 GROUP BY+key`, org-scoped.### `GET /sandboxes/{id}/usage` — drilldown```json{- "sandboxId": "sbx_abc",- "alias": "my-agent",+ "sandboxId": "sbx_abc", "alias": "my-agent",+ "status": "running","from": "...", "to": "...",- "memoryGbSeconds": 12345.6,- "diskOverageGbSeconds": 789.0,+ "memoryGbSeconds": 12345.6, "diskOverageGbSeconds": 789.0,"tags": { "env": "prod", "team": "payments" },- "status": "running",- "firstStartedAt": "...",- "lastEndedAt": null+ "tagsLastUpdatedAt": "2026-04-19T14:02:00Z",+ "firstStartedAt": "...", "lastEndedAt": null}```-Works for destroyed sandboxes too — `sandbox_scale_events` persists-after sandbox teardown.+Works for torn-down sandboxes — `sandbox_scale_events` and+`sandbox_tags` persist.++### `GET /sandboxes/{id}/tags`, `PUT /sandboxes/{id}/tags`-### `PATCH /sandboxes/{id}` — tag update+```+GET → { "tags": { "env": "prod", "team": "payments" },+ "tagsLastUpdatedAt": "2026-04-19T14:02:00Z" }++PUT body: { "env": "staging", "team": "growth" }+→ full replace, returns new state. `{}` clears all tags.+```-Body: `{ "metadata": { "team": "growth", "customer": null } }`. Merge-semantics: `null` value deletes the key, missing keys are left alone.-Scope of other PATCHable fields is out of this design.+Deliberately narrow. A broader `PATCH /sandboxes/{id}` would invite+feature creep (alias, memory, etc.), each with its own semantic+issues. Scoping to `/tags` keeps that door shut.-## Data freshness: live to the minute+### Validation (applied on PUT)-Running sandboxes are summed with `COALESCE(ended_at, now())`, so-scale-event-derived metrics (memory + disk GB-seconds) are fresh to the-minute. Sample-derived metrics (if any are added later) would lag by-the 5-minute flush interval. Document this in the endpoint reference.+- ≤ 50 tag keys per sandbox.+- Key: 1–128 chars, `[A-Za-z0-9_.-]` plus `:` for user namespacing.+- Value: 0–256 chars, UTF-8.+- `oc:` key prefix reserved for future system-set tags — PUT with+ such keys returns 400.## Explicit non-goals for v1- **Dollars.** Stripe owns invoicing.-- **Time series / bucketing.** Will drop in as `?interval=1d` on the- same endpoint, same response shape plus per-bucket rows. Not now.-- **Multi-dimensional group-by** (`groupBy=tag:team,tag:env` cross-tab).- Extends `groupBy` to comma-separated later — additive, non-breaking.-- **Filter trees** (OR/NOT/nested). Flat AND covers the real use cases- we've imagined. Add only on demand.-- **Per-event tag snapshotting.** Current tags drive all history.-- **Tag audit log.**-- **CSV / bulk export.** Paginated JSON.-- **Dashboard, CLI.** API only for v1.+- **Time series / bucketing.** Drops in as `?interval=1d` on `/usage`.+ Additive, non-breaking.+- **Multi-dim group-by** (`groupBy=tag:team,tag:env`). Extends+ `groupBy` to comma-separated. Additive, non-breaking.+- **Filter trees** (OR / NOT / nested). Flat AND covers the real+ flows.+- **Historical tag snapshotting / audit log.** `tagsLastUpdatedAt` is+ the v1 mitigation; upgrade path is a separate audit table.+- **`GET /sandboxes?tag=k:v` filter.** Obvious follow-up on the same+ `sandbox_tags` backend; tracked as a separate PR.+- **CSV / bulk export.** Paginated JSON covers it.+- **CLI / dashboard.** API only for v1.## Implementation notes-- **Migration**: one migration adding- `CREATE INDEX ... ON sandbox_sessions USING GIN (metadata jsonb_path_ops)`.- No schema change.+- **Migration**: one migration creating `sandbox_tags` (table ++ composite index). No change to existing tables.- **Query builder**: one function- `(orgID, window, groupBy, filters, sort, cursor) → rows` compiling to- SQL that joins `sandbox_scale_events` to `sandbox_sessions` for tag- access. Reuse the duration/GB-second math from `GetOrgUsage` and- `DiskOverageGBSeconds`.-- **Handlers**: new `internal/api/usage.go` for `/usage`, `/tags`, and- `/sandboxes/{id}/usage`; extend `internal/api/sandbox.go` for PATCH.- Wire in `internal/api/router.go` inside the authed group.-- **Tenancy**: every query scopes on `auth.GetOrgID(c)` — same pattern- as existing handlers.-- **Tests**: unit-test the query builder for each `groupBy × filter`- combo using fixtures in `sandbox_scale_events`; integration tests for- the three core flows (top-N sandboxes, group-by-team,- per-sandbox-drilldown).+ `(orgID, window, groupBy, filters, sort, cursor) → rows`, compiled+ to SQL joining `sandbox_scale_events` to `sandbox_tags`. Reuse the+ duration / GB-second math from `GetOrgUsage` /+ `DiskOverageGBSeconds` — do not reimplement.+- **Handlers**: new `internal/api/usage.go` (`/usage`, `/tags`,+ `/sandboxes/{id}/usage`); new `internal/api/sandbox_tags.go` (GET+ and PUT on `/sandboxes/{id}/tags`). Wire in+ `internal/api/router.go` inside the authed group.+- **Response additions**: `GET /sandboxes` list and `GET+ /sandboxes/{id}` gain `tags` + `tagsLastUpdatedAt`. Additive.+- **Tenancy**: every query scopes on `auth.GetOrgID(c)` — same as+ every existing handler. No sub-org visibility; an org-scoped API+ key sees the whole org's spend (pre-existing model).+- **Query guardrails**: reject `to - from > 90d`; reject `limit >+ 500`; handler-level timeout on `/usage` (10s suggested).+- **Tests**:+ - Query builder across each `groupBy × filter` combination.+ - **Reconciliation test**: assert `Σ by-sandbox = Σ by-tag ++ untagged = GetOrgUsage(org)` within float epsilon.+ - Integration tests for top-N sandboxes, group-by-team,+ drilldown.+ - PUT validation tests (size limits, reserved prefix, malformed+ keys).+- **SDKs**: TS + Python SDK stubs for the new endpoints. Convenience+ wrappers (`usage.byTag`, `usage.bySandbox`) designed in the SDK+ layer, not the server.+- **Docs**: reference entries under `docs/api-reference/` per+ endpoint; wire into `docs/mint.json`.## Open questions for implementation@@ -210,11 +289,9 @@ the 5-minute flush interval. Document this in the endpoint reference.from first/last scale event, or from `sandbox_sessions.started_at`?Session-level is probably more intuitive but needs a check forsandboxes with multiple sessions.-- Empty-filter "missing key" convention- (`filter[tag:team]=`) — document once; reuse on both `groupBy=sandbox`- and drilldowns so the untagged bucket from `GET /usage?groupBy=tag:team`- is always addressable.-- Org-level API key sandboxes (no user) — `GetSandboxOwner` handles them- for billing; confirm the new queries are consistent.-- SDK convenience wrappers (`usage.byTag(key)`, `usage.bySandbox()`) —- design in the SDK layer, not the server.+- Sandbox-destroy behavior for tag rows: tombstone or leave. Leaving+ preserves the drilldown for historical reports but slightly+ overstates `/tags` counts; tombstoning flips the trade-off. Lean+ leave; revisit if orgs complain.+- Whether to support a merge-mode (`PUT ...?mode=merge`) or keep+ strict full-replace and force GET+PUT for partial updates.