Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
547 lines (506 loc) · 18.7 KB
/
Copy pathindex.js
File metadata and controls
547 lines (506 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import { spawn as spawnProcess } from 'node:child_process';
import process from 'node:process';
import { prepareGuardProcess } from './dsh-process.js';
export const name = 'hol-guard-plugin';
export const inject = ['tools'];
const DEFAULT_TIMEOUT_MS = 10_000;
const MAX_TIMEOUT_MS = 30_000;
const MAX_CAPTURE_BYTES = 256 * 1024;
const MAX_PAYLOAD_BYTES = 256 * 1024;
const TERMINATION_GRACE_MS = 250;
const INCOMPLETE_REVIEW_REASON = 'HOL Guard review did not complete before DSH reached its monotonic tool guard.';
const EXECUTION_MUTATION_REASON = 'HOL Guard denied this DSH tool call because its identity, workspace, or arguments changed after review.';
const EXECUTION_IDENTITY_KEYS = Object.freeze([
'token',
'callId',
'rootCallId',
'name',
'arguments',
'agent',
'parent',
'deferContext',
'concludeTurn',
]);
function nonEmptyString(value) {
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
}
function boundedTimeout(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) return DEFAULT_TIMEOUT_MS;
return Math.min(MAX_TIMEOUT_MS, Math.max(250, Math.floor(parsed)));
}
function jsonSafe(value, seen = new WeakSet(), depth = 0) {
if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean') return value ?? null;
if (typeof value === 'number') return Number.isFinite(value) ? value : String(value);
if (typeof value === 'bigint') return value.toString();
if (typeof value !== 'object') return String(value);
if (depth >= 32) throw new Error('tool input exceeds the maximum nesting depth');
if (seen.has(value)) throw new Error('tool input contains a circular reference');
seen.add(value);
try {
if (Array.isArray(value)) return value.map((item) => jsonSafe(item, seen, depth + 1));
const output = {};
for (const [key, item] of Object.entries(value)) output[key] = jsonSafe(item, seen, depth + 1);
return output;
} finally {
seen.delete(value);
}
}
function workspaceFor(exec) {
const sessionCwd = exec?.agent?.session?.header?.cwd;
return nonEmptyString(sessionCwd) ?? process.cwd();
}
export function buildGuardPayload(exec) {
const toolName = nonEmptyString(exec?.name);
if (toolName === null) throw new Error('DSH tool execution is missing a tool name');
const rawArguments = exec?.arguments ?? exec?.args ?? {};
const payload = {
hook_event_name: 'PreToolUse',
hookEventName: 'PreToolUse',
tool_name: toolName,
tool_input: jsonSafe(rawArguments),
cwd: workspaceFor(exec),
};
const callId = nonEmptyString(exec?.callId);
if (callId !== null) payload.tool_use_id = callId;
const rootCallId = nonEmptyString(exec?.rootCallId);
if (rootCallId !== null) payload.root_tool_use_id = rootCallId;
const serialized = JSON.stringify(payload);
if (Buffer.byteLength(serialized, 'utf8') > MAX_PAYLOAD_BYTES) {
throw new Error(`HOL Guard payload exceeds ${MAX_PAYLOAD_BYTES} bytes`);
}
return { payload, serialized };
}
function lockExecutionIdentity(exec) {
if (!exec || typeof exec !== 'object') {
throw new Error('DSH tool execution is not an object');
}
for (const key of EXECUTION_IDENTITY_KEYS) {
const descriptor = Object.getOwnPropertyDescriptor(exec, key);
if (descriptor === undefined) continue;
if (!Object.hasOwn(descriptor, 'value')) {
throw new Error(`DSH execution identity field "${key}" is not a data property`);
}
if (descriptor.writable === false && descriptor.configurable === false) continue;
Object.defineProperty(exec, key, {
value: descriptor.value,
enumerable: descriptor.enumerable,
writable: false,
configurable: false,
});
}
}
function executionMutationReason(exec, reviewedSerialized) {
try {
const current = buildGuardPayload(exec).serialized;
return current === reviewedSerialized ? null : EXECUTION_MUTATION_REASON;
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
return `HOL Guard could not revalidate the reviewed DSH tool call: ${detail}`;
}
}
function parseJsonObject(candidate) {
try {
const parsed = JSON.parse(candidate);
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
} catch {
return null;
}
}
export function parseGuardResponse(stdout) {
const trimmed = typeof stdout === 'string' ? stdout.trim() : '';
if (!trimmed) return null;
const direct = parseJsonObject(trimmed);
if (direct !== null) return direct;
const lines = trimmed.split(/\r?\n/);
for (let index = lines.length - 1; index >= 0; index -= 1) {
const candidate = lines[index]?.trim();
if (!candidate) continue;
const parsed = parseJsonObject(candidate);
if (parsed !== null) return parsed;
}
return null;
}
function responseLayers(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return null;
const layers = [];
const queue = [payload];
const seen = new WeakSet();
while (queue.length > 0) {
if (layers.length >= 32) return null;
const current = queue.shift();
if (seen.has(current)) return null;
seen.add(current);
layers.push(current);
for (const key of ['data', 'payload', 'result']) {
const candidate = current[key];
if (candidate && typeof candidate === 'object' && !Array.isArray(candidate)) {
queue.push(candidate);
}
}
}
return layers;
}
function normalizedDecision(value) {
const decision = nonEmptyString(value);
return decision === null ? null : decision.toLowerCase();
}
function layerSignals(response) {
const hookOutput = response.hookSpecificOutput;
let hookDecision = null;
if (hookOutput && typeof hookOutput === 'object') {
hookDecision = normalizedDecision(hookOutput.permissionDecision);
}
const decisions = [
hookDecision,
normalizedDecision(response.permissionDecision),
normalizedDecision(response.decision),
].filter((decision) => decision !== null);
const policyAction = normalizedDecision(response.policy_action);
return {
response,
hookOutput,
hookDecision,
decisions,
policyAction,
hardDeny: response.blocked === true
|| response.continue === false
|| decisions.some((decision) => ['deny', 'block'].includes(decision))
|| ['block', 'sandbox-required'].includes(policyAction),
requiresApproval: decisions.includes('ask') || ['review', 'require-reapproval'].includes(policyAction),
allows: decisions.includes('allow') || (response.blocked === false && policyAction === 'allow'),
};
}
function signalReason(signal, kind) {
const hookReason = signal.hookOutput && typeof signal.hookOutput === 'object'
? nonEmptyString(signal.hookOutput.permissionDecisionReason)
: null;
const responseReason = nonEmptyString(signal.response.reason);
const reviewHint = nonEmptyString(signal.response.review_hint);
if (kind === 'deny') {
if (['deny', 'block'].includes(signal.hookDecision) && hookReason !== null) return hookReason;
return responseReason ?? reviewHint;
}
if (kind === 'ask') {
if (signal.hookDecision === 'ask' && hookReason !== null) return hookReason;
return responseReason ?? reviewHint;
}
if (signal.hookDecision === 'allow' && hookReason !== null) return hookReason;
return responseReason ?? reviewHint;
}
function signalMatchesKind(signal, kind) {
if (kind === 'deny') return signal.hardDeny;
if (kind === 'ask') return signal.requiresApproval;
return signal.allows;
}
function defaultDecisionReason(kind) {
if (kind === 'ask') return 'HOL Guard requires approval for this DSH tool call.';
if (kind === 'deny') return 'HOL Guard denied this DSH tool call.';
return 'HOL Guard allowed this DSH tool call.';
}
function reasonFromSignals(signals, kind) {
const matching = signals.filter((signal) => signalMatchesKind(signal, kind));
for (const signal of [...matching].reverse()) {
const reason = signalReason(signal, kind);
if (reason !== null) return reason;
}
return defaultDecisionReason(kind);
}
export function decisionFromGuardResponse(payload) {
const layers = responseLayers(payload);
if (layers === null) return null;
const signals = layers.map(layerSignals);
let kind = null;
if (signals.some((signal) => signal.hardDeny)) kind = 'deny';
else if (signals.some((signal) => signal.requiresApproval)) kind = 'ask';
else if (signals.some((signal) => signal.allows)) kind = 'allow';
if (kind === null) return null;
return { kind, reason: reasonFromSignals(signals, kind) };
}
function approvalFailure(reason) {
return { kind: 'deny', reason: `HOL Guard review failed closed: ${reason}` };
}
export async function resolveDshApproval(ctx, exec, decision) {
if (decision.kind !== 'ask') return decision;
if (!exec?.agent) {
return approvalFailure(`tool "${nonEmptyString(exec?.name) ?? 'unknown'}" requires approval, but the call has no DSH agent`);
}
let approval;
try {
approval = typeof ctx?.get === 'function' ? ctx.get('approval') : undefined;
} catch (error) {
return approvalFailure(`the DSH approval service could not be resolved: ${error instanceof Error ? error.message : String(error)}`);
}
if (!approval || typeof approval.request !== 'function') {
return approvalFailure('this DSH profile has no native approval service');
}
let outcome;
try {
outcome = await approval.request({
agent: exec.agent,
toolName: exec.name,
callId: exec.callId,
reason: decision.reason,
signal: exec.signal,
});
} catch (error) {
return approvalFailure(`the DSH approval request failed: ${error instanceof Error ? error.message : String(error)}`);
}
switch (outcome) {
case 'allowed-once':
return { kind: 'allow', reason: decision.reason };
case 'rejected':
return approvalFailure(`the user rejected tool "${nonEmptyString(exec.name) ?? 'unknown'}"`);
case 'cancelled':
return approvalFailure(`approval for tool "${nonEmptyString(exec.name) ?? 'unknown'}" was cancelled`);
case 'unavailable':
return approvalFailure(`no approval channel is available for tool "${nonEmptyString(exec.name) ?? 'unknown'}"`);
default:
return approvalFailure(`the DSH approval service returned an unknown outcome: ${String(outcome)}`);
}
}
async function terminateProcessTree(child) {
if (child.exitCode !== null || child.signalCode !== null) return;
const pid = child.pid;
if (process.platform === 'win32' && typeof pid === 'number') {
await new Promise((resolve) => {
let killer;
try {
killer = spawnProcess('taskkill', ['/PID', String(pid), '/T', '/F'], {
windowsHide: true,
stdio: 'ignore',
});
} catch {
resolve();
return;
}
const timer = setTimeout(resolve, TERMINATION_GRACE_MS);
timer.unref();
killer.once('error', () => {
if (timer !== undefined) clearTimeout(timer);
resolve();
});
killer.once('close', () => {
clearTimeout(timer);
resolve();
});
});
} else if (typeof pid === 'number') {
try {
process.kill(-pid, 'SIGTERM');
} catch {}
await new Promise((resolve) => setTimeout(resolve, TERMINATION_GRACE_MS));
try {
process.kill(-pid, 'SIGKILL');
} catch {}
}
try {
child.kill('SIGKILL');
} catch {}
}
export function spawnGuardHook({ command, args, cwd, env, input, signal, timeoutMs }) {
return new Promise((resolve, reject) => {
if (signal?.aborted) {
reject(new Error('DSH cancelled the tool call before HOL Guard review'));
return;
}
let child;
try {
child = spawnProcess(command, args, {
cwd,
env,
detached: process.platform !== 'win32',
shell: false,
windowsHide: true,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (error) {
reject(error);
return;
}
let settled = false;
let forcedError = null;
let timer;
let stdout = '';
let stderr = '';
const finish = (error, value) => {
if (settled) return;
settled = true;
clearTimeout(timer);
signal?.removeEventListener?.('abort', onAbort);
if (error) reject(error);
else resolve(value);
};
const append = (current, chunk, streamName) => {
const next = current + chunk;
if (Buffer.byteLength(next, 'utf8') > MAX_CAPTURE_BYTES) {
if (forcedError === null) {
forcedError = new Error(`HOL Guard ${streamName} exceeded ${MAX_CAPTURE_BYTES} bytes`);
void terminateProcessTree(child).finally(() => {
finish(forcedError);
});
}
return current;
}
return next;
};
child.stdout?.setEncoding('utf8');
child.stderr?.setEncoding('utf8');
child.stdout?.on('data', (chunk) => { stdout = append(stdout, chunk, 'stdout'); });
child.stderr?.on('data', (chunk) => { stderr = append(stderr, chunk, 'stderr'); });
child.once('error', (error) => {
if (forcedError !== null) return;
finish(error);
});
child.once('close', (code, closeSignal) => {
if (forcedError !== null) return;
finish(null, {
exitCode: code ?? 1,
signal: closeSignal,
stdout,
stderr,
});
});
const onAbort = () => {
forcedError = new Error('DSH cancelled the tool call during HOL Guard review');
void terminateProcessTree(child).finally(() => {
finish(forcedError);
});
};
signal?.addEventListener?.('abort', onAbort, { once: true });
timer = setTimeout(() => {
forcedError = new Error(`HOL Guard review timed out after ${timeoutMs} ms`);
void terminateProcessTree(child).finally(() => {
finish(forcedError);
});
}, timeoutMs);
timer.unref();
child.stdin?.on('error', (error) => finish(error));
child.stdin?.end(input);
});
}
async function evaluatePreparedToolExecution(exec, config, request) {
const timeoutMs = boundedTimeout(config.timeoutMs);
const workspace = request.payload.cwd;
let guard;
try {
guard = prepareGuardProcess(config, workspace);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
return { kind: 'deny', reason: `HOL Guard process trust validation failed closed: ${detail}` };
}
const args = [
...guard.prefixArgs,
'guard',
'hook',
'--harness',
'dsh',
'--workspace',
workspace,
];
if (guard.guardHome !== null) args.push('--guard-home', guard.guardHome);
let result;
try {
const runner = typeof config.runner === 'function' ? config.runner : spawnGuardHook;
result = await runner({
command: guard.executable,
args,
cwd: workspace,
env: guard.environment,
input: request.serialized,
signal: exec?.signal,
timeoutMs,
});
} catch (error) {
return { kind: 'deny', reason: `HOL Guard review failed closed: ${error instanceof Error ? error.message : String(error)}` };
}
const response = parseGuardResponse(result.stdout);
const decision = decisionFromGuardResponse(response);
if (decision === null) {
const detail = nonEmptyString(result.stderr) ?? `exit code ${result.exitCode}`;
return { kind: 'deny', reason: `HOL Guard returned no authoritative decision (${detail}).` };
}
if ((decision.kind === 'allow' || decision.kind === 'ask') && result.exitCode !== 0) {
const detail = nonEmptyString(result.stderr) ?? `exit code ${result.exitCode}`;
return { kind: 'deny', reason: `HOL Guard returned ${decision.kind} with a failing process (${detail}).` };
}
return decision;
}
export async function evaluateToolExecution(exec, config = {}) {
let request;
try {
request = buildGuardPayload(exec);
} catch (error) {
return { kind: 'deny', reason: `HOL Guard could not serialize this DSH tool call: ${error instanceof Error ? error.message : String(error)}` };
}
return evaluatePreparedToolExecution(exec, config, request);
}
export function apply(ctx, config = {}) {
if (!ctx || typeof ctx.on !== 'function') {
throw new Error('HOL Guard requires a valid DSH Cordis context.');
}
if (!ctx.tools || typeof ctx.tools.guard !== 'function') {
throw new Error('HOL Guard requires the DSH tools service with monotonic guard support.');
}
const decisions = new WeakMap();
ctx.tools.guard((exec) => {
const decision = decisions.get(exec);
if (decision === undefined) return INCOMPLETE_REVIEW_REASON;
if (decision.reviewedSerialized === null) return decision.reason;
const mutationReason = executionMutationReason(exec, decision.reviewedSerialized);
if (mutationReason !== null) return mutationReason;
if (decision.kind === 'allow') return undefined;
return decision.reason;
});
ctx.on('tools/pre-execute', async (exec, next) => {
let request;
try {
request = buildGuardPayload(exec);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
const reason = 'HOL Guard could not serialize this DSH tool call: ' + detail;
decisions.set(exec, { kind: 'deny', reason, reviewedSerialized: null });
return { kind: 'deny', reason };
}
decisions.set(exec, {
kind: 'deny',
reason: INCOMPLETE_REVIEW_REASON,
reviewedSerialized: request.serialized,
});
const reviewed = await evaluatePreparedToolExecution(exec, config, request);
const resolved = await resolveDshApproval(ctx, exec, reviewed);
if (resolved.kind !== 'allow') {
decisions.set(exec, { ...resolved, reviewedSerialized: request.serialized });
return { kind: 'deny', reason: resolved.reason };
}
const mutationBeforeDelegation = executionMutationReason(exec, request.serialized);
if (mutationBeforeDelegation !== null) {
decisions.set(exec, {
kind: 'deny',
reason: mutationBeforeDelegation,
reviewedSerialized: request.serialized,
});
return { kind: 'deny', reason: mutationBeforeDelegation };
}
try {
lockExecutionIdentity(exec);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
const reason = 'HOL Guard could not lock the reviewed DSH execution: ' + detail;
decisions.set(exec, { kind: 'deny', reason, reviewedSerialized: request.serialized });
return { kind: 'deny', reason };
}
decisions.set(exec, { ...resolved, reviewedSerialized: request.serialized });
const downstream = await next();
const mutationAfterDelegation = executionMutationReason(exec, request.serialized);
if (mutationAfterDelegation !== null) {
decisions.set(exec, {
kind: 'deny',
reason: mutationAfterDelegation,
reviewedSerialized: request.serialized,
});
return { kind: 'deny', reason: mutationAfterDelegation };
}
return downstream;
});
}