@lucern/graph-primitives 1.0.0 → 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,1421 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { requireProjectAccess, checkProjectAccess } from '@lucern/access-control/access';
|
|
3
|
+
import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
|
|
4
|
+
import { componentsGeneric, anyApi, mutationGeneric, internalMutationGeneric } from 'convex/server';
|
|
5
|
+
import { isNodeType, getLayerForNodeType } from '@lucern/contracts/schema-helpers/spine/tables/epistemicNodes';
|
|
6
|
+
import '@lucern/access-control/audience';
|
|
7
|
+
|
|
8
|
+
// src/epistemicEvidenceMutations.ts
|
|
9
|
+
var api = anyApi;
|
|
10
|
+
componentsGeneric();
|
|
11
|
+
var internal = anyApi;
|
|
12
|
+
var internalMutation = internalMutationGeneric;
|
|
13
|
+
var mutation = mutationGeneric;
|
|
14
|
+
|
|
15
|
+
// src/debug.ts
|
|
16
|
+
function isGraphPrimitiveDebugEnabled() {
|
|
17
|
+
const env = globalThis.process?.env;
|
|
18
|
+
return env?.LUCERN_COMPAT_FALLBACK_DEBUG === "1" || env?.LUCERN_GRAPH_DEBUG === "1";
|
|
19
|
+
}
|
|
20
|
+
function debugGraphPrimitiveFallback(message, context) {
|
|
21
|
+
if (!isGraphPrimitiveDebugEnabled()) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
console.debug(message, context ?? {});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/embeddingTrigger.ts
|
|
28
|
+
async function scheduleEmbeddingGeneration(args) {
|
|
29
|
+
try {
|
|
30
|
+
await args.ctx.scheduler.runAfter(
|
|
31
|
+
0,
|
|
32
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
33
|
+
{
|
|
34
|
+
nodeId: args.nodeId,
|
|
35
|
+
projectId: args.projectId ? String(args.projectId) : void 0,
|
|
36
|
+
topicId: args.topicId ? String(args.topicId) : void 0,
|
|
37
|
+
createdBy: args.createdBy,
|
|
38
|
+
nodeType: args.nodeType,
|
|
39
|
+
text: args.text.slice(0, 2e4),
|
|
40
|
+
hasAnswer: args.hasAnswer,
|
|
41
|
+
confidence: args.confidence
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
debugGraphPrimitiveFallback(
|
|
46
|
+
"[embeddingTrigger] Failed to schedule embedding generation",
|
|
47
|
+
{
|
|
48
|
+
error,
|
|
49
|
+
nodeId: String(args.nodeId),
|
|
50
|
+
nodeType: args.nodeType
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/globalId.ts
|
|
57
|
+
function generateGlobalId() {
|
|
58
|
+
const bytes = new Uint8Array(16);
|
|
59
|
+
crypto.getRandomValues(bytes);
|
|
60
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
61
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
62
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
63
|
+
""
|
|
64
|
+
);
|
|
65
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
66
|
+
}
|
|
67
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
68
|
+
function asMappedProjectId(topic) {
|
|
69
|
+
if (!topic) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD]);
|
|
73
|
+
if (directLegacyProjectId) {
|
|
74
|
+
return directLegacyProjectId;
|
|
75
|
+
}
|
|
76
|
+
const metadata = topic.metadata || {};
|
|
77
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
78
|
+
return candidate ? candidate : void 0;
|
|
79
|
+
}
|
|
80
|
+
function normalizeScopeValue(value) {
|
|
81
|
+
if (typeof value !== "string") {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const normalized = value.trim();
|
|
85
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
86
|
+
}
|
|
87
|
+
function pickPrimaryTopic(candidates) {
|
|
88
|
+
return [...candidates].sort((a, b) => {
|
|
89
|
+
const depthA = a.depth ?? 9999;
|
|
90
|
+
const depthB = b.depth ?? 9999;
|
|
91
|
+
if (depthA !== depthB) {
|
|
92
|
+
return depthA - depthB;
|
|
93
|
+
}
|
|
94
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
95
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
96
|
+
if (createdA !== createdB) {
|
|
97
|
+
return createdA - createdB;
|
|
98
|
+
}
|
|
99
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
100
|
+
})[0];
|
|
101
|
+
}
|
|
102
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
103
|
+
try {
|
|
104
|
+
return await ctx.db.query("topics").withIndex(
|
|
105
|
+
"by_graph_scope_project",
|
|
106
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
|
|
107
|
+
).collect();
|
|
108
|
+
} catch (error) {
|
|
109
|
+
debugGraphPrimitiveFallback(
|
|
110
|
+
"[topicScope] Failed to resolve scope alias via index",
|
|
111
|
+
{
|
|
112
|
+
error,
|
|
113
|
+
scopeId
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
const topics = await ctx.db.query("topics").collect();
|
|
117
|
+
return topics.filter((topic) => {
|
|
118
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
119
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
120
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
125
|
+
if (typeof ctx.runQuery !== "function") {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
return await ctx.runQuery(api.topics.get, {
|
|
130
|
+
id: topicId
|
|
131
|
+
}) ?? null;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
debugGraphPrimitiveFallback(
|
|
134
|
+
"[topicScope] Failed to resolve topic by host query",
|
|
135
|
+
{
|
|
136
|
+
error,
|
|
137
|
+
topicId
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
144
|
+
if (typeof ctx.runQuery !== "function") {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
149
|
+
projectId: legacyScopeId
|
|
150
|
+
}) ?? null;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
debugGraphPrimitiveFallback(
|
|
153
|
+
"[topicScope] Failed to resolve topic by legacy scope",
|
|
154
|
+
{
|
|
155
|
+
error,
|
|
156
|
+
legacyScopeId
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
163
|
+
const MAX_DEPTH = 10;
|
|
164
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
165
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
166
|
+
if (tenantId && workspaceId) {
|
|
167
|
+
return { tenantId, workspaceId };
|
|
168
|
+
}
|
|
169
|
+
let current = topic;
|
|
170
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
171
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
172
|
+
if (!current) break;
|
|
173
|
+
if (!tenantId) {
|
|
174
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
175
|
+
}
|
|
176
|
+
if (!workspaceId) {
|
|
177
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
178
|
+
}
|
|
179
|
+
if (tenantId && workspaceId) break;
|
|
180
|
+
}
|
|
181
|
+
return { tenantId, workspaceId };
|
|
182
|
+
}
|
|
183
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
184
|
+
if (args.topicId) {
|
|
185
|
+
let topic = null;
|
|
186
|
+
try {
|
|
187
|
+
topic = await ctx.db.get(
|
|
188
|
+
args.topicId
|
|
189
|
+
);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
debugGraphPrimitiveFallback(
|
|
192
|
+
"[topicScope] Failed to load topic by direct id",
|
|
193
|
+
{
|
|
194
|
+
error,
|
|
195
|
+
topicId: args.topicId
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
if (!topic) {
|
|
200
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
201
|
+
}
|
|
202
|
+
if (!topic) {
|
|
203
|
+
topic = pickPrimaryTopic(
|
|
204
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
205
|
+
) ?? null;
|
|
206
|
+
}
|
|
207
|
+
if (!topic) {
|
|
208
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
209
|
+
}
|
|
210
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
211
|
+
const mapped = asMappedProjectId(topic);
|
|
212
|
+
if (mapped) {
|
|
213
|
+
return {
|
|
214
|
+
topicId: topic._id,
|
|
215
|
+
projectId: mapped,
|
|
216
|
+
tenantId: inherited.tenantId,
|
|
217
|
+
workspaceId: inherited.workspaceId,
|
|
218
|
+
source: "topic"
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
topicId: topic._id,
|
|
223
|
+
tenantId: inherited.tenantId,
|
|
224
|
+
workspaceId: inherited.workspaceId,
|
|
225
|
+
source: "topic"
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (args.projectId) {
|
|
229
|
+
let directTopic = null;
|
|
230
|
+
try {
|
|
231
|
+
directTopic = await ctx.db.get(
|
|
232
|
+
args.projectId
|
|
233
|
+
);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
debugGraphPrimitiveFallback(
|
|
236
|
+
"[topicScope] Failed to load direct project topic",
|
|
237
|
+
{
|
|
238
|
+
error,
|
|
239
|
+
projectId: args.projectId
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (directTopic) {
|
|
244
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
245
|
+
const mapped = asMappedProjectId(directTopic);
|
|
246
|
+
return {
|
|
247
|
+
topicId: directTopic._id,
|
|
248
|
+
projectId: mapped ?? args.projectId,
|
|
249
|
+
tenantId: inherited.tenantId,
|
|
250
|
+
workspaceId: inherited.workspaceId,
|
|
251
|
+
source: "topic_inferred"
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
255
|
+
if (directTopic) {
|
|
256
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
257
|
+
const mapped = asMappedProjectId(directTopic);
|
|
258
|
+
return {
|
|
259
|
+
topicId: directTopic._id,
|
|
260
|
+
projectId: mapped ?? args.projectId,
|
|
261
|
+
tenantId: inherited.tenantId,
|
|
262
|
+
workspaceId: inherited.workspaceId,
|
|
263
|
+
source: "topic_inferred"
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
267
|
+
const primary = pickPrimaryTopic(topics);
|
|
268
|
+
if (primary) {
|
|
269
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
270
|
+
return {
|
|
271
|
+
topicId: primary._id,
|
|
272
|
+
projectId: args.projectId,
|
|
273
|
+
tenantId: inherited.tenantId,
|
|
274
|
+
workspaceId: inherited.workspaceId,
|
|
275
|
+
source: "project_mapped_topic"
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
throw new Error(
|
|
279
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
throw new Error(
|
|
283
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
var optionalScopeArgs = {
|
|
287
|
+
projectId: v.optional(v.string()),
|
|
288
|
+
topicId: v.optional(v.string())
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// src/workspaceIsolation.ts
|
|
292
|
+
function normalizeScopeValue2(value) {
|
|
293
|
+
if (typeof value !== "string") {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const normalized = value.trim();
|
|
297
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
298
|
+
}
|
|
299
|
+
function throwWorkspaceIsolationError(args) {
|
|
300
|
+
const error = new Error(args.message);
|
|
301
|
+
error.status = 409;
|
|
302
|
+
error.code = "INVARIANT_VIOLATION";
|
|
303
|
+
error.invariantCode = args.invariantCode;
|
|
304
|
+
error.suggestion = args.suggestion;
|
|
305
|
+
error.details = args.details;
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
function assertWorkspaceScopedEpistemicNodeScope(args) {
|
|
309
|
+
const layer = isNodeType(args.nodeType) ? getLayerForNodeType(args.nodeType) : void 0;
|
|
310
|
+
if (layer === "ontological") {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const workspaceId = normalizeScopeValue2(args.scope.workspaceId);
|
|
314
|
+
if (workspaceId) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
throwWorkspaceIsolationError({
|
|
318
|
+
message: "Workspace-scoped reasoning isolation requires workspaceId on non-ontological node creation.",
|
|
319
|
+
invariantCode: "workspace.scope_required_for_epistemic_nodes",
|
|
320
|
+
suggestion: "Resolve the topic/project scope through a workspace-bound topic before creating epistemic nodes.",
|
|
321
|
+
details: {
|
|
322
|
+
mutationName: args.mutationName,
|
|
323
|
+
nodeType: args.nodeType,
|
|
324
|
+
topicId: args.scope.topicId,
|
|
325
|
+
projectId: args.scope.projectId
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
function resolveRuntimePackMutationContext(args) {
|
|
330
|
+
if (!args.runtimeToolName && !args.runtimePackKey && !args.runtimePackInstallScope) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
toolName: args.runtimeToolName,
|
|
335
|
+
packKey: args.runtimePackKey,
|
|
336
|
+
packInstallScope: args.runtimePackInstallScope
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function assertTenantPackWorkspaceMutationAllowed(args) {
|
|
340
|
+
if (!args.runtime?.packKey || args.runtime.packInstallScope !== "tenant") {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const targetWorkspaceId = normalizeScopeValue2(args.target.workspaceId);
|
|
344
|
+
const targetLayer = typeof args.target.epistemicLayer === "string" ? args.target.epistemicLayer : void 0;
|
|
345
|
+
if (!targetWorkspaceId || targetLayer === "ontological") {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
throwWorkspaceIsolationError({
|
|
349
|
+
message: `Tenant-scoped pack "${args.runtime.packKey}" cannot mutate workspace-scoped reasoning state.`,
|
|
350
|
+
invariantCode: "workspace.tenant_pack_reasoning_write_forbidden",
|
|
351
|
+
suggestion: "Use a workspace-scoped pack for workspace-local graph mutations, or route the change through tenant-global canonical entity flows.",
|
|
352
|
+
details: {
|
|
353
|
+
mutationName: args.mutationName,
|
|
354
|
+
toolName: args.runtime.toolName,
|
|
355
|
+
packKey: args.runtime.packKey,
|
|
356
|
+
targetWorkspaceId,
|
|
357
|
+
targetNodeType: args.target.nodeType,
|
|
358
|
+
targetLayer
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/topicProjectOverlay.ts
|
|
364
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
365
|
+
function readNonEmptyString(value) {
|
|
366
|
+
if (typeof value !== "string") {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const normalized = value.trim();
|
|
370
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
371
|
+
}
|
|
372
|
+
function readStringArray(value) {
|
|
373
|
+
if (!Array.isArray(value)) {
|
|
374
|
+
return [];
|
|
375
|
+
}
|
|
376
|
+
return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
|
|
377
|
+
}
|
|
378
|
+
function readMetadata(topic) {
|
|
379
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
380
|
+
}
|
|
381
|
+
function readLegacyProjectId(value) {
|
|
382
|
+
if (!value) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
return readNonEmptyString(value[LEGACY_SCOPE_FIELD2]);
|
|
386
|
+
}
|
|
387
|
+
function coerceVisibility(value) {
|
|
388
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
389
|
+
}
|
|
390
|
+
function coerceStatus(value) {
|
|
391
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
392
|
+
}
|
|
393
|
+
function mapProjectType(topic, metadata) {
|
|
394
|
+
const explicit = readNonEmptyString(metadata.projectType);
|
|
395
|
+
if (explicit) {
|
|
396
|
+
return explicit;
|
|
397
|
+
}
|
|
398
|
+
if (topic.type === "theme") {
|
|
399
|
+
return "thematic";
|
|
400
|
+
}
|
|
401
|
+
return readNonEmptyString(topic.type) || "general";
|
|
402
|
+
}
|
|
403
|
+
function isProjectLikeTopic(topic) {
|
|
404
|
+
const metadata = readMetadata(topic);
|
|
405
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
406
|
+
}
|
|
407
|
+
function isMissingLucernChildComponentError(error) {
|
|
408
|
+
const message = getErrorMessage(error);
|
|
409
|
+
return message.includes(
|
|
410
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
411
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
412
|
+
}
|
|
413
|
+
function getErrorMessage(error) {
|
|
414
|
+
if (error instanceof Error) {
|
|
415
|
+
return error.message;
|
|
416
|
+
}
|
|
417
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
418
|
+
return error.message;
|
|
419
|
+
}
|
|
420
|
+
return "unknown error";
|
|
421
|
+
}
|
|
422
|
+
async function resolveTopicDoc(ctx, scopeId) {
|
|
423
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
424
|
+
try {
|
|
425
|
+
const directTopic = await ctx.db.get(
|
|
426
|
+
scopeId
|
|
427
|
+
);
|
|
428
|
+
if (directTopic) {
|
|
429
|
+
return directTopic;
|
|
430
|
+
}
|
|
431
|
+
} catch (error) {
|
|
432
|
+
debugGraphPrimitiveFallback(
|
|
433
|
+
"[topicProjectOverlay] Failed to resolve topic by direct ID",
|
|
434
|
+
{
|
|
435
|
+
error,
|
|
436
|
+
scopeId
|
|
437
|
+
}
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (typeof ctx.runQuery !== "function") {
|
|
442
|
+
return null;
|
|
443
|
+
}
|
|
444
|
+
try {
|
|
445
|
+
const topic = await ctx.runQuery(api.topics.get, {
|
|
446
|
+
id: String(scopeId)
|
|
447
|
+
});
|
|
448
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
449
|
+
return topic;
|
|
450
|
+
}
|
|
451
|
+
} catch (error) {
|
|
452
|
+
debugGraphPrimitiveFallback(
|
|
453
|
+
"[topicProjectOverlay] Failed to resolve topic by ID query",
|
|
454
|
+
{
|
|
455
|
+
error,
|
|
456
|
+
scopeId
|
|
457
|
+
}
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
462
|
+
projectId: String(scopeId)
|
|
463
|
+
});
|
|
464
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
465
|
+
return topic;
|
|
466
|
+
}
|
|
467
|
+
} catch (error) {
|
|
468
|
+
debugGraphPrimitiveFallback(
|
|
469
|
+
"[topicProjectOverlay] Failed to resolve topic by legacy scope ID",
|
|
470
|
+
{ error, scopeId }
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
function materializeTopicProjectOverlay(topic, idMode = "legacy") {
|
|
476
|
+
const metadata = readMetadata(topic);
|
|
477
|
+
const topicId = String(topic._id);
|
|
478
|
+
const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
|
|
479
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
480
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
481
|
+
const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
|
|
482
|
+
const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
|
|
483
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
484
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
485
|
+
return {
|
|
486
|
+
...metadata,
|
|
487
|
+
_id: outwardId,
|
|
488
|
+
projectId: outwardId,
|
|
489
|
+
topicId,
|
|
490
|
+
storageProjectId,
|
|
491
|
+
legacyProjectId,
|
|
492
|
+
name: readNonEmptyString(topic.name) || "Untitled Theme",
|
|
493
|
+
type: mapProjectType(topic, metadata),
|
|
494
|
+
description: readNonEmptyString(topic.description),
|
|
495
|
+
ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
|
|
496
|
+
sharedWith: readStringArray(metadata.sharedWith),
|
|
497
|
+
visibility,
|
|
498
|
+
tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
|
|
499
|
+
workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
|
|
500
|
+
status,
|
|
501
|
+
tags: readStringArray(metadata.tags),
|
|
502
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
503
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
504
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
505
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
506
|
+
createdAt,
|
|
507
|
+
updatedAt
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
|
|
511
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
512
|
+
if (!topic) {
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
return materializeTopicProjectOverlay(topic, options.idMode);
|
|
519
|
+
}
|
|
520
|
+
async function listTopicProjectOverlays(ctx, options = {}) {
|
|
521
|
+
let allTopics = [];
|
|
522
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
523
|
+
try {
|
|
524
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
525
|
+
} catch (error) {
|
|
526
|
+
debugGraphPrimitiveFallback(
|
|
527
|
+
"[topicProjectOverlay] Failed to read topics table; falling back to API",
|
|
528
|
+
{ error }
|
|
529
|
+
);
|
|
530
|
+
allTopics = [];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
534
|
+
allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
|
|
535
|
+
}
|
|
536
|
+
return allTopics.filter(
|
|
537
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
538
|
+
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
539
|
+
}
|
|
540
|
+
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
541
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
542
|
+
if (!topic) {
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
const nextMetadata = { ...readMetadata(topic) };
|
|
546
|
+
const patch = {};
|
|
547
|
+
const topicUpdateArgs = {
|
|
548
|
+
id: String(topic._id)
|
|
549
|
+
};
|
|
550
|
+
for (const [key, rawValue] of Object.entries(value)) {
|
|
551
|
+
switch (key) {
|
|
552
|
+
case "_id":
|
|
553
|
+
case "projectId":
|
|
554
|
+
case "topicId":
|
|
555
|
+
case "legacyProjectId":
|
|
556
|
+
case "storageProjectId":
|
|
557
|
+
break;
|
|
558
|
+
case "name":
|
|
559
|
+
case "description":
|
|
560
|
+
patch[key] = rawValue;
|
|
561
|
+
topicUpdateArgs[key] = rawValue;
|
|
562
|
+
break;
|
|
563
|
+
case "tenantId":
|
|
564
|
+
case "workspaceId":
|
|
565
|
+
case "ownerId":
|
|
566
|
+
throw new Error(
|
|
567
|
+
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
568
|
+
);
|
|
569
|
+
case "status": {
|
|
570
|
+
const status = coerceStatus(rawValue);
|
|
571
|
+
if (status) {
|
|
572
|
+
patch.status = status;
|
|
573
|
+
topicUpdateArgs.status = status;
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
case "visibility": {
|
|
578
|
+
const visibility = coerceVisibility(rawValue);
|
|
579
|
+
if (visibility) {
|
|
580
|
+
patch.visibility = visibility;
|
|
581
|
+
topicUpdateArgs.visibility = visibility;
|
|
582
|
+
}
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
case "type": {
|
|
586
|
+
const projectType = readNonEmptyString(rawValue);
|
|
587
|
+
if (projectType) {
|
|
588
|
+
nextMetadata.projectType = projectType;
|
|
589
|
+
} else {
|
|
590
|
+
delete nextMetadata.projectType;
|
|
591
|
+
}
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
case "updatedAt":
|
|
595
|
+
case "createdAt":
|
|
596
|
+
break;
|
|
597
|
+
default:
|
|
598
|
+
if (rawValue === void 0) {
|
|
599
|
+
delete nextMetadata[key];
|
|
600
|
+
} else {
|
|
601
|
+
nextMetadata[key] = rawValue;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
patch.updatedAt = Date.now();
|
|
606
|
+
patch.metadata = nextMetadata;
|
|
607
|
+
topicUpdateArgs.metadata = nextMetadata;
|
|
608
|
+
if (typeof ctx.runMutation === "function") {
|
|
609
|
+
try {
|
|
610
|
+
await ctx.runMutation(api.topics.update, topicUpdateArgs);
|
|
611
|
+
} catch (error) {
|
|
612
|
+
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
613
|
+
throw error;
|
|
614
|
+
}
|
|
615
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
616
|
+
}
|
|
617
|
+
} else if (ctx?.db && typeof ctx.db.patch === "function") {
|
|
618
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
619
|
+
} else {
|
|
620
|
+
throw new Error(
|
|
621
|
+
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
return materializeTopicProjectOverlay({
|
|
625
|
+
...topic,
|
|
626
|
+
...patch,
|
|
627
|
+
metadata: nextMetadata
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/resolvers.ts
|
|
632
|
+
function isMissingLucernChildComponentError2(error) {
|
|
633
|
+
const message = getErrorMessage2(error);
|
|
634
|
+
return message.includes(
|
|
635
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
636
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
637
|
+
}
|
|
638
|
+
function getErrorMessage2(error) {
|
|
639
|
+
if (error instanceof Error) {
|
|
640
|
+
return error.message;
|
|
641
|
+
}
|
|
642
|
+
if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
|
|
643
|
+
return error.message;
|
|
644
|
+
}
|
|
645
|
+
return "unknown error";
|
|
646
|
+
}
|
|
647
|
+
function isAdvisoryTopicPatch(value) {
|
|
648
|
+
const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
|
|
649
|
+
const keys = Object.keys(value);
|
|
650
|
+
return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
|
|
651
|
+
}
|
|
652
|
+
async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
653
|
+
try {
|
|
654
|
+
await patchTopicProjectOverlay(ctx, projectId, value);
|
|
655
|
+
} catch (error) {
|
|
656
|
+
if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
|
|
657
|
+
throw error;
|
|
658
|
+
}
|
|
659
|
+
console.warn(
|
|
660
|
+
"[lucern graph-primitives] Non-fatal advisory topic patch failure",
|
|
661
|
+
{
|
|
662
|
+
projectId,
|
|
663
|
+
keys: Object.keys(value),
|
|
664
|
+
error: getErrorMessage2(error)
|
|
665
|
+
}
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
function defaultResolvers() {
|
|
670
|
+
return {
|
|
671
|
+
getProject: (ctx, projectId) => resolveTopicProjectOverlay(ctx, projectId, {
|
|
672
|
+
idMode: "legacy",
|
|
673
|
+
projectLikeOnly: false
|
|
674
|
+
}),
|
|
675
|
+
patchProject: (ctx, projectId, value) => patchProjectWithTolerance(ctx, projectId, value),
|
|
676
|
+
listTopics: (ctx) => listTopicProjectOverlays(ctx, {
|
|
677
|
+
idMode: "legacy"
|
|
678
|
+
}),
|
|
679
|
+
getFinalArtifact: (ctx, artifactId) => ctx.db.get(artifactId)
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
var resolverOverrides = {};
|
|
683
|
+
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
684
|
+
return {
|
|
685
|
+
...defaultResolvers(),
|
|
686
|
+
...resolverOverrides
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// src/epistemicEvidenceHelpers.ts
|
|
691
|
+
var optionalEvidenceScopeArgs = optionalScopeArgs;
|
|
692
|
+
function generateContentHash(text) {
|
|
693
|
+
const content = `evidence:${text.trim().toLowerCase().replace(/\s+/g, " ").slice(0, 500)}`;
|
|
694
|
+
let hash = 5381;
|
|
695
|
+
for (let i = 0; i < content.length; i++) {
|
|
696
|
+
hash = (hash << 5) + hash + content.charCodeAt(i);
|
|
697
|
+
hash &= hash;
|
|
698
|
+
}
|
|
699
|
+
return Math.abs(hash).toString(16).padStart(8, "0");
|
|
700
|
+
}
|
|
701
|
+
function normalizeKind(kind) {
|
|
702
|
+
if (!kind) {
|
|
703
|
+
return "observation";
|
|
704
|
+
}
|
|
705
|
+
const validKinds = [
|
|
706
|
+
"fact",
|
|
707
|
+
"observation",
|
|
708
|
+
"claim",
|
|
709
|
+
"quote",
|
|
710
|
+
"statistic",
|
|
711
|
+
"signal"
|
|
712
|
+
];
|
|
713
|
+
return validKinds.find((k) => kind.toLowerCase() === k) || "observation";
|
|
714
|
+
}
|
|
715
|
+
function normalizeSourceType(sourceType) {
|
|
716
|
+
if (sourceType === "proprietary" || sourceType === "verified") {
|
|
717
|
+
return sourceType;
|
|
718
|
+
}
|
|
719
|
+
return "ai_generated";
|
|
720
|
+
}
|
|
721
|
+
async function markProjectGraphDirty(ctx, projectId, topicId) {
|
|
722
|
+
const normalizedProjectId = typeof projectId === "string" && projectId.trim().length > 0 ? projectId : void 0;
|
|
723
|
+
const normalizedTopicId = typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
|
|
724
|
+
if (!normalizedProjectId && !normalizedTopicId) {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
if (normalizedProjectId) {
|
|
728
|
+
await ctx.scheduler.runAfter(
|
|
729
|
+
0,
|
|
730
|
+
internal.graphAnalysisCache.markCacheStaleInternal,
|
|
731
|
+
{
|
|
732
|
+
projectId: normalizedProjectId
|
|
733
|
+
}
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
if (normalizedTopicId) {
|
|
737
|
+
await ctx.scheduler.runAfter(
|
|
738
|
+
0,
|
|
739
|
+
internal.graphAnalysisCache.markCacheStaleByTopic,
|
|
740
|
+
{
|
|
741
|
+
topicId: normalizedTopicId
|
|
742
|
+
}
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
await resolveGraphPrimitivesAppResolvers().patchProject(
|
|
746
|
+
ctx,
|
|
747
|
+
normalizedTopicId ?? normalizedProjectId,
|
|
748
|
+
{
|
|
749
|
+
lastActivityAt: Date.now()
|
|
750
|
+
}
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
async function resolveEvidenceScopeOrNull(ctx, args) {
|
|
754
|
+
if (!args.projectId && !args.topicId) {
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
757
|
+
try {
|
|
758
|
+
return await resolveTopicProjectScope(ctx, {
|
|
759
|
+
projectId: args.projectId ?? void 0,
|
|
760
|
+
topicId: args.topicId ?? void 0
|
|
761
|
+
});
|
|
762
|
+
} catch (error) {
|
|
763
|
+
debugGraphPrimitiveFallback(
|
|
764
|
+
"[epistemicEvidence] Failed to resolve evidence scope",
|
|
765
|
+
{
|
|
766
|
+
error,
|
|
767
|
+
projectId: args.projectId,
|
|
768
|
+
topicId: args.topicId
|
|
769
|
+
}
|
|
770
|
+
);
|
|
771
|
+
return null;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// src/epistemicEvidenceMutations.ts
|
|
776
|
+
var create = mutation({
|
|
777
|
+
args: {
|
|
778
|
+
...optionalEvidenceScopeArgs,
|
|
779
|
+
text: v.string(),
|
|
780
|
+
title: v.optional(v.string()),
|
|
781
|
+
content: v.optional(v.string()),
|
|
782
|
+
contentType: v.optional(v.string()),
|
|
783
|
+
kind: v.optional(v.string()),
|
|
784
|
+
tags: v.optional(v.array(v.string())),
|
|
785
|
+
sourceType: v.optional(v.string()),
|
|
786
|
+
externalSourceType: v.optional(v.string()),
|
|
787
|
+
sourceUrl: v.optional(v.string()),
|
|
788
|
+
sourceQuestionId: v.optional(v.string()),
|
|
789
|
+
userId: v.string(),
|
|
790
|
+
rationale: v.string(),
|
|
791
|
+
// Classification fields (from AI tools)
|
|
792
|
+
methodology: v.optional(v.string()),
|
|
793
|
+
informationAsymmetry: v.optional(v.string()),
|
|
794
|
+
sourceDescription: v.optional(v.string()),
|
|
795
|
+
metadata: v.optional(v.any()),
|
|
796
|
+
// Optional linking to beliefs
|
|
797
|
+
linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
|
|
798
|
+
evidenceRelation: v.optional(
|
|
799
|
+
v.union(v.literal("supports"), v.literal("contradicts"))
|
|
800
|
+
),
|
|
801
|
+
confidence: v.optional(v.number())
|
|
802
|
+
},
|
|
803
|
+
returns: permissiveReturn,
|
|
804
|
+
handler: async (ctx, args) => {
|
|
805
|
+
const scope = await resolveEvidenceScopeOrNull(ctx, args);
|
|
806
|
+
if (!scope) {
|
|
807
|
+
throw new Error("Invalid scope: projectId or topicId is required");
|
|
808
|
+
}
|
|
809
|
+
assertWorkspaceScopedEpistemicNodeScope({
|
|
810
|
+
scope,
|
|
811
|
+
nodeType: "evidence",
|
|
812
|
+
mutationName: "epistemicEvidence.create"
|
|
813
|
+
});
|
|
814
|
+
if (scope.projectId) {
|
|
815
|
+
await requireProjectAccess(ctx, scope.projectId, args.userId);
|
|
816
|
+
}
|
|
817
|
+
const now = Date.now();
|
|
818
|
+
const globalId = generateGlobalId();
|
|
819
|
+
const contentHash = generateContentHash(args.text);
|
|
820
|
+
const kind = normalizeKind(args.kind);
|
|
821
|
+
const sourceType = normalizeSourceType(args.sourceType);
|
|
822
|
+
const additionalMetadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
|
|
823
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
824
|
+
globalId,
|
|
825
|
+
topicId: scope.topicId,
|
|
826
|
+
projectId: scope.projectId,
|
|
827
|
+
tenantId: scope.tenantId,
|
|
828
|
+
workspaceId: scope.workspaceId,
|
|
829
|
+
nodeType: "evidence",
|
|
830
|
+
canonicalText: args.text,
|
|
831
|
+
contentHash,
|
|
832
|
+
...typeof args.title === "string" && args.title.trim().length > 0 ? { title: args.title.trim() } : {},
|
|
833
|
+
...typeof args.content === "string" && args.content.length > 0 ? { content: args.content } : {},
|
|
834
|
+
...typeof args.contentType === "string" && args.contentType.trim().length > 0 ? { contentType: args.contentType.trim() } : {},
|
|
835
|
+
status: "active",
|
|
836
|
+
epistemicLayer: "L2",
|
|
837
|
+
sourceType,
|
|
838
|
+
createdAt: now,
|
|
839
|
+
updatedAt: now,
|
|
840
|
+
createdBy: args.userId,
|
|
841
|
+
metadata: {
|
|
842
|
+
kind,
|
|
843
|
+
tags: args.tags || [],
|
|
844
|
+
externalSourceType: args.externalSourceType,
|
|
845
|
+
sourceUrl: args.sourceUrl,
|
|
846
|
+
sourceQuestionId: args.sourceQuestionId,
|
|
847
|
+
rationale: args.rationale,
|
|
848
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
849
|
+
evidenceRelation: args.evidenceRelation,
|
|
850
|
+
confidence: args.confidence,
|
|
851
|
+
methodology: args.methodology,
|
|
852
|
+
informationAsymmetry: args.informationAsymmetry,
|
|
853
|
+
sourceDescription: args.sourceDescription,
|
|
854
|
+
...additionalMetadata
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
858
|
+
nodeId,
|
|
859
|
+
operation: "upsert"
|
|
860
|
+
});
|
|
861
|
+
await scheduleEmbeddingGeneration({
|
|
862
|
+
ctx,
|
|
863
|
+
nodeId,
|
|
864
|
+
projectId: scope.projectId,
|
|
865
|
+
topicId: scope.topicId,
|
|
866
|
+
createdBy: args.userId,
|
|
867
|
+
nodeType: "evidence",
|
|
868
|
+
text: args.text
|
|
869
|
+
});
|
|
870
|
+
if (args.linkedBeliefNodeId && args.evidenceRelation) {
|
|
871
|
+
const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
|
|
872
|
+
if (beliefNode) {
|
|
873
|
+
const weight = args.evidenceRelation === "supports" ? 1 : -1;
|
|
874
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
875
|
+
globalId: crypto.randomUUID(),
|
|
876
|
+
fromGlobalId: globalId,
|
|
877
|
+
toGlobalId: beliefNode.globalId,
|
|
878
|
+
edgeType: "informs",
|
|
879
|
+
weight: weight * (args.confidence || 0.7),
|
|
880
|
+
createdBy: args.userId,
|
|
881
|
+
topicId: scope.projectId ? String(scope.projectId) : void 0,
|
|
882
|
+
fromNodeType: "evidence",
|
|
883
|
+
toNodeType: "belief",
|
|
884
|
+
fromLayer: "L2",
|
|
885
|
+
toLayer: "L3",
|
|
886
|
+
metadata: {
|
|
887
|
+
relation: args.evidenceRelation,
|
|
888
|
+
confidence: args.confidence
|
|
889
|
+
}
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
await ctx.db.insert("epistemicAudit", {
|
|
894
|
+
entityType: "evidence",
|
|
895
|
+
entityId: nodeId,
|
|
896
|
+
changeType: "created",
|
|
897
|
+
changedAt: now,
|
|
898
|
+
changedBy: args.userId,
|
|
899
|
+
isAgent: false,
|
|
900
|
+
projectId: scope.projectId,
|
|
901
|
+
rationale: args.rationale,
|
|
902
|
+
newState: {
|
|
903
|
+
text: args.text.slice(0, 200),
|
|
904
|
+
kind,
|
|
905
|
+
sourceType,
|
|
906
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
907
|
+
evidenceRelation: args.evidenceRelation
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
if (scope.projectId || scope.topicId) {
|
|
911
|
+
await ctx.scheduler.runAfter(
|
|
912
|
+
0,
|
|
913
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
914
|
+
{
|
|
915
|
+
nodeId,
|
|
916
|
+
projectId: scope.projectId,
|
|
917
|
+
topicId: scope.topicId ? String(scope.topicId) : void 0,
|
|
918
|
+
createdBy: args.userId,
|
|
919
|
+
nodeType: "evidence",
|
|
920
|
+
text: args.text
|
|
921
|
+
}
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
if (scope.projectId || scope.topicId) {
|
|
925
|
+
await ctx.scheduler.runAfter(
|
|
926
|
+
2e3,
|
|
927
|
+
internal.nodeClassification.scheduleClassification,
|
|
928
|
+
{
|
|
929
|
+
nodeId,
|
|
930
|
+
nodeType: "evidence",
|
|
931
|
+
projectId: scope.projectId,
|
|
932
|
+
topicId: String(scope.topicId)
|
|
933
|
+
}
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
937
|
+
return { nodeId };
|
|
938
|
+
}
|
|
939
|
+
});
|
|
940
|
+
var createAndLink = mutation({
|
|
941
|
+
args: {
|
|
942
|
+
...optionalEvidenceScopeArgs,
|
|
943
|
+
text: v.string(),
|
|
944
|
+
kind: v.optional(v.string()),
|
|
945
|
+
tags: v.optional(v.array(v.string())),
|
|
946
|
+
sourceType: v.optional(v.string()),
|
|
947
|
+
userId: v.string(),
|
|
948
|
+
beliefNodeId: v.id("epistemicNodes"),
|
|
949
|
+
relation: v.union(v.literal("supports"), v.literal("contradicts")),
|
|
950
|
+
confidence: v.optional(v.number())
|
|
951
|
+
},
|
|
952
|
+
returns: permissiveReturn,
|
|
953
|
+
handler: async (ctx, args) => {
|
|
954
|
+
const scope = await resolveEvidenceScopeOrNull(ctx, args);
|
|
955
|
+
if (!scope) {
|
|
956
|
+
throw new Error("Invalid scope: projectId or topicId is required");
|
|
957
|
+
}
|
|
958
|
+
await requireProjectAccess(ctx, String(scope.topicId), args.userId);
|
|
959
|
+
const now = Date.now();
|
|
960
|
+
const globalId = generateGlobalId();
|
|
961
|
+
const contentHash = generateContentHash(args.text);
|
|
962
|
+
const kind = normalizeKind(args.kind);
|
|
963
|
+
const sourceType = normalizeSourceType(args.sourceType);
|
|
964
|
+
const confidence = args.confidence ?? 0.7;
|
|
965
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
966
|
+
globalId,
|
|
967
|
+
topicId: scope.topicId,
|
|
968
|
+
projectId: scope.projectId,
|
|
969
|
+
tenantId: scope.tenantId,
|
|
970
|
+
workspaceId: scope.workspaceId,
|
|
971
|
+
nodeType: "evidence",
|
|
972
|
+
canonicalText: args.text,
|
|
973
|
+
contentHash,
|
|
974
|
+
status: "active",
|
|
975
|
+
epistemicLayer: "L2",
|
|
976
|
+
sourceType,
|
|
977
|
+
createdAt: now,
|
|
978
|
+
updatedAt: now,
|
|
979
|
+
createdBy: args.userId,
|
|
980
|
+
metadata: {
|
|
981
|
+
kind,
|
|
982
|
+
tags: args.tags || [],
|
|
983
|
+
linkedBeliefNodeId: args.beliefNodeId,
|
|
984
|
+
evidenceRelation: args.relation,
|
|
985
|
+
confidence
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
989
|
+
nodeId,
|
|
990
|
+
operation: "upsert"
|
|
991
|
+
});
|
|
992
|
+
const beliefNode = await ctx.db.get(args.beliefNodeId);
|
|
993
|
+
if (!beliefNode) {
|
|
994
|
+
throw new Error("Belief node not found for edge creation");
|
|
995
|
+
}
|
|
996
|
+
const weight = args.relation === "supports" ? confidence : -confidence;
|
|
997
|
+
const edgeGlobalId = crypto.randomUUID();
|
|
998
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
999
|
+
globalId: edgeGlobalId,
|
|
1000
|
+
fromGlobalId: globalId,
|
|
1001
|
+
toGlobalId: beliefNode.globalId,
|
|
1002
|
+
edgeType: "informs",
|
|
1003
|
+
weight,
|
|
1004
|
+
createdBy: args.userId,
|
|
1005
|
+
topicId: scope.projectId,
|
|
1006
|
+
fromNodeType: "evidence",
|
|
1007
|
+
toNodeType: "belief",
|
|
1008
|
+
fromLayer: "L2",
|
|
1009
|
+
toLayer: "L3",
|
|
1010
|
+
metadata: {
|
|
1011
|
+
relation: args.relation,
|
|
1012
|
+
confidence
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
1016
|
+
return { nodeId, edgeGlobalId };
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
var updateStatus = mutation({
|
|
1020
|
+
args: {
|
|
1021
|
+
nodeId: v.id("epistemicNodes"),
|
|
1022
|
+
status: v.union(
|
|
1023
|
+
v.literal("active"),
|
|
1024
|
+
v.literal("archived"),
|
|
1025
|
+
v.literal("acted_on")
|
|
1026
|
+
),
|
|
1027
|
+
userId: v.string()
|
|
1028
|
+
},
|
|
1029
|
+
returns: permissiveReturn,
|
|
1030
|
+
handler: async (ctx, args) => {
|
|
1031
|
+
const node = await ctx.db.get(args.nodeId);
|
|
1032
|
+
if (!node || node.nodeType !== "evidence") {
|
|
1033
|
+
throw new Error("Evidence not found");
|
|
1034
|
+
}
|
|
1035
|
+
const now = Date.now();
|
|
1036
|
+
await ctx.db.patch(args.nodeId, {
|
|
1037
|
+
status: args.status,
|
|
1038
|
+
updatedAt: now
|
|
1039
|
+
});
|
|
1040
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1041
|
+
entityType: "evidence",
|
|
1042
|
+
entityId: args.nodeId,
|
|
1043
|
+
changeType: "status_changed",
|
|
1044
|
+
changedAt: now,
|
|
1045
|
+
changedBy: args.userId,
|
|
1046
|
+
isAgent: false,
|
|
1047
|
+
projectId: node.projectId,
|
|
1048
|
+
previousState: { status: node.status },
|
|
1049
|
+
newState: { status: args.status }
|
|
1050
|
+
});
|
|
1051
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1052
|
+
nodeId: args.nodeId,
|
|
1053
|
+
operation: "upsert"
|
|
1054
|
+
});
|
|
1055
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
1056
|
+
return { nodeId: args.nodeId };
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
var internalCreate = internalMutation({
|
|
1060
|
+
args: {
|
|
1061
|
+
...optionalEvidenceScopeArgs,
|
|
1062
|
+
text: v.string(),
|
|
1063
|
+
title: v.optional(v.string()),
|
|
1064
|
+
content: v.optional(v.string()),
|
|
1065
|
+
contentType: v.optional(v.string()),
|
|
1066
|
+
kind: v.optional(v.string()),
|
|
1067
|
+
tags: v.optional(v.array(v.string())),
|
|
1068
|
+
sourceType: v.optional(v.string()),
|
|
1069
|
+
externalSourceType: v.optional(v.string()),
|
|
1070
|
+
sourceUrl: v.optional(v.string()),
|
|
1071
|
+
sourceQuestionId: v.optional(v.string()),
|
|
1072
|
+
userId: v.string(),
|
|
1073
|
+
rationale: v.string(),
|
|
1074
|
+
linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
|
|
1075
|
+
evidenceRelation: v.optional(v.string()),
|
|
1076
|
+
confidence: v.optional(v.number()),
|
|
1077
|
+
metadata: v.optional(v.any()),
|
|
1078
|
+
runtimeToolName: v.optional(v.string()),
|
|
1079
|
+
runtimePackKey: v.optional(v.string()),
|
|
1080
|
+
runtimePackInstallScope: v.optional(
|
|
1081
|
+
v.union(v.literal("tenant"), v.literal("workspace"))
|
|
1082
|
+
)
|
|
1083
|
+
},
|
|
1084
|
+
returns: permissiveReturn,
|
|
1085
|
+
handler: async (ctx, args) => {
|
|
1086
|
+
const now = Date.now();
|
|
1087
|
+
const scope = await resolveEvidenceScopeOrNull(ctx, args);
|
|
1088
|
+
if (!scope) {
|
|
1089
|
+
throw new Error("Invalid scope: projectId or topicId is required");
|
|
1090
|
+
}
|
|
1091
|
+
assertWorkspaceScopedEpistemicNodeScope({
|
|
1092
|
+
scope,
|
|
1093
|
+
nodeType: "evidence",
|
|
1094
|
+
mutationName: "epistemicEvidence.internalCreate"
|
|
1095
|
+
});
|
|
1096
|
+
assertTenantPackWorkspaceMutationAllowed({
|
|
1097
|
+
runtime: resolveRuntimePackMutationContext(args),
|
|
1098
|
+
target: {
|
|
1099
|
+
tenantId: scope.tenantId,
|
|
1100
|
+
workspaceId: scope.workspaceId,
|
|
1101
|
+
nodeType: "evidence",
|
|
1102
|
+
epistemicLayer: "L2"
|
|
1103
|
+
},
|
|
1104
|
+
mutationName: "epistemicEvidence.internalCreate"
|
|
1105
|
+
});
|
|
1106
|
+
const globalId = generateGlobalId();
|
|
1107
|
+
const contentHash = generateContentHash(args.text);
|
|
1108
|
+
const kind = normalizeKind(args.kind);
|
|
1109
|
+
const sourceType = normalizeSourceType(args.sourceType);
|
|
1110
|
+
const additionalMetadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
|
|
1111
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
1112
|
+
globalId,
|
|
1113
|
+
topicId: scope.topicId,
|
|
1114
|
+
projectId: scope.projectId,
|
|
1115
|
+
tenantId: scope.tenantId,
|
|
1116
|
+
workspaceId: scope.workspaceId,
|
|
1117
|
+
nodeType: "evidence",
|
|
1118
|
+
canonicalText: args.text,
|
|
1119
|
+
contentHash,
|
|
1120
|
+
...typeof args.title === "string" && args.title.trim().length > 0 ? { title: args.title.trim() } : {},
|
|
1121
|
+
...typeof args.content === "string" && args.content.length > 0 ? { content: args.content } : {},
|
|
1122
|
+
...typeof args.contentType === "string" && args.contentType.trim().length > 0 ? { contentType: args.contentType.trim() } : {},
|
|
1123
|
+
status: "active",
|
|
1124
|
+
epistemicLayer: "L2",
|
|
1125
|
+
sourceType,
|
|
1126
|
+
createdAt: now,
|
|
1127
|
+
updatedAt: now,
|
|
1128
|
+
createdBy: args.userId,
|
|
1129
|
+
metadata: {
|
|
1130
|
+
kind,
|
|
1131
|
+
tags: args.tags || [],
|
|
1132
|
+
externalSourceType: args.externalSourceType,
|
|
1133
|
+
sourceUrl: args.sourceUrl,
|
|
1134
|
+
sourceQuestionId: args.sourceQuestionId,
|
|
1135
|
+
rationale: args.rationale,
|
|
1136
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
1137
|
+
evidenceRelation: args.evidenceRelation,
|
|
1138
|
+
confidence: args.confidence,
|
|
1139
|
+
...additionalMetadata
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1143
|
+
entityType: "evidence",
|
|
1144
|
+
entityId: String(nodeId),
|
|
1145
|
+
changeType: "created",
|
|
1146
|
+
changedAt: now,
|
|
1147
|
+
changedBy: args.userId,
|
|
1148
|
+
isAgent: false,
|
|
1149
|
+
projectId: scope.projectId,
|
|
1150
|
+
rationale: args.rationale,
|
|
1151
|
+
newState: {
|
|
1152
|
+
text: args.text.slice(0, 200),
|
|
1153
|
+
kind,
|
|
1154
|
+
sourceType,
|
|
1155
|
+
externalSourceType: args.externalSourceType,
|
|
1156
|
+
sourceUrl: args.sourceUrl,
|
|
1157
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
1158
|
+
evidenceRelation: args.evidenceRelation,
|
|
1159
|
+
confidence: args.confidence
|
|
1160
|
+
},
|
|
1161
|
+
triggeringAction: "epistemicEvidence.internalCreate"
|
|
1162
|
+
});
|
|
1163
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1164
|
+
nodeId,
|
|
1165
|
+
operation: "upsert"
|
|
1166
|
+
});
|
|
1167
|
+
if (args.linkedBeliefNodeId && args.evidenceRelation) {
|
|
1168
|
+
const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
|
|
1169
|
+
if (beliefNode) {
|
|
1170
|
+
const confidence = args.confidence ?? 0.7;
|
|
1171
|
+
const weight = args.evidenceRelation === "supports" ? confidence : -confidence;
|
|
1172
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
1173
|
+
globalId: crypto.randomUUID(),
|
|
1174
|
+
fromGlobalId: globalId,
|
|
1175
|
+
toGlobalId: beliefNode.globalId,
|
|
1176
|
+
edgeType: "informs",
|
|
1177
|
+
weight,
|
|
1178
|
+
createdBy: args.userId,
|
|
1179
|
+
topicId: scope.projectId ? String(scope.projectId) : void 0,
|
|
1180
|
+
fromNodeType: "evidence",
|
|
1181
|
+
toNodeType: "belief",
|
|
1182
|
+
fromLayer: "L2",
|
|
1183
|
+
toLayer: "L3",
|
|
1184
|
+
metadata: {
|
|
1185
|
+
relation: args.evidenceRelation,
|
|
1186
|
+
confidence
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
if (scope.projectId || scope.topicId) {
|
|
1192
|
+
await ctx.scheduler.runAfter(
|
|
1193
|
+
0,
|
|
1194
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
1195
|
+
{
|
|
1196
|
+
nodeId,
|
|
1197
|
+
projectId: scope.projectId,
|
|
1198
|
+
topicId: scope.topicId ? String(scope.topicId) : void 0,
|
|
1199
|
+
createdBy: args.userId,
|
|
1200
|
+
nodeType: "evidence",
|
|
1201
|
+
text: args.text
|
|
1202
|
+
}
|
|
1203
|
+
);
|
|
1204
|
+
}
|
|
1205
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
1206
|
+
return { nodeId };
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
var updateVerificationStatus = mutation({
|
|
1210
|
+
args: {
|
|
1211
|
+
nodeId: v.id("epistemicNodes"),
|
|
1212
|
+
verificationHash: v.string(),
|
|
1213
|
+
verificationStatus: v.string(),
|
|
1214
|
+
lastVerificationId: v.optional(v.id("verificationResults"))
|
|
1215
|
+
},
|
|
1216
|
+
returns: permissiveReturn,
|
|
1217
|
+
handler: async (ctx, args) => {
|
|
1218
|
+
const node = await ctx.db.get(args.nodeId);
|
|
1219
|
+
if (!node || node.nodeType !== "evidence") {
|
|
1220
|
+
throw new Error("Evidence node not found");
|
|
1221
|
+
}
|
|
1222
|
+
const metadata = node.metadata || {};
|
|
1223
|
+
await ctx.db.patch(args.nodeId, {
|
|
1224
|
+
metadata: {
|
|
1225
|
+
...metadata,
|
|
1226
|
+
verificationHash: args.verificationHash,
|
|
1227
|
+
verificationStatus: args.verificationStatus,
|
|
1228
|
+
lastVerificationId: args.lastVerificationId
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
1232
|
+
return { success: true };
|
|
1233
|
+
}
|
|
1234
|
+
});
|
|
1235
|
+
var update = mutation({
|
|
1236
|
+
args: {
|
|
1237
|
+
nodeId: v.optional(v.id("epistemicNodes")),
|
|
1238
|
+
insightId: v.optional(v.string()),
|
|
1239
|
+
text: v.optional(v.string()),
|
|
1240
|
+
kind: v.optional(v.string()),
|
|
1241
|
+
tags: v.optional(v.array(v.string())),
|
|
1242
|
+
userId: v.string(),
|
|
1243
|
+
externalSourceUrl: v.optional(v.string()),
|
|
1244
|
+
verificationStatus: v.optional(v.string())
|
|
1245
|
+
},
|
|
1246
|
+
returns: permissiveReturn,
|
|
1247
|
+
handler: async (ctx, args) => {
|
|
1248
|
+
const resolvedId = args.nodeId ?? args.insightId;
|
|
1249
|
+
if (!resolvedId) {
|
|
1250
|
+
throw new Error("Either nodeId or insightId is required");
|
|
1251
|
+
}
|
|
1252
|
+
const node = await ctx.db.get(resolvedId);
|
|
1253
|
+
if (!node || node.nodeType !== "evidence") {
|
|
1254
|
+
throw new Error("Evidence node not found");
|
|
1255
|
+
}
|
|
1256
|
+
if (!node.projectId) {
|
|
1257
|
+
throw new Error("Evidence has no project scope");
|
|
1258
|
+
}
|
|
1259
|
+
await checkProjectAccess(ctx, node.projectId, args.userId);
|
|
1260
|
+
const now = Date.now();
|
|
1261
|
+
const existingMeta = node.metadata || {};
|
|
1262
|
+
const metaUpdates = { ...existingMeta };
|
|
1263
|
+
if (args.kind !== void 0) {
|
|
1264
|
+
metaUpdates.kind = args.kind;
|
|
1265
|
+
}
|
|
1266
|
+
if (args.tags !== void 0) {
|
|
1267
|
+
metaUpdates.tags = args.tags;
|
|
1268
|
+
}
|
|
1269
|
+
if (args.externalSourceUrl !== void 0) {
|
|
1270
|
+
metaUpdates.externalSourceUrl = args.externalSourceUrl;
|
|
1271
|
+
}
|
|
1272
|
+
await ctx.db.patch(resolvedId, {
|
|
1273
|
+
canonicalText: args.text ?? node.canonicalText,
|
|
1274
|
+
metadata: metaUpdates,
|
|
1275
|
+
updatedAt: now
|
|
1276
|
+
});
|
|
1277
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1278
|
+
nodeId: resolvedId,
|
|
1279
|
+
operation: "upsert"
|
|
1280
|
+
});
|
|
1281
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1282
|
+
entityType: "evidence",
|
|
1283
|
+
entityId: resolvedId,
|
|
1284
|
+
changeType: "updated",
|
|
1285
|
+
changedAt: now,
|
|
1286
|
+
changedBy: args.userId,
|
|
1287
|
+
isAgent: false,
|
|
1288
|
+
projectId: node.projectId,
|
|
1289
|
+
previousState: { text: node.canonicalText?.slice(0, 200) },
|
|
1290
|
+
newState: { text: (args.text ?? node.canonicalText)?.slice(0, 200) }
|
|
1291
|
+
});
|
|
1292
|
+
if (args.text !== void 0) {
|
|
1293
|
+
await ctx.scheduler.runAfter(
|
|
1294
|
+
0,
|
|
1295
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
1296
|
+
{
|
|
1297
|
+
nodeId: resolvedId,
|
|
1298
|
+
topicId: node.projectId,
|
|
1299
|
+
createdBy: node.createdBy,
|
|
1300
|
+
nodeType: "evidence",
|
|
1301
|
+
text: args.text
|
|
1302
|
+
}
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
1306
|
+
return { nodeId: resolvedId };
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
var flagAsIncorrect = mutation({
|
|
1310
|
+
args: {
|
|
1311
|
+
insightId: v.id("epistemicNodes"),
|
|
1312
|
+
reason: v.string(),
|
|
1313
|
+
suggestedCorrection: v.optional(v.string()),
|
|
1314
|
+
userId: v.string()
|
|
1315
|
+
},
|
|
1316
|
+
returns: permissiveReturn,
|
|
1317
|
+
handler: async (ctx, args) => {
|
|
1318
|
+
const now = Date.now();
|
|
1319
|
+
const node = await ctx.db.get(args.insightId);
|
|
1320
|
+
if (!node || node.nodeType !== "evidence") {
|
|
1321
|
+
throw new Error("Evidence not found in epistemic spine");
|
|
1322
|
+
}
|
|
1323
|
+
if (!node.projectId) {
|
|
1324
|
+
throw new Error("Evidence has no project scope");
|
|
1325
|
+
}
|
|
1326
|
+
await checkProjectAccess(ctx, node.projectId, args.userId);
|
|
1327
|
+
const existingMeta = node.metadata || {};
|
|
1328
|
+
await ctx.db.patch(node._id, {
|
|
1329
|
+
verificationStatus: "contradicted",
|
|
1330
|
+
metadata: {
|
|
1331
|
+
...existingMeta,
|
|
1332
|
+
contradictionReason: args.reason,
|
|
1333
|
+
suggestedCorrection: args.suggestedCorrection,
|
|
1334
|
+
contradictedBy: args.userId,
|
|
1335
|
+
contradictedAt: now
|
|
1336
|
+
},
|
|
1337
|
+
updatedAt: now
|
|
1338
|
+
});
|
|
1339
|
+
await ctx.db.insert("verificationResults", {
|
|
1340
|
+
targetType: "insight",
|
|
1341
|
+
targetId: node._id,
|
|
1342
|
+
claimText: node.canonicalText || "Evidence text not available",
|
|
1343
|
+
verdict: "contradicted",
|
|
1344
|
+
confidence: 1,
|
|
1345
|
+
sources: [],
|
|
1346
|
+
reasoning: args.reason,
|
|
1347
|
+
suggestedRevision: args.suggestedCorrection,
|
|
1348
|
+
caveats: [],
|
|
1349
|
+
mode: "deep_verify",
|
|
1350
|
+
verifiedAt: now,
|
|
1351
|
+
durationMs: 0,
|
|
1352
|
+
projectId: node.projectId,
|
|
1353
|
+
userId: args.userId
|
|
1354
|
+
});
|
|
1355
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1356
|
+
entityType: "evidence",
|
|
1357
|
+
entityId: node._id,
|
|
1358
|
+
changeType: "updated",
|
|
1359
|
+
changedAt: now,
|
|
1360
|
+
changedBy: args.userId,
|
|
1361
|
+
isAgent: false,
|
|
1362
|
+
projectId: node.projectId,
|
|
1363
|
+
newState: {
|
|
1364
|
+
reason: args.reason,
|
|
1365
|
+
suggestedCorrection: args.suggestedCorrection
|
|
1366
|
+
}
|
|
1367
|
+
});
|
|
1368
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
1369
|
+
console.log(
|
|
1370
|
+
`[EpistemicEvidence] Evidence flagged as incorrect by ${args.userId}: "${args.insightId.slice(0, 20)}..."`
|
|
1371
|
+
);
|
|
1372
|
+
return { success: true };
|
|
1373
|
+
}
|
|
1374
|
+
});
|
|
1375
|
+
var remove = mutation({
|
|
1376
|
+
args: {
|
|
1377
|
+
nodeId: v.optional(v.id("epistemicNodes")),
|
|
1378
|
+
insightId: v.optional(v.string()),
|
|
1379
|
+
userId: v.string()
|
|
1380
|
+
},
|
|
1381
|
+
returns: permissiveReturn,
|
|
1382
|
+
handler: async (ctx, args) => {
|
|
1383
|
+
const resolvedId = args.nodeId ?? args.insightId;
|
|
1384
|
+
if (!resolvedId) {
|
|
1385
|
+
throw new Error("Either nodeId or insightId is required");
|
|
1386
|
+
}
|
|
1387
|
+
const node = await ctx.db.get(resolvedId);
|
|
1388
|
+
if (!node || node.nodeType !== "evidence") {
|
|
1389
|
+
throw new Error("Evidence node not found");
|
|
1390
|
+
}
|
|
1391
|
+
if (node.createdBy !== args.userId) {
|
|
1392
|
+
throw new Error("Only the creator can archive this evidence");
|
|
1393
|
+
}
|
|
1394
|
+
const now = Date.now();
|
|
1395
|
+
await ctx.db.patch(resolvedId, {
|
|
1396
|
+
status: "archived",
|
|
1397
|
+
updatedAt: now
|
|
1398
|
+
});
|
|
1399
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
1400
|
+
nodeId: resolvedId,
|
|
1401
|
+
operation: "upsert"
|
|
1402
|
+
});
|
|
1403
|
+
await ctx.db.insert("epistemicAudit", {
|
|
1404
|
+
entityType: "evidence",
|
|
1405
|
+
entityId: resolvedId,
|
|
1406
|
+
changeType: "archived",
|
|
1407
|
+
changedAt: now,
|
|
1408
|
+
changedBy: args.userId,
|
|
1409
|
+
isAgent: false,
|
|
1410
|
+
projectId: node.projectId,
|
|
1411
|
+
previousState: { status: node.status },
|
|
1412
|
+
newState: { status: "archived" }
|
|
1413
|
+
});
|
|
1414
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
1415
|
+
return { nodeId: resolvedId };
|
|
1416
|
+
}
|
|
1417
|
+
});
|
|
1418
|
+
|
|
1419
|
+
export { create, createAndLink, flagAsIncorrect, internalCreate, remove, update, updateStatus, updateVerificationStatus };
|
|
1420
|
+
//# sourceMappingURL=epistemicEvidenceMutations.js.map
|
|
1421
|
+
//# sourceMappingURL=epistemicEvidenceMutations.js.map
|