@neurouter/executor 0.0.0-stage → 0.1.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.
Files changed (57) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +10 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/client.d.ts +57 -0
  5. package/dist/client.d.ts.map +1 -0
  6. package/dist/client.js +306 -0
  7. package/dist/client.js.map +1 -0
  8. package/dist/convert.d.ts +10 -0
  9. package/dist/convert.d.ts.map +1 -0
  10. package/dist/convert.js +57 -0
  11. package/dist/convert.js.map +1 -0
  12. package/dist/frames.d.ts +268 -0
  13. package/dist/frames.d.ts.map +1 -0
  14. package/dist/frames.js +139 -0
  15. package/dist/frames.js.map +1 -0
  16. package/dist/index.d.ts +12 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +12 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/local-executor.d.ts +17 -0
  21. package/dist/local-executor.d.ts.map +1 -0
  22. package/dist/local-executor.js +25 -0
  23. package/dist/local-executor.js.map +1 -0
  24. package/dist/logger.d.ts +5 -0
  25. package/dist/logger.d.ts.map +1 -0
  26. package/dist/logger.js +22 -0
  27. package/dist/logger.js.map +1 -0
  28. package/dist/main.d.ts +3 -0
  29. package/dist/main.d.ts.map +1 -0
  30. package/dist/main.js +56 -0
  31. package/dist/main.js.map +1 -0
  32. package/dist/monty-info.d.ts +11 -0
  33. package/dist/monty-info.d.ts.map +1 -0
  34. package/dist/monty-info.js +36 -0
  35. package/dist/monty-info.js.map +1 -0
  36. package/dist/monty-runner.d.ts +41 -0
  37. package/dist/monty-runner.d.ts.map +1 -0
  38. package/dist/monty-runner.js +390 -0
  39. package/dist/monty-runner.js.map +1 -0
  40. package/dist/server.d.ts +30 -0
  41. package/dist/server.d.ts.map +1 -0
  42. package/dist/server.js +182 -0
  43. package/dist/server.js.map +1 -0
  44. package/dist/spawn.d.ts +48 -0
  45. package/dist/spawn.d.ts.map +1 -0
  46. package/dist/spawn.js +124 -0
  47. package/dist/spawn.js.map +1 -0
  48. package/dist/types.d.ts +121 -0
  49. package/dist/types.d.ts.map +1 -0
  50. package/dist/types.js +17 -0
  51. package/dist/types.js.map +1 -0
  52. package/dist/version.d.ts +2 -0
  53. package/dist/version.d.ts.map +1 -0
  54. package/dist/version.js +2 -0
  55. package/dist/version.js.map +1 -0
  56. package/package.json +53 -4
  57. package/README.md +0 -3
