@verax-ai/proxy 0.4.1 → 0.4.3
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/anchors.d.ts +24 -0
- package/dist/anchors.js +86 -0
- package/dist/approvals.d.ts +3 -1
- package/dist/approvals.js +10 -2
- package/dist/control-sync.d.ts +49 -0
- package/dist/control-sync.js +364 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +3 -1
- package/dist/ledger.d.ts +11 -0
- package/dist/ledger.js +52 -32
- package/dist/proxy.d.ts +1 -0
- package/dist/proxy.js +53 -0
- package/dist/transparency-receipt.d.ts +38 -0
- package/dist/transparency-receipt.js +277 -0
- package/dist/types.d.ts +36 -0
- package/dist/verify-ledger.d.ts +29 -0
- package/dist/verify-ledger.js +317 -8
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const ANCHORS_NAME = "checkpoint-anchors.jsonl";
|
|
2
|
+
/** `register`: the SCITT digest route; the log's leaf entry is SHA-256 of the 32 checkpoint-hash octets. */
|
|
3
|
+
export type AnchorRoute = "register";
|
|
4
|
+
export type AnchorRow = {
|
|
5
|
+
v: 1;
|
|
6
|
+
checkpointHash: string;
|
|
7
|
+
service: string;
|
|
8
|
+
route: AnchorRoute;
|
|
9
|
+
entryHash: string;
|
|
10
|
+
leafIndex: number;
|
|
11
|
+
treeSize: number;
|
|
12
|
+
receiptB64: string;
|
|
13
|
+
atMs: number;
|
|
14
|
+
};
|
|
15
|
+
export declare function anchorsPath(dir: string): string;
|
|
16
|
+
/** The leaf entry the service's log holds for this checkpoint under `route`. */
|
|
17
|
+
export declare function anchorEntryHash(checkpointHash: string, route: AnchorRoute): string;
|
|
18
|
+
/** Rows in file order. A line that is not a row is named, not skipped silently. */
|
|
19
|
+
export declare function readAnchors(dir: string): {
|
|
20
|
+
present: boolean;
|
|
21
|
+
rows: AnchorRow[];
|
|
22
|
+
problems: string[];
|
|
23
|
+
};
|
|
24
|
+
export declare function appendAnchor(dir: string, row: AnchorRow): void;
|
package/dist/anchors.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkpoints registered with a Transparency Service run by someone else.
|
|
3
|
+
*
|
|
4
|
+
* `verax anchor` sends a service the SHA-256 of a checkpoint's COSE octets
|
|
5
|
+
* (the checkpoint hash core-03 defines) and nothing else: the window, the
|
|
6
|
+
* counts and the head stay on this machine. The service appends that digest to
|
|
7
|
+
* its RFC 9162 log and returns a COSE Receipt, which is kept here, one row per
|
|
8
|
+
* checkpoint and service, so `verax verify` can check it later with no network.
|
|
9
|
+
*
|
|
10
|
+
* What a verified receipt adds is a second party: the service's log held this
|
|
11
|
+
* checkpoint at the tree size in the receipt, under the service's key. A
|
|
12
|
+
* receipt does not show the service agrees with anything the checkpoint
|
|
13
|
+
* counts, and a reader who has only these files cannot tell whether a newer
|
|
14
|
+
* checkpoint was registered and then removed from them; asking the service is
|
|
15
|
+
* the check for that.
|
|
16
|
+
*/
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { appendFileSync, existsSync, lstatSync, readFileSync } from "node:fs";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
export const ANCHORS_NAME = "checkpoint-anchors.jsonl";
|
|
21
|
+
const HASH = /^[0-9a-f]{64}$/;
|
|
22
|
+
export function anchorsPath(dir) {
|
|
23
|
+
return join(dir, ANCHORS_NAME);
|
|
24
|
+
}
|
|
25
|
+
/** The leaf entry the service's log holds for this checkpoint under `route`. */
|
|
26
|
+
export function anchorEntryHash(checkpointHash, route) {
|
|
27
|
+
if (!HASH.test(checkpointHash))
|
|
28
|
+
throw new Error("anchor-checkpoint-hash");
|
|
29
|
+
if (route === "register")
|
|
30
|
+
return createHash("sha256").update(Buffer.from(checkpointHash, "hex")).digest("hex");
|
|
31
|
+
throw new Error(`anchor-route-${String(route)}`);
|
|
32
|
+
}
|
|
33
|
+
function asRow(value) {
|
|
34
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
35
|
+
return null;
|
|
36
|
+
const r = value;
|
|
37
|
+
if (r.v !== 1 || r.route !== "register")
|
|
38
|
+
return null;
|
|
39
|
+
if (typeof r.checkpointHash !== "string" || !HASH.test(r.checkpointHash))
|
|
40
|
+
return null;
|
|
41
|
+
if (typeof r.entryHash !== "string" || !HASH.test(r.entryHash))
|
|
42
|
+
return null;
|
|
43
|
+
if (typeof r.service !== "string" || r.service === "")
|
|
44
|
+
return null;
|
|
45
|
+
if (typeof r.receiptB64 !== "string" || r.receiptB64 === "")
|
|
46
|
+
return null;
|
|
47
|
+
if (!Number.isSafeInteger(r.leafIndex) || !Number.isSafeInteger(r.treeSize))
|
|
48
|
+
return null;
|
|
49
|
+
if (typeof r.atMs !== "number")
|
|
50
|
+
return null;
|
|
51
|
+
return r;
|
|
52
|
+
}
|
|
53
|
+
/** Rows in file order. A line that is not a row is named, not skipped silently. */
|
|
54
|
+
export function readAnchors(dir) {
|
|
55
|
+
const path = anchorsPath(dir);
|
|
56
|
+
if (!existsSync(path))
|
|
57
|
+
return { present: false, rows: [], problems: [] };
|
|
58
|
+
if (lstatSync(path).isSymbolicLink())
|
|
59
|
+
return { present: true, rows: [], problems: [`symbolic link: ${path}`] };
|
|
60
|
+
const rows = [];
|
|
61
|
+
const problems = [];
|
|
62
|
+
const lines = readFileSync(path, "utf8").split("\n");
|
|
63
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
64
|
+
const line = lines[i].trim();
|
|
65
|
+
if (line === "")
|
|
66
|
+
continue;
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(line);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
problems.push(`anchor line ${i + 1} is not JSON`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const row = asRow(parsed);
|
|
76
|
+
if (!row) {
|
|
77
|
+
problems.push(`anchor line ${i + 1} is not an anchor row`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
rows.push(row);
|
|
81
|
+
}
|
|
82
|
+
return { present: true, rows, problems };
|
|
83
|
+
}
|
|
84
|
+
export function appendAnchor(dir, row) {
|
|
85
|
+
appendFileSync(anchorsPath(dir), `${JSON.stringify(row)}\n`, { encoding: "utf8" });
|
|
86
|
+
}
|
package/dist/approvals.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare const approvalFs: {
|
|
|
6
6
|
export declare const drainMetrics: {
|
|
7
7
|
renameBusy: number;
|
|
8
8
|
};
|
|
9
|
-
import type { ApprovalChannel, InputsLog, Ledger, Policy, RecordSigner } from "./types.ts";
|
|
9
|
+
import type { ApprovalChannel, ApprovalSignature, InputsLog, Ledger, Policy, RecordSigner } from "./types.ts";
|
|
10
10
|
export type ApprovalRow = {
|
|
11
11
|
ref: string;
|
|
12
12
|
requestHash: string;
|
|
@@ -106,6 +106,8 @@ export declare function approvePending(opts: {
|
|
|
106
106
|
policyHash: string;
|
|
107
107
|
approvals: ApprovalsLog;
|
|
108
108
|
inputsLog?: InputsLog;
|
|
109
|
+
/** Set only for an HTTP approval whose passkey assertion already verified. */
|
|
110
|
+
signature?: ApprovalSignature;
|
|
109
111
|
budgetGuard?: (snap: ApprovalRow) => {
|
|
110
112
|
ok: true;
|
|
111
113
|
} | {
|
package/dist/approvals.js
CHANGED
|
@@ -220,6 +220,14 @@ async function hasResolves(inputsLog, ledger, deferRef) {
|
|
|
220
220
|
export async function approvePending(opts) {
|
|
221
221
|
return approvalLockFor(opts.ledger).enqueue(() => approvePendingUnlocked(opts));
|
|
222
222
|
}
|
|
223
|
+
function approverInputs(opts) {
|
|
224
|
+
return {
|
|
225
|
+
id: opts.approverId,
|
|
226
|
+
via: opts.via,
|
|
227
|
+
resolves: opts.ref,
|
|
228
|
+
...(opts.signature ? { signature: opts.signature } : {}),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
223
231
|
function ledgerStateDir(ledger) {
|
|
224
232
|
const dir = ledger.dir;
|
|
225
233
|
return typeof dir === "string" && dir !== "" ? dir : null;
|
|
@@ -268,7 +276,7 @@ async function approvePendingUnlocked(opts) {
|
|
|
268
276
|
const inputs = {
|
|
269
277
|
principal: prior?.principal ?? { brain: snap.brain, scopes: [] },
|
|
270
278
|
inputs: prior?.inputs ?? [],
|
|
271
|
-
approver:
|
|
279
|
+
approver: approverInputs(opts),
|
|
272
280
|
};
|
|
273
281
|
const inputsHash = sha256Canonical(inputs);
|
|
274
282
|
await inputsLog.append(expireRef, inputs);
|
|
@@ -303,7 +311,7 @@ async function approvePendingUnlocked(opts) {
|
|
|
303
311
|
const inputs = {
|
|
304
312
|
principal: prior?.principal ?? { brain: snap.brain, scopes: [] },
|
|
305
313
|
inputs: prior?.inputs ?? [],
|
|
306
|
-
approver:
|
|
314
|
+
approver: approverInputs(opts),
|
|
307
315
|
};
|
|
308
316
|
const inputsHash = sha256Canonical(inputs);
|
|
309
317
|
const effectHash = sha256Canonical(effectDescriptor(snap.subject, snap.args));
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { SignedDecisionRecord } from "@cedulon/core";
|
|
2
|
+
import type { LedgerWriter } from "./ledger.ts";
|
|
3
|
+
import type { InputsLog, RecordSigner } from "./types.ts";
|
|
4
|
+
/** `null` means `halt-history.jsonl` was absent. Content is not part of the stamp. */
|
|
5
|
+
type HistoryStamp = {
|
|
6
|
+
size: number;
|
|
7
|
+
mtimeMs: number;
|
|
8
|
+
} | null;
|
|
9
|
+
type Recorded = {
|
|
10
|
+
mismatched: boolean;
|
|
11
|
+
hasMismatch: boolean;
|
|
12
|
+
seenMismatch: boolean;
|
|
13
|
+
resumeFrom: number;
|
|
14
|
+
maxLine: number;
|
|
15
|
+
maxAfterAck: number;
|
|
16
|
+
fileHaltCovers: boolean;
|
|
17
|
+
/** Last control allow is a halt, so a halt window is open. */
|
|
18
|
+
openHalt: boolean;
|
|
19
|
+
hashesByLine: Map<number, string[]>;
|
|
20
|
+
};
|
|
21
|
+
export type ControlMemory = {
|
|
22
|
+
loaded: boolean;
|
|
23
|
+
recorded: Recorded;
|
|
24
|
+
stamp: HistoryStamp;
|
|
25
|
+
haltedPresent: boolean | null;
|
|
26
|
+
};
|
|
27
|
+
export declare function createControlMemory(): ControlMemory;
|
|
28
|
+
/**
|
|
29
|
+
* True when the history file and the switch look as they did at the last sync,
|
|
30
|
+
* so there is nothing to record. Cheap enough to run before taking the ledger
|
|
31
|
+
* queue: waiting a turn behind the previous call's durable writes only to find
|
|
32
|
+
* nothing changed doubled the cost of a call. Only `syncHaltControl` updates
|
|
33
|
+
* `memory`, inside the queue, and it checks again there.
|
|
34
|
+
*/
|
|
35
|
+
export declare function controlUnchanged(stateDir: string, memory: ControlMemory): boolean;
|
|
36
|
+
export declare function syncHaltControl(opts: {
|
|
37
|
+
stateDir: string;
|
|
38
|
+
now: () => number;
|
|
39
|
+
nonce: () => string;
|
|
40
|
+
policyHash: string;
|
|
41
|
+
recordSigner: RecordSigner;
|
|
42
|
+
inputsLog: InputsLog;
|
|
43
|
+
decisions: () => Promise<SignedDecisionRecord[]>;
|
|
44
|
+
memory: ControlMemory;
|
|
45
|
+
writer: LedgerWriter;
|
|
46
|
+
/** Test seam: runs just before the history file is read. */
|
|
47
|
+
beforeHistoryRead?: () => void;
|
|
48
|
+
}): Promise<void>;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { signDecisionRecord } from "@cedulon/core";
|
|
4
|
+
import { sha256Canonical } from "./hash.js";
|
|
5
|
+
/**
|
|
6
|
+
* Same file names as `packages/body/src/halt.ts`. The history file stays plain
|
|
7
|
+
* text; this module copies each line into the ledger as a signed control record.
|
|
8
|
+
* A CLI halt is not in the ledger until the body opens, the HTTP door syncs,
|
|
9
|
+
* or the next call. That delay is real.
|
|
10
|
+
*
|
|
11
|
+
* The body is the only ledger writer, and the ledger lock keeps it that way.
|
|
12
|
+
* After this proxy opens, no other process appends decisions. The first sync
|
|
13
|
+
* reads the ledger once; later syncs trust the snapshot held in the proxy
|
|
14
|
+
* closure and only reread `halt-history.jsonl` when its size or mtime changed
|
|
15
|
+
* or the `halted` switch appeared or disappeared.
|
|
16
|
+
*
|
|
17
|
+
* A switch with no history halt is one `via: "file"` halt. Deleting that
|
|
18
|
+
* switch while the last control record is still a halt, and while history has
|
|
19
|
+
* no resume that closes that window, is one symmetric `via: "file"` resume.
|
|
20
|
+
* The proxy catches a sync throw, leaves this snapshot unloaded, and lets the
|
|
21
|
+
* call continue; halt and rate limits stay fail-closed on their own path.
|
|
22
|
+
*/
|
|
23
|
+
const HALT_SWITCH = "halted";
|
|
24
|
+
const HALT_HISTORY = "halt-history.jsonl";
|
|
25
|
+
const CONTROL_EFFECT = "verax.control";
|
|
26
|
+
export function createControlMemory() {
|
|
27
|
+
return {
|
|
28
|
+
loaded: false,
|
|
29
|
+
recorded: {
|
|
30
|
+
mismatched: false,
|
|
31
|
+
hasMismatch: false,
|
|
32
|
+
seenMismatch: false,
|
|
33
|
+
resumeFrom: 0,
|
|
34
|
+
maxLine: -1,
|
|
35
|
+
maxAfterAck: -1,
|
|
36
|
+
fileHaltCovers: false,
|
|
37
|
+
openHalt: false,
|
|
38
|
+
hashesByLine: new Map(),
|
|
39
|
+
},
|
|
40
|
+
stamp: null,
|
|
41
|
+
haltedPresent: null,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* True when the history file and the switch look as they did at the last sync,
|
|
46
|
+
* so there is nothing to record. Cheap enough to run before taking the ledger
|
|
47
|
+
* queue: waiting a turn behind the previous call's durable writes only to find
|
|
48
|
+
* nothing changed doubled the cost of a call. Only `syncHaltControl` updates
|
|
49
|
+
* `memory`, inside the queue, and it checks again there.
|
|
50
|
+
*/
|
|
51
|
+
export function controlUnchanged(stateDir, memory) {
|
|
52
|
+
return (memory.loaded &&
|
|
53
|
+
sameStamp(memory.stamp, historyStamp(stateDir)) &&
|
|
54
|
+
memory.haltedPresent === existsSync(join(stateDir, HALT_SWITCH)));
|
|
55
|
+
}
|
|
56
|
+
export async function syncHaltControl(opts) {
|
|
57
|
+
const memory = opts.memory;
|
|
58
|
+
if (memory.loaded &&
|
|
59
|
+
sameStamp(memory.stamp, historyStamp(opts.stateDir)) &&
|
|
60
|
+
memory.haltedPresent === existsSync(join(opts.stateDir, HALT_SWITCH))) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Read the history first and the switch after it. haltBody creates the
|
|
64
|
+
// switch before it writes its line, so a halt line read here means the
|
|
65
|
+
// switch already existed; reading the switch first let a halt that landed
|
|
66
|
+
// in between look like a hand-removed switch and wrote a resume while the
|
|
67
|
+
// body was halted. If the history changed while it was read, read again.
|
|
68
|
+
let stamp = historyStamp(opts.stateDir);
|
|
69
|
+
let history = [];
|
|
70
|
+
let haltedPresent = false;
|
|
71
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
72
|
+
opts.beforeHistoryRead?.();
|
|
73
|
+
history = readHistory(opts.stateDir);
|
|
74
|
+
haltedPresent = existsSync(join(opts.stateDir, HALT_SWITCH));
|
|
75
|
+
const after = historyStamp(opts.stateDir);
|
|
76
|
+
if (sameStamp(stamp, after))
|
|
77
|
+
break;
|
|
78
|
+
stamp = after;
|
|
79
|
+
}
|
|
80
|
+
if (!memory.loaded) {
|
|
81
|
+
memory.recorded = await readRecorded(opts, history);
|
|
82
|
+
memory.loaded = true;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
markMismatch(memory.recorded, history);
|
|
86
|
+
}
|
|
87
|
+
const recorded = memory.recorded;
|
|
88
|
+
try {
|
|
89
|
+
let cursor;
|
|
90
|
+
if (recorded.mismatched && !recorded.hasMismatch) {
|
|
91
|
+
await writeMismatch(opts, history.length);
|
|
92
|
+
recorded.hasMismatch = true;
|
|
93
|
+
recorded.seenMismatch = true;
|
|
94
|
+
recorded.resumeFrom = history.length;
|
|
95
|
+
recorded.maxAfterAck = history.length - 1;
|
|
96
|
+
cursor = history.length;
|
|
97
|
+
}
|
|
98
|
+
else if (recorded.mismatched) {
|
|
99
|
+
cursor = recorded.maxAfterAck + 1;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
cursor = recorded.maxLine + 1;
|
|
103
|
+
}
|
|
104
|
+
let wroteHistory = false;
|
|
105
|
+
for (let i = cursor; i < history.length; i += 1) {
|
|
106
|
+
const raw = history[i];
|
|
107
|
+
const lineHash = sha256Canonical(raw);
|
|
108
|
+
const known = recorded.hashesByLine.get(i);
|
|
109
|
+
if (known?.includes(lineHash))
|
|
110
|
+
continue;
|
|
111
|
+
const event = parseHistoryLine(raw, i);
|
|
112
|
+
if (!event)
|
|
113
|
+
continue;
|
|
114
|
+
await writeControlAllow(opts, event, lineHash);
|
|
115
|
+
wroteHistory = true;
|
|
116
|
+
noteHistoryAllow(recorded, i, lineHash);
|
|
117
|
+
recorded.openHalt = event.action === "halt";
|
|
118
|
+
}
|
|
119
|
+
const needFile = haltedPresent && lastAction(history) !== "halt";
|
|
120
|
+
if (needFile && (wroteHistory || !recorded.fileHaltCovers)) {
|
|
121
|
+
await writeControlAllow(opts, { action: "halt", atMs: opts.now(), by: "unknown", via: "file", line: -1 }, sha256Canonical({ via: "file", line: -1 }));
|
|
122
|
+
recorded.fileHaltCovers = true;
|
|
123
|
+
recorded.openHalt = true;
|
|
124
|
+
}
|
|
125
|
+
// The switch is gone and the last control record is still a halt. A
|
|
126
|
+
// history resume that closed this window has already cleared `openHalt`.
|
|
127
|
+
// One file resume, symmetric with the file halt, and not repeated while
|
|
128
|
+
// the window stays closed.
|
|
129
|
+
if (!haltedPresent && recorded.openHalt) {
|
|
130
|
+
await writeControlAllow(opts, { action: "resume", atMs: opts.now(), by: "unknown", via: "file", line: -1 }, sha256Canonical({ via: "file", line: -1 }));
|
|
131
|
+
recorded.fileHaltCovers = false;
|
|
132
|
+
recorded.openHalt = false;
|
|
133
|
+
}
|
|
134
|
+
memory.stamp = stamp;
|
|
135
|
+
memory.haltedPresent = haltedPresent;
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
// A partial append is already on disk. Drop the snapshot so the next sync
|
|
139
|
+
// reads the ledger once and does not write that line again.
|
|
140
|
+
memory.loaded = false;
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function historyStamp(stateDir) {
|
|
145
|
+
try {
|
|
146
|
+
const st = statSync(join(stateDir, HALT_HISTORY));
|
|
147
|
+
return { size: st.size, mtimeMs: st.mtimeMs };
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
if (err.code === "ENOENT")
|
|
151
|
+
return null;
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function sameStamp(a, b) {
|
|
156
|
+
if (a === null || b === null)
|
|
157
|
+
return a === b;
|
|
158
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs;
|
|
159
|
+
}
|
|
160
|
+
function noteHistoryAllow(recorded, line, lineHash) {
|
|
161
|
+
recorded.fileHaltCovers = false;
|
|
162
|
+
const bag = recorded.hashesByLine.get(line) ?? [];
|
|
163
|
+
bag.push(lineHash);
|
|
164
|
+
recorded.hashesByLine.set(line, bag);
|
|
165
|
+
if (line > recorded.maxLine)
|
|
166
|
+
recorded.maxLine = line;
|
|
167
|
+
if (recorded.seenMismatch && line >= recorded.resumeFrom && line > recorded.maxAfterAck) {
|
|
168
|
+
recorded.maxAfterAck = line;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function markMismatch(recorded, history) {
|
|
172
|
+
let mismatched = recorded.maxLine > history.length;
|
|
173
|
+
for (const [line, hashes] of recorded.hashesByLine) {
|
|
174
|
+
if (line >= history.length) {
|
|
175
|
+
mismatched = true;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const nowHash = sha256Canonical(history[line]);
|
|
179
|
+
if (hashes.some((hash) => hash !== nowHash))
|
|
180
|
+
mismatched = true;
|
|
181
|
+
}
|
|
182
|
+
recorded.mismatched = mismatched;
|
|
183
|
+
}
|
|
184
|
+
function readHistory(stateDir) {
|
|
185
|
+
let text;
|
|
186
|
+
try {
|
|
187
|
+
text = readFileSync(join(stateDir, HALT_HISTORY), "utf8");
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (err.code === "ENOENT")
|
|
191
|
+
return [];
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
return text
|
|
195
|
+
.split("\n")
|
|
196
|
+
.map((line) => line.replace(/\r$/, ""))
|
|
197
|
+
.filter((line) => line.trim() !== "");
|
|
198
|
+
}
|
|
199
|
+
function parseHistoryLine(raw, line) {
|
|
200
|
+
let row;
|
|
201
|
+
try {
|
|
202
|
+
row = JSON.parse(raw);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
if (row.action !== "halt" && row.action !== "resume")
|
|
208
|
+
return null;
|
|
209
|
+
// A line that names no finite time or an implausible actor is not copied into a signed record.
|
|
210
|
+
if (typeof row.atMs !== "number" || !Number.isFinite(row.atMs))
|
|
211
|
+
return null;
|
|
212
|
+
if (typeof row.by !== "string" || row.by.length === 0 || row.by.length > 256)
|
|
213
|
+
return null;
|
|
214
|
+
const via = row.via === "http" ? "http" : row.via === "file" ? "file" : "cli";
|
|
215
|
+
return { action: row.action, atMs: row.atMs, by: row.by, via, line };
|
|
216
|
+
}
|
|
217
|
+
function lastAction(lines) {
|
|
218
|
+
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
219
|
+
const event = parseHistoryLine(lines[i], i);
|
|
220
|
+
if (event)
|
|
221
|
+
return event.action;
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
async function readRecorded(opts, history) {
|
|
226
|
+
const hashesByLine = new Map();
|
|
227
|
+
let maxLine = -1;
|
|
228
|
+
let hasMismatch = false;
|
|
229
|
+
let fileHaltCovers = false;
|
|
230
|
+
let openHalt = false;
|
|
231
|
+
let seenMismatch = false;
|
|
232
|
+
let resumeFrom = 0;
|
|
233
|
+
let maxAfterAck = -1;
|
|
234
|
+
for (const decision of await opts.decisions()) {
|
|
235
|
+
const reason = decision.claims.reasonCode;
|
|
236
|
+
const subject = decision.claims.subject;
|
|
237
|
+
const ref = decision.claims.ref;
|
|
238
|
+
if (reason !== "halt-history-mismatch" && subject !== "verax.halt" && subject !== "verax.resume") {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (typeof ref !== "string")
|
|
242
|
+
continue;
|
|
243
|
+
const inputs = await opts.inputsLog.get(ref);
|
|
244
|
+
const control = inputs?.control;
|
|
245
|
+
if (reason === "halt-history-mismatch") {
|
|
246
|
+
hasMismatch = true;
|
|
247
|
+
seenMismatch = true;
|
|
248
|
+
resumeFrom = typeof control?.line === "number" ? control.line : 0;
|
|
249
|
+
maxAfterAck = resumeFrom - 1;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (decision.claims.decision !== "allow" || !control)
|
|
253
|
+
continue;
|
|
254
|
+
if (control.line === -1 && control.via === "file" && (subject === "verax.halt" || subject === "verax.resume")) {
|
|
255
|
+
const halt = subject === "verax.halt";
|
|
256
|
+
fileHaltCovers = halt;
|
|
257
|
+
openHalt = halt;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (typeof control.line !== "number" || control.line < 0)
|
|
261
|
+
continue;
|
|
262
|
+
if (subject !== "verax.halt" && subject !== "verax.resume")
|
|
263
|
+
continue;
|
|
264
|
+
fileHaltCovers = false;
|
|
265
|
+
openHalt = subject === "verax.halt";
|
|
266
|
+
const bag = hashesByLine.get(control.line) ?? [];
|
|
267
|
+
if (typeof control.lineHash === "string")
|
|
268
|
+
bag.push(control.lineHash);
|
|
269
|
+
hashesByLine.set(control.line, bag);
|
|
270
|
+
if (control.line > maxLine)
|
|
271
|
+
maxLine = control.line;
|
|
272
|
+
if (seenMismatch && control.line >= resumeFrom && control.line > maxAfterAck) {
|
|
273
|
+
maxAfterAck = control.line;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const recorded = {
|
|
277
|
+
mismatched: false,
|
|
278
|
+
hasMismatch,
|
|
279
|
+
seenMismatch,
|
|
280
|
+
resumeFrom,
|
|
281
|
+
maxLine,
|
|
282
|
+
maxAfterAck,
|
|
283
|
+
fileHaltCovers,
|
|
284
|
+
openHalt,
|
|
285
|
+
hashesByLine,
|
|
286
|
+
};
|
|
287
|
+
markMismatch(recorded, history);
|
|
288
|
+
return recorded;
|
|
289
|
+
}
|
|
290
|
+
async function writeMismatch(opts, resumeFrom) {
|
|
291
|
+
const atMs = opts.now();
|
|
292
|
+
const control = {
|
|
293
|
+
action: "halt",
|
|
294
|
+
by: "unknown",
|
|
295
|
+
via: "file",
|
|
296
|
+
atMs,
|
|
297
|
+
line: resumeFrom,
|
|
298
|
+
lineHash: sha256Canonical("halt-history-mismatch"),
|
|
299
|
+
};
|
|
300
|
+
await writeDecision(opts, {
|
|
301
|
+
decision: "deny",
|
|
302
|
+
subject: CONTROL_EFFECT,
|
|
303
|
+
reasonCode: "halt-history-mismatch",
|
|
304
|
+
effectClass: CONTROL_EFFECT,
|
|
305
|
+
effectHash: null,
|
|
306
|
+
requestHash: sha256Canonical({ mismatch: "halt-history", resumeFrom }),
|
|
307
|
+
timestampMs: atMs,
|
|
308
|
+
control,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
async function writeControlAllow(opts, event, lineHash) {
|
|
312
|
+
const body = {
|
|
313
|
+
action: event.action,
|
|
314
|
+
atMs: event.atMs,
|
|
315
|
+
by: event.by,
|
|
316
|
+
via: event.via,
|
|
317
|
+
line: event.line,
|
|
318
|
+
};
|
|
319
|
+
const bound = sha256Canonical(body);
|
|
320
|
+
const ref = await writeDecision(opts, {
|
|
321
|
+
decision: "allow",
|
|
322
|
+
subject: event.action === "halt" ? "verax.halt" : "verax.resume",
|
|
323
|
+
reasonCode: event.action === "halt" ? "operator-halt" : "operator-resume",
|
|
324
|
+
effectClass: CONTROL_EFFECT,
|
|
325
|
+
effectHash: bound,
|
|
326
|
+
requestHash: bound,
|
|
327
|
+
timestampMs: event.atMs,
|
|
328
|
+
control: { ...body, lineHash },
|
|
329
|
+
});
|
|
330
|
+
const row = {
|
|
331
|
+
ref,
|
|
332
|
+
effectHash: bound,
|
|
333
|
+
effectClass: CONTROL_EFFECT,
|
|
334
|
+
timestampMs: event.atMs,
|
|
335
|
+
actor: event.by,
|
|
336
|
+
};
|
|
337
|
+
await opts.writer.appendEffect(row, "self", bound);
|
|
338
|
+
}
|
|
339
|
+
async function writeDecision(opts, row) {
|
|
340
|
+
const ref = opts.nonce();
|
|
341
|
+
const inputs = {
|
|
342
|
+
principal: { brain: "verax-proxy", scopes: [] },
|
|
343
|
+
inputs: [],
|
|
344
|
+
control: row.control,
|
|
345
|
+
};
|
|
346
|
+
const inputsHash = sha256Canonical(inputs);
|
|
347
|
+
await opts.inputsLog.append(ref, inputs);
|
|
348
|
+
await opts.writer.appendDecisionChained((prevRecordHash) => signDecisionRecord({
|
|
349
|
+
decider: "verax-proxy",
|
|
350
|
+
subject: row.subject,
|
|
351
|
+
requestHash: row.requestHash,
|
|
352
|
+
policyHash: opts.policyHash,
|
|
353
|
+
inputsHash,
|
|
354
|
+
decision: row.decision,
|
|
355
|
+
reasonCode: row.reasonCode,
|
|
356
|
+
ref,
|
|
357
|
+
effectHash: row.effectHash,
|
|
358
|
+
effectClass: row.effectClass,
|
|
359
|
+
timestampMs: row.timestampMs,
|
|
360
|
+
nonce: ref,
|
|
361
|
+
prevRecordHash,
|
|
362
|
+
}, opts.recordSigner.privateKeyPem, opts.recordSigner.publicKeyPem));
|
|
363
|
+
return ref;
|
|
364
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,12 +7,17 @@ export { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueAppr
|
|
|
7
7
|
export { loadDecisionRefsFromDir, loadEffectsFromDir, parseCardCsv, parseChannelJsonl, reconcile } from "./reconcile.ts";
|
|
8
8
|
export { tenantKey } from "./tenant.ts";
|
|
9
9
|
export { diskProbe } from "./disk.ts";
|
|
10
|
-
export { checkpointsPath } from "./checkpoints.ts";
|
|
10
|
+
export { checkpointsPath, readCheckpointFile } from "./checkpoints.ts";
|
|
11
11
|
export { signEffectAttestation } from "./ledger.ts";
|
|
12
12
|
export { spokenReason } from "./spoken-reason.ts";
|
|
13
13
|
export type { ApprovalRow, ApproveResult } from "./approvals.ts";
|
|
14
14
|
export type { CardCsvOpts, ChannelRow, ReconcileReport } from "./reconcile.ts";
|
|
15
15
|
export type { FileLedgerOpts, LedgerCounts } from "./ledger.ts";
|
|
16
|
+
export type { ApprovalSignature } from "./types.ts";
|
|
16
17
|
export type { EffectSigner, ExplainChain, ExplainPair, ExplainFinding, ExplainOpts, ExplainResult, ExplainTrustRoot, ExplainWarning, ExtractWindow, Ledger, LedgerEffect, Policy, PolicyDecision, Principal, ProxyDeps, RecordSigner, ToolCall, ToolResult, WitnessClass, } from "./types.ts";
|
|
17
18
|
export { verifyLedger } from "./verify-ledger.ts";
|
|
18
|
-
export type { VerifyResult, VerifyOptions, VerifyTrust } from "./verify-ledger.ts";
|
|
19
|
+
export type { VerifyResult, VerifyOptions, VerifyTrust, VerifyControl, VerifyInputs, VerifyAnchors, } from "./verify-ledger.ts";
|
|
20
|
+
export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.ts";
|
|
21
|
+
export type { AnchorRoute, AnchorRow } from "./anchors.ts";
|
|
22
|
+
export { verifyReceipt } from "./transparency-receipt.ts";
|
|
23
|
+
export type { ReceiptCheck, ReceiptStage } from "./transparency-receipt.ts";
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,9 @@ export { tenantKey } from "./tenant.js";
|
|
|
13
13
|
// through this package must be able to inject a full disk into the very
|
|
14
14
|
// instance the proxy uses. Deep imports are not in the exports map.
|
|
15
15
|
export { diskProbe } from "./disk.js";
|
|
16
|
-
export { checkpointsPath } from "./checkpoints.js";
|
|
16
|
+
export { checkpointsPath, readCheckpointFile } from "./checkpoints.js";
|
|
17
17
|
export { signEffectAttestation } from "./ledger.js";
|
|
18
18
|
export { spokenReason } from "./spoken-reason.js";
|
|
19
19
|
export { verifyLedger } from "./verify-ledger.js";
|
|
20
|
+
export { anchorEntryHash, appendAnchor, readAnchors } from "./anchors.js";
|
|
21
|
+
export { verifyReceipt } from "./transparency-receipt.js";
|
package/dist/ledger.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ import { type TailVisit } from "./jsonl-tail.ts";
|
|
|
11
11
|
import type { DecisionInputs, EffectSigner, ExtractWindow, Ledger, LedgerEffect, WitnessClass } from "./types.ts";
|
|
12
12
|
import type { DecisionKind, SignedDecisionRecord } from "@cedulon/core";
|
|
13
13
|
export type PermissionCheck = "owner-only" | "not checked on this platform";
|
|
14
|
+
/** Appends that already hold the ledger queue. Calling the public methods from here deadlocks. */
|
|
15
|
+
export type LedgerWriter = {
|
|
16
|
+
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
17
|
+
appendEffect(row: EffectRow, witnessClass: WitnessClass, resultHash?: string): Promise<void>;
|
|
18
|
+
};
|
|
14
19
|
/**
|
|
15
20
|
* A ledger file read from its end through the same seam the reads above use,
|
|
16
21
|
* so a test can count what a window costs.
|
|
@@ -63,6 +68,9 @@ export declare class MemoryLedger implements Ledger {
|
|
|
63
68
|
hasPrimaryEffect(ref: string): boolean;
|
|
64
69
|
appendDecision(signed: SignedDecisionRecord): Promise<void>;
|
|
65
70
|
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
71
|
+
private appendDecisionChainedUnlocked;
|
|
72
|
+
/** One turn of this ledger's queue: read a cursor and append without a second writer seeing the same cursor. */
|
|
73
|
+
exclusive<T>(fn: (writer: LedgerWriter) => Promise<T>): Promise<T>;
|
|
66
74
|
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string): Promise<void>;
|
|
67
75
|
private appendEffectUnlocked;
|
|
68
76
|
decisions(): Promise<SignedDecisionRecord[]>;
|
|
@@ -163,6 +171,9 @@ export declare class FileLedger implements Ledger {
|
|
|
163
171
|
close(): void;
|
|
164
172
|
appendDecision(signed: SignedDecisionRecord): Promise<void>;
|
|
165
173
|
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
174
|
+
private appendDecisionChainedUnlocked;
|
|
175
|
+
/** One turn of this ledger's queue: read a cursor and append without a second writer seeing the same cursor. */
|
|
176
|
+
exclusive<T>(fn: (writer: LedgerWriter) => Promise<T>): Promise<T>;
|
|
166
177
|
/**
|
|
167
178
|
* What /healthz reports, from memory: the counts are kept on load and on
|
|
168
179
|
* every append. Rereading both files to count lines cost a second per call
|