@dashclaw/openclaw-plugin 1.6.2 → 1.6.4
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 +246 -217
- package/dist/index.js +272 -19
- package/dist/liveness-probe.d.ts +79 -0
- package/dist/liveness-probe.js +185 -0
- package/openclaw.plugin.json +2 -2
- package/package.json +80 -80
- package/src/HOOK.md +9 -5
- package/src/dashclaw.d.ts +4 -2
- package/src/index.ts +351 -56
- package/src/liveness-probe.ts +232 -0
|
@@ -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
|
+
}
|