@sanning/proof 0.6.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/README.md +1 -1
- package/dist/chain.d.ts +99 -0
- package/dist/chain.js +389 -0
- package/dist/cli.js +42 -2
- package/dist/evidence.d.ts +148 -0
- package/dist/evidence.js +603 -70
- package/dist/index.d.ts +4 -2
- package/dist/index.js +22 -1
- package/dist/verifier.js +46 -7
- package/package.json +1 -1
- package/src/chain.ts +500 -0
- package/src/cli.ts +50 -1
- package/src/evidence.ts +813 -74
- package/src/index.ts +46 -0
- package/src/verifier.ts +49 -7
- package/vectors/chain-reading.json +293 -0
package/README.md
CHANGED
|
@@ -203,7 +203,7 @@ The primary signature covers `JCS(envelope minus signature minus co_signatures)`
|
|
|
203
203
|
|
|
204
204
|
## Why you can trust it (without trusting us)
|
|
205
205
|
|
|
206
|
-
Three **independent implementations** of this kernel exist — this package, the Python [`sanning-proof`](https://pypi.org/project/sanning-proof/) (PyPI), and the MIT-licensed Go reference, vendored into this repo under [`cross-kernel/vendor-agent/`](https://github.com/sanning-io/proof/tree/main/cross-kernel/vendor-agent) so anyone can build and run it. The TypeScript and Python kernels are conformance-gated **byte-for-byte** in CI against the shared [`test-vectors` corpus](https://github.com/sanning-io/proof/tree/main/test-vectors) (per-file SHA-256 pins; current cut tag `test-vectors-v3.
|
|
206
|
+
Three **independent implementations** of this kernel exist — this package, the Python [`sanning-proof`](https://pypi.org/project/sanning-proof/) (PyPI), and the MIT-licensed Go reference, vendored into this repo under [`cross-kernel/vendor-agent/`](https://github.com/sanning-io/proof/tree/main/cross-kernel/vendor-agent) so anyone can build and run it. The TypeScript and Python kernels are conformance-gated **byte-for-byte** in CI against the shared [`test-vectors` corpus](https://github.com/sanning-io/proof/tree/main/test-vectors) (per-file SHA-256 pins; current cut tag `test-vectors-v3.1`), and the [cross-kernel agreement gate](https://github.com/sanning-io/proof/tree/main/cross-kernel) re-runs the corpus plus adversarial negatives through both, asserting identical verdicts on identical bytes. The Go reference runs as a third leg of that same gate, built at the commit its vendoring pins.
|
|
207
207
|
|
|
208
208
|
That mutual gate is the point: anyone can verify these provenance records with **no Sanning code in the trust path** — write your own verifier against the [public specs](https://github.com/sanning-io/proof/tree/main/specs) and the corpus will tell you if it's conformant.
|
|
209
209
|
|
package/dist/chain.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one sentence a surface should print beside these findings, so the reader
|
|
3
|
+
* is never left to infer that "no findings" means "nothing is missing".
|
|
4
|
+
*
|
|
5
|
+
* Lifted from the profile (§5) rather than reworded, because a paraphrase of a
|
|
6
|
+
* scope limit is how a scope limit turns into a claim.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CHAIN_READING_SCOPE = "Chain links are deletion/reorder-evidence for the events present; they are not a completeness proof.";
|
|
9
|
+
/** Where the linkage was read from. */
|
|
10
|
+
export type ChainSource = "record" | "adapter";
|
|
11
|
+
/**
|
|
12
|
+
* The adapter metadata namespaces that carry a chain when the record does not.
|
|
13
|
+
*
|
|
14
|
+
* VERIFIED AGAINST `sanning-io/anchor` @ main, 2026-09-07. `run` is NOT a typo
|
|
15
|
+
* for `langchain`: anchor#68 promoted the LangChain linkage out of a
|
|
16
|
+
* framework-named namespace into a shared one, so the same fact has one path in
|
|
17
|
+
* every SDK. A table pinned on the old name would find no chains, report
|
|
18
|
+
* nothing, and look exactly like a clean bundle — the rename trap, in the
|
|
19
|
+
* direction that fails silently.
|
|
20
|
+
*
|
|
21
|
+
* Exported so a test can assert the table itself, and so a consumer can see
|
|
22
|
+
* what this reader knows about rather than guessing.
|
|
23
|
+
*/
|
|
24
|
+
export declare const ADAPTER_CHAIN_NAMESPACES: readonly {
|
|
25
|
+
namespace: string;
|
|
26
|
+
keyField: string;
|
|
27
|
+
}[];
|
|
28
|
+
export type ChainFindingKind =
|
|
29
|
+
/** A record points at a predecessor no record in this bundle presents. */
|
|
30
|
+
"chain-link-dangling"
|
|
31
|
+
/** A `seq` slot between two records this bundle presents has no record. */
|
|
32
|
+
| "chain-gap"
|
|
33
|
+
/** More than one record claims to begin the chain. */
|
|
34
|
+
| "chain-second-root";
|
|
35
|
+
/**
|
|
36
|
+
* One link that does not resolve, as data.
|
|
37
|
+
*
|
|
38
|
+
* Structured for the same reason `PackGradeCause` is: prose is the part that
|
|
39
|
+
* drifts hardest between surfaces and matters least. `renderChainFinding`
|
|
40
|
+
* produces the house wording; a surface with its own voice reads the fields.
|
|
41
|
+
*/
|
|
42
|
+
export interface ChainFinding {
|
|
43
|
+
kind: ChainFindingKind;
|
|
44
|
+
/** The chain this is about, as the records spell it. */
|
|
45
|
+
chainKey: string;
|
|
46
|
+
/** Whether the linkage came from the record core or an adapter namespace. */
|
|
47
|
+
source: ChainSource;
|
|
48
|
+
/** The adapter metadata namespace, when `source` is `"adapter"`. */
|
|
49
|
+
namespace?: string;
|
|
50
|
+
/** The producing key, since a chain is one head per (public_key, chain_key). */
|
|
51
|
+
publicKey: string;
|
|
52
|
+
/** How many links, slots or roots the finding is about. */
|
|
53
|
+
count: number;
|
|
54
|
+
/** How many records this bundle presents for the chain, framing `count`. */
|
|
55
|
+
of: number;
|
|
56
|
+
/** The affected `event_id`s, so a reader has somewhere to look. */
|
|
57
|
+
eventIds: string[];
|
|
58
|
+
/** The pointer that resolved to no record here (`chain-link-dangling`). */
|
|
59
|
+
unresolvedPointer?: string;
|
|
60
|
+
/** The first sequence slot with no record here (`chain-gap`). */
|
|
61
|
+
firstMissingSeq?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* One chain this bundle presents records for.
|
|
65
|
+
*
|
|
66
|
+
* `events` is HOW MANY RECORDS ARE HERE — never how many the chain has. The
|
|
67
|
+
* summary exists so a surface can say "read 3 chains, nothing to report"
|
|
68
|
+
* instead of "nothing to report", which is also what a reader that silently
|
|
69
|
+
* matched no records would say.
|
|
70
|
+
*/
|
|
71
|
+
export interface ChainSummary {
|
|
72
|
+
chainKey: string;
|
|
73
|
+
source: ChainSource;
|
|
74
|
+
namespace?: string;
|
|
75
|
+
publicKey: string;
|
|
76
|
+
events: number;
|
|
77
|
+
}
|
|
78
|
+
export interface ChainReading {
|
|
79
|
+
chains: ChainSummary[];
|
|
80
|
+
findings: ChainFinding[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Follow every chain the bundle's records describe, and report the links that
|
|
84
|
+
* do not resolve within it.
|
|
85
|
+
*
|
|
86
|
+
* Reports NOTHING for an intact chain, and nothing for a chain whose records
|
|
87
|
+
* are withheld — a link we could not read is unknown, never an accusation.
|
|
88
|
+
* `chains` is what makes that distinguishable from having read nothing at all.
|
|
89
|
+
*/
|
|
90
|
+
export declare function readChains(bundle: unknown): Promise<ChainReading>;
|
|
91
|
+
/**
|
|
92
|
+
* The house wording for one finding.
|
|
93
|
+
*
|
|
94
|
+
* Deliberately free of verdict vocabulary. Every sentence says what does not
|
|
95
|
+
* resolve IN THIS BUNDLE, because that is the whole of what was checked; none
|
|
96
|
+
* of them says failed, invalid, tampered or complete. A surface with its own
|
|
97
|
+
* voice should read the fields instead — but it must not upgrade the claim.
|
|
98
|
+
*/
|
|
99
|
+
export declare function renderChainFinding(finding: ChainFinding, subject?: string): string;
|
package/dist/chain.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// Chain reading — following the pointer every anchored event carries to the
|
|
2
|
+
// event before it, and reporting what does not line up.
|
|
3
|
+
//
|
|
4
|
+
// WHY THIS LIVES IN THE KERNEL, BESIDE PACK GRADING.
|
|
5
|
+
// `sanning.events/v1` records carry `previous_hash`, and until now nothing we
|
|
6
|
+
// shipped read it. The envelope verifier hashes each event's `record_bytes`
|
|
7
|
+
// and stops — `evidence.ts` says following the link is "consumer-layer logic
|
|
8
|
+
// above it", and it was right that a SINGLE-envelope verifier should not do
|
|
9
|
+
// it. But the consumer layer never did it either, so a bundle with an event
|
|
10
|
+
// removed, and a chain that restarted after a crash and now asserts two
|
|
11
|
+
// beginnings, both passed every shipped check (proof#43).
|
|
12
|
+
//
|
|
13
|
+
// Reading the link is not envelope verification and it is not pack grading: it
|
|
14
|
+
// is a third, pure rule over the records a bundle presents. Same seam as
|
|
15
|
+
// grading — data in, data out, no `fs`, no `fetch` — so the kernel's scope
|
|
16
|
+
// boundary (`specs/architecture.md` §3) is unchanged.
|
|
17
|
+
//
|
|
18
|
+
// ---- WHAT THIS IS NOT ------------------------------------------------------
|
|
19
|
+
//
|
|
20
|
+
// It is not a completeness statement and it must never be read as one. The
|
|
21
|
+
// profile says so in its own words (`anchor/docs/profile-sanning.events-v1.md`
|
|
22
|
+
// §5): chain semantics are "deletion/reorder-evidence for the events present
|
|
23
|
+
// ... not a completeness proof." Drop the last N events of a chain and nothing
|
|
24
|
+
// here dangles; the record that would have pointed at them was never written.
|
|
25
|
+
// Completeness is the checkpoint `leaf_count` mechanism, which lives in
|
|
26
|
+
// `grading.ts` and answers a different question.
|
|
27
|
+
//
|
|
28
|
+
// So nothing in this module returns a verdict, a status, an `ok`, or the word
|
|
29
|
+
// "complete". It returns FINDINGS — each one a specific link that does not
|
|
30
|
+
// resolve within the bundle in front of us — and a count of the chains it read,
|
|
31
|
+
// so a surface can tell "nothing to report" apart from "read nothing".
|
|
32
|
+
//
|
|
33
|
+
// ---- HOW A CHAIN IS IDENTIFIED ---------------------------------------------
|
|
34
|
+
//
|
|
35
|
+
// A chain is one head per `(public_key, chain_key)` — profile §3.2, and the
|
|
36
|
+
// reason `public_key` is part of every group key below. Two producers using the
|
|
37
|
+
// chain name "session-1" are two chains; merging them would manufacture a
|
|
38
|
+
// second root out of a naming coincidence.
|
|
39
|
+
//
|
|
40
|
+
// The linkage is written two ways today, and this reads both:
|
|
41
|
+
//
|
|
42
|
+
// 1. THE RECORD'S OWN CHAIN (`previous_hash` + `context.chain_key`). The
|
|
43
|
+
// profile's mechanism, and the one a third party can walk from the spec
|
|
44
|
+
// alone. Links resolve in PAYLOAD-HASH space.
|
|
45
|
+
// 2. THE ADAPTER'S METADATA (`metadata.<namespace>.prev_event_id` + `seq`).
|
|
46
|
+
// The batched paths write the linkage only here; their records say
|
|
47
|
+
// GENESIS. Links resolve in EVENT-ID space. Read as a FALLBACK, only when
|
|
48
|
+
// the record carries no chain of its own.
|
|
49
|
+
//
|
|
50
|
+
// (2) is a transitional accommodation, not the design. `metadata` is
|
|
51
|
+
// caller-owned and the profile says a verifier MUST NOT read it for
|
|
52
|
+
// verification — which is exactly why nothing here feeds a verdict. The fix is
|
|
53
|
+
// for every adapter to set the record's own `previous_hash` (filed separately
|
|
54
|
+
// against `anchor`); when it lands, the namespace table below stops mattering
|
|
55
|
+
// and can go.
|
|
56
|
+
import { hexToBytes, sha256Hex } from "./crypto.js";
|
|
57
|
+
import { resolveTraceEvents } from "./evidence.js";
|
|
58
|
+
/**
|
|
59
|
+
* The one sentence a surface should print beside these findings, so the reader
|
|
60
|
+
* is never left to infer that "no findings" means "nothing is missing".
|
|
61
|
+
*
|
|
62
|
+
* Lifted from the profile (§5) rather than reworded, because a paraphrase of a
|
|
63
|
+
* scope limit is how a scope limit turns into a claim.
|
|
64
|
+
*/
|
|
65
|
+
export const CHAIN_READING_SCOPE = "Chain links are deletion/reorder-evidence for the events present; they are not a completeness proof.";
|
|
66
|
+
/**
|
|
67
|
+
* The adapter metadata namespaces that carry a chain when the record does not.
|
|
68
|
+
*
|
|
69
|
+
* VERIFIED AGAINST `sanning-io/anchor` @ main, 2026-09-07. `run` is NOT a typo
|
|
70
|
+
* for `langchain`: anchor#68 promoted the LangChain linkage out of a
|
|
71
|
+
* framework-named namespace into a shared one, so the same fact has one path in
|
|
72
|
+
* every SDK. A table pinned on the old name would find no chains, report
|
|
73
|
+
* nothing, and look exactly like a clean bundle — the rename trap, in the
|
|
74
|
+
* direction that fails silently.
|
|
75
|
+
*
|
|
76
|
+
* Exported so a test can assert the table itself, and so a consumer can see
|
|
77
|
+
* what this reader knows about rather than guessing.
|
|
78
|
+
*/
|
|
79
|
+
export const ADAPTER_CHAIN_NAMESPACES = [
|
|
80
|
+
// @sanning/anchor LangChain callbacks, TS + Python (anchor#68). One chain per
|
|
81
|
+
// root run, so the root run id is the chain.
|
|
82
|
+
{ namespace: "run", keyField: "root_run_id" },
|
|
83
|
+
// @sanning/anchor Vercel AI SDK middleware. A flat sequence per correlation key.
|
|
84
|
+
{ namespace: "vercel_ai", keyField: "chain_key" },
|
|
85
|
+
// @sanning/anchor Interchange adapter. One chain per session; `seq` is
|
|
86
|
+
// Interchange's own reactor-owned sequence, passed through.
|
|
87
|
+
{ namespace: "interchange", keyField: "session_id" },
|
|
88
|
+
];
|
|
89
|
+
/**
|
|
90
|
+
* The events this bundle is about, unwrapping any evidence-export wrapper to
|
|
91
|
+
* the inline source bundle. DEFERRED to `evidence.ts`, which is the same
|
|
92
|
+
* descent `verifyEvidenceBundle` and `disclosedContent` already use — a second
|
|
93
|
+
* answer to "which events" inside one kernel is a second implementation of one
|
|
94
|
+
* rule, and it is how a reader ends up walking a different set of events than
|
|
95
|
+
* the verifier reported on.
|
|
96
|
+
*/
|
|
97
|
+
const eventsOf = (bundle) => resolveTraceEvents(bundle) ?? [];
|
|
98
|
+
/**
|
|
99
|
+
* Decode one event record from its committed bytes.
|
|
100
|
+
*
|
|
101
|
+
* Same discipline as `signedLeafCount`: hex, UTF-8, JSON, and an undecodable
|
|
102
|
+
* record is `null` rather than a throw or a guess. A withheld or unreadable
|
|
103
|
+
* record simply takes no part in a chain — `verifyEvidenceBundle` already
|
|
104
|
+
* reports its binding as undetermined, and inventing a second accusation here
|
|
105
|
+
* would say the same thing twice in a harsher voice.
|
|
106
|
+
*/
|
|
107
|
+
function decodeRecord(hex) {
|
|
108
|
+
if (typeof hex !== "string" || hex.length === 0)
|
|
109
|
+
return null;
|
|
110
|
+
try {
|
|
111
|
+
const bytes = hexToBytes(hex);
|
|
112
|
+
const record = JSON.parse(new TextDecoder().decode(bytes));
|
|
113
|
+
if (record === null || typeof record !== "object")
|
|
114
|
+
return null;
|
|
115
|
+
return { record, bytes };
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const str = (v) => (typeof v === "string" && v.length > 0 ? v : null);
|
|
122
|
+
/**
|
|
123
|
+
* The adapter linkage, if this record carries one this reader knows about.
|
|
124
|
+
*
|
|
125
|
+
* Order is the table's order and the first match wins; a record carrying two
|
|
126
|
+
* adapter namespaces is not a shape any SDK emits.
|
|
127
|
+
*/
|
|
128
|
+
function adapterLinkage(record) {
|
|
129
|
+
const metadata = record.metadata;
|
|
130
|
+
if (metadata === null || typeof metadata !== "object")
|
|
131
|
+
return null;
|
|
132
|
+
for (const { namespace, keyField } of ADAPTER_CHAIN_NAMESPACES) {
|
|
133
|
+
const ns = metadata[namespace];
|
|
134
|
+
if (ns === null || typeof ns !== "object")
|
|
135
|
+
continue;
|
|
136
|
+
const fields = ns;
|
|
137
|
+
const chainKey = str(fields[keyField]);
|
|
138
|
+
if (chainKey === null)
|
|
139
|
+
continue;
|
|
140
|
+
// `prev_event_id` is explicitly null at the head of an adapter chain — the
|
|
141
|
+
// adapters write the null rather than omitting the key, because absent and
|
|
142
|
+
// null mean different things to a reader. Absent is treated the same way
|
|
143
|
+
// here: nothing to resolve is a beginning.
|
|
144
|
+
const prev = str(fields.prev_event_id);
|
|
145
|
+
const seqRaw = fields.seq;
|
|
146
|
+
// SAFE integer, not merely integral. Beyond 2^53 a JSON number does not
|
|
147
|
+
// survive `JSON.parse` intact — `9007199254740993` arrives as
|
|
148
|
+
// `...992` — while Python's parser keeps it exactly, so the two kernels
|
|
149
|
+
// read DIFFERENT sequence numbers off identical bytes and report gaps of
|
|
150
|
+
// different widths. Measured at 2^53+1: a `chain-gap` count of
|
|
151
|
+
// 9007199254740990 here against 9007199254740992 there (proof#49). A value
|
|
152
|
+
// this kernel cannot represent is one it cannot read, which is the same
|
|
153
|
+
// answer it gives every other unreadable field: no sequence, take no part.
|
|
154
|
+
const seq = Number.isSafeInteger(seqRaw) ? seqRaw : null;
|
|
155
|
+
return { namespace, chainKey, prev, seq };
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Group the bundle's records into chains.
|
|
161
|
+
*
|
|
162
|
+
* Self-ids are DERIVED, never read off the envelope. A record chain's pointer
|
|
163
|
+
* is the previous record's `payload_hash`, and `payload_hash` is an asserted
|
|
164
|
+
* label sitting next to the bytes it claims to be about; matching one asserted
|
|
165
|
+
* label against another is how a relabelled checkpoint once verified cleanly
|
|
166
|
+
* (`evidence.ts`, `checkpoint_ref`). Hashing the bytes we already hold costs
|
|
167
|
+
* nothing and means a relabelled record DANGLES instead of quietly resolving.
|
|
168
|
+
* That is why this function is async.
|
|
169
|
+
*/
|
|
170
|
+
async function groupChains(bundle) {
|
|
171
|
+
const groups = new Map();
|
|
172
|
+
const events = eventsOf(bundle);
|
|
173
|
+
for (let order = 0; order < events.length; order++) {
|
|
174
|
+
const entry = events[order];
|
|
175
|
+
const eventId = str(entry?.envelope?.event_id);
|
|
176
|
+
const publicKey = str(entry?.envelope?.public_key);
|
|
177
|
+
// Without a producing key there is no chain identity (§3.2), and without an
|
|
178
|
+
// event id an adapter pointer has nothing to resolve against.
|
|
179
|
+
if (eventId === null || publicKey === null)
|
|
180
|
+
continue;
|
|
181
|
+
const decoded = decodeRecord(entry?.record_bytes);
|
|
182
|
+
if (decoded === null)
|
|
183
|
+
continue;
|
|
184
|
+
const { record, bytes } = decoded;
|
|
185
|
+
// 1. The record's own chain. `context.chain_key` is present IFF the record
|
|
186
|
+
// is chained (profile §5) — which is what keeps every unchained
|
|
187
|
+
// singleton, all of them carrying `previous_hash: "GENESIS"`, out of one
|
|
188
|
+
// enormous fictional chain that would report a second root per pair.
|
|
189
|
+
const recordChainKey = str(record.context?.chain_key);
|
|
190
|
+
let member;
|
|
191
|
+
let key;
|
|
192
|
+
let group;
|
|
193
|
+
if (recordChainKey !== null) {
|
|
194
|
+
const previous = str(record.previous_hash);
|
|
195
|
+
member = {
|
|
196
|
+
eventId,
|
|
197
|
+
selfId: await sha256Hex(bytes),
|
|
198
|
+
prev: previous === null || previous === "GENESIS" ? null : previous,
|
|
199
|
+
seq: null,
|
|
200
|
+
order,
|
|
201
|
+
};
|
|
202
|
+
group = { chainKey: recordChainKey, source: "record", publicKey };
|
|
203
|
+
key = `record ${publicKey} ${recordChainKey}`;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
// 2. Fallback only. A record that carries its own chain is never read
|
|
207
|
+
// through its adapter metadata: one record must not land in two
|
|
208
|
+
// chains, and the profile's own field is the one a third party can
|
|
209
|
+
// walk without knowing which SDK wrote it.
|
|
210
|
+
const linkage = adapterLinkage(record);
|
|
211
|
+
if (linkage === null)
|
|
212
|
+
continue;
|
|
213
|
+
member = { eventId, selfId: eventId, prev: linkage.prev, seq: linkage.seq, order };
|
|
214
|
+
group = {
|
|
215
|
+
chainKey: linkage.chainKey,
|
|
216
|
+
source: "adapter",
|
|
217
|
+
namespace: linkage.namespace,
|
|
218
|
+
publicKey,
|
|
219
|
+
};
|
|
220
|
+
key = `adapter ${linkage.namespace} ${publicKey} ${linkage.chainKey}`;
|
|
221
|
+
}
|
|
222
|
+
const existing = groups.get(key);
|
|
223
|
+
if (existing === undefined)
|
|
224
|
+
groups.set(key, { ...group, members: [member] });
|
|
225
|
+
else
|
|
226
|
+
existing.members.push(member);
|
|
227
|
+
}
|
|
228
|
+
return [...groups.values()];
|
|
229
|
+
}
|
|
230
|
+
// ---- The rule ----------------------------------------------------------------
|
|
231
|
+
/** By `seq` where both have one, else by position in the bundle. */
|
|
232
|
+
function byChainOrder(a, b) {
|
|
233
|
+
if (a.seq !== null && b.seq !== null && a.seq !== b.seq)
|
|
234
|
+
return a.seq - b.seq;
|
|
235
|
+
return a.order - b.order;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The smallest slot between the lowest and highest occupied ones that no record
|
|
239
|
+
* occupies, found by walking the SORTED occupants rather than the range they
|
|
240
|
+
* span. See the call site: the range's width is chosen by whoever wrote the
|
|
241
|
+
* bundle.
|
|
242
|
+
*/
|
|
243
|
+
function firstMissingSlot(present) {
|
|
244
|
+
const ordered = [...present].sort((a, b) => a - b);
|
|
245
|
+
for (let i = 0; i < ordered.length - 1; i++) {
|
|
246
|
+
if (ordered[i + 1] > ordered[i] + 1)
|
|
247
|
+
return ordered[i] + 1;
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
function findingsFor(group) {
|
|
252
|
+
const { members, chainKey, source, publicKey } = group;
|
|
253
|
+
const of = members.length;
|
|
254
|
+
const base = {
|
|
255
|
+
chainKey,
|
|
256
|
+
source,
|
|
257
|
+
publicKey,
|
|
258
|
+
of,
|
|
259
|
+
...(group.namespace === undefined ? {} : { namespace: group.namespace }),
|
|
260
|
+
};
|
|
261
|
+
const findings = [];
|
|
262
|
+
const selfIds = new Set(members.map((m) => m.selfId));
|
|
263
|
+
const roots = members.filter((m) => m.prev === null).sort(byChainOrder);
|
|
264
|
+
const dangling = members
|
|
265
|
+
.filter((m) => m.prev !== null && !selfIds.has(m.prev))
|
|
266
|
+
.sort(byChainOrder);
|
|
267
|
+
// A SECOND BEGINNING. One chain has one head per (public_key, chain_key), so
|
|
268
|
+
// two records claiming GENESIS under one key is a fork — the case a link-walk
|
|
269
|
+
// alone reports as CLEAN, because both halves walk back to a legitimate
|
|
270
|
+
// beginning and neither dangles. It is what a crash looks like when the SDK
|
|
271
|
+
// lost its chain head and started over under the same name.
|
|
272
|
+
if (roots.length > 1) {
|
|
273
|
+
findings.push({
|
|
274
|
+
kind: "chain-second-root",
|
|
275
|
+
...base,
|
|
276
|
+
count: roots.length,
|
|
277
|
+
eventIds: roots.map((m) => m.eventId),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
// A POINTER THAT LEAVES THIS BUNDLE. Exactly one such pointer is expected and
|
|
281
|
+
// is NOT a finding: a bundle is a window onto a chain that may have begun
|
|
282
|
+
// before this export, so the window's earliest record necessarily points at a
|
|
283
|
+
// predecessor nobody handed us. The allowance is spent only when the chain
|
|
284
|
+
// shows no GENESIS root — a chain that already has its beginning here has no
|
|
285
|
+
// window edge left to explain the next one.
|
|
286
|
+
const unexplained = roots.length === 0 ? dangling.slice(1) : dangling;
|
|
287
|
+
if (unexplained.length > 0) {
|
|
288
|
+
const first = unexplained[0];
|
|
289
|
+
findings.push({
|
|
290
|
+
kind: "chain-link-dangling",
|
|
291
|
+
...base,
|
|
292
|
+
count: unexplained.length,
|
|
293
|
+
eventIds: unexplained.map((m) => m.eventId),
|
|
294
|
+
unresolvedPointer: first.prev,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
// A HOLE IN THE SEQUENCE, where an adapter numbers its events. Only INTERIOR
|
|
298
|
+
// holes count: a window that starts at seq 7 is a window, not a deletion, and
|
|
299
|
+
// reporting its first six absent slots would bury the one that matters. A
|
|
300
|
+
// repeated seq is a real defect but a different one, and the present-set below
|
|
301
|
+
// is deliberately duplicate-safe rather than count-based so it cannot be
|
|
302
|
+
// mistaken for a gap.
|
|
303
|
+
const sequenced = members.filter((m) => m.seq !== null);
|
|
304
|
+
if (sequenced.length === of && of > 0) {
|
|
305
|
+
const present = new Set(sequenced.map((m) => m.seq));
|
|
306
|
+
const low = Math.min(...present);
|
|
307
|
+
const high = Math.max(...present);
|
|
308
|
+
// Counted, never walked. `seq` is attacker-chosen, so a bundle presenting
|
|
309
|
+
// two records at seq 0 and seq 1e15 made the old `for (let s = low; s <=
|
|
310
|
+
// high; s++)` spin for days — a verifier that HANGS on adversarial input is
|
|
311
|
+
// worse than one that raises, and this reader promises neither (proof#49).
|
|
312
|
+
// The present set is a subset of [low, high], so the absent slots are the
|
|
313
|
+
// width minus the occupants, and the first of them is found by walking the
|
|
314
|
+
// occupants in order.
|
|
315
|
+
const missingCount = high - low + 1 - present.size;
|
|
316
|
+
const firstMissing = missingCount > 0 ? firstMissingSlot(present) : null;
|
|
317
|
+
if (firstMissing !== null) {
|
|
318
|
+
const after = sequenced.filter((m) => m.seq > firstMissing).sort(byChainOrder)[0];
|
|
319
|
+
findings.push({
|
|
320
|
+
kind: "chain-gap",
|
|
321
|
+
...base,
|
|
322
|
+
count: missingCount,
|
|
323
|
+
eventIds: after === undefined ? [] : [after.eventId],
|
|
324
|
+
firstMissingSeq: firstMissing,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return findings;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Follow every chain the bundle's records describe, and report the links that
|
|
332
|
+
* do not resolve within it.
|
|
333
|
+
*
|
|
334
|
+
* Reports NOTHING for an intact chain, and nothing for a chain whose records
|
|
335
|
+
* are withheld — a link we could not read is unknown, never an accusation.
|
|
336
|
+
* `chains` is what makes that distinguishable from having read nothing at all.
|
|
337
|
+
*/
|
|
338
|
+
export async function readChains(bundle) {
|
|
339
|
+
const groups = await groupChains(bundle);
|
|
340
|
+
const order = (g) => `${g.source} ${g.namespace ?? ""} ${g.chainKey}`;
|
|
341
|
+
// Ordered by UTF-16 code unit, NOT `localeCompare`. A chain key is caller
|
|
342
|
+
// text, and `localeCompare` with no locale argument answers from the host's
|
|
343
|
+
// ICU collation: measured here, it puts "chain-2" AFTER "chain_1" where every
|
|
344
|
+
// code-wise comparison puts it before, and it can differ between two
|
|
345
|
+
// TypeScript installs, let alone between kernels. The Python reader sorts the
|
|
346
|
+
// same key encoded as `utf-16-be`, which is exactly this comparison — so the
|
|
347
|
+
// two kernels emit findings in one order (proof#49).
|
|
348
|
+
groups.sort((a, b) => {
|
|
349
|
+
const x = order(a);
|
|
350
|
+
const y = order(b);
|
|
351
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
352
|
+
});
|
|
353
|
+
return {
|
|
354
|
+
chains: groups.map((g) => ({
|
|
355
|
+
chainKey: g.chainKey,
|
|
356
|
+
source: g.source,
|
|
357
|
+
...(g.namespace === undefined ? {} : { namespace: g.namespace }),
|
|
358
|
+
publicKey: g.publicKey,
|
|
359
|
+
events: g.members.length,
|
|
360
|
+
})),
|
|
361
|
+
findings: groups.flatMap(findingsFor),
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* The house wording for one finding.
|
|
366
|
+
*
|
|
367
|
+
* Deliberately free of verdict vocabulary. Every sentence says what does not
|
|
368
|
+
* resolve IN THIS BUNDLE, because that is the whole of what was checked; none
|
|
369
|
+
* of them says failed, invalid, tampered or complete. A surface with its own
|
|
370
|
+
* voice should read the fields instead — but it must not upgrade the claim.
|
|
371
|
+
*/
|
|
372
|
+
export function renderChainFinding(finding, subject = "this bundle") {
|
|
373
|
+
const where = finding.source === "adapter"
|
|
374
|
+
? `chain ${finding.chainKey} (${finding.namespace} metadata)`
|
|
375
|
+
: `chain ${finding.chainKey}`;
|
|
376
|
+
switch (finding.kind) {
|
|
377
|
+
case "chain-link-dangling":
|
|
378
|
+
return (`${where}: ${finding.count} of ${finding.of} record(s) point at a predecessor ` +
|
|
379
|
+
`${subject} does not present (first: ${finding.eventIds[0]}, pointer ` +
|
|
380
|
+
`${finding.unresolvedPointer})`);
|
|
381
|
+
case "chain-gap":
|
|
382
|
+
return (`${where}: ${finding.count} sequence slot(s) between the records ${subject} presents ` +
|
|
383
|
+
`carry no record (first missing seq ${finding.firstMissingSeq}` +
|
|
384
|
+
`${finding.eventIds[0] === undefined ? "" : `, next present: ${finding.eventIds[0]}`})`);
|
|
385
|
+
case "chain-second-root":
|
|
386
|
+
return (`${where}: ${finding.count} records begin the chain where one chain has one beginning ` +
|
|
387
|
+
`(${finding.eventIds.join(", ")})`);
|
|
388
|
+
}
|
|
389
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -18,6 +18,7 @@ import { realpathSync } from "node:fs";
|
|
|
18
18
|
import { join } from "node:path";
|
|
19
19
|
import { fileURLToPath } from "node:url";
|
|
20
20
|
import { isAgentProofSpec, verifyAgentProofBundle } from "./agent-proof.js";
|
|
21
|
+
import { CHAIN_READING_SCOPE, readChains, renderChainFinding } from "./chain.js";
|
|
21
22
|
import { composeExport } from "./compose.js";
|
|
22
23
|
import { utf8 } from "./crypto.js";
|
|
23
24
|
import { verifyEvidenceBundle } from "./evidence.js";
|
|
@@ -147,7 +148,12 @@ export async function runCli(argv, io) {
|
|
|
147
148
|
...(content ? { content } : {}),
|
|
148
149
|
...(io.fetchImpl ? { fetchImpl: io.fetchImpl } : {}),
|
|
149
150
|
});
|
|
150
|
-
|
|
151
|
+
// Read the chains from the SAME parsed bundle the verifier saw. Findings are
|
|
152
|
+
// evidence about the records in front of us, not a verdict: they are printed
|
|
153
|
+
// and they never move `exit`, because a chain is not a completeness proof and
|
|
154
|
+
// an exit code is exactly the kind of thing people read as one.
|
|
155
|
+
const chains = await readChains(parsed);
|
|
156
|
+
const exit = reportEvidence(result, bundlePath, gateways, io, chains);
|
|
151
157
|
// Coverage is reported only once the bundle parsed far enough to have events
|
|
152
158
|
// to compare against; over a MALFORMED bundle there is nothing to say.
|
|
153
159
|
if (logs && result.status !== "malformed")
|
|
@@ -584,7 +590,7 @@ function printUsage(io) {
|
|
|
584
590
|
io.out("");
|
|
585
591
|
io.out("Exit codes: 0 verified · 1 failed · 2 malformed · 3 gateway-unavailable");
|
|
586
592
|
}
|
|
587
|
-
function reportEvidence(result, path, gateways, io) {
|
|
593
|
+
function reportEvidence(result, path, gateways, io, chains) {
|
|
588
594
|
if (result.status === "malformed") {
|
|
589
595
|
io.err(paint("MALFORMED", R) + ` ${path}`);
|
|
590
596
|
for (const e of result.errors)
|
|
@@ -620,6 +626,7 @@ function reportEvidence(result, path, gateways, io) {
|
|
|
620
626
|
io.out(paint(` - ${e}`, R));
|
|
621
627
|
}
|
|
622
628
|
}
|
|
629
|
+
reportChains(chains, io);
|
|
623
630
|
if (result.export) {
|
|
624
631
|
reportExport(result.export, io);
|
|
625
632
|
}
|
|
@@ -634,6 +641,39 @@ function reportEvidence(result, path, gateways, io) {
|
|
|
634
641
|
}
|
|
635
642
|
return mapStatusToExit(result.status, result.errors);
|
|
636
643
|
}
|
|
644
|
+
/**
|
|
645
|
+
* The chain block: what following each record's pointer to the record before it
|
|
646
|
+
* showed, over the records THIS bundle presents.
|
|
647
|
+
*
|
|
648
|
+
* Three deliberate choices, all of them about not overclaiming.
|
|
649
|
+
*
|
|
650
|
+
* 1. It prints the chains it READ, not just the findings. "3 chains, nothing to
|
|
651
|
+
* report" and silence are different statements, and a reader that quietly
|
|
652
|
+
* matched no records at all would produce the second one while covering
|
|
653
|
+
* nothing.
|
|
654
|
+
* 2. It prints the scope sentence whenever it read a chain, findings or not.
|
|
655
|
+
* "Delete an event and the next pointer dangles" is true; "so the bundle is
|
|
656
|
+
* complete" does not follow, and two customer-facing documents had already
|
|
657
|
+
* made that leap. The limit travels with the result or it gets dropped.
|
|
658
|
+
* 3. It does not touch the exit code. Findings are evidence; the exit code is
|
|
659
|
+
* the verdict surface, and the pinned 0/1/2/3 contract stays exactly what it
|
|
660
|
+
* was. A dangling link is something for a person to look at, not a machine
|
|
661
|
+
* decision this check is entitled to make.
|
|
662
|
+
*/
|
|
663
|
+
function reportChains(reading, io) {
|
|
664
|
+
if (reading.chains.length === 0)
|
|
665
|
+
return;
|
|
666
|
+
io.out("");
|
|
667
|
+
io.out(paint(" Event chains", DIM));
|
|
668
|
+
for (const c of reading.chains) {
|
|
669
|
+
const where = c.namespace === undefined ? "record" : `${c.namespace} metadata`;
|
|
670
|
+
io.out(paint(` ${c.chainKey} ${count(c.events, "record", "records")} present (linked in ${where})`, DIM));
|
|
671
|
+
}
|
|
672
|
+
for (const f of reading.findings) {
|
|
673
|
+
io.out(paint(` ! ${renderChainFinding(f, "this bundle")}`, Y));
|
|
674
|
+
}
|
|
675
|
+
io.out(paint(` ${CHAIN_READING_SCOPE}`, DIM));
|
|
676
|
+
}
|
|
637
677
|
// Render the export-specific block (evidence-export.md §5): source-bundle
|
|
638
678
|
// linkage, cached-vs-recomputed verdict agreement, and each embedded operator
|
|
639
679
|
// attestation (RSA-PSS sig · operator-address binding · data_hash binding).
|