@verax-ai/body 0.6.1 → 0.7.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/wiring.js CHANGED
@@ -1,9 +1,13 @@
1
1
  import { createProxy, FileLedger, loadPolicy, tenantKey, } from "@verax-ai/proxy";
2
2
  import { readFileSync } from "node:fs";
3
- import { POLICY_FILE_ERROR_PREFIX, policyFileRefusal } from "./config.js";
3
+ import { pokeAlarmDelivery, startAlarmDelivery } from "./alarm-webhook.js";
4
+ import { ALARM_WEBHOOK_URL_ERROR, alarmRetryMs, isAllowedAlarmWebhookUrl, POLICY_FILE_ERROR_PREFIX, policyFileRefusal, } from "./config.js";
5
+ import { completeMemoryIntegrityIncident, raiseMemoryIntegrityIncident } from "./incidents.js";
4
6
  import { persistPolicySnapshot } from "./policy-store.js";
5
7
  import { inputsPrincipal } from "./inputs-read.js";
6
- import { memoryBelongsToOtherTenant, memoryExistsForTenant, memoryGet, memoryPut, readMemoryMeta, } from "./tools/memory.js";
8
+ import { assertRecordKeyTrust, resolveHeadDir } from "./keys.js";
9
+ import { openMemoryStoreSync, publishMemoryStore } from "./memory-store.js";
10
+ import { memoryBelongsToOtherTenant, memoryExistsForTenant, memoryGet, memoryHistoryUnavailable, memoryPut, readMemoryMeta, } from "./tools/memory.js";
7
11
  import { auditExplain } from "./tools/audit.js";
8
12
  import { messageRead, messageSend } from "./tools/message.js";
9
13
  import { spendAuthorize } from "./tools/spend.js";
@@ -16,6 +20,9 @@ export const TOOL_NAMES = [
16
20
  "message.send",
17
21
  "spend",
18
22
  ];
23
+ function isLegacyExplainPin(value) {
24
+ return value.includes("BEGIN") && value.includes("PUBLIC KEY");
25
+ }
19
26
  /**
20
27
  * Tool functions live in a Map that is not exported. The only way to
21
28
  * reach them at runtime is `proxy.call` -> `inner`. Extra tools (a
@@ -24,6 +31,11 @@ export const TOOL_NAMES = [
24
31
  */
