@agentuity/server 1.0.18 → 1.0.20

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 (74) hide show
  1. package/dist/api/api.d.ts.map +1 -1
  2. package/dist/api/api.js.map +1 -1
  3. package/dist/api/queue/analytics.d.ts.map +1 -1
  4. package/dist/api/queue/analytics.js.map +1 -1
  5. package/dist/api/queue/destinations.d.ts.map +1 -1
  6. package/dist/api/queue/destinations.js +2 -2
  7. package/dist/api/queue/destinations.js.map +1 -1
  8. package/dist/api/queue/dlq.d.ts.map +1 -1
  9. package/dist/api/queue/dlq.js +1 -1
  10. package/dist/api/queue/dlq.js.map +1 -1
  11. package/dist/api/queue/queues.d.ts.map +1 -1
  12. package/dist/api/queue/queues.js.map +1 -1
  13. package/dist/api/queue/sources.d.ts.map +1 -1
  14. package/dist/api/queue/sources.js +1 -1
  15. package/dist/api/queue/sources.js.map +1 -1
  16. package/dist/api/queue/websocket.d.ts +24 -0
  17. package/dist/api/queue/websocket.d.ts.map +1 -1
  18. package/dist/api/queue/websocket.js +20 -2
  19. package/dist/api/queue/websocket.js.map +1 -1
  20. package/dist/api/region/create.d.ts.map +1 -1
  21. package/dist/api/region/create.js +6 -0
  22. package/dist/api/region/create.js.map +1 -1
  23. package/dist/api/sandbox/cli-list.d.ts.map +1 -1
  24. package/dist/api/sandbox/cli-list.js.map +1 -1
  25. package/dist/api/sandbox/client.d.ts +4 -0
  26. package/dist/api/sandbox/client.d.ts.map +1 -1
  27. package/dist/api/sandbox/client.js +1 -0
  28. package/dist/api/sandbox/client.js.map +1 -1
  29. package/dist/api/sandbox/create.d.ts +6 -0
  30. package/dist/api/sandbox/create.d.ts.map +1 -1
  31. package/dist/api/sandbox/create.js +12 -1
  32. package/dist/api/sandbox/create.js.map +1 -1
  33. package/dist/api/sandbox/get.d.ts +4 -0
  34. package/dist/api/sandbox/get.d.ts.map +1 -1
  35. package/dist/api/sandbox/get.js +15 -1
  36. package/dist/api/sandbox/get.js.map +1 -1
  37. package/dist/api/sandbox/getStatus.d.ts +16 -0
  38. package/dist/api/sandbox/getStatus.d.ts.map +1 -0
  39. package/dist/api/sandbox/getStatus.js +32 -0
  40. package/dist/api/sandbox/getStatus.js.map +1 -0
  41. package/dist/api/sandbox/index.d.ts +2 -0
  42. package/dist/api/sandbox/index.d.ts.map +1 -1
  43. package/dist/api/sandbox/index.js +1 -0
  44. package/dist/api/sandbox/index.js.map +1 -1
  45. package/dist/api/sandbox/list.d.ts +6 -0
  46. package/dist/api/sandbox/list.d.ts.map +1 -1
  47. package/dist/api/sandbox/list.js +15 -1
  48. package/dist/api/sandbox/list.js.map +1 -1
  49. package/dist/api/sandbox/pause.d.ts.map +1 -1
  50. package/dist/api/sandbox/pause.js.map +1 -1
  51. package/dist/api/sandbox/resume.d.ts.map +1 -1
  52. package/dist/api/sandbox/resume.js.map +1 -1
  53. package/dist/api/sandbox/run.d.ts.map +1 -1
  54. package/dist/api/sandbox/run.js +94 -70
  55. package/dist/api/sandbox/run.js.map +1 -1
  56. package/package.json +4 -4
  57. package/src/api/api.ts +3 -8
  58. package/src/api/queue/analytics.ts +1 -7
  59. package/src/api/queue/destinations.ts +5 -6
  60. package/src/api/queue/dlq.ts +7 -7
  61. package/src/api/queue/queues.ts +6 -1
  62. package/src/api/queue/sources.ts +1 -6
  63. package/src/api/queue/websocket.ts +32 -6
  64. package/src/api/region/create.ts +6 -0
  65. package/src/api/sandbox/cli-list.ts +9 -1
  66. package/src/api/sandbox/client.ts +6 -0
  67. package/src/api/sandbox/create.ts +14 -1
  68. package/src/api/sandbox/get.ts +15 -1
  69. package/src/api/sandbox/getStatus.ts +54 -0
  70. package/src/api/sandbox/index.ts +2 -0
  71. package/src/api/sandbox/list.ts +15 -1
  72. package/src/api/sandbox/pause.ts +1 -4
  73. package/src/api/sandbox/resume.ts +1 -4
  74. package/src/api/sandbox/run.ts +103 -78
