@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,4993 @@
1
+ import { v } from 'convex/values';
2
+ import { componentsGeneric, defineTable, mutationGeneric, anyApi, queryGeneric, internalQueryGeneric, internalMutationGeneric } from 'convex/server';
3
+
4
+ // src/epistemicQuestions.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
+ var checkProjectAccess = checkTopicAccess;
791
+
792
+ // ../access-control/src/audience.ts
793
+ var AUDIENCE_CLASS_RANK = {
794
+ public: 0,
795
+ restricted_external: 1,
796
+ internal: 2
797
+ };
798
+ function normalizeKey(key) {
799
+ return (key ?? "").trim().toLowerCase().replace(/[^a-z0-9:_-]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
800
+ }
801
+ function normalizeAudienceKey(key) {
802
+ return normalizeKey(key);
803
+ }
804
+ function classFromAudienceKey(audienceKey, fallback = "internal") {
805
+ const key = normalizeKey(audienceKey);
806
+ if (!key) {
807
+ return fallback;
808
+ }
809
+ if (key === "internal") {
810
+ return "internal";
811
+ }
812
+ if (key === "public") {
813
+ return "public";
814
+ }
815
+ if (key === "lp" || key === "external" || key === "client" || key === "partner" || key === "portfolio" || key === "network" || key === "restricted_external") {
816
+ return "restricted_external";
817
+ }
818
+ return fallback;
819
+ }
820
+ function canAudienceClassAccess(viewerClass, resourceClass) {
821
+ return AUDIENCE_CLASS_RANK[viewerClass] >= AUDIENCE_CLASS_RANK[resourceClass];
822
+ }
823
+
824
+ // ../access-control/src/audienceRegistry.ts
825
+ var DEFAULT_AUDIENCES = [
826
+ {
827
+ audienceKey: "internal",
828
+ audienceLabel: "Internal",
829
+ audienceClass: "internal"
830
+ },
831
+ {
832
+ audienceKey: "lp",
833
+ audienceLabel: "Limited Partners",
834
+ audienceClass: "restricted_external"
835
+ },
836
+ {
837
+ audienceKey: "public",
838
+ audienceLabel: "Public",
839
+ audienceClass: "public"
840
+ }
841
+ ];
842
+ var AUDIENCE_CLASS_PRIORITY = {
843
+ internal: 0,
844
+ restricted_external: 1,
845
+ public: 2
846
+ };
847
+ function normalizeRegistryRow(row) {
848
+ return {
849
+ audienceKey: normalizeAudienceKey(row.audienceKey),
850
+ audienceLabel: row.audienceLabel,
851
+ audienceClass: row.audienceClass,
852
+ workspaceId: row.workspaceId
853
+ };
854
+ }
855
+ function dedupeRegistryRows(rows) {
856
+ const byKey = /* @__PURE__ */ new Map();
857
+ for (const row of rows) {
858
+ const key = normalizeAudienceKey(row.audienceKey);
859
+ if (!key) {
860
+ continue;
861
+ }
862
+ const existing = byKey.get(key);
863
+ const isWorkspaceScoped = row.workspaceId !== void 0;
864
+ const existingWorkspaceScoped = existing?.workspaceId !== void 0;
865
+ if (!existing || isWorkspaceScoped && !existingWorkspaceScoped) {
866
+ byKey.set(key, {
867
+ ...row,
868
+ audienceKey: key
869
+ });
870
+ }
871
+ }
872
+ const normalized = [...byKey.values()];
873
+ normalized.sort((a, b) => {
874
+ const classDelta = AUDIENCE_CLASS_PRIORITY[a.audienceClass] - AUDIENCE_CLASS_PRIORITY[b.audienceClass];
875
+ if (classDelta !== 0) {
876
+ return classDelta;
877
+ }
878
+ return a.audienceKey.localeCompare(b.audienceKey);
879
+ });
880
+ return normalized;
881
+ }
882
+ async function queryRegistryRows(ctx, args) {
883
+ if (!args.tenantId) {
884
+ return [...DEFAULT_AUDIENCES];
885
+ }
886
+ const rows = await ctx.db.query("platformAudiences").withIndex("by_tenantId", (q) => q.eq("tenantId", args.tenantId)).collect();
887
+ const workspaceIdString = args.workspaceId ? String(args.workspaceId) : null;
888
+ const tenantScoped = rows.filter((row) => row.status === "active");
889
+ const applicable = tenantScoped.filter((row) => {
890
+ if (!row.workspaceId) {
891
+ return true;
892
+ }
893
+ if (!workspaceIdString) {
894
+ return false;
895
+ }
896
+ return String(row.workspaceId) === workspaceIdString;
897
+ });
898
+ return dedupeRegistryRows([
899
+ ...DEFAULT_AUDIENCES,
900
+ ...applicable.map(
901
+ (row) => normalizeRegistryRow({
902
+ audienceKey: row.audienceKey,
903
+ audienceLabel: row.audienceLabel,
904
+ audienceClass: row.audienceClass,
905
+ workspaceId: row.workspaceId
906
+ })
907
+ )
908
+ ]);
909
+ }
910
+ async function listAudienceRegistryRows(ctx, args) {
911
+ return queryRegistryRows(ctx, args);
912
+ }
913
+
914
+ // ../access-control/src/auth.ts
915
+ async function getCurrentUserId(ctx) {
916
+ const identity = await ctx.auth.getUserIdentity();
917
+ return identity?.subject ?? null;
918
+ }
919
+ var permissiveReturn = v.optional(v.any());
920
+ var looseJsonObject = v.record(v.string(), v.any());
921
+ var looseJsonArray = v.array(v.any());
922
+ v.union(
923
+ v.string(),
924
+ v.number(),
925
+ v.boolean(),
926
+ v.null(),
927
+ looseJsonObject,
928
+ looseJsonArray
929
+ );
930
+ var api2 = anyApi;
931
+ componentsGeneric();
932
+ var internal = anyApi;
933
+ var internalMutation = internalMutationGeneric;
934
+ var internalQuery = internalQueryGeneric;
935
+ var mutation = mutationGeneric;
936
+ var query = queryGeneric;
937
+
938
+ // src/embeddingTrigger.ts
939
+ async function scheduleEmbeddingGeneration(args) {
940
+ try {
941
+ await args.ctx.scheduler.runAfter(
942
+ 0,
943
+ "embeddingActions:generateEpistemicNodeEmbedding",
944
+ {
945
+ nodeId: args.nodeId,
946
+ projectId: args.projectId ? String(args.projectId) : void 0,
947
+ topicId: args.topicId ? String(args.topicId) : void 0,
948
+ createdBy: args.createdBy,
949
+ nodeType: args.nodeType,
950
+ text: args.text.slice(0, 2e4),
951
+ hasAnswer: args.hasAnswer,
952
+ confidence: args.confidence
953
+ }
954
+ );
955
+ } catch {
956
+ }
957
+ }
958
+
959
+ // src/globalId.ts
960
+ function generateGlobalId() {
961
+ const bytes = new Uint8Array(16);
962
+ crypto.getRandomValues(bytes);
963
+ bytes[6] = bytes[6] & 15 | 64;
964
+ bytes[8] = bytes[8] & 63 | 128;
965
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
966
+ ""
967
+ );
968
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
969
+ }
970
+
971
+ // src/topicProjectOverlay.ts
972
+ var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
973
+ function readNonEmptyString2(value) {
974
+ if (typeof value !== "string") {
975
+ return;
976
+ }
977
+ const normalized = value.trim();
978
+ return normalized.length > 0 ? normalized : void 0;
979
+ }
980
+ function readStringArray2(value) {
981
+ if (!Array.isArray(value)) {
982
+ return [];
983
+ }
984
+ return value.map((entry) => readNonEmptyString2(entry)).filter((entry) => Boolean(entry));
985
+ }
986
+ function readMetadata2(topic) {
987
+ return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
988
+ }
989
+ function readLegacyProjectId2(value) {
990
+ if (!value) {
991
+ return;
992
+ }
993
+ return readNonEmptyString2(value[LEGACY_SCOPE_FIELD2]);
994
+ }
995
+ function coerceVisibility2(value) {
996
+ return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
997
+ }
998
+ function coerceStatus2(value) {
999
+ return value === "active" || value === "archived" || value === "watching" ? value : void 0;
1000
+ }
1001
+ function mapProjectType2(topic, metadata) {
1002
+ const explicit = readNonEmptyString2(metadata.projectType);
1003
+ if (explicit) {
1004
+ return explicit;
1005
+ }
1006
+ if (topic.type === "theme") {
1007
+ return "thematic";
1008
+ }
1009
+ return readNonEmptyString2(topic.type) || "general";
1010
+ }
1011
+ function isProjectLikeTopic2(topic) {
1012
+ const metadata = readMetadata2(topic);
1013
+ return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId2(topic) !== void 0 || readNonEmptyString2(metadata.projectType) !== void 0;
1014
+ }
1015
+ function isMissingLucernChildComponentError(error) {
1016
+ const message = error instanceof Error ? error.message : String(error);
1017
+ return message.includes(
1018
+ 'Child component ComponentName(Identifier("lucern")) not found'
1019
+ ) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1020
+ }
1021
+ async function resolveTopicDoc2(ctx, scopeId) {
1022
+ if (ctx?.db && typeof ctx.db.get === "function") {
1023
+ try {
1024
+ const directTopic = await ctx.db.get(scopeId);
1025
+ if (directTopic) {
1026
+ return directTopic;
1027
+ }
1028
+ } catch {
1029
+ }
1030
+ }
1031
+ if (typeof ctx.runQuery !== "function") {
1032
+ return null;
1033
+ }
1034
+ try {
1035
+ const topic = await ctx.runQuery(api2.topics.get, {
1036
+ id: String(scopeId)
1037
+ });
1038
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1039
+ return topic;
1040
+ }
1041
+ } catch {
1042
+ }
1043
+ try {
1044
+ const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1045
+ projectId: String(scopeId)
1046
+ });
1047
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1048
+ return topic;
1049
+ }
1050
+ } catch {
1051
+ }
1052
+ return null;
1053
+ }
1054
+ function materializeTopicProjectOverlay2(topic, idMode = "legacy") {
1055
+ const metadata = readMetadata2(topic);
1056
+ const topicId = String(topic._id);
1057
+ const legacyProjectId = readLegacyProjectId2(topic) || readLegacyProjectId2(metadata) || readNonEmptyString2(metadata.legacyProjectId);
1058
+ const storageProjectId = legacyProjectId || topicId;
1059
+ const outwardId = idMode === "topic" ? topicId : storageProjectId;
1060
+ const visibility = coerceVisibility2(topic.visibility) || coerceVisibility2(metadata.visibility) || "private";
1061
+ const status = coerceStatus2(topic.status) || coerceStatus2(metadata.status) || "active";
1062
+ const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
1063
+ const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
1064
+ return {
1065
+ ...metadata,
1066
+ _id: outwardId,
1067
+ projectId: outwardId,
1068
+ topicId,
1069
+ storageProjectId,
1070
+ legacyProjectId,
1071
+ name: readNonEmptyString2(topic.name) || "Untitled Theme",
1072
+ type: mapProjectType2(topic, metadata),
1073
+ description: readNonEmptyString2(topic.description),
1074
+ ownerId: readNonEmptyString2(metadata.ownerId) || readNonEmptyString2(topic.createdBy) || "system",
1075
+ sharedWith: readStringArray2(metadata.sharedWith),
1076
+ visibility,
1077
+ tenantId: readNonEmptyString2(topic.tenantId) || readNonEmptyString2(metadata.tenantId),
1078
+ workspaceId: readNonEmptyString2(topic.workspaceId) || readNonEmptyString2(metadata.workspaceId),
1079
+ status,
1080
+ tags: readStringArray2(metadata.tags),
1081
+ chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
1082
+ artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
1083
+ lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
1084
+ _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
1085
+ createdAt,
1086
+ updatedAt
1087
+ };
1088
+ }
1089
+ async function resolveTopicProjectOverlay2(ctx, scopeId, options = {}) {
1090
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1091
+ if (!topic) {
1092
+ return null;
1093
+ }
1094
+ if (options.projectLikeOnly !== false && !isProjectLikeTopic2(topic)) {
1095
+ return null;
1096
+ }
1097
+ return materializeTopicProjectOverlay2(topic, options.idMode);
1098
+ }
1099
+ async function listTopicProjectOverlays2(ctx, options = {}) {
1100
+ let allTopics = [];
1101
+ if (ctx?.db?.query && typeof ctx.db.query === "function") {
1102
+ try {
1103
+ allTopics = await ctx.db.query("topics").collect();
1104
+ } catch {
1105
+ allTopics = [];
1106
+ }
1107
+ }
1108
+ if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
1109
+ allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
1110
+ }
1111
+ return allTopics.filter(
1112
+ (topic) => options.projectLikeOnly === false || isProjectLikeTopic2(topic)
1113
+ ).map((topic) => materializeTopicProjectOverlay2(topic, options.idMode));
1114
+ }
1115
+ async function patchTopicProjectOverlay(ctx, scopeId, value) {
1116
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1117
+ if (!topic) {
1118
+ return null;
1119
+ }
1120
+ const nextMetadata = { ...readMetadata2(topic) };
1121
+ const patch = {};
1122
+ const topicUpdateArgs = {
1123
+ id: String(topic._id)
1124
+ };
1125
+ for (const [key, rawValue] of Object.entries(value)) {
1126
+ switch (key) {
1127
+ case "_id":
1128
+ case "projectId":
1129
+ case "topicId":
1130
+ case "legacyProjectId":
1131
+ case "storageProjectId":
1132
+ break;
1133
+ case "name":
1134
+ case "description":
1135
+ patch[key] = rawValue;
1136
+ topicUpdateArgs[key] = rawValue;
1137
+ break;
1138
+ case "tenantId":
1139
+ case "workspaceId":
1140
+ case "ownerId":
1141
+ throw new Error(
1142
+ `patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
1143
+ );
1144
+ case "status": {
1145
+ const status = coerceStatus2(rawValue);
1146
+ if (status) {
1147
+ patch.status = status;
1148
+ topicUpdateArgs.status = status;
1149
+ }
1150
+ break;
1151
+ }
1152
+ case "visibility": {
1153
+ const visibility = coerceVisibility2(rawValue);
1154
+ if (visibility) {
1155
+ patch.visibility = visibility;
1156
+ topicUpdateArgs.visibility = visibility;
1157
+ }
1158
+ break;
1159
+ }
1160
+ case "type": {
1161
+ const projectType = readNonEmptyString2(rawValue);
1162
+ if (projectType) {
1163
+ nextMetadata.projectType = projectType;
1164
+ } else {
1165
+ delete nextMetadata.projectType;
1166
+ }
1167
+ break;
1168
+ }
1169
+ case "updatedAt":
1170
+ case "createdAt":
1171
+ break;
1172
+ default:
1173
+ if (rawValue === void 0) {
1174
+ delete nextMetadata[key];
1175
+ } else {
1176
+ nextMetadata[key] = rawValue;
1177
+ }
1178
+ }
1179
+ }
1180
+ patch.updatedAt = Date.now();
1181
+ patch.metadata = nextMetadata;
1182
+ topicUpdateArgs.metadata = nextMetadata;
1183
+ if (typeof ctx.runMutation === "function") {
1184
+ try {
1185
+ await ctx.runMutation(api2.topics.update, topicUpdateArgs);
1186
+ } catch (error) {
1187
+ if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
1188
+ throw error;
1189
+ }
1190
+ await ctx.db.patch(String(topic._id), patch);
1191
+ }
1192
+ } else if (ctx?.db && typeof ctx.db.patch === "function") {
1193
+ await ctx.db.patch(String(topic._id), patch);
1194
+ } else {
1195
+ throw new Error(
1196
+ "Cannot patch topic without component adapter (ctx.runMutation unavailable)"
1197
+ );
1198
+ }
1199
+ return materializeTopicProjectOverlay2(
1200
+ {
1201
+ ...topic,
1202
+ ...patch,
1203
+ metadata: nextMetadata
1204
+ }
1205
+ );
1206
+ }
1207
+
1208
+ // src/resolvers.ts
1209
+ function isMissingLucernChildComponentError2(error) {
1210
+ const message = error instanceof Error ? error.message : String(error);
1211
+ return message.includes('Child component ComponentName(Identifier("lucern")) not found') || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1212
+ }
1213
+ function isAdvisoryTopicPatch(value) {
1214
+ const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
1215
+ const keys = Object.keys(value);
1216
+ return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
1217
+ }
1218
+ async function patchProjectWithTolerance(ctx, projectId, value) {
1219
+ try {
1220
+ await patchTopicProjectOverlay(ctx, projectId, value);
1221
+ } catch (error) {
1222
+ if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
1223
+ throw error;
1224
+ }
1225
+ console.warn("[lucern graph-primitives] Non-fatal advisory topic patch failure", {
1226
+ projectId,
1227
+ keys: Object.keys(value),
1228
+ error: error instanceof Error ? error.message : error
1229
+ });
1230
+ }
1231
+ }
1232
+ function defaultResolvers2() {
1233
+ return {
1234
+ async getProject(ctx, projectId) {
1235
+ return await resolveTopicProjectOverlay2(ctx, projectId, {
1236
+ idMode: "legacy",
1237
+ projectLikeOnly: false
1238
+ });
1239
+ },
1240
+ async patchProject(ctx, projectId, value) {
1241
+ await patchProjectWithTolerance(ctx, projectId, value);
1242
+ },
1243
+ async listTopics(ctx) {
1244
+ return await listTopicProjectOverlays2(ctx, {
1245
+ idMode: "legacy"
1246
+ });
1247
+ },
1248
+ async getFinalArtifact(ctx, artifactId) {
1249
+ return await ctx.db.get(artifactId);
1250
+ }
1251
+ };
1252
+ }
1253
+ var resolverOverrides2 = {};
1254
+ function resolveGraphPrimitivesAppResolvers(_ctx) {
1255
+ return {
1256
+ ...defaultResolvers2(),
1257
+ ...resolverOverrides2
1258
+ };
1259
+ }
1260
+ var LEGACY_SCOPE_FIELD3 = "graphScopeProjectId";
1261
+ function asMappedProjectId(topic) {
1262
+ if (!topic) {
1263
+ return;
1264
+ }
1265
+ const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD3]);
1266
+ if (directLegacyProjectId) {
1267
+ return directLegacyProjectId;
1268
+ }
1269
+ const metadata = topic.metadata || {};
1270
+ const candidate = metadata[LEGACY_SCOPE_FIELD3] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
1271
+ return candidate ? candidate : void 0;
1272
+ }
1273
+ function normalizeScopeValue(value) {
1274
+ if (typeof value !== "string") {
1275
+ return;
1276
+ }
1277
+ const normalized = value.trim();
1278
+ return normalized.length > 0 ? normalized : void 0;
1279
+ }
1280
+ function pickPrimaryTopic(candidates) {
1281
+ return [...candidates].sort((a, b) => {
1282
+ const depthA = a.depth ?? 9999;
1283
+ const depthB = b.depth ?? 9999;
1284
+ if (depthA !== depthB) {
1285
+ return depthA - depthB;
1286
+ }
1287
+ const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
1288
+ const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
1289
+ if (createdA !== createdB) {
1290
+ return createdA - createdB;
1291
+ }
1292
+ return String(a.name || "").localeCompare(String(b.name || ""));
1293
+ })[0];
1294
+ }
1295
+ async function findTopicsByScopeAlias(ctx, scopeId) {
1296
+ try {
1297
+ return await ctx.db.query("topics").withIndex(
1298
+ "by_graph_scope_project",
1299
+ (q) => q.eq(LEGACY_SCOPE_FIELD3, scopeId)
1300
+ ).collect();
1301
+ } catch {
1302
+ const topics = await ctx.db.query("topics").collect();
1303
+ return topics.filter((topic) => {
1304
+ const normalizedGlobalId = normalizeScopeValue(topic.globalId);
1305
+ const mappedProjectId = asMappedProjectId(topic);
1306
+ return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
1307
+ });
1308
+ }
1309
+ }
1310
+ async function tryResolveHostTopicById(ctx, topicId) {
1311
+ if (typeof ctx.runQuery !== "function") {
1312
+ return null;
1313
+ }
1314
+ try {
1315
+ return await ctx.runQuery(api2.topics.get, {
1316
+ id: topicId
1317
+ }) ?? null;
1318
+ } catch {
1319
+ return null;
1320
+ }
1321
+ }
1322
+ async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
1323
+ if (typeof ctx.runQuery !== "function") {
1324
+ return null;
1325
+ }
1326
+ try {
1327
+ return await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1328
+ projectId: legacyScopeId
1329
+ }) ?? null;
1330
+ } catch {
1331
+ return null;
1332
+ }
1333
+ }
1334
+ async function resolveInheritedWorkspaceScope(ctx, topic) {
1335
+ const MAX_DEPTH = 10;
1336
+ let tenantId = normalizeScopeValue(topic.tenantId);
1337
+ let workspaceId = normalizeScopeValue(topic.workspaceId);
1338
+ if (tenantId && workspaceId) {
1339
+ return { tenantId, workspaceId };
1340
+ }
1341
+ let current = topic;
1342
+ for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
1343
+ current = await ctx.db.get(current.parentTopicId);
1344
+ if (!current) break;
1345
+ if (!tenantId) {
1346
+ tenantId = normalizeScopeValue(current.tenantId);
1347
+ }
1348
+ if (!workspaceId) {
1349
+ workspaceId = normalizeScopeValue(current.workspaceId);
1350
+ }
1351
+ if (tenantId && workspaceId) break;
1352
+ }
1353
+ return { tenantId, workspaceId };
1354
+ }
1355
+ async function resolveTopicProjectScope(ctx, args) {
1356
+ if (args.topicId) {
1357
+ let topic = null;
1358
+ try {
1359
+ topic = await ctx.db.get(args.topicId);
1360
+ } catch {
1361
+ }
1362
+ if (!topic) {
1363
+ topic = await tryResolveHostTopicById(ctx, String(args.topicId));
1364
+ }
1365
+ if (!topic) {
1366
+ topic = pickPrimaryTopic(
1367
+ await findTopicsByScopeAlias(ctx, String(args.topicId))
1368
+ ) ?? null;
1369
+ }
1370
+ if (!topic) {
1371
+ throw new Error(`Topic not found: ${String(args.topicId)}`);
1372
+ }
1373
+ const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
1374
+ const mapped = asMappedProjectId(topic);
1375
+ if (mapped) {
1376
+ return {
1377
+ topicId: topic._id,
1378
+ projectId: mapped,
1379
+ tenantId: inherited.tenantId,
1380
+ workspaceId: inherited.workspaceId,
1381
+ source: "topic"
1382
+ };
1383
+ }
1384
+ return {
1385
+ topicId: topic._id,
1386
+ tenantId: inherited.tenantId,
1387
+ workspaceId: inherited.workspaceId,
1388
+ source: "topic"
1389
+ };
1390
+ }
1391
+ if (args.projectId) {
1392
+ let directTopic = null;
1393
+ try {
1394
+ directTopic = await ctx.db.get(
1395
+ args.projectId
1396
+ );
1397
+ } catch {
1398
+ }
1399
+ if (directTopic) {
1400
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1401
+ const mapped = asMappedProjectId(directTopic);
1402
+ return {
1403
+ topicId: directTopic._id,
1404
+ projectId: mapped ?? args.projectId,
1405
+ tenantId: inherited.tenantId,
1406
+ workspaceId: inherited.workspaceId,
1407
+ source: "topic_inferred"
1408
+ };
1409
+ }
1410
+ directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
1411
+ if (directTopic) {
1412
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1413
+ const mapped = asMappedProjectId(directTopic);
1414
+ return {
1415
+ topicId: directTopic._id,
1416
+ projectId: mapped ?? args.projectId,
1417
+ tenantId: inherited.tenantId,
1418
+ workspaceId: inherited.workspaceId,
1419
+ source: "topic_inferred"
1420
+ };
1421
+ }
1422
+ const topics = await findTopicsByScopeAlias(ctx, args.projectId);
1423
+ const primary = pickPrimaryTopic(topics);
1424
+ if (primary) {
1425
+ const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
1426
+ return {
1427
+ topicId: primary._id,
1428
+ projectId: args.projectId,
1429
+ tenantId: inherited.tenantId,
1430
+ workspaceId: inherited.workspaceId,
1431
+ source: "project_mapped_topic"
1432
+ };
1433
+ }
1434
+ throw new Error(
1435
+ `Legacy project scope ${String(args.projectId)} has no mapped topic.`
1436
+ );
1437
+ }
1438
+ throw new Error(
1439
+ "Missing scope: provide topicId (preferred) or legacy projectId alias."
1440
+ );
1441
+ }
1442
+ var optionalScopeArgs = {
1443
+ projectId: v.optional(v.string()),
1444
+ topicId: v.optional(v.string())
1445
+ };
1446
+ v.number();
1447
+ v.union(
1448
+ v.literal("very_high"),
1449
+ // 0.9+
1450
+ v.literal("high"),
1451
+ // 0.7-0.9
1452
+ v.literal("medium"),
1453
+ // 0.4-0.7
1454
+ v.literal("low"),
1455
+ // 0.2-0.4
1456
+ v.literal("very_low")
1457
+ // 0-0.2
1458
+ );
1459
+ v.union(
1460
+ v.literal(1),
1461
+ // Critical
1462
+ v.literal(2),
1463
+ // High
1464
+ v.literal(3),
1465
+ // Medium
1466
+ v.literal(4),
1467
+ // Low
1468
+ v.literal(5)
1469
+ // Backlog
1470
+ );
1471
+ v.union(
1472
+ v.literal("critical"),
1473
+ v.literal("high"),
1474
+ v.literal("medium"),
1475
+ v.literal("low"),
1476
+ v.literal("backlog")
1477
+ );
1478
+ v.union(
1479
+ v.literal("active"),
1480
+ v.literal("paused"),
1481
+ v.literal("completed"),
1482
+ v.literal("archived")
1483
+ );
1484
+ v.union(
1485
+ v.literal("pending"),
1486
+ v.literal("processing"),
1487
+ v.literal("completed"),
1488
+ v.literal("failed")
1489
+ );
1490
+ v.object({
1491
+ crunchbaseId: v.optional(v.string()),
1492
+ linkedinUrl: v.optional(v.string()),
1493
+ pitchbookId: v.optional(v.string()),
1494
+ twitterUrl: v.optional(v.string()),
1495
+ domain: v.optional(v.string())
1496
+ });
1497
+ var sourceType = v.union(
1498
+ v.literal("proprietary"),
1499
+ // Internal Stack research
1500
+ v.literal("primary"),
1501
+ // Direct interviews, calls
1502
+ v.literal("secondary"),
1503
+ // Published sources
1504
+ v.literal("ai_generated"),
1505
+ // AI-synthesized
1506
+ v.literal("user_input"),
1507
+ // Manual user entry
1508
+ v.literal("inferred")
1509
+ // System inference
1510
+ );
1511
+ v.object({
1512
+ sourceType: v.optional(sourceType),
1513
+ sourceId: v.optional(v.string()),
1514
+ // Reference to source entity
1515
+ sourceUrl: v.optional(v.string()),
1516
+ sourceDate: v.optional(v.number()),
1517
+ sourceName: v.optional(v.string())
1518
+ });
1519
+ v.object({
1520
+ cursor: v.optional(v.string()),
1521
+ limit: v.optional(v.number())
1522
+ });
1523
+ v.object({
1524
+ hasMore: v.boolean(),
1525
+ nextCursor: v.optional(v.string()),
1526
+ totalCount: v.optional(v.number())
1527
+ });
1528
+ var richTextContent = v.object({
1529
+ type: v.literal("doc"),
1530
+ content: looseJsonArray
1531
+ });
1532
+ v.union(v.string(), richTextContent);
1533
+ v.object({
1534
+ promptTokens: v.optional(v.number()),
1535
+ completionTokens: v.optional(v.number()),
1536
+ totalTokens: v.optional(v.number())
1537
+ });
1538
+ v.object({
1539
+ fileName: v.optional(v.string()),
1540
+ fileSize: v.optional(v.number()),
1541
+ mimeType: v.optional(v.string()),
1542
+ storageId: v.optional(v.id("_storage")),
1543
+ externalUrl: v.optional(v.string())
1544
+ });
1545
+
1546
+ // ../schema-management/src/spine/tables/epistemicNodes.ts
1547
+ var nodeType = v.union(
1548
+ // --- L4: Audit Targets (decisions, outcomes) ---
1549
+ v.literal("decision"),
1550
+ // Investment decision with knowledge horizon snapshot
1551
+ // --- L3: Traversal Anchors (epistemic structure) ---
1552
+ v.literal("belief"),
1553
+ // Structured conviction (immutable formulation)
1554
+ v.literal("question"),
1555
+ // Unit of uncertainty
1556
+ v.literal("theme"),
1557
+ // Investment thesis / conviction cluster
1558
+ v.literal("deal"),
1559
+ // Investment evaluation process
1560
+ v.literal("topic"),
1561
+ // Hierarchical knowledge container
1562
+ // --- L2: Compression Boundary (minimum reasoning unit) ---
1563
+ v.literal("claim"),
1564
+ // Atomic assertion that can be true/false
1565
+ v.literal("evidence"),
1566
+ // Interpreted signal linked to beliefs
1567
+ v.literal("synthesis"),
1568
+ // Primers, deep research
1569
+ v.literal("answer"),
1570
+ // Immutable answer snapshot for a question
1571
+ // --- L1: Terminal Leaves (non-traversable, grounding) ---
1572
+ v.literal("atomic_fact"),
1573
+ // Raw fact from source (not interpreted)
1574
+ v.literal("excerpt"),
1575
+ // Direct quote from source document
1576
+ v.literal("source"),
1577
+ // News, documents, transcripts
1578
+ // --- Ontological Entities (things in the world) ---
1579
+ v.literal("company"),
1580
+ // Organization (subtype: private, corporate, portfolio)
1581
+ v.literal("person"),
1582
+ // Individual (founder, expert, LP, contact)
1583
+ v.literal("investor"),
1584
+ // Investment entity (subtype: vc, lp, cvc, pe, family_office, angel)
1585
+ v.literal("function"),
1586
+ // What a company does (from classifier)
1587
+ v.literal("value_chain")
1588
+ // Market structure / value flow
1589
+ );
1590
+ var epistemicLayer = v.union(
1591
+ v.literal("L4"),
1592
+ // Decisions, outcomes - audit targets
1593
+ v.literal("L3"),
1594
+ // Beliefs, questions, themes - traversal anchors
1595
+ v.literal("L2"),
1596
+ // Claims, evidence, synthesis - compression boundary
1597
+ v.literal("L1"),
1598
+ // Atomic facts, excerpts, sources - terminal leaves
1599
+ v.literal("ontological"),
1600
+ // Companies, people, etc - not epistemic
1601
+ v.literal("organizational")
1602
+ // Topics, lenses, worktrees — structural containers
1603
+ );
1604
+ var nodeStatus = v.union(
1605
+ v.literal("active"),
1606
+ v.literal("superseded"),
1607
+ // Replaced by newer version
1608
+ v.literal("archived"),
1609
+ v.literal("deleted")
1610
+ );
1611
+ var sourceType2 = v.union(
1612
+ v.literal("human"),
1613
+ // User created directly
1614
+ v.literal("ai_extracted"),
1615
+ // LLM extracted from a source
1616
+ v.literal("ai_generated"),
1617
+ // LLM synthesized/created
1618
+ v.literal("imported"),
1619
+ // External system import
1620
+ v.literal("system"),
1621
+ // System-generated (migrations, classifiers)
1622
+ v.literal("verified"),
1623
+ // Human-verified source
1624
+ v.literal("proprietary")
1625
+ // Proprietary/internal data
1626
+ );
1627
+ var verificationStatus = v.union(
1628
+ v.literal("unverified"),
1629
+ v.literal("human_verified"),
1630
+ v.literal("ai_verified"),
1631
+ v.literal("contradicted"),
1632
+ v.literal("outdated")
1633
+ );
1634
+ var syncStatus = v.union(
1635
+ v.literal("synced"),
1636
+ // Node and edges fully synced to Neo4j
1637
+ v.literal("pending_edges"),
1638
+ // Node created, edges being created
1639
+ v.literal("edge_creation_failed")
1640
+ // Edge creation failed, needs retry
1641
+ );
1642
+ var audienceLabel = v.string();
1643
+ var sensitivityTier = v.union(
1644
+ v.literal("low"),
1645
+ v.literal("medium"),
1646
+ v.literal("high"),
1647
+ v.literal("restricted")
1648
+ );
1649
+ var exportClass = v.union(
1650
+ v.literal("internal_only"),
1651
+ v.literal("client_safe"),
1652
+ v.literal("public_safe"),
1653
+ v.literal("restricted")
1654
+ );
1655
+ var anonymizationClass = v.union(
1656
+ v.literal("none"),
1657
+ v.literal("standard"),
1658
+ v.literal("strict")
1659
+ );
1660
+ var epistemicStatus = v.union(
1661
+ v.literal("hypothesis"),
1662
+ // Initial conjecture, low evidence
1663
+ v.literal("emerging"),
1664
+ // Building evidence, gaining traction
1665
+ v.literal("established"),
1666
+ // Well-evidenced, core to thesis
1667
+ v.literal("challenged"),
1668
+ // Contradicting evidence appeared
1669
+ v.literal("assumption"),
1670
+ // Taken as given, not actively tested
1671
+ v.literal("deprecated")
1672
+ // Superseded or abandoned
1673
+ );
1674
+ var beliefStatus = v.union(
1675
+ v.literal("assumption"),
1676
+ v.literal("hypothesis"),
1677
+ v.literal("belief"),
1678
+ v.literal("fact")
1679
+ );
1680
+ var reversibility = v.union(
1681
+ v.literal("irreversible"),
1682
+ // One-way door decision
1683
+ v.literal("hard_to_reverse"),
1684
+ // Significant cost to undo
1685
+ v.literal("reversible"),
1686
+ // Can change course with moderate effort
1687
+ v.literal("trivial")
1688
+ // Easy to adjust
1689
+ );
1690
+ var predictionOutcome = v.union(
1691
+ v.literal("pending"),
1692
+ v.literal("confirmed"),
1693
+ v.literal("disconfirmed"),
1694
+ v.literal("partial"),
1695
+ v.literal("expired")
1696
+ );
1697
+ var predictionMeta = v.object({
1698
+ isPrediction: v.boolean(),
1699
+ registeredAt: v.number(),
1700
+ // When prediction was made
1701
+ expectedBy: v.optional(v.number()),
1702
+ // When we expect resolution
1703
+ outcome: v.optional(predictionOutcome),
1704
+ outcomeRecordedAt: v.optional(v.number()),
1705
+ outcomeEvidenceId: v.optional(v.string()),
1706
+ // globalId of confirming evidence
1707
+ confidenceAtPrediction: v.optional(v.number()),
1708
+ // 0-1
1709
+ actualVsPredicted: v.optional(v.string())
1710
+ // Notes on how outcome compared
1711
+ });
1712
+ var methodology = v.union(
1713
+ // Primary Research (high value)
1714
+ v.literal("primary_research"),
1715
+ // Direct investigation
1716
+ v.literal("expert_interview"),
1717
+ // Expert call/interview
1718
+ v.literal("customer_interview"),
1719
+ // Customer research
1720
+ v.literal("field_observation"),
1721
+ // On-site observation
1722
+ v.literal("proprietary_data"),
1723
+ // Internal data analysis
1724
+ // Secondary Research
1725
+ v.literal("desk_research"),
1726
+ // Public sources
1727
+ v.literal("regulatory_filing"),
1728
+ // SEC, regulatory docs
1729
+ v.literal("news_article"),
1730
+ // News/press
1731
+ v.literal("academic_paper"),
1732
+ // Academic research
1733
+ // AI-Assisted
1734
+ v.literal("ai_synthesis"),
1735
+ // AI-generated synthesis
1736
+ v.literal("ai_extraction")
1737
+ // AI-extracted from source
1738
+ );
1739
+ var informationAsymmetry = v.union(
1740
+ v.literal("proprietary"),
1741
+ // Only we have this
1742
+ v.literal("early"),
1743
+ // We're early but others will get it
1744
+ v.literal("common")
1745
+ // Everyone has access
1746
+ );
1747
+ var temporalNature = v.union(
1748
+ v.literal("factual"),
1749
+ // Resolved outcome. Grounded in reality.
1750
+ v.literal("forecast"),
1751
+ // Prediction. Will resolve. Discounted weight.
1752
+ v.literal("unknown")
1753
+ // Not yet classified.
1754
+ );
1755
+ var questionType = v.union(
1756
+ v.literal("validation"),
1757
+ // Does evidence support this belief?
1758
+ v.literal("falsification"),
1759
+ // What would prove this belief wrong?
1760
+ v.literal("assumption_probe"),
1761
+ // Is this unstated assumption true?
1762
+ v.literal("prediction_test"),
1763
+ // Will this predicted outcome occur?
1764
+ v.literal("counterfactual"),
1765
+ // What would we expect if X were false?
1766
+ v.literal("discovery"),
1767
+ // What don't we know yet?
1768
+ v.literal("clarification"),
1769
+ // What does X actually mean?
1770
+ v.literal("comparison"),
1771
+ // How does X compare to Y?
1772
+ v.literal("causal"),
1773
+ // What caused X?
1774
+ v.literal("mechanism"),
1775
+ // How does X work?
1776
+ v.literal("general")
1777
+ // Unclassified
1778
+ );
1779
+ var questionPriority = v.union(
1780
+ v.literal("critical"),
1781
+ // Blocks decision-making
1782
+ v.literal("high"),
1783
+ // Important for thesis
1784
+ v.literal("medium"),
1785
+ // Would be nice to know
1786
+ v.literal("low")
1787
+ // Background/curiosity
1788
+ );
1789
+ var answerQuality = v.union(
1790
+ v.literal("definitive"),
1791
+ // Clear, well-supported
1792
+ v.literal("strong"),
1793
+ // Good evidence, high confidence
1794
+ v.literal("moderate"),
1795
+ // Some evidence
1796
+ v.literal("weak"),
1797
+ // Limited evidence
1798
+ v.literal("speculative"),
1799
+ // Mostly conjecture
1800
+ v.literal("unanswered")
1801
+ // No answer yet
1802
+ );
1803
+ var consensusView = v.union(
1804
+ v.literal("aligned"),
1805
+ // We agree with market consensus
1806
+ v.literal("ahead_of"),
1807
+ // We see this before consensus does
1808
+ v.literal("contrarian"),
1809
+ // We actively disagree with consensus
1810
+ v.literal("orthogonal"),
1811
+ // We're looking at something consensus isn't discussing
1812
+ v.literal("unknown")
1813
+ // We don't know what consensus thinks
1814
+ );
1815
+ var themeConviction = v.union(
1816
+ v.literal("high"),
1817
+ // Strong conviction, actively deploying
1818
+ v.literal("medium"),
1819
+ // Building conviction
1820
+ v.literal("low"),
1821
+ // Exploring, not convicted
1822
+ v.literal("negative")
1823
+ // Actively avoiding
1824
+ );
1825
+ var decisionType = v.union(
1826
+ v.literal("invest"),
1827
+ v.literal("pass"),
1828
+ v.literal("follow_on"),
1829
+ v.literal("exit"),
1830
+ v.literal("deep_dive"),
1831
+ v.literal("monitor"),
1832
+ v.literal("deprioritize"),
1833
+ v.literal("thesis_adopt"),
1834
+ v.literal("thesis_revise"),
1835
+ v.literal("thesis_abandon")
1836
+ );
1837
+ var decisionOutcome = v.union(
1838
+ v.literal("pending"),
1839
+ v.literal("successful"),
1840
+ v.literal("unsuccessful"),
1841
+ v.literal("mixed"),
1842
+ v.literal("unknown")
1843
+ );
1844
+ var externalIds2 = v.object({
1845
+ crunchbase: v.optional(v.string()),
1846
+ linkedin: v.optional(v.string()),
1847
+ pitchbook: v.optional(v.string()),
1848
+ twitter: v.optional(v.string()),
1849
+ website: v.optional(v.string())
1850
+ });
1851
+ defineTable({
1852
+ // === IDENTITY ===
1853
+ globalId: v.string(),
1854
+ // UUID - survives migration to Neo4j
1855
+ // === TYPE ===
1856
+ nodeType,
1857
+ // === EPISTEMIC LAYER ===
1858
+ epistemicLayer: v.optional(epistemicLayer),
1859
+ // === SUBTYPE (for typed entities) ===
1860
+ subtype: v.optional(v.string()),
1861
+ // company: private|corporate|portfolio, investor: vc|lp|cvc|pe|family_office|angel
1862
+ // === CONTENT ===
1863
+ canonicalText: v.string(),
1864
+ // The core content (belief statement, company name, etc.)
1865
+ contentHash: v.string(),
1866
+ // SHA256(nodeType + canonicalText) for deduplication
1867
+ // Extended content (for sources/syntheses)
1868
+ content: v.optional(v.string()),
1869
+ // Full text for documents/articles
1870
+ contentType: v.optional(v.string()),
1871
+ // "markdown", "html", "pdf", "text"
1872
+ // === METADATA ===
1873
+ title: v.optional(v.string()),
1874
+ // Display title
1875
+ tags: v.optional(v.array(v.string())),
1876
+ domain: v.optional(v.string()),
1877
+ // For companies: website domain
1878
+ // Type-specific metadata (flexible object - LEGACY)
1879
+ // New code should use the typed fields below when available
1880
+ metadata: v.optional(looseJsonObject),
1881
+ // === POLICY / ENTITLEMENT ===
1882
+ tenantId: v.optional(v.string()),
1883
+ workspaceId: v.optional(v.string()),
1884
+ ownerPrincipalId: v.optional(v.string()),
1885
+ audienceLabel: v.optional(audienceLabel),
1886
+ policyTags: v.optional(v.array(v.string())),
1887
+ sensitivityTier: v.optional(sensitivityTier),
1888
+ exportClass: v.optional(exportClass),
1889
+ anonymizationClass: v.optional(anonymizationClass),
1890
+ // === PUBLICATION (visibility-based, not copy-based) ===
1891
+ // Publication expands who can see a workspace-local node — the node stays
1892
+ // in its workspace, like a microservice exposing part of its API surface.
1893
+ // Rules-based: pack/tenant-level publicationRules auto-evaluate on
1894
+ // confidence changes and node creation. No manual click-by-click.
1895
+ publicationStatus: v.optional(
1896
+ v.union(
1897
+ v.literal("unpublished"),
1898
+ // Default: workspace-local only
1899
+ v.literal("published"),
1900
+ // Visible at tenant scope (rules matched)
1901
+ v.literal("suppressed")
1902
+ // Manually blocked even if rules match
1903
+ )
1904
+ ),
1905
+ publishedAt: v.optional(v.number()),
1906
+ // When publication status last changed to published
1907
+ publishedBy: v.optional(v.string()),
1908
+ // userId or "system:publication_rules" for auto-publish
1909
+ // === TYPED METADATA FIELDS ===
1910
+ // --- Belief ---
1911
+ // Belief type — validated against schemaEnumConfig category "belief_type"
1912
+ // Platform core: hypothesis, belief, principle, invariant, assumption,
1913
+ // tenet, prior, preference, goal, forecast
1914
+ beliefType: v.optional(v.string()),
1915
+ beliefStatus: v.optional(beliefStatus),
1916
+ epistemicStatus: v.optional(epistemicStatus),
1917
+ reversibility: v.optional(reversibility),
1918
+ predictionMeta: v.optional(predictionMeta),
1919
+ // Consensus tracking (for non-consensus detection)
1920
+ consensusView: v.optional(consensusView),
1921
+ consensusConfidence: v.optional(v.number()),
1922
+ // 0-1: What we think consensus confidence is
1923
+ consensusSource: v.optional(v.string()),
1924
+ // Where we got the consensus view (twitter, reports, etc.)
1925
+ // --- Evidence ---
1926
+ methodology: v.optional(methodology),
1927
+ informationAsymmetry: v.optional(informationAsymmetry),
1928
+ temporalNature: v.optional(temporalNature),
1929
+ // --- Question ---
1930
+ questionType: v.optional(questionType),
1931
+ questionPriority: v.optional(questionPriority),
1932
+ answerQuality: v.optional(answerQuality),
1933
+ // --- Theme ---
1934
+ themeConviction: v.optional(themeConviction),
1935
+ // Market timing (for "early on theme" detection)
1936
+ marketAwarenessDate: v.optional(v.number()),
1937
+ // When this theme became broadly discussed
1938
+ marketAwarenessSource: v.optional(v.string()),
1939
+ // How we know (first major report, twitter volume spike, etc.)
1940
+ earlySignalIds: v.optional(v.array(v.string())),
1941
+ // globalIds of evidence we had before market awareness
1942
+ // --- Decision ---
1943
+ decisionType: v.optional(decisionType),
1944
+ decisionOutcome: v.optional(decisionOutcome),
1945
+ // === EXTERNAL IDS (for ontological entities) ===
1946
+ externalIds: v.optional(externalIds2),
1947
+ // === PROVENANCE ===
1948
+ sourceType: sourceType2,
1949
+ aiProvider: v.optional(v.string()),
1950
+ // "claude", "gemini", "gpt-4", etc.
1951
+ extractedFromNodeId: v.optional(v.id("epistemicNodes")),
1952
+ // Quick reference to source
1953
+ // === EXTRACTION CONTEXT ===
1954
+ extractionModel: v.optional(v.string()),
1955
+ // "claude-sonnet-4-20250514"
1956
+ extractionPromptName: v.optional(v.string()),
1957
+ // "lucern/extract-evidence"
1958
+ extractionPromptVersion: v.optional(v.number()),
1959
+ extractionTemperature: v.optional(v.number()),
1960
+ extractionLangfuseTraceId: v.optional(v.string()),
1961
+ // === GROUNDING VERIFICATION ===
1962
+ groundingVerified: v.optional(v.boolean()),
1963
+ groundingConfidence: v.optional(v.number()),
1964
+ // 0-1 match quality
1965
+ groundingMatchedText: v.optional(v.string()),
1966
+ // Actual text from source
1967
+ groundingStartOffset: v.optional(v.number()),
1968
+ groundingEndOffset: v.optional(v.number()),
1969
+ groundingRejectionReason: v.optional(v.string()),
1970
+ // === CONFIDENCE & VERIFICATION ===
1971
+ confidence: v.optional(v.number()),
1972
+ // 0-1 projected probability P(x) = b + a*u
1973
+ verificationStatus: v.optional(verificationStatus),
1974
+ // === SL OPINION (Subjective Logic — Kernel v2) ===
1975
+ // Replaces scalar confidence with rich epistemic state.
1976
+ // b + d + u = 1. P(x) = b + a*u is stored in `confidence` for backward compat.
1977
+ opinion_b: v.optional(v.number()),
1978
+ // Belief: evidence FOR (0-1)
1979
+ opinion_d: v.optional(v.number()),
1980
+ // Disbelief: evidence AGAINST (0-1)
1981
+ opinion_u: v.optional(v.number()),
1982
+ // Uncertainty: absence of evidence (0-1)
1983
+ opinion_a: v.optional(v.number()),
1984
+ // Base rate / prior probability (0-1)
1985
+ tupleContradicted: v.optional(v.boolean()),
1986
+ // Single-belief tuple-space contradiction flag
1987
+ // === LIFECYCLE ===
1988
+ status: nodeStatus,
1989
+ supersededBy: v.optional(v.id("epistemicNodes")),
1990
+ // === OWNERSHIP ===
1991
+ topicId: v.optional(v.string()),
1992
+ // Canonical scope container (topic-first model)
1993
+ projectId: v.optional(v.string()),
1994
+ // DEPRECATED: Use belongs_to edges
1995
+ createdBy: v.string(),
1996
+ // Clerk user ID
1997
+ createdAt: v.number(),
1998
+ updatedAt: v.number(),
1999
+ // === NEO4J SYNC STATUS ===
2000
+ syncStatus: v.optional(syncStatus),
2001
+ syncError: v.optional(v.string())
2002
+ // Error message if sync failed
2003
+ }).index("by_globalId", ["globalId"]).index("by_contentHash", ["contentHash"]).index("by_nodeType", ["nodeType"]).index("by_subtype", ["nodeType", "subtype"]).index("by_domain", ["domain"]).index("by_project", ["projectId"]).index("by_project_type", ["projectId", "nodeType"]).index("by_topic", ["topicId"]).index("by_topic_type", ["topicId", "nodeType"]).index("by_tenantId", ["tenantId"]).index("by_workspaceId", ["workspaceId"]).index("by_tenant_workspace", ["tenantId", "workspaceId"]).index("by_audienceLabel", ["audienceLabel"]).index("by_sensitivityTier", ["sensitivityTier"]).index("by_exportClass", ["exportClass"]).index("by_status", ["status"]).index("by_sourceType", ["sourceType"]).index("by_verification", ["verificationStatus"]).index("by_layer", ["epistemicLayer"]).index("by_layer_type", ["epistemicLayer", "nodeType"]).index("by_syncStatus", ["syncStatus"]).index("by_publicationStatus", ["publicationStatus"]).index("by_tenant_publicationStatus", ["tenantId", "publicationStatus"]).index("by_belief_status", ["nodeType", "beliefStatus"]).index("by_epistemic_status", ["nodeType", "epistemicStatus"]).index("by_temporal_nature", ["nodeType", "temporalNature"]).index("by_methodology", ["nodeType", "methodology"]).index("by_reversibility", ["nodeType", "reversibility"]).index("by_questionType", ["nodeType", "questionType"]).index("by_questionPriority", ["nodeType", "questionPriority"]).searchIndex("search_canonicalText", {
2004
+ searchField: "canonicalText",
2005
+ filterFields: ["nodeType", "projectId", "topicId", "status"]
2006
+ });
2007
+ function getLayerForNodeType(type) {
2008
+ switch (type) {
2009
+ case "decision":
2010
+ return "L4";
2011
+ case "belief":
2012
+ case "question":
2013
+ case "theme":
2014
+ case "deal":
2015
+ return "L3";
2016
+ case "claim":
2017
+ case "evidence":
2018
+ case "synthesis":
2019
+ case "answer":
2020
+ return "L2";
2021
+ case "atomic_fact":
2022
+ case "excerpt":
2023
+ case "source":
2024
+ return "L1";
2025
+ case "topic":
2026
+ return "organizational";
2027
+ case "company":
2028
+ case "person":
2029
+ case "investor":
2030
+ case "function":
2031
+ case "value_chain":
2032
+ return "ontological";
2033
+ }
2034
+ }
2035
+
2036
+ // src/workspaceIsolation.ts
2037
+ function normalizeScopeValue2(value) {
2038
+ if (typeof value !== "string") {
2039
+ return;
2040
+ }
2041
+ const normalized = value.trim();
2042
+ return normalized.length > 0 ? normalized : void 0;
2043
+ }
2044
+ function throwWorkspaceIsolationError(args) {
2045
+ const error = new Error(args.message);
2046
+ error.status = 409;
2047
+ error.code = "INVARIANT_VIOLATION";
2048
+ error.invariantCode = args.invariantCode;
2049
+ error.suggestion = args.suggestion;
2050
+ error.details = args.details;
2051
+ throw error;
2052
+ }
2053
+ function assertWorkspaceScopedEpistemicNodeScope(args) {
2054
+ const layer = getLayerForNodeType(args.nodeType);
2055
+ if (layer === "ontological") {
2056
+ return;
2057
+ }
2058
+ const workspaceId = normalizeScopeValue2(args.scope.workspaceId);
2059
+ if (workspaceId) {
2060
+ return;
2061
+ }
2062
+ throwWorkspaceIsolationError({
2063
+ message: "Workspace-scoped reasoning isolation requires workspaceId on non-ontological node creation.",
2064
+ invariantCode: "workspace.scope_required_for_epistemic_nodes",
2065
+ suggestion: "Resolve the topic/project scope through a workspace-bound topic before creating epistemic nodes.",
2066
+ details: {
2067
+ mutationName: args.mutationName,
2068
+ nodeType: args.nodeType,
2069
+ topicId: args.scope.topicId,
2070
+ projectId: args.scope.projectId
2071
+ }
2072
+ });
2073
+ }
2074
+ function nodeMatchesWorkspaceReasoningScope(node, scope) {
2075
+ if (!node) {
2076
+ return false;
2077
+ }
2078
+ const scopeTenantId = normalizeScopeValue2(scope.tenantId);
2079
+ const scopeWorkspaceId = normalizeScopeValue2(scope.workspaceId);
2080
+ const nodeTenantId = normalizeScopeValue2(node.tenantId);
2081
+ const nodeWorkspaceId = normalizeScopeValue2(node.workspaceId);
2082
+ const epistemicLayer2 = typeof node.epistemicLayer === "string" ? node.epistemicLayer : void 0;
2083
+ if (scopeTenantId && nodeTenantId && scopeTenantId !== nodeTenantId) {
2084
+ return false;
2085
+ }
2086
+ if (epistemicLayer2 === "ontological" && nodeWorkspaceId === void 0) {
2087
+ return true;
2088
+ }
2089
+ if (!scopeWorkspaceId && node.publicationStatus === "published") {
2090
+ return true;
2091
+ }
2092
+ if (!scopeWorkspaceId) {
2093
+ return nodeWorkspaceId === void 0;
2094
+ }
2095
+ return scopeWorkspaceId === nodeWorkspaceId;
2096
+ }
2097
+ async function resolveNodeScopeForWorkspaceIsolation(ctx, node) {
2098
+ const epistemicLayer2 = typeof node?.epistemicLayer === "string" ? node.epistemicLayer : void 0;
2099
+ const resolved = {
2100
+ tenantId: normalizeScopeValue2(node?.tenantId),
2101
+ workspaceId: normalizeScopeValue2(node?.workspaceId),
2102
+ epistemicLayer: epistemicLayer2,
2103
+ nodeType: typeof node?.nodeType === "string" ? node.nodeType : void 0
2104
+ };
2105
+ if (!node) {
2106
+ return resolved;
2107
+ }
2108
+ if (resolved.epistemicLayer === "ontological") {
2109
+ return resolved;
2110
+ }
2111
+ if (resolved.tenantId || resolved.workspaceId) {
2112
+ return resolved;
2113
+ }
2114
+ if (node.topicId) {
2115
+ const topicScope = await resolveTopicProjectScope(ctx, {
2116
+ topicId: node.topicId
2117
+ });
2118
+ return {
2119
+ ...resolved,
2120
+ tenantId: topicScope.tenantId,
2121
+ workspaceId: topicScope.workspaceId
2122
+ };
2123
+ }
2124
+ if (node.projectId) {
2125
+ const topicScope = await resolveTopicProjectScope(ctx, {
2126
+ projectId: String(node.projectId)
2127
+ });
2128
+ return {
2129
+ ...resolved,
2130
+ tenantId: topicScope.tenantId,
2131
+ workspaceId: topicScope.workspaceId
2132
+ };
2133
+ }
2134
+ return resolved;
2135
+ }
2136
+ function resolveRuntimePackMutationContext(args) {
2137
+ if (!args.runtimeToolName && !args.runtimePackKey && !args.runtimePackInstallScope) {
2138
+ return;
2139
+ }
2140
+ return {
2141
+ toolName: args.runtimeToolName,
2142
+ packKey: args.runtimePackKey,
2143
+ packInstallScope: args.runtimePackInstallScope
2144
+ };
2145
+ }
2146
+ function assertTenantPackWorkspaceMutationAllowed(args) {
2147
+ if (!args.runtime?.packKey || args.runtime.packInstallScope !== "tenant") {
2148
+ return;
2149
+ }
2150
+ const targetWorkspaceId = normalizeScopeValue2(args.target.workspaceId);
2151
+ const targetLayer = typeof args.target.epistemicLayer === "string" ? args.target.epistemicLayer : void 0;
2152
+ if (!targetWorkspaceId || targetLayer === "ontological") {
2153
+ return;
2154
+ }
2155
+ throwWorkspaceIsolationError({
2156
+ message: `Tenant-scoped pack "${args.runtime.packKey}" cannot mutate workspace-scoped reasoning state.`,
2157
+ invariantCode: "workspace.tenant_pack_reasoning_write_forbidden",
2158
+ suggestion: "Use a workspace-scoped pack for workspace-local graph mutations, or route the change through tenant-global canonical entity flows.",
2159
+ details: {
2160
+ mutationName: args.mutationName,
2161
+ toolName: args.runtime.toolName,
2162
+ packKey: args.runtime.packKey,
2163
+ targetWorkspaceId,
2164
+ targetNodeType: args.target.nodeType,
2165
+ targetLayer
2166
+ }
2167
+ });
2168
+ }
2169
+
2170
+ // ../worktrees/src/v1/engine/scopeBridge.ts
2171
+ function normalizeString2(value) {
2172
+ if (typeof value !== "string") {
2173
+ return void 0;
2174
+ }
2175
+ const trimmed = value.trim();
2176
+ return trimmed.length > 0 ? trimmed : void 0;
2177
+ }
2178
+ function requireScopeId(...ids) {
2179
+ for (const id of ids) {
2180
+ const normalized = normalizeString2(id);
2181
+ if (normalized) {
2182
+ return normalized;
2183
+ }
2184
+ }
2185
+ throw new Error("No scope identifier provided (topicId or projectId required)");
2186
+ }
2187
+ async function resolveTopicProjectScope2(ctx, args) {
2188
+ const resolved = await resolveTopicProjectScope(ctx, {
2189
+ topicId: normalizeString2(args.topicId),
2190
+ projectId: normalizeString2(args.projectId)
2191
+ });
2192
+ const topicId = normalizeString2(resolved.topicId);
2193
+ const projectId = requireScopeId(
2194
+ resolved.projectId,
2195
+ args.projectId,
2196
+ topicId
2197
+ );
2198
+ return { projectId, ...topicId ? { topicId } : {} };
2199
+ }
2200
+
2201
+ // ../worktrees/src/v1/engine/worktreeWorkflowBridge.ts
2202
+ function isLegacySprintDoc(doc) {
2203
+ if (!doc || typeof doc !== "object") {
2204
+ return false;
2205
+ }
2206
+ if ("metadata" in doc && doc.metadata?.pairedSprintId) {
2207
+ return false;
2208
+ }
2209
+ return "sprintScope" in doc || "targetPillar" in doc || "pillarThesis" in doc || "synthesisState" in doc || "projectId" in doc && !("worktreeScope" in doc);
2210
+ }
2211
+ function getPairedSprintId(doc) {
2212
+ const pairedSprintId = doc?.metadata?.pairedSprintId;
2213
+ return typeof pairedSprintId === "string" && pairedSprintId.trim().length > 0 ? pairedSprintId : null;
2214
+ }
2215
+ function getStringField(doc, field) {
2216
+ const value = doc?.[field];
2217
+ return typeof value === "string" && value.trim().length > 0 ? value : void 0;
2218
+ }
2219
+ async function findPairedWorktreeForSprint(ctx, sprint) {
2220
+ const sprintId = typeof sprint._id === "string" && sprint._id.length > 0 ? sprint._id : null;
2221
+ if (!sprintId || typeof ctx.db.query !== "function") {
2222
+ return null;
2223
+ }
2224
+ let topicId = getStringField(sprint, "topicId");
2225
+ if (!topicId) {
2226
+ try {
2227
+ const scope = await resolveTopicProjectScope2(ctx, {
2228
+ topicId: getStringField(sprint, "topicId"),
2229
+ projectId: getStringField(sprint, "projectId")
2230
+ });
2231
+ topicId = scope.topicId;
2232
+ } catch {
2233
+ topicId = void 0;
2234
+ }
2235
+ }
2236
+ if (!topicId) {
2237
+ return null;
2238
+ }
2239
+ const worktrees = await ctx.db.query("worktrees").withIndex("by_topicId", (q) => q.eq("topicId", topicId)).collect();
2240
+ return worktrees.find(
2241
+ (worktree) => String(worktree?.metadata?.pairedSprintId || "") === sprintId
2242
+ ) ?? null;
2243
+ }
2244
+ async function resolveWorkflowBridgeDoc(ctx, workflowId) {
2245
+ const empty = {
2246
+ inputId: workflowId ?? null,
2247
+ sprint: null,
2248
+ worktree: null,
2249
+ sprintId: null,
2250
+ worktreeId: null
2251
+ };
2252
+ if (!workflowId) {
2253
+ return empty;
2254
+ }
2255
+ let doc;
2256
+ try {
2257
+ doc = await ctx.db.get(workflowId);
2258
+ } catch {
2259
+ return empty;
2260
+ }
2261
+ if (!doc || typeof doc !== "object") {
2262
+ return empty;
2263
+ }
2264
+ if (isLegacySprintDoc(doc)) {
2265
+ const worktree2 = await findPairedWorktreeForSprint(ctx, doc);
2266
+ return {
2267
+ ...empty,
2268
+ sprint: doc,
2269
+ worktree: worktree2,
2270
+ sprintId: typeof doc._id === "string" && doc._id.length > 0 ? doc._id : workflowId,
2271
+ worktreeId: worktree2 && typeof worktree2._id === "string" && worktree2._id.length > 0 ? worktree2._id : null,
2272
+ topicId: getStringField(worktree2, "topicId") ?? getStringField(doc, "topicId"),
2273
+ projectId: getStringField(doc, "projectId") ?? getStringField(worktree2, "projectId")
2274
+ };
2275
+ }
2276
+ const worktree = doc;
2277
+ const pairedSprintId = getPairedSprintId(worktree);
2278
+ let sprint = null;
2279
+ if (pairedSprintId) {
2280
+ try {
2281
+ const paired = await ctx.db.get(pairedSprintId);
2282
+ if (isLegacySprintDoc(paired)) {
2283
+ sprint = paired;
2284
+ }
2285
+ } catch {
2286
+ sprint = null;
2287
+ }
2288
+ }
2289
+ return {
2290
+ ...empty,
2291
+ worktree,
2292
+ sprint,
2293
+ worktreeId: typeof worktree._id === "string" && worktree._id.length > 0 ? worktree._id : workflowId,
2294
+ sprintId: sprint && typeof sprint._id === "string" && sprint._id.length > 0 ? sprint._id : pairedSprintId,
2295
+ topicId: getStringField(worktree, "topicId") ?? getStringField(sprint, "topicId"),
2296
+ projectId: getStringField(sprint, "projectId") ?? getStringField(worktree, "projectId")
2297
+ };
2298
+ }
2299
+
2300
+ // src/epistemicQuestions.ts
2301
+ function generateContentHash(text) {
2302
+ const content2 = `question:${text.trim().toLowerCase().replace(/\s+/g, " ")}`;
2303
+ let hash = 5381;
2304
+ for (let i = 0; i < content2.length; i++) {
2305
+ hash = (hash << 5) + hash + content2.charCodeAt(i);
2306
+ hash &= hash;
2307
+ }
2308
+ return Math.abs(hash).toString(16).padStart(8, "0");
2309
+ }
2310
+ function buildTestsEdgeGlobalId(fromGlobalId, toGlobalId) {
2311
+ return `edge-${fromGlobalId}-tests-${toGlobalId}`;
2312
+ }
2313
+ async function markProjectGraphDirty(ctx, projectId, topicId) {
2314
+ const normalizedProjectId = typeof projectId === "string" && projectId.trim().length > 0 ? projectId : void 0;
2315
+ const normalizedTopicId = typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
2316
+ if (!normalizedProjectId && !normalizedTopicId) {
2317
+ return;
2318
+ }
2319
+ if (normalizedProjectId) {
2320
+ await ctx.scheduler.runAfter(
2321
+ 0,
2322
+ internal.graphAnalysisCache.markCacheStaleInternal,
2323
+ {
2324
+ projectId: normalizedProjectId
2325
+ }
2326
+ );
2327
+ }
2328
+ if (normalizedTopicId) {
2329
+ await ctx.scheduler.runAfter(
2330
+ 0,
2331
+ internal.graphAnalysisCache.markCacheStaleByTopic,
2332
+ {
2333
+ topicId: normalizedTopicId
2334
+ }
2335
+ );
2336
+ }
2337
+ await resolveGraphPrimitivesAppResolvers().patchProject(
2338
+ ctx,
2339
+ normalizedTopicId ?? normalizedProjectId,
2340
+ {
2341
+ lastActivityAt: Date.now()
2342
+ }
2343
+ );
2344
+ }
2345
+ function normalizeCategory(category) {
2346
+ if (!category) {
2347
+ return "other";
2348
+ }
2349
+ const lower = category.toLowerCase();
2350
+ if (lower === "financial") {
2351
+ return "financials";
2352
+ }
2353
+ const validCategories = [
2354
+ "market",
2355
+ "competition",
2356
+ "product",
2357
+ "team",
2358
+ "financials",
2359
+ "deal",
2360
+ "risks"
2361
+ ];
2362
+ return validCategories.find((c) => lower.includes(c)) || "other";
2363
+ }
2364
+ var DEFAULT_QUESTION_PAGE_SIZE = 250;
2365
+ var MAX_QUESTION_PAGE_SIZE = 1e3;
2366
+ function clampQuestionLimit(limit, fallback = DEFAULT_QUESTION_PAGE_SIZE) {
2367
+ if (!Number.isFinite(limit)) {
2368
+ return fallback;
2369
+ }
2370
+ return Math.max(
2371
+ 1,
2372
+ Math.min(Math.floor(limit), MAX_QUESTION_PAGE_SIZE)
2373
+ );
2374
+ }
2375
+ function dedupeQuestionNodes(nodes) {
2376
+ const seen = /* @__PURE__ */ new Set();
2377
+ const deduped = [];
2378
+ for (const node of nodes) {
2379
+ const id = String(node._id);
2380
+ if (seen.has(id)) {
2381
+ continue;
2382
+ }
2383
+ seen.add(id);
2384
+ deduped.push(node);
2385
+ }
2386
+ return deduped;
2387
+ }
2388
+ function normalizeQuestionTopicId(topicId) {
2389
+ return typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
2390
+ }
2391
+ function resolveQuestionScopeId(scope) {
2392
+ return normalizeQuestionTopicId(scope.topicId) ?? scope.projectId ?? void 0;
2393
+ }
2394
+ async function resolveQuestionScopeOrNull(ctx, args) {
2395
+ if (!args.projectId && !args.topicId) {
2396
+ return null;
2397
+ }
2398
+ try {
2399
+ return await resolveTopicProjectScope(ctx, {
2400
+ projectId: args.projectId ?? void 0,
2401
+ topicId: args.topicId ?? void 0
2402
+ });
2403
+ } catch {
2404
+ return null;
2405
+ }
2406
+ }
2407
+ async function getQuestionNodesForScope(ctx, scope, args) {
2408
+ const fetchNodes = (query2) => typeof args?.scanLimit === "number" ? query2.order("desc").take(args.scanLimit) : query2.collect();
2409
+ const [topicNodes, projectNodes] = await Promise.all([
2410
+ scope.topicId ? fetchNodes(
2411
+ ctx.db.query("epistemicNodes").withIndex(
2412
+ "by_topic_type",
2413
+ (q) => q.eq("topicId", scope.topicId).eq("nodeType", "question")
2414
+ )
2415
+ ) : Promise.resolve([]),
2416
+ scope.projectId ? fetchNodes(
2417
+ ctx.db.query("epistemicNodes").withIndex(
2418
+ "by_project_type",
2419
+ (q) => q.eq("projectId", scope.projectId).eq("nodeType", "question")
2420
+ )
2421
+ ) : Promise.resolve([])
2422
+ ]);
2423
+ return dedupeQuestionNodes([...topicNodes, ...projectNodes]).filter(
2424
+ (node) => questionMatchesScope(node, scope)
2425
+ );
2426
+ }
2427
+ async function getQuestionEdgesForScope(ctx, scope) {
2428
+ const query2 = ctx.db.query("epistemicEdges").withIndex(
2429
+ "by_topic",
2430
+ (q) => q.eq("topicId", scope.topicId || scope.projectId)
2431
+ );
2432
+ return await query2.collect();
2433
+ }
2434
+ function questionMatchesScope(node, scope) {
2435
+ return scope.topicId !== void 0 && node.topicId === scope.topicId || scope.projectId !== void 0 && node.projectId === scope.projectId;
2436
+ }
2437
+ function resolveLinkedWorktreeId(meta) {
2438
+ const linkedWorktreeId = meta.linkedWorktreeId;
2439
+ return typeof linkedWorktreeId === "string" && linkedWorktreeId.trim() ? linkedWorktreeId : null;
2440
+ }
2441
+ function buildLinkedWorktreeMetadata(linkedWorktreeId) {
2442
+ return linkedWorktreeId ? {
2443
+ linkedWorktreeId
2444
+ } : {};
2445
+ }
2446
+ function questionMatchesWorkflowLink(meta, workflow) {
2447
+ const linkedWorktreeId = resolveLinkedWorktreeId(meta);
2448
+ return Boolean(
2449
+ linkedWorktreeId && (linkedWorktreeId === workflow.worktreeId || linkedWorktreeId === workflow.sprintId)
2450
+ );
2451
+ }
2452
+ function flattenQuestionNode(n) {
2453
+ const meta = n.metadata || {};
2454
+ const linkedWorktreeId = resolveLinkedWorktreeId(meta);
2455
+ return {
2456
+ ...n,
2457
+ question: n.canonicalText || "",
2458
+ category: meta.category || "other",
2459
+ priority: meta.priority || "medium",
2460
+ status: meta.questionStatus || meta.status || "open",
2461
+ beliefId: meta.linkedBeliefNodeId || meta.beliefId || null,
2462
+ relatedBeliefIds: meta.relatedBeliefIds || [],
2463
+ relatedInsightIds: meta.relatedInsightIds || [],
2464
+ linkedWorktreeId,
2465
+ sprintIndex: meta.sprintIndex || void 0,
2466
+ linkedBeliefId: meta.linkedBeliefNodeId || null,
2467
+ testType: meta.testType || "validates",
2468
+ importance: meta.importance || 5,
2469
+ isKeyQuestion: meta.isKeyQuestion || false,
2470
+ answer: meta.answer || null,
2471
+ convictionStage: meta.convictionStage || null,
2472
+ conviction: meta.conviction ?? null,
2473
+ source: meta.source || "ai_suggested"
2474
+ };
2475
+ }
2476
+ function flattenInternalQuestionNode(n) {
2477
+ const meta = n.metadata || {};
2478
+ const flattened = flattenQuestionNode(n);
2479
+ return {
2480
+ ...flattened,
2481
+ _epistemicNodeId: n._id,
2482
+ questionType: n.questionType || meta.questionType || flattened.testType,
2483
+ linkedBeliefIds: flattened.relatedBeliefIds,
2484
+ audienceLabel: n.audienceLabel,
2485
+ policyTags: n.policyTags,
2486
+ sensitivityTier: n.sensitivityTier,
2487
+ exportClass: n.exportClass,
2488
+ anonymizationClass: n.anonymizationClass
2489
+ };
2490
+ }
2491
+ var INACTIVE_NODE_STATUSES = /* @__PURE__ */ new Set(["archived", "superseded", "deleted"]);
2492
+ var INACTIVE_METADATA_STATUSES = /* @__PURE__ */ new Set([
2493
+ "archived",
2494
+ "superseded",
2495
+ "deleted"
2496
+ ]);
2497
+ function getQuestionStatusCandidates(node) {
2498
+ const meta = node.metadata || {};
2499
+ return [meta.questionStatus, meta.status, node.status].filter(
2500
+ (value) => typeof value === "string"
2501
+ );
2502
+ }
2503
+ function isActiveQuestionNode(node) {
2504
+ if (INACTIVE_NODE_STATUSES.has(node.status)) {
2505
+ return false;
2506
+ }
2507
+ return !getQuestionStatusCandidates(node).some(
2508
+ (status) => INACTIVE_METADATA_STATUSES.has(status.toLowerCase())
2509
+ );
2510
+ }
2511
+ function matchesRequestedQuestionStatus(node, requestedStatus) {
2512
+ const normalizedStatus = requestedStatus.toLowerCase();
2513
+ return getQuestionStatusCandidates(node).some(
2514
+ (status) => status.toLowerCase() === normalizedStatus
2515
+ );
2516
+ }
2517
+ var create = mutation({
2518
+ args: {
2519
+ ...optionalScopeArgs,
2520
+ question: v.string(),
2521
+ category: v.optional(v.string()),
2522
+ priority: v.optional(
2523
+ v.union(v.literal("high"), v.literal("medium"), v.literal("low"))
2524
+ ),
2525
+ source: v.optional(v.string()),
2526
+ userId: v.string(),
2527
+ // Optional linking
2528
+ linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
2529
+ testType: v.optional(
2530
+ v.union(
2531
+ v.literal("validates"),
2532
+ v.literal("invalidates"),
2533
+ v.literal("clarifies")
2534
+ )
2535
+ ),
2536
+ // Optional metadata
2537
+ importance: v.optional(v.number()),
2538
+ epistemicUnlock: v.optional(v.string()),
2539
+ // === SOURCE QUESTIONS (for key question derivation) ===
2540
+ // When a key question is synthesized from raw questions, track the source
2541
+ sourceQuestionIds: v.optional(v.array(v.string())),
2542
+ // globalIds of source questions
2543
+ // === WORKTREE LINKAGE ===
2544
+ linkedWorktreeId: v.optional(v.string()),
2545
+ // === CLASSIFICATION FIELDS ===
2546
+ questionType: v.optional(
2547
+ v.union(
2548
+ v.literal("validation"),
2549
+ v.literal("falsification"),
2550
+ v.literal("assumption_probe"),
2551
+ v.literal("prediction_test"),
2552
+ v.literal("counterfactual"),
2553
+ v.literal("discovery"),
2554
+ v.literal("clarification"),
2555
+ v.literal("comparison"),
2556
+ v.literal("causal"),
2557
+ v.literal("mechanism"),
2558
+ v.literal("general")
2559
+ )
2560
+ ),
2561
+ questionPriority: v.optional(
2562
+ v.union(
2563
+ v.literal("critical"),
2564
+ v.literal("high"),
2565
+ v.literal("medium"),
2566
+ v.literal("low")
2567
+ )
2568
+ )
2569
+ },
2570
+ returns: permissiveReturn,
2571
+ handler: async (ctx, args) => {
2572
+ const scope = await resolveTopicProjectScope(ctx, {
2573
+ topicId: args.topicId,
2574
+ projectId: args.projectId
2575
+ });
2576
+ assertWorkspaceScopedEpistemicNodeScope({
2577
+ scope,
2578
+ nodeType: "question",
2579
+ mutationName: "epistemicQuestions.create"
2580
+ });
2581
+ if (scope.projectId) {
2582
+ await checkProjectAccess(ctx, scope.projectId, args.userId);
2583
+ }
2584
+ const now = Date.now();
2585
+ const globalId = generateGlobalId();
2586
+ const contentHash = generateContentHash(args.question);
2587
+ const category = normalizeCategory(args.category);
2588
+ const nodeId = await ctx.db.insert("epistemicNodes", {
2589
+ globalId,
2590
+ topicId: scope.topicId,
2591
+ projectId: scope.projectId,
2592
+ tenantId: scope.tenantId,
2593
+ workspaceId: scope.workspaceId,
2594
+ nodeType: "question",
2595
+ canonicalText: args.question,
2596
+ contentHash,
2597
+ status: "active",
2598
+ epistemicLayer: "L3",
2599
+ // L3: Traversal Anchors (Questions)
2600
+ sourceType: args.source === "user" ? "human" : "ai_generated",
2601
+ // Classification fields
2602
+ questionType: args.questionType ?? "general",
2603
+ questionPriority: args.questionPriority ?? "medium",
2604
+ answerQuality: "unanswered",
2605
+ // New questions start unanswered
2606
+ createdAt: now,
2607
+ updatedAt: now,
2608
+ createdBy: args.userId,
2609
+ metadata: {
2610
+ category,
2611
+ priority: args.priority || "medium",
2612
+ source: args.source || "ai_suggested",
2613
+ questionStatus: "open",
2614
+ linkedBeliefNodeId: args.linkedBeliefNodeId,
2615
+ ...buildLinkedWorktreeMetadata(
2616
+ args.linkedWorktreeId
2617
+ ),
2618
+ testType: args.testType,
2619
+ importance: args.importance,
2620
+ epistemicUnlock: args.epistemicUnlock,
2621
+ // Track source questions for key question derivation
2622
+ prerequisiteQuestions: args.sourceQuestionIds
2623
+ }
2624
+ });
2625
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
2626
+ nodeId,
2627
+ operation: "upsert"
2628
+ });
2629
+ await scheduleEmbeddingGeneration({
2630
+ ctx,
2631
+ nodeId,
2632
+ projectId: scope.projectId,
2633
+ topicId: scope.topicId,
2634
+ createdBy: args.userId,
2635
+ nodeType: "question",
2636
+ text: args.question
2637
+ });
2638
+ if (args.linkedBeliefNodeId) {
2639
+ const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
2640
+ if (beliefNode?.globalId) {
2641
+ await ctx.scheduler.runAfter(100, internal.neo4jSync.syncNodeToNeo4j, {
2642
+ nodeId: args.linkedBeliefNodeId,
2643
+ operation: "upsert"
2644
+ });
2645
+ const edgeGlobalId = buildTestsEdgeGlobalId(
2646
+ beliefNode.globalId,
2647
+ globalId
2648
+ );
2649
+ await ctx.scheduler.runAfter(250, internal.neo4jEdgeAPI.createEdge, {
2650
+ globalId: edgeGlobalId,
2651
+ fromGlobalId: beliefNode.globalId,
2652
+ toGlobalId: globalId,
2653
+ edgeType: "tests",
2654
+ weight: 1,
2655
+ context: args.testType || "tests",
2656
+ createdBy: args.userId,
2657
+ topicId: scope.projectId ? String(scope.projectId) : void 0,
2658
+ fromNodeType: "belief",
2659
+ toNodeType: "question",
2660
+ fromLayer: "L3",
2661
+ toLayer: "L3"
2662
+ });
2663
+ }
2664
+ }
2665
+ await ctx.db.insert("epistemicAudit", {
2666
+ entityType: "question",
2667
+ entityId: nodeId,
2668
+ changeType: "created",
2669
+ changedAt: now,
2670
+ changedBy: args.userId,
2671
+ isAgent: false,
2672
+ projectId: scope.projectId,
2673
+ newState: {
2674
+ question: args.question,
2675
+ category,
2676
+ priority: args.priority || "medium"
2677
+ }
2678
+ });
2679
+ if (scope.projectId || scope.topicId) {
2680
+ await ctx.scheduler.runAfter(
2681
+ 0,
2682
+ "embeddingActions:generateEpistemicNodeEmbedding",
2683
+ {
2684
+ nodeId,
2685
+ projectId: scope.projectId,
2686
+ topicId: scope.topicId ? String(scope.topicId) : void 0,
2687
+ createdBy: args.userId,
2688
+ nodeType: "question",
2689
+ text: args.question,
2690
+ hasAnswer: false
2691
+ }
2692
+ );
2693
+ }
2694
+ if (scope.projectId || scope.topicId) {
2695
+ await ctx.scheduler.runAfter(
2696
+ 2e3,
2697
+ // 2 second delay
2698
+ internal.nodeClassification.scheduleClassification,
2699
+ {
2700
+ nodeId,
2701
+ nodeType: "question",
2702
+ projectId: scope.projectId,
2703
+ topicId: normalizeQuestionTopicId(scope.topicId)
2704
+ }
2705
+ );
2706
+ }
2707
+ await markProjectGraphDirty(
2708
+ ctx,
2709
+ scope.projectId,
2710
+ normalizeQuestionTopicId(scope.topicId)
2711
+ );
2712
+ return { nodeId };
2713
+ }
2714
+ });
2715
+ var createBatch = mutation({
2716
+ args: {
2717
+ ...optionalScopeArgs,
2718
+ questions: v.array(
2719
+ v.object({
2720
+ question: v.string(),
2721
+ category: v.optional(v.string()),
2722
+ priority: v.optional(
2723
+ v.union(v.literal("high"), v.literal("medium"), v.literal("low"))
2724
+ ),
2725
+ linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
2726
+ testType: v.optional(
2727
+ v.union(
2728
+ v.literal("validates"),
2729
+ v.literal("invalidates"),
2730
+ v.literal("clarifies")
2731
+ )
2732
+ )
2733
+ })
2734
+ ),
2735
+ source: v.optional(v.string()),
2736
+ userId: v.string()
2737
+ },
2738
+ returns: permissiveReturn,
2739
+ handler: async (ctx, args) => {
2740
+ const scope = await resolveTopicProjectScope(ctx, {
2741
+ topicId: args.topicId,
2742
+ projectId: args.projectId
2743
+ });
2744
+ const scopeId = resolveQuestionScopeId(scope);
2745
+ if (!scopeId) {
2746
+ throw new Error("No scope identifier provided");
2747
+ }
2748
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
2749
+ if (!hasAccess) {
2750
+ throw new Error("Access denied");
2751
+ }
2752
+ const now = Date.now();
2753
+ const nodeIds = [];
2754
+ for (const q of args.questions) {
2755
+ const globalId = generateGlobalId();
2756
+ const contentHash = generateContentHash(q.question);
2757
+ const category = normalizeCategory(q.category);
2758
+ const nodeId = await ctx.db.insert("epistemicNodes", {
2759
+ globalId,
2760
+ topicId: scope.topicId,
2761
+ projectId: scope.projectId,
2762
+ tenantId: scope.tenantId,
2763
+ workspaceId: scope.workspaceId,
2764
+ nodeType: "question",
2765
+ canonicalText: q.question,
2766
+ contentHash,
2767
+ status: "active",
2768
+ epistemicLayer: "L3",
2769
+ sourceType: args.source === "user" ? "human" : "ai_generated",
2770
+ createdAt: now,
2771
+ updatedAt: now,
2772
+ createdBy: args.userId,
2773
+ metadata: {
2774
+ category,
2775
+ priority: q.priority || "medium",
2776
+ source: args.source || "ai_suggested",
2777
+ questionStatus: "open",
2778
+ linkedBeliefNodeId: q.linkedBeliefNodeId,
2779
+ testType: q.testType
2780
+ }
2781
+ });
2782
+ nodeIds.push(nodeId);
2783
+ await ctx.db.insert("epistemicAudit", {
2784
+ entityType: "question",
2785
+ entityId: String(nodeId),
2786
+ changeType: "created",
2787
+ changedAt: now,
2788
+ changedBy: args.userId,
2789
+ isAgent: false,
2790
+ projectId: scope.projectId,
2791
+ topicId: normalizeQuestionTopicId(scope.topicId),
2792
+ newState: {
2793
+ question: q.question,
2794
+ category,
2795
+ priority: q.priority || "medium",
2796
+ source: args.source || "ai_suggested",
2797
+ linkedBeliefNodeId: q.linkedBeliefNodeId,
2798
+ testType: q.testType
2799
+ },
2800
+ triggeringAction: "epistemicQuestions.createBatch"
2801
+ });
2802
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
2803
+ nodeId,
2804
+ operation: "upsert"
2805
+ });
2806
+ if (q.linkedBeliefNodeId) {
2807
+ const beliefNode = await ctx.db.get(q.linkedBeliefNodeId);
2808
+ if (beliefNode) {
2809
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
2810
+ globalId: crypto.randomUUID(),
2811
+ fromGlobalId: beliefNode.globalId,
2812
+ toGlobalId: globalId,
2813
+ edgeType: "tests",
2814
+ weight: 1,
2815
+ createdBy: args.userId,
2816
+ topicId: scope.projectId,
2817
+ fromNodeType: "belief",
2818
+ toNodeType: "question",
2819
+ fromLayer: "L3",
2820
+ toLayer: "L3"
2821
+ });
2822
+ }
2823
+ }
2824
+ }
2825
+ await markProjectGraphDirty(
2826
+ ctx,
2827
+ scope.projectId,
2828
+ normalizeQuestionTopicId(scope.topicId)
2829
+ );
2830
+ return { nodeIds, count: nodeIds.length };
2831
+ }
2832
+ });
2833
+ var updateStatus = mutation({
2834
+ args: {
2835
+ nodeId: v.optional(v.id("epistemicNodes")),
2836
+ // Backward compatibility for older callers still sending questionId.
2837
+ questionId: v.optional(v.union(v.id("epistemicNodes"), v.string())),
2838
+ status: v.union(
2839
+ v.literal("open"),
2840
+ v.literal("researching"),
2841
+ v.literal("answered"),
2842
+ v.literal("parked"),
2843
+ v.literal("closed")
2844
+ ),
2845
+ userId: v.optional(v.string()),
2846
+ answer: v.optional(v.string()),
2847
+ answerStatus: v.optional(
2848
+ v.union(
2849
+ v.literal("ai_draft"),
2850
+ v.literal("user_draft"),
2851
+ v.literal("final")
2852
+ )
2853
+ ),
2854
+ runtimeToolName: v.optional(v.string()),
2855
+ runtimePackKey: v.optional(v.string()),
2856
+ runtimePackInstallScope: v.optional(
2857
+ v.union(v.literal("tenant"), v.literal("workspace"))
2858
+ )
2859
+ },
2860
+ returns: permissiveReturn,
2861
+ handler: async (ctx, args) => {
2862
+ const resolvedNodeId = args.nodeId ?? args.questionId;
2863
+ if (!resolvedNodeId) {
2864
+ throw new Error("Missing nodeId/questionId");
2865
+ }
2866
+ const resolvedUserId = args.userId || await getCurrentUserId(ctx);
2867
+ if (!resolvedUserId) {
2868
+ throw new Error("Not authenticated");
2869
+ }
2870
+ const node = await ctx.db.get(resolvedNodeId);
2871
+ if (!node || node.nodeType !== "question") {
2872
+ throw new Error("Question not found");
2873
+ }
2874
+ assertTenantPackWorkspaceMutationAllowed({
2875
+ runtime: resolveRuntimePackMutationContext(args),
2876
+ target: await resolveNodeScopeForWorkspaceIsolation(ctx, node),
2877
+ mutationName: "epistemicQuestions.updateStatus"
2878
+ });
2879
+ const now = Date.now();
2880
+ const metadata = node.metadata || {};
2881
+ const previousStatus = metadata.questionStatus || "open";
2882
+ await ctx.db.patch(resolvedNodeId, {
2883
+ updatedAt: now,
2884
+ metadata: {
2885
+ ...metadata,
2886
+ questionStatus: args.status,
2887
+ answer: args.answer || metadata.answer,
2888
+ answerStatus: args.answerStatus || metadata.answerStatus,
2889
+ answeredAt: args.status === "answered" ? now : metadata.answeredAt,
2890
+ answeredBy: args.status === "answered" ? resolvedUserId : metadata.answeredBy
2891
+ }
2892
+ });
2893
+ if (args.status === "answered" && args.answer) {
2894
+ await ctx.scheduler.runAfter(
2895
+ 0,
2896
+ internal.epistemicAnswers.createInternal,
2897
+ {
2898
+ projectId: node.projectId,
2899
+ topicId: normalizeQuestionTopicId(node.topicId),
2900
+ questionNodeId: resolvedNodeId,
2901
+ answerText: args.answer,
2902
+ answerSource: "human",
2903
+ userId: resolvedUserId
2904
+ }
2905
+ );
2906
+ }
2907
+ await ctx.db.insert("epistemicAudit", {
2908
+ entityType: "question",
2909
+ entityId: resolvedNodeId,
2910
+ changeType: "status_changed",
2911
+ changedAt: now,
2912
+ changedBy: resolvedUserId,
2913
+ isAgent: false,
2914
+ projectId: node.projectId,
2915
+ previousState: { status: previousStatus },
2916
+ newState: { status: args.status }
2917
+ });
2918
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
2919
+ nodeId: resolvedNodeId,
2920
+ operation: "upsert"
2921
+ });
2922
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
2923
+ return { nodeId: resolvedNodeId, previousStatus, newStatus: args.status };
2924
+ }
2925
+ });
2926
+ var updatePriority = mutation({
2927
+ args: {
2928
+ nodeId: v.id("epistemicNodes"),
2929
+ priority: v.union(v.literal("high"), v.literal("medium"), v.literal("low")),
2930
+ userId: v.string()
2931
+ },
2932
+ returns: permissiveReturn,
2933
+ handler: async (ctx, args) => {
2934
+ const node = await ctx.db.get(args.nodeId);
2935
+ if (!node || node.nodeType !== "question") {
2936
+ throw new Error("Question not found");
2937
+ }
2938
+ const now = Date.now();
2939
+ const metadata = node.metadata || {};
2940
+ const previousPriority = metadata.priority || "medium";
2941
+ await ctx.db.patch(args.nodeId, {
2942
+ updatedAt: now,
2943
+ metadata: {
2944
+ ...metadata,
2945
+ priority: args.priority
2946
+ }
2947
+ });
2948
+ await ctx.db.insert("epistemicAudit", {
2949
+ entityType: "question",
2950
+ entityId: args.nodeId,
2951
+ changeType: "priority_changed",
2952
+ changedAt: now,
2953
+ changedBy: args.userId,
2954
+ isAgent: false,
2955
+ projectId: node.projectId,
2956
+ previousState: { priority: previousPriority },
2957
+ newState: { priority: args.priority }
2958
+ });
2959
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
2960
+ return { nodeId: args.nodeId };
2961
+ }
2962
+ });
2963
+ var getById = query({
2964
+ args: {
2965
+ nodeId: v.optional(v.id("epistemicNodes")),
2966
+ questionId: v.optional(v.string())
2967
+ },
2968
+ returns: permissiveReturn,
2969
+ handler: async (ctx, args) => {
2970
+ const id = args.nodeId ?? args.questionId;
2971
+ if (!id) {
2972
+ return null;
2973
+ }
2974
+ const node = await ctx.db.get(
2975
+ id
2976
+ );
2977
+ if (!node || node.nodeType !== "question") {
2978
+ return null;
2979
+ }
2980
+ return flattenQuestionNode(node);
2981
+ }
2982
+ });
2983
+ var getByProject = query({
2984
+ args: {
2985
+ ...optionalScopeArgs,
2986
+ status: v.optional(v.string()),
2987
+ userId: v.optional(v.string()),
2988
+ limit: v.optional(v.number())
2989
+ },
2990
+ returns: permissiveReturn,
2991
+ handler: async (ctx, args) => {
2992
+ const scope = await resolveQuestionScopeOrNull(ctx, args);
2993
+ if (!scope) {
2994
+ return [];
2995
+ }
2996
+ const pageSize = clampQuestionLimit(args.limit);
2997
+ const scanLimit = Math.min(pageSize * 3, MAX_QUESTION_PAGE_SIZE);
2998
+ if (args.userId) {
2999
+ const scopeId = resolveQuestionScopeId(scope);
3000
+ if (!scopeId) {
3001
+ return [];
3002
+ }
3003
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
3004
+ if (!hasAccess) {
3005
+ return [];
3006
+ }
3007
+ }
3008
+ const workspaceScopedNodes = await getQuestionNodesForScope(ctx, scope, {
3009
+ scanLimit
3010
+ });
3011
+ const scopedNodes = args.status ? workspaceScopedNodes.filter(
3012
+ (node) => matchesRequestedQuestionStatus(node, args.status)
3013
+ ) : workspaceScopedNodes.filter(isActiveQuestionNode);
3014
+ return scopedNodes.map(flattenQuestionNode).slice(0, pageSize);
3015
+ }
3016
+ });
3017
+ var getByTopic = query({
3018
+ args: {
3019
+ topicId: v.string(),
3020
+ status: v.optional(v.string()),
3021
+ userId: v.optional(v.string()),
3022
+ limit: v.optional(v.number())
3023
+ },
3024
+ returns: permissiveReturn,
3025
+ handler: async (ctx, args) => {
3026
+ const pageSize = clampQuestionLimit(args.limit);
3027
+ const scanLimit = Math.min(pageSize * 3, MAX_QUESTION_PAGE_SIZE);
3028
+ const scope = await resolveTopicProjectScope(ctx, { topicId: args.topicId });
3029
+ const scopedNodes = await getQuestionNodesForScope(ctx, scope, {
3030
+ scanLimit
3031
+ });
3032
+ const filteredNodes = args.status ? scopedNodes.filter(
3033
+ (node) => matchesRequestedQuestionStatus(node, args.status)
3034
+ ) : scopedNodes.filter(isActiveQuestionNode);
3035
+ return filteredNodes.map(flattenQuestionNode).slice(0, pageSize);
3036
+ }
3037
+ });
3038
+ var getByCategory = query({
3039
+ args: {
3040
+ ...optionalScopeArgs,
3041
+ category: v.string()
3042
+ },
3043
+ returns: permissiveReturn,
3044
+ handler: async (ctx, args) => {
3045
+ const scope = await resolveQuestionScopeOrNull(ctx, args);
3046
+ if (!scope) {
3047
+ return [];
3048
+ }
3049
+ const nodes = await getQuestionNodesForScope(ctx, scope);
3050
+ return nodes.filter((n) => {
3051
+ const metadata = n.metadata || {};
3052
+ return questionMatchesScope(n, scope) && (isActiveQuestionNode(n) && metadata.category === normalizeCategory(args.category));
3053
+ });
3054
+ }
3055
+ });
3056
+ var getForBelief = query({
3057
+ args: {
3058
+ beliefNodeId: v.id("epistemicNodes")
3059
+ },
3060
+ returns: permissiveReturn,
3061
+ handler: async (ctx, args) => {
3062
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
3063
+ "by_from_type",
3064
+ (q) => q.eq("fromNodeId", args.beliefNodeId).eq("edgeType", "tests")
3065
+ ).collect();
3066
+ const questionNodeIds = edges.map((e) => e.toNodeId).filter(Boolean);
3067
+ const questions = await Promise.all(
3068
+ questionNodeIds.map((id) => ctx.db.get(id))
3069
+ );
3070
+ return questions.filter(
3071
+ (q) => q !== null && q.nodeType === "question" && isActiveQuestionNode(q)
3072
+ );
3073
+ }
3074
+ });
3075
+ var internalGetByProject = internalQuery({
3076
+ args: {
3077
+ ...optionalScopeArgs,
3078
+ status: v.optional(v.string()),
3079
+ limit: v.optional(v.number()),
3080
+ audienceMode: v.optional(v.string())
3081
+ },
3082
+ returns: permissiveReturn,
3083
+ handler: async (ctx, args) => {
3084
+ const pageSize = clampQuestionLimit(args.limit, 500);
3085
+ const scanLimit = Math.min(pageSize * 3, MAX_QUESTION_PAGE_SIZE);
3086
+ const audienceMode = args.audienceMode ?? "internal";
3087
+ const scope = await resolveTopicProjectScope(ctx, {
3088
+ projectId: args.projectId,
3089
+ topicId: args.topicId
3090
+ });
3091
+ const projectScopeId = resolveQuestionScopeId(scope);
3092
+ if (!projectScopeId) {
3093
+ return [];
3094
+ }
3095
+ const project = await resolveGraphPrimitivesAppResolvers().getProject(
3096
+ ctx,
3097
+ projectScopeId
3098
+ );
3099
+ const registryRows = await listAudienceRegistryRows(ctx, {
3100
+ tenantId: project?.tenantId,
3101
+ workspaceId: project?.workspaceId
3102
+ });
3103
+ const audienceClassByKey = new Map(
3104
+ registryRows.map((row) => [
3105
+ normalizeAudienceKey(row.audienceKey),
3106
+ row.audienceClass
3107
+ ])
3108
+ );
3109
+ const resolveAudienceClass = (audienceKey, fallback) => {
3110
+ const key = normalizeAudienceKey(audienceKey);
3111
+ if (!key) {
3112
+ return fallback;
3113
+ }
3114
+ return audienceClassByKey.get(key) ?? classFromAudienceKey(key, fallback);
3115
+ };
3116
+ const viewerClass = resolveAudienceClass(audienceMode, "public");
3117
+ const nodes = await getQuestionNodesForScope(ctx, scope, { scanLimit });
3118
+ const workspaceScopedNodes = nodes.filter(
3119
+ (node) => nodeMatchesWorkspaceReasoningScope(node, {
3120
+ tenantId: project?.tenantId,
3121
+ workspaceId: project?.workspaceId
3122
+ })
3123
+ );
3124
+ const visibleNodes = workspaceScopedNodes.filter(
3125
+ (node) => canAudienceClassAccess(
3126
+ viewerClass,
3127
+ resolveAudienceClass(node.audienceLabel, "internal")
3128
+ )
3129
+ );
3130
+ const scopedNodes = args.status ? visibleNodes.filter(
3131
+ (node) => matchesRequestedQuestionStatus(node, args.status)
3132
+ ) : visibleNodes.filter(isActiveQuestionNode);
3133
+ return scopedNodes.slice(0, pageSize).map(flattenInternalQuestionNode);
3134
+ }
3135
+ });
3136
+ var internalGetByTopic = internalQuery({
3137
+ args: {
3138
+ topicId: v.string(),
3139
+ status: v.optional(v.string()),
3140
+ limit: v.optional(v.number()),
3141
+ audienceMode: v.optional(v.string())
3142
+ },
3143
+ returns: permissiveReturn,
3144
+ handler: async (ctx, args) => {
3145
+ const pageSize = clampQuestionLimit(args.limit, 500);
3146
+ const scanLimit = Math.min(pageSize * 3, MAX_QUESTION_PAGE_SIZE);
3147
+ const audienceMode = args.audienceMode ?? "internal";
3148
+ const scope = await resolveTopicProjectScope(ctx, { topicId: args.topicId });
3149
+ const registryRows = await listAudienceRegistryRows(ctx, {
3150
+ tenantId: scope.tenantId,
3151
+ workspaceId: scope.workspaceId
3152
+ });
3153
+ const audienceClassByKey = new Map(
3154
+ registryRows.map((row) => [
3155
+ normalizeAudienceKey(row.audienceKey),
3156
+ row.audienceClass
3157
+ ])
3158
+ );
3159
+ const resolveAudienceClass = (audienceKey, fallback) => {
3160
+ const key = normalizeAudienceKey(audienceKey);
3161
+ if (!key) {
3162
+ return fallback;
3163
+ }
3164
+ return audienceClassByKey.get(key) ?? classFromAudienceKey(key, fallback);
3165
+ };
3166
+ const viewerClass = resolveAudienceClass(audienceMode, "public");
3167
+ const nodes = await getQuestionNodesForScope(ctx, scope, { scanLimit });
3168
+ const workspaceScopedNodes = nodes.filter(
3169
+ (node) => nodeMatchesWorkspaceReasoningScope(node, {
3170
+ tenantId: scope.tenantId,
3171
+ workspaceId: scope.workspaceId
3172
+ })
3173
+ );
3174
+ const visibleNodes = workspaceScopedNodes.filter(
3175
+ (node) => canAudienceClassAccess(
3176
+ viewerClass,
3177
+ resolveAudienceClass(node.audienceLabel, "internal")
3178
+ )
3179
+ );
3180
+ const scopedNodes = args.status ? visibleNodes.filter(
3181
+ (node) => matchesRequestedQuestionStatus(node, args.status)
3182
+ ) : visibleNodes.filter(isActiveQuestionNode);
3183
+ return scopedNodes.slice(0, pageSize).map(flattenInternalQuestionNode);
3184
+ }
3185
+ });
3186
+ var internalCreate = internalMutation({
3187
+ args: {
3188
+ ...optionalScopeArgs,
3189
+ question: v.string(),
3190
+ category: v.optional(v.string()),
3191
+ priority: v.optional(v.string()),
3192
+ source: v.optional(v.string()),
3193
+ userId: v.string(),
3194
+ linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
3195
+ testType: v.optional(v.string()),
3196
+ runtimeToolName: v.optional(v.string()),
3197
+ runtimePackKey: v.optional(v.string()),
3198
+ runtimePackInstallScope: v.optional(
3199
+ v.union(v.literal("tenant"), v.literal("workspace"))
3200
+ )
3201
+ },
3202
+ returns: permissiveReturn,
3203
+ handler: async (ctx, args) => {
3204
+ const now = Date.now();
3205
+ const scope = await resolveTopicProjectScope(ctx, {
3206
+ topicId: args.topicId,
3207
+ projectId: args.projectId
3208
+ });
3209
+ assertWorkspaceScopedEpistemicNodeScope({
3210
+ scope,
3211
+ nodeType: "question",
3212
+ mutationName: "epistemicQuestions.internalCreate"
3213
+ });
3214
+ assertTenantPackWorkspaceMutationAllowed({
3215
+ runtime: resolveRuntimePackMutationContext(args),
3216
+ target: {
3217
+ tenantId: scope.tenantId,
3218
+ workspaceId: scope.workspaceId,
3219
+ nodeType: "question",
3220
+ epistemicLayer: "L3"
3221
+ },
3222
+ mutationName: "epistemicQuestions.internalCreate"
3223
+ });
3224
+ const globalId = generateGlobalId();
3225
+ const contentHash = generateContentHash(args.question);
3226
+ const category = normalizeCategory(args.category);
3227
+ const nodeId = await ctx.db.insert("epistemicNodes", {
3228
+ globalId,
3229
+ topicId: scope.topicId,
3230
+ projectId: scope.projectId,
3231
+ tenantId: scope.tenantId,
3232
+ workspaceId: scope.workspaceId,
3233
+ nodeType: "question",
3234
+ canonicalText: args.question,
3235
+ contentHash,
3236
+ status: "active",
3237
+ epistemicLayer: "L3",
3238
+ sourceType: args.source === "user" ? "human" : "ai_generated",
3239
+ createdAt: now,
3240
+ updatedAt: now,
3241
+ createdBy: args.userId,
3242
+ metadata: {
3243
+ category,
3244
+ priority: args.priority || "medium",
3245
+ source: args.source || "ai_suggested",
3246
+ questionStatus: "open",
3247
+ linkedBeliefNodeId: args.linkedBeliefNodeId,
3248
+ testType: args.testType
3249
+ }
3250
+ });
3251
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
3252
+ nodeId,
3253
+ operation: "upsert"
3254
+ });
3255
+ if (args.linkedBeliefNodeId) {
3256
+ const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
3257
+ if (beliefNode) {
3258
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
3259
+ globalId: crypto.randomUUID(),
3260
+ fromGlobalId: beliefNode.globalId,
3261
+ toGlobalId: globalId,
3262
+ edgeType: "tests",
3263
+ weight: 1,
3264
+ createdBy: args.userId,
3265
+ topicId: scope.projectId ? String(scope.projectId) : void 0,
3266
+ fromNodeType: "belief",
3267
+ toNodeType: "question",
3268
+ fromLayer: "L3",
3269
+ toLayer: "L3"
3270
+ });
3271
+ }
3272
+ }
3273
+ if (scope.projectId || scope.topicId) {
3274
+ await ctx.scheduler.runAfter(
3275
+ 0,
3276
+ "embeddingActions:generateEpistemicNodeEmbedding",
3277
+ {
3278
+ nodeId,
3279
+ projectId: scope.projectId,
3280
+ topicId: scope.topicId ? String(scope.topicId) : void 0,
3281
+ createdBy: args.userId,
3282
+ nodeType: "question",
3283
+ text: args.question,
3284
+ hasAnswer: false
3285
+ }
3286
+ );
3287
+ }
3288
+ await markProjectGraphDirty(
3289
+ ctx,
3290
+ scope.projectId,
3291
+ normalizeQuestionTopicId(scope.topicId)
3292
+ );
3293
+ return { nodeId };
3294
+ }
3295
+ });
3296
+ var addQuestion = mutation({
3297
+ args: {
3298
+ ...optionalScopeArgs,
3299
+ question: v.string(),
3300
+ category: v.optional(v.string()),
3301
+ priority: v.optional(v.string()),
3302
+ source: v.optional(v.string()),
3303
+ userId: v.string(),
3304
+ beliefId: v.optional(v.string()),
3305
+ chatId: v.optional(v.string()),
3306
+ importance: v.optional(v.number()),
3307
+ epistemicUnlock: v.optional(v.string()),
3308
+ metadata: v.optional(v.any()),
3309
+ questionType: v.optional(v.string())
3310
+ },
3311
+ returns: permissiveReturn,
3312
+ handler: async (ctx, args) => {
3313
+ const scope = await resolveTopicProjectScope(ctx, {
3314
+ topicId: args.topicId,
3315
+ projectId: args.projectId
3316
+ });
3317
+ const scopeId = resolveQuestionScopeId(scope);
3318
+ if (!scopeId) {
3319
+ throw new Error("No scope identifier provided");
3320
+ }
3321
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
3322
+ if (!hasAccess) {
3323
+ throw new Error("Access denied");
3324
+ }
3325
+ const now = Date.now();
3326
+ const globalId = generateGlobalId();
3327
+ const contentHash = generateContentHash(args.question);
3328
+ const category = normalizeCategory(args.category);
3329
+ const additionalMetadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
3330
+ const nodeId = await ctx.db.insert("epistemicNodes", {
3331
+ globalId,
3332
+ topicId: scope.topicId,
3333
+ projectId: scope.projectId,
3334
+ tenantId: scope.tenantId,
3335
+ workspaceId: scope.workspaceId,
3336
+ nodeType: "question",
3337
+ canonicalText: args.question,
3338
+ contentHash,
3339
+ status: "active",
3340
+ epistemicLayer: "L3",
3341
+ sourceType: args.source === "user" ? "human" : "ai_generated",
3342
+ createdAt: now,
3343
+ updatedAt: now,
3344
+ createdBy: args.userId,
3345
+ metadata: {
3346
+ ...additionalMetadata,
3347
+ category,
3348
+ priority: args.priority || "medium",
3349
+ source: args.source || "ai_suggested",
3350
+ questionStatus: "open",
3351
+ beliefId: args.beliefId,
3352
+ linkedBeliefId: args.beliefId,
3353
+ chatId: args.chatId,
3354
+ importance: args.importance,
3355
+ epistemicUnlock: args.epistemicUnlock
3356
+ }
3357
+ });
3358
+ await ctx.db.insert("epistemicAudit", {
3359
+ entityType: "question",
3360
+ entityId: String(nodeId),
3361
+ changeType: "created",
3362
+ changedAt: now,
3363
+ changedBy: args.userId,
3364
+ isAgent: false,
3365
+ projectId: scope.projectId,
3366
+ topicId: normalizeQuestionTopicId(scope.topicId),
3367
+ newState: {
3368
+ ...additionalMetadata,
3369
+ question: args.question,
3370
+ category,
3371
+ priority: args.priority || "medium",
3372
+ source: args.source || "ai_suggested",
3373
+ beliefId: args.beliefId,
3374
+ chatId: args.chatId,
3375
+ importance: args.importance,
3376
+ epistemicUnlock: args.epistemicUnlock
3377
+ },
3378
+ triggeringAction: "epistemicQuestions.addQuestion"
3379
+ });
3380
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
3381
+ nodeId,
3382
+ operation: "upsert"
3383
+ });
3384
+ await ctx.scheduler.runAfter(
3385
+ 0,
3386
+ "embeddingActions:generateEpistemicNodeEmbedding",
3387
+ {
3388
+ nodeId,
3389
+ projectId: scope.projectId,
3390
+ topicId: normalizeQuestionTopicId(scope.topicId),
3391
+ createdBy: args.userId,
3392
+ nodeType: "question",
3393
+ text: args.question,
3394
+ hasAnswer: false
3395
+ }
3396
+ );
3397
+ await markProjectGraphDirty(
3398
+ ctx,
3399
+ scope.projectId,
3400
+ normalizeQuestionTopicId(scope.topicId)
3401
+ );
3402
+ return nodeId;
3403
+ }
3404
+ });
3405
+ var createEvidenceFromScoredQuestion = internalMutation({
3406
+ args: {
3407
+ questionNodeId: v.id("epistemicNodes"),
3408
+ questionText: v.string(),
3409
+ answerText: v.string(),
3410
+ beliefId: v.string(),
3411
+ relatedBeliefIds: v.array(v.string()),
3412
+ conviction: v.number(),
3413
+ rationale: v.string(),
3414
+ ...optionalScopeArgs,
3415
+ userId: v.string()
3416
+ },
3417
+ returns: permissiveReturn,
3418
+ handler: async (ctx, args) => {
3419
+ const now = Date.now();
3420
+ const questionNode = await ctx.db.get(args.questionNodeId);
3421
+ if (!questionNode) {
3422
+ return;
3423
+ }
3424
+ const qMeta = questionNode.metadata || {};
3425
+ if (qMeta.evidenceNodeId) {
3426
+ return;
3427
+ }
3428
+ const evidenceText = `Q: ${args.questionText}
3429
+
3430
+ A: ${args.answerText}`;
3431
+ const globalId = generateGlobalId();
3432
+ const contentHash = `evidence:${args.questionNodeId}:scored`;
3433
+ const evidenceNodeId = await ctx.db.insert("epistemicNodes", {
3434
+ globalId,
3435
+ projectId: args.projectId,
3436
+ topicId: normalizeQuestionTopicId(args.topicId),
3437
+ nodeType: "evidence",
3438
+ canonicalText: evidenceText,
3439
+ contentHash,
3440
+ status: "active",
3441
+ epistemicLayer: "L2",
3442
+ sourceType: "verified",
3443
+ // Human-scored sprint answer
3444
+ createdAt: now,
3445
+ updatedAt: now,
3446
+ createdBy: args.userId,
3447
+ metadata: {
3448
+ kind: "observation",
3449
+ sourceQuestionId: args.questionNodeId,
3450
+ sprintConviction: args.conviction,
3451
+ rationale: args.rationale,
3452
+ origin: "sprint_question_scoring"
3453
+ }
3454
+ });
3455
+ await ctx.db.patch(args.questionNodeId, {
3456
+ metadata: {
3457
+ ...qMeta,
3458
+ evidenceNodeId
3459
+ },
3460
+ updatedAt: now
3461
+ });
3462
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
3463
+ nodeId: evidenceNodeId,
3464
+ operation: "upsert"
3465
+ });
3466
+ const allBeliefIds = /* @__PURE__ */ new Set();
3467
+ allBeliefIds.add(args.beliefId);
3468
+ for (const id of args.relatedBeliefIds) {
3469
+ allBeliefIds.add(id);
3470
+ }
3471
+ const weight = args.conviction >= 0.5 ? args.conviction : -(1 - args.conviction);
3472
+ async function resolveBeliefNode(bId) {
3473
+ try {
3474
+ const node = await ctx.db.get(bId);
3475
+ if (node?.nodeType === "belief") {
3476
+ return node;
3477
+ }
3478
+ } catch {
3479
+ return null;
3480
+ }
3481
+ return null;
3482
+ }
3483
+ for (const bId of allBeliefIds) {
3484
+ const beliefNode = await resolveBeliefNode(bId);
3485
+ if (!beliefNode) {
3486
+ continue;
3487
+ }
3488
+ const edgeGlobalId = generateGlobalId();
3489
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
3490
+ globalId: edgeGlobalId,
3491
+ fromGlobalId: globalId,
3492
+ toGlobalId: beliefNode.globalId,
3493
+ edgeType: "informs",
3494
+ weight,
3495
+ confidence: Math.abs(weight),
3496
+ context: `origin=sprint_question_scoring | relation=${weight >= 0 ? "supports" : "contradicts"} | sourceQuestionId=${String(args.questionNodeId)}`,
3497
+ createdBy: args.userId,
3498
+ topicId: args.projectId ? String(args.projectId) : void 0,
3499
+ fromNodeType: "evidence",
3500
+ toNodeType: "belief",
3501
+ fromLayer: "L2",
3502
+ toLayer: "L3"
3503
+ });
3504
+ }
3505
+ const answersEdgeGlobalId = generateGlobalId();
3506
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
3507
+ globalId: answersEdgeGlobalId,
3508
+ fromGlobalId: globalId,
3509
+ toGlobalId: questionNode.globalId,
3510
+ edgeType: "derived_from",
3511
+ weight: 1,
3512
+ confidence: 1,
3513
+ context: `origin=sprint_question_scoring | sourceQuestionId=${String(args.questionNodeId)}`,
3514
+ createdBy: args.userId,
3515
+ topicId: args.projectId ? String(args.projectId) : void 0,
3516
+ fromNodeType: "evidence",
3517
+ toNodeType: "question",
3518
+ fromLayer: "L2",
3519
+ toLayer: "L3"
3520
+ });
3521
+ await ctx.db.insert("epistemicAudit", {
3522
+ entityType: "evidence",
3523
+ entityId: evidenceNodeId,
3524
+ changeType: "created",
3525
+ changedAt: now,
3526
+ changedBy: args.userId,
3527
+ isAgent: false,
3528
+ projectId: args.projectId,
3529
+ topicId: normalizeQuestionTopicId(args.topicId),
3530
+ newState: {
3531
+ text: evidenceText.slice(0, 200),
3532
+ kind: "observation",
3533
+ sourceType: "verified",
3534
+ origin: "sprint_question_scoring",
3535
+ linkedBeliefIds: [...allBeliefIds]
3536
+ }
3537
+ });
3538
+ await markProjectGraphDirty(
3539
+ ctx,
3540
+ args.projectId,
3541
+ normalizeQuestionTopicId(args.topicId)
3542
+ );
3543
+ return { evidenceNodeId };
3544
+ }
3545
+ });
3546
+ var backfillScoredQuestionEvidence = internalMutation({
3547
+ args: {
3548
+ ...optionalScopeArgs,
3549
+ userId: v.string(),
3550
+ dryRun: v.optional(v.boolean())
3551
+ },
3552
+ returns: permissiveReturn,
3553
+ handler: async (ctx, args) => {
3554
+ const dryRun = args.dryRun ?? false;
3555
+ const scope = await resolveQuestionScopeOrNull(ctx, args);
3556
+ if (!scope) {
3557
+ return {
3558
+ dryRun,
3559
+ totalQuestions: 0,
3560
+ candidateCount: 0,
3561
+ scheduled: 0,
3562
+ skipped: 0,
3563
+ candidates: []
3564
+ };
3565
+ }
3566
+ const allQuestions = await getQuestionNodesForScope(ctx, scope);
3567
+ const candidates = allQuestions.filter((n) => {
3568
+ const meta = n.metadata || {};
3569
+ return meta.convictionStage === "scored" && meta.answer && !meta.evidenceNodeId && (meta.linkedBeliefId || meta.beliefId);
3570
+ });
3571
+ if (dryRun) {
3572
+ return {
3573
+ dryRun: true,
3574
+ candidateCount: candidates.length,
3575
+ candidates: candidates.map((n) => {
3576
+ const meta = n.metadata || {};
3577
+ return {
3578
+ questionId: n._id,
3579
+ questionText: n.canonicalText?.slice(0, 80),
3580
+ beliefId: meta.linkedBeliefId || meta.beliefId,
3581
+ conviction: meta.conviction,
3582
+ hasAnswer: !!meta.answer
3583
+ };
3584
+ })
3585
+ };
3586
+ }
3587
+ let created = 0;
3588
+ let skipped = 0;
3589
+ for (const questionNode of candidates) {
3590
+ const meta = questionNode.metadata || {};
3591
+ const beliefId = meta.linkedBeliefId ?? meta.beliefId;
3592
+ const answerText = meta.answer;
3593
+ const conviction = meta.conviction ?? 0.5;
3594
+ if (!beliefId || !answerText) {
3595
+ skipped++;
3596
+ continue;
3597
+ }
3598
+ await ctx.scheduler.runAfter(
3599
+ created * 100,
3600
+ // Stagger to avoid overwhelming Neo4j
3601
+ internal.epistemicQuestions.createEvidenceFromScoredQuestion,
3602
+ {
3603
+ questionNodeId: questionNode._id,
3604
+ questionText: questionNode.canonicalText || "",
3605
+ answerText,
3606
+ beliefId,
3607
+ relatedBeliefIds: meta.relatedBeliefIds || [],
3608
+ conviction,
3609
+ rationale: meta.convictionRationale || "",
3610
+ projectId: scope.projectId,
3611
+ topicId: normalizeQuestionTopicId(scope.topicId),
3612
+ userId: args.userId
3613
+ }
3614
+ );
3615
+ created++;
3616
+ }
3617
+ if (created > 0) {
3618
+ await markProjectGraphDirty(
3619
+ ctx,
3620
+ scope.projectId,
3621
+ normalizeQuestionTopicId(scope.topicId)
3622
+ );
3623
+ }
3624
+ return {
3625
+ dryRun: false,
3626
+ totalQuestions: allQuestions.length,
3627
+ candidateCount: candidates.length,
3628
+ scheduled: created,
3629
+ skipped
3630
+ };
3631
+ }
3632
+ });
3633
+ var getForSprintCluster = query({
3634
+ args: {
3635
+ sprintId: v.optional(v.string()),
3636
+ worktreeId: v.optional(v.string()),
3637
+ userId: v.string()
3638
+ },
3639
+ returns: permissiveReturn,
3640
+ handler: async (ctx, args) => {
3641
+ const docId = args.sprintId || args.worktreeId;
3642
+ if (!docId) return [];
3643
+ const workflow = await resolveWorkflowBridgeDoc(ctx, docId);
3644
+ const scopeDoc = workflow.worktree ?? workflow.sprint;
3645
+ if (!scopeDoc) {
3646
+ return [];
3647
+ }
3648
+ const scope = await resolveQuestionScopeOrNull(ctx, {
3649
+ projectId: workflow.projectId,
3650
+ topicId: workflow.topicId
3651
+ });
3652
+ const scopeId = scope ? resolveQuestionScopeId(scope) : void 0;
3653
+ if (!scope || !scopeId) {
3654
+ return [];
3655
+ }
3656
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
3657
+ if (!hasAccess) {
3658
+ return [];
3659
+ }
3660
+ const synthesisState = scopeDoc.synthesisState;
3661
+ const hypothesisIds = new Set(
3662
+ (synthesisState?.synthesizedBeliefIds || []).map(
3663
+ (id) => String(id)
3664
+ )
3665
+ );
3666
+ const rawBeliefIds = new Set(
3667
+ (synthesisState?.originalBeliefIds || []).map(
3668
+ (id) => String(id)
3669
+ )
3670
+ );
3671
+ const clusterBeliefIds = new Set(hypothesisIds);
3672
+ const edges = await getQuestionEdgesForScope(ctx, scope);
3673
+ for (const edge of edges) {
3674
+ const fromStr = String(edge.fromNodeId || "");
3675
+ const toStr = String(edge.toNodeId || "");
3676
+ if (hypothesisIds.has(fromStr) && !rawBeliefIds.has(toStr)) {
3677
+ clusterBeliefIds.add(toStr);
3678
+ }
3679
+ if (hypothesisIds.has(toStr) && !rawBeliefIds.has(fromStr)) {
3680
+ clusterBeliefIds.add(fromStr);
3681
+ }
3682
+ }
3683
+ const questionNodes = await getQuestionNodesForScope(ctx, scope);
3684
+ const sprintQuestionIds = new Set(
3685
+ (workflow.worktree?.targetQuestionIds || workflow.sprint?.targetQuestionIds || []).map((id) => String(id))
3686
+ );
3687
+ const clusterQuestions = questionNodes.filter((q) => {
3688
+ if (sprintQuestionIds.has(String(q._id))) {
3689
+ return true;
3690
+ }
3691
+ const meta = q.metadata || {};
3692
+ const linkedId = String(meta.linkedBeliefNodeId || "");
3693
+ if (linkedId && clusterBeliefIds.has(linkedId)) {
3694
+ return true;
3695
+ }
3696
+ return edges.some(
3697
+ (e) => String(e.fromNodeId) === String(q._id) && clusterBeliefIds.has(String(e.toNodeId || "")) || String(e.toNodeId) === String(q._id) && clusterBeliefIds.has(String(e.fromNodeId || ""))
3698
+ );
3699
+ });
3700
+ const enrichedQuestions = await Promise.all(
3701
+ clusterQuestions.map(async (q) => {
3702
+ const meta = q.metadata || {};
3703
+ const linkedBeliefNodeIds = /* @__PURE__ */ new Set();
3704
+ const primaryBeliefId = String(meta.linkedBeliefNodeId || "");
3705
+ if (primaryBeliefId) {
3706
+ linkedBeliefNodeIds.add(primaryBeliefId);
3707
+ }
3708
+ for (const edge of edges) {
3709
+ if (String(edge.fromNodeId) === String(q._id)) {
3710
+ linkedBeliefNodeIds.add(String(edge.toNodeId || ""));
3711
+ }
3712
+ if (String(edge.toNodeId) === String(q._id)) {
3713
+ linkedBeliefNodeIds.add(String(edge.fromNodeId || ""));
3714
+ }
3715
+ }
3716
+ linkedBeliefNodeIds.delete("");
3717
+ const linkedBeliefs = (await Promise.all(
3718
+ Array.from(linkedBeliefNodeIds).map(async (nodeId) => {
3719
+ const node = await ctx.db.get(nodeId);
3720
+ if (!node || node.nodeType !== "belief") {
3721
+ return null;
3722
+ }
3723
+ const isHypothesis = hypothesisIds.has(nodeId);
3724
+ const isRaw = rawBeliefIds.has(nodeId);
3725
+ const isConditional = !isHypothesis && !isRaw && clusterBeliefIds.has(nodeId);
3726
+ return {
3727
+ beliefId: node._id,
3728
+ beliefText: node.canonicalText || "",
3729
+ isHypothesis,
3730
+ isConditional,
3731
+ isRaw,
3732
+ isPrimaryTarget: isHypothesis
3733
+ };
3734
+ })
3735
+ )).filter(Boolean);
3736
+ return {
3737
+ _id: q._id,
3738
+ _creationTime: q._creationTime,
3739
+ projectId: q.projectId,
3740
+ topicId: q.topicId,
3741
+ canonicalText: q.canonicalText,
3742
+ question: q.canonicalText,
3743
+ // legacy field name
3744
+ questionType: q.questionType || meta.questionType || "general",
3745
+ priority: meta.priority || "medium",
3746
+ isKeyQuestion: meta.isKeyQuestion || false,
3747
+ testType: meta.testType || "validates",
3748
+ category: meta.category || "other",
3749
+ status: q.status,
3750
+ linkedBeliefs,
3751
+ testLogic: meta.testType || "validates",
3752
+ testRationale: meta.rationale || null,
3753
+ beliefId: meta.linkedBeliefNodeId || null,
3754
+ relatedBeliefIds: Array.from(linkedBeliefNodeIds),
3755
+ // Conviction stage fields — needed by UI to show/hide action buttons
3756
+ convictionStage: meta.convictionStage || null,
3757
+ answer: meta.answer || null,
3758
+ conviction: meta.conviction ?? null,
3759
+ linkedWorktreeId: resolveLinkedWorktreeId(meta),
3760
+ metadata: q.metadata
3761
+ };
3762
+ })
3763
+ );
3764
+ const seen = /* @__PURE__ */ new Set();
3765
+ return enrichedQuestions.filter((q) => {
3766
+ if (seen.has(String(q._id))) {
3767
+ return false;
3768
+ }
3769
+ seen.add(String(q._id));
3770
+ return true;
3771
+ });
3772
+ }
3773
+ });
3774
+ var getInConviction = query({
3775
+ args: {
3776
+ sprintId: v.optional(v.string()),
3777
+ worktreeId: v.optional(v.string()),
3778
+ userId: v.string()
3779
+ },
3780
+ returns: permissiveReturn,
3781
+ handler: async (ctx, args) => {
3782
+ const docId = args.sprintId || args.worktreeId;
3783
+ if (!docId) return [];
3784
+ const workflow = await resolveWorkflowBridgeDoc(ctx, docId);
3785
+ const scopeDoc = workflow.worktree ?? workflow.sprint;
3786
+ if (!scopeDoc) {
3787
+ return [];
3788
+ }
3789
+ const scope = await resolveQuestionScopeOrNull(ctx, {
3790
+ projectId: workflow.projectId,
3791
+ topicId: workflow.topicId
3792
+ });
3793
+ const scopeId = scope ? resolveQuestionScopeId(scope) : void 0;
3794
+ if (!scope || !scopeId) {
3795
+ return [];
3796
+ }
3797
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
3798
+ if (!hasAccess) {
3799
+ return [];
3800
+ }
3801
+ const allQuestions = await getQuestionNodesForScope(ctx, scope);
3802
+ const convictionQuestions = allQuestions.filter((q) => {
3803
+ const meta = q.metadata || {};
3804
+ return meta.convictionStage === "in_conviction" || meta.convictionStage === "scored";
3805
+ });
3806
+ const sprintBeliefIds = /* @__PURE__ */ new Set([
3807
+ ...(scopeDoc.synthesisState?.synthesizedBeliefIds || []).map(
3808
+ String
3809
+ ),
3810
+ ...(workflow.worktree?.targetBeliefIds || workflow.sprint?.targetBeliefIds || []).map(String)
3811
+ ]);
3812
+ const sprintQuestions = convictionQuestions.filter((q) => {
3813
+ const meta = q.metadata || {};
3814
+ if (questionMatchesWorkflowLink(meta, workflow)) {
3815
+ return true;
3816
+ }
3817
+ if (meta.linkedBeliefNodeId && sprintBeliefIds.has(String(meta.linkedBeliefNodeId))) {
3818
+ return true;
3819
+ }
3820
+ if (meta.linkedBeliefId && sprintBeliefIds.has(String(meta.linkedBeliefId))) {
3821
+ return true;
3822
+ }
3823
+ if (meta.beliefId && sprintBeliefIds.has(String(meta.beliefId))) {
3824
+ return true;
3825
+ }
3826
+ const relatedIds = meta.relatedBeliefIds || [];
3827
+ if (relatedIds.some((id) => sprintBeliefIds.has(String(id)))) {
3828
+ return true;
3829
+ }
3830
+ return false;
3831
+ });
3832
+ const questionsWithDetails = await Promise.all(
3833
+ sprintQuestions.map(async (q) => {
3834
+ const meta = q.metadata || {};
3835
+ const evidenceLinks = await ctx.db.query("questionEvidenceLinks").withIndex("by_questionId", (qb) => qb.eq("questionId", q._id)).collect();
3836
+ const evidenceWithDetails = await Promise.all(
3837
+ evidenceLinks.map(async (link) => {
3838
+ const insight = await ctx.db.get(link.insightId);
3839
+ const insightMeta = insight?.metadata || {};
3840
+ const canonicalText = insight?.canonicalText;
3841
+ const legacyText = insight?.text;
3842
+ const contentText = insight?.content;
3843
+ const titleText = insight?.title;
3844
+ const snippetText = insightMeta.snippet;
3845
+ return {
3846
+ ...link,
3847
+ insightText: typeof canonicalText === "string" && canonicalText.trim() || typeof legacyText === "string" && legacyText.trim() || typeof contentText === "string" && contentText.trim() || typeof titleText === "string" && titleText.trim() || typeof snippetText === "string" && snippetText.trim() || "",
3848
+ insightSource: insight?.sourceType || "unknown"
3849
+ };
3850
+ })
3851
+ );
3852
+ const beliefIds = [];
3853
+ const relatedIds = meta.relatedBeliefIds || [];
3854
+ if (meta.linkedBeliefNodeId) {
3855
+ beliefIds.push(String(meta.linkedBeliefNodeId));
3856
+ }
3857
+ if (meta.linkedBeliefId) {
3858
+ beliefIds.push(String(meta.linkedBeliefId));
3859
+ }
3860
+ if (meta.beliefId) {
3861
+ beliefIds.push(String(meta.beliefId));
3862
+ }
3863
+ for (const id of relatedIds) {
3864
+ if (!beliefIds.includes(String(id))) {
3865
+ beliefIds.push(String(id));
3866
+ }
3867
+ }
3868
+ const beliefsWithDetails = await Promise.all(
3869
+ beliefIds.map(async (beliefId) => {
3870
+ try {
3871
+ const node = await ctx.db.get(beliefId);
3872
+ if (node && node.nodeType === "belief") {
3873
+ return {
3874
+ _id: node._id,
3875
+ belief: node.canonicalText || "",
3876
+ confidence: node.metadata?.confidence,
3877
+ pillar: node.metadata?.category,
3878
+ beliefType: node.status,
3879
+ dependsOnCount: 0,
3880
+ cascadesToCount: 0,
3881
+ exclusiveWithCount: 0
3882
+ };
3883
+ }
3884
+ } catch {
3885
+ }
3886
+ return null;
3887
+ })
3888
+ );
3889
+ return {
3890
+ _id: q._id,
3891
+ _creationTime: q._creationTime,
3892
+ projectId: q.projectId,
3893
+ question: q.canonicalText,
3894
+ canonicalText: q.canonicalText,
3895
+ category: meta.category || "other",
3896
+ priority: meta.priority || "medium",
3897
+ status: meta.questionStatus || q.status,
3898
+ questionType: q.questionType || meta.questionType || "general",
3899
+ testType: meta.testType || void 0,
3900
+ importance: meta.importance || void 0,
3901
+ isKeyQuestion: meta.isKeyQuestion || false,
3902
+ conviction: meta.conviction,
3903
+ convictionStage: meta.convictionStage,
3904
+ convictionRationale: meta.convictionRationale,
3905
+ convictionAdvancedAt: meta.convictionAdvancedAt,
3906
+ convictionAdvancedBy: meta.convictionAdvancedBy,
3907
+ convictionUpdatedAt: meta.convictionUpdatedAt,
3908
+ convictionUpdatedBy: meta.convictionUpdatedBy,
3909
+ answer: meta.answer,
3910
+ answerStatus: meta.answerStatus,
3911
+ answerCompleteness: meta.answerCompleteness,
3912
+ whatWeNeed: meta.whatWeNeed,
3913
+ linkedWorktreeId: resolveLinkedWorktreeId(meta) || void 0,
3914
+ beliefId: meta.linkedBeliefNodeId || meta.beliefId || null,
3915
+ linkedBeliefId: meta.linkedBeliefNodeId || meta.linkedBeliefId,
3916
+ relatedBeliefIds: relatedIds,
3917
+ evidenceCount: evidenceWithDetails.length,
3918
+ evidence: evidenceWithDetails,
3919
+ testedBeliefs: beliefsWithDetails.filter(Boolean),
3920
+ createdAt: q.createdAt,
3921
+ createdBy: q.createdBy,
3922
+ updatedAt: q.updatedAt,
3923
+ metadata: q.metadata
3924
+ };
3925
+ })
3926
+ );
3927
+ return questionsWithDetails.sort((a, b) => {
3928
+ if (a.convictionStage === "in_conviction" && b.convictionStage === "scored") {
3929
+ return -1;
3930
+ }
3931
+ if (a.convictionStage === "scored" && b.convictionStage === "in_conviction") {
3932
+ return 1;
3933
+ }
3934
+ return (a.importance || 5) - (b.importance || 5);
3935
+ });
3936
+ }
3937
+ });
3938
+ var advanceToConviction = mutation({
3939
+ args: {
3940
+ questionId: v.id("epistemicNodes"),
3941
+ userId: v.string(),
3942
+ sprintId: v.optional(v.string()),
3943
+ worktreeId: v.optional(v.string())
3944
+ },
3945
+ returns: permissiveReturn,
3946
+ handler: async (ctx, args) => {
3947
+ const node = await ctx.db.get(args.questionId);
3948
+ if (!node || node.nodeType !== "question") {
3949
+ throw new Error("Question not found");
3950
+ }
3951
+ const meta = node.metadata || {};
3952
+ if (meta.convictionStage === "in_conviction" || meta.convictionStage === "scored") {
3953
+ throw new Error("Question is already in conviction stage");
3954
+ }
3955
+ const now = Date.now();
3956
+ await ctx.db.patch(args.questionId, {
3957
+ updatedAt: now,
3958
+ metadata: {
3959
+ ...meta,
3960
+ convictionStage: "in_conviction",
3961
+ convictionAdvancedAt: now,
3962
+ convictionAdvancedBy: args.userId,
3963
+ ...buildLinkedWorktreeMetadata(args.worktreeId ?? args.sprintId)
3964
+ }
3965
+ });
3966
+ try {
3967
+ await ctx.db.insert("epistemicAudit", {
3968
+ entityType: "question",
3969
+ entityId: args.questionId,
3970
+ changeType: "status_changed",
3971
+ changedAt: now,
3972
+ changedBy: args.userId,
3973
+ isAgent: false,
3974
+ projectId: node.projectId,
3975
+ previousState: {
3976
+ convictionStage: meta.convictionStage || "researching"
3977
+ },
3978
+ newState: { convictionStage: "in_conviction" },
3979
+ triggeringAction: "question_advanced_to_conviction"
3980
+ });
3981
+ } catch (e) {
3982
+ console.error("[EpistemicAudit] Failed to log advanceToConviction:", e);
3983
+ }
3984
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
3985
+ return {
3986
+ questionId: args.questionId,
3987
+ convictionStage: "in_conviction",
3988
+ advancedAt: now
3989
+ };
3990
+ }
3991
+ });
3992
+ var updateConviction = mutation({
3993
+ args: {
3994
+ questionId: v.id("epistemicNodes"),
3995
+ conviction: v.optional(v.number()),
3996
+ answerCompleteness: v.optional(
3997
+ v.union(
3998
+ v.literal("unanswered"),
3999
+ v.literal("partial"),
4000
+ v.literal("sufficient"),
4001
+ v.literal("comprehensive")
4002
+ )
4003
+ ),
4004
+ convictionRationale: v.optional(v.string()),
4005
+ userId: v.string()
4006
+ },
4007
+ returns: permissiveReturn,
4008
+ handler: async (ctx, args) => {
4009
+ const node = await ctx.db.get(args.questionId);
4010
+ if (!node || node.nodeType !== "question") {
4011
+ throw new Error("Question not found");
4012
+ }
4013
+ const now = Date.now();
4014
+ const meta = node.metadata || {};
4015
+ const updates = { ...meta };
4016
+ if (args.conviction !== void 0) {
4017
+ updates.conviction = Math.max(0, Math.min(1, args.conviction));
4018
+ }
4019
+ if (args.answerCompleteness !== void 0) {
4020
+ updates.answerCompleteness = args.answerCompleteness;
4021
+ }
4022
+ if (args.convictionRationale !== void 0) {
4023
+ updates.convictionRationale = args.convictionRationale;
4024
+ }
4025
+ updates.convictionUpdatedAt = now;
4026
+ updates.convictionUpdatedBy = args.userId;
4027
+ await ctx.db.patch(args.questionId, {
4028
+ updatedAt: now,
4029
+ metadata: updates
4030
+ });
4031
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
4032
+ return {
4033
+ questionId: args.questionId,
4034
+ conviction: updates.conviction ?? meta.conviction,
4035
+ answerCompleteness: updates.answerCompleteness ?? meta.answerCompleteness
4036
+ };
4037
+ }
4038
+ });
4039
+ var finalizeConviction = mutation({
4040
+ args: {
4041
+ questionId: v.id("epistemicNodes"),
4042
+ conviction: v.number(),
4043
+ answer: v.string(),
4044
+ convictionRationale: v.optional(v.string()),
4045
+ userId: v.string(),
4046
+ answerCompleteness: v.optional(
4047
+ v.union(
4048
+ v.literal("unanswered"),
4049
+ v.literal("partial"),
4050
+ v.literal("sufficient"),
4051
+ v.literal("comprehensive"),
4052
+ v.literal("unanswerable")
4053
+ )
4054
+ ),
4055
+ whatWeNeed: v.optional(v.string())
4056
+ },
4057
+ returns: permissiveReturn,
4058
+ handler: async (ctx, args) => {
4059
+ const node = await ctx.db.get(args.questionId);
4060
+ if (!node || node.nodeType !== "question") {
4061
+ throw new Error("Question not found");
4062
+ }
4063
+ const now = Date.now();
4064
+ const clampedConviction = Math.max(0, Math.min(1, args.conviction));
4065
+ const isUnanswerable = args.answerCompleteness === "unanswerable";
4066
+ const meta = node.metadata || {};
4067
+ let completeness = args.answerCompleteness;
4068
+ if (!completeness) {
4069
+ completeness = clampedConviction >= 0.8 ? "comprehensive" : clampedConviction >= 0.6 ? "sufficient" : clampedConviction >= 0.3 ? "partial" : "unanswered";
4070
+ }
4071
+ await ctx.db.patch(args.questionId, {
4072
+ updatedAt: now,
4073
+ metadata: {
4074
+ ...meta,
4075
+ convictionStage: "scored",
4076
+ conviction: clampedConviction,
4077
+ convictionRationale: args.convictionRationale,
4078
+ convictionUpdatedAt: now,
4079
+ convictionUpdatedBy: args.userId,
4080
+ answer: args.answer,
4081
+ answerStatus: "final",
4082
+ questionStatus: isUnanswerable ? "blocked" : "answered",
4083
+ answeredAt: now,
4084
+ answeredBy: args.userId,
4085
+ answerCompleteness: completeness,
4086
+ whatWeNeed: args.whatWeNeed
4087
+ }
4088
+ });
4089
+ try {
4090
+ await ctx.db.insert("epistemicAudit", {
4091
+ entityType: "question",
4092
+ entityId: args.questionId,
4093
+ changeType: "status_changed",
4094
+ changedAt: now,
4095
+ changedBy: args.userId,
4096
+ isAgent: false,
4097
+ projectId: node.projectId,
4098
+ previousState: {
4099
+ convictionStage: meta.convictionStage || "in_conviction"
4100
+ },
4101
+ newState: {
4102
+ convictionStage: "scored",
4103
+ conviction: clampedConviction
4104
+ },
4105
+ triggeringAction: "question_conviction_finalized"
4106
+ });
4107
+ } catch (e) {
4108
+ console.error("[EpistemicAudit] Failed to log finalizeConviction:", e);
4109
+ }
4110
+ if (node.projectId || node.topicId) {
4111
+ const beliefId = meta.linkedBeliefNodeId || meta.linkedBeliefId || meta.beliefId;
4112
+ if (beliefId) {
4113
+ try {
4114
+ await ctx.scheduler.runAfter(
4115
+ 0,
4116
+ internal.epistemicQuestions.createEvidenceFromScoredQuestion,
4117
+ {
4118
+ questionNodeId: args.questionId,
4119
+ questionText: node.canonicalText || "",
4120
+ answerText: args.answer,
4121
+ beliefId,
4122
+ relatedBeliefIds: meta.relatedBeliefIds || [],
4123
+ conviction: clampedConviction,
4124
+ rationale: args.convictionRationale || "",
4125
+ projectId: node.projectId,
4126
+ topicId: normalizeQuestionTopicId(node.topicId),
4127
+ userId: args.userId
4128
+ }
4129
+ );
4130
+ } catch (e) {
4131
+ console.error(
4132
+ "[finalizeConviction] Failed to schedule evidence creation:",
4133
+ e
4134
+ );
4135
+ }
4136
+ }
4137
+ }
4138
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4139
+ nodeId: args.questionId,
4140
+ operation: "upsert"
4141
+ });
4142
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
4143
+ return {
4144
+ questionId: args.questionId,
4145
+ convictionStage: "scored",
4146
+ conviction: clampedConviction
4147
+ };
4148
+ }
4149
+ });
4150
+ var getByBeliefWithAccess = query({
4151
+ args: {
4152
+ beliefId: v.string(),
4153
+ userId: v.string()
4154
+ },
4155
+ returns: permissiveReturn,
4156
+ handler: async (ctx, args) => {
4157
+ let beliefNode = null;
4158
+ try {
4159
+ beliefNode = await ctx.db.get(args.beliefId);
4160
+ } catch {
4161
+ try {
4162
+ beliefNode = await ctx.db.get(args.beliefId);
4163
+ } catch {
4164
+ return [];
4165
+ }
4166
+ }
4167
+ if (!beliefNode) {
4168
+ return [];
4169
+ }
4170
+ const scope = await resolveQuestionScopeOrNull(ctx, {
4171
+ projectId: beliefNode.projectId,
4172
+ topicId: normalizeQuestionTopicId(beliefNode.topicId)
4173
+ });
4174
+ const scopeId = scope ? resolveQuestionScopeId(scope) : void 0;
4175
+ if (!scope || !scopeId) {
4176
+ return [];
4177
+ }
4178
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
4179
+ if (!hasAccess) {
4180
+ return [];
4181
+ }
4182
+ const questionNodes = await getQuestionNodesForScope(ctx, scope);
4183
+ const beliefIdStr = String(args.beliefId);
4184
+ return questionNodes.filter((q) => {
4185
+ const meta = q.metadata || {};
4186
+ if (String(meta.linkedBeliefNodeId || "") === beliefIdStr) {
4187
+ return true;
4188
+ }
4189
+ if (String(meta.linkedBeliefId || "") === beliefIdStr) {
4190
+ return true;
4191
+ }
4192
+ if (String(meta.beliefId || "") === beliefIdStr) {
4193
+ return true;
4194
+ }
4195
+ const relatedIds = meta.relatedBeliefIds || [];
4196
+ if (relatedIds.some((id) => String(id) === beliefIdStr)) {
4197
+ return true;
4198
+ }
4199
+ return false;
4200
+ });
4201
+ }
4202
+ });
4203
+ var updateQuestion = mutation({
4204
+ args: {
4205
+ questionId: v.id("epistemicNodes"),
4206
+ question: v.optional(v.string()),
4207
+ category: v.optional(
4208
+ v.union(
4209
+ v.literal("market"),
4210
+ v.literal("competition"),
4211
+ v.literal("product"),
4212
+ v.literal("team"),
4213
+ v.literal("financials"),
4214
+ v.literal("financial"),
4215
+ v.literal("regulatory"),
4216
+ v.literal("timing"),
4217
+ v.literal("customer"),
4218
+ v.literal("technology"),
4219
+ v.literal("distribution"),
4220
+ v.literal("strategic"),
4221
+ v.literal("other")
4222
+ )
4223
+ ),
4224
+ priority: v.optional(
4225
+ v.union(v.literal("high"), v.literal("medium"), v.literal("low"))
4226
+ )
4227
+ },
4228
+ returns: permissiveReturn,
4229
+ handler: async (ctx, args) => {
4230
+ const node = await ctx.db.get(args.questionId);
4231
+ if (!node || node.nodeType !== "question") {
4232
+ throw new Error("Question not found");
4233
+ }
4234
+ const now = Date.now();
4235
+ const meta = node.metadata || {};
4236
+ const metaUpdates = { ...meta };
4237
+ if (args.category) {
4238
+ const category = args.category === "financial" ? "financials" : args.category;
4239
+ metaUpdates.category = category;
4240
+ }
4241
+ if (args.priority) {
4242
+ metaUpdates.priority = args.priority;
4243
+ }
4244
+ const patchData = {
4245
+ updatedAt: now,
4246
+ metadata: metaUpdates
4247
+ };
4248
+ if (args.question !== void 0) {
4249
+ patchData.canonicalText = args.question;
4250
+ patchData.contentHash = generateContentHash(args.question);
4251
+ if (node.sourceType === "ai_generated") {
4252
+ patchData.sourceType = "human";
4253
+ }
4254
+ }
4255
+ await ctx.db.patch(args.questionId, patchData);
4256
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4257
+ nodeId: args.questionId,
4258
+ operation: "upsert"
4259
+ });
4260
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
4261
+ }
4262
+ });
4263
+ var linkToBelief = mutation({
4264
+ args: {
4265
+ questionId: v.id("epistemicNodes"),
4266
+ beliefId: v.string(),
4267
+ // Accept string for backward compat
4268
+ userId: v.string(),
4269
+ testType: v.optional(
4270
+ v.union(
4271
+ v.literal("validates"),
4272
+ v.literal("invalidates"),
4273
+ v.literal("clarifies")
4274
+ )
4275
+ ),
4276
+ answerImpact: v.optional(
4277
+ v.object({
4278
+ ifYes: v.string(),
4279
+ ifNo: v.string()
4280
+ })
4281
+ ),
4282
+ isPrimaryBelief: v.optional(v.boolean())
4283
+ },
4284
+ returns: permissiveReturn,
4285
+ handler: async (ctx, args) => {
4286
+ const questionNode = await ctx.db.get(args.questionId);
4287
+ if (!questionNode || questionNode.nodeType !== "question") {
4288
+ throw new Error("Question not found");
4289
+ }
4290
+ const questionScopeId = resolveQuestionScopeId({
4291
+ projectId: questionNode.projectId,
4292
+ topicId: normalizeQuestionTopicId(questionNode.topicId)
4293
+ });
4294
+ if (questionScopeId) {
4295
+ const hasAccess = await checkScopeAccess(
4296
+ ctx,
4297
+ questionScopeId,
4298
+ args.userId
4299
+ );
4300
+ if (!hasAccess) {
4301
+ throw new Error("Access denied");
4302
+ }
4303
+ }
4304
+ let beliefNode = null;
4305
+ try {
4306
+ beliefNode = await ctx.db.get(args.beliefId);
4307
+ } catch {
4308
+ try {
4309
+ beliefNode = await ctx.db.get(args.beliefId);
4310
+ } catch {
4311
+ throw new Error("Belief not found");
4312
+ }
4313
+ }
4314
+ if (!beliefNode) {
4315
+ throw new Error("Belief not found");
4316
+ }
4317
+ const now = Date.now();
4318
+ const meta = questionNode.metadata || {};
4319
+ const currentBeliefs = meta.relatedBeliefIds || [];
4320
+ const beliefIdStr = String(args.beliefId);
4321
+ const newRelated = currentBeliefs.includes(beliefIdStr) ? currentBeliefs : [...currentBeliefs, beliefIdStr];
4322
+ const metaUpdates = {
4323
+ ...meta,
4324
+ relatedBeliefIds: newRelated
4325
+ };
4326
+ if (args.isPrimaryBelief || args.testType) {
4327
+ metaUpdates.linkedBeliefNodeId = args.beliefId;
4328
+ metaUpdates.linkedBeliefId = args.beliefId;
4329
+ metaUpdates.questionType = "belief_test";
4330
+ if (args.testType) {
4331
+ metaUpdates.testType = args.testType;
4332
+ }
4333
+ if (args.answerImpact) {
4334
+ metaUpdates.answerImpact = args.answerImpact;
4335
+ }
4336
+ }
4337
+ await ctx.db.patch(args.questionId, {
4338
+ updatedAt: now,
4339
+ metadata: metaUpdates
4340
+ });
4341
+ try {
4342
+ const beliefGlobalId = beliefNode.globalId;
4343
+ if (beliefGlobalId && questionNode.globalId) {
4344
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4345
+ nodeId: args.questionId,
4346
+ operation: "upsert"
4347
+ });
4348
+ await ctx.scheduler.runAfter(100, internal.neo4jSync.syncNodeToNeo4j, {
4349
+ nodeId: args.beliefId,
4350
+ operation: "upsert"
4351
+ });
4352
+ const edgeGlobalId = buildTestsEdgeGlobalId(
4353
+ questionNode.globalId,
4354
+ beliefGlobalId
4355
+ );
4356
+ await ctx.scheduler.runAfter(250, internal.neo4jEdgeAPI.createEdge, {
4357
+ globalId: edgeGlobalId,
4358
+ fromGlobalId: questionNode.globalId,
4359
+ toGlobalId: beliefGlobalId,
4360
+ edgeType: "tests",
4361
+ context: args.testType || "tests",
4362
+ topicId: questionNode.projectId ? String(questionNode.projectId) : void 0,
4363
+ createdBy: args.userId,
4364
+ fromNodeType: "question",
4365
+ toNodeType: "belief",
4366
+ fromLayer: "L3",
4367
+ toLayer: "L3"
4368
+ });
4369
+ }
4370
+ } catch (e) {
4371
+ console.error("[linkToBelief] Failed to create tests edge:", e);
4372
+ }
4373
+ await markProjectGraphDirty(
4374
+ ctx,
4375
+ questionNode.projectId,
4376
+ questionNode.topicId
4377
+ );
4378
+ }
4379
+ });
4380
+ var linkToInsight = mutation({
4381
+ args: {
4382
+ questionId: v.id("epistemicNodes"),
4383
+ insightId: v.id("epistemicNodes"),
4384
+ userId: v.string(),
4385
+ sprintId: v.optional(v.string()),
4386
+ worktreeId: v.optional(v.string()),
4387
+ relevance: v.optional(v.number()),
4388
+ rationale: v.optional(v.string())
4389
+ },
4390
+ returns: permissiveReturn,
4391
+ handler: async (ctx, args) => {
4392
+ const questionNode = await ctx.db.get(args.questionId);
4393
+ if (!questionNode || questionNode.nodeType !== "question") {
4394
+ throw new Error("Question not found");
4395
+ }
4396
+ const insight = await ctx.db.get(args.insightId);
4397
+ if (!insight) {
4398
+ throw new Error("Insight not found");
4399
+ }
4400
+ const questionScopeId = resolveQuestionScopeId({
4401
+ projectId: questionNode.projectId,
4402
+ topicId: normalizeQuestionTopicId(questionNode.topicId)
4403
+ });
4404
+ if (questionScopeId) {
4405
+ const hasAccess = await checkScopeAccess(
4406
+ ctx,
4407
+ questionScopeId,
4408
+ args.userId
4409
+ );
4410
+ if (!hasAccess) {
4411
+ throw new Error("Access denied");
4412
+ }
4413
+ }
4414
+ const now = Date.now();
4415
+ const meta = questionNode.metadata || {};
4416
+ const currentInsights = meta.relatedInsightIds || [];
4417
+ const insightIdStr = String(args.insightId);
4418
+ if (!currentInsights.includes(insightIdStr)) {
4419
+ await ctx.db.patch(args.questionId, {
4420
+ updatedAt: now,
4421
+ metadata: {
4422
+ ...meta,
4423
+ relatedInsightIds: [...currentInsights, insightIdStr]
4424
+ }
4425
+ });
4426
+ try {
4427
+ const existingLinks = await ctx.db.query("questionEvidenceLinks").withIndex(
4428
+ "by_questionId",
4429
+ (q) => q.eq("questionId", args.questionId)
4430
+ ).collect();
4431
+ const duplicate = existingLinks.find(
4432
+ (link) => String(link.insightId) === String(args.insightId)
4433
+ );
4434
+ if (!duplicate) {
4435
+ await ctx.db.insert("questionEvidenceLinks", {
4436
+ questionId: args.questionId,
4437
+ insightId: args.insightId,
4438
+ helpsAnswer: true,
4439
+ relevance: args.relevance ?? 0.7,
4440
+ rationale: args.rationale || "Linked from research results",
4441
+ createdBy: args.userId,
4442
+ createdAt: now
4443
+ });
4444
+ }
4445
+ } catch (e) {
4446
+ console.error(
4447
+ "[linkToInsight] Failed to create questionEvidenceLink:",
4448
+ e
4449
+ );
4450
+ }
4451
+ }
4452
+ await markProjectGraphDirty(
4453
+ ctx,
4454
+ questionNode.projectId,
4455
+ questionNode.topicId
4456
+ );
4457
+ }
4458
+ });
4459
+ var unlinkInsight = mutation({
4460
+ args: {
4461
+ questionId: v.id("epistemicNodes"),
4462
+ insightId: v.id("epistemicNodes"),
4463
+ userId: v.string()
4464
+ },
4465
+ returns: permissiveReturn,
4466
+ handler: async (ctx, args) => {
4467
+ const questionNode = await ctx.db.get(args.questionId);
4468
+ if (!questionNode || questionNode.nodeType !== "question") {
4469
+ throw new Error("Question not found");
4470
+ }
4471
+ const questionScopeId = resolveQuestionScopeId({
4472
+ projectId: questionNode.projectId,
4473
+ topicId: normalizeQuestionTopicId(questionNode.topicId)
4474
+ });
4475
+ if (questionScopeId) {
4476
+ const hasAccess = await checkScopeAccess(
4477
+ ctx,
4478
+ questionScopeId,
4479
+ args.userId
4480
+ );
4481
+ if (!hasAccess) {
4482
+ throw new Error("Access denied");
4483
+ }
4484
+ }
4485
+ const now = Date.now();
4486
+ const meta = questionNode.metadata || {};
4487
+ const currentInsights = meta.relatedInsightIds || [];
4488
+ const newInsights = currentInsights.filter(
4489
+ (id) => String(id) !== String(args.insightId)
4490
+ );
4491
+ await ctx.db.patch(args.questionId, {
4492
+ updatedAt: now,
4493
+ metadata: {
4494
+ ...meta,
4495
+ relatedInsightIds: newInsights
4496
+ }
4497
+ });
4498
+ try {
4499
+ const links = await ctx.db.query("questionEvidenceLinks").withIndex(
4500
+ "by_questionId",
4501
+ (q) => q.eq("questionId", args.questionId)
4502
+ ).collect();
4503
+ for (const link of links) {
4504
+ if (String(link.insightId) === String(args.insightId)) {
4505
+ await ctx.db.delete(link._id);
4506
+ }
4507
+ }
4508
+ } catch (e) {
4509
+ console.error(
4510
+ "[unlinkInsight] Failed to remove questionEvidenceLink:",
4511
+ e
4512
+ );
4513
+ }
4514
+ try {
4515
+ const evidenceNode = await ctx.db.get(args.insightId);
4516
+ if (evidenceNode && evidenceNode.nodeType === "evidence") {
4517
+ const edges = await ctx.db.query("epistemicEdges").withIndex(
4518
+ "by_from_to",
4519
+ (q) => q.eq("fromNodeId", evidenceNode._id).eq("toNodeId", args.questionId)
4520
+ ).collect();
4521
+ for (const edge of edges) {
4522
+ if (edge.edgeType === "derived_from") {
4523
+ await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.deleteEdge, {
4524
+ globalId: edge.globalId
4525
+ });
4526
+ await ctx.db.delete(edge._id);
4527
+ }
4528
+ }
4529
+ }
4530
+ } catch (e) {
4531
+ console.error("[unlinkInsight] Failed to remove edge:", e);
4532
+ }
4533
+ await markProjectGraphDirty(
4534
+ ctx,
4535
+ questionNode.projectId,
4536
+ questionNode.topicId
4537
+ );
4538
+ }
4539
+ });
4540
+ var list = query({
4541
+ args: {
4542
+ ...optionalScopeArgs,
4543
+ userId: v.string(),
4544
+ status: v.optional(v.string())
4545
+ },
4546
+ returns: permissiveReturn,
4547
+ handler: async (ctx, args) => {
4548
+ const scope = await resolveQuestionScopeOrNull(ctx, args);
4549
+ if (!scope) {
4550
+ return [];
4551
+ }
4552
+ const scopeId = resolveQuestionScopeId(scope);
4553
+ if (!scopeId) {
4554
+ return [];
4555
+ }
4556
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
4557
+ if (!hasAccess) {
4558
+ return [];
4559
+ }
4560
+ const nodes = await getQuestionNodesForScope(ctx, scope);
4561
+ const filtered = args.status ? nodes.filter(
4562
+ (node) => matchesRequestedQuestionStatus(node, args.status)
4563
+ ) : nodes.filter(isActiveQuestionNode);
4564
+ return filtered.map(flattenInternalQuestionNode);
4565
+ }
4566
+ });
4567
+ var getByPillar = query({
4568
+ args: {
4569
+ ...optionalScopeArgs,
4570
+ pillar: v.string(),
4571
+ includeAnswered: v.optional(v.boolean()),
4572
+ userId: v.string()
4573
+ },
4574
+ returns: permissiveReturn,
4575
+ handler: async (ctx, args) => {
4576
+ const scope = await resolveQuestionScopeOrNull(ctx, args);
4577
+ if (!scope) {
4578
+ return { questions: [], beliefs: [] };
4579
+ }
4580
+ const scopeId = resolveQuestionScopeId(scope);
4581
+ if (!scopeId) {
4582
+ return { questions: [], beliefs: [] };
4583
+ }
4584
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
4585
+ if (!hasAccess) {
4586
+ return { questions: [], beliefs: [] };
4587
+ }
4588
+ const allBeliefNodes = await ctx.db.query("epistemicNodes").withIndex(
4589
+ scope.topicId ? "by_topic_type" : "by_project_type",
4590
+ (q) => scope.topicId ? q.eq("topicId", scope.topicId).eq("nodeType", "belief") : q.eq("projectId", scope.projectId).eq("nodeType", "belief")
4591
+ ).collect();
4592
+ const beliefs = allBeliefNodes.filter((n) => {
4593
+ const meta = n.metadata || {};
4594
+ return (meta.category === args.pillar || meta.topic === args.pillar || meta.pillar === args.pillar) && n.status !== "archived";
4595
+ });
4596
+ if (beliefs.length === 0) {
4597
+ return { questions: [], beliefs: [] };
4598
+ }
4599
+ const beliefIds = new Set(beliefs.map((b) => String(b._id)));
4600
+ const beliefMap = new Map(
4601
+ beliefs.map((b) => [String(b._id), b.canonicalText || ""])
4602
+ );
4603
+ const questionNodes = await getQuestionNodesForScope(ctx, scope);
4604
+ const pillarQuestions = questionNodes.filter((q) => {
4605
+ const meta = q.metadata || {};
4606
+ const linkedId = String(
4607
+ meta.linkedBeliefNodeId || meta.linkedBeliefId || meta.beliefId || ""
4608
+ );
4609
+ if (linkedId && beliefIds.has(linkedId)) {
4610
+ return true;
4611
+ }
4612
+ const relatedIds = meta.relatedBeliefIds || [];
4613
+ return relatedIds.some((id) => beliefIds.has(String(id)));
4614
+ });
4615
+ const includeAnswered = args.includeAnswered ?? true;
4616
+ const filteredQuestions = includeAnswered ? pillarQuestions : pillarQuestions.filter((q) => {
4617
+ const meta = q.metadata || {};
4618
+ return meta.questionStatus !== "answered";
4619
+ });
4620
+ const enrichedQuestions = filteredQuestions.map((q) => {
4621
+ const meta = q.metadata || {};
4622
+ const linkedId = String(
4623
+ meta.linkedBeliefNodeId || meta.linkedBeliefId || meta.beliefId || ""
4624
+ );
4625
+ return {
4626
+ _id: q._id,
4627
+ _creationTime: q._creationTime,
4628
+ projectId: q.projectId,
4629
+ question: q.canonicalText,
4630
+ canonicalText: q.canonicalText,
4631
+ category: meta.category || "other",
4632
+ priority: meta.priority || "medium",
4633
+ status: meta.questionStatus || q.status,
4634
+ questionType: q.questionType || meta.questionType || "general",
4635
+ beliefId: linkedId || void 0,
4636
+ beliefText: linkedId ? beliefMap.get(linkedId) : void 0,
4637
+ createdAt: q.createdAt,
4638
+ createdBy: q.createdBy,
4639
+ updatedAt: q.updatedAt
4640
+ };
4641
+ });
4642
+ const priorityOrder = {
4643
+ high: 0,
4644
+ medium: 1,
4645
+ low: 2
4646
+ };
4647
+ enrichedQuestions.sort((a, b) => {
4648
+ const aPriority = priorityOrder[a.priority || "medium"] ?? 1;
4649
+ const bPriority = priorityOrder[b.priority || "medium"] ?? 1;
4650
+ if (aPriority !== bPriority) {
4651
+ return aPriority - bPriority;
4652
+ }
4653
+ return (b.createdAt || 0) - (a.createdAt || 0);
4654
+ });
4655
+ return {
4656
+ questions: enrichedQuestions,
4657
+ beliefs: beliefs.map((b) => ({
4658
+ _id: b._id,
4659
+ belief: b.canonicalText,
4660
+ pillar: b.metadata?.category || b.metadata?.pillar,
4661
+ confidence: b.metadata?.confidence
4662
+ }))
4663
+ };
4664
+ }
4665
+ });
4666
+ var consolidate = mutation({
4667
+ args: {
4668
+ ...optionalScopeArgs,
4669
+ questionIds: v.array(v.string()),
4670
+ // Accept string[] for backward compat
4671
+ consolidatedQuestion: v.string(),
4672
+ consolidationRationale: v.optional(v.string()),
4673
+ linkedBeliefIds: v.optional(v.array(v.string())),
4674
+ priority: v.optional(
4675
+ v.union(v.literal("high"), v.literal("medium"), v.literal("low"))
4676
+ ),
4677
+ sprintId: v.optional(v.string()),
4678
+ consolidatedBy: v.string()
4679
+ },
4680
+ returns: permissiveReturn,
4681
+ handler: async (ctx, args) => {
4682
+ const scope = await resolveTopicProjectScope(ctx, {
4683
+ projectId: args.projectId,
4684
+ topicId: args.topicId
4685
+ });
4686
+ const scopeId = resolveQuestionScopeId(scope);
4687
+ if (!scopeId) {
4688
+ return [];
4689
+ }
4690
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.consolidatedBy);
4691
+ if (!hasAccess) {
4692
+ throw new Error("Access denied");
4693
+ }
4694
+ if (args.questionIds.length < 2) {
4695
+ throw new Error("At least 2 questions required for consolidation");
4696
+ }
4697
+ const originalQuestions = await Promise.all(
4698
+ args.questionIds.map(async (qid) => {
4699
+ try {
4700
+ return await ctx.db.get(qid);
4701
+ } catch {
4702
+ return null;
4703
+ }
4704
+ })
4705
+ );
4706
+ const validQuestions = originalQuestions.filter((q) => {
4707
+ if (!q || q.nodeType !== "question") {
4708
+ return false;
4709
+ }
4710
+ if (scope.topicId) {
4711
+ return String(q.topicId) === String(scope.topicId) || !q.topicId && scope.projectId !== void 0 && String(q.projectId) === String(scope.projectId);
4712
+ }
4713
+ return String(q.projectId) === String(scope.projectId);
4714
+ });
4715
+ if (validQuestions.length !== args.questionIds.length) {
4716
+ throw new Error(
4717
+ "Some questions not found or do not belong to this topic scope"
4718
+ );
4719
+ }
4720
+ const priorityOrder = {
4721
+ high: 0,
4722
+ medium: 1,
4723
+ low: 2
4724
+ };
4725
+ let bestPriority = args.priority;
4726
+ if (!bestPriority) {
4727
+ let bestScore = 2;
4728
+ for (const q of validQuestions) {
4729
+ const meta = q?.metadata || {};
4730
+ const score = priorityOrder[meta.priority || "medium"] ?? 1;
4731
+ if (score < bestScore) {
4732
+ bestScore = score;
4733
+ bestPriority = meta.priority || "medium";
4734
+ }
4735
+ }
4736
+ }
4737
+ const linkedBeliefSet = /* @__PURE__ */ new Set();
4738
+ if (args.linkedBeliefIds) {
4739
+ for (const bid of args.linkedBeliefIds) {
4740
+ linkedBeliefSet.add(String(bid));
4741
+ }
4742
+ } else {
4743
+ for (const q of validQuestions) {
4744
+ const meta = q?.metadata || {};
4745
+ if (meta.linkedBeliefNodeId) {
4746
+ linkedBeliefSet.add(String(meta.linkedBeliefNodeId));
4747
+ }
4748
+ if (meta.linkedBeliefId) {
4749
+ linkedBeliefSet.add(String(meta.linkedBeliefId));
4750
+ }
4751
+ }
4752
+ }
4753
+ let category = "other";
4754
+ if (linkedBeliefSet.size > 0) {
4755
+ const firstBeliefId = Array.from(linkedBeliefSet)[0];
4756
+ try {
4757
+ const belief = await ctx.db.get(firstBeliefId);
4758
+ if (belief) {
4759
+ const bMeta = belief.metadata || {};
4760
+ category = bMeta.category || bMeta.pillar || "other";
4761
+ }
4762
+ } catch {
4763
+ }
4764
+ }
4765
+ const now = Date.now();
4766
+ const globalId = generateGlobalId();
4767
+ const contentHash = generateContentHash(args.consolidatedQuestion);
4768
+ const newNodeId = await ctx.db.insert("epistemicNodes", {
4769
+ globalId,
4770
+ projectId: scope.projectId,
4771
+ topicId: scope.topicId,
4772
+ nodeType: "question",
4773
+ canonicalText: args.consolidatedQuestion,
4774
+ contentHash,
4775
+ status: "active",
4776
+ epistemicLayer: "L3",
4777
+ sourceType: "human",
4778
+ createdAt: now,
4779
+ updatedAt: now,
4780
+ createdBy: args.consolidatedBy,
4781
+ metadata: {
4782
+ category,
4783
+ priority: bestPriority || "medium",
4784
+ source: "consolidated",
4785
+ questionStatus: "open",
4786
+ linkedBeliefNodeId: linkedBeliefSet.size > 0 ? Array.from(linkedBeliefSet)[0] : void 0,
4787
+ consolidationMetadata: {
4788
+ originalQuestionIds: args.questionIds,
4789
+ rationale: args.consolidationRationale,
4790
+ consolidatedAt: now,
4791
+ consolidatedBy: args.consolidatedBy
4792
+ }
4793
+ }
4794
+ });
4795
+ for (const qid of args.questionIds) {
4796
+ try {
4797
+ const node = await ctx.db.get(qid);
4798
+ if (node) {
4799
+ const meta = node.metadata || {};
4800
+ await ctx.db.patch(qid, {
4801
+ status: "archived",
4802
+ updatedAt: now,
4803
+ metadata: {
4804
+ ...meta,
4805
+ questionStatus: "archived",
4806
+ archivedAt: now,
4807
+ archivedReason: `Consolidated into question ${newNodeId}`,
4808
+ consolidatedIntoId: newNodeId
4809
+ }
4810
+ });
4811
+ }
4812
+ } catch {
4813
+ }
4814
+ }
4815
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4816
+ nodeId: newNodeId,
4817
+ operation: "upsert"
4818
+ });
4819
+ await markProjectGraphDirty(
4820
+ ctx,
4821
+ scope.projectId,
4822
+ normalizeQuestionTopicId(scope.topicId)
4823
+ );
4824
+ return {
4825
+ newQuestionId: newNodeId,
4826
+ archivedCount: args.questionIds.length,
4827
+ linkedBeliefs: Array.from(linkedBeliefSet)
4828
+ };
4829
+ }
4830
+ });
4831
+ var deleteQuestion = mutation({
4832
+ args: {
4833
+ questionId: v.id("epistemicNodes"),
4834
+ userId: v.string(),
4835
+ runtimeToolName: v.optional(v.string()),
4836
+ runtimePackKey: v.optional(v.string()),
4837
+ runtimePackInstallScope: v.optional(
4838
+ v.union(v.literal("tenant"), v.literal("workspace"))
4839
+ )
4840
+ },
4841
+ returns: permissiveReturn,
4842
+ handler: async (ctx, args) => {
4843
+ const node = await ctx.db.get(args.questionId);
4844
+ if (!node || node.nodeType !== "question") {
4845
+ throw new Error("Question not found");
4846
+ }
4847
+ assertTenantPackWorkspaceMutationAllowed({
4848
+ runtime: resolveRuntimePackMutationContext(args),
4849
+ target: await resolveNodeScopeForWorkspaceIsolation(ctx, node),
4850
+ mutationName: "epistemicQuestions.deleteQuestion"
4851
+ });
4852
+ const now = Date.now();
4853
+ const meta = node.metadata || {};
4854
+ await ctx.db.patch(args.questionId, {
4855
+ status: "archived",
4856
+ updatedAt: now,
4857
+ metadata: {
4858
+ ...meta,
4859
+ questionStatus: "archived",
4860
+ archivedAt: now,
4861
+ archivedBy: args.userId
4862
+ }
4863
+ });
4864
+ await ctx.db.insert("epistemicAudit", {
4865
+ entityType: "question",
4866
+ entityId: args.questionId,
4867
+ changeType: "status_changed",
4868
+ changedAt: now,
4869
+ changedBy: args.userId,
4870
+ isAgent: false,
4871
+ projectId: node.projectId,
4872
+ previousState: {
4873
+ status: meta.questionStatus || node.status
4874
+ },
4875
+ newState: { status: "archived" }
4876
+ });
4877
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4878
+ nodeId: args.questionId,
4879
+ operation: "upsert"
4880
+ });
4881
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
4882
+ }
4883
+ });
4884
+ var markAnsweredWithArtifact = mutation({
4885
+ args: {
4886
+ questionId: v.id("epistemicNodes"),
4887
+ artifactId: v.id("finalArtifacts"),
4888
+ answer: v.optional(v.string()),
4889
+ userId: v.string()
4890
+ },
4891
+ returns: permissiveReturn,
4892
+ handler: async (ctx, args) => {
4893
+ const node = await ctx.db.get(args.questionId);
4894
+ if (!node || node.nodeType !== "question") {
4895
+ throw new Error("Question not found");
4896
+ }
4897
+ const artifact = await resolveGraphPrimitivesAppResolvers().getFinalArtifact(ctx, args.artifactId);
4898
+ if (!artifact) {
4899
+ throw new Error("Artifact not found");
4900
+ }
4901
+ const scopeId = resolveQuestionScopeId({
4902
+ projectId: node.projectId,
4903
+ topicId: normalizeQuestionTopicId(node.topicId)
4904
+ });
4905
+ if (scopeId) {
4906
+ const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
4907
+ if (!hasAccess) {
4908
+ throw new Error("Access denied");
4909
+ }
4910
+ }
4911
+ const now = Date.now();
4912
+ const meta = node.metadata || {};
4913
+ await ctx.db.patch(args.questionId, {
4914
+ updatedAt: now,
4915
+ metadata: {
4916
+ ...meta,
4917
+ questionStatus: "answered",
4918
+ answeringArtifactId: args.artifactId,
4919
+ answer: args.answer,
4920
+ answeredAt: now,
4921
+ answeredBy: args.userId
4922
+ }
4923
+ });
4924
+ await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
4925
+ nodeId: args.questionId,
4926
+ operation: "upsert"
4927
+ });
4928
+ await markProjectGraphDirty(ctx, node.projectId, node.topicId);
4929
+ }
4930
+ });
4931
+ var getQuestionClusterPositions = query({
4932
+ args: {
4933
+ ...optionalScopeArgs,
4934
+ userId: v.string()
4935
+ },
4936
+ returns: permissiveReturn,
4937
+ handler: async (ctx, args) => {
4938
+ let scope = null;
4939
+ try {
4940
+ scope = await resolveTopicProjectScope(ctx, {
4941
+ projectId: args.projectId,
4942
+ topicId: args.topicId
4943
+ });
4944
+ } catch {
4945
+ return {
4946
+ positions: {},
4947
+ counts: {
4948
+ cluster: 0,
4949
+ sprintLinked: 0,
4950
+ baseline: 0,
4951
+ total: 0
4952
+ }
4953
+ };
4954
+ }
4955
+ const questionNodes = await ctx.db.query("epistemicNodes").withIndex(
4956
+ scope.topicId ? "by_topic_type" : "by_project_type",
4957
+ (q) => scope?.topicId ? q.eq("topicId", scope.topicId).eq("nodeType", "question") : q.eq("projectId", scope?.projectId).eq("nodeType", "question")
4958
+ ).collect();
4959
+ const activeQuestionNodes = questionNodes.filter(isActiveQuestionNode);
4960
+ const positions = {};
4961
+ let clusterCount = 0;
4962
+ let sprintLinkedCount = 0;
4963
+ let baselineCount = 0;
4964
+ for (const question of activeQuestionNodes) {
4965
+ const id = question._id.toString();
4966
+ const meta = question.metadata || {};
4967
+ const questionType2 = typeof question.questionType === "string" ? question.questionType : typeof meta.questionType === "string" ? meta.questionType : void 0;
4968
+ if (questionType2 === "belief_test" || meta.testType) {
4969
+ positions[id] = "cluster";
4970
+ clusterCount++;
4971
+ } else if (resolveLinkedWorktreeId(meta)) {
4972
+ positions[id] = "sprint_linked";
4973
+ sprintLinkedCount++;
4974
+ } else {
4975
+ positions[id] = "baseline";
4976
+ baselineCount++;
4977
+ }
4978
+ }
4979
+ return {
4980
+ positions,
4981
+ counts: {
4982
+ cluster: clusterCount,
4983
+ sprintLinked: sprintLinkedCount,
4984
+ baseline: baselineCount,
4985
+ total: activeQuestionNodes.length
4986
+ }
4987
+ };
4988
+ }
4989
+ });
4990
+
4991
+ export { addQuestion, advanceToConviction, backfillScoredQuestionEvidence, consolidate, create, createBatch, createEvidenceFromScoredQuestion, deleteQuestion, finalizeConviction, flattenQuestionNode, getByBeliefWithAccess, getByCategory, getById, getByPillar, getByProject, getByTopic, getForBelief, getForSprintCluster, getInConviction, getQuestionClusterPositions, internalCreate, internalGetByProject, internalGetByTopic, isActiveQuestionNode, linkToBelief, linkToInsight, list, markAnsweredWithArtifact, matchesRequestedQuestionStatus, resolveLinkedWorktreeId, unlinkInsight, updateConviction, updatePriority, updateQuestion, updateStatus };
4992
+ //# sourceMappingURL=epistemicQuestions.js.map
4993
+ //# sourceMappingURL=epistemicQuestions.js.map