@lucern/graph-primitives 0.1.0-alpha.4 → 0.3.0-alpha.0

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.
Files changed (38) hide show
  1. package/dist/beliefDecay.js +199 -18
  2. package/dist/beliefDecay.js.map +1 -1
  3. package/dist/beliefEvidenceLinks.js.map +1 -1
  4. package/dist/confidencePropagationDispatch.js.map +1 -1
  5. package/dist/contradictions.js.map +1 -1
  6. package/dist/entityBridge.js.map +1 -1
  7. package/dist/entityLifecycle.js.map +1 -1
  8. package/dist/epistemicAnswers.js +21 -36
  9. package/dist/epistemicAnswers.js.map +1 -1
  10. package/dist/epistemicBeliefs.js +92 -651
  11. package/dist/epistemicBeliefs.js.map +1 -1
  12. package/dist/epistemicContracts.js +65 -624
  13. package/dist/epistemicContracts.js.map +1 -1
  14. package/dist/epistemicEdges.js.map +1 -1
  15. package/dist/epistemicEvidence.js +71 -630
  16. package/dist/epistemicEvidence.js.map +1 -1
  17. package/dist/epistemicHelpers.js +3 -2
  18. package/dist/epistemicHelpers.js.map +1 -1
  19. package/dist/epistemicLinking.js.map +1 -1
  20. package/dist/epistemicNodes.js +49 -585
  21. package/dist/epistemicNodes.js.map +1 -1
  22. package/dist/epistemicQuestions.js +46 -590
  23. package/dist/epistemicQuestions.js.map +1 -1
  24. package/dist/epistemicSources.js +29 -565
  25. package/dist/epistemicSources.js.map +1 -1
  26. package/dist/evaluators/index.js +65 -624
  27. package/dist/evaluators/index.js.map +1 -1
  28. package/dist/index.js +304 -905
  29. package/dist/index.js.map +1 -1
  30. package/dist/ontologyApproval.js.map +1 -1
  31. package/dist/ontologyDefinitions.js.map +1 -1
  32. package/dist/ontologyRegistry.js.map +1 -1
  33. package/dist/projectionReconciliation.js.map +1 -1
  34. package/dist/questionEvidenceLinks.js +188 -2
  35. package/dist/questionEvidenceLinks.js.map +1 -1
  36. package/dist/workspaceIsolation.js +30 -581
  37. package/dist/workspaceIsolation.js.map +1 -1
  38. package/package.json +5 -6
@@ -1112,6 +1112,188 @@ function resolveGraphPrimitivesAppResolvers(_ctx) {
1112
1112
  ...resolverOverrides2
1113
1113
  };
1114
1114
  }
