monomind 1.11.11 → 1.11.13

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 (227) hide show
  1. package/.claude/commands/mastermind/idea.md +1 -1
  2. package/.claude/commands/mastermind/master.md +1 -1
  3. package/.claude/skills/mastermind/_protocol.md +4 -4
  4. package/.claude/skills/mastermind/architect.md +7 -4
  5. package/.claude/skills/mastermind/autodev.md +4 -2
  6. package/.claude/skills/mastermind/build.md +3 -3
  7. package/.claude/skills/mastermind/content.md +3 -3
  8. package/.claude/skills/mastermind/createorg.md +2 -2
  9. package/.claude/skills/mastermind/finance.md +3 -3
  10. package/.claude/skills/mastermind/idea.md +8 -0
  11. package/.claude/skills/mastermind/marketing.md +3 -3
  12. package/.claude/skills/mastermind/ops.md +3 -3
  13. package/.claude/skills/mastermind/release.md +3 -3
  14. package/.claude/skills/mastermind/research.md +3 -3
  15. package/.claude/skills/mastermind/review.md +3 -3
  16. package/.claude/skills/mastermind/sales.md +3 -3
  17. package/package.json +1 -1
  18. package/packages/@monomind/cli/dist/src/index.js +19 -14
  19. package/packages/@monomind/cli/dist/src/init/statusline-generator.js +3 -3
  20. package/packages/@monomind/cli/dist/src/observability/replay-reader.d.ts +1 -1
  21. package/packages/@monomind/cli/dist/src/update/checker.js +24 -7
  22. package/packages/@monomind/cli/dist/src/update/index.d.ts +10 -0
  23. package/packages/@monomind/cli/dist/src/update/index.js +29 -2
  24. package/packages/@monomind/cli/package.json +1 -1
  25. package/packages/@monomind/cli/dist/src/agents/halt-signal.d.ts +0 -25
  26. package/packages/@monomind/cli/dist/src/agents/halt-signal.js +0 -76
  27. package/packages/@monomind/cli/dist/src/agents/index.d.ts +0 -18
  28. package/packages/@monomind/cli/dist/src/agents/index.js +0 -13
  29. package/packages/@monomind/cli/dist/src/agents/managed-agent.d.ts +0 -41
  30. package/packages/@monomind/cli/dist/src/agents/managed-agent.js +0 -69
  31. package/packages/@monomind/cli/dist/src/agents/prompt-experiment.d.ts +0 -23
  32. package/packages/@monomind/cli/dist/src/agents/prompt-experiment.js +0 -49
  33. package/packages/@monomind/cli/dist/src/agents/prompt-version-manager.d.ts +0 -22
  34. package/packages/@monomind/cli/dist/src/agents/prompt-version-manager.js +0 -80
  35. package/packages/@monomind/cli/dist/src/agents/registry-query.d.ts +0 -71
  36. package/packages/@monomind/cli/dist/src/agents/registry-query.js +0 -125
  37. package/packages/@monomind/cli/dist/src/agents/score-decay.d.ts +0 -19
  38. package/packages/@monomind/cli/dist/src/agents/score-decay.js +0 -22
  39. package/packages/@monomind/cli/dist/src/agents/shared-instructions-loader.d.ts +0 -13
  40. package/packages/@monomind/cli/dist/src/agents/shared-instructions-loader.js +0 -40
  41. package/packages/@monomind/cli/dist/src/agents/specialization-scorer.d.ts +0 -54
  42. package/packages/@monomind/cli/dist/src/agents/specialization-scorer.js +0 -212
  43. package/packages/@monomind/cli/dist/src/agents/termination-watcher.d.ts +0 -30
  44. package/packages/@monomind/cli/dist/src/agents/termination-watcher.js +0 -84
  45. package/packages/@monomind/cli/dist/src/agents/trigger-index.d.ts +0 -20
  46. package/packages/@monomind/cli/dist/src/agents/trigger-index.js +0 -38
  47. package/packages/@monomind/cli/dist/src/agents/trigger-scanner.d.ts +0 -64
  48. package/packages/@monomind/cli/dist/src/agents/trigger-scanner.js +0 -308
  49. package/packages/@monomind/cli/dist/src/agents/version-diff.d.ts +0 -18
  50. package/packages/@monomind/cli/dist/src/agents/version-diff.js +0 -64
  51. package/packages/@monomind/cli/dist/src/agents/version-store.d.ts +0 -60
  52. package/packages/@monomind/cli/dist/src/agents/version-store.js +0 -235
  53. package/packages/@monomind/cli/dist/src/benchmarks/pretrain/index.d.ts +0 -45
  54. package/packages/@monomind/cli/dist/src/benchmarks/pretrain/index.js +0 -404
  55. package/packages/@monomind/cli/dist/src/commands/agent-wasm.d.ts +0 -14
  56. package/packages/@monomind/cli/dist/src/commands/agent-wasm.js +0 -333
  57. package/packages/@monomind/cli/dist/src/commands/ui.js +0 -68
  58. package/packages/@monomind/cli/dist/src/consensus/index.d.ts +0 -7
  59. package/packages/@monomind/cli/dist/src/consensus/index.js +0 -6
  60. package/packages/@monomind/cli/dist/src/context/context-provider.d.ts +0 -44
  61. package/packages/@monomind/cli/dist/src/context/context-provider.js +0 -25
  62. package/packages/@monomind/cli/dist/src/context/git-state-provider.d.ts +0 -12
  63. package/packages/@monomind/cli/dist/src/context/git-state-provider.js +0 -34
  64. package/packages/@monomind/cli/dist/src/context/index.d.ts +0 -12
  65. package/packages/@monomind/cli/dist/src/context/index.js +0 -12
  66. package/packages/@monomind/cli/dist/src/context/project-conventions-provider.d.ts +0 -15
  67. package/packages/@monomind/cli/dist/src/context/project-conventions-provider.js +0 -19
  68. package/packages/@monomind/cli/dist/src/context/prompt-assembler.d.ts +0 -26
  69. package/packages/@monomind/cli/dist/src/context/prompt-assembler.js +0 -93
  70. package/packages/@monomind/cli/dist/src/context/task-history-provider.d.ts +0 -24
  71. package/packages/@monomind/cli/dist/src/context/task-history-provider.js +0 -32
  72. package/packages/@monomind/cli/dist/src/context/user-preferences-provider.d.ts +0 -14
  73. package/packages/@monomind/cli/dist/src/context/user-preferences-provider.js +0 -27
  74. package/packages/@monomind/cli/dist/src/dlq/dlq-reader.d.ts +0 -31
  75. package/packages/@monomind/cli/dist/src/dlq/dlq-reader.js +0 -81
  76. package/packages/@monomind/cli/dist/src/dlq/dlq-writer.d.ts +0 -24
  77. package/packages/@monomind/cli/dist/src/dlq/dlq-writer.js +0 -65
  78. package/packages/@monomind/cli/dist/src/dlq/index.d.ts +0 -10
  79. package/packages/@monomind/cli/dist/src/dlq/index.js +0 -7
  80. package/packages/@monomind/cli/dist/src/eval/dataset-manager.d.ts +0 -33
  81. package/packages/@monomind/cli/dist/src/eval/dataset-manager.js +0 -107
  82. package/packages/@monomind/cli/dist/src/eval/dataset-runner.d.ts +0 -23
  83. package/packages/@monomind/cli/dist/src/eval/dataset-runner.js +0 -59
  84. package/packages/@monomind/cli/dist/src/eval/index.d.ts +0 -10
  85. package/packages/@monomind/cli/dist/src/eval/index.js +0 -7
  86. package/packages/@monomind/cli/dist/src/eval/trace-collector.d.ts +0 -40
  87. package/packages/@monomind/cli/dist/src/eval/trace-collector.js +0 -102
  88. package/packages/@monomind/cli/dist/src/infrastructure/in-memory-repositories.d.ts +0 -68
  89. package/packages/@monomind/cli/dist/src/infrastructure/in-memory-repositories.js +0 -264
  90. package/packages/@monomind/cli/dist/src/interactive/interrupt.d.ts +0 -22
  91. package/packages/@monomind/cli/dist/src/interactive/interrupt.js +0 -71
  92. package/packages/@monomind/cli/dist/src/mcp/deprecation-injector.d.ts +0 -25
  93. package/packages/@monomind/cli/dist/src/mcp/deprecation-injector.js +0 -48
  94. package/packages/@monomind/cli/dist/src/mcp/tool-registry.d.ts +0 -61
  95. package/packages/@monomind/cli/dist/src/mcp/tool-registry.js +0 -246
  96. package/packages/@monomind/cli/dist/src/mcp-tools/wasm-agent-tools.d.ts +0 -9
  97. package/packages/@monomind/cli/dist/src/mcp-tools/wasm-agent-tools.js +0 -230
  98. package/packages/@monomind/cli/dist/src/model/complexity-scorer.d.ts +0 -21
  99. package/packages/@monomind/cli/dist/src/model/complexity-scorer.js +0 -106
  100. package/packages/@monomind/cli/dist/src/model/index.d.ts +0 -4
  101. package/packages/@monomind/cli/dist/src/model/index.js +0 -4
  102. package/packages/@monomind/cli/dist/src/model/model-settings.d.ts +0 -22
  103. package/packages/@monomind/cli/dist/src/model/model-settings.js +0 -33
  104. package/packages/@monomind/cli/dist/src/model/model-tier-resolver.d.ts +0 -24
  105. package/packages/@monomind/cli/dist/src/model/model-tier-resolver.js +0 -65
  106. package/packages/@monomind/cli/dist/src/monovector/capabilities.d.ts +0 -34
  107. package/packages/@monomind/cli/dist/src/monovector/capabilities.js +0 -37
  108. package/packages/@monomind/cli/dist/src/orchestration/index.d.ts +0 -7
  109. package/packages/@monomind/cli/dist/src/orchestration/index.js +0 -6
  110. package/packages/@monomind/cli/dist/src/orchestration/mode-dispatcher.d.ts +0 -11
  111. package/packages/@monomind/cli/dist/src/orchestration/mode-dispatcher.js +0 -31
  112. package/packages/@monomind/cli/dist/src/orchestration/routing-modes.d.ts +0 -68
  113. package/packages/@monomind/cli/dist/src/orchestration/routing-modes.js +0 -180
  114. package/packages/@monomind/cli/dist/src/plugins/tests/demo-plugin-store.d.ts +0 -7
  115. package/packages/@monomind/cli/dist/src/plugins/tests/demo-plugin-store.js +0 -126
  116. package/packages/@monomind/cli/dist/src/plugins/tests/standalone-test.d.ts +0 -12
  117. package/packages/@monomind/cli/dist/src/plugins/tests/standalone-test.js +0 -188
  118. package/packages/@monomind/cli/dist/src/plugins/tests/test-plugin-store.d.ts +0 -7
  119. package/packages/@monomind/cli/dist/src/plugins/tests/test-plugin-store.js +0 -206
  120. package/packages/@monomind/cli/dist/src/runtime/headless.d.ts +0 -60
  121. package/packages/@monomind/cli/dist/src/runtime/headless.js +0 -284
  122. package/packages/@monomind/cli/dist/src/services/agentic-flow-bridge.d.ts +0 -50
  123. package/packages/@monomind/cli/dist/src/services/agentic-flow-bridge.js +0 -95
  124. package/packages/@monomind/cli/dist/src/services/container-worker-pool.d.ts +0 -197
  125. package/packages/@monomind/cli/dist/src/services/container-worker-pool.js +0 -623
  126. package/packages/@monomind/cli/dist/src/services/index.d.ts +0 -13
  127. package/packages/@monomind/cli/dist/src/services/index.js +0 -11
  128. package/packages/@monomind/cli/dist/src/services/worker-queue.d.ts +0 -201
  129. package/packages/@monomind/cli/dist/src/services/worker-queue.js +0 -594
  130. package/packages/@monomind/cli/dist/src/swarm/communication-graph.d.ts +0 -25
  131. package/packages/@monomind/cli/dist/src/swarm/communication-graph.js +0 -77
  132. package/packages/@monomind/cli/dist/src/swarm/flow-enforcer.d.ts +0 -31
  133. package/packages/@monomind/cli/dist/src/swarm/flow-enforcer.js +0 -61
  134. package/packages/@monomind/cli/dist/src/swarm/flow-visualizer.d.ts +0 -19
  135. package/packages/@monomind/cli/dist/src/swarm/flow-visualizer.js +0 -68
  136. package/packages/@monomind/cli/dist/src/transfer/deploy-seraphine.d.ts +0 -13
  137. package/packages/@monomind/cli/dist/src/transfer/deploy-seraphine.js +0 -205
  138. package/packages/@monomind/cli/dist/src/transfer/store/tests/standalone-test.d.ts +0 -12
  139. package/packages/@monomind/cli/dist/src/transfer/store/tests/standalone-test.js +0 -190
  140. package/packages/@monomind/cli/dist/src/transfer/test-seraphine.d.ts +0 -6
  141. package/packages/@monomind/cli/dist/src/transfer/test-seraphine.js +0 -105
  142. package/packages/@monomind/cli/dist/src/transfer/tests/test-store.d.ts +0 -7
  143. package/packages/@monomind/cli/dist/src/transfer/tests/test-store.js +0 -214
  144. package/packages/@monomind/cli/dist/src/workflow/condition-evaluator.d.ts +0 -10
  145. package/packages/@monomind/cli/dist/src/workflow/condition-evaluator.js +0 -82
  146. package/packages/@monomind/cli/dist/src/workflow/context-resolver.d.ts +0 -12
  147. package/packages/@monomind/cli/dist/src/workflow/context-resolver.js +0 -23
  148. package/packages/@monomind/cli/dist/src/workflow/dag-builder.d.ts +0 -17
  149. package/packages/@monomind/cli/dist/src/workflow/dag-builder.js +0 -129
  150. package/packages/@monomind/cli/dist/src/workflow/dag-executor.d.ts +0 -9
  151. package/packages/@monomind/cli/dist/src/workflow/dag-executor.js +0 -116
  152. package/packages/@monomind/cli/dist/src/workflow/dag-types.d.ts +0 -41
  153. package/packages/@monomind/cli/dist/src/workflow/dag-types.js +0 -8
  154. package/packages/@monomind/cli/dist/src/workflow/dsl-parser.d.ts +0 -12
  155. package/packages/@monomind/cli/dist/src/workflow/dsl-parser.js +0 -20
  156. package/packages/@monomind/cli/dist/src/workflow/dsl-schema.d.ts +0 -165
  157. package/packages/@monomind/cli/dist/src/workflow/dsl-schema.js +0 -82
  158. package/packages/@monomind/cli/dist/src/workflow/index.d.ts +0 -13
  159. package/packages/@monomind/cli/dist/src/workflow/index.js +0 -11
  160. package/packages/@monomind/cli/dist/src/workflow/template-engine.d.ts +0 -11
  161. package/packages/@monomind/cli/dist/src/workflow/template-engine.js +0 -40
  162. package/packages/@monomind/cli/dist/src/workflow/workflow-executor.d.ts +0 -29
  163. package/packages/@monomind/cli/dist/src/workflow/workflow-executor.js +0 -227
  164. package/packages/@monomind/guidance/dist/adversarial.d.ts +0 -284
  165. package/packages/@monomind/guidance/dist/adversarial.js +0 -572
  166. package/packages/@monomind/guidance/dist/analyzer.d.ts +0 -530
  167. package/packages/@monomind/guidance/dist/analyzer.js +0 -2518
  168. package/packages/@monomind/guidance/dist/artifacts.d.ts +0 -283
  169. package/packages/@monomind/guidance/dist/artifacts.js +0 -356
  170. package/packages/@monomind/guidance/dist/authority.d.ts +0 -290
  171. package/packages/@monomind/guidance/dist/authority.js +0 -558
  172. package/packages/@monomind/guidance/dist/capabilities.d.ts +0 -209
  173. package/packages/@monomind/guidance/dist/capabilities.js +0 -485
  174. package/packages/@monomind/guidance/dist/coherence.d.ts +0 -233
  175. package/packages/@monomind/guidance/dist/coherence.js +0 -372
  176. package/packages/@monomind/guidance/dist/compiler.d.ts +0 -87
  177. package/packages/@monomind/guidance/dist/compiler.js +0 -419
  178. package/packages/@monomind/guidance/dist/conformance-kit.d.ts +0 -225
  179. package/packages/@monomind/guidance/dist/conformance-kit.js +0 -629
  180. package/packages/@monomind/guidance/dist/continue-gate.d.ts +0 -214
  181. package/packages/@monomind/guidance/dist/continue-gate.js +0 -353
  182. package/packages/@monomind/guidance/dist/crypto-utils.d.ts +0 -17
  183. package/packages/@monomind/guidance/dist/crypto-utils.js +0 -24
  184. package/packages/@monomind/guidance/dist/evolution.d.ts +0 -282
  185. package/packages/@monomind/guidance/dist/evolution.js +0 -500
  186. package/packages/@monomind/guidance/dist/gates.d.ts +0 -79
  187. package/packages/@monomind/guidance/dist/gates.js +0 -302
  188. package/packages/@monomind/guidance/dist/gateway.d.ts +0 -206
  189. package/packages/@monomind/guidance/dist/gateway.js +0 -452
  190. package/packages/@monomind/guidance/dist/generators.d.ts +0 -153
  191. package/packages/@monomind/guidance/dist/generators.js +0 -682
  192. package/packages/@monomind/guidance/dist/headless.d.ts +0 -177
  193. package/packages/@monomind/guidance/dist/headless.js +0 -342
  194. package/packages/@monomind/guidance/dist/hooks.d.ts +0 -109
  195. package/packages/@monomind/guidance/dist/hooks.js +0 -347
  196. package/packages/@monomind/guidance/dist/index.d.ts +0 -205
  197. package/packages/@monomind/guidance/dist/index.js +0 -321
  198. package/packages/@monomind/guidance/dist/ledger.d.ts +0 -162
  199. package/packages/@monomind/guidance/dist/ledger.js +0 -375
  200. package/packages/@monomind/guidance/dist/manifest-validator.d.ts +0 -289
  201. package/packages/@monomind/guidance/dist/manifest-validator.js +0 -838
  202. package/packages/@monomind/guidance/dist/memory-gate.d.ts +0 -222
  203. package/packages/@monomind/guidance/dist/memory-gate.js +0 -382
  204. package/packages/@monomind/guidance/dist/meta-governance.d.ts +0 -265
  205. package/packages/@monomind/guidance/dist/meta-governance.js +0 -348
  206. package/packages/@monomind/guidance/dist/optimizer.d.ts +0 -104
  207. package/packages/@monomind/guidance/dist/optimizer.js +0 -329
  208. package/packages/@monomind/guidance/dist/persistence.d.ts +0 -189
  209. package/packages/@monomind/guidance/dist/persistence.js +0 -464
  210. package/packages/@monomind/guidance/dist/proof.d.ts +0 -185
  211. package/packages/@monomind/guidance/dist/proof.js +0 -238
  212. package/packages/@monomind/guidance/dist/retriever.d.ts +0 -116
  213. package/packages/@monomind/guidance/dist/retriever.js +0 -394
  214. package/packages/@monomind/guidance/dist/ruvbot-integration.d.ts +0 -370
  215. package/packages/@monomind/guidance/dist/ruvbot-integration.js +0 -738
  216. package/packages/@monomind/guidance/dist/temporal.d.ts +0 -426
  217. package/packages/@monomind/guidance/dist/temporal.js +0 -658
  218. package/packages/@monomind/guidance/dist/trust.d.ts +0 -283
  219. package/packages/@monomind/guidance/dist/trust.js +0 -473
  220. package/packages/@monomind/guidance/dist/truth-anchors.d.ts +0 -276
  221. package/packages/@monomind/guidance/dist/truth-anchors.js +0 -488
  222. package/packages/@monomind/guidance/dist/types.d.ts +0 -378
  223. package/packages/@monomind/guidance/dist/types.js +0 -10
  224. package/packages/@monomind/guidance/dist/uncertainty.d.ts +0 -372
  225. package/packages/@monomind/guidance/dist/uncertainty.js +0 -619
  226. package/packages/@monomind/guidance/dist/wasm-kernel.d.ts +0 -48
  227. package/packages/@monomind/guidance/dist/wasm-kernel.js +0 -158
