Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aa978d6
Scaffold, core contracts and run pipeline
mths-mcfs Nov 15, 2026
b230183
Fix four seam defects found in adversarial review
mths-mcfs Nov 15, 2026
7c10018
Add the QuickJS-ng WASM sandbox
mths-mcfs Nov 15, 2026
131c8a2
Fix the sandbox on macOS and Windows
mths-mcfs Nov 15, 2026
0b0d85a
Bound reply buffering, fail closed without a permission model, honor …
mths-mcfs Nov 15, 2026
81756db
Ignore the probe's connection reset in the permission-model test
mths-mcfs Nov 15, 2026
2dec002
CI: run once per push and cancel superseded PR runs
mths-mcfs Nov 19, 2026
c9d30b2
Merge ci-speed/scaffold into ci-speed/sandbox
mths-mcfs Nov 19, 2026
535304b
fix(review): round 1
mths-mcfs Nov 19, 2026
3cba6ae
fix(review): round 2
mths-mcfs Nov 19, 2026
9824d9d
Refuse UNC workspace roots and bound edit preparation in the memory r…
mths-mcfs Nov 19, 2026
c61bedd
Refuse UNC session roots under a fixed root and count diff rows exactly
mths-mcfs Nov 19, 2026
7396009
fix(review): round 1
mths-mcfs Nov 19, 2026
fc6026b
fix(review): round 2
mths-mcfs Nov 19, 2026
6847074
Merge feat/scaffold (reviewed #1) into feat/sandbox
mths-mcfs Nov 19, 2026
deacf57
Merge main (squashed #1) into feat/sandbox
mths-mcfs Nov 19, 2026
147b193
fix(review): round 1
mths-mcfs Nov 19, 2026
c8d1f1c
fix(review): round 2
mths-mcfs Nov 19, 2026
1c77dbf
Close the startup grants, deadline and out-of-memory classes at the c…
mths-mcfs Nov 19, 2026
926652e
fix(review): enforce sandbox read grants and deadlines
mths-mcfs Nov 20, 2026
74d27cf
fix(review): round 1
mths-mcfs Nov 20, 2026
b34bf5f
fix(review): remove the unused child output queue limit
mths-mcfs Nov 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"bench": "node -e \"console.log('bench: not implemented yet')\""
},
"dependencies": {
"@jitl/quickjs-ng-wasmfile-release-sync": "^0.32.0",
"@modelcontextprotocol/sdk": "^1.32.2",
"quickjs-emscripten-core": "^0.32.0",
"zod": "^4.6.7"
},
"devDependencies": {
Expand Down
693 changes: 693 additions & 0 deletions src/sandbox/child.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/sandbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { QuickJsSandbox, DEFAULT_SANDBOX_MEMORY_BYTES, type QuickJsSandboxOptions } from './quickjs-sandbox.js';
94 changes: 94 additions & 0 deletions src/sandbox/protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Wire protocol between the sandbox parent (server process) and the sandbox
* child process. Messages are newline-delimited JSON: parent → child on the
* child's stdin, child → parent on the child's stdout.
*
* Each side validates what it receives. This file holds the parent side: the
* schemas of child messages (the child is treated as hostile) and the types
* of the messages the parent sends. The child (`child.ts`) has no runtime
* import of this file, so it can run from source with Node type stripping.
*/
import { z } from 'zod';
import type { CallReply } from '../core/types.js';

/**
* Exit code of a child whose engine ran out of memory: it exits from inside the refused memory growth, before any
* more script code runs, so no `done` message comes. Must equal `OOM_EXIT_CODE` in child.ts.
*/
export const OOM_EXIT_CODE = 86;

/** Limits of one run, sent to the child in the `start` message. */
export interface SandboxLimits {
/** Ceiling of the WASM memory of the run, the memory the QuickJS build allocates up front included. */
readonly memoryBytes: number;
/** QuickJS stack limit (`runtime.setMaxStackSize`). */
readonly stackBytes: number;
/** Maximum binding calls per run; later calls reject inside the VM. */
readonly maxCalls: number;
/** Maximum total console bytes per run; later lines are dropped. */
readonly maxLogBytes: number;
/** Maximum length of one console line, in characters. */
readonly maxLogLineChars: number;
/** Maximum length of the JSON-serialized return value, in characters. */
readonly maxResultChars: number;
/** Maximum length of the JSON-serialized arguments of one call, in characters. */
readonly maxArgsChars: number;
}

/** First message of a run: what to execute and with which limits. */
export interface StartMessage {
readonly type: 'start';
readonly code: string;
/** Wall-clock budget left for the script when the message was sent. */
readonly timeoutMs: number;
readonly limits: SandboxLimits;
}

/** Answer to one binding call. */
export interface ReplyMessage {
readonly type: 'reply';
readonly id: number;
readonly reply: CallReply;
}

export type ParentMessage = StartMessage | ReplyMessage;

/** Longest tool name a child may send. Real names are at most 5 characters. */
export const MAX_TOOL_NAME_CHARS = 64;

const ScriptErrorSchema = z.strictObject({
name: z.string().max(1_000),
message: z.string().max(100_000),
stack: z.string().max(100_000).optional(),
});

/**
* Schema of child → parent messages, built for the limits of one run so that
* every string field is bounded.
*/
export function childMessageSchema(limits: SandboxLimits) {
return z.discriminatedUnion('type', [
z.strictObject({
type: z.literal('call'),
id: z.number().int().min(1),
tool: z.string().max(MAX_TOOL_NAME_CHARS),
/** JSON text of the arguments. */
args: z.string().max(limits.maxArgsChars),
}),
z.strictObject({
type: z.literal('log'),
level: z.enum(['log', 'info', 'warn', 'error']),
text: z.string().max(limits.maxLogLineChars + 200),
}),
z.strictObject({
type: z.literal('done'),
outcome: z.discriminatedUnion('ok', [
/** `value` is the JSON text of the return value. */
z.strictObject({ ok: z.literal(true), value: z.string().max(limits.maxResultChars) }),
z.strictObject({ ok: z.literal(false), kind: z.enum(['error', 'timeout', 'oom']), error: ScriptErrorSchema }),
]),
}),
]);
}

export type ChildMessage = z.infer<ReturnType<typeof childMessageSchema>>;
Loading
Loading