@lucern/graph-primitives 1.0.50 → 1.0.53

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 +1 -1
  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
@@ -2,7 +2,7 @@ import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
2
2
  import { v } from 'convex/values';
3
3
  import { unsafeConvexAnyApi } from '@lucern/contracts/convex/unsafeAnyApi';
4
4
  import { componentsGeneric, internalMutationGeneric } from 'convex/server';
5
- import { generateGlobalId, assertUuidV7Identity } from '@lucern/contracts/ids';
5
+ import { generateGlobalId, assertUuidV7Identity, assertUuidV7Reference, isUuidV7 } from '@lucern/contracts/ids';
6
6
  import '@lucern/contracts/manifests/edge-policy-manifest';
7
7
  import '@lucern/contracts/manifests/edge-policy-manifest.data';
8
8
 
@@ -47,6 +47,9 @@ function debugGraphPrimitiveFallback(message, context) {
47
47
  }
48
48
  function insertEpistemicNode(ctx, doc) {
49
49
  assertUuidV7Identity("epistemicNodes", doc.globalId);
50
+ if (doc.topicId !== void 0 && doc.topicId !== null) {
51
+ assertUuidV7Reference("epistemicNodes.topicId", doc.topicId);
52
+ }
50
53
  return ctx.db.insert("epistemicNodes", doc);
51
54
  }
52
55
 
@@ -424,6 +427,8 @@ function resolveGraphPrimitivesAppResolvers(_ctx) {
424
427
  ...resolverOverrides
425
428
  };
426
429
  }
430
+
431
+ // src/topicScope.ts
427
432
  var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
428
433
  async function resolveTopicNodeScopeOrNull(ctx, ref) {
429
434
  if (!ctx?.db || typeof ctx.db.query !== "function") {
@@ -444,16 +449,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
444
449
  if (!node) {
445
450
  return null;
446
451
  }
447
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
452
+ const scopeKey = canonicalTopicGlobalId(node);
448
453
  if (!scopeKey) {
449
- return null;
454
+ throw new Error(
455
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
456
+ );
450
457
  }
458
+ const metadata = node.metadata ?? {};
451
459
  return {
452
460
  topicId: scopeKey,
453
461
  projectId: asMappedProjectId(node),
454
- source: "topic_node"
462
+ source: "topic_node",
463
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
464
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
455
465
  };
456
466
  }
467
+ function canonicalTopicGlobalId(node) {
468
+ const globalId = normalizeScopeValue(node.globalId);
469
+ if (globalId && isUuidV7(globalId)) {
470
+ return globalId;
471
+ }
472
+ const topicId = normalizeScopeValue(node.topicId);
473
+ return topicId && isUuidV7(topicId) ? topicId : null;
474
+ }
475
+ function requireUuidV7TopicScope(field, value) {
476
+ if (!isUuidV7(value)) {
477
+ throw new Error(
478
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
479
+ );
480
+ }
481
+ }
457
482
  function asMappedProjectId(topic) {
458
483
  if (!topic) {
459
484
  return;
@@ -475,200 +500,25 @@ function normalizeScopeValue(value) {
475
500
  const normalized = value.trim();
476
501
  return normalized.length > 0 ? normalized : void 0;
477
502
  }
478
- function pickPrimaryTopic(candidates) {
479
- return [...candidates].sort((a, b) => {
480
- const depthA = a.depth ?? 9999;
481
- const depthB = b.depth ?? 9999;
482
- if (depthA !== depthB) {
483
- return depthA - depthB;
484
- }
485
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
486
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
487
- if (createdA !== createdB) {
488
- return createdA - createdB;
489
- }
490
- return String(a.name || "").localeCompare(String(b.name || ""));
491
- })[0];
492
- }
493
- async function findTopicsByScopeAlias(ctx, scopeId) {
494
- const query2 = ctx.db.query("topics");
495
- try {
496
- return await query2.withIndex(
497
- "by_graph_scope_project",
498
- (q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
499
- ).collect();
500
- } catch (error) {
501
- debugGraphPrimitiveFallback(
502
- "[topicScope] Failed to resolve scope alias via index",
503
- {
504
- error,
505
- scopeId
506
- }
507
- );
508
- const topics = await query2.collect();
509
- return topics.filter((topic) => {
510
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
511
- const mappedProjectId = asMappedProjectId(topic);
512
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
513
- });
514
- }
515
- }
516
- async function tryResolveHostTopicById(ctx, topicId) {
517
- if (typeof ctx.runQuery !== "function") {
518
- return null;
519
- }
520
- try {
521
- return await ctx.runQuery(api.topics.get, {
522
- id: topicId
523
- }) ?? null;
524
- } catch (error) {
525
- debugGraphPrimitiveFallback(
526
- "[topicScope] Failed to resolve topic by host query",
527
- {
528
- error,
529
- topicId
530
- }
531
- );
532
- return null;
533
- }
534
- }
535
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
536
- if (typeof ctx.runQuery !== "function") {
537
- return null;
538
- }
539
- try {
540
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
541
- projectId: legacyScopeId
542
- }) ?? null;
543
- } catch (error) {
544
- debugGraphPrimitiveFallback(
545
- "[topicScope] Failed to resolve topic by legacy scope",
546
- {
547
- error,
548
- legacyScopeId
549
- }
550
- );
551
- return null;
552
- }
553
- }
554
- async function resolveInheritedWorkspaceScope(ctx, topic) {
555
- const MAX_DEPTH = 10;
556
- let tenantId = normalizeScopeValue(topic.tenantId);
557
- let workspaceId = normalizeScopeValue(topic.workspaceId);
558
- if (tenantId && workspaceId) {
559
- return { tenantId, workspaceId };
560
- }
561
- let current = topic;
562
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
563
- current = await ctx.db.get(current.parentTopicId);
564
- if (!current) {
565
- break;
566
- }
567
- if (!tenantId) {
568
- tenantId = normalizeScopeValue(current.tenantId);
569
- }
570
- if (!workspaceId) {
571
- workspaceId = normalizeScopeValue(current.workspaceId);
572
- }
573
- if (tenantId && workspaceId) {
574
- break;
575
- }
576
- }
577
- return { tenantId, workspaceId };
578
- }
579
503
  async function resolveTopicProjectScope(ctx, args) {
580
504
  if (args.topicId) {
581
505
  return await resolveScopeFromTopicId(ctx, args.topicId);
582
506
  }
583
507
  if (args.projectId) {
584
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
508
+ throw new Error(
509
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
510
+ );
585
511
  }
586
- throw new Error(
587
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
588
- );
512
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
589
513
  }