@@ -1,488 +0,0 @@
1
- /**
2
- * Truth Anchor System
3
- *
4
- * Immutable, externally-signed facts that anchor the system to reality
5
- * beyond tool outputs and internal memory.
6
- *
7
- * A Truth Anchor is a fact that:
8
- * - Is immutable once recorded (cannot be overwritten or decayed)
9
- * - Is externally signed (by a human, hardware sensor, or external authority)
10
- * - Supersedes any internal belief that contradicts it
11
- * - Can be referenced but never mutated
12
- * - Has a verifiable signature chain
13
- *
14
- * TruthAnchorStore:
15
- * - Creates and signs new truth anchors with HMAC-SHA256
16
- * - Append-only storage (anchors are never mutated after creation)
17
- * - Retrieval by ID, time range, kind, attester, or tags
18
- * - Signature verification for individual anchors or the full store
19
- * - Supersession chain: new anchors can declare they supersede old ones
20
- * - Export/import for persistence and transfer
21
- * - Max 50,000 anchors with LRU eviction of expired ones only
22
- *
23
- * TruthResolver:
24
- * - Resolves conflicts between internal beliefs and truth anchors
25
- * - Memory conflict resolution (truth anchor always wins)
26
- * - Decision conflict resolution (constrains proposed actions)
27
- * - Topic-based ground truth retrieval with fuzzy tag matching
28
- *
29
- * @module @monomind/guidance/truth-anchors
30
- */
31
- import { createHmac, randomUUID } from 'node:crypto';
32
- // ============================================================================
33
- // Default Configuration
34
- // ============================================================================
35
- const DEFAULT_CONFIG = {
36
- signingKey: '',
37
- maxAnchors: 50_000,
38
- };
39
- // ============================================================================
40
- // Signing Helpers
41
- // ============================================================================
42
- /**
43
- * Compute the canonical string representation of an anchor for signing.
44
- *
45
- * Deterministic ordering ensures the same anchor always produces
46
- * the same signature regardless of property insertion order.
47
- */
48
- function canonicalize(anchor) {
49
- return [
50
- anchor.id,
51
- anchor.kind,
52
- anchor.claim,
53
- anchor.evidence,
54
- anchor.attesterId,
55
- String(anchor.timestamp),
56
- String(anchor.validFrom),
57
- String(anchor.validUntil ?? 'null'),
58
- anchor.supersedes.join(','),
59
- anchor.tags.join(','),
60
- JSON.stringify(anchor.metadata),
61
- ].join('|');
62
- }
63
- /**
64
- * Produce an HMAC-SHA256 signature for the given data using the provided key.
65
- */
66
- function sign(data, key) {
67
- return createHmac('sha256', key).update(data).digest('hex');
68
- }
69
- // ============================================================================
70
- // Truth Anchor Store
71
- // ============================================================================
72
- /**
73
- * Append-only store for truth anchors.
74
- *
75
- * Anchors are immutable once created. The store provides signing,
76
- * verification, querying, supersession, and capacity management
77
- * with LRU eviction of expired anchors only.
78
- */
79
- export class TruthAnchorStore {
80
- config;
81
- anchors = [];
82
- indexById = new Map();
83
- constructor(config = {}) {
84
- this.config = { ...DEFAULT_CONFIG, ...config };
85
- if (!this.config.signingKey) {
86
- throw new Error('TruthAnchorStore requires a signingKey in config. ' +
87
- 'Anchors cannot be created without a signing key.');
88
- }
89
- }
90
- /**
91
- * Create and sign a new truth anchor.
92
- *
93
- * The anchor is appended to the store and can never be mutated.
94
- * If the store exceeds capacity, expired anchors are evicted
95
- * starting from the oldest.
96
- */
97
- anchor(params) {
98
- const now = Date.now();
99
- const partial = {
100
- id: randomUUID(),
101
- kind: params.kind,
102
- claim: params.claim,
103
- evidence: params.evidence,
104
- attesterId: params.attesterId,
105
- timestamp: now,
106
- validFrom: params.validFrom ?? now,
107
- validUntil: params.validUntil ?? null,
108
- supersedes: params.supersedes ?? [],
109
- tags: params.tags ?? [],
110
- metadata: params.metadata ?? {},
111
- };
112
- const signature = sign(canonicalize(partial), this.config.signingKey);
113
- const truthAnchor = {
114
- ...partial,
115
- signature,
116
- };
117
- // Append (never mutate existing entries)
118
- this.anchors.push(truthAnchor);
119
- this.indexById.set(truthAnchor.id, this.anchors.length - 1);
120
- // Enforce capacity by evicting expired anchors
121
- this.enforceCapacity(now);
122
- return truthAnchor;
123
- }
124
- /**
125
- * Retrieve a truth anchor by its ID.
126
- *
127
- * Returns undefined if the anchor does not exist.
128
- */
129
- get(id) {
130
- const index = this.indexById.get(id);
131
- if (index === undefined)
132
- return undefined;
133
- return this.anchors[index];
134
- }
135
- /**
136
- * Get all anchors that are valid at the given timestamp.
137
- *
138
- * An anchor is active when:
139
- * - `validFrom <= timestamp`
140
- * - `validUntil` is null (indefinite) or `validUntil > timestamp`
141
- *
142
- * Defaults to the current time if no timestamp is provided.
143
- */
144
- getActive(timestamp) {
145
- const ts = timestamp ?? Date.now();
146
- return this.anchors.filter(a => isActive(a, ts));
147
- }
148
- /**
149
- * Query anchors with optional filters.
150
- *
151
- * All provided filters are ANDed together. An anchor must match
152
- * every specified filter to be included in the result.
153
- */
154
- query(opts) {
155
- return this.anchors.filter(a => {
156
- if (opts.kind !== undefined && a.kind !== opts.kind)
157
- return false;
158
- if (opts.attesterId !== undefined && a.attesterId !== opts.attesterId)
159
- return false;
160
- if (opts.tags !== undefined && opts.tags.length > 0) {
161
- const hasMatch = opts.tags.some(tag => a.tags.includes(tag));
162
- if (!hasMatch)
163
- return false;
164
- }
165
- if (opts.validAt !== undefined && !isActive(a, opts.validAt))
166
- return false;
167
- return true;
168
- });
169
- }
170
- /**
171
- * Verify the HMAC-SHA256 signature of a single anchor.
172
- *
173
- * Recomputes the signature from the anchor's content and compares
174
- * it to the stored signature. Returns true if they match.
175
- */
176
- verify(id) {
177
- const anchor = this.get(id);
178
- if (!anchor)
179
- return false;
180
- const { signature, ...rest } = anchor;
181
- const expected = sign(canonicalize(rest), this.config.signingKey);
182
- return timingSafeEqual(signature, expected);
183
- }
184
- /**
185
- * Verify all anchors in the store.
186
- *
187
- * Returns a summary with the count of valid anchors and the
188
- * IDs of any anchors whose signatures do not match.
189
- */
190
- verifyAll() {
191
- let valid = 0;
192
- const invalid = [];
193
- for (const anchor of this.anchors) {
194
- const { signature, ...rest } = anchor;
195
- const expected = sign(canonicalize(rest), this.config.signingKey);
196
- if (timingSafeEqual(signature, expected)) {
197
- valid++;
198
- }
199
- else {
200
- invalid.push(anchor.id);
201
- }
202
- }
203
- return { valid, invalid };
204
- }
205
- /**
206
- * Create a new anchor that supersedes an existing one.
207
- *
208
- * The old anchor remains in the store (immutable) but the new
209
- * anchor's `supersedes` array includes the old anchor's ID.
210
- * This creates a verifiable supersession chain.
211
- *
212
- * Throws if the old anchor ID does not exist.
213
- */
214
- supersede(oldId, params) {
215
- const old = this.get(oldId);
216
- if (!old) {
217
- throw new Error(`Cannot supersede: anchor "${oldId}" not found`);
218
- }
219
- const supersedes = [...(params.supersedes ?? [])];
220
- if (!supersedes.includes(oldId)) {
221
- supersedes.push(oldId);
222
- }
223
- return this.anchor({
224
- ...params,
225
- supersedes,
226
- });
227
- }
228
- /**
229
- * Resolve a claim against an internal belief.
230
- *
231
- * Searches for active truth anchors whose claim matches the
232
- * provided claim text. If a matching truth anchor exists and
233
- * is currently valid, it wins over the internal belief.
234
- *
235
- * Returns the truth anchor if it exists, otherwise returns
236
- * undefined (meaning the internal belief stands).
237
- */
238
- resolve(claim, _internalBelief) {
239
- const now = Date.now();
240
- const normalizedClaim = claim.toLowerCase().trim();
241
- // Find active anchors whose claim matches
242
- for (const anchor of this.anchors) {
243
- if (!isActive(anchor, now))
244
- continue;
245
- if (anchor.claim.toLowerCase().trim() === normalizedClaim) {
246
- return anchor;
247
- }
248
- }
249
- return undefined;
250
- }
251
- /**
252
- * Export all anchors for persistence or transfer.
253
- *
254
- * Returns a shallow copy of the anchor array. The individual
255
- * anchor objects are returned as-is since they are immutable.
256
- */
257
- exportAnchors() {
258
- return [...this.anchors];
259
- }
260
- /**
261
- * Import anchors from an external source.
262
- *
263
- * Imported anchors are appended to the store. Duplicate IDs
264
- * (anchors already in the store) are silently skipped.
265
- * Capacity enforcement runs after import.
266
- */
267
- importAnchors(anchors) {
268
- const now = Date.now();
269
- for (const anchor of anchors) {
270
- // Skip duplicates
271
- if (this.indexById.has(anchor.id))
272
- continue;
273
- this.anchors.push(anchor);
274
- this.indexById.set(anchor.id, this.anchors.length - 1);
275
- }
276
- this.enforceCapacity(now);
277
- }
278
- /**
279
- * Get the total number of anchors in the store.
280
- */
281
- get size() {
282
- return this.anchors.length;
283
- }
284
- // ===== Private =====
285
- /**
286
- * Enforce the maximum anchor capacity.
287
- *
288
- * Only expired anchors are evicted, starting from the oldest.
289
- * If no expired anchors can be evicted and the store is still
290
- * over capacity, the oldest expired anchors are removed first.
291
- * Active (non-expired) anchors are never evicted.
292
- */
293
- enforceCapacity(now) {
294
- if (this.anchors.length <= this.config.maxAnchors)
295
- return;
296
- // Collect indices of expired anchors (oldest first, array is append-only)
297
- const expiredIndices = [];
298
- for (let i = 0; i < this.anchors.length; i++) {
299
- const a = this.anchors[i];
300
- if (a.validUntil !== null && a.validUntil <= now) {
301
- expiredIndices.push(i);
302
- }
303
- }
304
- // Determine how many we need to evict
305
- const excess = this.anchors.length - this.config.maxAnchors;
306
- const toEvict = Math.min(excess, expiredIndices.length);
307
- if (toEvict <= 0)
308
- return;
309
- // Build a set of indices to remove (oldest expired first)
310
- const removeSet = new Set(expiredIndices.slice(0, toEvict));
311
- // Rebuild the array and index, preserving order
312
- const surviving = [];
313
- this.indexById.clear();
314
- for (let i = 0; i < this.anchors.length; i++) {
315
- if (removeSet.has(i))
316
- continue;
317
- this.indexById.set(this.anchors[i].id, surviving.length);
318
- surviving.push(this.anchors[i]);
319
- }
320
- // Replace contents (splice to preserve the same array reference)
321
- this.anchors.length = 0;
322
- for (const a of surviving) {
323
- this.anchors.push(a);
324
- }
325
- }
326
- }
327
- // ============================================================================
328
- // Truth Resolver
329
- // ============================================================================
330
- /**
331
- * Resolves conflicts between internal system beliefs and externally
332
- * anchored truth.
333
- *
334
- * The fundamental principle: truth anchors always win. If a valid,
335
- * active truth anchor contradicts an internal belief, the anchor
336
- * takes precedence.
337
- */
338
- export class TruthResolver {
339
- store;
340
- constructor(store) {
341
- this.store = store;
342
- }
343
- /**
344
- * Check if any active truth anchor contradicts a memory value.
345
- *
346
- * Searches by namespace and key as tags, and by the memory value
347
- * as a claim. If a truth anchor exists that covers the same topic,
348
- * it wins over the internal memory.
349
- */
350
- resolveMemoryConflict(memoryKey, memoryValue, namespace) {
351
- const now = Date.now();
352
- const active = this.store.getActive(now);
353
- // Search for anchors tagged with the namespace or memory key
354
- const normalizedKey = memoryKey.toLowerCase().trim();
355
- const normalizedNs = namespace.toLowerCase().trim();
356
- const normalizedValue = memoryValue.toLowerCase().trim();
357
- for (const anchor of active) {
358
- const lowerTags = anchor.tags.map(t => t.toLowerCase());
359
- // Check if the anchor is relevant to this memory entry
360
- const tagMatch = lowerTags.includes(normalizedKey) ||
361
- lowerTags.includes(normalizedNs) ||
362
- lowerTags.includes(`${normalizedNs}:${normalizedKey}`);
363
- if (!tagMatch)
364
- continue;
365
- // Check if the anchor's claim contradicts the memory value
366
- const anchorClaim = anchor.claim.toLowerCase().trim();
367
- if (anchorClaim !== normalizedValue) {
368
- return {
369
- truthWins: true,
370
- anchor,
371
- reason: `Truth anchor "${anchor.id}" (${anchor.kind}) contradicts memory ` +
372
- `"${namespace}:${memoryKey}". Anchor claim: "${anchor.claim}" ` +
373
- `supersedes internal value: "${memoryValue}".`,
374
- };
375
- }
376
- }
377
- return {
378
- truthWins: false,
379
- reason: `No active truth anchor contradicts memory "${namespace}:${memoryKey}". ` +
380
- `Internal belief stands.`,
381
- };
382
- }
383
- /**
384
- * Check if any active truth anchor constrains a proposed action.
385
- *
386
- * Searches for anchors whose claims or tags relate to the proposed
387
- * action and its context. Returns a conflict resolution indicating
388
- * whether the action is constrained.
389
- */
390
- resolveDecisionConflict(proposedAction, context) {
391
- const now = Date.now();
392
- const active = this.store.getActive(now);
393
- const normalizedAction = proposedAction.toLowerCase().trim();
394
- const contextKeys = Object.keys(context).map(k => k.toLowerCase());
395
- for (const anchor of active) {
396
- const lowerTags = anchor.tags.map(t => t.toLowerCase());
397
- const lowerClaim = anchor.claim.toLowerCase();
398
- // Check if the anchor is relevant: tags intersect with context keys
399
- // or the action text appears in the claim
400
- const tagOverlap = lowerTags.some(t => contextKeys.includes(t) || normalizedAction.includes(t));
401
- const claimRelevance = lowerClaim.includes(normalizedAction) ||
402
- normalizedAction.includes(lowerClaim);
403
- if (!tagOverlap && !claimRelevance)
404
- continue;
405
- // The anchor is relevant -- it constrains this action
406
- return {
407
- truthWins: true,
408
- anchor,
409
- reason: `Truth anchor "${anchor.id}" (${anchor.kind}) constrains the ` +
410
- `proposed action "${proposedAction}". Anchor claim: "${anchor.claim}". ` +
411
- `Attested by: "${anchor.attesterId}".`,
412
- };
413
- }
414
- return {
415
- truthWins: false,
416
- reason: `No active truth anchor constrains the proposed action ` +
417
- `"${proposedAction}". Action may proceed.`,
418
- };
419
- }
420
- /**
421
- * Get all active truth anchors relevant to a topic.
422
- *
423
- * Uses fuzzy tag matching: a tag matches the topic if either
424
- * the tag contains the topic or the topic contains the tag
425
- * (case-insensitive). Also matches against the claim text.
426
- */
427
- getGroundTruth(topic) {
428
- const now = Date.now();
429
- const active = this.store.getActive(now);
430
- const normalizedTopic = topic.toLowerCase().trim();
431
- return active.filter(anchor => {
432
- // Fuzzy tag match
433
- const tagMatch = anchor.tags.some(tag => {
434
- const lowerTag = tag.toLowerCase();
435
- return lowerTag.includes(normalizedTopic) || normalizedTopic.includes(lowerTag);
436
- });
437
- // Claim text match
438
- const claimMatch = anchor.claim.toLowerCase().includes(normalizedTopic);
439
- return tagMatch || claimMatch;
440
- });
441
- }
442
- }
443
- // ============================================================================
444
- // Helpers
445
- // ============================================================================
446
- /**
447
- * Check whether a truth anchor is active at a given timestamp.
448
- */
449
- function isActive(anchor, timestamp) {
450
- if (anchor.validFrom > timestamp)
451
- return false;
452
- if (anchor.validUntil !== null && anchor.validUntil <= timestamp)
453
- return false;
454
- return true;
455
- }
456
- /**
457
- * Constant-time string comparison to prevent timing attacks on signatures.
458
- *
459
- * Compares two hex strings character by character, accumulating
460
- * differences without short-circuiting.
461
- */
462
- function timingSafeEqual(a, b) {
463
- if (a.length !== b.length)
464
- return false;
465
- let mismatch = 0;
466
- for (let i = 0; i < a.length; i++) {
467
- mismatch |= a.charCodeAt(i) ^ b.charCodeAt(i);
468
- }
469
- return mismatch === 0;
470
- }
471
- // ============================================================================
472
- // Factory Functions
473
- // ============================================================================
474
- /**
475
- * Create a TruthAnchorStore with the given configuration.
476
- *
477
- * @param config - Must include `signingKey`. `maxAnchors` defaults to 50,000.
478
- */
479
- export function createTruthAnchorStore(config) {
480
- return new TruthAnchorStore(config);
481
- }
482
- /**
483
- * Create a TruthResolver backed by the given store.
484
- */
485
- export function createTruthResolver(store) {
486
- return new TruthResolver(store);
487
- }
488
- //# sourceMappingURL=truth-anchors.js.map