@sanning/proof 0.10.0 → 0.11.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/case.js ADDED
@@ -0,0 +1,1318 @@
1
+ /**
2
+ * One verdict for a CASE, across packs and across organizations (anchor#198).
3
+ *
4
+ * The TypeScript half of `src/sanning_proof/case.py`. Both kernels are held to
5
+ * the same corpus set, `test-vectors/case/`, so neither can pass by agreeing
6
+ * with the other.
7
+ *
8
+ * WHY THIS LAYER EXISTS, AND WHAT IT IS THE CLOSURE FOR
9
+ * =====================================================
10
+ *
11
+ * `envelope-spec.md` 4.1 reads records. `evidence-bundle.md` 5.4 reads one pack
12
+ * against its own declaration. Neither can answer the question an audit team
13
+ * actually asks, and 4.1 (v1.10, A15) and 5.4.2 rule 7 (v1.10) now say so in
14
+ * these words:
15
+ *
16
+ * **A pack a party assembles about itself is not self-proving about what
17
+ * that party LEFT OUT.**
18
+ *
19
+ * `handoffs[]` MAY be empty (5.4.1), so a holder declares a record in
20
+ * `records[]` and claims it nowhere: rule 2 cannot reach it because it is
21
+ * declared, rule 7 cannot reach it because it is unclaimed, and the pack reads
22
+ * `verified` with no deviation, which is the same reading the honest pack gets.
23
+ * The corpus pins that pack as EXPECTED
24
+ * (`test-vectors/declaration/declaration-unclaimed-record-01`) rather than
25
+ * leaving it to be rediscovered. Six repairs treated a property of one-sided
26
+ * evidence as a defect in a rule (proof#137, ratified by Phil 2026-09-22), and
27
+ * the omission happens BEFORE any reader runs, so no rule at either of those
28
+ * layers reaches it.
29
+ *
30
+ * **This module is the closure, and the reason it can be is that it reads
31
+ * several parties' packs at once.** A `handoff.sent` with no counterpart among
32
+ * the records of the case is always NAMED, and when a pack signed by the key
33
+ * that record names is in the case, the reading says that pack holds no
34
+ * counterpart. No party can make a missing counterpart read clean.
35
+ *
36
+ * What it does NOT establish is that a party which presented no pack signed by
37
+ * the named key recorded nothing. Nothing binds a pack's wrapper key to the
38
+ * keys its records are signed with, so a counterparty that hands over its
39
+ * account under any other key, or inside an export an issuer signed, or not at
40
+ * all, gets the reading that says only that no pack in the case is signed by
41
+ * the named key. Proving that a party did NOT record something needs that
42
+ * party's account or a completeness mechanism over its records, and which of
43
+ * those this reading waits for is proof#143.
44
+ *
45
+ * WHAT THAT MEANS FOR HOW THE JOIN IS BUILT
46
+ * =========================================
47
+ *
48
+ * **The case reader does not consult `handoffs[]` to pair anything.** That is
49
+ * not an oversight, it is the whole mechanism. The declaration is written by
50
+ * the party being audited, so a join keyed on it inherits exactly the hole it
51
+ * is meant to close: claim the clean pair, keep the tampered receipt
52
+ * unclaimed, and a declaration-keyed reader never grades the receipt at all.
53
+ * This reader pairs records with records, through `readHandoff`, over the
54
+ * union of every record every pack in the case supplied. `handoffs[]` is read
55
+ * for ONE thing and one only: to name a record its own holder DECLARED and
56
+ * claimed nowhere (`RECORD_UNCLAIMED`), which is the pinned vector above
57
+ * becoming visible. That claim check runs over every record a pack declares,
58
+ * and not only over the ones 4.1 governs, because "is this a conformant
59
+ * hand-off record" is an answer the audited party writes.
60
+ *
61
+ * **Matching is on hashes and identifiers alone and NEVER on tenancy**
62
+ * (anchor#198, and DL-60: producer identity gates what the plane accepts and
63
+ * never whether evidence verifies). The union pile is not partitioned by pack,
64
+ * by issuer, by `tenant_id` or by organization anywhere in this module, so a
65
+ * pair assembled by two organizations reads exactly as a pair assembled by
66
+ * one. There is no code path here that reads a tenant.
67
+ *
68
+ * THE DEFECT CLASS THIS MODULE IS WRITTEN AGAINST
69
+ * ===============================================
70
+ *
71
+ * Seventeen findings on this program share one generator: **a deviation
72
+ * designed as a comparison between two present things, where the deviation
73
+ * itself removes one of them, so the code takes the "nothing to compare"
74
+ * branch written for the honest case.** A rule shaped that way reads the
75
+ * absence of a signal as "fine" rather than as "I could not tell", and a real
76
+ * deviation grades clean.
77
+ *
78
+ * Acceptance criterion 3 of anchor#198 is made of exactly that shape: *a
79
+ * one-sided hand-off is an exception, not a failure*. So the four rules below
80
+ * are structural and are the thing to preserve in any change here.
81
+ *
82
+ * 1. **Every exception is NAMED and appears in the output, and every exception
83
+ * contributes a verdict.** There is no path through this module that
84
+ * removes a record, a pack or a claim from the reading. An empty
85
+ * `exceptions` is the ONLY shape a reader may read as clean, and `readCase`
86
+ * is invariant: `verified` is reachable only when that array is empty.
87
+ * 2. **Nothing a party writes decides whether grading happens.** No graded
88
+ * value is part of any key that selects a code path. The pile is everything
89
+ * every pack supplied; there is no filter.
90
+ * 3. **Every field that selects a code path and is chosen by the party being
91
+ * audited has an accept-set, and an unknown value goes to a named exception
92
+ * rather than to silence.** `body_type` is `CASE_BODY_TYPES`;
93
+ * `declaration_version` is `checkDeclaration`'s; `event_type` and
94
+ * `event.counterparty_public_key` are 4.1's, through `readHandoffs`. A pack
95
+ * that supplies zero records is `PACK_HOLDS_NO_RECORDS` and a case with no
96
+ * hand-off record in it at all is `CASE_HOLDS_NO_HANDOFF_RECORDS`, because
97
+ * "nothing to grade" is never "nothing is wrong".
98
+ * 4. **The 4.1 predicates are CALLED, never restated.** `readHandoffs`,
99
+ * `normalizedSigningKey` and `packRecords` are the one implementation of
100
+ * each rule. A validation copied beside the one that enforces it drifts,
101
+ * and a copy that is looser blesses a value the real one then rejects.
102
+ *
103
+ * EVERY EARLY EXIT IN THIS MODULE, AND WHAT IT SKIPS
104
+ * ==================================================
105
+ *
106
+ * Three defects on this layer were one shape: an exit between a record
107
+ * entering the pile and a check running over it, with the audited party
108
+ * choosing whether the exit was taken. So the exits are enumerated here rather
109
+ * than left to be rediscovered a fourth time. `src/sanning_proof/case.py` has
110
+ * the same exits in the same order, and the pull request that added this list
111
+ * carries the same table.
112
+ *
113
+ * 1. **No packs** (`return`). Skips everything. Reached by the reader, who
114
+ * chose what to hand in. Named: `CASE_HOLDS_NO_PACKS`.
115
+ * 2. **A wrapper key this reader cannot read** (`continue`). Skips the bundle
116
+ * verification, the declaration check and every record in the pack. The
117
+ * holder writes the key, so it steers here freely, and the pack is NAMED
118
+ * (`PACK_KEY_UNREADABLE`). Both bundle verifiers now refuse every such
119
+ * spelling in the same words (proof#142), so this exit and exit 3 agree on
120
+ * which packs they take; it stays first because it names WHY. While any
121
+ * pack takes this exit, a one-sided record whose named key signed no pack
122
+ * reads `COUNTERPART_ABSENT_SIGNER_UNKNOWN` and never the innocent
123
+ * `COUNTERPART_ABSENT`.
124
+ * 3. **The wrapper does not hold** (`continue`): `signatureOk` and
125
+ * `bodyHashOk` are not both true. Skips the declaration check and every
126
+ * record in the pack, and the key it claims is never counted as a pack
127
+ * signer. **This is the forger's case and the only exit that turns on
128
+ * provenance**: those two fields are the whole of what ties a pack to a
129
+ * key. Reached by whoever built the pack, and by anyone who alters its
130
+ * unsigned `signature` field, which is indistinguishable from a forgery.
131
+ * Named: `PACK_DOES_NOT_VERIFY`, and it moves one-sided records to
132
+ * `COUNTERPART_ABSENT_SIGNER_UNKNOWN` exactly as exit 2 does.
133
+ *
134
+ * A pack whose wrapper HOLDS is never sent here, whatever is wrong inside
135
+ * it (gate 3 on proof#140): its key is counted, its own bundle verdict is
136
+ * `PACK_BUNDLE_NOT_VERIFIED` when that verdict is not `verified`, and each
137
+ * record is graded on its own envelope below.
138
+ * 3b. **The bundle verdict is `malformed`** (`continue`, after exit 3 and after
139
+ * `PACK_BUNDLE_NOT_VERIFIED`). The body is one the verifier could not read,
140
+ * so it graded no record. The key is already counted.
141
+ * 4. **`body_type` outside `CASE_BODY_TYPES`** (`continue`). Skips the
142
+ * declaration check and every record in the pack. The holder writes
143
+ * `body_type`, so the holder steers here freely, which is why it is named
144
+ * every time (`PACK_BODY_TYPE_UNKNOWN`) and why the pack's wrapper key is
145
+ * counted ABOVE it: a counterparty cannot leave the damning counterpart
146
+ * line by choosing a body (F3). `sanning.evidence.export/v2` lands here and
147
+ * is not unwrapped.
148
+ * 5. **No records** (`continue`). Skips the declaration check, over nothing.
149
+ * The holder steers here by handing over an empty `events[]`. Named:
150
+ * `PACK_HOLDS_NO_RECORDS`; the wrapper key is already counted.
151
+ * 5b. **A record under a quarantined profile** and 5c. **a record whose own
152
+ * envelope does not verify** (`continue`). Each skips 4.1 for that record,
153
+ * its candidacy for any other record, and the claim check, because 4.1
154
+ * compares only envelopes that verified (`HeldRecord`). Named:
155
+ * `RECORD_PROFILE_QUARANTINED` (`incomplete`, pending DL-63) and
156
+ * `RECORD_ENVELOPE_DOES_NOT_VERIFY` (`failed`). Neither removes a named
157
+ * failure from another record: the other records of the pack are still
158
+ * graded, and pairs still form over every record that verified.
159
+ * 6. **`event_bytes` withheld** and 7. **`event_bytes` unreadable**
160
+ * (`continue`). Each skips 4.1 for that record, its candidacy for any other
161
+ * record, and the claim check. The holder steers into both by what it
162
+ * discloses. Each is named in the claim check's place
163
+ * (`RECORD_BYTES_WITHHELD` `incomplete`, `RECORD_BYTES_UNREADABLE`
164
+ * `malformed`), so neither buys a cleaner reading than an unclaimed record
165
+ * would get, and neither decides which counterpart kind another record
166
+ * gets, since that reads wrapper keys.
167
+ *
168
+ * Not exits: a record whose envelope verifies and whose content does not
169
+ * match its commitment is `RECORD_CONTENT_MISMATCH`, and one that fails
170
+ * another check inside its pack is `RECORD_DOES_NOT_VERIFY_IN_ITS_PACK`.
171
+ * Both are named and both STAY in the pile, so neither unsigned content
172
+ * handed in beside a pack nor a broken inclusion proof removes a record
173
+ * from pairing.
174
+ * 8. **`not_a_handoff_record`** (`continue`). Skips the hand-off count and the
175
+ * counterpart, ambiguity and pair reporting, which 4.1 does not give a
176
+ * record outside its scope. The holder steers here by respelling a field.
177
+ * It no longer skips the claim check, which runs ABOVE it because it asks
178
+ * what the pack SAID and not what the record is (F1).
179
+ * 9. **THIS record is malformed** (`continue`). Skips the counterpart,
180
+ * ambiguity and pair reporting, and 4.1 computed none of them for a
181
+ * malformed subject, so nothing is lost. A party steers here only by
182
+ * breaking a record it signed, and the record is named `RECORD_MALFORMED`.
183
+ * A well formed record whose CANDIDATE is malformed does not take this
184
+ * exit: it is `COUNTERPART_MALFORMED` and the reporting below still runs
185
+ * (F2).
186
+ * 10. **`counterpart_absent`** (`continue`). Skips the ambiguity and pair
187
+ * reporting, and there is nothing to skip: 4.1 emits that finding only
188
+ * for an EMPTY candidate list. Which of the three counterpart kinds it
189
+ * gets reads the keys of the packs that verified, counted before exit 4,
190
+ * and whether any pack took exit 2 or 3.
191
+ *
192
+ * Not an exit, and listed because it is one more place this shape could
193
+ * hide: the claim check runs only where the pack's declaration is READABLE. An
194
+ * absent or unreadable one is named once for the pack (`PACK_UNDECLARED`,
195
+ * `PACK_DECLARATION_UNREADABLE`) rather than once per record.
196
+ *
197
+ * THE VERDICT VOCABULARY IS CLOSED
198
+ * ================================
199
+ *
200
+ * `verified`, `failed`, `partial`, `mixed`, `incomplete` and `malformed`,
201
+ * every one of them already in use (`evidence-bundle.md` 3 and
202
+ * `PACK_VERDICTS`). **No new word is coined anywhere in this module**, and
203
+ * `CASE_VERDICTS` is asserted against `PACK_VERDICTS` by the test suite so a
204
+ * future addition here cannot invent one quietly. Everything this module has
205
+ * to say that is not one of those six is an EXCEPTION KIND reported beside the
206
+ * verdict, exactly as `counterpart_absent` and `not_a_handoff_record` are
207
+ * findings beside a verdict in 4.1.
208
+ *
209
+ * A case with more than one outcome is `mixed` (anchor#198, criterion 4).
210
+ *
211
+ * WHAT THIS READER DOES NOT CLAIM
212
+ * ===============================
213
+ *
214
+ * * **Not that the case is complete.** Joining is voluntary. A counterparty
215
+ * that never handed over a pack leaves `COUNTERPART_ABSENT`, which is an
216
+ * exception and never a failure. It says that no pack in the case is signed
217
+ * by the named key, and nothing about what that party recorded.
218
+ * * **Not that a party which presented no pack under the named key recorded
219
+ * nothing.** See the top of this comment, and proof#143.
220
+ * * **Not that two organizations are independent.** One team holding both keys
221
+ * proves which agent said what, not that two parties agreed (4.1, 5.4.4).
222
+ * * **Not that a `verified` case has no other problem.** It reads hand-offs.
223
+ * It runs `verifyEvidenceBundle` over every pack itself, offline and with no
224
+ * gateways, and a pack whose own bundle verdict is not `verified` is named
225
+ * with that verdict and its reasons, so the case never reads above its
226
+ * weakest pack. On-chain re-fetch and `gradePack` are a caller's to run.
227
+ *
228
+ * THE COST OF 4.1's "GRADE EVERY CANDIDATE", INHERITED HERE AND STATED RATHER
229
+ * THAN LEFT TO BE FOUND (proof#134, ratified by Phil 2026-09-22). A reader
230
+ * whose pile holds two or more records of the complementary type signed by one
231
+ * counterparty key cannot reach `verified`: the other exchanges' records are
232
+ * candidates too, they commit to other messages, and they fail. One agent
233
+ * sending two messages to the same counterparty inside one case lands there,
234
+ * and it surfaces as `COUNTERPART_AMBIGUOUS` beside the failure. An
235
+ * alternating exchange (A sends, B replies) does NOT, because a reply is
236
+ * signed by the other key and is the other type. Narrowing that here with
237
+ * `handoffs[]` is precisely the move that would reopen proof#137's door.
238
+ */
239
+ import { checkDeclaration, declarationClaims, packRecords, } from "./declaration.js";
240
+ import { NOT_A_HANDOFF_FINDING, normalizedSigningKey, readHandoffs, recordIsMalformed, } from "./handoff.js";
241
+ import { verifyEvidenceBundle } from "./evidence.js";
242
+ // ---- The vocabulary ---------------------------------------------------------
243
+ /**
244
+ * The verdicts a case can reach. Every one is already in use in
245
+ * `evidence-bundle.md` 3 or `PACK_VERDICTS`, and anchor#198 says in terms that
246
+ * no new verdict word is introduced anywhere.
247
+ */
248
+ export const CASE_VERDICTS = [
249
+ "verified",
250
+ "failed",
251
+ "partial",
252
+ "mixed",
253
+ "incomplete",
254
+ "malformed",
255
+ ];
256
+ /**
257
+ * anchor#198 says in terms that no new verdict word is introduced anywhere, and
258
+ * this line is what ENFORCES it rather than asserting it in prose.
259
+ *
260
+ * `tsc` refuses to compile if any member of `CASE_VERDICTS` is not already a
261
+ * `PackVerdict`, so a seventh word added here fails the BUILD rather than
262
+ * reaching a test that might not cover it. The Python half asserts the same
263
+ * containment at run time against `grading.PACK_VERDICTS`, which is that
264
+ * language's equivalent gate.
265
+ */
266
+ const _caseVerdictsAreSharedVocabulary = [...CASE_VERDICTS];
267
+ void _caseVerdictsAreSharedVocabulary;
268
+ /**
269
+ * The two pack bodies that carry `events[]` and may carry a `declaration`
270
+ * (`evidence-bundle.md` 5.4.1). This is an ACCEPT-SET and not a hint.
271
+ *
272
+ * `body_type` selects a code path and is written by the party assembling the
273
+ * pack. Without this set, a pack whose records live under a key this reader
274
+ * does not know yields zero records, contributes nothing, and lets the rest of
275
+ * the case read clean over it: the exact defect class this module is written
276
+ * against. An unknown value is `PACK_BODY_TYPE_UNKNOWN`.
277
+ *
278
+ * **`sanning.evidence.export/v2` is NOT in this set and is NOT unwrapped.** It
279
+ * verifies as a bundle and carries a whole `sanning.anchor.trace/v3` pack under
280
+ * `body.source_bundle`, so its records do not reach the pile and the export is
281
+ * named `PACK_BODY_TYPE_UNKNOWN`, `incomplete`. Reading inside it changes which
282
+ * packs this reader grades, so it is a decision of its own. A reader who needs
283
+ * the records hands in the source bundle as a pack. The export's wrapper key is
284
+ * still read, so wrapping a pack in one does not change which counterpart kind
285
+ * another record gets.
286
+ */
287
+ export const CASE_BODY_TYPES = [
288
+ "sanning.anchor.trace/v3",
289
+ "sanning.stamp.evidence/v2",
290
+ ];
291
+ /**
292
+ * The envelope profiles the case reader reads records under: the ratified
293
+ * accept-set (`envelope-spec.md` 5) minus every profile under quarantine in
294
+ * `cross-kernel/accept-set.json`. A test in each kernel derives both arrays
295
+ * from that file, and a test here pins this one equal to this kernel's own
296
+ * `ACCEPTED_SPEC_MAJORS`, which is what `verifyEvidenceBundle` reads under:
297
+ * the Python kernel narrows its verifier to this set for the case reader, and
298
+ * this kernel's verifier already reads exactly it. If this kernel ever admits
299
+ * a quarantined profile, that test fails, and the case reader has to narrow
300
+ * its verifier the way the Python one does before it can pass.
301
+ */
302
+ export const CASE_RECORD_PROFILES = ["sanning.agent/v1", "sanning.events/v2"];
303
+ /** The ratified profiles under quarantine, pending DL-63. */
304
+ export const CASE_QUARANTINED_PROFILES = ["sanning.mlflow/v1"];
305
+ // ---- The exceptions ---------------------------------------------------------
306
+ //
307
+ // Each of these is a NAME for something a reader would otherwise have to infer
308
+ // from an absence. None of them is a verdict word.
309
+ /**
310
+ * A conformant hand-off record with no counterpart among the records this
311
+ * reader read, where every pack in the case verified under a key this reader
312
+ * can read and NO pack in the case is signed by the key the record names.
313
+ * **An exception, never a failure** (anchor#198, criterion 3).
314
+ *
315
+ * That is ALL it says. It does not say the counterparty's account is absent,
316
+ * and it does not say nothing was tampered with: a counterparty that signs its
317
+ * own pack under a second key, or hands over an export an issuer signed,
318
+ * reaches this reading too, and nothing offline binds a pack's key to the keys
319
+ * of the records inside it (proof#143). The rendered text states the fact and
320
+ * stops.
321
+ *
322
+ * It says nothing about RECORDS signed by that key either. A party may hold records
323
+ * its counterparty signed, and those are still graded as candidates, but they
324
+ * are not the counterparty's account of itself, so they do not move this
325
+ * record to `COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS`.
326
+ */
327
+ export const COUNTERPART_ABSENT = "counterpart_absent";
328
+ /**
329
+ * THE CLOSURE anchor#198 exists for.
330
+ *
331
+ * A conformant hand-off record with no counterpart, where a pack that VERIFIES
332
+ * and is SIGNED by the key this record names is in the case, and not one
333
+ * record this reader could read is this record's counterpart. It states that a
334
+ * pack signed by the named key is present and holds no counterpart this reader
335
+ * could read, and nothing more. 4.1 (v1.10, A15) and 5.4.2 rule 7 (v1.10) both point here, and
336
+ * `declaration/declaration-unclaimed-record-01` is the pack that reaches this
337
+ * line.
338
+ *
339
+ * **"Its own account" is read from the key that signed the PACK, and never
340
+ * from the keys that signed records.** A pack's wrapper key signs `body_hash`
341
+ * and `body` carries the declaration (`evidence-bundle.md` 5.4, "Who signs
342
+ * it"), so a wrapper signed by B is provenance the accusing party cannot forge
343
+ * without B's private key. Records signed by B that A merely HOLDS are not B's
344
+ * account of itself: keyed on record keys, one pack from A plus one unrelated
345
+ * record B signed at some other time reached this line when B had handed over
346
+ * nothing at all.
347
+ *
348
+ * What it does NOT prove, stated so no surface claims it: that B handed the
349
+ * pack over for THIS case. A pack B signed for some other purpose and a third
350
+ * party replayed reads the same, because the only values that could say
351
+ * otherwise (the declaration's `case_id` and `period`) are written by B and
352
+ * would let B leave this line by writing them differently. The cost the other
353
+ * way: a pack B assembles under any key other than the one its records name, a
354
+ * per-organization hand-over key (anchor#196) included, reads
355
+ * `COUNTERPART_ABSENT`, because nothing in the case binds that key to B. That
356
+ * reading is still true: no pack signed by the named key is here. So a
357
+ * counterparty can AVOID this line by choosing the key it signs under; what it
358
+ * cannot do is make the missing counterpart read clean.
359
+ *
360
+ * It is deliberately NOT also conditioned on a record signed by B being in the
361
+ * pile. B's pack can hold none this reader reads (a body it does not unwrap,
362
+ * bytes B withheld, an empty `events[]`), and each of those is a choice B makes
363
+ * and is named on its own. A second conjunct on them hands B the innocent line
364
+ * for making one.
365
+ *
366
+ * It is a DISTINCT kind from `COUNTERPART_ABSENT` and not a louder spelling of
367
+ * it. Collapsing the two would be the defect class again: "the counterparty
368
+ * never joined" and "the counterparty joined and its own account does not
369
+ * contain this hand-off" are the honest reading and the damning one, and one
370
+ * name for both lets the second read as the first.
371
+ *
372
+ * It is still an EXCEPTION and not a failure, which criterion 3 requires and
373
+ * which is also the honest call: the counterparty's pack is a selection over
374
+ * its own interval, and a hand-off outside that interval is legitimately not
375
+ * in it. What this names is that the question can now be ASKED of the other
376
+ * party's own evidence, which is what no single pack could ever support.
377
+ */
378
+ export const COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS = "counterpart_absent_from_counterparty_records";
379
+ /**
380
+ * A conformant hand-off record with no counterpart, no VERIFIED pack in the
381
+ * case signed by the key it names, and at least one pack in the case that this
382
+ * reader could not tie to a key: its wrapper key is unreadable
383
+ * (`PACK_KEY_UNREADABLE`) or it does not verify (`PACK_DOES_NOT_VERIFY`).
384
+ * Whether a pack signed by the named key is in the case is UNKNOWN, so neither
385
+ * of the other two kinds is true.
386
+ *
387
+ * The gate on the previous version of this reader showed why it exists. A
388
+ * counterparty that signed its own pack under `" " + key` got
389
+ * `PACK_KEY_UNREADABLE` for the pack and the innocent `COUNTERPART_ABSENT` for
390
+ * the record, and the output contradicted itself: whose pack it was is
391
+ * UNKNOWN, and then no pack is signed by the named key. The second sentence
392
+ * was not established.
393
+ *
394
+ * `incomplete`, like the other two one-sided kinds: "I could not tell".
395
+ */
396
+ export const COUNTERPART_ABSENT_SIGNER_UNKNOWN = "counterpart_absent_signer_unknown";
397
+ /**
398
+ * More than one candidate counterpart (4.1, and proof#134's stated cost). The
399
+ * reading is the worst of them and the ambiguity is named: a reader permitted
400
+ * to take one match can be handed an honest decoy beside a tampered record.
401
+ */
402
+ export const COUNTERPART_AMBIGUOUS = "counterpart_ambiguous";
403
+ /**
404
+ * A formed pair that failed a check. Carries which checks, from 4.1's own
405
+ * fixed order, so a reader is told the record AND the check.
406
+ */
407
+ export const PAIR_FAILED = "pair_failed";
408
+ /**
409
+ * THIS record is wrong: 4.1 governs it and `recordIsMalformed` says so of the
410
+ * record itself. A statement about the record this exception names, and about
411
+ * no other.
412
+ */
413
+ export const RECORD_MALFORMED = "record_malformed";
414
+ /**
415
+ * A CANDIDATE counterpart of this record is malformed, and this record is not.
416
+ *
417
+ * 4.1 grades every candidate and reports the worst of them, and `malformed`
418
+ * outranks `failed`, so one junk record signed by the counterparty's key makes
419
+ * the reading of a perfectly well formed record `malformed` too. Filed under
420
+ * `RECORD_MALFORMED`, that read as "this record is not a well formed hand-off
421
+ * record" about the HONEST record, and the branch it took skipped the
422
+ * counterpart, ambiguity and `failedChecks` reporting entirely: one junk record
423
+ * both blamed the wrong party and stripped a named `message_hash` failure out
424
+ * of the case. That is proof#133 ruling 6, which 4.1 closed by keeping the
425
+ * UNION of `failedChecks`, reappearing one layer out because this layer
426
+ * discarded that union.
427
+ *
428
+ * A record's own conformance and its candidate's are different facts and do
429
+ * not share a kind. This one is `incomplete` and not `malformed`: the proven
430
+ * defect belongs to the candidate, which carries its own `RECORD_MALFORMED`
431
+ * line, and what is true of THIS record is that its pairing could not be
432
+ * determined. Contributing `malformed` here would attribute the candidate's
433
+ * defect to the record a party being accused did not write.
434
+ */
435
+ export const COUNTERPART_MALFORMED = "counterpart_malformed";
436
+ /**
437
+ * A record that its own pack DECLARES and its own pack's READABLE declaration
438
+ * claims in no `handoffs[]` entry. This is proof#137's door, named at the layer
439
+ * that can see it: the holder declared the record and claimed it nowhere, so
440
+ * neither 5.4.2 rule 2 nor rule 7 reaches it and the pack reads `verified`.
441
+ *
442
+ * **It turns on what the PACK said about the record, and never on whether 4.1
443
+ * governs it.** Gating it on "is this a conformant hand-off record" put it
444
+ * behind a value the audited party writes: the same second receipt, respelled
445
+ * with an `ff` ligature in `event_type` and a Cyrillic `е` in
446
+ * `counterparty_public_key`, left 4.1's scope, took the `not_a_handoff_record`
447
+ * branch, and bought a case reading of `verified` with zero exceptions where
448
+ * the conformant spelling read `mixed` with four. proof#136 ratified that a
449
+ * respelling buys a DIFFERENT LINE rather than no line; at this layer it
450
+ * bought no line at all, and a strictly better outcome than honesty.
451
+ *
452
+ * It is not an accusation. `handoffs[]` MAY be empty and an unclaimed record
453
+ * may be entirely innocent. What it is, is the end of the silence: the reader
454
+ * cannot tell whether an unclaimed record is part of the hand-over the holder
455
+ * meant to give or one it is keeping out of its own account, and that is "I
456
+ * could not tell", which is why it carries `incomplete` and not `verified`.
457
+ *
458
+ * THE COST, stated rather than left to be found: a pack that declares records
459
+ * which are not hand-off sides at all, an ordinary log line among them, is
460
+ * named once per such record, so a case holding one cannot read `verified`.
461
+ * That is the price of a rule with no branch the holder selects, and the only
462
+ * narrower rule available keys on a value the holder writes. A surface that
463
+ * wants the two apart reads `CaseRecordReading.findings`, where
464
+ * `not_a_handoff_record` still says which is which.
465
+ */
466
+ export const RECORD_UNCLAIMED = "record_unclaimed_by_its_pack";
467
+ /**
468
+ * The pack supplied no `event_bytes` for a record, so this reader cannot see
469
+ * what the record IS in either direction.
470
+ *
471
+ * `event_bytes` is OPTIONAL (5.1, 5.3) and withholding it is minimum
472
+ * disclosure, which the corpus calls a customer's right and not a defect
473
+ * (proof#137, defect 3), so this is not a deviation and does not fail. It is
474
+ * still SAID, because a withheld record is exactly where a missing counterpart
475
+ * would hide, and a reader that dropped it silently would read a trimmed pack
476
+ * as an honest one.
477
+ */
478
+ export const RECORD_BYTES_WITHHELD = "record_bytes_withheld";
479
+ /**
480
+ * The pack SUPPLIED `event_bytes` this reader cannot read as
481
+ * `JCS(event record)` (5.4.2 rule 7's parser rule, one implementation in
482
+ * `packRecords`). Bytes the holder handed over that do not decode are a defect
483
+ * in what was handed over, not an absence, so this one is `malformed`.
484
+ */
485
+ export const RECORD_BYTES_UNREADABLE = "record_bytes_unreadable";
486
+ /**
487
+ * The pack deviates from its own declaration (5.4.2). A proven mismatch, so it
488
+ * fails: a case cannot be clean over a pack that is proven to be trimmed.
489
+ */
490
+ export const PACK_DECLARATION_DEVIATES = "pack_declaration_deviates";
491
+ /**
492
+ * The pack and the UNSIGNED content handed in beside it do not line up (5.4.2
493
+ * rules 3 and 4, proof#147, ratified by Phil 2026-09-22). A record the pack
494
+ * declares `disclosed` for which no bytes reached this reader, or one it
495
+ * declares `content_withheld` for which bytes reached this reader from the
496
+ * side-content map alone.
497
+ *
498
+ * `incomplete` and never `failed`, which is the whole point of the kind. 5.4
499
+ * grades a pack's declaration partly against content the READER was handed,
500
+ * and whoever assembles a case fills that map in. Under
501
+ * `PACK_DECLARATION_DEVIATES` an accuser could put an intact pack into a
502
+ * `failed` case WITH NO KEY AT ALL: supply bytes for a record its holder
503
+ * declared withheld, or drop the bytes of one it declared disclosed. The pack
504
+ * was intact, its wrapper verified, and its holder had done nothing. This is
505
+ * the mirror of proof#143, where a counterparty makes ITSELF look better by
506
+ * choosing what it hands over.
507
+ *
508
+ * It is emitted BESIDE `PACK_DECLARATION_DEVIATES` where a pack does both,
509
+ * because the two say different things about different records.
510
+ */
511
+ export const PACK_DECLARATION_CONTENT_DEPENDENT = "pack_declaration_content_dependent";
512
+ /**
513
+ * The pack carries a `declaration` this reader cannot read (5.4.3): an unknown
514
+ * `declaration_version`, an absent or `null` one, a non-object, or one that
515
+ * does not satisfy 5.4.1. `declaration_version` is chosen by the same party
516
+ * that assembles the pack and has no accept-set of its own, so this is UNKNOWN
517
+ * and never clean.
518
+ */
519
+ export const PACK_DECLARATION_UNREADABLE = "pack_declaration_unreadable";
520
+ /**
521
+ * The pack carries no `declaration` member at all, so it never said what it
522
+ * holds. `partial` is 5.4.3's own word for a claim nobody made: never
523
+ * `verified`, because there is nothing to check the pack against, and never
524
+ * `failed`, because nothing is wrong.
525
+ */
526
+ export const PACK_UNDECLARED = "pack_undeclared";
527
+ /**
528
+ * The pack's `body_type` is not one this reader knows to carry `events[]`
529
+ * (`CASE_BODY_TYPES`).
530
+ */
531
+ export const PACK_BODY_TYPE_UNKNOWN = "pack_body_type_unknown";
532
+ /**
533
+ * The pack carries a body this reader knows and no records in it. A pack that
534
+ * contributes nothing is named rather than passed over.
535
+ */
536
+ export const PACK_HOLDS_NO_RECORDS = "pack_holds_no_records";
537
+ /**
538
+ * The key the pack's wrapper names is not one this reader can read as a key
539
+ * (`normalizedSigningKey` returns nothing for it), so who signed the pack is
540
+ * UNKNOWN. The pack is not verified and none of its records are read, and
541
+ * every one-sided record whose named key signed no verified pack reads
542
+ * `COUNTERPART_ABSENT_SIGNER_UNKNOWN`.
543
+ *
544
+ * Found by constructing it, while enumerating this module's exits: the wrapper
545
+ * key is the ONE input that separates the two counterpart kinds, and a key this
546
+ * reader could not read used to drop out of that set in silence. The Python
547
+ * `verify_evidence_bundle` accepts a wrapper `public_key` with ASCII
548
+ * whitespace inside it or around it (`bytes.fromhex` skips it; this kernel's
549
+ * verifier rejects it), so a counterparty that signed its own pack under
550
+ * `" " + key` handed a Python reader a pack that verified, contributed no key,
551
+ * and moved the subject from `COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS` to
552
+ * the innocent `COUNTERPART_ABSENT`. The same repair 4.1 made for a RECORD
553
+ * whose envelope key it cannot read (proof#133, fourth hunt): a key a reader
554
+ * holds and cannot read is named, never treated as absent. It is `incomplete`
555
+ * rather than `malformed` because whether the spelling verifies is the envelope
556
+ * layer's call (proof#135), and this reader only knows it cannot read it.
557
+ */
558
+ export const PACK_KEY_UNREADABLE = "pack_key_unreadable";
559
+ /**
560
+ * The pack does not verify as a bundle: `verifyEvidenceBundle`, run here
561
+ * offline over the pack and its side content, returned `failed` or
562
+ * `malformed`. None of its records are read, because a record from it would
563
+ * reach 4.1 without the verified envelope `HeldRecord` requires, and the key
564
+ * its wrapper claims is never counted as a pack signer: an unverified wrapper
565
+ * claiming the counterparty's key is exactly what an accusing party would
566
+ * forge.
567
+ *
568
+ * `partial` is not this exception. `partial` means the wrapper signature and
569
+ * `body_hash` held and part of the body could not be determined, which is what
570
+ * an unknown body or withheld bytes produce, and those are named on their own.
571
+ *
572
+ * `failed`: a pack in the case is proven broken.
573
+ */
574
+ export const PACK_DOES_NOT_VERIFY = "pack_does_not_verify";
575
+ /**
576
+ * The pack's wrapper HOLDS and its own bundle verdict, from
577
+ * `verifyEvidenceBundle` run here offline over the pack and the content handed
578
+ * in beside it, is not `verified`. The exception carries that verdict
579
+ * (`status`) and the verifier's reasons (`reasons`: its bundle errors, then its
580
+ * stamp errors, then its checkpoint errors, each as the verifier wrote it), and
581
+ * it CONTRIBUTES that verdict, so the case verdict never reads above its
582
+ * weakest pack. Gate 3 on proof#140: two unwitnessed (`partial`) packs read as
583
+ * a `verified` case with no exception. It does not say who is at fault:
584
+ * content handed in beside a pack is not signed by its holder.
585
+ */
586
+ export const PACK_BUNDLE_NOT_VERIFIED = "pack_bundle_not_verified";
587
+ /**
588
+ * A record in a pack whose wrapper holds, whose OWN envelope does not verify
589
+ * (`EventResult.envelopeOk` is false). Not read by 4.1, and NAMED with the
590
+ * verifier's reasons. `failed`: a pack its holder signed carries a record that
591
+ * is proven not to verify. As a reason to drop the whole pack it let the
592
+ * counterparty add one junk event to its own pack, re-seal it, and strip a
593
+ * proven `message_hash` failure out of the case (gate 3 on proof#140).
594
+ */
595
+ export const RECORD_ENVELOPE_DOES_NOT_VERIFY = "record_envelope_does_not_verify";
596
+ /**
597
+ * A record whose envelope verifies and which fails another check the bundle
598
+ * verifier grades it on inside its pack (its inclusion proof, its join to a
599
+ * checkpoint or stamp, a retired field name). NAMED with the verifier's
600
+ * reasons, `failed`, and the record STAYS in the pile: a holder that could
601
+ * remove its own tampered receipt from pairing by breaking that receipt's
602
+ * inclusion proof would be choosing its own reading.
603
+ */
604
+ export const RECORD_DOES_NOT_VERIFY_IN_ITS_PACK = "record_does_not_verify_in_its_pack";
605
+ /**
606
+ * A record whose envelope verifies and whose disclosed content does not hash to
607
+ * the `content_hash` it commits to (`EventResult.contentOk` is false). `failed`,
608
+ * and the record STAYS in the pile: content beside a pack is unsigned and often
609
+ * supplied by whoever assembled the case, and gate 3 on proof#140 showed that
610
+ * party erasing an honest verified pair by swapping it.
611
+ */
612
+ export const RECORD_CONTENT_MISMATCH = "record_content_mismatch";
613
+ /**
614
+ * A record signed under a ratified profile that is under quarantine
615
+ * (`CASE_QUARANTINED_PROFILES`, pending DL-63). Not read, in any kernel.
616
+ * `incomplete`: whether it verifies is what the kernels disagree about.
617
+ */
618
+ export const RECORD_PROFILE_QUARANTINED = "record_profile_quarantined";
619
+ /** The reader was handed no packs at all. */
620
+ export const CASE_HOLDS_NO_PACKS = "case_holds_no_packs";
621
+ /**
622
+ * Every pack was read and not one hand-off record came out of any of them.
623
+ * There is no hand-off to have a verdict about, and saying `verified` over that
624
+ * would be the defect class in its purest form.
625
+ */
626
+ export const CASE_HOLDS_NO_HANDOFF_RECORDS = "case_holds_no_handoff_records";
627
+ /**
628
+ * Every exception this reader can report, as a closed set. A surface may render
629
+ * them and a test may assert over them; nothing outside this array is emitted.
630
+ */
631
+ export const CASE_EXCEPTION_KINDS = [
632
+ COUNTERPART_ABSENT,
633
+ COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS,
634
+ COUNTERPART_ABSENT_SIGNER_UNKNOWN,
635
+ COUNTERPART_AMBIGUOUS,
636
+ COUNTERPART_MALFORMED,
637
+ PAIR_FAILED,
638
+ RECORD_MALFORMED,
639
+ RECORD_UNCLAIMED,
640
+ RECORD_BYTES_WITHHELD,
641
+ RECORD_BYTES_UNREADABLE,
642
+ PACK_DECLARATION_DEVIATES,
643
+ PACK_DECLARATION_CONTENT_DEPENDENT,
644
+ PACK_DECLARATION_UNREADABLE,
645
+ PACK_UNDECLARED,
646
+ PACK_BODY_TYPE_UNKNOWN,
647
+ PACK_HOLDS_NO_RECORDS,
648
+ PACK_KEY_UNREADABLE,
649
+ PACK_DOES_NOT_VERIFY,
650
+ PACK_BUNDLE_NOT_VERIFIED,
651
+ RECORD_ENVELOPE_DOES_NOT_VERIFY,
652
+ RECORD_DOES_NOT_VERIFY_IN_ITS_PACK,
653
+ RECORD_CONTENT_MISMATCH,
654
+ RECORD_PROFILE_QUARANTINED,
655
+ CASE_HOLDS_NO_PACKS,
656
+ CASE_HOLDS_NO_HANDOFF_RECORDS,
657
+ ];
658
+ /**
659
+ * What each exception kind contributes to the case verdict. It is a TABLE and
660
+ * not a chain of branches, so "which exceptions move the verdict" is one thing
661
+ * a reader can read, and so that adding a kind without deciding what it means
662
+ * is a missing entry rather than a quiet `verified`.
663
+ *
664
+ * Read the `incomplete` rows as "I could not tell", which is the distinction
665
+ * this whole module turns on. None of them is `verified`: an exception never
666
+ * contributes a clean verdict, which is what makes the invariant in `verdictOf`
667
+ * hold.
668
+ *
669
+ * `PACK_BUNDLE_NOT_VERIFIED` is the one row that is not a constant: it
670
+ * contributes the pack's own bundle verdict, which is `partial`, `failed` or
671
+ * `malformed` and never `verified` (the exception is emitted only when it is
672
+ * not). `null` marks that row, and `readCase` fills it from `status`.
673
+ */
674
+ const EXCEPTION_VERDICT = {
675
+ [COUNTERPART_ABSENT]: "incomplete",
676
+ [COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS]: "incomplete",
677
+ [COUNTERPART_ABSENT_SIGNER_UNKNOWN]: "incomplete",
678
+ [COUNTERPART_AMBIGUOUS]: "incomplete",
679
+ [COUNTERPART_MALFORMED]: "incomplete",
680
+ [PAIR_FAILED]: "failed",
681
+ [RECORD_MALFORMED]: "malformed",
682
+ [RECORD_UNCLAIMED]: "incomplete",
683
+ [RECORD_BYTES_WITHHELD]: "incomplete",
684
+ [RECORD_BYTES_UNREADABLE]: "malformed",
685
+ [PACK_DECLARATION_DEVIATES]: "failed",
686
+ [PACK_DECLARATION_CONTENT_DEPENDENT]: "incomplete",
687
+ [PACK_DECLARATION_UNREADABLE]: "incomplete",
688
+ [PACK_UNDECLARED]: "partial",
689
+ [PACK_BODY_TYPE_UNKNOWN]: "incomplete",
690
+ [PACK_HOLDS_NO_RECORDS]: "incomplete",
691
+ [PACK_KEY_UNREADABLE]: "incomplete",
692
+ [PACK_DOES_NOT_VERIFY]: "failed",
693
+ [PACK_BUNDLE_NOT_VERIFIED]: null,
694
+ [RECORD_ENVELOPE_DOES_NOT_VERIFY]: "failed",
695
+ [RECORD_DOES_NOT_VERIFY_IN_ITS_PACK]: "failed",
696
+ [RECORD_CONTENT_MISMATCH]: "failed",
697
+ [RECORD_PROFILE_QUARANTINED]: "incomplete",
698
+ [CASE_HOLDS_NO_PACKS]: "partial",
699
+ [CASE_HOLDS_NO_HANDOFF_RECORDS]: "partial",
700
+ };
701
+ // ---- The reading ------------------------------------------------------------
702
+ const bodyTypeOf = (bundle) => typeof bundle === "object" && bundle !== null
703
+ ? bundle["body_type"]
704
+ : undefined;
705
+ /**
706
+ * The key that SIGNED the pack wrapper, which is the key that signed the pack's
707
+ * declaration.
708
+ *
709
+ * The wrapper signature covers `body_hash` and `body` carries the
710
+ * `declaration` (`evidence-bundle.md` 5.1, 5.4.1), so this key is the party
711
+ * whose signed account of itself this pack is. Read from the ENVELOPE of the
712
+ * pack and never from `issuer`: `issuer` is a label and DL-60 keeps it out of
713
+ * every reading, while `public_key` is the value the signature binds.
714
+ *
715
+ * It means something only for a pack whose wrapper HOLDS, which is why
716
+ * `readCase` counts it only after its own `verifyEvidenceBundle` call over the
717
+ * pack returned `signatureOk` and `bodyHashOk`.
718
+ */
719
+ const holderKeyOf = (bundle) => typeof bundle === "object" && bundle !== null
720
+ ? bundle["public_key"]
721
+ : undefined;
722
+ /**
723
+ * Provenance, and nothing else: the wrapper signature over `body_hash` verified
724
+ * and `body_hash` is the hash of the body (evidence-bundle.md 5.5.1 step 1).
725
+ * Nothing inside the body is part of this answer.
726
+ */
727
+ const wrapperHolds = (result) => result.signatureOk && result.bodyHashOk;
728
+ /**
729
+ * Why the pack reads what it reads, as the verifier said it: its bundle errors,
730
+ * then each stamp's, then each checkpoint's, then each event's behind the
731
+ * event's own id.
732
+ *
733
+ * The event errors are here as well as on the records' own exceptions on
734
+ * purpose. A pack whose only defect is one record reads `failed` with no
735
+ * bundle-level error at all, and an exception that named the verdict and no
736
+ * reason would send a reader looking for a defect in the pack as a whole.
737
+ */
738
+ const bundleReasons = (result) => [
739
+ ...result.errors,
740
+ ...(result.stamps ?? []).flatMap((stamp) => stamp.errors),
741
+ ...result.checkpoints.flatMap((cp) => cp.errors),
742
+ ...result.events.flatMap((ev) => ev.errors.map((e) => `${ev.eventId}: ${e}`)),
743
+ ];
744
+ /**
745
+ * The `spec_version` of the envelope of `body.events[index]`, as written.
746
+ * `packRecords` and the bundle verifier both walk `body.events` in order, so
747
+ * `index` is the same record in both.
748
+ */
749
+ function recordSpecVersion(bundle, index) {
750
+ const at = (v, k) => typeof v === "object" && v !== null ? v[k] : undefined;
751
+ const events = at(at(bundle, "body"), "events");
752
+ return Array.isArray(events) ? at(at(events[index], "envelope"), "spec_version") : undefined;
753
+ }
754
+ /**
755
+ * `specVersion` is a quarantined profile, on the same token boundary the
756
+ * envelope layer's own accept-check uses (`specVersionSupported`).
757
+ */
758
+ function underQuarantine(specVersion) {
759
+ if (typeof specVersion !== "string")
760
+ return false;
761
+ return CASE_QUARANTINED_PROFILES.some((m) => {
762
+ if (specVersion === m)
763
+ return true;
764
+ if (!specVersion.startsWith(`${m}.`))
765
+ return false;
766
+ const minor = specVersion.slice(m.length + 1);
767
+ return minor.length > 0 && /^[0-9]+$/.test(minor);
768
+ });
769
+ }
770
+ /**
771
+ * The case verdict, from the contributions alone.
772
+ *
773
+ * One outcome is that outcome; more than one is `mixed` (anchor#198,
774
+ * criterion 4). `mixed` is in the 3 vocabulary and coins no word.
775
+ *
776
+ * THE INVARIANT, stated so a mutation test has something to kill: `verified` is
777
+ * reachable ONLY when `exceptions` is empty. Every exception kind carries a
778
+ * non-`verified` contribution (`EXCEPTION_VERDICT`), so this falls out of the
779
+ * table rather than being enforced by a second rule that could drift from it.
780
+ * The assertion is here anyway, because that is the property every one of the
781
+ * seventeen findings on this program violated.
782
+ */
783
+ function verdictOf(exceptions, contributions) {
784
+ // Unreachable through `readCase`, which names a case with no contributions
785
+ // rather than returning one. Defended anyway: an empty aggregate reading as
786
+ // `verified` is the defect class itself.
787
+ if (contributions.length === 0)
788
+ return "partial";
789
+ const outcomes = new Set(contributions.map(([, v]) => v));
790
+ const verdict = outcomes.size === 1 ? [...outcomes][0] : "mixed";
791
+ if (verdict === "verified" && exceptions.length > 0) {
792
+ throw new Error(`verified over a non-empty exception list: ${exceptions.map((e) => e.kind).join(", ")}`);
793
+ }
794
+ return verdict;
795
+ }
796
+ /**
797
+ * Read a whole case: several packs in, one verdict and its exceptions out.
798
+ *
799
+ * The order of the work, and why it is this order:
800
+ *
801
+ * 1. **Each pack is read into records**, through `packRecords`, which is the
802
+ * one implementation of "what does this pack hold". A pack whose `body_type`
803
+ * this reader does not know, or which holds no records, is NAMED here and
804
+ * its records are not silently zero.
805
+ * 2. **Each pack is graded against its own declaration**, through
806
+ * `checkDeclaration`, which is the one implementation of 5.4. A pack proven
807
+ * to deviate from its own claim fails the case.
808
+ * 3. **Every record every pack supplied goes into ONE pile**, with no partition
809
+ * by pack, by issuer or by organization, and the pile is read by
810
+ * `readHandoffs`, which is the one implementation of 4.1. This is where
811
+ * matching happens, and it is on keys and hashes alone.
812
+ * 4. **Each reading becomes an exception or a clean contribution**, and the two
813
+ * counterpart kinds are separated by asking whether a pack SIGNED by the
814
+ * counterparty's key is in the case, a key read from every pack's wrapper
815
+ * before any exit can skip it.
816
+ *
817
+ * A record whose bytes the pack did not supply never reaches the pile, because
818
+ * there is nothing to put in it. That is exactly the shape this module has to
819
+ * refuse to pass over in silence, so it is named at step 1 and carries
820
+ * `incomplete`: the reader genuinely cannot tell whether the record it cannot
821
+ * see is the counterpart that is missing.
822
+ */
823
+ export async function readCase(packs) {
824
+ const exceptions = [];
825
+ const contributions = [];
826
+ const note = (kind, fields = {}) => {
827
+ exceptions.push({ kind, ...fields });
828
+ // `null` is PACK_BUNDLE_NOT_VERIFIED: the pack's own verdict, never verified.
829
+ const contribution = EXCEPTION_VERDICT[kind] ?? fields.status;
830
+ contributions.push([fields.eventId ?? fields.packRef ?? "case", contribution]);
831
+ };
832
+ if (packs.length === 0) {
833
+ note(CASE_HOLDS_NO_PACKS);
834
+ return {
835
+ verdict: verdictOf(exceptions, contributions),
836
+ exceptions,
837
+ records: [],
838
+ contributions,
839
+ handoffRecords: 0,
840
+ onChainChecked: false,
841
+ };
842
+ }
843
+ // Step 1 and 2, pack by pack, in the order the reader was given them.
844
+ // Whether every pack's witness was re-fetched on chain, ALL over the packs
845
+ // this reader verified. It passes no gateways, so this stays false and the
846
+ // reading says so (see `CaseReading.onChainChecked`).
847
+ const witnessChecked = [];
848
+ const held = [];
849
+ // The pack and the `event_id` behind each pile member, kept beside the pile
850
+ // rather than parsed back out of a reference string. A reference is for a
851
+ // human to read; a join rebuilt by splitting one is a join on a value a
852
+ // holder can put a separator in.
853
+ const provenance = [];
854
+ // Keyed by the pack's POSITION and never by its `ref`. `ref` is the reader's
855
+ // own name for a pack, and nothing stops two packs in one archive carrying
856
+ // the same one: keyed by ref, the second pack's claims silently replaced the
857
+ // first's and records from one pack were graded against another pack's
858
+ // declaration. Found by a constructed archive rather than by reading, which
859
+ // is the only way this class of defect is ever found.
860
+ const claimsByPack = new Map();
861
+ // Every key that SIGNED A PACK in this case: the wrapper key, which is the
862
+ // key that signs the pack's declaration. This is the ONE input that separates
863
+ // the two counterpart kinds (see `COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS`),
864
+ // and it replaces a set of every key that signed a RECORD anywhere in the
865
+ // case.
866
+ //
867
+ // The record-key set was wrong twice over, and both times in the direction
868
+ // that matters. It let the ACCUSER manufacture the damning line: one pack,
869
+ // plus one unrelated record the counterparty signed at some other time, read
870
+ // "the counterparty's OWN records ARE here" when the counterparty had handed
871
+ // over nothing (F4). And it let the COUNTERPARTY choose the innocent line: it
872
+ // was filled inside the record loop, which the `body_type` and no-records
873
+ // exits `continue` past, so a counterparty that wrapped its own pack in a body
874
+ // this reader does not read took its keys out of the set (F3), the third
875
+ // instance of a route already closed once for withheld `event_bytes`.
876
+ //
877
+ // A wrapper key closes both. The accuser cannot forge one without the
878
+ // counterparty's private key, and it is read HERE, first, before any exit
879
+ // below, from the pack envelope and never from the body, so no body type, no
880
+ // disclosure choice and no record content decides whether it is read.
881
+ const holderKeys = new Set();
882
+ // How many packs this reader could not tie to a key: an unreadable wrapper
883
+ // key, or a wrapper that does not hold. While it is not zero, "no pack in this
884
+ // case is signed by the named key" is not established, so the innocent
885
+ // one-sided kind is not available (COUNTERPART_ABSENT_SIGNER_UNKNOWN).
886
+ let unattributed = 0;
887
+ for (let position = 0; position < packs.length; position += 1) {
888
+ const pack = packs[position];
889
+ const holder = normalizedSigningKey(holderKeyOf(pack.bundle));
890
+ if (holder === null) {
891
+ // Named, never skipped: a pack whose key this reader cannot read is a
892
+ // pack whose signer is UNKNOWN, and not a pack nobody signed. Checked
893
+ // BEFORE verification, because the two bundle verifiers disagree on some
894
+ // of these spellings (proof#135) and the reading must not.
895
+ note(PACK_KEY_UNREADABLE, { packRef: pack.ref });
896
+ unattributed += 1;
897
+ continue;
898
+ }
899
+ // The precondition `HeldRecord` states, met HERE rather than trusted to a
900
+ // caller: every key this module compares is an envelope `public_key`, and
901
+ // "unforgeable" is a property of a VERIFIED envelope. Offline, no
902
+ // gateways: this reader makes no network call. This kernel's verifier
903
+ // reads envelopes under exactly CASE_RECORD_PROFILES (pinned by a test).
904
+ const verified = await verifyEvidenceBundle(pack.bundle, {
905
+ content: (pack.content ?? undefined),
906
+ });
907
+ witnessChecked.push(verified.onChainChecked);
908
+ // WHOSE PACK IS IT. The wrapper, and only the wrapper.
909
+ if (!wrapperHolds(verified)) {
910
+ note(PACK_DOES_NOT_VERIFY, { packRef: pack.ref });
911
+ unattributed += 1;
912
+ continue;
913
+ }
914
+ holderKeys.add(holder);
915
+ // WHAT IS WRONG INSIDE IT, from here on. First the pack's own assurance,
916
+ // which propagates: the case never reads above its weakest pack.
917
+ if (verified.status !== "verified") {
918
+ note(PACK_BUNDLE_NOT_VERIFIED, {
919
+ packRef: pack.ref,
920
+ status: verified.status,
921
+ reasons: bundleReasons(verified),
922
+ });
923
+ }
924
+ // The body is one the verifier could not read, so it graded no record, and
925
+ // nothing below has a grading to read. Named above.
926
+ if (verified.status === "malformed")
927
+ continue;
928
+ const bodyType = bodyTypeOf(pack.bundle);
929
+ if (!CASE_BODY_TYPES.includes(bodyType)) {
930
+ // `sanning.evidence.export/v2` lands here, and it is NOT unwrapped: its
931
+ // records sit under `body.source_bundle`, and reading them is a change to
932
+ // which packs this reader grades, so it is a decision of its own and not
933
+ // a repair (evidence-bundle.md 5.5.1 step 1 says so too). What this exit
934
+ // costs is that pack's RECORDS, and it is named every time. It no longer
935
+ // costs the pack's KEY: that was read above.
936
+ note(PACK_BODY_TYPE_UNKNOWN, { packRef: pack.ref });
937
+ continue;
938
+ }
939
+ const records = packRecords(pack.bundle, pack.content);
940
+ // The per-record gradings below are joined to `records` BY INDEX, so a
941
+ // verifier that graded a different number of records than the pack holds
942
+ // leaves this loop with no grading to read. That is not hypothetical: a
943
+ // pack whose `body_hash` does not cover its body reads `failed` with
944
+ // `events` EMPTY while `packRecords` reads every record in it, and only
945
+ // `wrapperHolds` above stands between that pack and this loop. A reader
946
+ // must never crash on a pack a party can build, so the gap is NAMED rather
947
+ // than indexed into, and the pack's records are dropped exactly as a
948
+ // `malformed` pack's are (gate 4 on proof#140, finding 3: under the
949
+ // mutation that dropped `bodyHashOk` from the wrapper test, this kernel
950
+ // threw `Cannot read properties of undefined (reading 'envelopeOk')` here
951
+ // and nothing in the ladder said so).
952
+ if (verified.events.length !== records.length) {
953
+ note(PACK_BUNDLE_NOT_VERIFIED, {
954
+ packRef: pack.ref,
955
+ status: verified.status,
956
+ reasons: [
957
+ `this verifier graded ${verified.events.length} of the ` +
958
+ `${records.length} record(s) the pack holds, so none of them is read`,
959
+ ],
960
+ });
961
+ continue;
962
+ }
963
+ if (records.length === 0) {
964
+ note(PACK_HOLDS_NO_RECORDS, { packRef: pack.ref });
965
+ continue;
966
+ }
967
+ const check = checkDeclaration(pack.bundle, pack.content);
968
+ claimsByPack.set(position, declarationClaims(pack.bundle));
969
+ if (check.contentFindings.length > 0) {
970
+ // BESIDE the deviation exception and never instead of it: a pack can do
971
+ // both, over different records, and the two say different things. This
972
+ // one is `incomplete` because whoever assembled the case chose the bytes
973
+ // it turns on (proof#147).
974
+ note(PACK_DECLARATION_CONTENT_DEPENDENT, {
975
+ packRef: pack.ref,
976
+ count: check.contentFindings.length,
977
+ });
978
+ }
979
+ if (check.verdict === "failed") {
980
+ note(PACK_DECLARATION_DEVIATES, {
981
+ packRef: pack.ref,
982
+ count: check.deviations.length,
983
+ });
984
+ }
985
+ else if (check.contentFindings.length > 0) {
986
+ // Already named above. `partial` here is not "the reader cannot read the
987
+ // claim": the claim was read, and part of it rests on bytes nobody
988
+ // signed.
989
+ }
990
+ else if (check.verdict === "verified") {
991
+ // A pack that agrees with its own claim contributes NOTHING, rather than
992
+ // a `verified` of its own. The case verdict is over the hand-off
993
+ // readings, and criterion 4's "mixed" is about mixed HAND-OFF outcomes: a
994
+ // pack-level clean contribution would put `verified` into the set on
995
+ // every case, so one broken pair beside two honest declarations would
996
+ // read `mixed` and the failure would be the quieter half of a two-word
997
+ // answer. A pack-level PROBLEM still contributes, through its exception.
998
+ }
999
+ else if (check.present) {
1000
+ // `partial` with a declaration present is 5.4.3's "a claim this reader
1001
+ // cannot read", which `gradePack` already carries to `incomplete`. The
1002
+ // same posture here, for the same reason: the field has no accept-set and
1003
+ // the holder writes it.
1004
+ note(PACK_DECLARATION_UNREADABLE, { packRef: pack.ref });
1005
+ }
1006
+ else {
1007
+ note(PACK_UNDECLARED, { packRef: pack.ref });
1008
+ }
1009
+ // Each record on its OWN envelope. `packRecords` and the verifier both walk
1010
+ // `body.events` in order and emit one entry per element, so the index is
1011
+ // the join, exactly as `held`, `provenance` and `readings` below.
1012
+ for (let index = 0; index < records.length; index += 1) {
1013
+ const record = records[index];
1014
+ const graded = verified.events[index];
1015
+ if (underQuarantine(recordSpecVersion(pack.bundle, index))) {
1016
+ note(RECORD_PROFILE_QUARANTINED, { packRef: pack.ref, eventId: record.eventId });
1017
+ continue;
1018
+ }
1019
+ if (!graded.envelopeOk) {
1020
+ note(RECORD_ENVELOPE_DOES_NOT_VERIFY, {
1021
+ packRef: pack.ref,
1022
+ eventId: record.eventId,
1023
+ reasons: [...graded.errors],
1024
+ });
1025
+ continue;
1026
+ }
1027
+ if (!record.bytesSupplied) {
1028
+ note(RECORD_BYTES_WITHHELD, {
1029
+ packRef: pack.ref,
1030
+ eventId: record.eventId,
1031
+ });
1032
+ continue;
1033
+ }
1034
+ if (record.record === null) {
1035
+ note(RECORD_BYTES_UNREADABLE, {
1036
+ packRef: pack.ref,
1037
+ eventId: record.eventId,
1038
+ });
1039
+ continue;
1040
+ }
1041
+ // Named, and the record stays in the pile (see the kinds).
1042
+ if (graded.contentOk === false) {
1043
+ note(RECORD_CONTENT_MISMATCH, {
1044
+ packRef: pack.ref,
1045
+ eventId: record.eventId,
1046
+ reasons: [...graded.errors],
1047
+ });
1048
+ }
1049
+ if (!(graded.inclusionOk && graded.checkpointBound) ||
1050
+ (!graded.ok && graded.contentOk !== false)) {
1051
+ note(RECORD_DOES_NOT_VERIFY_IN_ITS_PACK, {
1052
+ packRef: pack.ref,
1053
+ eventId: record.eventId,
1054
+ reasons: [...graded.errors],
1055
+ });
1056
+ }
1057
+ held.push({
1058
+ publicKey: record.publicKey,
1059
+ record: record.record,
1060
+ ref: `${pack.ref}/${record.eventId}`,
1061
+ });
1062
+ provenance.push({ position, packRef: pack.ref, eventId: record.eventId });
1063
+ }
1064
+ }
1065
+ // Step 3. ONE pile, no partition. This is the line anchor#198 turns on and
1066
+ // the line DL-60 constrains: there is nothing here that reads a tenant, an
1067
+ // issuer or an organization, so a pair across two organizations' packs is
1068
+ // read exactly as a pair inside one.
1069
+ const readings = readHandoffs(held);
1070
+ // Step 4.
1071
+ const records = [];
1072
+ let handoffRecords = 0;
1073
+ for (let index = 0; index < readings.length; index += 1) {
1074
+ const reading = readings[index];
1075
+ const { position, packRef, eventId } = provenance[index];
1076
+ records.push({
1077
+ packRef,
1078
+ eventId,
1079
+ verdict: reading.verdict,
1080
+ findings: [...reading.findings],
1081
+ failedChecks: [...reading.failedChecks],
1082
+ counterparts: [...reading.candidates],
1083
+ });
1084
+ // proof#137's door, named. Read from the ONE reader of the declaration
1085
+ // (`declarationClaims`), never from a second parse here. Only a READABLE
1086
+ // declaration can make a record unclaimed: where the claim itself is absent
1087
+ // or unreadable, that pack already carries its own exception and naming
1088
+ // every record in it again would report one defect many times.
1089
+ //
1090
+ // ABOVE the two `continue`s below, deliberately. This check asks what the
1091
+ // PACK said about the record, which is independent of whether 4.1 governs
1092
+ // the record and of whether the record is well formed. Behind the
1093
+ // `not_a_handoff_record` exit it was a check the audited party switched off
1094
+ // by respelling two fields, and behind the `malformed` exit it was one a
1095
+ // party switched off by breaking its own record.
1096
+ const claims = claimsByPack.get(position);
1097
+ if (claims !== undefined &&
1098
+ claims.readable &&
1099
+ !claims.claimedSides.includes(eventId)) {
1100
+ note(RECORD_UNCLAIMED, { packRef, eventId });
1101
+ }
1102
+ if (reading.findings.includes(NOT_A_HANDOFF_FINDING)) {
1103
+ // 4.1 (v1.9, A14): information, not absence. A record 4.1 does not govern
1104
+ // is not a hand-off exception, and it is still in `records` and still
1105
+ // subject to the claim check above.
1106
+ continue;
1107
+ }
1108
+ handoffRecords += 1;
1109
+ if (reading.verdict === "malformed") {
1110
+ // Whose defect is it? `recordIsMalformed` is 4.1's own predicate, CALLED
1111
+ // and not restated, and it answers about THIS record. A `malformed`
1112
+ // aggregate over a record that is itself well formed is a malformed
1113
+ // CANDIDATE, so it is a different kind, it does not `continue`, and the
1114
+ // counterpart, ambiguity and `failedChecks` reporting below all keep
1115
+ // running over it.
1116
+ if (recordIsMalformed(held[index])) {
1117
+ note(RECORD_MALFORMED, { packRef, eventId });
1118
+ continue;
1119
+ }
1120
+ note(COUNTERPART_MALFORMED, { packRef, eventId });
1121
+ }
1122
+ if (reading.findings.includes(COUNTERPART_ABSENT)) {
1123
+ // `held`, `provenance` and `readings` are three views of one list in one
1124
+ // order, built together and never filtered, so the index is the join.
1125
+ // Deliberately not a lookup by reference string: a reference is for a
1126
+ // person to read, and a join rebuilt by splitting one is a join on a
1127
+ // value a holder can put a separator inside.
1128
+ const subject = held[index];
1129
+ const event = subject.record["event"];
1130
+ const named = normalizedSigningKey(typeof event === "object" && event !== null
1131
+ ? event["counterparty_public_key"]
1132
+ : undefined);
1133
+ // A VERIFIED pack SIGNED by the named key is in the case, and nothing
1134
+ // else. Records signed by that key which some other party holds are not
1135
+ // that party's pack. Where some pack could not be tied to a key, "no
1136
+ // pack is signed by the named key" is not established and the reading
1137
+ // says it could not tell.
1138
+ const kind = named !== null && holderKeys.has(named)
1139
+ ? COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS
1140
+ : unattributed > 0
1141
+ ? COUNTERPART_ABSENT_SIGNER_UNKNOWN
1142
+ : COUNTERPART_ABSENT;
1143
+ note(kind, { packRef, eventId, counterpartyKey: named });
1144
+ continue;
1145
+ }
1146
+ if (reading.findings.includes(COUNTERPART_AMBIGUOUS)) {
1147
+ note(COUNTERPART_AMBIGUOUS, {
1148
+ packRef,
1149
+ eventId,
1150
+ count: reading.candidates.length,
1151
+ });
1152
+ }
1153
+ // ON `failedChecks` AND NOT ON THE VERDICT WORD. 4.1 keeps the UNION of
1154
+ // every candidate's failed checks precisely so a junk record cannot strip a
1155
+ // named failure out of the report (proof#133 ruling 6), and reading the
1156
+ // verdict instead threw that union away: `malformed` outranks `failed`, so
1157
+ // one junk candidate took the branch below and a proven `message_hash`
1158
+ // failure disappeared from the case. A named failed check is a named failed
1159
+ // check whatever the aggregate word is.
1160
+ if (reading.failedChecks.length > 0) {
1161
+ note(PAIR_FAILED, {
1162
+ packRef,
1163
+ eventId,
1164
+ failedChecks: [...reading.failedChecks],
1165
+ });
1166
+ }
1167
+ else if (reading.verdict === "verified") {
1168
+ contributions.push([`${packRef}/${eventId}`, "verified"]);
1169
+ }
1170
+ }
1171
+ if (handoffRecords === 0)
1172
+ note(CASE_HOLDS_NO_HANDOFF_RECORDS);
1173
+ return {
1174
+ verdict: verdictOf(exceptions, contributions),
1175
+ exceptions,
1176
+ records,
1177
+ contributions,
1178
+ handoffRecords,
1179
+ onChainChecked: witnessChecked.length > 0 && witnessChecked.every((c) => c),
1180
+ };
1181
+ }
1182
+ // ---- Presentation (optional) ------------------------------------------------
1183
+ /**
1184
+ * The house wording for one exception.
1185
+ *
1186
+ * Offered, not imposed, exactly as `renderDeviation` is: what must not vary
1187
+ * between surfaces is the verdict and the set of exceptions, and those come
1188
+ * from `readCase`. Both kernels return this same text for the same exception.
1189
+ */
1190
+ const because = (reasons) => reasons !== undefined && reasons.length > 0 ? `: ${reasons.join("; ")}` : "";
1191
+ export function renderException(exception) {
1192
+ const where = `${exception.packRef}/${exception.eventId}`;
1193
+ switch (exception.kind) {
1194
+ case COUNTERPART_ABSENT:
1195
+ return (`${where} has no counterpart among the records this verifier read, ` +
1196
+ "and no pack in this case is signed by the key it names");
1197
+ case COUNTERPART_ABSENT_FROM_COUNTERPARTY_RECORDS:
1198
+ return (`${where} has no counterpart among the records this verifier read, ` +
1199
+ "and a pack signed by the key it names IS in this case and holds " +
1200
+ "no counterpart this verifier could read");
1201
+ case COUNTERPART_ABSENT_SIGNER_UNKNOWN:
1202
+ return (`${where} has no counterpart among the records this verifier read, ` +
1203
+ "and a pack in this case could not be tied to a signing key, so " +
1204
+ "whether a pack signed by the key it names is in this case is " +
1205
+ "UNKNOWN");
1206
+ case COUNTERPART_AMBIGUOUS:
1207
+ return (`${where} has ${exception.count} candidate counterparts, so the ` +
1208
+ "reading is the worst of them and no one match was chosen");
1209
+ case PAIR_FAILED:
1210
+ return `${where} FAILED on ${(exception.failedChecks ?? []).join(", ")}`;
1211
+ case RECORD_MALFORMED:
1212
+ return `${where} is not a well formed hand-off record`;
1213
+ case COUNTERPART_MALFORMED:
1214
+ return (`${where} is well formed and a candidate counterpart of it is not, ` +
1215
+ "so what this record pairs with is UNKNOWN");
1216
+ case RECORD_UNCLAIMED:
1217
+ return (`${where} is a record its own pack declares and claims in no ` +
1218
+ "hand-off pair, so whether the holder meant to hand it over is " +
1219
+ "UNKNOWN");
1220
+ case RECORD_BYTES_WITHHELD:
1221
+ return `${where} travels with no record bytes, so what that record IS is UNKNOWN`;
1222
+ case RECORD_BYTES_UNREADABLE:
1223
+ return `${where} travels with record bytes this verifier cannot read`;
1224
+ case PACK_DECLARATION_DEVIATES:
1225
+ // "read together with" and not "deviates", flat: 5.4 grades the pack
1226
+ // against the content the reader was handed, and content beside a pack is
1227
+ // unsigned. Whoever assembles a case can swap it, so the sentence names
1228
+ // what was compared rather than only the pack.
1229
+ return (`${exception.packRef}, read together with any content handed in ` +
1230
+ `beside it, DEVIATES from its own declaration, in ` +
1231
+ `${exception.count} way(s)`);
1232
+ case PACK_DECLARATION_CONTENT_DEPENDENT:
1233
+ // Says what this verifier could not establish, and names the content
1234
+ // rather than the pack's holder: the bytes are unsigned and whoever
1235
+ // assembled the case chose them.
1236
+ return (`${exception.packRef} agrees with its own declaration everywhere its ` +
1237
+ `signature reaches, and in ${exception.count} way(s) it does not line ` +
1238
+ "up with the unsigned content handed in beside it, which anyone could " +
1239
+ "have supplied, so whether it holds what it declares is UNKNOWN");
1240
+ case PACK_DECLARATION_UNREADABLE:
1241
+ return (`${exception.packRef} says what it holds in a declaration this ` +
1242
+ "verifier cannot read, so whether it is complete is UNKNOWN");
1243
+ case PACK_UNDECLARED:
1244
+ return (`${exception.packRef} does not say what it holds, so whether it is ` +
1245
+ "complete is UNKNOWN");
1246
+ case PACK_BODY_TYPE_UNKNOWN:
1247
+ return (`${exception.packRef} is a pack body this verifier does not read, so ` +
1248
+ "what it holds is UNKNOWN");
1249
+ case PACK_HOLDS_NO_RECORDS:
1250
+ return `${exception.packRef} holds no records this verifier can read`;
1251
+ case PACK_KEY_UNREADABLE:
1252
+ return (`${exception.packRef} names a signing key this verifier cannot ` +
1253
+ "read, so who signed it is UNKNOWN and none of its records are read");
1254
+ case PACK_DOES_NOT_VERIFY:
1255
+ return (`${exception.packRef} carries a wrapper signature over its body that ` +
1256
+ "does not verify, so none of its records are read and the key it " +
1257
+ "names is not counted as its signer");
1258
+ case PACK_BUNDLE_NOT_VERIFIED: {
1259
+ let head = `${exception.packRef} reads ${(exception.status ?? "").toUpperCase()} as ` +
1260
+ "a bundle, together with any content handed in beside it";
1261
+ if (exception.status === "malformed")
1262
+ head += ", so none of its records are read";
1263
+ return head + because(exception.reasons);
1264
+ }
1265
+ case RECORD_ENVELOPE_DOES_NOT_VERIFY:
1266
+ return (`${where} is in a pack whose wrapper verifies, and its own envelope ` +
1267
+ "does not verify, so it is not read as a hand-off record" +
1268
+ because(exception.reasons));
1269
+ case RECORD_DOES_NOT_VERIFY_IN_ITS_PACK:
1270
+ return (`${where} has an envelope that verifies and fails another check ` +
1271
+ "inside its pack, and it is still read as a hand-off record" +
1272
+ because(exception.reasons));
1273
+ case RECORD_CONTENT_MISMATCH:
1274
+ return (`${where} is read with content that does not match what it commits ` +
1275
+ "to, and it is still read as a hand-off record" +
1276
+ because(exception.reasons));
1277
+ case RECORD_PROFILE_QUARANTINED:
1278
+ return (`${where} is signed under a profile this case reader does not read ` +
1279
+ "while its admission is undecided (DL-63), so what it says is UNKNOWN");
1280
+ case CASE_HOLDS_NO_PACKS:
1281
+ return "this case holds no packs";
1282
+ case CASE_HOLDS_NO_HANDOFF_RECORDS:
1283
+ return ("no record this verifier read in this case is a hand-off record, " +
1284
+ "so no hand-off was verified");
1285
+ default:
1286
+ return exception.kind;
1287
+ }
1288
+ }
1289
+ /**
1290
+ * One line a surface can print for a whole case.
1291
+ *
1292
+ * Both kernels print this sentence, so a reader who runs the Python command and
1293
+ * the TypeScript one over one case archive reads one sentence rather than two
1294
+ * that differ by a word.
1295
+ */
1296
+ export function renderCaseSummary(reading) {
1297
+ const n = reading.handoffRecords;
1298
+ // What was NOT checked, in the same sentence as what was, and in the
1299
+ // `verified` one above all (gate 4 on proof#140, finding 3). It is a
1300
+ // statement of SCOPE and not a doubt: every pack here was verified offline,
1301
+ // and no `anchor_ref` any of them names was fetched from a chain, because
1302
+ // this reader makes no network call. Left unsaid, a clean case reads as
1303
+ // though a witness had been checked, which nobody established. It is not an
1304
+ // exception, and it must never become one: an empty exception list is the
1305
+ // only shape a reader may read as clean, so a standing exception here would
1306
+ // put `verified` out of reach for good.
1307
+ const scope = reading.onChainChecked
1308
+ ? ""
1309
+ : "; no anchor_ref in it was re-fetched on chain, which this reader never does";
1310
+ if (reading.verdict === "verified") {
1311
+ return (`this case verifies across ${reading.records.length} record(s): ` +
1312
+ `${n} hand-off record(s), every one matched, no exceptions` +
1313
+ scope);
1314
+ }
1315
+ return (`this case reads ${reading.verdict.toUpperCase()} over ${n} hand-off ` +
1316
+ `record(s), with ${reading.exceptions.length} exception(s)` +
1317
+ scope);
1318
+ }