@sanning/proof 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -4
- package/dist/chain.d.ts +99 -0
- package/dist/chain.js +351 -0
- package/dist/cli.js +253 -38
- package/dist/compose.js +1 -1
- package/dist/evidence.d.ts +204 -7
- package/dist/evidence.js +621 -67
- package/dist/grading.d.ts +160 -0
- package/dist/grading.js +469 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +38 -1
- package/dist/side-content.d.ts +36 -0
- package/dist/side-content.js +98 -0
- package/dist/verifier.js +46 -7
- package/package.json +2 -1
- package/src/chain.ts +460 -0
- package/src/cli.ts +324 -35
- package/src/compose.ts +1 -1
- package/src/evidence.ts +875 -81
- package/src/grading.ts +623 -0
- package/src/index.ts +79 -0
- package/src/side-content.ts +119 -0
- package/src/verifier.ts +49 -7
- package/vectors/README.md +66 -0
- package/vectors/chain-reading.json +293 -0
- package/vectors/pack-grading.json +136 -0
package/src/chain.ts
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
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
|
+
|
|
57
|
+
import { hexToBytes, sha256Hex } from "./crypto.js";
|
|
58
|
+
import { resolveTraceEvents } from "./evidence.js";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The one sentence a surface should print beside these findings, so the reader
|
|
62
|
+
* is never left to infer that "no findings" means "nothing is missing".
|
|
63
|
+
*
|
|
64
|
+
* Lifted from the profile (§5) rather than reworded, because a paraphrase of a
|
|
65
|
+
* scope limit is how a scope limit turns into a claim.
|
|
66
|
+
*/
|
|
67
|
+
export const CHAIN_READING_SCOPE =
|
|
68
|
+
"Chain links are deletion/reorder-evidence for the events present; they are not a completeness proof.";
|
|
69
|
+
|
|
70
|
+
/** Where the linkage was read from. */
|
|
71
|
+
export type ChainSource = "record" | "adapter";
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The adapter metadata namespaces that carry a chain when the record does not.
|
|
75
|
+
*
|
|
76
|
+
* VERIFIED AGAINST `sanning-io/anchor` @ main, 2026-09-07. `run` is NOT a typo
|
|
77
|
+
* for `langchain`: anchor#68 promoted the LangChain linkage out of a
|
|
78
|
+
* framework-named namespace into a shared one, so the same fact has one path in
|
|
79
|
+
* every SDK. A table pinned on the old name would find no chains, report
|
|
80
|
+
* nothing, and look exactly like a clean bundle — the rename trap, in the
|
|
81
|
+
* direction that fails silently.
|
|
82
|
+
*
|
|
83
|
+
* Exported so a test can assert the table itself, and so a consumer can see
|
|
84
|
+
* what this reader knows about rather than guessing.
|
|
85
|
+
*/
|
|
86
|
+
export const ADAPTER_CHAIN_NAMESPACES: readonly { namespace: string; keyField: string }[] = [
|
|
87
|
+
// @sanning/anchor LangChain callbacks, TS + Python (anchor#68). One chain per
|
|
88
|
+
// root run, so the root run id is the chain.
|
|
89
|
+
{ namespace: "run", keyField: "root_run_id" },
|
|
90
|
+
// @sanning/anchor Vercel AI SDK middleware. A flat sequence per correlation key.
|
|
91
|
+
{ namespace: "vercel_ai", keyField: "chain_key" },
|
|
92
|
+
// @sanning/anchor Interchange adapter. One chain per session; `seq` is
|
|
93
|
+
// Interchange's own reactor-owned sequence, passed through.
|
|
94
|
+
{ namespace: "interchange", keyField: "session_id" },
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
export type ChainFindingKind =
|
|
98
|
+
/** A record points at a predecessor no record in this bundle presents. */
|
|
99
|
+
| "chain-link-dangling"
|
|
100
|
+
/** A `seq` slot between two records this bundle presents has no record. */
|
|
101
|
+
| "chain-gap"
|
|
102
|
+
/** More than one record claims to begin the chain. */
|
|
103
|
+
| "chain-second-root";
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* One link that does not resolve, as data.
|
|
107
|
+
*
|
|
108
|
+
* Structured for the same reason `PackGradeCause` is: prose is the part that
|
|
109
|
+
* drifts hardest between surfaces and matters least. `renderChainFinding`
|
|
110
|
+
* produces the house wording; a surface with its own voice reads the fields.
|
|
111
|
+
*/
|
|
112
|
+
export interface ChainFinding {
|
|
113
|
+
kind: ChainFindingKind;
|
|
114
|
+
/** The chain this is about, as the records spell it. */
|
|
115
|
+
chainKey: string;
|
|
116
|
+
/** Whether the linkage came from the record core or an adapter namespace. */
|
|
117
|
+
source: ChainSource;
|
|
118
|
+
/** The adapter metadata namespace, when `source` is `"adapter"`. */
|
|
119
|
+
namespace?: string;
|
|
120
|
+
/** The producing key, since a chain is one head per (public_key, chain_key). */
|
|
121
|
+
publicKey: string;
|
|
122
|
+
/** How many links, slots or roots the finding is about. */
|
|
123
|
+
count: number;
|
|
124
|
+
/** How many records this bundle presents for the chain, framing `count`. */
|
|
125
|
+
of: number;
|
|
126
|
+
/** The affected `event_id`s, so a reader has somewhere to look. */
|
|
127
|
+
eventIds: string[];
|
|
128
|
+
/** The pointer that resolved to no record here (`chain-link-dangling`). */
|
|
129
|
+
unresolvedPointer?: string;
|
|
130
|
+
/** The first sequence slot with no record here (`chain-gap`). */
|
|
131
|
+
firstMissingSeq?: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* One chain this bundle presents records for.
|
|
136
|
+
*
|
|
137
|
+
* `events` is HOW MANY RECORDS ARE HERE — never how many the chain has. The
|
|
138
|
+
* summary exists so a surface can say "read 3 chains, nothing to report"
|
|
139
|
+
* instead of "nothing to report", which is also what a reader that silently
|
|
140
|
+
* matched no records would say.
|
|
141
|
+
*/
|
|
142
|
+
export interface ChainSummary {
|
|
143
|
+
chainKey: string;
|
|
144
|
+
source: ChainSource;
|
|
145
|
+
namespace?: string;
|
|
146
|
+
publicKey: string;
|
|
147
|
+
events: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface ChainReading {
|
|
151
|
+
chains: ChainSummary[];
|
|
152
|
+
findings: ChainFinding[];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ---- Reading the records -----------------------------------------------------
|
|
156
|
+
|
|
157
|
+
interface EventRecord {
|
|
158
|
+
previous_hash?: unknown;
|
|
159
|
+
context?: { chain_key?: unknown };
|
|
160
|
+
metadata?: Record<string, unknown>;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** One record's place in one chain, normalised across the two linkage forms. */
|
|
164
|
+
interface Member {
|
|
165
|
+
eventId: string;
|
|
166
|
+
/** What the NEXT record's pointer must equal: payload hash, or event id. */
|
|
167
|
+
selfId: string;
|
|
168
|
+
/** The pointer this record carries, or `null` when it claims to begin the chain. */
|
|
169
|
+
prev: string | null;
|
|
170
|
+
seq: number | null;
|
|
171
|
+
/** Position in `body.events`, the tiebreak when there is no `seq`. */
|
|
172
|
+
order: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface Group {
|
|
176
|
+
chainKey: string;
|
|
177
|
+
source: ChainSource;
|
|
178
|
+
namespace?: string;
|
|
179
|
+
publicKey: string;
|
|
180
|
+
members: Member[];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The events this bundle is about, unwrapping any evidence-export wrapper to
|
|
185
|
+
* the inline source bundle. DEFERRED to `evidence.ts`, which is the same
|
|
186
|
+
* descent `verifyEvidenceBundle` and `disclosedContent` already use — a second
|
|
187
|
+
* answer to "which events" inside one kernel is a second implementation of one
|
|
188
|
+
* rule, and it is how a reader ends up walking a different set of events than
|
|
189
|
+
* the verifier reported on.
|
|
190
|
+
*/
|
|
191
|
+
const eventsOf = (bundle: unknown) => resolveTraceEvents(bundle) ?? [];
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Decode one event record from its committed bytes.
|
|
195
|
+
*
|
|
196
|
+
* Same discipline as `signedLeafCount`: hex, UTF-8, JSON, and an undecodable
|
|
197
|
+
* record is `null` rather than a throw or a guess. A withheld or unreadable
|
|
198
|
+
* record simply takes no part in a chain — `verifyEvidenceBundle` already
|
|
199
|
+
* reports its binding as undetermined, and inventing a second accusation here
|
|
200
|
+
* would say the same thing twice in a harsher voice.
|
|
201
|
+
*/
|
|
202
|
+
function decodeRecord(hex: unknown): { record: EventRecord; bytes: Uint8Array } | null {
|
|
203
|
+
if (typeof hex !== "string" || hex.length === 0) return null;
|
|
204
|
+
try {
|
|
205
|
+
const bytes = hexToBytes(hex);
|
|
206
|
+
const record = JSON.parse(new TextDecoder().decode(bytes)) as EventRecord;
|
|
207
|
+
if (record === null || typeof record !== "object") return null;
|
|
208
|
+
return { record, bytes };
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const str = (v: unknown): string | null => (typeof v === "string" && v.length > 0 ? v : null);
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The adapter linkage, if this record carries one this reader knows about.
|
|
218
|
+
*
|
|
219
|
+
* Order is the table's order and the first match wins; a record carrying two
|
|
220
|
+
* adapter namespaces is not a shape any SDK emits.
|
|
221
|
+
*/
|
|
222
|
+
function adapterLinkage(
|
|
223
|
+
record: EventRecord,
|
|
224
|
+
): { namespace: string; chainKey: string; prev: string | null; seq: number | null } | null {
|
|
225
|
+
const metadata = record.metadata;
|
|
226
|
+
if (metadata === null || typeof metadata !== "object") return null;
|
|
227
|
+
for (const { namespace, keyField } of ADAPTER_CHAIN_NAMESPACES) {
|
|
228
|
+
const ns = (metadata as Record<string, unknown>)[namespace];
|
|
229
|
+
if (ns === null || typeof ns !== "object") continue;
|
|
230
|
+
const fields = ns as Record<string, unknown>;
|
|
231
|
+
const chainKey = str(fields[keyField]);
|
|
232
|
+
if (chainKey === null) continue;
|
|
233
|
+
// `prev_event_id` is explicitly null at the head of an adapter chain — the
|
|
234
|
+
// adapters write the null rather than omitting the key, because absent and
|
|
235
|
+
// null mean different things to a reader. Absent is treated the same way
|
|
236
|
+
// here: nothing to resolve is a beginning.
|
|
237
|
+
const prev = str(fields.prev_event_id);
|
|
238
|
+
const seqRaw = fields.seq;
|
|
239
|
+
const seq = Number.isInteger(seqRaw) ? (seqRaw as number) : null;
|
|
240
|
+
return { namespace, chainKey, prev, seq };
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Group the bundle's records into chains.
|
|
247
|
+
*
|
|
248
|
+
* Self-ids are DERIVED, never read off the envelope. A record chain's pointer
|
|
249
|
+
* is the previous record's `payload_hash`, and `payload_hash` is an asserted
|
|
250
|
+
* label sitting next to the bytes it claims to be about; matching one asserted
|
|
251
|
+
* label against another is how a relabelled checkpoint once verified cleanly
|
|
252
|
+
* (`evidence.ts`, `checkpoint_ref`). Hashing the bytes we already hold costs
|
|
253
|
+
* nothing and means a relabelled record DANGLES instead of quietly resolving.
|
|
254
|
+
* That is why this function is async.
|
|
255
|
+
*/
|
|
256
|
+
async function groupChains(bundle: unknown): Promise<Group[]> {
|
|
257
|
+
const groups = new Map<string, Group>();
|
|
258
|
+
const events = eventsOf(bundle);
|
|
259
|
+
|
|
260
|
+
for (let order = 0; order < events.length; order++) {
|
|
261
|
+
const entry = events[order];
|
|
262
|
+
const eventId = str(entry?.envelope?.event_id);
|
|
263
|
+
const publicKey = str(entry?.envelope?.public_key);
|
|
264
|
+
// Without a producing key there is no chain identity (§3.2), and without an
|
|
265
|
+
// event id an adapter pointer has nothing to resolve against.
|
|
266
|
+
if (eventId === null || publicKey === null) continue;
|
|
267
|
+
|
|
268
|
+
const decoded = decodeRecord(entry?.record_bytes);
|
|
269
|
+
if (decoded === null) continue;
|
|
270
|
+
const { record, bytes } = decoded;
|
|
271
|
+
|
|
272
|
+
// 1. The record's own chain. `context.chain_key` is present IFF the record
|
|
273
|
+
// is chained (profile §5) — which is what keeps every unchained
|
|
274
|
+
// singleton, all of them carrying `previous_hash: "GENESIS"`, out of one
|
|
275
|
+
// enormous fictional chain that would report a second root per pair.
|
|
276
|
+
const recordChainKey = str(record.context?.chain_key);
|
|
277
|
+
let member: Member;
|
|
278
|
+
let key: string;
|
|
279
|
+
let group: Omit<Group, "members">;
|
|
280
|
+
|
|
281
|
+
if (recordChainKey !== null) {
|
|
282
|
+
const previous = str(record.previous_hash);
|
|
283
|
+
member = {
|
|
284
|
+
eventId,
|
|
285
|
+
selfId: await sha256Hex(bytes),
|
|
286
|
+
prev: previous === null || previous === "GENESIS" ? null : previous,
|
|
287
|
+
seq: null,
|
|
288
|
+
order,
|
|
289
|
+
};
|
|
290
|
+
group = { chainKey: recordChainKey, source: "record", publicKey };
|
|
291
|
+
key = `record ${publicKey} ${recordChainKey}`;
|
|
292
|
+
} else {
|
|
293
|
+
// 2. Fallback only. A record that carries its own chain is never read
|
|
294
|
+
// through its adapter metadata: one record must not land in two
|
|
295
|
+
// chains, and the profile's own field is the one a third party can
|
|
296
|
+
// walk without knowing which SDK wrote it.
|
|
297
|
+
const linkage = adapterLinkage(record);
|
|
298
|
+
if (linkage === null) continue;
|
|
299
|
+
member = { eventId, selfId: eventId, prev: linkage.prev, seq: linkage.seq, order };
|
|
300
|
+
group = {
|
|
301
|
+
chainKey: linkage.chainKey,
|
|
302
|
+
source: "adapter",
|
|
303
|
+
namespace: linkage.namespace,
|
|
304
|
+
publicKey,
|
|
305
|
+
};
|
|
306
|
+
key = `adapter ${linkage.namespace} ${publicKey} ${linkage.chainKey}`;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const existing = groups.get(key);
|
|
310
|
+
if (existing === undefined) groups.set(key, { ...group, members: [member] });
|
|
311
|
+
else existing.members.push(member);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return [...groups.values()];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---- The rule ----------------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
/** By `seq` where both have one, else by position in the bundle. */
|
|
320
|
+
function byChainOrder(a: Member, b: Member): number {
|
|
321
|
+
if (a.seq !== null && b.seq !== null && a.seq !== b.seq) return a.seq - b.seq;
|
|
322
|
+
return a.order - b.order;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function findingsFor(group: Group): ChainFinding[] {
|
|
326
|
+
const { members, chainKey, source, publicKey } = group;
|
|
327
|
+
const of = members.length;
|
|
328
|
+
const base = {
|
|
329
|
+
chainKey,
|
|
330
|
+
source,
|
|
331
|
+
publicKey,
|
|
332
|
+
of,
|
|
333
|
+
...(group.namespace === undefined ? {} : { namespace: group.namespace }),
|
|
334
|
+
};
|
|
335
|
+
const findings: ChainFinding[] = [];
|
|
336
|
+
|
|
337
|
+
const selfIds = new Set(members.map((m) => m.selfId));
|
|
338
|
+
const roots = members.filter((m) => m.prev === null).sort(byChainOrder);
|
|
339
|
+
const dangling = members
|
|
340
|
+
.filter((m): m is Member & { prev: string } => m.prev !== null && !selfIds.has(m.prev))
|
|
341
|
+
.sort(byChainOrder);
|
|
342
|
+
|
|
343
|
+
// A SECOND BEGINNING. One chain has one head per (public_key, chain_key), so
|
|
344
|
+
// two records claiming GENESIS under one key is a fork — the case a link-walk
|
|
345
|
+
// alone reports as CLEAN, because both halves walk back to a legitimate
|
|
346
|
+
// beginning and neither dangles. It is what a crash looks like when the SDK
|
|
347
|
+
// lost its chain head and started over under the same name.
|
|
348
|
+
if (roots.length > 1) {
|
|
349
|
+
findings.push({
|
|
350
|
+
kind: "chain-second-root",
|
|
351
|
+
...base,
|
|
352
|
+
count: roots.length,
|
|
353
|
+
eventIds: roots.map((m) => m.eventId),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// A POINTER THAT LEAVES THIS BUNDLE. Exactly one such pointer is expected and
|
|
358
|
+
// is NOT a finding: a bundle is a window onto a chain that may have begun
|
|
359
|
+
// before this export, so the window's earliest record necessarily points at a
|
|
360
|
+
// predecessor nobody handed us. The allowance is spent only when the chain
|
|
361
|
+
// shows no GENESIS root — a chain that already has its beginning here has no
|
|
362
|
+
// window edge left to explain the next one.
|
|
363
|
+
const unexplained = roots.length === 0 ? dangling.slice(1) : dangling;
|
|
364
|
+
if (unexplained.length > 0) {
|
|
365
|
+
const first = unexplained[0]!;
|
|
366
|
+
findings.push({
|
|
367
|
+
kind: "chain-link-dangling",
|
|
368
|
+
...base,
|
|
369
|
+
count: unexplained.length,
|
|
370
|
+
eventIds: unexplained.map((m) => m.eventId),
|
|
371
|
+
unresolvedPointer: first.prev,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// A HOLE IN THE SEQUENCE, where an adapter numbers its events. Only INTERIOR
|
|
376
|
+
// holes count: a window that starts at seq 7 is a window, not a deletion, and
|
|
377
|
+
// reporting its first six absent slots would bury the one that matters. A
|
|
378
|
+
// repeated seq is a real defect but a different one, and the present-set below
|
|
379
|
+
// is deliberately duplicate-safe rather than count-based so it cannot be
|
|
380
|
+
// mistaken for a gap.
|
|
381
|
+
const sequenced = members.filter((m) => m.seq !== null);
|
|
382
|
+
if (sequenced.length === of && of > 0) {
|
|
383
|
+
const present = new Set(sequenced.map((m) => m.seq as number));
|
|
384
|
+
const low = Math.min(...present);
|
|
385
|
+
const high = Math.max(...present);
|
|
386
|
+
const missing: number[] = [];
|
|
387
|
+
for (let s = low; s <= high; s++) if (!present.has(s)) missing.push(s);
|
|
388
|
+
if (missing.length > 0) {
|
|
389
|
+
const after = sequenced.filter((m) => (m.seq as number) > missing[0]!).sort(byChainOrder)[0];
|
|
390
|
+
findings.push({
|
|
391
|
+
kind: "chain-gap",
|
|
392
|
+
...base,
|
|
393
|
+
count: missing.length,
|
|
394
|
+
eventIds: after === undefined ? [] : [after.eventId],
|
|
395
|
+
firstMissingSeq: missing[0]!,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return findings;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Follow every chain the bundle's records describe, and report the links that
|
|
405
|
+
* do not resolve within it.
|
|
406
|
+
*
|
|
407
|
+
* Reports NOTHING for an intact chain, and nothing for a chain whose records
|
|
408
|
+
* are withheld — a link we could not read is unknown, never an accusation.
|
|
409
|
+
* `chains` is what makes that distinguishable from having read nothing at all.
|
|
410
|
+
*/
|
|
411
|
+
export async function readChains(bundle: unknown): Promise<ChainReading> {
|
|
412
|
+
const groups = await groupChains(bundle);
|
|
413
|
+
const order = (g: Group) => `${g.source} ${g.namespace ?? ""} ${g.chainKey}`;
|
|
414
|
+
groups.sort((a, b) => order(a).localeCompare(order(b)));
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
chains: groups.map((g) => ({
|
|
418
|
+
chainKey: g.chainKey,
|
|
419
|
+
source: g.source,
|
|
420
|
+
...(g.namespace === undefined ? {} : { namespace: g.namespace }),
|
|
421
|
+
publicKey: g.publicKey,
|
|
422
|
+
events: g.members.length,
|
|
423
|
+
})),
|
|
424
|
+
findings: groups.flatMap(findingsFor),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* The house wording for one finding.
|
|
430
|
+
*
|
|
431
|
+
* Deliberately free of verdict vocabulary. Every sentence says what does not
|
|
432
|
+
* resolve IN THIS BUNDLE, because that is the whole of what was checked; none
|
|
433
|
+
* of them says failed, invalid, tampered or complete. A surface with its own
|
|
434
|
+
* voice should read the fields instead — but it must not upgrade the claim.
|
|
435
|
+
*/
|
|
436
|
+
export function renderChainFinding(finding: ChainFinding, subject = "this bundle"): string {
|
|
437
|
+
const where =
|
|
438
|
+
finding.source === "adapter"
|
|
439
|
+
? `chain ${finding.chainKey} (${finding.namespace} metadata)`
|
|
440
|
+
: `chain ${finding.chainKey}`;
|
|
441
|
+
switch (finding.kind) {
|
|
442
|
+
case "chain-link-dangling":
|
|
443
|
+
return (
|
|
444
|
+
`${where}: ${finding.count} of ${finding.of} record(s) point at a predecessor ` +
|
|
445
|
+
`${subject} does not present (first: ${finding.eventIds[0]}, pointer ` +
|
|
446
|
+
`${finding.unresolvedPointer})`
|
|
447
|
+
);
|
|
448
|
+
case "chain-gap":
|
|
449
|
+
return (
|
|
450
|
+
`${where}: ${finding.count} sequence slot(s) between the records ${subject} presents ` +
|
|
451
|
+
`carry no record (first missing seq ${finding.firstMissingSeq}` +
|
|
452
|
+
`${finding.eventIds[0] === undefined ? "" : `, next present: ${finding.eventIds[0]}`})`
|
|
453
|
+
);
|
|
454
|
+
case "chain-second-root":
|
|
455
|
+
return (
|
|
456
|
+
`${where}: ${finding.count} records begin the chain where one chain has one beginning ` +
|
|
457
|
+
`(${finding.eventIds.join(", ")})`
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
}
|