1115
+ var LEGACY_SCOPE_FIELD3 = "graphScopeProjectId";
1116
+ function asMappedProjectId(topic) {
1117
+ if (!topic) {
1118
+ return;
1119
+ }
1120
+ const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD3]);
1121
+ if (directLegacyProjectId) {
1122
+ return directLegacyProjectId;
1123
+ }
1124
+ const metadata = topic.metadata || {};
1125
+ const candidate = metadata[LEGACY_SCOPE_FIELD3] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
1126
+ return candidate ? candidate : void 0;
1127
+ }
1128
+ function normalizeScopeValue(value) {
1129
+ if (typeof value !== "string") {
1130
+ return;
1131
+ }
1132
+ const normalized = value.trim();
1133
+ return normalized.length > 0 ? normalized : void 0;
1134
+ }
1135
+ function pickPrimaryTopic(candidates) {
1136
+ return [...candidates].sort((a, b) => {
1137
+ const depthA = a.depth ?? 9999;
1138
+ const depthB = b.depth ?? 9999;
1139
+ if (depthA !== depthB) {
1140
+ return depthA - depthB;
1141
+ }
1142
+ const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
1143
+ const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
1144
+ if (createdA !== createdB) {
1145
+ return createdA - createdB;
1146
+ }
1147
+ return String(a.name || "").localeCompare(String(b.name || ""));
1148
+ })[0];
1149
+ }
1150
+ async function findTopicsByScopeAlias(ctx, scopeId) {
1151
+ try {
1152
+ return await ctx.db.query("topics").withIndex(
1153
+ "by_graph_scope_project",
1154
+ (q) => q.eq(LEGACY_SCOPE_FIELD3, scopeId)
1155
+ ).collect();
1156
+ } catch {
1157
+ const topics = await ctx.db.query("topics").collect();
1158
+ return topics.filter((topic) => {
1159
+ const normalizedGlobalId = normalizeScopeValue(topic.globalId);
1160
+ const mappedProjectId = asMappedProjectId(topic);
1161
+ return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
1162
+ });
1163
+ }
1164
+ }
1165
+ async function tryResolveHostTopicById(ctx, topicId) {
1166
+ if (typeof ctx.runQuery !== "function") {
1167
+ return null;
1168
+ }
1169
+ try {
1170
+ return await ctx.runQuery(api2.topics.get, {
1171
+ id: topicId
1172
+ }) ?? null;
1173
+ } catch {
1174
+ return null;
1175
+ }
1176
+ }
1177
+ async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
1178
+ if (typeof ctx.runQuery !== "function") {
1179
+ return null;
1180
+ }
1181
+ try {
1182
+ return await ctx.runQuery(api2.topics.getByLegacyScopeId, {
1183
+ projectId: legacyScopeId
1184
+ }) ?? null;
1185
+ } catch {
1186
+ return null;
1187
+ }
1188
+ }
1189
+ async function resolveInheritedWorkspaceScope(ctx, topic) {
1190
+ const MAX_DEPTH = 10;
1191
+ let tenantId = normalizeScopeValue(topic.tenantId);
1192
+ let workspaceId = normalizeScopeValue(topic.workspaceId);
1193
+ if (tenantId && workspaceId) {
1194
+ return { tenantId, workspaceId };
1195
+ }
1196
+ let current = topic;
1197
+ for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
1198
+ current = await ctx.db.get(current.parentTopicId);
1199
+ if (!current) break;
1200
+ if (!tenantId) {
1201
+ tenantId = normalizeScopeValue(current.tenantId);
1202
+ }
1203
+ if (!workspaceId) {
1204
+ workspaceId = normalizeScopeValue(current.workspaceId);
1205
+ }
1206
+ if (tenantId && workspaceId) break;
1207
+ }
1208
+ return { tenantId, workspaceId };
1209
+ }
1210
+ async function resolveTopicProjectScope(ctx, args) {
1211
+ if (args.topicId) {
1212
+ let topic = null;
1213
+ try {
1214
+ topic = await ctx.db.get(args.topicId);
1215
+ } catch {
1216
+ }
1217
+ if (!topic) {
1218
+ topic = await tryResolveHostTopicById(ctx, String(args.topicId));
1219
+ }
1220
+ if (!topic) {
1221
+ topic = pickPrimaryTopic(
1222
+ await findTopicsByScopeAlias(ctx, String(args.topicId))
1223
+ ) ?? null;
1224
+ }
1225
+ if (!topic) {
1226
+ throw new Error(`Topic not found: ${String(args.topicId)}`);
1227
+ }
1228
+ const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
1229
+ const mapped = asMappedProjectId(topic);
1230
+ if (mapped) {
1231
+ return {
1232
+ topicId: topic._id,
1233
+ projectId: mapped,
1234
+ tenantId: inherited.tenantId,
1235
+ workspaceId: inherited.workspaceId,
1236
+ source: "topic"
1237
+ };
1238
+ }
1239
+ return {
1240
+ topicId: topic._id,
1241
+ tenantId: inherited.tenantId,
1242
+ workspaceId: inherited.workspaceId,
1243
+ source: "topic"
1244
+ };
1245
+ }
1246
+ if (args.projectId) {
1247
+ let directTopic = null;
1248
+ try {
1249
+ directTopic = await ctx.db.get(
1250
+ args.projectId
1251
+ );
1252
+ } catch {
1253
+ }
1254
+ if (directTopic) {
1255
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1256
+ const mapped = asMappedProjectId(directTopic);
1257
+ return {
1258
+ topicId: directTopic._id,
1259
+ projectId: mapped ?? args.projectId,
1260
+ tenantId: inherited.tenantId,
1261
+ workspaceId: inherited.workspaceId,
1262
+ source: "topic_inferred"
1263
+ };
1264
+ }
1265
+ directTopic = await tryResolveHostTopicByLegacyScope(ctx, args.projectId);
1266
+ if (directTopic) {
1267
+ const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
1268
+ const mapped = asMappedProjectId(directTopic);
1269
+ return {
1270
+ topicId: directTopic._id,
1271
+ projectId: mapped ?? args.projectId,
1272
+ tenantId: inherited.tenantId,
1273
+ workspaceId: inherited.workspaceId,
1274
+ source: "topic_inferred"
1275
+ };
1276
+ }
1277
+ const topics = await findTopicsByScopeAlias(ctx, args.projectId);
1278
+ const primary = pickPrimaryTopic(topics);
1279
+ if (primary) {
1280
+ const inherited = await resolveInheritedWorkspaceScope(ctx, primary);
1281
+ return {
1282
+ topicId: primary._id,
1283
+ projectId: args.projectId,
1284
+ tenantId: inherited.tenantId,
1285
+ workspaceId: inherited.workspaceId,
1286
+ source: "project_mapped_topic"
1287
+ };
1288
+ }
1289
+ throw new Error(
1290
+ `Legacy project scope ${String(args.projectId)} has no mapped topic.`
1291
+ );
1292
+ }
1293
+ throw new Error(
1294
+ "Missing scope: provide topicId (preferred) or legacy projectId alias."
1295
+ );
1296
+ }
1115
1297
  var optionalScopeArgs = {
1116
1298
  projectId: v.optional(v.string()),
1117
1299
  topicId: v.optional(v.string())
@@ -1618,11 +1800,15 @@ var getByProject = query({
1618
1800
  if (!hasAccess) {
1619
1801
  return [];
1620
1802
  }
1803
+ const scope = await resolveTopicProjectScope(ctx, args).catch(() => null);
1804
+ if (!scope) {
1805
+ return [];
1806
+ }
1621
1807
  const pageSize = Math.max(1, Math.min(Math.floor(args.limit ?? 300), 1e3));
1622
1808
  const questionScanLimit = Math.min(pageSize * 2, 1e3);
1623
1809
  const questions = await ctx.db.query("epistemicNodes").withIndex(
1624
- args.topicId ? "by_topic_type" : "by_project_type",
1625
- (q) => args.topicId ? q.eq("topicId", args.topicId).eq("nodeType", "question") : q.eq("projectId", args.projectId).eq("nodeType", "question")
1810
+ "by_topic_type",
1811
+ (q) => q.eq("topicId", scope.topicId).eq("nodeType", "question")
1626
1812
  ).order("desc").take(questionScanLimit);
1627
1813
  const questionIds = questions.slice(0, pageSize).map((q) => q._id);
1628
1814
  const allLinks = await Promise.all(