@verax-ai/body 0.6.1 → 0.8.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/alarm-outbox.d.ts +18 -0
- package/dist/alarm-outbox.js +199 -0
- package/dist/alarm-webhook.d.ts +16 -0
- package/dist/alarm-webhook.js +106 -0
- package/dist/approval-bound.d.ts +9 -0
- package/dist/approval-bound.js +16 -0
- package/dist/approve-cli.d.ts +7 -0
- package/dist/approve-cli.js +37 -17
- package/dist/cli.js +14 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +35 -0
- package/dist/control-signature.d.ts +1 -0
- package/dist/control-signature.js +15 -3
- package/dist/doctor.js +9 -0
- package/dist/downstream.js +13 -7
- package/dist/halt.d.ts +28 -9
- package/dist/halt.js +126 -18
- package/dist/held-spend.d.ts +1 -1
- package/dist/held-spend.js +11 -6
- package/dist/incidents.d.ts +67 -0
- package/dist/incidents.js +627 -0
- package/dist/keys.d.ts +18 -1
- package/dist/keys.js +109 -5
- package/dist/memory-reattest-cli.d.ts +9 -0
- package/dist/memory-reattest-cli.js +110 -0
- package/dist/memory-rehead-cli.d.ts +9 -0
- package/dist/memory-rehead-cli.js +65 -0
- package/dist/memory-store.d.ts +157 -0
- package/dist/memory-store.js +917 -0
- package/dist/operator-door.js +7 -3
- package/dist/operator-handlers.js +36 -8
- package/dist/reconcile-cli.js +3 -1
- package/dist/server.js +49 -17
- package/dist/tools/memory.d.ts +12 -6
- package/dist/tools/memory.js +80 -124
- package/dist/verify-cli.js +1 -0
- package/dist/watch.js +3 -1
- package/dist/wiring.d.ts +14 -1
- package/dist/wiring.js +71 -14
- package/package.json +3 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const ALARM_OUTBOX_DIR = "alarm-outbox";
|
|
2
|
+
export declare const ALARM_HISTORY = "alarm-history.jsonl";
|
|
3
|
+
export declare const ALARM_EVENT_ID: RegExp;
|
|
4
|
+
export type AlarmEnvelope = {
|
|
5
|
+
payload: Record<string, unknown>;
|
|
6
|
+
sig: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function isAlarmEventId(eventId: string): boolean;
|
|
9
|
+
/** A leaf name that cannot walk out of the outbox directory. */
|
|
10
|
+
export declare function isSafeOutboxEventId(eventId: string): boolean;
|
|
11
|
+
/** Same-name file beside the outbox directory. A folder here blocks the write. */
|
|
12
|
+
export declare const ALARM_OUTBOX_RECORD = "alarm-outbox.json";
|
|
13
|
+
export declare function hasAlarmOutbox(stateDir: string, eventId: string): boolean;
|
|
14
|
+
export declare function deliveredAlarmIds(stateDir: string): Set<string>;
|
|
15
|
+
export declare function hasAlarmDelivery(stateDir: string, eventId: string): boolean;
|
|
16
|
+
export declare function writeAlarmOutbox(stateDir: string, envelope: AlarmEnvelope): void;
|
|
17
|
+
export declare function listAlarmOutbox(stateDir: string): AlarmEnvelope[];
|
|
18
|
+
export declare function markAlarmDelivered(stateDir: string, envelope: AlarmEnvelope, now?: number): void;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { chmodSync, closeSync, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, statSync, unlinkSync, writeSync, } from "node:fs";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
export const ALARM_OUTBOX_DIR = "alarm-outbox";
|
|
5
|
+
export const ALARM_HISTORY = "alarm-history.jsonl";
|
|
6
|
+
export const ALARM_EVENT_ID = /^[0-9a-f-]{36}$/i;
|
|
7
|
+
export function isAlarmEventId(eventId) {
|
|
8
|
+
if (!ALARM_EVENT_ID.test(eventId))
|
|
9
|
+
return false;
|
|
10
|
+
if (eventId.includes("/") || eventId.includes("\\") || eventId.includes(".."))
|
|
11
|
+
return false;
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
/** A leaf name that cannot walk out of the outbox directory. */
|
|
15
|
+
export function isSafeOutboxEventId(eventId) {
|
|
16
|
+
if (eventId === "" || eventId.includes("/") || eventId.includes("\\") || eventId.includes(".."))
|
|
17
|
+
return false;
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
function outboxDir(stateDir) {
|
|
21
|
+
return join(stateDir, ALARM_OUTBOX_DIR);
|
|
22
|
+
}
|
|
23
|
+
function outboxPath(stateDir, eventId) {
|
|
24
|
+
if (!isSafeOutboxEventId(eventId))
|
|
25
|
+
throw new Error("alarm-event-id-invalid");
|
|
26
|
+
const dir = resolve(outboxDir(stateDir));
|
|
27
|
+
const leaf = `${eventId}.json`;
|
|
28
|
+
const path = resolve(dir, leaf);
|
|
29
|
+
const rel = relative(dir, path);
|
|
30
|
+
if (rel === "" || isAbsolute(rel) || rel.startsWith("..") || rel.split(sep).includes("..")) {
|
|
31
|
+
throw new Error("alarm-outbox-path-escape");
|
|
32
|
+
}
|
|
33
|
+
if (path !== join(dir, leaf))
|
|
34
|
+
throw new Error("alarm-outbox-path-escape");
|
|
35
|
+
return path;
|
|
36
|
+
}
|
|
37
|
+
function isUnsupportedDirFsync(err) {
|
|
38
|
+
const code = err.code;
|
|
39
|
+
return code === "EPERM" || code === "EISDIR" || code === "EINVAL" || code === "ENOTSUP" || code === "ENOSYS";
|
|
40
|
+
}
|
|
41
|
+
function fsyncDir(dir) {
|
|
42
|
+
try {
|
|
43
|
+
const fd = openSync(dir, "r");
|
|
44
|
+
try {
|
|
45
|
+
fsyncSync(fd);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
closeSync(fd);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
if (isUnsupportedDirFsync(err))
|
|
53
|
+
return;
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Temporary file, fsync, rename. A reader sees the old file or the new one. */
|
|
58
|
+
function writeFileDurable(finalPath, payload) {
|
|
59
|
+
const dir = dirname(finalPath);
|
|
60
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
61
|
+
fsyncDir(dir);
|
|
62
|
+
const tmp = `${finalPath}.${process.pid}.${randomBytes(8).toString("hex")}.tmp`;
|
|
63
|
+
const fd = openSync(tmp, "w", 0o600);
|
|
64
|
+
try {
|
|
65
|
+
writeSync(fd, payload, 0, "utf8");
|
|
66
|
+
fsyncSync(fd);
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
renameSync(tmp, finalPath);
|
|
72
|
+
try {
|
|
73
|
+
chmodSync(finalPath, 0o600);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Windows may refuse chmod; the file is already written.
|
|
77
|
+
}
|
|
78
|
+
fsyncDir(dir);
|
|
79
|
+
}
|
|
80
|
+
/** Same-name file beside the outbox directory. A folder here blocks the write. */
|
|
81
|
+
export const ALARM_OUTBOX_RECORD = `${ALARM_OUTBOX_DIR}.json`;
|
|
82
|
+
function outboxRecordPath(stateDir) {
|
|
83
|
+
return join(stateDir, ALARM_OUTBOX_RECORD);
|
|
84
|
+
}
|
|
85
|
+
function readOutboxEnvelope(path) {
|
|
86
|
+
try {
|
|
87
|
+
if (!statSync(path).isFile())
|
|
88
|
+
return null;
|
|
89
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
90
|
+
if (!parsed || typeof parsed !== "object" || typeof parsed.sig !== "string" || parsed.sig === "")
|
|
91
|
+
return null;
|
|
92
|
+
if (!parsed.payload || typeof parsed.payload !== "object")
|
|
93
|
+
return null;
|
|
94
|
+
const id = parsed.payload.eventId;
|
|
95
|
+
if (typeof id !== "string" || !isSafeOutboxEventId(id))
|
|
96
|
+
return null;
|
|
97
|
+
return parsed;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export function hasAlarmOutbox(stateDir, eventId) {
|
|
104
|
+
if (!isSafeOutboxEventId(eventId))
|
|
105
|
+
return false;
|
|
106
|
+
const envelope = readOutboxEnvelope(outboxPath(stateDir, eventId));
|
|
107
|
+
return envelope !== null && envelope.payload.eventId === eventId;
|
|
108
|
+
}
|
|
109
|
+
export function deliveredAlarmIds(stateDir) {
|
|
110
|
+
const path = join(stateDir, ALARM_HISTORY);
|
|
111
|
+
if (!existsSync(path))
|
|
112
|
+
return new Set();
|
|
113
|
+
const raw = readFileSync(path, "utf8");
|
|
114
|
+
const ids = new Set();
|
|
115
|
+
for (const line of raw.split("\n")) {
|
|
116
|
+
if (line.trim() === "")
|
|
117
|
+
continue;
|
|
118
|
+
try {
|
|
119
|
+
const row = JSON.parse(line);
|
|
120
|
+
if (typeof row.eventId === "string" && isSafeOutboxEventId(row.eventId))
|
|
121
|
+
ids.add(row.eventId);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// A torn history line is not a delivery.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return ids;
|
|
128
|
+
}
|
|
129
|
+
export function hasAlarmDelivery(stateDir, eventId) {
|
|
130
|
+
return deliveredAlarmIds(stateDir).has(eventId);
|
|
131
|
+
}
|
|
132
|
+
function appendFileDurable(path, line) {
|
|
133
|
+
const dir = dirname(path);
|
|
134
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
135
|
+
const fd = openSync(path, "a+", 0o600);
|
|
136
|
+
try {
|
|
137
|
+
const size = fstatSync(fd).size;
|
|
138
|
+
if (size > 0) {
|
|
139
|
+
const last = Buffer.alloc(1);
|
|
140
|
+
if (readSync(fd, last, 0, 1, size - 1) === 1 && last[0] !== 0x0a) {
|
|
141
|
+
writeSync(fd, "\n", null, "utf8");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
writeSync(fd, line, null, "utf8");
|
|
145
|
+
fsyncSync(fd);
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
closeSync(fd);
|
|
149
|
+
}
|
|
150
|
+
fsyncDir(dir);
|
|
151
|
+
}
|
|
152
|
+
export function writeAlarmOutbox(stateDir, envelope) {
|
|
153
|
+
const eventId = envelope.payload.eventId;
|
|
154
|
+
if (typeof eventId !== "string" || eventId === "")
|
|
155
|
+
throw new Error("alarm-event-id-missing");
|
|
156
|
+
if (!isSafeOutboxEventId(eventId))
|
|
157
|
+
throw new Error("alarm-event-id-invalid");
|
|
158
|
+
const body = `${JSON.stringify(envelope)}\n`;
|
|
159
|
+
writeFileDurable(outboxRecordPath(stateDir), body);
|
|
160
|
+
writeFileDurable(outboxPath(stateDir, eventId), body);
|
|
161
|
+
}
|
|
162
|
+
export function listAlarmOutbox(stateDir) {
|
|
163
|
+
const delivered = deliveredAlarmIds(stateDir);
|
|
164
|
+
const dir = outboxDir(stateDir);
|
|
165
|
+
if (!existsSync(dir))
|
|
166
|
+
return [];
|
|
167
|
+
const names = readdirSync(dir).filter((name) => name.endsWith(".json")).sort();
|
|
168
|
+
const out = [];
|
|
169
|
+
for (const name of names) {
|
|
170
|
+
const parsed = readOutboxEnvelope(join(dir, name));
|
|
171
|
+
if (!parsed)
|
|
172
|
+
continue;
|
|
173
|
+
const eventId = parsed.payload.eventId;
|
|
174
|
+
if (typeof eventId !== "string" || delivered.has(eventId))
|
|
175
|
+
continue;
|
|
176
|
+
out.push(parsed);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
export function markAlarmDelivered(stateDir, envelope, now = Date.now()) {
|
|
181
|
+
const eventId = envelope.payload.eventId;
|
|
182
|
+
if (typeof eventId !== "string" || !isSafeOutboxEventId(eventId))
|
|
183
|
+
return;
|
|
184
|
+
const line = {
|
|
185
|
+
eventId,
|
|
186
|
+
deliveredAtMs: now,
|
|
187
|
+
triggerCode: envelope.payload.triggerCode,
|
|
188
|
+
};
|
|
189
|
+
appendFileDurable(join(stateDir, ALARM_HISTORY), `${JSON.stringify(line)}\n`);
|
|
190
|
+
for (const path of [outboxPath(stateDir, eventId), outboxRecordPath(stateDir)]) {
|
|
191
|
+
try {
|
|
192
|
+
unlinkSync(path);
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
if (err.code !== "ENOENT")
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const ALARM_POST_TIMEOUT_MS = 10000;
|
|
2
|
+
export declare function assertAlarmWebhookUrl(url: string): void;
|
|
3
|
+
/**
|
|
4
|
+
* POST the signed envelope. Redirects are not followed. A 2xx status is
|
|
5
|
+
* acceptance; the body is not read. Anything else, including a 302, is not
|
|
6
|
+
* delivery.
|
|
7
|
+
*/
|
|
8
|
+
export declare function postAlarmWebhook(url: string, envelope: unknown, signal?: AbortSignal): Promise<boolean>;
|
|
9
|
+
/** Retry pending outbox files until each receiver answers 2xx. Same eventId every time. */
|
|
10
|
+
export declare function startAlarmDelivery(opts: {
|
|
11
|
+
stateDir: string;
|
|
12
|
+
url: string;
|
|
13
|
+
retryMs: number;
|
|
14
|
+
onTick?: () => void;
|
|
15
|
+
}): () => Promise<void>;
|
|
16
|
+
export declare function pokeAlarmDelivery(stateDir: string): void;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { listAlarmOutbox, markAlarmDelivered } from "./alarm-outbox.js";
|
|
2
|
+
import { ALARM_WEBHOOK_URL_ERROR, isAllowedAlarmWebhookUrl } from "./config.js";
|
|
3
|
+
export const ALARM_POST_TIMEOUT_MS = 10_000;
|
|
4
|
+
export function assertAlarmWebhookUrl(url) {
|
|
5
|
+
if (!isAllowedAlarmWebhookUrl(url))
|
|
6
|
+
throw new Error(ALARM_WEBHOOK_URL_ERROR);
|
|
7
|
+
}
|
|
8
|
+
function oneLine(err) {
|
|
9
|
+
const text = err instanceof Error ? err.message : String(err);
|
|
10
|
+
return text.replace(/[\r\n]+/g, " ").slice(0, 300);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* POST the signed envelope. Redirects are not followed. A 2xx status is
|
|
14
|
+
* acceptance; the body is not read. Anything else, including a 302, is not
|
|
15
|
+
* delivery.
|
|
16
|
+
*/
|
|
17
|
+
export async function postAlarmWebhook(url, envelope, signal) {
|
|
18
|
+
const ac = new AbortController();
|
|
19
|
+
const onAbort = () => ac.abort();
|
|
20
|
+
if (signal) {
|
|
21
|
+
if (signal.aborted)
|
|
22
|
+
ac.abort();
|
|
23
|
+
else
|
|
24
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
25
|
+
}
|
|
26
|
+
const timer = setTimeout(() => ac.abort(), ALARM_POST_TIMEOUT_MS);
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch(url, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: { "content-type": "application/json" },
|
|
31
|
+
body: JSON.stringify(envelope),
|
|
32
|
+
redirect: "manual",
|
|
33
|
+
signal: ac.signal,
|
|
34
|
+
});
|
|
35
|
+
const body = res.body;
|
|
36
|
+
if (body) {
|
|
37
|
+
try {
|
|
38
|
+
body.cancel();
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Closing an unread body is enough; a cancel that throws is not delivery.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return res.status >= 200 && res.status < 300;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
signal?.removeEventListener("abort", onAbort);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const pokers = new Map();
|
|
55
|
+
/** Retry pending outbox files until each receiver answers 2xx. Same eventId every time. */
|
|
56
|
+
export function startAlarmDelivery(opts) {
|
|
57
|
+
assertAlarmWebhookUrl(opts.url);
|
|
58
|
+
let stopped = false;
|
|
59
|
+
let inFlight = false;
|
|
60
|
+
let running = Promise.resolve();
|
|
61
|
+
const abort = new AbortController();
|
|
62
|
+
const tick = () => {
|
|
63
|
+
if (stopped || inFlight)
|
|
64
|
+
return;
|
|
65
|
+
inFlight = true;
|
|
66
|
+
running = (async () => {
|
|
67
|
+
try {
|
|
68
|
+
try {
|
|
69
|
+
opts.onTick?.();
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
process.stderr.write(`verax: alarm-delivery: ${oneLine(err)}\n`);
|
|
73
|
+
}
|
|
74
|
+
for (const envelope of listAlarmOutbox(opts.stateDir)) {
|
|
75
|
+
if (stopped)
|
|
76
|
+
break;
|
|
77
|
+
const ok = await postAlarmWebhook(opts.url, envelope, abort.signal);
|
|
78
|
+
if (stopped)
|
|
79
|
+
break;
|
|
80
|
+
if (ok)
|
|
81
|
+
markAlarmDelivered(opts.stateDir, envelope);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
process.stderr.write(`verax: alarm-delivery: ${oneLine(err)}\n`);
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
inFlight = false;
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
};
|
|
92
|
+
pokers.set(opts.stateDir, tick);
|
|
93
|
+
tick();
|
|
94
|
+
const timer = setInterval(tick, opts.retryMs);
|
|
95
|
+
timer.unref();
|
|
96
|
+
return async () => {
|
|
97
|
+
stopped = true;
|
|
98
|
+
abort.abort();
|
|
99
|
+
pokers.delete(opts.stateDir);
|
|
100
|
+
clearInterval(timer);
|
|
101
|
+
await running;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
export function pokeAlarmDelivery(stateDir) {
|
|
105
|
+
pokers.get(stateDir)?.();
|
|
106
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The approvals row on disk is not signed. The terminal, the operator door and
|
|
3
|
+
* the ledger API show it, while a running body approves its own copy. Each of
|
|
4
|
+
* them holds the row to the hashes the signed defer names, here, so a row
|
|
5
|
+
* edited on disk is never what an operator is asked to approve.
|
|
6
|
+
*/
|
|
7
|
+
import { type ApprovalRow } from "@verax-ai/proxy";
|
|
8
|
+
/** Reads the decision and inputs files once; the returned check is per row. */
|
|
9
|
+
export declare function boundOnDisk(stateDir: string): (row: ApprovalRow) => boolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The approvals row on disk is not signed. The terminal, the operator door and
|
|
3
|
+
* the ledger API show it, while a running body approves its own copy. Each of
|
|
4
|
+
* them holds the row to the hashes the signed defer names, here, so a row
|
|
5
|
+
* edited on disk is never what an operator is asked to approve.
|
|
6
|
+
*/
|
|
7
|
+
import { approvalArgsBound, loadDecisionRequestHashesFromDir, loadSignedForwardedHashesFromDir, } from "@verax-ai/proxy";
|
|
8
|
+
/** Reads the decision and inputs files once; the returned check is per row. */
|
|
9
|
+
export function boundOnDisk(stateDir) {
|
|
10
|
+
const requestHashes = loadDecisionRequestHashesFromDir(stateDir);
|
|
11
|
+
const forwardedHashes = loadSignedForwardedHashesFromDir(stateDir);
|
|
12
|
+
return (row) => {
|
|
13
|
+
const signed = requestHashes.get(row.ref);
|
|
14
|
+
return typeof signed === "string" && signed !== "" && approvalArgsBound(row, signed, forwardedHashes.get(row.ref));
|
|
15
|
+
};
|
|
16
|
+
}
|
package/dist/approve-cli.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ApprovalRow } from "@verax-ai/proxy";
|
|
1
2
|
import { type ToolExec, type WindowsMachineRoots } from "./install.ts";
|
|
2
3
|
export declare function resolveApproveRef(pending: {
|
|
3
4
|
ref: string;
|
|
@@ -10,6 +11,12 @@ export declare function resolveApproveRef(pending: {
|
|
|
10
11
|
reason: "unknown-ref" | "ambiguous-ref";
|
|
11
12
|
candidates: string[];
|
|
12
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* What the terminal prints for a held row before it asks, or null when a
|
|
16
|
+
* spend's amount cannot be read as an integer. `runApprove` shows it only for
|
|
17
|
+
* a row the signed defer names.
|
|
18
|
+
*/
|
|
19
|
+
export declare function heldPrompt(row: ApprovalRow): string | null;
|
|
13
20
|
export type ApproveIo = {
|
|
14
21
|
isTTY: boolean;
|
|
15
22
|
ask: (prompt: string) => Promise<string>;
|
package/dist/approve-cli.js
CHANGED
|
@@ -2,7 +2,8 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { userInfo } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { createInterface } from "node:readline";
|
|
5
|
-
import { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, FileLedger, loadApprovalsFromDir, loadPolicy, DISPLAY_ESCAPE_CLASS, } from "@verax-ai/proxy";
|
|
5
|
+
import { approvalDispatchArgs, approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, FileLedger, loadApprovalsFromDir, loadPolicy, DISPLAY_ESCAPE_CLASS, } from "@verax-ai/proxy";
|
|
6
|
+
import { boundOnDisk } from "./approval-bound.js";
|
|
6
7
|
import { EX_CONFIG } from "./config.js";
|
|
7
8
|
import { heldSpend } from "./held-spend.js";
|
|
8
9
|
import { cliCodeCheckPassedAlready, defaultElevated, directoryAccess, elevatedCommandCodeRefusal, elevatedStateDirRefusal, readWindowsMachineRoots, stateDirFor, SystemToolError, unreadableSentence, windowsProgramDataRefusal } from "./install.js";
|
|
@@ -99,10 +100,26 @@ function spendPrompt(row, spend, amount) {
|
|
|
99
100
|
}
|
|
100
101
|
function argumentPrompt(row) {
|
|
101
102
|
const lines = [quotedField("tool", row.subject)];
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
const args = approvalDispatchArgs(row);
|
|
104
|
+
for (const key of Object.keys(args).sort())
|
|
105
|
+
lines.push(quotedField(key, args[key]));
|
|
104
106
|
return lines.join("");
|
|
105
107
|
}
|
|
108
|
+
const TYPE_AMOUNT = "Type the amount in minor units:\n";
|
|
109
|
+
const TYPE_YES = "Type yes:\n";
|
|
110
|
+
/**
|
|
111
|
+
* What the terminal prints for a held row before it asks, or null when a
|
|
112
|
+
* spend's amount cannot be read as an integer. `runApprove` shows it only for
|
|
113
|
+
* a row the signed defer names.
|
|
114
|
+
*/
|
|
115
|
+
export function heldPrompt(row) {
|
|
116
|
+
const spend = heldSpend(row);
|
|
117
|
+
if (!spend)
|
|
118
|
+
return `${argumentPrompt(row)}${TYPE_YES}`;
|
|
119
|
+
if (spend.amountMinor === null)
|
|
120
|
+
return null;
|
|
121
|
+
return `${spendPrompt(row, spend, spend.amountMinor)}${TYPE_AMOUNT}`;
|
|
122
|
+
}
|
|
106
123
|
function readTypedAmount() {
|
|
107
124
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
108
125
|
return new Promise((resolve) => {
|
|
@@ -217,25 +234,28 @@ export async function runApprove(argv, writeErr = (s) => process.stderr.write(s)
|
|
|
217
234
|
writeErr("approve-unknown-ref\n");
|
|
218
235
|
return 78;
|
|
219
236
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
237
|
+
// The prompt shows the row on disk; a running body approves its own copy.
|
|
238
|
+
// Only a row the signed defer names is shown.
|
|
239
|
+
if (!boundOnDisk(stateDir)(waiting)) {
|
|
240
|
+
writeErr("approve-row-unbound\n");
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
const prompt = heldPrompt(waiting);
|
|
244
|
+
if (prompt === null) {
|
|
245
|
+
writeErr("approve-amount-unreadable\n");
|
|
246
|
+
return 1;
|
|
247
|
+
}
|
|
248
|
+
writeOut(prompt);
|
|
249
|
+
const expected = heldSpend(waiting)?.amountMinor;
|
|
250
|
+
if (typeof expected === "number") {
|
|
251
|
+
const typed = (await terminal.ask(TYPE_AMOUNT)).trim();
|
|
230
252
|
if (typed !== String(expected)) {
|
|
231
253
|
writeErr("approve-amount-mismatch\n");
|
|
232
254
|
return 1;
|
|
233
255
|
}
|
|
234
256
|
}
|
|
235
257
|
else {
|
|
236
|
-
|
|
237
|
-
writeOut("Type yes:\n");
|
|
238
|
-
const typed = (await terminal.ask("Type yes:\n")).trim();
|
|
258
|
+
const typed = (await terminal.ask(TYPE_YES)).trim();
|
|
239
259
|
if (typed !== "yes") {
|
|
240
260
|
writeErr("approve-confirm-mismatch\n");
|
|
241
261
|
return 1;
|
|
@@ -257,7 +277,7 @@ export async function runApprove(argv, writeErr = (s) => process.stderr.write(s)
|
|
|
257
277
|
return 1;
|
|
258
278
|
}
|
|
259
279
|
try {
|
|
260
|
-
const signers = loadOrCreateSigners(stateDir);
|
|
280
|
+
const signers = loadOrCreateSigners(stateDir, env.VERAX_KEYS_DIR);
|
|
261
281
|
const approvals = approvalsLogFor(ledger);
|
|
262
282
|
const decisions = await ledger.decisions();
|
|
263
283
|
const defer = decisions.find((d) => d.claims.ref === ref && d.claims.decision === "defer");
|
package/dist/cli.js
CHANGED
|
@@ -21,6 +21,8 @@ import { runVerify } from "./verify-cli.js";
|
|
|
21
21
|
import { runUnlock } from "./unlock.js";
|
|
22
22
|
import { runWitness } from "./witness.js";
|
|
23
23
|
import { runWatch } from "./watch.js";
|
|
24
|
+
import { runMemoryRehead } from "./memory-rehead-cli.js";
|
|
25
|
+
import { runMemoryReattest } from "./memory-reattest-cli.js";
|
|
24
26
|
const HELP = `verax - the body an agent asks before it acts, and the ledger it answers from.
|
|
25
27
|
|
|
26
28
|
Usage: verax <command> [options]
|
|
@@ -69,6 +71,12 @@ Usage: verax <command> [options]
|
|
|
69
71
|
halt <stateDir> stop the body from allowing anything further
|
|
70
72
|
resume <stateDir> lift a halt; who and when go to halt-history.jsonl
|
|
71
73
|
unlock [--force] <stateDir> clear a stale ledger lock
|
|
74
|
+
memory reattest <stateDir>
|
|
75
|
+
convert 0.6.1 flat memory files to signed V2 revisions.
|
|
76
|
+
The body must be stopped.
|
|
77
|
+
memory rehead <stateDir>
|
|
78
|
+
write a signed head from the current commit chain.
|
|
79
|
+
The body must be stopped.
|
|
72
80
|
|
|
73
81
|
--help, -h print this
|
|
74
82
|
--version, -v print the version
|
|
@@ -333,6 +341,12 @@ async function dispatchCli(argv, stdout, stderr, env) {
|
|
|
333
341
|
await runWitness(stateDir);
|
|
334
342
|
return 0;
|
|
335
343
|
}
|
|
344
|
+
if (argv[0] === "memory") {
|
|
345
|
+
if (argv[1] === "rehead") {
|
|
346
|
+
return runMemoryRehead(argv.slice(1), stdout, stderr, env);
|
|
347
|
+
}
|
|
348
|
+
return runMemoryReattest(argv.slice(1), stdout, stderr, env);
|
|
349
|
+
}
|
|
336
350
|
const head = argv[0];
|
|
337
351
|
if (head !== undefined && !head.startsWith("--")) {
|
|
338
352
|
stderr.write(`unknown command: ${terminalText(head)}\n`);
|
package/dist/config.d.ts
CHANGED
|
@@ -46,6 +46,27 @@ export type BodyConfig = {
|
|
|
46
46
|
messageQuotaBytes?: number;
|
|
47
47
|
/** The operator door, a second listener. Null leaves it shut. */
|
|
48
48
|
operatorDoor?: OperatorDoorConfig | null;
|
|
49
|
+
/**
|
|
50
|
+
* Directory holding record/effect signer PEMs. Default is `<stateDir>/keys`.
|
|
51
|
+
* `VERAX_KEYS_DIR` sets this when the keys must sit outside the state directory.
|
|
52
|
+
*/
|
|
53
|
+
keysDir?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Directory holding the signed memory head. Default is the keys directory.
|
|
56
|
+
* `VERAX_MEMORY_HEAD_DIR` sets this when the head must sit outside both.
|
|
57
|
+
*/
|
|
58
|
+
headDir?: string;
|
|
59
|
+
/**
|
|
60
|
+
* HTTPS webhook that receives a signed integrity incident. `http` is
|
|
61
|
+
* accepted only for 127.0.0.1, [::1], and localhost. `VERAX_ALARM_WEBHOOK_URL`
|
|
62
|
+
* sets this the same way `VERAX_KEYS_DIR` sets `keysDir`.
|
|
63
|
+
*/
|
|
64
|
+
alarmWebhookUrl?: string;
|
|
65
|
+
/**
|
|
66
|
+
* How often the body retries an undelivered alarm, in milliseconds.
|
|
67
|
+
* Default 30000; floor 100. `VERAX_ALARM_RETRY_MS` sets this.
|
|
68
|
+
*/
|
|
69
|
+
alarmRetryMs?: number;
|
|
49
70
|
};
|
|
50
71
|
export type OperatorDoorConfig = {
|
|
51
72
|
/** The one origin the door answers for. It is the passkey relying party. */
|
|
@@ -69,6 +90,12 @@ export type ConfigResult = {
|
|
|
69
90
|
code: typeof EX_CONFIG;
|
|
70
91
|
};
|
|
71
92
|
export declare function isLoopbackHost(host: string): boolean;
|
|
93
|
+
export declare const DEFAULT_ALARM_RETRY_MS = 30000;
|
|
94
|
+
export declare const MIN_ALARM_RETRY_MS = 100;
|
|
95
|
+
/** An https URL, or http on 127.0.0.1 / [::1] / localhost. Anything else is refused at start. */
|
|
96
|
+
export declare function isAllowedAlarmWebhookUrl(raw: string): boolean;
|
|
97
|
+
export declare const ALARM_WEBHOOK_URL_ERROR = "alarm webhook URL needs https (or http on 127.0.0.1, [::1], or localhost)";
|
|
98
|
+
export declare function alarmRetryMs(raw?: string | number): number;
|
|
72
99
|
/** A directory or an unreadable file. A path that is not there is left to the read. */
|
|
73
100
|
export declare function policyFileRefusal(path: string): string | null;
|
|
74
101
|
/**
|
package/dist/config.js
CHANGED
|
@@ -36,6 +36,30 @@ export function isLoopbackHost(host) {
|
|
|
36
36
|
const stripped = host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
|
|
37
37
|
return LOOPBACK.has(stripped);
|
|
38
38
|
}
|
|
39
|
+
export const DEFAULT_ALARM_RETRY_MS = 30_000;
|
|
40
|
+
export const MIN_ALARM_RETRY_MS = 100;
|
|
41
|
+
/** An https URL, or http on 127.0.0.1 / [::1] / localhost. Anything else is refused at start. */
|
|
42
|
+
export function isAllowedAlarmWebhookUrl(raw) {
|
|
43
|
+
let url;
|
|
44
|
+
try {
|
|
45
|
+
url = new URL(raw);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (url.protocol === "https:")
|
|
51
|
+
return true;
|
|
52
|
+
return url.protocol === "http:" && isLoopbackHost(url.hostname);
|
|
53
|
+
}
|
|
54
|
+
export const ALARM_WEBHOOK_URL_ERROR = "alarm webhook URL needs https (or http on 127.0.0.1, [::1], or localhost)";
|
|
55
|
+
export function alarmRetryMs(raw) {
|
|
56
|
+
if (raw === undefined || raw === "")
|
|
57
|
+
return DEFAULT_ALARM_RETRY_MS;
|
|
58
|
+
const n = typeof raw === "number" ? raw : Number(raw);
|
|
59
|
+
if (!Number.isFinite(n))
|
|
60
|
+
return DEFAULT_ALARM_RETRY_MS;
|
|
61
|
+
return Math.max(MIN_ALARM_RETRY_MS, Math.floor(n));
|
|
62
|
+
}
|
|
39
63
|
function jwksPinParses(raw) {
|
|
40
64
|
try {
|
|
41
65
|
const parsed = JSON.parse(raw);
|
|
@@ -186,6 +210,13 @@ export function loadConfig(env) {
|
|
|
186
210
|
}
|
|
187
211
|
const inventoryRaw = env.VERAX_INVENTORY_FILE?.trim() ?? "";
|
|
188
212
|
const downstreamRaw = env.VERAX_DOWNSTREAM?.trim() ?? "";
|
|
213
|
+
const keysDirRaw = env.VERAX_KEYS_DIR?.trim() ?? "";
|
|
214
|
+
const headDirRaw = env.VERAX_MEMORY_HEAD_DIR?.trim() ?? "";
|
|
215
|
+
const alarmWebhookRaw = env.VERAX_ALARM_WEBHOOK_URL?.trim() ?? "";
|
|
216
|
+
if (alarmWebhookRaw !== "" && !isAllowedAlarmWebhookUrl(alarmWebhookRaw)) {
|
|
217
|
+
return { ok: false, code: EX_CONFIG, reason: ALARM_WEBHOOK_URL_ERROR };
|
|
218
|
+
}
|
|
219
|
+
const alarmRetryRaw = env.VERAX_ALARM_RETRY_MS?.trim() ?? "";
|
|
189
220
|
return {
|
|
190
221
|
ok: true,
|
|
191
222
|
value: {
|
|
@@ -201,6 +232,10 @@ export function loadConfig(env) {
|
|
|
201
232
|
tlsTerminated,
|
|
202
233
|
inventoryFile: inventoryRaw === "" ? null : inventoryRaw,
|
|
203
234
|
downstreamFile: downstreamRaw === "" ? null : downstreamRaw,
|
|
235
|
+
...(keysDirRaw !== "" ? { keysDir: keysDirRaw } : {}),
|
|
236
|
+
...(headDirRaw !== "" ? { headDir: headDirRaw } : {}),
|
|
237
|
+
...(alarmWebhookRaw !== "" ? { alarmWebhookUrl: alarmWebhookRaw } : {}),
|
|
238
|
+
...(alarmRetryRaw !== "" ? { alarmRetryMs: alarmRetryMs(alarmRetryRaw) } : {}),
|
|
204
239
|
allowedOrigins: (env.VERAX_ALLOWED_ORIGINS ?? "")
|
|
205
240
|
.split(",")
|
|
206
241
|
.map((item) => item.trim())
|