@lucern/graph-primitives 0.3.0-alpha.9 → 1.0.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/README.md +13 -12
- package/dist/beliefDecay.js +24 -17
- package/dist/beliefDecay.js.map +1 -1
- package/dist/beliefEvidenceLinks.js +32 -8
- package/dist/beliefEvidenceLinks.js.map +1 -1
- package/dist/confidencePropagationDispatch.js.map +1 -1
- package/dist/contradictions.js +32 -9
- package/dist/contradictions.js.map +1 -1
- package/dist/convex.d.ts +55 -12
- package/dist/convex.js.map +1 -1
- package/dist/edgeValidation.d.ts +25 -2
- package/dist/edges/index.d.ts +9 -2
- package/dist/edges/index.js.map +1 -1
- package/dist/edges/propagationTypes.d.ts +2 -3
- package/dist/edges/propagationTypes.js.map +1 -1
- package/dist/entityBridge.js +10 -3
- package/dist/entityBridge.js.map +1 -1
- package/dist/entityLifecycle.js +15 -3
- package/dist/entityLifecycle.js.map +1 -1
- package/dist/epistemicAnswers.js.map +1 -1
- package/dist/epistemicBeliefs.admin.d.ts +36 -0
- package/dist/epistemicBeliefs.admin.js +745 -0
- package/dist/epistemicBeliefs.admin.js.map +1 -0
- package/dist/epistemicBeliefs.backfills.d.ts +62 -0
- package/dist/epistemicBeliefs.backfills.js +1004 -0
- package/dist/epistemicBeliefs.backfills.js.map +1 -0
- package/dist/epistemicBeliefs.confidence.d.ts +45 -0
- package/dist/epistemicBeliefs.confidence.js +1285 -0
- package/dist/epistemicBeliefs.confidence.js.map +1 -0
- package/dist/epistemicBeliefs.core.d.ts +35 -0
- package/dist/epistemicBeliefs.core.js +1508 -0
- package/dist/epistemicBeliefs.core.js.map +1 -0
- package/dist/epistemicBeliefs.d.ts +12 -3
- package/dist/epistemicBeliefs.helpers.d.ts +168 -0
- package/dist/epistemicBeliefs.helpers.js +1060 -0
- package/dist/epistemicBeliefs.helpers.js.map +1 -0
- package/dist/epistemicBeliefs.internal.d.ts +30 -0
- package/dist/epistemicBeliefs.internal.js +1329 -0
- package/dist/epistemicBeliefs.internal.js.map +1 -0
- package/dist/epistemicBeliefs.js +1196 -1184
- package/dist/epistemicBeliefs.js.map +1 -1
- package/dist/epistemicBeliefs.lifecycle.d.ts +19 -0
- package/dist/epistemicBeliefs.lifecycle.js +1608 -0
- package/dist/epistemicBeliefs.lifecycle.js.map +1 -0
- package/dist/epistemicBeliefs.links.d.ts +30 -0
- package/dist/epistemicBeliefs.links.js +761 -0
- package/dist/epistemicBeliefs.links.js.map +1 -0
- package/dist/epistemicBeliefs.queries.d.ts +16 -0
- package/dist/epistemicBeliefs.queries.js +90 -0
- package/dist/epistemicBeliefs.queries.js.map +1 -0
- package/dist/epistemicContractHelpers.d.ts +1 -1
- package/dist/epistemicContractHelpers.js +1 -1
- package/dist/epistemicContracts.d.ts +5 -76
- package/dist/epistemicContracts.evaluators.d.ts +36 -0
- package/dist/epistemicContracts.evaluators.js +2506 -0
- package/dist/epistemicContracts.evaluators.js.map +1 -0
- package/dist/epistemicContracts.handlers.d.ts +40 -0
- package/dist/epistemicContracts.handlers.js +3029 -0
- package/dist/epistemicContracts.handlers.js.map +1 -0
- package/dist/epistemicContracts.js +2006 -5281
- package/dist/epistemicContracts.js.map +1 -1
- package/dist/epistemicContracts.metrics.d.ts +26 -0
- package/dist/epistemicContracts.metrics.js +427 -0
- package/dist/epistemicContracts.metrics.js.map +1 -0
- package/dist/epistemicContracts.types.d.ts +159 -0
- package/dist/epistemicContracts.types.js +3 -0
- package/dist/epistemicContracts.types.js.map +1 -0
- package/dist/epistemicEdgeCreation.d.ts +73 -0
- package/dist/epistemicEdgeCreation.js +450 -0
- package/dist/epistemicEdgeCreation.js.map +1 -0
- package/dist/epistemicEdges-BF-cn4i3.d.ts +43 -0
- package/dist/epistemicEdges.d.ts +8 -1
- package/dist/epistemicEdges.handlers.d.ts +20 -0
- package/dist/epistemicEdges.handlers.js +289 -0
- package/dist/epistemicEdges.handlers.js.map +1 -0
- package/dist/epistemicEdges.helpers.d.ts +27 -0
- package/dist/epistemicEdges.helpers.js +162 -0
- package/dist/epistemicEdges.helpers.js.map +1 -0
- package/dist/epistemicEdges.js +797 -875
- package/dist/epistemicEdges.js.map +1 -1
- package/dist/epistemicEdges.mutations.d.ts +39 -0
- package/dist/epistemicEdges.mutations.js +1365 -0
- package/dist/epistemicEdges.mutations.js.map +1 -0
- package/dist/epistemicEdges.queries.d.ts +95 -0
- package/dist/epistemicEdges.queries.js +851 -0
- package/dist/epistemicEdges.queries.js.map +1 -0
- package/dist/epistemicEdges.types.d.ts +32 -0
- package/dist/epistemicEdges.types.js +3 -0
- package/dist/epistemicEdges.types.js.map +1 -0
- package/dist/epistemicEvidence-DvfchNt7.d.ts +46 -0
- package/dist/epistemicEvidence.d.ts +5 -2
- package/dist/epistemicEvidence.js +801 -807
- package/dist/epistemicEvidence.js.map +1 -1
- package/dist/epistemicEvidenceHelpers.d.ts +71 -0
- package/dist/epistemicEvidenceHelpers.js +769 -0
- package/dist/epistemicEvidenceHelpers.js.map +1 -0
- package/dist/epistemicEvidenceMutations.d.ts +10 -0
- package/dist/epistemicEvidenceMutations.js +1421 -0
- package/dist/epistemicEvidenceMutations.js.map +1 -0
- package/dist/epistemicEvidenceQueries.d.ts +10 -0
- package/dist/epistemicEvidenceQueries.js +1049 -0
- package/dist/epistemicEvidenceQueries.js.map +1 -0
- package/dist/epistemicHelpers.d.ts +4 -2
- package/dist/epistemicHelpers.js +132 -127
- package/dist/epistemicHelpers.js.map +1 -1
- package/dist/epistemicLayerRules.d.ts +138 -0
- package/dist/epistemicLayerRules.js +481 -0
- package/dist/epistemicLayerRules.js.map +1 -0
- package/dist/epistemicLinking.js +1 -1
- package/dist/epistemicLinking.js.map +1 -1
- package/dist/epistemicNodeCreation.d.ts +101 -0
- package/dist/epistemicNodeCreation.js +709 -0
- package/dist/epistemicNodeCreation.js.map +1 -0
- package/dist/epistemicNodes-BCQxpYx_.d.ts +54 -0
- package/dist/epistemicNodes.d.ts +5 -1
- package/dist/epistemicNodes.helpers.d.ts +51 -0
- package/dist/epistemicNodes.helpers.js +73 -0
- package/dist/epistemicNodes.helpers.js.map +1 -0
- package/dist/epistemicNodes.internal.d.ts +34 -0
- package/dist/epistemicNodes.internal.js +658 -0
- package/dist/epistemicNodes.internal.js.map +1 -0
- package/dist/epistemicNodes.js +698 -693
- package/dist/epistemicNodes.js.map +1 -1
- package/dist/epistemicNodes.mutations.d.ts +34 -0
- package/dist/epistemicNodes.mutations.js +1153 -0
- package/dist/epistemicNodes.mutations.js.map +1 -0
- package/dist/epistemicNodes.queries.d.ts +36 -0
- package/dist/epistemicNodes.queries.js +619 -0
- package/dist/epistemicNodes.queries.js.map +1 -0
- package/dist/epistemicNodes.validators.d.ts +23 -0
- package/dist/epistemicNodes.validators.js +105 -0
- package/dist/epistemicNodes.validators.js.map +1 -0
- package/dist/epistemicQuestions-bwHd2FWE.d.ts +68 -0
- package/dist/epistemicQuestions.conviction.d.ts +52 -0
- package/dist/epistemicQuestions.conviction.js +1389 -0
- package/dist/epistemicQuestions.conviction.js.map +1 -0
- package/dist/epistemicQuestions.create.d.ts +29 -0
- package/dist/epistemicQuestions.create.js +1300 -0
- package/dist/epistemicQuestions.create.js.map +1 -0
- package/dist/epistemicQuestions.d.ts +10 -2
- package/dist/epistemicQuestions.evidence.d.ts +22 -0
- package/dist/epistemicQuestions.evidence.js +929 -0
- package/dist/epistemicQuestions.evidence.js.map +1 -0
- package/dist/epistemicQuestions.helpers.d.ts +69 -0
- package/dist/epistemicQuestions.helpers.js +824 -0
- package/dist/epistemicQuestions.helpers.js.map +1 -0
- package/dist/epistemicQuestions.js +2435 -2430
- package/dist/epistemicQuestions.js.map +1 -1
- package/dist/epistemicQuestions.lifecycle.d.ts +24 -0
- package/dist/epistemicQuestions.lifecycle.js +838 -0
- package/dist/epistemicQuestions.lifecycle.js.map +1 -0
- package/dist/epistemicQuestions.queries.d.ts +41 -0
- package/dist/epistemicQuestions.queries.js +1013 -0
- package/dist/epistemicQuestions.queries.js.map +1 -0
- package/dist/epistemicQuestions.sprint.d.ts +22 -0
- package/dist/epistemicQuestions.sprint.js +757 -0
- package/dist/epistemicQuestions.sprint.js.map +1 -0
- package/dist/epistemicQuestions.tail.d.ts +42 -0
- package/dist/epistemicQuestions.tail.js +1345 -0
- package/dist/epistemicQuestions.tail.js.map +1 -0
- package/dist/epistemicSources.js +6 -2
- package/dist/epistemicSources.js.map +1 -1
- package/dist/evaluators/index.d.ts +2 -2
- package/dist/evaluators/index.js +45 -5320
- package/dist/evaluators/index.js.map +1 -1
- package/dist/evaluators/lintCheckerEvaluator.d.ts +1 -1
- package/dist/evaluators/sentryCheckerEvaluator.d.ts +1 -1
- package/dist/evaluators/testRunnerEvaluator.d.ts +1 -1
- package/dist/evaluators/tscCheckerEvaluator.d.ts +1 -1
- package/dist/{graphTypes-CpgIuCdo.d.ts → graphTypes-B8VaIjnl.d.ts} +1 -1
- package/dist/graphTypes.d.ts +1 -1
- package/dist/{helpers-BYHIk5vU.d.ts → helpers-DNYfg6mo.d.ts} +2 -3
- package/dist/helpers.d.ts +2 -2
- package/dist/helpers.js.map +1 -1
- package/dist/{index-Dq-7R-gi.d.ts → index-C-Kyd7hD.d.ts} +1 -1
- package/dist/index.d.ts +160 -14
- package/dist/index.js +12291 -13001
- package/dist/index.js.map +1 -1
- package/dist/logicalRoleInference.js.map +1 -1
- package/dist/ontologyApproval.js +1 -1
- package/dist/ontologyApproval.js.map +1 -1
- package/dist/ontologyDefinitions.js +25 -7
- package/dist/ontologyDefinitions.js.map +1 -1
- package/dist/ontologyRegistry.js.map +1 -1
- package/dist/projectionReconciliation.js.map +1 -1
- package/dist/questionEvidenceLinks.js +28 -7
- package/dist/questionEvidenceLinks.js.map +1 -1
- package/dist/resolvers.js.map +1 -1
- package/dist/scopeResolverCompat.js.map +1 -1
- package/dist/topicProjectOverlay.js.map +1 -1
- package/dist/topicScope.js.map +1 -1
- package/dist/workflowBridge.js.map +1 -1
- package/dist/workspaceIsolation.js.map +1 -1
- package/package.json +4 -5
- package/dist/edgeValidation-CeI0wc0r.d.ts +0 -35
- package/dist/epistemicBeliefs-DzKjZAeC.d.ts +0 -377
- package/dist/epistemicEdges-CvlKnEyy.d.ts +0 -191
- package/dist/epistemicEvidence-xw6UUrwh.d.ts +0 -128
- package/dist/epistemicHelpers-DevrYgPN.d.ts +0 -329
- package/dist/epistemicNodes-DjSUfvyD.d.ts +0 -167
- package/dist/epistemicQuestions-B_nUclrH.d.ts +0 -214
- package/dist/index-Dct1T70K.d.ts +0 -25
|
@@ -0,0 +1,929 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
|
|
3
|
+
import { componentsGeneric, anyApi, internalMutationGeneric } from 'convex/server';
|
|
4
|
+
|
|
5
|
+
// src/epistemicQuestions.evidence.ts
|
|
6
|
+
var api = anyApi;
|
|
7
|
+
componentsGeneric();
|
|
8
|
+
var internal = anyApi;
|
|
9
|
+
var internalMutation = internalMutationGeneric;
|
|
10
|
+
|
|
11
|
+
// src/debug.ts
|
|
12
|
+
function isGraphPrimitiveDebugEnabled() {
|
|
13
|
+
const env = globalThis.process?.env;
|
|
14
|
+
return env?.LUCERN_COMPAT_FALLBACK_DEBUG === "1" || env?.LUCERN_GRAPH_DEBUG === "1";
|
|
15
|
+
}
|
|
16
|
+
function formatGraphPrimitiveError(error) {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return `${error.name}: ${error.message}`;
|
|
19
|
+
}
|
|
20
|
+
if (typeof error === "string") {
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
if (error === null) {
|
|
24
|
+
return "null";
|
|
25
|
+
}
|
|
26
|
+
if (error === void 0) {
|
|
27
|
+
return "undefined";
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
return JSON.stringify(error);
|
|
31
|
+
} catch {
|
|
32
|
+
return Object.prototype.toString.call(error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function debugGraphPrimitiveFallback(message, context) {
|
|
36
|
+
if (!isGraphPrimitiveDebugEnabled()) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
console.debug(message, context ?? {});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/globalId.ts
|
|
43
|
+
function generateGlobalId() {
|
|
44
|
+
const bytes = new Uint8Array(16);
|
|
45
|
+
crypto.getRandomValues(bytes);
|
|
46
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
47
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
48
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
49
|
+
""
|
|
50
|
+
);
|
|
51
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
52
|
+
}
|
|
53
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
54
|
+
function asMappedProjectId(topic) {
|
|
55
|
+
if (!topic) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD]);
|
|
59
|
+
if (directLegacyProjectId) {
|
|
60
|
+
return directLegacyProjectId;
|
|
61
|
+
}
|
|
62
|
+
const metadata = topic.metadata || {};
|
|
63
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
64
|
+
return candidate ? candidate : void 0;
|
|
65
|
+
}
|
|
66
|
+
function normalizeScopeValue(value) {
|
|
67
|
+
if (typeof value !== "string") {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const normalized = value.trim();
|
|
71
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
72
|
+
}
|
|
73
|
+
function pickPrimaryTopic(candidates) {
|
|
74
|
+
return [...candidates].sort((a, b) => {
|
|
75
|
+
const depthA = a.depth ?? 9999;
|
|
76
|
+
const depthB = b.depth ?? 9999;
|
|
77
|
+
if (depthA !== depthB) {
|
|
78
|
+
return depthA - depthB;
|
|
79
|
+
}
|
|
80
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
81
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
82
|
+
if (createdA !== createdB) {
|
|
83
|
+
return createdA - createdB;
|
|
84
|
+
}
|
|
85
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
86
|
+
})[0];
|
|
87
|
+
}
|
|
88
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
89
|
+
try {
|
|
90
|
+
return await ctx.db.query("topics").withIndex(
|
|
91
|
+
"by_graph_scope_project",
|
|
92
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
|
|
93
|
+
).collect();
|
|
94
|
+
} catch (error) {
|
|
95
|
+
debugGraphPrimitiveFallback(
|
|
96
|
+
"[topicScope] Failed to resolve scope alias via index",
|
|
97
|
+
{
|
|
98
|
+
error,
|
|
99
|
+
scopeId
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
const topics = await ctx.db.query("topics").collect();
|
|
103
|
+
return topics.filter((topic) => {
|
|
104
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
105
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
106
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
111
|
+
if (typeof ctx.runQuery !== "function") {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return await ctx.runQuery(api.topics.get, {
|
|
116
|
+
id: topicId
|
|
117
|
+
}) ?? null;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
debugGraphPrimitiveFallback(
|
|
120
|
+
"[topicScope] Failed to resolve topic by host query",
|
|
121
|
+
{
|
|
122
|
+
error,
|
|
123
|
+
topicId
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
130
|
+
if (typeof ctx.runQuery !== "function") {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
135
|
+
projectId: legacyScopeId
|
|
136
|
+
}) ?? null;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
debugGraphPrimitiveFallback(
|
|
139
|
+
"[topicScope] Failed to resolve topic by legacy scope",
|
|
140
|
+
{
|
|
141
|
+
error,
|
|
142
|
+
legacyScopeId
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
149
|
+
const MAX_DEPTH = 10;
|
|
150
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
151
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
152
|
+
if (tenantId && workspaceId) {
|
|
153
|
+
return { tenantId, workspaceId };
|
|
154
|
+
}
|
|
155
|
+
let current = topic;
|
|
156
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
157
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
158
|
+
if (!current) break;
|
|
159
|
+
if (!tenantId) {
|
|
160
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
161
|
+
}
|
|
162
|
+
if (!workspaceId) {
|
|
163
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
164
|
+
}
|
|
165
|
+
if (tenantId && workspaceId) break;
|
|
166
|
+
}
|
|
167
|
+
return { tenantId, workspaceId };
|
|
168
|
+
}
|
|
169
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
170
|
+
if (args.topicId) {
|
|
171
|
+
let topic = null;
|
|
172
|
+
try {
|
|
173
|
+
topic = await ctx.db.get(
|
|
174
|
+
args.topicId
|
|
175
|
+
);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
debugGraphPrimitiveFallback(
|
|
178
|
+
"[topicScope] Failed to load topic by direct id",
|
|
179
|
+
{
|
|
180
|
+
error,
|
|
181
|
+
topicId: args.topicId
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (!topic) {
|
|
186
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
187
|
+
}
|
|
188
|
+
if (!topic) {
|
|
189
|
+
topic = pickPrimaryTopic(
|
|
190
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
191
|
+
) ?? null;
|
|
192
|
+
}
|
|
193
|
+
if (!topic) {
|
|
194
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
195
|
+
}
|
|
196
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
197
|
+
const mapped = asMappedProjectId(topic);
|
|
198
|
+
if (mapped) {
|
|
199
|
+
return {
|
|
200
|
+
topicId: topic._id,
|
|
201
|
+
projectId: mapped,
|
|
202
|
+
tenantId: inherited.tenantId,
|
|
203
|
+
workspaceId: inherited.workspaceId,
|
|
204
|
+
source: "topic"
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
topicId: topic._id,
|
|
209
|
+
tenantId: inherited.tenantId,
|
|
210
|
+
workspaceId: inherited.workspaceId,
|
|
211
|
+
source: "topic"
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (args.projectId) {
|
|
215
|
+
let directTopic = null;
|
|
216
|
+
try {
|
|
217
|
+
directTopic = await ctx.db.get(
|
|
218
|
+
args.projectId
|
|
219
|
+
);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
debugGraphPrimitiveFallback(
|
|
222
|
+
"[topicScope] Failed to load direct project topic",
|
|
223
|
+
{
|
|
224
|
+
error,
|
|
225
|
+
projectId: args.projectId
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
if (directTopic) {
|
|
230
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
231
|
+
const mapped = asMappedProjectId(directTopic);
|
|
232
|
+
return {
|
|
233
|
+
topicId: directTopic._id,
|
|
234
|
+
projectId: mapped ?? args.projectId,
|
|
235
|
+
tenantId: inherited.tenantId,
|
|
236
|
+
workspaceId: inherited.workspaceId,
|
|
237
|
+
source: "topic_inferred"
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
241
|
+
if (directTopic) {
|
|
242
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
243
|
+
const mapped = asMappedProjectId(directTopic);
|
|
244
|
+
return {
|
|
245
|
+
topicId: directTopic._id,
|
|
246
|
+
projectId: mapped ?? args.projectId,
|
|
247
|
+
tenantId: inherited.tenantId,
|
|
248
|
+
workspaceId: inherited.workspaceId,
|
|
249
|
+
source: "topic_inferred"
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
253
|
+
const primary = pickPrimaryTopic(topics);
|
|
254
|
+
if (primary) {
|
|
255
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
256
|
+
return {
|
|
257
|
+
topicId: primary._id,
|
|
258
|
+
projectId: args.projectId,
|
|
259
|
+
tenantId: inherited.tenantId,
|
|
260
|
+
workspaceId: inherited.workspaceId,
|
|
261
|
+
source: "project_mapped_topic"
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
throw new Error(
|
|
265
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
throw new Error(
|
|
269
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
var optionalScopeArgs = {
|
|
273
|
+
projectId: v.optional(v.string()),
|
|
274
|
+
topicId: v.optional(v.string())
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// src/topicProjectOverlay.ts
|
|
278
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
279
|
+
function readNonEmptyString(value) {
|
|
280
|
+
if (typeof value !== "string") {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const normalized = value.trim();
|
|
284
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
285
|
+
}
|
|
286
|
+
function readStringArray(value) {
|
|
287
|
+
if (!Array.isArray(value)) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
|
|
291
|
+
}
|
|
292
|
+
function readMetadata(topic) {
|
|
293
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
294
|
+
}
|
|
295
|
+
function readLegacyProjectId(value) {
|
|
296
|
+
if (!value) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
return readNonEmptyString(value[LEGACY_SCOPE_FIELD2]);
|
|
300
|
+
}
|
|
301
|
+
function coerceVisibility(value) {
|
|
302
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
303
|
+
}
|
|
304
|
+
function coerceStatus(value) {
|
|
305
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
306
|
+
}
|
|
307
|
+
function mapProjectType(topic, metadata) {
|
|
308
|
+
const explicit = readNonEmptyString(metadata.projectType);
|
|
309
|
+
if (explicit) {
|
|
310
|
+
return explicit;
|
|
311
|
+
}
|
|
312
|
+
if (topic.type === "theme") {
|
|
313
|
+
return "thematic";
|
|
314
|
+
}
|
|
315
|
+
return readNonEmptyString(topic.type) || "general";
|
|
316
|
+
}
|
|
317
|
+
function isProjectLikeTopic(topic) {
|
|
318
|
+
const metadata = readMetadata(topic);
|
|
319
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
320
|
+
}
|
|
321
|
+
function isMissingLucernChildComponentError(error) {
|
|
322
|
+
const message = getErrorMessage(error);
|
|
323
|
+
return message.includes(
|
|
324
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
325
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
326
|
+
}
|
|
327
|
+
function getErrorMessage(error) {
|
|
328
|
+
if (error instanceof Error) {
|
|
329
|
+
return error.message;
|
|
330
|
+
}
|
|
331
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
332
|
+
return error.message;
|
|
333
|
+
}
|
|
334
|
+
return "unknown error";
|
|
335
|
+
}
|
|
336
|
+
async function resolveTopicDoc(ctx, scopeId) {
|
|
337
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
338
|
+
try {
|
|
339
|
+
const directTopic = await ctx.db.get(
|
|
340
|
+
scopeId
|
|
341
|
+
);
|
|
342
|
+
if (directTopic) {
|
|
343
|
+
return directTopic;
|
|
344
|
+
}
|
|
345
|
+
} catch (error) {
|
|
346
|
+
debugGraphPrimitiveFallback(
|
|
347
|
+
"[topicProjectOverlay] Failed to resolve topic by direct ID",
|
|
348
|
+
{
|
|
349
|
+
error,
|
|
350
|
+
scopeId
|
|
351
|
+
}
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (typeof ctx.runQuery !== "function") {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
try {
|
|
359
|
+
const topic = await ctx.runQuery(api.topics.get, {
|
|
360
|
+
id: String(scopeId)
|
|
361
|
+
});
|
|
362
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
363
|
+
return topic;
|
|
364
|
+
}
|
|
365
|
+
} catch (error) {
|
|
366
|
+
debugGraphPrimitiveFallback(
|
|
367
|
+
"[topicProjectOverlay] Failed to resolve topic by ID query",
|
|
368
|
+
{
|
|
369
|
+
error,
|
|
370
|
+
scopeId
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
try {
|
|
375
|
+
const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
376
|
+
projectId: String(scopeId)
|
|
377
|
+
});
|
|
378
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
379
|
+
return topic;
|
|
380
|
+
}
|
|
381
|
+
} catch (error) {
|
|
382
|
+
debugGraphPrimitiveFallback(
|
|
383
|
+
"[topicProjectOverlay] Failed to resolve topic by legacy scope ID",
|
|
384
|
+
{ error, scopeId }
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
function materializeTopicProjectOverlay(topic, idMode = "legacy") {
|
|
390
|
+
const metadata = readMetadata(topic);
|
|
391
|
+
const topicId = String(topic._id);
|
|
392
|
+
const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
|
|
393
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
394
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
395
|
+
const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
|
|
396
|
+
const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
|
|
397
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
398
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
399
|
+
return {
|
|
400
|
+
...metadata,
|
|
401
|
+
_id: outwardId,
|
|
402
|
+
projectId: outwardId,
|
|
403
|
+
topicId,
|
|
404
|
+
storageProjectId,
|
|
405
|
+
legacyProjectId,
|
|
406
|
+
name: readNonEmptyString(topic.name) || "Untitled Theme",
|
|
407
|
+
type: mapProjectType(topic, metadata),
|
|
408
|
+
description: readNonEmptyString(topic.description),
|
|
409
|
+
ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
|
|
410
|
+
sharedWith: readStringArray(metadata.sharedWith),
|
|
411
|
+
visibility,
|
|
412
|
+
tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
|
|
413
|
+
workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
|
|
414
|
+
status,
|
|
415
|
+
tags: readStringArray(metadata.tags),
|
|
416
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
417
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
418
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
419
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
420
|
+
createdAt,
|
|
421
|
+
updatedAt
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
|
|
425
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
426
|
+
if (!topic) {
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
return materializeTopicProjectOverlay(topic, options.idMode);
|
|
433
|
+
}
|
|
434
|
+
async function listTopicProjectOverlays(ctx, options = {}) {
|
|
435
|
+
let allTopics = [];
|
|
436
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
437
|
+
try {
|
|
438
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
439
|
+
} catch (error) {
|
|
440
|
+
debugGraphPrimitiveFallback(
|
|
441
|
+
"[topicProjectOverlay] Failed to read topics table; falling back to API",
|
|
442
|
+
{ error }
|
|
443
|
+
);
|
|
444
|
+
allTopics = [];
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
448
|
+
allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
|
|
449
|
+
}
|
|
450
|
+
return allTopics.filter(
|
|
451
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
452
|
+
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
453
|
+
}
|
|
454
|
+
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
455
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
456
|
+
if (!topic) {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
const nextMetadata = { ...readMetadata(topic) };
|
|
460
|
+
const patch = {};
|
|
461
|
+
const topicUpdateArgs = {
|
|
462
|
+
id: String(topic._id)
|
|
463
|
+
};
|
|
464
|
+
for (const [key, rawValue] of Object.entries(value)) {
|
|
465
|
+
switch (key) {
|
|
466
|
+
case "_id":
|
|
467
|
+
case "projectId":
|
|
468
|
+
case "topicId":
|
|
469
|
+
case "legacyProjectId":
|
|
470
|
+
case "storageProjectId":
|
|
471
|
+
break;
|
|
472
|
+
case "name":
|
|
473
|
+
case "description":
|
|
474
|
+
patch[key] = rawValue;
|
|
475
|
+
topicUpdateArgs[key] = rawValue;
|
|
476
|
+
break;
|
|
477
|
+
case "tenantId":
|
|
478
|
+
case "workspaceId":
|
|
479
|
+
case "ownerId":
|
|
480
|
+
throw new Error(
|
|
481
|
+
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
482
|
+
);
|
|
483
|
+
case "status": {
|
|
484
|
+
const status = coerceStatus(rawValue);
|
|
485
|
+
if (status) {
|
|
486
|
+
patch.status = status;
|
|
487
|
+
topicUpdateArgs.status = status;
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
case "visibility": {
|
|
492
|
+
const visibility = coerceVisibility(rawValue);
|
|
493
|
+
if (visibility) {
|
|
494
|
+
patch.visibility = visibility;
|
|
495
|
+
topicUpdateArgs.visibility = visibility;
|
|
496
|
+
}
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
case "type": {
|
|
500
|
+
const projectType = readNonEmptyString(rawValue);
|
|
501
|
+
if (projectType) {
|
|
502
|
+
nextMetadata.projectType = projectType;
|
|
503
|
+
} else {
|
|
504
|
+
delete nextMetadata.projectType;
|
|
505
|
+
}
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case "updatedAt":
|
|
509
|
+
case "createdAt":
|
|
510
|
+
break;
|
|
511
|
+
default:
|
|
512
|
+
if (rawValue === void 0) {
|
|
513
|
+
delete nextMetadata[key];
|
|
514
|
+
} else {
|
|
515
|
+
nextMetadata[key] = rawValue;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
patch.updatedAt = Date.now();
|
|
520
|
+
patch.metadata = nextMetadata;
|
|
521
|
+
topicUpdateArgs.metadata = nextMetadata;
|
|
522
|
+
if (typeof ctx.runMutation === "function") {
|
|
523
|
+
try {
|
|
524
|
+
await ctx.runMutation(api.topics.update, topicUpdateArgs);
|
|
525
|
+
} catch (error) {
|
|
526
|
+
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
527
|
+
throw error;
|
|
528
|
+
}
|
|
529
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
530
|
+
}
|
|
531
|
+
} else if (ctx?.db && typeof ctx.db.patch === "function") {
|
|
532
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
533
|
+
} else {
|
|
534
|
+
throw new Error(
|
|
535
|
+
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
return materializeTopicProjectOverlay({
|
|
539
|
+
...topic,
|
|
540
|
+
...patch,
|
|
541
|
+
metadata: nextMetadata
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// src/resolvers.ts
|
|
546
|
+
function isMissingLucernChildComponentError2(error) {
|
|
547
|
+
const message = getErrorMessage2(error);
|
|
548
|
+
return message.includes(
|
|
549
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
550
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
551
|
+
}
|
|
552
|
+
function getErrorMessage2(error) {
|
|
553
|
+
if (error instanceof Error) {
|
|
554
|
+
return error.message;
|
|
555
|
+
}
|
|
556
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
557
|
+
return error.message;
|
|
558
|
+
}
|
|
559
|
+
return "unknown error";
|
|
560
|
+
}
|
|
561
|
+
function isAdvisoryTopicPatch(value) {
|
|
562
|
+
const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
|
|
563
|
+
const keys = Object.keys(value);
|
|
564
|
+
return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
|
|
565
|
+
}
|
|
566
|
+
async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
567
|
+
try {
|
|
568
|
+
await patchTopicProjectOverlay(ctx, projectId, value);
|
|
569
|
+
} catch (error) {
|
|
570
|
+
if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
|
|
571
|
+
throw error;
|
|
572
|
+
}
|
|
573
|
+
console.warn(
|
|
574
|
+
"[lucern graph-primitives] Non-fatal advisory topic patch failure",
|
|
575
|
+
{
|
|
576
|
+
projectId,
|
|
577
|
+
keys: Object.keys(value),
|
|
578
|
+
error: getErrorMessage2(error)
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function defaultResolvers() {
|
|
584
|
+
return {
|
|
585
|
+
getProject: (ctx, projectId) => resolveTopicProjectOverlay(ctx, projectId, {
|
|
586
|
+
idMode: "legacy",
|
|
587
|
+
projectLikeOnly: false
|
|
588
|
+
}),
|
|
589
|
+
patchProject: (ctx, projectId, value) => patchProjectWithTolerance(ctx, projectId, value),
|
|
590
|
+
listTopics: (ctx) => listTopicProjectOverlays(ctx, {
|
|
591
|
+
idMode: "legacy"
|
|
592
|
+
}),
|
|
593
|
+
getFinalArtifact: (ctx, artifactId) => ctx.db.get(artifactId)
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
var resolverOverrides = {};
|
|
597
|
+
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
598
|
+
return {
|
|
599
|
+
...defaultResolvers(),
|
|
600
|
+
...resolverOverrides
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// src/epistemicQuestions.helpers.ts
|
|
605
|
+
async function markProjectGraphDirty(ctx, projectId, topicId) {
|
|
606
|
+
const normalizedProjectId = typeof projectId === "string" && projectId.trim().length > 0 ? projectId : void 0;
|
|
607
|
+
const normalizedTopicId = typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
|
|
608
|
+
if (!normalizedProjectId && !normalizedTopicId) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (normalizedProjectId) {
|
|
612
|
+
await ctx.scheduler.runAfter(
|
|
613
|
+
0,
|
|
614
|
+
internal.graphAnalysisCache.markCacheStaleInternal,
|
|
615
|
+
{
|
|
616
|
+
projectId: normalizedProjectId
|
|
617
|
+
}
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
if (normalizedTopicId) {
|
|
621
|
+
await ctx.scheduler.runAfter(
|
|
622
|
+
0,
|
|
623
|
+
internal.graphAnalysisCache.markCacheStaleByTopic,
|
|
624
|
+
{
|
|
625
|
+
topicId: normalizedTopicId
|
|
626
|
+
}
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
await resolveGraphPrimitivesAppResolvers().patchProject(
|
|
630
|
+
ctx,
|
|
631
|
+
normalizedTopicId ?? normalizedProjectId,
|
|
632
|
+
{
|
|
633
|
+
lastActivityAt: Date.now()
|
|
634
|
+
}
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
function dedupeQuestionNodes(nodes) {
|
|
638
|
+
const seen = /* @__PURE__ */ new Set();
|
|
639
|
+
const deduped = [];
|
|
640
|
+
for (const node of nodes) {
|
|
641
|
+
const id = String(node._id);
|
|
642
|
+
if (seen.has(id)) {
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
seen.add(id);
|
|
646
|
+
deduped.push(node);
|
|
647
|
+
}
|
|
648
|
+
return deduped;
|
|
649
|
+
}
|
|
650
|
+
function normalizeQuestionTopicId(topicId) {
|
|
651
|
+
return typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
|
|
652
|
+
}
|
|
653
|
+
async function resolveQuestionScopeOrNull(ctx, args) {
|
|
654
|
+
if (!args.projectId && !args.topicId) {
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
try {
|
|
658
|
+
return await resolveTopicProjectScope(ctx, {
|
|
659
|
+
projectId: args.projectId ?? void 0,
|
|
660
|
+
topicId: args.topicId ?? void 0
|
|
661
|
+
});
|
|
662
|
+
} catch (error) {
|
|
663
|
+
debugGraphPrimitiveFallback(
|
|
664
|
+
"[epistemicQuestions] Failed to resolve question scope",
|
|
665
|
+
{
|
|
666
|
+
error: formatGraphPrimitiveError(error),
|
|
667
|
+
projectId: args.projectId,
|
|
668
|
+
topicId: args.topicId
|
|
669
|
+
}
|
|
670
|
+
);
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
async function getQuestionNodesForScope(ctx, scope, args) {
|
|
675
|
+
const fetchNodes = (query2) => query2.collect();
|
|
676
|
+
const topicNodes = await fetchNodes(
|
|
677
|
+
ctx.db.query("epistemicNodes").withIndex(
|
|
678
|
+
"by_topic_type",
|
|
679
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "question")
|
|
680
|
+
)
|
|
681
|
+
);
|
|
682
|
+
return dedupeQuestionNodes(topicNodes).filter(
|
|
683
|
+
(node) => questionMatchesScope(node, scope)
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
function questionMatchesScope(node, scope) {
|
|
687
|
+
return scope.topicId !== void 0 && node.topicId === scope.topicId || scope.projectId !== void 0 && node.projectId === scope.projectId;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// src/epistemicQuestions.evidence.ts
|
|
691
|
+
var createEvidenceFromScoredQuestion = internalMutation({
|
|
692
|
+
args: {
|
|
693
|
+
questionNodeId: v.id("epistemicNodes"),
|
|
694
|
+
questionText: v.string(),
|
|
695
|
+
answerText: v.string(),
|
|
696
|
+
beliefId: v.string(),
|
|
697
|
+
relatedBeliefIds: v.array(v.string()),
|
|
698
|
+
conviction: v.number(),
|
|
699
|
+
rationale: v.string(),
|
|
700
|
+
...optionalScopeArgs,
|
|
701
|
+
userId: v.string()
|
|
702
|
+
},
|
|
703
|
+
returns: permissiveReturn,
|
|
704
|
+
handler: async (ctx, args) => {
|
|
705
|
+
const now = Date.now();
|
|
706
|
+
const questionNode = await ctx.db.get(args.questionNodeId);
|
|
707
|
+
if (!questionNode) {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
const qMeta = questionNode.metadata || {};
|
|
711
|
+
if (qMeta.evidenceNodeId) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const evidenceText = `Q: ${args.questionText}
|
|
715
|
+
|
|
716
|
+
A: ${args.answerText}`;
|
|
717
|
+
const globalId = generateGlobalId();
|
|
718
|
+
const contentHash = `evidence:${args.questionNodeId}:scored`;
|
|
719
|
+
const evidenceNodeId = await ctx.db.insert("epistemicNodes", {
|
|
720
|
+
globalId,
|
|
721
|
+
projectId: args.projectId,
|
|
722
|
+
topicId: normalizeQuestionTopicId(args.topicId),
|
|
723
|
+
nodeType: "evidence",
|
|
724
|
+
canonicalText: evidenceText,
|
|
725
|
+
contentHash,
|
|
726
|
+
status: "active",
|
|
727
|
+
epistemicLayer: "L2",
|
|
728
|
+
sourceType: "verified",
|
|
729
|
+
// Human-scored sprint answer
|
|
730
|
+
createdAt: now,
|
|
731
|
+
updatedAt: now,
|
|
732
|
+
createdBy: args.userId,
|
|
733
|
+
metadata: {
|
|
734
|
+
kind: "observation",
|
|
735
|
+
sourceQuestionId: args.questionNodeId,
|
|
736
|
+
sprintConviction: args.conviction,
|
|
737
|
+
rationale: args.rationale,
|
|
738
|
+
origin: "sprint_question_scoring"
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
await ctx.db.patch(args.questionNodeId, {
|
|
742
|
+
metadata: {
|
|
743
|
+
...qMeta,
|
|
744
|
+
evidenceNodeId
|
|
745
|
+
},
|
|
746
|
+
updatedAt: now
|
|
747
|
+
});
|
|
748
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
749
|
+
nodeId: evidenceNodeId,
|
|
750
|
+
operation: "upsert"
|
|
751
|
+
});
|
|
752
|
+
const allBeliefIds = /* @__PURE__ */ new Set();
|
|
753
|
+
allBeliefIds.add(args.beliefId);
|
|
754
|
+
for (const id of args.relatedBeliefIds) {
|
|
755
|
+
allBeliefIds.add(id);
|
|
756
|
+
}
|
|
757
|
+
const weight = args.conviction >= 0.5 ? args.conviction : -(1 - args.conviction);
|
|
758
|
+
async function resolveBeliefNode(bId) {
|
|
759
|
+
try {
|
|
760
|
+
const node = await ctx.db.get(bId);
|
|
761
|
+
if (node?.nodeType === "belief") {
|
|
762
|
+
return node;
|
|
763
|
+
}
|
|
764
|
+
} catch (error) {
|
|
765
|
+
debugGraphPrimitiveFallback(
|
|
766
|
+
"[epistemicQuestions] Failed to resolve belief node",
|
|
767
|
+
{
|
|
768
|
+
error: formatGraphPrimitiveError(error),
|
|
769
|
+
beliefId: bId
|
|
770
|
+
}
|
|
771
|
+
);
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
for (const bId of allBeliefIds) {
|
|
777
|
+
const beliefNode = await resolveBeliefNode(bId);
|
|
778
|
+
if (!beliefNode) {
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
const edgeGlobalId = generateGlobalId();
|
|
782
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
783
|
+
globalId: edgeGlobalId,
|
|
784
|
+
fromGlobalId: globalId,
|
|
785
|
+
toGlobalId: beliefNode.globalId,
|
|
786
|
+
edgeType: "informs",
|
|
787
|
+
weight,
|
|
788
|
+
confidence: Math.abs(weight),
|
|
789
|
+
context: `origin=sprint_question_scoring | relation=${weight >= 0 ? "supports" : "contradicts"} | sourceQuestionId=${String(args.questionNodeId)}`,
|
|
790
|
+
createdBy: args.userId,
|
|
791
|
+
topicId: args.projectId ? String(args.projectId) : void 0,
|
|
792
|
+
fromNodeType: "evidence",
|
|
793
|
+
toNodeType: "belief",
|
|
794
|
+
fromLayer: "L2",
|
|
795
|
+
toLayer: "L3"
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
const answersEdgeGlobalId = generateGlobalId();
|
|
799
|
+
await ctx.scheduler.runAfter(0, internal.neo4jEdgeAPI.createEdge, {
|
|
800
|
+
globalId: answersEdgeGlobalId,
|
|
801
|
+
fromGlobalId: globalId,
|
|
802
|
+
toGlobalId: questionNode.globalId,
|
|
803
|
+
edgeType: "derived_from",
|
|
804
|
+
weight: 1,
|
|
805
|
+
confidence: 1,
|
|
806
|
+
context: `origin=sprint_question_scoring | sourceQuestionId=${String(args.questionNodeId)}`,
|
|
807
|
+
createdBy: args.userId,
|
|
808
|
+
topicId: args.projectId ? String(args.projectId) : void 0,
|
|
809
|
+
fromNodeType: "evidence",
|
|
810
|
+
toNodeType: "question",
|
|
811
|
+
fromLayer: "L2",
|
|
812
|
+
toLayer: "L3"
|
|
813
|
+
});
|
|
814
|
+
await ctx.db.insert("epistemicAudit", {
|
|
815
|
+
entityType: "evidence",
|
|
816
|
+
entityId: evidenceNodeId,
|
|
817
|
+
changeType: "created",
|
|
818
|
+
changedAt: now,
|
|
819
|
+
changedBy: args.userId,
|
|
820
|
+
isAgent: false,
|
|
821
|
+
projectId: args.projectId,
|
|
822
|
+
topicId: normalizeQuestionTopicId(args.topicId),
|
|
823
|
+
newState: {
|
|
824
|
+
text: evidenceText.slice(0, 200),
|
|
825
|
+
kind: "observation",
|
|
826
|
+
sourceType: "verified",
|
|
827
|
+
origin: "sprint_question_scoring",
|
|
828
|
+
linkedBeliefIds: [...allBeliefIds]
|
|
829
|
+
}
|
|
830
|
+
});
|
|
831
|
+
await markProjectGraphDirty(
|
|
832
|
+
ctx,
|
|
833
|
+
args.projectId,
|
|
834
|
+
normalizeQuestionTopicId(args.topicId)
|
|
835
|
+
);
|
|
836
|
+
return { evidenceNodeId };
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
var backfillScoredQuestionEvidence = internalMutation({
|
|
840
|
+
args: {
|
|
841
|
+
...optionalScopeArgs,
|
|
842
|
+
userId: v.string(),
|
|
843
|
+
dryRun: v.optional(v.boolean())
|
|
844
|
+
},
|
|
845
|
+
returns: permissiveReturn,
|
|
846
|
+
handler: async (ctx, args) => {
|
|
847
|
+
const dryRun = args.dryRun ?? false;
|
|
848
|
+
const scope = await resolveQuestionScopeOrNull(ctx, args);
|
|
849
|
+
if (!scope) {
|
|
850
|
+
return {
|
|
851
|
+
dryRun,
|
|
852
|
+
totalQuestions: 0,
|
|
853
|
+
candidateCount: 0,
|
|
854
|
+
scheduled: 0,
|
|
855
|
+
skipped: 0,
|
|
856
|
+
candidates: []
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
const allQuestions = await getQuestionNodesForScope(ctx, scope);
|
|
860
|
+
const candidates = allQuestions.filter((n) => {
|
|
861
|
+
const meta = n.metadata || {};
|
|
862
|
+
return meta.convictionStage === "scored" && meta.answer && !meta.evidenceNodeId && (meta.linkedBeliefId || meta.beliefId);
|
|
863
|
+
});
|
|
864
|
+
if (dryRun) {
|
|
865
|
+
return {
|
|
866
|
+
dryRun: true,
|
|
867
|
+
candidateCount: candidates.length,
|
|
868
|
+
candidates: candidates.map((n) => {
|
|
869
|
+
const meta = n.metadata || {};
|
|
870
|
+
return {
|
|
871
|
+
questionId: n._id,
|
|
872
|
+
questionText: n.canonicalText?.slice(0, 80),
|
|
873
|
+
beliefId: meta.linkedBeliefId || meta.beliefId,
|
|
874
|
+
conviction: meta.conviction,
|
|
875
|
+
hasAnswer: !!meta.answer
|
|
876
|
+
};
|
|
877
|
+
})
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
let created = 0;
|
|
881
|
+
let skipped = 0;
|
|
882
|
+
for (const questionNode of candidates) {
|
|
883
|
+
const meta = questionNode.metadata || {};
|
|
884
|
+
const beliefId = meta.linkedBeliefId ?? meta.beliefId;
|
|
885
|
+
const answerText = meta.answer;
|
|
886
|
+
const conviction = meta.conviction ?? 0.5;
|
|
887
|
+
if (!beliefId || !answerText) {
|
|
888
|
+
skipped++;
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
await ctx.scheduler.runAfter(
|
|
892
|
+
created * 100,
|
|
893
|
+
// Stagger to avoid overwhelming Neo4j
|
|
894
|
+
internal.epistemicQuestions.createEvidenceFromScoredQuestion,
|
|
895
|
+
{
|
|
896
|
+
questionNodeId: questionNode._id,
|
|
897
|
+
questionText: questionNode.canonicalText || "",
|
|
898
|
+
answerText,
|
|
899
|
+
beliefId,
|
|
900
|
+
relatedBeliefIds: meta.relatedBeliefIds || [],
|
|
901
|
+
conviction,
|
|
902
|
+
rationale: meta.convictionRationale || "",
|
|
903
|
+
projectId: scope.projectId,
|
|
904
|
+
topicId: normalizeQuestionTopicId(scope.topicId),
|
|
905
|
+
userId: args.userId
|
|
906
|
+
}
|
|
907
|
+
);
|
|
908
|
+
created++;
|
|
909
|
+
}
|
|
910
|
+
if (created > 0) {
|
|
911
|
+
await markProjectGraphDirty(
|
|
912
|
+
ctx,
|
|
913
|
+
scope.projectId,
|
|
914
|
+
normalizeQuestionTopicId(scope.topicId)
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
return {
|
|
918
|
+
dryRun: false,
|
|
919
|
+
totalQuestions: allQuestions.length,
|
|
920
|
+
candidateCount: candidates.length,
|
|
921
|
+
scheduled: created,
|
|
922
|
+
skipped
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
export { backfillScoredQuestionEvidence, createEvidenceFromScoredQuestion };
|
|
928
|
+
//# sourceMappingURL=epistemicQuestions.evidence.js.map
|
|
929
|
+
//# sourceMappingURL=epistemicQuestions.evidence.js.map
|