@masstensor/diagnose 0.1.0 → 0.3.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/README.md +86 -6
- package/dist/cli.js +252 -25
- package/dist/discover.d.ts +1 -0
- package/dist/discover.js +23 -0
- package/dist/engine.d.ts +20 -3
- package/dist/engine.js +172 -35
- package/dist/executor.d.ts +2 -2
- package/dist/executor.js +53 -5
- package/dist/forward.d.ts +10 -0
- package/dist/forward.js +115 -0
- package/dist/journal.d.ts +32 -0
- package/dist/journal.js +81 -0
- package/dist/mcp.js +69 -4
- package/dist/operator.d.ts +3 -1
- package/dist/operator.js +30 -3
- package/dist/peer.d.ts +1 -0
- package/dist/peer.js +1 -1
- package/dist/prepare-pty.d.ts +1 -0
- package/dist/prepare-pty.js +26 -0
- package/dist/relay.js +5 -2
- package/dist/resources.d.ts +28 -0
- package/dist/resources.js +427 -0
- package/dist/schema.d.ts +675 -1
- package/dist/schema.js +20 -1
- package/dist/transfers.d.ts +19 -0
- package/dist/transfers.js +118 -0
- package/npm-shrinkwrap.json +21 -3
- package/package.json +4 -2
- package/src/cli.ts +104 -19
- package/src/discover.ts +17 -0
- package/src/engine.ts +124 -34
- package/src/executor.ts +26 -7
- package/src/forward.ts +50 -0
- package/src/journal.ts +48 -0
- package/src/mcp.ts +34 -4
- package/src/operator.ts +15 -3
- package/src/peer.ts +2 -2
- package/src/prepare-pty.ts +15 -0
- package/src/relay.ts +1 -1
- package/src/resources.ts +180 -0
- package/src/schema.ts +27 -1
- package/src/transfers.ts +48 -0
- package/test/batch.test.ts +132 -0
- package/test/cli.test.ts +57 -1
- package/test/mcp.test.ts +13 -1
- package/test/resources.test.ts +146 -0
package/src/resources.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { spawn as spawnProcess } from 'node:child_process';
|
|
2
|
+
import { createHash, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { open, lstat, realpath, rename, unlink } from 'node:fs/promises';
|
|
5
|
+
import { dirname, resolve, join, basename } from 'node:path';
|
|
6
|
+
import net from 'node:net';
|
|
7
|
+
import { Journal, type ResourceMeta } from './journal.js';
|
|
8
|
+
import { killTree } from './executor.js';
|
|
9
|
+
import { Audit, sha256 } from './util.js';
|
|
10
|
+
import type { AtomicTask, ResourceRequest } from './schema.js';
|
|
11
|
+
import { preparePty } from './prepare-pty.js';
|
|
12
|
+
|
|
13
|
+
type Resource = { meta: ResourceMeta; owner: string; input?: (bytes: Buffer, eof: boolean) => Promise<void>; resize?: (cols: number, rows: number) => void; signal?: (signal: string) => void; read?: (request: ResourceRequest) => Promise<unknown>; control?: (request: ResourceRequest) => Promise<unknown>; close: () => Promise<void> | void };
|
|
14
|
+
const supported = new Set(['process.open', 'file.transfer', 'resource.resume', 'net.forward']);
|
|
15
|
+
export const resourceOperation = (operation: string): boolean => supported.has(operation);
|
|
16
|
+
const publicMeta = (meta: ResourceMeta): Record<string, unknown> => Object.fromEntries(Object.entries(meta).filter(([key]) => !['tokenHash', 'temp', 'parent', 'beforeSha256', 'mode'].includes(key)));
|
|
17
|
+
async function fileHash(path: string): Promise<string> { const file = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)); try { const hash = createHash('sha256'); for await (const chunk of file.createReadStream({ autoClose: false })) hash.update(chunk); return hash.digest('hex'); } finally { await file.close(); } }
|
|
18
|
+
async function currentHash(path: string): Promise<string | null> { try { const stat = await lstat(path); if (!stat.isFile() || stat.isSymbolicLink()) throw new Error('Target must be a regular, non-symlink file'); return await fileHash(path); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null; throw error; } }
|
|
19
|
+
|
|
20
|
+
export class SessionResources {
|
|
21
|
+
readonly journal: Journal;
|
|
22
|
+
private resources = new Map<string, Resource>();
|
|
23
|
+
private writes = new Map<string, Promise<unknown>>();
|
|
24
|
+
constructor(root: string, private audit: Audit) { this.journal = new Journal(root); }
|
|
25
|
+
setAudit(audit: Audit): void { this.audit = audit; }
|
|
26
|
+
private create(id: string, kind: ResourceMeta['kind'], owner: string, retainOnDisconnect: boolean, details: Record<string, unknown> = {}): { resource: Resource; token: string } {
|
|
27
|
+
if (this.resources.has(id) || this.journal.exists(id)) throw new Error('Resource ID already exists. Resume it; do not replay its creation.');
|
|
28
|
+
if ([...this.resources.values()].filter(r => ['running', 'open', 'listening'].includes(r.meta.state)).length >= 128) throw new Error('Active resource limit reached; close unused resources');
|
|
29
|
+
const token = randomBytes(32).toString('base64url'), now = new Date().toISOString();
|
|
30
|
+
const meta: ResourceMeta = { id, kind, state: 'open', createdAt: now, updatedAt: now, ownerPid: process.pid, tokenHash: sha256(Buffer.from(token)), retainOnDisconnect, ...details };
|
|
31
|
+
const resource: Resource = { meta, owner, close: () => { meta.state = 'closed'; this.journal.save(meta); } };
|
|
32
|
+
this.resources.set(id, resource); this.journal.save(meta); return { resource, token };
|
|
33
|
+
}
|
|
34
|
+
async open(task: AtomicTask, owner: string, signal?: AbortSignal): Promise<unknown> {
|
|
35
|
+
signal?.throwIfAborted();
|
|
36
|
+
if (task.operation === 'process.open') return this.startProcess(task, owner, signal);
|
|
37
|
+
if (task.operation === 'file.transfer') return this.startTransfer(task, owner, signal);
|
|
38
|
+
if (task.operation === 'net.forward') return this.startForward(task, owner, signal);
|
|
39
|
+
if (task.operation === 'resource.resume') {
|
|
40
|
+
const { id, resumeToken } = task.arguments; let resource = this.resources.get(id); const meta = resource?.meta ?? this.journal.load(id);
|
|
41
|
+
if (!timingSafeEqual(Buffer.from(meta.tokenHash, 'hex'), createHash('sha256').update(resumeToken).digest())) throw new Error('Invalid resource recovery credential');
|
|
42
|
+
if (!resource) {
|
|
43
|
+
if (meta.kind === 'upload' || meta.kind === 'download') resource = await this.restoreTransfer(meta, owner);
|
|
44
|
+
else if (meta.kind === 'process') { if (meta.state === 'running') { meta.state = 'interrupted'; meta.recoveryNote = 'Original host process is unavailable here. Stored output is recoverable; the command is never replayed.'; this.journal.save(meta); } resource = { meta, owner, close: () => {} }; }
|
|
45
|
+
else throw new Error('Network connections cannot survive host restart; create a new forward');
|
|
46
|
+
this.resources.set(id, resource);
|
|
47
|
+
}
|
|
48
|
+
if (signal?.aborted) { await resource.close(); signal.throwIfAborted(); }
|
|
49
|
+
resource.owner = owner; this.audit.record('resource.resumed', { id, kind: meta.kind }); return publicMeta(meta);
|
|
50
|
+
}
|
|
51
|
+
throw new Error('Unsupported resource operation');
|
|
52
|
+
}
|
|
53
|
+
async control(owner: string, request: ResourceRequest): Promise<unknown> {
|
|
54
|
+
if (request.id && ['input', 'commit'].includes(request.action)) {
|
|
55
|
+
const id = request.id, result = (this.writes.get(id) ?? Promise.resolve()).catch(() => {}).then(() => this.controlOne(owner, request));
|
|
56
|
+
this.writes.set(id, result); void result.finally(() => { if (this.writes.get(id) === result) this.writes.delete(id); }).catch(() => {}); return result;
|
|
57
|
+
}
|
|
58
|
+
return this.controlOne(owner, request);
|
|
59
|
+
}
|
|
60
|
+
private async controlOne(owner: string, request: ResourceRequest): Promise<unknown> {
|
|
61
|
+
if (request.action === 'list') return { resources: [...this.resources.values()].filter(r => r.owner === owner).map(r => publicMeta(r.meta)) };
|
|
62
|
+
const resource = this.resources.get(request.id ?? ''); if (!resource || resource.owner !== owner) throw new Error('Resource is not attached to this connection. Resume it with its ID and recovery credential first.');
|
|
63
|
+
const { meta } = resource;
|
|
64
|
+
if (request.action === 'status') return publicMeta(meta);
|
|
65
|
+
if (request.action === 'read') {
|
|
66
|
+
const stored = resource.read ? undefined : this.journal.read(meta.id, request.cursor, request.limit);
|
|
67
|
+
const value = resource.read ? await resource.read(request) : { ...stored, resource: publicMeta(meta), ended: meta.state !== 'running' && !stored!.hasMore };
|
|
68
|
+
if (resource.owner !== owner) throw new Error('Resource authorization was revoked');
|
|
69
|
+
this.audit.record('resource.read-sending', { id: meta.id, kind: meta.kind, cursor: request.cursor, responseBytes: Buffer.byteLength(JSON.stringify(value)) }); return value;
|
|
70
|
+
}
|
|
71
|
+
if (request.action !== 'close' && !['running', 'open', 'listening'].includes(meta.state)) throw new Error('Resource has finished or closed; only status/read are available');
|
|
72
|
+
if (request.action === 'input') {
|
|
73
|
+
if (!resource.input) throw new Error('This resource does not accept input'); const bytes = Buffer.from(request.data ?? '', 'base64');
|
|
74
|
+
if (bytes.length > 65536 || bytes.toString('base64') !== (request.data ?? '')) throw new Error('Invalid input encoding');
|
|
75
|
+
this.audit.record('resource.input', { id: meta.id, bytes: bytes.length, eof: request.eof }); await resource.input(bytes, request.eof); return { acceptedBytes: bytes.length };
|
|
76
|
+
}
|
|
77
|
+
if (request.action === 'resize') { if (!resource.resize || !request.cols || !request.rows) throw new Error('Resize requires a PTY and dimensions'); this.audit.record('resource.resize', { id: meta.id, cols: request.cols, rows: request.rows }); resource.resize(request.cols, request.rows); return publicMeta(meta); }
|
|
78
|
+
if (request.action === 'signal') { if (!resource.signal) throw new Error('Not a running process'); const signal = request.signal ?? 'SIGINT'; this.audit.record('resource.signal', { id: meta.id, signal }); resource.signal(signal); return { signalled: true }; }
|
|
79
|
+
if (request.action === 'close') { this.audit.record('resource.close', { id: meta.id }); await resource.close(); return publicMeta(meta); }
|
|
80
|
+
if (resource.control) return resource.control(request);
|
|
81
|
+
throw new Error('Unsupported resource action');
|
|
82
|
+
}
|
|
83
|
+
private async startProcess(task: Extract<AtomicTask, { operation: 'process.open' }>, owner: string, signal?: AbortSignal): Promise<unknown> {
|
|
84
|
+
const args = task.arguments, executable = args.executable ?? (process.platform === 'win32' ? process.env.ComSpec ?? 'cmd.exe' : process.env.SHELL ?? '/bin/sh');
|
|
85
|
+
const { resource, token } = this.create(task.id, 'process', owner, args.retainOnDisconnect, { state: 'running', executable, cwd: resolve(args.cwd ?? process.cwd()), pty: args.pty, cols: args.cols, rows: args.rows, bytes: 0 });
|
|
86
|
+
const { meta } = resource; const env = { ...process.env, ...args.env }; for (const key of Object.keys(env)) if (/^(?:DIAGNOSE_|OWON_|NPM_TOKEN|NODE_AUTH_TOKEN)/i.test(key)) delete env[key];
|
|
87
|
+
let timer: NodeJS.Timeout | undefined, force: NodeJS.Timeout | undefined, closing = false;
|
|
88
|
+
const ended = (code: number | null, signal?: number | string): void => { clearTimeout(timer); clearTimeout(force); meta.state = 'exited'; meta.exitCode = code; if (signal !== undefined) meta.signal = signal; this.journal.save(meta); this.audit.record('resource.process-exited', { id: meta.id, exitCode: code }); };
|
|
89
|
+
const collect = (stream: string, bytes: Buffer): void => {
|
|
90
|
+
try {
|
|
91
|
+
const keep = Math.max(0, Math.min(bytes.length, args.maxOutputBytes - Number(meta.bytes))); if (keep) this.journal.append(meta.id, stream, bytes.subarray(0, keep)); meta.bytes = Number(meta.bytes) + keep;
|
|
92
|
+
if (keep < bytes.length) { meta.truncated = true; resource.signal?.('SIGKILL'); }
|
|
93
|
+
} catch (error) { meta.error = (error as Error).message; resource.signal?.('SIGKILL'); }
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
if (args.pty) {
|
|
97
|
+
await preparePty();
|
|
98
|
+
const pty = await import('node-pty'); signal?.throwIfAborted(); if (resource.owner !== owner) throw new Error('Resource authorization was revoked'); const child = pty.spawn(executable, args.args, { name: 'xterm-256color', cols: args.cols, rows: args.rows, cwd: String(meta.cwd), env, encoding: null }); meta.pid = child.pid;
|
|
99
|
+
child.onData(value => collect('terminal', Buffer.isBuffer(value) ? value : Buffer.from(value))); child.onExit(value => ended(value.exitCode, value.signal));
|
|
100
|
+
resource.input = async (bytes, eof) => { child.write(bytes.toString('utf8')); if (eof) child.write('\x04'); };
|
|
101
|
+
resource.resize = (cols, rows) => { child.resize(cols, rows); meta.cols = cols; meta.rows = rows; this.journal.save(meta); };
|
|
102
|
+
resource.signal = signal => { try { child.kill(signal); } catch {} };
|
|
103
|
+
} else {
|
|
104
|
+
const child = spawnProcess(executable, args.args, { cwd: String(meta.cwd), env, detached: process.platform !== 'win32', stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }); meta.pid = child.pid;
|
|
105
|
+
child.stdout.on('data', bytes => collect('stdout', bytes)); child.stderr.on('data', bytes => collect('stderr', bytes)); child.stdin.on('error', () => {});
|
|
106
|
+
child.on('error', error => { meta.error = error.message; }); child.on('close', (code, signal) => ended(code, signal ?? undefined));
|
|
107
|
+
resource.input = (bytes, eof) => new Promise((resolveInput, reject) => { if (child.stdin.destroyed || child.stdin.writableEnded) { reject(new Error('Process stdin is closed')); return; } if (eof) child.stdin.end(bytes, resolveInput); else child.stdin.write(bytes, error => error ? reject(error) : resolveInput()); });
|
|
108
|
+
resource.signal = signal => { if (process.platform === 'win32') killTree(child, signal === 'SIGKILL'); else { try { process.kill(-child.pid!, signal as NodeJS.Signals); } catch {} } };
|
|
109
|
+
}
|
|
110
|
+
resource.close = () => { if (meta.state === 'running' && !closing) { closing = true; resource.signal?.('SIGTERM'); force = setTimeout(() => resource.signal?.('SIGKILL'), 1000); force.unref(); } };
|
|
111
|
+
if (args.timeoutMs) { timer = setTimeout(() => { meta.timedOut = true; void resource.close(); }, args.timeoutMs); timer.unref(); }
|
|
112
|
+
this.journal.save(meta); this.audit.record('resource.process-started', { id: meta.id, pid: meta.pid, pty: args.pty, retainOnDisconnect: args.retainOnDisconnect });
|
|
113
|
+
return { ...publicMeta(meta), resumeToken: token, output: 'Raw, unredacted bytes. Poll read from cursor 0 for live output; input/resize/signals are authorized for this handle.' };
|
|
114
|
+
} catch (error) { meta.state = 'failed'; meta.error = (error as Error).message; this.journal.save(meta); throw error; }
|
|
115
|
+
}
|
|
116
|
+
private async startTransfer(task: Extract<AtomicTask, { operation: 'file.transfer' }>, owner: string, signal?: AbortSignal): Promise<unknown> {
|
|
117
|
+
const args = task.arguments, path = resolve(args.path); let details: Record<string, unknown>;
|
|
118
|
+
if (args.direction === 'download') { const stat = await lstat(path); if (!stat.isFile() || stat.isSymbolicLink()) throw new Error('Download requires a regular non-symlink file'); details = { path, size: stat.size, sha256: await fileHash(path), offset: 0 }; }
|
|
119
|
+
else { if (args.size === undefined || !args.sha256) throw new Error('Upload requires exact size and SHA-256'); const before = await currentHash(path); if (before !== null && !args.overwrite) throw new Error('Target exists; explicit overwrite authorization required'); details = { path, parent: await realpath(dirname(path)), temp: join(dirname(path), `.${basename(path)}.diagnose-${task.id}`), beforeSha256: before, size: args.size, sha256: args.sha256, offset: 0 }; }
|
|
120
|
+
signal?.throwIfAborted(); const { resource, token } = this.create(task.id, args.direction, owner, args.retainOnDisconnect, details);
|
|
121
|
+
const live = await this.restoreTransfer(resource.meta, owner, true); if (signal?.aborted || resource.owner !== owner) { await live.close(); throw new Error('Transfer authorization was cancelled'); } this.resources.set(task.id, live);
|
|
122
|
+
this.audit.record('resource.transfer-opened', { id: task.id, direction: args.direction, path, size: details.size }); return { ...publicMeta(live.meta), resumeToken: token, encoding: 'base64', redaction: 'none; exact file bytes' };
|
|
123
|
+
}
|
|
124
|
+
private async restoreTransfer(meta: ResourceMeta, owner: string, fresh = false): Promise<Resource> {
|
|
125
|
+
if (meta.state !== 'open') return { meta, owner, close: () => {} };
|
|
126
|
+
const upload = meta.kind === 'upload';
|
|
127
|
+
if (upload && await realpath(dirname(String(meta.path))) !== meta.parent) throw new Error('Transfer parent changed');
|
|
128
|
+
const file = await open(String(upload ? meta.temp : meta.path), upload ? fresh ? 'wx+' : constants.O_RDWR | (constants.O_NOFOLLOW ?? 0) : constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
129
|
+
if (upload && !fresh && (await file.stat()).size !== meta.offset) { await file.close(); throw new Error('Partial upload size changed'); }
|
|
130
|
+
let closed = false;
|
|
131
|
+
const resource: Resource = { meta, owner, close: async () => { if (!closed) { closed = true; await file.close(); } if (upload) await unlink(String(meta.temp)).catch(() => {}); meta.state = 'closed'; this.journal.save(meta); } };
|
|
132
|
+
if (!upload) resource.read = async request => { const bytes = Buffer.alloc(Math.min(request.limit, Math.max(0, Number(meta.size) - request.cursor))); const read = await file.read(bytes, 0, bytes.length, request.cursor); return { data: bytes.subarray(0, read.bytesRead).toString('base64'), offset: request.cursor, nextOffset: request.cursor + read.bytesRead, ended: request.cursor + read.bytesRead >= Number(meta.size), size: meta.size, sha256: meta.sha256 }; };
|
|
133
|
+
else {
|
|
134
|
+
resource.input = async bytes => { if (closed) throw new Error('Upload closed'); if (Number(meta.offset) + bytes.length > Number(meta.size)) throw new Error('Upload exceeds declared size'); let written = 0; while (written < bytes.length) { const result = await file.write(bytes, written, bytes.length - written, Number(meta.offset) + written); if (!result.bytesWritten) throw new Error('Upload write made no progress'); written += result.bytesWritten; } meta.offset = Number(meta.offset) + bytes.length; this.journal.save(meta); };
|
|
135
|
+
resource.control = async request => {
|
|
136
|
+
if (request.action !== 'commit') throw new Error('Unsupported transfer action');
|
|
137
|
+
if (meta.offset !== meta.size) throw new Error('Upload is incomplete'); await file.sync();
|
|
138
|
+
if (await fileHash(String(meta.temp)) !== meta.sha256) throw new Error('Upload SHA-256 mismatch; destination untouched');
|
|
139
|
+
if (await realpath(dirname(String(meta.path))) !== meta.parent || await currentHash(String(meta.path)) !== meta.beforeSha256) throw new Error('Destination changed since upload authorization');
|
|
140
|
+
await file.close(); closed = true; await rename(String(meta.temp), String(meta.path)); meta.state = 'committed'; this.journal.save(meta); this.audit.record('resource.file-changed', { id: meta.id, path: meta.path, beforeSha256: meta.beforeSha256, afterSha256: meta.sha256, bytes: meta.size }); return publicMeta(meta);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return resource;
|
|
144
|
+
}
|
|
145
|
+
private tcp(socket: net.Socket, owner: string, parentId: string): Resource {
|
|
146
|
+
const { resource } = this.create(randomUUID(), 'tcp', owner, false, { parentId, state: 'open', bytesRead: 0, bytesWritten: 0 }); let chunks: Buffer[] = [], size = 0, ended = false;
|
|
147
|
+
socket.on('data', chunk => { chunks.push(Buffer.from(chunk)); size += chunk.length; resource.meta.bytesRead = Number(resource.meta.bytesRead) + chunk.length; if (size > 256 * 1024) socket.pause(); });
|
|
148
|
+
socket.on('end', () => { ended = true; }); socket.on('close', () => { ended = true; resource.meta.state = 'closed'; this.journal.save(resource.meta); }); socket.on('error', error => { resource.meta.error = error.message; ended = true; });
|
|
149
|
+
resource.read = async request => { const count = Math.min(size, request.limit), result = Buffer.alloc(count); let offset = 0; while (offset < count) { const chunk = chunks[0], keep = Math.min(chunk.length, count - offset); chunk.copy(result, offset, 0, keep); if (keep === chunk.length) chunks.shift(); else chunks[0] = chunk.subarray(keep); offset += keep; } size -= count; if (size < 128 * 1024 && !ended) socket.resume(); return { data: result.toString('base64'), ended: ended && size === 0 }; };
|
|
150
|
+
resource.input = (bytes, eof) => new Promise((resolveInput, reject) => { if (socket.destroyed) { reject(new Error('TCP connection closed')); return; } resource.meta.bytesWritten = Number(resource.meta.bytesWritten) + bytes.length; if (eof) socket.end(bytes, resolveInput); else socket.write(bytes, error => error ? reject(error) : resolveInput()); });
|
|
151
|
+
resource.close = () => { chunks = []; size = 0; socket.destroy(); }; return resource;
|
|
152
|
+
}
|
|
153
|
+
private async startForward(task: Extract<AtomicTask, { operation: 'net.forward' }>, owner: string, signal?: AbortSignal): Promise<unknown> {
|
|
154
|
+
const args = task.arguments; if (args.direction !== 'socks' && !args.port) throw new Error('A fixed target port is required');
|
|
155
|
+
const { resource, token } = this.create(task.id, 'forward', owner, false, { ...args, state: args.direction === 'remote' ? 'listening' : 'open' }); let server: net.Server | undefined; const pending: string[] = [];
|
|
156
|
+
if (args.direction === 'remote') {
|
|
157
|
+
server = net.createServer({ allowHalfOpen: true }, socket => { if (signal?.aborted || resource.owner !== owner || pending.length >= 32) { socket.destroy(); return; } try { pending.push(this.tcp(socket, resource.owner, task.id).meta.id); } catch { socket.destroy(); } });
|
|
158
|
+
server.on('error', error => { resource.meta.error = error.message; }); await new Promise<void>((resolveListen, reject) => { server!.once('error', reject); server!.listen(args.bindPort, args.bindHost, () => { server!.off('error', reject); resolveListen(); }); }); resource.meta.listeningPort = (server.address() as net.AddressInfo).port;
|
|
159
|
+
}
|
|
160
|
+
if (signal?.aborted || resource.owner !== owner) { server?.close(); resource.meta.state = 'closed'; this.journal.save(resource.meta); throw new Error('Forward authorization was cancelled'); }
|
|
161
|
+
resource.control = async request => {
|
|
162
|
+
if (request.action === 'accept' && server) return { connections: pending.splice(0, 16) };
|
|
163
|
+
if (request.action !== 'connect' || server) throw new Error('Unsupported forwarding action');
|
|
164
|
+
const host = args.direction === 'socks' ? request.host : args.host, port = args.direction === 'socks' ? request.port : args.port;
|
|
165
|
+
if (!host || !port || (args.direction === 'socks' && !args.allowedHosts.includes('*') && !args.allowedHosts.includes(host))) throw new Error('Destination is outside the authorized forwarding scope');
|
|
166
|
+
const socket = net.createConnection({ host, port, allowHalfOpen: true }); await new Promise<void>((resolveConnect, reject) => { socket.once('connect', resolveConnect); socket.once('error', reject); socket.setTimeout(10000, () => socket.destroy(new Error('TCP connect timeout'))); }); socket.setTimeout(0);
|
|
167
|
+
if (resource.owner !== owner || resource.meta.state === 'closed') { socket.destroy(); throw new Error('Forward authorization was revoked'); }
|
|
168
|
+
const child = this.tcp(socket, resource.owner, task.id); this.audit.record('resource.tcp-connected', { id: child.meta.id, parentId: task.id, host, port }); return publicMeta(child.meta);
|
|
169
|
+
};
|
|
170
|
+
resource.close = async () => { server?.close(); for (const child of this.resources.values()) if (child.meta.parentId === task.id) await child.close(); resource.meta.state = 'closed'; this.journal.save(resource.meta); };
|
|
171
|
+
this.journal.save(resource.meta); return { ...publicMeta(resource.meta), resumeToken: token };
|
|
172
|
+
}
|
|
173
|
+
async disconnect(owner: string): Promise<void> { for (const resource of this.resources.values()) if (resource.owner === owner) { resource.owner = ''; if (!resource.meta.retainOnDisconnect) await resource.close(); } }
|
|
174
|
+
async abandon(task: AtomicTask, owner: string): Promise<void> { const id = task.operation === 'resource.resume' ? task.arguments.id : task.id, resource = this.resources.get(id); if (resource?.owner === owner) { resource.owner = ''; if (task.operation !== 'resource.resume' || !resource.meta.retainOnDisconnect) await resource.close(); } }
|
|
175
|
+
async revoke(owner?: string): Promise<void> { for (const resource of this.resources.values()) if (owner === undefined || resource.owner === owner) { resource.owner = ''; await resource.close(); } }
|
|
176
|
+
listLocal(): Record<string, unknown>[] { return [...this.resources.values()].map(resource => publicMeta(resource.meta)); }
|
|
177
|
+
async localInput(id: string, value: Buffer): Promise<void> { const resource = this.resources.get(id); if (!resource?.input || resource.meta.state !== 'running') throw new Error('No live process with this ID'); this.audit.record('resource.local-input', { id, bytes: value.length }); await resource.input(value, false); }
|
|
178
|
+
renewCredential(id: string): string { const meta = this.resources.get(id)?.meta ?? this.journal.load(id); const token = randomBytes(32).toString('base64url'); meta.tokenHash = sha256(Buffer.from(token)); this.journal.save(meta); this.audit.record('resource.local-credential-renewed', { id }); return token; }
|
|
179
|
+
get hasRetained(): boolean { return [...this.resources.values()].some(r => r.meta.retainOnDisconnect && ['running', 'open'].includes(r.meta.state)); }
|
|
180
|
+
}
|
package/src/schema.ts
CHANGED
|
@@ -5,19 +5,39 @@ export const CHUNK_SIZE = 12 * 1024;
|
|
|
5
5
|
export const MAX_OUTPUT = 10 * 1024 * 1024;
|
|
6
6
|
const path = z.string().min(1).max(4096);
|
|
7
7
|
const common = { id: z.string().uuid(), reason: z.string().min(1).max(1000) };
|
|
8
|
-
export const
|
|
8
|
+
export const atomicOperationNames = ['system.inspect', 'file.read', 'file.write', 'file.stat', 'file.list', 'file.mkdir', 'file.remove', 'file.rename', 'file.chmod', 'file.transfer', 'process.run', 'process.script', 'process.open', 'resource.resume', 'net.forward', 'serial.capture', 'serial.exchange'] as const;
|
|
9
|
+
export const batchOperationNames = ['system.inspect', 'file.read', 'file.write', 'file.stat', 'file.list', 'file.mkdir', 'file.remove', 'file.rename', 'file.chmod', 'process.run', 'process.script', 'serial.capture', 'serial.exchange'] as const;
|
|
10
|
+
const environment = z.record(z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/), z.string().max(65536)).optional();
|
|
11
|
+
export const atomicTaskSchema = z.discriminatedUnion('operation', [
|
|
9
12
|
z.object({ ...common, operation: z.literal('system.inspect'), arguments: z.object({}).strict() }).strict(),
|
|
10
13
|
z.object({ ...common, operation: z.literal('file.read'), arguments: z.object({ path, offset: z.number().int().min(0).default(0), length: z.number().int().min(1).max(1024 * 1024).default(65536) }).strict() }).strict(),
|
|
11
14
|
z.object({ ...common, operation: z.literal('file.write'), arguments: z.object({ path, content: z.string().max(1024 * 1024), expectedSha256: z.string().regex(/^[a-f0-9]{64}$/).optional() }).strict() }).strict(),
|
|
15
|
+
z.object({ ...common, operation: z.literal('file.stat'), arguments: z.object({ path }).strict() }).strict(),
|
|
16
|
+
z.object({ ...common, operation: z.literal('file.list'), arguments: z.object({ path, offset: z.number().int().min(0).default(0), limit: z.number().int().min(1).max(200).default(50) }).strict() }).strict(),
|
|
17
|
+
z.object({ ...common, operation: z.literal('file.mkdir'), arguments: z.object({ path, recursive: z.boolean().default(false) }).strict() }).strict(),
|
|
18
|
+
z.object({ ...common, operation: z.literal('file.remove'), arguments: z.object({ path, recursive: z.boolean().default(false) }).strict() }).strict(),
|
|
19
|
+
z.object({ ...common, operation: z.literal('file.rename'), arguments: z.object({ path, destination: path, overwrite: z.boolean().default(false) }).strict() }).strict(),
|
|
20
|
+
z.object({ ...common, operation: z.literal('file.chmod'), arguments: z.object({ path, mode: z.number().int().min(0).max(0o777) }).strict() }).strict(),
|
|
21
|
+
z.object({ ...common, operation: z.literal('file.transfer'), arguments: z.object({ path, direction: z.enum(['upload', 'download']), size: z.number().int().min(0).max(1024 ** 4).optional(), sha256: z.string().regex(/^[a-f0-9]{64}$/).optional(), overwrite: z.boolean().default(false), retainOnDisconnect: z.boolean().default(true) }).strict() }).strict(),
|
|
22
|
+
z.object({ ...common, operation: z.literal('process.open'), arguments: z.object({ executable: path.optional(), args: z.array(z.string().max(65536)).max(128).default([]), cwd: path.optional(), env: environment, pty: z.boolean().default(false), cols: z.number().int().min(20).max(500).default(100), rows: z.number().int().min(5).max(200).default(30), timeoutMs: z.number().int().min(0).max(86400000).default(3600000), retainOnDisconnect: z.boolean().default(false), maxOutputBytes: z.number().int().min(1024).max(1024 ** 3).default(64 * 1024 ** 2) }).strict() }).strict(),
|
|
23
|
+
z.object({ ...common, operation: z.literal('resource.resume'), arguments: z.object({ id: z.string().uuid(), resumeToken: z.string().regex(/^[A-Za-z0-9_-]{43}$/) }).strict() }).strict(),
|
|
24
|
+
z.object({ ...common, operation: z.literal('net.forward'), arguments: z.object({ direction: z.enum(['local', 'remote', 'socks']), host: z.string().min(1).max(253).default('127.0.0.1'), port: z.number().int().min(0).max(65535).default(0), bindHost: z.enum(['127.0.0.1', '::1']).default('127.0.0.1'), bindPort: z.number().int().min(0).max(65535).default(0), allowedHosts: z.array(z.string().min(1).max(253)).min(1).max(100).default(['*']) }).strict() }).strict(),
|
|
12
25
|
z.object({ ...common, operation: z.literal('process.run'), arguments: z.object({ executable: path, args: z.array(z.string().max(65536)).max(128).default([]), cwd: path.optional(), timeoutMs: z.number().int().min(100).max(300000).default(60000) }).strict() }).strict(),
|
|
26
|
+
z.object({ ...common, operation: z.literal('process.script'), arguments: z.object({ interpreter: path, script: z.string().min(1).max(256 * 1024), args: z.array(z.string().max(65536)).max(128).default([]), cwd: path.optional(), timeoutMs: z.number().int().min(100).max(300000).default(60000) }).strict() }).strict(),
|
|
13
27
|
...(['serial.capture', 'serial.exchange'] as const).map(operation => z.object({ ...common, operation: z.literal(operation), arguments: z.object({ path, baudRate: z.number().int().min(300).max(3000000).default(115200), durationMs: z.number().int().min(100).max(300000).default(20000), dataHex: z.string().regex(/^(?:[a-fA-F0-9]{2})*$/).max(65536).optional() }).strict() }).strict()),
|
|
14
28
|
]);
|
|
29
|
+
export const batchTaskSchema = z.object({ ...common, operation: z.literal('batch.run'), arguments: z.object({ steps: z.array(atomicTaskSchema).min(1).max(32), stopOnError: z.boolean().default(true), timeoutMs: z.number().int().min(100).max(300000).default(300000) }).strict() }).strict().refine(task => new Set([task.id, ...task.arguments.steps.map(step => step.id)]).size === task.arguments.steps.length + 1, 'Batch task IDs must be unique');
|
|
30
|
+
export const taskSchema = z.union([atomicTaskSchema, batchTaskSchema]);
|
|
31
|
+
export type AtomicTask = z.infer<typeof atomicTaskSchema>;
|
|
32
|
+
export type BatchTask = z.infer<typeof batchTaskSchema>;
|
|
15
33
|
export type Task = z.infer<typeof taskSchema>;
|
|
16
34
|
export type TaskInput = { operation: Task['operation']; arguments: Record<string, unknown>; reason: string };
|
|
17
35
|
export type ApprovalStage = 'execute' | 'send';
|
|
18
36
|
export type Approval = { stage: ApprovalStage; task: Task; digest: string; preview: string; bytes?: number; signal?: AbortSignal };
|
|
19
37
|
export type ApprovalHandler = (request: Approval) => Promise<boolean>;
|
|
20
38
|
export type Result = { status: 'ok' | 'error' | 'denied' | 'cancelled' | 'duplicate'; data?: unknown; error?: string; exitCode?: number | null; truncated?: boolean };
|
|
39
|
+
export const resourceRequestSchema = z.object({ id: z.string().uuid().optional(), action: z.enum(['list', 'read', 'input', 'resize', 'signal', 'status', 'close', 'commit', 'connect', 'accept']), cursor: z.number().int().min(0).default(0), limit: z.number().int().min(1).max(65536).default(65536), data: z.string().max(90000).optional(), eof: z.boolean().default(false), cols: z.number().int().min(20).max(500).optional(), rows: z.number().int().min(5).max(200).optional(), signal: z.enum(['SIGINT', 'SIGTERM', 'SIGKILL', 'SIGHUP', 'SIGQUIT']).optional(), host: z.string().min(1).max(253).optional(), port: z.number().int().min(1).max(65535).optional() }).strict();
|
|
40
|
+
export type ResourceRequest = z.infer<typeof resourceRequestSchema>;
|
|
21
41
|
export type WireMessage =
|
|
22
42
|
| { type: 'session.accepted'; label: string; fingerprint: string }
|
|
23
43
|
| { type: 'session.closed'; reason: string }
|
|
@@ -26,6 +46,10 @@ export type WireMessage =
|
|
|
26
46
|
| { type: 'task.output'; id: string; stream: string; chunk: string; index: number }
|
|
27
47
|
| { type: 'task.result'; id: string; result: Result }
|
|
28
48
|
| { type: 'task.cancel'; id: string }
|
|
49
|
+
| { type: 'resource.request'; requestId: string; request: ResourceRequest }
|
|
50
|
+
| { type: 'resource.result'; requestId: string; value?: unknown; error?: string }
|
|
51
|
+
| { type: 'resource.request'; requestId: string; request: ResourceRequest }
|
|
52
|
+
| { type: 'resource.result'; requestId: string; value?: unknown; error?: string }
|
|
29
53
|
| { type: 'ping' | 'pong' };
|
|
30
54
|
|
|
31
55
|
export function parseMessage(value: unknown): WireMessage {
|
|
@@ -33,6 +57,8 @@ export function parseMessage(value: unknown): WireMessage {
|
|
|
33
57
|
switch (basic.type) {
|
|
34
58
|
case 'task.request': return { type: basic.type, task: taskSchema.parse(basic.task) };
|
|
35
59
|
case 'task.cancel': return { type: basic.type, id: z.string().uuid().parse(basic.id) };
|
|
60
|
+
case 'resource.request': return { type: basic.type, requestId: z.string().uuid().parse(basic.requestId), request: resourceRequestSchema.parse(basic.request) };
|
|
61
|
+
case 'resource.result': return z.object({ type: z.literal('resource.result'), requestId: z.string().uuid(), value: z.unknown().optional(), error: z.string().max(2000).optional() }).strict().parse(value);
|
|
36
62
|
case 'ping': case 'pong': return { type: basic.type };
|
|
37
63
|
case 'session.accepted': return z.object({ type: z.literal('session.accepted'), label: z.string().max(120), fingerprint: z.string().max(100) }).strict().parse(value);
|
|
38
64
|
case 'session.closed': return z.object({ type: z.literal('session.closed'), reason: z.string().max(200) }).strict().parse(value);
|
package/src/transfers.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { open, stat, rename, writeFile, readFile, unlink } from 'node:fs/promises';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import type { Operator } from './operator.js';
|
|
5
|
+
export type TransferProgress = { state: string; bytes: number; size: number; resourceId?: string; resumeToken?: string; sha256?: string; error?: string };
|
|
6
|
+
export type TransferOptions = { resumeId?: string; resumeToken?: string; overwrite?: boolean; signal?: AbortSignal; progress?: (progress: TransferProgress) => void };
|
|
7
|
+
type Descriptor = { id: string; resumeToken?: string; size: number; sha256: string; offset: number };
|
|
8
|
+
async function hash(path: string): Promise<string> { const file = await open(path, 'r'); try { const value = createHash('sha256'); for await (const bytes of file.createReadStream({ autoClose: false })) value.update(bytes); return value.digest('hex'); } finally { await file.close(); } }
|
|
9
|
+
async function grant(operator: Operator, input: Parameters<Operator['run']>[0], options: TransferOptions): Promise<Descriptor> {
|
|
10
|
+
options.signal?.throwIfAborted(); const id = await operator.start(options.resumeId ? { operation: 'resource.resume', arguments: { id: options.resumeId, resumeToken: options.resumeToken }, reason: input.reason } : input);
|
|
11
|
+
const abort = () => { void operator.cancel(id).catch(() => {}); }; options.signal?.addEventListener('abort', abort, { once: true });
|
|
12
|
+
try { while (true) { if (options.signal?.aborted) { abort(); options.signal.throwIfAborted(); } const result = await operator.wait(id, 1000); if (!('pending' in result)) { if (result.result.status !== 'ok') throw new Error(result.result.error ?? result.result.status); return result.result.data as Descriptor; } } }
|
|
13
|
+
finally { options.signal?.removeEventListener('abort', abort); operator.release(id); }
|
|
14
|
+
}
|
|
15
|
+
export async function upload(operator: Operator, localPath: string, remotePath: string, reason: string, options: TransferOptions = {}): Promise<TransferProgress> {
|
|
16
|
+
const size = (await stat(localPath)).size, checksum = await hash(localPath);
|
|
17
|
+
const resource = await grant(operator, { operation: 'file.transfer', arguments: { direction: 'upload', path: remotePath, size, sha256: checksum, overwrite: options.overwrite ?? false }, reason }, options);
|
|
18
|
+
if (resource.size !== size || resource.sha256 !== checksum) throw new Error('Resume target does not match the selected local file');
|
|
19
|
+
const progress: TransferProgress = { state: 'uploading', bytes: resource.offset, size, resourceId: resource.id, resumeToken: resource.resumeToken ?? options.resumeToken, sha256: checksum }; options.progress?.({ ...progress });
|
|
20
|
+
const file = await open(localPath, 'r');
|
|
21
|
+
try {
|
|
22
|
+
const buffer = Buffer.alloc(65536);
|
|
23
|
+
while (progress.bytes < size) { options.signal?.throwIfAborted(); const { bytesRead } = await file.read(buffer, 0, Math.min(buffer.length, size - progress.bytes), progress.bytes); if (!bytesRead) throw new Error('Local upload source changed'); await operator.resource({ id: resource.id, action: 'input', data: buffer.subarray(0, bytesRead).toString('base64') }); progress.bytes += bytesRead; options.progress?.({ ...progress }); }
|
|
24
|
+
options.signal?.throwIfAborted(); await operator.resource({ id: resource.id, action: 'commit' }); progress.state = 'completed'; options.progress?.({ ...progress }); return progress;
|
|
25
|
+
} finally { await file.close(); }
|
|
26
|
+
}
|
|
27
|
+
export async function download(operator: Operator, remotePath: string, localPath: string, reason: string, options: TransferOptions = {}): Promise<TransferProgress> {
|
|
28
|
+
const destination = resolve(localPath), partial = destination + '.diagnose.part', sidecar = destination + '.diagnose.json';
|
|
29
|
+
try { await stat(destination); if (!options.overwrite) throw new Error('Local target exists; explicit overwrite is required'); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
|
|
30
|
+
let saved: { resourceId: string; resumeToken: string; sha256: string } | undefined;
|
|
31
|
+
try { saved = JSON.parse(await readFile(sidecar, 'utf8')); } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }
|
|
32
|
+
const effective = options.resumeId ? options : saved ? { ...options, resumeId: saved.resourceId, resumeToken: saved.resumeToken } : options;
|
|
33
|
+
const resource = await grant(operator, { operation: 'file.transfer', arguments: { direction: 'download', path: remotePath }, reason }, effective);
|
|
34
|
+
if (saved && saved.sha256 !== resource.sha256) throw new Error('Partial download belongs to a different source');
|
|
35
|
+
const file = await open(partial, 'a+', 0o600), bytes = (await file.stat()).size;
|
|
36
|
+
const progress: TransferProgress = { state: 'downloading', bytes, size: resource.size, resourceId: resource.id, resumeToken: resource.resumeToken ?? effective.resumeToken, sha256: resource.sha256 };
|
|
37
|
+
await writeFile(sidecar, JSON.stringify(progress), { mode: 0o600 }); options.progress?.({ ...progress });
|
|
38
|
+
try {
|
|
39
|
+
while (progress.bytes < resource.size) {
|
|
40
|
+
options.signal?.throwIfAborted(); const result = await operator.resource({ id: resource.id, action: 'read', cursor: progress.bytes }) as { data: string; nextOffset: number };
|
|
41
|
+
const block = Buffer.from(result.data, 'base64'); if (!block.length || result.nextOffset !== progress.bytes + block.length) throw new Error('Invalid download response');
|
|
42
|
+
await file.writeFile(block); progress.bytes += block.length; options.progress?.({ ...progress });
|
|
43
|
+
}
|
|
44
|
+
await file.sync();
|
|
45
|
+
} finally { await file.close(); }
|
|
46
|
+
if (progress.bytes !== resource.size || await hash(partial) !== resource.sha256) throw new Error('Download integrity mismatch; destination untouched');
|
|
47
|
+
options.signal?.throwIfAborted(); await rename(partial, destination); await unlink(sidecar); await operator.resource({ id: resource.id, action: 'close' }); progress.state = 'completed'; options.progress?.({ ...progress }); return progress;
|
|
48
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdtempSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
import { createRelay } from '../dist/relay.js';
|
|
8
|
+
import { createCode } from '../dist/crypto.js';
|
|
9
|
+
import { Peer } from '../dist/peer.js';
|
|
10
|
+
import { ProbeEngine } from '../dist/engine.js';
|
|
11
|
+
import { Operator } from '../dist/operator.js';
|
|
12
|
+
import { Audit } from '../dist/util.js';
|
|
13
|
+
import { taskSchema, type ApprovalHandler, type TaskInput } from '../dist/schema.js';
|
|
14
|
+
|
|
15
|
+
const dir = mkdtempSync(join(tmpdir(), 'diagnose-batch-'));
|
|
16
|
+
const script = (source: string) => ({ operation: 'process.script', arguments: { interpreter: process.execPath, script: source } });
|
|
17
|
+
async function session(approve: ApprovalHandler = async () => true, notice: (text: string) => void = () => {}) {
|
|
18
|
+
const relay = createRelay({ port: 0 }); const url = `ws://127.0.0.1:${await relay.listen()}`; const code = createCode();
|
|
19
|
+
const host = new Peer({ role: 'host', code, relay: url, accept: async () => true });
|
|
20
|
+
const audit = new Audit(join(dir, randomUUID() + '.jsonl')); const engine = new ProbeEngine(host, approve, audit, notice); await host.open();
|
|
21
|
+
const peer = new Peer({ role: 'join', code, relay: url }); const operator = new Operator(peer); await peer.open();
|
|
22
|
+
return { engine, audit, operator, peer, async close() { peer.close(); host.close(); await relay.close(); } };
|
|
23
|
+
}
|
|
24
|
+
function batch(steps: unknown[], extra = {}): TaskInput { return { operation: 'batch.run', arguments: { steps, ...extra }, reason: 'Batch acceptance' }; }
|
|
25
|
+
async function waitFile(path: string): Promise<void> { for (let i = 0; i < 100 && !existsSync(path); i++) await new Promise(resolve => setTimeout(resolve, 20)); assert.ok(existsSync(path), 'diagnostic process started'); }
|
|
26
|
+
|
|
27
|
+
test('one plan approval and one transfer approval cover ordered writes, reads and a full script', async () => {
|
|
28
|
+
const stages: string[] = [], previews: string[] = []; const target = join(dir, randomUUID());
|
|
29
|
+
const source = 'const n = 2 + 3;\nconsole.log("script result", n);';
|
|
30
|
+
const s = await session(async request => { stages.push(request.stage); previews.push(request.preview); return true; });
|
|
31
|
+
try {
|
|
32
|
+
const result = await s.operator.run(batch([{ operation: 'file.write', arguments: { path: target, content: 'plan-data' } }, { operation: 'file.read', arguments: { path: target } }, script(source)]));
|
|
33
|
+
assert.equal(result.result.status, 'ok'); assert.deepEqual(stages, ['execute', 'send']); assert.ok(previews[0].includes(source), 'complete multiline script shown');
|
|
34
|
+
assert.equal(result.outputs['1.file'], 'plan-data'); assert.equal(result.outputs['2.stdout'], 'script result 5\n');
|
|
35
|
+
const data = result.result.data as any; assert.equal(data.completedSteps, 3); assert.equal(data.steps[0].result.status, 'ok');
|
|
36
|
+
const entries = readFileSync(s.audit.path, 'utf8').trim().split('\n').map(line => JSON.parse(line));
|
|
37
|
+
assert.equal(entries.filter(e => e.event === 'task.executing').length, 3); assert.ok(entries.some(e => e.event === 'task.file-changed' && e.parentId === result.id));
|
|
38
|
+
} finally { await s.close(); }
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('batch refusal does not execute; transfer refusal returns no collected output', async () => {
|
|
42
|
+
let executeAllowed = false; const target = join(dir, randomUUID()); const s = await session(async request => executeAllowed && request.stage === 'execute');
|
|
43
|
+
try {
|
|
44
|
+
const refused = await s.operator.run(batch([{ operation: 'file.write', arguments: { path: target, content: 'no' } }]));
|
|
45
|
+
assert.equal(refused.result.status, 'denied'); assert.equal(existsSync(target), false);
|
|
46
|
+
executeAllowed = true;
|
|
47
|
+
const local = await s.operator.run(batch([script('console.log("LOCAL_BATCH_ONLY")'), script('console.log("LOCAL_SECOND")')]));
|
|
48
|
+
assert.equal(local.result.status, 'denied'); assert.deepEqual(local.outputs, {});
|
|
49
|
+
} finally { await s.close(); }
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('batch failure stops later steps by default; explicit continue runs independent steps', async () => {
|
|
53
|
+
const s = await session(); const target = join(dir, randomUUID());
|
|
54
|
+
try {
|
|
55
|
+
const steps = [script('throw new Error("expected failure")'), { operation: 'file.write', arguments: { path: target, content: 'continued' } }];
|
|
56
|
+
const stopped = await s.operator.run(batch(steps)); assert.equal(stopped.result.status, 'error'); assert.equal(existsSync(target), false); assert.equal((stopped.result.data as any).completedSteps, 1);
|
|
57
|
+
const continued = await s.operator.run(batch(steps, { stopOnError: false })); assert.equal(continued.result.status, 'error'); assert.equal(readFileSync(target, 'utf8'), 'continued'); assert.equal((continued.result.data as any).completedSteps, 2);
|
|
58
|
+
} finally { await s.close(); }
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('batch deadline terminates the child and leaves later steps unexecuted', async () => {
|
|
62
|
+
const pidPath = join(dir, randomUUID() + '.pid'), target = join(dir, randomUUID()); const s = await session();
|
|
63
|
+
try {
|
|
64
|
+
const result = await s.operator.run(batch([script(`require('fs').writeFileSync(${JSON.stringify(pidPath)},String(process.pid));setInterval(()=>{},1000)`), { operation: 'file.write', arguments: { path: target, content: 'no' } }], { timeoutMs: 350 }));
|
|
65
|
+
assert.equal(result.result.error, 'Batch timeout'); assert.equal(existsSync(target), false); assert.ok(existsSync(pidPath));
|
|
66
|
+
assert.throws(() => process.kill(Number(readFileSync(pidPath, 'utf8')), 0), { code: 'ESRCH' });
|
|
67
|
+
} finally { await s.close(); }
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('auto mode approves execution AND transfer; local revocation cancels work and restores prompts', async () => {
|
|
71
|
+
let prompts = 0; const pidPath = join(dir, randomUUID() + '.pid'), target = join(dir, randomUUID());
|
|
72
|
+
const s = await session(async () => { prompts++; return false; });
|
|
73
|
+
try {
|
|
74
|
+
assert.equal(s.engine.permissionMode, 'ask'); s.engine.setPermissionMode('auto');
|
|
75
|
+
const result = await s.operator.run(batch([script('console.log("automatic first")'), script('console.log("automatic second")')]));
|
|
76
|
+
assert.equal(result.result.status, 'ok'); assert.equal(result.outputs['1.stdout'], 'automatic second\n'); assert.equal(prompts, 0);
|
|
77
|
+
const id = await s.operator.start(batch([script(`require('fs').writeFileSync(${JSON.stringify(pidPath)},String(process.pid));setInterval(()=>{},1000)`), { operation: 'file.write', arguments: { path: target, content: 'no' } }]));
|
|
78
|
+
await waitFile(pidPath); s.engine.setPermissionMode('ask');
|
|
79
|
+
const cancelled = await s.operator.wait(id, 5000); assert.equal('result' in cancelled && cancelled.result.status, 'cancelled'); assert.equal(existsSync(target), false);
|
|
80
|
+
assert.throws(() => process.kill(Number(readFileSync(pidPath, 'utf8')), 0), { code: 'ESRCH' });
|
|
81
|
+
const denied = await s.operator.run({ operation: 'system.inspect', arguments: {}, reason: 'After revoke' }); assert.equal(denied.result.status, 'denied'); assert.equal(prompts, 1);
|
|
82
|
+
const events = readFileSync(s.audit.path, 'utf8').trim().split('\n').map(line => JSON.parse(line));
|
|
83
|
+
assert.ok(events.some(e => e.event === 'permissions.changed' && e.automaticTransfer));
|
|
84
|
+
assert.ok(events.some(e => e.event === 'task.auto-approved' && e.stage === 'send'));
|
|
85
|
+
} finally { await s.close(); }
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('revoking during automatic send approval prevents output transmission', async () => {
|
|
89
|
+
let revoke = () => {}; const s = await session(async () => false, value => { if (value.startsWith('[AUTO send]')) revoke(); });
|
|
90
|
+
try {
|
|
91
|
+
s.engine.setPermissionMode('auto'); revoke = () => s.engine.setPermissionMode('ask');
|
|
92
|
+
const result = await s.operator.run(batch([script('console.log("REVOKED_BEFORE_TRANSFER")')]));
|
|
93
|
+
assert.equal(result.result.status, 'cancelled'); assert.deepEqual(result.outputs, {});
|
|
94
|
+
} finally { await s.close(); }
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('explicit batch cancel stops the process and remaining steps', async () => {
|
|
98
|
+
const pidPath = join(dir, randomUUID() + '.pid'), target = join(dir, randomUUID()); const s = await session();
|
|
99
|
+
try {
|
|
100
|
+
const id = await s.operator.start(batch([script(`require('fs').writeFileSync(${JSON.stringify(pidPath)},String(process.pid));setInterval(()=>{},1000)`), { operation: 'file.write', arguments: { path: target, content: 'no' } }]));
|
|
101
|
+
await waitFile(pidPath); await s.operator.cancel(id); const result = await s.operator.wait(id, 5000);
|
|
102
|
+
assert.equal('result' in result && result.result.status, 'cancelled'); assert.equal(existsSync(target), false);
|
|
103
|
+
} finally { await s.close(); }
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('all batch output shares a bounded budget and no later modifications occur after exhaustion', async () => {
|
|
107
|
+
const target = join(dir, randomUUID()); let bytes = 0; const s = await session(async request => { if (request.stage === 'send') { bytes = request.bytes!; return false; } return true; });
|
|
108
|
+
try {
|
|
109
|
+
const result = await s.operator.run(batch([script('process.stdout.write("z".repeat(6*1024*1024))'), script('process.stdout.write("z".repeat(6*1024*1024))'), { operation: 'file.write', arguments: { path: target, content: 'no' } }]));
|
|
110
|
+
assert.equal(result.result.status, 'denied'); assert.equal(bytes, 10 * 1024 * 1024); assert.equal(existsSync(target), false);
|
|
111
|
+
} finally { await s.close(); }
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('parameter tampering, remote permission fields, nested plans and duplicate step IDs are rejected', async () => {
|
|
115
|
+
const target = join(dir, randomUUID()); const s = await session(async request => { if (request.stage === 'execute' && request.task.operation === 'batch.run') request.task.arguments.steps[0].reason = 'changed'; return true; });
|
|
116
|
+
try {
|
|
117
|
+
const result = await s.operator.run(batch([{ operation: 'file.write', arguments: { path: target, content: 'no' } }])); assert.equal(result.result.status, 'error'); assert.equal(existsSync(target), false);
|
|
118
|
+
await assert.rejects(s.operator.start(batch([script('console.log(1)')], { permissions: 'auto' })));
|
|
119
|
+
await assert.rejects(s.operator.start(batch([{ operation: 'batch.run', arguments: { steps: [script('console.log(1)')] } }])));
|
|
120
|
+
const id = randomUUID(); const step = { ...script('console.log(1)'), id, reason: 'duplicate' };
|
|
121
|
+
assert.equal(taskSchema.safeParse({ ...batch([step, step]), id: randomUUID() }).success, false);
|
|
122
|
+
assert.equal(s.engine.permissionMode, 'ask');
|
|
123
|
+
} finally { await s.close(); }
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('a requested partial file range does not stop unrelated batch steps', async () => {
|
|
127
|
+
const target = join(dir, randomUUID()); writeFileSync(target, '0123456789'); const s = await session();
|
|
128
|
+
try {
|
|
129
|
+
const result = await s.operator.run(batch([{ operation: 'file.read', arguments: { path: target, length: 3 } }, script('console.log("after partial read")')]));
|
|
130
|
+
assert.equal(result.result.status, 'ok'); assert.equal(result.outputs['0.file'], '012'); assert.equal(result.outputs['1.stdout'], 'after partial read\n');
|
|
131
|
+
} finally { await s.close(); }
|
|
132
|
+
});
|
package/test/cli.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { test } from 'node:test';
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import { spawn } from 'node:child_process';
|
|
4
|
-
import { resolve } from 'node:path';
|
|
4
|
+
import { resolve, join } from 'node:path';
|
|
5
5
|
import { mkdtempSync } from 'node:fs';
|
|
6
6
|
import { tmpdir } from 'node:os';
|
|
7
7
|
import { createRelay } from '../dist/relay.js';
|
|
@@ -31,3 +31,59 @@ test('actual host CLI displays and enforces pairing, execution and transfer prom
|
|
|
31
31
|
assert.equal(result.result.status, 'ok'); assert.ok(pairing && executing && sending); assert.ok(text.includes('TRANSFER APPROVAL'));
|
|
32
32
|
} finally { clearTimeout(timer); peer?.close(); child.stdin.end('\x03'); if (child.pid) { try { process.kill(-child.pid, 'SIGTERM'); } catch {} } await relay.close(); }
|
|
33
33
|
});
|
|
34
|
+
|
|
35
|
+
test('host CLI auto selection skips task prompts, can revoke, and requires local confirmation to enable again', { skip: process.platform !== 'darwin', timeout: 15000 }, async () => {
|
|
36
|
+
const relay = createRelay({ port: 0 }); const url = `ws://127.0.0.1:${await relay.listen()}`;
|
|
37
|
+
const child = spawn('/bin/sh', ['-c', 'cat | /usr/bin/script -q /dev/null "$@"', 'diagnose-cli-auto-test', process.execPath, resolve('dist/cli.js'), 'start', url], { detached: true, env: { ...process.env, HOME: mkdtempSync(tmpdir() + '/diagnose-cli-auto-') }, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
38
|
+
let output = '', paired = false, executionPrompts = 0, transferPrompts = 0; let resolveCode!: (value: string) => void;
|
|
39
|
+
const code = new Promise<string>(resolve => { resolveCode = resolve; });
|
|
40
|
+
child.stderr.resume();
|
|
41
|
+
child.stdout.on('data', bytes => {
|
|
42
|
+
output += bytes.toString(); const match = /D1\.[A-Za-z0-9_-]{16}\.[A-Za-z0-9_-]{43}/.exec(output); if (match) resolveCode(match[0]);
|
|
43
|
+
if (!paired && output.includes('Allow this temporary diagnostic session?')) { paired = true; child.stdin.write('a\n'); }
|
|
44
|
+
const executionCount = output.split('Allow exactly this operation?').length - 1;
|
|
45
|
+
if (executionCount > executionPrompts) { executionPrompts = executionCount; child.stdin.write('n\n'); }
|
|
46
|
+
transferPrompts = output.split('Send these results (').length - 1;
|
|
47
|
+
});
|
|
48
|
+
const waitFor = async (predicate: () => boolean) => { for (let i = 0; i < 100 && !predicate(); i++) await new Promise(resolve => setTimeout(resolve, 20)); assert.ok(predicate()); };
|
|
49
|
+
let peer: Peer | undefined;
|
|
50
|
+
try {
|
|
51
|
+
peer = new Peer({ role: 'join', code: await code, relay: url }); const operator = new Operator(peer); await peer.open();
|
|
52
|
+
const plan = { operation: 'batch.run' as const, arguments: { steps: [{ operation: 'process.script', arguments: { interpreter: process.execPath, script: 'console.log("CLI auto")' } }, { operation: 'system.inspect', arguments: {} }] }, reason: 'Verify CLI auto plan' };
|
|
53
|
+
const result = await operator.run(plan); assert.equal(result.result.status, 'ok'); assert.equal(result.outputs['0.stdout'], 'CLI auto\n'); assert.equal(executionPrompts, 0); assert.equal(transferPrompts, 0); assert.ok(output.includes('without further prompts'));
|
|
54
|
+
child.stdin.write('permissions ask\n'); await waitFor(() => output.includes('Automatic permissions revoked'));
|
|
55
|
+
const denied = await operator.run({ operation: 'system.inspect', arguments: {}, reason: 'After revoke' }); assert.equal(denied.result.status, 'denied'); assert.equal(executionPrompts, 1);
|
|
56
|
+
child.stdin.write('permissions auto\n'); await waitFor(() => output.includes('within 60 seconds to grant'));
|
|
57
|
+
child.stdin.write('permissions allow-auto\n'); await waitFor(() => output.split('Permission mode: auto').length >= 3);
|
|
58
|
+
const again = await operator.run(plan); assert.equal(again.result.status, 'ok'); assert.equal(executionPrompts, 1); assert.equal(transferPrompts, 0);
|
|
59
|
+
} finally { peer?.close(); child.stdin.end('\x03'); if (child.pid) { try { process.kill(-child.pid, 'SIGTERM'); } catch {} } await relay.close(); }
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('terminal CLI attaches a native PTY and executes interactive input', { skip: process.platform !== 'darwin', timeout: 15000 }, async () => {
|
|
63
|
+
const { ProbeEngine } = await import('../dist/engine.js'); const { Audit } = await import('../dist/util.js'); const { createCode } = await import('../dist/crypto.js');
|
|
64
|
+
const directory = mkdtempSync(join(tmpdir(), 'diagnose-terminal-')), relay = createRelay({ port: 0 }), url = `ws://127.0.0.1:${await relay.listen()}`, code = createCode();
|
|
65
|
+
const host = new Peer({ role: 'host', code, relay: url, accept: async () => true }); const engine = new ProbeEngine(host, async () => true, new Audit(join(directory, 'audit')), () => {}); await host.open();
|
|
66
|
+
const child = spawn('/bin/sh', ['-c', 'cat | /usr/bin/script -q /dev/null "$@"', 'terminal-test', process.execPath, 'dist/cli.js', 'terminal', url], { detached: true, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
67
|
+
let output = '', stage = 0; child.stderr.resume();
|
|
68
|
+
child.stdout.on('data', bytes => { output += bytes.toString(); if (stage === 0 && output.includes('Pairing code:')) { stage++; child.stdin.write(code + '\n'); } if (stage === 1 && output.includes('Your label')) { stage++; child.stdin.write('terminal integration\n'); } if (stage === 2 && output.includes('[Ctrl+] detaches')) { stage++; child.stdin.write('printf "NATIVE_%s\\n" "TERMINAL_OK"; test -t 0 && printf "TTY_%s\\n" "CONFIRMED"; exit\n'); } if (stage === 3 && output.includes('NATIVE_TERMINAL_OK') && output.includes('TTY_CONFIRMED')) { stage++; child.stdin.end(); } });
|
|
69
|
+
const timer = setTimeout(() => { if (child.pid) try { process.kill(-child.pid, 'SIGTERM'); } catch {} }, 10000);
|
|
70
|
+
try { const exit = await new Promise<number | null>(resolve => child.once('exit', resolve)); assert.equal(exit, 0, output); assert.match(output, /NATIVE_TERMINAL_OK/); assert.match(output, /TTY_CONFIRMED/); }
|
|
71
|
+
finally { clearTimeout(timer); engine.cancelAll(); host.close(); if (child.pid) try { process.kill(-child.pid, 'SIGTERM'); } catch {} await relay.close(); }
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('host CLI re-pairs after disconnect and resumes retained process without rerunning it', { skip: process.platform !== 'darwin', timeout: 15000 }, async () => {
|
|
75
|
+
const childHome = mkdtempSync(join(tmpdir(), 'diagnose-repair-')), relay = createRelay({ port: 0 }), url = `ws://127.0.0.1:${await relay.listen()}`;
|
|
76
|
+
const child = spawn('/bin/sh', ['-c', 'cat | /usr/bin/script -q /dev/null "$@"', 'repair-test', process.execPath, 'dist/cli.js', 'start', url], { detached: true, env: { ...process.env, HOME: childHome }, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
77
|
+
let output = '', approvals = 0; child.stderr.resume(); child.stdout.on('data', bytes => { output += bytes.toString(); const count = output.split('Allow this temporary diagnostic session?').length - 1; if (count > approvals) { approvals = count; child.stdin.write('a\n'); } });
|
|
78
|
+
const codes = () => [...new Set(output.match(/D1\.[A-Za-z0-9_-]{16}\.[A-Za-z0-9_-]{43}/g) ?? [])];
|
|
79
|
+
const until = async (predicate: () => boolean) => { for (let n = 0; n < 200 && !predicate(); n++) await new Promise(resolve => setTimeout(resolve, 20)); assert.ok(predicate(), output); };
|
|
80
|
+
let peer: Peer | undefined;
|
|
81
|
+
try {
|
|
82
|
+
await until(() => codes().length === 1); peer = new Peer({ role: 'join', code: codes()[0], relay: url }); let operator = new Operator(peer); await peer.open();
|
|
83
|
+
const result = await operator.run({ operation: 'process.open', arguments: { executable: process.execPath, args: ['-e', 'console.log("REPAIR_READY");process.stdin.on("data",()=>{console.log("REPAIR_INPUT");process.exit(0)})'], retainOnDisconnect: true }, reason: 'Verify retained terminal after actual CLI reconnect' }); assert.equal(result.result.status, 'ok'); const job = result.result.data as { id: string; pid: number; resumeToken: string };
|
|
84
|
+
peer.close(); await until(() => codes().length >= 2); peer = new Peer({ role: 'join', code: codes()[1], relay: url }); operator = new Operator(peer); await peer.open();
|
|
85
|
+
const resumed = await operator.run({ operation: 'resource.resume', arguments: { id: job.id, resumeToken: job.resumeToken }, reason: 'Recover original process' }); assert.equal(resumed.result.status, 'ok'); assert.equal((resumed.result.data as { pid: number }).pid, job.pid); assert.equal(approvals, 2);
|
|
86
|
+
await operator.resource({ id: job.id, action: 'input', data: Buffer.from('continue\n').toString('base64') });
|
|
87
|
+
let text = ''; for (let n = 0; n < 100 && !text.includes('REPAIR_INPUT'); n++) { const value = await operator.resource({ id: job.id, action: 'read' }) as { records: { data: string }[] }; text = value.records.map(record => Buffer.from(record.data, 'base64').toString()).join(''); await new Promise(resolve => setTimeout(resolve, 20)); } assert.match(text, /REPAIR_INPUT/);
|
|
88
|
+
} finally { peer?.close(); child.stdin.end('\x03'); if (child.pid) try { process.kill(-child.pid, 'SIGTERM'); } catch {} await relay.close(); }
|
|
89
|
+
});
|
package/test/mcp.test.ts
CHANGED
|
@@ -19,11 +19,23 @@ test('real stdio MCP client pairs, starts, waits and disconnects', async () => {
|
|
|
19
19
|
const client = new Client({ name: 'acceptance', version: '1.0.0' });
|
|
20
20
|
const text = (result: any) => { assert.ok(!result.isError, JSON.stringify(result)); return JSON.parse(result.content[0].text); };
|
|
21
21
|
try {
|
|
22
|
-
await client.connect(transport); assert.deepEqual((await client.listTools()).tools.map(tool => tool.name).sort(), ['diagnose_cancel', 'diagnose_disconnect', 'diagnose_pair', 'diagnose_start', 'diagnose_wait']);
|
|
22
|
+
await client.connect(transport); assert.deepEqual((await client.listTools()).tools.map(tool => tool.name).sort(), ['diagnose_batch', 'diagnose_cancel', 'diagnose_disconnect', 'diagnose_forward', 'diagnose_pair', 'diagnose_process', 'diagnose_resource', 'diagnose_resume', 'diagnose_start', 'diagnose_transfer', 'diagnose_wait']);
|
|
23
23
|
assert.equal(text(await client.callTool({ name: 'diagnose_pair', arguments: { code, label: 'MCP test' } })).connected, true);
|
|
24
24
|
const { id } = text(await client.callTool({ name: 'diagnose_start', arguments: { operation: 'system.inspect', arguments: {}, reason: 'Verify MCP execution' } }));
|
|
25
25
|
const result = text(await client.callTool({ name: 'diagnose_wait', arguments: { id, timeoutMs: 5000, release: true } }));
|
|
26
26
|
assert.equal(result.result.status, 'ok'); assert.equal(result.result.data.platform, process.platform);
|
|
27
|
+
const plan = text(await client.callTool({ name: 'diagnose_batch', arguments: { reason: 'Verify MCP batch and script', steps: [{ operation: 'system.inspect', arguments: {} }, { operation: 'process.script', arguments: { interpreter: process.execPath, script: 'console.log("MCP batch script")' } }] } }));
|
|
28
|
+
const combined = text(await client.callTool({ name: 'diagnose_wait', arguments: { id: plan.id, timeoutMs: 5000, release: true } }));
|
|
29
|
+
assert.equal(combined.result.status, 'ok'); assert.equal(combined.result.data.completedSteps, 2); assert.equal(combined.outputs['1.stdout'], 'MCP batch script\n');
|
|
30
|
+
const liveTask = text(await client.callTool({ name: 'diagnose_process', arguments: { executable: process.execPath, args: ['-e', 'console.log("MCP_READY");process.stdin.on("data",x=>console.log("MCP_INPUT="+x.toString()));'], reason: 'Verify live input and output' } }));
|
|
31
|
+
const descriptor = text(await client.callTool({ name: 'diagnose_wait', arguments: { id: liveTask.id, timeoutMs: 5000 } })).result.data;
|
|
32
|
+
let cursor = 0, liveText = '';
|
|
33
|
+
for (let i = 0; i < 30 && !liveText.includes('MCP_READY'); i++) { const output = text(await client.callTool({ name: 'diagnose_resource', arguments: { id: descriptor.id, action: 'read', cursor, textOutput: true } })); cursor = output.cursor; liveText += output.records.map((entry: any) => entry.text).join(''); }
|
|
34
|
+
assert.ok(liveText.includes('MCP_READY'));
|
|
35
|
+
text(await client.callTool({ name: 'diagnose_resource', arguments: { id: descriptor.id, action: 'input', text: 'interactive\n' } }));
|
|
36
|
+
for (let i = 0; i < 30 && !liveText.includes('MCP_INPUT=interactive'); i++) { const output = text(await client.callTool({ name: 'diagnose_resource', arguments: { id: descriptor.id, action: 'read', cursor, textOutput: true } })); cursor = output.cursor; liveText += output.records.map((entry: any) => entry.text).join(''); }
|
|
37
|
+
assert.ok(liveText.includes('MCP_INPUT=interactive'));
|
|
38
|
+
text(await client.callTool({ name: 'diagnose_resource', arguments: { id: descriptor.id, action: 'close' } }));
|
|
27
39
|
assert.equal(text(await client.callTool({ name: 'diagnose_disconnect', arguments: {} })).disconnected, true);
|
|
28
40
|
} finally { await client.close(); host.close(); await relay.close(); }
|
|
29
41
|
});
|