25
32
  export function createBodyServices(opts) {
26
33
  const extraNames = [];
34
+ const alarmUrl = opts.alarmWebhookUrl?.trim() ?? "";
35
+ if (alarmUrl !== "" && !isAllowedAlarmWebhookUrl(alarmUrl)) {
36
+ throw new Error(ALARM_WEBHOOK_URL_ERROR);
37
+ }
38
+ const retryMs = alarmRetryMs(opts.alarmRetryMs);
27
39
  const reserved = new Set(TOOL_NAMES);
28
40
  for (const tool of opts.extraTools ?? []) {
29
41
  if (reserved.has(tool.name) || extraNames.includes(tool.name)) {
@@ -52,6 +64,7 @@ export function createBodyServices(opts) {
52
64
  }
53
65
  const policyDocument = JSON.parse(policyText);
54
66
  const policy = loadPolicy(policyText);
67
+ const publicKeyPem = assertRecordKeyTrust(opts);
55
68
  const ledger = new FileLedger(opts.stateDir);
56
69
  ledger.remoteWitness = (row, resultHash) => requestWitnessSign(opts.stateDir, row, resultHash);
57
70
  ledger.onPieceClose = async (window) => {
@@ -61,27 +74,65 @@ export function createBodyServices(opts) {
61
74
  endMs: window.endMs,
62
75
  });
63
76
  };
77
+ let store;
78
+ const now = opts.now ?? (() => Date.now());
79
+ const nonce = opts.nonce ?? (() => crypto.randomUUID());
64
80
  try {
65
81
  persistPolicySnapshot(opts.stateDir, policy.hash, policyDocument);
82
+ store = publishMemoryStore(openMemoryStoreSync({
83
+ stateDir: opts.stateDir,
84
+ publicKeyPem,
85
+ privateKeyPem: opts.recordSigner.privateKeyPem,
86
+ headDir: resolveHeadDir(opts.stateDir, opts.keysDir, opts.headDir ?? process.env.VERAX_MEMORY_HEAD_DIR),
87
+ }));
88
+ completeMemoryIntegrityIncident({
89
+ stateDir: opts.stateDir,
90
+ publicKeyPem,
91
+ privateKeyPem: opts.recordSigner.privateKeyPem,
92
+ memoryHead: store.memoryHead(),
93
+ ...(alarmUrl !== "" ? { alarmWebhookUrl: alarmUrl } : {}),
94
+ });
66
95
  }
67
96
  catch (err) {
68
97
  ledger.close();
69
98
  throw err;
70
99
  }
71
- const now = opts.now ?? (() => Date.now());
72
- const nonce = opts.nonce ?? (() => crypto.randomUUID());
100
+ const finishPending = () => {
101
+ completeMemoryIntegrityIncident({
102
+ stateDir: opts.stateDir,
103
+ publicKeyPem,
104
+ privateKeyPem: opts.recordSigner.privateKeyPem,
105
+ memoryHead: store.memoryHead(),
106
+ ...(alarmUrl !== "" ? { alarmWebhookUrl: alarmUrl } : {}),
107
+ });
108
+ };
109
+ const stopAlarms = alarmUrl === ""
110
+ ? async () => { }
111
+ : startAlarmDelivery({ stateDir: opts.stateDir, url: alarmUrl, retryMs, onTick: finishPending });
112
+ const onIntegrity = (reason) => {
113
+ raiseMemoryIntegrityIncident({
114
+ stateDir: opts.stateDir,
115
+ reason,
116
+ publicKeyPem,
117
+ privateKeyPem: opts.recordSigner.privateKeyPem,
118
+ memoryHead: store.memoryHead(),
119
+ now: now(),
120
+ ...(alarmUrl !== "" ? { alarmWebhookUrl: alarmUrl } : {}),
121
+ });
122
+ pokeAlarmDelivery(opts.stateDir);
123
+ };
73
124
  const registry = new Map();
74
- registry.set("memory.get", (call, principal) => memoryGet(call, opts.stateDir, now, principal));
75
- registry.set("memory.put", (call, principal) => memoryPut(call, opts.stateDir, principal));
125
+ registry.set("memory.get", (call, principal, _ref, bound) => memoryGet(call, opts.stateDir, now, principal, store, bound, onIntegrity));
126
+ registry.set("memory.put", (call, principal, ref) => memoryPut(call, opts.stateDir, principal, ref ?? "", store));
76
127
  const explainOpts = async () => {
77
- const env = process.env.VERAX_RECORD_PUBKEY_PIN;
78
- const pem = env && env.trim() !== "" ? env : opts.recordSigner.publicKeyPem;
128
+ const env = process.env.VERAX_RECORD_PUBKEY_PIN?.trim() ?? "";
129
+ const pem = env !== "" && isLegacyExplainPin(env) ? env : opts.recordSigner.publicKeyPem;
79
130
  return {
80
131
  ...(pem
81
132
  ? {
82
133
  issuerTrust: {
83
134
  publicKeyPem: pem,
84
- source: env && env.trim() !== "" ? "env" : "own-key",
135
+ source: env !== "" && isLegacyExplainPin(env) ? "env" : "own-key",
85
136
  },
86
137
  }
87
138
  : {}),
@@ -94,7 +145,7 @@ export function createBodyServices(opts) {
94
145
  for (const tool of opts.extraTools ?? []) {
95
146
  registry.set(tool.name, tool.fn);
96
147
  }
97
- const inner = async (call, principal, ref) => {
148
+ const inner = async (call, principal, ref, bound) => {
98
149
  const fn = registry.get(call.name);
99
150
  if (!fn) {
100
151
  return {
@@ -102,7 +153,7 @@ export function createBodyServices(opts) {
102
153
  isError: true,
103
154
  };
104
155
  }
105
- return fn(call, principal, ref);
156
+ return fn(call, principal, ref, bound);
106
157
  };
107
158
  // Halt and resume lines are copied into the ledger when the body opens
108
159
  // (`listen` awaits `proxy.syncControlRecords` before the socket accepts)
@@ -115,15 +166,17 @@ export function createBodyServices(opts) {
115
166
  now,
116
167
  nonce,
117
168
  inner,
118
- resolveInput: (id, principal) => readMemoryMeta(opts.stateDir, id, principal),
169
+ resolveInput: (id, principal) => readMemoryMeta(opts.stateDir, id, principal, store, onIntegrity),
119
170
  checkTenantMismatch: async (call, principal) => {
120
171
  if (call.name === "memory.get") {
172
+ if (memoryHistoryUnavailable(opts.stateDir, store))
173
+ return false;
121
174
  const id = call.arguments.id;
122
175
  if (typeof id !== "string")
123
176
  return false;
124
- if (memoryExistsForTenant(opts.stateDir, id, principal))
177
+ if (memoryExistsForTenant(opts.stateDir, id, principal, store))
125
178
  return false;
126
- return memoryBelongsToOtherTenant(opts.stateDir, id, tenantKey(principal));
179
+ return memoryBelongsToOtherTenant(opts.stateDir, id, tenantKey(principal), store);
127
180
  }
128
181
  if (call.name === "audit.explain") {
129
182
  const ref = call.arguments.ref;
@@ -148,5 +201,9 @@ export function createBodyServices(opts) {
148
201
  policyDocument,
149
202
  listTools: () => (extraNames.length === 0 ? TOOL_NAMES : [...TOOL_NAMES, ...extraNames]),
150
203
  explainOpts,
204
+ recordSigner: opts.recordSigner,
205
+ memoryStore: store,
206
+ stopAlarms,
207
+ ...(opts.keysDir ? { keysDir: opts.keysDir } : {}),
151
208
  };
152
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/body",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -26,8 +26,8 @@
26
26
  "@cedulon/cose": "0.13.1",
27
27
  "@cedulon/effect-extract": "0.13.1",
28
28
  "@modelcontextprotocol/sdk": "^1.30.0",
29
- "@verax-ai/inventory": "^0.6.1",
30
- "@verax-ai/proxy": "^0.6.1",
29
+ "@verax-ai/inventory": "^0.7.0",
30
+ "@verax-ai/proxy": "^0.7.0",
31
31
  "jose": "^6.2.12"
32
32
  },
33
33
  "description": "VERAX body: an MCP server that decides, records and proves what an agent did.",