Skip to content

feat(cloud-agent): add per-session sandbox isolation for designated orgs#1147

Merged
eshurakov merged 7 commits intomainfrom
eshurakov/cloud-agent-sandbox-per-session-id
Mar 17, 2026
Merged

feat(cloud-agent): add per-session sandbox isolation for designated orgs#1147
eshurakov merged 7 commits intomainfrom
eshurakov/cloud-agent-sandbox-per-session-id

Conversation

@eshurakov
Copy link
Contributor

@eshurakov eshurakov commented Mar 16, 2026

Summary

Add per-session sandbox isolation for designated organizations. Sessions for orgs in a hardcoded allowlist (PER_SESSION_SANDBOX_ORG_IDS) receive a dedicated SandboxSmall (standard-2 instance type) container via a new ses-{hash} sandbox ID format, while all other sessions continue using the shared Sandbox container.

Key architectural changes:

  • New SandboxSmall Durable Object class (re-exports Sandbox from @cloudflare/sandbox) with standard-2 instance type and separate wrangler bindings/migrations
  • getSandboxNamespace(env, sandboxId) routing layer inserted at every getSandbox() call site — routes ses-* IDs to env.SandboxSmall, everything else to env.Sandbox
  • sandboxId is now persisted in session metadata so resumed/cold-start sessions use the correct namespace without re-deriving
  • Fallback to generateSandboxId() for sessions that predate sandboxId storage ensures full backward compatibility
  • SandboxId type expanded to explicitly cover all generated formats (org-, usr-, bot-, ubt-, ses-, plus legacy __-delimited)
  • Schema validation uses regex .refine() instead of heuristic .transform() with mistyped as casts

Verification

  • pnpm run typecheck — passed (tsgo + wrapper tsc)
  • pnpm run test — 662/662 tests passed
  • New unit tests for generatePerSessionSandboxId, isPerSessionSandboxOrg, getSandboxNamespace
  • New integration test verifying deleteSession routes ses- prefixed IDs to SandboxSmall namespace

Visual Changes

N/A

Introduce SandboxSmall (standard-2) Durable Object for per-session
container isolation. Sessions for orgs in PER_SESSION_SANDBOX_ORG_IDS
get a dedicated ses-{hash} sandbox routed to SandboxSmall; all other
sessions continue using the shared Sandbox namespace.

- Add generatePerSessionSandboxId, isPerSessionSandboxOrg, getSandboxNamespace
- Store sandboxId in session metadata; fall back to generateSandboxId for legacy sessions
- Expand SandboxId type to cover hash-based prefixes (org-, usr-, bot-, ubt-, ses-)
- Replace schema transform heuristic with regex validation
- Add wrangler bindings and migrations for SandboxSmall DO
- Add unit and integration tests for new routing logic
@eshurakov eshurakov self-assigned this Mar 16, 2026
@eshurakov eshurakov marked this pull request as ready for review March 16, 2026 21:37
@kilo-code-bot
Copy link
Contributor

kilo-code-bot bot commented Mar 16, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (15 files)
  • cloud-agent-next/src/helpers.ts
  • cloud-agent-next/src/index.ts
  • cloud-agent-next/src/persistence/CloudAgentSession.ts
  • cloud-agent-next/src/persistence/schemas.ts
  • cloud-agent-next/src/persistence/types.ts
  • cloud-agent-next/src/router.test.ts
  • cloud-agent-next/src/router/handlers/session-management.ts
  • cloud-agent-next/src/router/handlers/session-prepare.ts
  • cloud-agent-next/src/router/handlers/session-questions.ts
  • cloud-agent-next/src/sandbox-id.test.ts
  • cloud-agent-next/src/sandbox-id.ts
  • cloud-agent-next/src/session-service.ts
  • cloud-agent-next/src/types.ts
  • cloud-agent-next/worker-configuration.d.ts
  • cloud-agent-next/wrangler.jsonc

Reviewed by gpt-5.4-20260305 · 2,296,046 tokens

…ession-service paths

Hoist sandboxId computation before prepare() in the initiate branch so it
gets stored in DO metadata. Add sandboxId to saveSessionMetadata() so
sessions created via SessionService also persist it. Fix wildcard '*'
matching in generateSandboxId to work even when orgId is undefined.
@eshurakov eshurakov merged commit c212f3b into main Mar 17, 2026
18 checks passed
@eshurakov eshurakov deleted the eshurakov/cloud-agent-sandbox-per-session-id branch March 17, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants