@atbash/sdk 0.10.9-dev.0 → 0.10.10-dev.0

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/dist/index.d.mts CHANGED
@@ -430,6 +430,16 @@ declare class Atbash {
430
430
  * calls don't re-hit the dashboard. Cleared by `clearChainCache()`.
431
431
  */
432
432
  private readonly _chainCache;
433
+ /**
434
+ * Short-TTL cache for `/api/ai/exists`. The `registered` field is
435
+ * monotonic (once true, stays true), so most calls in a burst re-fetch
436
+ * data that hasn't changed. The `org_encryption_pubkey` field CAN change
437
+ * — an org toggling encryption mid-session — so the TTL is deliberately
438
+ * short (see `AGENT_EXISTS_TTL_MS`). Keyed by (pubkey, network) so
439
+ * cross-agent / cross-network calls don't collide.
440
+ */
441
+ private _agentExistsCache;
442
+ private static readonly AGENT_EXISTS_TTL_MS;
433
443
  /**
434
444
  * Cached bearer token for risk-engine / insurance read calls. Built
435
445
  * lazily as a signed `log_tool_call` tx and refreshed every 4 min so
@@ -547,6 +557,8 @@ declare class Atbash {
547
557
  private resolveChainFromMap;
548
558
  /** Drop any cached chain resolutions. Useful in tests. */
549
559
  clearChainCache(): void;
560
+ /** Drop the short-TTL `/api/ai/exists` cache. Useful in tests. */
561
+ clearAgentExistsCache(): void;
550
562
  /**
551
563
  * Wrap an SDK method body in telemetry — records the call at start
552
564
  * and a success/error duration at end. Re-throws on failure so the
@@ -893,19 +905,6 @@ interface ClassifyMemoryWriteOptions {
893
905
  */
894
906
  declare function classifyMemoryWrite(event: unknown, ctx: unknown, opts?: ClassifyMemoryWriteOptions): MemoryEntry | null;
895
907
 
896
- /**
897
- * Plugin-agnostic memory-write guard.
898
- *
899
- * A single call that replaces the plugin's usual memory-write branch:
900
- * classify → scan (Layer 1 regex + Layer 2 LLM) → gate on verdict →
901
- * persist to chain (fire-and-forget when allowed) → return decision.
902
- *
903
- * Plugins call this from their `before_tool_call` hook. When it returns
904
- * `{ handled: false }` the call wasn't a memory write and the plugin
905
- * should fall through to its regular tool-call audit. When
906
- * `{ handled: true }` the plugin returns `decision` directly.
907
- */
908
-
909
908
  /**
910
909
  * Minimal logger accepted by `guardMemoryWrite`. Plugins pass their
911
910
  * host runtime's logger (openclaw's `api.logger`, MCP's console, etc.).
@@ -936,6 +935,8 @@ interface GuardMemoryWriteInput extends ClassifyMemoryWriteOptions {
936
935
  debug?: boolean;
937
936
  /** Optional logger for debug probe + persist-failure warnings. */
938
937
  logger?: GuardLogger;
938
+ /** Absolute path of the agent's managed memory file. Required for chain commits — omitted skips them. */
939
+ memoryFilePath?: string;
939
940
  }
940
941
  /** Plugin-facing decision. Shape mirrors what plugins return from `before_tool_call`. */
