@zenera/sandbox 1.3.37 → 1.3.40

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/README.md CHANGED
@@ -1,3 +1,25 @@
1
+ <div align="center">
2
+ <img
3
+ src="https://raw.githubusercontent.com/zenera-ai/ZeneraNeo/main/docs/imgs/zenera-logo-with-media-queries.svg"
4
+ alt="Zenera Logo"
5
+ width="20"
6
+ />
7
+ <div>Powered By Zenera</div>
8
+ </div>
9
+ <br>
10
+
11
+ <div align="center">
12
+ <img src="https://raw.githubusercontent.com/zenera-ai/ZeneraNeo/main/docs/imgs/banner-sandbox-with-media-queries.svg" alt="ZENERA SANDBOX" width="1008">
13
+
14
+ [![npm](https://img.shields.io/npm/v/@zenera/sandbox.svg)](https://www.npmjs.com/package/@zenera/sandbox)
15
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/zenera-ai/ZeneraNeo/blob/main/LICENSE)
16
+ [![Node](https://img.shields.io/badge/node-%E2%89%A524-brightgreen.svg)](https://nodejs.org)
17
+ [![CI](https://github.com/zenera-ai/ZeneraNeo/actions/workflows/ci.yml/badge.svg)](https://github.com/zenera-ai/ZeneraNeo/actions/workflows/ci.yml)
18
+
19
+ [The contract](#the-contract) · [The image](#the-image) · [Implementations](#implementations) · [Testing](#testing-an-implementation)
20
+
21
+ </div>
22
+
1
23
  # @zenera/sandbox
2
24
 
3
25
  Where an agent's commands run, and the files those commands see. One
package/dist/base.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import type { PrepareOptions } from './contract.ts';
2
2
  import type { Runner } from './process.ts';
3
3
  export declare const SANDBOX_BASE_REPO = "ghcr.io/zenera-ai/zenera-sandbox";
4
+ /**
5
+ * The Playwright whose browser the base carries, for `zen page`: the version @zenera/browser pins
6
+ * (a test keeps them equal), since a Playwright drives only the browser build it was made for.
7
+ */
8
+ export declare const PLAYWRIGHT_VERSION = "1.65.1";
9
+ /** Where the base keeps that browser; an ENV of the image, so every command finds it. */
10
+ export declare const PLAYWRIGHT_BROWSERS_PATH = "/opt/ms-playwright";
4
11
  /**
5
12
  * Shell that fails unless `python` and `python3` are both on PATH and are one interpreter:
6
13
  * a script's `#!/usr/bin/env python` must run where `python3 -m pip install` put its packages.
package/dist/base.js CHANGED
@@ -18,6 +18,13 @@ import { join } from 'node:path';
18
18
  export const SANDBOX_BASE_REPO = 'ghcr.io/zenera-ai/zenera-sandbox';
19
19
  /** `FROM [--platform=…] ghcr.io/zenera-ai/zenera-sandbox:<version>`: the version is the tag. */
20
20
  const BASE_FROM = /^\s*FROM\s+(?:--platform=\S+\s+)?ghcr\.io\/zenera-ai\/zenera-sandbox:([^\s@]+)/gim;
21
+ /**
22
+ * The Playwright whose browser the base carries, for `zen page`: the version @zenera/browser pins
23
+ * (a test keeps them equal), since a Playwright drives only the browser build it was made for.
24
+ */
25
+ export const PLAYWRIGHT_VERSION = '1.65.1';
26
+ /** Where the base keeps that browser; an ENV of the image, so every command finds it. */
27
+ export const PLAYWRIGHT_BROWSERS_PATH = '/opt/ms-playwright';
21
28
  const REAL_EXECUTABLE = `-c 'import os, sys; print(os.path.realpath(sys.executable))'`;
22
29
  /**
23
30
  * Shell that fails unless `python` and `python3` are both on PATH and are one interpreter:
@@ -94,6 +101,15 @@ RUN npm install -g \\
94
101
  "@types/react@^19.2.18" \\
95
102
  "@types/react-dom@^19.2.3"
96
103
 
104
+ # A browser for \`zen page\` (the html-page skill's check: a page's errors, screenshots, clicks):
105
+ # Chromium's headless shell and the libraries it needs, for the Playwright @zenera/browser pins, and
106
+ # fonts so text and emoji draw as on a desktop.
107
+ ENV PLAYWRIGHT_BROWSERS_PATH=${PLAYWRIGHT_BROWSERS_PATH}
108
+ RUN npx -y "playwright-core@${PLAYWRIGHT_VERSION}" install --with-deps --only-shell chromium \\
109
+ && apt-get install -y --no-install-recommends \\
110
+ fonts-liberation fonts-noto-core fonts-noto-color-emoji \\
111
+ && rm -rf /var/lib/apt/lists/* /root/.npm
112
+
97
113
  # Libraries agents' scripts commonly reach for: HTTP clients, JSON Schema validation, YAML,
98
114
  # dates, HTML. \`python\` and \`python3\` must be one interpreter, the one pip installs into.
99
115
  # Global npm packages resolve for require() through NODE_PATH; ESM import does not read it
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './fs.ts';
5
5
  export * from './image.ts';
6
6
  export * from './in-memory-fs.ts';
7
7
  export * from './limits.ts';
8
+ export * from './local.ts';
8
9
  export * from './mounts.ts';
9
10
  export * from './podman.ts';
10
11
  export * from './process.ts';
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./fs.js";
8
8
  export * from "./image.js";
9
9
  export * from "./in-memory-fs.js";
10
10
  export * from "./limits.js";
11
+ export * from "./local.js";
11
12
  export * from "./mounts.js";
12
13
  export * from "./podman.js";
13
14
  export * from "./process.js";
@@ -0,0 +1,31 @@
1
+ import { type ExecOptions, type ExecResult, type JobOutput, type JobStopped, type Sandbox } from './contract.ts';
2
+ import { type FsDriver } from './fs.ts';
3
+ import { type Runner } from './process.ts';
4
+ export interface ProcessSandboxOptions {
5
+ /** where commands run when they name no cwd, and what a relative cwd is under */
6
+ root: string;
7
+ /** the whole environment of every command; this process's when unset */
8
+ env?: NodeJS.ProcessEnv;
9
+ /** directories put in front of a path-like variable (`PYTHONPATH`) for every command */
10
+ execPaths?: Record<string, readonly string[]>;
11
+ /** seconds a command may take when it says nothing; 120 */
12
+ timeout?: number;
13
+ /** how processes are spawned - the seam tests replace */
14
+ run?: Runner;
15
+ }
16
+ export declare class ProcessSandbox implements Sandbox {
17
+ #private;
18
+ readonly name = "process";
19
+ readonly fs: FsDriver;
20
+ readonly started = true;
21
+ constructor(opts: ProcessSandboxOptions);
22
+ prepare(): Promise<void>;
23
+ start(): Promise<void>;
24
+ exec(command: string, opts?: ExecOptions): Promise<ExecResult>;
25
+ startJob(): Promise<string>;
26
+ readJob(id: string): Promise<JobOutput>;
27
+ stopJob(id: string): Promise<JobStopped>;
28
+ /** Stops every command still running. */
29
+ close(): Promise<void>;
30
+ }
31
+ //# sourceMappingURL=local.d.ts.map
package/dist/local.js ADDED
@@ -0,0 +1,67 @@
1
+ import { isAbsolute, resolve } from 'node:path';
2
+ import { SandboxError, } from "./contract.js";
3
+ import { localFs } from "./fs.js";
4
+ import { MAX_TIMEOUT_MS, runProcess } from "./process.js";
5
+ const DEFAULT_TIMEOUT_SECONDS = 120;
6
+ export class ProcessSandbox {
7
+ name = 'process';
8
+ fs = localFs;
9
+ started = true;
10
+ #opts;
11
+ #closing = new AbortController();
12
+ constructor(opts) {
13
+ this.#opts = opts;
14
+ }
15
+ async prepare() { }
16
+ async start() { }
17
+ async exec(command, opts = {}) {
18
+ const cwd = opts.cwd
19
+ ? isAbsolute(opts.cwd)
20
+ ? opts.cwd
21
+ : resolve(this.#opts.root, opts.cwd)
22
+ : this.#opts.root;
23
+ const signal = opts.signal
24
+ ? AbortSignal.any([opts.signal, this.#closing.signal])
25
+ : this.#closing.signal;
26
+ const startedAt = Date.now();
27
+ const quoted = `'${cwd.replace(/'/g, `'\\''`)}'`;
28
+ const res = await (this.#opts.run ?? runProcess)('/bin/sh', ['-s'], {
29
+ input: `cd ${quoted} || exit 1\n${command}`,
30
+ timeoutMs: Math.min((opts.timeout ?? this.#opts.timeout ?? DEFAULT_TIMEOUT_SECONDS) * 1000, MAX_TIMEOUT_MS),
31
+ signal,
32
+ env: this.#env(),
33
+ group: true,
34
+ ...(opts.maxBytes !== undefined ? { maxBytes: opts.maxBytes } : {}),
35
+ });
36
+ return {
37
+ exit_code: res.code,
38
+ stdout: res.stdout,
39
+ stderr: res.stderr,
40
+ truncated: res.truncated || undefined,
41
+ timed_out: res.timedOut || undefined,
42
+ duration_ms: Date.now() - startedAt,
43
+ };
44
+ }
45
+ #env() {
46
+ const env = { ...(this.#opts.env ?? process.env) };
47
+ for (const [name, dirs] of Object.entries(this.#opts.execPaths ?? {})) {
48
+ if (dirs.length)
49
+ env[name] = [...dirs, env[name]].filter(Boolean).join(':');
50
+ }
51
+ return env;
52
+ }
53
+ async startJob() {
54
+ throw new SandboxError('background jobs are not run here', 'run the command itself');
55
+ }
56
+ async readJob(id) {
57
+ throw new SandboxError(`no such job: ${id}`);
58
+ }
59
+ async stopJob(id) {
60
+ throw new SandboxError(`no such job: ${id}`);
61
+ }
62
+ /** Stops every command still running. */
63
+ async close() {
64
+ this.#closing.abort();
65
+ }
66
+ }
67
+ //# sourceMappingURL=local.js.map
package/dist/process.d.ts CHANGED
@@ -9,6 +9,11 @@ export interface ProcOptions {
9
9
  maxBytes?: number;
10
10
  /** the child's whole environment; this process's when unset */
11
11
  env?: NodeJS.ProcessEnv;
12
+ /**
13
+ * Run in a process group of its own and stop the whole group: what a plain shell started
14
+ * would otherwise outlive it and hold its output open.
15
+ */
16
+ group?: boolean;
12
17
  }
13
18
  export interface ProcResult {
14
19
  code: number;
package/dist/process.js CHANGED
@@ -17,7 +17,20 @@ export const runProcess = (bin, args, opts = {}) => new Promise((settle, fail) =
17
17
  shell: false,
18
18
  stdio: ['pipe', 'pipe', 'pipe'],
19
19
  env: opts.env,
20
+ detached: opts.group === true,
20
21
  });
22
+ const signal = (sig) => {
23
+ if (opts.group && child.pid !== undefined) {
24
+ try {
25
+ process.kill(-child.pid, sig);
26
+ return;
27
+ }
28
+ catch {
29
+ // The group is gone already, or not ours to signal: stop the child itself.
30
+ }
31
+ }
32
+ child.kill(sig);
33
+ };
21
34
  const keep = (into, size, chunk) => {
22
35
  const room = max - size;
23
36
  if (room <= 0) {
@@ -38,8 +51,8 @@ export const runProcess = (bin, args, opts = {}) => new Promise((settle, fail) =
38
51
  // one that ignores it cannot hold the turn open forever.
39
52
  let hard;
40
53
  const stop = () => {
41
- child.kill('SIGTERM');
42
- hard = setTimeout(() => child.kill('SIGKILL'), 2000);
54
+ signal('SIGTERM');
55
+ hard = setTimeout(() => signal('SIGKILL'), 2000);
43
56
  hard.unref();
44
57
  };
45
58
  const timer = opts.timeoutMs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenera/sandbox",
3
- "version": "1.3.37",
3
+ "version": "1.3.40",
4
4
  "description": "Where an agent's commands run and the files they see: the Sandbox contract, podman and bubblewrap implementations, filesystem drivers and a contract test suite for other implementations.",
5
5
  "keywords": [
6
6
  "sandbox",