@lucern/graph-primitives 1.0.48 → 1.0.52

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 (67) hide show
  1. package/dist/beliefDecay.js +34 -186
  2. package/dist/beliefEvidenceLinks.js +32 -187
  3. package/dist/contradictions.js +34 -187
  4. package/dist/entityLifecycle.js +88 -205
  5. package/dist/epistemicAnswers.js +35 -187
  6. package/dist/epistemicBeliefs.admin.js +34 -187
  7. package/dist/epistemicBeliefs.backfills.js +34 -188
  8. package/dist/epistemicBeliefs.confidence.d.ts +1 -1
  9. package/dist/epistemicBeliefs.confidence.js +32 -188
  10. package/dist/epistemicBeliefs.core.js +37 -187
  11. package/dist/epistemicBeliefs.d.ts +1 -1
  12. package/dist/epistemicBeliefs.helpers.d.ts +1 -1
  13. package/dist/epistemicBeliefs.helpers.js +34 -186
  14. package/dist/epistemicBeliefs.internal.js +37 -187
  15. package/dist/epistemicBeliefs.js +37 -187
  16. package/dist/epistemicBeliefs.lifecycle.js +32 -188
  17. package/dist/epistemicBeliefs.links.js +34 -188
  18. package/dist/epistemicContracts.evaluators.js +34 -188
  19. package/dist/epistemicContracts.handlers.js +34 -188
  20. package/dist/epistemicContracts.js +34 -188
  21. package/dist/epistemicEdges.d.ts +1 -1
  22. package/dist/epistemicEdges.helpers.d.ts +2 -2
  23. package/dist/epistemicEdges.js +32 -187
  24. package/dist/epistemicEdges.mutations.js +34 -186
  25. package/dist/epistemicEdges.queries.js +35 -188
  26. package/dist/epistemicEdges.types.d.ts +1 -1
  27. package/dist/epistemicEvidence.d.ts +1 -1
  28. package/dist/epistemicEvidence.js +37 -187
  29. package/dist/epistemicEvidenceHelpers.d.ts +1 -1
  30. package/dist/epistemicEvidenceHelpers.js +34 -186
  31. package/dist/epistemicEvidenceMutations.js +37 -187
  32. package/dist/epistemicEvidenceQueries.js +34 -186
  33. package/dist/epistemicHelpers.js +4 -1
  34. package/dist/epistemicInsert.js +4 -1
  35. package/dist/epistemicNodeCreation.js +4 -1
  36. package/dist/epistemicNodes.helpers.d.ts +1 -1
  37. package/dist/epistemicNodes.internal.js +35 -188
  38. package/dist/epistemicNodes.js +37 -188
  39. package/dist/epistemicNodes.mutations.js +35 -188
  40. package/dist/epistemicNodes.queries.js +35 -188
  41. package/dist/epistemicQuestions.conviction.js +34 -186
  42. package/dist/epistemicQuestions.create.js +37 -187
  43. package/dist/epistemicQuestions.d.ts +1 -1
  44. package/dist/epistemicQuestions.evidence.js +37 -187
  45. package/dist/epistemicQuestions.helpers.d.ts +1 -1
  46. package/dist/epistemicQuestions.helpers.js +34 -186
  47. package/dist/epistemicQuestions.js +37 -187
  48. package/dist/epistemicQuestions.lifecycle.js +34 -186
  49. package/dist/epistemicQuestions.queries.js +34 -186
  50. package/dist/epistemicQuestions.sprint.js +35 -188
  51. package/dist/epistemicQuestions.tail.js +37 -187
  52. package/dist/epistemicSources.js +35 -188
  53. package/dist/index.d.ts +1 -1
  54. package/dist/index.js +98 -213
  55. package/dist/proof-attestation.json +1 -1
  56. package/dist/questionEvidenceLinks.js +34 -187
  57. package/dist/scopeResolverCompat.d.ts +1 -1
  58. package/dist/scopeResolverCompat.js +32 -193
  59. package/dist/topicOntologyResolver.d.ts +3 -3
  60. package/dist/topicOntologyResolver.js +57 -18
  61. package/dist/{topicScope-DJVa0mLa.d.ts → topicScope-CL1IVOmv.d.ts} +2 -2
  62. package/dist/topicScope.d.ts +1 -1
  63. package/dist/topicScope.js +32 -193
  64. package/dist/workflowBridge.js +32 -193
  65. package/dist/workspaceIsolation.d.ts +1 -1
  66. package/dist/workspaceIsolation.js +32 -193
  67. package/package.json +4 -4
