@hmharness/sandbox 0.8.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.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/sandbox.d.ts +53 -0
- package/dist/sandbox.js +108 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './sandbox.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./sandbox.js";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Permission tiers - the Codex/OpenAI three-tier model, plus the extension
|
|
2
|
+
* tiers the V2 blueprint names for device/network/desktop capabilities. */
|
|
3
|
+
export type SandboxTier = 'READ_ONLY' | 'WORKSPACE_WRITE' | 'FULL_ACCESS';
|
|
4
|
+
export interface SandboxSpec {
|
|
5
|
+
/** Existing empty-or-new directory; omitted -> a fresh temp dir. */
|
|
6
|
+
dir?: string;
|
|
7
|
+
tier?: SandboxTier;
|
|
8
|
+
}
|
|
9
|
+
export interface SandboxSession {
|
|
10
|
+
id: string;
|
|
11
|
+
dir: string;
|
|
12
|
+
tier: SandboxTier;
|
|
13
|
+
}
|
|
14
|
+
export interface ExecSpec {
|
|
15
|
+
command: string;
|
|
16
|
+
args?: string[];
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ExecResult {
|
|
20
|
+
exitCode: number;
|
|
21
|
+
stdout: string;
|
|
22
|
+
stderr: string;
|
|
23
|
+
}
|
|
24
|
+
export interface SnapshotRef {
|
|
25
|
+
id: string;
|
|
26
|
+
/** git commit sha */
|
|
27
|
+
sha: string;
|
|
28
|
+
label?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface WorkspaceDiff {
|
|
31
|
+
/** unified diff of uncommitted changes ('' when clean) */
|
|
32
|
+
patch: string;
|
|
33
|
+
files: string[];
|
|
34
|
+
}
|
|
35
|
+
export declare class SandboxDenied extends Error {
|
|
36
|
+
constructor(action: string, tier: SandboxTier);
|
|
37
|
+
}
|
|
38
|
+
export declare const sandbox: {
|
|
39
|
+
/** Create an isolated workspace with a fresh git repo (snapshots need it). */
|
|
40
|
+
create(spec?: SandboxSpec): Promise<SandboxSession>;
|
|
41
|
+
/** Run a command INSIDE the sandbox (execFile, no shell, cwd = sandbox). */
|
|
42
|
+
exec(s: SandboxSession, spec: ExecSpec): Promise<ExecResult>;
|
|
43
|
+
read(s: SandboxSession, path: string): Promise<string>;
|
|
44
|
+
write(s: SandboxSession, path: string, data: string): Promise<void>;
|
|
45
|
+
list(s: SandboxSession): Promise<string[]>;
|
|
46
|
+
/** Snapshot = commit everything (incl. untracked) - restore is exact. */
|
|
47
|
+
snapshot(s: SandboxSession, label?: string): Promise<SnapshotRef>;
|
|
48
|
+
/** Restore = hard reset + clean: the sandbox becomes byte-identical to the snapshot. */
|
|
49
|
+
restore(s: SandboxSession, ref: SnapshotRef): Promise<void>;
|
|
50
|
+
/** Unified diff of uncommitted changes (workspace drift since last snapshot). */
|
|
51
|
+
diff(s: SandboxSession): Promise<WorkspaceDiff>;
|
|
52
|
+
destroy(s: SandboxSession): Promise<void>;
|
|
53
|
+
};
|
package/dist/sandbox.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/sandbox - isolated workspace runtime (V2 blueprint M3).
|
|
3
|
+
*
|
|
4
|
+
* The ownership split (ADR-0001, after the OpenAI Agents SDK boundary):
|
|
5
|
+
* the RUNTIME owns agent turns/approvals/tracing; the SANDBOX owns the
|
|
6
|
+
* workspace, its processes and its state. Snapshots are git commits inside
|
|
7
|
+
* the sandbox dir - restore is a hard reset + clean, so a rolled-back
|
|
8
|
+
* sandbox is byte-identical to the snapshot (tests assert exactly that).
|
|
9
|
+
* Everything executes execFile-style (no shell strings, shellgate doctrine).
|
|
10
|
+
*/
|
|
11
|
+
import { execFile } from 'node:child_process';
|
|
12
|
+
import { mkdtemp, rm, readFile, writeFile, readdir } from 'node:fs/promises';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { promisify } from 'node:util';
|
|
16
|
+
const execCb = promisify(execFile);
|
|
17
|
+
export class SandboxDenied extends Error {
|
|
18
|
+
constructor(action, tier) {
|
|
19
|
+
super(`sandbox: ${action} denied at tier ${tier}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/* git binary resolution: .cmd shims can't execFile-spawn on modern Node */
|
|
23
|
+
const GIT_CANDIDATES = ['git', 'C:\\Program Files\\Git\\cmd\\git.exe', 'C:\\Program Files (x86)\\Git\\cmd\\git.exe'];
|
|
24
|
+
let gitBin;
|
|
25
|
+
async function git(args, opts) {
|
|
26
|
+
let lastErr;
|
|
27
|
+
for (const exe of [gitBin, ...GIT_CANDIDATES].filter(Boolean)) {
|
|
28
|
+
try {
|
|
29
|
+
const r = await execCb(exe, args, { cwd: opts.cwd, timeout: opts.timeoutMs ?? 30_000, windowsHide: true, maxBuffer: 8 * 1024 * 1024 });
|
|
30
|
+
gitBin = exe;
|
|
31
|
+
return { stdout: String(r.stdout ?? ''), stderr: String(r.stderr ?? '') };
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
const e = err;
|
|
35
|
+
if (typeof e.code === 'number' && (opts.ok ?? [0]).includes(e.code)) {
|
|
36
|
+
gitBin = exe;
|
|
37
|
+
return { stdout: String(e.stdout ?? ''), stderr: String(e.stderr ?? '') };
|
|
38
|
+
}
|
|
39
|
+
lastErr = err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw lastErr ?? new Error('git not found');
|
|
43
|
+
}
|
|
44
|
+
let seq = 0;
|
|
45
|
+
export const sandbox = {
|
|
46
|
+
/** Create an isolated workspace with a fresh git repo (snapshots need it). */
|
|
47
|
+
async create(spec = {}) {
|
|
48
|
+
const dir = spec.dir ?? await mkdtemp(join(tmpdir(), 'hmh-sbx-'));
|
|
49
|
+
const session = { id: `sbx_${Date.now().toString(36)}_${++seq}`, dir, tier: spec.tier ?? 'WORKSPACE_WRITE' };
|
|
50
|
+
await git(['init', '-q'], { cwd: dir });
|
|
51
|
+
await git(['config', 'user.email', 'sandbox@hmharness.local'], { cwd: dir });
|
|
52
|
+
await git(['config', 'user.name', 'hmh-sandbox'], { cwd: dir });
|
|
53
|
+
return session;
|
|
54
|
+
},
|
|
55
|
+
/** Run a command INSIDE the sandbox (execFile, no shell, cwd = sandbox). */
|
|
56
|
+
async exec(s, spec) {
|
|
57
|
+
if (s.tier === 'READ_ONLY')
|
|
58
|
+
throw new SandboxDenied('exec', s.tier);
|
|
59
|
+
try {
|
|
60
|
+
const r = await execCb(spec.command, spec.args ?? [], { cwd: s.dir, timeout: spec.timeoutMs ?? 120_000, windowsHide: true, maxBuffer: 8 * 1024 * 1024 });
|
|
61
|
+
return { exitCode: 0, stdout: String(r.stdout ?? ''), stderr: String(r.stderr ?? '') };
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
const e = err;
|
|
65
|
+
return { exitCode: typeof e.code === 'number' ? e.code : -1, stdout: String(e.stdout ?? ''), stderr: String(e.stderr ?? '') + (e.killed ? ' (timed out)' : '') };
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
async read(s, path) {
|
|
69
|
+
return readFile(join(s.dir, path), 'utf8');
|
|
70
|
+
},
|
|
71
|
+
async write(s, path, data) {
|
|
72
|
+
if (s.tier === 'READ_ONLY')
|
|
73
|
+
throw new SandboxDenied('write', s.tier);
|
|
74
|
+
await writeFile(join(s.dir, path), data, 'utf8');
|
|
75
|
+
},
|
|
76
|
+
async list(s) {
|
|
77
|
+
return readdir(s.dir);
|
|
78
|
+
},
|
|
79
|
+
/** Snapshot = commit everything (incl. untracked) - restore is exact. */
|
|
80
|
+
async snapshot(s, label) {
|
|
81
|
+
await git(['add', '-A'], { cwd: s.dir });
|
|
82
|
+
// an empty repo with nothing staged still needs a root commit: --allow-empty
|
|
83
|
+
const r = await git(['commit', '-q', '--allow-empty', '-m', `snapshot${label ? ': ' + label : ''}`], { cwd: s.dir });
|
|
84
|
+
void r;
|
|
85
|
+
const sha = (await git(['rev-parse', 'HEAD'], { cwd: s.dir })).stdout.trim();
|
|
86
|
+
return { id: sha.slice(0, 10), sha, label };
|
|
87
|
+
},
|
|
88
|
+
/** Restore = hard reset + clean: the sandbox becomes byte-identical to the snapshot. */
|
|
89
|
+
async restore(s, ref) {
|
|
90
|
+
if (s.tier === 'READ_ONLY')
|
|
91
|
+
throw new SandboxDenied('restore', s.tier);
|
|
92
|
+
await git(['reset', '--hard', ref.sha], { cwd: s.dir }); // safe: inside the sandbox repo only
|
|
93
|
+
await git(['clean', '-fdq'], { cwd: s.dir });
|
|
94
|
+
},
|
|
95
|
+
/** Unified diff of uncommitted changes (workspace drift since last snapshot). */
|
|
96
|
+
async diff(s) {
|
|
97
|
+
await git(['add', '-A'], { cwd: s.dir });
|
|
98
|
+
const r = await git(['diff', '--cached', '--stat', '--name-only'], { cwd: s.dir, ok: [0, 1] });
|
|
99
|
+
const files = r.stdout.trim().split('\n').filter(Boolean);
|
|
100
|
+
if (files.length === 0)
|
|
101
|
+
return { patch: '', files: [] };
|
|
102
|
+
const full = await git(['diff', '--cached'], { cwd: s.dir, ok: [0, 1] });
|
|
103
|
+
return { patch: full.stdout, files };
|
|
104
|
+
},
|
|
105
|
+
async destroy(s) {
|
|
106
|
+
await rm(s.dir, { recursive: true, force: true });
|
|
107
|
+
},
|
|
108
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmharness/sandbox",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "hmharness sandbox runtime (V2 blueprint M3): isolated workspaces with create/exec/read/write/snapshot/restore/diff/destroy, tiered permissions (READ_ONLY / WORKSPACE_WRITE / FULL_ACCESS), git-backed snapshots. Runtime owns agent turns; the sandbox owns workspace/process state (ADR-0001).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/swsgbl/hmharness.git"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
}
|
|
28
|
+
}
|