@senpi-ai/runtime 3.0.13 → 3.0.30

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.
Files changed (40) hide show
  1. package/dist/approvals/approval-store.d.ts +35 -0
  2. package/dist/approvals/approval-store.d.ts.map +1 -0
  3. package/dist/approvals/approval-store.js +87 -0
  4. package/dist/approvals/approval-store.js.map +1 -0
  5. package/dist/approvals/manifest.d.ts +47 -0
  6. package/dist/approvals/manifest.d.ts.map +1 -0
  7. package/dist/approvals/manifest.js +90 -0
  8. package/dist/approvals/manifest.js.map +1 -0
  9. package/dist/approvals/telemetry.d.ts +41 -0
  10. package/dist/approvals/telemetry.d.ts.map +1 -0
  11. package/dist/approvals/telemetry.js +35 -0
  12. package/dist/approvals/telemetry.js.map +1 -0
  13. package/dist/approvals/tool-approval-gate.d.ts +96 -0
  14. package/dist/approvals/tool-approval-gate.d.ts.map +1 -0
  15. package/dist/approvals/tool-approval-gate.js +233 -0
  16. package/dist/approvals/tool-approval-gate.js.map +1 -0
  17. package/dist/approvals/tool-approvals-manifest.json +27 -0
  18. package/dist/index.d.ts +15 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +54 -19
  21. package/dist/index.js.map +1 -1
  22. package/dist/runtime/auto-update/coordinator.d.ts +37 -12
  23. package/dist/runtime/auto-update/coordinator.d.ts.map +1 -1
  24. package/dist/runtime/auto-update/coordinator.js +136 -17
  25. package/dist/runtime/auto-update/coordinator.js.map +1 -1
  26. package/dist/runtime/auto-update/gateway-restart.d.ts +4 -4
  27. package/dist/runtime/auto-update/gateway-restart.js +4 -4
  28. package/dist/runtime/auto-update/types.d.ts +1 -0
  29. package/dist/runtime/auto-update/types.d.ts.map +1 -1
  30. package/dist/runtime/auto-update/types.js.map +1 -1
  31. package/dist/telemetry/metrics.d.ts +11 -0
  32. package/dist/telemetry/metrics.d.ts.map +1 -1
  33. package/dist/telemetry/metrics.js +22 -0
  34. package/dist/telemetry/metrics.js.map +1 -1
  35. package/dist/utils/event-catalog.d.ts +103 -2
  36. package/dist/utils/event-catalog.d.ts.map +1 -1
  37. package/dist/utils/event-catalog.js +28 -0
  38. package/dist/utils/event-catalog.js.map +1 -1
  39. package/openclaw.plugin.json +10 -1
  40. package/package.json +3 -2
