@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
package/src/index.ts
CHANGED
|
@@ -29,7 +29,11 @@ import {
|
|
|
29
29
|
type ActionRecord,
|
|
30
30
|
type GuardDecision,
|
|
31
31
|
} from 'dashclaw';
|
|
32
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
33
|
+
import { randomUUID } from 'node:crypto';
|
|
34
|
+
import { resolve as resolvePath } from 'node:path';
|
|
32
35
|
import { maybeAutoPair } from './auto-pairing.js';
|
|
36
|
+
import { runLivenessProbe, shouldProbeNow, PROBE_AGENT_ID } from './liveness-probe.js';
|
|
33
37
|
|
|
34
38
|
// ---------------------------------------------------------------------------
|
|
35
39
|
// Config
|
|
@@ -43,9 +47,10 @@ interface PluginConfig {
|
|
|
43
47
|
// field. Without this, the server's `estimateCost` treats the usage as
|
|
44
48
|
// unpriceable and stores `cost_estimate = 0` — producing the "tokens
|
|
45
49
|
// but no cost" failure mode. Empty string disables the fallback.
|
|
46
|
-
defaultModel: string;
|
|
47
|
-
failClosed: boolean;
|
|
48
|
-
|
|
50
|
+
defaultModel: string;
|
|
51
|
+
failClosed: boolean;
|
|
52
|
+
requireExecutionClaims: boolean;
|
|
53
|
+
autoPairing: boolean;
|
|
49
54
|
riskScoreDefault: number;
|
|
50
55
|
highRiskTools: ReadonlySet<string>;
|
|
51
56
|
// Bounded human-approval wait. Codex's embedded dynamic-tool RPC enforces a
|
|
@@ -81,7 +86,7 @@ function stringSetFromConfig(value: unknown): ReadonlySet<string> {
|
|
|
81
86
|
);
|
|
82
87
|
}
|
|
83
88
|
|
|
84
|
-
function numberFromConfig(
|
|
89
|
+
function numberFromConfig(
|
|
85
90
|
value: unknown,
|
|
86
91
|
fallback: number,
|
|
87
92
|
allowNegative = true,
|
|
@@ -89,14 +94,19 @@ function numberFromConfig(
|
|
|
89
94
|
if (typeof value !== 'number') return fallback;
|
|
90
95
|
if (!Number.isFinite(value)) return fallback;
|
|
91
96
|
if (!allowNegative && value < 0) return fallback;
|
|
92
|
-
return value;
|
|
93
|
-
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function enabledEnvFlag(value: unknown): boolean {
|
|
101
|
+
return typeof value === 'string' && ['1', 'true'].includes(value.toLowerCase());
|
|
102
|
+
}
|
|
94
103
|
|
|
95
104
|
function resolveConfig(raw: Record<string, unknown> | undefined): PluginConfig {
|
|
96
105
|
const cfg = raw ?? {};
|
|
97
106
|
const env = typeof process !== 'undefined' && process?.env ? process.env : {};
|
|
98
107
|
|
|
99
|
-
const failClosed = cfg.failClosed !== false; // default true
|
|
108
|
+
const failClosed = cfg.failClosed !== false; // default true
|
|
109
|
+
const requireExecutionClaims = enabledEnvFlag(env.DASHCLAW_REQUIRE_EXECUTION_CLAIMS);
|
|
100
110
|
const autoPairing = cfg.autoPairing !== false; // default true
|
|
101
111
|
const riskScoreDefault = numberFromConfig(cfg.riskScoreDefault, 50);
|
|
102
112
|
const highRiskTools = stringSetFromConfig(cfg.highRiskTools);
|
|
@@ -123,9 +133,10 @@ function resolveConfig(raw: Record<string, unknown> | undefined): PluginConfig {
|
|
|
123
133
|
dashclawUrl,
|
|
124
134
|
dashclawApiKey,
|
|
125
135
|
agentId,
|
|
126
|
-
defaultModel,
|
|
127
|
-
failClosed,
|
|
128
|
-
|
|
136
|
+
defaultModel,
|
|
137
|
+
failClosed,
|
|
138
|
+
requireExecutionClaims,
|
|
139
|
+
autoPairing,
|
|
129
140
|
riskScoreDefault,
|
|
130
141
|
highRiskTools,
|
|
131
142
|
approvalWaitMs,
|
|
@@ -304,8 +315,17 @@ interface ActionClassification {
|
|
|
304
315
|
reversible: boolean;
|
|
305
316
|
systemsTouched: string[];
|
|
306
317
|
declaredGoal: string;
|
|
318
|
+
act?: GuardAct;
|
|
307
319
|
}
|
|
308
320
|
|
|
321
|
+
type GuardAct =
|
|
322
|
+
| {
|
|
323
|
+
kind: 'shell';
|
|
324
|
+
command: string;
|
|
325
|
+
script?: { path: string; content_excerpt?: string };
|
|
326
|
+
}
|
|
327
|
+
| { kind: 'file'; file: { path: string } };
|
|
328
|
+
|
|
309
329
|
const READONLY_COMMANDS = new Set([
|
|
310
330
|
'cat', 'head', 'tail', 'less', 'more', 'wc', 'file', 'stat', 'du', 'df',
|
|
311
331
|
'ls', 'tree', 'find', 'locate', 'which', 'whereis', 'type',
|
|
@@ -336,6 +356,71 @@ const DEPLOY_PATTERN = /(?:git\s+push|deploy|vercel|kubectl|terraform|docker\s+p
|
|
|
336
356
|
const DESTRUCTIVE_PATTERN = /(?:rm\s+-rf|DROP\s+TABLE|DELETE\s+FROM|TRUNCATE)/i;
|
|
337
357
|
const SENSITIVE_PATH_PATTERN = /(?:\.env|secret|credential|private_key|\.pem|id_rsa|\.key)/i;
|
|
338
358
|
|
|
359
|
+
// Interpreters/runners that indicate the next non-flag token is a local
|
|
360
|
+
// script file worth attaching as evidence (see incident: `node
|
|
361
|
+
// tmp/domain-buy.mjs <name>` was graded from self-declared risk alone
|
|
362
|
+
// because the plugin attached no `act`).
|
|
363
|
+
const SCRIPT_RUNNERS = new Set([
|
|
364
|
+
'node', 'nodejs', 'python', 'python2', 'python3', 'bash', 'sh', 'zsh',
|
|
365
|
+
'deno', 'bun', 'tsx', 'ts-node', 'ruby', 'perl', 'php', 'npx',
|
|
366
|
+
]);
|
|
367
|
+
|
|
368
|
+
const SCRIPT_CONTENT_MAX_BYTES = 64 * 1024;
|
|
369
|
+
const SCRIPT_EXCERPT_MAX_CHARS = 6144;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Find the first chain segment (split on &&, ||, ;, |) whose command word,
|
|
373
|
+
* after stripping a leading `cd ...` segment, env assignments, `timeout N`,
|
|
374
|
+
* and `sudo`, is a known script runner — then return the next non-flag
|
|
375
|
+
* token as the candidate script path. Returns undefined when no segment
|
|
376
|
+
* qualifies.
|
|
377
|
+
*/
|
|
378
|
+
function findScriptPathToken(command: string): string | undefined {
|
|
379
|
+
const segments = command.split(/&&|\|\||;|\|/);
|
|
380
|
+
for (const segment of segments) {
|
|
381
|
+
const tokens = segment.trim().split(/\s+/).filter(Boolean);
|
|
382
|
+
if (tokens.length === 0 || tokens[0] === 'cd') continue;
|
|
383
|
+
while (tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[0])) tokens.shift();
|
|
384
|
+
if (tokens[0] === 'timeout') tokens.splice(0, 2);
|
|
385
|
+
if (tokens[0] === 'sudo') tokens.shift();
|
|
386
|
+
if (tokens.length === 0) continue;
|
|
387
|
+
const cmdWord = tokens[0].replace(/^.*[/\\]/, '');
|
|
388
|
+
if (!SCRIPT_RUNNERS.has(cmdWord)) continue;
|
|
389
|
+
const pathToken = tokens.slice(1).find((t) => !t.startsWith('-'));
|
|
390
|
+
if (pathToken) return pathToken;
|
|
391
|
+
}
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Attach the local script's content when a shell command invokes one, so
|
|
397
|
+
* the server's evidence classifier sees what actually runs instead of
|
|
398
|
+
* grading only the self-declared risk_score. Fail-soft everywhere: any
|
|
399
|
+
* resolution, stat, or read error yields no `script` at all. Sensitive
|
|
400
|
+
* paths (.env, credentials, keys) still attach `path` for policy matching
|
|
401
|
+
* but never their content.
|
|
402
|
+
*/
|
|
403
|
+
function detectLocalScript(
|
|
404
|
+
command: string,
|
|
405
|
+
workspace: string | undefined,
|
|
406
|
+
): { path: string; content_excerpt?: string } | undefined {
|
|
407
|
+
try {
|
|
408
|
+
const rawPath = findScriptPathToken(command);
|
|
409
|
+
if (!rawPath) return undefined;
|
|
410
|
+
const resolved = resolvePath(workspace || process.cwd(), rawPath);
|
|
411
|
+
if (SENSITIVE_PATH_PATTERN.test(rawPath)) {
|
|
412
|
+
return existsSync(resolved) ? { path: rawPath } : undefined;
|
|
413
|
+
}
|
|
414
|
+
if (!existsSync(resolved)) return undefined;
|
|
415
|
+
const stat = statSync(resolved);
|
|
416
|
+
if (!stat.isFile() || stat.size > SCRIPT_CONTENT_MAX_BYTES) return undefined;
|
|
417
|
+
const content = readFileSync(resolved, 'utf8');
|
|
418
|
+
return { path: rawPath, content_excerpt: content.slice(0, SCRIPT_EXCERPT_MAX_CHARS) };
|
|
419
|
+
} catch {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
339
424
|
function classifyBash(
|
|
340
425
|
command: string | undefined,
|
|
341
426
|
defaultRisk: number,
|
|
@@ -420,12 +505,15 @@ function classifyToolCall(
|
|
|
420
505
|
toolName: string,
|
|
421
506
|
params: Record<string, unknown> | undefined,
|
|
422
507
|
config: PluginConfig,
|
|
508
|
+
workspace?: string,
|
|
423
509
|
): ActionClassification {
|
|
424
510
|
const defaultRisk = config.highRiskTools.has(toolName) ? 85 : config.riskScoreDefault;
|
|
425
511
|
const classified = TOOL_CLASSIFIERS
|
|
426
512
|
.map((classify) => classify(toolName, params, defaultRisk))
|
|
427
513
|
.find((result): result is ActionClassification => result !== null);
|
|
428
|
-
|
|
514
|
+
const base = classified ?? classifyDefaultTool(toolName, params, defaultRisk);
|
|
515
|
+
const act = buildGuardAct(toolName, params, workspace);
|
|
516
|
+
return act ? { ...base, act } : base;
|
|
429
517
|
}
|
|
430
518
|
|
|
431
519
|
type ToolClassifier = (
|
|
@@ -444,6 +532,42 @@ const REVIEW_TOOLS = new Set([
|
|
|
444
532
|
'image',
|
|
445
533
|
]);
|
|
446
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Translate OpenClaw tool params into the narrow evidence-first guard wire
|
|
537
|
+
* contract. Do not truncate the command or the file path: a lost suffix
|
|
538
|
+
* could hide the risk. If a value exceeds the server contract, omit it and
|
|
539
|
+
* retain the normal classified request. File content is intentionally
|
|
540
|
+
* excluded, since a path is sufficient for protected/sensitive-path
|
|
541
|
+
* policies and content may itself be sensitive. Shell commands that
|
|
542
|
+
* execute a local script (e.g. `node tmp/domain-buy.mjs`) are the
|
|
543
|
+
* exception: the script's content is what actually runs, so its first
|
|
544
|
+
* 6144 chars are attached via `script.content_excerpt` (see
|
|
545
|
+
* detectLocalScript) — bounded per the server contract, not exempt from it.
|
|
546
|
+
*/
|
|
547
|
+
function buildGuardAct(
|
|
548
|
+
toolName: string,
|
|
549
|
+
params: Record<string, unknown> | undefined,
|
|
550
|
+
workspace?: string,
|
|
551
|
+
): GuardAct | undefined {
|
|
552
|
+
if (toolName === 'bash' || toolName === 'exec') {
|
|
553
|
+
const command = params?.command;
|
|
554
|
+
if (typeof command === 'string' && command.length > 0 && command.length <= 8192) {
|
|
555
|
+
const script = detectLocalScript(command, workspace);
|
|
556
|
+
return script ? { kind: 'shell', command, script } : { kind: 'shell', command };
|
|
557
|
+
}
|
|
558
|
+
return undefined;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (WRITE_TOOLS.has(toolName)) {
|
|
562
|
+
const path = params?.file_path ?? params?.path;
|
|
563
|
+
if (typeof path === 'string' && path.length > 0 && path.length <= 1024) {
|
|
564
|
+
return { kind: 'file', file: { path } };
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return undefined;
|
|
569
|
+
}
|
|
570
|
+
|
|
447
571
|
const TOOL_CLASSIFIERS: ToolClassifier[] = [
|
|
448
572
|
classifyShellTool,
|
|
449
573
|
classifyWriteTool,
|
|
@@ -549,10 +673,10 @@ interface AgentEndContext {
|
|
|
549
673
|
runId?: string;
|
|
550
674
|
}
|
|
551
675
|
|
|
552
|
-
interface CreatedAction {
|
|
553
|
-
actionId
|
|
554
|
-
status?: string;
|
|
555
|
-
}
|
|
676
|
+
interface CreatedAction {
|
|
677
|
+
actionId: string;
|
|
678
|
+
status?: string;
|
|
679
|
+
}
|
|
556
680
|
|
|
557
681
|
function registerGovernanceGate(api: OpenClawPluginApi, config: PluginConfig): void {
|
|
558
682
|
api.on('before_tool_call', async (event, _ctx) =>
|
|
@@ -578,13 +702,54 @@ function registerOutcomeRecorder(api: OpenClawPluginApi, config: PluginConfig):
|
|
|
578
702
|
);
|
|
579
703
|
}
|
|
580
704
|
|
|
705
|
+
/**
|
|
706
|
+
* Enforcement-liveness probe on `session_start` (v8.2; per-seam since
|
|
707
|
+
* drizzle/0072). Proves the `before_tool_call` veto above actually HOLDS a
|
|
708
|
+
* synthetic action, and files the verdict under `runtime: openclaw` so this
|
|
709
|
+
* seam is scored on its own instead of inheriting another runtime's green.
|
|
710
|
+
*
|
|
711
|
+
* Fire-and-forget, like maybeAutoPair: session start is never delayed or
|
|
712
|
+
* failed by it. Self-throttled to once per 12h.
|
|
713
|
+
*
|
|
714
|
+
* It drives handleBeforeToolCall — the REAL handler, not a copy — under a
|
|
715
|
+
* synthetic identity. `smoke-` prefixed agents are excluded from every
|
|
716
|
+
* aggregate, and the identity swap is orthogonal to the enforcement mechanics
|
|
717
|
+
* being probed, so it costs no seam fidelity while keeping the probe's guard
|
|
718
|
+
* rows out of the operator's real numbers.
|
|
719
|
+
*/
|
|
720
|
+
function registerLivenessProbe(api: OpenClawPluginApi, config: PluginConfig): void {
|
|
721
|
+
api.on('session_start', async (_event, _ctx) => {
|
|
722
|
+
if (!config.dashclawUrl || !config.dashclawApiKey) return;
|
|
723
|
+
if (!shouldProbeNow()) return;
|
|
724
|
+
const probeConfig: PluginConfig = { ...config, agentId: PROBE_AGENT_ID };
|
|
725
|
+
void runLivenessProbe({
|
|
726
|
+
dashclawUrl: config.dashclawUrl,
|
|
727
|
+
dashclawApiKey: config.dashclawApiKey,
|
|
728
|
+
driveSeam: (event) => handleBeforeToolCall(event as ToolCallEvent, probeConfig),
|
|
729
|
+
}).catch((err) => {
|
|
730
|
+
console.warn(
|
|
731
|
+
`[dashclaw-governance] liveness probe failed: ${errorMessage(err) || 'unknown'}`
|
|
732
|
+
);
|
|
733
|
+
});
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
|
|
581
737
|
async function handleBeforeToolCall(
|
|
582
738
|
event: ToolCallEvent,
|
|
583
739
|
config: PluginConfig,
|
|
584
740
|
): Promise<HookResult> {
|
|
585
|
-
const { toolName, params, toolCallId, runId } = event;
|
|
586
|
-
const key = callKey(toolName, toolCallId, runId);
|
|
587
|
-
|
|
741
|
+
const { toolName, params, toolCallId, runId } = event;
|
|
742
|
+
const key = callKey(toolName, toolCallId, runId);
|
|
743
|
+
if (!toolCallId && pendingActions.has(key)) {
|
|
744
|
+
return {
|
|
745
|
+
block: true,
|
|
746
|
+
blockReason:
|
|
747
|
+
'OpenClaw did not provide a stable tool call ID and another matching call is still open. ' +
|
|
748
|
+
'Execution was interrupted because its outcome could not be correlated safely.',
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
const workspace = typeof event.workspace === 'string' ? event.workspace : undefined;
|
|
752
|
+
const classification = classifyToolCall(toolName, params, config, workspace);
|
|
588
753
|
|
|
589
754
|
const client = getBeforeClient(config);
|
|
590
755
|
if ('result' in client) return client.result;
|
|
@@ -652,21 +817,31 @@ async function maybeStartSession(
|
|
|
652
817
|
}
|
|
653
818
|
}
|
|
654
819
|
|
|
655
|
-
async function guardClassifiedAction(
|
|
656
|
-
client: DashClaw,
|
|
657
|
-
classification: ActionClassification,
|
|
658
|
-
config: PluginConfig,
|
|
659
|
-
): Promise<{ value:
|
|
660
|
-
try {
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
820
|
+
async function guardClassifiedAction(
|
|
821
|
+
client: DashClaw,
|
|
822
|
+
classification: ActionClassification,
|
|
823
|
+
config: PluginConfig,
|
|
824
|
+
): Promise<{ value: ClaimAwareGuardDecision } | { result: HookResult }> {
|
|
825
|
+
try {
|
|
826
|
+
const raw = await client.guard({
|
|
827
|
+
action_type: classification.actionType,
|
|
828
|
+
risk_score: classification.riskScore,
|
|
829
|
+
declared_goal: classification.declaredGoal,
|
|
830
|
+
reversible: classification.reversible,
|
|
831
|
+
systems_touched: classification.systemsTouched,
|
|
832
|
+
client_capabilities: ['execution_claims'],
|
|
833
|
+
...(classification.act ? { act: classification.act } : {}),
|
|
834
|
+
});
|
|
835
|
+
const validated = validateGuardDecision(raw, config.requireExecutionClaims);
|
|
836
|
+
if ('reason' in validated) {
|
|
837
|
+
return {
|
|
838
|
+
result: {
|
|
839
|
+
block: true,
|
|
840
|
+
blockReason: validated.reason,
|
|
841
|
+
},
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
return { value: validated.value };
|
|
670
845
|
} catch (err) {
|
|
671
846
|
const msg = errorMessage(err) || 'unknown error';
|
|
672
847
|
if (config.failClosed) {
|
|
@@ -679,8 +854,40 @@ async function guardClassifiedAction(
|
|
|
679
854
|
}
|
|
680
855
|
console.warn(`[dashclaw-governance] guard call failed (fail-open): ${msg}`);
|
|
681
856
|
return { result: undefined };
|
|
682
|
-
}
|
|
683
|
-
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
type ClaimAwareGuardDecision = GuardDecision & {
|
|
861
|
+
execution_claim_required?: boolean;
|
|
862
|
+
claim_protocol?: number;
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
const GUARD_DECISIONS = new Set(['allow', 'block', 'warn', 'require_approval']);
|
|
866
|
+
const EXECUTION_CLAIM_TIMEOUT_MS = 30_000;
|
|
867
|
+
|
|
868
|
+
function validateGuardDecision(value: unknown, requireExecutionClaims: boolean):
|
|
869
|
+
| { value: ClaimAwareGuardDecision }
|
|
870
|
+
| { reason: string } {
|
|
871
|
+
if (!value || typeof value !== 'object') {
|
|
872
|
+
return { reason: 'DashClaw returned a malformed guard response; tool execution was interrupted.' };
|
|
873
|
+
}
|
|
874
|
+
const response = value as Record<string, unknown>;
|
|
875
|
+
if (typeof response.decision !== 'string' || !GUARD_DECISIONS.has(response.decision)) {
|
|
876
|
+
return { reason: 'DashClaw returned a malformed or unknown guard decision; tool execution was interrupted.' };
|
|
877
|
+
}
|
|
878
|
+
if (response.decision === 'block') {
|
|
879
|
+
return { value: value as ClaimAwareGuardDecision };
|
|
880
|
+
}
|
|
881
|
+
const advertised = 'execution_claim_required' in response || 'claim_protocol' in response;
|
|
882
|
+
const validClaims = response.execution_claim_required === true && response.claim_protocol === 1;
|
|
883
|
+
if ((requireExecutionClaims || advertised) && !validClaims) {
|
|
884
|
+
return {
|
|
885
|
+
reason:
|
|
886
|
+
'DashClaw server upgrade required: execution-claim protocol 1 was not advertised, so this tool call cannot run safely.',
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
return { value: value as ClaimAwareGuardDecision };
|
|
890
|
+
}
|
|
684
891
|
|
|
685
892
|
function blockResultForDecision(
|
|
686
893
|
decision: GuardDecision,
|
|
@@ -700,23 +907,27 @@ function blockResultForDecision(
|
|
|
700
907
|
return undefined;
|
|
701
908
|
}
|
|
702
909
|
|
|
703
|
-
interface OpenActionContext {
|
|
910
|
+
interface OpenActionContext {
|
|
704
911
|
client: DashClaw;
|
|
705
912
|
classification: ActionClassification;
|
|
706
|
-
decision:
|
|
913
|
+
decision: ClaimAwareGuardDecision;
|
|
707
914
|
toolName: string;
|
|
708
915
|
key: string;
|
|
709
916
|
runId?: string;
|
|
710
917
|
config: PluginConfig;
|
|
711
918
|
}
|
|
712
919
|
|
|
713
|
-
async function openActionRecord(ctx: OpenActionContext): Promise<HookResult> {
|
|
920
|
+
async function openActionRecord(ctx: OpenActionContext): Promise<HookResult> {
|
|
714
921
|
const created = await createGovernanceAction(ctx);
|
|
715
922
|
if ('result' in created) return created.result;
|
|
716
923
|
|
|
717
|
-
const approval = await waitForRequiredApproval(ctx, created.value);
|
|
718
|
-
if (approval) return approval;
|
|
719
|
-
|
|
924
|
+
const approval = await waitForRequiredApproval(ctx, created.value);
|
|
925
|
+
if (approval) return approval;
|
|
926
|
+
if (ctx.decision.execution_claim_required === true && ctx.decision.claim_protocol === 1) {
|
|
927
|
+
const claim = await claimExecution(ctx, created.value.actionId);
|
|
928
|
+
if (claim) return claim;
|
|
929
|
+
}
|
|
930
|
+
rememberPendingAction(ctx.key, created.value.actionId, ctx.runId);
|
|
720
931
|
return;
|
|
721
932
|
}
|
|
722
933
|
|
|
@@ -729,17 +940,24 @@ async function createGovernanceAction(
|
|
|
729
940
|
const created = await ctx.client.createAction({
|
|
730
941
|
action_type: actionType,
|
|
731
942
|
declared_goal: declaredGoal,
|
|
732
|
-
risk_score: riskScore,
|
|
733
|
-
reversible,
|
|
734
|
-
systems_touched: systemsTouched,
|
|
943
|
+
risk_score: riskScore,
|
|
944
|
+
reversible,
|
|
945
|
+
systems_touched: systemsTouched,
|
|
946
|
+
client_capabilities: ['execution_claims'],
|
|
947
|
+
...(ctx.classification.act ? { act: ctx.classification.act } : {}),
|
|
735
948
|
metadata: { openclaw_tool_name: ctx.toolName },
|
|
736
949
|
});
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
950
|
+
const ids = [created.action_id, created.action?.action_id, created.action?.id]
|
|
951
|
+
.filter((id): id is string => typeof id === 'string' && id.length > 0);
|
|
952
|
+
if (ids.length === 0 || new Set(ids).size !== 1) {
|
|
953
|
+
return {
|
|
954
|
+
result: {
|
|
955
|
+
block: true,
|
|
956
|
+
blockReason: 'DashClaw action response did not contain one exact action ID; tool execution was interrupted.',
|
|
957
|
+
},
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
return { value: { actionId: ids[0], status: created.action?.status } };
|
|
743
961
|
} catch (err) {
|
|
744
962
|
const msg = errorMessage(err) || 'unknown';
|
|
745
963
|
console.warn(`[dashclaw-governance] createAction failed: ${msg}`);
|
|
@@ -752,17 +970,93 @@ async function createGovernanceAction(
|
|
|
752
970
|
};
|
|
753
971
|
}
|
|
754
972
|
return { result: undefined };
|
|
755
|
-
}
|
|
756
|
-
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
async function claimExecution(
|
|
977
|
+
ctx: OpenActionContext,
|
|
978
|
+
actionId: string,
|
|
979
|
+
): Promise<HookBlockResult | undefined> {
|
|
980
|
+
const attemptId = randomUUID();
|
|
981
|
+
const body = {
|
|
982
|
+
claim_execution: true,
|
|
983
|
+
attempt_id: attemptId,
|
|
984
|
+
agent_id: ctx.config.agentId,
|
|
985
|
+
...(ctx.classification.act ? { act: ctx.classification.act } : {}),
|
|
986
|
+
};
|
|
987
|
+
let response: Response;
|
|
988
|
+
let payload: unknown;
|
|
989
|
+
const controller = new AbortController();
|
|
990
|
+
const timeoutError = new Error(
|
|
991
|
+
`execution claim timed out after ${EXECUTION_CLAIM_TIMEOUT_MS}ms`
|
|
992
|
+
);
|
|
993
|
+
const timer = setTimeout(
|
|
994
|
+
() => controller.abort(timeoutError),
|
|
995
|
+
EXECUTION_CLAIM_TIMEOUT_MS
|
|
996
|
+
);
|
|
997
|
+
const aborted = new Promise<never>((_resolve, reject) => {
|
|
998
|
+
controller.signal.addEventListener(
|
|
999
|
+
'abort',
|
|
1000
|
+
() => reject(controller.signal.reason instanceof Error ? controller.signal.reason : timeoutError),
|
|
1001
|
+
{ once: true }
|
|
1002
|
+
);
|
|
1003
|
+
});
|
|
1004
|
+
try {
|
|
1005
|
+
response = await Promise.race([
|
|
1006
|
+
fetch(
|
|
1007
|
+
`${ctx.config.dashclawUrl}/api/actions/${encodeURIComponent(actionId)}`,
|
|
1008
|
+
{
|
|
1009
|
+
method: 'PATCH',
|
|
1010
|
+
headers: {
|
|
1011
|
+
'Content-Type': 'application/json',
|
|
1012
|
+
'x-api-key': ctx.config.dashclawApiKey,
|
|
1013
|
+
},
|
|
1014
|
+
body: JSON.stringify(body),
|
|
1015
|
+
signal: controller.signal,
|
|
1016
|
+
},
|
|
1017
|
+
),
|
|
1018
|
+
aborted,
|
|
1019
|
+
]);
|
|
1020
|
+
if (!response.ok) {
|
|
1021
|
+
return {
|
|
1022
|
+
block: true,
|
|
1023
|
+
blockReason: `DashClaw execution claim failed (HTTP ${response.status}); reconcile action ${actionId} before retrying.`,
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
payload = await Promise.race([response.json(), aborted]);
|
|
1027
|
+
} catch (err) {
|
|
1028
|
+
return {
|
|
1029
|
+
block: true,
|
|
1030
|
+
blockReason:
|
|
1031
|
+
`DashClaw execution claim failed (${errorMessage(err) || 'response lost'}); ` +
|
|
1032
|
+
`reconcile action ${actionId} before retrying.`,
|
|
1033
|
+
};
|
|
1034
|
+
} finally {
|
|
1035
|
+
clearTimeout(timer);
|
|
1036
|
+
}
|
|
1037
|
+
const claimed = payload as Record<string, unknown> | null;
|
|
1038
|
+
if (!claimed
|
|
1039
|
+
|| claimed.claimed !== true
|
|
1040
|
+
|| claimed.action_id !== actionId
|
|
1041
|
+
|| claimed.attempt_id !== attemptId) {
|
|
1042
|
+
return {
|
|
1043
|
+
block: true,
|
|
1044
|
+
blockReason:
|
|
1045
|
+
`DashClaw execution claim returned a malformed acknowledgement; ` +
|
|
1046
|
+
`reconcile action ${actionId} before retrying.`,
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
return undefined;
|
|
1050
|
+
}
|
|
757
1051
|
|
|
758
1052
|
async function waitForRequiredApproval(
|
|
759
1053
|
ctx: OpenActionContext,
|
|
760
1054
|
created: CreatedAction,
|
|
761
1055
|
): Promise<HookBlockResult | undefined> {
|
|
762
|
-
const needsApproval =
|
|
763
|
-
ctx.decision.decision === 'require_approval' ||
|
|
764
|
-
created.status === 'pending_approval';
|
|
765
|
-
if (!needsApproval
|
|
1056
|
+
const needsApproval =
|
|
1057
|
+
ctx.decision.decision === 'require_approval' ||
|
|
1058
|
+
created.status === 'pending_approval';
|
|
1059
|
+
if (!needsApproval) return undefined;
|
|
766
1060
|
|
|
767
1061
|
const timeout = ctx.config.approvalWaitMs;
|
|
768
1062
|
try {
|
|
@@ -1008,6 +1302,7 @@ const pluginEntry: ReturnType<typeof definePluginEntry> = definePluginEntry({
|
|
|
1008
1302
|
registerRunCleanup(api, config);
|
|
1009
1303
|
|
|
1010
1304
|
registerOutcomeRecorder(api, config);
|
|
1305
|
+
registerLivenessProbe(api, config);
|
|
1011
1306
|
},
|
|
1012
1307
|
});
|
|
1013
1308
|
|