advisers 0.3.1 → 0.4.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.
Files changed (48) hide show
  1. package/README-zh.md +13 -15
  2. package/README.md +32 -22
  3. package/THIRD-PARTY-NOTICES.md +1 -1
  4. package/bin/advisers.js +2769 -183
  5. package/index.js +1 -1
  6. package/lib/brief-archive.js +267 -0
  7. package/lib/client-pack.js +1016 -0
  8. package/lib/consent.js +390 -0
  9. package/lib/credentials.js +195 -0
  10. package/lib/customer-read.js +524 -0
  11. package/lib/error-codes.js +516 -7
  12. package/lib/evidence.js +928 -120
  13. package/lib/kb-extract.js +626 -0
  14. package/lib/kb-facts.js +429 -0
  15. package/lib/kb-pages.js +1084 -0
  16. package/lib/kb.js +1036 -0
  17. package/lib/ledger-read.js +109 -1
  18. package/lib/mail-errors.js +46 -0
  19. package/lib/mail-extract.js +351 -0
  20. package/lib/mail-imap.js +446 -0
  21. package/lib/mail-import.js +176 -0
  22. package/lib/mail-parse.js +223 -0
  23. package/lib/mail-store.js +731 -0
  24. package/lib/mail.js +506 -0
  25. package/lib/mcp-server.js +397 -11
  26. package/lib/notify.js +265 -7
  27. package/lib/policy/decision.js +315 -0
  28. package/lib/policy/egress.js +268 -0
  29. package/lib/policy/errors.js +71 -0
  30. package/lib/policy/index.js +99 -0
  31. package/lib/policy/rules.js +468 -0
  32. package/lib/policy/surfaces.js +141 -0
  33. package/lib/receipt-verify.js +283 -26
  34. package/lib/receivables.js +1653 -0
  35. package/lib/scheduler.js +453 -9
  36. package/lib/shell.js +1406 -0
  37. package/lib/source.js +98 -3
  38. package/lib/sqlite.js +501 -49
  39. package/lib/tos/index.js +2 -2
  40. package/lib/tos/manifest.json +6 -6
  41. package/lib/tos/privacy.zh-CN.md +33 -20
  42. package/lib/tos/safety.zh-CN.md +7 -6
  43. package/lib/tos/summary.zh-CN.md +3 -2
  44. package/lib/tos/terms.zh-CN.md +31 -16
  45. package/lib/tui/labels.js +1 -1
  46. package/lib/tui/render.js +1 -1
  47. package/lib/urgency.js +467 -0
  48. package/package.json +4 -4
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Offline verifier for Advisers delivery receipt packs (format v1.1).
2
+ * Offline verifier for Advisers delivery receipt packs (manifest v1 and v2).
3
3
  *
4
4
  * A receipt pack is a directory produced by buildReceiptPack in lib/evidence.js:
5
5
  *
@@ -40,12 +40,18 @@ import { createHash } from 'node:crypto'
40
40
  import { closeSync, existsSync, openSync, readFileSync, readdirSync, readSync, statSync } from 'node:fs'
41
41
  import { join, resolve } from 'node:path'
42
42
  import {
43
+ EVIDENCE_KINDS,
44
+ EVIDENCE_PACK_VERSIONS,
45
+ EVIDENCE_RESOLUTIONS,
43
46
  MAX_QUOTE_CHARS,
44
47
  RECEIPT_DELIVERIES_PREFIX,
45
48
  RECEIPT_MANIFEST_FORMAT,
46
- RECEIPT_MANIFEST_VERSION,
49
+ RECEIPT_MANIFEST_VERSIONS,
47
50
  RECEIPT_REQUIRED_FILES,
51
+ RECEIPT_SCOPES,
48
52
  RECEIPT_SHA256SUMS_FILE,
53
+ RECEIPT_SUBJECT_KINDS,
54
+ VERIFICATION_BASES,
49
55
  isUnsafePackPath,
50
56
  sha256Hex,
51
57
  } from './evidence.js'
@@ -127,6 +133,10 @@ function isNonNegativeInteger(value) {
127
133
  return Number.isInteger(value) && value >= 0
128
134
  }
129
135
 
