@axiom-lattice/microsandbox-service 0.1.12 → 2.0.0

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.
package/dist/index.d.mts CHANGED
@@ -87,10 +87,10 @@ declare const ensureSandboxSchema: z.ZodObject<{
87
87
  /** Guest ports to publish; each is mapped to a deterministic host port. */
88
88
  exposePorts: z.ZodOptional<z.ZodArray<z.ZodNumber, "many">>;
89
89
  }, "strip", z.ZodTypeAny, {
90
+ env?: Record<string, string> | undefined;
90
91
  image?: string | undefined;
91
92
  cpus?: number | undefined;
92
93
  memoryMib?: number | undefined;
93
- env?: Record<string, string> | undefined;
94
94
  volumes?: Record<string, {
95
95
  type: "bind";
96
96
  source: string;
@@ -107,10 +107,10 @@ declare const ensureSandboxSchema: z.ZodObject<{
107
107
  timeoutSeconds?: number | null | undefined;
108
108
  exposePorts?: number[] | undefined;
109
109
  }, {
110
+ env?: Record<string, string> | undefined;
110
111
  image?: string | undefined;
111
112
  cpus?: number | undefined;
112
113
  memoryMib?: number | undefined;
113
- env?: Record<string, string> | undefined;
114
114
  volumes?: Record<string, {
115
115
  type: "bind";
116
116
  source: string;
@@ -132,29 +132,30 @@ declare const listSandboxesQuerySchema: z.ZodObject<{
132
132
  image: z.ZodOptional<z.ZodString>;
133
133
  search: z.ZodOptional<z.ZodString>;
134
134
  }, "strip", z.ZodTypeAny, {
135
- status?: "unknown" | "running" | "stopped" | "crashed" | undefined;
136
135
  search?: string | undefined;
136
+ status?: "unknown" | "running" | "stopped" | "crashed" | undefined;
137
137
  image?: string | undefined;
138
138
  }, {
139
- status?: "unknown" | "running" | "stopped" | "crashed" | undefined;
140
139
  search?: string | undefined;
140
+ status?: "unknown" | "running" | "stopped" | "crashed" | undefined;
141
141
  image?: string | undefined;
142
142
  }>;
143
143
  declare const shellExecSchema: z.ZodObject<{
144
144
  sandboxName: z.ZodString;
145
145
  command: z.ZodString;
146
146
  exec_dir: z.ZodOptional<z.ZodString>;
147
- timeout: z.ZodOptional<z.ZodNumber>;
147
+ /** Maximum execution time in milliseconds. */
148
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
148
149
  }, "strip", z.ZodTypeAny, {
149
150
  sandboxName: string;
150
151
  command: string;
151
- timeout?: number | undefined;
152
152
  exec_dir?: string | undefined;
153
+ timeoutMs?: number | undefined;
153
154
  }, {
154
155
  sandboxName: string;
155
156
  command: string;
156
- timeout?: number | undefined;
157
157
  exec_dir?: string | undefined;
158
+ timeoutMs?: number | undefined;
158
159
  }>;
159
160
  declare const sandboxLogsSchema: z.ZodObject<{
160
161
  tail: z.ZodOptional<z.ZodNumber>;
@@ -489,6 +490,12 @@ declare class MicrosandboxRuntimeService {
489
490
  stderr: string;
490
491
  exitCode: number;
491
492
  }>;
493
+ /**
494
+ * Run a shell command with a hard timeout by streaming it and killing the
495
+ * handle once `timeoutMs` elapses, preserving the same shell semantics as
496
+ * {@link RuntimeNative.shell} (which has no timeout knob).
497
+ */
498
+ private runShellWithTimeout;
492
499
  getSandboxLogs(name: string, opts: SandboxLogsInput): Promise<{
493
500
  entries: Array<{
494
501
  timestamp: string;
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  MicrosandboxRuntimeService,
3
3
  SandboxRegistry,
4
4
  buildApp
5
- } from "./chunk-Z6NN3T6H.mjs";
5
+ } from "./chunk-F4FMPABT.mjs";
6
6
  export {
7
7
  MicrosandboxRuntimeService,
8
8
  SandboxRegistry,
package/dist/server.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  startServer
3
- } from "./chunk-O5VQK7SY.mjs";
4
- import "./chunk-Z6NN3T6H.mjs";
3
+ } from "./chunk-TOHCWZXQ.mjs";
4
+ import "./chunk-F4FMPABT.mjs";
5
5
  export {
6
6
  startServer
7
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/microsandbox-service",
3
- "version": "0.1.12",
3
+ "version": "2.0.0",
4
4
  "main": "dist/index.mjs",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -212,6 +212,31 @@ describe("MicrosandboxRuntimeService", () => {
212
212
  expect(Volume.builder).not.toHaveBeenCalled();
213
213
  });
214
214
 
215
+ it("defaults memory to 1024 MiB when the caller omits it", async () => {
216
+ const previousMemory = process.env.MICROSANDBOX_MEMORY;
217
+ delete process.env.MICROSANDBOX_MEMORY;
218
+
219
+ try {
220
+ const registry = {
221
+ ensure: jest.fn().mockResolvedValue({ id: "native" }),
222
+ } as unknown as SandboxRegistry;
223
+ const service = new MicrosandboxRuntimeService({ registry });
224
+
225
+ await service.ensureSandbox("tenant-a", { image: "python:3.11-slim" });
226
+
227
+ expect(registry.ensure).toHaveBeenCalledWith(
228
+ "tenant-a",
229
+ expect.objectContaining({ memoryMib: 1024 })
230
+ );
231
+ } finally {
232
+ if (previousMemory === undefined) {
233
+ delete process.env.MICROSANDBOX_MEMORY;
234
+ } else {
235
+ process.env.MICROSANDBOX_MEMORY = previousMemory;
236
+ }
237
+ }
238
+ });
239
+
215
240
  it("startSandbox clears the cached handle and re-ensures the sandbox", async () => {
216
241
  const registry = {
217
242
  remove: jest.fn(),
@@ -725,3 +750,43 @@ describe("MicrosandboxRuntimeService", () => {
725
750
  expect(getEndpoint).toHaveBeenCalledWith("plugin-abc", 8080);
726
751
  });
727
752
  });
753
+
754
+ describe("MicrosandboxRuntimeService.execCommand timeout", () => {
755
+ it("uses the plain shell when no timeout is supplied", async () => {
756
+ const shell = jest.fn().mockResolvedValue({ stdout: () => "ok", stderr: () => "", code: 0 });
757
+ const shellStream = jest.fn();
758
+ const registry = {
759
+ ensure: jest.fn().mockResolvedValue({ shell, shellStream }),
760
+ } as unknown as SandboxRegistry;
761
+ const service = new MicrosandboxRuntimeService({ registry });
762
+
763
+ await service.execCommand({ sandboxName: "sb", command: "echo hi" });
764
+
765
+ expect(shell).toHaveBeenCalledWith("echo hi");
766
+ expect(shellStream).not.toHaveBeenCalled();
767
+ });
768
+
769
+ it("streams the shell and kills it once the timeout elapses", async () => {
770
+ let resolveCollect!: (value: unknown) => void;
771
+ const collect = jest.fn().mockImplementation(
772
+ () => new Promise((resolve) => { resolveCollect = resolve; }),
773
+ );
774
+ const kill = jest.fn().mockImplementation(async () => {
775
+ resolveCollect({ stdout: () => "", stderr: () => "", code: 137 });
776
+ });
777
+ const shellStream = jest.fn().mockResolvedValue({ collect, kill });
778
+ const shell = jest.fn();
779
+ const registry = {
780
+ ensure: jest.fn().mockResolvedValue({ shell, shellStream }),
781
+ } as unknown as SandboxRegistry;
782
+ const service = new MicrosandboxRuntimeService({ registry });
783
+
784
+ const result = await service.execCommand({ sandboxName: "sb", command: "sleep 99", timeoutMs: 20 });
785
+
786
+ expect(shell).not.toHaveBeenCalled();
787
+ expect(shellStream).toHaveBeenCalledWith("sleep 99");
788
+ expect(kill).toHaveBeenCalledTimes(1);
789
+ expect(collect).toHaveBeenCalledTimes(1);
790
+ expect(result.exitCode).toBe(137);
791
+ });
792
+ });
@@ -485,7 +485,7 @@ describe("SandboxRegistry", () => {
485
485
  await registry.ensure("plugin-abc", { image: "base:latest", exposePorts: [9090] });
486
486
 
487
487
  expect(builder.cpus).toHaveBeenCalledWith(2);
488
- expect(builder.memory).toHaveBeenCalledWith(1024);
488
+ expect(builder.memory).toHaveBeenCalledWith(2048);
489
489
  registry.destroy();
490
490
  });
491
491
 
@@ -516,6 +516,18 @@ describe("SandboxRegistry", () => {
516
516
  registry.destroy();
517
517
  });
518
518
 
519
+ it("defaults memory to 1024 MiB when create options omit it", async () => {
520
+ const native = makeNative("created");
521
+ (Sandbox.get as jest.Mock).mockRejectedValueOnce(new Error("not found"));
522
+ createDetached.mockResolvedValue(native);
523
+
524
+ const registry = new SandboxRegistry(0);
525
+ await registry.ensure("plugin-default-memory", { image: "base:latest" });
526
+
527
+ expect(builder.memory).toHaveBeenCalledWith(1024);
528
+ registry.destroy();
529
+ });
530
+
519
531
  it("does not apply an idle lifetime when timeoutSeconds is undefined", async () => {
520
532
  const native = makeNative("created");
521
533
  (Sandbox.get as jest.Mock).mockRejectedValueOnce(new Error("not found"));
@@ -395,7 +395,7 @@ describe("microsandbox-service routes", () => {
395
395
  const execResponse = await app.inject({
396
396
  method: "POST",
397
397
  url: "/api/shell/exec",
398
- payload: { sandboxName: "tenant-project", command: "pwd", exec_dir: "/tmp", timeout: 10 },
398
+ payload: { sandboxName: "tenant-project", command: "pwd", exec_dir: "/tmp", timeoutMs: 10_000 },
399
399
  });
400
400
 
401
401
  expect(startResponse.statusCode).toBe(200);
@@ -440,7 +440,7 @@ describe("microsandbox-service routes", () => {
440
440
  sandboxName: "tenant-project",
441
441
  command: "pwd",
442
442
  exec_dir: "/tmp",
443
- timeout: 10,
443
+ timeoutMs: 10_000,
444
444
  });
445
445
  expect(startResponse.json()).toEqual({
446
446
  success: true,
@@ -29,8 +29,8 @@
29
29
  export const RAW_PERSISTED_SANDBOX_CONFIG_JSON = String.raw`{"name":"fixture-capture","image":{"Oci":{"reference":"base:latest"}},"resources":{"cpus":1,"memory_mib":512},"runtime":{"workdir":null,"shell":null,"scripts":{},"entrypoint":["node","/root/.agents/.plugin-host/server.js"],"cmd":null,"hostname":null,"user":null,"log_level":null,"metrics_sample_interval_ms":1000,"disable_metrics_sample":false},"env":[{"key":"LATTICE_PUBLISHED_PORTS","value":"{\"8080\":41234}"}],"labels":{},"rlimits":[],"mounts":[],"patches":[],"network":{"enabled":true,"ports":[{"host_port":41234,"guest_port":8080,"protocol":"tcp","host_bind":"127.0.0.1"}],"max_connections":null,"trust_host_cas":false},"init":null,"pull_policy":"IfMissing","security_profile":"default","lifecycle":{"ephemeral":false,"max_duration_secs":null,"idle_timeout_secs":null},"manifest_digest":null}`;
30
30
 
31
31
  /**
32
- * Same raw capture, but with `cpus(2).memory(1024)` and guest port 9090
32
+ * Same raw capture, but with `cpus(2).memory(2048)` and guest port 9090
33
33
  * published to host 52345 — non-default resource values so resource recovery
34
- * is observable (the primary fixture's 1/512 coincide with registry defaults).
34
+ * is observable (the registry default memory is 1024 MiB).
35
35
  */
36
- export const RAW_PERSISTED_SANDBOX_CONFIG_JSON_VARIANT = String.raw`{"name":"fixture-capture-resources","image":{"Oci":{"reference":"base:latest"}},"resources":{"cpus":2,"memory_mib":1024},"runtime":{"workdir":null,"shell":null,"scripts":{},"entrypoint":["node","/root/.agents/.plugin-host/server.js"],"cmd":null,"hostname":null,"user":null,"log_level":null,"metrics_sample_interval_ms":1000,"disable_metrics_sample":false},"env":[{"key":"LATTICE_PUBLISHED_PORTS","value":"{\"9090\":52345}"}],"labels":{},"rlimits":[],"mounts":[],"patches":[],"network":{"enabled":true,"ports":[{"host_port":52345,"guest_port":9090,"protocol":"tcp","host_bind":"127.0.0.1"}],"max_connections":null,"trust_host_cas":false},"init":null,"pull_policy":"IfMissing","security_profile":"default","lifecycle":{"ephemeral":false,"max_duration_secs":null,"idle_timeout_secs":null},"manifest_digest":null}`;
36
+ export const RAW_PERSISTED_SANDBOX_CONFIG_JSON_VARIANT = String.raw`{"name":"fixture-capture-resources","image":{"Oci":{"reference":"base:latest"}},"resources":{"cpus":2,"memory_mib":2048},"runtime":{"workdir":null,"shell":null,"scripts":{},"entrypoint":["node","/root/.agents/.plugin-host/server.js"],"cmd":null,"hostname":null,"user":null,"log_level":null,"metrics_sample_interval_ms":1000,"disable_metrics_sample":false},"env":[{"key":"LATTICE_PUBLISHED_PORTS","value":"{\"9090\":52345}"}],"labels":{},"rlimits":[],"mounts":[],"patches":[],"network":{"enabled":true,"ports":[{"host_port":52345,"guest_port":9090,"protocol":"tcp","host_bind":"127.0.0.1"}],"max_connections":null,"trust_host_cas":false},"init":null,"pull_policy":"IfMissing","security_profile":"default","lifecycle":{"ephemeral":false,"max_duration_secs":null,"idle_timeout_secs":null},"manifest_digest":null}`;
package/src/app.ts CHANGED
@@ -23,7 +23,7 @@ export async function buildApp({
23
23
  } = {}): Promise<FastifyInstance> {
24
24
  const app = fastify({
25
25
  logger: false,
26
- bodyLimit: Number(process.env.BODY_LIMIT) || 100 * 1024 * 1024,
26
+ bodyLimit: Number(process.env.BODY_LIMIT) || 500 * 1024 * 1024,
27
27
  });
28
28
 
29
29
  app.register(cors, {
@@ -39,7 +39,7 @@ export function registerSandboxRoutes(
39
39
  app.register(async (instance) => {
40
40
  // Fastify does not apply `bodyLimit` to stream parsers, so enforce the
41
41
  // configured limit here while still passing bytes through unbuffered.
42
- const bodyLimit = instance.initialConfig.bodyLimit ?? 100 * 1024 * 1024;
42
+ const bodyLimit = instance.initialConfig.bodyLimit ?? 500 * 1024 * 1024;
43
43
  // The inherited default `application/json` parser would otherwise consume
44
44
  // the body before the proxy sees it; dropping every parser for this scope
45
45
  // only makes the `"*"` parser the effective one.
@@ -95,7 +95,8 @@ export const shellExecSchema = z.object({
95
95
  sandboxName: z.string().min(1),
96
96
  command: z.string().min(1),
97
97
  exec_dir: z.string().optional(),
98
- timeout: z.number().int().positive().optional(),
98
+ /** Maximum execution time in milliseconds. */
99
+ timeoutMs: z.number().int().positive().optional(),
99
100
  });
100
101
 
101
102
  export const sandboxLogsSchema = z.object({
@@ -14,6 +14,12 @@ const execAsync = promisify(exec);
14
14
 
15
15
  type VolumeConfig = NonNullable<EnsureSandboxInput["volumes"]>[string];
16
16
 
17
+ type ExecOutputLike = {
18
+ stdout(): string;
19
+ stderr(): string;
20
+ get code(): number;
21
+ };
22
+
17
23
  type RuntimeNative = {
18
24
  fs(): {
19
25
  readString(path: string): Promise<string>;
@@ -30,10 +36,10 @@ type RuntimeNative = {
30
36
  stderr(): string;
31
37
  get code(): number;
32
38
  }>;
33
- shell(script: string): Promise<{
34
- stdout(): string;
35
- stderr(): string;
36
- get code(): number;
39
+ shell(script: string): Promise<ExecOutputLike>;
40
+ shellStream(script: string): Promise<{
41
+ collect(): Promise<ExecOutputLike>;
42
+ kill(): Promise<void>;
37
43
  }>;
38
44
  stop(): Promise<void>;
39
45
  kill(): Promise<void>;
@@ -229,7 +235,7 @@ export class MicrosandboxRuntimeService {
229
235
  }> {
230
236
  const image = input.image ?? process.env.MICROSANDBOX_IMAGE ?? "python:3.11-slim";
231
237
  const cpus = input.cpus ?? Number(process.env.MICROSANDBOX_CPUS ?? "1");
232
- const memoryMib = input.memoryMib ?? Number(process.env.MICROSANDBOX_MEMORY ?? "512");
238
+ const memoryMib = input.memoryMib ?? Number(process.env.MICROSANDBOX_MEMORY ?? "1024");
233
239
 
234
240
  await this.ensureNamedVolumes(input.volumes);
235
241
 
@@ -638,7 +644,9 @@ export class MicrosandboxRuntimeService {
638
644
 
639
645
  async execCommand(input: ShellExecInput): Promise<{ stdout: string; stderr: string; exitCode: number }> {
640
646
  const native = await this.getOrEnsureNative(input.sandboxName);
641
- const output = await native.shell(input.command);
647
+ const output = input.timeoutMs
648
+ ? await this.runShellWithTimeout(native, input.command, input.timeoutMs)
649
+ : await native.shell(input.command);
642
650
 
643
651
  return {
644
652
  stdout: output.stdout(),
@@ -647,6 +655,27 @@ export class MicrosandboxRuntimeService {
647
655
  };
648
656
  }
649
657
 
658
+ /**
659
+ * Run a shell command with a hard timeout by streaming it and killing the
660
+ * handle once `timeoutMs` elapses, preserving the same shell semantics as
661
+ * {@link RuntimeNative.shell} (which has no timeout knob).
662
+ */
663
+ private async runShellWithTimeout(
664
+ native: RuntimeNative,
665
+ command: string,
666
+ timeoutMs: number
667
+ ): Promise<ExecOutputLike> {
668
+ const handle = await native.shellStream(command);
669
+ const timer = setTimeout(() => {
670
+ void handle.kill().catch(() => {});
671
+ }, timeoutMs);
672
+ try {
673
+ return await handle.collect();
674
+ } finally {
675
+ clearTimeout(timer);
676
+ }
677
+ }
678
+
650
679
  async getSandboxLogs(
651
680
  name: string,
652
681
  opts: SandboxLogsInput
@@ -108,7 +108,7 @@ function extractConfig(configJson: string): {
108
108
  return {
109
109
  image,
110
110
  cpus: rawResources?.cpus ?? raw.cpus ?? 1,
111
- memoryMib: rawResources?.memory_mib ?? rawResources?.memoryMib ?? raw.memory_mib ?? raw.memoryMib ?? 512,
111
+ memoryMib: rawResources?.memory_mib ?? rawResources?.memoryMib ?? raw.memory_mib ?? raw.memoryMib ?? 1024,
112
112
  env,
113
113
  mounts: Array.isArray(raw.mounts) ? raw.mounts : [],
114
114
  entrypoint,
@@ -435,7 +435,7 @@ export class SandboxRegistry {
435
435
  let native: unknown;
436
436
  const image = (createOptions?.image as string) ?? DEFAULT_IMAGE;
437
437
  const cpus = (createOptions?.cpus as number) ?? 1;
438
- const memoryMib = (createOptions?.memoryMib as number) ?? 512;
438
+ const memoryMib = (createOptions?.memoryMib as number) ?? 1024;
439
439
  const env = (createOptions?.env as Record<string, string>) ?? undefined;
440
440
 
441
441
  let originalConfig: ReturnType<typeof extractConfig>;