@lucern/graph-primitives 0.1.0-alpha.2

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 (224) hide show
  1. package/README.md +29 -0
  2. package/dist/beliefDecay-Q_26RTc-.d.ts +72 -0
  3. package/dist/beliefDecay.d.ts +2 -0
  4. package/dist/beliefDecay.js +1628 -0
  5. package/dist/beliefDecay.js.map +1 -0
  6. package/dist/beliefEvidenceLinks-42FlR48t.d.ts +77 -0
  7. package/dist/beliefEvidenceLinks.d.ts +1 -0
  8. package/dist/beliefEvidenceLinks.js +1978 -0
  9. package/dist/beliefEvidenceLinks.js.map +1 -0
  10. package/dist/beliefLifecycle-C-AehZgF.d.ts +43 -0
  11. package/dist/beliefLifecycle.d.ts +1 -0
  12. package/dist/beliefLifecycle.js +98 -0
  13. package/dist/beliefLifecycle.js.map +1 -0
  14. package/dist/confidencePropagationDispatch.d.ts +46 -0
  15. package/dist/confidencePropagationDispatch.js +744 -0
  16. package/dist/confidencePropagationDispatch.js.map +1 -0
  17. package/dist/contradictions-Hdwl7zid.d.ts +71 -0
  18. package/dist/contradictions.d.ts +1 -0
  19. package/dist/contradictions.js +1557 -0
  20. package/dist/contradictions.js.map +1 -0
  21. package/dist/convex.d.ts +23 -0
  22. package/dist/convex.js +17 -0
  23. package/dist/convex.js.map +1 -0
  24. package/dist/edgeValidation-CeI0wc0r.d.ts +35 -0
  25. package/dist/edgeValidation.d.ts +2 -0
  26. package/dist/edgeValidation.js +307 -0
  27. package/dist/edgeValidation.js.map +1 -0
  28. package/dist/edges/contains.d.ts +6 -0
  29. package/dist/edges/contains.js +14 -0
  30. package/dist/edges/contains.js.map +1 -0
  31. package/dist/edges/contradicts.d.ts +6 -0
  32. package/dist/edges/contradicts.js +183 -0
  33. package/dist/edges/contradicts.js.map +1 -0
  34. package/dist/edges/dependsOn.d.ts +6 -0
  35. package/dist/edges/dependsOn.js +240 -0
  36. package/dist/edges/dependsOn.js.map +1 -0
  37. package/dist/edges/derivedFrom.d.ts +6 -0
  38. package/dist/edges/derivedFrom.js +14 -0
  39. package/dist/edges/derivedFrom.js.map +1 -0
  40. package/dist/edges/elaborates.d.ts +6 -0
  41. package/dist/edges/elaborates.js +100 -0
  42. package/dist/edges/elaborates.js.map +1 -0
  43. package/dist/edges/index.d.ts +3 -0
  44. package/dist/edges/index.js +556 -0
  45. package/dist/edges/index.js.map +1 -0
  46. package/dist/edges/informs.d.ts +6 -0
  47. package/dist/edges/informs.js +112 -0
  48. package/dist/edges/informs.js.map +1 -0
  49. package/dist/edges/propagationTypes.d.ts +39 -0
  50. package/dist/edges/propagationTypes.js +17 -0
  51. package/dist/edges/propagationTypes.js.map +1 -0
  52. package/dist/edges/refutes.d.ts +6 -0
  53. package/dist/edges/refutes.js +108 -0
  54. package/dist/edges/refutes.js.map +1 -0
  55. package/dist/edges/supports.d.ts +6 -0
  56. package/dist/edges/supports.js +193 -0
  57. package/dist/edges/supports.js.map +1 -0
  58. package/dist/edges/tests.d.ts +6 -0
  59. package/dist/edges/tests.js +14 -0
  60. package/dist/edges/tests.js.map +1 -0
  61. package/dist/edges/utils.d.ts +12 -0
  62. package/dist/edges/utils.js +188 -0
  63. package/dist/edges/utils.js.map +1 -0
  64. package/dist/embeddingTrigger.d.ts +24 -0
  65. package/dist/embeddingTrigger.js +24 -0
  66. package/dist/embeddingTrigger.js.map +1 -0
  67. package/dist/entityBridge-DMaKooYn.d.ts +59 -0
  68. package/dist/entityBridge.d.ts +1 -0
  69. package/dist/entityBridge.js +663 -0
  70. package/dist/entityBridge.js.map +1 -0
  71. package/dist/entityLifecycle-BkhRJ-XI.d.ts +69 -0
  72. package/dist/entityLifecycle.d.ts +1 -0
  73. package/dist/entityLifecycle.js +2083 -0
  74. package/dist/entityLifecycle.js.map +1 -0
  75. package/dist/entityValidation-KLZ_Xl2D.d.ts +50 -0
  76. package/dist/entityValidation.d.ts +3 -0
  77. package/dist/entityValidation.js +71 -0
  78. package/dist/entityValidation.js.map +1 -0
  79. package/dist/epistemicAnswers-DSP1slZ9.d.ts +67 -0
  80. package/dist/epistemicAnswers.d.ts +1 -0
  81. package/dist/epistemicAnswers.js +1650 -0
  82. package/dist/epistemicAnswers.js.map +1 -0
  83. package/dist/epistemicBeliefs-DtFVTp-k.d.ts +377 -0
  84. package/dist/epistemicBeliefs.d.ts +5 -0
  85. package/dist/epistemicBeliefs.js +6386 -0
  86. package/dist/epistemicBeliefs.js.map +1 -0
  87. package/dist/epistemicContractHelpers.d.ts +1 -0
  88. package/dist/epistemicContractHelpers.js +320 -0
  89. package/dist/epistemicContractHelpers.js.map +1 -0
  90. package/dist/epistemicContracts.d.ts +77 -0
  91. package/dist/epistemicContracts.js +8436 -0
  92. package/dist/epistemicContracts.js.map +1 -0
  93. package/dist/epistemicEdges-DcA8ErUG.d.ts +191 -0
  94. package/dist/epistemicEdges.d.ts +2 -0
  95. package/dist/epistemicEdges.js +2749 -0
  96. package/dist/epistemicEdges.js.map +1 -0
  97. package/dist/epistemicEvidence-Bo638XDP.d.ts +128 -0
  98. package/dist/epistemicEvidence.d.ts +3 -0
  99. package/dist/epistemicEvidence.js +3282 -0
  100. package/dist/epistemicEvidence.js.map +1 -0
  101. package/dist/epistemicHelpers-Bd9xbaib.d.ts +329 -0
  102. package/dist/epistemicHelpers.d.ts +4 -0
  103. package/dist/epistemicHelpers.js +999 -0
  104. package/dist/epistemicHelpers.js.map +1 -0
  105. package/dist/epistemicLinking-CyeLOIzN.d.ts +35 -0
  106. package/dist/epistemicLinking.d.ts +1 -0
  107. package/dist/epistemicLinking.js +1391 -0
  108. package/dist/epistemicLinking.js.map +1 -0
  109. package/dist/epistemicNodes-BpD6Koud.d.ts +167 -0
  110. package/dist/epistemicNodes.d.ts +2 -0
  111. package/dist/epistemicNodes.js +2942 -0
  112. package/dist/epistemicNodes.js.map +1 -0
  113. package/dist/epistemicQuestions-CmEeY6zQ.d.ts +214 -0
  114. package/dist/epistemicQuestions.d.ts +3 -0
  115. package/dist/epistemicQuestions.js +4993 -0
  116. package/dist/epistemicQuestions.js.map +1 -0
  117. package/dist/epistemicSources-ZazxHOK1.d.ts +25 -0
  118. package/dist/epistemicSources.d.ts +1 -0
  119. package/dist/epistemicSources.js +2025 -0
  120. package/dist/epistemicSources.js.map +1 -0
  121. package/dist/evaluators/index.d.ts +9 -0
  122. package/dist/evaluators/index.js +8440 -0
  123. package/dist/evaluators/index.js.map +1 -0
  124. package/dist/evaluators/lintCheckerEvaluator.d.ts +11 -0
  125. package/dist/evaluators/lintCheckerEvaluator.js +155 -0
  126. package/dist/evaluators/lintCheckerEvaluator.js.map +1 -0
  127. package/dist/evaluators/sentryCheckerEvaluator.d.ts +11 -0
  128. package/dist/evaluators/sentryCheckerEvaluator.js +126 -0
  129. package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -0
  130. package/dist/evaluators/shared.d.ts +27 -0
  131. package/dist/evaluators/shared.js +92 -0
  132. package/dist/evaluators/shared.js.map +1 -0
  133. package/dist/evaluators/testRunnerEvaluator.d.ts +17 -0
  134. package/dist/evaluators/testRunnerEvaluator.js +232 -0
  135. package/dist/evaluators/testRunnerEvaluator.js.map +1 -0
  136. package/dist/evaluators/tscCheckerEvaluator.d.ts +11 -0
  137. package/dist/evaluators/tscCheckerEvaluator.js +189 -0
  138. package/dist/evaluators/tscCheckerEvaluator.js.map +1 -0
  139. package/dist/globalId-DKh9d_uD.d.ts +20 -0
  140. package/dist/globalId.d.ts +1 -0
  141. package/dist/globalId.js +15 -0
  142. package/dist/globalId.js.map +1 -0
  143. package/dist/graphTypes-CpgIuCdo.d.ts +52 -0
  144. package/dist/graphTypes.d.ts +1 -0
  145. package/dist/graphTypes.js +120 -0
  146. package/dist/graphTypes.js.map +1 -0
  147. package/dist/helpers-BYHIk5vU.d.ts +27 -0
  148. package/dist/helpers.d.ts +4 -0
  149. package/dist/helpers.js +313 -0
  150. package/dist/helpers.js.map +1 -0
  151. package/dist/index-Dct1T70K.d.ts +25 -0
  152. package/dist/index-Dq-7R-gi.d.ts +31 -0
  153. package/dist/index.d.ts +45 -0
  154. package/dist/index.js +22294 -0
  155. package/dist/index.js.map +1 -0
  156. package/dist/invariantEnforcement.d.ts +52 -0
  157. package/dist/invariantEnforcement.js +231 -0
  158. package/dist/invariantEnforcement.js.map +1 -0
  159. package/dist/logicalRoleInference-CJxqWi3u.d.ts +16 -0
  160. package/dist/logicalRoleInference.d.ts +3 -0
  161. package/dist/logicalRoleInference.js +64 -0
  162. package/dist/logicalRoleInference.js.map +1 -0
  163. package/dist/matcherFeedbackUtils.d.ts +33 -0
  164. package/dist/matcherFeedbackUtils.js +95 -0
  165. package/dist/matcherFeedbackUtils.js.map +1 -0
  166. package/dist/ontology-matching-Buhu23ss.d.ts +48 -0
  167. package/dist/ontology-matching.d.ts +2 -0
  168. package/dist/ontology-matching.js +346 -0
  169. package/dist/ontology-matching.js.map +1 -0
  170. package/dist/ontologyApproval-Ba0Jjk1k.d.ts +26 -0
  171. package/dist/ontologyApproval.d.ts +1 -0
  172. package/dist/ontologyApproval.js +78 -0
  173. package/dist/ontologyApproval.js.map +1 -0
  174. package/dist/ontologyDefinitions.d.ts +72 -0
  175. package/dist/ontologyDefinitions.js +635 -0
  176. package/dist/ontologyDefinitions.js.map +1 -0
  177. package/dist/ontologyHelpers.d.ts +79 -0
  178. package/dist/ontologyHelpers.js +81 -0
  179. package/dist/ontologyHelpers.js.map +1 -0
  180. package/dist/ontologyRegistry-B67rPJ16.d.ts +31 -0
  181. package/dist/ontologyRegistry.d.ts +1 -0
  182. package/dist/ontologyRegistry.js +296 -0
  183. package/dist/ontologyRegistry.js.map +1 -0
  184. package/dist/projectionReconciliation-CxrXYGaB.d.ts +20 -0
  185. package/dist/projectionReconciliation.d.ts +1 -0
  186. package/dist/projectionReconciliation.js +261 -0
  187. package/dist/projectionReconciliation.js.map +1 -0
  188. package/dist/projectionStaleness-CAdpIsaW.d.ts +51 -0
  189. package/dist/projectionStaleness.d.ts +1 -0
  190. package/dist/projectionStaleness.js +57 -0
  191. package/dist/projectionStaleness.js.map +1 -0
  192. package/dist/questionEvidenceLinks-BdQD0TkM.d.ts +34 -0
  193. package/dist/questionEvidenceLinks.d.ts +1 -0
  194. package/dist/questionEvidenceLinks.js +1690 -0
  195. package/dist/questionEvidenceLinks.js.map +1 -0
  196. package/dist/resolverTypes-CC8Ea2E2.d.ts +20 -0
  197. package/dist/resolverTypes.d.ts +4 -0
  198. package/dist/resolverTypes.js +3 -0
  199. package/dist/resolverTypes.js.map +1 -0
  200. package/dist/resolvers-Br1a6eLV.d.ts +14 -0
  201. package/dist/resolvers.d.ts +5 -0
  202. package/dist/resolvers.js +308 -0
  203. package/dist/resolvers.js.map +1 -0
  204. package/dist/scopeResolverCompat.d.ts +26 -0
  205. package/dist/scopeResolverCompat.js +242 -0
  206. package/dist/scopeResolverCompat.js.map +1 -0
  207. package/dist/text-matching-CMn2WnVD.d.ts +40 -0
  208. package/dist/text-matching.d.ts +2 -0
  209. package/dist/text-matching.js +246 -0
  210. package/dist/text-matching.js.map +1 -0
  211. package/dist/topicOntologyResolver.d.ts +80 -0
  212. package/dist/topicOntologyResolver.js +67 -0
  213. package/dist/topicOntologyResolver.js.map +1 -0
  214. package/dist/topicProjectOverlay.d.ts +92 -0
  215. package/dist/topicProjectOverlay.js +249 -0
  216. package/dist/topicProjectOverlay.js.map +1 -0
  217. package/dist/topicScope-By_zp4tt.d.ts +34 -0
  218. package/dist/topicScope.d.ts +3 -0
  219. package/dist/topicScope.js +206 -0
  220. package/dist/topicScope.js.map +1 -0
  221. package/dist/workspaceIsolation.d.ts +44 -0
  222. package/dist/workspaceIsolation.js +950 -0
  223. package/dist/workspaceIsolation.js.map +1 -0
  224. package/package.json +46 -0