136
+ function isNonEmptyString(value) {
137
+ return typeof value === 'string' && value.length > 0
138
+ }
139
+
130
140
  /**
131
141
  * Parse the strict receipt-pack SHA256SUMS format. SHA256SUMS must not list
132
142
  * itself (it cannot contain its own digest).
@@ -292,7 +302,7 @@ function checkDocxStructure(file, limits) {
292
302
  return { zip, hasDocument, bytes }
293
303
  }
294
304
 
295
- // --- manifest schema (format v1; keep it in sync with buildReceiptPack) ------
305
+ // --- manifest schema (format v1/v2; keep it in sync with buildReceiptPack) ----
296
306
 
297
307
  function validateGenerator(generator, errors) {
298
308
  if (!isPlainObject(generator)) {
@@ -327,12 +337,72 @@ function validateRecipient(recipient, errors) {
327
337
  }
328
338
  }
329
339
 
330
- function validateDelivery(delivery, index, sums, deliveryFiles, errors) {
340
+ // --- manifest v2 additions (subject / scope / row-level lineage) -------------
341
+
342
+ function validateSubject(subject, errors) {
343
+ const where = 'manifest.subject'
344
+ if (!isPlainObject(subject)) {
345
+ addError(errors, 'schema: ' + where + ' must be an object')
346
+ return
347
+ }
348
+ if (!RECEIPT_SUBJECT_KINDS.includes(subject.kind)) {
349
+ addError(errors, 'schema: ' + where + '.kind must be one of ' + RECEIPT_SUBJECT_KINDS.map((kind) => '"' + kind + '"').join(', '))
350
+ }
351
+ if (subject.kind === 'none') {
352
+ if ('id' in subject || 'name' in subject) {
353
+ addError(errors, "schema: " + where + " must not carry id/name when kind is 'none'")
354
+ }
355
+ return
356
+ }
357
+ for (const key of ['id', 'name']) {
358
+ if (key in subject && !isNonEmptyString(subject[key])) {
359
+ addError(errors, 'schema: ' + where + '.' + key + ' must be a non-empty string')
360
+ }
361
+ }
362
+ if (!isNonEmptyString(subject.id) && !isNonEmptyString(subject.name)) {
363
+ addError(errors, 'schema: ' + where + ' requires at least one of id/name')
364
+ }
365
+ }
366
+
367
+ function validateScope(scope, errors) {
368
+ if (!RECEIPT_SCOPES.includes(scope)) {
369
+ addError(errors, 'schema: manifest.scope must be one of ' + RECEIPT_SCOPES.map((value) => '"' + value + '"').join(', '))
370
+ }
371
+ }
372
+
373
+ /** v2-only row lineage. v1 rows keep their exact old validation. */
374
+ function validateDeliveryLineage(delivery, where, errors) {
375
+ for (const key of ['clientId', 'projectId', 'matterId']) {
376
+ if (key in delivery && !isNonEmptyString(delivery[key])) {
377
+ addError(errors, 'schema: ' + where + '.' + key + ' must be a non-empty string')
378
+ }
379
+ }
380
+ if (!('template' in delivery)) return
381
+ const template = delivery.template
382
+ if (!isPlainObject(template)) {
383
+ addError(errors, 'schema: ' + where + '.template must be an object')
384
+ return
385
+ }
386
+ for (const key of ['name', 'version']) {
387
+ if (!isNonEmptyString(template[key])) {
388
+ addError(errors, 'schema: ' + where + '.template.' + key + ' must be a non-empty string')
389
+ }
390
+ }
391
+ if ('quoteNo' in template && !isNonEmptyString(template.quoteNo)) {
392
+ addError(errors, 'schema: ' + where + '.template.quoteNo must be a non-empty string')
393
+ }
394
+ if ('dataSha256' in template && !isSha256Hex(template.dataSha256)) {
395
+ addError(errors, 'schema: ' + where + '.template.dataSha256 must be 64 lowercase hex characters')
396
+ }
397
+ }
398
+
399
+ function validateDelivery(delivery, index, sums, deliveryFiles, errors, version) {
331
400
  const where = 'manifest.deliveries[' + index + ']'
332
401
  if (!isPlainObject(delivery)) {
333
402
  addError(errors, 'schema: ' + where + ' must be an object')
334
403
  return
335
404
  }
405
+ if (version === 2) validateDeliveryLineage(delivery, where, errors)
336
406
  if (typeof delivery.id !== 'string' || !delivery.id) addError(errors, 'schema: ' + where + '.id must be a non-empty string')
337
407
  if (!('title' in delivery) || !(delivery.title === null || typeof delivery.title === 'string')) {
338
408
  addError(errors, 'schema: ' + where + '.title must be a string or null')
@@ -428,33 +498,122 @@ function validateAnchor(anchor, index, sums, errors) {
428
498
  }
429
499
  }
430
500
 
431
- function validateEvidenceSource(source, where, errors) {
432
- if (!isPlainObject(source)) {
433
- addError(errors, 'schema: ' + where + ' must be an object')
501
+ const TYPED_ITEM_STRING_FIELDS = Object.freeze(['ref', 'sessionId', 'turnId', 'messageId', 'subject', 'docId', 'pageId', 'url', 'tool', 'chunkId'])
502
+
503
+ function validateEvidenceQuote(source, where, errors) {
504
+ if (typeof source.quote !== 'string') {
505
+ addError(errors, 'schema: ' + where + '.quote must be a string')
434
506
  return
435
507
  }
436
- if (typeof source.sessionId !== 'string') addError(errors, 'schema: ' + where + '.sessionId must be a string')
437
- if (typeof source.turnId !== 'string') addError(errors, 'schema: ' + where + '.turnId must be a string')
438
- if (typeof source.quote !== 'string') addError(errors, 'schema: ' + where + '.quote must be a string')
439
- else {
440
- if (Array.from(source.quote).length > MAX_QUOTE_CHARS) {
441
- addError(errors, 'schema: ' + where + '.quote is longer than ' + MAX_QUOTE_CHARS + ' characters')
442
- }
443
- if (!isSha256Hex(source.quoteSha256)) addError(errors, 'schema: ' + where + '.quoteSha256 must be 64 lowercase hex characters')
444
- else if (typeof source.quote === 'string' && sha256Hex(source.quote) !== source.quoteSha256) {
445
- addError(errors, 'evidence quoteSha256 mismatch at ' + where)
446
- }
508
+ if (Array.from(source.quote).length > MAX_QUOTE_CHARS) {
509
+ addError(errors, 'schema: ' + where + '.quote is longer than ' + MAX_QUOTE_CHARS + ' characters')
510
+ }
511
+ if (!isSha256Hex(source.quoteSha256)) addError(errors, 'schema: ' + where + '.quoteSha256 must be 64 lowercase hex characters')
512
+ else if (sha256Hex(source.quote) !== source.quoteSha256) {
513
+ addError(errors, 'evidence quoteSha256 mismatch at ' + where)
447
514
  }
448
515
  if (!isNonNegativeInteger(source.quoteChars)) addError(errors, 'schema: ' + where + '.quoteChars must be a non-negative integer')
449
- else if (typeof source.quote === 'string' && source.quoteChars < Array.from(source.quote).length) {
516
+ else if (source.quoteChars < Array.from(source.quote).length) {
450
517
  addError(errors, 'schema: ' + where + '.quoteChars is smaller than the stored quote')
451
518
  }
452
519
  if (typeof source.truncated !== 'boolean') addError(errors, 'schema: ' + where + '.truncated must be a boolean')
520
+ }
521
+
522
+ function validateEvidenceSourceV2(source, where, errors) {
523
+ if (!isPlainObject(source)) {
524
+ addError(errors, 'schema: ' + where + ' must be an object')
525
+ return
526
+ }
527
+ if (!EVIDENCE_KINDS.includes(source.kind)) {
528
+ addError(errors, 'schema: ' + where + '.kind must be one of ' + EVIDENCE_KINDS.map((kind) => '"' + kind + '"').join(', '))
529
+ }
530
+ if (typeof source.type !== 'string' || !source.type) addError(errors, 'schema: ' + where + '.type must be a non-empty string')
531
+ if (!EVIDENCE_RESOLUTIONS.includes(source.resolution)) {
532
+ addError(errors, 'schema: ' + where + '.resolution must be one of ' + EVIDENCE_RESOLUTIONS.map((value) => '"' + value + '"').join(', '))
533
+ } else if (source.resolution !== 'verified') {
534
+ // The sources[] array is "locally resolved at build time". Unverifiable or
535
+ // not_checked items belong in their own arrays; a pack must not smuggle
536
+ // manual/unverifiable evidence in as a source.
537
+ addError(errors, 'schema: ' + where + '.resolution must be "verified" for entries in sources[]')
538
+ }
539
+ if ((source.kind === 'manual' || source.type === 'manual') && source.resolution === 'verified') {
540
+ addError(errors, 'manual evidence must not be marked verified: ' + where)
541
+ }
542
+ if (source.type === 'conversation') {
543
+ if (typeof source.sessionId !== 'string') addError(errors, 'schema: ' + where + '.sessionId must be a string')
544
+ if (typeof source.turnId !== 'string') addError(errors, 'schema: ' + where + '.turnId must be a string')
545
+ } else {
546
+ if ('sessionId' in source && typeof source.sessionId !== 'string') addError(errors, 'schema: ' + where + '.sessionId must be a string')
547
+ if ('turnId' in source && typeof source.turnId !== 'string') addError(errors, 'schema: ' + where + '.turnId must be a string')
548
+ }
549
+ validateEvidenceQuote(source, where, errors)
550
+ if ('quoteVerified' in source && typeof source.quoteVerified !== 'boolean') {
551
+ addError(errors, 'schema: ' + where + '.quoteVerified must be a boolean')
552
+ }
553
+ for (const key of TYPED_ITEM_STRING_FIELDS) {
554
+ if (key in source && typeof source[key] !== 'string') addError(errors, 'schema: ' + where + '.' + key + ' must be a string')
555
+ }
556
+ if ('retained' in source && typeof source.retained !== 'boolean') addError(errors, 'schema: ' + where + '.retained must be a boolean')
557
+ if ('chunkIndex' in source && !isNonNegativeInteger(source.chunkIndex)) {
558
+ addError(errors, 'schema: ' + where + '.chunkIndex must be a non-negative integer')
559
+ }
560
+ if ('sourceDigest' in source && !isSha256Hex(source.sourceDigest)) {
561
+ addError(errors, 'schema: ' + where + '.sourceDigest must be 64 lowercase hex characters')
562
+ }
563
+ if ('turnSeq' in source && !Number.isInteger(source.turnSeq)) addError(errors, 'schema: ' + where + '.turnSeq must be an integer')
564
+ if ('turnRole' in source && typeof source.turnRole !== 'string') addError(errors, 'schema: ' + where + '.turnRole must be a string')
565
+ }
566
+
567
+ function validateEvidenceSource(source, where, errors, version) {
568
+ if (version >= 2) {
569
+ validateEvidenceSourceV2(source, where, errors)
570
+ return
571
+ }
572
+ if (!isPlainObject(source)) {
573
+ addError(errors, 'schema: ' + where + ' must be an object')
574
+ return
575
+ }
576
+ if (typeof source.sessionId !== 'string') addError(errors, 'schema: ' + where + '.sessionId must be a string')
577
+ if (typeof source.turnId !== 'string') addError(errors, 'schema: ' + where + '.turnId must be a string')
578
+ validateEvidenceQuote(source, where, errors)
453
579
  if ('turnSeq' in source && !Number.isInteger(source.turnSeq)) addError(errors, 'schema: ' + where + '.turnSeq must be an integer')
454
580
  if ('turnRole' in source && typeof source.turnRole !== 'string') addError(errors, 'schema: ' + where + '.turnRole must be a string')
455
581
  }
456
582
 
457
- function validateEvidenceEntry(entry, index, errors) {
583
+ function validateEvidenceTypedItem(item, where, errors, { requireKind = false } = {}) {
584
+ if (!isPlainObject(item)) {
585
+ addError(errors, 'schema: ' + where + ' must be an object')
586
+ return false
587
+ }
588
+ if (typeof item.reason !== 'string' || !item.reason) addError(errors, 'schema: ' + where + '.reason must be a non-empty string')
589
+ if (requireKind && !EVIDENCE_KINDS.includes(item.kind)) {
590
+ addError(errors, 'schema: ' + where + '.kind must be one of ' + EVIDENCE_KINDS.map((kind) => '"' + kind + '"').join(', '))
591
+ } else if ('kind' in item && !EVIDENCE_KINDS.includes(item.kind)) {
592
+ addError(errors, 'schema: ' + where + '.kind must be one of ' + EVIDENCE_KINDS.map((kind) => '"' + kind + '"').join(', '))
593
+ }
594
+ if ('type' in item && (typeof item.type !== 'string' || !item.type)) addError(errors, 'schema: ' + where + '.type must be a non-empty string')
595
+ for (const key of TYPED_ITEM_STRING_FIELDS) {
596
+ if (key in item && typeof item[key] !== 'string') addError(errors, 'schema: ' + where + '.' + key + ' must be a string')
597
+ }
598
+ if ('retained' in item && typeof item.retained !== 'boolean') addError(errors, 'schema: ' + where + '.retained must be a boolean')
599
+ if ('chunkIndex' in item && !isNonNegativeInteger(item.chunkIndex)) {
600
+ addError(errors, 'schema: ' + where + '.chunkIndex must be a non-negative integer')
601
+ }
602
+ if ('quote' in item) {
603
+ if (typeof item.quote !== 'string') addError(errors, 'schema: ' + where + '.quote must be a string')
604
+ else {
605
+ if (Array.from(item.quote).length > MAX_QUOTE_CHARS) {
606
+ addError(errors, 'schema: ' + where + '.quote is longer than ' + MAX_QUOTE_CHARS + ' characters')
607
+ }
608
+ if (!isSha256Hex(item.quoteSha256) || sha256Hex(item.quote) !== item.quoteSha256) {
609
+ addError(errors, 'evidence quoteSha256 mismatch at ' + where)
610
+ }
611
+ }
612
+ }
613
+ return true
614
+ }
615
+
616
+ function validateEvidenceEntry(entry, index, errors, version) {
458
617
  const where = 'manifest.evidence.entries[' + index + ']'
459
618
  if (!isPlainObject(entry)) {
460
619
  addError(errors, 'schema: ' + where + ' must be an object')
@@ -464,11 +623,15 @@ function validateEvidenceEntry(entry, index, errors) {
464
623
  if (typeof entry.id !== 'string') addError(errors, 'schema: ' + where + '.id must be a string')
465
624
  if (typeof entry.found !== 'boolean') addError(errors, 'schema: ' + where + '.found must be a boolean')
466
625
  if (!Array.isArray(entry.sources)) addError(errors, 'schema: ' + where + '.sources must be an array')
467
- else entry.sources.forEach((source, sourceIndex) => validateEvidenceSource(source, where + '.sources[' + sourceIndex + ']', errors))
626
+ else entry.sources.forEach((source, sourceIndex) => validateEvidenceSource(source, where + '.sources[' + sourceIndex + ']', errors, version))
468
627
  if (!Array.isArray(entry.missing)) addError(errors, 'schema: ' + where + '.missing must be an array')
469
628
  else {
470
629
  entry.missing.forEach((item, itemIndex) => {
471
630
  const itemWhere = where + '.missing[' + itemIndex + ']'
631
+ if (version >= 2) {
632
+ validateEvidenceTypedItem(item, itemWhere, errors)
633
+ return
634
+ }
472
635
  if (!isPlainObject(item)) {
473
636
  addError(errors, 'schema: ' + itemWhere + ' must be an object')
474
637
  return
@@ -490,6 +653,85 @@ function validateEvidenceEntry(entry, index, errors) {
490
653
  }
491
654
  })
492
655
  }
656
+ for (const key of ['unverifiable', 'notChecked']) {
657
+ if (!(key in entry)) continue
658
+ if (!Array.isArray(entry[key])) {
659
+ addError(errors, 'schema: ' + where + '.' + key + ' must be an array')
660
+ continue
661
+ }
662
+ entry[key].forEach((item, itemIndex) => {
663
+ validateEvidenceTypedItem(item, where + '.' + key + '[' + itemIndex + ']', errors, { requireKind: key === 'notChecked' })
664
+ })
665
+ }
666
+ }
667
+
668
+ function tallyEvidenceItems(entries) {
669
+ const byKind = {}
670
+ for (const kind of EVIDENCE_KINDS) byKind[kind] = 0
671
+ let sourceCount = 0
672
+ let missingCount = 0
673
+ let unverifiableCount = 0
674
+ let operatorEnteredCount = 0
675
+ let notCheckedCount = 0
676
+ for (const entry of entries) {
677
+ if (!isPlainObject(entry)) continue
678
+ for (const key of ['sources', 'missing', 'unverifiable', 'notChecked']) {
679
+ const list = Array.isArray(entry[key]) ? entry[key] : []
680
+ if (key === 'sources') sourceCount += list.length
681
+ else if (key === 'missing') missingCount += list.length
682
+ else if (key === 'unverifiable') unverifiableCount += list.length
683
+ else notCheckedCount += list.length
684
+ for (const item of list) {
685
+ const kind = EVIDENCE_KINDS.includes(item?.kind) ? item.kind : 'observed'
686
+ byKind[kind] += 1
687
+ if (key === 'unverifiable' && (item?.kind === 'manual' || item?.type === 'manual')) operatorEnteredCount += 1
688
+ }
689
+ }
690
+ }
691
+ return { byKind, sourceCount, missingCount, unverifiableCount, operatorEnteredCount, notCheckedCount }
692
+ }
693
+
694
+ function validateEvidenceV2(evidence, errors) {
695
+ const tallied = tallyEvidenceItems(evidence.entries)
696
+ if (!isNonNegativeInteger(evidence.notCheckedCount)) {
697
+ addError(errors, 'schema: manifest.evidence.notCheckedCount must be a non-negative integer (v2)')
698
+ } else if (evidence.notCheckedCount !== tallied.notCheckedCount) {
699
+ addError(errors, 'schema: manifest.evidence.notCheckedCount disagrees with the entries')
700
+ }
701
+ if (!isNonNegativeInteger(evidence.unverifiableCount)) {
702
+ addError(errors, 'schema: manifest.evidence.unverifiableCount must be a non-negative integer (v2)')
703
+ } else if (evidence.unverifiableCount !== tallied.unverifiableCount) {
704
+ addError(errors, 'schema: manifest.evidence.unverifiableCount disagrees with the entries')
705
+ }
706
+ if (!isNonNegativeInteger(evidence.operatorEnteredCount)) {
707
+ addError(errors, 'schema: manifest.evidence.operatorEnteredCount must be a non-negative integer (v2)')
708
+ } else if (evidence.operatorEnteredCount !== tallied.operatorEnteredCount) {
709
+ addError(errors, 'schema: manifest.evidence.operatorEnteredCount disagrees with the entries')
710
+ }
711
+ if (!isPlainObject(evidence.byKind)) {
712
+ addError(errors, 'schema: manifest.evidence.byKind must be an object (v2)')
713
+ } else {
714
+ let declaredTotal = 0
715
+ for (const kind of Object.keys(evidence.byKind)) {
716
+ if (!EVIDENCE_KINDS.includes(kind)) addError(errors, 'schema: manifest.evidence.byKind has an unknown kind: ' + kind)
717
+ }
718
+ for (const kind of EVIDENCE_KINDS) {
719
+ const value = evidence.byKind[kind]
720
+ if (!isNonNegativeInteger(value)) {
721
+ addError(errors, 'schema: manifest.evidence.byKind.' + kind + ' must be a non-negative integer')
722
+ } else {
723
+ declaredTotal += value
724
+ if (value !== tallied.byKind[kind]) {
725
+ addError(errors, 'schema: manifest.evidence.byKind.' + kind + ' disagrees with the entries')
726
+ }
727
+ }
728
+ }
729
+ const totalItems = tallied.sourceCount + tallied.missingCount + tallied.unverifiableCount + tallied.notCheckedCount
730
+ if (declaredTotal !== totalItems) addError(errors, 'schema: manifest.evidence.byKind does not sum to the evidence item total')
731
+ }
732
+ if (typeof evidence.verificationBasis !== 'string' || !VERIFICATION_BASES.includes(evidence.verificationBasis)) {
733
+ addError(errors, 'schema: manifest.evidence.verificationBasis must be one of ' + VERIFICATION_BASES.map((value) => '"' + value + '"').join(', ') + ' (v2)')
734
+ }
493
735
  }
494
736
 
495
737
  function validateEvidence(evidence, errors) {
@@ -498,12 +740,15 @@ function validateEvidence(evidence, errors) {
498
740
  return
499
741
  }
500
742
  if (typeof evidence.format !== 'string' || !evidence.format) addError(errors, 'schema: manifest.evidence.format must be a non-empty string')
501
- if (!Number.isInteger(evidence.version) || evidence.version < 1) addError(errors, 'schema: manifest.evidence.version must be a positive integer')
743
+ if (!Number.isInteger(evidence.version) || !EVIDENCE_PACK_VERSIONS.includes(evidence.version)) {
744
+ addError(errors, 'schema: manifest.evidence.version must be ' + EVIDENCE_PACK_VERSIONS.join(' or ') + ' (got ' + JSON.stringify(evidence.version) + ')')
745
+ }
502
746
  if (!Array.isArray(evidence.entries)) {
503
747
  addError(errors, 'schema: manifest.evidence.entries must be an array')
504
748
  return
505
749
  }
506
- evidence.entries.forEach((entry, index) => validateEvidenceEntry(entry, index, errors))
750
+ const version = Number.isInteger(evidence.version) ? evidence.version : 1
751
+ evidence.entries.forEach((entry, index) => validateEvidenceEntry(entry, index, errors, version))
507
752
  if (evidence.entryCount !== evidence.entries.length) {
508
753
  addError(errors, 'schema: manifest.evidence.entryCount must equal the number of entries')
509
754
  }
@@ -540,6 +785,7 @@ function validateEvidence(evidence, errors) {
540
785
  }
541
786
  if (declared !== evidence.entries.length) addError(errors, 'schema: manifest.evidence.counts do not sum to entryCount')
542
787
  }
788
+ if (version >= 2) validateEvidenceV2(evidence, errors)
543
789
  }
544
790
 
545
791
  function validateReceiptManifest(manifest, sums, errors, result) {
@@ -550,12 +796,23 @@ function validateReceiptManifest(manifest, sums, errors, result) {
550
796
  if (manifest.format !== RECEIPT_MANIFEST_FORMAT) {
551
797
  addError(errors, 'schema: manifest.format must be ' + RECEIPT_MANIFEST_FORMAT)
552
798
  }
553
- if (manifest.version !== RECEIPT_MANIFEST_VERSION) {
554
- addError(errors, 'schema: manifest.version must be ' + RECEIPT_MANIFEST_VERSION)
799
+ // v1 packs were written by 0.2.x/0.3.x and must keep verifying exactly as
800
+ // before; v2 adds subject/scope/lineage checks below. Anything else is drift.
801
+ if (!RECEIPT_MANIFEST_VERSIONS.includes(manifest.version)) {
802
+ addError(
803
+ errors,
804
+ 'schema: manifest.version must be ' + RECEIPT_MANIFEST_VERSIONS.join(' or ') + ' (got ' + JSON.stringify(manifest.version) + ')',
805
+ )
555
806
  }
556
807
  if (!isIsoDate(manifest.createdAt)) addError(errors, 'schema: manifest.createdAt must be an ISO-8601 UTC timestamp')
557
808
  validateGenerator(manifest.generator, errors)
558
809
  validateRecipient(manifest.recipient, errors)
810
+ const version = manifest.version
811
+ if (version === 2) {
812
+ if (!('scope' in manifest)) addError(errors, 'schema: manifest.scope is required for manifest version 2')
813
+ else validateScope(manifest.scope, errors)
814
+ if ('subject' in manifest) validateSubject(manifest.subject, errors)
815
+ }
559
816
 
560
817
  const deliveryFiles = new Map()
561
818
  if (!Array.isArray(manifest.deliveries) || manifest.deliveries.length === 0) {
@@ -567,7 +824,7 @@ function validateReceiptManifest(manifest, sums, errors, result) {
567
824
  if (ids.has(delivery.id)) addError(errors, 'schema: manifest.deliveries[' + index + '].id is duplicated: ' + delivery.id)
568
825
  else ids.add(delivery.id)
569
826
  }
570
- validateDelivery(delivery, index, sums, deliveryFiles, errors)
827
+ validateDelivery(delivery, index, sums, deliveryFiles, errors, version)
571
828
  })
572
829
  }
573
830