@lucern/graph-primitives 0.1.0-alpha.4 → 0.3.0-alpha.1
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.
- package/dist/beliefDecay.js +229 -1115
- package/dist/beliefDecay.js.map +1 -1
- package/dist/beliefEvidenceLinks.js +53 -834
- package/dist/beliefEvidenceLinks.js.map +1 -1
- package/dist/confidencePropagationDispatch.d.ts +3 -3
- package/dist/confidencePropagationDispatch.js +30 -308
- package/dist/confidencePropagationDispatch.js.map +1 -1
- package/dist/contradictions.js +5 -797
- package/dist/contradictions.js.map +1 -1
- package/dist/edges/contradicts.js +1 -122
- package/dist/edges/contradicts.js.map +1 -1
- package/dist/edges/dependsOn.js +14 -172
- package/dist/edges/dependsOn.js.map +1 -1
- package/dist/edges/elaborates.js +1 -49
- package/dist/edges/elaborates.js.map +1 -1
- package/dist/edges/index.js +14 -277
- package/dist/edges/index.js.map +1 -1
- package/dist/edges/informs.js +1 -62
- package/dist/edges/informs.js.map +1 -1
- package/dist/edges/propagationTypes.d.ts +2 -2
- package/dist/edges/propagationTypes.js.map +1 -1
- package/dist/edges/refutes.js +1 -62
- package/dist/edges/refutes.js.map +1 -1
- package/dist/edges/supports.js +1 -122
- package/dist/edges/supports.js.map +1 -1
- package/dist/edges/utils.d.ts +6 -6
- package/dist/edges/utils.js +1 -130
- package/dist/edges/utils.js.map +1 -1
- package/dist/entityBridge.js +2 -17
- package/dist/entityBridge.js.map +1 -1
- package/dist/entityLifecycle.js +62 -848
- package/dist/entityLifecycle.js.map +1 -1
- package/dist/epistemicAnswers.js +27 -838
- package/dist/epistemicAnswers.js.map +1 -1
- package/dist/epistemicBeliefs.js +186 -2214
- package/dist/epistemicBeliefs.js.map +1 -1
- package/dist/epistemicContractHelpers.js +1 -318
- package/dist/epistemicContractHelpers.js.map +1 -1
- package/dist/epistemicContracts.js +163 -2467
- package/dist/epistemicContracts.js.map +1 -1
- package/dist/epistemicEdges.js +60 -863
- package/dist/epistemicEdges.js.map +1 -1
- package/dist/epistemicEvidence.js +116 -1647
- package/dist/epistemicEvidence.js.map +1 -1
- package/dist/epistemicHelpers.js +3 -2
- package/dist/epistemicHelpers.js.map +1 -1
- package/dist/epistemicLinking.js +2 -785
- package/dist/epistemicLinking.js.map +1 -1
- package/dist/epistemicNodes.js +34 -1427
- package/dist/epistemicNodes.js.map +1 -1
- package/dist/epistemicQuestions.js +88 -1637
- package/dist/epistemicQuestions.js.map +1 -1
- package/dist/epistemicSources.js +28 -1421
- package/dist/epistemicSources.js.map +1 -1
- package/dist/evaluators/index.js +163 -2467
- package/dist/evaluators/index.js.map +1 -1
- package/dist/index.js +486 -3649
- package/dist/index.js.map +1 -1
- package/dist/ontology-matching.js +1 -344
- package/dist/ontology-matching.js.map +1 -1
- package/dist/ontologyApproval.js +1 -13
- package/dist/ontologyApproval.js.map +1 -1
- package/dist/ontologyDefinitions.js +2 -17
- package/dist/ontologyDefinitions.js.map +1 -1
- package/dist/ontologyRegistry.js +2 -17
- package/dist/ontologyRegistry.js.map +1 -1
- package/dist/projectionReconciliation.js +2 -17
- package/dist/projectionReconciliation.js.map +1 -1
- package/dist/questionEvidenceLinks.js +242 -837
- package/dist/questionEvidenceLinks.js.map +1 -1
- package/dist/text-matching.js +1 -244
- package/dist/text-matching.js.map +1 -1
- package/dist/workflowBridge.d.ts +27 -0
- package/dist/workflowBridge.js +303 -0
- package/dist/workflowBridge.js.map +1 -0
- package/dist/workspaceIsolation.js +8 -609
- package/dist/workspaceIsolation.js.map +1 -1
- package/package.json +6 -6
package/dist/beliefDecay.js
CHANGED
|
@@ -1,355 +1,16 @@
|
|
|
1
1
|
import { v } from 'convex/values';
|
|
2
|
+
import { getRescoringSchedule } from '@lucern/confidence';
|
|
3
|
+
export { DEADLINE_URGENCY, DECAY_TIERS, bayesianUpdate, computeBaseDecay, computeDeadlineUrgency, computeEffectiveDecay, getRescoringSchedule } from '@lucern/confidence';
|
|
4
|
+
import { getAccessibleProjectIds } from '@lucern/access-control/access';
|
|
5
|
+
import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
|
|
2
6
|
import { componentsGeneric, queryGeneric, anyApi } from 'convex/server';
|
|
3
7
|
|
|
4
8
|
// 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
9
|
var api = anyApi;
|
|
350
10
|
componentsGeneric();
|
|
11
|
+
var query = queryGeneric;
|
|
351
12
|
|
|
352
|
-
//
|
|
13
|
+
// src/topicProjectOverlay.ts
|
|
353
14
|
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
354
15
|
function readNonEmptyString(value) {
|
|
355
16
|
if (typeof value !== "string") {
|
|
@@ -393,6 +54,12 @@ function isProjectLikeTopic(topic) {
|
|
|
393
54
|
const metadata = readMetadata(topic);
|
|
394
55
|
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
395
56
|
}
|
|
57
|
+
function isMissingLucernChildComponentError(error) {
|
|
58
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
59
|
+
return message.includes(
|
|
60
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
61
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
62
|
+
}
|
|
396
63
|
async function resolveTopicDoc(ctx, scopeId) {
|
|
397
64
|
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
398
65
|
try {
|
|
@@ -487,746 +154,12 @@ async function listTopicProjectOverlays(ctx, options = {}) {
|
|
|
487
154
|
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
488
155
|
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
489
156
|
}
|
|
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
157
|
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
1225
|
-
const topic = await
|
|
158
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
1226
159
|
if (!topic) {
|
|
1227
160
|
return null;
|
|
1228
161
|
}
|
|
1229
|
-
const nextMetadata = { ...
|
|
162
|
+
const nextMetadata = { ...readMetadata(topic) };
|
|
1230
163
|
const patch = {};
|
|
1231
164
|
const topicUpdateArgs = {
|
|
1232
165
|
id: String(topic._id)
|
|
@@ -1251,7 +184,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1251
184
|
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
1252
185
|
);
|
|
1253
186
|
case "status": {
|
|
1254
|
-
const status =
|
|
187
|
+
const status = coerceStatus(rawValue);
|
|
1255
188
|
if (status) {
|
|
1256
189
|
patch.status = status;
|
|
1257
190
|
topicUpdateArgs.status = status;
|
|
@@ -1259,7 +192,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1259
192
|
break;
|
|
1260
193
|
}
|
|
1261
194
|
case "visibility": {
|
|
1262
|
-
const visibility =
|
|
195
|
+
const visibility = coerceVisibility(rawValue);
|
|
1263
196
|
if (visibility) {
|
|
1264
197
|
patch.visibility = visibility;
|
|
1265
198
|
topicUpdateArgs.visibility = visibility;
|
|
@@ -1267,7 +200,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1267
200
|
break;
|
|
1268
201
|
}
|
|
1269
202
|
case "type": {
|
|
1270
|
-
const projectType =
|
|
203
|
+
const projectType = readNonEmptyString(rawValue);
|
|
1271
204
|
if (projectType) {
|
|
1272
205
|
nextMetadata.projectType = projectType;
|
|
1273
206
|
} else {
|
|
@@ -1291,7 +224,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1291
224
|
topicUpdateArgs.metadata = nextMetadata;
|
|
1292
225
|
if (typeof ctx.runMutation === "function") {
|
|
1293
226
|
try {
|
|
1294
|
-
await ctx.runMutation(
|
|
227
|
+
await ctx.runMutation(api.topics.update, topicUpdateArgs);
|
|
1295
228
|
} catch (error) {
|
|
1296
229
|
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
1297
230
|
throw error;
|
|
@@ -1305,7 +238,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1305
238
|
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
1306
239
|
);
|
|
1307
240
|
}
|
|
1308
|
-
return
|
|
241
|
+
return materializeTopicProjectOverlay(
|
|
1309
242
|
{
|
|
1310
243
|
...topic,
|
|
1311
244
|
...patch,
|
|
@@ -1338,10 +271,10 @@ async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
|
1338
271
|
});
|
|
1339
272
|
}
|
|
1340
273
|
}
|
|
1341
|
-
function
|
|
274
|
+
function defaultResolvers() {
|
|
1342
275
|
return {
|
|
1343
276
|
async getProject(ctx, projectId) {
|
|
1344
|
-
return await
|
|
277
|
+
return await resolveTopicProjectOverlay(ctx, projectId, {
|
|
1345
278
|
idMode: "legacy",
|
|
1346
279
|
projectLikeOnly: false
|
|
1347
280
|
});
|
|
@@ -1350,7 +283,7 @@ function defaultResolvers2() {
|
|
|
1350
283
|
await patchProjectWithTolerance(ctx, projectId, value);
|
|
1351
284
|
},
|
|
1352
285
|
async listTopics(ctx) {
|
|
1353
|
-
return await
|
|
286
|
+
return await listTopicProjectOverlays(ctx, {
|
|
1354
287
|
idMode: "legacy"
|
|
1355
288
|
});
|
|
1356
289
|
},
|
|
@@ -1359,13 +292,195 @@ function defaultResolvers2() {
|
|
|
1359
292
|
}
|
|
1360
293
|
};
|
|
1361
294
|
}
|
|
1362
|
-
var
|
|
295
|
+
var resolverOverrides = {};
|
|
1363
296
|
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
1364
297
|
return {
|
|
1365
|
-
...
|
|
1366
|
-
...
|
|
298
|
+
...defaultResolvers(),
|
|
299
|
+
...resolverOverrides
|
|
1367
300
|
};
|
|
1368
301
|
}
|
|
302
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
303
|
+
function asMappedProjectId(topic) {
|
|
304
|
+
if (!topic) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD2]);
|
|
308
|
+
if (directLegacyProjectId) {
|
|
309
|
+
return directLegacyProjectId;
|
|
310
|
+
}
|
|
311
|
+
const metadata = topic.metadata || {};
|
|
312
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD2] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
313
|
+
return candidate ? candidate : void 0;
|
|
314
|
+
}
|
|
315
|
+
function normalizeScopeValue(value) {
|
|
316
|
+
if (typeof value !== "string") {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const normalized = value.trim();
|
|
320
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
321
|
+
}
|
|
322
|
+
function pickPrimaryTopic(candidates) {
|
|
323
|
+
return [...candidates].sort((a, b) => {
|
|
324
|
+
const depthA = a.depth ?? 9999;
|
|
325
|
+
const depthB = b.depth ?? 9999;
|
|
326
|
+
if (depthA !== depthB) {
|
|
327
|
+
return depthA - depthB;
|
|
328
|
+
}
|
|
329
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
330
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
331
|
+
if (createdA !== createdB) {
|
|
332
|
+
return createdA - createdB;
|
|
333
|
+
}
|
|
334
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
335
|
+
})[0];
|
|
336
|
+
}
|
|
337
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
338
|
+
try {
|
|
339
|
+
return await ctx.db.query("topics").withIndex(
|
|
340
|
+
"by_graph_scope_project",
|
|
341
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
|
|
342
|
+
).collect();
|
|
343
|
+
} catch {
|
|
344
|
+
const topics = await ctx.db.query("topics").collect();
|
|
345
|
+
return topics.filter((topic) => {
|
|
346
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
347
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
348
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
353
|
+
if (typeof ctx.runQuery !== "function") {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
return await ctx.runQuery(api.topics.get, {
|
|
358
|
+
id: topicId
|
|
359
|
+
}) ?? null;
|
|
360
|
+
} catch {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
365
|
+
if (typeof ctx.runQuery !== "function") {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
try {
|
|
369
|
+
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
370
|
+
projectId: legacyScopeId
|
|
371
|
+
}) ?? null;
|
|
372
|
+
} catch {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
377
|
+
const MAX_DEPTH = 10;
|
|
378
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
379
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
380
|
+
if (tenantId && workspaceId) {
|
|
381
|
+
return { tenantId, workspaceId };
|
|
382
|
+
}
|
|
383
|
+
let current = topic;
|
|
384
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
385
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
386
|
+
if (!current) break;
|
|
387
|
+
if (!tenantId) {
|
|
388
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
389
|
+
}
|
|
390
|
+
if (!workspaceId) {
|
|
391
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
392
|
+
}
|
|
393
|
+
if (tenantId && workspaceId) break;
|
|
394
|
+
}
|
|
395
|
+
return { tenantId, workspaceId };
|
|
396
|
+
}
|
|
397
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
398
|
+
if (args.topicId) {
|
|
399
|
+
let topic = null;
|
|
400
|
+
try {
|
|
401
|
+
topic = await ctx.db.get(args.topicId);
|
|
402
|
+
} catch {
|
|
403
|
+
}
|
|
404
|
+
if (!topic) {
|
|
405
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
406
|
+
}
|
|
407
|
+
if (!topic) {
|
|
408
|
+
topic = pickPrimaryTopic(
|
|
409
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
410
|
+
) ?? null;
|
|
411
|
+
}
|
|
412
|
+
if (!topic) {
|
|
413
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
414
|
+
}
|
|
415
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
416
|
+
const mapped = asMappedProjectId(topic);
|
|
417
|
+
if (mapped) {
|
|
418
|
+
return {
|
|
419
|
+
topicId: topic._id,
|
|
420
|
+
projectId: mapped,
|
|
421
|
+
tenantId: inherited.tenantId,
|
|
422
|
+
workspaceId: inherited.workspaceId,
|
|
423
|
+
source: "topic"
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
topicId: topic._id,
|
|
428
|
+
tenantId: inherited.tenantId,
|
|
429
|
+
workspaceId: inherited.workspaceId,
|
|
430
|
+
source: "topic"
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
if (args.projectId) {
|
|
434
|
+
let directTopic = null;
|
|
435
|
+
try {
|
|
436
|
+
directTopic = await ctx.db.get(
|
|
437
|
+
args.projectId
|
|
438
|
+
);
|
|
439
|
+
} catch {
|
|
440
|
+
}
|
|
441
|
+
if (directTopic) {
|
|
442
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
443
|
+
const mapped = asMappedProjectId(directTopic);
|
|
444
|
+
return {
|
|
445
|
+
topicId: directTopic._id,
|
|
446
|
+
projectId: mapped ?? args.projectId,
|
|
447
|
+
tenantId: inherited.tenantId,
|
|
448
|
+
workspaceId: inherited.workspaceId,
|
|
449
|
+
source: "topic_inferred"
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
453
|
+
if (directTopic) {
|
|
454
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
455
|
+
const mapped = asMappedProjectId(directTopic);
|
|
456
|
+
return {
|
|
457
|
+
topicId: directTopic._id,
|
|
458
|
+
projectId: mapped ?? args.projectId,
|
|
459
|
+
tenantId: inherited.tenantId,
|
|
460
|
+
workspaceId: inherited.workspaceId,
|
|
461
|
+
source: "topic_inferred"
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
465
|
+
const primary = pickPrimaryTopic(topics);
|
|
466
|
+
if (primary) {
|
|
467
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
468
|
+
return {
|
|
469
|
+
topicId: primary._id,
|
|
470
|
+
projectId: args.projectId,
|
|
471
|
+
tenantId: inherited.tenantId,
|
|
472
|
+
workspaceId: inherited.workspaceId,
|
|
473
|
+
source: "project_mapped_topic"
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
throw new Error(
|
|
477
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
throw new Error(
|
|
481
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
482
|
+
);
|
|
483
|
+
}
|
|
1369
484
|
var optionalScopeArgs = {
|
|
1370
485
|
projectId: v.optional(v.string()),
|
|
1371
486
|
topicId: v.optional(v.string())
|
|
@@ -1388,7 +503,11 @@ var identifyBeliefsNeedingRescore = query({
|
|
|
1388
503
|
},
|
|
1389
504
|
returns: permissiveReturn,
|
|
1390
505
|
handler: async (ctx, args) => {
|
|
1391
|
-
const {
|
|
506
|
+
const { maxResults = 20, minPriority = "medium" } = args;
|
|
507
|
+
const scope = await resolveTopicProjectScope(ctx, args).catch(() => null);
|
|
508
|
+
if (!scope) {
|
|
509
|
+
return [];
|
|
510
|
+
}
|
|
1392
511
|
const priorityRank = {
|
|
1393
512
|
critical: 4,
|
|
1394
513
|
high: 3,
|
|
@@ -1397,8 +516,8 @@ var identifyBeliefsNeedingRescore = query({
|
|
|
1397
516
|
};
|
|
1398
517
|
const minRank = priorityRank[minPriority] ?? 2;
|
|
1399
518
|
const beliefs = await ctx.db.query("epistemicNodes").withIndex(
|
|
1400
|
-
|
|
1401
|
-
(q) =>
|
|
519
|
+
"by_topic_type",
|
|
520
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "belief")
|
|
1402
521
|
).collect();
|
|
1403
522
|
const activeBeliefs = beliefs.filter((b) => b.status === "active");
|
|
1404
523
|
const results = [];
|
|
@@ -1484,25 +603,20 @@ var getGlobalBeliefHealth = query({
|
|
|
1484
603
|
const allProjects = await resolveGraphPrimitivesAppResolvers().listTopics(ctx);
|
|
1485
604
|
const accessibleProjectIds = await getAccessibleProjectIds(ctx, clerkId);
|
|
1486
605
|
const accessibleProjects = allProjects.filter(
|
|
1487
|
-
(
|
|
606
|
+
(project) => accessibleProjectIds.has(project._id)
|
|
1488
607
|
);
|
|
1489
608
|
const allResults = [];
|
|
1490
|
-
for (const
|
|
1491
|
-
const
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
).
|
|
1500
|
-
|
|
1501
|
-
const beliefs = Array.from(
|
|
1502
|
-
new Map(
|
|
1503
|
-
[...topicBeliefs, ...projectBeliefs].map((b) => [String(b._id), b])
|
|
1504
|
-
).values()
|
|
1505
|
-
);
|
|
609
|
+
for (const project of accessibleProjects.slice(0, 20)) {
|
|
610
|
+
const scope = await resolveTopicProjectScope(ctx, {
|
|
611
|
+
projectId: String(project._id)
|
|
612
|
+
}).catch(() => null);
|
|
613
|
+
if (!scope) {
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
const beliefs = await ctx.db.query("epistemicNodes").withIndex(
|
|
617
|
+
"by_topic_type",
|
|
618
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "belief")
|
|
619
|
+
).collect();
|
|
1506
620
|
const activeBeliefs = beliefs.filter((b) => b.status === "active");
|
|
1507
621
|
for (const belief of activeBeliefs) {
|
|
1508
622
|
const metadata = belief.metadata || {};
|
|
@@ -1520,8 +634,8 @@ var getGlobalBeliefHealth = query({
|
|
|
1520
634
|
});
|
|
1521
635
|
if (priorityRank[schedule.priority] >= minRank) {
|
|
1522
636
|
allResults.push({
|
|
1523
|
-
projectId:
|
|
1524
|
-
projectName:
|
|
637
|
+
projectId: project._id,
|
|
638
|
+
projectName: project.name,
|
|
1525
639
|
beliefId: belief._id,
|
|
1526
640
|
beliefText: belief.canonicalText,
|
|
1527
641
|
confidence,
|
|
@@ -1623,6 +737,6 @@ var getBeliefDecayInfo = query({
|
|
|
1623
737
|
}
|
|
1624
738
|
});
|
|
1625
739
|
|
|
1626
|
-
export {
|
|
740
|
+
export { getBeliefDecayInfo, getGlobalBeliefHealth, identifyBeliefsNeedingRescore };
|
|
1627
741
|
//# sourceMappingURL=beliefDecay.js.map
|
|
1628
742
|
//# sourceMappingURL=beliefDecay.js.map
|