@@ -0,0 +1,35 @@
1
+ /** Minimal logger contract so the store does not depend on a concrete logger. */
2
+ interface LoggerLike {
3
+ warn(...args: unknown[]): void;
4
+ }
5
+ /** File name inside the plugin state dir (volume-backed in prod). */
6
+ export declare const TOOL_APPROVALS_STATE_FILE = "tool-approvals.json";
7
+ /**
8
+ * Persistent per-tool "always allow" decisions for the MCP tool approval gate.
9
+ *
10
+ * OpenClaw does not persist `allow-always` for plugin approvals (unlike the exec
11
+ * tool's allowlist file) — the decision arrives once via the `onResolution`
12
+ * callback and is otherwise one-shot. This store is that missing persistence:
13
+ * a JSON file in the plugin state dir, so decisions survive gateway restarts,
14
+ * redeploys, and plugin auto-updates.
15
+ *
16
+ * To make a tool prompt again, delete its entry from `tool-approvals.json`
17
+ * (or the whole file) in the state dir.
18
+ *
19
+ * Load is synchronous (once, at plugin register()); writes are synchronous
20
+ * atomic temp → rename, and only happen on an explicit user decision.
21
+ */
22
+ export declare class ToolApprovalStore {
23
+ private readonly logger?;
24
+ private readonly filePath;
25
+ private readonly allowAlways;
26
+ constructor(stateDir: string, logger?: LoggerLike | undefined);
27
+ private load;
28
+ isAllowedAlways(tool: string): boolean;
29
+ get size(): number;
30
+ /** Record a user's allow-always decision and persist it immediately. */
31
+ rememberAllowAlways(tool: string): void;
32
+ private persist;
33
+ }
34
+ export {};
35
+ //# sourceMappingURL=approval-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-store.d.ts","sourceRoot":"","sources":["../../src/approvals/approval-store.ts"],"names":[],"mappings":"AAGA,iFAAiF;AACjF,UAAU,UAAU;IAClB,IAAI,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;CAChC;AAED,qEAAqE;AACrE,eAAO,MAAM,yBAAyB,wBAAwB,CAAC;AAU/D;;;;;;;;;;;;;;GAcG;AACH,qBAAa,iBAAiB;IAM1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IAL1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAuC;gBAGjE,QAAQ,EAAE,MAAM,EACC,MAAM,CAAC,EAAE,UAAU,YAAA;IAMtC,OAAO,CAAC,IAAI;IAiCZ,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAItC,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,wEAAwE;IACxE,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAQvC,OAAO,CAAC,OAAO;CAehB"}
@@ -0,0 +1,87 @@
1
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ /** File name inside the plugin state dir (volume-backed in prod). */
4
+ export const TOOL_APPROVALS_STATE_FILE = "tool-approvals.json";
5
+ /**
6
+ * Persistent per-tool "always allow" decisions for the MCP tool approval gate.
7
+ *
8
+ * OpenClaw does not persist `allow-always` for plugin approvals (unlike the exec
9
+ * tool's allowlist file) — the decision arrives once via the `onResolution`
10
+ * callback and is otherwise one-shot. This store is that missing persistence:
11
+ * a JSON file in the plugin state dir, so decisions survive gateway restarts,
12
+ * redeploys, and plugin auto-updates.
13
+ *
14
+ * To make a tool prompt again, delete its entry from `tool-approvals.json`
15
+ * (or the whole file) in the state dir.
16
+ *
17
+ * Load is synchronous (once, at plugin register()); writes are synchronous
18
+ * atomic temp → rename, and only happen on an explicit user decision.
19
+ */
20
+ export class ToolApprovalStore {
21
+ logger;
22
+ filePath;
23
+ allowAlways = new Map();
24
+ constructor(stateDir, logger) {
25
+ this.logger = logger;
26
+ this.filePath = join(stateDir, TOOL_APPROVALS_STATE_FILE);
27
+ this.load();
28
+ }
29
+ load() {
30
+ let raw;
31
+ try {
32
+ raw = readFileSync(this.filePath, "utf8");
33
+ }
34
+ catch (err) {
35
+ const isNotFound = err instanceof Error && "code" in err && err.code === "ENOENT";
36
+ if (!isNotFound) {
37
+ this.logger?.warn(`[senpi-tool-approvals] failed to read state file, starting empty | path=${this.filePath} err=${err instanceof Error ? err.message : String(err)}`);
38
+ }
39
+ return;
40
+ }
41
+ try {
42
+ const parsed = JSON.parse(raw);
43
+ const entries = parsed?.allowAlways;
44
+ if (entries && typeof entries === "object" && !Array.isArray(entries)) {
45
+ for (const [tool, entry] of Object.entries(entries)) {
46
+ if (typeof tool === "string" && tool.length > 0) {
47
+ this.allowAlways.set(tool, {
48
+ decidedAt: typeof entry?.decidedAt === "string" ? entry.decidedAt : "",
49
+ });
50
+ }
51
+ }
52
+ }
53
+ }
54
+ catch (err) {
55
+ this.logger?.warn(`[senpi-tool-approvals] corrupt state file, starting empty (all gated tools will prompt again) | path=${this.filePath} err=${err instanceof Error ? err.message : String(err)}`);
56
+ }
57
+ }
58
+ isAllowedAlways(tool) {
59
+ return this.allowAlways.has(tool);
60
+ }
61
+ get size() {
62
+ return this.allowAlways.size;
63
+ }
64
+ /** Record a user's allow-always decision and persist it immediately. */
65
+ rememberAllowAlways(tool) {
66
+ if (!tool || this.allowAlways.has(tool)) {
67
+ return;
68
+ }
69
+ this.allowAlways.set(tool, { decidedAt: new Date().toISOString() });
70
+ this.persist();
71
+ }
72
+ persist() {
73
+ try {
74
+ mkdirSync(dirname(this.filePath), { recursive: true });
75
+ const shape = {
76
+ allowAlways: Object.fromEntries(this.allowAlways),
77
+ };
78
+ const tmpPath = `${this.filePath}.tmp`;
79
+ writeFileSync(tmpPath, JSON.stringify(shape, null, 2), "utf8");
80
+ renameSync(tmpPath, this.filePath);
81
+ }
82
+ catch (err) {
83
+ this.logger?.warn(`[senpi-tool-approvals] failed to persist state (decision kept in memory only) | path=${this.filePath} err=${err instanceof Error ? err.message : String(err)}`);
84
+ }
85
+ }
86
+ }
87
+ //# sourceMappingURL=approval-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-store.js","sourceRoot":"","sources":["../../src/approvals/approval-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAO1C,qEAAqE;AACrE,MAAM,CAAC,MAAM,yBAAyB,GAAG,qBAAqB,CAAC;AAU/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,iBAAiB;IAMT;IALF,QAAQ,CAAS;IACjB,WAAW,GAAG,IAAI,GAAG,EAA4B,CAAC;IAEnE,YACE,QAAgB,EACC,MAAmB;QAAnB,WAAM,GAAN,MAAM,CAAa;QAEpC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,yBAAyB,CAAC,CAAC;QAC1D,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;IAEO,IAAI;QACV,IAAI,GAAW,CAAC;QAChB,IAAI,CAAC;YACH,GAAG,GAAG,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC5C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,UAAU,GACd,GAAG,YAAY,KAAK,IAAI,MAAM,IAAI,GAAG,IAAK,GAA6B,CAAC,IAAI,KAAK,QAAQ,CAAC;YAC5F,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,IAAI,CAAC,MAAM,EAAE,IAAI,CACf,2EAA2E,IAAI,CAAC,QAAQ,QAAQ,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CACnJ,CAAC;YACJ,CAAC;YACD,OAAO;QACT,CAAC;QACD,IAAI,CAAC;YACH,MAAM,MAAM,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CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;QACpE,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAEO,OAAO;QACb,IAAI,CAAC;YACH,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YACvD,MAAM,KAAK,GAA2B;gBACpC,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC;aAClD,CAAC;YACF,MAAM,OAAO,GAAG,GAAG,IAAI,CAAC,QAAQ,MAAM,CAAC;YACvC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;YAC/D,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,EAAE,IAAI,CACf,wFAAwF,IAAI,CAAC,QAAQ,QAAQ,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAChK,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Tool-approvals manifest — the single, codebase-controlled source of truth
3
+ * for which senpi MCP tools the approval gate treats as trades (gated in the
4
+ * default mode) and for the gate's defaults (mode, prompt timeout).
5
+ *
6
+ * The manifest is a checked-in JSON file (`tool-approvals-manifest.json`,
7
+ * copied into `dist/` at build time) shipped *with the runtime*, so changing
8
+ * the gated tool set or the fleet default is one reviewable diff — no env
9
+ * vars, no scattered constants. Mirrors the skills-manager manifest pattern
10
+ * (`src/skills-manager/manifest.ts`).
11
+ *
12
+ * Runtime overrides still win over the manifest default for the mode:
13
+ * env `SENPI_TOOL_APPROVALS` > plugin config `toolApprovals` > manifest
14
+ * `defaults.mode` (see `resolveToolApprovalsMode` in tool-approval-gate.ts).
15
+ */
16
+ export type ToolApprovalsMode = "off" | "trades" | "all";
17
+ /** Parsed + validated manifest shape. */
18
+ export interface ToolApprovalsManifest {
19
+ schemaVersion: number;
20
+ defaults: {
21
+ mode: ToolApprovalsMode;
22
+ /** Prompt timeout; gateway clamps plugin approvals to max 600_000 ms. */
23
+ timeoutMs: number;
24
+ };
25
+ /** Bare senpi MCP tool names (no `senpi__` prefix) gated in `trades` mode. */
26
+ tradeTools: string[];
27
+ }
28
+ /**
29
+ * Hardcoded fallback used only when the checked-in JSON cannot be read or
30
+ * parsed (corrupt file, missing from dist). Keeps the plugin from crashing at
31
+ * import time; logged loudly by {@link loadToolApprovalsManifest}. Kept in
32
+ * sync with `tool-approvals-manifest.json` (a test asserts this).
33
+ */
34
+ export declare const FALLBACK_TOOL_APPROVALS_MANIFEST: ToolApprovalsManifest;
35
+ /**
36
+ * Load + validate the manifest from disk, cached after first read. Resolves
37
+ * the JSON path relative to this module so it works both from `src/` (vitest)
38
+ * and `dist/` (the build copies the JSON next to the compiled module).
39
+ *
40
+ * On any read/parse/validation failure, logs a warning and returns
41
+ * {@link FALLBACK_TOOL_APPROVALS_MANIFEST} — the gate must not fail to start
42
+ * because of a packaging mistake.
43
+ */
44
+ export declare function loadToolApprovalsManifest(): ToolApprovalsManifest;
45
+ /** Reset the manifest cache. Test-only. */
46
+ export declare function resetToolApprovalsManifestCacheForTests(): void;
47
+ //# sourceMappingURL=manifest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../src/approvals/manifest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAOH,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,QAAQ,GAAG,KAAK,CAAC;AAEzD,yCAAyC;AACzC,MAAM,WAAW,qBAAqB;IACpC,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE;QACR,IAAI,EAAE,iBAAiB,CAAC;QACxB,yEAAyE;QACzE,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,8EAA8E;IAC9E,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AAWD;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,EAAE,qBA0B9C,CAAC;AAIF;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,IAAI,qBAAqB,CAcjE;AAED,2CAA2C;AAC3C,wBAAgB,uCAAuC,IAAI,IAAI,CAE9D"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Tool-approvals manifest — the single, codebase-controlled source of truth
3
+ * for which senpi MCP tools the approval gate treats as trades (gated in the
4
+ * default mode) and for the gate's defaults (mode, prompt timeout).
5
+ *
6
+ * The manifest is a checked-in JSON file (`tool-approvals-manifest.json`,
7
+ * copied into `dist/` at build time) shipped *with the runtime*, so changing
8
+ * the gated tool set or the fleet default is one reviewable diff — no env
9
+ * vars, no scattered constants. Mirrors the skills-manager manifest pattern
10
+ * (`src/skills-manager/manifest.ts`).
11
+ *
12
+ * Runtime overrides still win over the manifest default for the mode:
13
+ * env `SENPI_TOOL_APPROVALS` > plugin config `toolApprovals` > manifest
14
+ * `defaults.mode` (see `resolveToolApprovalsMode` in tool-approval-gate.ts).
15
+ */
16
+ import { readFileSync } from "node:fs";
17
+ import { fileURLToPath } from "node:url";
18
+ import { z } from "zod";
19
+ import { createLogger } from "../utils/logger.js";
20
+ const manifestSchema = z.object({
21
+ schemaVersion: z.number(),
22
+ defaults: z.object({
23
+ mode: z.union([z.literal("off"), z.literal("trades"), z.literal("all")]),
24
+ timeoutMs: z.number().int().positive().max(600_000),
25
+ }),
26
+ tradeTools: z.array(z.string().min(1)).min(1),
27
+ });
28
+ /**
29
+ * Hardcoded fallback used only when the checked-in JSON cannot be read or
30
+ * parsed (corrupt file, missing from dist). Keeps the plugin from crashing at
31
+ * import time; logged loudly by {@link loadToolApprovalsManifest}. Kept in
32
+ * sync with `tool-approvals-manifest.json` (a test asserts this).
33
+ */
34
+ export const FALLBACK_TOOL_APPROVALS_MANIFEST = {
35
+ schemaVersion: 1,
36
+ defaults: { mode: "trades", timeoutMs: 600_000 },
37
+ tradeTools: [
38
+ "create_position",
39
+ "close_position",
40
+ "edit_position",
41
+ "cancel_order",
42
+ "strategy_create",
43
+ "strategy_create_custom_strategy",
44
+ "strategy_close",
45
+ "strategy_close_positions",
46
+ "strategy_update",
47
+ "strategy_pause",
48
+ "strategy_top_up",
49
+ "strategy_withdraw_funds",
50
+ "strategy_bridge_funds_from_hyperliquid_to_evm",
51
+ "transfer_spot_to_perps",
52
+ // Not currently exposed by the senpi MCP server, but senpi-web's permission
53
+ // registry lists it — gating a tool that does not exist is a no-op, and it
54
+ // must not slip through unapproved if it ever ships.
55
+ "send_usdc",
56
+ "ratchet_stop_add",
57
+ "ratchet_stop_edit",
58
+ "ratchet_stop_delete",
59
+ ],
60
+ };
61
+ let cached = null;
62
+ /**
63
+ * Load + validate the manifest from disk, cached after first read. Resolves
64
+ * the JSON path relative to this module so it works both from `src/` (vitest)
65
+ * and `dist/` (the build copies the JSON next to the compiled module).
66
+ *
67
+ * On any read/parse/validation failure, logs a warning and returns
68
+ * {@link FALLBACK_TOOL_APPROVALS_MANIFEST} — the gate must not fail to start
69
+ * because of a packaging mistake.
70
+ */
71
+ export function loadToolApprovalsManifest() {
72
+ if (cached)
73
+ return cached;
74
+ try {
75
+ const jsonPath = fileURLToPath(new URL("./tool-approvals-manifest.json", import.meta.url));
76
+ const parsed = JSON.parse(readFileSync(jsonPath, "utf8"));
77
+ cached = manifestSchema.parse(parsed);
78
+ }
79
+ catch (err) {
80
+ const message = err instanceof Error ? err.message : String(err);
81
+ createLogger("[senpi-tool-approvals][manifest]").warn(`failed to load tool-approvals-manifest.json, using built-in fallback: ${message}`);
82
+ cached = FALLBACK_TOOL_APPROVALS_MANIFEST;
83
+ }
84
+ return cached;
85
+ }
86
+ /** Reset the manifest cache. Test-only. */
87
+ export function resetToolApprovalsManifestCacheForTests() {
88
+ cached = null;
89
+ }
90
+ //# sourceMappingURL=manifest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/approvals/manifest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAgBlD,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IAC9B,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;IACzB,QAAQ,EAAE,CAAC,CAAC,MAAM,CAAC;QACjB,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QACxE,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC;KACpD,CAAC;IACF,UAAU,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;CAC9C,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAA0B;IACrE,aAAa,EAAE,CAAC;IAChB,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE;IAChD,UAAU,EAAE;QACV,iBAAiB;QACjB,gBAAgB;QAChB,eAAe;QACf,cAAc;QACd,iBAAiB;QACjB,iCAAiC;QACjC,gBAAgB;QAChB,0BAA0B;QAC1B,iBAAiB;QACjB,gBAAgB;QAChB,iBAAiB;QACjB,yBAAyB;QACzB,+CAA+C;QAC/C,wBAAwB;QACxB,4EAA4E;QAC5E,2EAA2E;QAC3E,qDAAqD;QACrD,WAAW;QACX,kBAAkB;QAClB,mBAAmB;QACnB,qBAAqB;KACtB;CACF,CAAC;AAEF,IAAI,MAAM,GAAiC,IAAI,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB;IACvC,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAC1B,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,gCAAgC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAC3F,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;QACnE,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACxC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjE,YAAY,CAAC,kCAAkC,CAAC,CAAC,IAAI,CACnD,yEAAyE,OAAO,EAAE,CACnF,CAAC;QACF,MAAM,GAAG,gCAAgC,CAAC;IAC5C,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,2CAA2C;AAC3C,MAAM,UAAU,uCAAuC;IACrD,MAAM,GAAG,IAAI,CAAC;AAChB,CAAC"}
@@ -0,0 +1,41 @@
1
+ /** Terminal user choice at the approval gate. */
2
+ export type ApprovalDecision = "allow-once" | "allow-always" | "deny" | "timeout";
3
+ /**
4
+ * Telemetry seam for the MCP tool approval gate.
5
+ *
6
+ * Injected into {@link registerToolApprovalGate} so the gate stays unit-testable
7
+ * (tests pass a fake and assert on it) while production wires the real event/
8
+ * metric planes. Mirrors how `span-hooks` reaches `emitToolCalled` /
9
+ * `recordToolCall` directly, and how `index.ts` emits `runtime.scanners_unwired`
10
+ * through a dedicated OTel-backed `createLogger` (the plugin's own
11
+ * `PluginLogger` has no `event()`).
12
+ */
13
+ export interface GateTelemetry {
14
+ /**
15
+ * Audit EVERY gate decision — the only place the approve path is recorded, so
16
+ * we know whether the user chose `allow-once` vs `allow-always` (and, for the
17
+ * deny path, `deny` vs `timeout`). Emits the `tool_approval.resolved` event
18
+ * (info for allows, warn for deny/timeout) + `tool_approval.decisions` metric,
19
+ * so allow-always adoption and deny/timeout rates per tool are chartable.
20
+ */
21
+ resolved(toolName: string, decision: ApprovalDecision, isTrade: boolean): void;
22
+ /**
23
+ * A user deny or a 10-minute timeout — a state-changing tool the user did NOT
24
+ * authorize, so it did not run. Recorded on the origin-uniform `tool.called`
25
+ * event + `tool.calls` metric with the reserved `denied` outcome, so
26
+ * deny-rate and timeout-deny-rate land on the existing tool dashboards. A
27
+ * timeout-deny is a product signal (the user missed the prompt and their
28
+ * trade silently didn't execute). Complements {@link resolved}, which lives
29
+ * on the separate gate-decision plane.
30
+ */
31
+ denied(toolName: string, reason: "user_denied" | "approval_timeout"): void;
32
+ /**
33
+ * The gate's fail-open catch fired: the gate hit an internal bug and let the
34
+ * tool through ungated. Catalogued as `tool_approval.gate_error` at error
35
+ * level so it can sit behind an alert instead of a log grep.
36
+ */
37
+ gateError(err: unknown, toolName: string | undefined): void;
38
+ }
39
+ /** Production wiring: real event + metric planes. */
40
+ export declare const defaultGateTelemetry: GateTelemetry;
41
+ //# sourceMappingURL=telemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/approvals/telemetry.ts"],"names":[],"mappings":"AAKA,iDAAiD;AACjD,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG,cAAc,GAAG,MAAM,GAAG,SAAS,CAAC;AAElF;;;;;;;;;GASG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC/E;;;;;;;;OAQG;IACH,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,kBAAkB,GAAG,IAAI,CAAC;IAC3E;;;;OAIG;IACH,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;CAC7D;AAID,qDAAqD;AACrD,eAAO,MAAM,oBAAoB,EAAE,aA2BlC,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { recordApprovalDecision, recordToolCall } from "../telemetry/metrics.js";
2
+ import { emitToolCalled } from "../telemetry/tool-plane.js";
3
+ import { EVENT_NAME, eventAttrs } from "../utils/event-catalog.js";
4
+ import { createLogger } from "../utils/logger.js";
5
+ const eventLogger = createLogger("[senpi-tool-approvals]");
6
+ /** Production wiring: real event + metric planes. */
7
+ export const defaultGateTelemetry = {
8
+ resolved(toolName, decision, isTrade) {
9
+ const denyish = decision === "deny" || decision === "timeout";
10
+ eventLogger.event(EVENT_NAME.toolApprovalResolved, denyish ? "warn" : "info", {
11
+ body: `tool approval ${decision} for ${toolName}`,
12
+ attributes: eventAttrs({
13
+ "gen_ai.tool.name": toolName,
14
+ "senpi.tool_approval.decision": decision,
15
+ "senpi.tool.is_trade": String(isTrade),
16
+ }),
17
+ });
18
+ recordApprovalDecision({ toolName, decision, isTrade });
19
+ },
20
+ denied(toolName, reason) {
21
+ emitToolCalled({ name: toolName, origin: "agent", outcome: "denied", errorType: reason });
22
+ recordToolCall({ toolName, origin: "agent", outcome: "denied", errorType: reason });
23
+ },
24
+ gateError(err, toolName) {
25
+ eventLogger.event(EVENT_NAME.toolApprovalGateError, "error", {
26
+ body: `tool approval gate failed open${toolName ? ` for ${toolName}` : ""}`,
27
+ attributes: eventAttrs({
28
+ "gen_ai.tool.name": toolName,
29
+ "error.type": err instanceof Error ? err.name : "unknown",
30
+ }),
31
+ redact: { error_message: err instanceof Error ? err.message : String(err) },
32
+ });
33
+ },
34
+ };
35
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../../src/approvals/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACjF,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAiB,MAAM,2BAA2B,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA0ClD,MAAM,WAAW,GAAG,YAAY,CAAC,wBAAwB,CAAC,CAAC;AAE3D,qDAAqD;AACrD,MAAM,CAAC,MAAM,oBAAoB,GAAkB;IACjD,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO;QAClC,MAAM,OAAO,GAAG,QAAQ,KAAK,MAAM,IAAI,QAAQ,KAAK,SAAS,CAAC;QAC9D,WAAW,CAAC,KAAK,CAAC,UAAU,CAAC,oBAAoB,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE;YAC5E,IAAI,EAAE,iBAAiB,QAAQ,QAAQ,QAAQ,EAAE;YACjD,UAAU,EAAE,UAAU,CAAmD;gBACvE,kBAAkB,EAAE,QAAQ;gBAC5B,8BAA8B,EAAE,QAAQ;gBACxC,qBAAqB,EAAE,MAAM,CAAC,OAAO,CAAC;aACvC,CAAC;SACH,CAAC,CAAC;QACH,sBAAsB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;IAC1D,CAAC;IACD,MAAM,CAAC,QAAQ,EAAE,MAAM;QACrB,cAAc,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;QAC1F,cAAc,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,SAAS,CAAC,GAAG,EAAE,QAAQ;QACrB,WAAW,CAAC,KAAK,CAAC,UAAU,CAAC,qBAAqB,EAAE,OAAO,EAAE;YAC3D,IAAI,EAAE,iCAAiC,QAAQ,CAAC,CAAC,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE;YAC3E,UAAU,EAAE,UAAU,CAAoD;gBACxE,kBAAkB,EAAE,QAAQ;gBAC5B,YAAY,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS;aAC1D,CAAC;YACF,MAAM,EAAE,EAAE,aAAa,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;SAC5E,CAAC,CAAC;IACL,CAAC;CACF,CAAC"}
@@ -0,0 +1,96 @@
1
+ import { ToolApprovalStore } from "./approval-store.js";
2
+ import { type ToolApprovalsManifest, type ToolApprovalsMode } from "./manifest.js";
3
+ import { type GateTelemetry } from "./telemetry.js";
4
+ /**
5
+ * Tool approval gate: requires an interactive user approval before
6
+ * state-changing senpi MCP tools execute.
7
+ *
8
+ * OpenClaw v2026.5.x has no declarative "ask" policy for MCP tools (only the
9
+ * exec tool has `tools.exec.ask`); the supported mechanism is a plugin
10
+ * `before_tool_call` hook returning `requireApproval`, which the gateway
11
+ * drives via `plugin.approval.request` / `plugin.approval.resolve`
12
+ * (decisions: allow-once / allow-always / deny; timeout → deny). Approval
13
+ * prompts are broadcast to gateway clients holding the `operator.approvals`
14
+ * scope — the senpi agent-bridge connection — and surface in senpi-web.
15
+ *
16
+ * The gated tool set and the defaults (mode, prompt timeout) come from the
17
+ * checked-in manifest (`tool-approvals-manifest.json`, see manifest.ts) — the
18
+ * reviewed, fleet-wide control surface. Mode precedence: env
19
+ * {@link TOOL_APPROVALS_ENV_VAR} > plugin config `toolApprovals` > manifest
20
+ * `defaults.mode`.
21
+ *
22
+ * `allow-always` decisions are persisted per tool via {@link ToolApprovalStore}
23
+ * (OpenClaw treats them as one-shot for plugin approvals).
24
+ *
25
+ * Design doc: docs/mcp-tool-approvals-design.md
26
+ */
27
+ interface LoggerLike {
28
+ info(...args: unknown[]): void;
29
+ warn(...args: unknown[]): void;
30
+ }
31
+ /** Minimal shape of the OpenClaw plugin API surface the gate needs. */
32
+ export interface ToolApprovalGatePluginApi {
33
+ on(hookName: string, handler: (...args: unknown[]) => unknown, opts?: {
34
+ priority?: number;
35
+ }): void;
36
+ }
37
+ export type { ToolApprovalsMode };
38
+ /** Env var that overrides the `toolApprovals` plugin config and the manifest default. */
39
+ export declare const TOOL_APPROVALS_ENV_VAR = "SENPI_TOOL_APPROVALS";
40
+ /**
41
+ * Match a tool-registry name against the senpi MCP server and return the bare
42
+ * tool name. Covers the embedded-MCP shape (`senpi__create_position`), the
43
+ * CLI-runner backend shape (`mcp__senpi__create_position`), and openclaw's
44
+ * server-name dedupe suffix (`senpi-2__…`).
45
+ */
46
+ export declare function matchSenpiTool(toolName: unknown): string | null;
47
+ export declare function isToolApprovalsMode(value: unknown): value is ToolApprovalsMode;
48
+ /**
49
+ * Resolve the gate mode: env {@link TOOL_APPROVALS_ENV_VAR} > plugin config
50
+ * `toolApprovals` > manifest `defaults.mode`. Unrecognized values warn and
51
+ * fall through to the manifest default rather than silently disabling the gate.
52
+ */
53
+ export declare function resolveToolApprovalsMode(params: {
54
+ pluginConfig?: Record<string, unknown>;
55
+ env?: NodeJS.ProcessEnv;
56
+ logger?: LoggerLike;
57
+ manifest?: ToolApprovalsManifest;
58
+ }): ToolApprovalsMode;
59
+ /**
60
+ * Human sentence for a senpi trade tool call, mirroring senpi-web's own
61
+ * per-tool phrasing (its `getDescription` registry).
62
+ *
63
+ * Why the plugin builds this rather than leaving it to the UI: senpi-web keys
64
+ * that registry by the **bare** tool name and feeds it the call `args`, but for
65
+ * plugin approvals openclaw sends the prefixed registry name
66
+ * (`senpi__create_position`) and has **no `args` field at all` in
67
+ * `PluginApprovalRequestParamsSchema` (additionalProperties: false). So the UI
68
+ * takes its fallback branch and renders `description` verbatim, with no args
69
+ * block — meaning this string is the *only* place the user sees what they are
70
+ * approving. It must therefore carry the numbers, not just the intent.
71
+ */
72
+ export declare function describeSenpiToolCall(tool: string, params: unknown): string | undefined;
73
+ /**
74
+ * Prompt body: the human sentence when we know the tool, plus the compact raw
75
+ * args so the exact numbers are always visible (senpi-web renders no args block
76
+ * for plugin approvals — see {@link describeSenpiToolCall}). Capped to the
77
+ * gateway's description limit, which rejects (not truncates) anything longer.
78
+ */
79
+ export declare function buildApprovalDescription(tool: string, params: unknown): string;
80
+ export interface RegisterToolApprovalGateOptions {
81
+ api: ToolApprovalGatePluginApi;
82
+ mode: ToolApprovalsMode;
83
+ /** Base plugin state dir; the store file lives directly under it. */
84
+ stateDir: string;
85
+ logger?: LoggerLike;
86
+ /** Injectable for tests; defaults to the checked-in manifest. */
87
+ manifest?: ToolApprovalsManifest;
88
+ /** Injectable for tests; defaults to the real event/metric planes. */
89
+ telemetry?: GateTelemetry;
90
+ }
91
+ /**
92
+ * Register the `before_tool_call` approval gate. No-op in `off` mode.
93
+ * Returns the store (for tests/introspection) when registered.
94
+ */
95
+ export declare function registerToolApprovalGate(options: RegisterToolApprovalGateOptions): ToolApprovalStore | undefined;
96
+ //# sourceMappingURL=tool-approval-gate.d.ts.map
@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