@dashclaw/openclaw-plugin 1.6.1 → 1.6.3
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 +233 -219
- package/dist/index.js +141 -3
- package/dist/liveness-probe.d.ts +79 -0
- package/dist/liveness-probe.js +185 -0
- package/openclaw.plugin.json +3 -3
- package/package.json +80 -80
- package/src/index.ts +154 -2
- package/src/liveness-probe.ts +232 -0
package/src/index.ts
CHANGED
|
@@ -29,7 +29,10 @@ import {
|
|
|
29
29
|
type ActionRecord,
|
|
30
30
|
type GuardDecision,
|
|
31
31
|
} from 'dashclaw';
|
|
32
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
33
|
+
import { resolve as resolvePath } from 'node:path';
|
|
32
34
|
import { maybeAutoPair } from './auto-pairing.js';
|
|
35
|
+
import { runLivenessProbe, shouldProbeNow, PROBE_AGENT_ID } from './liveness-probe.js';
|
|
33
36
|
|
|
34
37
|
// ---------------------------------------------------------------------------
|
|
35
38
|
// Config
|
|
@@ -304,8 +307,17 @@ interface ActionClassification {
|
|
|
304
307
|
reversible: boolean;
|
|
305
308
|
systemsTouched: string[];
|
|
306
309
|
declaredGoal: string;
|
|
310
|
+
act?: GuardAct;
|
|
307
311
|
}
|
|
308
312
|
|
|
313
|
+
type GuardAct =
|
|
314
|
+
| {
|
|
315
|
+
kind: 'shell';
|
|
316
|
+
command: string;
|
|
317
|
+
script?: { path: string; content_excerpt?: string };
|
|
318
|
+
}
|
|
319
|
+
| { kind: 'file'; file: { path: string } };
|
|
320
|
+
|
|
309
321
|
const READONLY_COMMANDS = new Set([
|
|
310
322
|
'cat', 'head', 'tail', 'less', 'more', 'wc', 'file', 'stat', 'du', 'df',
|
|
311
323
|
'ls', 'tree', 'find', 'locate', 'which', 'whereis', 'type',
|
|
@@ -336,6 +348,71 @@ const DEPLOY_PATTERN = /(?:git\s+push|deploy|vercel|kubectl|terraform|docker\s+p
|
|
|
336
348
|
const DESTRUCTIVE_PATTERN = /(?:rm\s+-rf|DROP\s+TABLE|DELETE\s+FROM|TRUNCATE)/i;
|
|
337
349
|
const SENSITIVE_PATH_PATTERN = /(?:\.env|secret|credential|private_key|\.pem|id_rsa|\.key)/i;
|
|
338
350
|
|
|
351
|
+
// Interpreters/runners that indicate the next non-flag token is a local
|
|
352
|
+
// script file worth attaching as evidence (see incident: `node
|
|
353
|
+
// tmp/domain-buy.mjs <name>` was graded from self-declared risk alone
|
|
354
|
+
// because the plugin attached no `act`).
|
|
355
|
+
const SCRIPT_RUNNERS = new Set([
|
|
356
|
+
'node', 'nodejs', 'python', 'python2', 'python3', 'bash', 'sh', 'zsh',
|
|
357
|
+
'deno', 'bun', 'tsx', 'ts-node', 'ruby', 'perl', 'php', 'npx',
|
|
358
|
+
]);
|
|
359
|
+
|
|
360
|
+
const SCRIPT_CONTENT_MAX_BYTES = 64 * 1024;
|
|
361
|
+
const SCRIPT_EXCERPT_MAX_CHARS = 6144;
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Find the first chain segment (split on &&, ||, ;, |) whose command word,
|
|
365
|
+
* after stripping a leading `cd ...` segment, env assignments, `timeout N`,
|
|
366
|
+
* and `sudo`, is a known script runner — then return the next non-flag
|
|
367
|
+
* token as the candidate script path. Returns undefined when no segment
|
|
368
|
+
* qualifies.
|
|
369
|
+
*/
|
|
370
|
+
function findScriptPathToken(command: string): string | undefined {
|
|
371
|
+
const segments = command.split(/&&|\|\||;|\|/);
|
|
372
|
+
for (const segment of segments) {
|
|
373
|
+
const tokens = segment.trim().split(/\s+/).filter(Boolean);
|
|
374
|
+
if (tokens.length === 0 || tokens[0] === 'cd') continue;
|
|
375
|
+
while (tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[0])) tokens.shift();
|
|
376
|
+
if (tokens[0] === 'timeout') tokens.splice(0, 2);
|
|
377
|
+
if (tokens[0] === 'sudo') tokens.shift();
|
|
378
|
+
if (tokens.length === 0) continue;
|
|
379
|
+
const cmdWord = tokens[0].replace(/^.*[/\\]/, '');
|
|
380
|
+
if (!SCRIPT_RUNNERS.has(cmdWord)) continue;
|
|
381
|
+
const pathToken = tokens.slice(1).find((t) => !t.startsWith('-'));
|
|
382
|
+
if (pathToken) return pathToken;
|
|
383
|
+
}
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Attach the local script's content when a shell command invokes one, so
|
|
389
|
+
* the server's evidence classifier sees what actually runs instead of
|
|
390
|
+
* grading only the self-declared risk_score. Fail-soft everywhere: any
|
|
391
|
+
* resolution, stat, or read error yields no `script` at all. Sensitive
|
|
392
|
+
* paths (.env, credentials, keys) still attach `path` for policy matching
|
|
393
|
+
* but never their content.
|
|
394
|
+
*/
|
|
395
|
+
function detectLocalScript(
|
|
396
|
+
command: string,
|
|
397
|
+
workspace: string | undefined,
|
|
398
|
+
): { path: string; content_excerpt?: string } | undefined {
|
|
399
|
+
try {
|
|
400
|
+
const rawPath = findScriptPathToken(command);
|
|
401
|
+
if (!rawPath) return undefined;
|
|
402
|
+
const resolved = resolvePath(workspace || process.cwd(), rawPath);
|
|
403
|
+
if (SENSITIVE_PATH_PATTERN.test(rawPath)) {
|
|
404
|
+
return existsSync(resolved) ? { path: rawPath } : undefined;
|
|
405
|
+
}
|
|
406
|
+
if (!existsSync(resolved)) return undefined;
|
|
407
|
+
const stat = statSync(resolved);
|
|
408
|
+
if (!stat.isFile() || stat.size > SCRIPT_CONTENT_MAX_BYTES) return undefined;
|
|
409
|
+
const content = readFileSync(resolved, 'utf8');
|
|
410
|
+
return { path: rawPath, content_excerpt: content.slice(0, SCRIPT_EXCERPT_MAX_CHARS) };
|
|
411
|
+
} catch {
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
339
416
|
function classifyBash(
|
|
340
417
|
command: string | undefined,
|
|
341
418
|
defaultRisk: number,
|
|
@@ -420,12 +497,15 @@ function classifyToolCall(
|
|
|
420
497
|
toolName: string,
|
|
421
498
|
params: Record<string, unknown> | undefined,
|
|
422
499
|
config: PluginConfig,
|
|
500
|
+
workspace?: string,
|
|
423
501
|
): ActionClassification {
|
|
424
502
|
const defaultRisk = config.highRiskTools.has(toolName) ? 85 : config.riskScoreDefault;
|
|
425
503
|
const classified = TOOL_CLASSIFIERS
|
|
426
504
|
.map((classify) => classify(toolName, params, defaultRisk))
|
|
427
505
|
.find((result): result is ActionClassification => result !== null);
|
|
428
|
-
|
|
506
|
+
const base = classified ?? classifyDefaultTool(toolName, params, defaultRisk);
|
|
507
|
+
const act = buildGuardAct(toolName, params, workspace);
|
|
508
|
+
return act ? { ...base, act } : base;
|
|
429
509
|
}
|
|
430
510
|
|
|
431
511
|
type ToolClassifier = (
|
|
@@ -444,6 +524,42 @@ const REVIEW_TOOLS = new Set([
|
|
|
444
524
|
'image',
|
|
445
525
|
]);
|
|
446
526
|
|
|
527
|
+
/**
|
|
528
|
+
* Translate OpenClaw tool params into the narrow evidence-first guard wire
|
|
529
|
+
* contract. Do not truncate the command or the file path: a lost suffix
|
|
530
|
+
* could hide the risk. If a value exceeds the server contract, omit it and
|
|
531
|
+
* retain the normal classified request. File content is intentionally
|
|
532
|
+
* excluded, since a path is sufficient for protected/sensitive-path
|
|
533
|
+
* policies and content may itself be sensitive. Shell commands that
|
|
534
|
+
* execute a local script (e.g. `node tmp/domain-buy.mjs`) are the
|
|
535
|
+
* exception: the script's content is what actually runs, so its first
|
|
536
|
+
* 6144 chars are attached via `script.content_excerpt` (see
|
|
537
|
+
* detectLocalScript) — bounded per the server contract, not exempt from it.
|
|
538
|
+
*/
|
|
539
|
+
function buildGuardAct(
|
|
540
|
+
toolName: string,
|
|
541
|
+
params: Record<string, unknown> | undefined,
|
|
542
|
+
workspace?: string,
|
|
543
|
+
): GuardAct | undefined {
|
|
544
|
+
if (toolName === 'bash' || toolName === 'exec') {
|
|
545
|
+
const command = params?.command;
|
|
546
|
+
if (typeof command === 'string' && command.length > 0 && command.length <= 8192) {
|
|
547
|
+
const script = detectLocalScript(command, workspace);
|
|
548
|
+
return script ? { kind: 'shell', command, script } : { kind: 'shell', command };
|
|
549
|
+
}
|
|
550
|
+
return undefined;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (WRITE_TOOLS.has(toolName)) {
|
|
554
|
+
const path = params?.file_path ?? params?.path;
|
|
555
|
+
if (typeof path === 'string' && path.length > 0 && path.length <= 1024) {
|
|
556
|
+
return { kind: 'file', file: { path } };
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
|
|
447
563
|
const TOOL_CLASSIFIERS: ToolClassifier[] = [
|
|
448
564
|
classifyShellTool,
|
|
449
565
|
classifyWriteTool,
|
|
@@ -578,13 +694,46 @@ function registerOutcomeRecorder(api: OpenClawPluginApi, config: PluginConfig):
|
|
|
578
694
|
);
|
|
579
695
|
}
|
|
580
696
|
|
|
697
|
+
/**
|
|
698
|
+
* Enforcement-liveness probe on `session_start` (v8.2; per-seam since
|
|
699
|
+
* drizzle/0072). Proves the `before_tool_call` veto above actually HOLDS a
|
|
700
|
+
* synthetic action, and files the verdict under `runtime: openclaw` so this
|
|
701
|
+
* seam is scored on its own instead of inheriting another runtime's green.
|
|
702
|
+
*
|
|
703
|
+
* Fire-and-forget, like maybeAutoPair: session start is never delayed or
|
|
704
|
+
* failed by it. Self-throttled to once per 12h.
|
|
705
|
+
*
|
|
706
|
+
* It drives handleBeforeToolCall — the REAL handler, not a copy — under a
|
|
707
|
+
* synthetic identity. `smoke-` prefixed agents are excluded from every
|
|
708
|
+
* aggregate, and the identity swap is orthogonal to the enforcement mechanics
|
|
709
|
+
* being probed, so it costs no seam fidelity while keeping the probe's guard
|
|
710
|
+
* rows out of the operator's real numbers.
|
|
711
|
+
*/
|
|
712
|
+
function registerLivenessProbe(api: OpenClawPluginApi, config: PluginConfig): void {
|
|
713
|
+
api.on('session_start', async (_event, _ctx) => {
|
|
714
|
+
if (!config.dashclawUrl || !config.dashclawApiKey) return;
|
|
715
|
+
if (!shouldProbeNow()) return;
|
|
716
|
+
const probeConfig: PluginConfig = { ...config, agentId: PROBE_AGENT_ID };
|
|
717
|
+
void runLivenessProbe({
|
|
718
|
+
dashclawUrl: config.dashclawUrl,
|
|
719
|
+
dashclawApiKey: config.dashclawApiKey,
|
|
720
|
+
driveSeam: (event) => handleBeforeToolCall(event as ToolCallEvent, probeConfig),
|
|
721
|
+
}).catch((err) => {
|
|
722
|
+
console.warn(
|
|
723
|
+
`[dashclaw-governance] liveness probe failed: ${errorMessage(err) || 'unknown'}`
|
|
724
|
+
);
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
581
729
|
async function handleBeforeToolCall(
|
|
582
730
|
event: ToolCallEvent,
|
|
583
731
|
config: PluginConfig,
|
|
584
732
|
): Promise<HookResult> {
|
|
585
733
|
const { toolName, params, toolCallId, runId } = event;
|
|
586
734
|
const key = callKey(toolName, toolCallId, runId);
|
|
587
|
-
const
|
|
735
|
+
const workspace = typeof event.workspace === 'string' ? event.workspace : undefined;
|
|
736
|
+
const classification = classifyToolCall(toolName, params, config, workspace);
|
|
588
737
|
|
|
589
738
|
const client = getBeforeClient(config);
|
|
590
739
|
if ('result' in client) return client.result;
|
|
@@ -665,6 +814,7 @@ async function guardClassifiedAction(
|
|
|
665
814
|
declared_goal: classification.declaredGoal,
|
|
666
815
|
reversible: classification.reversible,
|
|
667
816
|
systems_touched: classification.systemsTouched,
|
|
817
|
+
...(classification.act ? { act: classification.act } : {}),
|
|
668
818
|
}),
|
|
669
819
|
};
|
|
670
820
|
} catch (err) {
|
|
@@ -732,6 +882,7 @@ async function createGovernanceAction(
|
|
|
732
882
|
risk_score: riskScore,
|
|
733
883
|
reversible,
|
|
734
884
|
systems_touched: systemsTouched,
|
|
885
|
+
...(ctx.classification.act ? { act: ctx.classification.act } : {}),
|
|
735
886
|
metadata: { openclaw_tool_name: ctx.toolName },
|
|
736
887
|
});
|
|
737
888
|
return {
|
|
@@ -1008,6 +1159,7 @@ const pluginEntry: ReturnType<typeof definePluginEntry> = definePluginEntry({
|
|
|
1008
1159
|
registerRunCleanup(api, config);
|
|
1009
1160
|
|
|
1010
1161
|
registerOutcomeRecorder(api, config);
|
|
1162
|
+
registerLivenessProbe(api, config);
|
|
1011
1163
|
},
|
|
1012
1164
|
});
|
|
1013
1165
|
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enforcement-liveness probe for the OpenClaw seam (roadmap v8.2, per-seam
|
|
3
|
+
* since drizzle/0072).
|
|
4
|
+
*
|
|
5
|
+
* Why this is TypeScript instead of the canonical `hooks/enforcement_liveness_probe.py`.
|
|
6
|
+
* That script probes a seam it can SPAWN: it reads a harness config file, runs
|
|
7
|
+
* the installed hook command as a subprocess, and emulates the harness's
|
|
8
|
+
* contract around it. OpenClaw's seam has none of those parts. Governance here
|
|
9
|
+
* is an in-process `before_tool_call` handler whose return value the gateway
|
|
10
|
+
* obeys directly — there is no config file to read, no command to run, and no
|
|
11
|
+
* exit code to interpret. A subprocess probe cannot reach it at all. So this
|
|
12
|
+
* module reuses the python probe's CONTRACT (same synthetic held action, same
|
|
13
|
+
* witness rule, same three verdicts, same POST payload) while driving the seam
|
|
14
|
+
* the only way it can be driven: by calling it.
|
|
15
|
+
*
|
|
16
|
+
* The witness rule is preserved exactly, and it is the point. The verdict never
|
|
17
|
+
* comes from what the guard DECIDED — the decision ledger is what kept lying in
|
|
18
|
+
* v4.72.1. It comes from whether the held action executed: if the seam does not
|
|
19
|
+
* veto, the probe performs the write the seam should have stopped, and the
|
|
20
|
+
* file's existence is the evidence.
|
|
21
|
+
*
|
|
22
|
+
* Verdicts:
|
|
23
|
+
* held the seam vetoed the synthetic action; the witness was never written.
|
|
24
|
+
* executed the seam let it through and the witness file exists.
|
|
25
|
+
* unprovable the seam ran but enforcement cannot be proven (no policy holds
|
|
26
|
+
* the probe action, or the probe could not run). Rendered broken
|
|
27
|
+
* on the surfaces — you cannot claim enforcement you cannot prove.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { mkdirSync, existsSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
31
|
+
import { homedir } from 'node:os';
|
|
32
|
+
import { join } from 'node:path';
|
|
33
|
+
|
|
34
|
+
/** `smoke-` is the established synthetic marker: these guard rows are excluded
|
|
35
|
+
* from every aggregate, exactly as the python probe's agent id is. */
|
|
36
|
+
export const PROBE_AGENT_ID = 'smoke-liveness-probe';
|
|
37
|
+
export const PROBE_RUNTIME = 'openclaw';
|
|
38
|
+
const THROTTLE_MS = 12 * 60 * 60 * 1000;
|
|
39
|
+
|
|
40
|
+
export interface SeamResult {
|
|
41
|
+
block?: boolean;
|
|
42
|
+
blockReason?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ProbeCheck {
|
|
46
|
+
id: string;
|
|
47
|
+
title: string;
|
|
48
|
+
status: 'pass' | 'fail';
|
|
49
|
+
detail?: string;
|
|
50
|
+
durationMs?: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface LivenessRun {
|
|
54
|
+
source: string;
|
|
55
|
+
runtime: string;
|
|
56
|
+
verdict: 'held' | 'executed' | 'unprovable';
|
|
57
|
+
detail: string;
|
|
58
|
+
witness: { path: string; executed: boolean };
|
|
59
|
+
checks: ProbeCheck[];
|
|
60
|
+
startedAt: string;
|
|
61
|
+
finishedAt: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ProbeOptions {
|
|
65
|
+
/** Drives the REAL before_tool_call handler with the synthetic event. */
|
|
66
|
+
driveSeam: (event: {
|
|
67
|
+
toolName: string;
|
|
68
|
+
params: Record<string, unknown>;
|
|
69
|
+
toolCallId: string;
|
|
70
|
+
runId: string;
|
|
71
|
+
}) => Promise<SeamResult | void>;
|
|
72
|
+
dashclawUrl: string;
|
|
73
|
+
dashclawApiKey: string;
|
|
74
|
+
source?: string;
|
|
75
|
+
/** Overridable so tests do not touch the real home directory. */
|
|
76
|
+
witnessRoot?: string;
|
|
77
|
+
now?: () => number;
|
|
78
|
+
fetchImpl?: typeof fetch;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function probeRoot(): string {
|
|
82
|
+
return join(homedir(), '.dashclaw', 'liveness-probe');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Once per 12h, mirroring the python probe's SessionStart throttle. Returns
|
|
87
|
+
* true when this session should probe, and stamps the marker as it does so —
|
|
88
|
+
* a failure to stamp never blocks the run.
|
|
89
|
+
*/
|
|
90
|
+
export function shouldProbeNow(root: string = probeRoot(), now: number = Date.now()): boolean {
|
|
91
|
+
const marker = join(root, '.last-run-openclaw');
|
|
92
|
+
try {
|
|
93
|
+
if (now - statSync(marker).mtimeMs < THROTTLE_MS) return false;
|
|
94
|
+
} catch {
|
|
95
|
+
// no marker yet: first run in this install
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
mkdirSync(root, { recursive: true });
|
|
99
|
+
writeFileSync(marker, String(now), 'utf8');
|
|
100
|
+
} catch {
|
|
101
|
+
// a throttle we cannot record is still a probe worth running
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The synthetic held action: a Write to a probe-owned `.env` path outside the
|
|
108
|
+
* workspace. Identical in shape to the python probe's, so both seams are
|
|
109
|
+
* judged against the same class of action — the path trips sensitive-path and
|
|
110
|
+
* outside-workspace risk boosts and matches protected-path/.env policy rules,
|
|
111
|
+
* while remaining a file the probe owns and may safely create when the seam
|
|
112
|
+
* fails open (broken enforcement is exactly when it WILL be written).
|
|
113
|
+
*/
|
|
114
|
+
function syntheticEvent(witnessPath: string, runId: string) {
|
|
115
|
+
return {
|
|
116
|
+
toolName: 'Write',
|
|
117
|
+
params: {
|
|
118
|
+
file_path: witnessPath,
|
|
119
|
+
content: `DashClaw enforcement-liveness probe witness ${runId} — if this file exists, an action the policy should have held EXECUTED.`,
|
|
120
|
+
},
|
|
121
|
+
toolCallId: `elp-${runId}`,
|
|
122
|
+
runId: `liveness-probe-${runId}`,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function runLivenessProbe(options: ProbeOptions): Promise<LivenessRun> {
|
|
127
|
+
const now = options.now ?? Date.now;
|
|
128
|
+
const startedAt = new Date(now()).toISOString();
|
|
129
|
+
const t0 = now();
|
|
130
|
+
// Not crypto: this only has to be unique per run so concurrent probes do not
|
|
131
|
+
// share a witness path.
|
|
132
|
+
const runId = `${now().toString(36)}${Math.floor(Math.random() * 1e6).toString(36)}`;
|
|
133
|
+
const witnessDir = join(options.witnessRoot ?? probeRoot(), runId);
|
|
134
|
+
const witnessPath = join(witnessDir, '.env');
|
|
135
|
+
const checks: ProbeCheck[] = [];
|
|
136
|
+
|
|
137
|
+
let verdict: LivenessRun['verdict'];
|
|
138
|
+
let detail: string;
|
|
139
|
+
let witnessExecuted = false;
|
|
140
|
+
|
|
141
|
+
const seamT0 = now();
|
|
142
|
+
let seam: SeamResult | void = undefined;
|
|
143
|
+
let seamError: string | null = null;
|
|
144
|
+
try {
|
|
145
|
+
seam = await options.driveSeam(syntheticEvent(witnessPath, runId));
|
|
146
|
+
} catch (err) {
|
|
147
|
+
seamError = err instanceof Error ? err.message : String(err);
|
|
148
|
+
}
|
|
149
|
+
const seamMs = now() - seamT0;
|
|
150
|
+
|
|
151
|
+
if (seamError) {
|
|
152
|
+
// The handler threw. OpenClaw treats a throwing hook as non-blocking, so
|
|
153
|
+
// the tool would have proceeded — same fail-open reading the python probe
|
|
154
|
+
// applies to a hook that cannot run.
|
|
155
|
+
verdict = 'unprovable';
|
|
156
|
+
detail = `The governance hook threw instead of deciding (${seamError}) — the gateway would have let the action proceed, and enforcement cannot be proven this run.`;
|
|
157
|
+
checks.push({ id: 'seam', title: 'before_tool_call threw', status: 'fail', detail, durationMs: seamMs });
|
|
158
|
+
} else if (seam && seam.block) {
|
|
159
|
+
verdict = 'held';
|
|
160
|
+
detail = seam.blockReason
|
|
161
|
+
? `Probe action vetoed at the seam: ${seam.blockReason}`
|
|
162
|
+
: 'Probe action vetoed at the seam; the witness was never executed.';
|
|
163
|
+
checks.push({ id: 'seam', title: 'Hook held the synthetic action', status: 'pass', detail, durationMs: seamMs });
|
|
164
|
+
} else {
|
|
165
|
+
// The seam let it through: execute the witness, exactly as the gateway
|
|
166
|
+
// would have. Its existence is the evidence — never the ledger.
|
|
167
|
+
try {
|
|
168
|
+
mkdirSync(witnessDir, { recursive: true });
|
|
169
|
+
writeFileSync(witnessPath, `executed by openclaw liveness probe ${runId} at ${new Date(now()).toISOString()}\n`, 'utf8');
|
|
170
|
+
witnessExecuted = existsSync(witnessPath);
|
|
171
|
+
} catch {
|
|
172
|
+
witnessExecuted = false;
|
|
173
|
+
}
|
|
174
|
+
verdict = 'unprovable';
|
|
175
|
+
detail =
|
|
176
|
+
'No policy held the probe action — the seam ran and allowed it. Add or scope a policy that blocks writes to *.env (protected paths or risk_threshold) so enforcement is provable.';
|
|
177
|
+
checks.push({
|
|
178
|
+
id: 'seam',
|
|
179
|
+
title: 'Hook let the synthetic action proceed',
|
|
180
|
+
status: 'fail',
|
|
181
|
+
detail,
|
|
182
|
+
durationMs: seamMs,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
checks.push({
|
|
187
|
+
id: 'witness',
|
|
188
|
+
title: 'Witness: did the held action execute?',
|
|
189
|
+
status: witnessExecuted ? 'fail' : 'pass',
|
|
190
|
+
detail: `${witnessPath} ${witnessExecuted ? 'EXISTS — the action executed' : 'absent'}`,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
rmSync(witnessDir, { recursive: true, force: true });
|
|
195
|
+
} catch {
|
|
196
|
+
// best effort — a leftover witness never changes a verdict already decided
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const run: LivenessRun = {
|
|
200
|
+
source: options.source ?? 'session-start',
|
|
201
|
+
runtime: PROBE_RUNTIME,
|
|
202
|
+
verdict,
|
|
203
|
+
detail,
|
|
204
|
+
witness: { path: witnessPath, executed: witnessExecuted },
|
|
205
|
+
checks,
|
|
206
|
+
startedAt,
|
|
207
|
+
finishedAt: new Date(now()).toISOString(),
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
await reportRun(run, options);
|
|
211
|
+
void t0;
|
|
212
|
+
return run;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function reportRun(run: LivenessRun, options: ProbeOptions): Promise<void> {
|
|
216
|
+
const base = (options.dashclawUrl || '').replace(/\/+$/, '');
|
|
217
|
+
if (!base || !options.dashclawApiKey) return;
|
|
218
|
+
const doFetch = options.fetchImpl ?? fetch;
|
|
219
|
+
try {
|
|
220
|
+
await doFetch(`${base}/api/enforcement-liveness`, {
|
|
221
|
+
method: 'POST',
|
|
222
|
+
headers: { 'content-type': 'application/json', 'x-api-key': options.dashclawApiKey },
|
|
223
|
+
body: JSON.stringify(run),
|
|
224
|
+
});
|
|
225
|
+
} catch (err) {
|
|
226
|
+
// A verdict we cannot file is not a reason to break session start. The
|
|
227
|
+
// seam going quiet is itself rendered `stale` by the fleet rollup.
|
|
228
|
+
console.warn(
|
|
229
|
+
`[dashclaw-governance] liveness verdict not reported: ${err instanceof Error ? err.message : 'unknown'}`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|