@@ -5,16 +5,15 @@ import '@lucern/access-control/auth';
5
5
  import { throwStructuredMutationError } from '@lucern/access-control/structuredMutationError';
6
6
  import { normalizeTupleContradictionPolicy, mkOpinion, conditionalDeduction, project, dampedDependencyCascade, trustDiscount, applyNegativeSupport, cumulativeFusion, applyNegativeEvidence, confidenceFromSL, detectTupleContradiction, evaluateTupleContradictionTransition, readOpinionFromRecord, hasProjectedOpinionChanged, deriveContractModulationPlan, deriveContractStatus, parseEvidentialEvaluatorConfig, compareMetricValue, resolveComparisonResult, buildEvidentialRationale, parseMetricCheckerConfig, getEvaluatorInputRecord, pickFiniteNumber, buildComparisonRationale, parseReferenceCheckCounterConfig, parseTemporalDeadlineConfig, parseMarketIndexComparatorConfig } from '@lucern/confidence';
7
7
  import { v } from 'convex/values';
8
+ import { generateUuidV7, isUuidV7 } from '@lucern/contracts/ids';
8
9
  import '@lucern/contracts/schema-helpers/spine/tables/epistemicNodes';
9
10
  import { requireScopeWriteAccess } from '@lucern/access-control/access';
10
- import { generateUuidV7 } from '@lucern/contracts/ids';
11
11
  import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
12
12
 
13
13
  // src/convex.ts
14
14
  var unsafeApi = unsafeConvexAnyApi(
15
15
  "graph-primitives top-level module bundle lacks a committed Convex _generated/api surface"
16
16
  );
17
- var api = unsafeApi;
18
17
  componentsGeneric();
19
18
  var internal = unsafeApi;
20
19
  var internalMutation = internalMutationGeneric;
@@ -151,6 +150,8 @@ function promoteBeliefStatusAfterScoring(status, opts) {
151
150
  }
152
151
  return status;
153
152
  }
153
+
154
+ // src/topicScope.ts
154
155
  var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
