This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Devbox SDK is an enterprise TypeScript monorepo for Sealos Devbox management with HTTP API + Bun runtime architecture. The project consists of:
- @sealos/devbox-sdk: TypeScript/Node.js SDK for Devbox lifecycle management, connection pooling, and file operations
- @sealos/devbox-server: High-performance HTTP server running inside Devbox containers (Bun runtime)
- @sealos/devbox-shared: Shared types and utilities
Current Status (as of 2025-11-03): Core implementation complete, Phase 4 testing in progress.
# Build all packages
npm run build
# Build specific packages
npm run build:sdk
npm run build:server
# Clean build artifacts
npm run clean# Run all tests (requires .env file with DEVBOX_API_URL and KUBECONFIG)
npm test
# Run tests in watch mode (SDK only)
cd packages/sdk && npm run test:watch
# Run E2E tests (requires live Devbox environment)
npm run test:e2e
# Run benchmarks
cd packages/sdk && npm test -- --run benchmarksTest Requirements: Tests require environment variables DEVBOX_API_URL and KUBECONFIG in a .env file at the root. Tests interact with real Devbox instances and include automatic cleanup.
# Lint all packages (Biome)
npm run lint
# Fix linting issues
npm run lint:fix
# Type check
npm run typecheck# Run server in development mode
npm run dev
# Or run server directly
cd packages/server && bun run src/index.tsThe SDK follows a layered architecture:
-
Core Layer (
src/core/):DevboxSDK.ts: Main SDK class, factory for DevboxInstance objectsDevboxInstance.ts: Represents individual Devbox containers with file ops, command execution, monitoringtypes.ts: Core type definitionsconstants.ts: Default configuration values
-
API Integration Layer (
src/api/):client.ts: DevboxAPI class - REST client for Sealos Devbox API with 17 endpointsauth.ts: Kubeconfig-based authentication viaKubeconfigAuthenticatorendpoints.ts: API endpoint definitions- Uses custom
SimpleHTTPClientfor HTTP requests
-
HTTP Connection Layer (
src/http/):manager.ts:ConnectionManagerhandles pool lifecyclepool.ts:ConnectionPoolimplements intelligent connection reuse (>98% reuse rate)types.ts: Connection-related types- Connections are pooled per Devbox instance URL
-
Transfer Engine (
src/transfer/):engine.ts: Adaptive file transfer strategies- Planned support for batch uploads, compression, progress tracking
-
Security (
src/security/):adapter.ts: Security policy enforcement- Path validation and access control
-
Monitoring (
src/monitoring/):metrics.ts: Performance metrics collection- Connection pool stats, transfer metrics
The server runs inside Devbox containers on Bun runtime:
-
Core (
src/core/):server.ts: Main HTTP server (deprecated, being refactored)container.ts: DI container (ServiceContainer)router.ts: Pattern-based routingmiddleware.ts: CORS, logging, error handling, timeoutresponse-builder.ts: Standardized API responsesvalidation-middleware.ts: Zod-based request validation
-
Handlers (
src/handlers/):files.ts: File operations (read, write, delete, list, batch-upload)process.ts: Command execution and process managementsession.ts: Interactive shell sessions with stateful contexthealth.ts: Health checks and metricswebsocket.ts: Real-time file watching via WebSocket
-
Session Management (
src/session/):manager.ts:SessionManager- manages multiple shell sessionssession.ts:ShellSession- individual session with environment, cwd tracking
-
Utilities:
utils/process-tracker.ts: Background process lifecycle trackingutils/file-watcher.ts: Chokidar-based file watchingvalidators/schemas.ts: Zod validation schemas
Entry Point: src/index.ts bootstraps DevboxHTTPServer with environment config.
Connection Pooling: SDK maintains per-URL connection pools with health checks, automatic cleanup, and high reuse rates. The ConnectionManager coordinates multiple pools, while ConnectionPool handles individual pool lifecycle.
Two-Layer Communication:
- SDK → Sealos Devbox API (REST): Lifecycle management (create, delete, list, SSH info, monitoring)
- SDK → Devbox Container Server (HTTP/WS): File operations, command execution via the Bun server running at
http://{podIP}:3000
Error Handling: Custom DevboxSDKError with typed error codes (ERROR_CODES) for consistent error handling across SDK and server.
Type Safety: Shared types in @sealos/devbox-shared ensure contract consistency between SDK and server.
Environment variables (for tests):
DEVBOX_API_URL: Sealos Devbox API endpointKUBECONFIG: Kubernetes configuration for authentication
Environment variables:
PORT: Server port (default: 3000)HOST: Server host (default: 0.0.0.0)WORKSPACE_PATH: Workspace directory (default: /workspace)ENABLE_CORS: Enable CORS (default: false)MAX_FILE_SIZE: Max file size in bytes (default: 100MB)
- Monorepo: Turborepo with npm workspaces
- SDK Build: tsup (ESM + CJS, ~44KB each), outputs to
packages/sdk/dist/ - Server Build:
bun build --compilecreates standalone binariesnpm run build: Current platformnpm run build:linux: Linux x64npm run build:macos: macOS ARM64
- Linting: Biome (configured in
biome.json) - use single quotes, 100 char line width, semicolons "asNeeded" - Type Checking: TypeScript 5.5+, target ES2022, Node 22+
Tests are organized by type:
-
Unit Tests (
__tests__/unit/): Test individual components in isolationconnection-pool.test.ts: Connection pool behaviordevbox-sdk.test.ts: SDK core functionalitydevbox-instance.test.ts: Instance operations
-
Integration Tests (
__tests__/integration/): Test component interactionsapi-client.test.ts: API client integrationworkflow.test.ts: End-to-end workflowsconcurrency.test.ts: Concurrent operations
-
E2E Tests (
__tests__/e2e/): Test against live Devboxfile-operations.test.ts: File operationsapp-deployment.test.ts: Application deployment scenarios
-
Benchmarks (
__tests__/benchmarks/): Performance testingperformance.bench.ts: Connection pool, file transfer benchmarks
Test Helpers (__tests__/setup.ts):
TestHelper: Manages test Devbox lifecycle with automatic cleanupglobalHelper: Singleton instance for shared test resources- Use
waitForDevboxReady()to ensure Devbox is running before tests
- Tests require a live Sealos Devbox environment
- Set
DEVBOX_API_URLandKUBECONFIGin.env - Tests create real Devbox instances (prefixed with
test-{timestamp}-{random}) - Cleanup is automatic via
TestHelper.cleanup()inafterAllhooks - Test timeouts: 5 minutes for tests, 3 minutes for hooks
Run a specific test file:
cd packages/sdk && npm test -- __tests__/unit/connection-pool.test.ts- Main exports from
packages/sdk/src/index.ts:DevboxSDK,DevboxInstance, types - To add new API endpoints: Update
api/client.ts,api/endpoints.ts, andapi/types.ts - Connection pool config in
core/constants.ts(DEFAULT_CONFIG)
- Server binds to all interfaces (0.0.0.0) by default for container networking
- Use
SessionHandlerfor stateful shell interactions (maintains cwd, env) - Use
ProcessHandlerfor one-off commands - All handlers return standardized responses via
ResponseBuilder
The server package uses Bun-specific APIs:
Bun.write(),Bun.file()for file operationsBun.spawn()for process execution- WebSocket is Bun's native implementation
Do not use Bun APIs in the SDK package (Node.js runtime).
- Formatting: Enforced by Biome (semicolons "asNeeded", single quotes, 100 char width)
- Naming: camelCase for variables/functions, PascalCase for classes/types
- Imports: Use path aliases (
@sdk/,@server/,@shared/) in tests - Exports: Prefer named exports over default exports
- Error Handling: Use
DevboxSDKErrorwith appropriateERROR_CODES
- Main README:
/README.md - Package READMEs:
packages/*/README.md - Task tracking:
tasks/directory with PRDs and implementation plans - Architecture docs:
plans/REFACTOR_PLAN.md - API specs:
openspec/directory
- Changesets are configured (
@changesets/cli) - Version bumping:
npm run version - Publishing:
npm run release - CI/Release workflows currently disabled (manual trigger only)