590
514
  async function resolveScopeFromTopicId(ctx, topicId) {
591
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
592
- if (topic) {
593
- return await buildTopicScope(ctx, topic, "topic");
594
- }
595
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
515
+ const topicGlobalId = String(topicId);
516
+ requireUuidV7TopicScope("topicId", topicGlobalId);
517
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
596
518
  if (nodeScope) {
597
519
  return nodeScope;
598
520
  }
599
- throw new Error(`Topic not found: ${String(topicId)}`);
600
- }
601
- async function resolveTopicDocFromTopicId(ctx, topicId) {
602
- const direct = await tryReadTopicDoc(ctx, topicId, {
603
- failureLog: "[topicScope] Failed to load topic by direct id",
604
- idLogKey: "topicId"
605
- });
606
- if (direct) {
607
- return direct;
608
- }
609
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
610
- if (hostTopic) {
611
- return hostTopic;
612
- }
613
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
614
- }
615
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
616
- const directTopic = await resolveDirectLegacyProjectTopic(
617
- ctx,
618
- legacyProjectId
619
- );
620
- if (directTopic) {
621
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
622
- fallbackProjectId: legacyProjectId
623
- });
624
- }
625
- const primary = pickPrimaryTopic(
626
- await findTopicsByScopeAlias(ctx, legacyProjectId)
627
- );
628
- if (primary) {
629
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
630
- fallbackProjectId: legacyProjectId
631
- });
632
- }
633
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
634
- if (nodeScope) {
635
- return {
636
- ...nodeScope,
637
- projectId: nodeScope.projectId ?? legacyProjectId
638
- };
639
- }
640
- throw new Error(
641
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
642
- );
643
- }
644
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
645
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
646
- failureLog: "[topicScope] Failed to load direct project topic",
647
- idLogKey: "projectId"
648
- });
649
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
650
- }
651
- async function tryReadTopicDoc(ctx, id, log) {
652
- try {
653
- return await ctx.db.get(id);
654
- } catch (error) {
655
- debugGraphPrimitiveFallback(log.failureLog, {
656
- error,
657
- [log.idLogKey]: id
658
- });
659
- return null;
660
- }
661
- }
662
- async function buildTopicScope(ctx, topic, source, options = {}) {
663
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
664
- const mapped = asMappedProjectId(topic);
665
- return {
666
- topicId: topic._id,
667
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
668
- tenantId: inherited.tenantId,
669
- workspaceId: inherited.workspaceId,
670
- source
671
- };
521
+ throw new Error(`Topic not found: ${topicGlobalId}`);
672
522
  }
