@lucern/graph-primitives 1.0.50 → 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 +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
@@ -3,7 +3,7 @@ import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
3
3
  import { v } from 'convex/values';
4
4
  import { unsafeConvexAnyApi } from '@lucern/contracts/convex/unsafeAnyApi';
5
5
  import { componentsGeneric, mutationGeneric, internalMutationGeneric, queryGeneric } from 'convex/server';
6
- import { generateGlobalId, assertUuidV7Identity } from '@lucern/contracts/ids';
6
+ import { generateGlobalId, assertUuidV7Identity, assertUuidV7Reference, isUuidV7 } from '@lucern/contracts/ids';
7
7
  import '@lucern/contracts/manifests/edge-policy-manifest';
8
8
  import '@lucern/contracts/manifests/edge-policy-manifest.data';
9
9
 
@@ -19,6 +19,9 @@ var mutation = mutationGeneric;
19
19
  var query = queryGeneric;
20
20
  function insertEpistemicNode(ctx, doc) {
21
21
  assertUuidV7Identity("epistemicNodes", doc.globalId);
22
+ if (doc.topicId !== void 0 && doc.topicId !== null) {
23
+ assertUuidV7Reference("epistemicNodes.topicId", doc.topicId);
24
+ }
22
25
  return ctx.db.insert("epistemicNodes", doc);
23
26
  }
24
27
 
@@ -53,16 +56,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
53
56
  if (!node) {
54
57
  return null;
55
58
  }
56
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
59
+ const scopeKey = canonicalTopicGlobalId(node);
57
60
  if (!scopeKey) {
58
- return null;
61
+ throw new Error(
62
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
63
+ );
59
64
  }
65
+ const metadata = node.metadata ?? {};
60
66
  return {
61
67
  topicId: scopeKey,
62
68
  projectId: asMappedProjectId(node),
63
- source: "topic_node"
69
+ source: "topic_node",
70
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
71
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
64
72
  };
65
73
  }
