@verax-ai/proxy 0.7.0 → 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/approvals.d.ts +30 -1
- package/dist/approvals.js +69 -13
- package/dist/data-classes.d.ts +36 -0
- package/dist/data-classes.js +412 -0
- package/dist/explain.js +63 -13
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/inputs.d.ts +4 -0
- package/dist/inputs.js +41 -0
- package/dist/ledger-manifest.d.ts +2 -0
- package/dist/ledger-manifest.js +1 -0
- package/dist/ledger.d.ts +2 -0
- package/dist/ledger.js +4 -0
- package/dist/policy.d.ts +12 -2
- package/dist/policy.js +208 -7
- package/dist/proxy.js +149 -20
- package/dist/reconcile.d.ts +10 -1
- package/dist/reconcile.js +79 -14
- package/dist/result-classes.d.ts +35 -0
- package/dist/result-classes.js +485 -0
- package/dist/types.d.ts +59 -5
- package/dist/types.js +6 -1
- package/dist/verify-ledger.d.ts +7 -0
- package/dist/verify-ledger.js +127 -2
- package/package.json +3 -2
package/dist/proxy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isProxyHalted } from "./process-halt.js";
|
|
2
2
|
import { signDecisionRecord } from "@cedulon/core";
|
|
3
|
-
import { approvePending, approvalsLogFor, createApprovalBudgetGuard, drainApprovalCommands, spentTodayMinorOf, } from "./approvals.js";
|
|
3
|
+
import { approvalDispatchArgs, approvalDispatchHash, approvePending, approvalsLogFor, createApprovalBudgetGuard, drainApprovalCommands, signedApprovalDispatch, spentTodayMinorOf, } from "./approvals.js";
|
|
4
4
|
import { SerialQueue } from "./serial-queue.js";
|
|
5
5
|
import { diskProbe } from "./disk.js";
|
|
6
6
|
import { markEnded, markStarted, startedWithoutEnd } from "./in-flight-log.js";
|
|
@@ -10,9 +10,12 @@ import { controlUnchanged, createControlMemory, syncHaltControl } from "./contro
|
|
|
10
10
|
import { createOperatorRemovalMemory, operatorRemovalUnchanged, syncOperatorRemovalControl } from "./operator-removal-sync.js";
|
|
11
11
|
import { createRevokeMemory, revokeUnchanged, syncRevokeControl } from "./revoke-sync.js";
|
|
12
12
|
import { countedWork, hasPrimaryEffect, lookupDecisionByRef, lookupReauthByHash, lookupResolvedBy, noteResolution, noteTenantRef, } from "./ledger.js";
|
|
13
|
+
import { argumentDepth, gateDataClasses, MAX_ARGUMENT_DEPTH, scanCallMeta } from "./data-classes.js";
|
|
14
|
+
import { gateResultClasses, gateThrownError } from "./result-classes.js";
|
|
13
15
|
import { terminalText } from "./policy.js";
|
|
14
16
|
import { spokenReason } from "./spoken-reason.js";
|
|
15
17
|
import { tenantKey } from "./tenant.js";
|
|
18
|
+
import { isTextContent } from "./types.js";
|
|
16
19
|
function scopedClaimsRef(principal, raw) {
|
|
17
20
|
if (principal.iss || principal.tenant || principal.org) {
|
|
18
21
|
return `${tenantKey(principal)}:${raw}`;
|
|
@@ -84,7 +87,7 @@ function effectUnrecorded(ref) {
|
|
|
84
87
|
};
|
|
85
88
|
}
|
|
86
89
|
function isEffectUnrecorded(result) {
|
|
87
|
-
return result.content.some((part) => part.text.startsWith("effect-unrecorded:"));
|
|
90
|
+
return result.content.some((part) => isTextContent(part) && part.text.startsWith("effect-unrecorded:"));
|
|
88
91
|
}
|
|
89
92
|
async function replayAfterEffect(ledger, ref) {
|
|
90
93
|
const primary = (await ledger.effects()).find((e) => e.row.ref === ref && e.row.effectClass !== "duplicate-effect");
|
|
@@ -180,6 +183,17 @@ function toolCallOf(call) {
|
|
|
180
183
|
delete args._ref;
|
|
181
184
|
return { name: call.name, arguments: args };
|
|
182
185
|
}
|
|
186
|
+
function inputsWithoutSource(inputs) {
|
|
187
|
+
return {
|
|
188
|
+
...inputs,
|
|
189
|
+
inputs: inputs.inputs.map((row) => ({
|
|
190
|
+
id: row.id,
|
|
191
|
+
versionHash: row.versionHash,
|
|
192
|
+
validFromMs: row.validFromMs,
|
|
193
|
+
validUntilMs: row.validUntilMs,
|
|
194
|
+
})),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
183
197
|
export function createProxy(deps) {
|
|
184
198
|
const inputsLog = deps.inputsLog ?? inputsLogFor(deps.ledger);
|
|
185
199
|
const approvals = approvalsLogFor(deps.ledger);
|
|
@@ -318,6 +332,7 @@ export function createProxy(deps) {
|
|
|
318
332
|
result = await deps.inner(frozenCall, principal, ref, bound);
|
|
319
333
|
}
|
|
320
334
|
catch (err) {
|
|
335
|
+
const resultHash = sha256Canonical(thrownPayload(err));
|
|
321
336
|
const row = {
|
|
322
337
|
ref,
|
|
323
338
|
effectHash: thrownHash,
|
|
@@ -326,13 +341,22 @@ export function createProxy(deps) {
|
|
|
326
341
|
actor: principal.brain,
|
|
327
342
|
};
|
|
328
343
|
try {
|
|
329
|
-
await deps.ledger.appendEffect(row, "self",
|
|
344
|
+
await deps.ledger.appendEffect(row, "self", resultHash);
|
|
330
345
|
effectRecorded.ok = true;
|
|
331
346
|
}
|
|
332
347
|
catch (writeErr) {
|
|
333
348
|
throw writeErr;
|
|
334
349
|
}
|
|
335
|
-
|
|
350
|
+
// Without result classes the tool's own error leaves, untouched and not awaited.
|
|
351
|
+
if (!deps.policy.resultClasses && !deps.policy.media)
|
|
352
|
+
throw err;
|
|
353
|
+
const gated = await gateThrownError(err, dispatchedName, ref, resultHash, principal, {
|
|
354
|
+
policy: deps.policy,
|
|
355
|
+
nonce: deps.nonce,
|
|
356
|
+
now: deps.now,
|
|
357
|
+
writeRecord,
|
|
358
|
+
});
|
|
359
|
+
throw gated.error;
|
|
336
360
|
}
|
|
337
361
|
const row = {
|
|
338
362
|
ref,
|
|
@@ -342,14 +366,15 @@ export function createProxy(deps) {
|
|
|
342
366
|
actor: principal.brain,
|
|
343
367
|
};
|
|
344
368
|
const { effectReceipt, ...normalResult } = result;
|
|
369
|
+
const resultHash = sha256Canonical(normalResult);
|
|
345
370
|
const signedRow = effectReceipt?.body?.effects?.[0];
|
|
346
371
|
const external = effectReceipt?.body?.effects?.length === 1 && signedRow?.ref === ref &&
|
|
347
372
|
signedRow.effectHash === dispatchedHash && signedRow.effectClass === dispatchedName;
|
|
348
373
|
try {
|
|
349
374
|
if (external)
|
|
350
|
-
await deps.ledger.appendEffect(signedRow, "third-party",
|
|
375
|
+
await deps.ledger.appendEffect(signedRow, "third-party", resultHash, effectReceipt);
|
|
351
376
|
else
|
|
352
|
-
await deps.ledger.appendEffect(row, "self",
|
|
377
|
+
await deps.ledger.appendEffect(row, "self", resultHash);
|
|
353
378
|
effectRecorded.ok = true;
|
|
354
379
|
}
|
|
355
380
|
catch {
|
|
@@ -358,7 +383,12 @@ export function createProxy(deps) {
|
|
|
358
383
|
// stays false) and a restart answers outcome-unknown instead of running again.
|
|
359
384
|
return effectUnrecorded(ref);
|
|
360
385
|
}
|
|
361
|
-
return normalResult
|
|
386
|
+
return gateResultClasses(normalResult, dispatchedName, ref, resultHash, principal, {
|
|
387
|
+
policy: deps.policy,
|
|
388
|
+
nonce: deps.nonce,
|
|
389
|
+
now: deps.now,
|
|
390
|
+
writeRecord,
|
|
391
|
+
});
|
|
362
392
|
}
|
|
363
393
|
async function resolveInputs(call, principal, timestampMs) {
|
|
364
394
|
const declared = declaredInputs(call.arguments);
|
|
@@ -571,9 +601,22 @@ export function createProxy(deps) {
|
|
|
571
601
|
await syncControlRecords();
|
|
572
602
|
}
|
|
573
603
|
const dispatched = toolCallOf(call);
|
|
574
|
-
const
|
|
604
|
+
const tooDeep = deps.policy.dataClasses !== undefined && argumentDepth(call.arguments) > MAX_ARGUMENT_DEPTH;
|
|
605
|
+
const requestHash = tooDeep
|
|
606
|
+
? sha256Canonical({ name: dispatched.name, tooDeep: true })
|
|
607
|
+
: sha256Canonical(dispatched);
|
|
575
608
|
const timestampMs = deps.now();
|
|
576
609
|
const resolved = await resolveInputs(call, principal, timestampMs);
|
|
610
|
+
// A class in `_inputs` must not reach disk on the halt or disk-low
|
|
611
|
+
// record. Scan here only to scrub; the deny reason stays 0.7.0's.
|
|
612
|
+
const metaReason = deps.policy.dataClasses
|
|
613
|
+
? scanCallMeta(call, deps.policy.dataClasses)
|
|
614
|
+
: null;
|
|
615
|
+
const preMetaInputs = metaReason
|
|
616
|
+
? { principal: principalInputs(principal), inputs: [] }
|
|
617
|
+
: deps.policy.dataClasses
|
|
618
|
+
? inputsWithoutSource(resolved.inputs)
|
|
619
|
+
: resolved.inputs;
|
|
577
620
|
async function denyIfHalted() {
|
|
578
621
|
if (!stateDir || !isProxyHalted(stateDir))
|
|
579
622
|
return null;
|
|
@@ -583,7 +626,7 @@ export function createProxy(deps) {
|
|
|
583
626
|
reasonCode: "halted",
|
|
584
627
|
ref,
|
|
585
628
|
requestHash,
|
|
586
|
-
inputs:
|
|
629
|
+
inputs: preMetaInputs,
|
|
587
630
|
effectHash: null,
|
|
588
631
|
subject: call.name,
|
|
589
632
|
timestampMs,
|
|
@@ -602,7 +645,7 @@ export function createProxy(deps) {
|
|
|
602
645
|
reasonCode: "ledger-disk-low",
|
|
603
646
|
ref,
|
|
604
647
|
requestHash,
|
|
605
|
-
inputs:
|
|
648
|
+
inputs: preMetaInputs,
|
|
606
649
|
effectHash: null,
|
|
607
650
|
subject: call.name,
|
|
608
651
|
timestampMs,
|
|
@@ -620,6 +663,38 @@ export function createProxy(deps) {
|
|
|
620
663
|
if (halted)
|
|
621
664
|
return { kind: "done", result: halted };
|
|
622
665
|
}
|
|
666
|
+
if (tooDeep) {
|
|
667
|
+
const reasonCode = "data-class-unscanned";
|
|
668
|
+
const ref = deps.nonce();
|
|
669
|
+
await writeRecord({
|
|
670
|
+
decision: "deny",
|
|
671
|
+
reasonCode,
|
|
672
|
+
ref,
|
|
673
|
+
requestHash,
|
|
674
|
+
inputs: { principal: principalInputs(principal), inputs: [] },
|
|
675
|
+
effectHash: null,
|
|
676
|
+
subject: call.name,
|
|
677
|
+
timestampMs,
|
|
678
|
+
});
|
|
679
|
+
return { kind: "done", result: denied(reasonCode, ref) };
|
|
680
|
+
}
|
|
681
|
+
if (deps.policy.dataClasses) {
|
|
682
|
+
const metaReason = scanCallMeta(call, deps.policy.dataClasses);
|
|
683
|
+
if (metaReason) {
|
|
684
|
+
const ref = deps.nonce();
|
|
685
|
+
await writeRecord({
|
|
686
|
+
decision: "deny",
|
|
687
|
+
reasonCode: metaReason,
|
|
688
|
+
ref,
|
|
689
|
+
requestHash,
|
|
690
|
+
inputs: { principal: principalInputs(principal), inputs: [] },
|
|
691
|
+
effectHash: null,
|
|
692
|
+
subject: call.name,
|
|
693
|
+
timestampMs,
|
|
694
|
+
});
|
|
695
|
+
return { kind: "done", result: denied(metaReason, ref) };
|
|
696
|
+
}
|
|
697
|
+
}
|
|
623
698
|
if (given === "invalid") {
|
|
624
699
|
const ref = deps.nonce();
|
|
625
700
|
await writeRecord({
|
|
@@ -772,13 +847,46 @@ export function createProxy(deps) {
|
|
|
772
847
|
});
|
|
773
848
|
return { kind: "done", result: denied("tenant-mismatch", denyRef) };
|
|
774
849
|
}
|
|
850
|
+
const toRun = snap
|
|
851
|
+
? { name: dispatched.name, arguments: approvalDispatchArgs(snap) }
|
|
852
|
+
: dispatched;
|
|
853
|
+
// What is sent is what the signed defer committed to, with or
|
|
854
|
+
// without an approvals row: a missing row does not send the raw call.
|
|
855
|
+
const signedDispatch = await signedApprovalDispatch(deps.ledger, inputsLog, scopedRef, existing);
|
|
856
|
+
if (signedDispatch === null ||
|
|
857
|
+
sha256Canonical({ name: toRun.name, arguments: toRun.arguments }) !== signedDispatch.hash) {
|
|
858
|
+
return {
|
|
859
|
+
kind: "done",
|
|
860
|
+
result: await signedRetryDeny("hash-mismatch", requestHash, resolved.inputs, timestampMs, call.name, scopedRef),
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
if (deps.policy.dataClasses) {
|
|
864
|
+
const classGated = gateDataClasses(dispatched, deps.policy.dataClasses);
|
|
865
|
+
if (classGated.action === "deny") {
|
|
866
|
+
return {
|
|
867
|
+
kind: "done",
|
|
868
|
+
result: await signedRetryDeny(classGated.reasonCode, requestHash, resolved.inputs, timestampMs, call.name, scopedRef),
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
if (classGated.action === "forward") {
|
|
872
|
+
const snapHash = snap
|
|
873
|
+
? approvalDispatchHash(snap)
|
|
874
|
+
: sha256Canonical({ name: dispatched.name, arguments: dispatched.arguments });
|
|
875
|
+
if (snapHash !== classGated.dataClasses.forwardedHash) {
|
|
876
|
+
return {
|
|
877
|
+
kind: "done",
|
|
878
|
+
result: await signedRetryDeny("data-class-changed", requestHash, resolved.inputs, timestampMs, call.name, scopedRef),
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
775
883
|
if (stateDir && isProxyHalted(stateDir)) {
|
|
776
884
|
return {
|
|
777
885
|
kind: "done",
|
|
778
|
-
result: await recordHaltedAfterAllow(allow.ref,
|
|
886
|
+
result: await recordHaltedAfterAllow(allow.ref, toRun, principal),
|
|
779
887
|
};
|
|
780
888
|
}
|
|
781
|
-
return launchInner(
|
|
889
|
+
return launchInner(toRun, principal, allow.ref, scopedRef, boundRows);
|
|
782
890
|
}
|
|
783
891
|
return { kind: "done", result: deferred(given) };
|
|
784
892
|
}
|
|
@@ -842,6 +950,25 @@ export function createProxy(deps) {
|
|
|
842
950
|
decision = "deny";
|
|
843
951
|
reasonCode = "tenant-mismatch";
|
|
844
952
|
}
|
|
953
|
+
let forwarded = dispatched;
|
|
954
|
+
let recordInputs = resolved.inputs;
|
|
955
|
+
let snapshotForwarded;
|
|
956
|
+
let snapshotDataClasses;
|
|
957
|
+
if (decision === "allow" || decision === "defer") {
|
|
958
|
+
const classGated = gateDataClasses(dispatched, deps.policy.dataClasses);
|
|
959
|
+
if (classGated.action === "deny") {
|
|
960
|
+
decision = "deny";
|
|
961
|
+
reasonCode = classGated.reasonCode;
|
|
962
|
+
}
|
|
963
|
+
else if (classGated.action === "forward") {
|
|
964
|
+
forwarded = { name: dispatched.name, arguments: classGated.arguments };
|
|
965
|
+
recordInputs = { ...resolved.inputs, dataClasses: classGated.dataClasses };
|
|
966
|
+
if (decision === "defer") {
|
|
967
|
+
snapshotForwarded = classGated.arguments;
|
|
968
|
+
snapshotDataClasses = classGated.dataClasses;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
845
972
|
const ref = typeof given === "string" ? scopedClaimsRef(principal, given) : deps.nonce();
|
|
846
973
|
// What the brain is told: its own `_ref`, so a retry carries the same key back.
|
|
847
974
|
const shown = typeof given === "string" ? given : ref;
|
|
@@ -851,8 +978,8 @@ export function createProxy(deps) {
|
|
|
851
978
|
reasonCode,
|
|
852
979
|
ref,
|
|
853
980
|
requestHash,
|
|
854
|
-
inputs:
|
|
855
|
-
effectHash: allow ? sha256Canonical(effectDescriptor(
|
|
981
|
+
inputs: recordInputs,
|
|
982
|
+
effectHash: allow ? sha256Canonical(effectDescriptor(forwarded.name, forwarded.arguments)) : null,
|
|
856
983
|
subject: call.name,
|
|
857
984
|
timestampMs,
|
|
858
985
|
});
|
|
@@ -861,14 +988,16 @@ export function createProxy(deps) {
|
|
|
861
988
|
}
|
|
862
989
|
if (decision === "defer") {
|
|
863
990
|
const rule = deps.policy.rule(verdict.rule);
|
|
864
|
-
const amount =
|
|
865
|
-
const payee =
|
|
866
|
-
const currency =
|
|
991
|
+
const amount = forwarded.name === "spend" ? forwarded.arguments.amountMinor : forwarded.arguments.amount;
|
|
992
|
+
const payee = forwarded.arguments.payee;
|
|
993
|
+
const currency = forwarded.arguments.currency;
|
|
867
994
|
await approvals.append({
|
|
868
995
|
ref,
|
|
869
996
|
requestHash,
|
|
870
997
|
subject: dispatched.name,
|
|
871
|
-
args:
|
|
998
|
+
args: forwarded.arguments,
|
|
999
|
+
...(snapshotForwarded !== undefined ? { forwardedArgs: snapshotForwarded } : {}),
|
|
1000
|
+
...(snapshotDataClasses !== undefined ? { dataClasses: snapshotDataClasses } : {}),
|
|
872
1001
|
ruleId: verdict.rule,
|
|
873
1002
|
ruleText: rule?.text ?? null,
|
|
874
1003
|
inputsSummary: {
|
|
@@ -889,9 +1018,9 @@ export function createProxy(deps) {
|
|
|
889
1018
|
return { kind: "done", result: denied(reasonCode, shown, call) };
|
|
890
1019
|
}
|
|
891
1020
|
if (stateDir && isProxyHalted(stateDir)) {
|
|
892
|
-
return { kind: "done", result: await recordHaltedAfterAllow(ref,
|
|
1021
|
+
return { kind: "done", result: await recordHaltedAfterAllow(ref, forwarded, principal) };
|
|
893
1022
|
}
|
|
894
|
-
return launchInner(
|
|
1023
|
+
return launchInner(forwarded, principal, ref, ref, resolved.inputs.inputs);
|
|
895
1024
|
});
|
|
896
1025
|
if (plan.kind === "done")
|
|
897
1026
|
return plan.result;
|
package/dist/reconcile.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EffectRow } from "@cedulon/effect-extract";
|
|
2
|
-
import type
|
|
2
|
+
import { type ApprovalRow } from "./approvals.ts";
|
|
3
3
|
import type { LedgerEffect } from "./types.ts";
|
|
4
4
|
export type ChannelRow = {
|
|
5
5
|
channel: string;
|
|
@@ -67,6 +67,13 @@ export type CardCsvOpts = {
|
|
|
67
67
|
};
|
|
68
68
|
export declare function parseCardCsv(text: string, opts: CardCsvOpts): ParsedCardCsv;
|
|
69
69
|
export declare function parseChannelJsonl(text: string): ChannelRow[];
|
|
70
|
+
/**
|
|
71
|
+
* forwardedHash on the inputs row each decision's inputsHash names, keyed by
|
|
72
|
+
* that decision's ref. Same files as `loadDecisionRequestHashesFromDir`.
|
|
73
|
+
* Null when no inputs row on disk hashes to the decision: a row appended
|
|
74
|
+
* after it, or an edited one, does not stand in for it.
|
|
75
|
+
*/
|
|
76
|
+
export declare function loadSignedForwardedHashesFromDir(stateDir: string): Map<string, string | null>;
|
|
70
77
|
/** Decision refs, read the same way `verifyLedger` lists decision files. */
|
|
71
78
|
export declare function loadDecisionRefsFromDir(stateDir: string): Set<string>;
|
|
72
79
|
/**
|
|
@@ -88,4 +95,6 @@ export declare function reconcile(channelRows: readonly ChannelRow[], effects: r
|
|
|
88
95
|
decisionRefs?: ReadonlySet<string>;
|
|
89
96
|
/** Effect ref to the requestHash on the signed decision of that ref. */
|
|
90
97
|
decisionRequestHashes?: ReadonlyMap<string, string>;
|
|
98
|
+
/** Approval-row ref to the forwardedHash on that defer's signed inputs. */
|
|
99
|
+
decisionForwardedHashes?: ReadonlyMap<string, string | null>;
|
|
91
100
|
}): ReconcileReport;
|
package/dist/reconcile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
import { approvalArgsBound, approvalDispatchArgs } from "./approvals.js";
|
|
4
5
|
import { sha256Canonical } from "./hash.js";
|
|
5
6
|
import { readLedgerManifest, requireLedgerPiecePath } from "./ledger-manifest.js";
|
|
6
7
|
function asChannelRow(raw, line) {
|
|
@@ -243,6 +244,65 @@ function readDecisionLines(stateDir) {
|
|
|
243
244
|
}
|
|
244
245
|
return claims;
|
|
245
246
|
}
|
|
247
|
+
function inputPaths(dir) {
|
|
248
|
+
const manifest = readLedgerManifest(dir);
|
|
249
|
+
if (manifest && Array.isArray(manifest.pieces) && manifest.pieces.length > 0) {
|
|
250
|
+
return manifest.pieces
|
|
251
|
+
.map((p) => requireLedgerPiecePath(dir, p.inputs))
|
|
252
|
+
.filter((p) => existsSync(p));
|
|
253
|
+
}
|
|
254
|
+
const tek = join(dir, "inputs.jsonl");
|
|
255
|
+
return existsSync(tek) ? [tek] : [];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* forwardedHash on the inputs row each decision's inputsHash names, keyed by
|
|
259
|
+
* that decision's ref. Same files as `loadDecisionRequestHashesFromDir`.
|
|
260
|
+
* Null when no inputs row on disk hashes to the decision: a row appended
|
|
261
|
+
* after it, or an edited one, does not stand in for it.
|
|
262
|
+
*/
|
|
263
|
+
export function loadSignedForwardedHashesFromDir(stateDir) {
|
|
264
|
+
const wanted = new Map();
|
|
265
|
+
for (const claims of readDecisionLines(stateDir)) {
|
|
266
|
+
if (typeof claims.ref !== "string" || typeof claims.inputsHash !== "string")
|
|
267
|
+
continue;
|
|
268
|
+
wanted.set(claims.ref, claims.inputsHash);
|
|
269
|
+
}
|
|
270
|
+
const out = new Map();
|
|
271
|
+
if (wanted.size === 0)
|
|
272
|
+
return out;
|
|
273
|
+
const found = new Set();
|
|
274
|
+
for (const path of inputPaths(stateDir)) {
|
|
275
|
+
let text = "";
|
|
276
|
+
try {
|
|
277
|
+
text = readFileSync(path, "utf8");
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
if (err.code === "ENOENT")
|
|
281
|
+
continue;
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
for (const line of text.split("\n")) {
|
|
285
|
+
if (line === "")
|
|
286
|
+
continue;
|
|
287
|
+
const row = JSON.parse(line);
|
|
288
|
+
if (typeof row.ref !== "string" || found.has(row.ref))
|
|
289
|
+
continue;
|
|
290
|
+
const expected = wanted.get(row.ref);
|
|
291
|
+
if (expected === undefined || row.inputs === undefined)
|
|
292
|
+
continue;
|
|
293
|
+
if (sha256Canonical(row.inputs) !== expected)
|
|
294
|
+
continue;
|
|
295
|
+
found.add(row.ref);
|
|
296
|
+
const hash = row.inputs.dataClasses?.forwardedHash;
|
|
297
|
+
if (typeof hash === "string" && hash !== "")
|
|
298
|
+
out.set(row.ref, hash);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
for (const ref of wanted.keys())
|
|
302
|
+
if (!found.has(ref))
|
|
303
|
+
out.set(ref, null);
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
246
306
|
/** Decision refs, read the same way `verifyLedger` lists decision files. */
|
|
247
307
|
export function loadDecisionRefsFromDir(stateDir) {
|
|
248
308
|
const refs = new Set();
|
|
@@ -313,7 +373,7 @@ function approvalForEffect(approvals, effectRef) {
|
|
|
313
373
|
return approvals.find((r) => r.allowRef === effectRef || r.ref === effectRef);
|
|
314
374
|
}
|
|
315
375
|
/** The row may lend amount, currency and payee only when it still hashes to the signed request. */
|
|
316
|
-
function boundApproval(effect, approvals, requestHashes) {
|
|
376
|
+
function boundApproval(effect, approvals, requestHashes, forwardedHashes) {
|
|
317
377
|
if (!requestHashes)
|
|
318
378
|
return undefined;
|
|
319
379
|
const signed = requestHashes.get(effect.row.ref);
|
|
@@ -322,15 +382,18 @@ function boundApproval(effect, approvals, requestHashes) {
|
|
|
322
382
|
const snap = approvalForEffect(approvals, effect.row.ref);
|
|
323
383
|
if (!snap)
|
|
324
384
|
return undefined;
|
|
325
|
-
const
|
|
326
|
-
if (
|
|
385
|
+
const signedForwarded = forwardedHashes?.get(snap.ref);
|
|
386
|
+
if (!approvalArgsBound(snap, signed, signedForwarded))
|
|
327
387
|
return undefined;
|
|
328
388
|
return snap;
|
|
329
389
|
}
|
|
330
|
-
function effectAmount(effect, approvals, requestHashes) {
|
|
331
|
-
const snap = boundApproval(effect, approvals, requestHashes);
|
|
332
|
-
|
|
333
|
-
|
|
390
|
+
function effectAmount(effect, approvals, requestHashes, forwardedHashes) {
|
|
391
|
+
const snap = boundApproval(effect, approvals, requestHashes, forwardedHashes);
|
|
392
|
+
if (!snap)
|
|
393
|
+
return {};
|
|
394
|
+
const args = approvalDispatchArgs(snap);
|
|
395
|
+
const amountMinor = typeof args.amountMinor === "number" ? args.amountMinor : undefined;
|
|
396
|
+
const currency = typeof args.currency === "string" ? args.currency : undefined;
|
|
334
397
|
return { amountMinor, currency };
|
|
335
398
|
}
|
|
336
399
|
function amountsClose(a, b) {
|
|
@@ -353,12 +416,14 @@ function amountGate(row, fromSnap) {
|
|
|
353
416
|
}
|
|
354
417
|
return "match";
|
|
355
418
|
}
|
|
356
|
-
function descriptorsFor(effect, approvals, byPayee, requestHashes) {
|
|
419
|
+
function descriptorsFor(effect, approvals, byPayee, requestHashes, forwardedHashes) {
|
|
357
420
|
if (!byPayee)
|
|
358
421
|
return undefined;
|
|
359
|
-
const snap = boundApproval(effect, approvals, requestHashes);
|
|
360
|
-
|
|
361
|
-
|
|
422
|
+
const snap = boundApproval(effect, approvals, requestHashes, forwardedHashes);
|
|
423
|
+
if (!snap)
|
|
424
|
+
return undefined;
|
|
425
|
+
const payee = approvalDispatchArgs(snap).payee;
|
|
426
|
+
if (typeof payee !== "string")
|
|
362
427
|
return undefined;
|
|
363
428
|
const stamps = byPayee[payee];
|
|
364
429
|
return stamps && stamps.length > 0 ? stamps : undefined;
|
|
@@ -440,7 +505,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
440
505
|
ghost.push(asGhost(marked, effects));
|
|
441
506
|
continue;
|
|
442
507
|
}
|
|
443
|
-
const fromSnap = effectAmount(effects[idx], opts?.approvals, opts?.decisionRequestHashes);
|
|
508
|
+
const fromSnap = effectAmount(effects[idx], opts?.approvals, opts?.decisionRequestHashes, opts?.decisionForwardedHashes);
|
|
444
509
|
const gate = amountGate(row, fromSnap);
|
|
445
510
|
if (gate === "unknown") {
|
|
446
511
|
ghost.push(asGhost(marked, effects, "amount-unknown"));
|
|
@@ -466,7 +531,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
466
531
|
return false;
|
|
467
532
|
if (Math.abs(row.occurredAtMs - e.row.timestampMs) > rowTol)
|
|
468
533
|
return false;
|
|
469
|
-
const stamps = descriptorsFor(e, opts?.approvals, opts?.descriptorsByPayee, opts?.decisionRequestHashes);
|
|
534
|
+
const stamps = descriptorsFor(e, opts?.approvals, opts?.descriptorsByPayee, opts?.decisionRequestHashes, opts?.decisionForwardedHashes);
|
|
470
535
|
if (stamps) {
|
|
471
536
|
const text = row.descriptor ?? "";
|
|
472
537
|
if (!descriptorHits(text, stamps)) {
|
|
@@ -474,7 +539,7 @@ export function reconcile(channelRows, effects, opts) {
|
|
|
474
539
|
return false;
|
|
475
540
|
}
|
|
476
541
|
}
|
|
477
|
-
const fromSnap = effectAmount(e, opts?.approvals, opts?.decisionRequestHashes);
|
|
542
|
+
const fromSnap = effectAmount(e, opts?.approvals, opts?.decisionRequestHashes, opts?.decisionForwardedHashes);
|
|
478
543
|
const gate = amountGate(row, fromSnap);
|
|
479
544
|
if (gate === "unknown") {
|
|
480
545
|
unknownNear = true;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { maskField } from "@conarium-ai/mask";
|
|
2
|
+
import type { DecisionKind } from "@cedulon/core";
|
|
3
|
+
import type { DecisionInputs, Policy, Principal, ToolResult } from "./types.ts";
|
|
4
|
+
export type ResultRecordWriter = (opts: {
|
|
5
|
+
decision: DecisionKind;
|
|
6
|
+
reasonCode: string;
|
|
7
|
+
ref: string;
|
|
8
|
+
requestHash: string;
|
|
9
|
+
inputs: DecisionInputs;
|
|
10
|
+
effectHash: string | null;
|
|
11
|
+
subject: string;
|
|
12
|
+
timestampMs: number;
|
|
13
|
+
}) => Promise<void>;
|
|
14
|
+
export type ResultMasker = typeof maskField;
|
|
15
|
+
export type ResultGateDeps = {
|
|
16
|
+
policy: Policy;
|
|
17
|
+
nonce: () => string;
|
|
18
|
+
now: () => number;
|
|
19
|
+
writeRecord: ResultRecordWriter;
|
|
20
|
+
/** Defaults to Conarium's `maskField`. */
|
|
21
|
+
masker?: ResultMasker;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Mask a tool throw the same way as a text block. The `:threw` row is already
|
|
25
|
+
* written over the original payload. Returns a new object so a string, a
|
|
26
|
+
* frozen Error, a stack that was already read, or a stuck cause cannot
|
|
27
|
+
* carry the raw text out. A throw inside this gate becomes
|
|
28
|
+
* `withheld:result-class-error:<ref>`, never the original. The error comes
|
|
29
|
+
* back wrapped, so awaiting the gate cannot adopt a thenable error. Call it
|
|
30
|
+
* only when the policy sets `resultClasses`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function gateThrownError(err: unknown, tool: string, callRef: string, resultHash: string, principal: Principal, deps: ResultGateDeps): Promise<{
|
|
33
|
+
error: unknown;
|
|
34
|
+
}>;
|
|
35
|
+
export declare function gateResultClasses(result: ToolResult, tool: string, callRef: string, resultHash: string, principal: Principal, deps: ResultGateDeps): Promise<ToolResult>;
|