@cloudflare/sandbox 0.0.0-0dad837 → 0.0.0-104f455

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +121 -0
  2. package/Dockerfile +101 -70
  3. package/README.md +88 -710
  4. package/dist/chunk-2P3MDMNJ.js +2367 -0
  5. package/dist/chunk-2P3MDMNJ.js.map +1 -0
  6. package/dist/chunk-BFVUNTP4.js +104 -0
  7. package/dist/chunk-BFVUNTP4.js.map +1 -0
  8. package/dist/chunk-EKSWCBCA.js +86 -0
  9. package/dist/chunk-EKSWCBCA.js.map +1 -0
  10. package/dist/chunk-JXZMAU2C.js +559 -0
  11. package/dist/chunk-JXZMAU2C.js.map +1 -0
  12. package/dist/chunk-Z532A7QC.js +78 -0
  13. package/dist/chunk-Z532A7QC.js.map +1 -0
  14. package/dist/file-stream.d.ts +43 -0
  15. package/dist/file-stream.js +9 -0
  16. package/dist/file-stream.js.map +1 -0
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.js +66 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/interpreter.d.ts +33 -0
  21. package/dist/interpreter.js +8 -0
  22. package/dist/interpreter.js.map +1 -0
  23. package/dist/request-handler.d.ts +18 -0
  24. package/dist/request-handler.js +12 -0
  25. package/dist/request-handler.js.map +1 -0
  26. package/dist/sandbox-CZTMzV2R.d.ts +587 -0
  27. package/dist/sandbox.d.ts +4 -0
  28. package/dist/sandbox.js +12 -0
  29. package/dist/sandbox.js.map +1 -0
  30. package/dist/security.d.ts +31 -0
  31. package/dist/security.js +13 -0
  32. package/dist/security.js.map +1 -0
  33. package/dist/sse-parser.d.ts +28 -0
  34. package/dist/sse-parser.js +11 -0
  35. package/dist/sse-parser.js.map +1 -0
  36. package/package.json +13 -5
  37. package/src/clients/base-client.ts +280 -0
  38. package/src/clients/command-client.ts +115 -0
  39. package/src/clients/file-client.ts +269 -0
  40. package/src/clients/git-client.ts +92 -0
  41. package/src/clients/index.ts +63 -0
  42. package/src/{jupyter-client.ts → clients/interpreter-client.ts} +148 -168
  43. package/src/clients/port-client.ts +105 -0
  44. package/src/clients/process-client.ts +177 -0
  45. package/src/clients/sandbox-client.ts +41 -0
  46. package/src/clients/types.ts +84 -0
  47. package/src/clients/utility-client.ts +94 -0
  48. package/src/errors/adapter.ts +180 -0
  49. package/src/errors/classes.ts +469 -0
  50. package/src/errors/index.ts +105 -0
  51. package/src/file-stream.ts +164 -0
  52. package/src/index.ts +87 -50
  53. package/src/interpreter.ts +22 -13
  54. package/src/request-handler.ts +69 -43
  55. package/src/sandbox.ts +594 -446
  56. package/src/security.ts +14 -23
  57. package/src/sse-parser.ts +4 -8
  58. package/startup.sh +3 -0
  59. package/tests/base-client.test.ts +328 -0
  60. package/tests/command-client.test.ts +407 -0
  61. package/tests/file-client.test.ts +643 -0
  62. package/tests/file-stream.test.ts +306 -0
  63. package/tests/git-client.test.ts +328 -0
  64. package/tests/port-client.test.ts +301 -0
  65. package/tests/process-client.test.ts +658 -0
  66. package/tests/sandbox.test.ts +465 -0
  67. package/tests/sse-parser.test.ts +290 -0
  68. package/tests/utility-client.test.ts +266 -0
  69. package/tests/wrangler.jsonc +35 -0
  70. package/tsconfig.json +9 -1
  71. package/vitest.config.ts +31 -0
  72. package/container_src/bun.lock +0 -122
  73. package/container_src/circuit-breaker.ts +0 -121
  74. package/container_src/handler/exec.ts +0 -340
  75. package/container_src/handler/file.ts +0 -1064
  76. package/container_src/handler/git.ts +0 -182
  77. package/container_src/handler/ports.ts +0 -314
  78. package/container_src/handler/process.ts +0 -640
  79. package/container_src/index.ts +0 -663
  80. package/container_src/jupyter-server.ts +0 -579
  81. package/container_src/jupyter-service.ts +0 -461
  82. package/container_src/jupyter_config.py +0 -48
  83. package/container_src/mime-processor.ts +0 -255
  84. package/container_src/package.json +0 -18
  85. package/container_src/startup.sh +0 -84
  86. package/container_src/types.ts +0 -117
  87. package/src/client.ts +0 -1024
  88. package/src/errors.ts +0 -218
  89. package/src/interpreter-types.ts +0 -383
  90. package/src/types.ts +0 -511
package/src/sandbox.ts CHANGED
@@ -1,77 +1,100 @@
1
+ import type { DurableObject } from 'cloudflare:workers';
1
2
  import { Container, getContainer } from "@cloudflare/containers";
2
- import { CodeInterpreter } from "./interpreter";
3
3
  import type {
4
4
  CodeContext,
5
5
  CreateContextOptions,
6
+ ExecEvent,
7
+ ExecOptions,
8
+ ExecResult,
6
9
  ExecutionResult,
10
+ ExecutionSession,
11
+ ISandbox,
12
+ Process,
13
+ ProcessOptions,
14
+ ProcessStatus,
7
15
  RunCodeOptions,
8
- } from "./interpreter-types";
9
- import { JupyterClient } from "./jupyter-client";
16
+ SessionOptions,
17
+ StreamOptions
18
+ } from "@repo/shared";
19
+ import { createLogger, runWithLogger, TraceContext } from "@repo/shared";
20
+ import { type ExecuteResponse, SandboxClient } from "./clients";
21
+ import type { ErrorResponse } from './errors';
22
+ import { CustomDomainRequiredError, ErrorCode } from './errors';
23
+ import { CodeInterpreter } from "./interpreter";
10
24
  import { isLocalhostPattern } from "./request-handler";
11
25
  import {
12
- logSecurityEvent,
13
26
  SecurityError,
14
27
  sanitizeSandboxId,
15
- validatePort,
28
+ validatePort
16
29
  } from "./security";
17
30
  import { parseSSEStream } from "./sse-parser";
18
- import type {
19
- ExecEvent,
20
- ExecOptions,
21
- ExecResult,
22
- ExecuteResponse,
23
- ISandbox,
24
- Process,
25
- ProcessOptions,
26
- ProcessStatus,
27
- StreamOptions,
28
- } from "./types";
29
- import { ProcessNotFoundError, SandboxError } from "./types";
30
31
 
31
- export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
32
+ export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string, options?: {
33
+ baseUrl: string
34
+ }) {
32
35
  const stub = getContainer(ns, id);
33
36
 
34
37
  // Store the name on first access
35
38
  stub.setSandboxName?.(id);
36
39
 
40
+ if(options?.baseUrl) {
41
+ stub.setBaseUrl(options.baseUrl);
42
+ }
43
+
37
44
  return stub;
38
45
  }
