@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,2749 @@
1
+ import { v } from 'convex/values';
2
+ import { componentsGeneric, queryGeneric, mutationGeneric, anyApi, internalMutationGeneric } from 'convex/server';
3
+
4
+ // src/epistemicEdges.ts
5
+ var api = anyApi;
6
+ componentsGeneric();
7
+
8
+ // ../access-control/src/topicProjectOverlay.ts
9
+ var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
10
+ function readNonEmptyString(value) {
11
+ if (typeof value !== "string") {
12
+ return;
13
+ }
14
+ const normalized = value.trim();
15
+ return normalized.length > 0 ? normalized : void 0;
16
+ }
17
+ function readStringArray(value) {
18
+ if (!Array.isArray(value)) {
19
+ return [];
20
+ }
21
+ return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
22
+ }
23
+ function readMetadata(topic) {
24
+ return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
25
+ }
26
+ function readLegacyProjectId(value) {
27
+ if (!value) {
28
+ return;
29
+ }
30
+ return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
31
+ }
32
+ function coerceVisibility(value) {
33
+ return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
34
+ }
35
+ function coerceStatus(value) {
36
+ return value === "active" || value === "archived" || value === "watching" ? value : void 0;
37
+ }
38
+ function mapProjectType(topic, metadata) {
39
+ const explicit = readNonEmptyString(metadata.projectType);
40
+ if (explicit) {
41
+ return explicit;
42
+ }
43
+ if (topic.type === "theme") {
44
+ return "thematic";
45
+ }
46
+ return readNonEmptyString(topic.type) || "general";
47
+ }
48
+ function isProjectLikeTopic(topic) {
49
+ const metadata = readMetadata(topic);
50
+ return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
51
+ }
52
+ async function resolveTopicDoc(ctx, scopeId) {
53
+ if (ctx?.db && typeof ctx.db.get === "function") {
54
+ try {
55
+ const directTopic = await ctx.db.get(scopeId);
56
+ if (directTopic) {
57
+ return directTopic;
58
+ }
59
+ } catch {
60
+ }
61
+ }
62
+ if (typeof ctx.runQuery !== "function") {
63
+ return null;
64
+ }
65
+ try {
66
+ const topic = await ctx.runQuery(api.topics.get, {
67
+ id: String(scopeId)
68
+ });
69
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
70
+ return topic;
71
+ }
72
+ } catch {
73
+ }
74
+ try {
75
+ const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
76
+ projectId: String(scopeId)
77
+ });
78
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
79
+ return topic;
80
+ }
81
+ } catch {
82
+ }
83
+ return null;
84
+ }
85
+ function materializeTopicProjectOverlay(topic, idMode = "legacy") {
86
+ const metadata = readMetadata(topic);
87
+ const topicId = String(topic._id);
88
+ const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
89
+ const storageProjectId = legacyProjectId || topicId;
90
+ const outwardId = idMode === "topic" ? topicId : storageProjectId;
91
+ const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
92
+ const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
93
+ const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
94
+ const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
95
+ return {
96
+ ...metadata,
97
+ _id: outwardId,
98
+ projectId: outwardId,
99
+ topicId,
100
+ storageProjectId,
101
+ legacyProjectId,
102
+ name: readNonEmptyString(topic.name) || "Untitled Theme",
103
+ type: mapProjectType(topic, metadata),
104
+ description: readNonEmptyString(topic.description),
105
+ ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
106
+ sharedWith: readStringArray(metadata.sharedWith),
107
+ visibility,
108
+ tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
109
+ workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
110
+ status,
111
+ tags: readStringArray(metadata.tags),
112
+ chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
113
+ artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
114
+ lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
115
+ _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
116
+ createdAt,
117
+ updatedAt
118
+ };
119
+ }
120
+ async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
121
+ const topic = await resolveTopicDoc(ctx, scopeId);
122
+ if (!topic) {
123
+ return null;
124
+ }
125
+ if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
126
+ return null;
127
+ }
128
+ return materializeTopicProjectOverlay(topic, options.idMode);
129
+ }
130
+ async function listTopicProjectOverlays(ctx, options = {}) {
131
+ let allTopics = [];
132
+ if (ctx?.db?.query && typeof ctx.db.query === "function") {
133
+ try {
134
+ allTopics = await ctx.db.query("topics").collect();
135
+ } catch {
136
+ allTopics = [];
137
+ }
138
+ }
139
+ if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
140
+ allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
141
+ }
142
+ return allTopics.filter(
143
+ (topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
144
+ ).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
145
+ }
146
+
147
+ // ../access-control/src/projectGrantsBridge.ts
148
+ var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
149
+ function normalizeString(value) {
150
+ if (typeof value !== "string") {
151
+ return;
152
+ }
153
+ const trimmed = value.trim();
154
+ return trimmed.length > 0 ? trimmed : void 0;
155
+ }
156
+ async function resolveGrantScopeIds(ctx, args) {
157
+ const topicId = normalizeString(args.topicId);
158
+ const projectId = normalizeString(args.projectId);
159
+ for (const scopeId of [topicId, projectId]) {
160
+ if (!scopeId) {
161
+ continue;
162
+ }
163
+ try {
164
+ const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
165
+ idMode: "legacy",
166
+ projectLikeOnly: false
167
+ });
168
+ if (overlay) {
169
+ return {
170
+ topicId: normalizeString(overlay.topicId) ?? topicId,
171
+ projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
172
+ };
173
+ }
174
+ } catch {
175
+ }
176
+ }
177
+ return { topicId, projectId };
178
+ }
179
+ async function normalizeProjectGrantRow(ctx, row) {
180
+ const scope = await resolveGrantScopeIds(ctx, {
181
+ topicId: row.topicId,
182
+ projectId: row.projectId
183
+ });
184
+ return {
185
+ ...row,
186
+ ...scope.topicId ? { topicId: scope.topicId } : {},
187
+ ...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
188
+ };
189
+ }
190
+ async function normalizeProjectGrantRows(ctx, rows) {
191
+ return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
192
+ }
193
+ async function listProjectGrantsByPrincipal(ctx, principalId) {
194
+ const rows = await Promise.all(
195
+ PROJECT_GRANT_STATUSES.map(
196
+ (status) => ctx.db.query("projectGrants").withIndex(
197
+ "by_principal_status",
198
+ (q) => q.eq("principalId", principalId).eq("status", status)
199
+ ).collect()
200
+ )
201
+ );
202
+ return await normalizeProjectGrantRows(ctx, rows.flat());
203
+ }
204
+ async function listProjectGrantsByGroup(ctx, groupId) {
205
+ const rows = await Promise.all(
206
+ PROJECT_GRANT_STATUSES.map(
207
+ (status) => ctx.db.query("projectGrants").withIndex(
208
+ "by_group_status",
209
+ (q) => q.eq("groupId", groupId).eq("status", status)
210
+ ).collect()
211
+ )
212
+ );
213
+ return await normalizeProjectGrantRows(ctx, rows.flat());
214
+ }
215
+ function buildScopeMatchers(inputScopeId, resolved) {
216
+ return new Set(
217
+ [inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
218
+ );
219
+ }
220
+ function matchesResolvedScope(row, scopeIds) {
221
+ const rowTopicId = normalizeString(row.topicId);
222
+ const rowProjectId = normalizeString(row.projectId);
223
+ return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
224
+ }
225
+ async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
226
+ const resolved = await resolveGrantScopeIds(ctx, { topicId });
227
+ const scopeIds = buildScopeMatchers(topicId, resolved);
228
+ const rows = await listProjectGrantsByPrincipal(ctx, principalId);
229
+ return rows.filter((row) => matchesResolvedScope(row, scopeIds));
230
+ }
231
+ async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
232
+ const resolved = await resolveGrantScopeIds(ctx, { topicId });
233
+ const scopeIds = buildScopeMatchers(topicId, resolved);
234
+ const rows = await listProjectGrantsByGroup(ctx, groupId);
235
+ return rows.filter((row) => matchesResolvedScope(row, scopeIds));
236
+ }
237
+ async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
238
+ const rows = await listProjectGrantsByPrincipal(ctx, principalId);
239
+ return rows.filter((row) => row.status === status);
240
+ }
241
+ async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
242
+ const rows = await listProjectGrantsByGroup(ctx, groupId);
243
+ return rows.filter((row) => row.status === status);
244
+ }
245
+ async function bridgeInsertProjectGrant(ctx, value) {
246
+ const resolved = await resolveGrantScopeIds(ctx, value);
247
+ return await ctx.db.insert("projectGrants", {
248
+ ...value,
249
+ ...resolved.topicId ? { topicId: resolved.topicId } : {},
250
+ ...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
251
+ });
252
+ }
253
+
254
+ // ../access-control/src/resolvers.ts
255
+ async function findUserByClerkId(ctx, clerkId) {
256
+ const normalizedClerkId = clerkId.trim();
257
+ if (!normalizedClerkId) {
258
+ return null;
259
+ }
260
+ if (typeof ctx.runQuery === "function") {
261
+ try {
262
+ const bridgedUser = await ctx.runQuery(api.users.getUserByClerkId, {
263
+ clerkId: normalizedClerkId
264
+ });
265
+ if (bridgedUser) {
266
+ return bridgedUser;
267
+ }
268
+ } catch {
269
+ }
270
+ }
271
+ try {
272
+ const users = await ctx.db.query("users").collect();
273
+ return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
274
+ } catch {
275
+ return null;
276
+ }
277
+ }
278
+ async function findUserByPrincipalId(ctx, principalId) {
279
+ const normalizedPrincipalId = principalId.trim();
280
+ if (!normalizedPrincipalId) {
281
+ return null;
282
+ }
283
+ try {
284
+ const users = await ctx.db.query("users").collect();
285
+ return users.find(
286
+ (user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
287
+ ) ?? null;
288
+ } catch {
289
+ return null;
290
+ }
291
+ }
292
+ async function findAgentByPrincipalId(ctx, principalId) {
293
+ const normalizedPrincipalId = principalId.trim();
294
+ if (!normalizedPrincipalId) {
295
+ return null;
296
+ }
297
+ if (typeof ctx.runQuery === "function") {
298
+ try {
299
+ const bridgedAgent = await ctx.runQuery(
300
+ api.agents.getAgentByPrincipalId,
301
+ {
302
+ principalId: normalizedPrincipalId
303
+ }
304
+ );
305
+ if (bridgedAgent) {
306
+ return bridgedAgent;
307
+ }
308
+ } catch {
309
+ }
310
+ }
311
+ try {
312
+ const agents = await ctx.db.query("agents").collect();
313
+ return agents.find(
314
+ (agent) => String(agent.principalId ?? "") === normalizedPrincipalId
315
+ ) ?? null;
316
+ } catch {
317
+ return null;
318
+ }
319
+ }
320
+ function defaultResolvers() {
321
+ return {
322
+ async getProject(ctx, topicId) {
323
+ return await resolveTopicProjectOverlay(ctx, topicId, {
324
+ idMode: "legacy",
325
+ projectLikeOnly: false
326
+ });
327
+ },
328
+ async listTopics(ctx) {
329
+ return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
330
+ },
331
+ async listTopicsByOwner(ctx, ownerId) {
332
+ const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
333
+ return topics.filter((topic) => topic.ownerId === ownerId);
334
+ },
335
+ async listTopicsByVisibility(ctx, visibility) {
336
+ const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
337
+ return topics.filter((topic) => topic.visibility === visibility);
338
+ },
339
+ async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
340
+ return await bridgeListProjectGrantsByTopicAndPrincipal(
341
+ ctx,
342
+ topicId,
343
+ principalId
344
+ );
345
+ },
346
+ async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
347
+ return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
348
+ },
349
+ async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
350
+ return await bridgeListProjectGrantsByPrincipalStatus(
351
+ ctx,
352
+ principalId,
353
+ status
354
+ );
355
+ },
356
+ async listProjectGrantsByGroupStatus(ctx, groupId, status) {
357
+ return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
358
+ },
359
+ async insertProjectGrant(ctx, value) {
360
+ return await bridgeInsertProjectGrant(ctx, value);
361
+ },
362
+ async getAgentByPrincipalId(ctx, principalId) {
363
+ return await findAgentByPrincipalId(ctx, principalId);
364
+ },
365
+ async getUserByClerkId(ctx, clerkId) {
366
+ return await findUserByClerkId(ctx, clerkId);
367
+ },
368
+ async getUserByPrincipalId(ctx, principalId) {
369
+ return await findUserByPrincipalId(ctx, principalId);
370
+ }
371
+ };
372
+ }
373
+ var resolverOverrides = {};
374
+ function resolveAccessControlAppResolvers(_ctx) {
375
+ return {
376
+ ...defaultResolvers(),
377
+ ...resolverOverrides
378
+ };
379
+ }
380
+
381
+ // ../access-control/src/principalContext.ts
382
+ function requireCanonicalResolvedUser(user, clerkId) {
383
+ const resolved = user;
384
+ if (!resolved) {
385
+ throw new Error(
386
+ `[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
387
+ );
388
+ }
389
+ const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
390
+ if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
391
+ throw new Error(
392
+ `[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
393
+ );
394
+ }
395
+ if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
396
+ throw new Error(
397
+ `[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
398
+ );
399
+ }
400
+ if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
401
+ throw new Error(
402
+ `[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
403
+ );
404
+ }
405
+ if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
406
+ throw new Error(
407
+ `[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
408
+ );
409
+ }
410
+ return {
411
+ mcRole,
412
+ defaultTenantId: defaultTenantId.trim(),
413
+ defaultWorkspaceId: defaultWorkspaceId.trim(),
414
+ defaultPrincipalId: defaultPrincipalId.trim()
415
+ };
416
+ }
417
+ function isPrincipalIdInput(value) {
418
+ return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
419
+ }
420
+ async function resolveCanonicalUserRecord(ctx, actorId) {
421
+ const normalizedActorId = actorId.trim();
422
+ const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
423
+ const resolvers = resolveAccessControlAppResolvers();
424
+ const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
425
+ if (resolvedByClerkId) {
426
+ return {
427
+ resolvedUser: resolvedByClerkId,
428
+ clerkId,
429
+ contextClerkId: clerkId
430
+ };
431
+ }
432
+ const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
433
+ ctx,
434
+ normalizedActorId
435
+ );
436
+ return {
437
+ resolvedUser: resolvedByPrincipalId ?? null,
438
+ clerkId,
439
+ contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
440
+ };
441
+ }
442
+ function uniqRoles(roles) {
443
+ const roleSet = /* @__PURE__ */ new Set();
444
+ for (const role of roles) {
445
+ if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
446
+ roleSet.add(role);
447
+ }
448
+ }
449
+ return [...roleSet];
450
+ }
451
+ function normalizeGroupIds(value) {
452
+ if (!Array.isArray(value)) {
453
+ return [];
454
+ }
455
+ return [...new Set(
456
+ value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
457
+ )];
458
+ }
459
+ function requireServiceAgentUser(user, actorId) {
460
+ const canonicalUser = requireCanonicalResolvedUser(user, actorId);
461
+ if (canonicalUser.mcRole !== "service_agent") {
462
+ throw new Error(
463
+ `[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
464
+ );
465
+ }
466
+ return canonicalUser;
467
+ }
468
+ function requireCanonicalResolvedAgent(agent, actorId) {
469
+ const resolved = agent;
470
+ if (!resolved) {
471
+ throw new Error(
472
+ `[AccessControl] Agent "${actorId}" not found in agents or users table.`
473
+ );
474
+ }
475
+ if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
476
+ throw new Error(
477
+ `[AccessControl] Canonical agent principalId required for ${actorId}.`
478
+ );
479
+ }
480
+ if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
481
+ throw new Error(
482
+ `[AccessControl] Canonical home tenant required for ${actorId}.`
483
+ );
484
+ }
485
+ if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
486
+ throw new Error(
487
+ `[AccessControl] Canonical home workspace required for ${actorId}.`
488
+ );
489
+ }
490
+ return {
491
+ principalId: resolved.principalId.trim(),
492
+ tenantId: resolved.tenantId.trim(),
493
+ workspaceId: resolved.workspaceId.trim(),
494
+ roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
495
+ groupIds: normalizeGroupIds(resolved.groupIds)
496
+ };
497
+ }
498
+ async function resolvePrincipalContext(ctx, actorId) {
499
+ if (actorId.startsWith("agent:")) {
500
+ const resolvers = resolveAccessControlAppResolvers();
501
+ const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
502
+ if (resolvedAgent) {
503
+ const agent = requireCanonicalResolvedAgent(
504
+ resolvedAgent,
505
+ actorId
506
+ );
507
+ return {
508
+ principalId: agent.principalId,
509
+ principalType: "service",
510
+ clerkId: actorId,
511
+ tenantId: agent.tenantId,
512
+ workspaceId: agent.workspaceId,
513
+ roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
514
+ groupIds: agent.groupIds,
515
+ isPlatformAdmin: false,
516
+ isTenantAdmin: false,
517
+ isWorkspaceAdmin: false,
518
+ isSystemFallback: false
519
+ };
520
+ }
521
+ const resolvedUser2 = await resolvers.getUserByClerkId(
522
+ ctx,
523
+ actorId
524
+ );
525
+ if (!resolvedUser2) {
526
+ throw new Error(
527
+ `[AccessControl] Agent "${actorId}" not found in agents or users table.`
528
+ );
529
+ }
530
+ const user2 = requireServiceAgentUser(
531
+ resolvedUser2,
532
+ actorId
533
+ );
534
+ console.warn(
535
+ `[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
536
+ );
537
+ return {
538
+ principalId: user2.defaultPrincipalId,
539
+ principalType: "service",
540
+ clerkId: actorId,
541
+ tenantId: user2.defaultTenantId,
542
+ workspaceId: user2.defaultWorkspaceId,
543
+ roles: ["service_agent"],
544
+ groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
545
+ isPlatformAdmin: false,
546
+ isTenantAdmin: false,
547
+ isWorkspaceAdmin: false,
548
+ isSystemFallback: false
549
+ };
550
+ }
551
+ const {
552
+ resolvedUser,
553
+ contextClerkId
554
+ } = await resolveCanonicalUserRecord(ctx, actorId);
555
+ const user = requireCanonicalResolvedUser(
556
+ resolvedUser,
557
+ contextClerkId
558
+ );
559
+ if (!user.defaultPrincipalId) {
560
+ throw new Error(
561
+ `[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
562
+ );
563
+ }
564
+ if (user.mcRole === "service_agent") {
565
+ return {
566
+ principalId: user.defaultPrincipalId,
567
+ principalType: "service",
568
+ clerkId: contextClerkId,
569
+ tenantId: user.defaultTenantId,
570
+ workspaceId: user.defaultWorkspaceId,
571
+ roles: ["service_agent"],
572
+ groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
573
+ isPlatformAdmin: false,
574
+ isTenantAdmin: false,
575
+ isWorkspaceAdmin: false,
576
+ isSystemFallback: false
577
+ };
578
+ }
579
+ const principalId = user.defaultPrincipalId;
580
+ const effectiveRole = user.mcRole;
581
+ const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
582
+ const tenantId = user.defaultTenantId;
583
+ const workspaceId = user.defaultWorkspaceId;
584
+ const isPlatformAdmin = effectiveRole === "platform_admin";
585
+ return {
586
+ principalId,
587
+ principalType: "user",
588
+ clerkId: contextClerkId,
589
+ tenantId,
590
+ workspaceId,
591
+ roles: uniqRoles(roles),
592
+ groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
593
+ isPlatformAdmin,
594
+ isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
595
+ isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
596
+ isSystemFallback: false
597
+ };
598
+ }
599
+
600
+ // ../access-control/src/access.ts
601
+ function isTopicInPrincipalTenant(topic, principalTenantId) {
602
+ if (!topic.tenantId) {
603
+ return false;
604
+ }
605
+ if (!principalTenantId) {
606
+ return false;
607
+ }
608
+ return String(topic.tenantId) === String(principalTenantId);
609
+ }
610
+ function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
611
+ if (!topic.workspaceId) {
612
+ return false;
613
+ }
614
+ if (!principalWorkspaceId) {
615
+ return false;
616
+ }
617
+ return String(topic.workspaceId) === String(principalWorkspaceId);
618
+ }
619
+ function isLegacyUnscopedTopic(topic) {
620
+ return !topic.tenantId || !topic.workspaceId;
621
+ }
622
+ function isGrantScopeAlignedToTopic(topic, grant) {
623
+ if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
624
+ return false;
625
+ }
626
+ if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
627
+ return false;
628
+ }
629
+ return true;
630
+ }
631
+ function isGrantSourceAllowedForVisibility(visibility, source) {
632
+ if (source !== "external_share") {
633
+ return true;
634
+ }
635
+ return visibility === "external" || visibility === "public";
636
+ }
637
+ function isGrantActive(grant) {
638
+ if (grant.status !== "active") {
639
+ return false;
640
+ }
641
+ if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
642
+ return false;
643
+ }
644
+ return true;
645
+ }
646
+ async function hasPrincipalGrant(ctx, args) {
647
+ const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndPrincipal(
648
+ ctx,
649
+ args.topic._id,
650
+ args.principalId
651
+ );
652
+ if (grants.some(
653
+ (grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
654
+ args.topic.visibility,
655
+ grant.source
656
+ ) && (!args.principalIsExternal || args.topic.visibility === "public" || grant.source === "external_share")
657
+ )) {
658
+ return true;
659
+ }
660
+ return false;
661
+ }
662
+ async function hasGroupGrant(ctx, args) {
663
+ if (args.groupIds.length === 0) {
664
+ return false;
665
+ }
666
+ for (const groupId of args.groupIds) {
667
+ const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndGroup(ctx, args.topic._id, groupId);
668
+ if (grants.some(
669
+ (grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
670
+ args.topic.visibility,
671
+ grant.source
672
+ )
673
+ )) {
674
+ return true;
675
+ }
676
+ }
677
+ return false;
678
+ }
679
+ function isExternalPrincipal(_ctx, _args) {
680
+ return false;
681
+ }
682
+ async function evaluateTopicAccessDetailed(ctx, args) {
683
+ if (args.legacyUserId) {
684
+ return {
685
+ hasAccess: true,
686
+ isAdmin: false,
687
+ isOwner: false,
688
+ isShared: false,
689
+ hasGrant: true,
690
+ isFirmVisible: true,
691
+ isExternalVisible: false,
692
+ isPublicVisible: false,
693
+ isTenantScopeMatch: true,
694
+ isWorkspaceScopeMatch: true,
695
+ isPrincipalExternal: false
696
+ };
697
+ }
698
+ const topic = await resolveAccessControlAppResolvers().getProject(
699
+ ctx,
700
+ args.topicId
701
+ );
702
+ if (!topic) {
703
+ return {
704
+ hasAccess: false,
705
+ isAdmin: false,
706
+ isOwner: false,
707
+ isShared: false,
708
+ hasGrant: false,
709
+ isFirmVisible: false,
710
+ isExternalVisible: false,
711
+ isPublicVisible: false,
712
+ isTenantScopeMatch: false,
713
+ isWorkspaceScopeMatch: false,
714
+ isPrincipalExternal: false
715
+ };
716
+ }
717
+ const { principalContext, legacyUserId } = args;
718
+ const userIsAdmin = principalContext.isPlatformAdmin;
719
+ const isOwner = topic.ownerId === legacyUserId;
720
+ const isShared = (topic.sharedWith ?? []).includes(legacyUserId);
721
+ const principalIsExternal = await isExternalPrincipal(ctx, {
722
+ groupIds: principalContext.groupIds,
723
+ topicTenantId: topic.tenantId,
724
+ topicWorkspaceId: topic.workspaceId
725
+ });
726
+ const hasPrincipalGrantResult = await hasPrincipalGrant(ctx, {
727
+ topic,
728
+ principalId: principalContext.principalId,
729
+ principalIsExternal
730
+ });
731
+ const hasGroupGrantResult = await hasGroupGrant(ctx, {
732
+ topic,
733
+ groupIds: principalContext.groupIds
734
+ });
735
+ const hasGrant = isShared || hasPrincipalGrantResult || hasGroupGrantResult;
736
+ const legacyUnscoped = isLegacyUnscopedTopic(topic);
737
+ const tenantScopeMatch = isTopicInPrincipalTenant(
738
+ topic,
739
+ principalContext.tenantId
740
+ );
741
+ const workspaceScopeMatch = isTopicInPrincipalWorkspace(
742
+ topic,
743
+ principalContext.workspaceId
744
+ );
745
+ const isPublicVisible = topic.visibility === "public";
746
+ const isFirmVisible = topic.visibility === "firm" && !legacyUnscoped && tenantScopeMatch && workspaceScopeMatch && !principalIsExternal;
747
+ const hasScopedGrant = hasGrant && (legacyUnscoped || tenantScopeMatch && workspaceScopeMatch);
748
+ const isExternalVisible = topic.visibility === "external" && hasScopedGrant;
749
+ const hasAccess = userIsAdmin || isOwner || hasScopedGrant || isPublicVisible || isFirmVisible;
750
+ return {
751
+ hasAccess,
752
+ isAdmin: userIsAdmin,
753
+ isOwner,
754
+ isShared,
755
+ hasGrant,
756
+ isFirmVisible,
757
+ isExternalVisible,
758
+ isPublicVisible,
759
+ isTenantScopeMatch: tenantScopeMatch,
760
+ isWorkspaceScopeMatch: workspaceScopeMatch,
761
+ isPrincipalExternal: principalIsExternal
762
+ };
763
+ }
764
+ async function checkTopicAccessDetailed(ctx, topicId, userId) {
765
+ const principalContext = await resolvePrincipalContext(ctx, userId);
766
+ return evaluateTopicAccessDetailed(ctx, {
767
+ topicId,
768
+ legacyUserId: userId,
769
+ principalContext
770
+ });
771
+ }
772
+ async function checkTopicAccess(ctx, topicId, userId) {
773
+ const result = await checkTopicAccessDetailed(ctx, topicId, userId);
774
+ return result.hasAccess;
775
+ }
776
+ async function checkScopeAccess(ctx, scopeId, userId) {
777
+ try {
778
+ const topic = await ctx.db.get(scopeId);
779
+ if (topic && topic.name !== void 0 && topic.type !== void 0) {
780
+ return true;
781
+ }
782
+ } catch {
783
+ }
784
+ try {
785
+ return await checkTopicAccess(ctx, scopeId, userId);
786
+ } catch {
787
+ return false;
788
+ }
789
+ }
790
+ async function requireTopicAccess(ctx, topicId, userId) {
791
+ const hasAccess = await checkTopicAccess(ctx, topicId, userId);
792
+ if (!hasAccess) {
793
+ throw new Error(
794
+ "Access denied: You don't have permission to access this topic"
795
+ );
796
+ }
797
+ }
798
+ var requireProjectAccess = requireTopicAccess;
799
+ var permissiveReturn = v.optional(v.any());
800
+ var looseJsonObject = v.record(v.string(), v.any());
801
+ var looseJsonArray = v.array(v.any());
802
+ v.union(
803
+ v.string(),
804
+ v.number(),
805
+ v.boolean(),
806
+ v.null(),
807
+ looseJsonObject,
808
+ looseJsonArray
809
+ );
810
+ var api2 = anyApi;
811
+ componentsGeneric();
812
+ var internal = anyApi;
813
+ var internalMutation = internalMutationGeneric;
814
+ var mutation = mutationGeneric;
815
+ var query = queryGeneric;
816
+
817
+ // src/graphTypes.ts
818
+ var EDGE_TYPE_TO_REL = {
819
+ // === THE SIX CANONICAL EPISTEMIC EDGE TYPES ===
820
+ supports: "SUPPORTS",
821
+ // L3↔L3: belief bears on belief (weight -1 to +1)
822
+ informs: "INFORMS",
823
+ // L2→L3: evidence bears on belief
824
+ depends_on: "DEPENDS_ON",
825
+ // L3→L3, Q→Q: structural gate
826
+ derived_from: "DERIVED_FROM",
827
+ // Any→Any: provenance chain (fork, synthesis, extraction, answer)
828
+ contains: "CONTAINS",
829
+ // Any→Any: hierarchy, scoping, membership
830
+ tests: "TESTS",
831
+ // Q→L3: question interrogates belief
832
+ // === L4 DECISION EDGES (derived_from with derivationType=decision) ===
833
+ // Kept as separate Neo4j relationship types for backward compat with L4 queries.
834
+ // New code should use derived_from + metadata.
835
+ based_on_belief: "BASED_ON_BELIEF",
836
+ based_on_question: "BASED_ON_QUESTION",
837
+ blocked_by_contradiction: "BLOCKED_BY_CONTRADICTION",
838
+ informed_by_theme: "INFORMED_BY_THEME",
839
+ // === ONTOLOGICAL EDGES (tenant-extensible, managed by ontology system) ===
840
+ works_at: "WORKS_AT",
841
+ invested_in: "INVESTED_IN",
842
+ competes_with: "COMPETES_WITH",
843
+ participates_in: "PARTICIPATES_IN",
844
+ founded_by: "FOUNDED_BY",
845
+ evaluates: "EVALUATES",
846
+ performs: "PERFORMS",
847
+ function_in: "FUNCTION_IN",
848
+ impacts: "IMPACTS",
849
+ raised_from: "RAISED_FROM",
850
+ mentioned_in: "MENTIONED_IN",
851
+ perspective_on: "PERSPECTIVE_ON",
852
+ about_entity: "ABOUT_ENTITY",
853
+ entity_referenced_in: "ENTITY_REFERENCED_IN"
854
+ };
855
+ function getNodeLayer(nodeType) {
856
+ const L4_TYPES = ["decision"];
857
+ const L3_TYPES = ["belief", "question", "theme", "deal"];
858
+ const L2_TYPES = ["claim", "evidence", "synthesis", "answer"];
859
+ const L1_TYPES = ["atomic_fact", "excerpt", "source"];
860
+ const ONTOLOGICAL_TYPES = [
861
+ "company",
862
+ "person",
863
+ "investor",
864
+ "function",
865
+ "value_chain"
866
+ ];
867
+ const ORGANIZATIONAL_TYPES = ["topic"];
868
+ if (L4_TYPES.includes(nodeType)) {
869
+ return "L4";
870
+ }
871
+ if (L3_TYPES.includes(nodeType)) {
872
+ return "L3";
873
+ }
874
+ if (L2_TYPES.includes(nodeType)) {
875
+ return "L2";
876
+ }
877
+ if (L1_TYPES.includes(nodeType)) {
878
+ return "L1";
879
+ }
880
+ if (ONTOLOGICAL_TYPES.includes(nodeType)) {
881
+ return "ontological";
882
+ }
883
+ if (ORGANIZATIONAL_TYPES.includes(nodeType)) {
884
+ return "organizational";
885
+ }
886
+ console.warn(`[GraphTypes] Unknown nodeType "${nodeType}", defaulting to L2`);
887
+ return "L2";
888
+ }
889
+ var CANONICAL_EPISTEMIC_TYPES = /* @__PURE__ */ new Set([
890
+ "supports",
891
+ "informs",
892
+ "depends_on",
893
+ "derived_from",
894
+ "contains",
895
+ "tests"
896
+ ]);
897
+ function isDeprecatedEdgeType(edgeType) {
898
+ if (CANONICAL_EPISTEMIC_TYPES.has(edgeType)) return false;
899
+ if (edgeType in EDGE_TYPE_TO_REL) return false;
900
+ return true;
901
+ }
902
+
903
+ // src/edgeValidation.ts
904
+ function getLayerDepth(layer) {
905
+ switch (layer) {
906
+ case "L4":
907
+ return 4;
908
+ case "L3":
909
+ return 3;
910
+ case "L2":
911
+ return 2;
912
+ case "L1":
913
+ return 1;
914
+ case "ontological":
915
+ return 0;
916
+ default:
917
+ return -1;
918
+ }
919
+ }
920
+ function isValidLayerConnection(fromLayer, toLayer) {
921
+ if (fromLayer === toLayer) {
922
+ return true;
923
+ }
924
+ if (fromLayer === "ontological" && toLayer === "ontological") {
925
+ return true;
926
+ }
927
+ if (fromLayer === "ontological" && (toLayer === "L3" || toLayer === "L2" || toLayer === "L1")) {
928
+ return true;
929
+ }
930
+ if ((fromLayer === "L3" || fromLayer === "L2" || fromLayer === "L1") && toLayer === "ontological") {
931
+ return true;
932
+ }
933
+ if (fromLayer === "L2" && toLayer === "L1") {
934
+ return true;
935
+ }
936
+ if (fromLayer === "L2" && toLayer === "L3") {
937
+ return true;
938
+ }
939
+ if (fromLayer === "L3" && toLayer === "L2") {
940
+ return true;
941
+ }
942
+ if (fromLayer === "L3" && toLayer === "L4") {
943
+ return true;
944
+ }
945
+ if (fromLayer === "L4" && toLayer === "L3") {
946
+ return true;
947
+ }
948
+ if (fromLayer === "L3" && toLayer === "organizational") {
949
+ return true;
950
+ }
951
+ if (fromLayer === "organizational" && toLayer === "L3") {
952
+ return true;
953
+ }
954
+ const fromDepth = getLayerDepth(fromLayer);
955
+ const toDepth = getLayerDepth(toLayer);
956
+ if (Math.abs(fromDepth - toDepth) > 1) {
957
+ return false;
958
+ }
959
+ return true;
960
+ }
961
+ function shouldContinueTraversal(currentLayer, targetLayer, options) {
962
+ const currentDepth = getLayerDepth(currentLayer);
963
+ const targetDepth = getLayerDepth(targetLayer);
964
+ if (options.minLayer !== void 0 && targetDepth < options.minLayer) {
965
+ return false;
966
+ }
967
+ if (options.maxLayer !== void 0 && targetDepth > options.maxLayer) {
968
+ return false;
969
+ }
970
+ switch (options.mode) {
971
+ case "anchor_down":
972
+ return targetDepth <= currentDepth;
973
+ case "anchor_up":
974
+ return targetDepth >= currentDepth;
975
+ case "same_layer":
976
+ return targetDepth === currentDepth;
977
+ case "decision_trace":
978
+ return currentDepth === 4 ? targetDepth === 3 : targetDepth === currentDepth;
979
+ default:
980
+ return true;
981
+ }
982
+ }
983
+ function getDefaultMinLayer(mode) {
984
+ switch (mode) {
985
+ case "anchor_down":
986
+ return 1;
987
+ case "anchor_up":
988
+ return 2;
989
+ case "same_layer":
990
+ return 1;
991
+ case "decision_trace":
992
+ return 3;
993
+ default:
994
+ return 1;
995
+ }
996
+ }
997
+ var EDGE_LAYER_RULES = {
998
+ // === 6 Canonical Epistemic Types ===
999
+ supports: {
1000
+ from: ["L3"],
1001
+ to: ["L3"],
1002
+ description: "Belief bears on Belief (weight: +1 supports, -1 contradicts)"
1003
+ },
1004
+ informs: {
1005
+ from: ["L2"],
1006
+ to: ["L3"],
1007
+ description: "Evidence -> Belief (L2 -> L3)"
1008
+ },
1009
+ depends_on: {
1010
+ from: ["L3"],
1011
+ to: ["L3"],
1012
+ description: "Belief B requires Belief A (structural gate)"
1013
+ },
1014
+ derived_from: {
1015
+ from: ["L2", "L3", "L4"],
1016
+ to: ["L1", "L2", "L3"],
1017
+ description: "A was produced from B (provenance chain)"
1018
+ },
1019
+ contains: {
1020
+ from: ["L3", "L4", "ontological"],
1021
+ to: ["L2", "L3", "ontological"],
1022
+ description: "A scopes/aggregates B (hierarchy)"
1023
+ },
1024
+ tests: {
1025
+ from: ["L3"],
1026
+ to: ["L3"],
1027
+ description: "Question -> Belief (L3 -> L3)"
1028
+ },
1029
+ // === Structural / Lifecycle ===
1030
+ supersedes: {
1031
+ from: ["L3"],
1032
+ to: ["L3"],
1033
+ description: "NewNode -> OldNode (fork lineage)"
1034
+ },
1035
+ responds_to: {
1036
+ from: ["L2", "L3"],
1037
+ to: ["L3"],
1038
+ description: "Answer -> Question (L2/L3 -> L3)"
1039
+ },
1040
+ belongs_to: {
1041
+ from: ["L2", "L3", "ontological"],
1042
+ to: ["L3"],
1043
+ description: "Membership (migrating to contains)"
1044
+ },
1045
+ relates_to_thesis: {
1046
+ from: ["L3"],
1047
+ to: ["L3"],
1048
+ description: "Belief -> Theme (L3 -> L3)"
1049
+ },
1050
+ // === Ontological (entity-entity or entity-epistemic bridge) ===
1051
+ works_at: {
1052
+ from: ["ontological"],
1053
+ to: ["ontological"],
1054
+ description: "Person -> Company"
1055
+ },
1056
+ invested_in: {
1057
+ from: ["ontological"],
1058
+ to: ["ontological"],
1059
+ description: "Investor -> Company"
1060
+ },
1061
+ competes_with: {
1062
+ from: ["ontological"],
1063
+ to: ["ontological"],
1064
+ description: "Company -> Company"
1065
+ },
1066
+ participates_in: {
1067
+ from: ["ontological"],
1068
+ to: ["ontological"],
1069
+ description: "Company -> ValueChain"
1070
+ },
1071
+ founded_by: {
1072
+ from: ["ontological"],
1073
+ to: ["ontological"],
1074
+ description: "Company -> Person"
1075
+ },
1076
+ evaluates: {
1077
+ from: ["ontological"],
1078
+ to: ["ontological"],
1079
+ description: "Deal -> Company"
1080
+ },
1081
+ performs: {
1082
+ from: ["ontological"],
1083
+ to: ["ontological"],
1084
+ description: "Company -> Function"
1085
+ },
1086
+ function_in: {
1087
+ from: ["ontological"],
1088
+ to: ["ontological"],
1089
+ description: "Function -> ValueChain"
1090
+ },
1091
+ impacts: {
1092
+ from: ["ontological", "L3"],
1093
+ to: ["ontological"],
1094
+ description: "Theme/Entity -> ValueChain"
1095
+ },
1096
+ raised_from: {
1097
+ from: ["ontological"],
1098
+ to: ["ontological"],
1099
+ description: "Company -> Investor"
1100
+ },
1101
+ mentioned_in: {
1102
+ from: ["ontological"],
1103
+ to: ["L1", "L2"],
1104
+ description: "Entity -> Source/Evidence"
1105
+ },
1106
+ perspective_on: {
1107
+ from: ["ontological"],
1108
+ to: ["L3"],
1109
+ description: "Person -> Belief/Theme"
1110
+ },
1111
+ plays_theme: {
1112
+ from: ["ontological"],
1113
+ to: ["L3"],
1114
+ description: "Deal -> Theme"
1115
+ }
1116
+ };
1117
+ function validateEdgeLayers(edgeType, fromLayer, toLayer) {
1118
+ const rules = EDGE_LAYER_RULES[edgeType];
1119
+ if (!rules) {
1120
+ console.warn(
1121
+ `[EdgeValidation] Unknown edge type: ${edgeType}, allowing by default`
1122
+ );
1123
+ return { valid: true };
1124
+ }
1125
+ if (edgeType === "supersedes") {
1126
+ if (fromLayer !== toLayer) {
1127
+ return {
1128
+ valid: false,
1129
+ reason: `${edgeType} edges must be between nodes of the same layer. Got ${fromLayer} -> ${toLayer}`
1130
+ };
1131
+ }
1132
+ return { valid: true };
1133
+ }
1134
+ if (!rules.from.includes(fromLayer)) {
1135
+ return {
1136
+ valid: false,
1137
+ reason: `Edge type '${edgeType}' does not allow source layer ${fromLayer}. Allowed: ${rules.from.join(", ")}`
1138
+ };
1139
+ }
1140
+ if (!rules.to.includes(toLayer)) {
1141
+ return {
1142
+ valid: false,
1143
+ reason: `Edge type '${edgeType}' does not allow target layer ${toLayer}. Allowed: ${rules.to.join(", ")}`
1144
+ };
1145
+ }
1146
+ if (!isValidLayerConnection(fromLayer, toLayer)) {
1147
+ return {
1148
+ valid: false,
1149
+ reason: `Layer transition ${fromLayer} -> ${toLayer} is not permitted`
1150
+ };
1151
+ }
1152
+ return { valid: true };
1153
+ }
1154
+
1155
+ // src/topicProjectOverlay.ts
1156
+ var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
1157
+ function readNonEmptyString2(value) {
1158
+ if (typeof value !== "string") {
1159
+ return;
1160
+ }
1161
+ const normalized = value.trim();
1162
+ return normalized.length > 0 ? normalized : void 0;
1163
+ }
1164
+ function readStringArray2(value) {
1165
+ if (!Array.isArray(value)) {
1166
+ return [];
1167
+ }
1168
+ return value.map((entry) => readNonEmptyString2(entry)).filter((entry) => Boolean(entry));
1169
+ }
1170
+ function readMetadata2(topic) {
1171
+ return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
1172
+ }
1173
+ function readLegacyProjectId2(value) {
1174
+ if (!value) {
1175
+ return;
1176
+ }
1177
+ return readNonEmptyString2(value[LEGACY_SCOPE_FIELD2]);
1178
+ }
1179
+ function coerceVisibility2(value) {
1180
+ return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
1181
+ }
1182
+ function coerceStatus2(value) {
1183
+ return value === "active" || value === "archived" || value === "watching" ? value : void 0;
1184
+ }
1185
+ function mapProjectType2(topic, metadata) {
1186
+ const explicit = readNonEmptyString2(metadata.projectType);
1187
+ if (explicit) {
1188
+ return explicit;
1189
+ }
1190
+ if (topic.type === "theme") {
1191
+ return "thematic";
1192
+ }
1193
+ return readNonEmptyString2(topic.type) || "general";
1194
+ }
1195
+ function isProjectLikeTopic2(topic) {
1196
+ const metadata = readMetadata2(topic);
1197
+ return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId2(topic) !== void 0 || readNonEmptyString2(metadata.projectType) !== void 0;
1198
+ }
1199
+ function isMissingLucernChildComponentError(error) {
1200
+ const message = error instanceof Error ? error.message : String(error);
1201
+ return message.includes(
1202
+ 'Child component ComponentName(Identifier("lucern")) not found'
1203
+ ) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1204
+ }
1205
+ async function resolveTopicDoc2(ctx, scopeId) {
1206
+ if (ctx?.db && typeof ctx.db.get === "function") {
1207
+ try {
1208
+ const directTopic = await ctx.db.get(scopeId);
1209
+ if (directTopic) {
1210
+ return directTopic;
1211
+ }
1212
+ } catch {
1213
+ }
1214
+ }
1215
+ if (typeof ctx.runQuery !== "function") {
1216
+ return null;
1217
+ }
1218
+ try {
1219
+ const topic = await ctx.runQuery(api2.topics.get, {
1220
+ id: String(scopeId)
1221
+ });
1222
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1223
+ return topic;
1224
+ }
1225
+ } catch {
1226
+ }
1227
+ try {
1228
+ const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1229
+ projectId: String(scopeId)
1230
+ });
1231
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1232
+ return topic;
1233
+ }
1234
+ } catch {
1235
+ }
1236
+ return null;
1237
+ }
1238
+ function materializeTopicProjectOverlay2(topic, idMode = "legacy") {
1239
+ const metadata = readMetadata2(topic);
1240
+ const topicId = String(topic._id);
1241
+ const legacyProjectId = readLegacyProjectId2(topic) || readLegacyProjectId2(metadata) || readNonEmptyString2(metadata.legacyProjectId);
1242
+ const storageProjectId = legacyProjectId || topicId;
1243
+ const outwardId = idMode === "topic" ? topicId : storageProjectId;
1244
+ const visibility = coerceVisibility2(topic.visibility) || coerceVisibility2(metadata.visibility) || "private";
1245
+ const status = coerceStatus2(topic.status) || coerceStatus2(metadata.status) || "active";
1246
+ const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
1247
+ const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
1248
+ return {
1249
+ ...metadata,
1250
+ _id: outwardId,
1251
+ projectId: outwardId,
1252
+ topicId,
1253
+ storageProjectId,
1254
+ legacyProjectId,
1255
+ name: readNonEmptyString2(topic.name) || "Untitled Theme",
1256
+ type: mapProjectType2(topic, metadata),
1257
+ description: readNonEmptyString2(topic.description),
1258
+ ownerId: readNonEmptyString2(metadata.ownerId) || readNonEmptyString2(topic.createdBy) || "system",
1259
+ sharedWith: readStringArray2(metadata.sharedWith),
1260
+ visibility,
1261
+ tenantId: readNonEmptyString2(topic.tenantId) || readNonEmptyString2(metadata.tenantId),
1262
+ workspaceId: readNonEmptyString2(topic.workspaceId) || readNonEmptyString2(metadata.workspaceId),
1263
+ status,
1264
+ tags: readStringArray2(metadata.tags),
1265
+ chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
1266
+ artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
1267
+ lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
1268
+ _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
1269
+ createdAt,
1270
+ updatedAt
1271
+ };
1272
+ }
1273
+ async function resolveTopicProjectOverlay2(ctx, scopeId, options = {}) {
1274
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1275
+ if (!topic) {
1276
+ return null;
1277
+ }
1278
+ if (options.projectLikeOnly !== false && !isProjectLikeTopic2(topic)) {
1279
+ return null;
1280
+ }
1281
+ return materializeTopicProjectOverlay2(topic, options.idMode);
1282
+ }
1283
+ async function listTopicProjectOverlays2(ctx, options = {}) {
1284
+ let allTopics = [];
1285
+ if (ctx?.db?.query && typeof ctx.db.query === "function") {
1286
+ try {
1287
+ allTopics = await ctx.db.query("topics").collect();
1288
+ } catch {
1289
+ allTopics = [];
1290
+ }
1291
+ }
1292
+ if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
1293
+ allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
1294
+ }
1295
+ return allTopics.filter(
1296
+ (topic) => options.projectLikeOnly === false || isProjectLikeTopic2(topic)
1297
+ ).map((topic) => materializeTopicProjectOverlay2(topic, options.idMode));
1298
+ }
1299
+ async function patchTopicProjectOverlay(ctx, scopeId, value) {
1300
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1301
+ if (!topic) {
1302
+ return null;
1303
+ }
1304
+ const nextMetadata = { ...readMetadata2(topic) };
1305
+ const patch = {};
1306
+ const topicUpdateArgs = {
1307
+ id: String(topic._id)
1308
+ };
1309
+ for (const [key, rawValue] of Object.entries(value)) {
1310
+ switch (key) {
1311
+ case "_id":
1312
+ case "projectId":
1313
+ case "topicId":
1314
+ case "legacyProjectId":
1315
+ case "storageProjectId":
1316
+ break;
1317
+ case "name":
1318
+ case "description":
1319
+ patch[key] = rawValue;
1320
+ topicUpdateArgs[key] = rawValue;
1321
+ break;
1322
+ case "tenantId":
1323
+ case "workspaceId":
1324
+ case "ownerId":
1325
+ throw new Error(
1326
+ `patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
1327
+ );
1328
+ case "status": {
1329
+ const status = coerceStatus2(rawValue);
1330
+ if (status) {
1331
+ patch.status = status;
1332
+ topicUpdateArgs.status = status;
1333
+ }
1334
+ break;
1335
+ }
1336
+ case "visibility": {
1337
+ const visibility = coerceVisibility2(rawValue);
1338
+ if (visibility) {
1339
+ patch.visibility = visibility;
1340
+ topicUpdateArgs.visibility = visibility;
1341
+ }
1342
+ break;
1343
+ }
1344
+ case "type": {
1345
+ const projectType = readNonEmptyString2(rawValue);
1346
+ if (projectType) {
1347
+ nextMetadata.projectType = projectType;
1348
+ } else {
1349
+ delete nextMetadata.projectType;
1350
+ }
1351
+ break;
1352
+ }
1353
+ case "updatedAt":
1354
+ case "createdAt":
1355
+ break;
1356
+ default:
1357
+ if (rawValue === void 0) {
1358
+ delete nextMetadata[key];
1359
+ } else {
1360
+ nextMetadata[key] = rawValue;
1361
+ }
1362
+ }
1363
+ }
1364
+ patch.updatedAt = Date.now();
1365
+ patch.metadata = nextMetadata;
1366
+ topicUpdateArgs.metadata = nextMetadata;
1367
+ if (typeof ctx.runMutation === "function") {
1368
+ try {
1369
+ await ctx.runMutation(api2.topics.update, topicUpdateArgs);
1370
+ } catch (error) {
1371
+ if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
1372
+ throw error;
1373
+ }
1374
+ await ctx.db.patch(String(topic._id), patch);
1375
+ }
1376
+ } else if (ctx?.db && typeof ctx.db.patch === "function") {
1377
+ await ctx.db.patch(String(topic._id), patch);
1378
+ } else {
1379
+ throw new Error(
1380
+ "Cannot patch topic without component adapter (ctx.runMutation unavailable)"
1381
+ );
1382
+ }
1383
+ return materializeTopicProjectOverlay2(
1384
+ {
1385
+ ...topic,
1386
+ ...patch,
1387
+ metadata: nextMetadata
1388
+ }
1389
+ );
1390
+ }
1391
+
1392
+ // src/resolvers.ts
1393
+ function isMissingLucernChildComponentError2(error) {
1394
+ const message = error instanceof Error ? error.message : String(error);
1395
+ return message.includes('Child component ComponentName(Identifier("lucern")) not found') || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1396
+ }
1397
+ function isAdvisoryTopicPatch(value) {
1398
+ const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
1399
+ const keys = Object.keys(value);
1400
+ return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
1401
+ }
1402
+ async function patchProjectWithTolerance(ctx, projectId, value) {
1403
+ try {
1404
+ await patchTopicProjectOverlay(ctx, projectId, value);
1405
+ } catch (error) {
1406
+ if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
1407
+ throw error;
1408
+ }
1409
+ console.warn("[lucern graph-primitives] Non-fatal advisory topic patch failure", {
1410
+ projectId,
1411
+ keys: Object.keys(value),
1412
+ error: error instanceof Error ? error.message : error
1413
+ });
1414
+ }
1415
+ }
1416
+ function defaultResolvers2() {
1417
+ return {
1418
+ async getProject(ctx, projectId) {
1419
+ return await resolveTopicProjectOverlay2(ctx, projectId, {
1420
+ idMode: "legacy",
1421
+ projectLikeOnly: false
1422
+ });
1423
+ },
1424
+ async patchProject(ctx, projectId, value) {
1425
+ await patchProjectWithTolerance(ctx, projectId, value);
1426
+ },
1427
+ async listTopics(ctx) {
1428
+ return await listTopicProjectOverlays2(ctx, {
1429
+ idMode: "legacy"
1430
+ });
1431
+ },
1432
+ async getFinalArtifact(ctx, artifactId) {
1433
+ return await ctx.db.get(artifactId);
1434
+ }
1435
+ };
1436
+ }
1437
+ var resolverOverrides2 = {};
1438
+ function resolveGraphPrimitivesAppResolvers(_ctx) {
1439
+ return {
1440
+ ...defaultResolvers2(),
1441
+ ...resolverOverrides2
1442
+ };
1443
+ }
1444
+ var LEGACY_SCOPE_FIELD3 = "graphScopeProjectId";
1445
+ function asMappedProjectId(topic) {
1446
+ if (!topic) {
1447
+ return;
1448
+ }
1449
+ const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD3]);
1450
+ if (directLegacyProjectId) {
1451
+ return directLegacyProjectId;
1452
+ }
1453
+ const metadata = topic.metadata || {};
1454
+ const candidate = metadata[LEGACY_SCOPE_FIELD3] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
1455
+ return candidate ? candidate : void 0;
1456
+ }
1457
+ function normalizeScopeValue(value) {
1458
+ if (typeof value !== "string") {
1459
+ return;
1460
+ }
1461
+ const normalized = value.trim();
1462
+ return normalized.length > 0 ? normalized : void 0;
1463
+ }
1464
+ function pickPrimaryTopic(candidates) {
1465
+ return [...candidates].sort((a, b) => {
1466
+ const depthA = a.depth ?? 9999;
1467
+ const depthB = b.depth ?? 9999;
1468
+ if (depthA !== depthB) {
1469
+ return depthA - depthB;
1470
+ }
1471
+ const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
1472
+ const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
1473
+ if (createdA !== createdB) {
1474
+ return createdA - createdB;
1475
+ }
1476
+ return String(a.name || "").localeCompare(String(b.name || ""));
1477
+ })[0];
1478
+ }
1479
+ async function findTopicsByScopeAlias(ctx, scopeId) {
1480
+ try {
1481
+ return await ctx.db.query("topics").withIndex(
1482
+ "by_graph_scope_project",
1483
+ (q) => q.eq(LEGACY_SCOPE_FIELD3, scopeId)
1484
+ ).collect();
1485
+ } catch {
1486
+ const topics = await ctx.db.query("topics").collect();
1487
+ return topics.filter((topic) => {
1488
+ const normalizedGlobalId = normalizeScopeValue(topic.globalId);
1489
+ const mappedProjectId = asMappedProjectId(topic);
1490
+ return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
1491
+ });
1492
+ }
1493
+ }
1494
+ async function tryResolveHostTopicById(ctx, topicId) {
1495
+ if (typeof ctx.runQuery !== "function") {
1496
+ return null;
1497
+ }
1498
+ try {
1499
+ return await ctx.runQuery(api2.topics.get, {
1500
+ id: topicId
1501
+ }) ?? null;
1502
+ } catch {
1503
+ return null;
1504
+ }
1505
+ }
1506
+ async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
1507
+ if (typeof ctx.runQuery !== "function") {
1508
+ return null;
1509
+ }
1510
+ try {
1511
+ return await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1512
+ projectId: legacyScopeId
1513
+ }) ?? null;
1514
+ } catch {
1515
+ return null;
1516
+ }
1517
+ }
1518
+ async function resolveInheritedWorkspaceScope(ctx, topic) {
1519
+ const MAX_DEPTH = 10;
1520
+ let tenantId = normalizeScopeValue(topic.tenantId);
1521
+ let workspaceId = normalizeScopeValue(topic.workspaceId);
1522
+ if (tenantId && workspaceId) {
1523
+ return { tenantId, workspaceId };
1524
+ }
1525
+ let current = topic;
1526
+ for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
1527
+ current = await ctx.db.get(current.parentTopicId);
1528
+ if (!current) break;
1529
+ if (!tenantId) {
1530
+ tenantId = normalizeScopeValue(current.tenantId);
1531
+ }
1532
+ if (!workspaceId) {
1533
+ workspaceId = normalizeScopeValue(current.workspaceId);
1534
+ }
1535
+ if (tenantId && workspaceId) break;
1536
+ }
1537
+ return { tenantId, workspaceId };
1538
+ }
1539
+ async function resolveTopicProjectScope(ctx, args) {
1540
+ if (args.topicId) {
1541
+ let topic = null;
1542
+ try {
1543
+ topic = await ctx.db.get(args.topicId);
1544
+ } catch {
1545
+ }
1546
+ if (!topic) {
1547
+ topic = await tryResolveHostTopicById(ctx, String(args.topicId));
1548
+ }
1549
+ if (!topic) {
1550
+ topic = pickPrimaryTopic(
1551
+ await findTopicsByScopeAlias(ctx, String(args.topicId))
1552
+ ) ?? null;
1553
+ }
1554
+ if (!topic) {
1555
+ throw new Error(`Topic not found: ${String(args.topicId)}`);
1556
+ }
1557
+ const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
1558
+ const mapped = asMappedProjectId(topic);
1559
+ if (mapped) {
1560
+ return {
1561
+ topicId: topic._id,
1562
+ projectId: mapped,
1563
+ tenantId: inherited.tenantId,
1564
+ workspaceId: inherited.workspaceId,
1565
+ source: "topic"
1566
+ };
1567
+ }
1568
+ return {
1569
+ topicId: topic._id,
1570
+ tenantId: inherited.tenantId,
1571
+ workspaceId: inherited.workspaceId,
1572
+ source: "topic"
1573
+ };
1574
+ }
1575
+ if (args.projectId) {
1576
+ let directTopic = null;
1577
+ try {
1578
+ directTopic = await ctx.db.get(
1579
+ args.projectId
1580
+ );
1581
+ } catch {
1582
+ }
1583
+ if (directTopic) {
1584
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1585
+ const mapped = asMappedProjectId(directTopic);
1586
+ return {
1587
+ topicId: directTopic._id,
1588
+ projectId: mapped ?? args.projectId,
1589
+ tenantId: inherited.tenantId,
1590
+ workspaceId: inherited.workspaceId,
1591
+ source: "topic_inferred"
1592
+ };
1593
+ }
1594
+ directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
1595
+ if (directTopic) {
1596
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1597
+ const mapped = asMappedProjectId(directTopic);
1598
+ return {
1599
+ topicId: directTopic._id,
1600
+ projectId: mapped ?? args.projectId,
1601
+ tenantId: inherited.tenantId,
1602
+ workspaceId: inherited.workspaceId,
1603
+ source: "topic_inferred"
1604
+ };
1605
+ }
1606
+ const topics = await findTopicsByScopeAlias(ctx, args.projectId);
1607
+ const primary = pickPrimaryTopic(topics);
1608
+ if (primary) {
1609
+ const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
1610
+ return {
1611
+ topicId: primary._id,
1612
+ projectId: args.projectId,
1613
+ tenantId: inherited.tenantId,
1614
+ workspaceId: inherited.workspaceId,
1615
+ source: "project_mapped_topic"
1616
+ };
1617
+ }
1618
+ throw new Error(
1619
+ `Legacy project scope ${String(args.projectId)} has no mapped topic.`
1620
+ );
1621
+ }
1622
+ throw new Error(
1623
+ "Missing scope: provide topicId (preferred) or legacy projectId alias."
1624
+ );
1625
+ }
1626
+ var optionalScopeArgs = {
1627
+ projectId: v.optional(v.string()),
1628
+ topicId: v.optional(v.string())
1629
+ };
1630
+
1631
+ // src/epistemicEdges.ts
1632
+ var epistemicLayerValidator = v.union(
1633
+ v.literal("L4"),
1634
+ v.literal("L3"),
1635
+ v.literal("L2"),
1636
+ v.literal("L1"),
1637
+ v.literal("ontological"),
1638
+ v.literal("organizational")
1639
+ );
1640
+ var subjectiveOpinionValidator = v.object({
1641
+ b: v.number(),
1642
+ d: v.number(),
1643
+ u: v.number(),
1644
+ a: v.number()
1645
+ });
1646
+ var edgeTypeValidator = v.union(
1647
+ // --- L4 Decision Edges (Phase 2A) ---
1648
+ v.literal("based_on_belief"),
1649
+ // Decision → Belief (L4 → L3)
1650
+ v.literal("based_on_question"),
1651
+ // Decision → Question (L4 → L3)
1652
+ v.literal("blocked_by_contradiction"),
1653
+ // Decision → Contradiction (L4 → L3)
1654
+ v.literal("informed_by_theme"),
1655
+ // Decision → Theme (L4 → L3)
1656
+ // --- Evidence Flow (L2 → L3, L2 → L1) ---
1657
+ v.literal("derived_from"),
1658
+ // Evidence/Synthesis → Source/Question (provenance)
1659
+ v.literal("responds_to"),
1660
+ // Answer → Question (L2 → L3)
1661
+ v.literal("informs"),
1662
+ // Evidence → Belief (L2 → L3) - use weight: -1 to +1
1663
+ v.literal("tests"),
1664
+ // Question → Belief (L3 → L3)
1665
+ v.literal("explores"),
1666
+ // Question → Belief assumption (L3 → L3)
1667
+ v.literal("qualifies"),
1668
+ // Evidence → Belief (L2 → L3)
1669
+ // --- Synthesis (L2 → L2, L2 → L1) ---
1670
+ // "based_on" removed — use "derived_from" instead
1671
+ // --- Theme Relationships (L3 → L3) ---
1672
+ v.literal("relates_to_thesis"),
1673
+ // Belief → Theme (L3 → L3) - use weight: -1 to +1
1674
+ v.literal("belongs_to"),
1675
+ // Any → Theme (? → L3)
1676
+ v.literal("plays_theme"),
1677
+ // Deal → Theme (L3 → L3)
1678
+ v.literal("scoped_by"),
1679
+ // Belief/Question → Topic (L3 → organizational)
1680
+ // --- Deal/Company ---
1681
+ v.literal("evaluates"),
1682
+ // Deal → Company (L3 → ontological)
1683
+ // --- People (ontological → ontological, ontological → L3) ---
1684
+ v.literal("perspective_on"),
1685
+ // Person → Belief/Theme
1686
+ v.literal("works_at"),
1687
+ // Person → Company/Investor
1688
+ v.literal("mentioned_in"),
1689
+ // Person/Company → Source (ontological → L1)
1690
+ v.literal("founded_by"),
1691
+ // Company → Person (ontological → ontological)
1692
+ // --- Value Chain (ontological → ontological) ---
1693
+ v.literal("participates_in"),
1694
+ // Company → ValueChain
1695
+ v.literal("performs"),
1696
+ // Company → Function
1697
+ v.literal("function_in"),
1698
+ // Function → ValueChain
1699
+ v.literal("impacts"),
1700
+ // Theme → ValueChain (L3 → ontological)
1701
+ // --- Investment (ontological → ontological) ---
1702
+ v.literal("invested_in"),
1703
+ // Investor → Company
1704
+ v.literal("raised_from"),
1705
+ // Company → Investor
1706
+ // --- Lifecycle (same layer only) ---
1707
+ v.literal("supersedes"),
1708
+ // NewNode → OldNode (same layer)
1709
+ v.literal("same_as"),
1710
+ // Duplicate detection (same layer)
1711
+ // --- Same-Type Relationships: Belief ↔ Belief (L3 → L3) ---
1712
+ v.literal("depends_on"),
1713
+ // Belief B requires Belief A to be true
1714
+ v.literal("supports"),
1715
+ // Beliefs strengthen each other (weight > 0)
1716
+ v.literal("contains"),
1717
+ // Parent contains child (hierarchical containment)
1718
+ // --- Belief Cluster Mapping (L3 → L3) - For Thesis Validation Sprints ---
1719
+ // These edge types capture the full range of belief-to-belief relationships
1720
+ // needed for cluster mapping, confidence propagation, and thesis testing.
1721
+ v.literal("counterfactual_of"),
1722
+ // If A is TRUE, B FAILS (inverse dependency)
1723
+ v.literal("cascade_to"),
1724
+ // If B fails, A also fails (downstream impact)
1725
+ v.literal("cascade_from"),
1726
+ // Changes propagate from A to B (upstream dependency)
1727
+ v.literal("mutually_exclusive"),
1728
+ // A and B cannot BOTH be true (zero-sum)
1729
+ v.literal("correlates_with"),
1730
+ // A and B move together non-causally (observed pattern)
1731
+ v.literal("amplifies"),
1732
+ // A strengthens B unidirectionally (one-way boost)
1733
+ v.literal("precondition_for"),
1734
+ // A must validate before B can be tested (temporal)
1735
+ v.literal("in_tension_with"),
1736
+ // A and B pull opposite directions (soft conflict)
1737
+ // --- Belief ↔ Belief: Epistemic Impact (Confidence Propagation) ---
1738
+ v.literal("falsified_by"),
1739
+ // If B is validated, A becomes false
1740
+ v.literal("exclusive_with"),
1741
+ // A XOR B (sum of confidences ≤ 1)
1742
+ v.literal("contradicts"),
1743
+ // A directly contradicts B
1744
+ v.literal("collapses_if"),
1745
+ // If A falls below threshold, B collapses
1746
+ v.literal("strengthened_by"),
1747
+ // If A rises, B rises
1748
+ v.literal("weakened_by"),
1749
+ // If A rises, B falls
1750
+ v.literal("alternative_to"),
1751
+ // Competing explanations
1752
+ v.literal("subsumes"),
1753
+ // A is more general, includes B
1754
+ v.literal("validated_by"),
1755
+ // If B is true, A gains confidence
1756
+ v.literal("required_for"),
1757
+ // A must be true for B to be meaningful
1758
+ v.literal("blocks"),
1759
+ // A being unresolved blocks B
1760
+ // --- Same-Type Relationships: Question ↔ Question (L3 → L3) ---
1761
+ v.literal("prerequisite_for"),
1762
+ // Question A must be answered first
1763
+ v.literal("parallel_to"),
1764
+ // Same topic, different angles
1765
+ // --- Same-Type Relationships: Evidence ↔ Evidence (L2 → L2) ---
1766
+ v.literal("corroborates"),
1767
+ // Independent support for same conclusion
1768
+ v.literal("extends"),
1769
+ // Adds depth to other evidence
1770
+ v.literal("same_source_as"),
1771
+ // Same document/study
1772
+ v.literal("same_theme_as"),
1773
+ // Same topic/entity
1774
+ // --- NEW: Deep Epistemic Analysis Edges (Phase: Schema Upgrade) ---
1775
+ v.literal("assumes"),
1776
+ // Hidden dependency discovered by AI (Belief B implicitly assumes A)
1777
+ v.literal("would_predict"),
1778
+ // Pre-registered prediction for validation (If Belief true, expect Evidence)
1779
+ v.literal("analogous_to"),
1780
+ // Explicit analogy with disanalogies (Belief A like Belief B because...)
1781
+ v.literal("independent_of"),
1782
+ // True evidence independence marker (Evidence A independent of B)
1783
+ // --- Entity↔Belief Bridge (OE-B) ---
1784
+ // "about_entity" and "entity_referenced_in" removed — use "contains" instead
1785
+ v.literal("competes_with")
1786
+ // Company → Company (ontological → ontological)
1787
+ );
1788
+ var get = query({
1789
+ args: { edgeId: v.id("epistemicEdges") },
1790
+ returns: permissiveReturn,
1791
+ handler: async (ctx, args) => {
1792
+ return await ctx.db.get(args.edgeId);
1793
+ }
1794
+ });
1795
+ var getByGlobalId = query({
1796
+ args: { globalId: v.string() },
1797
+ returns: permissiveReturn,
1798
+ handler: async (ctx, args) => {
1799
+ return await ctx.db.query("epistemicEdges").withIndex("by_globalId", (q) => q.eq("globalId", args.globalId)).first();
1800
+ }
1801
+ });
1802
+ var getFromNode = query({
1803
+ args: {
1804
+ fromNodeId: v.id("epistemicNodes"),
1805
+ edgeType: v.optional(edgeTypeValidator)
1806
+ },
1807
+ returns: permissiveReturn,
1808
+ handler: async (ctx, args) => {
1809
+ const { edgeType } = args;
1810
+ if (edgeType) {
1811
+ return await ctx.db.query("epistemicEdges").withIndex(
1812
+ "by_from_type",
1813
+ (q) => q.eq("fromNodeId", args.fromNodeId).eq("edgeType", edgeType)
1814
+ ).collect();
1815
+ }
1816
+ return await ctx.db.query("epistemicEdges").withIndex("by_from", (q) => q.eq("fromNodeId", args.fromNodeId)).collect();
1817
+ }
1818
+ });
1819
+ var getToNode = query({
1820
+ args: {
1821
+ toNodeId: v.id("epistemicNodes"),
1822
+ edgeType: v.optional(edgeTypeValidator)
1823
+ },
1824
+ returns: permissiveReturn,
1825
+ handler: async (ctx, args) => {
1826
+ const { edgeType } = args;
1827
+ if (edgeType) {
1828
+ return await ctx.db.query("epistemicEdges").withIndex(
1829
+ "by_to_type",
1830
+ (q) => q.eq("toNodeId", args.toNodeId).eq("edgeType", edgeType)
1831
+ ).collect();
1832
+ }
1833
+ return await ctx.db.query("epistemicEdges").withIndex("by_to", (q) => q.eq("toNodeId", args.toNodeId)).collect();
1834
+ }
1835
+ });
1836
+ var getBySourceNode = query({
1837
+ args: {
1838
+ sourceNodeId: v.string(),
1839
+ edgeType: v.optional(v.string())
1840
+ },
1841
+ returns: permissiveReturn,
1842
+ handler: async (ctx, args) => {
1843
+ const isConvexId = args.sourceNodeId.length === 32 || args.sourceNodeId.includes("epistemicNodes");
1844
+ if (isConvexId) {
1845
+ const query2 = args.edgeType ? ctx.db.query("epistemicEdges").withIndex(
1846
+ "by_from_type",
1847
+ (q) => q.eq("fromNodeId", args.sourceNodeId).eq("edgeType", args.edgeType)
1848
+ ) : ctx.db.query("epistemicEdges").withIndex(
1849
+ "by_from",
1850
+ (q) => q.eq("fromNodeId", args.sourceNodeId)
1851
+ );
1852
+ return await query2.collect();
1853
+ }
1854
+ const byGlobalId = await ctx.db.query("epistemicEdges").withIndex(
1855
+ "by_source_global_id",
1856
+ (q) => q.eq("sourceGlobalId", args.sourceNodeId)
1857
+ ).collect();
1858
+ if (args.edgeType) {
1859
+ return byGlobalId.filter((edge) => edge.edgeType === args.edgeType);
1860
+ }
1861
+ return byGlobalId;
1862
+ }
1863
+ });
1864
+ var getByTargetNode = query({
1865
+ args: {
1866
+ targetNodeId: v.string(),
1867
+ edgeType: v.optional(v.string())
1868
+ },
1869
+ returns: permissiveReturn,
1870
+ handler: async (ctx, args) => {
1871
+ const isConvexId = args.targetNodeId.length === 32 || args.targetNodeId.includes("epistemicNodes");
1872
+ if (isConvexId) {
1873
+ const query2 = args.edgeType ? ctx.db.query("epistemicEdges").withIndex(
1874
+ "by_to_type",
1875
+ (q) => q.eq("toNodeId", args.targetNodeId).eq("edgeType", args.edgeType)
1876
+ ) : ctx.db.query("epistemicEdges").withIndex(
1877
+ "by_to",
1878
+ (q) => q.eq("toNodeId", args.targetNodeId)
1879
+ );
1880
+ return await query2.collect();
1881
+ }
1882
+ const byGlobalId = await ctx.db.query("epistemicEdges").withIndex(
1883
+ "by_target_global_id",
1884
+ (q) => q.eq("targetGlobalId", args.targetNodeId)
1885
+ ).collect();
1886
+ if (args.edgeType) {
1887
+ return byGlobalId.filter((edge) => edge.edgeType === args.edgeType);
1888
+ }
1889
+ return byGlobalId;
1890
+ }
1891
+ });
1892
+ var getBetween = query({
1893
+ args: {
1894
+ fromNodeId: v.id("epistemicNodes"),
1895
+ toNodeId: v.id("epistemicNodes"),
1896
+ edgeType: v.optional(edgeTypeValidator)
1897
+ },
1898
+ returns: permissiveReturn,
1899
+ handler: async (ctx, args) => {
1900
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
1901
+ "by_from_to",
1902
+ (q) => q.eq("fromNodeId", args.fromNodeId).eq("toNodeId", args.toNodeId)
1903
+ ).collect();
1904
+ if (args.edgeType) {
1905
+ return edges.filter((e) => e.edgeType === args.edgeType);
1906
+ }
1907
+ return edges;
1908
+ }
1909
+ });
1910
+ var getByNodes = query({
1911
+ args: {
1912
+ fromNodeId: v.id("epistemicNodes"),
1913
+ toNodeId: v.id("epistemicNodes")
1914
+ },
1915
+ returns: permissiveReturn,
1916
+ handler: async (ctx, args) => {
1917
+ return await ctx.db.query("epistemicEdges").withIndex(
1918
+ "by_from_to",
1919
+ (q) => q.eq("fromNodeId", args.fromNodeId).eq("toNodeId", args.toNodeId)
1920
+ ).first();
1921
+ }
1922
+ });
1923
+ var getByProjectAndType = query({
1924
+ args: {
1925
+ ...optionalScopeArgs,
1926
+ edgeType: edgeTypeValidator
1927
+ },
1928
+ returns: permissiveReturn,
1929
+ handler: async (ctx, args) => {
1930
+ if (!args.projectId && !args.topicId) {
1931
+ return [];
1932
+ }
1933
+ let scope;
1934
+ try {
1935
+ scope = await resolveTopicProjectScope(ctx, {
1936
+ projectId: args.projectId,
1937
+ topicId: args.topicId
1938
+ });
1939
+ } catch {
1940
+ return [];
1941
+ }
1942
+ const projectEdges = await collectScopedEdges(ctx, scope, 5e3);
1943
+ return projectEdges.filter(
1944
+ (e) => e.edgeType === args.edgeType && edgeMatchesWorkspaceReasoningScope(e, scope)
1945
+ ).slice(0, 5e3);
1946
+ }
1947
+ });
1948
+ var getByProject = query({
1949
+ args: {
1950
+ ...optionalScopeArgs,
1951
+ userId: v.optional(v.string()),
1952
+ limit: v.optional(v.number())
1953
+ },
1954
+ returns: permissiveReturn,
1955
+ handler: async (ctx, args) => {
1956
+ if (!args.projectId && !args.topicId) {
1957
+ return [];
1958
+ }
1959
+ let scope;
1960
+ try {
1961
+ scope = await resolveTopicProjectScope(ctx, {
1962
+ projectId: args.projectId,
1963
+ topicId: args.topicId
1964
+ });
1965
+ } catch {
1966
+ return [];
1967
+ }
1968
+ if (args.userId) {
1969
+ const hasAccess = await checkScopeAccess(
1970
+ ctx,
1971
+ String(scope.topicId ?? scope.projectId),
1972
+ args.userId
1973
+ );
1974
+ if (!hasAccess) {
1975
+ return [];
1976
+ }
1977
+ }
1978
+ const pageSize = Math.max(1, Math.min(Math.floor(args.limit ?? 500), 2e3));
1979
+ const edges = await collectScopedEdges(ctx, scope, Math.min(pageSize * 3, 6e3));
1980
+ return edges.filter((edge) => edgeMatchesWorkspaceReasoningScope(edge, scope)).slice(0, pageSize);
1981
+ }
1982
+ });
1983
+ var getByTopic = getByProject;
1984
+ function dedupeWorkspaceEdges(edges) {
1985
+ const seen = /* @__PURE__ */ new Set();
1986
+ const deduped = [];
1987
+ for (const edge of edges) {
1988
+ const key = String(edge._id);
1989
+ if (seen.has(key)) {
1990
+ continue;
1991
+ }
1992
+ seen.add(key);
1993
+ deduped.push(edge);
1994
+ }
1995
+ return deduped;
1996
+ }
1997
+ function edgeMatchesWorkspaceReasoningScope(edge, scope) {
1998
+ return scope.topicId !== void 0 && edge.topicId === scope.topicId || scope.projectId !== void 0 && edge.projectId === scope.projectId;
1999
+ }
2000
+ async function collectScopedEdges(ctx, scope, scanLimit) {
2001
+ const queries = [];
2002
+ if (scope.topicId) {
2003
+ queries.push(
2004
+ ctx.db.query("epistemicEdges").withIndex("by_topic", (q) => q.eq("topicId", scope.topicId)).order("desc").take(scanLimit)
2005
+ );
2006
+ }
2007
+ if (scope.projectId) {
2008
+ queries.push(
2009
+ ctx.db.query("epistemicEdges").withIndex("by_topic", (q) => q.eq("topicId", scope.projectId)).order("desc").take(scanLimit)
2010
+ );
2011
+ }
2012
+ return dedupeWorkspaceEdges((await Promise.all(queries)).flat());
2013
+ }
2014
+ var listAll = query({
2015
+ args: {
2016
+ limit: v.optional(v.number())
2017
+ },
2018
+ returns: permissiveReturn,
2019
+ handler: async (ctx, args) => {
2020
+ const pageSize = Math.max(
2021
+ 1,
2022
+ Math.min(Math.floor(args.limit ?? 5e3), 1e4)
2023
+ );
2024
+ return await ctx.db.query("epistemicEdges").order("desc").take(pageSize);
2025
+ }
2026
+ });
2027
+ var findContradictions = query({
2028
+ args: {
2029
+ nodeId: v.id("epistemicNodes")
2030
+ },
2031
+ returns: permissiveReturn,
2032
+ handler: async (ctx, args) => {
2033
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
2034
+ "by_to_type",
2035
+ (q) => q.eq("toNodeId", args.nodeId).eq("edgeType", "informs")
2036
+ ).collect();
2037
+ return edges.filter((e) => (e.weight ?? 0) < 0);
2038
+ }
2039
+ });
2040
+ var findSupport = query({
2041
+ args: {
2042
+ nodeId: v.id("epistemicNodes")
2043
+ },
2044
+ returns: permissiveReturn,
2045
+ handler: async (ctx, args) => {
2046
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
2047
+ "by_to_type",
2048
+ (q) => q.eq("toNodeId", args.nodeId).eq("edgeType", "informs")
2049
+ ).collect();
2050
+ return edges.filter((e) => (e.weight ?? 0) >= 0);
2051
+ }
2052
+ });
2053
+ var create = mutation({
2054
+ args: {
2055
+ globalId: v.string(),
2056
+ fromNodeId: v.id("epistemicNodes"),
2057
+ toNodeId: v.id("epistemicNodes"),
2058
+ edgeType: edgeTypeValidator,
2059
+ weight: v.optional(v.number()),
2060
+ confidence: v.optional(v.number()),
2061
+ context: v.optional(v.string()),
2062
+ // NOTE: 'relation' field has been removed. Use 'weight' instead.
2063
+ derivationType: v.optional(v.string()),
2064
+ // For derived_from edges
2065
+ createdBy: v.string(),
2066
+ ...optionalScopeArgs,
2067
+ // Phase 2C: Allow skipping validation for migrations
2068
+ skipLayerValidation: v.optional(v.boolean())
2069
+ },
2070
+ returns: permissiveReturn,
2071
+ handler: async (ctx, args) => {
2072
+ const fromNode = await ctx.db.get(args.fromNodeId);
2073
+ const toNode = await ctx.db.get(args.toNodeId);
2074
+ if (!fromNode || !toNode) {
2075
+ throw new Error("One or both nodes not found");
2076
+ }
2077
+ const resolvedScope = args.topicId || args.projectId ? await resolveTopicProjectScope(ctx, {
2078
+ topicId: args.topicId,
2079
+ projectId: args.projectId
2080
+ }) : void 0;
2081
+ const resolvedProjectId = resolvedScope?.projectId ?? args.projectId;
2082
+ if (resolvedProjectId) {
2083
+ await requireProjectAccess(ctx, resolvedProjectId, args.createdBy);
2084
+ }
2085
+ const fromLayer = fromNode.epistemicLayer || getNodeLayer(fromNode.nodeType);
2086
+ const toLayer = toNode.epistemicLayer || getNodeLayer(toNode.nodeType);
2087
+ if (!args.skipLayerValidation) {
2088
+ const validation = validateEdgeLayers(args.edgeType, fromLayer, toLayer);
2089
+ if (!validation.valid) {
2090
+ throw new Error(
2091
+ `[EdgeValidation] Invalid edge: ${validation.reason}. Attempted: ${args.edgeType} from ${fromNode.nodeType}(${fromLayer}) \u2192 ${toNode.nodeType}(${toLayer})`
2092
+ );
2093
+ }
2094
+ }
2095
+ if (isDeprecatedEdgeType(args.edgeType)) {
2096
+ throw new Error(
2097
+ `FORBIDDEN: Edge type '${args.edgeType}' has been removed from StackOS schema.`
2098
+ );
2099
+ }
2100
+ const existing = await ctx.db.query("epistemicEdges").withIndex(
2101
+ "by_from_to",
2102
+ (q) => q.eq("fromNodeId", args.fromNodeId).eq("toNodeId", args.toNodeId)
2103
+ ).collect();
2104
+ const duplicateEdge = existing.find((e) => e.edgeType === args.edgeType);
2105
+ if (duplicateEdge) {
2106
+ return { edgeId: duplicateEdge._id, isDuplicate: true };
2107
+ }
2108
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
2109
+ globalId: args.globalId,
2110
+ fromGlobalId: fromNode.globalId,
2111
+ toGlobalId: toNode.globalId,
2112
+ edgeType: args.edgeType,
2113
+ weight: args.weight,
2114
+ confidence: args.confidence,
2115
+ context: args.context,
2116
+ derivationType: args.derivationType,
2117
+ createdBy: args.createdBy,
2118
+ topicId: resolvedProjectId ? String(resolvedProjectId) : void 0,
2119
+ fromNodeType: fromNode.nodeType,
2120
+ toNodeType: toNode.nodeType,
2121
+ fromLayer,
2122
+ toLayer
2123
+ });
2124
+ const auditProjectId = resolvedProjectId || fromNode.projectId;
2125
+ if (auditProjectId) {
2126
+ await ctx.db.insert("epistemicAudit", {
2127
+ entityType: "edge",
2128
+ entityId: args.globalId,
2129
+ changeType: "edge_added",
2130
+ changedAt: Date.now(),
2131
+ changedBy: args.createdBy,
2132
+ isAgent: false,
2133
+ newState: {
2134
+ edgeType: args.edgeType,
2135
+ fromNodeId: String(args.fromNodeId),
2136
+ toNodeId: String(args.toNodeId),
2137
+ weight: args.weight
2138
+ },
2139
+ projectId: auditProjectId
2140
+ });
2141
+ await resolveGraphPrimitivesAppResolvers().patchProject(
2142
+ ctx,
2143
+ auditProjectId,
2144
+ {
2145
+ lastActivityAt: Date.now()
2146
+ }
2147
+ );
2148
+ }
2149
+ return { edgeGlobalId: args.globalId, isDuplicate: false };
2150
+ }
2151
+ });
2152
+ var update = mutation({
2153
+ args: {
2154
+ edgeId: v.id("epistemicEdges"),
2155
+ weight: v.optional(v.number()),
2156
+ confidence: v.optional(v.number()),
2157
+ context: v.optional(v.string()),
2158
+ // NOTE: 'relation' field has been removed. Use 'weight' instead.
2159
+ derivationType: v.optional(v.string()),
2160
+ userId: v.optional(v.string()),
2161
+ // EK-4: SL opinion metadata (Kernel v2 schema fields)
2162
+ constraint: v.optional(v.string()),
2163
+ propagation: v.optional(v.string()),
2164
+ blocking: v.optional(v.boolean()),
2165
+ implicit: v.optional(v.boolean()),
2166
+ conditionalA: v.optional(subjectiveOpinionValidator),
2167
+ conditionalNotA: v.optional(subjectiveOpinionValidator),
2168
+ containment: v.optional(v.string()),
2169
+ interrogation: v.optional(v.string()),
2170
+ reasoningMethod: v.optional(v.string()),
2171
+ analogyBasis: v.optional(v.string()),
2172
+ disanalogies: v.optional(v.array(v.string())),
2173
+ analogyStrength: v.optional(v.number()),
2174
+ causal: v.optional(v.boolean()),
2175
+ predictionFulfilled: v.optional(v.boolean()),
2176
+ predictionFulfilledAt: v.optional(v.number()),
2177
+ predictionFulfilledBy: v.optional(v.string())
2178
+ },
2179
+ returns: permissiveReturn,
2180
+ handler: async (ctx, args) => {
2181
+ const { edgeId, userId, ...updates } = args;
2182
+ const edge = await ctx.db.get(edgeId);
2183
+ if (!edge) {
2184
+ throw new Error("Edge not found");
2185
+ }
2186
+ if (edge.projectId && userId) {
2187
+ await requireProjectAccess(ctx, edge.projectId, userId);
2188
+ }
2189
+ const cleanUpdates = {};
2190
+ for (const [key, value] of Object.entries(updates)) {
2191
+ if (value !== void 0) {
2192
+ cleanUpdates[key] = value;
2193
+ }
2194
+ }
2195
+ await ctx.db.patch(edgeId, cleanUpdates);
2196
+ if (edge.projectId) {
2197
+ await ctx.db.insert("epistemicAudit", {
2198
+ entityType: "edge",
2199
+ entityId: edge.globalId,
2200
+ changeType: "updated",
2201
+ changedAt: Date.now(),
2202
+ changedBy: userId || edge.createdBy,
2203
+ isAgent: false,
2204
+ previousState: {
2205
+ weight: edge.weight,
2206
+ confidence: edge.confidence,
2207
+ context: edge.context
2208
+ },
2209
+ newState: cleanUpdates,
2210
+ projectId: edge.projectId
2211
+ });
2212
+ }
2213
+ return { success: true };
2214
+ }
2215
+ });
2216
+ var remove = mutation({
2217
+ args: {
2218
+ edgeId: v.id("epistemicEdges"),
2219
+ userId: v.optional(v.string())
2220
+ },
2221
+ returns: permissiveReturn,
2222
+ handler: async (ctx, args) => {
2223
+ const edge = await ctx.db.get(args.edgeId);
2224
+ if (!edge) {
2225
+ return { success: false, error: "Edge not found" };
2226
+ }
2227
+ if (edge.projectId && args.userId) {
2228
+ await requireProjectAccess(ctx, edge.projectId, args.userId);
2229
+ }
2230
+ if (edge.projectId) {
2231
+ await ctx.db.insert("epistemicAudit", {
2232
+ entityType: "edge",
2233
+ entityId: edge.globalId,
2234
+ changeType: "edge_removed",
2235
+ changedAt: Date.now(),
2236
+ changedBy: args.userId || edge.createdBy,
2237
+ isAgent: false,
2238
+ previousState: {
2239
+ edgeType: edge.edgeType,
2240
+ fromNodeId: String(edge.fromNodeId),
2241
+ toNodeId: edge.toNodeId ? String(edge.toNodeId) : void 0,
2242
+ weight: edge.weight
2243
+ },
2244
+ projectId: edge.projectId
2245
+ });
2246
+ }
2247
+ if (edge.projectId) {
2248
+ await resolveGraphPrimitivesAppResolvers().patchProject(
2249
+ ctx,
2250
+ edge.projectId,
2251
+ {
2252
+ lastActivityAt: Date.now()
2253
+ }
2254
+ );
2255
+ }
2256
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.deleteEdge, {
2257
+ globalId: edge.globalId
2258
+ });
2259
+ await ctx.db.delete(args.edgeId);
2260
+ return { success: true };
2261
+ }
2262
+ });
2263
+ var removeBetween = mutation({
2264
+ args: {
2265
+ fromNodeId: v.id("epistemicNodes"),
2266
+ toNodeId: v.id("epistemicNodes"),
2267
+ edgeType: v.optional(edgeTypeValidator)
2268
+ },
2269
+ returns: permissiveReturn,
2270
+ handler: async (ctx, args) => {
2271
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
2272
+ "by_from_to",
2273
+ (q) => q.eq("fromNodeId", args.fromNodeId).eq("toNodeId", args.toNodeId)
2274
+ ).collect();
2275
+ let deleted = 0;
2276
+ for (const edge of edges) {
2277
+ if (!args.edgeType || edge.edgeType === args.edgeType) {
2278
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.deleteEdge, {
2279
+ globalId: edge.globalId
2280
+ });
2281
+ await ctx.db.delete(edge._id);
2282
+ deleted++;
2283
+ }
2284
+ }
2285
+ return { deleted };
2286
+ }
2287
+ });
2288
+ var batchCreate = mutation({
2289
+ args: {
2290
+ edges: v.array(
2291
+ v.object({
2292
+ globalId: v.string(),
2293
+ fromNodeId: v.id("epistemicNodes"),
2294
+ toNodeId: v.id("epistemicNodes"),
2295
+ edgeType: edgeTypeValidator,
2296
+ weight: v.optional(v.number()),
2297
+ confidence: v.optional(v.number()),
2298
+ context: v.optional(v.string()),
2299
+ // NOTE: 'relation' field has been removed. Use 'weight' instead.
2300
+ derivationType: v.optional(v.string()),
2301
+ createdBy: v.string(),
2302
+ ...optionalScopeArgs
2303
+ })
2304
+ ),
2305
+ // Phase 2C: Allow skipping validation for migrations
2306
+ skipLayerValidation: v.optional(v.boolean())
2307
+ },
2308
+ returns: permissiveReturn,
2309
+ handler: async (ctx, args) => {
2310
+ const results = [];
2311
+ const errors = [];
2312
+ for (const edge of args.edges) {
2313
+ const fromNode = await ctx.db.get(edge.fromNodeId);
2314
+ const toNode = await ctx.db.get(edge.toNodeId);
2315
+ if (!fromNode || !toNode) {
2316
+ errors.push({
2317
+ globalId: edge.globalId,
2318
+ error: "One or both nodes not found"
2319
+ });
2320
+ continue;
2321
+ }
2322
+ const fromLayer = fromNode.epistemicLayer || getNodeLayer(fromNode.nodeType);
2323
+ const toLayer = toNode.epistemicLayer || getNodeLayer(toNode.nodeType);
2324
+ if (!args.skipLayerValidation) {
2325
+ const validation = validateEdgeLayers(
2326
+ edge.edgeType,
2327
+ fromLayer,
2328
+ toLayer
2329
+ );
2330
+ if (!validation.valid) {
2331
+ errors.push({
2332
+ globalId: edge.globalId,
2333
+ error: validation.reason || "Invalid layer combination"
2334
+ });
2335
+ continue;
2336
+ }
2337
+ }
2338
+ if (isDeprecatedEdgeType(edge.edgeType)) {
2339
+ errors.push({
2340
+ globalId: edge.globalId,
2341
+ error: `FORBIDDEN: Edge type '${edge.edgeType}' has been removed from StackOS schema.`
2342
+ });
2343
+ continue;
2344
+ }
2345
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
2346
+ globalId: edge.globalId,
2347
+ fromGlobalId: fromNode.globalId,
2348
+ toGlobalId: toNode.globalId,
2349
+ edgeType: edge.edgeType,
2350
+ weight: edge.weight,
2351
+ confidence: edge.confidence,
2352
+ context: edge.context,
2353
+ derivationType: edge.derivationType,
2354
+ createdBy: edge.createdBy,
2355
+ topicId: edge.projectId ? String(edge.projectId) : void 0,
2356
+ fromNodeType: fromNode.nodeType,
2357
+ toNodeType: toNode.nodeType,
2358
+ fromLayer,
2359
+ toLayer
2360
+ });
2361
+ results.push({ globalId: edge.globalId, edgeGlobalId: edge.globalId });
2362
+ }
2363
+ const projectIds = new Set(
2364
+ args.edges.flatMap(
2365
+ (edge) => typeof edge.projectId === "string" ? [edge.projectId] : []
2366
+ )
2367
+ );
2368
+ for (const pid of projectIds) {
2369
+ if (pid) {
2370
+ await resolveGraphPrimitivesAppResolvers().patchProject(ctx, pid, {
2371
+ lastActivityAt: Date.now()
2372
+ });
2373
+ }
2374
+ }
2375
+ return { created: results.length, results, errors };
2376
+ }
2377
+ });
2378
+ var getLineage = query({
2379
+ args: {
2380
+ nodeId: v.id("epistemicNodes"),
2381
+ maxDepth: v.optional(v.number()),
2382
+ // Phase 2D: Layer-aware traversal options
2383
+ minLayer: v.optional(v.number()),
2384
+ // 1=L1, 2=L2, 3=L3, 4=L4
2385
+ maxLayer: v.optional(v.number()),
2386
+ mode: v.optional(
2387
+ v.union(
2388
+ v.literal("anchor_down"),
2389
+ v.literal("anchor_up"),
2390
+ v.literal("same_layer"),
2391
+ v.literal("decision_trace")
2392
+ )
2393
+ )
2394
+ },
2395
+ returns: permissiveReturn,
2396
+ handler: async (ctx, args) => {
2397
+ const maxDepth = args.maxDepth ?? 10;
2398
+ const mode = args.mode ?? "anchor_down";
2399
+ const minLayer = args.minLayer ?? getDefaultMinLayer(mode);
2400
+ const maxLayer = args.maxLayer ?? 4;
2401
+ const lineage = [];
2402
+ const visited = /* @__PURE__ */ new Set();
2403
+ const startNode = await ctx.db.get(args.nodeId);
2404
+ if (!startNode) {
2405
+ return lineage;
2406
+ }
2407
+ const startLayer = startNode.epistemicLayer || getNodeLayer(startNode.nodeType);
2408
+ const queue = [{ nodeId: args.nodeId, depth: 0, currentLayer: startLayer }];
2409
+ while (queue.length > 0) {
2410
+ const current = queue.shift();
2411
+ if (!current || current.depth >= maxDepth) {
2412
+ continue;
2413
+ }
2414
+ const nodeIdStr = current.nodeId.toString();
2415
+ if (visited.has(nodeIdStr)) {
2416
+ continue;
2417
+ }
2418
+ visited.add(nodeIdStr);
2419
+ const edges = await ctx.db.query("epistemicEdges").withIndex("by_from", (q) => q.eq("fromNodeId", current.nodeId)).collect();
2420
+ const lineageEdges = edges.filter(
2421
+ (e) => e.edgeType === "derived_from"
2422
+ );
2423
+ for (const edge of lineageEdges) {
2424
+ if (!edge.toNodeId) {
2425
+ continue;
2426
+ }
2427
+ const targetNode = await ctx.db.get(edge.toNodeId);
2428
+ if (!targetNode || visited.has(edge.toNodeId.toString())) {
2429
+ continue;
2430
+ }
2431
+ const targetLayer = edge.toLayer || targetNode.epistemicLayer || getNodeLayer(targetNode.nodeType);
2432
+ const shouldContinue = shouldContinueTraversal(
2433
+ current.currentLayer,
2434
+ targetLayer,
2435
+ { mode, minLayer, maxLayer }
2436
+ );
2437
+ if (!shouldContinue) {
2438
+ continue;
2439
+ }
2440
+ lineage.push({
2441
+ node: targetNode,
2442
+ depth: current.depth + 1,
2443
+ edgeType: edge.edgeType,
2444
+ layer: targetLayer
2445
+ });
2446
+ queue.push({
2447
+ nodeId: edge.toNodeId,
2448
+ depth: current.depth + 1,
2449
+ currentLayer: targetLayer
2450
+ });
2451
+ }
2452
+ }
2453
+ return lineage;
2454
+ }
2455
+ });
2456
+ var getEvidenceForBelief = query({
2457
+ args: {
2458
+ beliefNodeId: v.id("epistemicNodes")
2459
+ },
2460
+ returns: permissiveReturn,
2461
+ handler: async (ctx, args) => {
2462
+ const informsEdges = await ctx.db.query("epistemicEdges").withIndex(
2463
+ "by_to_type",
2464
+ (q) => q.eq("toNodeId", args.beliefNodeId).eq("edgeType", "informs")
2465
+ ).collect();
2466
+ const supportEdges = informsEdges.filter((e) => (e.weight ?? 0) >= 0);
2467
+ const contradictEdges = informsEdges.filter((e) => (e.weight ?? 0) < 0);
2468
+ const supportingEvidence = await Promise.all(
2469
+ supportEdges.map(async (edge) => ({
2470
+ edge,
2471
+ node: await ctx.db.get(edge.fromNodeId)
2472
+ }))
2473
+ );
2474
+ const contradictingEvidence = await Promise.all(
2475
+ contradictEdges.map(async (edge) => ({
2476
+ edge,
2477
+ node: await ctx.db.get(edge.fromNodeId)
2478
+ }))
2479
+ );
2480
+ const filteredSupporting = supportingEvidence.filter(
2481
+ (e) => e.node !== null
2482
+ );
2483
+ const filteredContradicting = contradictingEvidence.filter(
2484
+ (e) => e.node !== null
2485
+ );
2486
+ return {
2487
+ supporting: filteredSupporting,
2488
+ contradicting: filteredContradicting,
2489
+ supportCount: filteredSupporting.length,
2490
+ contradictCount: filteredContradicting.length
2491
+ };
2492
+ }
2493
+ });
2494
+ var cleanupDeprecatedEdges = mutation({
2495
+ args: {
2496
+ ...optionalScopeArgs,
2497
+ dryRun: v.optional(v.boolean())
2498
+ // If true, just reports what would be deleted
2499
+ },
2500
+ returns: permissiveReturn,
2501
+ handler: async (ctx, args) => {
2502
+ const DEPRECATED_TYPES = [
2503
+ "contradicts"
2504
+ // Old generic contradict edge (use weight: -1 on informs)
2505
+ ];
2506
+ const scopeId = args.topicId || args.projectId;
2507
+ const allEdges = scopeId ? await ctx.db.query("epistemicEdges").withIndex("by_topic", (q) => q.eq("topicId", scopeId)).collect() : [];
2508
+ const deprecatedEdges = allEdges.filter(
2509
+ (edge) => DEPRECATED_TYPES.includes(edge.edgeType)
2510
+ );
2511
+ if (args.dryRun) {
2512
+ return {
2513
+ dryRun: true,
2514
+ found: deprecatedEdges.length,
2515
+ edges: deprecatedEdges.map((e) => ({
2516
+ id: e._id,
2517
+ type: e.edgeType,
2518
+ from: e.fromNodeId,
2519
+ to: e.toNodeId,
2520
+ description: e.context?.description || e.description
2521
+ })),
2522
+ message: `Would delete ${deprecatedEdges.length} deprecated edges`
2523
+ };
2524
+ }
2525
+ for (const edge of deprecatedEdges) {
2526
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.deleteEdge, {
2527
+ globalId: edge.globalId
2528
+ });
2529
+ await ctx.db.delete(edge._id);
2530
+ }
2531
+ return {
2532
+ dryRun: false,
2533
+ deleted: deprecatedEdges.length,
2534
+ edges: deprecatedEdges.map((e) => ({
2535
+ id: e._id,
2536
+ type: e.edgeType
2537
+ })),
2538
+ message: `Deleted ${deprecatedEdges.length} deprecated edges`
2539
+ };
2540
+ }
2541
+ });
2542
+ var deleteEdges = mutation({
2543
+ args: {
2544
+ edgeIds: v.array(v.id("epistemicEdges"))
2545
+ },
2546
+ returns: permissiveReturn,
2547
+ handler: async (ctx, args) => {
2548
+ const deleted = [];
2549
+ const notFound = [];
2550
+ for (const edgeId of args.edgeIds) {
2551
+ const edge = await ctx.db.get(edgeId);
2552
+ if (edge) {
2553
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.deleteEdge, {
2554
+ globalId: edge.globalId
2555
+ });
2556
+ await ctx.db.delete(edgeId);
2557
+ deleted.push(edgeId);
2558
+ } else {
2559
+ notFound.push(edgeId);
2560
+ }
2561
+ }
2562
+ return {
2563
+ deleted: deleted.length,
2564
+ notFound: notFound.length,
2565
+ message: `Deleted ${deleted.length} edges, ${notFound.length} not found`
2566
+ };
2567
+ }
2568
+ });
2569
+ var getClusterEdges = query({
2570
+ args: {
2571
+ beliefIds: v.array(v.string())
2572
+ },
2573
+ returns: permissiveReturn,
2574
+ handler: async (ctx, args) => {
2575
+ if (args.beliefIds.length === 0) {
2576
+ return [];
2577
+ }
2578
+ const beliefIdSet = new Set(args.beliefIds);
2579
+ const allEdges = await ctx.db.query("epistemicEdges").collect();
2580
+ return allEdges.filter((edge) => {
2581
+ const fromInCluster = beliefIdSet.has(String(edge.fromNodeId)) || edge.sourceGlobalId && beliefIdSet.has(edge.sourceGlobalId);
2582
+ const toInCluster = beliefIdSet.has(String(edge.toNodeId)) || edge.targetGlobalId && beliefIdSet.has(edge.targetGlobalId);
2583
+ return fromInCluster && toInCluster;
2584
+ });
2585
+ }
2586
+ });
2587
+ var mirrorEdgeToConvex = internalMutation({
2588
+ args: {
2589
+ globalId: v.string(),
2590
+ fromGlobalId: v.string(),
2591
+ toGlobalId: v.string(),
2592
+ edgeType: v.string(),
2593
+ weight: v.optional(v.number()),
2594
+ confidence: v.optional(v.number()),
2595
+ context: v.optional(v.string()),
2596
+ derivationType: v.optional(v.string()),
2597
+ createdBy: v.string(),
2598
+ ...optionalScopeArgs,
2599
+ fromLayer: v.optional(v.string()),
2600
+ toLayer: v.optional(v.string()),
2601
+ fromNodeType: v.optional(v.string()),
2602
+ toNodeType: v.optional(v.string()),
2603
+ // === CLASSIFICATION FIELDS ===
2604
+ reasoningMethod: v.optional(v.string()),
2605
+ logicalRole: v.optional(v.string()),
2606
+ temporalClass: v.optional(v.string()),
2607
+ validFrom: v.optional(v.number()),
2608
+ validUntil: v.optional(v.number()),
2609
+ constraint: v.optional(v.string()),
2610
+ propagation: v.optional(v.string()),
2611
+ blocking: v.optional(v.boolean()),
2612
+ implicit: v.optional(v.boolean()),
2613
+ conditionalA: v.optional(subjectiveOpinionValidator),
2614
+ conditionalNotA: v.optional(subjectiveOpinionValidator)
2615
+ },
2616
+ returns: permissiveReturn,
2617
+ handler: async (ctx, args) => {
2618
+ const fromNode = await ctx.db.query("epistemicNodes").withIndex("by_globalId", (q) => q.eq("globalId", args.fromGlobalId)).first();
2619
+ if (!fromNode) {
2620
+ console.log(
2621
+ `[Dual-Write] Skipping mirror - source node not in Convex: ${args.fromGlobalId}`
2622
+ );
2623
+ return { success: false, reason: "source_not_in_convex" };
2624
+ }
2625
+ const toNode = await ctx.db.query("epistemicNodes").withIndex("by_globalId", (q) => q.eq("globalId", args.toGlobalId)).first();
2626
+ const existing = await ctx.db.query("epistemicEdges").withIndex("by_globalId", (q) => q.eq("globalId", args.globalId)).first();
2627
+ if (existing) {
2628
+ console.log(`[Dual-Write] Edge already exists: ${args.globalId}`);
2629
+ return { success: true, edgeId: existing._id, existed: true };
2630
+ }
2631
+ const now = Date.now();
2632
+ const edgeId = await ctx.db.insert("epistemicEdges", {
2633
+ globalId: args.globalId,
2634
+ fromNodeId: fromNode._id,
2635
+ toNodeId: toNode?._id,
2636
+ // Optional - may be undefined for cross-graph edges
2637
+ sourceGlobalId: args.fromGlobalId,
2638
+ targetGlobalId: args.toGlobalId,
2639
+ // Cast through any because EdgeType may include types not yet in
2640
+ // Convex _generated types (deployed schema lags source schema).
2641
+ edgeType: args.edgeType,
2642
+ weight: args.weight,
2643
+ confidence: args.confidence,
2644
+ context: args.context,
2645
+ derivationType: args.derivationType,
2646
+ constraint: args.constraint,
2647
+ propagation: args.propagation,
2648
+ blocking: args.blocking,
2649
+ implicit: args.implicit,
2650
+ conditionalA: args.conditionalA,
2651
+ conditionalNotA: args.conditionalNotA,
2652
+ createdBy: args.createdBy,
2653
+ createdAt: now,
2654
+ updatedAt: now,
2655
+ topicId: args.projectId,
2656
+ fromLayer: args.fromLayer,
2657
+ toLayer: args.toLayer,
2658
+ fromNodeType: args.fromNodeType,
2659
+ toNodeType: args.toNodeType,
2660
+ // Classification fields
2661
+ reasoningMethod: args.reasoningMethod,
2662
+ logicalRole: args.logicalRole,
2663
+ temporalClass: args.temporalClass,
2664
+ validFrom: args.validFrom,
2665
+ validUntil: args.validUntil
2666
+ });
2667
+ console.log(
2668
+ `[Dual-Write] Mirrored edge to Convex: ${args.globalId} (${args.edgeType})`
2669
+ );
2670
+ return { success: true, edgeId, existed: false };
2671
+ }
2672
+ });
2673
+ var deleteEdgeFromConvex = internalMutation({
2674
+ args: {
2675
+ globalId: v.string()
2676
+ },
2677
+ returns: permissiveReturn,
2678
+ handler: async (ctx, args) => {
2679
+ const existing = await ctx.db.query("epistemicEdges").withIndex("by_globalId", (q) => q.eq("globalId", args.globalId)).first();
2680
+ if (!existing) {
2681
+ return { success: false, reason: "not_found" };
2682
+ }
2683
+ await ctx.db.delete(existing._id);
2684
+ console.log(`[Dual-Write] Deleted edge from Convex: ${args.globalId}`);
2685
+ return { success: true };
2686
+ }
2687
+ });
2688
+ var updateEdgeInConvex = internalMutation({
2689
+ args: {
2690
+ globalId: v.string(),
2691
+ weight: v.optional(v.number()),
2692
+ confidence: v.optional(v.number()),
2693
+ context: v.optional(v.string()),
2694
+ derivationType: v.optional(v.string()),
2695
+ constraint: v.optional(v.string()),
2696
+ propagation: v.optional(v.string()),
2697
+ blocking: v.optional(v.boolean()),
2698
+ implicit: v.optional(v.boolean()),
2699
+ conditionalA: v.optional(subjectiveOpinionValidator),
2700
+ conditionalNotA: v.optional(subjectiveOpinionValidator)
2701
+ },
2702
+ returns: permissiveReturn,
2703
+ handler: async (ctx, args) => {
2704
+ const existing = await ctx.db.query("epistemicEdges").withIndex("by_globalId", (q) => q.eq("globalId", args.globalId)).first();
2705
+ if (!existing) {
2706
+ return { success: false, reason: "not_found" };
2707
+ }
2708
+ const updates = {
2709
+ updatedAt: Date.now()
2710
+ };
2711
+ if (args.weight !== void 0) {
2712
+ updates.weight = args.weight;
2713
+ }
2714
+ if (args.confidence !== void 0) {
2715
+ updates.confidence = args.confidence;
2716
+ }
2717
+ if (args.context !== void 0) {
2718
+ updates.context = args.context;
2719
+ }
2720
+ if (args.derivationType !== void 0) {
2721
+ updates.derivationType = args.derivationType;
2722
+ }
2723
+ if (args.constraint !== void 0) {
2724
+ updates.constraint = args.constraint;
2725
+ }
2726
+ if (args.propagation !== void 0) {
2727
+ updates.propagation = args.propagation;
2728
+ }
2729
+ if (args.blocking !== void 0) {
2730
+ updates.blocking = args.blocking;
2731
+ }
2732
+ if (args.implicit !== void 0) {
2733
+ updates.implicit = args.implicit;
2734
+ }
2735
+ if (args.conditionalA !== void 0) {
2736
+ updates.conditionalA = args.conditionalA;
2737
+ }
2738
+ if (args.conditionalNotA !== void 0) {
2739
+ updates.conditionalNotA = args.conditionalNotA;
2740
+ }
2741
+ await ctx.db.patch(existing._id, updates);
2742
+ console.log(`[Dual-Write] Updated edge in Convex: ${args.globalId}`);
2743
+ return { success: true };
2744
+ }
2745
+ });
2746
+
2747
+ export { batchCreate, cleanupDeprecatedEdges, create, deleteEdgeFromConvex, deleteEdges, edgeTypeValidator, epistemicLayerValidator, findContradictions, findSupport, get, getBetween, getByGlobalId, getByNodes, getByProject, getByProjectAndType, getBySourceNode, getByTargetNode, getByTopic, getClusterEdges, getEvidenceForBelief, getFromNode, getLineage, getToNode, listAll, mirrorEdgeToConvex, remove, removeBetween, update, updateEdgeInConvex };
2748
+ //# sourceMappingURL=epistemicEdges.js.map
2749
+ //# sourceMappingURL=epistemicEdges.js.map