@lucern/graph-primitives 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/dist/beliefDecay-Q_26RTc-.d.ts +72 -0
- package/dist/beliefDecay.d.ts +2 -0
- package/dist/beliefDecay.js +1628 -0
- package/dist/beliefDecay.js.map +1 -0
- package/dist/beliefEvidenceLinks-42FlR48t.d.ts +77 -0
- package/dist/beliefEvidenceLinks.d.ts +1 -0
- package/dist/beliefEvidenceLinks.js +1978 -0
- package/dist/beliefEvidenceLinks.js.map +1 -0
- package/dist/beliefLifecycle-C-AehZgF.d.ts +43 -0
- package/dist/beliefLifecycle.d.ts +1 -0
- package/dist/beliefLifecycle.js +98 -0
- package/dist/beliefLifecycle.js.map +1 -0
- package/dist/confidencePropagationDispatch.d.ts +46 -0
- package/dist/confidencePropagationDispatch.js +744 -0
- package/dist/confidencePropagationDispatch.js.map +1 -0
- package/dist/contradictions-Hdwl7zid.d.ts +71 -0
- package/dist/contradictions.d.ts +1 -0
- package/dist/contradictions.js +1557 -0
- package/dist/contradictions.js.map +1 -0
- package/dist/convex.d.ts +23 -0
- package/dist/convex.js +17 -0
- package/dist/convex.js.map +1 -0
- package/dist/edgeValidation-CeI0wc0r.d.ts +35 -0
- package/dist/edgeValidation.d.ts +2 -0
- package/dist/edgeValidation.js +307 -0
- package/dist/edgeValidation.js.map +1 -0
- package/dist/edges/contains.d.ts +6 -0
- package/dist/edges/contains.js +14 -0
- package/dist/edges/contains.js.map +1 -0
- package/dist/edges/contradicts.d.ts +6 -0
- package/dist/edges/contradicts.js +183 -0
- package/dist/edges/contradicts.js.map +1 -0
- package/dist/edges/dependsOn.d.ts +6 -0
- package/dist/edges/dependsOn.js +240 -0
- package/dist/edges/dependsOn.js.map +1 -0
- package/dist/edges/derivedFrom.d.ts +6 -0
- package/dist/edges/derivedFrom.js +14 -0
- package/dist/edges/derivedFrom.js.map +1 -0
- package/dist/edges/elaborates.d.ts +6 -0
- package/dist/edges/elaborates.js +100 -0
- package/dist/edges/elaborates.js.map +1 -0
- package/dist/edges/index.d.ts +3 -0
- package/dist/edges/index.js +556 -0
- package/dist/edges/index.js.map +1 -0
- package/dist/edges/informs.d.ts +6 -0
- package/dist/edges/informs.js +112 -0
- package/dist/edges/informs.js.map +1 -0
- package/dist/edges/propagationTypes.d.ts +39 -0
- package/dist/edges/propagationTypes.js +17 -0
- package/dist/edges/propagationTypes.js.map +1 -0
- package/dist/edges/refutes.d.ts +6 -0
- package/dist/edges/refutes.js +108 -0
- package/dist/edges/refutes.js.map +1 -0
- package/dist/edges/supports.d.ts +6 -0
- package/dist/edges/supports.js +193 -0
- package/dist/edges/supports.js.map +1 -0
- package/dist/edges/tests.d.ts +6 -0
- package/dist/edges/tests.js +14 -0
- package/dist/edges/tests.js.map +1 -0
- package/dist/edges/utils.d.ts +12 -0
- package/dist/edges/utils.js +188 -0
- package/dist/edges/utils.js.map +1 -0
- package/dist/embeddingTrigger.d.ts +24 -0
- package/dist/embeddingTrigger.js +24 -0
- package/dist/embeddingTrigger.js.map +1 -0
- package/dist/entityBridge-DMaKooYn.d.ts +59 -0
- package/dist/entityBridge.d.ts +1 -0
- package/dist/entityBridge.js +663 -0
- package/dist/entityBridge.js.map +1 -0
- package/dist/entityLifecycle-BkhRJ-XI.d.ts +69 -0
- package/dist/entityLifecycle.d.ts +1 -0
- package/dist/entityLifecycle.js +2083 -0
- package/dist/entityLifecycle.js.map +1 -0
- package/dist/entityValidation-KLZ_Xl2D.d.ts +50 -0
- package/dist/entityValidation.d.ts +3 -0
- package/dist/entityValidation.js +71 -0
- package/dist/entityValidation.js.map +1 -0
- package/dist/epistemicAnswers-DSP1slZ9.d.ts +67 -0
- package/dist/epistemicAnswers.d.ts +1 -0
- package/dist/epistemicAnswers.js +1650 -0
- package/dist/epistemicAnswers.js.map +1 -0
- package/dist/epistemicBeliefs-DtFVTp-k.d.ts +377 -0
- package/dist/epistemicBeliefs.d.ts +5 -0
- package/dist/epistemicBeliefs.js +6386 -0
- package/dist/epistemicBeliefs.js.map +1 -0
- package/dist/epistemicContractHelpers.d.ts +1 -0
- package/dist/epistemicContractHelpers.js +320 -0
- package/dist/epistemicContractHelpers.js.map +1 -0
- package/dist/epistemicContracts.d.ts +77 -0
- package/dist/epistemicContracts.js +8436 -0
- package/dist/epistemicContracts.js.map +1 -0
- package/dist/epistemicEdges-DcA8ErUG.d.ts +191 -0
- package/dist/epistemicEdges.d.ts +2 -0
- package/dist/epistemicEdges.js +2749 -0
- package/dist/epistemicEdges.js.map +1 -0
- package/dist/epistemicEvidence-Bo638XDP.d.ts +128 -0
- package/dist/epistemicEvidence.d.ts +3 -0
- package/dist/epistemicEvidence.js +3282 -0
- package/dist/epistemicEvidence.js.map +1 -0
- package/dist/epistemicHelpers-Bd9xbaib.d.ts +329 -0
- package/dist/epistemicHelpers.d.ts +4 -0
- package/dist/epistemicHelpers.js +999 -0
- package/dist/epistemicHelpers.js.map +1 -0
- package/dist/epistemicLinking-CyeLOIzN.d.ts +35 -0
- package/dist/epistemicLinking.d.ts +1 -0
- package/dist/epistemicLinking.js +1391 -0
- package/dist/epistemicLinking.js.map +1 -0
- package/dist/epistemicNodes-BpD6Koud.d.ts +167 -0
- package/dist/epistemicNodes.d.ts +2 -0
- package/dist/epistemicNodes.js +2942 -0
- package/dist/epistemicNodes.js.map +1 -0
- package/dist/epistemicQuestions-CmEeY6zQ.d.ts +214 -0
- package/dist/epistemicQuestions.d.ts +3 -0
- package/dist/epistemicQuestions.js +4993 -0
- package/dist/epistemicQuestions.js.map +1 -0
- package/dist/epistemicSources-ZazxHOK1.d.ts +25 -0
- package/dist/epistemicSources.d.ts +1 -0
- package/dist/epistemicSources.js +2025 -0
- package/dist/epistemicSources.js.map +1 -0
- package/dist/evaluators/index.d.ts +9 -0
- package/dist/evaluators/index.js +8440 -0
- package/dist/evaluators/index.js.map +1 -0
- package/dist/evaluators/lintCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/lintCheckerEvaluator.js +155 -0
- package/dist/evaluators/lintCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/sentryCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/sentryCheckerEvaluator.js +126 -0
- package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/shared.d.ts +27 -0
- package/dist/evaluators/shared.js +92 -0
- package/dist/evaluators/shared.js.map +1 -0
- package/dist/evaluators/testRunnerEvaluator.d.ts +17 -0
- package/dist/evaluators/testRunnerEvaluator.js +232 -0
- package/dist/evaluators/testRunnerEvaluator.js.map +1 -0
- package/dist/evaluators/tscCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/tscCheckerEvaluator.js +189 -0
- package/dist/evaluators/tscCheckerEvaluator.js.map +1 -0
- package/dist/globalId-DKh9d_uD.d.ts +20 -0
- package/dist/globalId.d.ts +1 -0
- package/dist/globalId.js +15 -0
- package/dist/globalId.js.map +1 -0
- package/dist/graphTypes-CpgIuCdo.d.ts +52 -0
- package/dist/graphTypes.d.ts +1 -0
- package/dist/graphTypes.js +120 -0
- package/dist/graphTypes.js.map +1 -0
- package/dist/helpers-BYHIk5vU.d.ts +27 -0
- package/dist/helpers.d.ts +4 -0
- package/dist/helpers.js +313 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index-Dct1T70K.d.ts +25 -0
- package/dist/index-Dq-7R-gi.d.ts +31 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +22294 -0
- package/dist/index.js.map +1 -0
- package/dist/invariantEnforcement.d.ts +52 -0
- package/dist/invariantEnforcement.js +231 -0
- package/dist/invariantEnforcement.js.map +1 -0
- package/dist/logicalRoleInference-CJxqWi3u.d.ts +16 -0
- package/dist/logicalRoleInference.d.ts +3 -0
- package/dist/logicalRoleInference.js +64 -0
- package/dist/logicalRoleInference.js.map +1 -0
- package/dist/matcherFeedbackUtils.d.ts +33 -0
- package/dist/matcherFeedbackUtils.js +95 -0
- package/dist/matcherFeedbackUtils.js.map +1 -0
- package/dist/ontology-matching-Buhu23ss.d.ts +48 -0
- package/dist/ontology-matching.d.ts +2 -0
- package/dist/ontology-matching.js +346 -0
- package/dist/ontology-matching.js.map +1 -0
- package/dist/ontologyApproval-Ba0Jjk1k.d.ts +26 -0
- package/dist/ontologyApproval.d.ts +1 -0
- package/dist/ontologyApproval.js +78 -0
- package/dist/ontologyApproval.js.map +1 -0
- package/dist/ontologyDefinitions.d.ts +72 -0
- package/dist/ontologyDefinitions.js +635 -0
- package/dist/ontologyDefinitions.js.map +1 -0
- package/dist/ontologyHelpers.d.ts +79 -0
- package/dist/ontologyHelpers.js +81 -0
- package/dist/ontologyHelpers.js.map +1 -0
- package/dist/ontologyRegistry-B67rPJ16.d.ts +31 -0
- package/dist/ontologyRegistry.d.ts +1 -0
- package/dist/ontologyRegistry.js +296 -0
- package/dist/ontologyRegistry.js.map +1 -0
- package/dist/projectionReconciliation-CxrXYGaB.d.ts +20 -0
- package/dist/projectionReconciliation.d.ts +1 -0
- package/dist/projectionReconciliation.js +261 -0
- package/dist/projectionReconciliation.js.map +1 -0
- package/dist/projectionStaleness-CAdpIsaW.d.ts +51 -0
- package/dist/projectionStaleness.d.ts +1 -0
- package/dist/projectionStaleness.js +57 -0
- package/dist/projectionStaleness.js.map +1 -0
- package/dist/questionEvidenceLinks-BdQD0TkM.d.ts +34 -0
- package/dist/questionEvidenceLinks.d.ts +1 -0
- package/dist/questionEvidenceLinks.js +1690 -0
- package/dist/questionEvidenceLinks.js.map +1 -0
- package/dist/resolverTypes-CC8Ea2E2.d.ts +20 -0
- package/dist/resolverTypes.d.ts +4 -0
- package/dist/resolverTypes.js +3 -0
- package/dist/resolverTypes.js.map +1 -0
- package/dist/resolvers-Br1a6eLV.d.ts +14 -0
- package/dist/resolvers.d.ts +5 -0
- package/dist/resolvers.js +308 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/scopeResolverCompat.d.ts +26 -0
- package/dist/scopeResolverCompat.js +242 -0
- package/dist/scopeResolverCompat.js.map +1 -0
- package/dist/text-matching-CMn2WnVD.d.ts +40 -0
- package/dist/text-matching.d.ts +2 -0
- package/dist/text-matching.js +246 -0
- package/dist/text-matching.js.map +1 -0
- package/dist/topicOntologyResolver.d.ts +80 -0
- package/dist/topicOntologyResolver.js +67 -0
- package/dist/topicOntologyResolver.js.map +1 -0
- package/dist/topicProjectOverlay.d.ts +92 -0
- package/dist/topicProjectOverlay.js +249 -0
- package/dist/topicProjectOverlay.js.map +1 -0
- package/dist/topicScope-By_zp4tt.d.ts +34 -0
- package/dist/topicScope.d.ts +3 -0
- package/dist/topicScope.js +206 -0
- package/dist/topicScope.js.map +1 -0
- package/dist/workspaceIsolation.d.ts +44 -0
- package/dist/workspaceIsolation.js +950 -0
- package/dist/workspaceIsolation.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,3282 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { componentsGeneric, defineTable, mutationGeneric, anyApi, queryGeneric, internalQueryGeneric, internalMutationGeneric } from 'convex/server';
|
|
3
|
+
|
|
4
|
+
// src/epistemicEvidence.ts
|
|
5
|
+
var api = anyApi;
|
|
6
|
+
componentsGeneric();
|
|
7
|
+
|
|
8
|
+
// ../access-control/src/topicProjectOverlay.ts
|
|
9
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
10
|
+
function readNonEmptyString(value) {
|
|
11
|
+
if (typeof value !== "string") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const normalized = value.trim();
|
|
15
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
16
|
+
}
|
|
17
|
+
function readStringArray(value) {
|
|
18
|
+
if (!Array.isArray(value)) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
|
|
22
|
+
}
|
|
23
|
+
function readMetadata(topic) {
|
|
24
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
25
|
+
}
|
|
26
|
+
function readLegacyProjectId(value) {
|
|
27
|
+
if (!value) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
|
|
31
|
+
}
|
|
32
|
+
function coerceVisibility(value) {
|
|
33
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
34
|
+
}
|
|
35
|
+
function coerceStatus(value) {
|
|
36
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
37
|
+
}
|
|
38
|
+
function mapProjectType(topic, metadata) {
|
|
39
|
+
const explicit = readNonEmptyString(metadata.projectType);
|
|
40
|
+
if (explicit) {
|
|
41
|
+
return explicit;
|
|
42
|
+
}
|
|
43
|
+
if (topic.type === "theme") {
|
|
44
|
+
return "thematic";
|
|
45
|
+
}
|
|
46
|
+
return readNonEmptyString(topic.type) || "general";
|
|
47
|
+
}
|
|
48
|
+
function isProjectLikeTopic(topic) {
|
|
49
|
+
const metadata = readMetadata(topic);
|
|
50
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
|
|
51
|
+
}
|
|
52
|
+
async function resolveTopicDoc(ctx, scopeId) {
|
|
53
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
54
|
+
try {
|
|
55
|
+
const directTopic = await ctx.db.get(scopeId);
|
|
56
|
+
if (directTopic) {
|
|
57
|
+
return directTopic;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (typeof ctx.runQuery !== "function") {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
const topic = await ctx.runQuery(api.topics.get, {
|
|
67
|
+
id: String(scopeId)
|
|
68
|
+
});
|
|
69
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
70
|
+
return topic;
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const topic = await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
76
|
+
projectId: String(scopeId)
|
|
77
|
+
});
|
|
78
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
79
|
+
return topic;
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
function materializeTopicProjectOverlay(topic, idMode = "legacy") {
|
|
86
|
+
const metadata = readMetadata(topic);
|
|
87
|
+
const topicId = String(topic._id);
|
|
88
|
+
const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
|
|
89
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
90
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
91
|
+
const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
|
|
92
|
+
const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
|
|
93
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
94
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
95
|
+
return {
|
|
96
|
+
...metadata,
|
|
97
|
+
_id: outwardId,
|
|
98
|
+
projectId: outwardId,
|
|
99
|
+
topicId,
|
|
100
|
+
storageProjectId,
|
|
101
|
+
legacyProjectId,
|
|
102
|
+
name: readNonEmptyString(topic.name) || "Untitled Theme",
|
|
103
|
+
type: mapProjectType(topic, metadata),
|
|
104
|
+
description: readNonEmptyString(topic.description),
|
|
105
|
+
ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
|
|
106
|
+
sharedWith: readStringArray(metadata.sharedWith),
|
|
107
|
+
visibility,
|
|
108
|
+
tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
|
|
109
|
+
workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
|
|
110
|
+
status,
|
|
111
|
+
tags: readStringArray(metadata.tags),
|
|
112
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
113
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
114
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
115
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
116
|
+
createdAt,
|
|
117
|
+
updatedAt
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
|
|
121
|
+
const topic = await resolveTopicDoc(ctx, scopeId);
|
|
122
|
+
if (!topic) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return materializeTopicProjectOverlay(topic, options.idMode);
|
|
129
|
+
}
|
|
130
|
+
async function listTopicProjectOverlays(ctx, options = {}) {
|
|
131
|
+
let allTopics = [];
|
|
132
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
133
|
+
try {
|
|
134
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
135
|
+
} catch {
|
|
136
|
+
allTopics = [];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
140
|
+
allTopics = (await ctx.runQuery(api.topics.list, {}) ?? []) || [];
|
|
141
|
+
}
|
|
142
|
+
return allTopics.filter(
|
|
143
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
|
|
144
|
+
).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ../access-control/src/projectGrantsBridge.ts
|
|
148
|
+
var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
|
|
149
|
+
function normalizeString(value) {
|
|
150
|
+
if (typeof value !== "string") {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const trimmed = value.trim();
|
|
154
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
155
|
+
}
|
|
156
|
+
async function resolveGrantScopeIds(ctx, args) {
|
|
157
|
+
const topicId = normalizeString(args.topicId);
|
|
158
|
+
const projectId = normalizeString(args.projectId);
|
|
159
|
+
for (const scopeId of [topicId, projectId]) {
|
|
160
|
+
if (!scopeId) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
|
|
165
|
+
idMode: "legacy",
|
|
166
|
+
projectLikeOnly: false
|
|
167
|
+
});
|
|
168
|
+
if (overlay) {
|
|
169
|
+
return {
|
|
170
|
+
topicId: normalizeString(overlay.topicId) ?? topicId,
|
|
171
|
+
projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { topicId, projectId };
|
|
178
|
+
}
|
|
179
|
+
async function normalizeProjectGrantRow(ctx, row) {
|
|
180
|
+
const scope = await resolveGrantScopeIds(ctx, {
|
|
181
|
+
topicId: row.topicId,
|
|
182
|
+
projectId: row.projectId
|
|
183
|
+
});
|
|
184
|
+
return {
|
|
185
|
+
...row,
|
|
186
|
+
...scope.topicId ? { topicId: scope.topicId } : {},
|
|
187
|
+
...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
async function normalizeProjectGrantRows(ctx, rows) {
|
|
191
|
+
return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
|
|
192
|
+
}
|
|
193
|
+
async function listProjectGrantsByPrincipal(ctx, principalId) {
|
|
194
|
+
const rows = await Promise.all(
|
|
195
|
+
PROJECT_GRANT_STATUSES.map(
|
|
196
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
197
|
+
"by_principal_status",
|
|
198
|
+
(q) => q.eq("principalId", principalId).eq("status", status)
|
|
199
|
+
).collect()
|
|
200
|
+
)
|
|
201
|
+
);
|
|
202
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
203
|
+
}
|
|
204
|
+
async function listProjectGrantsByGroup(ctx, groupId) {
|
|
205
|
+
const rows = await Promise.all(
|
|
206
|
+
PROJECT_GRANT_STATUSES.map(
|
|
207
|
+
(status) => ctx.db.query("projectGrants").withIndex(
|
|
208
|
+
"by_group_status",
|
|
209
|
+
(q) => q.eq("groupId", groupId).eq("status", status)
|
|
210
|
+
).collect()
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
return await normalizeProjectGrantRows(ctx, rows.flat());
|
|
214
|
+
}
|
|
215
|
+
function buildScopeMatchers(inputScopeId, resolved) {
|
|
216
|
+
return new Set(
|
|
217
|
+
[inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
function matchesResolvedScope(row, scopeIds) {
|
|
221
|
+
const rowTopicId = normalizeString(row.topicId);
|
|
222
|
+
const rowProjectId = normalizeString(row.projectId);
|
|
223
|
+
return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
|
|
224
|
+
}
|
|
225
|
+
async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
|
|
226
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
227
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
228
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
229
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
230
|
+
}
|
|
231
|
+
async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
|
|
232
|
+
const resolved = await resolveGrantScopeIds(ctx, { topicId });
|
|
233
|
+
const scopeIds = buildScopeMatchers(topicId, resolved);
|
|
234
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
235
|
+
return rows.filter((row) => matchesResolvedScope(row, scopeIds));
|
|
236
|
+
}
|
|
237
|
+
async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
238
|
+
const rows = await listProjectGrantsByPrincipal(ctx, principalId);
|
|
239
|
+
return rows.filter((row) => row.status === status);
|
|
240
|
+
}
|
|
241
|
+
async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
242
|
+
const rows = await listProjectGrantsByGroup(ctx, groupId);
|
|
243
|
+
return rows.filter((row) => row.status === status);
|
|
244
|
+
}
|
|
245
|
+
async function bridgeInsertProjectGrant(ctx, value) {
|
|
246
|
+
const resolved = await resolveGrantScopeIds(ctx, value);
|
|
247
|
+
return await ctx.db.insert("projectGrants", {
|
|
248
|
+
...value,
|
|
249
|
+
...resolved.topicId ? { topicId: resolved.topicId } : {},
|
|
250
|
+
...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ../access-control/src/resolvers.ts
|
|
255
|
+
async function findUserByClerkId(ctx, clerkId) {
|
|
256
|
+
const normalizedClerkId = clerkId.trim();
|
|
257
|
+
if (!normalizedClerkId) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
if (typeof ctx.runQuery === "function") {
|
|
261
|
+
try {
|
|
262
|
+
const bridgedUser = await ctx.runQuery(api.users.getUserByClerkId, {
|
|
263
|
+
clerkId: normalizedClerkId
|
|
264
|
+
});
|
|
265
|
+
if (bridgedUser) {
|
|
266
|
+
return bridgedUser;
|
|
267
|
+
}
|
|
268
|
+
} catch {
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
const users = await ctx.db.query("users").collect();
|
|
273
|
+
return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
|
|
274
|
+
} catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async function findUserByPrincipalId(ctx, principalId) {
|
|
279
|
+
const normalizedPrincipalId = principalId.trim();
|
|
280
|
+
if (!normalizedPrincipalId) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const users = await ctx.db.query("users").collect();
|
|
285
|
+
return users.find(
|
|
286
|
+
(user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
|
|
287
|
+
) ?? null;
|
|
288
|
+
} catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function findAgentByPrincipalId(ctx, principalId) {
|
|
293
|
+
const normalizedPrincipalId = principalId.trim();
|
|
294
|
+
if (!normalizedPrincipalId) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
if (typeof ctx.runQuery === "function") {
|
|
298
|
+
try {
|
|
299
|
+
const bridgedAgent = await ctx.runQuery(
|
|
300
|
+
api.agents.getAgentByPrincipalId,
|
|
301
|
+
{
|
|
302
|
+
principalId: normalizedPrincipalId
|
|
303
|
+
}
|
|
304
|
+
);
|
|
305
|
+
if (bridgedAgent) {
|
|
306
|
+
return bridgedAgent;
|
|
307
|
+
}
|
|
308
|
+
} catch {
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const agents = await ctx.db.query("agents").collect();
|
|
313
|
+
return agents.find(
|
|
314
|
+
(agent) => String(agent.principalId ?? "") === normalizedPrincipalId
|
|
315
|
+
) ?? null;
|
|
316
|
+
} catch {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function defaultResolvers() {
|
|
321
|
+
return {
|
|
322
|
+
async getProject(ctx, topicId) {
|
|
323
|
+
return await resolveTopicProjectOverlay(ctx, topicId, {
|
|
324
|
+
idMode: "legacy",
|
|
325
|
+
projectLikeOnly: false
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
async listTopics(ctx) {
|
|
329
|
+
return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
330
|
+
},
|
|
331
|
+
async listTopicsByOwner(ctx, ownerId) {
|
|
332
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
333
|
+
return topics.filter((topic) => topic.ownerId === ownerId);
|
|
334
|
+
},
|
|
335
|
+
async listTopicsByVisibility(ctx, visibility) {
|
|
336
|
+
const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
|
|
337
|
+
return topics.filter((topic) => topic.visibility === visibility);
|
|
338
|
+
},
|
|
339
|
+
async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
|
|
340
|
+
return await bridgeListProjectGrantsByTopicAndPrincipal(
|
|
341
|
+
ctx,
|
|
342
|
+
topicId,
|
|
343
|
+
principalId
|
|
344
|
+
);
|
|
345
|
+
},
|
|
346
|
+
async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
|
|
347
|
+
return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
|
|
348
|
+
},
|
|
349
|
+
async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
|
|
350
|
+
return await bridgeListProjectGrantsByPrincipalStatus(
|
|
351
|
+
ctx,
|
|
352
|
+
principalId,
|
|
353
|
+
status
|
|
354
|
+
);
|
|
355
|
+
},
|
|
356
|
+
async listProjectGrantsByGroupStatus(ctx, groupId, status) {
|
|
357
|
+
return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
|
|
358
|
+
},
|
|
359
|
+
async insertProjectGrant(ctx, value) {
|
|
360
|
+
return await bridgeInsertProjectGrant(ctx, value);
|
|
361
|
+
},
|
|
362
|
+
async getAgentByPrincipalId(ctx, principalId) {
|
|
363
|
+
return await findAgentByPrincipalId(ctx, principalId);
|
|
364
|
+
},
|
|
365
|
+
async getUserByClerkId(ctx, clerkId) {
|
|
366
|
+
return await findUserByClerkId(ctx, clerkId);
|
|
367
|
+
},
|
|
368
|
+
async getUserByPrincipalId(ctx, principalId) {
|
|
369
|
+
return await findUserByPrincipalId(ctx, principalId);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
var resolverOverrides = {};
|
|
374
|
+
function resolveAccessControlAppResolvers(_ctx) {
|
|
375
|
+
return {
|
|
376
|
+
...defaultResolvers(),
|
|
377
|
+
...resolverOverrides
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ../access-control/src/principalContext.ts
|
|
382
|
+
function requireCanonicalResolvedUser(user, clerkId) {
|
|
383
|
+
const resolved = user;
|
|
384
|
+
if (!resolved) {
|
|
385
|
+
throw new Error(
|
|
386
|
+
`[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
|
|
390
|
+
if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
|
|
391
|
+
throw new Error(
|
|
392
|
+
`[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
|
|
401
|
+
throw new Error(
|
|
402
|
+
`[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
|
|
406
|
+
throw new Error(
|
|
407
|
+
`[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
mcRole,
|
|
412
|
+
defaultTenantId: defaultTenantId.trim(),
|
|
413
|
+
defaultWorkspaceId: defaultWorkspaceId.trim(),
|
|
414
|
+
defaultPrincipalId: defaultPrincipalId.trim()
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function isPrincipalIdInput(value) {
|
|
418
|
+
return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
|
|
419
|
+
}
|
|
420
|
+
async function resolveCanonicalUserRecord(ctx, actorId) {
|
|
421
|
+
const normalizedActorId = actorId.trim();
|
|
422
|
+
const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
|
|
423
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
424
|
+
const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
|
|
425
|
+
if (resolvedByClerkId) {
|
|
426
|
+
return {
|
|
427
|
+
resolvedUser: resolvedByClerkId,
|
|
428
|
+
clerkId,
|
|
429
|
+
contextClerkId: clerkId
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
|
|
433
|
+
ctx,
|
|
434
|
+
normalizedActorId
|
|
435
|
+
);
|
|
436
|
+
return {
|
|
437
|
+
resolvedUser: resolvedByPrincipalId ?? null,
|
|
438
|
+
clerkId,
|
|
439
|
+
contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function uniqRoles(roles) {
|
|
443
|
+
const roleSet = /* @__PURE__ */ new Set();
|
|
444
|
+
for (const role of roles) {
|
|
445
|
+
if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
|
|
446
|
+
roleSet.add(role);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return [...roleSet];
|
|
450
|
+
}
|
|
451
|
+
function normalizeGroupIds(value) {
|
|
452
|
+
if (!Array.isArray(value)) {
|
|
453
|
+
return [];
|
|
454
|
+
}
|
|
455
|
+
return [...new Set(
|
|
456
|
+
value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
|
|
457
|
+
)];
|
|
458
|
+
}
|
|
459
|
+
function requireServiceAgentUser(user, actorId) {
|
|
460
|
+
const canonicalUser = requireCanonicalResolvedUser(user, actorId);
|
|
461
|
+
if (canonicalUser.mcRole !== "service_agent") {
|
|
462
|
+
throw new Error(
|
|
463
|
+
`[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
return canonicalUser;
|
|
467
|
+
}
|
|
468
|
+
function requireCanonicalResolvedAgent(agent, actorId) {
|
|
469
|
+
const resolved = agent;
|
|
470
|
+
if (!resolved) {
|
|
471
|
+
throw new Error(
|
|
472
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
|
|
476
|
+
throw new Error(
|
|
477
|
+
`[AccessControl] Canonical agent principalId required for ${actorId}.`
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
|
|
481
|
+
throw new Error(
|
|
482
|
+
`[AccessControl] Canonical home tenant required for ${actorId}.`
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
|
|
486
|
+
throw new Error(
|
|
487
|
+
`[AccessControl] Canonical home workspace required for ${actorId}.`
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
return {
|
|
491
|
+
principalId: resolved.principalId.trim(),
|
|
492
|
+
tenantId: resolved.tenantId.trim(),
|
|
493
|
+
workspaceId: resolved.workspaceId.trim(),
|
|
494
|
+
roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
|
|
495
|
+
groupIds: normalizeGroupIds(resolved.groupIds)
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
async function resolvePrincipalContext(ctx, actorId) {
|
|
499
|
+
if (actorId.startsWith("agent:")) {
|
|
500
|
+
const resolvers = resolveAccessControlAppResolvers();
|
|
501
|
+
const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
|
|
502
|
+
if (resolvedAgent) {
|
|
503
|
+
const agent = requireCanonicalResolvedAgent(
|
|
504
|
+
resolvedAgent,
|
|
505
|
+
actorId
|
|
506
|
+
);
|
|
507
|
+
return {
|
|
508
|
+
principalId: agent.principalId,
|
|
509
|
+
principalType: "service",
|
|
510
|
+
clerkId: actorId,
|
|
511
|
+
tenantId: agent.tenantId,
|
|
512
|
+
workspaceId: agent.workspaceId,
|
|
513
|
+
roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
|
|
514
|
+
groupIds: agent.groupIds,
|
|
515
|
+
isPlatformAdmin: false,
|
|
516
|
+
isTenantAdmin: false,
|
|
517
|
+
isWorkspaceAdmin: false,
|
|
518
|
+
isSystemFallback: false
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
const resolvedUser2 = await resolvers.getUserByClerkId(
|
|
522
|
+
ctx,
|
|
523
|
+
actorId
|
|
524
|
+
);
|
|
525
|
+
if (!resolvedUser2) {
|
|
526
|
+
throw new Error(
|
|
527
|
+
`[AccessControl] Agent "${actorId}" not found in agents or users table.`
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
const user2 = requireServiceAgentUser(
|
|
531
|
+
resolvedUser2,
|
|
532
|
+
actorId
|
|
533
|
+
);
|
|
534
|
+
console.warn(
|
|
535
|
+
`[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
|
|
536
|
+
);
|
|
537
|
+
return {
|
|
538
|
+
principalId: user2.defaultPrincipalId,
|
|
539
|
+
principalType: "service",
|
|
540
|
+
clerkId: actorId,
|
|
541
|
+
tenantId: user2.defaultTenantId,
|
|
542
|
+
workspaceId: user2.defaultWorkspaceId,
|
|
543
|
+
roles: ["service_agent"],
|
|
544
|
+
groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
|
|
545
|
+
isPlatformAdmin: false,
|
|
546
|
+
isTenantAdmin: false,
|
|
547
|
+
isWorkspaceAdmin: false,
|
|
548
|
+
isSystemFallback: false
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
const {
|
|
552
|
+
resolvedUser,
|
|
553
|
+
contextClerkId
|
|
554
|
+
} = await resolveCanonicalUserRecord(ctx, actorId);
|
|
555
|
+
const user = requireCanonicalResolvedUser(
|
|
556
|
+
resolvedUser,
|
|
557
|
+
contextClerkId
|
|
558
|
+
);
|
|
559
|
+
if (!user.defaultPrincipalId) {
|
|
560
|
+
throw new Error(
|
|
561
|
+
`[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
if (user.mcRole === "service_agent") {
|
|
565
|
+
return {
|
|
566
|
+
principalId: user.defaultPrincipalId,
|
|
567
|
+
principalType: "service",
|
|
568
|
+
clerkId: contextClerkId,
|
|
569
|
+
tenantId: user.defaultTenantId,
|
|
570
|
+
workspaceId: user.defaultWorkspaceId,
|
|
571
|
+
roles: ["service_agent"],
|
|
572
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
573
|
+
isPlatformAdmin: false,
|
|
574
|
+
isTenantAdmin: false,
|
|
575
|
+
isWorkspaceAdmin: false,
|
|
576
|
+
isSystemFallback: false
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
const principalId = user.defaultPrincipalId;
|
|
580
|
+
const effectiveRole = user.mcRole;
|
|
581
|
+
const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
|
|
582
|
+
const tenantId = user.defaultTenantId;
|
|
583
|
+
const workspaceId = user.defaultWorkspaceId;
|
|
584
|
+
const isPlatformAdmin = effectiveRole === "platform_admin";
|
|
585
|
+
return {
|
|
586
|
+
principalId,
|
|
587
|
+
principalType: "user",
|
|
588
|
+
clerkId: contextClerkId,
|
|
589
|
+
tenantId,
|
|
590
|
+
workspaceId,
|
|
591
|
+
roles: uniqRoles(roles),
|
|
592
|
+
groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
|
|
593
|
+
isPlatformAdmin,
|
|
594
|
+
isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
|
|
595
|
+
isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
|
|
596
|
+
isSystemFallback: false
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ../access-control/src/access.ts
|
|
601
|
+
function isTopicInPrincipalTenant(topic, principalTenantId) {
|
|
602
|
+
if (!topic.tenantId) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
if (!principalTenantId) {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
return String(topic.tenantId) === String(principalTenantId);
|
|
609
|
+
}
|
|
610
|
+
function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
|
|
611
|
+
if (!topic.workspaceId) {
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
if (!principalWorkspaceId) {
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
return String(topic.workspaceId) === String(principalWorkspaceId);
|
|
618
|
+
}
|
|
619
|
+
function isLegacyUnscopedTopic(topic) {
|
|
620
|
+
return !topic.tenantId || !topic.workspaceId;
|
|
621
|
+
}
|
|
622
|
+
function isGrantScopeAlignedToTopic(topic, grant) {
|
|
623
|
+
if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
|
|
627
|
+
return false;
|
|
628
|
+
}
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
function isGrantSourceAllowedForVisibility(visibility, source) {
|
|
632
|
+
if (source !== "external_share") {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
return visibility === "external" || visibility === "public";
|
|
636
|
+
}
|
|
637
|
+
function isGrantActive(grant) {
|
|
638
|
+
if (grant.status !== "active") {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
async function hasPrincipalGrant(ctx, args) {
|
|
647
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndPrincipal(
|
|
648
|
+
ctx,
|
|
649
|
+
args.topic._id,
|
|
650
|
+
args.principalId
|
|
651
|
+
);
|
|
652
|
+
if (grants.some(
|
|
653
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
654
|
+
args.topic.visibility,
|
|
655
|
+
grant.source
|
|
656
|
+
) && (!args.principalIsExternal || args.topic.visibility === "public" || grant.source === "external_share")
|
|
657
|
+
)) {
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
async function hasGroupGrant(ctx, args) {
|
|
663
|
+
if (args.groupIds.length === 0) {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
for (const groupId of args.groupIds) {
|
|
667
|
+
const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndGroup(ctx, args.topic._id, groupId);
|
|
668
|
+
if (grants.some(
|
|
669
|
+
(grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
|
|
670
|
+
args.topic.visibility,
|
|
671
|
+
grant.source
|
|
672
|
+
)
|
|
673
|
+
)) {
|
|
674
|
+
return true;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
function isExternalPrincipal(_ctx, _args) {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
async function evaluateTopicAccessDetailed(ctx, args) {
|
|
683
|
+
if (args.legacyUserId) {
|
|
684
|
+
return {
|
|
685
|
+
hasAccess: true,
|
|
686
|
+
isAdmin: false,
|
|
687
|
+
isOwner: false,
|
|
688
|
+
isShared: false,
|
|
689
|
+
hasGrant: true,
|
|
690
|
+
isFirmVisible: true,
|
|
691
|
+
isExternalVisible: false,
|
|
692
|
+
isPublicVisible: false,
|
|
693
|
+
isTenantScopeMatch: true,
|
|
694
|
+
isWorkspaceScopeMatch: true,
|
|
695
|
+
isPrincipalExternal: false
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
const topic = await resolveAccessControlAppResolvers().getProject(
|
|
699
|
+
ctx,
|
|
700
|
+
args.topicId
|
|
701
|
+
);
|
|
702
|
+
if (!topic) {
|
|
703
|
+
return {
|
|
704
|
+
hasAccess: false,
|
|
705
|
+
isAdmin: false,
|
|
706
|
+
isOwner: false,
|
|
707
|
+
isShared: false,
|
|
708
|
+
hasGrant: false,
|
|
709
|
+
isFirmVisible: false,
|
|
710
|
+
isExternalVisible: false,
|
|
711
|
+
isPublicVisible: false,
|
|
712
|
+
isTenantScopeMatch: false,
|
|
713
|
+
isWorkspaceScopeMatch: false,
|
|
714
|
+
isPrincipalExternal: false
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
const { principalContext, legacyUserId } = args;
|
|
718
|
+
const userIsAdmin = principalContext.isPlatformAdmin;
|
|
719
|
+
const isOwner = topic.ownerId === legacyUserId;
|
|
720
|
+
const isShared = (topic.sharedWith ?? []).includes(legacyUserId);
|
|
721
|
+
const principalIsExternal = await isExternalPrincipal(ctx, {
|
|
722
|
+
groupIds: principalContext.groupIds,
|
|
723
|
+
topicTenantId: topic.tenantId,
|
|
724
|
+
topicWorkspaceId: topic.workspaceId
|
|
725
|
+
});
|
|
726
|
+
const hasPrincipalGrantResult = await hasPrincipalGrant(ctx, {
|
|
727
|
+
topic,
|
|
728
|
+
principalId: principalContext.principalId,
|
|
729
|
+
principalIsExternal
|
|
730
|
+
});
|
|
731
|
+
const hasGroupGrantResult = await hasGroupGrant(ctx, {
|
|
732
|
+
topic,
|
|
733
|
+
groupIds: principalContext.groupIds
|
|
734
|
+
});
|
|
735
|
+
const hasGrant = isShared || hasPrincipalGrantResult || hasGroupGrantResult;
|
|
736
|
+
const legacyUnscoped = isLegacyUnscopedTopic(topic);
|
|
737
|
+
const tenantScopeMatch = isTopicInPrincipalTenant(
|
|
738
|
+
topic,
|
|
739
|
+
principalContext.tenantId
|
|
740
|
+
);
|
|
741
|
+
const workspaceScopeMatch = isTopicInPrincipalWorkspace(
|
|
742
|
+
topic,
|
|
743
|
+
principalContext.workspaceId
|
|
744
|
+
);
|
|
745
|
+
const isPublicVisible = topic.visibility === "public";
|
|
746
|
+
const isFirmVisible = topic.visibility === "firm" && !legacyUnscoped && tenantScopeMatch && workspaceScopeMatch && !principalIsExternal;
|
|
747
|
+
const hasScopedGrant = hasGrant && (legacyUnscoped || tenantScopeMatch && workspaceScopeMatch);
|
|
748
|
+
const isExternalVisible = topic.visibility === "external" && hasScopedGrant;
|
|
749
|
+
const hasAccess = userIsAdmin || isOwner || hasScopedGrant || isPublicVisible || isFirmVisible;
|
|
750
|
+
return {
|
|
751
|
+
hasAccess,
|
|
752
|
+
isAdmin: userIsAdmin,
|
|
753
|
+
isOwner,
|
|
754
|
+
isShared,
|
|
755
|
+
hasGrant,
|
|
756
|
+
isFirmVisible,
|
|
757
|
+
isExternalVisible,
|
|
758
|
+
isPublicVisible,
|
|
759
|
+
isTenantScopeMatch: tenantScopeMatch,
|
|
760
|
+
isWorkspaceScopeMatch: workspaceScopeMatch,
|
|
761
|
+
isPrincipalExternal: principalIsExternal
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
async function checkTopicAccessDetailed(ctx, topicId, userId) {
|
|
765
|
+
const principalContext = await resolvePrincipalContext(ctx, userId);
|
|
766
|
+
return evaluateTopicAccessDetailed(ctx, {
|
|
767
|
+
topicId,
|
|
768
|
+
legacyUserId: userId,
|
|
769
|
+
principalContext
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
async function checkTopicAccess(ctx, topicId, userId) {
|
|
773
|
+
const result = await checkTopicAccessDetailed(ctx, topicId, userId);
|
|
774
|
+
return result.hasAccess;
|
|
775
|
+
}
|
|
776
|
+
async function checkScopeAccess(ctx, scopeId, userId) {
|
|
777
|
+
try {
|
|
778
|
+
const topic = await ctx.db.get(scopeId);
|
|
779
|
+
if (topic && topic.name !== void 0 && topic.type !== void 0) {
|
|
780
|
+
return true;
|
|
781
|
+
}
|
|
782
|
+
} catch {
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
return await checkTopicAccess(ctx, scopeId, userId);
|
|
786
|
+
} catch {
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
async function requireTopicAccess(ctx, topicId, userId) {
|
|
791
|
+
const hasAccess = await checkTopicAccess(ctx, topicId, userId);
|
|
792
|
+
if (!hasAccess) {
|
|
793
|
+
throw new Error(
|
|
794
|
+
"Access denied: You don't have permission to access this topic"
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
var checkProjectAccess = checkTopicAccess;
|
|
799
|
+
var requireProjectAccess = requireTopicAccess;
|
|
800
|
+
|
|
801
|
+
// ../access-control/src/audience.ts
|
|
802
|
+
var AUDIENCE_CLASS_RANK = {
|
|
803
|
+
public: 0,
|
|
804
|
+
restricted_external: 1,
|
|
805
|
+
internal: 2
|
|
806
|
+
};
|
|
807
|
+
function normalizeKey(key) {
|
|
808
|
+
return (key ?? "").trim().toLowerCase().replace(/[^a-z0-9:_-]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
809
|
+
}
|
|
810
|
+
function normalizeAudienceKey(key) {
|
|
811
|
+
return normalizeKey(key);
|
|
812
|
+
}
|
|
813
|
+
function classFromAudienceKey(audienceKey, fallback = "internal") {
|
|
814
|
+
const key = normalizeKey(audienceKey);
|
|
815
|
+
if (!key) {
|
|
816
|
+
return fallback;
|
|
817
|
+
}
|
|
818
|
+
if (key === "internal") {
|
|
819
|
+
return "internal";
|
|
820
|
+
}
|
|
821
|
+
if (key === "public") {
|
|
822
|
+
return "public";
|
|
823
|
+
}
|
|
824
|
+
if (key === "lp" || key === "external" || key === "client" || key === "partner" || key === "portfolio" || key === "network" || key === "restricted_external") {
|
|
825
|
+
return "restricted_external";
|
|
826
|
+
}
|
|
827
|
+
return fallback;
|
|
828
|
+
}
|
|
829
|
+
function canAudienceClassAccess(viewerClass, resourceClass) {
|
|
830
|
+
return AUDIENCE_CLASS_RANK[viewerClass] >= AUDIENCE_CLASS_RANK[resourceClass];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// ../access-control/src/audienceRegistry.ts
|
|
834
|
+
var DEFAULT_AUDIENCES = [
|
|
835
|
+
{
|
|
836
|
+
audienceKey: "internal",
|
|
837
|
+
audienceLabel: "Internal",
|
|
838
|
+
audienceClass: "internal"
|
|
839
|
+
},
|
|
840
|
+
{
|
|
841
|
+
audienceKey: "lp",
|
|
842
|
+
audienceLabel: "Limited Partners",
|
|
843
|
+
audienceClass: "restricted_external"
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
audienceKey: "public",
|
|
847
|
+
audienceLabel: "Public",
|
|
848
|
+
audienceClass: "public"
|
|
849
|
+
}
|
|
850
|
+
];
|
|
851
|
+
var AUDIENCE_CLASS_PRIORITY = {
|
|
852
|
+
internal: 0,
|
|
853
|
+
restricted_external: 1,
|
|
854
|
+
public: 2
|
|
855
|
+
};
|
|
856
|
+
function normalizeRegistryRow(row) {
|
|
857
|
+
return {
|
|
858
|
+
audienceKey: normalizeAudienceKey(row.audienceKey),
|
|
859
|
+
audienceLabel: row.audienceLabel,
|
|
860
|
+
audienceClass: row.audienceClass,
|
|
861
|
+
workspaceId: row.workspaceId
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function dedupeRegistryRows(rows) {
|
|
865
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
866
|
+
for (const row of rows) {
|
|
867
|
+
const key = normalizeAudienceKey(row.audienceKey);
|
|
868
|
+
if (!key) {
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
const existing = byKey.get(key);
|
|
872
|
+
const isWorkspaceScoped = row.workspaceId !== void 0;
|
|
873
|
+
const existingWorkspaceScoped = existing?.workspaceId !== void 0;
|
|
874
|
+
if (!existing || isWorkspaceScoped && !existingWorkspaceScoped) {
|
|
875
|
+
byKey.set(key, {
|
|
876
|
+
...row,
|
|
877
|
+
audienceKey: key
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
const normalized = [...byKey.values()];
|
|
882
|
+
normalized.sort((a, b) => {
|
|
883
|
+
const classDelta = AUDIENCE_CLASS_PRIORITY[a.audienceClass] - AUDIENCE_CLASS_PRIORITY[b.audienceClass];
|
|
884
|
+
if (classDelta !== 0) {
|
|
885
|
+
return classDelta;
|
|
886
|
+
}
|
|
887
|
+
return a.audienceKey.localeCompare(b.audienceKey);
|
|
888
|
+
});
|
|
889
|
+
return normalized;
|
|
890
|
+
}
|
|
891
|
+
async function queryRegistryRows(ctx, args) {
|
|
892
|
+
if (!args.tenantId) {
|
|
893
|
+
return [...DEFAULT_AUDIENCES];
|
|
894
|
+
}
|
|
895
|
+
const rows = await ctx.db.query("platformAudiences").withIndex("by_tenantId", (q) => q.eq("tenantId", args.tenantId)).collect();
|
|
896
|
+
const workspaceIdString = args.workspaceId ? String(args.workspaceId) : null;
|
|
897
|
+
const tenantScoped = rows.filter((row) => row.status === "active");
|
|
898
|
+
const applicable = tenantScoped.filter((row) => {
|
|
899
|
+
if (!row.workspaceId) {
|
|
900
|
+
return true;
|
|
901
|
+
}
|
|
902
|
+
if (!workspaceIdString) {
|
|
903
|
+
return false;
|
|
904
|
+
}
|
|
905
|
+
return String(row.workspaceId) === workspaceIdString;
|
|
906
|
+
});
|
|
907
|
+
return dedupeRegistryRows([
|
|
908
|
+
...DEFAULT_AUDIENCES,
|
|
909
|
+
...applicable.map(
|
|
910
|
+
(row) => normalizeRegistryRow({
|
|
911
|
+
audienceKey: row.audienceKey,
|
|
912
|
+
audienceLabel: row.audienceLabel,
|
|
913
|
+
audienceClass: row.audienceClass,
|
|
914
|
+
workspaceId: row.workspaceId
|
|
915
|
+
})
|
|
916
|
+
)
|
|
917
|
+
]);
|
|
918
|
+
}
|
|
919
|
+
async function listAudienceRegistryRows(ctx, args) {
|
|
920
|
+
return queryRegistryRows(ctx, args);
|
|
921
|
+
}
|
|
922
|
+
var permissiveReturn = v.optional(v.any());
|
|
923
|
+
var looseJsonObject = v.record(v.string(), v.any());
|
|
924
|
+
var looseJsonArray = v.array(v.any());
|
|
925
|
+
v.union(
|
|
926
|
+
v.string(),
|
|
927
|
+
v.number(),
|
|
928
|
+
v.boolean(),
|
|
929
|
+
v.null(),
|
|
930
|
+
looseJsonObject,
|
|
931
|
+
looseJsonArray
|
|
932
|
+
);
|
|
933
|
+
var api2 = anyApi;
|
|
934
|
+
componentsGeneric();
|
|
935
|
+
var internal = anyApi;
|
|
936
|
+
var internalMutation = internalMutationGeneric;
|
|
937
|
+
var internalQuery = internalQueryGeneric;
|
|
938
|
+
var mutation = mutationGeneric;
|
|
939
|
+
var query = queryGeneric;
|
|
940
|
+
|
|
941
|
+
// src/embeddingTrigger.ts
|
|
942
|
+
async function scheduleEmbeddingGeneration(args) {
|
|
943
|
+
try {
|
|
944
|
+
await args.ctx.scheduler.runAfter(
|
|
945
|
+
0,
|
|
946
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
947
|
+
{
|
|
948
|
+
nodeId: args.nodeId,
|
|
949
|
+
projectId: args.projectId ? String(args.projectId) : void 0,
|
|
950
|
+
topicId: args.topicId ? String(args.topicId) : void 0,
|
|
951
|
+
createdBy: args.createdBy,
|
|
952
|
+
nodeType: args.nodeType,
|
|
953
|
+
text: args.text.slice(0, 2e4),
|
|
954
|
+
hasAnswer: args.hasAnswer,
|
|
955
|
+
confidence: args.confidence
|
|
956
|
+
}
|
|
957
|
+
);
|
|
958
|
+
} catch {
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// src/globalId.ts
|
|
963
|
+
function generateGlobalId() {
|
|
964
|
+
const bytes = new Uint8Array(16);
|
|
965
|
+
crypto.getRandomValues(bytes);
|
|
966
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
967
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
968
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
969
|
+
""
|
|
970
|
+
);
|
|
971
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// src/topicProjectOverlay.ts
|
|
975
|
+
var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
|
|
976
|
+
function readNonEmptyString2(value) {
|
|
977
|
+
if (typeof value !== "string") {
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
const normalized = value.trim();
|
|
981
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
982
|
+
}
|
|
983
|
+
function readStringArray2(value) {
|
|
984
|
+
if (!Array.isArray(value)) {
|
|
985
|
+
return [];
|
|
986
|
+
}
|
|
987
|
+
return value.map((entry) => readNonEmptyString2(entry)).filter((entry) => Boolean(entry));
|
|
988
|
+
}
|
|
989
|
+
function readMetadata2(topic) {
|
|
990
|
+
return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
|
|
991
|
+
}
|
|
992
|
+
function readLegacyProjectId2(value) {
|
|
993
|
+
if (!value) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
return readNonEmptyString2(value[LEGACY_SCOPE_FIELD2]);
|
|
997
|
+
}
|
|
998
|
+
function coerceVisibility2(value) {
|
|
999
|
+
return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
|
|
1000
|
+
}
|
|
1001
|
+
function coerceStatus2(value) {
|
|
1002
|
+
return value === "active" || value === "archived" || value === "watching" ? value : void 0;
|
|
1003
|
+
}
|
|
1004
|
+
function mapProjectType2(topic, metadata) {
|
|
1005
|
+
const explicit = readNonEmptyString2(metadata.projectType);
|
|
1006
|
+
if (explicit) {
|
|
1007
|
+
return explicit;
|
|
1008
|
+
}
|
|
1009
|
+
if (topic.type === "theme") {
|
|
1010
|
+
return "thematic";
|
|
1011
|
+
}
|
|
1012
|
+
return readNonEmptyString2(topic.type) || "general";
|
|
1013
|
+
}
|
|
1014
|
+
function isProjectLikeTopic2(topic) {
|
|
1015
|
+
const metadata = readMetadata2(topic);
|
|
1016
|
+
return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId2(topic) !== void 0 || readNonEmptyString2(metadata.projectType) !== void 0;
|
|
1017
|
+
}
|
|
1018
|
+
function isMissingLucernChildComponentError(error) {
|
|
1019
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1020
|
+
return message.includes(
|
|
1021
|
+
'Child component ComponentName(Identifier("lucern")) not found'
|
|
1022
|
+
) || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
1023
|
+
}
|
|
1024
|
+
async function resolveTopicDoc2(ctx, scopeId) {
|
|
1025
|
+
if (ctx?.db && typeof ctx.db.get === "function") {
|
|
1026
|
+
try {
|
|
1027
|
+
const directTopic = await ctx.db.get(scopeId);
|
|
1028
|
+
if (directTopic) {
|
|
1029
|
+
return directTopic;
|
|
1030
|
+
}
|
|
1031
|
+
} catch {
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1035
|
+
return null;
|
|
1036
|
+
}
|
|
1037
|
+
try {
|
|
1038
|
+
const topic = await ctx.runQuery(api2.topics.get, {
|
|
1039
|
+
id: String(scopeId)
|
|
1040
|
+
});
|
|
1041
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
1042
|
+
return topic;
|
|
1043
|
+
}
|
|
1044
|
+
} catch {
|
|
1045
|
+
}
|
|
1046
|
+
try {
|
|
1047
|
+
const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
|
|
1048
|
+
projectId: String(scopeId)
|
|
1049
|
+
});
|
|
1050
|
+
if (topic?.name !== void 0 && topic?.type !== void 0) {
|
|
1051
|
+
return topic;
|
|
1052
|
+
}
|
|
1053
|
+
} catch {
|
|
1054
|
+
}
|
|
1055
|
+
return null;
|
|
1056
|
+
}
|
|
1057
|
+
function materializeTopicProjectOverlay2(topic, idMode = "legacy") {
|
|
1058
|
+
const metadata = readMetadata2(topic);
|
|
1059
|
+
const topicId = String(topic._id);
|
|
1060
|
+
const legacyProjectId = readLegacyProjectId2(topic) || readLegacyProjectId2(metadata) || readNonEmptyString2(metadata.legacyProjectId);
|
|
1061
|
+
const storageProjectId = legacyProjectId || topicId;
|
|
1062
|
+
const outwardId = idMode === "topic" ? topicId : storageProjectId;
|
|
1063
|
+
const visibility = coerceVisibility2(topic.visibility) || coerceVisibility2(metadata.visibility) || "private";
|
|
1064
|
+
const status = coerceStatus2(topic.status) || coerceStatus2(metadata.status) || "active";
|
|
1065
|
+
const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
|
|
1066
|
+
const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
|
|
1067
|
+
return {
|
|
1068
|
+
...metadata,
|
|
1069
|
+
_id: outwardId,
|
|
1070
|
+
projectId: outwardId,
|
|
1071
|
+
topicId,
|
|
1072
|
+
storageProjectId,
|
|
1073
|
+
legacyProjectId,
|
|
1074
|
+
name: readNonEmptyString2(topic.name) || "Untitled Theme",
|
|
1075
|
+
type: mapProjectType2(topic, metadata),
|
|
1076
|
+
description: readNonEmptyString2(topic.description),
|
|
1077
|
+
ownerId: readNonEmptyString2(metadata.ownerId) || readNonEmptyString2(topic.createdBy) || "system",
|
|
1078
|
+
sharedWith: readStringArray2(metadata.sharedWith),
|
|
1079
|
+
visibility,
|
|
1080
|
+
tenantId: readNonEmptyString2(topic.tenantId) || readNonEmptyString2(metadata.tenantId),
|
|
1081
|
+
workspaceId: readNonEmptyString2(topic.workspaceId) || readNonEmptyString2(metadata.workspaceId),
|
|
1082
|
+
status,
|
|
1083
|
+
tags: readStringArray2(metadata.tags),
|
|
1084
|
+
chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
|
|
1085
|
+
artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
|
|
1086
|
+
lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
|
|
1087
|
+
_creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
|
|
1088
|
+
createdAt,
|
|
1089
|
+
updatedAt
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
async function resolveTopicProjectOverlay2(ctx, scopeId, options = {}) {
|
|
1093
|
+
const topic = await resolveTopicDoc2(ctx, scopeId);
|
|
1094
|
+
if (!topic) {
|
|
1095
|
+
return null;
|
|
1096
|
+
}
|
|
1097
|
+
if (options.projectLikeOnly !== false && !isProjectLikeTopic2(topic)) {
|
|
1098
|
+
return null;
|
|
1099
|
+
}
|
|
1100
|
+
return materializeTopicProjectOverlay2(topic, options.idMode);
|
|
1101
|
+
}
|
|
1102
|
+
async function listTopicProjectOverlays2(ctx, options = {}) {
|
|
1103
|
+
let allTopics = [];
|
|
1104
|
+
if (ctx?.db?.query && typeof ctx.db.query === "function") {
|
|
1105
|
+
try {
|
|
1106
|
+
allTopics = await ctx.db.query("topics").collect();
|
|
1107
|
+
} catch {
|
|
1108
|
+
allTopics = [];
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
|
|
1112
|
+
allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
|
|
1113
|
+
}
|
|
1114
|
+
return allTopics.filter(
|
|
1115
|
+
(topic) => options.projectLikeOnly === false || isProjectLikeTopic2(topic)
|
|
1116
|
+
).map((topic) => materializeTopicProjectOverlay2(topic, options.idMode));
|
|
1117
|
+
}
|
|
1118
|
+
async function patchTopicProjectOverlay(ctx, scopeId, value) {
|
|
1119
|
+
const topic = await resolveTopicDoc2(ctx, scopeId);
|
|
1120
|
+
if (!topic) {
|
|
1121
|
+
return null;
|
|
1122
|
+
}
|
|
1123
|
+
const nextMetadata = { ...readMetadata2(topic) };
|
|
1124
|
+
const patch = {};
|
|
1125
|
+
const topicUpdateArgs = {
|
|
1126
|
+
id: String(topic._id)
|
|
1127
|
+
};
|
|
1128
|
+
for (const [key, rawValue] of Object.entries(value)) {
|
|
1129
|
+
switch (key) {
|
|
1130
|
+
case "_id":
|
|
1131
|
+
case "projectId":
|
|
1132
|
+
case "topicId":
|
|
1133
|
+
case "legacyProjectId":
|
|
1134
|
+
case "storageProjectId":
|
|
1135
|
+
break;
|
|
1136
|
+
case "name":
|
|
1137
|
+
case "description":
|
|
1138
|
+
patch[key] = rawValue;
|
|
1139
|
+
topicUpdateArgs[key] = rawValue;
|
|
1140
|
+
break;
|
|
1141
|
+
case "tenantId":
|
|
1142
|
+
case "workspaceId":
|
|
1143
|
+
case "ownerId":
|
|
1144
|
+
throw new Error(
|
|
1145
|
+
`patchTopicProjectOverlay cannot mutate ${key} via component-owned topics`
|
|
1146
|
+
);
|
|
1147
|
+
case "status": {
|
|
1148
|
+
const status = coerceStatus2(rawValue);
|
|
1149
|
+
if (status) {
|
|
1150
|
+
patch.status = status;
|
|
1151
|
+
topicUpdateArgs.status = status;
|
|
1152
|
+
}
|
|
1153
|
+
break;
|
|
1154
|
+
}
|
|
1155
|
+
case "visibility": {
|
|
1156
|
+
const visibility = coerceVisibility2(rawValue);
|
|
1157
|
+
if (visibility) {
|
|
1158
|
+
patch.visibility = visibility;
|
|
1159
|
+
topicUpdateArgs.visibility = visibility;
|
|
1160
|
+
}
|
|
1161
|
+
break;
|
|
1162
|
+
}
|
|
1163
|
+
case "type": {
|
|
1164
|
+
const projectType = readNonEmptyString2(rawValue);
|
|
1165
|
+
if (projectType) {
|
|
1166
|
+
nextMetadata.projectType = projectType;
|
|
1167
|
+
} else {
|
|
1168
|
+
delete nextMetadata.projectType;
|
|
1169
|
+
}
|
|
1170
|
+
break;
|
|
1171
|
+
}
|
|
1172
|
+
case "updatedAt":
|
|
1173
|
+
case "createdAt":
|
|
1174
|
+
break;
|
|
1175
|
+
default:
|
|
1176
|
+
if (rawValue === void 0) {
|
|
1177
|
+
delete nextMetadata[key];
|
|
1178
|
+
} else {
|
|
1179
|
+
nextMetadata[key] = rawValue;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
patch.updatedAt = Date.now();
|
|
1184
|
+
patch.metadata = nextMetadata;
|
|
1185
|
+
topicUpdateArgs.metadata = nextMetadata;
|
|
1186
|
+
if (typeof ctx.runMutation === "function") {
|
|
1187
|
+
try {
|
|
1188
|
+
await ctx.runMutation(api2.topics.update, topicUpdateArgs);
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
if (!isMissingLucernChildComponentError(error) || !ctx?.db || typeof ctx.db.patch !== "function") {
|
|
1191
|
+
throw error;
|
|
1192
|
+
}
|
|
1193
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
1194
|
+
}
|
|
1195
|
+
} else if (ctx?.db && typeof ctx.db.patch === "function") {
|
|
1196
|
+
await ctx.db.patch(String(topic._id), patch);
|
|
1197
|
+
} else {
|
|
1198
|
+
throw new Error(
|
|
1199
|
+
"Cannot patch topic without component adapter (ctx.runMutation unavailable)"
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
return materializeTopicProjectOverlay2(
|
|
1203
|
+
{
|
|
1204
|
+
...topic,
|
|
1205
|
+
...patch,
|
|
1206
|
+
metadata: nextMetadata
|
|
1207
|
+
}
|
|
1208
|
+
);
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// src/resolvers.ts
|
|
1212
|
+
function isMissingLucernChildComponentError2(error) {
|
|
1213
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1214
|
+
return message.includes('Child component ComponentName(Identifier("lucern")) not found') || message.includes("Child component") && message.includes("lucern") && message.includes("not found");
|
|
1215
|
+
}
|
|
1216
|
+
function isAdvisoryTopicPatch(value) {
|
|
1217
|
+
const advisoryKeys = /* @__PURE__ */ new Set(["lastActivityAt", "updatedAt"]);
|
|
1218
|
+
const keys = Object.keys(value);
|
|
1219
|
+
return keys.length > 0 && keys.every((key) => advisoryKeys.has(key));
|
|
1220
|
+
}
|
|
1221
|
+
async function patchProjectWithTolerance(ctx, projectId, value) {
|
|
1222
|
+
try {
|
|
1223
|
+
await patchTopicProjectOverlay(ctx, projectId, value);
|
|
1224
|
+
} catch (error) {
|
|
1225
|
+
if (!isAdvisoryTopicPatch(value) || !isMissingLucernChildComponentError2(error)) {
|
|
1226
|
+
throw error;
|
|
1227
|
+
}
|
|
1228
|
+
console.warn("[lucern graph-primitives] Non-fatal advisory topic patch failure", {
|
|
1229
|
+
projectId,
|
|
1230
|
+
keys: Object.keys(value),
|
|
1231
|
+
error: error instanceof Error ? error.message : error
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
function defaultResolvers2() {
|
|
1236
|
+
return {
|
|
1237
|
+
async getProject(ctx, projectId) {
|
|
1238
|
+
return await resolveTopicProjectOverlay2(ctx, projectId, {
|
|
1239
|
+
idMode: "legacy",
|
|
1240
|
+
projectLikeOnly: false
|
|
1241
|
+
});
|
|
1242
|
+
},
|
|
1243
|
+
async patchProject(ctx, projectId, value) {
|
|
1244
|
+
await patchProjectWithTolerance(ctx, projectId, value);
|
|
1245
|
+
},
|
|
1246
|
+
async listTopics(ctx) {
|
|
1247
|
+
return await listTopicProjectOverlays2(ctx, {
|
|
1248
|
+
idMode: "legacy"
|
|
1249
|
+
});
|
|
1250
|
+
},
|
|
1251
|
+
async getFinalArtifact(ctx, artifactId) {
|
|
1252
|
+
return await ctx.db.get(artifactId);
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
var resolverOverrides2 = {};
|
|
1257
|
+
function resolveGraphPrimitivesAppResolvers(_ctx) {
|
|
1258
|
+
return {
|
|
1259
|
+
...defaultResolvers2(),
|
|
1260
|
+
...resolverOverrides2
|
|
1261
|
+
};
|
|
1262
|
+
}
|
|
1263
|
+
var LEGACY_SCOPE_FIELD3 = "graphScopeProjectId";
|
|
1264
|
+
function asMappedProjectId(topic) {
|
|
1265
|
+
if (!topic) {
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD3]);
|
|
1269
|
+
if (directLegacyProjectId) {
|
|
1270
|
+
return directLegacyProjectId;
|
|
1271
|
+
}
|
|
1272
|
+
const metadata = topic.metadata || {};
|
|
1273
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD3] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
1274
|
+
return candidate ? candidate : void 0;
|
|
1275
|
+
}
|
|
1276
|
+
function normalizeScopeValue(value) {
|
|
1277
|
+
if (typeof value !== "string") {
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
const normalized = value.trim();
|
|
1281
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
1282
|
+
}
|
|
1283
|
+
function pickPrimaryTopic(candidates) {
|
|
1284
|
+
return [...candidates].sort((a, b) => {
|
|
1285
|
+
const depthA = a.depth ?? 9999;
|
|
1286
|
+
const depthB = b.depth ?? 9999;
|
|
1287
|
+
if (depthA !== depthB) {
|
|
1288
|
+
return depthA - depthB;
|
|
1289
|
+
}
|
|
1290
|
+
const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
1291
|
+
const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
|
|
1292
|
+
if (createdA !== createdB) {
|
|
1293
|
+
return createdA - createdB;
|
|
1294
|
+
}
|
|
1295
|
+
return String(a.name || "").localeCompare(String(b.name || ""));
|
|
1296
|
+
})[0];
|
|
1297
|
+
}
|
|
1298
|
+
async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
1299
|
+
try {
|
|
1300
|
+
return await ctx.db.query("topics").withIndex(
|
|
1301
|
+
"by_graph_scope_project",
|
|
1302
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD3, scopeId)
|
|
1303
|
+
).collect();
|
|
1304
|
+
} catch {
|
|
1305
|
+
const topics = await ctx.db.query("topics").collect();
|
|
1306
|
+
return topics.filter((topic) => {
|
|
1307
|
+
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
1308
|
+
const mappedProjectId = asMappedProjectId(topic);
|
|
1309
|
+
return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
async function tryResolveHostTopicById(ctx, topicId) {
|
|
1314
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1315
|
+
return null;
|
|
1316
|
+
}
|
|
1317
|
+
try {
|
|
1318
|
+
return await ctx.runQuery(api2.topics.get, {
|
|
1319
|
+
id: topicId
|
|
1320
|
+
}) ?? null;
|
|
1321
|
+
} catch {
|
|
1322
|
+
return null;
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
1326
|
+
if (typeof ctx.runQuery !== "function") {
|
|
1327
|
+
return null;
|
|
1328
|
+
}
|
|
1329
|
+
try {
|
|
1330
|
+
return await ctx.runQuery(api2.topics.getByLegacyScopeId, {
|
|
1331
|
+
projectId: legacyScopeId
|
|
1332
|
+
}) ?? null;
|
|
1333
|
+
} catch {
|
|
1334
|
+
return null;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
async function resolveInheritedWorkspaceScope(ctx, topic) {
|
|
1338
|
+
const MAX_DEPTH = 10;
|
|
1339
|
+
let tenantId = normalizeScopeValue(topic.tenantId);
|
|
1340
|
+
let workspaceId = normalizeScopeValue(topic.workspaceId);
|
|
1341
|
+
if (tenantId && workspaceId) {
|
|
1342
|
+
return { tenantId, workspaceId };
|
|
1343
|
+
}
|
|
1344
|
+
let current = topic;
|
|
1345
|
+
for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
|
|
1346
|
+
current = await ctx.db.get(current.parentTopicId);
|
|
1347
|
+
if (!current) break;
|
|
1348
|
+
if (!tenantId) {
|
|
1349
|
+
tenantId = normalizeScopeValue(current.tenantId);
|
|
1350
|
+
}
|
|
1351
|
+
if (!workspaceId) {
|
|
1352
|
+
workspaceId = normalizeScopeValue(current.workspaceId);
|
|
1353
|
+
}
|
|
1354
|
+
if (tenantId && workspaceId) break;
|
|
1355
|
+
}
|
|
1356
|
+
return { tenantId, workspaceId };
|
|
1357
|
+
}
|
|
1358
|
+
async function resolveTopicProjectScope(ctx, args) {
|
|
1359
|
+
if (args.topicId) {
|
|
1360
|
+
let topic = null;
|
|
1361
|
+
try {
|
|
1362
|
+
topic = await ctx.db.get(args.topicId);
|
|
1363
|
+
} catch {
|
|
1364
|
+
}
|
|
1365
|
+
if (!topic) {
|
|
1366
|
+
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
1367
|
+
}
|
|
1368
|
+
if (!topic) {
|
|
1369
|
+
topic = pickPrimaryTopic(
|
|
1370
|
+
await findTopicsByScopeAlias(ctx, String(args.topicId))
|
|
1371
|
+
) ?? null;
|
|
1372
|
+
}
|
|
1373
|
+
if (!topic) {
|
|
1374
|
+
throw new Error(`Topic not found: ${String(args.topicId)}`);
|
|
1375
|
+
}
|
|
1376
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
|
|
1377
|
+
const mapped = asMappedProjectId(topic);
|
|
1378
|
+
if (mapped) {
|
|
1379
|
+
return {
|
|
1380
|
+
topicId: topic._id,
|
|
1381
|
+
projectId: mapped,
|
|
1382
|
+
tenantId: inherited.tenantId,
|
|
1383
|
+
workspaceId: inherited.workspaceId,
|
|
1384
|
+
source: "topic"
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
return {
|
|
1388
|
+
topicId: topic._id,
|
|
1389
|
+
tenantId: inherited.tenantId,
|
|
1390
|
+
workspaceId: inherited.workspaceId,
|
|
1391
|
+
source: "topic"
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
if (args.projectId) {
|
|
1395
|
+
let directTopic = null;
|
|
1396
|
+
try {
|
|
1397
|
+
directTopic = await ctx.db.get(
|
|
1398
|
+
args.projectId
|
|
1399
|
+
);
|
|
1400
|
+
} catch {
|
|
1401
|
+
}
|
|
1402
|
+
if (directTopic) {
|
|
1403
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
1404
|
+
const mapped = asMappedProjectId(directTopic);
|
|
1405
|
+
return {
|
|
1406
|
+
topicId: directTopic._id,
|
|
1407
|
+
projectId: mapped ?? args.projectId,
|
|
1408
|
+
tenantId: inherited.tenantId,
|
|
1409
|
+
workspaceId: inherited.workspaceId,
|
|
1410
|
+
source: "topic_inferred"
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
|
|
1414
|
+
if (directTopic) {
|
|
1415
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
1416
|
+
const mapped = asMappedProjectId(directTopic);
|
|
1417
|
+
return {
|
|
1418
|
+
topicId: directTopic._id,
|
|
1419
|
+
projectId: mapped ?? args.projectId,
|
|
1420
|
+
tenantId: inherited.tenantId,
|
|
1421
|
+
workspaceId: inherited.workspaceId,
|
|
1422
|
+
source: "topic_inferred"
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
const topics = await findTopicsByScopeAlias(ctx, args.projectId);
|
|
1426
|
+
const primary = pickPrimaryTopic(topics);
|
|
1427
|
+
if (primary) {
|
|
1428
|
+
const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
|
|
1429
|
+
return {
|
|
1430
|
+
topicId: primary._id,
|
|
1431
|
+
projectId: args.projectId,
|
|
1432
|
+
tenantId: inherited.tenantId,
|
|
1433
|
+
workspaceId: inherited.workspaceId,
|
|
1434
|
+
source: "project_mapped_topic"
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
throw new Error(
|
|
1438
|
+
`Legacy project scope ${String(args.projectId)} has no mapped topic.`
|
|
1439
|
+
);
|
|
1440
|
+
}
|
|
1441
|
+
throw new Error(
|
|
1442
|
+
"Missing scope: provide topicId (preferred) or legacy projectId alias."
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
var optionalScopeArgs = {
|
|
1446
|
+
projectId: v.optional(v.string()),
|
|
1447
|
+
topicId: v.optional(v.string())
|
|
1448
|
+
};
|
|
1449
|
+
v.number();
|
|
1450
|
+
v.union(
|
|
1451
|
+
v.literal("very_high"),
|
|
1452
|
+
// 0.9+
|
|
1453
|
+
v.literal("high"),
|
|
1454
|
+
// 0.7-0.9
|
|
1455
|
+
v.literal("medium"),
|
|
1456
|
+
// 0.4-0.7
|
|
1457
|
+
v.literal("low"),
|
|
1458
|
+
// 0.2-0.4
|
|
1459
|
+
v.literal("very_low")
|
|
1460
|
+
// 0-0.2
|
|
1461
|
+
);
|
|
1462
|
+
v.union(
|
|
1463
|
+
v.literal(1),
|
|
1464
|
+
// Critical
|
|
1465
|
+
v.literal(2),
|
|
1466
|
+
// High
|
|
1467
|
+
v.literal(3),
|
|
1468
|
+
// Medium
|
|
1469
|
+
v.literal(4),
|
|
1470
|
+
// Low
|
|
1471
|
+
v.literal(5)
|
|
1472
|
+
// Backlog
|
|
1473
|
+
);
|
|
1474
|
+
v.union(
|
|
1475
|
+
v.literal("critical"),
|
|
1476
|
+
v.literal("high"),
|
|
1477
|
+
v.literal("medium"),
|
|
1478
|
+
v.literal("low"),
|
|
1479
|
+
v.literal("backlog")
|
|
1480
|
+
);
|
|
1481
|
+
v.union(
|
|
1482
|
+
v.literal("active"),
|
|
1483
|
+
v.literal("paused"),
|
|
1484
|
+
v.literal("completed"),
|
|
1485
|
+
v.literal("archived")
|
|
1486
|
+
);
|
|
1487
|
+
v.union(
|
|
1488
|
+
v.literal("pending"),
|
|
1489
|
+
v.literal("processing"),
|
|
1490
|
+
v.literal("completed"),
|
|
1491
|
+
v.literal("failed")
|
|
1492
|
+
);
|
|
1493
|
+
v.object({
|
|
1494
|
+
crunchbaseId: v.optional(v.string()),
|
|
1495
|
+
linkedinUrl: v.optional(v.string()),
|
|
1496
|
+
pitchbookId: v.optional(v.string()),
|
|
1497
|
+
twitterUrl: v.optional(v.string()),
|
|
1498
|
+
domain: v.optional(v.string())
|
|
1499
|
+
});
|
|
1500
|
+
var sourceType = v.union(
|
|
1501
|
+
v.literal("proprietary"),
|
|
1502
|
+
// Internal Stack research
|
|
1503
|
+
v.literal("primary"),
|
|
1504
|
+
// Direct interviews, calls
|
|
1505
|
+
v.literal("secondary"),
|
|
1506
|
+
// Published sources
|
|
1507
|
+
v.literal("ai_generated"),
|
|
1508
|
+
// AI-synthesized
|
|
1509
|
+
v.literal("user_input"),
|
|
1510
|
+
// Manual user entry
|
|
1511
|
+
v.literal("inferred")
|
|
1512
|
+
// System inference
|
|
1513
|
+
);
|
|
1514
|
+
v.object({
|
|
1515
|
+
sourceType: v.optional(sourceType),
|
|
1516
|
+
sourceId: v.optional(v.string()),
|
|
1517
|
+
// Reference to source entity
|
|
1518
|
+
sourceUrl: v.optional(v.string()),
|
|
1519
|
+
sourceDate: v.optional(v.number()),
|
|
1520
|
+
sourceName: v.optional(v.string())
|
|
1521
|
+
});
|
|
1522
|
+
v.object({
|
|
1523
|
+
cursor: v.optional(v.string()),
|
|
1524
|
+
limit: v.optional(v.number())
|
|
1525
|
+
});
|
|
1526
|
+
v.object({
|
|
1527
|
+
hasMore: v.boolean(),
|
|
1528
|
+
nextCursor: v.optional(v.string()),
|
|
1529
|
+
totalCount: v.optional(v.number())
|
|
1530
|
+
});
|
|
1531
|
+
var richTextContent = v.object({
|
|
1532
|
+
type: v.literal("doc"),
|
|
1533
|
+
content: looseJsonArray
|
|
1534
|
+
});
|
|
1535
|
+
v.union(v.string(), richTextContent);
|
|
1536
|
+
v.object({
|
|
1537
|
+
promptTokens: v.optional(v.number()),
|
|
1538
|
+
completionTokens: v.optional(v.number()),
|
|
1539
|
+
totalTokens: v.optional(v.number())
|
|
1540
|
+
});
|
|
1541
|
+
v.object({
|
|
1542
|
+
fileName: v.optional(v.string()),
|
|
1543
|
+
fileSize: v.optional(v.number()),
|
|
1544
|
+
mimeType: v.optional(v.string()),
|
|
1545
|
+
storageId: v.optional(v.id("_storage")),
|
|
1546
|
+
externalUrl: v.optional(v.string())
|
|
1547
|
+
});
|
|
1548
|
+
|
|
1549
|
+
// ../schema-management/src/spine/tables/epistemicNodes.ts
|
|
1550
|
+
var nodeType = v.union(
|
|
1551
|
+
// --- L4: Audit Targets (decisions, outcomes) ---
|
|
1552
|
+
v.literal("decision"),
|
|
1553
|
+
// Investment decision with knowledge horizon snapshot
|
|
1554
|
+
// --- L3: Traversal Anchors (epistemic structure) ---
|
|
1555
|
+
v.literal("belief"),
|
|
1556
|
+
// Structured conviction (immutable formulation)
|
|
1557
|
+
v.literal("question"),
|
|
1558
|
+
// Unit of uncertainty
|
|
1559
|
+
v.literal("theme"),
|
|
1560
|
+
// Investment thesis / conviction cluster
|
|
1561
|
+
v.literal("deal"),
|
|
1562
|
+
// Investment evaluation process
|
|
1563
|
+
v.literal("topic"),
|
|
1564
|
+
// Hierarchical knowledge container
|
|
1565
|
+
// --- L2: Compression Boundary (minimum reasoning unit) ---
|
|
1566
|
+
v.literal("claim"),
|
|
1567
|
+
// Atomic assertion that can be true/false
|
|
1568
|
+
v.literal("evidence"),
|
|
1569
|
+
// Interpreted signal linked to beliefs
|
|
1570
|
+
v.literal("synthesis"),
|
|
1571
|
+
// Primers, deep research
|
|
1572
|
+
v.literal("answer"),
|
|
1573
|
+
// Immutable answer snapshot for a question
|
|
1574
|
+
// --- L1: Terminal Leaves (non-traversable, grounding) ---
|
|
1575
|
+
v.literal("atomic_fact"),
|
|
1576
|
+
// Raw fact from source (not interpreted)
|
|
1577
|
+
v.literal("excerpt"),
|
|
1578
|
+
// Direct quote from source document
|
|
1579
|
+
v.literal("source"),
|
|
1580
|
+
// News, documents, transcripts
|
|
1581
|
+
// --- Ontological Entities (things in the world) ---
|
|
1582
|
+
v.literal("company"),
|
|
1583
|
+
// Organization (subtype: private, corporate, portfolio)
|
|
1584
|
+
v.literal("person"),
|
|
1585
|
+
// Individual (founder, expert, LP, contact)
|
|
1586
|
+
v.literal("investor"),
|
|
1587
|
+
// Investment entity (subtype: vc, lp, cvc, pe, family_office, angel)
|
|
1588
|
+
v.literal("function"),
|
|
1589
|
+
// What a company does (from classifier)
|
|
1590
|
+
v.literal("value_chain")
|
|
1591
|
+
// Market structure / value flow
|
|
1592
|
+
);
|
|
1593
|
+
var epistemicLayer = v.union(
|
|
1594
|
+
v.literal("L4"),
|
|
1595
|
+
// Decisions, outcomes - audit targets
|
|
1596
|
+
v.literal("L3"),
|
|
1597
|
+
// Beliefs, questions, themes - traversal anchors
|
|
1598
|
+
v.literal("L2"),
|
|
1599
|
+
// Claims, evidence, synthesis - compression boundary
|
|
1600
|
+
v.literal("L1"),
|
|
1601
|
+
// Atomic facts, excerpts, sources - terminal leaves
|
|
1602
|
+
v.literal("ontological"),
|
|
1603
|
+
// Companies, people, etc - not epistemic
|
|
1604
|
+
v.literal("organizational")
|
|
1605
|
+
// Topics, lenses, worktrees — structural containers
|
|
1606
|
+
);
|
|
1607
|
+
var nodeStatus = v.union(
|
|
1608
|
+
v.literal("active"),
|
|
1609
|
+
v.literal("superseded"),
|
|
1610
|
+
// Replaced by newer version
|
|
1611
|
+
v.literal("archived"),
|
|
1612
|
+
v.literal("deleted")
|
|
1613
|
+
);
|
|
1614
|
+
var sourceType2 = v.union(
|
|
1615
|
+
v.literal("human"),
|
|
1616
|
+
// User created directly
|
|
1617
|
+
v.literal("ai_extracted"),
|
|
1618
|
+
// LLM extracted from a source
|
|
1619
|
+
v.literal("ai_generated"),
|
|
1620
|
+
// LLM synthesized/created
|
|
1621
|
+
v.literal("imported"),
|
|
1622
|
+
// External system import
|
|
1623
|
+
v.literal("system"),
|
|
1624
|
+
// System-generated (migrations, classifiers)
|
|
1625
|
+
v.literal("verified"),
|
|
1626
|
+
// Human-verified source
|
|
1627
|
+
v.literal("proprietary")
|
|
1628
|
+
// Proprietary/internal data
|
|
1629
|
+
);
|
|
1630
|
+
var verificationStatus = v.union(
|
|
1631
|
+
v.literal("unverified"),
|
|
1632
|
+
v.literal("human_verified"),
|
|
1633
|
+
v.literal("ai_verified"),
|
|
1634
|
+
v.literal("contradicted"),
|
|
1635
|
+
v.literal("outdated")
|
|
1636
|
+
);
|
|
1637
|
+
var syncStatus = v.union(
|
|
1638
|
+
v.literal("synced"),
|
|
1639
|
+
// Node and edges fully synced to Neo4j
|
|
1640
|
+
v.literal("pending_edges"),
|
|
1641
|
+
// Node created, edges being created
|
|
1642
|
+
v.literal("edge_creation_failed")
|
|
1643
|
+
// Edge creation failed, needs retry
|
|
1644
|
+
);
|
|
1645
|
+
var audienceLabel = v.string();
|
|
1646
|
+
var sensitivityTier = v.union(
|
|
1647
|
+
v.literal("low"),
|
|
1648
|
+
v.literal("medium"),
|
|
1649
|
+
v.literal("high"),
|
|
1650
|
+
v.literal("restricted")
|
|
1651
|
+
);
|
|
1652
|
+
var exportClass = v.union(
|
|
1653
|
+
v.literal("internal_only"),
|
|
1654
|
+
v.literal("client_safe"),
|
|
1655
|
+
v.literal("public_safe"),
|
|
1656
|
+
v.literal("restricted")
|
|
1657
|
+
);
|
|
1658
|
+
var anonymizationClass = v.union(
|
|
1659
|
+
v.literal("none"),
|
|
1660
|
+
v.literal("standard"),
|
|
1661
|
+
v.literal("strict")
|
|
1662
|
+
);
|
|
1663
|
+
var epistemicStatus = v.union(
|
|
1664
|
+
v.literal("hypothesis"),
|
|
1665
|
+
// Initial conjecture, low evidence
|
|
1666
|
+
v.literal("emerging"),
|
|
1667
|
+
// Building evidence, gaining traction
|
|
1668
|
+
v.literal("established"),
|
|
1669
|
+
// Well-evidenced, core to thesis
|
|
1670
|
+
v.literal("challenged"),
|
|
1671
|
+
// Contradicting evidence appeared
|
|
1672
|
+
v.literal("assumption"),
|
|
1673
|
+
// Taken as given, not actively tested
|
|
1674
|
+
v.literal("deprecated")
|
|
1675
|
+
// Superseded or abandoned
|
|
1676
|
+
);
|
|
1677
|
+
var beliefStatus = v.union(
|
|
1678
|
+
v.literal("assumption"),
|
|
1679
|
+
v.literal("hypothesis"),
|
|
1680
|
+
v.literal("belief"),
|
|
1681
|
+
v.literal("fact")
|
|
1682
|
+
);
|
|
1683
|
+
var reversibility = v.union(
|
|
1684
|
+
v.literal("irreversible"),
|
|
1685
|
+
// One-way door decision
|
|
1686
|
+
v.literal("hard_to_reverse"),
|
|
1687
|
+
// Significant cost to undo
|
|
1688
|
+
v.literal("reversible"),
|
|
1689
|
+
// Can change course with moderate effort
|
|
1690
|
+
v.literal("trivial")
|
|
1691
|
+
// Easy to adjust
|
|
1692
|
+
);
|
|
1693
|
+
var predictionOutcome = v.union(
|
|
1694
|
+
v.literal("pending"),
|
|
1695
|
+
v.literal("confirmed"),
|
|
1696
|
+
v.literal("disconfirmed"),
|
|
1697
|
+
v.literal("partial"),
|
|
1698
|
+
v.literal("expired")
|
|
1699
|
+
);
|
|
1700
|
+
var predictionMeta = v.object({
|
|
1701
|
+
isPrediction: v.boolean(),
|
|
1702
|
+
registeredAt: v.number(),
|
|
1703
|
+
// When prediction was made
|
|
1704
|
+
expectedBy: v.optional(v.number()),
|
|
1705
|
+
// When we expect resolution
|
|
1706
|
+
outcome: v.optional(predictionOutcome),
|
|
1707
|
+
outcomeRecordedAt: v.optional(v.number()),
|
|
1708
|
+
outcomeEvidenceId: v.optional(v.string()),
|
|
1709
|
+
// globalId of confirming evidence
|
|
1710
|
+
confidenceAtPrediction: v.optional(v.number()),
|
|
1711
|
+
// 0-1
|
|
1712
|
+
actualVsPredicted: v.optional(v.string())
|
|
1713
|
+
// Notes on how outcome compared
|
|
1714
|
+
});
|
|
1715
|
+
var methodology = v.union(
|
|
1716
|
+
// Primary Research (high value)
|
|
1717
|
+
v.literal("primary_research"),
|
|
1718
|
+
// Direct investigation
|
|
1719
|
+
v.literal("expert_interview"),
|
|
1720
|
+
// Expert call/interview
|
|
1721
|
+
v.literal("customer_interview"),
|
|
1722
|
+
// Customer research
|
|
1723
|
+
v.literal("field_observation"),
|
|
1724
|
+
// On-site observation
|
|
1725
|
+
v.literal("proprietary_data"),
|
|
1726
|
+
// Internal data analysis
|
|
1727
|
+
// Secondary Research
|
|
1728
|
+
v.literal("desk_research"),
|
|
1729
|
+
// Public sources
|
|
1730
|
+
v.literal("regulatory_filing"),
|
|
1731
|
+
// SEC, regulatory docs
|
|
1732
|
+
v.literal("news_article"),
|
|
1733
|
+
// News/press
|
|
1734
|
+
v.literal("academic_paper"),
|
|
1735
|
+
// Academic research
|
|
1736
|
+
// AI-Assisted
|
|
1737
|
+
v.literal("ai_synthesis"),
|
|
1738
|
+
// AI-generated synthesis
|
|
1739
|
+
v.literal("ai_extraction")
|
|
1740
|
+
// AI-extracted from source
|
|
1741
|
+
);
|
|
1742
|
+
var informationAsymmetry = v.union(
|
|
1743
|
+
v.literal("proprietary"),
|
|
1744
|
+
// Only we have this
|
|
1745
|
+
v.literal("early"),
|
|
1746
|
+
// We're early but others will get it
|
|
1747
|
+
v.literal("common")
|
|
1748
|
+
// Everyone has access
|
|
1749
|
+
);
|
|
1750
|
+
var temporalNature = v.union(
|
|
1751
|
+
v.literal("factual"),
|
|
1752
|
+
// Resolved outcome. Grounded in reality.
|
|
1753
|
+
v.literal("forecast"),
|
|
1754
|
+
// Prediction. Will resolve. Discounted weight.
|
|
1755
|
+
v.literal("unknown")
|
|
1756
|
+
// Not yet classified.
|
|
1757
|
+
);
|
|
1758
|
+
var questionType = v.union(
|
|
1759
|
+
v.literal("validation"),
|
|
1760
|
+
// Does evidence support this belief?
|
|
1761
|
+
v.literal("falsification"),
|
|
1762
|
+
// What would prove this belief wrong?
|
|
1763
|
+
v.literal("assumption_probe"),
|
|
1764
|
+
// Is this unstated assumption true?
|
|
1765
|
+
v.literal("prediction_test"),
|
|
1766
|
+
// Will this predicted outcome occur?
|
|
1767
|
+
v.literal("counterfactual"),
|
|
1768
|
+
// What would we expect if X were false?
|
|
1769
|
+
v.literal("discovery"),
|
|
1770
|
+
// What don't we know yet?
|
|
1771
|
+
v.literal("clarification"),
|
|
1772
|
+
// What does X actually mean?
|
|
1773
|
+
v.literal("comparison"),
|
|
1774
|
+
// How does X compare to Y?
|
|
1775
|
+
v.literal("causal"),
|
|
1776
|
+
// What caused X?
|
|
1777
|
+
v.literal("mechanism"),
|
|
1778
|
+
// How does X work?
|
|
1779
|
+
v.literal("general")
|
|
1780
|
+
// Unclassified
|
|
1781
|
+
);
|
|
1782
|
+
var questionPriority = v.union(
|
|
1783
|
+
v.literal("critical"),
|
|
1784
|
+
// Blocks decision-making
|
|
1785
|
+
v.literal("high"),
|
|
1786
|
+
// Important for thesis
|
|
1787
|
+
v.literal("medium"),
|
|
1788
|
+
// Would be nice to know
|
|
1789
|
+
v.literal("low")
|
|
1790
|
+
// Background/curiosity
|
|
1791
|
+
);
|
|
1792
|
+
var answerQuality = v.union(
|
|
1793
|
+
v.literal("definitive"),
|
|
1794
|
+
// Clear, well-supported
|
|
1795
|
+
v.literal("strong"),
|
|
1796
|
+
// Good evidence, high confidence
|
|
1797
|
+
v.literal("moderate"),
|
|
1798
|
+
// Some evidence
|
|
1799
|
+
v.literal("weak"),
|
|
1800
|
+
// Limited evidence
|
|
1801
|
+
v.literal("speculative"),
|
|
1802
|
+
// Mostly conjecture
|
|
1803
|
+
v.literal("unanswered")
|
|
1804
|
+
// No answer yet
|
|
1805
|
+
);
|
|
1806
|
+
var consensusView = v.union(
|
|
1807
|
+
v.literal("aligned"),
|
|
1808
|
+
// We agree with market consensus
|
|
1809
|
+
v.literal("ahead_of"),
|
|
1810
|
+
// We see this before consensus does
|
|
1811
|
+
v.literal("contrarian"),
|
|
1812
|
+
// We actively disagree with consensus
|
|
1813
|
+
v.literal("orthogonal"),
|
|
1814
|
+
// We're looking at something consensus isn't discussing
|
|
1815
|
+
v.literal("unknown")
|
|
1816
|
+
// We don't know what consensus thinks
|
|
1817
|
+
);
|
|
1818
|
+
var themeConviction = v.union(
|
|
1819
|
+
v.literal("high"),
|
|
1820
|
+
// Strong conviction, actively deploying
|
|
1821
|
+
v.literal("medium"),
|
|
1822
|
+
// Building conviction
|
|
1823
|
+
v.literal("low"),
|
|
1824
|
+
// Exploring, not convicted
|
|
1825
|
+
v.literal("negative")
|
|
1826
|
+
// Actively avoiding
|
|
1827
|
+
);
|
|
1828
|
+
var decisionType = v.union(
|
|
1829
|
+
v.literal("invest"),
|
|
1830
|
+
v.literal("pass"),
|
|
1831
|
+
v.literal("follow_on"),
|
|
1832
|
+
v.literal("exit"),
|
|
1833
|
+
v.literal("deep_dive"),
|
|
1834
|
+
v.literal("monitor"),
|
|
1835
|
+
v.literal("deprioritize"),
|
|
1836
|
+
v.literal("thesis_adopt"),
|
|
1837
|
+
v.literal("thesis_revise"),
|
|
1838
|
+
v.literal("thesis_abandon")
|
|
1839
|
+
);
|
|
1840
|
+
var decisionOutcome = v.union(
|
|
1841
|
+
v.literal("pending"),
|
|
1842
|
+
v.literal("successful"),
|
|
1843
|
+
v.literal("unsuccessful"),
|
|
1844
|
+
v.literal("mixed"),
|
|
1845
|
+
v.literal("unknown")
|
|
1846
|
+
);
|
|
1847
|
+
var externalIds2 = v.object({
|
|
1848
|
+
crunchbase: v.optional(v.string()),
|
|
1849
|
+
linkedin: v.optional(v.string()),
|
|
1850
|
+
pitchbook: v.optional(v.string()),
|
|
1851
|
+
twitter: v.optional(v.string()),
|
|
1852
|
+
website: v.optional(v.string())
|
|
1853
|
+
});
|
|
1854
|
+
defineTable({
|
|
1855
|
+
// === IDENTITY ===
|
|
1856
|
+
globalId: v.string(),
|
|
1857
|
+
// UUID - survives migration to Neo4j
|
|
1858
|
+
// === TYPE ===
|
|
1859
|
+
nodeType,
|
|
1860
|
+
// === EPISTEMIC LAYER ===
|
|
1861
|
+
epistemicLayer: v.optional(epistemicLayer),
|
|
1862
|
+
// === SUBTYPE (for typed entities) ===
|
|
1863
|
+
subtype: v.optional(v.string()),
|
|
1864
|
+
// company: private|corporate|portfolio, investor: vc|lp|cvc|pe|family_office|angel
|
|
1865
|
+
// === CONTENT ===
|
|
1866
|
+
canonicalText: v.string(),
|
|
1867
|
+
// The core content (belief statement, company name, etc.)
|
|
1868
|
+
contentHash: v.string(),
|
|
1869
|
+
// SHA256(nodeType + canonicalText) for deduplication
|
|
1870
|
+
// Extended content (for sources/syntheses)
|
|
1871
|
+
content: v.optional(v.string()),
|
|
1872
|
+
// Full text for documents/articles
|
|
1873
|
+
contentType: v.optional(v.string()),
|
|
1874
|
+
// "markdown", "html", "pdf", "text"
|
|
1875
|
+
// === METADATA ===
|
|
1876
|
+
title: v.optional(v.string()),
|
|
1877
|
+
// Display title
|
|
1878
|
+
tags: v.optional(v.array(v.string())),
|
|
1879
|
+
domain: v.optional(v.string()),
|
|
1880
|
+
// For companies: website domain
|
|
1881
|
+
// Type-specific metadata (flexible object - LEGACY)
|
|
1882
|
+
// New code should use the typed fields below when available
|
|
1883
|
+
metadata: v.optional(looseJsonObject),
|
|
1884
|
+
// === POLICY / ENTITLEMENT ===
|
|
1885
|
+
tenantId: v.optional(v.string()),
|
|
1886
|
+
workspaceId: v.optional(v.string()),
|
|
1887
|
+
ownerPrincipalId: v.optional(v.string()),
|
|
1888
|
+
audienceLabel: v.optional(audienceLabel),
|
|
1889
|
+
policyTags: v.optional(v.array(v.string())),
|
|
1890
|
+
sensitivityTier: v.optional(sensitivityTier),
|
|
1891
|
+
exportClass: v.optional(exportClass),
|
|
1892
|
+
anonymizationClass: v.optional(anonymizationClass),
|
|
1893
|
+
// === PUBLICATION (visibility-based, not copy-based) ===
|
|
1894
|
+
// Publication expands who can see a workspace-local node — the node stays
|
|
1895
|
+
// in its workspace, like a microservice exposing part of its API surface.
|
|
1896
|
+
// Rules-based: pack/tenant-level publicationRules auto-evaluate on
|
|
1897
|
+
// confidence changes and node creation. No manual click-by-click.
|
|
1898
|
+
publicationStatus: v.optional(
|
|
1899
|
+
v.union(
|
|
1900
|
+
v.literal("unpublished"),
|
|
1901
|
+
// Default: workspace-local only
|
|
1902
|
+
v.literal("published"),
|
|
1903
|
+
// Visible at tenant scope (rules matched)
|
|
1904
|
+
v.literal("suppressed")
|
|
1905
|
+
// Manually blocked even if rules match
|
|
1906
|
+
)
|
|
1907
|
+
),
|
|
1908
|
+
publishedAt: v.optional(v.number()),
|
|
1909
|
+
// When publication status last changed to published
|
|
1910
|
+
publishedBy: v.optional(v.string()),
|
|
1911
|
+
// userId or "system:publication_rules" for auto-publish
|
|
1912
|
+
// === TYPED METADATA FIELDS ===
|
|
1913
|
+
// --- Belief ---
|
|
1914
|
+
// Belief type — validated against schemaEnumConfig category "belief_type"
|
|
1915
|
+
// Platform core: hypothesis, belief, principle, invariant, assumption,
|
|
1916
|
+
// tenet, prior, preference, goal, forecast
|
|
1917
|
+
beliefType: v.optional(v.string()),
|
|
1918
|
+
beliefStatus: v.optional(beliefStatus),
|
|
1919
|
+
epistemicStatus: v.optional(epistemicStatus),
|
|
1920
|
+
reversibility: v.optional(reversibility),
|
|
1921
|
+
predictionMeta: v.optional(predictionMeta),
|
|
1922
|
+
// Consensus tracking (for non-consensus detection)
|
|
1923
|
+
consensusView: v.optional(consensusView),
|
|
1924
|
+
consensusConfidence: v.optional(v.number()),
|
|
1925
|
+
// 0-1: What we think consensus confidence is
|
|
1926
|
+
consensusSource: v.optional(v.string()),
|
|
1927
|
+
// Where we got the consensus view (twitter, reports, etc.)
|
|
1928
|
+
// --- Evidence ---
|
|
1929
|
+
methodology: v.optional(methodology),
|
|
1930
|
+
informationAsymmetry: v.optional(informationAsymmetry),
|
|
1931
|
+
temporalNature: v.optional(temporalNature),
|
|
1932
|
+
// --- Question ---
|
|
1933
|
+
questionType: v.optional(questionType),
|
|
1934
|
+
questionPriority: v.optional(questionPriority),
|
|
1935
|
+
answerQuality: v.optional(answerQuality),
|
|
1936
|
+
// --- Theme ---
|
|
1937
|
+
themeConviction: v.optional(themeConviction),
|
|
1938
|
+
// Market timing (for "early on theme" detection)
|
|
1939
|
+
marketAwarenessDate: v.optional(v.number()),
|
|
1940
|
+
// When this theme became broadly discussed
|
|
1941
|
+
marketAwarenessSource: v.optional(v.string()),
|
|
1942
|
+
// How we know (first major report, twitter volume spike, etc.)
|
|
1943
|
+
earlySignalIds: v.optional(v.array(v.string())),
|
|
1944
|
+
// globalIds of evidence we had before market awareness
|
|
1945
|
+
// --- Decision ---
|
|
1946
|
+
decisionType: v.optional(decisionType),
|
|
1947
|
+
decisionOutcome: v.optional(decisionOutcome),
|
|
1948
|
+
// === EXTERNAL IDS (for ontological entities) ===
|
|
1949
|
+
externalIds: v.optional(externalIds2),
|
|
1950
|
+
// === PROVENANCE ===
|
|
1951
|
+
sourceType: sourceType2,
|
|
1952
|
+
aiProvider: v.optional(v.string()),
|
|
1953
|
+
// "claude", "gemini", "gpt-4", etc.
|
|
1954
|
+
extractedFromNodeId: v.optional(v.id("epistemicNodes")),
|
|
1955
|
+
// Quick reference to source
|
|
1956
|
+
// === EXTRACTION CONTEXT ===
|
|
1957
|
+
extractionModel: v.optional(v.string()),
|
|
1958
|
+
// "claude-sonnet-4-20250514"
|
|
1959
|
+
extractionPromptName: v.optional(v.string()),
|
|
1960
|
+
// "lucern/extract-evidence"
|
|
1961
|
+
extractionPromptVersion: v.optional(v.number()),
|
|
1962
|
+
extractionTemperature: v.optional(v.number()),
|
|
1963
|
+
extractionLangfuseTraceId: v.optional(v.string()),
|
|
1964
|
+
// === GROUNDING VERIFICATION ===
|
|
1965
|
+
groundingVerified: v.optional(v.boolean()),
|
|
1966
|
+
groundingConfidence: v.optional(v.number()),
|
|
1967
|
+
// 0-1 match quality
|
|
1968
|
+
groundingMatchedText: v.optional(v.string()),
|
|
1969
|
+
// Actual text from source
|
|
1970
|
+
groundingStartOffset: v.optional(v.number()),
|
|
1971
|
+
groundingEndOffset: v.optional(v.number()),
|
|
1972
|
+
groundingRejectionReason: v.optional(v.string()),
|
|
1973
|
+
// === CONFIDENCE & VERIFICATION ===
|
|
1974
|
+
confidence: v.optional(v.number()),
|
|
1975
|
+
// 0-1 projected probability P(x) = b + a*u
|
|
1976
|
+
verificationStatus: v.optional(verificationStatus),
|
|
1977
|
+
// === SL OPINION (Subjective Logic — Kernel v2) ===
|
|
1978
|
+
// Replaces scalar confidence with rich epistemic state.
|
|
1979
|
+
// b + d + u = 1. P(x) = b + a*u is stored in `confidence` for backward compat.
|
|
1980
|
+
opinion_b: v.optional(v.number()),
|
|
1981
|
+
// Belief: evidence FOR (0-1)
|
|
1982
|
+
opinion_d: v.optional(v.number()),
|
|
1983
|
+
// Disbelief: evidence AGAINST (0-1)
|
|
1984
|
+
opinion_u: v.optional(v.number()),
|
|
1985
|
+
// Uncertainty: absence of evidence (0-1)
|
|
1986
|
+
opinion_a: v.optional(v.number()),
|
|
1987
|
+
// Base rate / prior probability (0-1)
|
|
1988
|
+
tupleContradicted: v.optional(v.boolean()),
|
|
1989
|
+
// Single-belief tuple-space contradiction flag
|
|
1990
|
+
// === LIFECYCLE ===
|
|
1991
|
+
status: nodeStatus,
|
|
1992
|
+
supersededBy: v.optional(v.id("epistemicNodes")),
|
|
1993
|
+
// === OWNERSHIP ===
|
|
1994
|
+
topicId: v.optional(v.string()),
|
|
1995
|
+
// Canonical scope container (topic-first model)
|
|
1996
|
+
projectId: v.optional(v.string()),
|
|
1997
|
+
// DEPRECATED: Use belongs_to edges
|
|
1998
|
+
createdBy: v.string(),
|
|
1999
|
+
// Clerk user ID
|
|
2000
|
+
createdAt: v.number(),
|
|
2001
|
+
updatedAt: v.number(),
|
|
2002
|
+
// === NEO4J SYNC STATUS ===
|
|
2003
|
+
syncStatus: v.optional(syncStatus),
|
|
2004
|
+
syncError: v.optional(v.string())
|
|
2005
|
+
// Error message if sync failed
|
|
2006
|
+
}).index("by_globalId", ["globalId"]).index("by_contentHash", ["contentHash"]).index("by_nodeType", ["nodeType"]).index("by_subtype", ["nodeType", "subtype"]).index("by_domain", ["domain"]).index("by_project", ["projectId"]).index("by_project_type", ["projectId", "nodeType"]).index("by_topic", ["topicId"]).index("by_topic_type", ["topicId", "nodeType"]).index("by_tenantId", ["tenantId"]).index("by_workspaceId", ["workspaceId"]).index("by_tenant_workspace", ["tenantId", "workspaceId"]).index("by_audienceLabel", ["audienceLabel"]).index("by_sensitivityTier", ["sensitivityTier"]).index("by_exportClass", ["exportClass"]).index("by_status", ["status"]).index("by_sourceType", ["sourceType"]).index("by_verification", ["verificationStatus"]).index("by_layer", ["epistemicLayer"]).index("by_layer_type", ["epistemicLayer", "nodeType"]).index("by_syncStatus", ["syncStatus"]).index("by_publicationStatus", ["publicationStatus"]).index("by_tenant_publicationStatus", ["tenantId", "publicationStatus"]).index("by_belief_status", ["nodeType", "beliefStatus"]).index("by_epistemic_status", ["nodeType", "epistemicStatus"]).index("by_temporal_nature", ["nodeType", "temporalNature"]).index("by_methodology", ["nodeType", "methodology"]).index("by_reversibility", ["nodeType", "reversibility"]).index("by_questionType", ["nodeType", "questionType"]).index("by_questionPriority", ["nodeType", "questionPriority"]).searchIndex("search_canonicalText", {
|
|
2007
|
+
searchField: "canonicalText",
|
|
2008
|
+
filterFields: ["nodeType", "projectId", "topicId", "status"]
|
|
2009
|
+
});
|
|
2010
|
+
function getLayerForNodeType(type) {
|
|
2011
|
+
switch (type) {
|
|
2012
|
+
case "decision":
|
|
2013
|
+
return "L4";
|
|
2014
|
+
case "belief":
|
|
2015
|
+
case "question":
|
|
2016
|
+
case "theme":
|
|
2017
|
+
case "deal":
|
|
2018
|
+
return "L3";
|
|
2019
|
+
case "claim":
|
|
2020
|
+
case "evidence":
|
|
2021
|
+
case "synthesis":
|
|
2022
|
+
case "answer":
|
|
2023
|
+
return "L2";
|
|
2024
|
+
case "atomic_fact":
|
|
2025
|
+
case "excerpt":
|
|
2026
|
+
case "source":
|
|
2027
|
+
return "L1";
|
|
2028
|
+
case "topic":
|
|
2029
|
+
return "organizational";
|
|
2030
|
+
case "company":
|
|
2031
|
+
case "person":
|
|
2032
|
+
case "investor":
|
|
2033
|
+
case "function":
|
|
2034
|
+
case "value_chain":
|
|
2035
|
+
return "ontological";
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
// src/workspaceIsolation.ts
|
|
2040
|
+
function normalizeScopeValue2(value) {
|
|
2041
|
+
if (typeof value !== "string") {
|
|
2042
|
+
return;
|
|
2043
|
+
}
|
|
2044
|
+
const normalized = value.trim();
|
|
2045
|
+
return normalized.length > 0 ? normalized : void 0;
|
|
2046
|
+
}
|
|
2047
|
+
function throwWorkspaceIsolationError(args) {
|
|
2048
|
+
const error = new Error(args.message);
|
|
2049
|
+
error.status = 409;
|
|
2050
|
+
error.code = "INVARIANT_VIOLATION";
|
|
2051
|
+
error.invariantCode = args.invariantCode;
|
|
2052
|
+
error.suggestion = args.suggestion;
|
|
2053
|
+
error.details = args.details;
|
|
2054
|
+
throw error;
|
|
2055
|
+
}
|
|
2056
|
+
function assertWorkspaceScopedEpistemicNodeScope(args) {
|
|
2057
|
+
const layer = getLayerForNodeType(args.nodeType);
|
|
2058
|
+
if (layer === "ontological") {
|
|
2059
|
+
return;
|
|
2060
|
+
}
|
|
2061
|
+
const workspaceId = normalizeScopeValue2(args.scope.workspaceId);
|
|
2062
|
+
if (workspaceId) {
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
throwWorkspaceIsolationError({
|
|
2066
|
+
message: "Workspace-scoped reasoning isolation requires workspaceId on non-ontological node creation.",
|
|
2067
|
+
invariantCode: "workspace.scope_required_for_epistemic_nodes",
|
|
2068
|
+
suggestion: "Resolve the topic/project scope through a workspace-bound topic before creating epistemic nodes.",
|
|
2069
|
+
details: {
|
|
2070
|
+
mutationName: args.mutationName,
|
|
2071
|
+
nodeType: args.nodeType,
|
|
2072
|
+
topicId: args.scope.topicId,
|
|
2073
|
+
projectId: args.scope.projectId
|
|
2074
|
+
}
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
function nodeMatchesWorkspaceReasoningScope(node, scope) {
|
|
2078
|
+
if (!node) {
|
|
2079
|
+
return false;
|
|
2080
|
+
}
|
|
2081
|
+
const scopeTenantId = normalizeScopeValue2(scope.tenantId);
|
|
2082
|
+
const scopeWorkspaceId = normalizeScopeValue2(scope.workspaceId);
|
|
2083
|
+
const nodeTenantId = normalizeScopeValue2(node.tenantId);
|
|
2084
|
+
const nodeWorkspaceId = normalizeScopeValue2(node.workspaceId);
|
|
2085
|
+
const epistemicLayer2 = typeof node.epistemicLayer === "string" ? node.epistemicLayer : void 0;
|
|
2086
|
+
if (scopeTenantId && nodeTenantId && scopeTenantId !== nodeTenantId) {
|
|
2087
|
+
return false;
|
|
2088
|
+
}
|
|
2089
|
+
if (epistemicLayer2 === "ontological" && nodeWorkspaceId === void 0) {
|
|
2090
|
+
return true;
|
|
2091
|
+
}
|
|
2092
|
+
if (!scopeWorkspaceId && node.publicationStatus === "published") {
|
|
2093
|
+
return true;
|
|
2094
|
+
}
|
|
2095
|
+
if (!scopeWorkspaceId) {
|
|
2096
|
+
return nodeWorkspaceId === void 0;
|
|
2097
|
+
}
|
|
2098
|
+
return scopeWorkspaceId === nodeWorkspaceId;
|
|
2099
|
+
}
|
|
2100
|
+
function resolveRuntimePackMutationContext(args) {
|
|
2101
|
+
if (!args.runtimeToolName && !args.runtimePackKey && !args.runtimePackInstallScope) {
|
|
2102
|
+
return;
|
|
2103
|
+
}
|
|
2104
|
+
return {
|
|
2105
|
+
toolName: args.runtimeToolName,
|
|
2106
|
+
packKey: args.runtimePackKey,
|
|
2107
|
+
packInstallScope: args.runtimePackInstallScope
|
|
2108
|
+
};
|
|
2109
|
+
}
|
|
2110
|
+
function assertTenantPackWorkspaceMutationAllowed(args) {
|
|
2111
|
+
if (!args.runtime?.packKey || args.runtime.packInstallScope !== "tenant") {
|
|
2112
|
+
return;
|
|
2113
|
+
}
|
|
2114
|
+
const targetWorkspaceId = normalizeScopeValue2(args.target.workspaceId);
|
|
2115
|
+
const targetLayer = typeof args.target.epistemicLayer === "string" ? args.target.epistemicLayer : void 0;
|
|
2116
|
+
if (!targetWorkspaceId || targetLayer === "ontological") {
|
|
2117
|
+
return;
|
|
2118
|
+
}
|
|
2119
|
+
throwWorkspaceIsolationError({
|
|
2120
|
+
message: `Tenant-scoped pack "${args.runtime.packKey}" cannot mutate workspace-scoped reasoning state.`,
|
|
2121
|
+
invariantCode: "workspace.tenant_pack_reasoning_write_forbidden",
|
|
2122
|
+
suggestion: "Use a workspace-scoped pack for workspace-local graph mutations, or route the change through tenant-global canonical entity flows.",
|
|
2123
|
+
details: {
|
|
2124
|
+
mutationName: args.mutationName,
|
|
2125
|
+
toolName: args.runtime.toolName,
|
|
2126
|
+
packKey: args.runtime.packKey,
|
|
2127
|
+
targetWorkspaceId,
|
|
2128
|
+
targetNodeType: args.target.nodeType,
|
|
2129
|
+
targetLayer
|
|
2130
|
+
}
|
|
2131
|
+
});
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
// src/epistemicEvidence.ts
|
|
2135
|
+
function generateContentHash(text) {
|
|
2136
|
+
const content2 = `evidence:${text.trim().toLowerCase().replace(/\s+/g, " ").slice(0, 500)}`;
|
|
2137
|
+
let hash = 5381;
|
|
2138
|
+
for (let i = 0; i < content2.length; i++) {
|
|
2139
|
+
hash = (hash << 5) + hash + content2.charCodeAt(i);
|
|
2140
|
+
hash &= hash;
|
|
2141
|
+
}
|
|
2142
|
+
return Math.abs(hash).toString(16).padStart(8, "0");
|
|
2143
|
+
}
|
|
2144
|
+
function normalizeKind(kind) {
|
|
2145
|
+
if (!kind) {
|
|
2146
|
+
return "observation";
|
|
2147
|
+
}
|
|
2148
|
+
const validKinds = [
|
|
2149
|
+
"fact",
|
|
2150
|
+
"observation",
|
|
2151
|
+
"claim",
|
|
2152
|
+
"quote",
|
|
2153
|
+
"statistic",
|
|
2154
|
+
"signal"
|
|
2155
|
+
];
|
|
2156
|
+
return validKinds.find((k) => kind.toLowerCase() === k) || "observation";
|
|
2157
|
+
}
|
|
2158
|
+
function normalizeSourceType(sourceType3) {
|
|
2159
|
+
if (sourceType3 === "proprietary" || sourceType3 === "verified") {
|
|
2160
|
+
return sourceType3;
|
|
2161
|
+
}
|
|
2162
|
+
return "ai_generated";
|
|
2163
|
+
}
|
|
2164
|
+
async function markProjectGraphDirty(ctx, projectId, topicId) {
|
|
2165
|
+
const normalizedProjectId = typeof projectId === "string" && projectId.trim().length > 0 ? projectId : void 0;
|
|
2166
|
+
const normalizedTopicId = typeof topicId === "string" && topicId.trim().length > 0 ? topicId : void 0;
|
|
2167
|
+
if (!normalizedProjectId && !normalizedTopicId) {
|
|
2168
|
+
return;
|
|
2169
|
+
}
|
|
2170
|
+
if (normalizedProjectId) {
|
|
2171
|
+
await ctx.scheduler.runAfter(
|
|
2172
|
+
0,
|
|
2173
|
+
internal.graphAnalysisCache.markCacheStaleInternal,
|
|
2174
|
+
{
|
|
2175
|
+
projectId: normalizedProjectId
|
|
2176
|
+
}
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
if (normalizedTopicId) {
|
|
2180
|
+
await ctx.scheduler.runAfter(
|
|
2181
|
+
0,
|
|
2182
|
+
internal.graphAnalysisCache.markCacheStaleByTopic,
|
|
2183
|
+
{
|
|
2184
|
+
topicId: normalizedTopicId
|
|
2185
|
+
}
|
|
2186
|
+
);
|
|
2187
|
+
}
|
|
2188
|
+
await resolveGraphPrimitivesAppResolvers().patchProject(
|
|
2189
|
+
ctx,
|
|
2190
|
+
normalizedTopicId ?? normalizedProjectId,
|
|
2191
|
+
{
|
|
2192
|
+
lastActivityAt: Date.now()
|
|
2193
|
+
}
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2196
|
+
var DEFAULT_EVIDENCE_PAGE_SIZE = 250;
|
|
2197
|
+
var MAX_EVIDENCE_PAGE_SIZE = 1e3;
|
|
2198
|
+
var LEGACY_SPRINT_LINK_KEY = "linkedSprintId";
|
|
2199
|
+
var optionalEvidenceScopeArgs = optionalScopeArgs;
|
|
2200
|
+
function clampEvidenceLimit(limit, fallback = DEFAULT_EVIDENCE_PAGE_SIZE) {
|
|
2201
|
+
if (!Number.isFinite(limit)) {
|
|
2202
|
+
return fallback;
|
|
2203
|
+
}
|
|
2204
|
+
return Math.max(
|
|
2205
|
+
1,
|
|
2206
|
+
Math.min(Math.floor(limit), MAX_EVIDENCE_PAGE_SIZE)
|
|
2207
|
+
);
|
|
2208
|
+
}
|
|
2209
|
+
function dedupeEvidenceNodes(nodes) {
|
|
2210
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2211
|
+
const deduped = [];
|
|
2212
|
+
for (const node of nodes) {
|
|
2213
|
+
const id = String(node._id);
|
|
2214
|
+
if (seen.has(id)) {
|
|
2215
|
+
continue;
|
|
2216
|
+
}
|
|
2217
|
+
seen.add(id);
|
|
2218
|
+
deduped.push(node);
|
|
2219
|
+
}
|
|
2220
|
+
return deduped;
|
|
2221
|
+
}
|
|
2222
|
+
function evidenceMatchesScope(node, scope) {
|
|
2223
|
+
return scope.topicId !== void 0 && node.topicId === scope.topicId || scope.projectId !== void 0 && node.projectId === scope.projectId;
|
|
2224
|
+
}
|
|
2225
|
+
function resolveEvidenceLinkedWorktreeId(metadata) {
|
|
2226
|
+
const worktreeId = metadata?.linkedWorktreeId;
|
|
2227
|
+
if (typeof worktreeId === "string" && worktreeId.trim().length > 0) {
|
|
2228
|
+
return worktreeId;
|
|
2229
|
+
}
|
|
2230
|
+
const sprintId = metadata?.[LEGACY_SPRINT_LINK_KEY];
|
|
2231
|
+
return typeof sprintId === "string" && sprintId.trim().length > 0 ? sprintId : void 0;
|
|
2232
|
+
}
|
|
2233
|
+
async function resolveEvidenceScopeOrNull(ctx, args) {
|
|
2234
|
+
if (!args.projectId && !args.topicId) {
|
|
2235
|
+
return null;
|
|
2236
|
+
}
|
|
2237
|
+
try {
|
|
2238
|
+
return await resolveTopicProjectScope(ctx, {
|
|
2239
|
+
projectId: args.projectId ?? void 0,
|
|
2240
|
+
topicId: args.topicId ?? void 0
|
|
2241
|
+
});
|
|
2242
|
+
} catch {
|
|
2243
|
+
return null;
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
async function getEvidenceNodesForScope(ctx, scope, args) {
|
|
2247
|
+
const scanLimit = typeof args?.scanLimit === "number" ? args.scanLimit : void 0;
|
|
2248
|
+
const [topicNodes, projectNodes] = await Promise.all([
|
|
2249
|
+
scope.topicId ? scanLimit ? ctx.db.query("epistemicNodes").withIndex(
|
|
2250
|
+
"by_topic_type",
|
|
2251
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "evidence")
|
|
2252
|
+
).order("desc").take(scanLimit) : ctx.db.query("epistemicNodes").withIndex(
|
|
2253
|
+
"by_topic_type",
|
|
2254
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "evidence")
|
|
2255
|
+
).collect() : Promise.resolve([]),
|
|
2256
|
+
scope.projectId ? scanLimit ? ctx.db.query("epistemicNodes").withIndex(
|
|
2257
|
+
"by_project_type",
|
|
2258
|
+
(q) => q.eq("projectId", scope.projectId).eq("nodeType", "evidence")
|
|
2259
|
+
).order("desc").take(scanLimit) : ctx.db.query("epistemicNodes").withIndex(
|
|
2260
|
+
"by_project_type",
|
|
2261
|
+
(q) => q.eq("projectId", scope.projectId).eq("nodeType", "evidence")
|
|
2262
|
+
).collect() : Promise.resolve([])
|
|
2263
|
+
]);
|
|
2264
|
+
return dedupeEvidenceNodes([...topicNodes, ...projectNodes]).filter(
|
|
2265
|
+
(node) => evidenceMatchesScope(node, scope)
|
|
2266
|
+
);
|
|
2267
|
+
}
|
|
2268
|
+
function createEvidenceAudienceResolver(registryRows) {
|
|
2269
|
+
const audienceClassByKey = new Map(
|
|
2270
|
+
registryRows.map((row) => [
|
|
2271
|
+
normalizeAudienceKey(row.audienceKey),
|
|
2272
|
+
row.audienceClass
|
|
2273
|
+
])
|
|
2274
|
+
);
|
|
2275
|
+
return (audienceKey, fallback) => {
|
|
2276
|
+
const key = normalizeAudienceKey(audienceKey);
|
|
2277
|
+
if (!key) {
|
|
2278
|
+
return fallback;
|
|
2279
|
+
}
|
|
2280
|
+
return audienceClassByKey.get(key) ?? classFromAudienceKey(key, fallback);
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
2283
|
+
function flattenEvidenceNode(n) {
|
|
2284
|
+
const meta = n.metadata || {};
|
|
2285
|
+
const linkedWorktreeId = resolveEvidenceLinkedWorktreeId(meta);
|
|
2286
|
+
return {
|
|
2287
|
+
...n,
|
|
2288
|
+
text: n.canonicalText || "",
|
|
2289
|
+
title: n.title || "",
|
|
2290
|
+
kind: meta.kind || "observation",
|
|
2291
|
+
tags: meta.tags || [],
|
|
2292
|
+
sourceQuestionId: meta.sourceQuestionId || void 0,
|
|
2293
|
+
linkedBeliefId: meta.linkedBeliefNodeId || meta.linkedBeliefId || void 0,
|
|
2294
|
+
linkedWorktreeId,
|
|
2295
|
+
[LEGACY_SPRINT_LINK_KEY]: meta[LEGACY_SPRINT_LINK_KEY] || void 0,
|
|
2296
|
+
sprintIndex: meta.sprintIndex || void 0,
|
|
2297
|
+
externalSourceUrl: meta.sourceUrl || void 0,
|
|
2298
|
+
externalSourceType: meta.externalSourceType || void 0
|
|
2299
|
+
};
|
|
2300
|
+
}
|
|
2301
|
+
var create = mutation({
|
|
2302
|
+
args: {
|
|
2303
|
+
...optionalEvidenceScopeArgs,
|
|
2304
|
+
text: v.string(),
|
|
2305
|
+
title: v.optional(v.string()),
|
|
2306
|
+
content: v.optional(v.string()),
|
|
2307
|
+
contentType: v.optional(v.string()),
|
|
2308
|
+
kind: v.optional(v.string()),
|
|
2309
|
+
tags: v.optional(v.array(v.string())),
|
|
2310
|
+
sourceType: v.optional(v.string()),
|
|
2311
|
+
externalSourceType: v.optional(v.string()),
|
|
2312
|
+
sourceUrl: v.optional(v.string()),
|
|
2313
|
+
sourceQuestionId: v.optional(v.string()),
|
|
2314
|
+
userId: v.string(),
|
|
2315
|
+
// Classification fields (from AI tools)
|
|
2316
|
+
methodology: v.optional(v.string()),
|
|
2317
|
+
informationAsymmetry: v.optional(v.string()),
|
|
2318
|
+
sourceDescription: v.optional(v.string()),
|
|
2319
|
+
metadata: v.optional(v.any()),
|
|
2320
|
+
// Optional linking to beliefs
|
|
2321
|
+
linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
|
|
2322
|
+
evidenceRelation: v.optional(
|
|
2323
|
+
v.union(v.literal("supports"), v.literal("contradicts"))
|
|
2324
|
+
),
|
|
2325
|
+
confidence: v.optional(v.number())
|
|
2326
|
+
},
|
|
2327
|
+
returns: permissiveReturn,
|
|
2328
|
+
handler: async (ctx, args) => {
|
|
2329
|
+
const scope = await resolveTopicProjectScope(ctx, {
|
|
2330
|
+
topicId: args.topicId,
|
|
2331
|
+
projectId: args.projectId
|
|
2332
|
+
});
|
|
2333
|
+
assertWorkspaceScopedEpistemicNodeScope({
|
|
2334
|
+
scope,
|
|
2335
|
+
nodeType: "evidence",
|
|
2336
|
+
mutationName: "epistemicEvidence.create"
|
|
2337
|
+
});
|
|
2338
|
+
if (scope.projectId) {
|
|
2339
|
+
await requireProjectAccess(ctx, scope.projectId, args.userId);
|
|
2340
|
+
}
|
|
2341
|
+
const now = Date.now();
|
|
2342
|
+
const globalId = generateGlobalId();
|
|
2343
|
+
const contentHash = generateContentHash(args.text);
|
|
2344
|
+
const kind = normalizeKind(args.kind);
|
|
2345
|
+
const sourceType3 = normalizeSourceType(args.sourceType);
|
|
2346
|
+
const additionalMetadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
|
|
2347
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
2348
|
+
globalId,
|
|
2349
|
+
topicId: scope.topicId,
|
|
2350
|
+
projectId: scope.projectId,
|
|
2351
|
+
tenantId: scope.tenantId,
|
|
2352
|
+
workspaceId: scope.workspaceId,
|
|
2353
|
+
nodeType: "evidence",
|
|
2354
|
+
canonicalText: args.text,
|
|
2355
|
+
contentHash,
|
|
2356
|
+
...typeof args.title === "string" && args.title.trim().length > 0 ? { title: args.title.trim() } : {},
|
|
2357
|
+
...typeof args.content === "string" && args.content.length > 0 ? { content: args.content } : {},
|
|
2358
|
+
...typeof args.contentType === "string" && args.contentType.trim().length > 0 ? { contentType: args.contentType.trim() } : {},
|
|
2359
|
+
status: "active",
|
|
2360
|
+
epistemicLayer: "L2",
|
|
2361
|
+
// L2: Compression Boundary (Evidence/Claims)
|
|
2362
|
+
sourceType: sourceType3,
|
|
2363
|
+
createdAt: now,
|
|
2364
|
+
updatedAt: now,
|
|
2365
|
+
createdBy: args.userId,
|
|
2366
|
+
metadata: {
|
|
2367
|
+
kind,
|
|
2368
|
+
tags: args.tags || [],
|
|
2369
|
+
externalSourceType: args.externalSourceType,
|
|
2370
|
+
sourceUrl: args.sourceUrl,
|
|
2371
|
+
sourceQuestionId: args.sourceQuestionId,
|
|
2372
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
2373
|
+
evidenceRelation: args.evidenceRelation,
|
|
2374
|
+
confidence: args.confidence,
|
|
2375
|
+
methodology: args.methodology,
|
|
2376
|
+
informationAsymmetry: args.informationAsymmetry,
|
|
2377
|
+
sourceDescription: args.sourceDescription,
|
|
2378
|
+
...additionalMetadata
|
|
2379
|
+
}
|
|
2380
|
+
});
|
|
2381
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
2382
|
+
nodeId,
|
|
2383
|
+
operation: "upsert"
|
|
2384
|
+
});
|
|
2385
|
+
await scheduleEmbeddingGeneration({
|
|
2386
|
+
ctx,
|
|
2387
|
+
nodeId,
|
|
2388
|
+
projectId: scope.projectId,
|
|
2389
|
+
topicId: scope.topicId,
|
|
2390
|
+
createdBy: args.userId,
|
|
2391
|
+
nodeType: "evidence",
|
|
2392
|
+
text: args.text
|
|
2393
|
+
});
|
|
2394
|
+
if (args.linkedBeliefNodeId && args.evidenceRelation) {
|
|
2395
|
+
const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
|
|
2396
|
+
if (beliefNode) {
|
|
2397
|
+
const weight = args.evidenceRelation === "supports" ? 1 : -1;
|
|
2398
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
2399
|
+
globalId: crypto.randomUUID(),
|
|
2400
|
+
fromGlobalId: globalId,
|
|
2401
|
+
toGlobalId: beliefNode.globalId,
|
|
2402
|
+
edgeType: "informs",
|
|
2403
|
+
weight: weight * (args.confidence || 0.7),
|
|
2404
|
+
createdBy: args.userId,
|
|
2405
|
+
topicId: scope.projectId ? String(scope.projectId) : void 0,
|
|
2406
|
+
fromNodeType: "evidence",
|
|
2407
|
+
toNodeType: "belief",
|
|
2408
|
+
fromLayer: "L2",
|
|
2409
|
+
toLayer: "L3",
|
|
2410
|
+
metadata: {
|
|
2411
|
+
relation: args.evidenceRelation,
|
|
2412
|
+
confidence: args.confidence
|
|
2413
|
+
}
|
|
2414
|
+
});
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
await ctx.db.insert("epistemicAudit", {
|
|
2418
|
+
entityType: "evidence",
|
|
2419
|
+
entityId: nodeId,
|
|
2420
|
+
changeType: "created",
|
|
2421
|
+
changedAt: now,
|
|
2422
|
+
changedBy: args.userId,
|
|
2423
|
+
isAgent: false,
|
|
2424
|
+
projectId: scope.projectId,
|
|
2425
|
+
newState: {
|
|
2426
|
+
text: args.text.slice(0, 200),
|
|
2427
|
+
kind,
|
|
2428
|
+
sourceType: sourceType3,
|
|
2429
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
2430
|
+
evidenceRelation: args.evidenceRelation
|
|
2431
|
+
}
|
|
2432
|
+
});
|
|
2433
|
+
if (scope.projectId || scope.topicId) {
|
|
2434
|
+
await ctx.scheduler.runAfter(
|
|
2435
|
+
0,
|
|
2436
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
2437
|
+
{
|
|
2438
|
+
nodeId,
|
|
2439
|
+
projectId: scope.projectId,
|
|
2440
|
+
topicId: scope.topicId ? String(scope.topicId) : void 0,
|
|
2441
|
+
createdBy: args.userId,
|
|
2442
|
+
nodeType: "evidence",
|
|
2443
|
+
text: args.text
|
|
2444
|
+
}
|
|
2445
|
+
);
|
|
2446
|
+
}
|
|
2447
|
+
if (scope.projectId || scope.topicId) {
|
|
2448
|
+
await ctx.scheduler.runAfter(
|
|
2449
|
+
2e3,
|
|
2450
|
+
// 2 second delay
|
|
2451
|
+
internal.nodeClassification.scheduleClassification,
|
|
2452
|
+
{
|
|
2453
|
+
nodeId,
|
|
2454
|
+
nodeType: "evidence",
|
|
2455
|
+
projectId: scope.projectId,
|
|
2456
|
+
topicId: String(scope.topicId)
|
|
2457
|
+
}
|
|
2458
|
+
);
|
|
2459
|
+
}
|
|
2460
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
2461
|
+
return { nodeId };
|
|
2462
|
+
}
|
|
2463
|
+
});
|
|
2464
|
+
var createAndLink = mutation({
|
|
2465
|
+
args: {
|
|
2466
|
+
...optionalEvidenceScopeArgs,
|
|
2467
|
+
text: v.string(),
|
|
2468
|
+
kind: v.optional(v.string()),
|
|
2469
|
+
tags: v.optional(v.array(v.string())),
|
|
2470
|
+
sourceType: v.optional(v.string()),
|
|
2471
|
+
userId: v.string(),
|
|
2472
|
+
beliefNodeId: v.id("epistemicNodes"),
|
|
2473
|
+
relation: v.union(v.literal("supports"), v.literal("contradicts")),
|
|
2474
|
+
confidence: v.optional(v.number())
|
|
2475
|
+
},
|
|
2476
|
+
returns: permissiveReturn,
|
|
2477
|
+
handler: async (ctx, args) => {
|
|
2478
|
+
const scope = await resolveTopicProjectScope(ctx, {
|
|
2479
|
+
topicId: args.topicId,
|
|
2480
|
+
projectId: args.projectId
|
|
2481
|
+
});
|
|
2482
|
+
await requireProjectAccess(ctx, String(scope.topicId), args.userId);
|
|
2483
|
+
const now = Date.now();
|
|
2484
|
+
const globalId = generateGlobalId();
|
|
2485
|
+
const contentHash = generateContentHash(args.text);
|
|
2486
|
+
const kind = normalizeKind(args.kind);
|
|
2487
|
+
const sourceType3 = normalizeSourceType(args.sourceType);
|
|
2488
|
+
const confidence2 = args.confidence ?? 0.7;
|
|
2489
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
2490
|
+
globalId,
|
|
2491
|
+
topicId: scope.topicId,
|
|
2492
|
+
projectId: scope.projectId,
|
|
2493
|
+
tenantId: scope.tenantId,
|
|
2494
|
+
workspaceId: scope.workspaceId,
|
|
2495
|
+
nodeType: "evidence",
|
|
2496
|
+
canonicalText: args.text,
|
|
2497
|
+
contentHash,
|
|
2498
|
+
status: "active",
|
|
2499
|
+
epistemicLayer: "L2",
|
|
2500
|
+
sourceType: sourceType3,
|
|
2501
|
+
createdAt: now,
|
|
2502
|
+
updatedAt: now,
|
|
2503
|
+
createdBy: args.userId,
|
|
2504
|
+
metadata: {
|
|
2505
|
+
kind,
|
|
2506
|
+
tags: args.tags || [],
|
|
2507
|
+
linkedBeliefNodeId: args.beliefNodeId,
|
|
2508
|
+
evidenceRelation: args.relation,
|
|
2509
|
+
confidence: confidence2
|
|
2510
|
+
}
|
|
2511
|
+
});
|
|
2512
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
2513
|
+
nodeId,
|
|
2514
|
+
operation: "upsert"
|
|
2515
|
+
});
|
|
2516
|
+
const beliefNode = await ctx.db.get(args.beliefNodeId);
|
|
2517
|
+
if (!beliefNode) {
|
|
2518
|
+
throw new Error("Belief node not found for edge creation");
|
|
2519
|
+
}
|
|
2520
|
+
const weight = args.relation === "supports" ? confidence2 : -confidence2;
|
|
2521
|
+
const edgeGlobalId = crypto.randomUUID();
|
|
2522
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
2523
|
+
globalId: edgeGlobalId,
|
|
2524
|
+
fromGlobalId: globalId,
|
|
2525
|
+
toGlobalId: beliefNode.globalId,
|
|
2526
|
+
edgeType: "informs",
|
|
2527
|
+
weight,
|
|
2528
|
+
createdBy: args.userId,
|
|
2529
|
+
topicId: scope.projectId,
|
|
2530
|
+
fromNodeType: "evidence",
|
|
2531
|
+
toNodeType: "belief",
|
|
2532
|
+
fromLayer: "L2",
|
|
2533
|
+
toLayer: "L3",
|
|
2534
|
+
metadata: {
|
|
2535
|
+
relation: args.relation,
|
|
2536
|
+
confidence: confidence2
|
|
2537
|
+
}
|
|
2538
|
+
});
|
|
2539
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
2540
|
+
return { nodeId, edgeGlobalId };
|
|
2541
|
+
}
|
|
2542
|
+
});
|
|
2543
|
+
var updateStatus = mutation({
|
|
2544
|
+
args: {
|
|
2545
|
+
nodeId: v.id("epistemicNodes"),
|
|
2546
|
+
status: v.union(
|
|
2547
|
+
v.literal("active"),
|
|
2548
|
+
v.literal("archived"),
|
|
2549
|
+
v.literal("acted_on")
|
|
2550
|
+
),
|
|
2551
|
+
userId: v.string()
|
|
2552
|
+
},
|
|
2553
|
+
returns: permissiveReturn,
|
|
2554
|
+
handler: async (ctx, args) => {
|
|
2555
|
+
const node = await ctx.db.get(args.nodeId);
|
|
2556
|
+
if (!node || node.nodeType !== "evidence") {
|
|
2557
|
+
throw new Error("Evidence not found");
|
|
2558
|
+
}
|
|
2559
|
+
const now = Date.now();
|
|
2560
|
+
await ctx.db.patch(args.nodeId, {
|
|
2561
|
+
status: args.status,
|
|
2562
|
+
updatedAt: now
|
|
2563
|
+
});
|
|
2564
|
+
await ctx.db.insert("epistemicAudit", {
|
|
2565
|
+
entityType: "evidence",
|
|
2566
|
+
entityId: args.nodeId,
|
|
2567
|
+
changeType: "status_changed",
|
|
2568
|
+
changedAt: now,
|
|
2569
|
+
changedBy: args.userId,
|
|
2570
|
+
isAgent: false,
|
|
2571
|
+
projectId: node.projectId,
|
|
2572
|
+
previousState: { status: node.status },
|
|
2573
|
+
newState: { status: args.status }
|
|
2574
|
+
});
|
|
2575
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
2576
|
+
nodeId: args.nodeId,
|
|
2577
|
+
operation: "upsert"
|
|
2578
|
+
});
|
|
2579
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
2580
|
+
return { nodeId: args.nodeId };
|
|
2581
|
+
}
|
|
2582
|
+
});
|
|
2583
|
+
var getById = query({
|
|
2584
|
+
args: {
|
|
2585
|
+
nodeId: v.optional(v.id("epistemicNodes")),
|
|
2586
|
+
insightId: v.optional(v.string()),
|
|
2587
|
+
evidenceId: v.optional(v.string())
|
|
2588
|
+
},
|
|
2589
|
+
returns: permissiveReturn,
|
|
2590
|
+
handler: async (ctx, args) => {
|
|
2591
|
+
const id = args.nodeId ?? args.insightId ?? args.evidenceId;
|
|
2592
|
+
if (!id) {
|
|
2593
|
+
return null;
|
|
2594
|
+
}
|
|
2595
|
+
const node = await ctx.db.get(
|
|
2596
|
+
id
|
|
2597
|
+
);
|
|
2598
|
+
if (!node || node.nodeType !== "evidence") {
|
|
2599
|
+
return null;
|
|
2600
|
+
}
|
|
2601
|
+
return node;
|
|
2602
|
+
}
|
|
2603
|
+
});
|
|
2604
|
+
var getByProject = query({
|
|
2605
|
+
args: {
|
|
2606
|
+
...optionalEvidenceScopeArgs,
|
|
2607
|
+
status: v.optional(v.string()),
|
|
2608
|
+
userId: v.optional(v.string()),
|
|
2609
|
+
limit: v.optional(v.number())
|
|
2610
|
+
},
|
|
2611
|
+
returns: permissiveReturn,
|
|
2612
|
+
handler: async (ctx, args) => {
|
|
2613
|
+
if (!args.projectId && !args.topicId) {
|
|
2614
|
+
return [];
|
|
2615
|
+
}
|
|
2616
|
+
const pageSize = clampEvidenceLimit(args.limit);
|
|
2617
|
+
const scanLimit = Math.min(pageSize * 3, MAX_EVIDENCE_PAGE_SIZE);
|
|
2618
|
+
let scope;
|
|
2619
|
+
try {
|
|
2620
|
+
scope = await resolveTopicProjectScope(ctx, {
|
|
2621
|
+
projectId: args.projectId,
|
|
2622
|
+
topicId: args.topicId
|
|
2623
|
+
});
|
|
2624
|
+
} catch {
|
|
2625
|
+
return [];
|
|
2626
|
+
}
|
|
2627
|
+
if (args.userId) {
|
|
2628
|
+
const scopeId = scope.topicId ? String(scope.topicId) : scope.projectId;
|
|
2629
|
+
if (!scopeId) {
|
|
2630
|
+
return [];
|
|
2631
|
+
}
|
|
2632
|
+
const hasAccess = await checkScopeAccess(ctx, scopeId, args.userId);
|
|
2633
|
+
if (!hasAccess) {
|
|
2634
|
+
return [];
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
const [topicNodes, projectNodes] = await Promise.all([
|
|
2638
|
+
scope.topicId ? ctx.db.query("epistemicNodes").withIndex(
|
|
2639
|
+
"by_topic_type",
|
|
2640
|
+
(q) => q.eq("topicId", scope.topicId).eq("nodeType", "evidence")
|
|
2641
|
+
).order("desc").take(scanLimit) : Promise.resolve([]),
|
|
2642
|
+
scope.projectId ? ctx.db.query("epistemicNodes").withIndex(
|
|
2643
|
+
"by_project_type",
|
|
2644
|
+
(q) => q.eq("projectId", scope.projectId).eq("nodeType", "evidence")
|
|
2645
|
+
).order("desc").take(scanLimit) : Promise.resolve([])
|
|
2646
|
+
]);
|
|
2647
|
+
const scopedNodes = dedupeEvidenceNodes([
|
|
2648
|
+
...topicNodes,
|
|
2649
|
+
...projectNodes
|
|
2650
|
+
]).filter((node) => evidenceMatchesScope(node, scope));
|
|
2651
|
+
const filteredNodes = args.status ? scopedNodes.filter((node) => node.status === args.status) : scopedNodes;
|
|
2652
|
+
return filteredNodes.map(flattenEvidenceNode).slice(0, pageSize);
|
|
2653
|
+
}
|
|
2654
|
+
});
|
|
2655
|
+
var getByTopic = query({
|
|
2656
|
+
args: {
|
|
2657
|
+
topicId: v.string(),
|
|
2658
|
+
status: v.optional(v.string()),
|
|
2659
|
+
userId: v.optional(v.string()),
|
|
2660
|
+
limit: v.optional(v.number())
|
|
2661
|
+
},
|
|
2662
|
+
returns: permissiveReturn,
|
|
2663
|
+
handler: async (ctx, args) => {
|
|
2664
|
+
const pageSize = clampEvidenceLimit(args.limit);
|
|
2665
|
+
const scanLimit = Math.min(pageSize * 3, MAX_EVIDENCE_PAGE_SIZE);
|
|
2666
|
+
const scope = await resolveTopicProjectScope(ctx, { topicId: args.topicId });
|
|
2667
|
+
const [topicNodes, projectNodes] = await Promise.all([
|
|
2668
|
+
ctx.db.query("epistemicNodes").withIndex(
|
|
2669
|
+
"by_topic_type",
|
|
2670
|
+
(q) => q.eq("topicId", args.topicId).eq("nodeType", "evidence")
|
|
2671
|
+
).order("desc").take(scanLimit),
|
|
2672
|
+
scope.projectId ? ctx.db.query("epistemicNodes").withIndex(
|
|
2673
|
+
"by_project_type",
|
|
2674
|
+
(q) => q.eq("projectId", scope.projectId).eq("nodeType", "evidence")
|
|
2675
|
+
).order("desc").take(scanLimit) : Promise.resolve([])
|
|
2676
|
+
]);
|
|
2677
|
+
const scopedNodes = dedupeEvidenceNodes([
|
|
2678
|
+
...topicNodes,
|
|
2679
|
+
...projectNodes
|
|
2680
|
+
]).filter((node) => evidenceMatchesScope(node, scope));
|
|
2681
|
+
const filteredNodes = args.status ? scopedNodes.filter((node) => node.status === args.status) : scopedNodes;
|
|
2682
|
+
return filteredNodes.map(flattenEvidenceNode).slice(0, pageSize);
|
|
2683
|
+
}
|
|
2684
|
+
});
|
|
2685
|
+
var getForBelief = query({
|
|
2686
|
+
args: {
|
|
2687
|
+
beliefNodeId: v.id("epistemicNodes")
|
|
2688
|
+
},
|
|
2689
|
+
returns: permissiveReturn,
|
|
2690
|
+
handler: async (ctx, args) => {
|
|
2691
|
+
const edges = await ctx.db.query("epistemicEdges").withIndex(
|
|
2692
|
+
"by_to_type",
|
|
2693
|
+
(q) => q.eq("toNodeId", args.beliefNodeId).eq("edgeType", "informs")
|
|
2694
|
+
).collect();
|
|
2695
|
+
const evidenceNodeIds = edges.map((e) => e.fromNodeId);
|
|
2696
|
+
const evidenceNodes = await Promise.all(
|
|
2697
|
+
evidenceNodeIds.map((id) => ctx.db.get(id))
|
|
2698
|
+
);
|
|
2699
|
+
return evidenceNodes.filter((e) => e !== null && e.nodeType === "evidence").map((e, i) => ({
|
|
2700
|
+
...e,
|
|
2701
|
+
relation: (edges[i]?.weight ?? 0) >= 0 ? "supports" : "contradicts",
|
|
2702
|
+
confidence: Math.abs(edges[i]?.weight ?? 0),
|
|
2703
|
+
edgeId: edges[i]?._id
|
|
2704
|
+
}));
|
|
2705
|
+
}
|
|
2706
|
+
});
|
|
2707
|
+
var internalGetByProject = internalQuery({
|
|
2708
|
+
args: {
|
|
2709
|
+
...optionalEvidenceScopeArgs,
|
|
2710
|
+
status: v.optional(v.string()),
|
|
2711
|
+
limit: v.optional(v.number()),
|
|
2712
|
+
audienceMode: v.optional(v.string())
|
|
2713
|
+
},
|
|
2714
|
+
returns: permissiveReturn,
|
|
2715
|
+
handler: async (ctx, args) => {
|
|
2716
|
+
const pageSize = clampEvidenceLimit(args.limit, 500);
|
|
2717
|
+
const scanLimit = Math.min(pageSize * 3, MAX_EVIDENCE_PAGE_SIZE);
|
|
2718
|
+
const scope = await resolveEvidenceScopeOrNull(ctx, args);
|
|
2719
|
+
if (!scope) {
|
|
2720
|
+
return [];
|
|
2721
|
+
}
|
|
2722
|
+
const audienceMode = args.audienceMode ?? "internal";
|
|
2723
|
+
const project = await resolveGraphPrimitivesAppResolvers().getProject(
|
|
2724
|
+
ctx,
|
|
2725
|
+
scope.topicId ? String(scope.topicId) : scope.projectId
|
|
2726
|
+
);
|
|
2727
|
+
const registryRows = await listAudienceRegistryRows(ctx, {
|
|
2728
|
+
tenantId: project?.tenantId,
|
|
2729
|
+
workspaceId: project?.workspaceId
|
|
2730
|
+
});
|
|
2731
|
+
const resolveAudienceClass = createEvidenceAudienceResolver(registryRows);
|
|
2732
|
+
const viewerClass = resolveAudienceClass(audienceMode, "public");
|
|
2733
|
+
const nodes = await getEvidenceNodesForScope(ctx, scope, { scanLimit });
|
|
2734
|
+
const workspaceScopedNodes = nodes.filter(
|
|
2735
|
+
(node) => nodeMatchesWorkspaceReasoningScope(node, {
|
|
2736
|
+
tenantId: project?.tenantId,
|
|
2737
|
+
workspaceId: project?.workspaceId
|
|
2738
|
+
})
|
|
2739
|
+
);
|
|
2740
|
+
return workspaceScopedNodes.filter(
|
|
2741
|
+
(n) => canAudienceClassAccess(
|
|
2742
|
+
viewerClass,
|
|
2743
|
+
resolveAudienceClass(n.audienceLabel, "internal")
|
|
2744
|
+
) && (!args.status || n.status === args.status)
|
|
2745
|
+
).slice(0, pageSize).map((n) => {
|
|
2746
|
+
const metadata = n.metadata || {};
|
|
2747
|
+
return {
|
|
2748
|
+
_id: n._id,
|
|
2749
|
+
_creationTime: n.createdAt,
|
|
2750
|
+
projectId: n.projectId,
|
|
2751
|
+
topicId: n.topicId,
|
|
2752
|
+
text: n.canonicalText,
|
|
2753
|
+
kind: metadata.kind || "observation",
|
|
2754
|
+
tags: metadata.tags || [],
|
|
2755
|
+
sourceType: n.sourceType,
|
|
2756
|
+
externalSourceType: metadata.externalSourceType,
|
|
2757
|
+
sourceUrl: metadata.sourceUrl,
|
|
2758
|
+
status: n.status,
|
|
2759
|
+
createdBy: n.createdBy,
|
|
2760
|
+
createdAt: n.createdAt,
|
|
2761
|
+
updatedAt: n.updatedAt,
|
|
2762
|
+
audienceLabel: n.audienceLabel,
|
|
2763
|
+
policyTags: n.policyTags,
|
|
2764
|
+
sensitivityTier: n.sensitivityTier,
|
|
2765
|
+
exportClass: n.exportClass,
|
|
2766
|
+
anonymizationClass: n.anonymizationClass
|
|
2767
|
+
};
|
|
2768
|
+
});
|
|
2769
|
+
}
|
|
2770
|
+
});
|
|
2771
|
+
var internalGetByTopic = internalQuery({
|
|
2772
|
+
args: {
|
|
2773
|
+
topicId: v.string(),
|
|
2774
|
+
status: v.optional(v.string()),
|
|
2775
|
+
limit: v.optional(v.number()),
|
|
2776
|
+
audienceMode: v.optional(v.string())
|
|
2777
|
+
},
|
|
2778
|
+
returns: permissiveReturn,
|
|
2779
|
+
handler: async (ctx, args) => {
|
|
2780
|
+
const pageSize = clampEvidenceLimit(args.limit, 500);
|
|
2781
|
+
const scanLimit = Math.min(pageSize * 3, MAX_EVIDENCE_PAGE_SIZE);
|
|
2782
|
+
const audienceMode = args.audienceMode ?? "internal";
|
|
2783
|
+
const scope = await resolveTopicProjectScope(ctx, { topicId: args.topicId });
|
|
2784
|
+
const registryRows = await listAudienceRegistryRows(ctx, {
|
|
2785
|
+
tenantId: scope.tenantId,
|
|
2786
|
+
workspaceId: scope.workspaceId
|
|
2787
|
+
});
|
|
2788
|
+
const resolveAudienceClass = createEvidenceAudienceResolver(registryRows);
|
|
2789
|
+
const viewerClass = resolveAudienceClass(audienceMode, "public");
|
|
2790
|
+
const nodes = await ctx.db.query("epistemicNodes").withIndex(
|
|
2791
|
+
"by_topic_type",
|
|
2792
|
+
(q) => q.eq("topicId", args.topicId).eq("nodeType", "evidence")
|
|
2793
|
+
).order("desc").take(scanLimit);
|
|
2794
|
+
const workspaceScopedNodes = nodes.filter(
|
|
2795
|
+
(node) => nodeMatchesWorkspaceReasoningScope(node, {
|
|
2796
|
+
tenantId: scope.tenantId,
|
|
2797
|
+
workspaceId: scope.workspaceId
|
|
2798
|
+
})
|
|
2799
|
+
);
|
|
2800
|
+
return workspaceScopedNodes.filter(
|
|
2801
|
+
(n) => canAudienceClassAccess(
|
|
2802
|
+
viewerClass,
|
|
2803
|
+
resolveAudienceClass(n.audienceLabel, "internal")
|
|
2804
|
+
) && (!args.status || n.status === args.status)
|
|
2805
|
+
).slice(0, pageSize).map((n) => {
|
|
2806
|
+
const metadata = n.metadata || {};
|
|
2807
|
+
return {
|
|
2808
|
+
_id: n._id,
|
|
2809
|
+
_creationTime: n.createdAt,
|
|
2810
|
+
projectId: n.projectId,
|
|
2811
|
+
topicId: n.topicId,
|
|
2812
|
+
text: n.canonicalText,
|
|
2813
|
+
kind: metadata.kind || "observation",
|
|
2814
|
+
tags: metadata.tags || [],
|
|
2815
|
+
sourceType: n.sourceType,
|
|
2816
|
+
externalSourceType: metadata.externalSourceType,
|
|
2817
|
+
sourceUrl: metadata.sourceUrl,
|
|
2818
|
+
status: n.status,
|
|
2819
|
+
createdBy: n.createdBy,
|
|
2820
|
+
createdAt: n.createdAt,
|
|
2821
|
+
updatedAt: n.updatedAt,
|
|
2822
|
+
audienceLabel: n.audienceLabel,
|
|
2823
|
+
policyTags: n.policyTags,
|
|
2824
|
+
sensitivityTier: n.sensitivityTier,
|
|
2825
|
+
exportClass: n.exportClass,
|
|
2826
|
+
anonymizationClass: n.anonymizationClass
|
|
2827
|
+
};
|
|
2828
|
+
});
|
|
2829
|
+
}
|
|
2830
|
+
});
|
|
2831
|
+
var internalCreate = internalMutation({
|
|
2832
|
+
args: {
|
|
2833
|
+
...optionalEvidenceScopeArgs,
|
|
2834
|
+
text: v.string(),
|
|
2835
|
+
title: v.optional(v.string()),
|
|
2836
|
+
content: v.optional(v.string()),
|
|
2837
|
+
contentType: v.optional(v.string()),
|
|
2838
|
+
kind: v.optional(v.string()),
|
|
2839
|
+
tags: v.optional(v.array(v.string())),
|
|
2840
|
+
sourceType: v.optional(v.string()),
|
|
2841
|
+
externalSourceType: v.optional(v.string()),
|
|
2842
|
+
sourceUrl: v.optional(v.string()),
|
|
2843
|
+
sourceQuestionId: v.optional(v.string()),
|
|
2844
|
+
userId: v.string(),
|
|
2845
|
+
linkedBeliefNodeId: v.optional(v.id("epistemicNodes")),
|
|
2846
|
+
evidenceRelation: v.optional(v.string()),
|
|
2847
|
+
confidence: v.optional(v.number()),
|
|
2848
|
+
/** Optional extra metadata fields merged into the node's metadata object.
|
|
2849
|
+
* Use for domain overlays like coding intelligence (codeAnchors, failedApproach, etc.) */
|
|
2850
|
+
metadata: v.optional(v.any()),
|
|
2851
|
+
runtimeToolName: v.optional(v.string()),
|
|
2852
|
+
runtimePackKey: v.optional(v.string()),
|
|
2853
|
+
runtimePackInstallScope: v.optional(
|
|
2854
|
+
v.union(v.literal("tenant"), v.literal("workspace"))
|
|
2855
|
+
)
|
|
2856
|
+
},
|
|
2857
|
+
returns: permissiveReturn,
|
|
2858
|
+
handler: async (ctx, args) => {
|
|
2859
|
+
const now = Date.now();
|
|
2860
|
+
const scope = await resolveTopicProjectScope(ctx, {
|
|
2861
|
+
topicId: args.topicId,
|
|
2862
|
+
projectId: args.projectId
|
|
2863
|
+
});
|
|
2864
|
+
assertWorkspaceScopedEpistemicNodeScope({
|
|
2865
|
+
scope,
|
|
2866
|
+
nodeType: "evidence",
|
|
2867
|
+
mutationName: "epistemicEvidence.internalCreate"
|
|
2868
|
+
});
|
|
2869
|
+
assertTenantPackWorkspaceMutationAllowed({
|
|
2870
|
+
runtime: resolveRuntimePackMutationContext(args),
|
|
2871
|
+
target: {
|
|
2872
|
+
tenantId: scope.tenantId,
|
|
2873
|
+
workspaceId: scope.workspaceId,
|
|
2874
|
+
nodeType: "evidence",
|
|
2875
|
+
epistemicLayer: "L2"
|
|
2876
|
+
},
|
|
2877
|
+
mutationName: "epistemicEvidence.internalCreate"
|
|
2878
|
+
});
|
|
2879
|
+
const globalId = generateGlobalId();
|
|
2880
|
+
const contentHash = generateContentHash(args.text);
|
|
2881
|
+
const kind = normalizeKind(args.kind);
|
|
2882
|
+
const sourceType3 = normalizeSourceType(args.sourceType);
|
|
2883
|
+
const additionalMetadata = args.metadata && typeof args.metadata === "object" ? args.metadata : {};
|
|
2884
|
+
const nodeId = await ctx.db.insert("epistemicNodes", {
|
|
2885
|
+
globalId,
|
|
2886
|
+
topicId: scope.topicId,
|
|
2887
|
+
projectId: scope.projectId,
|
|
2888
|
+
tenantId: scope.tenantId,
|
|
2889
|
+
workspaceId: scope.workspaceId,
|
|
2890
|
+
nodeType: "evidence",
|
|
2891
|
+
canonicalText: args.text,
|
|
2892
|
+
contentHash,
|
|
2893
|
+
...typeof args.title === "string" && args.title.trim().length > 0 ? { title: args.title.trim() } : {},
|
|
2894
|
+
...typeof args.content === "string" && args.content.length > 0 ? { content: args.content } : {},
|
|
2895
|
+
...typeof args.contentType === "string" && args.contentType.trim().length > 0 ? { contentType: args.contentType.trim() } : {},
|
|
2896
|
+
status: "active",
|
|
2897
|
+
epistemicLayer: "L2",
|
|
2898
|
+
sourceType: sourceType3,
|
|
2899
|
+
createdAt: now,
|
|
2900
|
+
updatedAt: now,
|
|
2901
|
+
createdBy: args.userId,
|
|
2902
|
+
metadata: {
|
|
2903
|
+
kind,
|
|
2904
|
+
tags: args.tags || [],
|
|
2905
|
+
externalSourceType: args.externalSourceType,
|
|
2906
|
+
sourceUrl: args.sourceUrl,
|
|
2907
|
+
sourceQuestionId: args.sourceQuestionId,
|
|
2908
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
2909
|
+
evidenceRelation: args.evidenceRelation,
|
|
2910
|
+
confidence: args.confidence,
|
|
2911
|
+
...additionalMetadata
|
|
2912
|
+
}
|
|
2913
|
+
});
|
|
2914
|
+
await ctx.db.insert("epistemicAudit", {
|
|
2915
|
+
entityType: "evidence",
|
|
2916
|
+
entityId: String(nodeId),
|
|
2917
|
+
changeType: "created",
|
|
2918
|
+
changedAt: now,
|
|
2919
|
+
changedBy: args.userId,
|
|
2920
|
+
isAgent: false,
|
|
2921
|
+
projectId: scope.projectId,
|
|
2922
|
+
newState: {
|
|
2923
|
+
text: args.text.slice(0, 200),
|
|
2924
|
+
kind,
|
|
2925
|
+
sourceType: sourceType3,
|
|
2926
|
+
externalSourceType: args.externalSourceType,
|
|
2927
|
+
sourceUrl: args.sourceUrl,
|
|
2928
|
+
linkedBeliefNodeId: args.linkedBeliefNodeId,
|
|
2929
|
+
evidenceRelation: args.evidenceRelation,
|
|
2930
|
+
confidence: args.confidence
|
|
2931
|
+
},
|
|
2932
|
+
triggeringAction: "epistemicEvidence.internalCreate"
|
|
2933
|
+
});
|
|
2934
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
2935
|
+
nodeId,
|
|
2936
|
+
operation: "upsert"
|
|
2937
|
+
});
|
|
2938
|
+
if (args.linkedBeliefNodeId && args.evidenceRelation) {
|
|
2939
|
+
const beliefNode = await ctx.db.get(args.linkedBeliefNodeId);
|
|
2940
|
+
if (beliefNode) {
|
|
2941
|
+
const confidence2 = args.confidence ?? 0.7;
|
|
2942
|
+
const weight = args.evidenceRelation === "supports" ? confidence2 : -confidence2;
|
|
2943
|
+
await ctx.scheduler.runAfter(5e3, internal.neo4jEdgeAPI.createEdge, {
|
|
2944
|
+
globalId: crypto.randomUUID(),
|
|
2945
|
+
fromGlobalId: globalId,
|
|
2946
|
+
toGlobalId: beliefNode.globalId,
|
|
2947
|
+
edgeType: "informs",
|
|
2948
|
+
weight,
|
|
2949
|
+
createdBy: args.userId,
|
|
2950
|
+
topicId: scope.projectId ? String(scope.projectId) : void 0,
|
|
2951
|
+
fromNodeType: "evidence",
|
|
2952
|
+
toNodeType: "belief",
|
|
2953
|
+
fromLayer: "L2",
|
|
2954
|
+
toLayer: "L3",
|
|
2955
|
+
metadata: {
|
|
2956
|
+
relation: args.evidenceRelation,
|
|
2957
|
+
confidence: confidence2
|
|
2958
|
+
}
|
|
2959
|
+
});
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
if (scope.projectId || scope.topicId) {
|
|
2963
|
+
await ctx.scheduler.runAfter(
|
|
2964
|
+
0,
|
|
2965
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
2966
|
+
{
|
|
2967
|
+
nodeId,
|
|
2968
|
+
projectId: scope.projectId,
|
|
2969
|
+
topicId: scope.topicId ? String(scope.topicId) : void 0,
|
|
2970
|
+
createdBy: args.userId,
|
|
2971
|
+
nodeType: "evidence",
|
|
2972
|
+
text: args.text
|
|
2973
|
+
}
|
|
2974
|
+
);
|
|
2975
|
+
}
|
|
2976
|
+
await markProjectGraphDirty(ctx, scope.projectId, String(scope.topicId));
|
|
2977
|
+
return { nodeId };
|
|
2978
|
+
}
|
|
2979
|
+
});
|
|
2980
|
+
var updateVerificationStatus = mutation({
|
|
2981
|
+
args: {
|
|
2982
|
+
nodeId: v.id("epistemicNodes"),
|
|
2983
|
+
verificationHash: v.string(),
|
|
2984
|
+
verificationStatus: v.string(),
|
|
2985
|
+
lastVerificationId: v.optional(v.id("verificationResults"))
|
|
2986
|
+
},
|
|
2987
|
+
returns: permissiveReturn,
|
|
2988
|
+
handler: async (ctx, args) => {
|
|
2989
|
+
const node = await ctx.db.get(args.nodeId);
|
|
2990
|
+
if (!node || node.nodeType !== "evidence") {
|
|
2991
|
+
throw new Error("Evidence node not found");
|
|
2992
|
+
}
|
|
2993
|
+
const metadata = node.metadata || {};
|
|
2994
|
+
await ctx.db.patch(args.nodeId, {
|
|
2995
|
+
metadata: {
|
|
2996
|
+
...metadata,
|
|
2997
|
+
verificationHash: args.verificationHash,
|
|
2998
|
+
verificationStatus: args.verificationStatus,
|
|
2999
|
+
lastVerificationId: args.lastVerificationId
|
|
3000
|
+
}
|
|
3001
|
+
});
|
|
3002
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
3003
|
+
return { success: true };
|
|
3004
|
+
}
|
|
3005
|
+
});
|
|
3006
|
+
function formatEvidenceNode(n) {
|
|
3007
|
+
const metadata = n.metadata || {};
|
|
3008
|
+
const linkedWorktreeId = resolveEvidenceLinkedWorktreeId(metadata);
|
|
3009
|
+
return {
|
|
3010
|
+
_id: n._id,
|
|
3011
|
+
_epistemicNodeId: n._id,
|
|
3012
|
+
_creationTime: n.createdAt,
|
|
3013
|
+
projectId: n.projectId,
|
|
3014
|
+
topicId: n.topicId,
|
|
3015
|
+
text: n.canonicalText,
|
|
3016
|
+
kind: metadata.kind || "observation",
|
|
3017
|
+
tags: metadata.tags || [],
|
|
3018
|
+
sourceType: n.sourceType,
|
|
3019
|
+
externalSourceType: metadata.externalSourceType,
|
|
3020
|
+
externalSourceUrl: metadata.sourceUrl,
|
|
3021
|
+
sourceArtifactId: metadata.sourceArtifactId,
|
|
3022
|
+
sourceQuestionId: metadata.sourceQuestionId,
|
|
3023
|
+
linkedWorktreeId,
|
|
3024
|
+
[LEGACY_SPRINT_LINK_KEY]: metadata[LEGACY_SPRINT_LINK_KEY] || void 0,
|
|
3025
|
+
sourceAnchor: metadata.sourceAnchor,
|
|
3026
|
+
aiProvider: metadata.aiProvider,
|
|
3027
|
+
verificationStatus: metadata.verificationStatus,
|
|
3028
|
+
status: n.status,
|
|
3029
|
+
createdBy: n.createdBy,
|
|
3030
|
+
createdAt: n.createdAt,
|
|
3031
|
+
updatedAt: n.updatedAt
|
|
3032
|
+
};
|
|
3033
|
+
}
|
|
3034
|
+
var getByProjectSystem = query({
|
|
3035
|
+
args: {
|
|
3036
|
+
...optionalEvidenceScopeArgs,
|
|
3037
|
+
kind: v.optional(v.string()),
|
|
3038
|
+
limit: v.optional(v.number())
|
|
3039
|
+
},
|
|
3040
|
+
returns: permissiveReturn,
|
|
3041
|
+
handler: async (ctx, args) => {
|
|
3042
|
+
const pageSize = clampEvidenceLimit(args.limit, 500);
|
|
3043
|
+
const scanLimit = Math.min(pageSize * 3, MAX_EVIDENCE_PAGE_SIZE);
|
|
3044
|
+
const scope = await resolveEvidenceScopeOrNull(ctx, args);
|
|
3045
|
+
if (!scope) {
|
|
3046
|
+
return [];
|
|
3047
|
+
}
|
|
3048
|
+
const nodes = await getEvidenceNodesForScope(ctx, scope, { scanLimit });
|
|
3049
|
+
const filtered = args.kind ? nodes.filter((n) => {
|
|
3050
|
+
const meta = n.metadata || {};
|
|
3051
|
+
return meta.kind === args.kind;
|
|
3052
|
+
}) : nodes;
|
|
3053
|
+
filtered.sort((a, b) => b.createdAt - a.createdAt);
|
|
3054
|
+
return filtered.slice(0, pageSize).map((n) => formatEvidenceNode(n));
|
|
3055
|
+
}
|
|
3056
|
+
});
|
|
3057
|
+
var getEvidenceBalance = query({
|
|
3058
|
+
args: {
|
|
3059
|
+
...optionalEvidenceScopeArgs,
|
|
3060
|
+
userId: v.string()
|
|
3061
|
+
},
|
|
3062
|
+
returns: permissiveReturn,
|
|
3063
|
+
handler: async (ctx, args) => {
|
|
3064
|
+
const scope = await resolveTopicProjectScope(ctx, {
|
|
3065
|
+
topicId: args.topicId,
|
|
3066
|
+
projectId: args.projectId
|
|
3067
|
+
});
|
|
3068
|
+
const hasAccess = await checkScopeAccess(
|
|
3069
|
+
ctx,
|
|
3070
|
+
scope.topicId ? String(scope.topicId) : scope.projectId,
|
|
3071
|
+
args.userId
|
|
3072
|
+
);
|
|
3073
|
+
if (!hasAccess) {
|
|
3074
|
+
return { supporting: 0, challenging: 0, total: 0 };
|
|
3075
|
+
}
|
|
3076
|
+
const resolvedTopicId = scope.topicId || scope.projectId;
|
|
3077
|
+
const edges = resolvedTopicId ? await ctx.db.query("epistemicEdges").withIndex("by_topic", (q) => q.eq("topicId", resolvedTopicId)).collect() : [];
|
|
3078
|
+
const evidenceEdges = edges.filter(
|
|
3079
|
+
(e) => e.edgeType === "informs" && e.fromLayer === "L2" && e.toLayer === "L3"
|
|
3080
|
+
);
|
|
3081
|
+
let supporting = 0;
|
|
3082
|
+
let challenging = 0;
|
|
3083
|
+
for (const edge of evidenceEdges) {
|
|
3084
|
+
const weight = edge.weight ?? 0;
|
|
3085
|
+
if (weight > 0) {
|
|
3086
|
+
supporting++;
|
|
3087
|
+
} else if (weight < 0) {
|
|
3088
|
+
challenging++;
|
|
3089
|
+
}
|
|
3090
|
+
}
|
|
3091
|
+
return { supporting, challenging, total: evidenceEdges.length };
|
|
3092
|
+
}
|
|
3093
|
+
});
|
|
3094
|
+
var update = mutation({
|
|
3095
|
+
args: {
|
|
3096
|
+
// Accept both native and legacy-shaped IDs
|
|
3097
|
+
nodeId: v.optional(v.id("epistemicNodes")),
|
|
3098
|
+
insightId: v.optional(v.string()),
|
|
3099
|
+
text: v.optional(v.string()),
|
|
3100
|
+
kind: v.optional(v.string()),
|
|
3101
|
+
tags: v.optional(v.array(v.string())),
|
|
3102
|
+
userId: v.string(),
|
|
3103
|
+
externalSourceUrl: v.optional(v.string()),
|
|
3104
|
+
verificationStatus: v.optional(v.string())
|
|
3105
|
+
},
|
|
3106
|
+
returns: permissiveReturn,
|
|
3107
|
+
handler: async (ctx, args) => {
|
|
3108
|
+
const resolvedId = args.nodeId ?? args.insightId;
|
|
3109
|
+
if (!resolvedId) {
|
|
3110
|
+
throw new Error("Either nodeId or insightId is required");
|
|
3111
|
+
}
|
|
3112
|
+
const node = await ctx.db.get(resolvedId);
|
|
3113
|
+
if (!node || node.nodeType !== "evidence") {
|
|
3114
|
+
throw new Error("Evidence node not found");
|
|
3115
|
+
}
|
|
3116
|
+
if (!node.projectId) {
|
|
3117
|
+
throw new Error("Evidence has no project scope");
|
|
3118
|
+
}
|
|
3119
|
+
await checkProjectAccess(ctx, node.projectId, args.userId);
|
|
3120
|
+
const now = Date.now();
|
|
3121
|
+
const existingMeta = node.metadata || {};
|
|
3122
|
+
const metaUpdates = { ...existingMeta };
|
|
3123
|
+
if (args.kind !== void 0) {
|
|
3124
|
+
metaUpdates.kind = args.kind;
|
|
3125
|
+
}
|
|
3126
|
+
if (args.tags !== void 0) {
|
|
3127
|
+
metaUpdates.tags = args.tags;
|
|
3128
|
+
}
|
|
3129
|
+
if (args.externalSourceUrl !== void 0) {
|
|
3130
|
+
metaUpdates.externalSourceUrl = args.externalSourceUrl;
|
|
3131
|
+
}
|
|
3132
|
+
await ctx.db.patch(resolvedId, {
|
|
3133
|
+
canonicalText: args.text ?? node.canonicalText,
|
|
3134
|
+
metadata: metaUpdates,
|
|
3135
|
+
updatedAt: now
|
|
3136
|
+
});
|
|
3137
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
3138
|
+
nodeId: resolvedId,
|
|
3139
|
+
operation: "upsert"
|
|
3140
|
+
});
|
|
3141
|
+
await ctx.db.insert("epistemicAudit", {
|
|
3142
|
+
entityType: "evidence",
|
|
3143
|
+
entityId: resolvedId,
|
|
3144
|
+
changeType: "updated",
|
|
3145
|
+
changedAt: now,
|
|
3146
|
+
changedBy: args.userId,
|
|
3147
|
+
isAgent: false,
|
|
3148
|
+
projectId: node.projectId,
|
|
3149
|
+
previousState: { text: node.canonicalText?.slice(0, 200) },
|
|
3150
|
+
newState: { text: (args.text ?? node.canonicalText)?.slice(0, 200) }
|
|
3151
|
+
});
|
|
3152
|
+
if (args.text !== void 0) {
|
|
3153
|
+
await ctx.scheduler.runAfter(
|
|
3154
|
+
0,
|
|
3155
|
+
"embeddingActions:generateEpistemicNodeEmbedding",
|
|
3156
|
+
{
|
|
3157
|
+
nodeId: resolvedId,
|
|
3158
|
+
topicId: node.projectId,
|
|
3159
|
+
createdBy: node.createdBy,
|
|
3160
|
+
nodeType: "evidence",
|
|
3161
|
+
text: args.text
|
|
3162
|
+
}
|
|
3163
|
+
);
|
|
3164
|
+
}
|
|
3165
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
3166
|
+
return { nodeId: resolvedId };
|
|
3167
|
+
}
|
|
3168
|
+
});
|
|
3169
|
+
var flagAsIncorrect = mutation({
|
|
3170
|
+
args: {
|
|
3171
|
+
insightId: v.id("epistemicNodes"),
|
|
3172
|
+
reason: v.string(),
|
|
3173
|
+
suggestedCorrection: v.optional(v.string()),
|
|
3174
|
+
userId: v.string()
|
|
3175
|
+
},
|
|
3176
|
+
returns: permissiveReturn,
|
|
3177
|
+
handler: async (ctx, args) => {
|
|
3178
|
+
const now = Date.now();
|
|
3179
|
+
const node = await ctx.db.get(args.insightId);
|
|
3180
|
+
if (!node || node.nodeType !== "evidence") {
|
|
3181
|
+
throw new Error("Evidence not found in epistemic spine");
|
|
3182
|
+
}
|
|
3183
|
+
if (!node.projectId) {
|
|
3184
|
+
throw new Error("Evidence has no project scope");
|
|
3185
|
+
}
|
|
3186
|
+
await checkProjectAccess(ctx, node.projectId, args.userId);
|
|
3187
|
+
const existingMeta = node.metadata || {};
|
|
3188
|
+
await ctx.db.patch(node._id, {
|
|
3189
|
+
verificationStatus: "contradicted",
|
|
3190
|
+
metadata: {
|
|
3191
|
+
...existingMeta,
|
|
3192
|
+
contradictionReason: args.reason,
|
|
3193
|
+
suggestedCorrection: args.suggestedCorrection,
|
|
3194
|
+
contradictedBy: args.userId,
|
|
3195
|
+
contradictedAt: now
|
|
3196
|
+
},
|
|
3197
|
+
updatedAt: now
|
|
3198
|
+
});
|
|
3199
|
+
await ctx.db.insert("verificationResults", {
|
|
3200
|
+
targetType: "insight",
|
|
3201
|
+
targetId: node._id,
|
|
3202
|
+
claimText: node.canonicalText || "Evidence text not available",
|
|
3203
|
+
verdict: "contradicted",
|
|
3204
|
+
confidence: 1,
|
|
3205
|
+
sources: [],
|
|
3206
|
+
reasoning: args.reason,
|
|
3207
|
+
suggestedRevision: args.suggestedCorrection,
|
|
3208
|
+
caveats: [],
|
|
3209
|
+
mode: "deep_verify",
|
|
3210
|
+
verifiedAt: now,
|
|
3211
|
+
durationMs: 0,
|
|
3212
|
+
projectId: node.projectId,
|
|
3213
|
+
userId: args.userId
|
|
3214
|
+
});
|
|
3215
|
+
await ctx.db.insert("epistemicAudit", {
|
|
3216
|
+
entityType: "evidence",
|
|
3217
|
+
entityId: node._id,
|
|
3218
|
+
changeType: "updated",
|
|
3219
|
+
changedAt: now,
|
|
3220
|
+
changedBy: args.userId,
|
|
3221
|
+
isAgent: false,
|
|
3222
|
+
projectId: node.projectId,
|
|
3223
|
+
newState: {
|
|
3224
|
+
reason: args.reason,
|
|
3225
|
+
suggestedCorrection: args.suggestedCorrection
|
|
3226
|
+
}
|
|
3227
|
+
});
|
|
3228
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
3229
|
+
console.log(
|
|
3230
|
+
`[EpistemicEvidence] Evidence flagged as incorrect by ${args.userId}: "${args.insightId.slice(0, 20)}..."`
|
|
3231
|
+
);
|
|
3232
|
+
return { success: true };
|
|
3233
|
+
}
|
|
3234
|
+
});
|
|
3235
|
+
var remove = mutation({
|
|
3236
|
+
args: {
|
|
3237
|
+
// Accept both native and legacy-shaped IDs
|
|
3238
|
+
nodeId: v.optional(v.id("epistemicNodes")),
|
|
3239
|
+
insightId: v.optional(v.string()),
|
|
3240
|
+
userId: v.string()
|
|
3241
|
+
},
|
|
3242
|
+
returns: permissiveReturn,
|
|
3243
|
+
handler: async (ctx, args) => {
|
|
3244
|
+
const resolvedId = args.nodeId ?? args.insightId;
|
|
3245
|
+
if (!resolvedId) {
|
|
3246
|
+
throw new Error("Either nodeId or insightId is required");
|
|
3247
|
+
}
|
|
3248
|
+
const node = await ctx.db.get(resolvedId);
|
|
3249
|
+
if (!node || node.nodeType !== "evidence") {
|
|
3250
|
+
throw new Error("Evidence node not found");
|
|
3251
|
+
}
|
|
3252
|
+
if (node.createdBy !== args.userId) {
|
|
3253
|
+
throw new Error("Only the creator can archive this evidence");
|
|
3254
|
+
}
|
|
3255
|
+
const now = Date.now();
|
|
3256
|
+
await ctx.db.patch(resolvedId, {
|
|
3257
|
+
status: "archived",
|
|
3258
|
+
updatedAt: now
|
|
3259
|
+
});
|
|
3260
|
+
await ctx.scheduler.runAfter(0, internal.neo4jSync.syncNodeToNeo4j, {
|
|
3261
|
+
nodeId: resolvedId,
|
|
3262
|
+
operation: "upsert"
|
|
3263
|
+
});
|
|
3264
|
+
await ctx.db.insert("epistemicAudit", {
|
|
3265
|
+
entityType: "evidence",
|
|
3266
|
+
entityId: resolvedId,
|
|
3267
|
+
changeType: "archived",
|
|
3268
|
+
changedAt: now,
|
|
3269
|
+
changedBy: args.userId,
|
|
3270
|
+
isAgent: false,
|
|
3271
|
+
projectId: node.projectId,
|
|
3272
|
+
previousState: { status: node.status },
|
|
3273
|
+
newState: { status: "archived" }
|
|
3274
|
+
});
|
|
3275
|
+
await markProjectGraphDirty(ctx, node.projectId, node.topicId);
|
|
3276
|
+
return { nodeId: resolvedId };
|
|
3277
|
+
}
|
|
3278
|
+
});
|
|
3279
|
+
|
|
3280
|
+
export { create, createAndLink, flagAsIncorrect, flattenEvidenceNode, getById, getByProject, getByProjectSystem, getByTopic, getEvidenceBalance, getForBelief, internalCreate, internalGetByProject, internalGetByTopic, remove, resolveEvidenceLinkedWorktreeId, update, updateStatus, updateVerificationStatus };
|
|
3281
|
+
//# sourceMappingURL=epistemicEvidence.js.map
|
|
3282
|
+
//# sourceMappingURL=epistemicEvidence.js.map
|