155
156
  async function resolveTopicNodeScopeOrNull(ctx, ref) {
156
157
  if (!ctx?.db || typeof ctx.db.query !== "function") {
@@ -171,16 +172,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
171
172
  if (!node) {
172
173
  return null;
173
174
  }
174
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
175
+ const scopeKey = canonicalTopicGlobalId(node);
175
176
  if (!scopeKey) {
176
- return null;
177
+ throw new Error(
178
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
179
+ );
177
180
  }
181
+ const metadata = node.metadata ?? {};
178
182
  return {
179
183
  topicId: scopeKey,
180
184
  projectId: asMappedProjectId(node),
181
- source: "topic_node"
185
+ source: "topic_node",
186
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
187
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
182
188
  };
183
189
  }
190
+ function canonicalTopicGlobalId(node) {
191
+ const globalId = normalizeScopeValue(node.globalId);
192
+ if (globalId && isUuidV7(globalId)) {
193
+ return globalId;
194
+ }
195
+ const topicId = normalizeScopeValue(node.topicId);
196
+ return topicId && isUuidV7(topicId) ? topicId : null;
197
+ }
198
+ function requireUuidV7TopicScope(field, value) {
199
+ if (!isUuidV7(value)) {
200
+ throw new Error(
201
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
202
+ );
203
+ }
204
+ }
184
205
  function asMappedProjectId(topic) {
185
206
  if (!topic) {
186
207
  return;
@@ -202,200 +223,25 @@ function normalizeScopeValue(value) {
202
223
  const normalized = value.trim();
203
224
  return normalized.length > 0 ? normalized : void 0;
204
225
  }
205
- function pickPrimaryTopic(candidates) {
206
- return [...candidates].sort((a, b) => {
207
- const depthA = a.depth ?? 9999;
208
- const depthB = b.depth ?? 9999;
209
- if (depthA !== depthB) {
210
- return depthA - depthB;
211
- }
212
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
213
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
214
- if (createdA !== createdB) {
215
- return createdA - createdB;
216
- }
217
- return String(a.name || "").localeCompare(String(b.name || ""));
218
- })[0];
219
- }
220
- async function findTopicsByScopeAlias(ctx, scopeId) {
221
- const query = ctx.db.query("topics");
222
- try {
223
- return await query.withIndex(
224
- "by_graph_scope_project",
225
- (q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
226
- ).collect();
227
- } catch (error) {
228
- debugGraphPrimitiveFallback(
229
- "[topicScope] Failed to resolve scope alias via index",
230
- {
231
- error,
232
- scopeId
233
- }
234
- );
235
- const topics = await query.collect();
236
- return topics.filter((topic) => {
237
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
238
- const mappedProjectId = asMappedProjectId(topic);
239
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
240
- });
241
- }
242
- }
243
- async function tryResolveHostTopicById(ctx, topicId) {
244
- if (typeof ctx.runQuery !== "function") {
245
- return null;
246
- }
247
- try {
248
- return await ctx.runQuery(api.topics.get, {
249
- id: topicId
250
- }) ?? null;
251
- } catch (error) {
252
- debugGraphPrimitiveFallback(
253
- "[topicScope] Failed to resolve topic by host query",
254
- {
255
- error,
256
- topicId
257
- }
258
- );
259
- return null;
260
- }
261
- }
262
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
263
- if (typeof ctx.runQuery !== "function") {
264
- return null;
265
- }
266
- try {
267
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
268
- projectId: legacyScopeId
269
- }) ?? null;
270
- } catch (error) {
271
- debugGraphPrimitiveFallback(
272
- "[topicScope] Failed to resolve topic by legacy scope",
273
- {
274
- error,
275
- legacyScopeId
276
- }
277
- );
278
- return null;
279
- }
280
- }
281
- async function resolveInheritedWorkspaceScope(ctx, topic) {
282
- const MAX_DEPTH = 10;
283
- let tenantId = normalizeScopeValue(topic.tenantId);
284
- let workspaceId = normalizeScopeValue(topic.workspaceId);
285
- if (tenantId && workspaceId) {
286
- return { tenantId, workspaceId };
287
- }
288
- let current = topic;
289
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
290
- current = await ctx.db.get(current.parentTopicId);
291
- if (!current) {
292
- break;
293
- }
294
- if (!tenantId) {
295
- tenantId = normalizeScopeValue(current.tenantId);
296
- }
297
- if (!workspaceId) {
298
- workspaceId = normalizeScopeValue(current.workspaceId);
299
- }
300
- if (tenantId && workspaceId) {
301
- break;
302
- }
303
- }
304
- return { tenantId, workspaceId };
305
- }
306
226
  async function resolveTopicProjectScope(ctx, args) {
307
227
  if (args.topicId) {
308
228
  return await resolveScopeFromTopicId(ctx, args.topicId);
309
229
  }
310
230
  if (args.projectId) {
311
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
231
+ throw new Error(
232
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
233
+ );
312
234
  }
313
- throw new Error(
314
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
315
- );
235
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
316
236
  }
