@verax-ai/body 0.6.0 → 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,8 +1,13 @@
1
1
  import { createProxy, FileLedger, loadPolicy, tenantKey, } from "@verax-ai/proxy";
2
2
  import { readFileSync } from "node:fs";
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";
3
6
  import { persistPolicySnapshot } from "./policy-store.js";
4
7
  import { inputsPrincipal } from "./inputs-read.js";
5
- 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";
6
11
  import { auditExplain } from "./tools/audit.js";
7
12
  import { messageRead, messageSend } from "./tools/message.js";
8
13
  import { spendAuthorize } from "./tools/spend.js";
@@ -15,6 +20,9 @@ export const TOOL_NAMES = [
15
20
  "message.send",
16
21
  "spend",
17
22
  ];
23
+ function isLegacyExplainPin(value) {
24
+ return value.includes("BEGIN") && value.includes("PUBLIC KEY");
25
+ }
18
26
  /**
19
27
  * Tool functions live in a Map that is not exported. The only way to
20
28
  * reach them at runtime is `proxy.call` -> `inner`. Extra tools (a
@@ -23,6 +31,11 @@ export const TOOL_NAMES = [
23
31
  */
24
32
  export function createBodyServices(opts) {
25
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);
26
39
  const reserved = new Set(TOOL_NAMES);
27
40
  for (const tool of opts.extraTools ?? []) {
28
41
  if (reserved.has(tool.name) || extraNames.includes(tool.name)) {
@@ -36,9 +49,22 @@ export function createBodyServices(opts) {
36
49
  }
37
50
  // The policy is read before the ledger opens: opening takes the ledger lock,
38
51
  // and a start the policy stops must not leave that lock for the next one.
39
- const policyText = readFileSync(opts.policyFile, "utf8");
52
+ const policyRefusal = policyFileRefusal(opts.policyFile);
53
+ if (policyRefusal)
54
+ throw new Error(policyRefusal);
55
+ let policyText;
56
+ try {
57
+ policyText = readFileSync(opts.policyFile, "utf8");
58
+ }
59
+ catch (err) {
60
+ const code = err.code;
61
+ if (code === "EISDIR")
62
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is a directory: ${opts.policyFile}`);
63
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${opts.policyFile}`);
64
+ }
40
65
  const policyDocument = JSON.parse(policyText);
41
66
  const policy = loadPolicy(policyText);
67
+ const publicKeyPem = assertRecordKeyTrust(opts);
42
68
  const ledger = new FileLedger(opts.stateDir);
43
69
  ledger.remoteWitness = (row, resultHash) => requestWitnessSign(opts.stateDir, row, resultHash);
44
70
  ledger.onPieceClose = async (window) => {
@@ -48,27 +74,65 @@ export function createBodyServices(opts) {
48
74
  endMs: window.endMs,
49
75
  });
50
76
  };
77
+ let store;
78
+ const now = opts.now ?? (() => Date.now());
79
+ const nonce = opts.nonce ?? (() => crypto.randomUUID());
51
80
  try {
52
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
+ });
53
95
  }
54
96
  catch (err) {
55
97
  ledger.close();
56
98
  throw err;
57
99
  }
58
- const now = opts.now ?? (() => Date.now());
59
- 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
+ };
60
124
  const registry = new Map();
61
- registry.set("memory.get", (call, principal) => memoryGet(call, opts.stateDir, now, principal));
62
- 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));
63
127
  const explainOpts = async () => {
64
- const env = process.env.VERAX_RECORD_PUBKEY_PIN;
65
- 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;
66
130
  return {
67
131
  ...(pem
68
132
  ? {
69
133
  issuerTrust: {
70
134
  publicKeyPem: pem,
71
- source: env && env.trim() !== "" ? "env" : "own-key",
135
+ source: env !== "" && isLegacyExplainPin(env) ? "env" : "own-key",
72
136
  },
73
137
  }
74
138
  : {}),
@@ -81,7 +145,7 @@ export function createBodyServices(opts) {
81
145
  for (const tool of opts.extraTools ?? []) {
82
146
  registry.set(tool.name, tool.fn);
83
147
  }
84
- const inner = async (call, principal, ref) => {
148
+ const inner = async (call, principal, ref, bound) => {
85
149
  const fn = registry.get(call.name);
86
150
  if (!fn) {
87
151
  return {
@@ -89,7 +153,7 @@ export function createBodyServices(opts) {
89
153
  isError: true,
90
154
  };
91
155
  }
92
- return fn(call, principal, ref);
156
+ return fn(call, principal, ref, bound);
93
157
  };
94
158
  // Halt and resume lines are copied into the ledger when the body opens
95
159
  // (`listen` awaits `proxy.syncControlRecords` before the socket accepts)
@@ -102,15 +166,17 @@ export function createBodyServices(opts) {
102
166
  now,
103
167
  nonce,
104
168
  inner,
105
- resolveInput: (id, principal) => readMemoryMeta(opts.stateDir, id, principal),
169
+ resolveInput: (id, principal) => readMemoryMeta(opts.stateDir, id, principal, store, onIntegrity),
106
170
  checkTenantMismatch: async (call, principal) => {
107
171
  if (call.name === "memory.get") {
172
+ if (memoryHistoryUnavailable(opts.stateDir, store))
173
+ return false;
108
174
  const id = call.arguments.id;
109
175
  if (typeof id !== "string")
110
176
  return false;
111
- if (memoryExistsForTenant(opts.stateDir, id, principal))
177
+ if (memoryExistsForTenant(opts.stateDir, id, principal, store))
112
178
  return false;
113
- return memoryBelongsToOtherTenant(opts.stateDir, id, tenantKey(principal));
179
+ return memoryBelongsToOtherTenant(opts.stateDir, id, tenantKey(principal), store);
114
180
  }
115
181
  if (call.name === "audit.explain") {
116
182
  const ref = call.arguments.ref;
@@ -135,5 +201,9 @@ export function createBodyServices(opts) {
135
201
  policyDocument,
136
202
  listTools: () => (extraNames.length === 0 ? TOOL_NAMES : [...TOOL_NAMES, ...extraNames]),
137
203
  explainOpts,
204
+ recordSigner: opts.recordSigner,
205
+ memoryStore: store,
206
+ stopAlarms,
207
+ ...(opts.keysDir ? { keysDir: opts.keysDir } : {}),
138
208
  };
139
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/body",
3
- "version": "0.6.0",
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.0",
30
- "@verax-ai/proxy": "^0.6.0",
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.",