@masstensor/diagnose 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/deploy/Caddyfile +3 -0
- package/deploy/ecosystem.config.cjs +1 -0
- package/deploy/start-relay.sh +4 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +145 -0
- package/dist/crypto.d.ts +25 -0
- package/dist/crypto.js +60 -0
- package/dist/engine.d.ts +27 -0
- package/dist/engine.js +245 -0
- package/dist/executor.d.ts +18 -0
- package/dist/executor.js +208 -0
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +38 -0
- package/dist/operator.d.ts +21 -0
- package/dist/operator.js +69 -0
- package/dist/peer.d.ts +51 -0
- package/dist/peer.js +253 -0
- package/dist/relay-main.d.ts +2 -0
- package/dist/relay-main.js +6 -0
- package/dist/relay.d.ts +15 -0
- package/dist/relay.js +176 -0
- package/dist/schema.d.ts +298 -0
- package/dist/schema.js +32 -0
- package/dist/util.d.ts +11 -0
- package/dist/util.js +46 -0
- package/npm-shrinkwrap.json +2229 -0
- package/package.json +46 -4
- package/src/cli.ts +86 -0
- package/src/crypto.ts +54 -0
- package/src/engine.ts +156 -0
- package/src/executor.ts +131 -0
- package/src/mcp.ts +27 -0
- package/src/operator.ts +46 -0
- package/src/peer.ts +174 -0
- package/src/relay-main.ts +5 -0
- package/src/relay.ts +97 -0
- package/src/schema.ts +50 -0
- package/src/util.ts +39 -0
- package/src/vendor.d.ts +22 -0
- package/test/CACOPHONY-LICENSE +24 -0
- package/test/batch.test.ts +132 -0
- package/test/cli.test.ts +60 -0
- package/test/mcp.test.ts +32 -0
- package/test/noise-vector.json +40 -0
- package/test/probe.test.ts +131 -0
- package/test/serial.test.ts +24 -0
- package/test/vector.test.ts +32 -0
- package/tsconfig.json +8 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const MAX_FRAME: number;
|
|
3
|
+
export declare const CHUNK_SIZE: number;
|
|
4
|
+
export declare const MAX_OUTPUT: number;
|
|
5
|
+
export declare const atomicOperationNames: readonly ['system.inspect', 'file.read', 'file.write', 'process.run', 'process.script', 'serial.capture', 'serial.exchange'];
|
|
6
|
+
export declare const atomicTaskSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
7
|
+
id: z.ZodString;
|
|
8
|
+
reason: z.ZodString;
|
|
9
|
+
operation: z.ZodLiteral<"system.inspect">;
|
|
10
|
+
arguments: z.ZodObject<{}, z.core.$strict>;
|
|
11
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
12
|
+
id: z.ZodString;
|
|
13
|
+
reason: z.ZodString;
|
|
14
|
+
operation: z.ZodLiteral<"file.read">;
|
|
15
|
+
arguments: z.ZodObject<{
|
|
16
|
+
path: z.ZodString;
|
|
17
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
18
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
19
|
+
}, z.core.$strict>;
|
|
20
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
21
|
+
id: z.ZodString;
|
|
22
|
+
reason: z.ZodString;
|
|
23
|
+
operation: z.ZodLiteral<"file.write">;
|
|
24
|
+
arguments: z.ZodObject<{
|
|
25
|
+
path: z.ZodString;
|
|
26
|
+
content: z.ZodString;
|
|
27
|
+
expectedSha256: z.ZodOptional<z.ZodString>;
|
|
28
|
+
}, z.core.$strict>;
|
|
29
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
30
|
+
id: z.ZodString;
|
|
31
|
+
reason: z.ZodString;
|
|
32
|
+
operation: z.ZodLiteral<"process.run">;
|
|
33
|
+
arguments: z.ZodObject<{
|
|
34
|
+
executable: z.ZodString;
|
|
35
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
36
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
37
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
38
|
+
}, z.core.$strict>;
|
|
39
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
40
|
+
id: z.ZodString;
|
|
41
|
+
reason: z.ZodString;
|
|
42
|
+
operation: z.ZodLiteral<"process.script">;
|
|
43
|
+
arguments: z.ZodObject<{
|
|
44
|
+
interpreter: z.ZodString;
|
|
45
|
+
script: z.ZodString;
|
|
46
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
47
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
48
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
49
|
+
}, z.core.$strict>;
|
|
50
|
+
}, z.core.$strict>, ...z.ZodObject<{
|
|
51
|
+
id: z.ZodString;
|
|
52
|
+
reason: z.ZodString;
|
|
53
|
+
operation: z.ZodLiteral<"serial.capture" | "serial.exchange">;
|
|
54
|
+
arguments: z.ZodObject<{
|
|
55
|
+
path: z.ZodString;
|
|
56
|
+
baudRate: z.ZodDefault<z.ZodNumber>;
|
|
57
|
+
durationMs: z.ZodDefault<z.ZodNumber>;
|
|
58
|
+
dataHex: z.ZodOptional<z.ZodString>;
|
|
59
|
+
}, z.core.$strict>;
|
|
60
|
+
}, z.core.$strict>[]], "operation">;
|
|
61
|
+
export declare const batchTaskSchema: z.ZodObject<{
|
|
62
|
+
id: z.ZodString;
|
|
63
|
+
reason: z.ZodString;
|
|
64
|
+
operation: z.ZodLiteral<"batch.run">;
|
|
65
|
+
arguments: z.ZodObject<{
|
|
66
|
+
steps: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
67
|
+
id: z.ZodString;
|
|
68
|
+
reason: z.ZodString;
|
|
69
|
+
operation: z.ZodLiteral<"system.inspect">;
|
|
70
|
+
arguments: z.ZodObject<{}, z.core.$strict>;
|
|
71
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
72
|
+
id: z.ZodString;
|
|
73
|
+
reason: z.ZodString;
|
|
74
|
+
operation: z.ZodLiteral<"file.read">;
|
|
75
|
+
arguments: z.ZodObject<{
|
|
76
|
+
path: z.ZodString;
|
|
77
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
78
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
79
|
+
}, z.core.$strict>;
|
|
80
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
81
|
+
id: z.ZodString;
|
|
82
|
+
reason: z.ZodString;
|
|
83
|
+
operation: z.ZodLiteral<"file.write">;
|
|
84
|
+
arguments: z.ZodObject<{
|
|
85
|
+
path: z.ZodString;
|
|
86
|
+
content: z.ZodString;
|
|
87
|
+
expectedSha256: z.ZodOptional<z.ZodString>;
|
|
88
|
+
}, z.core.$strict>;
|
|
89
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
90
|
+
id: z.ZodString;
|
|
91
|
+
reason: z.ZodString;
|
|
92
|
+
operation: z.ZodLiteral<"process.run">;
|
|
93
|
+
arguments: z.ZodObject<{
|
|
94
|
+
executable: z.ZodString;
|
|
95
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
96
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
97
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
98
|
+
}, z.core.$strict>;
|
|
99
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
100
|
+
id: z.ZodString;
|
|
101
|
+
reason: z.ZodString;
|
|
102
|
+
operation: z.ZodLiteral<"process.script">;
|
|
103
|
+
arguments: z.ZodObject<{
|
|
104
|
+
interpreter: z.ZodString;
|
|
105
|
+
script: z.ZodString;
|
|
106
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
107
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
108
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
109
|
+
}, z.core.$strict>;
|
|
110
|
+
}, z.core.$strict>, ...z.ZodObject<{
|
|
111
|
+
id: z.ZodString;
|
|
112
|
+
reason: z.ZodString;
|
|
113
|
+
operation: z.ZodLiteral<"serial.capture" | "serial.exchange">;
|
|
114
|
+
arguments: z.ZodObject<{
|
|
115
|
+
path: z.ZodString;
|
|
116
|
+
baudRate: z.ZodDefault<z.ZodNumber>;
|
|
117
|
+
durationMs: z.ZodDefault<z.ZodNumber>;
|
|
118
|
+
dataHex: z.ZodOptional<z.ZodString>;
|
|
119
|
+
}, z.core.$strict>;
|
|
120
|
+
}, z.core.$strict>[]], "operation">>;
|
|
121
|
+
stopOnError: z.ZodDefault<z.ZodBoolean>;
|
|
122
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
123
|
+
}, z.core.$strict>;
|
|
124
|
+
}, z.core.$strict>;
|
|
125
|
+
export declare const taskSchema: z.ZodUnion<readonly [z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
126
|
+
id: z.ZodString;
|
|
127
|
+
reason: z.ZodString;
|
|
128
|
+
operation: z.ZodLiteral<"system.inspect">;
|
|
129
|
+
arguments: z.ZodObject<{}, z.core.$strict>;
|
|
130
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
131
|
+
id: z.ZodString;
|
|
132
|
+
reason: z.ZodString;
|
|
133
|
+
operation: z.ZodLiteral<"file.read">;
|
|
134
|
+
arguments: z.ZodObject<{
|
|
135
|
+
path: z.ZodString;
|
|
136
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
137
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
138
|
+
}, z.core.$strict>;
|
|
139
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
140
|
+
id: z.ZodString;
|
|
141
|
+
reason: z.ZodString;
|
|
142
|
+
operation: z.ZodLiteral<"file.write">;
|
|
143
|
+
arguments: z.ZodObject<{
|
|
144
|
+
path: z.ZodString;
|
|
145
|
+
content: z.ZodString;
|
|
146
|
+
expectedSha256: z.ZodOptional<z.ZodString>;
|
|
147
|
+
}, z.core.$strict>;
|
|
148
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
149
|
+
id: z.ZodString;
|
|
150
|
+
reason: z.ZodString;
|
|
151
|
+
operation: z.ZodLiteral<"process.run">;
|
|
152
|
+
arguments: z.ZodObject<{
|
|
153
|
+
executable: z.ZodString;
|
|
154
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
155
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
156
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
157
|
+
}, z.core.$strict>;
|
|
158
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
159
|
+
id: z.ZodString;
|
|
160
|
+
reason: z.ZodString;
|
|
161
|
+
operation: z.ZodLiteral<"process.script">;
|
|
162
|
+
arguments: z.ZodObject<{
|
|
163
|
+
interpreter: z.ZodString;
|
|
164
|
+
script: z.ZodString;
|
|
165
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
166
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
167
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
168
|
+
}, z.core.$strict>;
|
|
169
|
+
}, z.core.$strict>, ...z.ZodObject<{
|
|
170
|
+
id: z.ZodString;
|
|
171
|
+
reason: z.ZodString;
|
|
172
|
+
operation: z.ZodLiteral<"serial.capture" | "serial.exchange">;
|
|
173
|
+
arguments: z.ZodObject<{
|
|
174
|
+
path: z.ZodString;
|
|
175
|
+
baudRate: z.ZodDefault<z.ZodNumber>;
|
|
176
|
+
durationMs: z.ZodDefault<z.ZodNumber>;
|
|
177
|
+
dataHex: z.ZodOptional<z.ZodString>;
|
|
178
|
+
}, z.core.$strict>;
|
|
179
|
+
}, z.core.$strict>[]], "operation">, z.ZodObject<{
|
|
180
|
+
id: z.ZodString;
|
|
181
|
+
reason: z.ZodString;
|
|
182
|
+
operation: z.ZodLiteral<"batch.run">;
|
|
183
|
+
arguments: z.ZodObject<{
|
|
184
|
+
steps: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
185
|
+
id: z.ZodString;
|
|
186
|
+
reason: z.ZodString;
|
|
187
|
+
operation: z.ZodLiteral<"system.inspect">;
|
|
188
|
+
arguments: z.ZodObject<{}, z.core.$strict>;
|
|
189
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
190
|
+
id: z.ZodString;
|
|
191
|
+
reason: z.ZodString;
|
|
192
|
+
operation: z.ZodLiteral<"file.read">;
|
|
193
|
+
arguments: z.ZodObject<{
|
|
194
|
+
path: z.ZodString;
|
|
195
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
196
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
197
|
+
}, z.core.$strict>;
|
|
198
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
199
|
+
id: z.ZodString;
|
|
200
|
+
reason: z.ZodString;
|
|
201
|
+
operation: z.ZodLiteral<"file.write">;
|
|
202
|
+
arguments: z.ZodObject<{
|
|
203
|
+
path: z.ZodString;
|
|
204
|
+
content: z.ZodString;
|
|
205
|
+
expectedSha256: z.ZodOptional<z.ZodString>;
|
|
206
|
+
}, z.core.$strict>;
|
|
207
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
208
|
+
id: z.ZodString;
|
|
209
|
+
reason: z.ZodString;
|
|
210
|
+
operation: z.ZodLiteral<"process.run">;
|
|
211
|
+
arguments: z.ZodObject<{
|
|
212
|
+
executable: z.ZodString;
|
|
213
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
214
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
215
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
216
|
+
}, z.core.$strict>;
|
|
217
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
218
|
+
id: z.ZodString;
|
|
219
|
+
reason: z.ZodString;
|
|
220
|
+
operation: z.ZodLiteral<"process.script">;
|
|
221
|
+
arguments: z.ZodObject<{
|
|
222
|
+
interpreter: z.ZodString;
|
|
223
|
+
script: z.ZodString;
|
|
224
|
+
args: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
225
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
226
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
227
|
+
}, z.core.$strict>;
|
|
228
|
+
}, z.core.$strict>, ...z.ZodObject<{
|
|
229
|
+
id: z.ZodString;
|
|
230
|
+
reason: z.ZodString;
|
|
231
|
+
operation: z.ZodLiteral<"serial.capture" | "serial.exchange">;
|
|
232
|
+
arguments: z.ZodObject<{
|
|
233
|
+
path: z.ZodString;
|
|
234
|
+
baudRate: z.ZodDefault<z.ZodNumber>;
|
|
235
|
+
durationMs: z.ZodDefault<z.ZodNumber>;
|
|
236
|
+
dataHex: z.ZodOptional<z.ZodString>;
|
|
237
|
+
}, z.core.$strict>;
|
|
238
|
+
}, z.core.$strict>[]], "operation">>;
|
|
239
|
+
stopOnError: z.ZodDefault<z.ZodBoolean>;
|
|
240
|
+
timeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
241
|
+
}, z.core.$strict>;
|
|
242
|
+
}, z.core.$strict>]>;
|
|
243
|
+
export type AtomicTask = z.infer<typeof atomicTaskSchema>;
|
|
244
|
+
export type BatchTask = z.infer<typeof batchTaskSchema>;
|
|
245
|
+
export type Task = z.infer<typeof taskSchema>;
|
|
246
|
+
export type TaskInput = {
|
|
247
|
+
operation: Task['operation'];
|
|
248
|
+
arguments: Record<string, unknown>;
|
|
249
|
+
reason: string;
|
|
250
|
+
};
|
|
251
|
+
export type ApprovalStage = 'execute' | 'send';
|
|
252
|
+
export type Approval = {
|
|
253
|
+
stage: ApprovalStage;
|
|
254
|
+
task: Task;
|
|
255
|
+
digest: string;
|
|
256
|
+
preview: string;
|
|
257
|
+
bytes?: number;
|
|
258
|
+
signal?: AbortSignal;
|
|
259
|
+
};
|
|
260
|
+
export type ApprovalHandler = (request: Approval) => Promise<boolean>;
|
|
261
|
+
export type Result = {
|
|
262
|
+
status: 'ok' | 'error' | 'denied' | 'cancelled' | 'duplicate';
|
|
263
|
+
data?: unknown;
|
|
264
|
+
error?: string;
|
|
265
|
+
exitCode?: number | null;
|
|
266
|
+
truncated?: boolean;
|
|
267
|
+
};
|
|
268
|
+
export type WireMessage = {
|
|
269
|
+
type: 'session.accepted';
|
|
270
|
+
label: string;
|
|
271
|
+
fingerprint: string;
|
|
272
|
+
} | {
|
|
273
|
+
type: 'session.closed';
|
|
274
|
+
reason: string;
|
|
275
|
+
} | {
|
|
276
|
+
type: 'task.request';
|
|
277
|
+
task: Task;
|
|
278
|
+
} | {
|
|
279
|
+
type: 'task.started';
|
|
280
|
+
id: string;
|
|
281
|
+
digest: string;
|
|
282
|
+
} | {
|
|
283
|
+
type: 'task.output';
|
|
284
|
+
id: string;
|
|
285
|
+
stream: string;
|
|
286
|
+
chunk: string;
|
|
287
|
+
index: number;
|
|
288
|
+
} | {
|
|
289
|
+
type: 'task.result';
|
|
290
|
+
id: string;
|
|
291
|
+
result: Result;
|
|
292
|
+
} | {
|
|
293
|
+
type: 'task.cancel';
|
|
294
|
+
id: string;
|
|
295
|
+
} | {
|
|
296
|
+
type: 'ping' | 'pong';
|
|
297
|
+
};
|
|
298
|
+
export declare function parseMessage(value: unknown): WireMessage;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const MAX_FRAME = 64 * 1024;
|
|
3
|
+
export const CHUNK_SIZE = 12 * 1024;
|
|
4
|
+
export const MAX_OUTPUT = 10 * 1024 * 1024;
|
|
5
|
+
const path = z.string().min(1).max(4096);
|
|
6
|
+
const common = { id: z.string().uuid(), reason: z.string().min(1).max(1000) };
|
|
7
|
+
export const atomicOperationNames = ['system.inspect', 'file.read', 'file.write', 'process.run', 'process.script', 'serial.capture', 'serial.exchange'];
|
|
8
|
+
export const atomicTaskSchema = z.discriminatedUnion('operation', [
|
|
9
|
+
z.object({ ...common, operation: z.literal('system.inspect'), arguments: z.object({}).strict() }).strict(),
|
|
10
|
+
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
|
+
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(),
|
|
12
|
+
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(),
|
|
13
|
+
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(),
|
|
14
|
+
...['serial.capture', 'serial.exchange'].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()),
|
|
15
|
+
]);
|
|
16
|
+
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');
|
|
17
|
+
export const taskSchema = z.union([atomicTaskSchema, batchTaskSchema]);
|
|
18
|
+
export function parseMessage(value) {
|
|
19
|
+
const basic = z.object({ type: z.string() }).passthrough().parse(value);
|
|
20
|
+
switch (basic.type) {
|
|
21
|
+
case 'task.request': return { type: basic.type, task: taskSchema.parse(basic.task) };
|
|
22
|
+
case 'task.cancel': return { type: basic.type, id: z.string().uuid().parse(basic.id) };
|
|
23
|
+
case 'ping':
|
|
24
|
+
case 'pong': return { type: basic.type };
|
|
25
|
+
case 'session.accepted': return z.object({ type: z.literal('session.accepted'), label: z.string().max(120), fingerprint: z.string().max(100) }).strict().parse(value);
|
|
26
|
+
case 'session.closed': return z.object({ type: z.literal('session.closed'), reason: z.string().max(200) }).strict().parse(value);
|
|
27
|
+
case 'task.started': return z.object({ type: z.literal('task.started'), id: z.string().uuid(), digest: z.string().regex(/^[a-f0-9]{64}$/) }).strict().parse(value);
|
|
28
|
+
case 'task.output': return z.object({ type: z.literal('task.output'), id: z.string().uuid(), stream: z.string().max(20), chunk: z.string().max(CHUNK_SIZE * 2), index: z.number().int().min(0) }).strict().parse(value);
|
|
29
|
+
case 'task.result': return z.object({ type: z.literal('task.result'), id: z.string().uuid(), result: z.object({ status: z.enum(['ok', 'error', 'denied', 'cancelled', 'duplicate']), data: z.unknown().optional(), error: z.string().max(2000).optional(), exitCode: z.number().int().nullable().optional(), truncated: z.boolean().optional() }).strict() }).strict().parse(value);
|
|
30
|
+
default: throw new Error('Unsupported message type');
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function safeText(text: string): string;
|
|
2
|
+
export declare function redact(text: string): string;
|
|
3
|
+
export declare function canonical(value: unknown): string;
|
|
4
|
+
export declare const digest: (value: unknown) => string;
|
|
5
|
+
export declare const sha256: (bytes: Buffer) => string;
|
|
6
|
+
export declare function inside(root: string, path: string): boolean;
|
|
7
|
+
export declare class Audit {
|
|
8
|
+
readonly path: string;
|
|
9
|
+
constructor(path?: string);
|
|
10
|
+
record(event: string, details: Record<string, unknown>): void;
|
|
11
|
+
}
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { appendFileSync, mkdirSync, openSync, closeSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { dirname, resolve, relative, isAbsolute } from 'node:path';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
export function safeText(text) {
|
|
6
|
+
return text.replace(/\x1b\][^\x07]*(?:\x07|\x1b\\)/g, '').replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '').replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
|
|
7
|
+
}
|
|
8
|
+
export function redact(text) {
|
|
9
|
+
return safeText(text)
|
|
10
|
+
.replace(/("?(?:password|passwd|pswd|secret|token|session|authorization|api[_-]?key)"?\s*[:=]\s*)(?:"[^"\r\n]*"|'[^'\r\n]*'|[^\s,}\r\n]+)/gi, '$1[REDACTED]')
|
|
11
|
+
.replace(/\bD1\.[A-Za-z0-9_-]{16}\.[A-Za-z0-9_-]{43}\b/g, '[PAIRING CODE]')
|
|
12
|
+
.replace(/\b[a-f0-9]{32,}\b/gi, '[REDACTED]')
|
|
13
|
+
.replace(/-----BEGIN [^-]*PRIVATE KEY-----[\s\S]*?-----END [^-]*PRIVATE KEY-----/g, '[PRIVATE KEY]');
|
|
14
|
+
}
|
|
15
|
+
export function canonical(value) {
|
|
16
|
+
if (Array.isArray(value))
|
|
17
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
18
|
+
if (value !== null && typeof value === 'object')
|
|
19
|
+
return `{${Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(',')}}`;
|
|
20
|
+
return JSON.stringify(value);
|
|
21
|
+
}
|
|
22
|
+
export const digest = (value) => createHash('sha256').update(canonical(value)).digest('hex');
|
|
23
|
+
export const sha256 = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
24
|
+
export function inside(root, path) {
|
|
25
|
+
try {
|
|
26
|
+
const diff = relative(realpathSync(root), realpathSync(path));
|
|
27
|
+
return diff === '' || (!diff.startsWith('..') && !isAbsolute(diff));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class Audit {
|
|
34
|
+
path;
|
|
35
|
+
constructor(path = resolve(os.homedir(), '.diagnose', `audit-${Date.now()}.jsonl`)) {
|
|
36
|
+
this.path = path;
|
|
37
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
38
|
+
const fd = openSync(path, 'ax', 0o600);
|
|
39
|
+
closeSync(fd);
|
|
40
|
+
this.record('session.created', {});
|
|
41
|
+
}
|
|
42
|
+
record(event, details) {
|
|
43
|
+
// No command text, payload, credentials, or file contents are persisted by default.
|
|
44
|
+
appendFileSync(this.path, JSON.stringify({ at: new Date().toISOString(), event, ...details }) + '\n', { mode: 0o600, flush: true });
|
|
45
|
+
}
|
|
46
|
+
}
|