317
237
  async function resolveScopeFromTopicId(ctx, topicId) {
318
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
319
- if (topic) {
320
- return await buildTopicScope(ctx, topic, "topic");
321
- }
322
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
238
+ const topicGlobalId = String(topicId);
239
+ requireUuidV7TopicScope("topicId", topicGlobalId);
240
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
323
241
  if (nodeScope) {
324
242
  return nodeScope;
325
243
  }
326
- throw new Error(`Topic not found: ${String(topicId)}`);
327
- }
328
- async function resolveTopicDocFromTopicId(ctx, topicId) {
329
- const direct = await tryReadTopicDoc(ctx, topicId, {
330
- failureLog: "[topicScope] Failed to load topic by direct id",
331
- idLogKey: "topicId"
332
- });
333
- if (direct) {
334
- return direct;
335
- }
336
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
337
- if (hostTopic) {
338
- return hostTopic;
339
- }
340
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
341
- }
342
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
343
- const directTopic = await resolveDirectLegacyProjectTopic(
344
- ctx,
345
- legacyProjectId
346
- );
347
- if (directTopic) {
348
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
349
- fallbackProjectId: legacyProjectId
350
- });
351
- }
352
- const primary = pickPrimaryTopic(
353
- await findTopicsByScopeAlias(ctx, legacyProjectId)
354
- );
355
- if (primary) {
356
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
357
- fallbackProjectId: legacyProjectId
358
- });
359
- }
360
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
361
- if (nodeScope) {
362
- return {
363
- ...nodeScope,
364
- projectId: nodeScope.projectId ?? legacyProjectId
365
- };
366
- }
367
- throw new Error(
368
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
369
- );
370
- }
371
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
372
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
373
- failureLog: "[topicScope] Failed to load direct project topic",
374
- idLogKey: "projectId"
375
- });
376
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
377
- }
378
- async function tryReadTopicDoc(ctx, id, log) {
379
- try {
380
- return await ctx.db.get(id);
381
- } catch (error) {
382
- debugGraphPrimitiveFallback(log.failureLog, {
383
- error,
384
- [log.idLogKey]: id
385
- });
386
- return null;
387
- }
388
- }
389
- async function buildTopicScope(ctx, topic, source, options = {}) {
390
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
391
- const mapped = asMappedProjectId(topic);
392
- return {
393
- topicId: topic._id,
394
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
395
- tenantId: inherited.tenantId,
396
- workspaceId: inherited.workspaceId,
397
- source
398
- };
244
+ throw new Error(`Topic not found: ${topicGlobalId}`);
399
245
  }