@@ -105,7 +105,17 @@ export const SandboxInfoDataSchema = z
105
105
  name: z.string().optional().describe('Sandbox name'),
106
106
  description: z.string().optional().describe('Sandbox description'),
107
107
  status: z
108
- .enum(['creating', 'idle', 'running', 'paused', 'stopping', 'suspended', 'terminated', 'failed', 'deleted'])
108
+ .enum([
109
+ 'creating',
110
+ 'idle',
111
+ 'running',
112
+ 'paused',
113
+ 'stopping',
114
+ 'suspended',
115
+ 'terminated',
116
+ 'failed',
117
+ 'deleted',
118
+ ])
109
119
  .describe('Current status of the sandbox'),
110
120
  mode: z.string().optional().describe('Sandbox mode (interactive or oneshot)'),
111
121
  createdAt: z.string().describe('ISO timestamp when the sandbox was created'),
@@ -119,6 +129,8 @@ export const SandboxInfoDataSchema = z
119
129
  .describe('Exit code from the last execution (only for terminated/failed sandboxes)'),
120
130
  stdoutStreamUrl: z.string().optional().describe('URL for streaming stdout output'),
121
131
  stderrStreamUrl: z.string().optional().describe('URL for streaming stderr output'),
132
+ auditStreamId: z.string().optional().describe('ID of the audit event stream'),
133
+ auditStreamUrl: z.string().optional().describe('URL for streaming audit events'),
122
134
  dependencies: z
123
135
  .array(z.string())
124
136
  .optional()