74
+ function canonicalTopicGlobalId(node) {
75
+ const globalId = normalizeScopeValue(node.globalId);
76
+ if (globalId && isUuidV7(globalId)) {
77
+ return globalId;
78
+ }
79
+ const topicId = normalizeScopeValue(node.topicId);
80
+ return topicId && isUuidV7(topicId) ? topicId : null;
81
+ }
82
+ function requireUuidV7TopicScope(field, value) {
83
+ if (!isUuidV7(value)) {
84
+ throw new Error(
85
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
86
+ );
87
+ }
88
+ }
66
89
  function asMappedProjectId(topic) {
67
90
  if (!topic) {
68
91
  return;
@@ -84,200 +107,25 @@ function normalizeScopeValue(value) {
84
107
  const normalized = value.trim();
85
108
  return normalized.length > 0 ? normalized : void 0;
86
109
  }
87
- function pickPrimaryTopic(candidates) {
88
- return [...candidates].sort((a, b) => {
89
- const depthA = a.depth ?? 9999;
90
- const depthB = b.depth ?? 9999;
91
- if (depthA !== depthB) {
92
- return depthA - depthB;
93
- }
94
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
95
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
96
- if (createdA !== createdB) {
97
- return createdA - createdB;
98
- }
99
- return String(a.name || "").localeCompare(String(b.name || ""));
100
- })[0];
101
- }
102
- async function findTopicsByScopeAlias(ctx, scopeId) {
103
- const query2 = ctx.db.query("topics");
104
- try {
105
- return await query2.withIndex(
106
- "by_graph_scope_project",
107
- (q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
108
- ).collect();
109
- } catch (error) {
110
- debugGraphPrimitiveFallback(
111
- "[topicScope] Failed to resolve scope alias via index",
112
- {
113
- error,
114
- scopeId
115
- }
116
- );
117
- const topics = await query2.collect();
118
- return topics.filter((topic) => {
119
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
120
- const mappedProjectId = asMappedProjectId(topic);
121
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
122
- });
123
- }
124
- }
125
- async function tryResolveHostTopicById(ctx, topicId) {
126
- if (typeof ctx.runQuery !== "function") {
127
- return null;
128
- }
129
- try {
130
- return await ctx.runQuery(api.topics.get, {
131
- id: topicId
132
- }) ?? null;
133
- } catch (error) {
134
- debugGraphPrimitiveFallback(
135
- "[topicScope] Failed to resolve topic by host query",
136
- {
137
- error,
138
- topicId
139
- }
140
- );
141
- return null;
142
- }
143
- }
144
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
145
- if (typeof ctx.runQuery !== "function") {
146
- return null;
147
- }
148
- try {
149
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
150
- projectId: legacyScopeId
151
- }) ?? null;
152
- } catch (error) {
153
- debugGraphPrimitiveFallback(
154
- "[topicScope] Failed to resolve topic by legacy scope",
155
- {
156
- error,
157
- legacyScopeId
158
- }
159
- );
160
- return null;
161
- }
162
- }
163
- async function resolveInheritedWorkspaceScope(ctx, topic) {
164
- const MAX_DEPTH = 10;
165
- let tenantId = normalizeScopeValue(topic.tenantId);
166
- let workspaceId = normalizeScopeValue(topic.workspaceId);
167
- if (tenantId && workspaceId) {
168
- return { tenantId, workspaceId };
169
- }
170
- let current = topic;
171
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
172
- current = await ctx.db.get(current.parentTopicId);
173
- if (!current) {
174
- break;
175
- }
176
- if (!tenantId) {
177
- tenantId = normalizeScopeValue(current.tenantId);
178
- }
179
- if (!workspaceId) {
180
- workspaceId = normalizeScopeValue(current.workspaceId);
181
- }
182
- if (tenantId && workspaceId) {
183
- break;
184
- }
185
- }
186
- return { tenantId, workspaceId };
187
- }
188
110
  async function resolveTopicProjectScope(ctx, args) {
189
111
  if (args.topicId) {
190
112
  return await resolveScopeFromTopicId(ctx, args.topicId);
191
113
  }
192
114
  if (args.projectId) {
193
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
115
+ throw new Error(
116
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
117
+ );
194
118
  }
195
- throw new Error(
196
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
197
- );
119
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
198
120
  }
199
121
  async function resolveScopeFromTopicId(ctx, topicId) {
200
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
201
- if (topic) {
202
- return await buildTopicScope(ctx, topic, "topic");
203
- }
204
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
122
+ const topicGlobalId = String(topicId);
123
+ requireUuidV7TopicScope("topicId", topicGlobalId);
124
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
205
125
  if (nodeScope) {
206
126
  return nodeScope;
207
127
  }
208
- throw new Error(`Topic not found: ${String(topicId)}`);
209
- }
210
- async function resolveTopicDocFromTopicId(ctx, topicId) {
211
- const direct = await tryReadTopicDoc(ctx, topicId, {
212
- failureLog: "[topicScope] Failed to load topic by direct id",
213
- idLogKey: "topicId"
214
- });
215
- if (direct) {
216
- return direct;
217
- }
218
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
219
- if (hostTopic) {
220
- return hostTopic;
221
- }
222
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
223
- }
224
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
225
- const directTopic = await resolveDirectLegacyProjectTopic(
226
- ctx,
227
- legacyProjectId
228
- );
229
- if (directTopic) {
230
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
231
- fallbackProjectId: legacyProjectId
232
- });
233
- }
234
- const primary = pickPrimaryTopic(
235
- await findTopicsByScopeAlias(ctx, legacyProjectId)
236
- );
237
- if (primary) {
238
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
239
- fallbackProjectId: legacyProjectId
240
- });
241
- }
242
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
243
- if (nodeScope) {
244
- return {
245
- ...nodeScope,
246
- projectId: nodeScope.projectId ?? legacyProjectId
247
- };
248
- }
249
- throw new Error(
250
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
251
- );
252
- }
253
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
254
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
255
- failureLog: "[topicScope] Failed to load direct project topic",
256
- idLogKey: "projectId"
257
- });
258
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
259
- }
260
- async function tryReadTopicDoc(ctx, id, log) {
261
- try {
262
- return await ctx.db.get(id);
263
- } catch (error) {
264
- debugGraphPrimitiveFallback(log.failureLog, {
265
- error,
266
- [log.idLogKey]: id
267
- });
268
- return null;
269
- }
270
- }
271
- async function buildTopicScope(ctx, topic, source, options = {}) {
272
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
273
- const mapped = asMappedProjectId(topic);
274
- return {
275
- topicId: topic._id,
276
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
277
- tenantId: inherited.tenantId,
278
- workspaceId: inherited.workspaceId,
279
- source
280
- };
128
+ throw new Error(`Topic not found: ${topicGlobalId}`);
281
129
  }