400
246
  ({
401
247
  projectId: v.optional(v.string()),
@@ -7,14 +7,13 @@ import { componentsGeneric, internalMutationGeneric, queryGeneric, mutationGener
7
7
  import { normalizeTupleContradictionPolicy, mkOpinion, conditionalDeduction, project, dampedDependencyCascade, deriveContractModulationPlan, deriveContractStatus, trustDiscount, applyNegativeSupport, cumulativeFusion, applyNegativeEvidence, confidenceFromSL, detectTupleContradiction, evaluateTupleContradictionTransition, readOpinionFromRecord, parseEvidentialEvaluatorConfig, compareMetricValue, resolveComparisonResult, buildEvidentialRationale, parseMetricCheckerConfig, getEvaluatorInputRecord, pickFiniteNumber, buildComparisonRationale, parseReferenceCheckCounterConfig, parseTemporalDeadlineConfig, parseMarketIndexComparatorConfig, hasProjectedOpinionChanged, createInheritedContractRecord } from '@lucern/confidence';
8
8
  import '@lucern/access-control/audience';
9
9
  import { throwStructuredMutationError } from '@lucern/access-control/structuredMutationError';
10
+ import { generateGlobalId, generateUuidV7, isUuidV7 } from '@lucern/contracts/ids';
10
11
  import '@lucern/contracts/schema-helpers/spine/tables/epistemicNodes';
11
- import { generateGlobalId, generateUuidV7 } from '@lucern/contracts/ids';
12
12
 
13
13
  // src/epistemicContracts.handlers.ts
14
14
  var unsafeApi = unsafeConvexAnyApi(
15
15
  "graph-primitives top-level module bundle lacks a committed Convex _generated/api surface"
16
16
  );
17
- var api = unsafeApi;
18
17
  componentsGeneric();
19
18
  var internal = unsafeApi;
20
19
  var internalMutation = internalMutationGeneric;
@@ -153,6 +152,8 @@ function promoteBeliefStatusAfterScoring(status, opts) {
153
152
  }
154
153
  return status;
155
154
  }
155
+
156
+ // src/topicScope.ts
156
157
  var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
157
158
  async function resolveTopicNodeScopeOrNull(ctx, ref) {
158
159
  if (!ctx?.db || typeof ctx.db.query !== "function") {
@@ -173,16 +174,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
173
174
  if (!node) {
174
175
  return null;
175
176
  }
176
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
177
+ const scopeKey = canonicalTopicGlobalId(node);
177
178
  if (!scopeKey) {
178
- return null;
179
+ throw new Error(
180
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
181
+ );
179
182
  }
183
+ const metadata = node.metadata ?? {};
180
184
  return {
181
185
  topicId: scopeKey,
182
186
  projectId: asMappedProjectId(node),
183
- source: "topic_node"
187
+ source: "topic_node",
188
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
189
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
184
190
  };
185
191
  }
192
+ function canonicalTopicGlobalId(node) {
193
+ const globalId = normalizeScopeValue(node.globalId);
194
+ if (globalId && isUuidV7(globalId)) {
195
+ return globalId;
196
+ }
197
+ const topicId = normalizeScopeValue(node.topicId);
198
+ return topicId && isUuidV7(topicId) ? topicId : null;
199
+ }
200
+ function requireUuidV7TopicScope(field, value) {
201
+ if (!isUuidV7(value)) {
202
+ throw new Error(
203
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
204
+ );
205
+ }
206
+ }
186
207
  function asMappedProjectId(topic) {
187
208
  if (!topic) {
188
209
  return;
@@ -204,200 +225,25 @@ function normalizeScopeValue(value) {
204
225
  const normalized = value.trim();
205
226
  return normalized.length > 0 ? normalized : void 0;
206
227
  }
207
- function pickPrimaryTopic(candidates) {
208
- return [...candidates].sort((a, b) => {
209
- const depthA = a.depth ?? 9999;
210
- const depthB = b.depth ?? 9999;
211
- if (depthA !== depthB) {
212
- return depthA - depthB;
213
- }
214
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
215
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
216
- if (createdA !== createdB) {
217
- return createdA - createdB;
218
- }
219
- return String(a.name || "").localeCompare(String(b.name || ""));
220
- })[0];
221
- }
222
- async function findTopicsByScopeAlias(ctx, scopeId) {
223
- const query2 = ctx.db.query("topics");
224
- try {
225
- return await query2.withIndex(
226
- "by_graph_scope_project",
227
- (q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
228
- ).collect();
229
- } catch (error) {
230
- debugGraphPrimitiveFallback(
231
- "[topicScope] Failed to resolve scope alias via index",
232
- {
233
- error,
234
- scopeId
235
- }
236
- );
237
- const topics = await query2.collect();
238
- return topics.filter((topic) => {
239
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
240
- const mappedProjectId = asMappedProjectId(topic);
241
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
242
- });
243
- }
244
- }
245
- async function tryResolveHostTopicById(ctx, topicId) {
246
- if (typeof ctx.runQuery !== "function") {
247
- return null;
248
- }
249
- try {
250
- return await ctx.runQuery(api.topics.get, {
251
- id: topicId
252
- }) ?? null;
253
- } catch (error) {
254
- debugGraphPrimitiveFallback(
255
- "[topicScope] Failed to resolve topic by host query",
256
- {
257
- error,
258
- topicId
259
- }
260
- );
261
- return null;
262
- }
263
- }
264
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
265
- if (typeof ctx.runQuery !== "function") {
266
- return null;
267
- }
268
- try {
269
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
270
- projectId: legacyScopeId
271
- }) ?? null;
272
- } catch (error) {
273
- debugGraphPrimitiveFallback(
274
- "[topicScope] Failed to resolve topic by legacy scope",
275
- {
276
- error,
277
- legacyScopeId
278
- }
279
- );
280
- return null;
281
- }
282
- }
283
- async function resolveInheritedWorkspaceScope(ctx, topic) {
284
- const MAX_DEPTH = 10;
285
- let tenantId = normalizeScopeValue(topic.tenantId);
286
- let workspaceId = normalizeScopeValue(topic.workspaceId);
287
- if (tenantId && workspaceId) {
288
- return { tenantId, workspaceId };
289
- }
290
- let current = topic;
291
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
292
- current = await ctx.db.get(current.parentTopicId);
293
- if (!current) {
294
- break;
295
- }
296
- if (!tenantId) {
297
- tenantId = normalizeScopeValue(current.tenantId);
298
- }
299
- if (!workspaceId) {
300
- workspaceId = normalizeScopeValue(current.workspaceId);
301
- }
302
- if (tenantId && workspaceId) {
303
- break;
304
- }
305
- }
306
- return { tenantId, workspaceId };
307
- }
308
228
  async function resolveTopicProjectScope(ctx, args) {
309
229
  if (args.topicId) {
310
230
  return await resolveScopeFromTopicId(ctx, args.topicId);
311
231
  }
312
232
  if (args.projectId) {
313
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
233
+ throw new Error(
234
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
235
+ );
314
236
  }
315
- throw new Error(
316
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
317
- );
237
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
318
238
  }
319
239
  async function resolveScopeFromTopicId(ctx, topicId) {
320
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
321
- if (topic) {
322
- return await buildTopicScope(ctx, topic, "topic");
323
- }
324
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
240
+ const topicGlobalId = String(topicId);
241
+ requireUuidV7TopicScope("topicId", topicGlobalId);
242
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
325
243
  if (nodeScope) {
326
244
  return nodeScope;
327
245
  }
328
- throw new Error(`Topic not found: ${String(topicId)}`);
329
- }
330
- async function resolveTopicDocFromTopicId(ctx, topicId) {
331
- const direct = await tryReadTopicDoc(ctx, topicId, {
332
- failureLog: "[topicScope] Failed to load topic by direct id",
333
- idLogKey: "topicId"
334
- });
335
- if (direct) {
336
- return direct;
337
- }
338
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
339
- if (hostTopic) {
340
- return hostTopic;
341
- }
342
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
343
- }
344
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
345
- const directTopic = await resolveDirectLegacyProjectTopic(
346
- ctx,
347
- legacyProjectId
348
- );
349
- if (directTopic) {
350
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
351
- fallbackProjectId: legacyProjectId
352
- });
353
- }
354
- const primary = pickPrimaryTopic(
355
- await findTopicsByScopeAlias(ctx, legacyProjectId)
356
- );
357
- if (primary) {
358
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
359
- fallbackProjectId: legacyProjectId
360
- });
361
- }
362
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
363
- if (nodeScope) {
364
- return {
365
- ...nodeScope,
366
- projectId: nodeScope.projectId ?? legacyProjectId
367
- };
368
- }
369
- throw new Error(
370
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
371
- );
372
- }
373
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
374
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
375
- failureLog: "[topicScope] Failed to load direct project topic",
376
- idLogKey: "projectId"
377
- });
378
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
379
- }
380
- async function tryReadTopicDoc(ctx, id, log) {
381
- try {
382
- return await ctx.db.get(id);
383
- } catch (error) {
384
- debugGraphPrimitiveFallback(log.failureLog, {
385
- error,
386
- [log.idLogKey]: id
387
- });
388
- return null;
389
- }
390
- }
391
- async function buildTopicScope(ctx, topic, source, options = {}) {
392
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
393
- const mapped = asMappedProjectId(topic);
394
- return {
395
- topicId: topic._id,
396
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
397
- tenantId: inherited.tenantId,
398
- workspaceId: inherited.workspaceId,
399
- source
400
- };
246
+ throw new Error(`Topic not found: ${topicGlobalId}`);
401
247
  }
402
248
  ({
403
249
  projectId: v.optional(v.string()),