package/dist/client.js ADDED
@@ -0,0 +1,306 @@
1
+ import net from 'node:net';
2
+ import { encodeFrame, FrameDecoder, parseFrame, ToServerFrame } from './frames.js';
3
+ import { silentLogger } from './logger.js';
4
+ import { PROTOCOL_VERSION } from './types.js';
5
+ /** Parses `unix:///path.sock` or `tcp://host:port` (`NEUROUTER_EXECUTOR_URL`). */
6
+ export function parseExecutorUrl(url) {
7
+ if (url.startsWith('unix://'))
8
+ return { socketPath: url.slice('unix://'.length) };
9
+ const m = /^tcp:\/\/([^:/]+):(\d+)\/?$/.exec(url);
10
+ if (m)
11
+ return { host: m[1], port: Number(m[2]) };
12
+ throw new Error(`invalid executor URL ${url} (expected unix:///path.sock or tcp://host:port)`);
13
+ }
14
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
15
+ /**
16
+ * Server-side client of the executor (§10.6). One connection, multiplexed by `runId`; executor frames
17
+ * are untrusted: every frame is schema-checked and each `call` must match its run's capability and
18
+ * allowed function list. A lost connection fails in-flight runs with status `crashed`; the next call
19
+ * reconnects.
20
+ */
21
+ export class SocketExecutor {
22
+ opts;
23
+ kind = 'socket';
24
+ sock = null;
25
+ connecting = null;
26
+ ack = null;
27
+ runs = new Map();
28
+ pingTimer = null;
29
+ missedPongs = 0;
30
+ closed = false;
31
+ lastError = null;
32
+ log;
33
+ constructor(opts) {
34
+ this.opts = opts;
35
+ this.log = opts.logger ?? silentLogger;
36
+ }
37
+ get connected() {
38
+ return !!this.sock && !!this.ack && !this.sock.destroyed;
39
+ }
40
+ get helloAck() {
41
+ return this.ack;
42
+ }
43
+ /** Connects (retrying until `timeoutMs`). */
44
+ async connect(timeoutMs = this.opts.connectTimeoutMs ?? 5000) {
45
+ if (this.closed)
46
+ throw new Error('executor client closed');
47
+ if (this.connected)
48
+ return;
49
+ this.connecting ??= this.connectLoop(timeoutMs).finally(() => (this.connecting = null));
50
+ return this.connecting;
51
+ }
52
+ async connectLoop(timeoutMs) {
53
+ const deadline = Date.now() + timeoutMs;
54
+ let delay = 50;
55
+ for (;;) {
56
+ try {
57
+ await this.connectOnce();
58
+ return;
59
+ }
60
+ catch (err) {
61
+ this.lastError = err instanceof Error ? err.message : String(err);
62
+ if (this.closed || Date.now() + delay > deadline)
63
+ throw new Error(`executor unavailable: ${this.lastError}`, { cause: err });
64
+ await sleep(delay);
65
+ delay = Math.min(delay * 2, 500);
66
+ }
67
+ }
68
+ }
69
+ connectOnce() {
70
+ return new Promise((resolve, reject) => {
71
+ const addr = this.opts.address;
72
+ const sock = 'socketPath' in addr ? net.createConnection(addr.socketPath) : net.createConnection(addr.port, addr.host);
73
+ const decoder = new FrameDecoder();
74
+ let settled = false;
75
+ const settle = (err) => {
76
+ if (settled)
77
+ return;
78
+ settled = true;
79
+ clearTimeout(timer);
80
+ if (err) {
81
+ sock.destroy();
82
+ reject(err);
83
+ }
84
+ else
85
+ resolve();
86
+ };
87
+ const timer = setTimeout(() => settle(new Error('hello timeout')), 5000);
88
+ sock.once('connect', () => {
89
+ sock.write(encodeFrame({ type: 'hello', protocol: PROTOCOL_VERSION, token: this.opts.token }));
90
+ });
91
+ sock.on('data', (chunk) => {
92
+ let raws;
93
+ try {
94
+ raws = decoder.push(chunk);
95
+ }
96
+ catch (err) {
97
+ return this.protocolError(sock, err);
98
+ }
99
+ for (const raw of raws) {
100
+ let frame;
101
+ try {
102
+ frame = parseFrame(ToServerFrame, raw);
103
+ }
104
+ catch (err) {
105
+ if (!settled)
106
+ return settle(err);
107
+ return this.protocolError(sock, err);
108
+ }
109
+ if (!settled) {
110
+ if (frame.type === 'error')
111
+ return settle(new Error(`executor refused: ${frame.message}`));
112
+ if (frame.type !== 'hello_ack')
113
+ return settle(new Error(`unexpected ${frame.type} before hello_ack`));
114
+ if (this.opts.expectedMontySha256 && frame.montyBinarySha256 !== this.opts.expectedMontySha256) {
115
+ return settle(new Error('executor monty binary checksum mismatch'));
116
+ }
117
+ this.ack = frame;
118
+ this.sock = sock;
119
+ this.startPings(sock);
120
+ this.lastError = null;
121
+ settle();
122
+ continue;
123
+ }
124
+ this.onFrame(sock, frame);
125
+ }
126
+ });
127
+ sock.on('error', (err) => {
128
+ this.lastError = err.message;
129
+ settle(err);
130
+ });
131
+ sock.on('close', () => {
132
+ settle(new Error('connection closed'));
133
+ if (this.sock === sock)
134
+ this.onDisconnect('executor connection lost');
135
+ });
136
+ });
137
+ }
138
+ startPings(sock) {
139
+ this.missedPongs = 0;
140
+ if (this.pingTimer)
141
+ clearInterval(this.pingTimer);
142
+ this.pingTimer = setInterval(() => {
143
+ if (this.missedPongs >= 3) {
144
+ this.log.warn({}, 'executor missed 3 pongs; resetting connection');
145
+ sock.destroy();
146
+ return;
147
+ }
148
+ this.missedPongs++;
149
+ this.write({ type: 'ping', t: Date.now() });
150
+ }, this.opts.pingIntervalMs ?? 10_000);
151
+ this.pingTimer.unref();
152
+ }
153
+ protocolError(sock, err) {
154
+ this.log.warn({ err }, 'executor protocol error');
155
+ const msg = err instanceof Error ? err.message : 'protocol error';
156
+ this.failAll('executor_protocol_error', msg);
157
+ sock.destroy();
158
+ }
159
+ onDisconnect(reason) {
160
+ if (this.pingTimer)
161
+ clearInterval(this.pingTimer);
162
+ this.pingTimer = null;
163
+ this.sock = null;
164
+ this.ack = null;
165
+ this.failAll('executor_disconnected', reason);
166
+ }
167
+ failAll(type, message) {
168
+ for (const [runId, run] of this.runs) {
169
+ this.runs.delete(runId);
170
+ run.resolve({ status: 'crashed', valueBytes: 0, error: { type, message }, stats: { wallMs: Date.now() - run.t0, suspensions: 0, toolCalls: 0 }, stdout: '' });
171
+ }
172
+ }
173
+ write(frame) {
174
+ if (!this.sock || this.sock.destroyed)
175
+ return false;
176
+ this.sock.write(encodeFrame(frame));
177
+ return true;
178
+ }
179
+ onFrame(sock, frame) {
180
+ switch (frame.type) {
181
+ case 'pong':
182
+ this.missedPongs = 0;
183
+ return;
184
+ case 'ping':
185
+ this.write({ type: 'pong', ...(frame.t !== undefined ? { t: frame.t } : {}) });
186
+ return;
187
+ case 'print':
188
+ return; // stdout is delivered with `done`
189
+ case 'error':
190
+ this.log.warn({ message: frame.message }, 'executor reported an error');
191
+ return;
192
+ case 'hello_ack':
193
+ return this.protocolError(sock, new Error('duplicate hello_ack'));
194
+ case 'call':
195
+ void this.onCall(frame);
196
+ return;
197
+ case 'done': {
198
+ const run = this.runs.get(frame.runId);
199
+ if (!run)
200
+ return;
201
+ this.runs.delete(frame.runId);
202
+ const { type: _t, runId: _r, ...result } = frame;
203
+ run.resolve(result);
204
+ return;
205
+ }
206
+ }
207
+ }
208
+ async onCall(frame) {
209
+ const run = this.runs.get(frame.runId);
210
+ const reject = (code, message) => this.write({ type: 'call_result', runId: frame.runId, callId: frame.callId, ok: false, error: { code, message } });
211
+ // defense in depth: the executor only exposes allowed functions, but its frames are untrusted
212
+ if (!run || frame.capability !== run.req.capability) {
213
+ this.log.warn({ runId: frame.runId, fn: frame.fn }, 'executor call rejected: unknown run or capability mismatch');
214
+ return void reject('not_permitted', 'run capability mismatch');
215
+ }
216
+ if (!run.allowed.has(frame.fn)) {
217
+ this.log.warn({ runId: frame.runId, fn: frame.fn }, 'executor call rejected: function not allowed');
218
+ return void reject('unknown_tool', 'Tool not found or not permitted');
219
+ }
220
+ let result;
221
+ try {
222
+ result = await run.host.call(frame.fn, frame.kwargs);
223
+ }
224
+ catch (err) {
225
+ result = { ok: false, error: { code: 'internal', message: err instanceof Error ? err.message : String(err) } };
226
+ }
227
+ if (!this.runs.has(frame.runId))
228
+ return;
229
+ try {
230
+ this.write({ type: 'call_result', runId: frame.runId, callId: frame.callId, ...result });
231
+ }
232
+ catch {
233
+ reject('response_too_large', 'tool result exceeds the executor frame limit');
234
+ }
235
+ }
236
+ async health() {
237
+ try {
238
+ await this.connect(1000);
239
+ }
240
+ catch (err) {
241
+ return { ok: false, montyVersion: 'unknown', poolSize: 0, busy: this.runs.size, detail: err instanceof Error ? err.message : String(err) };
242
+ }
243
+ return { ok: true, montyVersion: this.ack?.montyVersion ?? 'unknown', poolSize: this.ack?.poolMax ?? 0, busy: this.runs.size };
244
+ }
245
+ async run(req, host, signal) {
246
+ const t0 = Date.now();
247
+ const crashed = (type, message) => ({ status: 'crashed', valueBytes: 0, error: { type, message }, stats: { wallMs: Date.now() - t0, suspensions: 0, toolCalls: 0 }, stdout: '' });
248
+ if (signal.aborted)
249
+ return { ...crashed('cancelled', 'cancelled'), status: 'cancelled' };
250
+ try {
251
+ await this.connect();
252
+ }
253
+ catch (err) {
254
+ return crashed('executor_unavailable', err instanceof Error ? err.message : String(err));
255
+ }
256
+ if (this.runs.has(req.runId))
257
+ return crashed('executor_error', `duplicate runId ${req.runId}`);
258
+ const allowed = new Set();
259
+ for (const [ns, spec] of Object.entries(req.modules))
260
+ for (const fn of spec.functions)
261
+ allowed.add(`${ns}.${fn}`);
262
+ return new Promise((resolve) => {
263
+ let fallback = null;
264
+ const onAbort = () => {
265
+ this.write({ type: 'cancel', runId: req.runId, reason: String(signal.reason ?? 'cancelled') });
266
+ // the executor answers `done` (cancelled) after killing the worker; don't wait forever
267
+ fallback = setTimeout(() => {
268
+ if (!this.runs.delete(req.runId))
269
+ return;
270
+ resolve({ ...crashed('cancelled', 'cancelled (executor did not confirm)'), status: 'cancelled' });
271
+ }, 5000);
272
+ };
273
+ this.runs.set(req.runId, {
274
+ req,
275
+ host,
276
+ allowed,
277
+ t0,
278
+ resolve: (r) => {
279
+ signal.removeEventListener('abort', onAbort);
280
+ if (fallback)
281
+ clearTimeout(fallback);
282
+ resolve(r);
283
+ },
284
+ });
285
+ signal.addEventListener('abort', onAbort, { once: true });
286
+ try {
287
+ if (!this.write({ type: 'run', ...req }))
288
+ throw new Error('not connected');
289
+ }
290
+ catch (err) {
291
+ this.runs.delete(req.runId);
292
+ signal.removeEventListener('abort', onAbort);
293
+ resolve(crashed('executor_error', err instanceof Error ? err.message : String(err)));
294
+ }
295
+ });
296
+ }
297
+ async close() {
298
+ this.closed = true;
299
+ this.failAll('executor_unavailable', 'executor client closed');
300
+ if (this.pingTimer)
301
+ clearInterval(this.pingTimer);
302
+ this.sock?.destroy();
303
+ this.sock = null;
304
+ }
305
+ }
306
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAsC,MAAM,aAAa,CAAC;AACvH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAyG,MAAM,YAAY,CAAC;AAuBrJ,kFAAkF;AAClF,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,IAAI,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;IAClF,MAAM,CAAC,GAAG,6BAA6B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClD,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAE,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAClD,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,kDAAkD,CAAC,CAAC;AACjG,CAAC;AAED,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OA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AG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACvF,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,OAAO,CAAC,sBAAsB,EAAE,wBAAwB,CAAC,CAAC;QAC/D,IAAI,IAAI,CAAC,SAAS;YAAE,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Converts values coming out of Monty (Python dicts are `Map`s, sets are `Set`s, big ints may be
3
+ * `bigint`) into plain JSON values. Anything without a JSON form becomes its string representation.
4
+ */
5
+ export declare function toJson(value: unknown, depth?: number): unknown;
6
+ /** Byte length of the compact JSON form (`null` for `undefined`). */
7
+ export declare function jsonBytes(value: unknown): number;
8
+ /** Truncates a UTF-8 string to at most `max` bytes without splitting a code point. */
9
+ export declare function truncateUtf8(s: string, max: number): string;
10
+ //# sourceMappingURL=convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,OAAO,CA8BzD;AAED,qEAAqE;AACrE,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEhD;AAED,sFAAsF;AACtF,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAK3D"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Converts values coming out of Monty (Python dicts are `Map`s, sets are `Set`s, big ints may be
3
+ * `bigint`) into plain JSON values. Anything without a JSON form becomes its string representation.
4
+ */
5
+ export function toJson(value, depth = 0) {
6
+ if (depth > 200)
7
+ return '<nested too deep>';
8
+ if (value === null || value === undefined)
9
+ return null;
10
+ switch (typeof value) {
11
+ case 'string':
12
+ case 'boolean':
13
+ return value;
14
+ case 'number':
15
+ return Number.isFinite(value) ? value : String(value);
16
+ case 'bigint':
17
+ return Number.isSafeInteger(Number(value)) ? Number(value) : value.toString();
18
+ case 'function':
19
+ case 'symbol':
20
+ return String(value);
21
+ }
22
+ if (Array.isArray(value))
23
+ return value.map((v) => toJson(v, depth + 1));
24
+ if (value instanceof Map) {
25
+ const out = {};
26
+ for (const [k, v] of value)
27
+ out[typeof k === 'string' ? k : String(toJson(k, depth + 1))] = toJson(v, depth + 1);
28
+ return out;
29
+ }
30
+ if (value instanceof Set)
31
+ return [...value].map((v) => toJson(v, depth + 1));
32
+ if (value instanceof Date)
33
+ return value.toISOString();
34
+ if (value instanceof Uint8Array)
35
+ return Buffer.from(value).toString('base64');
36
+ if (typeof value === 'object') {
37
+ const out = {};
38
+ for (const [k, v] of Object.entries(value))
39
+ out[k] = toJson(v, depth + 1);
40
+ return out;
41
+ }
42
+ return String(value);
43
+ }
44
+ /** Byte length of the compact JSON form (`null` for `undefined`). */
45
+ export function jsonBytes(value) {
46
+ return Buffer.byteLength(JSON.stringify(value ?? null), 'utf8');
47
+ }
48
+ /** Truncates a UTF-8 string to at most `max` bytes without splitting a code point. */
49
+ export function truncateUtf8(s, max) {
50
+ if (Buffer.byteLength(s, 'utf8') <= max)
51
+ return s;
52
+ let out = Buffer.from(s, 'utf8').subarray(0, max).toString('utf8');
53
+ if (out.endsWith('�'))
54
+ out = out.slice(0, -1);
55
+ return out;
56
+ }
57
+ //# sourceMappingURL=convert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.js","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA;;;G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MAAM,CAAC,CAAC;IACnE,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC9C,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,268 @@
1
+ import * as z from 'zod';
2
+ /**
3
+ * Executor wire protocol (§10.6): 4-byte big-endian length + UTF-8 JSON, max 2 MiB per frame.
4
+ * Every received frame is validated; executor frames are untrusted on the server side.
5
+ */
6
+ export declare const MAX_FRAME_BYTES: number;
7
+ export declare const HelloFrame: z.ZodObject<{
8
+ type: z.ZodLiteral<"hello">;
9
+ protocol: z.ZodLiteral<1>;
10
+ token: z.ZodString;
11
+ }, z.core.$strip>;
12
+ export declare const HelloAckFrame: z.ZodObject<{
13
+ type: z.ZodLiteral<"hello_ack">;
14
+ protocol: z.ZodLiteral<1>;
15
+ executorVersion: z.ZodString;
16
+ montyVersion: z.ZodString;
17
+ montyBinarySha256: z.ZodString;
18
+ poolMax: z.ZodNumber;
19
+ }, z.core.$strip>;
20
+ export declare const RunFrame: z.ZodObject<{
21
+ type: z.ZodLiteral<"run">;
22
+ runId: z.ZodString;
23
+ capability: z.ZodString;
24
+ code: z.ZodString;
25
+ lineOffset: z.ZodOptional<z.ZodNumber>;
26
+ colOffset: z.ZodOptional<z.ZodNumber>;
27
+ prelude: z.ZodOptional<z.ZodString>;
28
+ modules: z.ZodRecord<z.ZodString, z.ZodObject<{
29
+ functions: z.ZodArray<z.ZodString>;
30
+ stubs: z.ZodString;
31
+ }, z.core.$strip>>;
32
+ inputs: z.ZodRecord<z.ZodString, z.ZodUnknown>;
33
+ limits: z.ZodObject<{
34
+ cpuTimeSec: z.ZodNumber;
35
+ wallTimeSec: z.ZodNumber;
36
+ maxMemoryMb: z.ZodNumber;
37
+ maxToolCalls: z.ZodNumber;
38
+ maxConcurrentCalls: z.ZodNumber;
39
+ maxResultBytes: z.ZodNumber;
40
+ maxRecursionDepth: z.ZodOptional<z.ZodNumber>;
41
+ }, z.core.$strip>;
42
+ osPolicy: z.ZodObject<{
43
+ datetime: z.ZodString;
44
+ seed: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
45
+ }, z.core.$strip>;
46
+ typeCheck: z.ZodOptional<z.ZodBoolean>;
47
+ }, z.core.$strip>;
48
+ export declare const CallFrame: z.ZodObject<{
49
+ type: z.ZodLiteral<"call">;
50
+ runId: z.ZodString;
51
+ capability: z.ZodString;
52
+ callId: z.ZodNumber;
53
+ fn: z.ZodString;
54
+ kwargs: z.ZodRecord<z.ZodString, z.ZodUnknown>;
55
+ }, z.core.$strip>;
56
+ export declare const CallResultFrame: z.ZodUnion<readonly [z.ZodObject<{
57
+ type: z.ZodLiteral<"call_result">;
58
+ runId: z.ZodString;
59
+ callId: z.ZodNumber;
60
+ ok: z.ZodLiteral<true>;
61
+ value: z.ZodUnknown;
62
+ }, z.core.$strip>, z.ZodObject<{
63
+ type: z.ZodLiteral<"call_result">;
64
+ runId: z.ZodString;
65
+ callId: z.ZodNumber;
66
+ ok: z.ZodLiteral<false>;
67
+ error: z.ZodObject<{
68
+ code: z.ZodString;
69
+ message: z.ZodString;
70
+ errorId: z.ZodOptional<z.ZodString>;
71
+ }, z.core.$strip>;
72
+ }, z.core.$strip>]>;
73
+ export declare const PrintFrame: z.ZodObject<{
74
+ type: z.ZodLiteral<"print">;
75
+ runId: z.ZodString;
76
+ text: z.ZodString;
77
+ }, z.core.$strip>;
78
+ export declare const CancelFrame: z.ZodObject<{
79
+ type: z.ZodLiteral<"cancel">;
80
+ runId: z.ZodString;
81
+ reason: z.ZodString;
82
+ }, z.core.$strip>;
83
+ export declare const DoneFrame: z.ZodObject<{
84
+ type: z.ZodLiteral<"done">;
85
+ runId: z.ZodString;
86
+ status: z.ZodEnum<{
87
+ ok: "ok";
88
+ script_error: "script_error";
89
+ syntax_error: "syntax_error";
90
+ type_error: "type_error";
91
+ limit_exceeded: "limit_exceeded";
92
+ crashed: "crashed";
93
+ cancelled: "cancelled";
94
+ }>;
95
+ value: z.ZodOptional<z.ZodUnknown>;
96
+ valueBytes: z.ZodNumber;
97
+ truncated: z.ZodOptional<z.ZodBoolean>;
98
+ preview: z.ZodOptional<z.ZodString>;
99
+ error: z.ZodOptional<z.ZodObject<{
100
+ type: z.ZodString;
101
+ message: z.ZodString;
102
+ line: z.ZodOptional<z.ZodNumber>;
103
+ col: z.ZodOptional<z.ZodNumber>;
104
+ traceback: z.ZodOptional<z.ZodString>;
105
+ }, z.core.$strip>>;
106
+ stats: z.ZodObject<{
107
+ wallMs: z.ZodNumber;
108
+ suspensions: z.ZodNumber;
109
+ toolCalls: z.ZodNumber;
110
+ }, z.core.$strip>;
111
+ stdout: z.ZodString;
112
+ }, z.core.$strip>;
113
+ export declare const PingFrame: z.ZodObject<{
114
+ type: z.ZodLiteral<"ping">;
115
+ t: z.ZodOptional<z.ZodNumber>;
116
+ }, z.core.$strip>;
117
+ export declare const PongFrame: z.ZodObject<{
118
+ type: z.ZodLiteral<"pong">;
119
+ t: z.ZodOptional<z.ZodNumber>;
120
+ }, z.core.$strip>;
121
+ export declare const ErrorFrame: z.ZodObject<{
122
+ type: z.ZodLiteral<"error">;
123
+ message: z.ZodString;
124
+ }, z.core.$strip>;
125
+ /** Frames the executor accepts (server → executor). */
126
+ export declare const ToExecutorFrame: z.ZodUnion<readonly [z.ZodDiscriminatedUnion<[z.ZodObject<{
127
+ type: z.ZodLiteral<"hello">;
128
+ protocol: z.ZodLiteral<1>;
129
+ token: z.ZodString;
130
+ }, z.core.$strip>, z.ZodObject<{
131
+ type: z.ZodLiteral<"run">;
132
+ runId: z.ZodString;
133
+ capability: z.ZodString;
134
+ code: z.ZodString;
135
+ lineOffset: z.ZodOptional<z.ZodNumber>;
136
+ colOffset: z.ZodOptional<z.ZodNumber>;
137
+ prelude: z.ZodOptional<z.ZodString>;
138
+ modules: z.ZodRecord<z.ZodString, z.ZodObject<{
139
+ functions: z.ZodArray<z.ZodString>;
140
+ stubs: z.ZodString;
141
+ }, z.core.$strip>>;
142
+ inputs: z.ZodRecord<z.ZodString, z.ZodUnknown>;
143
+ limits: z.ZodObject<{
144
+ cpuTimeSec: z.ZodNumber;
145
+ wallTimeSec: z.ZodNumber;
146
+ maxMemoryMb: z.ZodNumber;
147
+ maxToolCalls: z.ZodNumber;
148
+ maxConcurrentCalls: z.ZodNumber;
149
+ maxResultBytes: z.ZodNumber;
150
+ maxRecursionDepth: z.ZodOptional<z.ZodNumber>;
151
+ }, z.core.$strip>;
152
+ osPolicy: z.ZodObject<{
153
+ datetime: z.ZodString;
154
+ seed: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
155
+ }, z.core.$strip>;
156
+ typeCheck: z.ZodOptional<z.ZodBoolean>;
157
+ }, z.core.$strip>, z.ZodObject<{
158
+ type: z.ZodLiteral<"cancel">;
159
+ runId: z.ZodString;
160
+ reason: z.ZodString;
161
+ }, z.core.$strip>, z.ZodObject<{
162
+ type: z.ZodLiteral<"ping">;
163
+ t: z.ZodOptional<z.ZodNumber>;
164
+ }, z.core.$strip>, z.ZodObject<{
165
+ type: z.ZodLiteral<"pong">;
166
+ t: z.ZodOptional<z.ZodNumber>;
167
+ }, z.core.$strip>], "type">, z.ZodUnion<readonly [z.ZodObject<{
168
+ type: z.ZodLiteral<"call_result">;
169
+ runId: z.ZodString;
170
+ callId: z.ZodNumber;
171
+ ok: z.ZodLiteral<true>;
172
+ value: z.ZodUnknown;
173
+ }, z.core.$strip>, z.ZodObject<{
174
+ type: z.ZodLiteral<"call_result">;
175
+ runId: z.ZodString;
176
+ callId: z.ZodNumber;
177
+ ok: z.ZodLiteral<false>;
178
+ error: z.ZodObject<{
179
+ code: z.ZodString;
180
+ message: z.ZodString;
181
+ errorId: z.ZodOptional<z.ZodString>;
182
+ }, z.core.$strip>;
183
+ }, z.core.$strip>]>]>;
184
+ /** Frames the server accepts (executor → server). */
185
+ export declare const ToServerFrame: z.ZodDiscriminatedUnion<[z.ZodObject<{
186
+ type: z.ZodLiteral<"hello_ack">;
187
+ protocol: z.ZodLiteral<1>;
188
+ executorVersion: z.ZodString;
189
+ montyVersion: z.ZodString;
190
+ montyBinarySha256: z.ZodString;
191
+ poolMax: z.ZodNumber;
192
+ }, z.core.$strip>, z.ZodObject<{
193
+ type: z.ZodLiteral<"call">;
194
+ runId: z.ZodString;
195
+ capability: z.ZodString;
196
+ callId: z.ZodNumber;
197
+ fn: z.ZodString;
198
+ kwargs: z.ZodRecord<z.ZodString, z.ZodUnknown>;
199
+ }, z.core.$strip>, z.ZodObject<{
200
+ type: z.ZodLiteral<"print">;
201
+ runId: z.ZodString;
202
+ text: z.ZodString;
203
+ }, z.core.$strip>, z.ZodObject<{
204
+ type: z.ZodLiteral<"done">;
205
+ runId: z.ZodString;
206
+ status: z.ZodEnum<{
207
+ ok: "ok";
208
+ script_error: "script_error";
209
+ syntax_error: "syntax_error";
210
+ type_error: "type_error";
211
+ limit_exceeded: "limit_exceeded";
212
+ crashed: "crashed";
213
+ cancelled: "cancelled";
214
+ }>;
215
+ value: z.ZodOptional<z.ZodUnknown>;
216
+ valueBytes: z.ZodNumber;
217
+ truncated: z.ZodOptional<z.ZodBoolean>;
218
+ preview: z.ZodOptional<z.ZodString>;
219
+ error: z.ZodOptional<z.ZodObject<{
220
+ type: z.ZodString;
221
+ message: z.ZodString;
222
+ line: z.ZodOptional<z.ZodNumber>;
223
+ col: z.ZodOptional<z.ZodNumber>;
224
+ traceback: z.ZodOptional<z.ZodString>;
225
+ }, z.core.$strip>>;
226
+ stats: z.ZodObject<{
227
+ wallMs: z.ZodNumber;
228
+ suspensions: z.ZodNumber;
229
+ toolCalls: z.ZodNumber;
230
+ }, z.core.$strip>;
231
+ stdout: z.ZodString;
232
+ }, z.core.$strip>, z.ZodObject<{
233
+ type: z.ZodLiteral<"ping">;
234
+ t: z.ZodOptional<z.ZodNumber>;
235
+ }, z.core.$strip>, z.ZodObject<{
236
+ type: z.ZodLiteral<"pong">;
237
+ t: z.ZodOptional<z.ZodNumber>;
238
+ }, z.core.$strip>, z.ZodObject<{
239
+ type: z.ZodLiteral<"error">;
240
+ message: z.ZodString;
241
+ }, z.core.$strip>], "type">;
242
+ export type HelloFrame = z.infer<typeof HelloFrame>;
243
+ export type HelloAckFrame = z.infer<typeof HelloAckFrame>;
244
+ export type RunFrame = z.infer<typeof RunFrame>;
245
+ export type CallFrame = z.infer<typeof CallFrame>;
246
+ export type CallResultFrame = z.infer<typeof CallResultFrame>;
247
+ export type DoneFrame = z.infer<typeof DoneFrame>;
248
+ export type ToExecutorFrame = z.infer<typeof ToExecutorFrame>;
249
+ export type ToServerFrame = z.infer<typeof ToServerFrame>;
250
+ export declare class FrameError extends Error {
251
+ constructor(message: string);
252
+ }
253
+ /** Encodes one frame. Throws `FrameError` when the payload exceeds the frame limit. */
254
+ export declare function encodeFrame(frame: object, maxBytes?: number): Buffer;
255
+ /**
256
+ * Incremental decoder: `push(chunk)` returns the complete raw JSON values received so far.
257
+ * Throws `FrameError` on an oversize length prefix or malformed JSON (the caller must close the connection).
258
+ */
259
+ export declare class FrameDecoder {
260
+ private readonly maxBytes;
261
+ private buf;
262
+ constructor(maxBytes?: number);
263
+ push(chunk: Buffer): unknown[];
264
+ get pendingBytes(): number;
265
+ }
266
+ /** Validates a decoded frame against `schema`; throws `FrameError` with the first issue. */
267
+ export declare function parseFrame<T>(schema: z.ZodType<T>, raw: unknown): T;
268
+ //# sourceMappingURL=frames.d.ts.map