@@ -212,6 +224,8 @@ export async function sandboxGet(
212
224
  exitCode: resp.data.exitCode,
213
225
  stdoutStreamUrl: resp.data.stdoutStreamUrl,
214
226
  stderrStreamUrl: resp.data.stderrStreamUrl,
227
+ auditStreamId: resp.data.auditStreamId,
228
+ auditStreamUrl: resp.data.auditStreamUrl,
215
229
  dependencies: resp.data.dependencies,
216
230
  packages: resp.data.packages,
217
231
  metadata: resp.data.metadata as Record<string, unknown> | undefined,
@@ -0,0 +1,54 @@
1
+ import { z } from 'zod';
2
+ import { type APIClient, APIResponseSchema } from '../api.ts';
3
+ import { API_VERSION, throwSandboxError } from './util.ts';
4
+
5
+ const SandboxStatusDataSchema = z.object({
6
+ sandboxId: z.string(),
7
+ status: z.string(),
8
+ exitCode: z.number().optional(),
9
+ });
10
+
11
+ const SandboxStatusResponseSchema = APIResponseSchema(SandboxStatusDataSchema);
12
+
13
+ export interface SandboxGetStatusParams {
14
+ sandboxId: string;
15
+ orgId?: string;
16
+ }
17
+
18
+ export interface SandboxStatusResult {
19
+ sandboxId: string;
20
+ status: string;
21
+ exitCode?: number;
22
+ }
23
+
24
+ /**
25
+ * Retrieves lightweight sandbox status (status + exitCode only).
26
+ * Optimized for the sandbox run flow — backed by Redis for ~1ms response time.
27
+ */
28
+ export async function sandboxGetStatus(
29
+ client: APIClient,
30
+ params: SandboxGetStatusParams
31
+ ): Promise<SandboxStatusResult> {
32
+ const { sandboxId, orgId } = params;
33
+ const queryParams = new URLSearchParams();
34
+ if (orgId) {
35
+ queryParams.set('orgId', orgId);
36
+ }
37
+ const queryString = queryParams.toString();
38
+ const url = `/sandbox/${API_VERSION}/status/${sandboxId}${queryString ? `?${queryString}` : ''}`;
39
+
40
+ const resp = await client.get<z.infer<typeof SandboxStatusResponseSchema>>(
41
+ url,
42
+ SandboxStatusResponseSchema
43
+ );
44
+
45
+ if (resp.success) {
46
+ return {
47
+ sandboxId: resp.data.sandboxId,
48
+ status: resp.data.status,
49
+ exitCode: resp.data.exitCode,
50
+ };
51
+ }
52
+
53
+ throwSandboxError(resp, { sandboxId });
54
+ }
@@ -95,6 +95,8 @@ export {
95
95
  SandboxUserInfoSchema,
96
96
  sandboxGet,
97
97
  } from './get.ts';
98
+ export type { SandboxGetStatusParams, SandboxStatusResult } from './getStatus.ts';
99
+ export { sandboxGetStatus } from './getStatus.ts';
98
100
  export type { SandboxListParams } from './list.ts';
99
101
  export {
100
102
  ListSandboxesDataSchema,
@@ -76,7 +76,17 @@ export const SandboxInfoSchema = z
76
76
  name: z.string().optional().describe('Sandbox name'),
77
77
  description: z.string().optional().describe('Sandbox description'),
78
78
  status: z
79
- .enum(['creating', 'idle', 'running', 'paused', 'stopping', 'suspended', 'terminated', 'failed', 'deleted'])
79
+ .enum([
80
+ 'creating',
81
+ 'idle',
82
+ 'running',
83
+ 'paused',
84
+ 'stopping',
85
+ 'suspended',
86
+ 'terminated',
87
+ 'failed',
88
+ 'deleted',
89
+ ])
80
90
  .describe('Current status of the sandbox'),
81
91
  mode: z.string().optional().describe('Sandbox mode (interactive or oneshot)'),
82
92
  createdAt: z.string().describe('ISO timestamp when the sandbox was created'),
@@ -86,6 +96,8 @@ export const SandboxInfoSchema = z
86
96
  executions: z.number().describe('Total number of executions in this sandbox'),
87
97
  stdoutStreamUrl: z.string().optional().describe('URL for streaming stdout output'),
88
98
  stderrStreamUrl: z.string().optional().describe('URL for streaming stderr output'),
99
+ auditStreamId: z.string().optional().describe('ID of the audit event stream'),
100
+ auditStreamUrl: z.string().optional().describe('URL for streaming audit events'),
89
101
  networkEnabled: z.boolean().optional().describe('Whether network access is enabled'),
90
102
  networkPort: z.number().optional().describe('Network port exposed from the sandbox'),
91
103
  url: z
@@ -197,6 +209,8 @@ export async function sandboxList(
197
209
  executions: s.executions,
198
210
  stdoutStreamUrl: s.stdoutStreamUrl,
199
211
  stderrStreamUrl: s.stderrStreamUrl,
212
+ auditStreamId: s.auditStreamId,
213
+ auditStreamUrl: s.auditStreamUrl,
200
214
  networkEnabled: s.networkEnabled,
201
215
  networkPort: s.networkPort,
202
216
  url: s.url,
@@ -16,10 +16,7 @@ export interface SandboxPauseParams {
16
16
  * @param params - Parameters including the sandbox ID to pause
17
17
  * @throws {SandboxResponseError} If the sandbox is not found or pause fails
18
18
  */
19
- export async function sandboxPause(
20
- client: APIClient,
21
- params: SandboxPauseParams
22
- ): Promise<void> {
19
+ export async function sandboxPause(client: APIClient, params: SandboxPauseParams): Promise<void> {
23
20
  const { sandboxId, orgId } = params;
24
21
  const queryParams = new URLSearchParams();
25
22
  if (orgId) {
@@ -16,10 +16,7 @@ export interface SandboxResumeParams {
16
16
  * @param params - Parameters including the sandbox ID to resume
17
17
  * @throws {SandboxResponseError} If the sandbox is not found or resume fails
18
18
  */
19
- export async function sandboxResume(
20
- client: APIClient,
21
- params: SandboxResumeParams
22
- ): Promise<void> {
19
+ export async function sandboxResume(client: APIClient, params: SandboxResumeParams): Promise<void> {
23
20
  const { sandboxId, orgId } = params;
24
21
  const queryParams = new URLSearchParams();
25
22
  if (orgId) {
@@ -4,11 +4,13 @@ import { PassThrough } from 'node:stream';
4
4
  import { APIClient, PaymentRequiredError } from '../api.ts';
5
5
  import { sandboxCreate } from './create.ts';
6
6
  import { sandboxDestroy } from './destroy.ts';
7
- import { sandboxGet } from './get.ts';
8
- import { ExecutionCancelledError, ExecutionTimeoutError, writeAndDrain } from './util.ts';
7
+ import { sandboxGetStatus } from './getStatus.ts';
8
+ import { ExecutionCancelledError, writeAndDrain } from './util.ts';
9
9
  import type { SandboxRunOptions, SandboxRunResult } from '@agentuity/core';
10
10
  import { getServiceUrls } from '../../config.ts';
11
11
 
12
+ const timingLogsEnabled = false;
13
+
12
14
  /**
13
15
  * Creates a Writable stream that captures all chunks to a buffer array
14
16
  * and optionally tees (forwards) them to one or more user-provided streams.
@@ -28,16 +30,13 @@ function createTeeWritable(chunks: Buffer[], ...userStreams: (Writable | undefin
28
30
  // Pipe to all provided user streams with proper backpressure handling
29
31
  for (const userStream of userStreams) {
30
32
  if (userStream) {
31
- tee.pipe(userStream);
33
+ tee.pipe(userStream, { end: false });
32
34
  }
33
35
  }
34
36
 
35
37
  return tee;
36
38
  }
37
39
 
38
- const POLL_INTERVAL_MS = 500;
39
- const MAX_POLL_ATTEMPTS = 7200;
40
-
41
40
  export interface SandboxRunParams {
42
41
  options: SandboxRunOptions;
43
42
  orgId?: string;
@@ -67,6 +66,7 @@ export async function sandboxRun(
67
66
  ): Promise<SandboxRunResult> {
68
67
  const { options, orgId, region, apiKey, signal, stdin, stdout, stderr, logger } = params;
69
68
  const started = Date.now();
69
+ if (timingLogsEnabled) console.error(`[TIMING] +0ms: sandbox run started`);
70
70
 
71
71
  let stdinStreamId: string | undefined;
72
72
  let stdinStreamUrl: string | undefined;
@@ -105,6 +105,8 @@ export async function sandboxRun(
105
105
  stdoutStreamUrl ?? 'none',
106
106
  stderrStreamUrl ?? 'none'
107
107
  );
108
+ if (timingLogsEnabled)
109
+ console.error(`[TIMING] +${Date.now() - started}ms: sandbox created (${sandboxId})`);
108
110
 
109
111
  const abortController = new AbortController();
110
112
  const streamPromises: Promise<void>[] = [];
@@ -131,16 +133,16 @@ export async function sandboxRun(
131
133
  stdoutStreamUrl && stderrStreamUrl && stdoutStreamUrl === stderrStreamUrl;
132
134
 
133
135
  if (isCombinedOutput) {
134
- // Stream combined output - capture to stdoutChunks, tee to both user's stdout AND stderr
136
+ // Stream combined output to stdout only to avoid duplicates
135
137
  if (stdoutStreamUrl) {
136
138
  logger?.debug('using combined output stream (stdout === stderr)');
137
- // Tee to both stdout and stderr so both user streams receive real-time data
138
- const teeStream = createTeeWritable(stdoutChunks, stdout, stderr);
139
+ const teeStream = createTeeWritable(stdoutChunks, stdout);
139
140
  const combinedPromise = streamUrlToWritable(
140
141
  stdoutStreamUrl,
141
142
  teeStream,
142
143
  abortController.signal,
143
- logger
144
+ logger,
145
+ started
144
146
  );
145
147
  streamPromises.push(combinedPromise);
146
148
  }
@@ -152,7 +154,8 @@ export async function sandboxRun(
152
154
  stdoutStreamUrl,
153
155
  teeStream,
154
156
  abortController.signal,
155
- logger
157
+ logger,
158
+ started
156
159
  );
157
160
  streamPromises.push(stdoutPromise);
158
161
  }
@@ -164,87 +167,95 @@ export async function sandboxRun(
164
167
  stderrStreamUrl,
165
168
  teeStream,
166
169
  abortController.signal,
167
- logger
170
+ logger,
171
+ started
168
172
  );
169
173
  streamPromises.push(stderrPromise);
170
174
  }
171
175
  }
172
176
 
173
- // Poll for sandbox completion in parallel with streaming
174
- let attempts = 0;
175
- let finalStatus: 'terminated' | 'failed' | null = null;
176
- let finalExitCode: number | undefined;
177
-
178
- while (attempts < MAX_POLL_ATTEMPTS) {
179
- if (signal?.aborted) {
180
- abortController.abort();
181
- throw new ExecutionCancelledError({
182
- message: 'Sandbox execution cancelled',
183
- sandboxId,
184
- });
185
- }
186
-
187
- await sleep(POLL_INTERVAL_MS);
188
- attempts++;
189
-
190
- try {
191
- const sandboxInfo = await sandboxGet(client, { sandboxId, orgId });
177
+ // Wait for streams to complete Pulse closes streams on sandbox termination (EOF).
178
+ // This is our primary completion signal; no polling needed.
179
+ logger?.debug('waiting for streams to complete...');
192
180
 
193
- if (sandboxInfo.status === 'terminated') {
194
- finalStatus = 'terminated';
195
- finalExitCode = sandboxInfo.exitCode;
196
- break;
181
+ if (streamPromises.length > 0) {
182
+ if (signal) {
183
+ // Race streams against abort signal, cleaning up the listener
184
+ // in all cases so an orphaned reject cannot fire after settlement.
185
+ let onAbort: (() => void) | undefined;
186
+ try {
187
+ await Promise.race([
188
+ Promise.allSettled(streamPromises),
189
+ new Promise<never>((_, reject) => {
190
+ onAbort = () => {
191
+ abortController.abort();
192
+ reject(
193
+ new ExecutionCancelledError({
194
+ message: 'Sandbox execution cancelled',
195
+ sandboxId,
196
+ })
197
+ );
198
+ };
199
+ if (signal.aborted) {
200
+ onAbort();
201
+ } else {
202
+ signal.addEventListener('abort', onAbort, { once: true });
203
+ }
204
+ }),
205
+ ]);
206
+ } finally {
207
+ if (onAbort && signal) {
208
+ signal.removeEventListener('abort', onAbort);
209
+ }
197
210
  }
211
+ } else {
212
+ await Promise.allSettled(streamPromises);
213
+ }
214
+ } else {
215
+ // No streams available (shouldn't happen for oneshot, but handle defensively).
216
+ // Fall back to a single wait then check.
217
+ logger?.debug('no streams to wait on, checking sandbox status directly');
218
+ }
198
219
 
199
- if (sandboxInfo.status === 'failed') {
200
- finalStatus = 'failed';
201
- finalExitCode = sandboxInfo.exitCode;
202
- break;
203
- }
204
- } catch {
205
- // Ignore polling errors, continue
206
- continue;
220
+ if (timingLogsEnabled)
221
+ console.error(`[TIMING] +${Date.now() - started}ms: all streams done, fetching exit code`);
222
+ logger?.debug('streams completed, fetching final status');
223
+
224
+ // Stream EOF means the sandbox is done — hadron only closes streams after the
225
+ // container exits. Fetch status once for the exit code; if lifecycle events
226
+ // haven't propagated to Catalyst yet, default to exit code 0.
227
+ let exitCode = 0;
228
+ try {
229
+ const sandboxStatus = await sandboxGetStatus(client, { sandboxId, orgId });
230
+ if (sandboxStatus.exitCode != null) {
231
+ exitCode = sandboxStatus.exitCode;
232
+ } else if (sandboxStatus.status === 'failed') {
233
+ exitCode = 1;
207
234
  }
235
+ } catch {
236
+ // Sandbox may already be destroyed (fire-and-forget teardown).
237
+ // Stream EOF already confirmed execution completed.
238
+ logger?.debug('sandboxGetStatus failed after stream EOF, using default exit code 0');
208
239
  }
209
240
 
210
- // Sandbox completed - wait for streams to complete naturally (EOF)
211
- // Pulse closes streams when the sandbox terminates, so streams should EOF
212
- // We must wait for streams to fully drain before returning
213
- logger?.debug('waiting for streams to complete...');
214
- await Promise.allSettled(streamPromises);
215
- logger?.debug('streams completed');
241
+ if (timingLogsEnabled)
242
+ console.error(
243
+ `[TIMING] +${Date.now() - started}ms: sandboxGet complete (exit: ${exitCode})`
244
+ );
216
245
 
217
246
  // Build captured output strings
218
247
  const capturedStdout = Buffer.concat(stdoutChunks).toString('utf-8');
219
- // For combined output, stderr is the same as stdout; otherwise use stderrChunks
220
248
  const capturedStderr = isCombinedOutput
221
249
  ? capturedStdout
222
250
  : Buffer.concat(stderrChunks).toString('utf-8');
223
251
 
224
- if (finalStatus === 'terminated') {
225
- return {
226
- sandboxId,
227
- exitCode: finalExitCode ?? 0,
228
- durationMs: Date.now() - started,
229
- stdout: capturedStdout,
230
- stderr: capturedStderr,
231
- };
232
- }
233
-
234
- if (finalStatus === 'failed') {
235
- return {
236
- sandboxId,
237
- exitCode: finalExitCode ?? 1,
238
- durationMs: Date.now() - started,
239
- stdout: capturedStdout,
240
- stderr: capturedStderr,
241
- };
242
- }
243
-
244
- throw new ExecutionTimeoutError({
245
- message: 'Sandbox execution polling timed out',
252
+ return {
246
253
  sandboxId,
247
- });
254
+ exitCode,
255
+ durationMs: Date.now() - started,
256
+ stdout: capturedStdout,
257
+ stderr: capturedStderr,
258
+ };
248
259
  } catch (error) {
249
260
  abortController.abort();
250
261
  try {
@@ -373,12 +384,17 @@ async function streamUrlToWritable(
373
384
  url: string,
374
385
  writable: Writable,
375
386
  signal: AbortSignal,
376
- logger?: Logger
387
+ logger?: Logger,
388
+ started?: number
377
389
  ): Promise<void> {
378
390
  try {
379
391
  logger?.debug('fetching stream: %s', url);
380
392
  const response = await fetch(url, { signal });
381
393
  logger?.debug('stream response status: %d', response.status);
394
+ if (timingLogsEnabled && started)
395
+ console.error(
396
+ `[TIMING] +${Date.now() - started}ms: stream response received (status: ${response.status})`
397
+ );
382
398
 
383
399
  if (!response.ok || !response.body) {
384
400
  logger?.debug('stream response not ok or no body');
@@ -386,20 +402,33 @@ async function streamUrlToWritable(
386
402
  }
387
403
 
388
404
  const reader = response.body.getReader();
405
+ let firstChunk = true;
389
406
 
390
407
  // Read until EOF - Pulse will block until data is available
391
408
  while (true) {
392
409
  const { done, value } = await reader.read();
393
410
  if (done) {
394
411
  logger?.debug('stream EOF');
412
+ if (timingLogsEnabled && started)
413
+ console.error(`[TIMING] +${Date.now() - started}ms: stream EOF`);
395
414
  break;
396
415
  }
397
416
 
398
417
  if (value) {
418
+ if (firstChunk && started) {
419
+ if (timingLogsEnabled)
420
+ console.error(
421
+ `[TIMING] +${Date.now() - started}ms: first chunk (${value.length} bytes)`
422
+ );
423
+ firstChunk = false;
424
+ }
399
425
  logger?.debug('stream chunk: %d bytes', value.length);
400
426
  await writeAndDrain(writable, value);
401
427
  }
402
428
  }
429
+ // Signal end-of-stream to the tee/pipe chain so downstream
430
+ // consumers (e.g. process.stdout pipe) know no more data is coming.
431
+ writable.end();
403
432
  } catch (err) {
404
433
  if (err instanceof Error && err.name === 'AbortError') {
405
434
  logger?.debug('stream aborted');
@@ -408,7 +437,3 @@ async function streamUrlToWritable(
408
437
  logger?.debug('stream error: %s', err);
409
438
  }
410
439
  }
411
-
412
- function sleep(ms: number): Promise<void> {
413
- return new Promise((resolve) => setTimeout(resolve, ms));
414
- }