@masstensor/diagnose 0.1.0 → 0.2.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.
@@ -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
@@ -31,3 +31,30 @@ 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
+ });
package/test/mcp.test.ts CHANGED
@@ -19,11 +19,14 @@ 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_pair', 'diagnose_start', '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');
27
30
  assert.equal(text(await client.callTool({ name: 'diagnose_disconnect', arguments: {} })).disconnected, true);
28
31
  } finally { await client.close(); host.close(); await relay.close(); }
29
32
  });