673
523
  var optionalScopeArgs = {
674
524
  projectId: v.optional(v.string()),
@@ -1,5 +1,5 @@
1
1
  import * as _lucern_access_control_convex_js from '@lucern/access-control/convex.js';
2
- import { r as resolveTopicProjectScope, T as TopicProjectScope } from './topicScope-DJVa0mLa.js';
2
+ import { r as resolveTopicProjectScope, T as TopicProjectScope } from './topicScope-CL1IVOmv.js';
3
3
  import { Id, Doc, MutationCtx, QueryCtx, internalMutation, internalQuery, mutation, query } from './convex.js';
4
4
  import 'convex/values';
5
5
  import '@lucern/access-control/convex';
@@ -1,6 +1,7 @@
1
1
  import { unsafeConvexAnyApi } from '@lucern/contracts/convex/unsafeAnyApi';
2
2
  import { componentsGeneric, internalMutationGeneric, internalQueryGeneric, mutationGeneric, queryGeneric } from 'convex/server';
3
3
  import { v } from 'convex/values';
4
+ import { isUuidV7 } from '@lucern/contracts/ids';
4
5
 
5
6
  // src/convex.ts
6
7
  var unsafeApi = unsafeConvexAnyApi(
@@ -419,6 +420,8 @@ function resolveGraphPrimitivesAppResolvers(_ctx) {
419
420
  ...resolverOverrides
420
421
  };
421
422
  }
423
+
424
+ // src/topicScope.ts
422
425
  var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
423
426
  async function resolveTopicNodeScopeOrNull(ctx, ref) {
424
427
  if (!ctx?.db || typeof ctx.db.query !== "function") {
@@ -439,16 +442,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
439
442
  if (!node) {
440
443
  return null;
441
444
  }
442
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
445
+ const scopeKey = canonicalTopicGlobalId(node);
443
446
  if (!scopeKey) {
444
- return null;
447
+ throw new Error(
448
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
449
+ );
445
450
  }
451
+ const metadata = node.metadata ?? {};
446
452
  return {
447
453
  topicId: scopeKey,
448
454
  projectId: asMappedProjectId(node),
449
- source: "topic_node"
455
+ source: "topic_node",
456
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
457
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
450
458
  };
451
459
  }
460
+ function canonicalTopicGlobalId(node) {
461
+ const globalId = normalizeScopeValue(node.globalId);
462
+ if (globalId && isUuidV7(globalId)) {
463
+ return globalId;
464
+ }
465
+ const topicId = normalizeScopeValue(node.topicId);
466
+ return topicId && isUuidV7(topicId) ? topicId : null;
467
+ }
468
+ function requireUuidV7TopicScope(field, value) {
469
+ if (!isUuidV7(value)) {
470
+ throw new Error(
471
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
472
+ );
473
+ }
474
+ }
452
475
  function asMappedProjectId(topic) {
453
476
  if (!topic) {
454
477
  return;
@@ -470,200 +493,25 @@ function normalizeScopeValue(value) {
470
493
  const normalized = value.trim();
471
494
  return normalized.length > 0 ? normalized : void 0;
472
495
  }
473
- function pickPrimaryTopic(candidates) {
474
- return [...candidates].sort((a, b) => {
475
- const depthA = a.depth ?? 9999;
476
- const depthB = b.depth ?? 9999;
477
- if (depthA !== depthB) {
478
- return depthA - depthB;
479
- }
480
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
481
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
482
- if (createdA !== createdB) {
483
- return createdA - createdB;
484
- }
485
- return String(a.name || "").localeCompare(String(b.name || ""));
486
- })[0];
487
- }
488
- async function findTopicsByScopeAlias(ctx, scopeId) {
489
- const query2 = ctx.db.query("topics");
490
- try {
491
- return await query2.withIndex(
492
- "by_graph_scope_project",
493
- (q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
494
- ).collect();
495
- } catch (error) {
496
- debugGraphPrimitiveFallback(
497
- "[topicScope] Failed to resolve scope alias via index",
498
- {
499
- error,
500
- scopeId
501
- }
502
- );
503
- const topics = await query2.collect();
504
- return topics.filter((topic) => {
505
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
506
- const mappedProjectId = asMappedProjectId(topic);
507
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
508
- });
509
- }
510
- }
511
- async function tryResolveHostTopicById(ctx, topicId) {
512
- if (typeof ctx.runQuery !== "function") {
513
- return null;
514
- }
515
- try {
516
- return await ctx.runQuery(api.topics.get, {
517
- id: topicId
518
- }) ?? null;
519
- } catch (error) {
520
- debugGraphPrimitiveFallback(
521
- "[topicScope] Failed to resolve topic by host query",
522
- {
523
- error,
524
- topicId
525
- }
526
- );
527
- return null;
528
- }
529
- }
530
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
531
- if (typeof ctx.runQuery !== "function") {
532
- return null;
533
- }
534
- try {
535
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
536
- projectId: legacyScopeId
537
- }) ?? null;
538
- } catch (error) {
539
- debugGraphPrimitiveFallback(
540
- "[topicScope] Failed to resolve topic by legacy scope",
541
- {
542
- error,
543
- legacyScopeId
544
- }
545
- );
546
- return null;
547
- }
548
- }
549
- async function resolveInheritedWorkspaceScope(ctx, topic) {
550
- const MAX_DEPTH = 10;
551
- let tenantId = normalizeScopeValue(topic.tenantId);
552
- let workspaceId = normalizeScopeValue(topic.workspaceId);
553
- if (tenantId && workspaceId) {
554
- return { tenantId, workspaceId };
555
- }
556
- let current = topic;
557
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
558
- current = await ctx.db.get(current.parentTopicId);
559
- if (!current) {
560
- break;
561
- }
562
- if (!tenantId) {
563
- tenantId = normalizeScopeValue(current.tenantId);
564
- }
565
- if (!workspaceId) {
566
- workspaceId = normalizeScopeValue(current.workspaceId);
567
- }
568
- if (tenantId && workspaceId) {
569
- break;
570
- }
571
- }
572
- return { tenantId, workspaceId };
573
- }
574
496
  async function resolveTopicProjectScope(ctx, args) {
575
497
  if (args.topicId) {
576
498
  return await resolveScopeFromTopicId(ctx, args.topicId);
577
499
  }
578
500
  if (args.projectId) {
579
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
501
+ throw new Error(
502
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
503
+ );
580
504
  }
581
- throw new Error(
582
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
583
- );
505
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
584
506
  }
585
507
  async function resolveScopeFromTopicId(ctx, topicId) {
586
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
587
- if (topic) {
588
- return await buildTopicScope(ctx, topic, "topic");
589
- }
590
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
508
+ const topicGlobalId = String(topicId);
509
+ requireUuidV7TopicScope("topicId", topicGlobalId);
510
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
591
511
  if (nodeScope) {
592
512
  return nodeScope;
593
513
  }
594
- throw new Error(`Topic not found: ${String(topicId)}`);
595
- }
596
- async function resolveTopicDocFromTopicId(ctx, topicId) {
597
- const direct = await tryReadTopicDoc(ctx, topicId, {
598
- failureLog: "[topicScope] Failed to load topic by direct id",
599
- idLogKey: "topicId"
600
- });
601
- if (direct) {
602
- return direct;
603
- }
604
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
605
- if (hostTopic) {
606
- return hostTopic;
607
- }
608
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
609
- }
610
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
611
- const directTopic = await resolveDirectLegacyProjectTopic(
612
- ctx,
613
- legacyProjectId
614
- );
615
- if (directTopic) {
616
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
617
- fallbackProjectId: legacyProjectId
618
- });
619
- }
620
- const primary = pickPrimaryTopic(
621
- await findTopicsByScopeAlias(ctx, legacyProjectId)
622
- );
623
- if (primary) {
624
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
625
- fallbackProjectId: legacyProjectId
626
- });
627
- }
628
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
629
- if (nodeScope) {
630
- return {
631
- ...nodeScope,
632
- projectId: nodeScope.projectId ?? legacyProjectId
633
- };
634
- }
635
- throw new Error(
636
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
637
- );
638
- }
639
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
640
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
641
- failureLog: "[topicScope] Failed to load direct project topic",
642
- idLogKey: "projectId"
643
- });
644
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
645
- }
646
- async function tryReadTopicDoc(ctx, id, log) {
647
- try {
648
- return await ctx.db.get(id);
649
- } catch (error) {
650
- debugGraphPrimitiveFallback(log.failureLog, {
651
- error,
652
- [log.idLogKey]: id
653
- });
654
- return null;
655
- }
656
- }
657
- async function buildTopicScope(ctx, topic, source, options = {}) {
658
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
659
- const mapped = asMappedProjectId(topic);
660
- return {
661
- topicId: topic._id,
662
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
663
- tenantId: inherited.tenantId,
664
- workspaceId: inherited.workspaceId,
665
- source
666
- };
514
+ throw new Error(`Topic not found: ${topicGlobalId}`);
667
515
  }
668
516
  ({
669
517
  projectId: v.optional(v.string()),