advisers 0.3.2 → 0.4.1

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 (44) hide show
  1. package/README-zh.md +13 -14
  2. package/README.md +37 -23
  3. package/THIRD-PARTY-NOTICES.md +1 -1
  4. package/bin/advisers.js +2302 -95
  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/error-codes.js +516 -7
  11. package/lib/evidence.js +697 -113
  12. package/lib/kb-extract.js +626 -0
  13. package/lib/kb-facts.js +429 -0
  14. package/lib/kb-pages.js +1084 -0
  15. package/lib/kb.js +1036 -0
  16. package/lib/mail-errors.js +46 -0
  17. package/lib/mail-extract.js +351 -0
  18. package/lib/mail-imap.js +446 -0
  19. package/lib/mail-import.js +176 -0
  20. package/lib/mail-parse.js +223 -0
  21. package/lib/mail-store.js +731 -0
  22. package/lib/mail.js +506 -0
  23. package/lib/mcp-server.js +196 -10
  24. package/lib/notify.js +265 -7
  25. package/lib/policy/decision.js +315 -0
  26. package/lib/policy/egress.js +268 -0
  27. package/lib/policy/errors.js +71 -0
  28. package/lib/policy/index.js +99 -0
  29. package/lib/policy/rules.js +468 -0
  30. package/lib/policy/surfaces.js +141 -0
  31. package/lib/receipt-verify.js +199 -19
  32. package/lib/receivables.js +1653 -0
  33. package/lib/scheduler.js +453 -9
  34. package/lib/shell.js +1406 -0
  35. package/lib/source.js +98 -3
  36. package/lib/sqlite.js +366 -49
  37. package/lib/tos/index.js +2 -2
  38. package/lib/tos/manifest.json +6 -6
  39. package/lib/tos/privacy.zh-CN.md +33 -20
  40. package/lib/tos/safety.zh-CN.md +7 -6
  41. package/lib/tos/summary.zh-CN.md +3 -2
  42. package/lib/tos/terms.zh-CN.md +31 -16
  43. package/lib/urgency.js +467 -0
  44. package/package.json +4 -4
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Policy surface registry (0.4 subset).
3
+ *
4
+ * The 0.2/0.3 "six surfaces" list in docs/ARCHITECTURE.md is a design sketch;
5
+ * 0.4 ships a registry instead, because mail/KB/TUI/notify add data surfaces and
6
+ * a matrix must be generated, never hand-maintained. Every row records the
7
+ * honest gate state for that surface in 0.4:
8
+ *
9
+ * enforced-policy goes through lib/policy evaluate()/authorize()
10
+ * legacy-gate pre-0.4 gate only (active-matter egress, TOS, clearance)
11
+ * self-attested local-only path with no egress; the docs claim no more
12
+ * not-wired registered but not enabled in 0.4 (default off)
13
+ *
14
+ * Modules are pure data: no I/O, no network, no timestamps. lib/policy/index.js
15
+ * publishes surfaceMatrix() for `privacy --matrix` and for docs/contract gates.
16
+ */
17
+
18
+ export const POLICY_VERSION = 1
19
+ export const RULESET_VERSION = '2026.11.4'
20
+
21
+ /** Gate states; kept in sync with docs/ARCHITECTURE.md and SECURITY.md rows. */
22
+ export const SURFACE_GATES = Object.freeze(['enforced-policy', 'legacy-gate', 'self-attested', 'not-wired'])
23
+
24
+ /** Default enablement of a surface in 0.4. */
25
+ export const SURFACE_DEFAULTS = Object.freeze(['on', 'off'])
26
+
27
+ /**
28
+ * One row per surface.
29
+ *
30
+ * actions canonical policy actions the surface may request
31
+ * dataClass the most sensitive data class the surface can carry
32
+ * sink where data can leave the process ('none' for read-only)
33
+ * default on = usable after the normal gates; off = requires an explicit
34
+ * local enablement (consent entry / config flag)
35
+ * gate honest wiring state in 0.4 (see header)
36
+ * tosRef clause covering this surface (docs/TOS.md)
37
+ * doc user-facing documentation
38
+ */
39
+ export const SURFACE_REGISTRY = Object.freeze([
40
+ Object.freeze({ surface: 'chat', description: 'interactive model chat', actions: Object.freeze(['model-egress']), dataClass: 'conversation', sink: 'model', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
41
+ Object.freeze({ surface: 'capture', description: 'model-assisted candidate extraction', actions: Object.freeze(['model-egress']), dataClass: 'conversation', sink: 'model', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/SOURCE-SCHEMA.md' }),
42
+ Object.freeze({ surface: 'research', description: 'web research via the configured model', actions: Object.freeze(['model-egress']), dataClass: 'conversation', sink: 'model', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
43
+ Object.freeze({ surface: 'agent', description: 'agent tool loop (read/write/exec)', actions: Object.freeze(['read', 'write-fs', 'write-ledger', 'exec', 'model-egress']), dataClass: 'tool', sink: 'spawn', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#5', doc: 'docs/SAFETY.md' }),
44
+ Object.freeze({ surface: 'mcp', description: 'MCP read-only projection', actions: Object.freeze(['read']), dataClass: 'ledger', sink: 'none', default: 'on', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/MCP.md' }),
45
+ Object.freeze({ surface: 'dsh', description: 'DeepSeek Harness read-only projection', actions: Object.freeze(['read']), dataClass: 'ledger', sink: 'none', default: 'on', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/PLUGIN-BOUNDARY.md' }),
46
+ Object.freeze({ surface: 'pet-brief', description: 'portable brief for consumer agents', actions: Object.freeze(['read', 'export']), dataClass: 'ledger', sink: 'fs', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/PET-BRIEF-CONSUMERS.md' }),
47
+ Object.freeze({ surface: 'export', description: 'evidence/export packs and delivery receipts', actions: Object.freeze(['export']), dataClass: 'ledger', sink: 'fs', default: 'on', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/DELIVERY-RECEIPT.md' }),
48
+ Object.freeze({ surface: 'mail.sync', description: 'IMAP sync to the user-configured mailbox', actions: Object.freeze(['net-egress']), dataClass: 'mail', sink: 'net', default: 'on', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
49
+ Object.freeze({ surface: 'mail.extract', description: 'mail candidate extraction (local rules or explicit model opt-in)', actions: Object.freeze(['read', 'model-egress']), dataClass: 'mail', sink: 'model', default: 'off', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
50
+ Object.freeze({ surface: 'kb.ingest', description: 'local KB ingest (no egress)', actions: Object.freeze(['read', 'write-ledger']), dataClass: 'kb', sink: 'fs', default: 'on', gate: 'self-attested', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
51
+ Object.freeze({ surface: 'kb.search', description: 'local KB search (clearance-filtered)', actions: Object.freeze(['read']), dataClass: 'kb', sink: 'none', default: 'on', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
52
+ Object.freeze({ surface: 'kb.ask', description: 'model-assisted KB answers (explicit opt-in)', actions: Object.freeze(['read', 'model-egress']), dataClass: 'kb', sink: 'model', default: 'off', gate: 'enforced-policy', tosRef: 'docs/TOS.md#9.3', doc: 'docs/ARCHITECTURE.md' }),
53
+ Object.freeze({ surface: 'notify.webhook', description: 'outbound notification webhook', actions: Object.freeze(['net-egress']), dataClass: 'reminder', sink: 'net', default: 'off', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/REMINDERS.md' }),
54
+ Object.freeze({ surface: 'bridge', description: 'Hermes bridge file exchange', actions: Object.freeze(['read', 'net-egress', 'send']), dataClass: 'conversation', sink: 'net', default: 'off', gate: 'legacy-gate', tosRef: 'docs/TOS.md#9.3', doc: 'docs/PLUGIN-BOUNDARY.md' }),
55
+ Object.freeze({ surface: 'tui', description: 'TUI shell dispatch (no data egress of its own)', actions: Object.freeze(['read']), dataClass: 'ledger', sink: 'none', default: 'on', gate: 'self-attested', tosRef: 'docs/TOS.md#9.3', doc: 'docs/TUI.md' }),
56
+ ])
57
+
58
+ /** Canonical policy actions understood by evaluate(). */
59
+ export const POLICY_ACTIONS = Object.freeze([
60
+ 'read', 'write-ledger', 'write-fs', 'exec', 'send', 'model-egress', 'net-egress', 'export', 'delete',
61
+ ])
62
+
63
+ /** Canonical data classes carried by a policy decision. */
64
+ export const DATA_CLASSES = Object.freeze([
65
+ 'conversation', 'ledger', 'mail', 'kb', 'attachment', 'reminder', 'web', 'tool', 'credential', 'unknown',
66
+ ])
67
+
68
+ /** Actions that need a TOS acceptance fact before they are allowed. */
69
+ export const TOS_REQUIRED_ACTIONS = Object.freeze([
70
+ 'read', 'write-ledger', 'write-fs', 'exec', 'send', 'model-egress', 'net-egress', 'export', 'delete',
71
+ ])
72
+
73
+ /** Actions that leave the process / mutate durable state (audit + fail-closed). */
74
+ export const SIDE_EFFECT_ACTIONS = Object.freeze([
75
+ 'write-ledger', 'write-fs', 'exec', 'send', 'model-egress', 'net-egress', 'export', 'delete',
76
+ ])
77
+
78
+ /** Actions that egress data to a model or the network. */
79
+ export const EGRESS_ACTIONS = Object.freeze(['model-egress', 'net-egress', 'send'])
80
+
81
+ /** Read-only projections that respect per-record clearance. */
82
+ export const CLEARANCE_ACTIONS = Object.freeze(['read', 'export'])
83
+
84
+ /**
85
+ * Actions whose input content must carry a safety verdict before they are
86
+ * allowed. Legacy (0.4) call sites that already ran inspectCaptureText / the
87
+ * safety classifier pass {action:'allow'}; new data surfaces fail closed when
88
+ * the verdict is absent.
89
+ */
90
+ export const SAFETY_REQUIRED_ACTIONS = Object.freeze(['model-egress', 'send', 'export'])
91
+
92
+ /** Actions whose target path must be classified (workspace boundary enforcement). */
93
+ export const PATH_ACTIONS = Object.freeze(['write-fs', 'delete'])
94
+
95
+ const SURFACE_BY_NAME = new Map(SURFACE_REGISTRY.map((row) => [row.surface, row]))
96
+
97
+ /** Canonical surface name or null (case/space tolerant, never guesses). */
98
+ export function normalizeSurface(value) {
99
+ if (typeof value !== 'string') return null
100
+ const text = value.trim().toLowerCase()
101
+ if (!text) return null
102
+ return SURFACE_BY_NAME.has(text) ? text : null
103
+ }
104
+
105
+ /** Surface registry row or null. */
106
+ export function getSurface(value) {
107
+ const name = normalizeSurface(value)
108
+ return name ? SURFACE_BY_NAME.get(name) : null
109
+ }
110
+
111
+ /** True when the surface declares the action. */
112
+ export function surfaceSupportsAction(surface, action) {
113
+ const row = getSurface(surface)
114
+ if (!row || typeof action !== 'string') return false
115
+ return row.actions.includes(action)
116
+ }
117
+
118
+ /** True when the surface is gate 'not-wired' (registered but disabled in 0.4). */
119
+ export function surfaceNotWired(surface) {
120
+ const row = getSurface(surface)
121
+ return Boolean(row) && row.gate === 'not-wired'
122
+ }
123
+
124
+ /** Deterministic JSON-ready matrix (used by privacy --matrix and docs gates). */
125
+ export function surfaceMatrix() {
126
+ return {
127
+ policyVersion: POLICY_VERSION,
128
+ rulesetVersion: RULESET_VERSION,
129
+ rows: SURFACE_REGISTRY.map((row) => ({
130
+ surface: row.surface,
131
+ description: row.description,
132
+ actions: [...row.actions],
133
+ dataClass: row.dataClass,
134
+ sink: row.sink,
135
+ default: row.default,
136
+ gate: row.gate,
137
+ tosRef: row.tosRef,
138
+ doc: row.doc,
139
+ })),
140
+ }
141
+ }
@@ -40,6 +40,9 @@ 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,
@@ -48,6 +51,7 @@ import {
48
51
  RECEIPT_SCOPES,
49
52
  RECEIPT_SHA256SUMS_FILE,
50
53
  RECEIPT_SUBJECT_KINDS,
54
+ VERIFICATION_BASES,
51
55
  isUnsafePackPath,
52
56
  sha256Hex,
53
57
  } from './evidence.js'
@@ -494,33 +498,122 @@ function validateAnchor(anchor, index, sums, errors) {
494
498
  }
495
499
  }
496
500
 
497
- function validateEvidenceSource(source, where, errors) {
498
- if (!isPlainObject(source)) {
499
- 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')
500
506
  return
501
507
  }
502
- if (typeof source.sessionId !== 'string') addError(errors, 'schema: ' + where + '.sessionId must be a string')
503
- if (typeof source.turnId !== 'string') addError(errors, 'schema: ' + where + '.turnId must be a string')
504
- if (typeof source.quote !== 'string') addError(errors, 'schema: ' + where + '.quote must be a string')
505
- else {
506
- if (Array.from(source.quote).length > MAX_QUOTE_CHARS) {
507
- addError(errors, 'schema: ' + where + '.quote is longer than ' + MAX_QUOTE_CHARS + ' characters')
508
- }
509
- if (!isSha256Hex(source.quoteSha256)) addError(errors, 'schema: ' + where + '.quoteSha256 must be 64 lowercase hex characters')
510
- else if (typeof source.quote === 'string' && sha256Hex(source.quote) !== source.quoteSha256) {
511
- addError(errors, 'evidence quoteSha256 mismatch at ' + where)
512
- }
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)
513
514
  }
514
515
  if (!isNonNegativeInteger(source.quoteChars)) addError(errors, 'schema: ' + where + '.quoteChars must be a non-negative integer')
515
- else if (typeof source.quote === 'string' && source.quoteChars < Array.from(source.quote).length) {
516
+ else if (source.quoteChars < Array.from(source.quote).length) {
516
517
  addError(errors, 'schema: ' + where + '.quoteChars is smaller than the stored quote')
517
518
  }
518
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)
519
579
  if ('turnSeq' in source && !Number.isInteger(source.turnSeq)) addError(errors, 'schema: ' + where + '.turnSeq must be an integer')
520
580
  if ('turnRole' in source && typeof source.turnRole !== 'string') addError(errors, 'schema: ' + where + '.turnRole must be a string')
521
581
  }
522
582
 
523
- 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) {
524
617
  const where = 'manifest.evidence.entries[' + index + ']'
525
618
  if (!isPlainObject(entry)) {
526
619
  addError(errors, 'schema: ' + where + ' must be an object')
@@ -530,11 +623,15 @@ function validateEvidenceEntry(entry, index, errors) {
530
623
  if (typeof entry.id !== 'string') addError(errors, 'schema: ' + where + '.id must be a string')
531
624
  if (typeof entry.found !== 'boolean') addError(errors, 'schema: ' + where + '.found must be a boolean')
532
625
  if (!Array.isArray(entry.sources)) addError(errors, 'schema: ' + where + '.sources must be an array')
533
- 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))
534
627
  if (!Array.isArray(entry.missing)) addError(errors, 'schema: ' + where + '.missing must be an array')
535
628
  else {
536
629
  entry.missing.forEach((item, itemIndex) => {
537
630
  const itemWhere = where + '.missing[' + itemIndex + ']'
631
+ if (version >= 2) {
632
+ validateEvidenceTypedItem(item, itemWhere, errors)
633
+ return
634
+ }
538
635
  if (!isPlainObject(item)) {
539
636
  addError(errors, 'schema: ' + itemWhere + ' must be an object')
540
637
  return
@@ -556,6 +653,85 @@ function validateEvidenceEntry(entry, index, errors) {
556
653
  }
557
654
  })
558
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
+ }
559
735
  }
560
736
 
561
737
  function validateEvidence(evidence, errors) {
@@ -564,12 +740,15 @@ function validateEvidence(evidence, errors) {
564
740
  return
565
741
  }
566
742
  if (typeof evidence.format !== 'string' || !evidence.format) addError(errors, 'schema: manifest.evidence.format must be a non-empty string')
567
- 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
+ }
568
746
  if (!Array.isArray(evidence.entries)) {
569
747
  addError(errors, 'schema: manifest.evidence.entries must be an array')
570
748
  return
571
749
  }
572
- 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))
573
752
  if (evidence.entryCount !== evidence.entries.length) {
574
753
  addError(errors, 'schema: manifest.evidence.entryCount must equal the number of entries')
575
754
  }
@@ -606,6 +785,7 @@ function validateEvidence(evidence, errors) {
606
785
  }
607
786
  if (declared !== evidence.entries.length) addError(errors, 'schema: manifest.evidence.counts do not sum to entryCount')
608
787
  }
788
+ if (version >= 2) validateEvidenceV2(evidence, errors)
609
789
  }
610
790
 
611
791
  function validateReceiptManifest(manifest, sums, errors, result) {