@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,1628 @@
1
+ import { v } from 'convex/values';
2
+ import { componentsGeneric, queryGeneric, anyApi } from 'convex/server';
3
+
4
+ // src/beliefDecay.ts
5
+
6
+ // ../confidence/src/v1/operations/subjectiveLogic/index.ts
7
+ function project(o) {
8
+ return o.b + o.a * o.u;
9
+ }
10
+
11
+ // ../confidence/src/v1/operations/bridge/index.ts
12
+ var DEFAULT_NON_INFORMATIVE_WEIGHT = 2;
13
+ function clamp01(value) {
14
+ return Math.max(0, Math.min(1, value));
15
+ }
16
+ function clampNonNegative(value) {
17
+ return Number.isFinite(value) ? Math.max(0, value) : 0;
18
+ }
19
+ function normalizeNonInformativeWeight(weight) {
20
+ if (weight === void 0) {
21
+ return DEFAULT_NON_INFORMATIVE_WEIGHT;
22
+ }
23
+ return Number.isFinite(weight) ? Math.max(0, weight) : DEFAULT_NON_INFORMATIVE_WEIGHT;
24
+ }
25
+ function normalizeBaseRateVector(baseRate, size) {
26
+ if (size === 0) {
27
+ return [];
28
+ }
29
+ const fallback = Array.from({ length: size }, () => 1 / size);
30
+ if (!baseRate) {
31
+ return fallback;
32
+ }
33
+ if (baseRate.length !== size) {
34
+ throw new Error(
35
+ `Base-rate vector length ${baseRate.length} must match evidence vector length ${size}.`
36
+ );
37
+ }
38
+ const normalized = baseRate.map((value) => clampNonNegative(value));
39
+ const total = normalized.reduce((sum, value) => sum + value, 0);
40
+ if (total === 0) {
41
+ return fallback;
42
+ }
43
+ return normalized.map((value) => value / total);
44
+ }
45
+ function opinionFromDirichlet(alpha, nonInformativeWeight = DEFAULT_NON_INFORMATIVE_WEIGHT, baseRate) {
46
+ const evidence = alpha.map((value) => clampNonNegative(value));
47
+ const safeWeight = normalizeNonInformativeWeight(nonInformativeWeight);
48
+ const normalizedBaseRate = normalizeBaseRateVector(baseRate, evidence.length);
49
+ const totalEvidence = evidence.reduce((sum, value) => sum + value, 0);
50
+ const denominator = totalEvidence + safeWeight;
51
+ if (denominator === 0) {
52
+ return {
53
+ b: evidence.map(() => 0),
54
+ u: 1,
55
+ a: normalizedBaseRate
56
+ };
57
+ }
58
+ return {
59
+ b: evidence.map((value) => value / denominator),
60
+ u: safeWeight / denominator,
61
+ a: normalizedBaseRate
62
+ };
63
+ }
64
+ function opinionFromBeta(alpha, beta, nonInformativeWeight = DEFAULT_NON_INFORMATIVE_WEIGHT, baseRate = 0.5) {
65
+ const dirichlet = opinionFromDirichlet(
66
+ [alpha, beta],
67
+ nonInformativeWeight,
68
+ [clamp01(baseRate), 1 - clamp01(baseRate)]
69
+ );
70
+ return {
71
+ b: dirichlet.b[0] ?? 0,
72
+ d: dirichlet.b[1] ?? 0,
73
+ u: dirichlet.u,
74
+ a: dirichlet.a[0] ?? clamp01(baseRate)
75
+ };
76
+ }
77
+
78
+ // ../confidence/src/v1/operations/dynamics/revision.ts
79
+ function clamp012(value) {
80
+ return Math.max(0, Math.min(1, value));
81
+ }
82
+ function toEvidence(probability, weight) {
83
+ const safeProbability = clamp012(probability);
84
+ const safeWeight = Number.isFinite(weight) ? Math.max(0, weight) : 0;
85
+ return {
86
+ alpha: safeProbability * safeWeight,
87
+ beta: (1 - safeProbability) * safeWeight
88
+ };
89
+ }
90
+ function bayesianUpdate(priorConfidence, priorWeight, newAssessment, newWeight, options) {
91
+ return reviseConfidence({
92
+ priorConfidence,
93
+ priorWeight,
94
+ newAssessment,
95
+ newWeight,
96
+ baseRate: options?.baseRate,
97
+ nonInformativeWeight: options?.nonInformativeWeight
98
+ });
99
+ }
100
+ function reviseConfidence(args) {
101
+ return project(reviseConfidenceOpinion(args));
102
+ }
103
+ function reviseConfidenceOpinion(args) {
104
+ const priorEvidence = toEvidence(args.priorConfidence, args.priorWeight);
105
+ const newEvidence = toEvidence(args.newAssessment, args.newWeight ?? 1);
106
+ return opinionFromBeta(
107
+ priorEvidence.alpha + newEvidence.alpha,
108
+ priorEvidence.beta + newEvidence.beta,
109
+ args.nonInformativeWeight ?? DEFAULT_NON_INFORMATIVE_WEIGHT,
110
+ args.baseRate ?? 0.5
111
+ );
112
+ }
113
+
114
+ // ../confidence/src/v1/operations/dynamics/decay.ts
115
+ var DECAY_TIERS = {
116
+ FRESH: {
117
+ maxAgeDays: 30,
118
+ weight: 1,
119
+ label: "fresh",
120
+ action: "Full confidence \u2014 recently validated",
121
+ rescoreInDays: 30
122
+ },
123
+ AGING: {
124
+ maxAgeDays: 90,
125
+ weight: 0.8,
126
+ label: "aging",
127
+ action: "Evidence refresh recommended",
128
+ rescoreInDays: 14
129
+ },
130
+ STALE: {
131
+ maxAgeDays: 180,
132
+ weight: 0.5,
133
+ label: "stale",
134
+ action: "Evidence update required before trusting",
135
+ rescoreInDays: 7
136
+ },
137
+ EXPIRED: {
138
+ maxAgeDays: Number.POSITIVE_INFINITY,
139
+ weight: 0.2,
140
+ label: "expired",
141
+ action: "Full re-evaluation needed",
142
+ rescoreInDays: 0
143
+ }
144
+ };
145
+ var DEADLINE_URGENCY = {
146
+ DISTANT: {
147
+ minDaysToDeadline: 365,
148
+ urgencyMultiplier: 1,
149
+ label: "distant",
150
+ rescoreIntervalDays: 90,
151
+ action: "Quarterly confidence check"
152
+ },
153
+ APPROACHING: {
154
+ minDaysToDeadline: 180,
155
+ urgencyMultiplier: 0.9,
156
+ label: "approaching",
157
+ rescoreIntervalDays: 30,
158
+ action: "Monthly confidence check \u2014 conditions may be shifting"
159
+ },
160
+ NEAR: {
161
+ minDaysToDeadline: 90,
162
+ urgencyMultiplier: 0.75,
163
+ label: "near",
164
+ rescoreIntervalDays: 14,
165
+ action: "Biweekly rescore \u2014 deadline within 3 months"
166
+ },
167
+ IMMINENT: {
168
+ minDaysToDeadline: 30,
169
+ urgencyMultiplier: 0.6,
170
+ label: "imminent",
171
+ rescoreIntervalDays: 7,
172
+ action: "Weekly rescore \u2014 deadline within 1 month"
173
+ },
174
+ CRITICAL: {
175
+ minDaysToDeadline: 7,
176
+ urgencyMultiplier: 0.4,
177
+ label: "critical",
178
+ rescoreIntervalDays: 1,
179
+ action: "Daily rescore \u2014 deadline THIS WEEK"
180
+ },
181
+ OVERDUE: {
182
+ minDaysToDeadline: Number.NEGATIVE_INFINITY,
183
+ urgencyMultiplier: 0.2,
184
+ label: "overdue",
185
+ rescoreIntervalDays: 0,
186
+ action: "OVERDUE \u2014 must validate or archive immediately"
187
+ }
188
+ };
189
+ function normalizeBeliefConfidence(confidence) {
190
+ if (typeof confidence !== "number" || !Number.isFinite(confidence)) {
191
+ return null;
192
+ }
193
+ if (confidence >= 0 && confidence <= 1) {
194
+ return confidence;
195
+ }
196
+ if (confidence > 1 && confidence <= 100) {
197
+ return confidence / 100;
198
+ }
199
+ return null;
200
+ }
201
+ function hasResolvedPredictionOutcome(predictionMeta) {
202
+ if (!predictionMeta || typeof predictionMeta !== "object") {
203
+ return false;
204
+ }
205
+ const outcome = predictionMeta.outcome;
206
+ return outcome === "confirmed" || outcome === "disconfirmed" || outcome === "partial" || outcome === "expired";
207
+ }
208
+ function resolveLifecycleBucket(args) {
209
+ if (normalizeBeliefConfidence(args.confidence) === 0 || normalizeBeliefConfidence(args.confidence) === 1 || hasResolvedPredictionOutcome(args.predictionMeta)) {
210
+ return "fact";
211
+ }
212
+ if (args.beliefStatus === "assumption" || args.beliefStatus === "hypothesis" || args.beliefStatus === "belief" || args.beliefStatus === "fact") {
213
+ if (normalizeBeliefConfidence(args.confidence) !== null && (args.beliefStatus === "assumption" || args.beliefStatus === "hypothesis")) {
214
+ return "belief";
215
+ }
216
+ return args.beliefStatus;
217
+ }
218
+ return "assumption";
219
+ }
220
+ function lifecycleMultiplier(status) {
221
+ if (status === "assumption") {
222
+ return 0.65;
223
+ }
224
+ if (status === "hypothesis") {
225
+ return 0.8;
226
+ }
227
+ return 1;
228
+ }
229
+ function computeBaseDecay(lastScoredAt) {
230
+ const ageDays = (Date.now() - lastScoredAt) / (1e3 * 60 * 60 * 24);
231
+ let tier = DECAY_TIERS.EXPIRED;
232
+ if (ageDays <= DECAY_TIERS.FRESH.maxAgeDays) {
233
+ tier = DECAY_TIERS.FRESH;
234
+ } else if (ageDays <= DECAY_TIERS.AGING.maxAgeDays) {
235
+ tier = DECAY_TIERS.AGING;
236
+ } else if (ageDays <= DECAY_TIERS.STALE.maxAgeDays) {
237
+ tier = DECAY_TIERS.STALE;
238
+ }
239
+ return { ageDays, tier, weight: tier.weight };
240
+ }
241
+ function computeDeadlineUrgency(expectedBy) {
242
+ if (!expectedBy) {
243
+ return null;
244
+ }
245
+ const daysToDeadline = (expectedBy - Date.now()) / (1e3 * 60 * 60 * 24);
246
+ let urgencyTier = DEADLINE_URGENCY.DISTANT;
247
+ if (daysToDeadline < 0) {
248
+ urgencyTier = DEADLINE_URGENCY.OVERDUE;
249
+ } else if (daysToDeadline <= 7) {
250
+ urgencyTier = DEADLINE_URGENCY.CRITICAL;
251
+ } else if (daysToDeadline <= 30) {
252
+ urgencyTier = DEADLINE_URGENCY.IMMINENT;
253
+ } else if (daysToDeadline <= 90) {
254
+ urgencyTier = DEADLINE_URGENCY.NEAR;
255
+ } else if (daysToDeadline <= 180) {
256
+ urgencyTier = DEADLINE_URGENCY.APPROACHING;
257
+ }
258
+ return {
259
+ daysToDeadline: Math.round(daysToDeadline),
260
+ urgencyTier,
261
+ urgencyMultiplier: urgencyTier.urgencyMultiplier,
262
+ isOverdue: daysToDeadline < 0
263
+ };
264
+ }
265
+ function computeEffectiveDecay(lastScoredAt, expectedBy, beliefStatus) {
266
+ const base = computeBaseDecay(lastScoredAt);
267
+ const urgency = computeDeadlineUrgency(expectedBy);
268
+ const effectiveWeight = (urgency ? base.weight * urgency.urgencyMultiplier : base.weight) * lifecycleMultiplier(beliefStatus);
269
+ const urgencyRescoreDays = urgency?.urgencyTier.rescoreIntervalDays ?? Number.POSITIVE_INFINITY;
270
+ const rescoreInDays = Math.min(base.tier.rescoreInDays, urgencyRescoreDays);
271
+ const rescoreByDate = lastScoredAt + rescoreInDays * 24 * 60 * 60 * 1e3;
272
+ let action = base.tier.action;
273
+ if (urgency && urgency.urgencyTier.label !== "distant") {
274
+ action = `${urgency.urgencyTier.action}. ${base.tier.action}`;
275
+ }
276
+ return {
277
+ ageDays: base.ageDays,
278
+ baseTier: base.tier,
279
+ baseWeight: base.weight,
280
+ urgency,
281
+ effectiveWeight: Math.max(0, Math.min(1, effectiveWeight)),
282
+ rescoreByDate,
283
+ rescoreInDays,
284
+ action
285
+ };
286
+ }
287
+ function getRescoringSchedule(opts) {
288
+ const lifecycleStatus = resolveLifecycleBucket({
289
+ beliefStatus: opts.beliefStatus,
290
+ confidence: opts.confidence,
291
+ predictionMeta: opts.predictionMeta
292
+ });
293
+ const decayState = computeEffectiveDecay(
294
+ opts.lastScoredAt,
295
+ opts.expectedBy,
296
+ lifecycleStatus
297
+ );
298
+ const daysUntilRescore = (decayState.rescoreByDate - Date.now()) / (1e3 * 60 * 60 * 24);
299
+ const isOverdue = daysUntilRescore < 0;
300
+ let priority;
301
+ let reason;
302
+ if (isOverdue && lifecycleStatus === "assumption") {
303
+ priority = "critical";
304
+ reason = `Untested assumption is stale (${Math.round(decayState.ageDays)}d) \u2014 validate or supersede`;
305
+ } else if (isOverdue && lifecycleStatus === "hypothesis" || lifecycleStatus === "hypothesis" && daysUntilRescore < 7) {
306
+ priority = "high";
307
+ reason = `Hypothesis aging without validation (${Math.round(decayState.ageDays)}d) \u2014 run/finish sprint testing`;
308
+ } else if (decayState.urgency?.isOverdue) {
309
+ priority = "critical";
310
+ reason = `Prediction deadline passed ${Math.abs(decayState.urgency.daysToDeadline)}d ago \u2014 validate or archive`;
311
+ } else if (isOverdue && decayState.baseTier.label === "expired") {
312
+ priority = "critical";
313
+ reason = `Not scored in ${Math.round(decayState.ageDays)}d \u2014 confidence severely degraded`;
314
+ } else if (isOverdue && decayState.baseTier.label === "stale") {
315
+ priority = "high";
316
+ reason = `Stale (${Math.round(decayState.ageDays)}d since scoring) \u2014 evidence update required`;
317
+ } else if (decayState.urgency && decayState.urgency.urgencyTier.label === "critical") {
318
+ priority = "critical";
319
+ reason = `Deadline in ${decayState.urgency.daysToDeadline}d \u2014 needs immediate rescoring`;
320
+ } else if (decayState.urgency && decayState.urgency.urgencyTier.label === "imminent") {
321
+ priority = "high";
322
+ reason = `Deadline in ${decayState.urgency.daysToDeadline}d \u2014 weekly rescoring required`;
323
+ } else if (isOverdue) {
324
+ priority = "high";
325
+ reason = `Rescore overdue by ${Math.abs(Math.round(daysUntilRescore))}d`;
326
+ } else if (opts.temporalNature === "forecast" && daysUntilRescore < 7) {
327
+ priority = "medium";
328
+ reason = `Forecast belief \u2014 next rescore due in ${Math.round(daysUntilRescore)}d`;
329
+ } else if (daysUntilRescore < 14) {
330
+ priority = "medium";
331
+ reason = `Rescore due in ${Math.round(daysUntilRescore)}d`;
332
+ } else {
333
+ priority = "low";
334
+ reason = `On schedule \u2014 next rescore in ${Math.round(daysUntilRescore)}d`;
335
+ }
336
+ if ((opts.confidence ?? 0) >= 0.8 && decayState.baseTier.label !== "fresh" && priority === "medium") {
337
+ priority = "high";
338
+ reason = `High-confidence belief (${((opts.confidence ?? 0) * 100).toFixed(0)}%) aging without rescore \u2014 ${reason}`;
339
+ }
340
+ return {
341
+ nextRescoreBy: decayState.rescoreByDate,
342
+ daysUntilRescore: Math.round(daysUntilRescore),
343
+ isOverdue,
344
+ priority,
345
+ reason,
346
+ decay: decayState
347
+ };
348
+ }
349
+ var api = anyApi;
350
+ componentsGeneric();
351
+
352
+ // ../access-control/src/topicProjectOverlay.ts
353
+ var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
354
+ function readNonEmptyString(value) {
355
+ if (typeof value !== "string") {
356
+ return;
357
+ }
358
+ const normalized = value.trim();
359
+ return normalized.length > 0 ? normalized : void 0;
360
+ }
361
+ function readStringArray(value) {
362
+ if (!Array.isArray(value)) {
363
+ return [];
364
+ }
365
+ return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
366
+ }
367
+ function readMetadata(topic) {
368
+ return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
369
+ }
370
+ function readLegacyProjectId(value) {
371
+ if (!value) {
372
+ return;
373
+ }
374
+ return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
375
+ }
376
+ function coerceVisibility(value) {
377
+ return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
378
+ }
379
+ function coerceStatus(value) {
380
+ return value === "active" || value === "archived" || value === "watching" ? value : void 0;
381
+ }
382
+ function mapProjectType(topic, metadata) {
383
+ const explicit = readNonEmptyString(metadata.projectType);
384
+ if (explicit) {
385
+ return explicit;
386
+ }
387
+ if (topic.type === "theme") {
388
+ return "thematic";
389
+ }
390
+ return readNonEmptyString(topic.type) || "general";
391
+ }
392
+ function isProjectLikeTopic(topic) {
393
+ const metadata = readMetadata(topic);
394
+ return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
395
+ }
396
+ async function resolveTopicDoc(ctx, scopeId) {
397
+ if (ctx?.db && typeof ctx.db.get === "function") {
398
+ try {
399
+ const directTopic = await ctx.db.get(scopeId);
400
+ if (directTopic) {
401
+ return directTopic;
402
+ }
403
+ } catch {
404
+ }
405
+ }
406
+ if (typeof ctx.runQuery !== "function") {
407
+ return null;
408
+ }
409
+ try {
410
+ const topic = await ctx.runQuery(api.topics.get, {
411
+ id: String(scopeId)
412
+ });
413
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
414
+ return topic;
415
+ }
416
+ } catch {
417
+ }
418
+ try {
419
+ const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
420
+ projectId: String(scopeId)
421
+ });
422
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
423
+ return topic;
424
+ }
425
+ } catch {
426
+ }
427
+ return null;
428
+ }
429
+ function materializeTopicProjectOverlay(topic, idMode = "legacy") {
430
+ const metadata = readMetadata(topic);
431
+ const topicId = String(topic._id);
432
+ const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
433
+ const storageProjectId = legacyProjectId || topicId;
434
+ const outwardId = idMode === "topic" ? topicId : storageProjectId;
435
+ const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
436
+ const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
437
+ const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
438
+ const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
439
+ return {
440
+ ...metadata,
441
+ _id: outwardId,
442
+ projectId: outwardId,
443
+ topicId,
444
+ storageProjectId,
445
+ legacyProjectId,
446
+ name: readNonEmptyString(topic.name) || "Untitled Theme",
447
+ type: mapProjectType(topic, metadata),
448
+ description: readNonEmptyString(topic.description),
449
+ ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
450
+ sharedWith: readStringArray(metadata.sharedWith),
451
+ visibility,
452
+ tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
453
+ workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
454
+ status,
455
+ tags: readStringArray(metadata.tags),
456
+ chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
457
+ artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
458
+ lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
459
+ _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
460
+ createdAt,
461
+ updatedAt
462
+ };
463
+ }
464
+ async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
465
+ const topic = await resolveTopicDoc(ctx, scopeId);
466
+ if (!topic) {
467
+ return null;
468
+ }
469
+ if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
470
+ return null;
471
+ }
472
+ return materializeTopicProjectOverlay(topic, options.idMode);
473
+ }
474
+ async function listTopicProjectOverlays(ctx, options = {}) {
475
+ let allTopics = [];
476
+ if (ctx?.db?.query && typeof ctx.db.query === "function") {
477
+ try {
478
+ allTopics = await ctx.db.query("topics").collect();
479
+ } catch {
480
+ allTopics = [];
481
+ }
482
+ }
483
+ if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
484
+ allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
485
+ }
486
+ return allTopics.filter(
487
+ (topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
488
+ ).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
489
+ }
490
+
491
+ // ../access-control/src/projectGrantsBridge.ts
492
+ var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
493
+ function normalizeString(value) {
494
+ if (typeof value !== "string") {
495
+ return;
496
+ }
497
+ const trimmed = value.trim();
498
+ return trimmed.length > 0 ? trimmed : void 0;
499
+ }
500
+ async function resolveGrantScopeIds(ctx, args) {
501
+ const topicId = normalizeString(args.topicId);
502
+ const projectId = normalizeString(args.projectId);
503
+ for (const scopeId of [topicId, projectId]) {
504
+ if (!scopeId) {
505
+ continue;
506
+ }
507
+ try {
508
+ const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
509
+ idMode: "legacy",
510
+ projectLikeOnly: false
511
+ });
512
+ if (overlay) {
513
+ return {
514
+ topicId: normalizeString(overlay.topicId) ?? topicId,
515
+ projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
516
+ };
517
+ }
518
+ } catch {
519
+ }
520
+ }
521
+ return { topicId, projectId };
522
+ }
523
+ async function normalizeProjectGrantRow(ctx, row) {
524
+ const scope = await resolveGrantScopeIds(ctx, {
525
+ topicId: row.topicId,
526
+ projectId: row.projectId
527
+ });
528
+ return {
529
+ ...row,
530
+ ...scope.topicId ? { topicId: scope.topicId } : {},
531
+ ...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
532
+ };
533
+ }
534
+ async function normalizeProjectGrantRows(ctx, rows) {
535
+ return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
536
+ }
537
+ async function listProjectGrantsByPrincipal(ctx, principalId) {
538
+ const rows = await Promise.all(
539
+ PROJECT_GRANT_STATUSES.map(
540
+ (status) => ctx.db.query("projectGrants").withIndex(
541
+ "by_principal_status",
542
+ (q) => q.eq("principalId", principalId).eq("status", status)
543
+ ).collect()
544
+ )
545
+ );
546
+ return await normalizeProjectGrantRows(ctx, rows.flat());
547
+ }
548
+ async function listProjectGrantsByGroup(ctx, groupId) {
549
+ const rows = await Promise.all(
550
+ PROJECT_GRANT_STATUSES.map(
551
+ (status) => ctx.db.query("projectGrants").withIndex(
552
+ "by_group_status",
553
+ (q) => q.eq("groupId", groupId).eq("status", status)
554
+ ).collect()
555
+ )
556
+ );
557
+ return await normalizeProjectGrantRows(ctx, rows.flat());
558
+ }
559
+ function buildScopeMatchers(inputScopeId, resolved) {
560
+ return new Set(
561
+ [inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
562
+ );
563
+ }
564
+ function matchesResolvedScope(row, scopeIds) {
565
+ const rowTopicId = normalizeString(row.topicId);
566
+ const rowProjectId = normalizeString(row.projectId);
567
+ return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
568
+ }
569
+ async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
570
+ const resolved = await resolveGrantScopeIds(ctx, { topicId });
571
+ const scopeIds = buildScopeMatchers(topicId, resolved);
572
+ const rows = await listProjectGrantsByPrincipal(ctx, principalId);
573
+ return rows.filter((row) => matchesResolvedScope(row, scopeIds));
574
+ }
575
+ async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
576
+ const resolved = await resolveGrantScopeIds(ctx, { topicId });
577
+ const scopeIds = buildScopeMatchers(topicId, resolved);
578
+ const rows = await listProjectGrantsByGroup(ctx, groupId);
579
+ return rows.filter((row) => matchesResolvedScope(row, scopeIds));
580
+ }
581
+ async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
582
+ const rows = await listProjectGrantsByPrincipal(ctx, principalId);
583
+ return rows.filter((row) => row.status === status);
584
+ }
585
+ async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
586
+ const rows = await listProjectGrantsByGroup(ctx, groupId);
587
+ return rows.filter((row) => row.status === status);
588
+ }
589
+ async function bridgeInsertProjectGrant(ctx, value) {
590
+ const resolved = await resolveGrantScopeIds(ctx, value);
591
+ return await ctx.db.insert("projectGrants", {
592
+ ...value,
593
+ ...resolved.topicId ? { topicId: resolved.topicId } : {},
594
+ ...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
595
+ });
596
+ }
597
+
598
+ // ../access-control/src/resolvers.ts
599
+ async function findUserByClerkId(ctx, clerkId) {
600
+ const normalizedClerkId = clerkId.trim();
601
+ if (!normalizedClerkId) {
602
+ return null;
603
+ }
604
+ if (typeof ctx.runQuery === "function") {
605
+ try {
606
+ const bridgedUser = await ctx.runQuery(api.users.getUserByClerkId, {
607
+ clerkId: normalizedClerkId
608
+ });
609
+ if (bridgedUser) {
610
+ return bridgedUser;
611
+ }
612
+ } catch {
613
+ }
614
+ }
615
+ try {
616
+ const users = await ctx.db.query("users").collect();
617
+ return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
618
+ } catch {
619
+ return null;
620
+ }
621
+ }
622
+ async function findUserByPrincipalId(ctx, principalId) {
623
+ const normalizedPrincipalId = principalId.trim();
624
+ if (!normalizedPrincipalId) {
625
+ return null;
626
+ }
627
+ try {
628
+ const users = await ctx.db.query("users").collect();
629
+ return users.find(
630
+ (user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
631
+ ) ?? null;
632
+ } catch {
633
+ return null;
634
+ }
635
+ }
636
+ async function findAgentByPrincipalId(ctx, principalId) {
637
+ const normalizedPrincipalId = principalId.trim();
638
+ if (!normalizedPrincipalId) {
639
+ return null;
640
+ }
641
+ if (typeof ctx.runQuery === "function") {
642
+ try {
643
+ const bridgedAgent = await ctx.runQuery(
644
+ api.agents.getAgentByPrincipalId,
645
+ {
646
+ principalId: normalizedPrincipalId
647
+ }
648
+ );
649
+ if (bridgedAgent) {
650
+ return bridgedAgent;
651
+ }
652
+ } catch {
653
+ }
654
+ }
655
+ try {
656
+ const agents = await ctx.db.query("agents").collect();
657
+ return agents.find(
658
+ (agent) => String(agent.principalId ?? "") === normalizedPrincipalId
659
+ ) ?? null;
660
+ } catch {
661
+ return null;
662
+ }
663
+ }
664
+ function defaultResolvers() {
665
+ return {
666
+ async getProject(ctx, topicId) {
667
+ return await resolveTopicProjectOverlay(ctx, topicId, {
668
+ idMode: "legacy",
669
+ projectLikeOnly: false
670
+ });
671
+ },
672
+ async listTopics(ctx) {
673
+ return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
674
+ },
675
+ async listTopicsByOwner(ctx, ownerId) {
676
+ const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
677
+ return topics.filter((topic) => topic.ownerId === ownerId);
678
+ },
679
+ async listTopicsByVisibility(ctx, visibility) {
680
+ const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
681
+ return topics.filter((topic) => topic.visibility === visibility);
682
+ },
683
+ async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
684
+ return await bridgeListProjectGrantsByTopicAndPrincipal(
685
+ ctx,
686
+ topicId,
687
+ principalId
688
+ );
689
+ },
690
+ async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
691
+ return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
692
+ },
693
+ async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
694
+ return await bridgeListProjectGrantsByPrincipalStatus(
695
+ ctx,
696
+ principalId,
697
+ status
698
+ );
699
+ },
700
+ async listProjectGrantsByGroupStatus(ctx, groupId, status) {
701
+ return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
702
+ },
703
+ async insertProjectGrant(ctx, value) {
704
+ return await bridgeInsertProjectGrant(ctx, value);
705
+ },
706
+ async getAgentByPrincipalId(ctx, principalId) {
707
+ return await findAgentByPrincipalId(ctx, principalId);
708
+ },
709
+ async getUserByClerkId(ctx, clerkId) {
710
+ return await findUserByClerkId(ctx, clerkId);
711
+ },
712
+ async getUserByPrincipalId(ctx, principalId) {
713
+ return await findUserByPrincipalId(ctx, principalId);
714
+ }
715
+ };
716
+ }
717
+ var resolverOverrides = {};
718
+ function resolveAccessControlAppResolvers(_ctx) {
719
+ return {
720
+ ...defaultResolvers(),
721
+ ...resolverOverrides
722
+ };
723
+ }
724
+
725
+ // ../access-control/src/principalContext.ts
726
+ function requireCanonicalResolvedUser(user, clerkId) {
727
+ const resolved = user;
728
+ if (!resolved) {
729
+ throw new Error(
730
+ `[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
731
+ );
732
+ }
733
+ const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
734
+ if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
735
+ throw new Error(
736
+ `[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
737
+ );
738
+ }
739
+ if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
740
+ throw new Error(
741
+ `[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
742
+ );
743
+ }
744
+ if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
745
+ throw new Error(
746
+ `[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
747
+ );
748
+ }
749
+ if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
750
+ throw new Error(
751
+ `[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
752
+ );
753
+ }
754
+ return {
755
+ mcRole,
756
+ defaultTenantId: defaultTenantId.trim(),
757
+ defaultWorkspaceId: defaultWorkspaceId.trim(),
758
+ defaultPrincipalId: defaultPrincipalId.trim()
759
+ };
760
+ }
761
+ function isPrincipalIdInput(value) {
762
+ return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
763
+ }
764
+ async function resolveCanonicalUserRecord(ctx, actorId) {
765
+ const normalizedActorId = actorId.trim();
766
+ const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
767
+ const resolvers = resolveAccessControlAppResolvers();
768
+ const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
769
+ if (resolvedByClerkId) {
770
+ return {
771
+ resolvedUser: resolvedByClerkId,
772
+ clerkId,
773
+ contextClerkId: clerkId
774
+ };
775
+ }
776
+ const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
777
+ ctx,
778
+ normalizedActorId
779
+ );
780
+ return {
781
+ resolvedUser: resolvedByPrincipalId ?? null,
782
+ clerkId,
783
+ contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
784
+ };
785
+ }
786
+ function uniqRoles(roles) {
787
+ const roleSet = /* @__PURE__ */ new Set();
788
+ for (const role of roles) {
789
+ if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
790
+ roleSet.add(role);
791
+ }
792
+ }
793
+ return [...roleSet];
794
+ }
795
+ function normalizeGroupIds(value) {
796
+ if (!Array.isArray(value)) {
797
+ return [];
798
+ }
799
+ return [...new Set(
800
+ value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
801
+ )];
802
+ }
803
+ function requireServiceAgentUser(user, actorId) {
804
+ const canonicalUser = requireCanonicalResolvedUser(user, actorId);
805
+ if (canonicalUser.mcRole !== "service_agent") {
806
+ throw new Error(
807
+ `[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
808
+ );
809
+ }
810
+ return canonicalUser;
811
+ }
812
+ function requireCanonicalResolvedAgent(agent, actorId) {
813
+ const resolved = agent;
814
+ if (!resolved) {
815
+ throw new Error(
816
+ `[AccessControl] Agent "${actorId}" not found in agents or users table.`
817
+ );
818
+ }
819
+ if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
820
+ throw new Error(
821
+ `[AccessControl] Canonical agent principalId required for ${actorId}.`
822
+ );
823
+ }
824
+ if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
825
+ throw new Error(
826
+ `[AccessControl] Canonical home tenant required for ${actorId}.`
827
+ );
828
+ }
829
+ if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
830
+ throw new Error(
831
+ `[AccessControl] Canonical home workspace required for ${actorId}.`
832
+ );
833
+ }
834
+ return {
835
+ principalId: resolved.principalId.trim(),
836
+ tenantId: resolved.tenantId.trim(),
837
+ workspaceId: resolved.workspaceId.trim(),
838
+ roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
839
+ groupIds: normalizeGroupIds(resolved.groupIds)
840
+ };
841
+ }
842
+ async function resolvePrincipalContext(ctx, actorId) {
843
+ if (actorId.startsWith("agent:")) {
844
+ const resolvers = resolveAccessControlAppResolvers();
845
+ const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
846
+ if (resolvedAgent) {
847
+ const agent = requireCanonicalResolvedAgent(
848
+ resolvedAgent,
849
+ actorId
850
+ );
851
+ return {
852
+ principalId: agent.principalId,
853
+ principalType: "service",
854
+ clerkId: actorId,
855
+ tenantId: agent.tenantId,
856
+ workspaceId: agent.workspaceId,
857
+ roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
858
+ groupIds: agent.groupIds,
859
+ isPlatformAdmin: false,
860
+ isTenantAdmin: false,
861
+ isWorkspaceAdmin: false,
862
+ isSystemFallback: false
863
+ };
864
+ }
865
+ const resolvedUser2 = await resolvers.getUserByClerkId(
866
+ ctx,
867
+ actorId
868
+ );
869
+ if (!resolvedUser2) {
870
+ throw new Error(
871
+ `[AccessControl] Agent "${actorId}" not found in agents or users table.`
872
+ );
873
+ }
874
+ const user2 = requireServiceAgentUser(
875
+ resolvedUser2,
876
+ actorId
877
+ );
878
+ console.warn(
879
+ `[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
880
+ );
881
+ return {
882
+ principalId: user2.defaultPrincipalId,
883
+ principalType: "service",
884
+ clerkId: actorId,
885
+ tenantId: user2.defaultTenantId,
886
+ workspaceId: user2.defaultWorkspaceId,
887
+ roles: ["service_agent"],
888
+ groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
889
+ isPlatformAdmin: false,
890
+ isTenantAdmin: false,
891
+ isWorkspaceAdmin: false,
892
+ isSystemFallback: false
893
+ };
894
+ }
895
+ const {
896
+ resolvedUser,
897
+ contextClerkId
898
+ } = await resolveCanonicalUserRecord(ctx, actorId);
899
+ const user = requireCanonicalResolvedUser(
900
+ resolvedUser,
901
+ contextClerkId
902
+ );
903
+ if (!user.defaultPrincipalId) {
904
+ throw new Error(
905
+ `[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
906
+ );
907
+ }
908
+ if (user.mcRole === "service_agent") {
909
+ return {
910
+ principalId: user.defaultPrincipalId,
911
+ principalType: "service",
912
+ clerkId: contextClerkId,
913
+ tenantId: user.defaultTenantId,
914
+ workspaceId: user.defaultWorkspaceId,
915
+ roles: ["service_agent"],
916
+ groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
917
+ isPlatformAdmin: false,
918
+ isTenantAdmin: false,
919
+ isWorkspaceAdmin: false,
920
+ isSystemFallback: false
921
+ };
922
+ }
923
+ const principalId = user.defaultPrincipalId;
924
+ const effectiveRole = user.mcRole;
925
+ const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
926
+ const tenantId = user.defaultTenantId;
927
+ const workspaceId = user.defaultWorkspaceId;
928
+ const isPlatformAdmin = effectiveRole === "platform_admin";
929
+ return {
930
+ principalId,
931
+ principalType: "user",
932
+ clerkId: contextClerkId,
933
+ tenantId,
934
+ workspaceId,
935
+ roles: uniqRoles(roles),
936
+ groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
937
+ isPlatformAdmin,
938
+ isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
939
+ isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
940
+ isSystemFallback: false
941
+ };
942
+ }
943
+
944
+ // ../access-control/src/access.ts
945
+ function isTopicInPrincipalTenant(topic, principalTenantId) {
946
+ if (!topic.tenantId) {
947
+ return false;
948
+ }
949
+ if (!principalTenantId) {
950
+ return false;
951
+ }
952
+ return String(topic.tenantId) === String(principalTenantId);
953
+ }
954
+ function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
955
+ if (!topic.workspaceId) {
956
+ return false;
957
+ }
958
+ if (!principalWorkspaceId) {
959
+ return false;
960
+ }
961
+ return String(topic.workspaceId) === String(principalWorkspaceId);
962
+ }
963
+ function isLegacyUnscopedTopic(topic) {
964
+ return !topic.tenantId || !topic.workspaceId;
965
+ }
966
+ function isGrantScopeAlignedToTopic(topic, grant) {
967
+ if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
968
+ return false;
969
+ }
970
+ if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
971
+ return false;
972
+ }
973
+ return true;
974
+ }
975
+ function isGrantSourceAllowedForVisibility(visibility, source) {
976
+ if (source !== "external_share") {
977
+ return true;
978
+ }
979
+ return visibility === "external" || visibility === "public";
980
+ }
981
+ function isGrantActive(grant) {
982
+ if (grant.status !== "active") {
983
+ return false;
984
+ }
985
+ if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
986
+ return false;
987
+ }
988
+ return true;
989
+ }
990
+ function isExternalPrincipal(_ctx, _args) {
991
+ return false;
992
+ }
993
+ async function getAccessibleTopicIds(ctx, userId) {
994
+ const principalContext = await resolvePrincipalContext(ctx, userId);
995
+ if (principalContext.isPlatformAdmin) {
996
+ const allTopics2 = await resolveAccessControlAppResolvers().listTopics(ctx);
997
+ return new Set(allTopics2.map((topic) => topic._id));
998
+ }
999
+ const topicIds = /* @__PURE__ */ new Set();
1000
+ const ownedTopics = await resolveAccessControlAppResolvers().listTopicsByOwner(ctx, userId);
1001
+ for (const topic of ownedTopics) {
1002
+ topicIds.add(topic._id);
1003
+ }
1004
+ const publicTopics = await resolveAccessControlAppResolvers().listTopicsByVisibility(ctx, "public");
1005
+ for (const topic of publicTopics) {
1006
+ topicIds.add(topic._id);
1007
+ }
1008
+ const principalIsExternal = await isExternalPrincipal(ctx, {
1009
+ groupIds: principalContext.groupIds,
1010
+ topicTenantId: principalContext.tenantId ?? void 0,
1011
+ topicWorkspaceId: principalContext.workspaceId ?? void 0
1012
+ });
1013
+ if (!principalIsExternal) {
1014
+ const firmTopics = await resolveAccessControlAppResolvers().listTopicsByVisibility(ctx, "firm");
1015
+ for (const topic of firmTopics) {
1016
+ if (isTopicInPrincipalTenant(topic, principalContext.tenantId) && isTopicInPrincipalWorkspace(topic, principalContext.workspaceId)) {
1017
+ topicIds.add(topic._id);
1018
+ }
1019
+ }
1020
+ }
1021
+ const directGrants = await resolveAccessControlAppResolvers().listProjectGrantsByPrincipalStatus(
1022
+ ctx,
1023
+ principalContext.principalId,
1024
+ "active"
1025
+ );
1026
+ for (const grant of directGrants) {
1027
+ if (!isGrantActive(grant)) {
1028
+ continue;
1029
+ }
1030
+ const topic = await resolveAccessControlAppResolvers().getProject(
1031
+ ctx,
1032
+ grant.projectId
1033
+ );
1034
+ if (!topic) {
1035
+ continue;
1036
+ }
1037
+ if (!isLegacyUnscopedTopic(topic)) {
1038
+ if (!isTopicInPrincipalTenant(topic, principalContext.tenantId)) {
1039
+ continue;
1040
+ }
1041
+ if (!isTopicInPrincipalWorkspace(topic, principalContext.workspaceId)) {
1042
+ continue;
1043
+ }
1044
+ }
1045
+ if (!isGrantScopeAlignedToTopic(topic, grant)) {
1046
+ continue;
1047
+ }
1048
+ if (!isGrantSourceAllowedForVisibility(topic.visibility, grant.source)) {
1049
+ continue;
1050
+ }
1051
+ if (principalIsExternal && topic.visibility !== "public" && grant.source !== "external_share") {
1052
+ continue;
1053
+ }
1054
+ topicIds.add(grant.projectId);
1055
+ }
1056
+ const allTopics = await resolveAccessControlAppResolvers().listTopics(ctx);
1057
+ for (const topic of allTopics) {
1058
+ if ((topic.sharedWith ?? []).includes(userId) && (isLegacyUnscopedTopic(topic) || isTopicInPrincipalTenant(topic, principalContext.tenantId) && isTopicInPrincipalWorkspace(topic, principalContext.workspaceId))) {
1059
+ topicIds.add(topic._id);
1060
+ }
1061
+ }
1062
+ return topicIds;
1063
+ }
1064
+ var getAccessibleProjectIds = getAccessibleTopicIds;
1065
+ var permissiveReturn = v.optional(v.any());
1066
+ var looseJsonObject = v.record(v.string(), v.any());
1067
+ var looseJsonArray = v.array(v.any());
1068
+ v.union(
1069
+ v.string(),
1070
+ v.number(),
1071
+ v.boolean(),
1072
+ v.null(),
1073
+ looseJsonObject,
1074
+ looseJsonArray
1075
+ );
1076
+ var api2 = anyApi;
1077
+ componentsGeneric();
1078
+ var query = queryGeneric;
1079
+
1080
+ // src/topicProjectOverlay.ts
1081
+ var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
1082
+ function readNonEmptyString2(value) {
1083
+ if (typeof value !== "string") {
1084
+ return;
1085
+ }
1086
+ const normalized = value.trim();
1087
+ return normalized.length > 0 ? normalized : void 0;
1088
+ }
1089
+ function readStringArray2(value) {
1090
+ if (!Array.isArray(value)) {
1091
+ return [];
1092
+ }
1093
+ return value.map((entry) => readNonEmptyString2(entry)).filter((entry) => Boolean(entry));
1094
+ }
1095
+ function readMetadata2(topic) {
1096
+ return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
1097
+ }
1098
+ function readLegacyProjectId2(value) {
1099
+ if (!value) {
1100
+ return;
1101
+ }
1102
+ return readNonEmptyString2(value[LEGACY_SCOPE_FIELD2]);
1103
+ }
1104
+ function coerceVisibility2(value) {
1105
+ return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
1106
+ }
1107
+ function coerceStatus2(value) {
1108
+ return value === "active" || value === "archived" || value === "watching" ? value : void 0;
1109
+ }
1110
+ function mapProjectType2(topic, metadata) {
1111
+ const explicit = readNonEmptyString2(metadata.projectType);
1112
+ if (explicit) {
1113
+ return explicit;
1114
+ }
1115
+ if (topic.type === "theme") {
1116
+ return "thematic";
1117
+ }
1118
+ return readNonEmptyString2(topic.type) || "general";
1119
+ }
1120
+ function isProjectLikeTopic2(topic) {
1121
+ const metadata = readMetadata2(topic);
1122
+ return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId2(topic) !== void 0 || readNonEmptyString2(metadata.projectType) !== void 0;
1123
+ }
1124
+ function isMissingLucernChildComponentError(error) {
1125
+ const message = error instanceof Error ? error.message : String(error);
1126
+ return message.includes(
1127
+ 'Child component ComponentName(Identifier("lucern")) not found'
1128
+ ) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1129
+ }
1130
+ async function resolveTopicDoc2(ctx, scopeId) {
1131
+ if (ctx?.db && typeof ctx.db.get === "function") {
1132
+ try {
1133
+ const directTopic = await ctx.db.get(scopeId);
1134
+ if (directTopic) {
1135
+ return directTopic;
1136
+ }
1137
+ } catch {
1138
+ }
1139
+ }
1140
+ if (typeof ctx.runQuery !== "function") {
1141
+ return null;
1142
+ }
1143
+ try {
1144
+ const topic = await ctx.runQuery(api2.topics.get, {
1145
+ id: String(scopeId)
1146
+ });
1147
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1148
+ return topic;
1149
+ }
1150
+ } catch {
1151
+ }
1152
+ try {
1153
+ const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1154
+ projectId: String(scopeId)
1155
+ });
1156
+ if (topic?.name !== void 0 && topic?.type !== void 0) {
1157
+ return topic;
1158
+ }
1159
+ } catch {
1160
+ }
1161
+ return null;
1162
+ }
1163
+ function materializeTopicProjectOverlay2(topic, idMode = "legacy") {
1164
+ const metadata = readMetadata2(topic);
1165
+ const topicId = String(topic._id);
1166
+ const legacyProjectId = readLegacyProjectId2(topic) || readLegacyProjectId2(metadata) || readNonEmptyString2(metadata.legacyProjectId);
1167
+ const storageProjectId = legacyProjectId || topicId;
1168
+ const outwardId = idMode === "topic" ? topicId : storageProjectId;
1169
+ const visibility = coerceVisibility2(topic.visibility) || coerceVisibility2(metadata.visibility) || "private";
1170
+ const status = coerceStatus2(topic.status) || coerceStatus2(metadata.status) || "active";
1171
+ const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
1172
+ const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
1173
+ return {
1174
+ ...metadata,
1175
+ _id: outwardId,
1176
+ projectId: outwardId,
1177
+ topicId,
1178
+ storageProjectId,
1179
+ legacyProjectId,
1180
+ name: readNonEmptyString2(topic.name) || "Untitled Theme",
1181
+ type: mapProjectType2(topic, metadata),
1182
+ description: readNonEmptyString2(topic.description),
1183
+ ownerId: readNonEmptyString2(metadata.ownerId) || readNonEmptyString2(topic.createdBy) || "system",
1184
+ sharedWith: readStringArray2(metadata.sharedWith),
1185
+ visibility,
1186
+ tenantId: readNonEmptyString2(topic.tenantId) || readNonEmptyString2(metadata.tenantId),
1187
+ workspaceId: readNonEmptyString2(topic.workspaceId) || readNonEmptyString2(metadata.workspaceId),
1188
+ status,
1189
+ tags: readStringArray2(metadata.tags),
1190
+ chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
1191
+ artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
1192
+ lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
1193
+ _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
1194
+ createdAt,
1195
+ updatedAt
1196
+ };
1197
+ }
1198
+ async function resolveTopicProjectOverlay2(ctx, scopeId, options = {}) {
1199
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1200
+ if (!topic) {
1201
+ return null;
1202
+ }
1203
+ if (options.projectLikeOnly !== false && !isProjectLikeTopic2(topic)) {
1204
+ return null;
1205
+ }
1206
+ return materializeTopicProjectOverlay2(topic, options.idMode);
1207
+ }
1208
+ async function listTopicProjectOverlays2(ctx, options = {}) {
1209
+ let allTopics = [];
1210
+ if (ctx?.db?.query && typeof ctx.db.query === "function") {
1211
+ try {
1212
+ allTopics = await ctx.db.query("topics").collect();
1213
+ } catch {
1214
+ allTopics = [];
1215
+ }
1216
+ }
1217
+ if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
1218
+ allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
1219
+ }
1220
+ return allTopics.filter(
1221
+ (topic) => options.projectLikeOnly === false || isProjectLikeTopic2(topic)
1222
+ ).map((topic) => materializeTopicProjectOverlay2(topic, options.idMode));
1223
+ }
1224
+ async function patchTopicProjectOverlay(ctx, scopeId, value) {
1225
+ const topic = await resolveTopicDoc2(ctx, scopeId);
1226
+ if (!topic) {
1227
+ return null;
1228
+ }
1229
+ const nextMetadata = { ...readMetadata2(topic) };
1230
+ const patch = {};
1231
+ const topicUpdateArgs = {
1232
+ id: String(topic._id)
1233
+ };
1234
+ for (const [key, rawValue] of Object.entries(value)) {
1235
+ switch (key) {
1236
+ case "_id":
1237
+ case "projectId":
1238
+ case "topicId":
1239
+ case "legacyProjectId":
1240
+ case "storageProjectId":
1241
+ break;
1242
+ case "name":
1243
+ case "description":
1244
+ patch[key] = rawValue;
1245
+ topicUpdateArgs[key] = rawValue;
1246
+ break;
1247
+ case "tenantId":
1248
+ case "workspaceId":
1249
+ case "ownerId":
1250
+ throw new Error(
1251
+ `patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
1252
+ );
1253
+ case "status": {
1254
+ const status = coerceStatus2(rawValue);
1255
+ if (status) {
1256
+ patch.status = status;
1257
+ topicUpdateArgs.status = status;
1258
+ }
1259
+ break;
1260
+ }
1261
+ case "visibility": {
1262
+ const visibility = coerceVisibility2(rawValue);
1263
+ if (visibility) {
1264
+ patch.visibility = visibility;
1265
+ topicUpdateArgs.visibility = visibility;
1266
+ }
1267
+ break;
1268
+ }
1269
+ case "type": {
1270
+ const projectType = readNonEmptyString2(rawValue);
1271
+ if (projectType) {
1272
+ nextMetadata.projectType = projectType;
1273
+ } else {
1274
+ delete nextMetadata.projectType;
1275
+ }
1276
+ break;
1277
+ }
1278
+ case "updatedAt":
1279
+ case "createdAt":
1280
+ break;
1281
+ default:
1282
+ if (rawValue === void 0) {
1283
+ delete nextMetadata[key];
1284
+ } else {
1285
+ nextMetadata[key] = rawValue;
1286
+ }
1287
+ }
1288
+ }
1289
+ patch.updatedAt = Date.now();
1290
+ patch.metadata = nextMetadata;
1291
+ topicUpdateArgs.metadata = nextMetadata;
1292
+ if (typeof ctx.runMutation === "function") {
1293
+ try {
1294
+ await ctx.runMutation(api2.topics.update, topicUpdateArgs);
1295
+ } catch (error) {
1296
+ if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
1297
+ throw error;
1298
+ }
1299
+ await ctx.db.patch(String(topic._id), patch);
1300
+ }
1301
+ } else if (ctx?.db && typeof ctx.db.patch === "function") {
1302
+ await ctx.db.patch(String(topic._id), patch);
1303
+ } else {
1304
+ throw new Error(
1305
+ "Cannot patch topic without component adapter (ctx.runMutation unavailable)"
1306
+ );
1307
+ }
1308
+ return materializeTopicProjectOverlay2(
1309
+ {
1310
+ ...topic,
1311
+ ...patch,
1312
+ metadata: nextMetadata
1313
+ }
1314
+ );
1315
+ }
1316
+
1317
+ // src/resolvers.ts
1318
+ function isMissingLucernChildComponentError2(error) {
1319
+ const message = error instanceof Error ? error.message : String(error);
1320
+ return message.includes('Child component ComponentName(Identifier("lucern")) not found') || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
1321
+ }
1322
+ function isAdvisoryTopicPatch(value) {
1323
+ const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
1324
+ const keys = Object.keys(value);
1325
+ return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
1326
+ }
1327
+ async function patchProjectWithTolerance(ctx, projectId, value) {
1328
+ try {
1329
+ await patchTopicProjectOverlay(ctx, projectId, value);
1330
+ } catch (error) {
1331
+ if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
1332
+ throw error;
1333
+ }
1334
+ console.warn("[lucern graph-primitives] Non-fatal advisory topic patch failure", {
1335
+ projectId,
1336
+ keys: Object.keys(value),
1337
+ error: error instanceof Error ? error.message : error
1338
+ });
1339
+ }
1340
+ }
1341
+ function defaultResolvers2() {
1342
+ return {
1343
+ async getProject(ctx, projectId) {
1344
+ return await resolveTopicProjectOverlay2(ctx, projectId, {
1345
+ idMode: "legacy",
1346
+ projectLikeOnly: false
1347
+ });
1348
+ },
1349
+ async patchProject(ctx, projectId, value) {
1350
+ await patchProjectWithTolerance(ctx, projectId, value);
1351
+ },
1352
+ async listTopics(ctx) {
1353
+ return await listTopicProjectOverlays2(ctx, {
1354
+ idMode: "legacy"
1355
+ });
1356
+ },
1357
+ async getFinalArtifact(ctx, artifactId) {
1358
+ return await ctx.db.get(artifactId);
1359
+ }
1360
+ };
1361
+ }
1362
+ var resolverOverrides2 = {};
1363
+ function resolveGraphPrimitivesAppResolvers(_ctx) {
1364
+ return {
1365
+ ...defaultResolvers2(),
1366
+ ...resolverOverrides2
1367
+ };
1368
+ }
1369
+ var optionalScopeArgs = {
1370
+ projectId: v.optional(v.string()),
1371
+ topicId: v.optional(v.string())
1372
+ };
1373
+
1374
+ // src/beliefDecay.ts
1375
+ var identifyBeliefsNeedingRescore = query({
1376
+ args: {
1377
+ ...optionalScopeArgs,
1378
+ maxResults: v.optional(v.number()),
1379
+ // Only return beliefs at or above this priority
1380
+ minPriority: v.optional(
1381
+ v.union(
1382
+ v.literal("critical"),
1383
+ v.literal("high"),
1384
+ v.literal("medium"),
1385
+ v.literal("low")
1386
+ )
1387
+ )
1388
+ },
1389
+ returns: permissiveReturn,
1390
+ handler: async (ctx, args) => {
1391
+ const { projectId, maxResults = 20, minPriority = "medium" } = args;
1392
+ const priorityRank = {
1393
+ critical: 4,
1394
+ high: 3,
1395
+ medium: 2,
1396
+ low: 1
1397
+ };
1398
+ const minRank = priorityRank[minPriority] ?? 2;
1399
+ const beliefs = await ctx.db.query("epistemicNodes").withIndex(
1400
+ args.topicId ? "by_topic_type" : "by_project_type",
1401
+ (q) => args.topicId ? q.eq("topicId", args.topicId).eq("nodeType", "belief") : q.eq("projectId", projectId).eq("nodeType", "belief")
1402
+ ).collect();
1403
+ const activeBeliefs = beliefs.filter((b) => b.status === "active");
1404
+ const results = [];
1405
+ for (const belief of activeBeliefs) {
1406
+ const metadata = belief.metadata || {};
1407
+ const predictionMeta = belief.predictionMeta;
1408
+ const temporalNature = belief.temporalNature || "unknown";
1409
+ const beliefStatus = belief.beliefStatus || metadata.beliefStatus || null;
1410
+ const confidenceHistory = await ctx.db.query("beliefConfidence").withIndex("by_beliefId", (q) => q.eq("beliefId", belief._id)).collect();
1411
+ const sorted = confidenceHistory.sort(
1412
+ (a, b) => b.assessedAt - a.assessedAt
1413
+ );
1414
+ const lastEntry = sorted[0] ?? null;
1415
+ const lastScoredAt = lastEntry?.assessedAt || belief.updatedAt || belief.createdAt || Date.now();
1416
+ const confidence = lastEntry?.confidence ?? metadata.confidence ?? 0.5;
1417
+ const schedule = getRescoringSchedule({
1418
+ lastScoredAt,
1419
+ temporalNature,
1420
+ expectedBy: predictionMeta?.expectedBy ?? null,
1421
+ predictionMeta: predictionMeta ?? null,
1422
+ confidence,
1423
+ beliefStatus
1424
+ });
1425
+ if (priorityRank[schedule.priority] >= minRank) {
1426
+ results.push({
1427
+ beliefId: belief._id,
1428
+ beliefText: belief.canonicalText,
1429
+ confidence,
1430
+ beliefStatus: beliefStatus ?? "assumption",
1431
+ temporalNature,
1432
+ expectedBy: predictionMeta?.expectedBy ?? null,
1433
+ lastScoredAt,
1434
+ daysSinceScoring: Math.round(schedule.decay.ageDays),
1435
+ schedule
1436
+ });
1437
+ }
1438
+ }
1439
+ results.sort((a, b) => {
1440
+ const rankDiff = priorityRank[b.schedule.priority] - priorityRank[a.schedule.priority];
1441
+ if (rankDiff !== 0) {
1442
+ return rankDiff;
1443
+ }
1444
+ return a.schedule.daysUntilRescore - b.schedule.daysUntilRescore;
1445
+ });
1446
+ return {
1447
+ beliefs: results.slice(0, maxResults),
1448
+ summary: {
1449
+ total: results.length,
1450
+ critical: results.filter((r) => r.schedule.priority === "critical").length,
1451
+ high: results.filter((r) => r.schedule.priority === "high").length,
1452
+ medium: results.filter((r) => r.schedule.priority === "medium").length,
1453
+ overdue: results.filter((r) => r.schedule.isOverdue).length,
1454
+ forecastsApproaching: results.filter(
1455
+ (r) => r.temporalNature === "forecast" && r.schedule.decay.urgency?.urgencyTier.label !== "distant"
1456
+ ).length
1457
+ }
1458
+ };
1459
+ }
1460
+ });
1461
+ var getGlobalBeliefHealth = query({
1462
+ args: {
1463
+ clerkId: v.string(),
1464
+ maxResults: v.optional(v.number()),
1465
+ minPriority: v.optional(
1466
+ v.union(
1467
+ v.literal("critical"),
1468
+ v.literal("high"),
1469
+ v.literal("medium"),
1470
+ v.literal("low")
1471
+ )
1472
+ )
1473
+ },
1474
+ returns: permissiveReturn,
1475
+ handler: async (ctx, args) => {
1476
+ const { clerkId, maxResults = 30, minPriority = "medium" } = args;
1477
+ const priorityRank = {
1478
+ critical: 4,
1479
+ high: 3,
1480
+ medium: 2,
1481
+ low: 1
1482
+ };
1483
+ const minRank = priorityRank[minPriority] ?? 2;
1484
+ const allProjects = await resolveGraphPrimitivesAppResolvers().listTopics(ctx);
1485
+ const accessibleProjectIds = await getAccessibleProjectIds(ctx, clerkId);
1486
+ const accessibleProjects = allProjects.filter(
1487
+ (project2) => accessibleProjectIds.has(project2._id)
1488
+ );
1489
+ const allResults = [];
1490
+ for (const project2 of accessibleProjects.slice(0, 20)) {
1491
+ const [topicBeliefs, projectBeliefs] = await Promise.all([
1492
+ ctx.db.query("epistemicNodes").withIndex(
1493
+ "by_topic_type",
1494
+ (q) => q.eq("topicId", project2._id).eq("nodeType", "belief")
1495
+ ).collect(),
1496
+ ctx.db.query("epistemicNodes").withIndex(
1497
+ "by_project_type",
1498
+ (q) => q.eq("projectId", project2._id).eq("nodeType", "belief")
1499
+ ).collect()
1500
+ ]);
1501
+ const beliefs = Array.from(
1502
+ new Map(
1503
+ [...topicBeliefs, ...projectBeliefs].map((b) => [String(b._id), b])
1504
+ ).values()
1505
+ );
1506
+ const activeBeliefs = beliefs.filter((b) => b.status === "active");
1507
+ for (const belief of activeBeliefs) {
1508
+ const metadata = belief.metadata || {};
1509
+ const predictionMeta = belief.predictionMeta;
1510
+ const temporalNature = belief.temporalNature || "unknown";
1511
+ const confidence = belief.confidence ?? metadata.confidence ?? 0.5;
1512
+ const lastScoredAt = belief.updatedAt || belief.createdAt || belief._creationTime;
1513
+ const schedule = getRescoringSchedule({
1514
+ lastScoredAt,
1515
+ temporalNature,
1516
+ expectedBy: predictionMeta?.expectedBy ?? null,
1517
+ predictionMeta: predictionMeta ?? null,
1518
+ confidence,
1519
+ beliefStatus: belief.beliefStatus || metadata.beliefStatus || null
1520
+ });
1521
+ if (priorityRank[schedule.priority] >= minRank) {
1522
+ allResults.push({
1523
+ projectId: project2._id,
1524
+ projectName: project2.name,
1525
+ beliefId: belief._id,
1526
+ beliefText: belief.canonicalText,
1527
+ confidence,
1528
+ temporalNature,
1529
+ expectedBy: predictionMeta?.expectedBy ?? null,
1530
+ daysSinceScoring: Math.round(schedule.decay.ageDays),
1531
+ priority: schedule.priority,
1532
+ reason: schedule.reason,
1533
+ deadlineUrgency: schedule.decay.urgency?.urgencyTier.label ?? null
1534
+ });
1535
+ }
1536
+ }
1537
+ }
1538
+ allResults.sort((a, b) => {
1539
+ const rankDiff = priorityRank[b.priority] - priorityRank[a.priority];
1540
+ if (rankDiff !== 0) {
1541
+ return rankDiff;
1542
+ }
1543
+ return b.daysSinceScoring - a.daysSinceScoring;
1544
+ });
1545
+ const results = allResults.slice(0, maxResults);
1546
+ const projectGroups = /* @__PURE__ */ new Map();
1547
+ for (const r of allResults) {
1548
+ projectGroups.set(
1549
+ r.projectName,
1550
+ (projectGroups.get(r.projectName) || 0) + 1
1551
+ );
1552
+ }
1553
+ return {
1554
+ beliefs: results,
1555
+ summary: {
1556
+ total: allResults.length,
1557
+ critical: allResults.filter((r) => r.priority === "critical").length,
1558
+ high: allResults.filter((r) => r.priority === "high").length,
1559
+ medium: allResults.filter((r) => r.priority === "medium").length,
1560
+ projectsAffected: projectGroups.size,
1561
+ forecastsApproaching: allResults.filter(
1562
+ (r) => r.temporalNature === "forecast" && r.deadlineUrgency && r.deadlineUrgency !== "distant"
1563
+ ).length,
1564
+ byProject: Array.from(projectGroups.entries()).map(([name, count]) => ({
1565
+ projectName: name,
1566
+ count
1567
+ }))
1568
+ }
1569
+ };
1570
+ }
1571
+ });
1572
+ var getBeliefDecayInfo = query({
1573
+ args: {
1574
+ beliefId: v.id("epistemicNodes")
1575
+ },
1576
+ returns: permissiveReturn,
1577
+ handler: async (ctx, args) => {
1578
+ const belief = await ctx.db.get(args.beliefId);
1579
+ if (!belief) {
1580
+ return null;
1581
+ }
1582
+ const metadata = belief.metadata || {};
1583
+ const predictionMeta = belief.predictionMeta;
1584
+ const temporalNature = belief.temporalNature || "unknown";
1585
+ const confidenceHistory = await ctx.db.query("beliefConfidence").withIndex("by_beliefId", (q) => q.eq("beliefId", args.beliefId)).collect();
1586
+ const sorted = confidenceHistory.sort(
1587
+ (a, b) => b.assessedAt - a.assessedAt
1588
+ );
1589
+ const lastEntry = sorted[0] ?? null;
1590
+ const lastScoredAt = lastEntry?.assessedAt || belief.updatedAt || belief.createdAt || Date.now();
1591
+ const confidence = lastEntry?.confidence ?? metadata.confidence ?? 0.5;
1592
+ const schedule = getRescoringSchedule({
1593
+ lastScoredAt,
1594
+ temporalNature,
1595
+ expectedBy: predictionMeta?.expectedBy ?? null,
1596
+ predictionMeta: predictionMeta ?? null,
1597
+ confidence,
1598
+ beliefStatus: belief.beliefStatus || metadata.beliefStatus || null
1599
+ });
1600
+ const decay = schedule.decay;
1601
+ return {
1602
+ confidence,
1603
+ decayedConfidence: confidence * decay.effectiveWeight,
1604
+ freshness: {
1605
+ label: decay.baseTier.label,
1606
+ ageDays: Math.round(decay.ageDays),
1607
+ weight: decay.effectiveWeight,
1608
+ action: decay.action
1609
+ },
1610
+ deadline: decay.urgency ? {
1611
+ daysToDeadline: decay.urgency.daysToDeadline,
1612
+ urgency: decay.urgency.urgencyTier.label,
1613
+ isOverdue: decay.urgency.isOverdue
1614
+ } : null,
1615
+ rescoringSchedule: {
1616
+ nextRescoreBy: schedule.nextRescoreBy,
1617
+ daysUntilRescore: schedule.daysUntilRescore,
1618
+ isOverdue: schedule.isOverdue,
1619
+ priority: schedule.priority,
1620
+ reason: schedule.reason
1621
+ }
1622
+ };
1623
+ }
1624
+ });
1625
+
1626
+ export { DEADLINE_URGENCY, DECAY_TIERS, bayesianUpdate, computeBaseDecay, computeDeadlineUrgency, computeEffectiveDecay, getBeliefDecayInfo, getGlobalBeliefHealth, getRescoringSchedule, identifyBeliefsNeedingRescore };
1627
+ //# sourceMappingURL=beliefDecay.js.map
1628
+ //# sourceMappingURL=beliefDecay.js.map