941
942
  interface GuardMemoryDecision {
@@ -1000,12 +1001,19 @@ interface SyncMemoryOptions {
1000
1001
  * `drifted: false` — pointer is still valid; caller can keep serving the local copy.
1001
1002
  * `drifted: true` — active id changed on chain; `current` is the fresh decrypted row
1002
1003
  * (or `null` if active memory was removed entirely).
1004
+ *
1005
+ * `checked` — whether this call actually queried chain. `false` means the TTL
1006
+ * window was still open and the pointer was trusted without contacting chain, so
1007
+ * `drifted: false` carries no evidence about the current state. Callers that
1008
+ * vouch for content to a third party must not treat an unchecked result as proof.
1003
1009
  */
1004
1010
  type SyncMemoryResult = {
1005
1011
  drifted: false;
1012
+ checked: boolean;
1006
1013
  pointer: MemoryPointer;
1007
1014
  } | {
1008
1015
  drifted: true;
1016
+ checked: true;
1009
1017
  current: AgentMemoryEntry | null;
1010
1018
  pointer: MemoryPointer;
1011
1019
  };
@@ -1067,12 +1075,42 @@ interface ClassifyMemoryReadOptions {
1067
1075
  */
1068
1076
  declare function classifyMemoryRead(event: unknown, ctx: unknown, opts?: ClassifyMemoryReadOptions): boolean;
1069
1077
 
1070
- /** Decision the manager returns to the plugin's `before_tool_call` handler. */
1078
+ /**
1079
+ * Decision the manager returns to the plugin's `before_tool_call` handler.
1080
+ *
1081
+ * `allow: true` alone is NOT evidence that anything was checked. Read `audited`
1082
+ * to tell the two apart, and route un-audited calls to your own judge — see the
1083
+ * field docs below.
1084
+ */
1071
1085
  interface HookDecision {
1072
1086
  allow?: boolean;
1073
1087
  block?: boolean;
1074
1088
  blockReason?: string;
1075
1089
  reason?: string;
1090
+ /**
1091
+ * Whether the guard reached an enforcement decision about *this* call.
1092
+ *
1093
+ * Note this describes whether the guard **decided**, not whether it allowed.
1094
+ * Every `block` is `audited: true` — a blocked call is the most thoroughly
1095
+ * checked outcome the guard produces (a red scan, a ciphertext integrity
1096
+ * failure, a rolled-back version), and a host must never re-judge its way past
1097
+ * one.
1098
+ *
1099
+ * Absent or false means the guard reached no decision — it was inside its cache
1100
+ * window, chain was unreachable, the scan never ran, the file it can vouch for
1101
+ * is not the file being read, or it is in observe mode. Those calls are
1102
+ * unaudited: fall through to your own judge exactly as for a `null` return.
1103
+ *
1104
+ * So the host rule is:
1105
+ * `if (d.block) deny; else if (d.audited) allow; else judge it yourself;`
1106
+ *
1107
+ * Treating a bare `allow: true` as a completed audit is what this field exists
1108
+ * to prevent. A host that ignores it and returns the decision verbatim will
1109
+ * execute unaudited tool calls.
1110
+ */
1111
+ audited?: boolean;
1112
+ /** Scan verdict when one was produced (`green` | `yellow` | `red`). Absent when no scan ran. */
1113
+ verdict?: string;
1076
1114
  }
1077
1115
  interface MemoryGuardManagerOptions {
1078
1116
  auth: AgentAuth;
@@ -1095,6 +1133,13 @@ interface MemoryGuardManagerOptions {
1095
1133
  rollbackMinScore?: number;
1096
1134
  /** True → return `{block:true}` on defense triggers. False → log and return `null` (audit-only). Default true. */
1097
1135
  enforce?: boolean;
1136
+ /**
1137
+ * Chain targeting for the pointer sync (network, blockchainRid, nodeUrls).
1138
+ * Defaults to the SDK's configured chain. Without this the manager could only
1139
+ * ever talk to the default chain, which left the whole memory-read path
1140
+ * untestable — `syncLocalMemory` already accepted these options.
1141
+ */
1142
+ chainOpts?: ChainOpts;
1098
1143
  /** Host-specific tuning of what counts as a memory read. */
1099
1144
  memoryReadClassifier?: ClassifyMemoryReadOptions;
1100
1145
  /** Passed through to `guardMemoryWrite`. Host memory-write tool names override. */
@@ -1131,12 +1176,33 @@ declare class MemoryGuardManager {
1131
1176
  */
1132
1177
  runBootProbe(): Promise<void>;
1133
1178
  /**
1134
- * Returns a `HookDecision` when the event is a memory read or write
1135
- * (host returns it verbatim to its runtime). Returns `null` when the
1136
- * event isn't memory-related — host falls through to its own audit.
1179
+ * Returns a `HookDecision` when the guard reached a decision about this event.
1180
+ * Returns `null` when it did not — either the event isn't memory-related, or it
1181
+ * is but the guard could not check it. In both cases the host falls through to
1182
+ * its own audit.
1183
+ *
1184
+ * A returned decision carries `audited` (see `HookDecision`). Only
1185
+ * `{ allow: true, audited: true }` means "checked and cleared"; anything else
1186
+ * that allows is a call the host still needs to judge.
1137
1187
  */
1138
1188
  handleBeforeToolCall(event: unknown, ctx: unknown): Promise<HookDecision | null>;
1139
1189
  private mapGuardResult;
1190
+ /**
1191
+ * Whether the pointer state this manager tracks actually describes the file
1192
+ * this call is about to read.
1193
+ *
1194
+ * The classifier fires on nine patterns — including the bare tokens
1195
+ * `"memory/"`, `"CLAUDE.md"` and `"AGENTS.md"` — but the sync path only ever
1196
+ * reads, refreshes, or vouches for `this.memoryFilePath`. Without this check a
1197
+ * read of `/repo/CLAUDE.md` (or any path merely containing `memory/`) would
1198
+ * receive an `audited: true` for a file the guard never opened.
1199
+ *
1200
+ * Conservative on purpose: every path-shaped value found must resolve to the
1201
+ * managed file. If none is found, or any one differs, the answer is no. That
1202
+ * also covers events carrying two different path keys, where the classifier
1203
+ * and the host could otherwise disagree about which one is authoritative.
1204
+ */
1205
+ private vouchesForTarget;
1140
1206
  private handleMemoryRead;
1141
1207
  private writeMemoryAtomic;
1142
1208
  }
package/dist/index.d.ts CHANGED
@@ -430,6 +430,16 @@ declare class Atbash {
430
430
  * calls don't re-hit the dashboard. Cleared by `clearChainCache()`.
431
431
  */
432
432
  private readonly _chainCache;
433
+ /**
434
+ * Short-TTL cache for `/api/ai/exists`. The `registered` field is
435
+ * monotonic (once true, stays true), so most calls in a burst re-fetch
436
+ * data that hasn't changed. The `org_encryption_pubkey` field CAN change
437
+ * — an org toggling encryption mid-session — so the TTL is deliberately
438
+ * short (see `AGENT_EXISTS_TTL_MS`). Keyed by (pubkey, network) so
439
+ * cross-agent / cross-network calls don't collide.
440
+ */
441
+ private _agentExistsCache;
442
+ private static readonly AGENT_EXISTS_TTL_MS;
433
443
  /**
434
444
  * Cached bearer token for risk-engine / insurance read calls. Built
435
445
  * lazily as a signed `log_tool_call` tx and refreshed every 4 min so
@@ -547,6 +557,8 @@ declare class Atbash {
547
557
  private resolveChainFromMap;
548
558
  /** Drop any cached chain resolutions. Useful in tests. */
549
559
  clearChainCache(): void;
560
+ /** Drop the short-TTL `/api/ai/exists` cache. Useful in tests. */
561
+ clearAgentExistsCache(): void;
550
562
  /**
551
563
  * Wrap an SDK method body in telemetry — records the call at start
552
564
  * and a success/error duration at end. Re-throws on failure so the
@@ -893,19 +905,6 @@ interface ClassifyMemoryWriteOptions {
893
905
  */
894
906
  declare function classifyMemoryWrite(event: unknown, ctx: unknown, opts?: ClassifyMemoryWriteOptions): MemoryEntry | null;
895
907
 
896
- /**
897
- * Plugin-agnostic memory-write guard.
898
- *
899
- * A single call that replaces the plugin's usual memory-write branch:
900
- * classify → scan (Layer 1 regex + Layer 2 LLM) → gate on verdict →
901
- * persist to chain (fire-and-forget when allowed) → return decision.
902
- *
903
- * Plugins call this from their `before_tool_call` hook. When it returns
904
- * `{ handled: false }` the call wasn't a memory write and the plugin
905
- * should fall through to its regular tool-call audit. When
906
- * `{ handled: true }` the plugin returns `decision` directly.
907
- */
908
-
909
908
  /**
910
909
  * Minimal logger accepted by `guardMemoryWrite`. Plugins pass their
911
910
  * host runtime's logger (openclaw's `api.logger`, MCP's console, etc.).
@@ -936,6 +935,8 @@ interface GuardMemoryWriteInput extends ClassifyMemoryWriteOptions {
936
935
  debug?: boolean;
937
936
  /** Optional logger for debug probe + persist-failure warnings. */
938
937
  logger?: GuardLogger;
938
+ /** Absolute path of the agent's managed memory file. Required for chain commits — omitted skips them. */
939
+ memoryFilePath?: string;
939
940
  }
940
941
  /** Plugin-facing decision. Shape mirrors what plugins return from `before_tool_call`. */
941
942
  interface GuardMemoryDecision {
@@ -1000,12 +1001,19 @@ interface SyncMemoryOptions {
1000
1001
  * `drifted: false` — pointer is still valid; caller can keep serving the local copy.
1001
1002
  * `drifted: true` — active id changed on chain; `current` is the fresh decrypted row
1002
1003
  * (or `null` if active memory was removed entirely).
1004
+ *
1005
+ * `checked` — whether this call actually queried chain. `false` means the TTL
1006
+ * window was still open and the pointer was trusted without contacting chain, so
1007
+ * `drifted: false` carries no evidence about the current state. Callers that
1008
+ * vouch for content to a third party must not treat an unchecked result as proof.
1003
1009
  */
1004
1010
  type SyncMemoryResult = {
1005
1011
  drifted: false;
1012
+ checked: boolean;
1006
1013
  pointer: MemoryPointer;
1007
1014
  } | {
1008
1015
  drifted: true;
1016
+ checked: true;
1009
1017
  current: AgentMemoryEntry | null;
1010
1018
  pointer: MemoryPointer;
1011
1019
  };
@@ -1067,12 +1075,42 @@ interface ClassifyMemoryReadOptions {
1067
1075
  */
1068
1076
  declare function classifyMemoryRead(event: unknown, ctx: unknown, opts?: ClassifyMemoryReadOptions): boolean;
1069
1077
 
1070
- /** Decision the manager returns to the plugin's `before_tool_call` handler. */
1078
+ /**
1079
+ * Decision the manager returns to the plugin's `before_tool_call` handler.
1080
+ *
1081
+ * `allow: true` alone is NOT evidence that anything was checked. Read `audited`
1082
+ * to tell the two apart, and route un-audited calls to your own judge — see the
1083
+ * field docs below.
1084
+ */
1071
1085
  interface HookDecision {
1072
1086
  allow?: boolean;
1073
1087
  block?: boolean;
1074
1088
  blockReason?: string;
1075
1089
  reason?: string;
1090
+ /**
1091
+ * Whether the guard reached an enforcement decision about *this* call.
1092
+ *
1093
+ * Note this describes whether the guard **decided**, not whether it allowed.
1094
+ * Every `block` is `audited: true` — a blocked call is the most thoroughly
1095
+ * checked outcome the guard produces (a red scan, a ciphertext integrity
1096
+ * failure, a rolled-back version), and a host must never re-judge its way past
1097
+ * one.
1098
+ *
1099
+ * Absent or false means the guard reached no decision — it was inside its cache
1100
+ * window, chain was unreachable, the scan never ran, the file it can vouch for
1101
+ * is not the file being read, or it is in observe mode. Those calls are
1102
+ * unaudited: fall through to your own judge exactly as for a `null` return.
1103
+ *
1104
+ * So the host rule is:
1105
+ * `if (d.block) deny; else if (d.audited) allow; else judge it yourself;`
1106
+ *
1107
+ * Treating a bare `allow: true` as a completed audit is what this field exists
1108
+ * to prevent. A host that ignores it and returns the decision verbatim will
1109
+ * execute unaudited tool calls.
1110
+ */
1111
+ audited?: boolean;
1112
+ /** Scan verdict when one was produced (`green` | `yellow` | `red`). Absent when no scan ran. */
1113
+ verdict?: string;
1076
1114
  }
1077
1115
  interface MemoryGuardManagerOptions {
1078
1116
  auth: AgentAuth;
@@ -1095,6 +1133,13 @@ interface MemoryGuardManagerOptions {
1095
1133
  rollbackMinScore?: number;
1096
1134
  /** True → return `{block:true}` on defense triggers. False → log and return `null` (audit-only). Default true. */
1097
1135
  enforce?: boolean;
1136
+ /**
1137
+ * Chain targeting for the pointer sync (network, blockchainRid, nodeUrls).
1138
+ * Defaults to the SDK's configured chain. Without this the manager could only
1139
+ * ever talk to the default chain, which left the whole memory-read path
1140
+ * untestable — `syncLocalMemory` already accepted these options.
1141
+ */
1142
+ chainOpts?: ChainOpts;
1098
1143
  /** Host-specific tuning of what counts as a memory read. */
1099
1144
  memoryReadClassifier?: ClassifyMemoryReadOptions;
1100
1145
  /** Passed through to `guardMemoryWrite`. Host memory-write tool names override. */
@@ -1131,12 +1176,33 @@ declare class MemoryGuardManager {
1131
1176
  */
1132
1177
  runBootProbe(): Promise<void>;
1133
1178
  /**
1134
- * Returns a `HookDecision` when the event is a memory read or write
1135
- * (host returns it verbatim to its runtime). Returns `null` when the
1136
- * event isn't memory-related — host falls through to its own audit.
1179
+ * Returns a `HookDecision` when the guard reached a decision about this event.
1180
+ * Returns `null` when it did not — either the event isn't memory-related, or it
1181
+ * is but the guard could not check it. In both cases the host falls through to
1182
+ * its own audit.
1183
+ *
1184
+ * A returned decision carries `audited` (see `HookDecision`). Only
1185
+ * `{ allow: true, audited: true }` means "checked and cleared"; anything else
1186
+ * that allows is a call the host still needs to judge.
1137
1187
  */
1138
1188
  handleBeforeToolCall(event: unknown, ctx: unknown): Promise<HookDecision | null>;
1139
1189
  private mapGuardResult;
1190
+ /**
1191
+ * Whether the pointer state this manager tracks actually describes the file
1192
+ * this call is about to read.
1193
+ *
1194
+ * The classifier fires on nine patterns — including the bare tokens
1195
+ * `"memory/"`, `"CLAUDE.md"` and `"AGENTS.md"` — but the sync path only ever
1196
+ * reads, refreshes, or vouches for `this.memoryFilePath`. Without this check a
1197
+ * read of `/repo/CLAUDE.md` (or any path merely containing `memory/`) would
1198
+ * receive an `audited: true` for a file the guard never opened.
1199
+ *
1200
+ * Conservative on purpose: every path-shaped value found must resolve to the
1201
+ * managed file. If none is found, or any one differs, the answer is no. That
1202
+ * also covers events carrying two different path keys, where the classifier
1203
+ * and the host could otherwise disagree about which one is authoritative.
1204
+ */
1205
+ private vouchesForTarget;
1140
1206
  private handleMemoryRead;
1141
1207
  private writeMemoryAtomic;
1142
1208
  }