@@ -0,0 +1,999 @@
1
+ import { componentsGeneric, anyApi } from 'convex/server';
2
+
3
+ // src/convex.ts
4
+ componentsGeneric();
5
+ var internal = anyApi;
6
+
7
+ // src/globalId.ts
8
+ function generateGlobalId() {
9
+ const bytes = new Uint8Array(16);
10
+ crypto.getRandomValues(bytes);
11
+ bytes[6] = bytes[6] & 15 | 64;
12
+ bytes[8] = bytes[8] & 63 | 128;
13
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
14
+ ""
15
+ );
16
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
17
+ }
18
+
19
+ // src/graphTypes.ts
20
+ var NODE_TYPE_TO_LABEL = {
21
+ // L4: Audit Targets
22
+ decision: "Decision",
23
+ // L3: Traversal Anchors
24
+ belief: "Belief",
25
+ question: "Question",
26
+ theme: "Theme",
27
+ deal: "Deal",
28
+ topic: "Topic",
29
+ // L2: Compression Boundary
30
+ claim: "Claim",
31
+ evidence: "Evidence",
32
+ synthesis: "Synthesis",
33
+ answer: "Answer",
34
+ // L1: Terminal Leaves
35
+ atomic_fact: "AtomicFact",
36
+ excerpt: "Excerpt",
37
+ source: "Source",
38
+ // Ontological (Neo4j is SOT)
39
+ company: "Company",
40
+ person: "Person",
41
+ investor: "Investor",
42
+ function: "Function",
43
+ value_chain: "ValueChain"
44
+ };
45
+ var EDGE_TYPE_TO_REL = {
46
+ // === THE SIX CANONICAL EPISTEMIC EDGE TYPES ===
47
+ supports: "SUPPORTS",
48
+ // L3↔L3: belief bears on belief (weight -1 to +1)
49
+ informs: "INFORMS",
50
+ // L2→L3: evidence bears on belief
51
+ depends_on: "DEPENDS_ON",
52
+ // L3→L3, Q→Q: structural gate
53
+ derived_from: "DERIVED_FROM",
54
+ // Any→Any: provenance chain (fork, synthesis, extraction, answer)
55
+ contains: "CONTAINS",
56
+ // Any→Any: hierarchy, scoping, membership
57
+ tests: "TESTS",
58
+ // Q→L3: question interrogates belief
59
+ // === L4 DECISION EDGES (derived_from with derivationType=decision) ===
60
+ // Kept as separate Neo4j relationship types for backward compat with L4 queries.
61
+ // New code should use derived_from + metadata.
62
+ based_on_belief: "BASED_ON_BELIEF",
63
+ based_on_question: "BASED_ON_QUESTION",
64
+ blocked_by_contradiction: "BLOCKED_BY_CONTRADICTION",
65
+ informed_by_theme: "INFORMED_BY_THEME",
66
+ // === ONTOLOGICAL EDGES (tenant-extensible, managed by ontology system) ===
67
+ works_at: "WORKS_AT",
68
+ invested_in: "INVESTED_IN",
69
+ competes_with: "COMPETES_WITH",
70
+ participates_in: "PARTICIPATES_IN",
71
+ founded_by: "FOUNDED_BY",
72
+ evaluates: "EVALUATES",
73
+ performs: "PERFORMS",
74
+ function_in: "FUNCTION_IN",
75
+ impacts: "IMPACTS",
76
+ raised_from: "RAISED_FROM",
77
+ mentioned_in: "MENTIONED_IN",
78
+ perspective_on: "PERSPECTIVE_ON",
79
+ about_entity: "ABOUT_ENTITY",
80
+ entity_referenced_in: "ENTITY_REFERENCED_IN"
81
+ };
82
+ function getNeo4jLabel(nodeType) {
83
+ return NODE_TYPE_TO_LABEL[nodeType] || nodeType.charAt(0).toUpperCase() + nodeType.slice(1);
84
+ }
85
+ function getNeo4jRelType(edgeType) {
86
+ return EDGE_TYPE_TO_REL[edgeType] || edgeType.toUpperCase();
87
+ }
88
+ var CANONICAL_EPISTEMIC_TYPES = /* @__PURE__ */ new Set([
89
+ "supports",
90
+ "informs",
91
+ "depends_on",
92
+ "derived_from",
93
+ "contains",
94
+ "tests"
95
+ ]);
96
+ function isDeprecatedEdgeType(edgeType) {
97
+ if (CANONICAL_EPISTEMIC_TYPES.has(edgeType)) return false;
98
+ if (edgeType in EDGE_TYPE_TO_REL) return false;
99
+ return true;
100
+ }
101
+
102
+ // src/epistemicHelpers.ts
103
+ function generateContentHash(nodeType, text) {
104
+ const content = `${nodeType}:${normalizeText(text)}`;
105
+ let hash = 5381;
106
+ for (let i = 0; i < content.length; i++) {
107
+ hash = (hash << 5) + hash + content.charCodeAt(i);
108
+ hash &= hash;
109
+ }
110
+ const hashHex = Math.abs(hash).toString(16).padStart(8, "0");
111
+ const lengthHex = content.length.toString(16).padStart(4, "0");
112
+ const checksum = content.split("").reduce((a, c) => a + c.charCodeAt(0), 0).toString(16).padStart(8, "0");
113
+ return `${hashHex}${lengthHex}${checksum}`;
114
+ }
115
+ function normalizeText(text) {
116
+ return text.trim().toLowerCase().replace(/\s+/g, " ");
117
+ }
118
+ function getNodeLayer(nodeType) {
119
+ switch (nodeType) {
120
+ // L4: Audit targets
121
+ case "decision":
122
+ return "L4";
123
+ // L3: Traversal anchors
124
+ case "belief":
125
+ case "question":
126
+ case "theme":
127
+ case "deal":
128
+ return "L3";
129
+ // L2: Compression boundary
130
+ case "claim":
131
+ case "evidence":
132
+ case "synthesis":
133
+ case "answer":
134
+ return "L2";
135
+ // L1: Terminal leaves
136
+ case "atomic_fact":
137
+ case "excerpt":
138
+ case "source":
139
+ return "L1";
140
+ // Ontological entities
141
+ case "company":
142
+ case "person":
143
+ case "investor":
144
+ case "function":
145
+ case "value_chain":
146
+ return "ontological";
147
+ // Organizational containers
148
+ case "topic":
149
+ return "organizational";
150
+ default:
151
+ console.warn(
152
+ `[EpistemicLayer] Unknown nodeType: ${nodeType}, defaulting to L2`
153
+ );
154
+ return "L2";
155
+ }
156
+ }
157
+ var LAYER_TRAVERSAL_RULES = {
158
+ L4: {
159
+ canReach: ["L3", "L4"],
160
+ // Decisions can reach beliefs/questions
161
+ mustPassThrough: "L3"
162
+ // Must go through L3 to reach L2
163
+ },
164
+ L3: {
165
+ canReach: ["L2", "L3", "L4"],
166
+ // Beliefs can reach evidence, other beliefs, or decisions
167
+ mustPassThrough: "L2"
168
+ // Must go through L2 to reach L1
169
+ },
170
+ L2: {
171
+ canReach: ["L1", "L2", "L3"],
172
+ // Evidence can reach sources, other evidence, or beliefs
173
+ mustPassThrough: null
174
+ // L2 can reach L1 directly
175
+ },
176
+ L1: {
177
+ canReach: ["L1"],
178
+ // Sources can only reach other sources
179
+ mustPassThrough: null
180
+ // Terminal - no traversal beyond
181
+ },
182
+ ontological: {
183
+ canReach: ["L3", "L2", "ontological"],
184
+ // Entities can link to epistemic structure
185
+ mustPassThrough: null
186
+ // No layer constraints for entities
187
+ },
188
+ organizational: {
189
+ canReach: ["L3", "L2", "organizational"],
190
+ // Containers scope epistemic structure + nest
191
+ mustPassThrough: null
192
+ // No layer constraints for containers
193
+ }
194
+ };
195
+ function isValidLayerConnection(fromLayer, toLayer) {
196
+ const rules = LAYER_TRAVERSAL_RULES[fromLayer];
197
+ return rules.canReach.includes(toLayer);
198
+ }
199
+ function getLayerDepth(layer) {
200
+ switch (layer) {
201
+ case "L4":
202
+ return 4;
203
+ case "L3":
204
+ return 3;
205
+ case "L2":
206
+ return 2;
207
+ case "L1":
208
+ return 1;
209
+ case "ontological":
210
+ return 0;
211
+ // Ontological exists outside the epistemic hierarchy
212
+ case "organizational":
213
+ return 0;
214
+ // Organizational exists outside the epistemic hierarchy
215
+ default:
216
+ return 2;
217
+ }
218
+ }
219
+ function canTraverseToLayer(fromLayer, toLayer) {
220
+ const fromDepth = getLayerDepth(fromLayer);
221
+ const toDepth = getLayerDepth(toLayer);
222
+ if (fromLayer === "ontological" || toLayer === "ontological" || fromLayer === "organizational" || toLayer === "organizational") {
223
+ return isValidLayerConnection(fromLayer, toLayer);
224
+ }
225
+ if (fromDepth === toDepth) {
226
+ return true;
227
+ }
228
+ return isValidLayerConnection(fromLayer, toLayer);
229
+ }
230
+ function shouldContinueTraversal(currentLayer, targetLayer, options) {
231
+ const currentDepth = getLayerDepth(currentLayer);
232
+ const targetDepth = getLayerDepth(targetLayer);
233
+ if (options.minLayer !== void 0 && targetDepth < options.minLayer) {
234
+ return false;
235
+ }
236
+ if (options.maxLayer !== void 0 && targetDepth > options.maxLayer) {
237
+ return false;
238
+ }
239
+ switch (options.mode) {
240
+ case "anchor_down":
241
+ return targetDepth < currentDepth || targetDepth === currentDepth;
242
+ case "anchor_up":
243
+ return targetDepth > currentDepth || targetDepth === currentDepth;
244
+ case "same_layer":
245
+ return targetDepth === currentDepth;
246
+ case "decision_trace":
247
+ return currentDepth === 4 ? targetDepth === 3 : targetDepth === currentDepth;
248
+ default:
249
+ return true;
250
+ }
251
+ }
252
+ function getDefaultMinLayer(mode) {
253
+ switch (mode) {
254
+ case "anchor_down":
255
+ return 1;
256
+ // Can go all the way to L1 sources
257
+ case "anchor_up":
258
+ return 2;
259
+ // Start at L2 evidence minimum
260
+ case "same_layer":
261
+ return 1;
262
+ // No constraint
263
+ case "decision_trace":
264
+ return 3;
265
+ // Stop at L3 beliefs (don't go to L2 evidence)
266
+ default:
267
+ return 1;
268
+ }
269
+ }
270
+ function getDefaultEdgeTypesForMode(mode) {
271
+ switch (mode) {
272
+ case "anchor_down":
273
+ return [
274
+ "informs",
275
+ // evidence → belief (traverse backwards)
276
+ "derived_from"
277
+ // evidence → source (provenance) + evidence → question
278
+ ];
279
+ case "anchor_up":
280
+ return ["informs", "derived_from"];
281
+ case "same_layer":
282
+ return [
283
+ // L3: Belief ↔ Belief
284
+ "depends_on",
285
+ "supports",
286
+ "contains",
287
+ // L3: Question ↔ Question
288
+ "prerequisite_for",
289
+ "parallel_to",
290
+ // L2: Evidence ↔ Evidence
291
+ "corroborates",
292
+ "extends",
293
+ "same_source_as",
294
+ "same_theme_as"
295
+ ];
296
+ case "decision_trace":
297
+ return [
298
+ "derived_from",
299
+ "depends_on",
300
+ "contains"
301
+ ];
302
+ default:
303
+ return [];
304
+ }
305
+ }
306
+ var EDGE_LAYER_RULES = {
307
+ // === L4 Decision Edges ===
308
+ based_on_belief: {
309
+ from: ["L4"],
310
+ to: ["L3"],
311
+ description: "Decision \u2192 Belief (L4 \u2192 L3)"
312
+ },
313
+ based_on_question: {
314
+ from: ["L4"],
315
+ to: ["L3"],
316
+ description: "Decision \u2192 Question (L4 \u2192 L3)"
317
+ },
318
+ blocked_by_contradiction: {
319
+ from: ["L4"],
320
+ to: ["L3"],
321
+ description: "Decision \u2192 Contradiction (L4 \u2192 L3)"
322
+ },
323
+ informed_by_theme: {
324
+ from: ["L4"],
325
+ to: ["L3"],
326
+ description: "Decision \u2192 Theme (L4 \u2192 L3)"
327
+ },
328
+ // === Evidence Flow (L2 → L3, L2 → L1) ===
329
+ derived_from: {
330
+ from: ["L2", "L3", "L4"],
331
+ to: ["L1", "L2", "L3"],
332
+ description: "A was produced from B (provenance chain)"
333
+ },
334
+ answers: {
335
+ from: ["L2"],
336
+ to: ["L3"],
337
+ description: "Evidence \u2192 Question (L2 \u2192 L3)"
338
+ },
339
+ responds_to: {
340
+ from: ["L2"],
341
+ to: ["L3"],
342
+ description: "Answer \u2192 Question (L2 \u2192 L3)"
343
+ },
344
+ informs: {
345
+ from: ["L2"],
346
+ to: ["L3"],
347
+ description: "Evidence \u2192 Belief (L2 \u2192 L3)"
348
+ },
349
+ qualifies: {
350
+ from: ["L2"],
351
+ to: ["L3"],
352
+ description: "Evidence \u2192 Belief (L2 \u2192 L3)"
353
+ },
354
+ // === Question → Belief (L3 → L3) ===
355
+ tests: {
356
+ from: ["L3"],
357
+ to: ["L3"],
358
+ description: "Question \u2192 Belief (L3 \u2192 L3)"
359
+ },
360
+ explores: {
361
+ from: ["L3"],
362
+ to: ["L3"],
363
+ description: "Question \u2192 Belief assumption (L3 \u2192 L3)"
364
+ },
365
+ // === Synthesis (L2 → L2, L2 → L1) ===
366
+ based_on: {
367
+ from: ["L2"],
368
+ to: ["L2", "L1"],
369
+ description: "Synthesis \u2192 Evidence/Source (L2 \u2192 L2/L1)"
370
+ },
371
+ // === Theme Relationships (L3 → L3) ===
372
+ relates_to_thesis: {
373
+ from: ["L3"],
374
+ to: ["L3"],
375
+ description: "Belief \u2192 Theme (L3 \u2192 L3)"
376
+ },
377
+ belongs_to: {
378
+ from: ["L3", "L2"],
379
+ // Can belong to theme from L3 or L2
380
+ to: ["L3"],
381
+ description: "Any \u2192 Theme (L3/L2 \u2192 L3)"
382
+ },
383
+ plays_theme: {
384
+ from: ["L3"],
385
+ to: ["L3"],
386
+ description: "Deal \u2192 Theme (L3 \u2192 L3)"
387
+ },
388
+ // === Topic Hierarchy (L3 → organizational) ===
389
+ scoped_by: {
390
+ from: ["L3"],
391
+ to: ["organizational"],
392
+ description: "Belief/Question \u2192 Topic (L3 \u2192 organizational)"
393
+ },
394
+ // === Deal/Company ===
395
+ evaluates: {
396
+ from: ["L3"],
397
+ to: ["ontological"],
398
+ description: "Deal \u2192 Company (L3 \u2192 ontological)"
399
+ },
400
+ // === People (ontological → ontological, ontological → L3) ===
401
+ perspective_on: {
402
+ from: ["ontological"],
403
+ to: ["L3"],
404
+ description: "Person \u2192 Belief/Theme (ontological \u2192 L3)"
405
+ },
406
+ works_at: {
407
+ from: ["ontological"],
408
+ to: ["ontological"],
409
+ description: "Person \u2192 Company (ontological \u2192 ontological)"
410
+ },
411
+ // === Value Chain (ontological) ===
412
+ participates_in: {
413
+ from: ["ontological"],
414
+ to: ["ontological"],
415
+ description: "Company \u2192 ValueChain (ontological \u2192 ontological)"
416
+ },
417
+ performs: {
418
+ from: ["ontological"],
419
+ to: ["ontological"],
420
+ description: "Company \u2192 Function (ontological \u2192 ontological)"
421
+ },
422
+ function_in: {
423
+ from: ["ontological"],
424
+ to: ["ontological"],
425
+ description: "Function \u2192 ValueChain (ontological \u2192 ontological)"
426
+ },
427
+ impacts: {
428
+ from: ["L3"],
429
+ to: ["ontological"],
430
+ description: "Theme \u2192 ValueChain (L3 \u2192 ontological)"
431
+ },
432
+ // === Investment (ontological) ===
433
+ invested_in: {
434
+ from: ["ontological"],
435
+ to: ["ontological"],
436
+ description: "Investor \u2192 Company (ontological \u2192 ontological)"
437
+ },
438
+ raised_from: {
439
+ from: ["ontological"],
440
+ to: ["ontological"],
441
+ description: "Company \u2192 Investor (ontological \u2192 ontological)"
442
+ },
443
+ // === Entity↔Belief Bridge (OE-B) ===
444
+ about_entity: {
445
+ from: ["L3"],
446
+ to: ["ontological"],
447
+ description: "Belief/Question/Theme \u2192 Entity (L3 \u2192 ontological)"
448
+ },
449
+ entity_referenced_in: {
450
+ from: ["ontological"],
451
+ to: ["L2"],
452
+ description: "Entity \u2192 Evidence (ontological \u2192 L2)"
453
+ },
454
+ mentioned_in: {
455
+ from: ["ontological"],
456
+ to: ["L1"],
457
+ description: "Entity \u2192 Source document (ontological \u2192 L1)"
458
+ },
459
+ founded_by: {
460
+ from: ["ontological"],
461
+ to: ["ontological"],
462
+ description: "Company \u2192 Person (ontological \u2192 ontological)"
463
+ },
464
+ competes_with: {
465
+ from: ["ontological"],
466
+ to: ["ontological"],
467
+ description: "Company \u2192 Company (ontological \u2192 ontological)"
468
+ },
469
+ contains: {
470
+ from: ["ontological"],
471
+ to: ["ontological"],
472
+ description: "ValueChain \u2192 Function (ontological \u2192 ontological)"
473
+ },
474
+ // === Lifecycle (same layer only) ===
475
+ supersedes: {
476
+ from: ["L4", "L3", "L2", "L1", "ontological", "organizational"],
477
+ to: ["L4", "L3", "L2", "L1", "ontological", "organizational"],
478
+ description: "NewNode \u2192 OldNode (same layer only - validated separately)"
479
+ },
480
+ same_as: {
481
+ from: ["L4", "L3", "L2", "L1", "ontological", "organizational"],
482
+ to: ["L4", "L3", "L2", "L1", "ontological", "organizational"],
483
+ description: "Duplicate detection (same layer only - validated separately)"
484
+ },
485
+ // === Same-Type: Belief ↔ Belief (L3 → L3) ===
486
+ depends_on: {
487
+ from: ["L3"],
488
+ to: ["L3"],
489
+ description: "Belief B requires Belief A (L3 \u2192 L3)"
490
+ },
491
+ reinforces: {
492
+ from: ["L3"],
493
+ to: ["L3"],
494
+ description: "Beliefs strengthen each other (L3 \u2192 L3)"
495
+ },
496
+ parent_of: {
497
+ from: ["L3", "organizational"],
498
+ to: ["L3", "organizational"],
499
+ description: "A is higher-level than B (L3 \u2192 L3, organizational \u2192 organizational)"
500
+ },
501
+ child_of: {
502
+ from: ["L3"],
503
+ to: ["L3"],
504
+ description: "Belief A is more specific (L3 \u2192 L3)"
505
+ },
506
+ // === Same-Type: Question ↔ Question (L3 → L3) ===
507
+ prerequisite_for: {
508
+ from: ["L3"],
509
+ to: ["L3"],
510
+ description: "Question A must be answered first (L3 \u2192 L3)"
511
+ },
512
+ parallel_to: {
513
+ from: ["L3"],
514
+ to: ["L3"],
515
+ description: "Same topic, different angles (L3 \u2192 L3)"
516
+ },
517
+ // === Same-Type: Evidence ↔ Evidence (L2 → L2) ===
518
+ corroborates: {
519
+ from: ["L2"],
520
+ to: ["L2"],
521
+ description: "Independent support (L2 \u2192 L2)"
522
+ },
523
+ extends: {
524
+ from: ["L2"],
525
+ to: ["L2"],
526
+ description: "Adds depth (L2 \u2192 L2)"
527
+ },
528
+ same_source_as: {
529
+ from: ["L2"],
530
+ to: ["L2"],
531
+ description: "Same document/study (L2 \u2192 L2)"
532
+ },
533
+ same_theme_as: {
534
+ from: ["L2"],
535
+ to: ["L2"],
536
+ description: "Same topic/entity (L2 \u2192 L2)"
537
+ },
538
+ // === NEW: Deep Epistemic Analysis Edges (Phase: Schema Upgrade) ===
539
+ assumes: {
540
+ from: ["L3"],
541
+ to: ["L3"],
542
+ description: "Hidden dependency - Belief B implicitly assumes Belief A (L3 \u2192 L3)"
543
+ },
544
+ would_predict: {
545
+ from: ["L3"],
546
+ to: ["L2"],
547
+ description: "Pre-registered prediction - If Belief true, expect Evidence (L3 \u2192 L2)"
548
+ },
549
+ analogous_to: {
550
+ from: ["L3"],
551
+ to: ["L3"],
552
+ description: "Explicit analogy - Belief A is like Belief B (L3 \u2192 L3)"
553
+ },
554
+ independent_of: {
555
+ from: ["L2"],
556
+ to: ["L2"],
557
+ description: "True evidence independence - Evidence A independent of B (L2 \u2192 L2)"
558
+ }
559
+ // NOTE: Deprecated edge types (supports, contradicts, derived_from, cites,
560
+ // summarizes, related_to, partially_answers, blocks, refines, branches_from)
561
+ // have been REMOVED from the system. Use compliant alternatives instead.
562
+ };
563
+ function validateEdgeLayers(edgeType, fromLayer, toLayer) {
564
+ const rules = EDGE_LAYER_RULES[edgeType];
565
+ if (!rules) {
566
+ console.warn(
567
+ `[EdgeValidation] Unknown edge type: ${edgeType}, allowing by default`
568
+ );
569
+ return { valid: true };
570
+ }
571
+ if (edgeType === "supersedes") {
572
+ if (fromLayer !== toLayer) {
573
+ return {
574
+ valid: false,
575
+ reason: `${edgeType} edges must be between nodes of the same layer. Got ${fromLayer} \u2192 ${toLayer}`
576
+ };
577
+ }
578
+ return { valid: true };
579
+ }
580
+ if (!rules.from.includes(fromLayer)) {
581
+ return {
582
+ valid: false,
583
+ reason: `Edge type '${edgeType}' does not allow source layer ${fromLayer}. Allowed: ${rules.from.join(", ")}`
584
+ };
585
+ }
586
+ if (!rules.to.includes(toLayer)) {
587
+ return {
588
+ valid: false,
589
+ reason: `Edge type '${edgeType}' does not allow target layer ${toLayer}. Allowed: ${rules.to.join(", ")}`
590
+ };
591
+ }
592
+ return { valid: true };
593
+ }
594
+ function mapInsightSourceType(sourceType) {
595
+ switch (sourceType) {
596
+ case "verified":
597
+ case "proprietary":
598
+ return "human";
599
+ case "ai_generated":
600
+ return "ai_generated";
601
+ default:
602
+ return "human";
603
+ }
604
+ }
605
+ function mapVerificationStatus(status) {
606
+ switch (status) {
607
+ case "manually_verified":
608
+ return "human_verified";
609
+ case "deep_verified":
610
+ case "pre_screened":
611
+ return "ai_verified";
612
+ default:
613
+ return "unverified";
614
+ }
615
+ }
616
+ async function createEpistemicNodeForInsight(ctx, _insightId, insight) {
617
+ const now = Date.now();
618
+ const globalId = generateGlobalId();
619
+ const contentHash = generateContentHash("evidence", insight.text);
620
+ const existing = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash)).first();
621
+ if (existing && existing.status === "active") {
622
+ await ctx.db.patch(existing._id, {
623
+ updatedAt: now
624
+ });
625
+ return existing._id;
626
+ }
627
+ const nodeId = await ctx.db.insert("epistemicNodes", {
628
+ globalId,
629
+ nodeType: "evidence",
630
+ epistemicLayer: "L2",
631
+ // Evidence is at L2 (compression boundary)
632
+ canonicalText: insight.text,
633
+ contentHash,
634
+ title: insight.text.slice(0, 100) + (insight.text.length > 100 ? "..." : ""),
635
+ tags: insight.tags,
636
+ metadata: {
637
+ kind: insight.kind,
638
+ pillar: insight.tags?.find(
639
+ (t) => [
640
+ "market",
641
+ "competition",
642
+ "product",
643
+ "team",
644
+ "financials",
645
+ "regulatory",
646
+ "timing",
647
+ "customer",
648
+ "technology",
649
+ "distribution"
650
+ ].includes(t)
651
+ ),
652
+ // Include sourceArtifactId for source document panel
653
+ sourceArtifactId: insight.sourceArtifactId,
654
+ // Include sourceQuestionId if this evidence was created to answer a question
655
+ sourceQuestionId: insight.sourceQuestionId,
656
+ // Include sourceAnchor for linking evidence back to source documents
657
+ sourceAnchor: insight.sourceAnchor
658
+ },
659
+ sourceType: mapInsightSourceType(insight.sourceType),
660
+ aiProvider: insight.aiProvider,
661
+ confidence: insight.verificationStatus === "manually_verified" ? 0.9 : insight.verificationStatus === "deep_verified" ? 0.7 : insight.verificationStatus === "pre_screened" ? 0.5 : 0.3,
662
+ verificationStatus: mapVerificationStatus(insight.verificationStatus),
663
+ status: "active",
664
+ topicId: insight.projectId,
665
+ createdBy: insight.createdBy,
666
+ createdAt: now,
667
+ updatedAt: now
668
+ });
669
+ if (insight.sourceArtifactId) {
670
+ const sourceNode = await findOrCreateSourceNode(
671
+ ctx,
672
+ insight.sourceArtifactId,
673
+ insight.createdBy,
674
+ insight.projectId
675
+ );
676
+ if (sourceNode) {
677
+ await createEpistemicEdge(ctx, {
678
+ fromNodeId: nodeId,
679
+ toNodeId: sourceNode,
680
+ edgeType: "derived_from",
681
+ projectId: insight.projectId,
682
+ createdBy: insight.createdBy
683
+ });
684
+ }
685
+ }
686
+ return nodeId;
687
+ }
688
+ async function createEpistemicNodeForBelief(ctx, beliefId, belief) {
689
+ const now = Date.now();
690
+ const globalId = generateGlobalId();
691
+ const contentHash = generateContentHash("belief", belief.belief);
692
+ const existing = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash)).first();
693
+ if (existing && existing.status === "active") {
694
+ await ctx.db.patch(existing._id, {
695
+ metadata: {
696
+ ...existing.metadata || {},
697
+ sourceBeliefId: beliefId
698
+ },
699
+ updatedAt: now
700
+ });
701
+ return existing._id;
702
+ }
703
+ const nodeId = await ctx.db.insert("epistemicNodes", {
704
+ globalId,
705
+ nodeType: "belief",
706
+ epistemicLayer: "L3",
707
+ // Beliefs are at L3 (traversal anchors)
708
+ canonicalText: belief.belief,
709
+ contentHash,
710
+ content: belief.rationale,
711
+ title: belief.belief.slice(0, 100) + (belief.belief.length > 100 ? "..." : ""),
712
+ metadata: {
713
+ sourceBeliefId: beliefId,
714
+ beliefStatus: "assumption",
715
+ topic: belief.topic,
716
+ // Use 'topic' for consistency with legacy schema
717
+ pillar: belief.topic,
718
+ // Keep 'pillar' for backward compat with existing data
719
+ // No confidence/confidenceLevel — only set after sprint completion
720
+ rationale: belief.rationale,
721
+ status: "active",
722
+ // Include sourceAnchor for linking beliefs back to source documents
723
+ sourceAnchor: belief.sourceAnchor
724
+ },
725
+ beliefStatus: "assumption",
726
+ epistemicStatus: "assumption",
727
+ sourceType: "human",
728
+ confidence: void 0,
729
+ // No confidence until sprint completion
730
+ verificationStatus: "unverified",
731
+ status: "active",
732
+ topicId: belief.projectId,
733
+ createdBy: belief.createdBy,
734
+ createdAt: now,
735
+ updatedAt: now
736
+ });
737
+ return nodeId;
738
+ }
739
+ async function createEpistemicNodeForQuestion(ctx, _questionId, question) {
740
+ const now = Date.now();
741
+ const globalId = generateGlobalId();
742
+ const contentHash = generateContentHash("question", question.question);
743
+ const existing = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash)).first();
744
+ if (existing && existing.status === "active") {
745
+ await ctx.db.patch(existing._id, {
746
+ updatedAt: now
747
+ });
748
+ return existing._id;
749
+ }
750
+ const sourceType = question.source === "manual" ? "human" : question.source === "ai_suggested" ? "ai_generated" : "ai_extracted";
751
+ const nodeId = await ctx.db.insert("epistemicNodes", {
752
+ globalId,
753
+ nodeType: "question",
754
+ epistemicLayer: "L3",
755
+ // Questions are at L3 (traversal anchors)
756
+ canonicalText: question.question,
757
+ contentHash,
758
+ title: question.question.slice(0, 100) + (question.question.length > 100 ? "..." : ""),
759
+ metadata: {
760
+ pillar: question.category,
761
+ priority: question.priority,
762
+ source: question.source,
763
+ // Include sourceAnchor for linking questions back to source documents
764
+ sourceAnchor: question.sourceAnchor
765
+ },
766
+ sourceType,
767
+ verificationStatus: "unverified",
768
+ status: "active",
769
+ topicId: question.projectId,
770
+ createdBy: question.createdBy,
771
+ createdAt: now,
772
+ updatedAt: now
773
+ });
774
+ return nodeId;
775
+ }
776
+ async function createEpistemicNodeForArtifact(ctx, artifactId, artifact) {
777
+ const now = Date.now();
778
+ const globalId = generateGlobalId();
779
+ let nodeType = "source";
780
+ const isSynthesis = artifact.isDeepResearch || artifact.type.includes("deep") || artifact.type.includes("research") || artifact.type.includes("primer");
781
+ if (isSynthesis) {
782
+ nodeType = "synthesis";
783
+ console.log(
784
+ `[EpistemicHelpers] Skipping synthesis node creation for "${artifact.type}" - will create when evidence is extracted`
785
+ );
786
+ const contentHash2 = generateContentHash(
787
+ nodeType,
788
+ artifact.title + artifact.content.slice(0, 500)
789
+ );
790
+ const existing2 = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash2)).first();
791
+ if (existing2 && existing2.status === "active") {
792
+ return existing2._id;
793
+ }
794
+ throw new Error("SKIP_SYNTHESIS_NODE_CREATION");
795
+ }
796
+ const contentHash = generateContentHash(
797
+ nodeType,
798
+ artifact.title + artifact.content.slice(0, 500)
799
+ );
800
+ const existing = await ctx.db.query("epistemicNodes").withIndex("by_contentHash", (q) => q.eq("contentHash", contentHash)).first();
801
+ if (existing && existing.status === "active") {
802
+ await ctx.db.patch(existing._id, {
803
+ metadata: {
804
+ ...existing.metadata || {},
805
+ legacyArtifactId: artifactId
806
+ },
807
+ updatedAt: now
808
+ });
809
+ return existing._id;
810
+ }
811
+ const epistemicLayer = "L1";
812
+ const nodeId = await ctx.db.insert("epistemicNodes", {
813
+ globalId,
814
+ nodeType,
815
+ epistemicLayer,
816
+ // Synthesis is L2, Source is L1
817
+ canonicalText: artifact.title,
818
+ contentHash,
819
+ content: artifact.content,
820
+ contentType: "markdown",
821
+ title: artifact.title,
822
+ metadata: {
823
+ legacyArtifactId: artifactId,
824
+ artifactType: artifact.type
825
+ },
826
+ sourceType: "ai_generated",
827
+ aiProvider: artifact.aiProvider || (artifact.isDeepResearch ? "gemini" : "anthropic"),
828
+ verificationStatus: "unverified",
829
+ status: "active",
830
+ topicId: artifact.projectId,
831
+ createdBy: artifact.createdBy,
832
+ createdAt: now,
833
+ updatedAt: now
834
+ });
835
+ return nodeId;
836
+ }
837
+ async function findOrCreateSourceNode(ctx, artifactId, createdBy, scopeProjectId) {
838
+ const artifact = await ctx.db.get(artifactId);
839
+ const effectiveProjectId = scopeProjectId || artifact?.projectId;
840
+ const effectiveTopicId = artifact?.topicId;
841
+ let existingNodes;
842
+ if (effectiveTopicId) {
843
+ existingNodes = await ctx.db.query("epistemicNodes").withIndex("by_topic", (q) => q.eq("topicId", effectiveTopicId)).collect();
844
+ } else if (effectiveProjectId) {
845
+ existingNodes = await ctx.db.query("epistemicNodes").withIndex("by_project", (q) => q.eq("projectId", effectiveProjectId)).collect();
846
+ } else {
847
+ existingNodes = await ctx.db.query("epistemicNodes").withIndex("by_nodeType", (q) => q.eq("nodeType", "source")).collect();
848
+ }
849
+ const existing = existingNodes.find((n) => {
850
+ const metadata = n.metadata;
851
+ return metadata?.legacyArtifactId === artifactId;
852
+ });
853
+ if (existing) {
854
+ return existing._id;
855
+ }
856
+ if (!artifact) {
857
+ return null;
858
+ }
859
+ const now = Date.now();
860
+ const globalId = generateGlobalId();
861
+ const artifactType = artifact.metadata?.type || "";
862
+ const isDeepResearch = artifact.metadata?.isDeepResearch;
863
+ let nodeType = "source";
864
+ if (isDeepResearch || artifactType.includes("deep") || artifactType.includes("research")) {
865
+ nodeType = "synthesis";
866
+ } else if (artifactType.includes("primer")) {
867
+ nodeType = "synthesis";
868
+ }
869
+ const title = artifact.metadata?.title || artifact.metadata?.theme || "Untitled";
870
+ const contentHash = generateContentHash(
871
+ nodeType,
872
+ title + (artifact.content?.slice(0, 500) || "")
873
+ );
874
+ const epistemicLayer = nodeType === "synthesis" ? "L2" : "L1";
875
+ const nodeId = await ctx.db.insert("epistemicNodes", {
876
+ globalId,
877
+ nodeType,
878
+ epistemicLayer,
879
+ // Synthesis is L2, Source is L1
880
+ canonicalText: title,
881
+ contentHash,
882
+ content: artifact.content,
883
+ contentType: "markdown",
884
+ title,
885
+ metadata: {
886
+ legacyArtifactId: artifactId,
887
+ artifactType,
888
+ stage: artifact.stage
889
+ },
890
+ sourceType: isDeepResearch ? "ai_generated" : "ai_extracted",
891
+ aiProvider: isDeepResearch ? "gemini" : "anthropic",
892
+ verificationStatus: "unverified",
893
+ status: "active",
894
+ topicId: artifact.projectId,
895
+ createdBy,
896
+ createdAt: now,
897
+ updatedAt: now
898
+ });
899
+ return nodeId;
900
+ }
901
+ function normalizeConfidence(confidence) {
902
+ if (typeof confidence === "number") {
903
+ return confidence > 1 ? confidence / 100 : confidence;
904
+ }
905
+ if (typeof confidence === "string") {
906
+ switch (confidence) {
907
+ case "high":
908
+ return 0.8;
909
+ case "medium":
910
+ return 0.5;
911
+ case "low":
912
+ return 0.3;
913
+ default:
914
+ return 0.5;
915
+ }
916
+ }
917
+ return 0.5;
918
+ }
919
+ async function createEpistemicEdge(ctx, params) {
920
+ const globalId = generateGlobalId();
921
+ const fromNode = await ctx.db.get(params.fromNodeId);
922
+ const toNode = await ctx.db.get(params.toNodeId);
923
+ if (!fromNode || !toNode) {
924
+ throw new Error("One or both nodes not found");
925
+ }
926
+ const fromLayer = fromNode.epistemicLayer || getNodeLayer(fromNode.nodeType);
927
+ const toLayer = toNode.epistemicLayer || getNodeLayer(toNode.nodeType);
928
+ if (!params.skipLayerValidation) {
929
+ const validation = validateEdgeLayers(params.edgeType, fromLayer, toLayer);
930
+ if (!validation.valid) {
931
+ throw new Error(
932
+ `[EdgeValidation] Invalid edge: ${validation.reason}. Attempted: ${params.edgeType} from ${fromNode.nodeType}(${fromLayer}) \u2192 ${toNode.nodeType}(${toLayer})`
933
+ );
934
+ }
935
+ }
936
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
937
+ globalId,
938
+ fromGlobalId: fromNode.globalId,
939
+ toGlobalId: toNode.globalId,
940
+ edgeType: params.edgeType,
941
+ weight: params.weight,
942
+ confidence: params.confidence,
943
+ context: params.context,
944
+ createdBy: params.createdBy,
945
+ topicId: params.projectId ? String(params.projectId) : void 0,
946
+ fromNodeType: fromNode.nodeType,
947
+ toNodeType: toNode.nodeType,
948
+ fromLayer,
949
+ toLayer
950
+ });
951
+ return globalId;
952
+ }
953
+ async function createEdgeForInsightQuestionLink(ctx, questionId, insightId, createdBy) {
954
+ console.log("[EpistemicSpine] Creating edge for insight-question link:", {
955
+ questionId: String(questionId),
956
+ insightId: String(insightId)
957
+ });
958
+ const questionNode = await findNodeByLegacyId(ctx, "question", questionId);
959
+ const insightNode = await findNodeByLegacyId(ctx, "insight", insightId);
960
+ console.log("[EpistemicSpine] Found nodes:", {
961
+ questionNode: questionNode ? String(questionNode) : null,
962
+ insightNode: insightNode ? String(insightNode) : null
963
+ });
964
+ if (!questionNode || !insightNode) {
965
+ console.log("[EpistemicSpine] Missing nodes, skipping edge creation");
966
+ return null;
967
+ }
968
+ const question = await ctx.db.get(questionId);
969
+ const projectId = question?.projectId;
970
+ return await createEpistemicEdge(ctx, {
971
+ fromNodeId: insightNode,
972
+ toNodeId: questionNode,
973
+ edgeType: "derived_from",
974
+ projectId,
975
+ createdBy,
976
+ context: "Linked from questions workspace"
977
+ });
978
+ }
979
+ async function findNodeByLegacyId(ctx, legacyType, legacyId) {
980
+ const nodeType = legacyType === "insight" ? "evidence" : legacyType === "artifact" ? "source" : legacyType;
981
+ const nodes = await ctx.db.query("epistemicNodes").withIndex("by_nodeType", (q) => q.eq("nodeType", nodeType)).collect();
982
+ const legacyKey = `legacy${legacyType.charAt(0).toUpperCase() + legacyType.slice(1)}Id`;
983
+ const legacyIdStr = String(legacyId);
984
+ const found = nodes.find((n) => {
985
+ const metadata = n.metadata;
986
+ const storedId = metadata?.[legacyKey];
987
+ return storedId && String(storedId) === legacyIdStr;
988
+ });
989
+ if (!found) {
990
+ console.log(
991
+ `[EpistemicSpine] Node not found for ${legacyType}:${legacyIdStr}, searched ${nodes.length} ${nodeType} nodes`
992
+ );
993
+ }
994
+ return found?._id ?? null;
995
+ }
996
+
997
+ export { EDGE_LAYER_RULES, LAYER_TRAVERSAL_RULES, canTraverseToLayer, createEdgeForInsightQuestionLink, createEpistemicEdge, createEpistemicNodeForArtifact, createEpistemicNodeForBelief, createEpistemicNodeForInsight, createEpistemicNodeForQuestion, getDefaultEdgeTypesForMode, getDefaultMinLayer, getLayerDepth, getNeo4jLabel, getNeo4jRelType, getNodeLayer, isDeprecatedEdgeType, isValidLayerConnection, normalizeConfidence, shouldContinueTraversal, validateEdgeLayers };
998
+ //# sourceMappingURL=epistemicHelpers.js.map
999
+ //# sourceMappingURL=epistemicHelpers.js.map