@lucern/graph-primitives 0.3.0-alpha.0 → 0.3.0-alpha.10
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-Q_26RTc-.d.ts → beliefDecay-DZ6tkLYq.d.ts} +1 -1
- package/dist/beliefDecay.d.ts +1 -1
- package/dist/beliefDecay.js +188 -1144
- package/dist/beliefDecay.js.map +1 -1
- package/dist/{beliefEvidenceLinks-42FlR48t.d.ts → beliefEvidenceLinks-CWOXxxJg.d.ts} +1 -1
- package/dist/beliefEvidenceLinks.d.ts +1 -1
- package/dist/beliefEvidenceLinks.js +186 -871
- package/dist/beliefEvidenceLinks.js.map +1 -1
- package/dist/{beliefLifecycle-C-AehZgF.d.ts → beliefLifecycle-y8WLXqQj.d.ts} +1 -1
- package/dist/beliefLifecycle.d.ts +1 -1
- package/dist/confidencePropagationDispatch.d.ts +4 -4
- package/dist/confidencePropagationDispatch.js +31 -311
- package/dist/confidencePropagationDispatch.js.map +1 -1
- package/dist/{contradictions-Hdwl7zid.d.ts → contradictions-51VLsESq.d.ts} +1 -1
- package/dist/contradictions.d.ts +1 -1
- package/dist/contradictions.js +67 -800
- package/dist/contradictions.js.map +1 -1
- package/dist/debug.d.ts +4 -0
- package/dist/debug.js +34 -0
- package/dist/debug.js.map +1 -0
- 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 +15 -280
- package/dist/edges/index.js.map +1 -1
- package/dist/edges/informs.js +2 -65
- 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 +2 -65
- 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 +7 -7
- package/dist/edges/utils.js +2 -133
- package/dist/edges/utils.js.map +1 -1
- package/dist/embeddingTrigger.js +21 -1
- package/dist/embeddingTrigger.js.map +1 -1
- package/dist/entityBridge.js +3 -18
- package/dist/entityBridge.js.map +1 -1
- package/dist/{entityLifecycle-BkhRJ-XI.d.ts → entityLifecycle-CvgSK5FV.d.ts} +1 -1
- package/dist/entityLifecycle.d.ts +1 -1
- package/dist/entityLifecycle.js +193 -892
- package/dist/entityLifecycle.js.map +1 -1
- package/dist/{epistemicAnswers-DSP1slZ9.d.ts → epistemicAnswers-C5ib4z6_.d.ts} +1 -1
- package/dist/epistemicAnswers.d.ts +1 -1
- package/dist/epistemicAnswers.js +73 -810
- package/dist/epistemicAnswers.js.map +1 -1
- package/dist/{epistemicBeliefs-DtFVTp-k.d.ts → epistemicBeliefs-DzKjZAeC.d.ts} +3 -3
- package/dist/epistemicBeliefs.d.ts +2 -2
- package/dist/epistemicBeliefs.js +404 -1698
- package/dist/epistemicBeliefs.js.map +1 -1
- package/dist/epistemicContractHelpers.js +1 -318
- package/dist/epistemicContractHelpers.js.map +1 -1
- package/dist/epistemicContracts.d.ts +1 -1
- package/dist/epistemicContracts.js +417 -1980
- package/dist/epistemicContracts.js.map +1 -1
- package/dist/{epistemicEdges-DcA8ErUG.d.ts → epistemicEdges-CD5vxmlH.d.ts} +3 -3
- package/dist/epistemicEdges.d.ts +1 -1
- package/dist/epistemicEdges.js +248 -919
- package/dist/epistemicEdges.js.map +1 -1
- package/dist/{epistemicEvidence-Bo638XDP.d.ts → epistemicEvidence-xw6UUrwh.d.ts} +1 -1
- package/dist/epistemicEvidence.d.ts +1 -1
- package/dist/epistemicEvidence.js +229 -1087
- package/dist/epistemicEvidence.js.map +1 -1
- package/dist/{epistemicHelpers-Bd9xbaib.d.ts → epistemicHelpers-DevrYgPN.d.ts} +1 -1
- package/dist/epistemicHelpers.d.ts +1 -1
- package/dist/{epistemicLinking-CyeLOIzN.d.ts → epistemicLinking-CfE00tHJ.d.ts} +1 -1
- package/dist/epistemicLinking.d.ts +1 -1
- package/dist/epistemicLinking.js +3 -786
- package/dist/epistemicLinking.js.map +1 -1
- package/dist/{epistemicNodes-BpD6Koud.d.ts → epistemicNodes-NBrPW7fk.d.ts} +2 -2
- package/dist/epistemicNodes.d.ts +1 -1
- package/dist/epistemicNodes.js +172 -899
- package/dist/epistemicNodes.js.map +1 -1
- package/dist/{epistemicQuestions-CmEeY6zQ.d.ts → epistemicQuestions-B_nUclrH.d.ts} +1 -1
- package/dist/epistemicQuestions.d.ts +1 -1
- package/dist/epistemicQuestions.js +369 -1125
- package/dist/epistemicQuestions.js.map +1 -1
- package/dist/{epistemicSources-ZazxHOK1.d.ts → epistemicSources-dlKj58Jp.d.ts} +1 -1
- package/dist/epistemicSources.d.ts +1 -1
- package/dist/epistemicSources.js +86 -886
- package/dist/epistemicSources.js.map +1 -1
- package/dist/evaluators/index.js +417 -1980
- package/dist/evaluators/index.js.map +1 -1
- package/dist/evaluators/lintCheckerEvaluator.js.map +1 -1
- package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -1
- package/dist/evaluators/shared.js +20 -1
- package/dist/evaluators/shared.js.map +1 -1
- package/dist/evaluators/testRunnerEvaluator.js +20 -1
- package/dist/evaluators/testRunnerEvaluator.js.map +1 -1
- package/dist/evaluators/tscCheckerEvaluator.js.map +1 -1
- package/dist/index.d.ts +20 -20
- package/dist/index.js +965 -3004
- package/dist/index.js.map +1 -1
- package/dist/{ontology-matching-Buhu23ss.d.ts → ontology-matching-C6rrz2VP.d.ts} +1 -1
- package/dist/ontology-matching.d.ts +1 -1
- package/dist/ontology-matching.js +1 -344
- package/dist/ontology-matching.js.map +1 -1
- package/dist/{ontologyApproval-Ba0Jjk1k.d.ts → ontologyApproval-CFYmqKmk.d.ts} +1 -1
- package/dist/ontologyApproval.d.ts +1 -1
- package/dist/ontologyApproval.js +1 -13
- package/dist/ontologyApproval.js.map +1 -1
- package/dist/ontologyDefinitions.js +6 -20
- package/dist/ontologyDefinitions.js.map +1 -1
- package/dist/ontologyHelpers.d.ts +1 -1
- package/dist/ontologyHelpers.js +4 -3
- package/dist/ontologyHelpers.js.map +1 -1
- package/dist/ontologyRegistry.js +2 -17
- package/dist/ontologyRegistry.js.map +1 -1
- package/dist/{projectionReconciliation-CxrXYGaB.d.ts → projectionReconciliation-jww2fBI0.d.ts} +1 -1
- package/dist/projectionReconciliation.d.ts +1 -1
- package/dist/projectionReconciliation.js +16 -37
- package/dist/projectionReconciliation.js.map +1 -1
- package/dist/{projectionStaleness-CAdpIsaW.d.ts → projectionStaleness-CmdbpjVK.d.ts} +1 -1
- package/dist/projectionStaleness.d.ts +1 -1
- package/dist/{questionEvidenceLinks-BdQD0TkM.d.ts → questionEvidenceLinks-DFlyPpAj.d.ts} +1 -1
- package/dist/questionEvidenceLinks.d.ts +1 -1
- package/dist/questionEvidenceLinks.js +199 -881
- package/dist/questionEvidenceLinks.js.map +1 -1
- package/dist/resolvers.js +86 -37
- package/dist/resolvers.js.map +1 -1
- package/dist/scopeResolverCompat.js +64 -7
- package/dist/scopeResolverCompat.js.map +1 -1
- package/dist/{text-matching-CMn2WnVD.d.ts → text-matching-DNg4M5Wd.d.ts} +1 -1
- package/dist/text-matching.d.ts +1 -1
- package/dist/text-matching.js +1 -244
- package/dist/text-matching.js.map +1 -1
- package/dist/topicProjectOverlay.js +56 -13
- package/dist/topicProjectOverlay.js.map +1 -1
- package/dist/topicScope.js +55 -6
- package/dist/topicScope.js.map +1 -1
- package/dist/workflowBridge.d.ts +27 -0
- package/dist/workflowBridge.js +352 -0
- package/dist/workflowBridge.js.map +1 -0
- package/dist/workspaceIsolation.js +56 -57
- package/dist/workspaceIsolation.js.map +1 -1
- package/package.json +6 -5
package/dist/beliefDecay.js
CHANGED
|
@@ -1,355 +1,28 @@
|
|
|
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
|
|
9
|
+
var api = anyApi;
|
|
10
|
+
componentsGeneric();
|
|
11
|
+
var query = queryGeneric;
|
|
5
12
|
|
|
6
|
-
//
|
|
7
|
-
function
|
|
8
|
-
|
|
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";
|
|
13
|
+
// src/debug.ts
|
|
14
|
+
function isGraphPrimitiveDebugEnabled() {
|
|
15
|
+
const env = globalThis.process?.env;
|
|
16
|
+
return env?.LUCERN_COMPAT_FALLBACK_DEBUG === "1" || env?.LUCERN_GRAPH_DEBUG === "1";
|
|
207
17
|
}
|
|
208
|
-
function
|
|
209
|
-
if (
|
|
210
|
-
return
|
|
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}`;
|
|
18
|
+
function debugGraphPrimitiveFallback(message, context) {
|
|
19
|
+
if (!isGraphPrimitiveDebugEnabled()) {
|
|
20
|
+
return;
|
|
339
21
|
}
|
|
340
|
-
|
|
341
|
-
nextRescoreBy: decayState.rescoreByDate,
|
|
342
|
-
daysUntilRescore: Math.round(daysUntilRescore),
|
|
343
|
-
isOverdue,
|
|
344
|
-
priority,
|
|
345
|
-
reason,
|
|
346
|
-
decay: decayState
|
|
347
|
-
};
|
|
22
|
+
console.debug(message, context ?? {});
|
|
348
23
|
}
|
|
349
|
-
var api = anyApi;
|
|
350
|
-
componentsGeneric();
|
|
351
24
|
|
|
352
|
-
//
|
|
25
|
+
// src/topicProjectOverlay.ts
|
|
353
26
|
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
354
27
|
function readNonEmptyString(value) {
|
|
355
28
|
if (typeof value !== "string") {
|
|
@@ -393,14 +66,38 @@ function isProjectLikeTopic(topic) {
|
|
|
393
66
|
const metadata = readMetadata(topic);
|
|
394
67
|
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
395
68
|
}
|
|
69
|
+
function isMissingLucernChildComponentError(error) {
|
|
70
|
+
const message = getErrorMessage(error);
|
|
71
|
+
return message.includes(
|
|
72
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
73
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
74
|
+
}
|
|
75
|
+
function getErrorMessage(error) {
|
|
76
|
+
if (error instanceof Error) {
|
|
77
|
+
return error.message;
|
|
78
|
+
}
|
|
79
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
80
|
+
return error.message;
|
|
81
|
+
}
|
|
82
|
+
return "unknown error";
|
|
83
|
+
}
|
|
396
84
|
async function resolveTopicDoc(ctx, scopeId) {
|
|
397
85
|
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
398
86
|
try {
|
|
399
|
-
const directTopic = await ctx.db.get(
|
|
87
|
+
const directTopic = await ctx.db.get(
|
|
88
|
+
scopeId
|
|
89
|
+
);
|
|
400
90
|
if (directTopic) {
|
|
401
91
|
return directTopic;
|
|
402
92
|
}
|
|
403
|
-
} catch {
|
|
93
|
+
} catch (error) {
|
|
94
|
+
debugGraphPrimitiveFallback(
|
|
95
|
+
"[topicProjectOverlay] Failed to resolve topic by direct ID",
|
|
96
|
+
{
|
|
97
|
+
error,
|
|
98
|
+
scopeId
|
|
99
|
+
}
|
|
100
|
+
);
|
|
404
101
|
}
|
|
405
102
|
}
|
|
406
103
|
if (typeof ctx.runQuery !== "function") {
|
|
@@ -413,7 +110,14 @@ async function resolveTopicDoc(ctx, scopeId) {
|
|
|
413
110
|
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
414
111
|
return topic;
|
|
415
112
|
}
|
|
416
|
-
} catch {
|
|
113
|
+
} catch (error) {
|
|
114
|
+
debugGraphPrimitiveFallback(
|
|
115
|
+
"[topicProjectOverlay] Failed to resolve topic by ID query",
|
|
116
|
+
{
|
|
117
|
+
error,
|
|
118
|
+
scopeId
|
|
119
|
+
}
|
|
120
|
+
);
|
|
417
121
|
}
|
|
418
122
|
try {
|
|
419
123
|
const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
@@ -422,7 +126,11 @@ async function resolveTopicDoc(ctx, scopeId) {
|
|
|
422
126
|
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
423
127
|
return topic;
|
|
424
128
|
}
|
|
425
|
-
} catch {
|
|
129
|
+
} catch (error) {
|
|
130
|
+
debugGraphPrimitiveFallback(
|
|
131
|
+
"[topicProjectOverlay] Failed to resolve topic by legacy scope ID",
|
|
132
|
+
{ error, scopeId }
|
|
133
|
+
);
|
|
426
134
|
}
|
|
427
135
|
return null;
|
|
428
136
|
}
|
|
@@ -476,7 +184,11 @@ async function listTopicProjectOverlays(ctx, options = {}) {
|
|
|
476
184
|
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
477
185
|
try {
|
|
478
186
|
allTopics = await ctx.db.query("topics").collect();
|
|
479
|
-
} catch {
|
|
187
|
+
} catch (error) {
|
|
188
|
+
debugGraphPrimitiveFallback(
|
|
189
|
+
"[topicProjectOverlay] Failed to read topics table; falling back to API",
|
|
190
|
+
{ error }
|
|
191
|
+
);
|
|
480
192
|
allTopics = [];
|
|
481
193
|
}
|
|
482
194
|
}
|
|
@@ -487,746 +199,12 @@ async function listTopicProjectOverlays(ctx, options = {}) {
|
|
|
487
199
|
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
488
200
|
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
489
201
|
}
|
|
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
202
|
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
1225
|
-
const topic = await
|
|
203
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
1226
204
|
if (!topic) {
|
|
1227
205
|
return null;
|
|
1228
206
|
}
|
|
1229
|
-
const nextMetadata = { ...
|
|
207
|
+
const nextMetadata = { ...readMetadata(topic) };
|
|
1230
208
|
const patch = {};
|
|
1231
209
|
const topicUpdateArgs = {
|
|
1232
210
|
id: String(topic._id)
|
|
@@ -1251,7 +229,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1251
229
|
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
1252
230
|
);
|
|
1253
231
|
case "status": {
|
|
1254
|
-
const status =
|
|
232
|
+
const status = coerceStatus(rawValue);
|
|
1255
233
|
if (status) {
|
|
1256
234
|
patch.status = status;
|
|
1257
235
|
topicUpdateArgs.status = status;
|
|
@@ -1259,7 +237,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1259
237
|
break;
|
|
1260
238
|
}
|
|
1261
239
|
case "visibility": {
|
|
1262
|
-
const visibility =
|
|
240
|
+
const visibility = coerceVisibility(rawValue);
|
|
1263
241
|
if (visibility) {
|
|
1264
242
|
patch.visibility = visibility;
|
|
1265
243
|
topicUpdateArgs.visibility = visibility;
|
|
@@ -1267,7 +245,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1267
245
|
break;
|
|
1268
246
|
}
|
|
1269
247
|
case "type": {
|
|
1270
|
-
const projectType =
|
|
248
|
+
const projectType = readNonEmptyString(rawValue);
|
|
1271
249
|
if (projectType) {
|
|
1272
250
|
nextMetadata.projectType = projectType;
|
|
1273
251
|
} else {
|
|
@@ -1291,7 +269,7 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1291
269
|
topicUpdateArgs.metadata = nextMetadata;
|
|
1292
270
|
if (typeof ctx.runMutation === "function") {
|
|
1293
271
|
try {
|
|
1294
|
-
await ctx.runMutation(
|
|
272
|
+
await ctx.runMutation(api.topics.update, topicUpdateArgs);
|
|
1295
273
|
} catch (error) {
|
|
1296
274
|
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
1297
275
|
throw error;
|
|
@@ -1305,19 +283,28 @@ async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
|
1305
283
|
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
1306
284
|
);
|
|
1307
285
|
}
|
|
1308
|
-
return
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
}
|
|
1314
|
-
);
|
|
286
|
+
return materializeTopicProjectOverlay({
|
|
287
|
+
...topic,
|
|
288
|
+
...patch,
|
|
289
|
+
metadata: nextMetadata
|
|
290
|
+
});
|
|
1315
291
|
}
|
|
1316
292
|
|
|
1317
293
|
// src/resolvers.ts
|
|
1318
294
|
function isMissingLucernChildComponentError2(error) {
|
|
1319
|
-
const message =
|
|
1320
|
-
return message.includes(
|
|
295
|
+
const message = getErrorMessage2(error);
|
|
296
|
+
return message.includes(
|
|
297
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
298
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
299
|
+
}
|
|
300
|
+
function getErrorMessage2(error) {
|
|
301
|
+
if (error instanceof Error) {
|
|
302
|
+
return error.message;
|
|
303
|
+
}
|
|
304
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
305
|
+
return error.message;
|
|
306
|
+
}
|
|
307
|
+
return "unknown error";
|
|
1321
308
|
}
|
|
1322
309
|
function isAdvisoryTopicPatch(value) {
|
|
1323
310
|
const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
|
|
@@ -1331,52 +318,47 @@ async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
|
1331
318
|
if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
|
|
1332
319
|
throw error;
|
|
1333
320
|
}
|
|
1334
|
-
console.warn(
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
321
|
+
console.warn(
|
|
322
|
+
"[lucern graph-primitives] Non-fatal advisory topic patch failure",
|
|
323
|
+
{
|
|
324
|
+
projectId,
|
|
325
|
+
keys: Object.keys(value),
|
|
326
|
+
error: getErrorMessage2(error)
|
|
327
|
+
}
|
|
328
|
+
);
|
|
1339
329
|
}
|
|
1340
330
|
}
|
|
1341
|
-
function
|
|
331
|
+
function defaultResolvers() {
|
|
1342
332
|
return {
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
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
|
-
}
|
|
333
|
+
getProject: (ctx, projectId) => resolveTopicProjectOverlay(ctx, projectId, {
|
|
334
|
+
idMode: "legacy",
|
|
335
|
+
projectLikeOnly: false
|
|
336
|
+
}),
|
|
337
|
+
patchProject: (ctx, projectId, value) => patchProjectWithTolerance(ctx, projectId, value),
|
|
338
|
+
listTopics: (ctx) => listTopicProjectOverlays(ctx, {
|
|
339
|
+
idMode: "legacy"
|
|
340
|
+
}),
|
|
341
|
+
getFinalArtifact: (ctx, artifactId) => ctx.db.get(artifactId)
|
|
1360
342
|
};
|
|
1361
343
|
}
|
|
1362
|
-
var
|
|
344
|
+
var resolverOverrides = {};
|
|
1363
345
|
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
1364
346
|
return {
|
|
1365
|
-
...
|
|
1366
|
-
...
|
|
347
|
+
...defaultResolvers(),
|
|
348
|
+
...resolverOverrides
|
|
1367
349
|
};
|
|
1368
350
|
}
|
|
1369
|
-
var
|
|
351
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
1370
352
|
function asMappedProjectId(topic) {
|
|
1371
353
|
if (!topic) {
|
|
1372
354
|
return;
|
|
1373
355
|
}
|
|
1374
|
-
const directLegacyProjectId = normalizeScopeValue(topic[
|
|
356
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD2]);
|
|
1375
357
|
if (directLegacyProjectId) {
|
|
1376
358
|
return directLegacyProjectId;
|
|
1377
359
|
}
|
|
1378
360
|
const metadata = topic.metadata || {};
|
|
1379
|
-
const candidate = metadata[
|
|
361
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD2] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
1380
362
|
return candidate ? candidate : void 0;
|
|
1381
363
|
}
|
|
1382
364
|
function normalizeScopeValue(value) {
|
|
@@ -1405,9 +387,16 @@ async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
|
1405
387
|
try {
|
|
1406
388
|
return await ctx.db.query("topics").withIndex(
|
|
1407
389
|
"by_graph_scope_project",
|
|
1408
|
-
(q) => q.eq(
|
|
390
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
|
|
1409
391
|
).collect();
|
|
1410
|
-
} catch {
|
|
392
|
+
} catch (error) {
|
|
393
|
+
debugGraphPrimitiveFallback(
|
|
394
|
+
"[topicScope] Failed to resolve scope alias via index",
|
|
395
|
+
{
|
|
396
|
+
error,
|
|
397
|
+
scopeId
|
|
398
|
+
}
|
|
399
|
+
);
|
|
1411
400
|
const topics = await ctx.db.query("topics").collect();
|
|
1412
401
|
return topics.filter((topic) => {
|
|
1413
402
|
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
@@ -1421,10 +410,17 @@ async function tryResolveHostTopicById(ctx, topicId) {
|
|
|
1421
410
|
return null;
|
|
1422
411
|
}
|
|
1423
412
|
try {
|
|
1424
|
-
return await ctx.runQuery(
|
|
413
|
+
return await ctx.runQuery(api.topics.get, {
|
|
1425
414
|
id: topicId
|
|
1426
415
|
}) ?? null;
|
|
1427
|
-
} catch {
|
|
416
|
+
} catch (error) {
|
|
417
|
+
debugGraphPrimitiveFallback(
|
|
418
|
+
"[topicScope] Failed to resolve topic by host query",
|
|
419
|
+
{
|
|
420
|
+
error,
|
|
421
|
+
topicId
|
|
422
|
+
}
|
|
423
|
+
);
|
|
1428
424
|
return null;
|
|
1429
425
|
}
|
|
1430
426
|
}
|
|
@@ -1433,10 +429,17 @@ async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
|
1433
429
|
return null;
|
|
1434
430
|
}
|
|
1435
431
|
try {
|
|
1436
|
-
return await ctx.runQuery(
|
|
432
|
+
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
1437
433
|
projectId: legacyScopeId
|
|
1438
434
|
}) ?? null;
|
|
1439
|
-
} catch {
|
|
435
|
+
} catch (error) {
|
|
436
|
+
debugGraphPrimitiveFallback(
|
|
437
|
+
"[topicScope] Failed to resolve topic by legacy scope",
|
|
438
|
+
{
|
|
439
|
+
error,
|
|
440
|
+
legacyScopeId
|
|
441
|
+
}
|
|
442
|
+
);
|
|
1440
443
|
return null;
|
|
1441
444
|
}
|
|
1442
445
|
}
|
|
@@ -1465,8 +468,17 @@ async function resolveTopicProjectScope(ctx, args) {
|
|
|
1465
468
|
if (args.topicId) {
|
|
1466
469
|
let topic = null;
|
|
1467
470
|
try {
|
|
1468
|
-
topic = await ctx.db.get(
|
|
1469
|
-
|
|
471
|
+
topic = await ctx.db.get(
|
|
472
|
+
args.topicId
|
|
473
|
+
);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
debugGraphPrimitiveFallback(
|
|
476
|
+
"[topicScope] Failed to load topic by direct id",
|
|
477
|
+
{
|
|
478
|
+
error,
|
|
479
|
+
topicId: args.topicId
|
|
480
|
+
}
|
|
481
|
+
);
|
|
1470
482
|
}
|
|
1471
483
|
if (!topic) {
|
|
1472
484
|
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
@@ -1503,7 +515,14 @@ async function resolveTopicProjectScope(ctx, args) {
|
|
|
1503
515
|
directTopic = await ctx.db.get(
|
|
1504
516
|
args.projectId
|
|
1505
517
|
);
|
|
1506
|
-
} catch {
|
|
518
|
+
} catch (error) {
|
|
519
|
+
debugGraphPrimitiveFallback(
|
|
520
|
+
"[topicScope] Failed to load direct project topic",
|
|
521
|
+
{
|
|
522
|
+
error,
|
|
523
|
+
projectId: args.projectId
|
|
524
|
+
}
|
|
525
|
+
);
|
|
1507
526
|
}
|
|
1508
527
|
if (directTopic) {
|
|
1509
528
|
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
@@ -1571,7 +590,20 @@ var identifyBeliefsNeedingRescore = query({
|
|
|
1571
590
|
returns: permissiveReturn,
|
|
1572
591
|
handler: async (ctx, args) => {
|
|
1573
592
|
const { maxResults = 20, minPriority = "medium" } = args;
|
|
1574
|
-
|
|
593
|
+
let scope;
|
|
594
|
+
try {
|
|
595
|
+
scope = await resolveTopicProjectScope(ctx, args);
|
|
596
|
+
} catch (error) {
|
|
597
|
+
debugGraphPrimitiveFallback(
|
|
598
|
+
"[beliefDecay] Failed to resolve scope for identify beliefs",
|
|
599
|
+
{
|
|
600
|
+
error,
|
|
601
|
+
projectId: args.projectId,
|
|
602
|
+
topicId: args.topicId
|
|
603
|
+
}
|
|
604
|
+
);
|
|
605
|
+
scope = null;
|
|
606
|
+
}
|
|
1575
607
|
if (!scope) {
|
|
1576
608
|
return [];
|
|
1577
609
|
}
|
|
@@ -1670,13 +702,25 @@ var getGlobalBeliefHealth = query({
|
|
|
1670
702
|
const allProjects = await resolveGraphPrimitivesAppResolvers().listTopics(ctx);
|
|
1671
703
|
const accessibleProjectIds = await getAccessibleProjectIds(ctx, clerkId);
|
|
1672
704
|
const accessibleProjects = allProjects.filter(
|
|
1673
|
-
(
|
|
705
|
+
(project) => accessibleProjectIds.has(project._id)
|
|
1674
706
|
);
|
|
1675
707
|
const allResults = [];
|
|
1676
|
-
for (const
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
708
|
+
for (const project of accessibleProjects.slice(0, 20)) {
|
|
709
|
+
let scope;
|
|
710
|
+
try {
|
|
711
|
+
scope = await resolveTopicProjectScope(ctx, {
|
|
712
|
+
projectId: String(project._id)
|
|
713
|
+
});
|
|
714
|
+
} catch (error) {
|
|
715
|
+
debugGraphPrimitiveFallback(
|
|
716
|
+
"[beliefDecay] Failed to resolve project scope in health query",
|
|
717
|
+
{
|
|
718
|
+
error,
|
|
719
|
+
projectId: String(project._id)
|
|
720
|
+
}
|
|
721
|
+
);
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
1680
724
|
if (!scope) {
|
|
1681
725
|
continue;
|
|
1682
726
|
}
|
|
@@ -1701,8 +745,8 @@ var getGlobalBeliefHealth = query({
|
|
|
1701
745
|
});
|
|
1702
746
|
if (priorityRank[schedule.priority] >= minRank) {
|
|
1703
747
|
allResults.push({
|
|
1704
|
-
projectId:
|
|
1705
|
-
projectName:
|
|
748
|
+
projectId: project._id,
|
|
749
|
+
projectName: project.name,
|
|
1706
750
|
beliefId: belief._id,
|
|
1707
751
|
beliefText: belief.canonicalText,
|
|
1708
752
|
confidence,
|
|
@@ -1804,6 +848,6 @@ var getBeliefDecayInfo = query({
|
|
|
1804
848
|
}
|
|
1805
849
|
});
|
|
1806
850
|
|
|
1807
|
-
export {
|
|
851
|
+
export { getBeliefDecayInfo, getGlobalBeliefHealth, identifyBeliefsNeedingRescore };
|
|
1808
852
|
//# sourceMappingURL=beliefDecay.js.map
|
|
1809
853
|
//# sourceMappingURL=beliefDecay.js.map
|