39
46
 
40
47
  export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
41
48
  defaultPort = 3000; // Default port for the container's Bun server
42
- sleepAfter = "20m"; // Keep container warm for 20 minutes to avoid cold starts
43
- client: JupyterClient;
44
- private sandboxName: string | null = null;
49
+ sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
50
+
51
+ client: SandboxClient;
45
52
  private codeInterpreter: CodeInterpreter;
53
+ private sandboxName: string | null = null;
54
+ private baseUrl: string | null = null;
55
+ private portTokens: Map<number, string> = new Map();
56
+ private defaultSession: string | null = null;
57
+ envVars: Record<string, string> = {};
58
+ private logger: ReturnType<typeof createLogger>;
46
59
 
47
- constructor(ctx: DurableObjectState, env: Env) {
60
+ constructor(ctx: DurableObject['ctx'], env: Env) {
48
61
  super(ctx, env);
49
- this.client = new JupyterClient({
50
- onCommandComplete: (success, exitCode, _stdout, _stderr, command) => {
51
- console.log(
52
- `[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
53
- );
54
- },
55
- onCommandStart: (command) => {
56
- console.log(`[Container] Command started: ${command}`);
57
- },
58
- onError: (error, _command) => {
59
- console.error(`[Container] Command error: ${error}`);
60
- },
61
- onOutput: (stream, data, _command) => {
62
- console.log(`[Container] [${stream}] ${data}`);
63
- },
62
+
63
+ const envObj = env as any;
64
+ // Set sandbox environment variables from env object
65
+ const sandboxEnvKeys = ['SANDBOX_LOG_LEVEL', 'SANDBOX_LOG_FORMAT'] as const;
66
+ sandboxEnvKeys.forEach(key => {
67
+ if (envObj?.[key]) {
68
+ this.envVars[key] = envObj[key];
69
+ }
70
+ });
71
+
72
+ this.logger = createLogger({
73
+ component: 'sandbox-do',
74
+ sandboxId: this.ctx.id.toString()
75
+ });
76
+
77
+ this.client = new SandboxClient({
78
+ logger: this.logger,
64
79
  port: 3000, // Control plane port
65
80
  stub: this,
66
81
  });
67
82
 
68
- // Initialize code interpreter
83
+ // Initialize code interpreter - pass 'this' after client is ready
84
+ // The CodeInterpreter extracts client.interpreter from the sandbox
69
85
  this.codeInterpreter = new CodeInterpreter(this);
70
86
 
71
- // Load the sandbox name from storage on initialization
87
+ // Load the sandbox name, port tokens, and default session from storage on initialization
72
88
  this.ctx.blockConcurrencyWhile(async () => {
73
- this.sandboxName =
74
- (await this.ctx.storage.get<string>("sandboxName")) || null;
89
+ this.sandboxName = await this.ctx.storage.get<string>('sandboxName') || null;
90
+ this.defaultSession = await this.ctx.storage.get<string>('defaultSession') || null;
91
+ const storedTokens = await this.ctx.storage.get<Record<string, string>>('portTokens') || {};
92
+
93
+ // Convert stored tokens back to Map
94
+ this.portTokens = new Map();
95
+ for (const [portStr, token] of Object.entries(storedTokens)) {
96
+ this.portTokens.set(parseInt(portStr, 10), token);
97
+ }
75
98
  });
76
99
  }
77
100
 
@@ -79,56 +102,94 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
79
102
  async setSandboxName(name: string): Promise<void> {
80
103
  if (!this.sandboxName) {
81
104
  this.sandboxName = name;
82
- await this.ctx.storage.put("sandboxName", name);
83
- console.log(`[Sandbox] Stored sandbox name via RPC: ${name}`);
105
+ await this.ctx.storage.put('sandboxName', name);
106
+ }
107
+ }
108
+
109
+ // RPC method to set the base URL
110
+ async setBaseUrl(baseUrl: string): Promise<void> {
111
+ if (!this.baseUrl) {
112
+ this.baseUrl = baseUrl;
113
+ await this.ctx.storage.put('baseUrl', baseUrl);
114
+ } else {
115
+ if(this.baseUrl !== baseUrl) {
116
+ throw new Error('Base URL already set and different from one previously provided');
117
+ }
84
118
  }
85
119
  }
86
120
 
87
121
  // RPC method to set environment variables
88
122
  async setEnvVars(envVars: Record<string, string>): Promise<void> {
123
+ // Update local state for new sessions
89
124
  this.envVars = { ...this.envVars, ...envVars };
90
- console.log(`[Sandbox] Updated environment variables`);
125
+
126
+ // If default session already exists, update it directly
127
+ if (this.defaultSession) {
128
+ // Set environment variables by executing export commands in the existing session
129
+ for (const [key, value] of Object.entries(envVars)) {
130
+ const escapedValue = value.replace(/'/g, "'\\''");
131
+ const exportCommand = `export ${key}='${escapedValue}'`;
132
+
133
+ const result = await this.client.commands.execute(exportCommand, this.defaultSession);
134
+
135
+ if (result.exitCode !== 0) {
136
+ throw new Error(`Failed to set ${key}: ${result.stderr || 'Unknown error'}`);
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Cleanup and destroy the sandbox container
144
+ */
145
+ override async destroy(): Promise<void> {
146
+ this.logger.info('Destroying sandbox container');
147
+ await super.destroy();
91
148
  }
92
149
 
93
150
  override onStart() {
94
- console.log("Sandbox successfully started");
151
+ this.logger.debug('Sandbox started');
95
152
  }
96
153
 
97
154
  override onStop() {
98
- console.log("Sandbox successfully shut down");
99
- if (this.client) {
100
- this.client.clearSession();
101
- }
155
+ this.logger.debug('Sandbox stopped');
102
156
  }
103
157
 
104
158
  override onError(error: unknown) {
105
- console.log("Sandbox error:", error);
159
+ this.logger.error('Sandbox error', error instanceof Error ? error : new Error(String(error)));
106
160
  }
107
161
 
108
162
  // Override fetch to route internal container requests to appropriate ports
109
163
  override async fetch(request: Request): Promise<Response> {
110
- const url = new URL(request.url);
164
+ // Extract or generate trace ID from request
165
+ const traceId = TraceContext.fromHeaders(request.headers) || TraceContext.generate();
111
166
 
112
- // Capture and store the sandbox name from the header if present
113
- if (!this.sandboxName && request.headers.has("X-Sandbox-Name")) {
114
- const name = request.headers.get("X-Sandbox-Name")!;
115
- this.sandboxName = name;
116
- await this.ctx.storage.put("sandboxName", name);
117
- console.log(`[Sandbox] Stored sandbox name: ${this.sandboxName}`);
118
- }
167
+ // Create request-specific logger with trace ID
168
+ const requestLogger = this.logger.child({ traceId, operation: 'fetch' });
169
+
170
+ return await runWithLogger(requestLogger, async () => {
171
+ const url = new URL(request.url);
172
+
173
+ // Capture and store the sandbox name from the header if present
174
+ if (!this.sandboxName && request.headers.has('X-Sandbox-Name')) {
175
+ const name = request.headers.get('X-Sandbox-Name')!;
176
+ this.sandboxName = name;
177
+ await this.ctx.storage.put('sandboxName', name);
178
+ }
119
179
 
120
- // Determine which port to route to
121
- const port = this.determinePort(url);
180
+ // Determine which port to route to
181
+ const port = this.determinePort(url);
122
182
 
123
- // Route to the appropriate port
124
- return await this.containerFetch(request, port);
183
+ // Route to the appropriate port
184
+ return await this.containerFetch(request, port);
185
+ });
125
186
  }
126
187
 
127
188
  private determinePort(url: URL): number {
128
189
  // Extract port from proxy requests (e.g., /proxy/8080/*)
129
190
  const proxyMatch = url.pathname.match(/^\/proxy\/(\d+)/);
130
191
  if (proxyMatch) {
131
- return parseInt(proxyMatch[1]);
192
+ return parseInt(proxyMatch[1], 10);
132
193
  }
133
194
 
134
195
  // All other requests go to control plane on port 3000
@@ -136,9 +197,62 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
136
197
  return 3000;
137
198
  }
138
199
 
200
+ /**
201
+ * Ensure default session exists - lazy initialization
202
+ * This is called automatically by all public methods that need a session
203
+ *
204
+ * The session is persisted to Durable Object storage to survive hot reloads
205
+ * during development. If a session already exists in the container after reload,
206
+ * we reuse it instead of trying to create a new one.
207
+ */
208
+ private async ensureDefaultSession(): Promise<string> {
209
+ if (!this.defaultSession) {
210
+ const sessionId = `sandbox-${this.sandboxName || 'default'}`;
211
+
212
+ try {
213
+ // Try to create session in container
214
+ await this.client.utils.createSession({
215
+ id: sessionId,
216
+ env: this.envVars || {},
217
+ cwd: '/workspace',
218
+ });
219
+
220
+ this.defaultSession = sessionId;
221
+ // Persist to storage so it survives hot reloads
222
+ await this.ctx.storage.put('defaultSession', sessionId);
223
+ this.logger.debug('Default session initialized', { sessionId });
224
+ } catch (error: any) {
225
+ // If session already exists (e.g., after hot reload), reuse it
226
+ if (error?.message?.includes('already exists') || error?.message?.includes('Session')) {
227
+ this.logger.debug('Reusing existing session after reload', { sessionId });
228
+ this.defaultSession = sessionId;
229
+ // Persist to storage in case it wasn't saved before
230
+ await this.ctx.storage.put('defaultSession', sessionId);
231
+ } else {
232
+ // Re-throw other errors
233
+ throw error;
234
+ }
235
+ }
236
+ }
237
+ return this.defaultSession;
238
+ }
239
+
139
240
  // Enhanced exec method - always returns ExecResult with optional streaming
140
241
  // This replaces the old exec method to match ISandbox interface
141
242
  async exec(command: string, options?: ExecOptions): Promise<ExecResult> {
243
+ const session = await this.ensureDefaultSession();
244
+ return this.execWithSession(command, session, options);
245
+ }
246
+
247
+ /**
248
+ * Internal session-aware exec implementation
249
+ * Used by both public exec() and session wrappers
250
+ */
251
+ private async execWithSession(
252
+ command: string,
253
+ sessionId: string,
254
+ options?: ExecOptions
255
+ ): Promise<ExecResult> {
142
256
  const startTime = Date.now();
143
257
  const timestamp = new Date().toISOString();
144
258
 
@@ -148,33 +262,20 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
148
262
  try {
149
263
  // Handle cancellation
150
264
  if (options?.signal?.aborted) {
151
- throw new Error("Operation was aborted");
265
+ throw new Error('Operation was aborted');
152
266
  }
153
267
 
154
268
  let result: ExecResult;
155
269
 
156
270
  if (options?.stream && options?.onOutput) {
157
271
  // Streaming with callbacks - we need to collect the final result
158
- result = await this.executeWithStreaming(
159
- command,
160
- options,
161
- startTime,
162
- timestamp
163
- );
272
+ result = await this.executeWithStreaming(command, sessionId, options, startTime, timestamp);
164
273
  } else {
165
- // Regular execution
166
- const response = await this.client.execute(command, {
167
- sessionId: options?.sessionId,
168
- cwd: options?.cwd,
169
- env: options?.env,
170
- });
274
+ // Regular execution with session
275
+ const response = await this.client.commands.execute(command, sessionId);
171
276
 
172
277
  const duration = Date.now() - startTime;
173
- result = this.mapExecuteResponseToExecResult(
174
- response,
175
- duration,
176
- options?.sessionId
177
- );
278
+ result = this.mapExecuteResponseToExecResult(response, duration, sessionId);
178
279
  }
179
280
 
180
281
  // Call completion callback if provided
@@ -197,32 +298,30 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
197
298
 
198
299
  private async executeWithStreaming(
199
300
  command: string,
301
+ sessionId: string,
200
302
  options: ExecOptions,
201
303
  startTime: number,
202
304
  timestamp: string
203
305
  ): Promise<ExecResult> {
204
- let stdout = "";
205
- let stderr = "";
306
+ let stdout = '';
307
+ let stderr = '';
206
308
 
207
309
  try {
208
- const stream = await this.client.executeCommandStream(
209
- command,
210
- options.sessionId
211
- );
310
+ const stream = await this.client.commands.executeStream(command, sessionId);
212
311
 
213
312
  for await (const event of parseSSEStream<ExecEvent>(stream)) {
214
313
  // Check for cancellation
215
314
  if (options.signal?.aborted) {
216
- throw new Error("Operation was aborted");
315
+ throw new Error('Operation was aborted');
217
316
  }
218
317
 
219
318
  switch (event.type) {
220
- case "stdout":
221
- case "stderr":
319
+ case 'stdout':
320
+ case 'stderr':
222
321
  if (event.data) {
223
322
  // Update accumulated output
224
- if (event.type === "stdout") stdout += event.data;
225
- if (event.type === "stderr") stderr += event.data;
323
+ if (event.type === 'stdout') stdout += event.data;
324
+ if (event.type === 'stderr') stderr += event.data;
226
325
 
227
326
  // Call user's callback
228
327
  if (options.onOutput) {
@@ -231,33 +330,32 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
231
330
  }
232
331
  break;
233
332
 
234
- case "complete": {
333
+ case 'complete': {
235
334
  // Use result from complete event if available
236
335
  const duration = Date.now() - startTime;
237
- return (
238
- event.result || {
239
- success: event.exitCode === 0,
240
- exitCode: event.exitCode || 0,
241
- stdout,
242
- stderr,
243
- command,
244
- duration,
245
- timestamp,
246
- sessionId: options.sessionId,
247
- }
248
- );
336
+ return {
337
+ success: (event.exitCode ?? 0) === 0,
338
+ exitCode: event.exitCode ?? 0,
339
+ stdout,
340
+ stderr,
341
+ command,
342
+ duration,
343
+ timestamp,
344
+ sessionId
345
+ };
249
346
  }
250
347
 
251
- case "error":
252
- throw new Error(event.error || "Command execution failed");
348
+ case 'error':
349
+ throw new Error(event.data || 'Command execution failed');
253
350
  }
254
351
  }
255
352
 
256
353
  // If we get here without a complete event, something went wrong
257
- throw new Error("Stream ended without completion event");
354
+ throw new Error('Stream ended without completion event');
355
+
258
356
  } catch (error) {
259
357
  if (options.signal?.aborted) {
260
- throw new Error("Operation was aborted");
358
+ throw new Error('Operation was aborted');
261
359
  }
262
360
  throw error;
263
361
  }
@@ -276,63 +374,72 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
276
374
  command: response.command,
277
375
  duration,
278
376
  timestamp: response.timestamp,
377
+ sessionId
378
+ };
379
+ }
380
+
381
+ /**
382
+ * Create a Process domain object from HTTP client DTO
383
+ * Centralizes process object creation with bound methods
384
+ * This eliminates duplication across startProcess, listProcesses, getProcess, and session wrappers
385
+ */
386
+ private createProcessFromDTO(
387
+ data: {
388
+ id: string;
389
+ pid?: number;
390
+ command: string;
391
+ status: ProcessStatus;
392
+ startTime: string | Date;
393
+ endTime?: string | Date;
394
+ exitCode?: number;
395
+ },
396
+ sessionId: string
397
+ ): Process {
398
+ return {
399
+ id: data.id,
400
+ pid: data.pid,
401
+ command: data.command,
402
+ status: data.status,
403
+ startTime: typeof data.startTime === 'string' ? new Date(data.startTime) : data.startTime,
404
+ endTime: data.endTime ? (typeof data.endTime === 'string' ? new Date(data.endTime) : data.endTime) : undefined,
405
+ exitCode: data.exitCode,
279
406
  sessionId,
407
+
408
+ kill: async (signal?: string) => {
409
+ await this.killProcess(data.id, signal);
410
+ },
411
+
412
+ getStatus: async () => {
413
+ const current = await this.getProcess(data.id);
414
+ return current?.status || 'error';
415
+ },
416
+
417
+ getLogs: async () => {
418
+ const logs = await this.getProcessLogs(data.id);
419
+ return { stdout: logs.stdout, stderr: logs.stderr };
420
+ }
280
421
  };
281
422
  }
282
423
 
424
+
283
425
  // Background process management
284
- async startProcess(
285
- command: string,
286
- options?: ProcessOptions
287
- ): Promise<Process> {
426
+ async startProcess(command: string, options?: ProcessOptions, sessionId?: string): Promise<Process> {
288
427
  // Use the new HttpClient method to start the process
289
428
  try {
290
- const response = await this.client.startProcess(command, {
291
- processId: options?.processId,
292
- sessionId: options?.sessionId,
293
- timeout: options?.timeout,
294
- env: options?.env,
295
- cwd: options?.cwd,
296
- encoding: options?.encoding,
297
- autoCleanup: options?.autoCleanup,
429
+ const session = sessionId ?? await this.ensureDefaultSession();
430
+ const response = await this.client.processes.startProcess(command, session, {
431
+ processId: options?.processId
298
432
  });
299
433
 
300
- const process = response.process;
301
- const processObj: Process = {
302
- id: process.id,
303
- pid: process.pid,
304
- command: process.command,
305
- status: process.status as ProcessStatus,
306
- startTime: new Date(process.startTime),
434
+ const processObj = this.createProcessFromDTO({
435
+ id: response.processId,
436
+ pid: response.pid,
437
+ command: response.command,
438
+ status: 'running' as ProcessStatus,
439
+ startTime: new Date(),
307
440
  endTime: undefined,
308
- exitCode: undefined,
309
- sessionId: process.sessionId,
310
-
311
- async kill(): Promise<void> {
312
- throw new Error("Method will be replaced");
313
- },
314
- async getStatus(): Promise<ProcessStatus> {
315
- throw new Error("Method will be replaced");
316
- },
317
- async getLogs(): Promise<{ stdout: string; stderr: string }> {
318
- throw new Error("Method will be replaced");
319
- },
320
- };
321
-
322
- // Bind context properly
323
- processObj.kill = async (signal?: string) => {
324
- await this.killProcess(process.id, signal);
325
- };
326
-
327
- processObj.getStatus = async () => {
328
- const current = await this.getProcess(process.id);
329
- return current?.status || "error";
330
- };
331
-
332
- processObj.getLogs = async () => {
333
- const logs = await this.getProcessLogs(process.id);
334
- return { stdout: logs.stdout, stderr: logs.stderr };
335
- };
441
+ exitCode: undefined
442
+ }, session);
336
443
 
337
444
  // Call onStart callback if provided
338
445
  if (options?.onStart) {
@@ -340,6 +447,7 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
340
447
  }
341
448
 
342
449
  return processObj;
450
+
343
451
  } catch (error) {
344
452
  if (options?.onError && error instanceof Error) {
345
453
  options.onError(error);
@@ -349,216 +457,212 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
349
457
  }
350
458
  }
351
459
 
352
- async listProcesses(): Promise<Process[]> {
353
- const response = await this.client.listProcesses();
354
-
355
- return response.processes.map((processData) => ({
356
- id: processData.id,
357
- pid: processData.pid,
358
- command: processData.command,
359
- status: processData.status,
360
- startTime: new Date(processData.startTime),
361
- endTime: processData.endTime ? new Date(processData.endTime) : undefined,
362
- exitCode: processData.exitCode,
363
- sessionId: processData.sessionId,
364
-
365
- kill: async (signal?: string) => {
366
- await this.killProcess(processData.id, signal);
367
- },
368
-
369
- getStatus: async () => {
370
- const current = await this.getProcess(processData.id);
371
- return current?.status || "error";
372
- },
373
-
374
- getLogs: async () => {
375
- const logs = await this.getProcessLogs(processData.id);
376
- return { stdout: logs.stdout, stderr: logs.stderr };
377
- },
378
- }));
460
+ async listProcesses(sessionId?: string): Promise<Process[]> {
461
+ const session = sessionId ?? await this.ensureDefaultSession();
462
+ const response = await this.client.processes.listProcesses();
463
+
464
+ return response.processes.map(processData =>
465
+ this.createProcessFromDTO({
466
+ id: processData.id,
467
+ pid: processData.pid,
468
+ command: processData.command,
469
+ status: processData.status,
470
+ startTime: processData.startTime,
471
+ endTime: processData.endTime,
472
+ exitCode: processData.exitCode
473
+ }, session)
474
+ );
379
475
  }
380
476
 
381
- async getProcess(id: string): Promise<Process | null> {
382
- const response = await this.client.getProcess(id);
477
+ async getProcess(id: string, sessionId?: string): Promise<Process | null> {
478
+ const session = sessionId ?? await this.ensureDefaultSession();
479
+ const response = await this.client.processes.getProcess(id);
383
480
  if (!response.process) {
384
481
  return null;
385
482
  }
386
483
 
387
484
  const processData = response.process;
388
- return {
485
+ return this.createProcessFromDTO({
389
486
  id: processData.id,
390
487
  pid: processData.pid,
391
488
  command: processData.command,
392
489
  status: processData.status,
393
- startTime: new Date(processData.startTime),
394
- endTime: processData.endTime ? new Date(processData.endTime) : undefined,
395
- exitCode: processData.exitCode,
396
- sessionId: processData.sessionId,
397
-
398
- kill: async (signal?: string) => {
399
- await this.killProcess(processData.id, signal);
400
- },
401
-
402
- getStatus: async () => {
403
- const current = await this.getProcess(processData.id);
404
- return current?.status || "error";
405
- },
406
-
407
- getLogs: async () => {
408
- const logs = await this.getProcessLogs(processData.id);
409
- return { stdout: logs.stdout, stderr: logs.stderr };
410
- },
411
- };
490
+ startTime: processData.startTime,
491
+ endTime: processData.endTime,
492
+ exitCode: processData.exitCode
493
+ }, session);
412
494
  }
413
495
 
414
- async killProcess(id: string, _signal?: string): Promise<void> {
415
- try {
416
- // Note: signal parameter is not currently supported by the HttpClient implementation
417
- await this.client.killProcess(id);
418
- } catch (error) {
419
- if (
420
- error instanceof Error &&
421
- error.message.includes("Process not found")
422
- ) {
423
- throw new ProcessNotFoundError(id);
424
- }
425
- throw new SandboxError(
426
- `Failed to kill process ${id}: ${
427
- error instanceof Error ? error.message : "Unknown error"
428
- }`,
429
- "KILL_PROCESS_FAILED"
430
- );
431
- }
496
+ async killProcess(id: string, signal?: string, sessionId?: string): Promise<void> {
497
+ // Note: signal parameter is not currently supported by the HttpClient implementation
498
+ // The HTTP client already throws properly typed errors, so we just let them propagate
499
+ await this.client.processes.killProcess(id);
432
500
  }
433
501
 
434
- async killAllProcesses(): Promise<number> {
435
- const response = await this.client.killAllProcesses();
436
- return response.killedCount;
502
+ async killAllProcesses(sessionId?: string): Promise<number> {
503
+ const response = await this.client.processes.killAllProcesses();
504
+ return response.cleanedCount;
437
505
  }
438
506
 
439
- async cleanupCompletedProcesses(): Promise<number> {
507
+ async cleanupCompletedProcesses(sessionId?: string): Promise<number> {
440
508
  // For now, this would need to be implemented as a container endpoint
441
509
  // as we no longer maintain local process storage
442
510
  // We'll return 0 as a placeholder until the container endpoint is added
443
511
  return 0;
444
512
  }
445
513
 
446
- async getProcessLogs(
447
- id: string
448
- ): Promise<{ stdout: string; stderr: string }> {
449
- try {
450
- const response = await this.client.getProcessLogs(id);
451
- return {
452
- stdout: response.stdout,
453
- stderr: response.stderr,
454
- };
455
- } catch (error) {
456
- if (
457
- error instanceof Error &&
458
- error.message.includes("Process not found")
459
- ) {
460
- throw new ProcessNotFoundError(id);
461
- }
462
- throw error;
463
- }
514
+ async getProcessLogs(id: string, sessionId?: string): Promise<{ stdout: string; stderr: string; processId: string }> {
515
+ // The HTTP client already throws properly typed errors, so we just let them propagate
516
+ const response = await this.client.processes.getProcessLogs(id);
517
+ return {
518
+ stdout: response.stdout,
519
+ stderr: response.stderr,
520
+ processId: response.processId
521
+ };
464
522
  }
465
523
 
524
+
466
525
  // Streaming methods - return ReadableStream for RPC compatibility
467
- async execStream(
468
- command: string,
469
- options?: StreamOptions
470
- ): Promise<ReadableStream<Uint8Array>> {
526
+ async execStream(command: string, options?: StreamOptions): Promise<ReadableStream<Uint8Array>> {
471
527
  // Check for cancellation
472
528
  if (options?.signal?.aborted) {
473
- throw new Error("Operation was aborted");
529
+ throw new Error('Operation was aborted');
474
530
  }
475
531
 
476
- // Get the stream from HttpClient (need to add this method)
477
- const stream = await this.client.executeCommandStream(
478
- command,
479
- options?.sessionId
480
- );
481
-
482
- // Return the ReadableStream directly - can be converted to AsyncIterable by consumers
483
- return stream;
532
+ const session = await this.ensureDefaultSession();
533
+ // Get the stream from CommandClient
534
+ return this.client.commands.executeStream(command, session);
484
535
  }
485
536
 
486
- async streamProcessLogs(
487
- processId: string,
488
- options?: { signal?: AbortSignal }
489
- ): Promise<ReadableStream<Uint8Array>> {
537
+ /**
538
+ * Internal session-aware execStream implementation
539
+ */
540
+ private async execStreamWithSession(command: string, sessionId: string, options?: StreamOptions): Promise<ReadableStream<Uint8Array>> {
490
541
  // Check for cancellation
491
542
  if (options?.signal?.aborted) {
492
- throw new Error("Operation was aborted");
543
+ throw new Error('Operation was aborted');
493
544
  }
494
545
 
495
- // Get the stream from HttpClient
496
- const stream = await this.client.streamProcessLogs(processId);
546
+ return this.client.commands.executeStream(command, sessionId);
547
+ }
497
548
 
498
- // Return the ReadableStream directly - can be converted to AsyncIterable by consumers
499
- return stream;
549
+ async streamProcessLogs(processId: string, options?: { signal?: AbortSignal }): Promise<ReadableStream<Uint8Array>> {
550
+ // Check for cancellation
551
+ if (options?.signal?.aborted) {
552
+ throw new Error('Operation was aborted');
553
+ }
554
+
555
+ return this.client.processes.streamProcessLogs(processId);
500
556
  }
501
557
 
502
558
  async gitCheckout(
503
559
  repoUrl: string,
504
- options: { branch?: string; targetDir?: string }
560
+ options: { branch?: string; targetDir?: string; sessionId?: string }
505
561
  ) {
506
- return this.client.gitCheckout(repoUrl, options.branch, options.targetDir);
562
+ const session = options.sessionId ?? await this.ensureDefaultSession();
563
+ return this.client.git.checkout(repoUrl, session, {
564
+ branch: options.branch,
565
+ targetDir: options.targetDir
566
+ });
507
567
  }
508
568
 
509
- async mkdir(path: string, options: { recursive?: boolean } = {}) {
510
- return this.client.mkdir(path, options.recursive);
569
+ async mkdir(
570
+ path: string,
571
+ options: { recursive?: boolean; sessionId?: string } = {}
572
+ ) {
573
+ const session = options.sessionId ?? await this.ensureDefaultSession();
574
+ return this.client.files.mkdir(path, session, { recursive: options.recursive });
511
575
  }
512
576
 
513
577
  async writeFile(
514
578
  path: string,
515
579
  content: string,
516
- options: { encoding?: string } = {}
580
+ options: { encoding?: string; sessionId?: string } = {}
517
581
  ) {
518
- return this.client.writeFile(path, content, options.encoding);
582
+ const session = options.sessionId ?? await this.ensureDefaultSession();
583
+ return this.client.files.writeFile(path, content, session, { encoding: options.encoding });
519
584
  }
520
585
 
521
- async deleteFile(path: string) {
522
- return this.client.deleteFile(path);
586
+ async deleteFile(path: string, sessionId?: string) {
587
+ const session = sessionId ?? await this.ensureDefaultSession();
588
+ return this.client.files.deleteFile(path, session);
523
589
  }
524
590
 
525
- async renameFile(oldPath: string, newPath: string) {
526
- return this.client.renameFile(oldPath, newPath);
591
+ async renameFile(
592
+ oldPath: string,
593
+ newPath: string,
594
+ sessionId?: string
595
+ ) {
596
+ const session = sessionId ?? await this.ensureDefaultSession();
597
+ return this.client.files.renameFile(oldPath, newPath, session);
527
598
  }
528
599
 
529
- async moveFile(sourcePath: string, destinationPath: string) {
530
- return this.client.moveFile(sourcePath, destinationPath);
600
+ async moveFile(
601
+ sourcePath: string,
602
+ destinationPath: string,
603
+ sessionId?: string
604
+ ) {
605
+ const session = sessionId ?? await this.ensureDefaultSession();
606
+ return this.client.files.moveFile(sourcePath, destinationPath, session);
607
+ }
608
+
609
+ async readFile(
610
+ path: string,
611
+ options: { encoding?: string; sessionId?: string } = {}
612
+ ) {
613
+ const session = options.sessionId ?? await this.ensureDefaultSession();
614
+ return this.client.files.readFile(path, session, { encoding: options.encoding });
531
615
  }
532
616
 
533
- async readFile(path: string, options: { encoding?: string } = {}) {
534
- return this.client.readFile(path, options.encoding);
617
+ /**
618
+ * Stream a file from the sandbox using Server-Sent Events
619
+ * Returns a ReadableStream that can be consumed with streamFile() or collectFile() utilities
620
+ * @param path - Path to the file to stream
621
+ * @param options - Optional session ID
622
+ */
623
+ async readFileStream(
624
+ path: string,
625
+ options: { sessionId?: string } = {}
626
+ ): Promise<ReadableStream<Uint8Array>> {
627
+ const session = options.sessionId ?? await this.ensureDefaultSession();
628
+ return this.client.files.readFileStream(path, session);
535
629
  }
536
630
 
537
631
  async listFiles(
538
632
  path: string,
539
- options: {
540
- recursive?: boolean;
541
- includeHidden?: boolean;
542
- } = {}
633
+ options?: { recursive?: boolean; includeHidden?: boolean }
543
634
  ) {
544
- return this.client.listFiles(path, options);
635
+ const session = await this.ensureDefaultSession();
636
+ return this.client.files.listFiles(path, session, options);
545
637
  }
546
638
 
547
639
  async exposePort(port: number, options: { name?: string; hostname: string }) {
548
- await this.client.exposePort(port, options?.name);
640
+ // Check if hostname is workers.dev domain (doesn't support wildcard subdomains)
641
+ if (options.hostname.endsWith('.workers.dev')) {
642
+ const errorResponse: ErrorResponse = {
643
+ code: ErrorCode.CUSTOM_DOMAIN_REQUIRED,
644
+ message: `Port exposure requires a custom domain. .workers.dev domains do not support wildcard subdomains required for port proxying.`,
645
+ context: { originalError: options.hostname },
646
+ httpStatus: 400,
647
+ timestamp: new Date().toISOString()
648
+ };
649
+ throw new CustomDomainRequiredError(errorResponse);
650
+ }
651
+
652
+ const sessionId = await this.ensureDefaultSession();
653
+ await this.client.ports.exposePort(port, sessionId, options?.name);
549
654
 
550
655
  // We need the sandbox name to construct preview URLs
551
656
  if (!this.sandboxName) {
552
- throw new Error(
553
- "Sandbox name not available. Ensure sandbox is accessed through getSandbox()"
554
- );
657
+ throw new Error('Sandbox name not available. Ensure sandbox is accessed through getSandbox()');
555
658
  }
556
659
 
557
- const url = this.constructPreviewUrl(
558
- port,
559
- this.sandboxName,
560
- options.hostname
561
- );
660
+ // Generate and store token for this port
661
+ const token = this.generatePortToken();
662
+ this.portTokens.set(port, token);
663
+ await this.persistPortTokens();
664
+
665
+ const url = this.constructPreviewUrl(port, this.sandboxName, options.hostname, token);
562
666
 
563
667
  return {
564
668
  url,
@@ -569,129 +673,119 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
569
673
 
570
674
  async unexposePort(port: number) {
571
675
  if (!validatePort(port)) {
572
- logSecurityEvent(
573
- "INVALID_PORT_UNEXPOSE",
574
- {
575
- port,
576
- },
577
- "high"
578
- );
579
- throw new SecurityError(
580
- `Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`
581
- );
676
+ throw new SecurityError(`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`);
582
677
  }
583
678
 
584
- await this.client.unexposePort(port);
679
+ const sessionId = await this.ensureDefaultSession();
680
+ await this.client.ports.unexposePort(port, sessionId);
585
681
 
586
- logSecurityEvent(
587
- "PORT_UNEXPOSED",
588
- {
589
- port,
590
- },
591
- "low"
592
- );
682
+ // Clean up token for this port
683
+ if (this.portTokens.has(port)) {
684
+ this.portTokens.delete(port);
685
+ await this.persistPortTokens();
686
+ }
593
687
  }
594
688
 
595
689
  async getExposedPorts(hostname: string) {
596
- const response = await this.client.getExposedPorts();
690
+ const sessionId = await this.ensureDefaultSession();
691
+ const response = await this.client.ports.getExposedPorts(sessionId);
597
692
 
598
693
  // We need the sandbox name to construct preview URLs
599
694
  if (!this.sandboxName) {
600
- throw new Error(
601
- "Sandbox name not available. Ensure sandbox is accessed through getSandbox()"
602
- );
695
+ throw new Error('Sandbox name not available. Ensure sandbox is accessed through getSandbox()');
603
696
  }
604
697
 
605
- return response.ports.map((port) => ({
606
- url: this.constructPreviewUrl(port.port, this.sandboxName!, hostname),
607
- port: port.port,
608
- name: port.name,
609
- exposedAt: port.exposedAt,
610
- }));
698
+ return response.ports.map(port => {
699
+ // Get token for this port - must exist for all exposed ports
700
+ const token = this.portTokens.get(port.port);
701
+ if (!token) {
702
+ throw new Error(`Port ${port.port} is exposed but has no token. This should not happen.`);
703
+ }
704
+
705
+ return {
706
+ url: this.constructPreviewUrl(port.port, this.sandboxName!, hostname, token),
707
+ port: port.port,
708
+ status: port.status,
709
+ };
710
+ });
611
711
  }
612
712
 
613
- private constructPreviewUrl(
614
- port: number,
615
- sandboxId: string,
616
- hostname: string
617
- ): string {
618
- if (!validatePort(port)) {
619
- logSecurityEvent(
620
- "INVALID_PORT_REJECTED",
621
- {
622
- port,
623
- sandboxId,
624
- hostname,
625
- },
626
- "high"
627
- );
628
- throw new SecurityError(
629
- `Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`
630
- );
631
- }
632
713
 
633
- let sanitizedSandboxId: string;
714
+ async isPortExposed(port: number): Promise<boolean> {
634
715
  try {
635
- sanitizedSandboxId = sanitizeSandboxId(sandboxId);
716
+ const sessionId = await this.ensureDefaultSession();
717
+ const response = await this.client.ports.getExposedPorts(sessionId);
718
+ return response.ports.some(exposedPort => exposedPort.port === port);
636
719
  } catch (error) {
637
- logSecurityEvent(
638
- "INVALID_SANDBOX_ID_REJECTED",
639
- {
640
- sandboxId,
641
- port,
642
- hostname,
643
- error: error instanceof Error ? error.message : "Unknown error",
644
- },
645
- "high"
646
- );
647
- throw error;
720
+ this.logger.error('Error checking if port is exposed', error instanceof Error ? error : new Error(String(error)), { port });
721
+ return false;
722
+ }
723
+ }
724
+
725
+ async validatePortToken(port: number, token: string): Promise<boolean> {
726
+ // First check if port is exposed
727
+ const isExposed = await this.isPortExposed(port);
728
+ if (!isExposed) {
729
+ return false;
730
+ }
731
+
732
+ // Get stored token for this port - must exist for all exposed ports
733
+ const storedToken = this.portTokens.get(port);
734
+ if (!storedToken) {
735
+ // This should not happen - all exposed ports must have tokens
736
+ this.logger.error('Port is exposed but has no token - bug detected', undefined, { port });
737
+ return false;
738
+ }
739
+
740
+ // Constant-time comparison to prevent timing attacks
741
+ return storedToken === token;
742
+ }
743
+
744
+ private generatePortToken(): string {
745
+ // Generate cryptographically secure 16-character token using Web Crypto API
746
+ // Available in Cloudflare Workers runtime
747
+ const array = new Uint8Array(12); // 12 bytes = 16 base64url chars (after padding removal)
748
+ crypto.getRandomValues(array);
749
+
750
+ // Convert to base64url format (URL-safe, no padding, lowercase)
751
+ const base64 = btoa(String.fromCharCode(...array));
752
+ return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '').toLowerCase();
753
+ }
754
+
755
+ private async persistPortTokens(): Promise<void> {
756
+ // Convert Map to plain object for storage
757
+ const tokensObj: Record<string, string> = {};
758
+ for (const [port, token] of this.portTokens.entries()) {
759
+ tokensObj[port.toString()] = token;
760
+ }
761
+ await this.ctx.storage.put('portTokens', tokensObj);
762
+ }
763
+
764
+ private constructPreviewUrl(port: number, sandboxId: string, hostname: string, token: string): string {
765
+ if (!validatePort(port)) {
766
+ throw new SecurityError(`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`);
648
767
  }
649
768
 
769
+ // Validate sandbox ID (will throw SecurityError if invalid)
770
+ const sanitizedSandboxId = sanitizeSandboxId(sandboxId);
771
+
650
772
  const isLocalhost = isLocalhostPattern(hostname);
651
773
 
652
774
  if (isLocalhost) {
653
775
  // Unified subdomain approach for localhost (RFC 6761)
654
- const [host, portStr] = hostname.split(":");
655
- const mainPort = portStr || "80";
776
+ const [host, portStr] = hostname.split(':');
777
+ const mainPort = portStr || '80';
656
778
 
657
779
  // Use URL constructor for safe URL building
658
780
  try {
659
781
  const baseUrl = new URL(`http://${host}:${mainPort}`);
660
- // Construct subdomain safely
661
- const subdomainHost = `${port}-${sanitizedSandboxId}.${host}`;
782
+ // Construct subdomain safely with mandatory token
783
+ const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${host}`;
662
784
  baseUrl.hostname = subdomainHost;
663
785
 
664
- const finalUrl = baseUrl.toString();
665
-
666
- logSecurityEvent(
667
- "PREVIEW_URL_CONSTRUCTED",
668
- {
669
- port,
670
- sandboxId: sanitizedSandboxId,
671
- hostname,
672
- resultUrl: finalUrl,
673
- environment: "localhost",
674
- },
675
- "low"
676
- );
677
-
678
- return finalUrl;
786
+ return baseUrl.toString();
679
787
  } catch (error) {
680
- logSecurityEvent(
681
- "URL_CONSTRUCTION_FAILED",
682
- {
683
- port,
684
- sandboxId: sanitizedSandboxId,
685
- hostname,
686
- error: error instanceof Error ? error.message : "Unknown error",
687
- },
688
- "high"
689
- );
690
- throw new SecurityError(
691
- `Failed to construct preview URL: ${
692
- error instanceof Error ? error.message : "Unknown error"
693
- }`
694
- );
788
+ throw new SecurityError(`Failed to construct preview URL: ${error instanceof Error ? error.message : 'Unknown error'}`);
695
789
  }
696
790
  }
697
791
 
@@ -701,87 +795,141 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
701
795
  const protocol = "https";
702
796
  const baseUrl = new URL(`${protocol}://${hostname}`);
703
797
 
704
- // Construct subdomain safely
705
- const subdomainHost = `${port}-${sanitizedSandboxId}.${hostname}`;
798
+ // Construct subdomain safely with mandatory token
799
+ const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${hostname}`;
706
800
  baseUrl.hostname = subdomainHost;
707
801
 
708
- const finalUrl = baseUrl.toString();
709
-
710
- logSecurityEvent(
711
- "PREVIEW_URL_CONSTRUCTED",
712
- {
713
- port,
714
- sandboxId: sanitizedSandboxId,
715
- hostname,
716
- resultUrl: finalUrl,
717
- environment: "production",
718
- },
719
- "low"
720
- );
721
-
722
- return finalUrl;
802
+ return baseUrl.toString();
723
803
  } catch (error) {
724
- logSecurityEvent(
725
- "URL_CONSTRUCTION_FAILED",
726
- {
727
- port,
728
- sandboxId: sanitizedSandboxId,
729
- hostname,
730
- error: error instanceof Error ? error.message : "Unknown error",
731
- },
732
- "high"
733
- );
734
- throw new SecurityError(
735
- `Failed to construct preview URL: ${
736
- error instanceof Error ? error.message : "Unknown error"
737
- }`
738
- );
804
+ throw new SecurityError(`Failed to construct preview URL: ${error instanceof Error ? error.message : 'Unknown error'}`);
739
805
  }
740
806
  }
741
807
 
742
- // Code Interpreter Methods
808
+ // ============================================================================
809
+ // Session Management - Advanced Use Cases
810
+ // ============================================================================
743
811
 
744
812
  /**
745
- * Create a new code execution context
813
+ * Create isolated execution session for advanced use cases
814
+ * Returns ExecutionSession with full sandbox API bound to specific session
746
815
  */
747
- async createCodeContext(
748
- options?: CreateContextOptions
749
- ): Promise<CodeContext> {
750
- return this.codeInterpreter.createCodeContext(options);
816
+ async createSession(options?: SessionOptions): Promise<ExecutionSession> {
817
+ const sessionId = options?.id || `session-${Date.now()}`;
818
+
819
+ // Create session in container
820
+ await this.client.utils.createSession({
821
+ id: sessionId,
822
+ env: options?.env,
823
+ cwd: options?.cwd,
824
+ });
825
+
826
+ // Return wrapper that binds sessionId to all operations
827
+ return this.getSessionWrapper(sessionId);
751
828
  }
752
829
 
753
830
  /**
754
- * Run code with streaming callbacks
831
+ * Get an existing session by ID
832
+ * Returns ExecutionSession wrapper bound to the specified session
833
+ *
834
+ * This is useful for retrieving sessions across different requests/contexts
835
+ * without storing the ExecutionSession object (which has RPC lifecycle limitations)
836
+ *
837
+ * @param sessionId - The ID of an existing session
838
+ * @returns ExecutionSession wrapper bound to the session
755
839
  */
756
- async runCode(
757
- code: string,
758
- options?: RunCodeOptions
759
- ): Promise<ExecutionResult> {
760
- const execution = await this.codeInterpreter.runCode(code, options);
761
- // Convert to plain object for RPC serialization
762
- return execution.toJSON();
840
+ async getSession(sessionId: string): Promise<ExecutionSession> {
841
+ // No need to verify session exists in container - operations will fail naturally if it doesn't
842
+ return this.getSessionWrapper(sessionId);
763
843
  }
764
844
 
765
845
  /**
766
- * Run code and return a streaming response
846
+ * Internal helper to create ExecutionSession wrapper for a given sessionId
847
+ * Used by both createSession and getSession
767
848
  */
768
- async runCodeStream(
769
- code: string,
770
- options?: RunCodeOptions
771
- ): Promise<ReadableStream> {
849
+ private getSessionWrapper(sessionId: string): ExecutionSession {
850
+ return {
851
+ id: sessionId,
852
+
853
+ // Command execution - delegate to internal session-aware methods
854
+ exec: (command, options) => this.execWithSession(command, sessionId, options),
855
+ execStream: (command, options) => this.execStreamWithSession(command, sessionId, options),
856
+
857
+ // Process management
858
+ startProcess: (command, options) => this.startProcess(command, options, sessionId),
859
+ listProcesses: () => this.listProcesses(sessionId),
860
+ getProcess: (id) => this.getProcess(id, sessionId),
861
+ killProcess: (id, signal) => this.killProcess(id, signal),
862
+ killAllProcesses: () => this.killAllProcesses(),
863
+ cleanupCompletedProcesses: () => this.cleanupCompletedProcesses(),
864
+ getProcessLogs: (id) => this.getProcessLogs(id),
865
+ streamProcessLogs: (processId, options) => this.streamProcessLogs(processId, options),
866
+
867
+ // File operations - pass sessionId via options or parameter
868
+ writeFile: (path, content, options) => this.writeFile(path, content, { ...options, sessionId }),
869
+ readFile: (path, options) => this.readFile(path, { ...options, sessionId }),
870
+ readFileStream: (path) => this.readFileStream(path, { sessionId }),
871
+ mkdir: (path, options) => this.mkdir(path, { ...options, sessionId }),
872
+ deleteFile: (path) => this.deleteFile(path, sessionId),
873
+ renameFile: (oldPath, newPath) => this.renameFile(oldPath, newPath, sessionId),
874
+ moveFile: (sourcePath, destPath) => this.moveFile(sourcePath, destPath, sessionId),
875
+ listFiles: (path, options) => this.client.files.listFiles(path, sessionId, options),
876
+
877
+ // Git operations
878
+ gitCheckout: (repoUrl, options) => this.gitCheckout(repoUrl, { ...options, sessionId }),
879
+
880
+ // Environment management - needs special handling
881
+ setEnvVars: async (envVars: Record<string, string>) => {
882
+ try {
883
+ // Set environment variables by executing export commands
884
+ for (const [key, value] of Object.entries(envVars)) {
885
+ const escapedValue = value.replace(/'/g, "'\\''");
886
+ const exportCommand = `export ${key}='${escapedValue}'`;
887
+
888
+ const result = await this.client.commands.execute(exportCommand, sessionId);
889
+
890
+ if (result.exitCode !== 0) {
891
+ throw new Error(`Failed to set ${key}: ${result.stderr || 'Unknown error'}`);
892
+ }
893
+ }
894
+ } catch (error) {
895
+ this.logger.error('Failed to set environment variables', error instanceof Error ? error : new Error(String(error)), { sessionId });
896
+ throw error;
897
+ }
898
+ },
899
+
900
+ // Code interpreter methods - delegate to sandbox's code interpreter
901
+ createCodeContext: (options) => this.codeInterpreter.createCodeContext(options),
902
+ runCode: async (code, options) => {
903
+ const execution = await this.codeInterpreter.runCode(code, options);
904
+ return execution.toJSON();
905
+ },
906
+ runCodeStream: (code, options) => this.codeInterpreter.runCodeStream(code, options),
907
+ listCodeContexts: () => this.codeInterpreter.listCodeContexts(),
908
+ deleteCodeContext: (contextId) => this.codeInterpreter.deleteCodeContext(contextId),
909
+ };
910
+ }
911
+
912
+ // ============================================================================
913
+ // Code interpreter methods - delegate to CodeInterpreter wrapper
914
+ // ============================================================================
915
+
916
+ async createCodeContext(options?: CreateContextOptions): Promise<CodeContext> {
917
+ return this.codeInterpreter.createCodeContext(options);
918
+ }
919
+
920
+ async runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult> {
921
+ const execution = await this.codeInterpreter.runCode(code, options);
922
+ return execution.toJSON();
923
+ }
924
+
925
+ async runCodeStream(code: string, options?: RunCodeOptions): Promise<ReadableStream> {
772
926
  return this.codeInterpreter.runCodeStream(code, options);
773
927
  }
774
928
 
775
- /**
776
- * List all code contexts
777
- */
778
929
  async listCodeContexts(): Promise<CodeContext[]> {
779
930
  return this.codeInterpreter.listCodeContexts();
780
931
  }
781
932
 
782
- /**
783
- * Delete a code context
784
- */
785
933
  async deleteCodeContext(contextId: string): Promise<void> {
786
934
  return this.codeInterpreter.deleteCodeContext(contextId);
787
935
  }