282
130
  var optionalScopeArgs = {
283
131
  projectId: v.optional(v.string()),
@@ -7,13 +7,13 @@ import '@lucern/access-control/audience';
7
7
  import { getCurrentUserId } from '@lucern/access-control/auth';
8
8
  import { throwStructuredMutationError } from '@lucern/access-control/structuredMutationError';
9
9
  import { normalizeTupleContradictionPolicy } from '@lucern/confidence';
10
+ import { isUuidV7 } from '@lucern/contracts/ids';
10
11
  import '@lucern/contracts/schema-helpers/spine/tables/epistemicNodes';
11
12
 
12
13
  // src/epistemicBeliefs.admin.ts
13
14
  var unsafeApi = unsafeConvexAnyApi(
14
15
  "graph-primitives top-level module bundle lacks a committed Convex _generated/api surface"
15
16
  );
16
- var api = unsafeApi;
17
17
  componentsGeneric();
18
18
  var internal = unsafeApi;
19
19
  var mutation = mutationGeneric;
@@ -30,6 +30,8 @@ function debugGraphPrimitiveFallback(message, context) {
30
30
  }
31
31
  console.debug(message, context ?? {});
32
32
  }
33
+
34
+ // src/topicScope.ts
33
35
  var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
34
36
  async function resolveTopicNodeScopeOrNull(ctx, ref) {
35
37
  if (!ctx?.db || typeof ctx.db.query !== "function") {
@@ -50,16 +52,36 @@ async function resolveTopicNodeScopeOrNull(ctx, ref) {
50
52
  if (!node) {
51
53
  return null;
52
54
  }
53
- const scopeKey = normalizeScopeValue(node.topicId) ?? normalizeScopeValue(node.globalId);
55
+ const scopeKey = canonicalTopicGlobalId(node);
54
56
  if (!scopeKey) {
55
- return null;
57
+ throw new Error(
58
+ `topic.uuidv7_identity_required: topic node ${ref} is missing a UUIDv7 globalId.`
59
+ );
56
60
  }
61
+ const metadata = node.metadata ?? {};
57
62
  return {
58
63
  topicId: scopeKey,
59
64
  projectId: asMappedProjectId(node),
60
- source: "topic_node"
65
+ source: "topic_node",
66
+ tenantId: normalizeScopeValue(node.tenantId) ?? normalizeScopeValue(metadata.tenantId),
67
+ workspaceId: normalizeScopeValue(node.workspaceId) ?? normalizeScopeValue(metadata.workspaceId)
61
68
  };
62
69
  }
70
+ function canonicalTopicGlobalId(node) {
71
+ const globalId = normalizeScopeValue(node.globalId);
72
+ if (globalId && isUuidV7(globalId)) {
73
+ return globalId;
74
+ }
75
+ const topicId = normalizeScopeValue(node.topicId);
76
+ return topicId && isUuidV7(topicId) ? topicId : null;
77
+ }
78
+ function requireUuidV7TopicScope(field, value) {
79
+ if (!isUuidV7(value)) {
80
+ throw new Error(
81
+ `topic.uuidv7_required: ${field} must be a UUIDv7 public topic identifier.`
82
+ );
83
+ }
84
+ }
63
85
  function asMappedProjectId(topic) {
64
86
  if (!topic) {
65
87
  return;
@@ -81,200 +103,25 @@ function normalizeScopeValue(value) {
81
103
  const normalized = value.trim();
82
104
  return normalized.length > 0 ? normalized : void 0;
83
105
  }
84
- function pickPrimaryTopic(candidates) {
85
- return [...candidates].sort((a, b) => {
86
- const depthA = a.depth ?? 9999;
87
- const depthB = b.depth ?? 9999;
88
- if (depthA !== depthB) {
89
- return depthA - depthB;
90
- }
91
- const createdA = a.createdAt ?? Number.MAX_SAFE_INTEGER;
92
- const createdB = b.createdAt ?? Number.MAX_SAFE_INTEGER;
93
- if (createdA !== createdB) {
94
- return createdA - createdB;
95
- }
96
- return String(a.name || "").localeCompare(String(b.name || ""));
97
- })[0];
98
- }
99
- async function findTopicsByScopeAlias(ctx, scopeId) {
100
- const query2 = ctx.db.query("topics");
101
- try {
102
- return await query2.withIndex(
103
- "by_graph_scope_project",
104
- (q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
105
- ).collect();
106
- } catch (error) {
107
- debugGraphPrimitiveFallback(
108
- "[topicScope] Failed to resolve scope alias via index",
109
- {
110
- error,
111
- scopeId
112
- }
113
- );
114
- const topics = await query2.collect();
115
- return topics.filter((topic) => {
116
- const normalizedGlobalId = normalizeScopeValue(topic.globalId);
117
- const mappedProjectId = asMappedProjectId(topic);
118
- return String(topic._id) === scopeId || normalizedGlobalId === scopeId || mappedProjectId === scopeId;
119
- });
120
- }
121
- }
122
- async function tryResolveHostTopicById(ctx, topicId) {
123
- if (typeof ctx.runQuery !== "function") {
124
- return null;
125
- }
126
- try {
127
- return await ctx.runQuery(api.topics.get, {
128
- id: topicId
129
- }) ?? null;
130
- } catch (error) {
131
- debugGraphPrimitiveFallback(
132
- "[topicScope] Failed to resolve topic by host query",
133
- {
134
- error,
135
- topicId
136
- }
137
- );
138
- return null;
139
- }
140
- }
141
- async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
142
- if (typeof ctx.runQuery !== "function") {
143
- return null;
144
- }
145
- try {
146
- return await ctx.runQuery(api.topics.getByLegacyScopeId, {
147
- projectId: legacyScopeId
148
- }) ?? null;
149
- } catch (error) {
150
- debugGraphPrimitiveFallback(
151
- "[topicScope] Failed to resolve topic by legacy scope",
152
- {
153
- error,
154
- legacyScopeId
155
- }
156
- );
157
- return null;
158
- }
159
- }
160
- async function resolveInheritedWorkspaceScope(ctx, topic) {
161
- const MAX_DEPTH = 10;
162
- let tenantId = normalizeScopeValue(topic.tenantId);
163
- let workspaceId = normalizeScopeValue(topic.workspaceId);
164
- if (tenantId && workspaceId) {
165
- return { tenantId, workspaceId };
166
- }
167
- let current = topic;
168
- for (let i = 0; i < MAX_DEPTH && current?.parentTopicId; i++) {
169
- current = await ctx.db.get(current.parentTopicId);
170
- if (!current) {
171
- break;
172
- }
173
- if (!tenantId) {
174
- tenantId = normalizeScopeValue(current.tenantId);
175
- }
176
- if (!workspaceId) {
177
- workspaceId = normalizeScopeValue(current.workspaceId);
178
- }
179
- if (tenantId && workspaceId) {
180
- break;
181
- }
182
- }
183
- return { tenantId, workspaceId };
184
- }
185
106
  async function resolveTopicProjectScope(ctx, args) {
186
107
  if (args.topicId) {
187
108
  return await resolveScopeFromTopicId(ctx, args.topicId);
188
109
  }
189
110
  if (args.projectId) {
190
- return await resolveScopeFromLegacyProjectId(ctx, args.projectId);
111
+ throw new Error(
112
+ "topic.uuidv7_required: projectId scope aliases are retired; pass topicId as the UUIDv7 topic globalId."
113
+ );
191
114
  }
192
- throw new Error(
193
- "Missing scope: provide topicId (preferred) or legacy projectId alias."
194
- );
115
+ throw new Error("topic.uuidv7_required: Missing scope: provide topicId.");
195
116
  }
196
117
  async function resolveScopeFromTopicId(ctx, topicId) {
197
- const topic = await resolveTopicDocFromTopicId(ctx, topicId);
198
- if (topic) {
199
- return await buildTopicScope(ctx, topic, "topic");
200
- }
201
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, String(topicId));
118
+ const topicGlobalId = String(topicId);
119
+ requireUuidV7TopicScope("topicId", topicGlobalId);
120
+ const nodeScope = await resolveTopicNodeScopeOrNull(ctx, topicGlobalId);
202
121
  if (nodeScope) {
203
122
  return nodeScope;
204
123
  }
205
- throw new Error(`Topic not found: ${String(topicId)}`);
206
- }
207
- async function resolveTopicDocFromTopicId(ctx, topicId) {
208
- const direct = await tryReadTopicDoc(ctx, topicId, {
209
- failureLog: "[topicScope] Failed to load topic by direct id",
210
- idLogKey: "topicId"
211
- });
212
- if (direct) {
213
- return direct;
214
- }
215
- const hostTopic = await tryResolveHostTopicById(ctx, String(topicId));
216
- if (hostTopic) {
217
- return hostTopic;
218
- }
219
- return pickPrimaryTopic(await findTopicsByScopeAlias(ctx, String(topicId))) ?? null;
220
- }
221
- async function resolveScopeFromLegacyProjectId(ctx, legacyProjectId) {
222
- const directTopic = await resolveDirectLegacyProjectTopic(
223
- ctx,
224
- legacyProjectId
225
- );
226
- if (directTopic) {
227
- return await buildTopicScope(ctx, directTopic, "topic_inferred", {
228
- fallbackProjectId: legacyProjectId
229
- });
230
- }
231
- const primary = pickPrimaryTopic(
232
- await findTopicsByScopeAlias(ctx, legacyProjectId)
233
- );
234
- if (primary) {
235
- return await buildTopicScope(ctx, primary, "project_mapped_topic", {
236
- fallbackProjectId: legacyProjectId
237
- });
238
- }
239
- const nodeScope = await resolveTopicNodeScopeOrNull(ctx, legacyProjectId);
240
- if (nodeScope) {
241
- return {
242
- ...nodeScope,
243
- projectId: nodeScope.projectId ?? legacyProjectId
244
- };
245
- }
246
- throw new Error(
247
- `Legacy project scope ${legacyProjectId} has no mapped topic.`
248
- );
249
- }
250
- async function resolveDirectLegacyProjectTopic(ctx, legacyProjectId) {
251
- const directTopic = await tryReadTopicDoc(ctx, legacyProjectId, {
252
- failureLog: "[topicScope] Failed to load direct project topic",
253
- idLogKey: "projectId"
254
- });
255
- return directTopic ?? tryResolveHostTopicByLegacyScope(ctx, legacyProjectId);
256
- }
257
- async function tryReadTopicDoc(ctx, id, log) {
258
- try {
259
- return await ctx.db.get(id);
260
- } catch (error) {
261
- debugGraphPrimitiveFallback(log.failureLog, {
262
- error,
263
- [log.idLogKey]: id
264
- });
265
- return null;
266
- }
267
- }
268
- async function buildTopicScope(ctx, topic, source, options = {}) {
269
- const inherited = await resolveInheritedWorkspaceScope(ctx, topic);
270
- const mapped = asMappedProjectId(topic);
271
- return {
272
- topicId: topic._id,
273
- ...mapped || options.fallbackProjectId ? { projectId: mapped ?? options.fallbackProjectId } : {},
274
- tenantId: inherited.tenantId,
275
- workspaceId: inherited.workspaceId,
276
- source
277
- };
124
+ throw new Error(`Topic not found: ${topicGlobalId}`);
278
125
  }
279
126
  var optionalScopeArgs = {
280
127
  projectId: v.optional(v.string()),