@lucern/graph-primitives 0.3.0-alpha.0 → 0.3.0-alpha.1

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 (76) hide show
  1. package/dist/beliefDecay.js +37 -1104
  2. package/dist/beliefDecay.js.map +1 -1
  3. package/dist/beliefEvidenceLinks.js +53 -834
  4. package/dist/beliefEvidenceLinks.js.map +1 -1
  5. package/dist/confidencePropagationDispatch.d.ts +3 -3
  6. package/dist/confidencePropagationDispatch.js +30 -308
  7. package/dist/confidencePropagationDispatch.js.map +1 -1
  8. package/dist/contradictions.js +5 -797
  9. package/dist/contradictions.js.map +1 -1
  10. package/dist/edges/contradicts.js +1 -122
  11. package/dist/edges/contradicts.js.map +1 -1
  12. package/dist/edges/dependsOn.js +14 -172
  13. package/dist/edges/dependsOn.js.map +1 -1
  14. package/dist/edges/elaborates.js +1 -49
  15. package/dist/edges/elaborates.js.map +1 -1
  16. package/dist/edges/index.js +14 -277
  17. package/dist/edges/index.js.map +1 -1
  18. package/dist/edges/informs.js +1 -62
  19. package/dist/edges/informs.js.map +1 -1
  20. package/dist/edges/propagationTypes.d.ts +2 -2
  21. package/dist/edges/propagationTypes.js.map +1 -1
  22. package/dist/edges/refutes.js +1 -62
  23. package/dist/edges/refutes.js.map +1 -1
  24. package/dist/edges/supports.js +1 -122
  25. package/dist/edges/supports.js.map +1 -1
  26. package/dist/edges/utils.d.ts +6 -6
  27. package/dist/edges/utils.js +1 -130
  28. package/dist/edges/utils.js.map +1 -1
  29. package/dist/entityBridge.js +2 -17
  30. package/dist/entityBridge.js.map +1 -1
  31. package/dist/entityLifecycle.js +62 -848
  32. package/dist/entityLifecycle.js.map +1 -1
  33. package/dist/epistemicAnswers.js +6 -802
  34. package/dist/epistemicAnswers.js.map +1 -1
  35. package/dist/epistemicBeliefs.js +125 -1594
  36. package/dist/epistemicBeliefs.js.map +1 -1
  37. package/dist/epistemicContractHelpers.js +1 -318
  38. package/dist/epistemicContractHelpers.js.map +1 -1
  39. package/dist/epistemicContracts.js +129 -1874
  40. package/dist/epistemicContracts.js.map +1 -1
  41. package/dist/epistemicEdges.js +60 -863
  42. package/dist/epistemicEdges.js.map +1 -1
  43. package/dist/epistemicEvidence.js +69 -1041
  44. package/dist/epistemicEvidence.js.map +1 -1
  45. package/dist/epistemicLinking.js +2 -785
  46. package/dist/epistemicLinking.js.map +1 -1
  47. package/dist/epistemicNodes.js +9 -866
  48. package/dist/epistemicNodes.js.map +1 -1
  49. package/dist/epistemicQuestions.js +66 -1071
  50. package/dist/epistemicQuestions.js.map +1 -1
  51. package/dist/epistemicSources.js +23 -880
  52. package/dist/epistemicSources.js.map +1 -1
  53. package/dist/evaluators/index.js +129 -1874
  54. package/dist/evaluators/index.js.map +1 -1
  55. package/dist/index.js +182 -2744
  56. package/dist/index.js.map +1 -1
  57. package/dist/ontology-matching.js +1 -344
  58. package/dist/ontology-matching.js.map +1 -1
  59. package/dist/ontologyApproval.js +1 -13
  60. package/dist/ontologyApproval.js.map +1 -1
  61. package/dist/ontologyDefinitions.js +2 -17
  62. package/dist/ontologyDefinitions.js.map +1 -1
  63. package/dist/ontologyRegistry.js +2 -17
  64. package/dist/ontologyRegistry.js.map +1 -1
  65. package/dist/projectionReconciliation.js +2 -17
  66. package/dist/projectionReconciliation.js.map +1 -1
  67. package/dist/questionEvidenceLinks.js +60 -841
  68. package/dist/questionEvidenceLinks.js.map +1 -1
  69. package/dist/text-matching.js +1 -244
  70. package/dist/text-matching.js.map +1 -1
  71. package/dist/workflowBridge.d.ts +27 -0
  72. package/dist/workflowBridge.js +303 -0
  73. package/dist/workflowBridge.js.map +1 -0
  74. package/dist/workspaceIsolation.js +2 -52
  75. package/dist/workspaceIsolation.js.map +1 -1
  76. package/package.json +6 -5
@@ -1,5 +1,7 @@
1
1
  import { v } from 'convex/values';
2
2
  import { componentsGeneric, queryGeneric, anyApi, mutationGeneric } from 'convex/server';
3
+ import { checkProjectAccess, requireProjectAccess } from '@lucern/access-control/access';
4
+ import { permissiveReturn } from '@lucern/contracts/schema-helpers/validators';
3
5
 
4
6
  // src/contradictions.ts
5
7
  var api = anyApi;
@@ -7,798 +9,17 @@ componentsGeneric();
7
9
  var internal = anyApi;
8
10
  var mutation = mutationGeneric;
9
11
  var query = queryGeneric;
10
- var api2 = anyApi;
11
- componentsGeneric();
12
-
13
- // ../access-control/src/topicProjectOverlay.ts
14
12
  var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
15
- function readNonEmptyString(value) {
16
- if (typeof value !== "string") {
17
- return;
18
- }
19
- const normalized = value.trim();
20
- return normalized.length > 0 ? normalized : void 0;
21
- }
22
- function readStringArray(value) {
23
- if (!Array.isArray(value)) {
24
- return [];
25
- }
26
- return value.map((entry) => readNonEmptyString(entry)).filter((entry) => Boolean(entry));
27
- }
28
- function readMetadata(topic) {
29
- return topic.metadata && typeof topic.metadata === "object" ? topic.metadata : {};
30
- }
31
- function readLegacyProjectId(value) {
32
- if (!value) {
33
- return;
34
- }
35
- return readNonEmptyString(value[LEGACY_SCOPE_FIELD]);
36
- }
37
- function coerceVisibility(value) {
38
- return value === "private" || value === "team" || value === "firm" || value === "external" || value === "public" ? value : void 0;
39
- }
40
- function coerceStatus(value) {
41
- return value === "active" || value === "archived" || value === "watching" ? value : void 0;
42
- }
43
- function mapProjectType(topic, metadata) {
44
- const explicit = readNonEmptyString(metadata.projectType);
45
- if (explicit) {
46
- return explicit;
47
- }
48
- if (topic.type === "theme") {
49
- return "thematic";
50
- }
51
- return readNonEmptyString(topic.type) || "general";
52
- }
53
- function isProjectLikeTopic(topic) {
54
- const metadata = readMetadata(topic);
55
- return topic.type === "theme" || topic.type === "thematic" || topic.type === "deal" || topic.type === "monitoring" || readLegacyProjectId(topic) !== void 0 || readNonEmptyString(metadata.projectType) !== void 0;
56
- }
57
- async function resolveTopicDoc(ctx, scopeId) {
58
- if (ctx?.db && typeof ctx.db.get === "function") {
59
- try {
60
- const directTopic = await ctx.db.get(scopeId);
61
- if (directTopic) {
62
- return directTopic;
63
- }
64
- } catch {
65
- }
66
- }
67
- if (typeof ctx.runQuery !== "function") {
68
- return null;
69
- }
70
- try {
71
- const topic = await ctx.runQuery(api2.topics.get, {
72
- id: String(scopeId)
73
- });
74
- if (topic?.name !== void 0 && topic?.type !== void 0) {
75
- return topic;
76
- }
77
- } catch {
78
- }
79
- try {
80
- const topic = await ctx.runQuery(api2.topics.getByLegacyScopeId, {
81
- projectId: String(scopeId)
82
- });
83
- if (topic?.name !== void 0 && topic?.type !== void 0) {
84
- return topic;
85
- }
86
- } catch {
87
- }
88
- return null;
89
- }
90
- function materializeTopicProjectOverlay(topic, idMode = "legacy") {
91
- const metadata = readMetadata(topic);
92
- const topicId = String(topic._id);
93
- const legacyProjectId = readLegacyProjectId(topic) || readLegacyProjectId(metadata) || readNonEmptyString(metadata.legacyProjectId);
94
- const storageProjectId = legacyProjectId || topicId;
95
- const outwardId = idMode === "topic" ? topicId : storageProjectId;
96
- const visibility = coerceVisibility(topic.visibility) || coerceVisibility(metadata.visibility) || "private";
97
- const status = coerceStatus(topic.status) || coerceStatus(metadata.status) || "active";
98
- const createdAt = typeof topic.createdAt === "number" ? topic.createdAt : typeof topic._creationTime === "number" ? topic._creationTime : 0;
99
- const updatedAt = typeof topic.updatedAt === "number" ? topic.updatedAt : typeof metadata.updatedAt === "number" ? metadata.updatedAt : createdAt;
100
- return {
101
- ...metadata,
102
- _id: outwardId,
103
- projectId: outwardId,
104
- topicId,
105
- storageProjectId,
106
- legacyProjectId,
107
- name: readNonEmptyString(topic.name) || "Untitled Theme",
108
- type: mapProjectType(topic, metadata),
109
- description: readNonEmptyString(topic.description),
110
- ownerId: readNonEmptyString(metadata.ownerId) || readNonEmptyString(topic.createdBy) || "system",
111
- sharedWith: readStringArray(metadata.sharedWith),
112
- visibility,
113
- tenantId: readNonEmptyString(topic.tenantId) || readNonEmptyString(metadata.tenantId),
114
- workspaceId: readNonEmptyString(topic.workspaceId) || readNonEmptyString(metadata.workspaceId),
115
- status,
116
- tags: readStringArray(metadata.tags),
117
- chatCount: typeof metadata.chatCount === "number" ? metadata.chatCount : 0,
118
- artifactCount: typeof metadata.artifactCount === "number" ? metadata.artifactCount : 0,
119
- lastActivityAt: typeof metadata.lastActivityAt === "number" ? metadata.lastActivityAt : updatedAt,
120
- _creationTime: typeof topic._creationTime === "number" ? topic._creationTime : createdAt,
121
- createdAt,
122
- updatedAt
123
- };
124
- }
125
- async function resolveTopicProjectOverlay(ctx, scopeId, options = {}) {
126
- const topic = await resolveTopicDoc(ctx, scopeId);
127
- if (!topic) {
128
- return null;
129
- }
130
- if (options.projectLikeOnly !== false && !isProjectLikeTopic(topic)) {
131
- return null;
132
- }
133
- return materializeTopicProjectOverlay(topic, options.idMode);
134
- }
135
- async function listTopicProjectOverlays(ctx, options = {}) {
136
- let allTopics = [];
137
- if (ctx?.db?.query && typeof ctx.db.query === "function") {
138
- try {
139
- allTopics = await ctx.db.query("topics").collect();
140
- } catch {
141
- allTopics = [];
142
- }
143
- }
144
- if (allTopics.length === 0 && typeof ctx.runQuery === "function") {
145
- allTopics = (await ctx.runQuery(api2.topics.list, {}) ?? []) || [];
146
- }
147
- return allTopics.filter(
148
- (topic) => options.projectLikeOnly === false || isProjectLikeTopic(topic)
149
- ).map((topic) => materializeTopicProjectOverlay(topic, options.idMode));
150
- }
151
-
152
- // ../access-control/src/projectGrantsBridge.ts
153
- var PROJECT_GRANT_STATUSES = ["active", "revoked", "expired"];
154
- function normalizeString(value) {
155
- if (typeof value !== "string") {
156
- return;
157
- }
158
- const trimmed = value.trim();
159
- return trimmed.length > 0 ? trimmed : void 0;
160
- }
161
- async function resolveGrantScopeIds(ctx, args) {
162
- const topicId = normalizeString(args.topicId);
163
- const projectId = normalizeString(args.projectId);
164
- for (const scopeId of [topicId, projectId]) {
165
- if (!scopeId) {
166
- continue;
167
- }
168
- try {
169
- const overlay = await resolveTopicProjectOverlay(ctx, scopeId, {
170
- idMode: "legacy",
171
- projectLikeOnly: false
172
- });
173
- if (overlay) {
174
- return {
175
- topicId: normalizeString(overlay.topicId) ?? topicId,
176
- projectId: normalizeString(overlay.projectId) ?? projectId ?? scopeId
177
- };
178
- }
179
- } catch {
180
- }
181
- }
182
- return { topicId, projectId };
183
- }
184
- async function normalizeProjectGrantRow(ctx, row) {
185
- const scope = await resolveGrantScopeIds(ctx, {
186
- topicId: row.topicId,
187
- projectId: row.projectId
188
- });
189
- return {
190
- ...row,
191
- ...scope.topicId ? { topicId: scope.topicId } : {},
192
- ...scope.projectId ?? scope.topicId ? { projectId: scope.projectId ?? scope.topicId } : {}
193
- };
194
- }
195
- async function normalizeProjectGrantRows(ctx, rows) {
196
- return await Promise.all(rows.map((row) => normalizeProjectGrantRow(ctx, row)));
197
- }
198
- async function listProjectGrantsByPrincipal(ctx, principalId) {
199
- const rows = await Promise.all(
200
- PROJECT_GRANT_STATUSES.map(
201
- (status) => ctx.db.query("projectGrants").withIndex(
202
- "by_principal_status",
203
- (q) => q.eq("principalId", principalId).eq("status", status)
204
- ).collect()
205
- )
206
- );
207
- return await normalizeProjectGrantRows(ctx, rows.flat());
208
- }
209
- async function listProjectGrantsByGroup(ctx, groupId) {
210
- const rows = await Promise.all(
211
- PROJECT_GRANT_STATUSES.map(
212
- (status) => ctx.db.query("projectGrants").withIndex(
213
- "by_group_status",
214
- (q) => q.eq("groupId", groupId).eq("status", status)
215
- ).collect()
216
- )
217
- );
218
- return await normalizeProjectGrantRows(ctx, rows.flat());
219
- }
220
- function buildScopeMatchers(inputScopeId, resolved) {
221
- return new Set(
222
- [inputScopeId, resolved.topicId, resolved.projectId].map((value) => normalizeString(value)).filter((value) => Boolean(value))
223
- );
224
- }
225
- function matchesResolvedScope(row, scopeIds) {
226
- const rowTopicId = normalizeString(row.topicId);
227
- const rowProjectId = normalizeString(row.projectId);
228
- return rowTopicId !== void 0 && scopeIds.has(rowTopicId) || rowProjectId !== void 0 && scopeIds.has(rowProjectId);
229
- }
230
- async function bridgeListProjectGrantsByTopicAndPrincipal(ctx, topicId, principalId) {
231
- const resolved = await resolveGrantScopeIds(ctx, { topicId });
232
- const scopeIds = buildScopeMatchers(topicId, resolved);
233
- const rows = await listProjectGrantsByPrincipal(ctx, principalId);
234
- return rows.filter((row) => matchesResolvedScope(row, scopeIds));
235
- }
236
- async function bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId) {
237
- const resolved = await resolveGrantScopeIds(ctx, { topicId });
238
- const scopeIds = buildScopeMatchers(topicId, resolved);
239
- const rows = await listProjectGrantsByGroup(ctx, groupId);
240
- return rows.filter((row) => matchesResolvedScope(row, scopeIds));
241
- }
242
- async function bridgeListProjectGrantsByPrincipalStatus(ctx, principalId, status) {
243
- const rows = await listProjectGrantsByPrincipal(ctx, principalId);
244
- return rows.filter((row) => row.status === status);
245
- }
246
- async function bridgeListProjectGrantsByGroupStatus(ctx, groupId, status) {
247
- const rows = await listProjectGrantsByGroup(ctx, groupId);
248
- return rows.filter((row) => row.status === status);
249
- }
250
- async function bridgeInsertProjectGrant(ctx, value) {
251
- const resolved = await resolveGrantScopeIds(ctx, value);
252
- return await ctx.db.insert("projectGrants", {
253
- ...value,
254
- ...resolved.topicId ? { topicId: resolved.topicId } : {},
255
- ...resolved.projectId ?? resolved.topicId ? { projectId: resolved.projectId ?? resolved.topicId } : {}
256
- });
257
- }
258
-
259
- // ../access-control/src/resolvers.ts
260
- async function findUserByClerkId(ctx, clerkId) {
261
- const normalizedClerkId = clerkId.trim();
262
- if (!normalizedClerkId) {
263
- return null;
264
- }
265
- if (typeof ctx.runQuery === "function") {
266
- try {
267
- const bridgedUser = await ctx.runQuery(api2.users.getUserByClerkId, {
268
- clerkId: normalizedClerkId
269
- });
270
- if (bridgedUser) {
271
- return bridgedUser;
272
- }
273
- } catch {
274
- }
275
- }
276
- try {
277
- const users = await ctx.db.query("users").collect();
278
- return users.find((user) => String(user.clerkId ?? "") === normalizedClerkId) ?? null;
279
- } catch {
280
- return null;
281
- }
282
- }
283
- async function findUserByPrincipalId(ctx, principalId) {
284
- const normalizedPrincipalId = principalId.trim();
285
- if (!normalizedPrincipalId) {
286
- return null;
287
- }
288
- try {
289
- const users = await ctx.db.query("users").collect();
290
- return users.find(
291
- (user) => String(user.defaultPrincipalId ?? "") === normalizedPrincipalId
292
- ) ?? null;
293
- } catch {
294
- return null;
295
- }
296
- }
297
- async function findAgentByPrincipalId(ctx, principalId) {
298
- const normalizedPrincipalId = principalId.trim();
299
- if (!normalizedPrincipalId) {
300
- return null;
301
- }
302
- if (typeof ctx.runQuery === "function") {
303
- try {
304
- const bridgedAgent = await ctx.runQuery(
305
- api2.agents.getAgentByPrincipalId,
306
- {
307
- principalId: normalizedPrincipalId
308
- }
309
- );
310
- if (bridgedAgent) {
311
- return bridgedAgent;
312
- }
313
- } catch {
314
- }
315
- }
316
- try {
317
- const agents = await ctx.db.query("agents").collect();
318
- return agents.find(
319
- (agent) => String(agent.principalId ?? "") === normalizedPrincipalId
320
- ) ?? null;
321
- } catch {
322
- return null;
323
- }
324
- }
325
- function defaultResolvers() {
326
- return {
327
- async getProject(ctx, topicId) {
328
- return await resolveTopicProjectOverlay(ctx, topicId, {
329
- idMode: "legacy",
330
- projectLikeOnly: false
331
- });
332
- },
333
- async listTopics(ctx) {
334
- return await listTopicProjectOverlays(ctx, { idMode: "legacy" });
335
- },
336
- async listTopicsByOwner(ctx, ownerId) {
337
- const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
338
- return topics.filter((topic) => topic.ownerId === ownerId);
339
- },
340
- async listTopicsByVisibility(ctx, visibility) {
341
- const topics = await listTopicProjectOverlays(ctx, { idMode: "legacy" });
342
- return topics.filter((topic) => topic.visibility === visibility);
343
- },
344
- async listProjectGrantsByProjectAndPrincipal(ctx, topicId, principalId) {
345
- return await bridgeListProjectGrantsByTopicAndPrincipal(
346
- ctx,
347
- topicId,
348
- principalId
349
- );
350
- },
351
- async listProjectGrantsByProjectAndGroup(ctx, topicId, groupId) {
352
- return await bridgeListProjectGrantsByTopicAndGroup(ctx, topicId, groupId);
353
- },
354
- async listProjectGrantsByPrincipalStatus(ctx, principalId, status) {
355
- return await bridgeListProjectGrantsByPrincipalStatus(
356
- ctx,
357
- principalId,
358
- status
359
- );
360
- },
361
- async listProjectGrantsByGroupStatus(ctx, groupId, status) {
362
- return await bridgeListProjectGrantsByGroupStatus(ctx, groupId, status);
363
- },
364
- async insertProjectGrant(ctx, value) {
365
- return await bridgeInsertProjectGrant(ctx, value);
366
- },
367
- async getAgentByPrincipalId(ctx, principalId) {
368
- return await findAgentByPrincipalId(ctx, principalId);
369
- },
370
- async getUserByClerkId(ctx, clerkId) {
371
- return await findUserByClerkId(ctx, clerkId);
372
- },
373
- async getUserByPrincipalId(ctx, principalId) {
374
- return await findUserByPrincipalId(ctx, principalId);
375
- }
376
- };
377
- }
378
- var resolverOverrides = {};
379
- function resolveAccessControlAppResolvers(_ctx) {
380
- return {
381
- ...defaultResolvers(),
382
- ...resolverOverrides
383
- };
384
- }
385
-
386
- // ../access-control/src/principalContext.ts
387
- function requireCanonicalResolvedUser(user, clerkId) {
388
- const resolved = user;
389
- if (!resolved) {
390
- throw new Error(
391
- `[AccessControl] Canonical user identity required for ${clerkId}. Sync users.upsertUser before user-bound access checks.`
392
- );
393
- }
394
- const { mcRole, defaultTenantId, defaultWorkspaceId, defaultPrincipalId } = resolved;
395
- if (mcRole !== "platform_admin" && mcRole !== "tenant_admin" && mcRole !== "workspace_admin" && mcRole !== "editor" && mcRole !== "viewer" && mcRole !== "auditor" && mcRole !== "service_agent") {
396
- throw new Error(
397
- `[AccessControl] Canonical MC role required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
398
- );
399
- }
400
- if (typeof defaultTenantId !== "string" || defaultTenantId.trim().length === 0) {
401
- throw new Error(
402
- `[AccessControl] Canonical home tenant required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
403
- );
404
- }
405
- if (typeof defaultWorkspaceId !== "string" || defaultWorkspaceId.trim().length === 0) {
406
- throw new Error(
407
- `[AccessControl] Canonical home workspace required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
408
- );
409
- }
410
- if (typeof defaultPrincipalId !== "string" || defaultPrincipalId.trim().length === 0) {
411
- throw new Error(
412
- `[AccessControl] Canonical federated principal required for ${clerkId}. Re-sync Master Control identity before user-bound access checks.`
413
- );
414
- }
415
- return {
416
- mcRole,
417
- defaultTenantId: defaultTenantId.trim(),
418
- defaultWorkspaceId: defaultWorkspaceId.trim(),
419
- defaultPrincipalId: defaultPrincipalId.trim()
420
- };
421
- }
422
- function isPrincipalIdInput(value) {
423
- return value.startsWith("user:") || value.startsWith("group:") || value.startsWith("service:") || value.startsWith("agent:") || value.startsWith("external_viewer:");
424
- }
425
- async function resolveCanonicalUserRecord(ctx, actorId) {
426
- const normalizedActorId = actorId.trim();
427
- const clerkId = isPrincipalIdInput(normalizedActorId) && normalizedActorId.startsWith("user:") ? normalizedActorId.slice("user:".length) : normalizedActorId;
428
- const resolvers = resolveAccessControlAppResolvers();
429
- const resolvedByClerkId = await resolvers.getUserByClerkId(ctx, clerkId);
430
- if (resolvedByClerkId) {
431
- return {
432
- resolvedUser: resolvedByClerkId,
433
- clerkId,
434
- contextClerkId: clerkId
435
- };
436
- }
437
- const resolvedByPrincipalId = await resolvers.getUserByPrincipalId(
438
- ctx,
439
- normalizedActorId
440
- );
441
- return {
442
- resolvedUser: resolvedByPrincipalId ?? null,
443
- clerkId,
444
- contextClerkId: normalizedActorId.startsWith("user:") && clerkId.length > 0 ? clerkId : normalizedActorId
445
- };
446
- }
447
- function uniqRoles(roles) {
448
- const roleSet = /* @__PURE__ */ new Set();
449
- for (const role of roles) {
450
- if (role === "platform_admin" || role === "tenant_admin" || role === "workspace_admin" || role === "editor" || role === "viewer" || role === "auditor" || role === "service_agent") {
451
- roleSet.add(role);
452
- }
453
- }
454
- return [...roleSet];
455
- }
456
- function normalizeGroupIds(value) {
457
- if (!Array.isArray(value)) {
458
- return [];
459
- }
460
- return [...new Set(
461
- value.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter(Boolean)
462
- )];
463
- }
464
- function requireServiceAgentUser(user, actorId) {
465
- const canonicalUser = requireCanonicalResolvedUser(user, actorId);
466
- if (canonicalUser.mcRole !== "service_agent") {
467
- throw new Error(
468
- `[AccessControl] Canonical service_agent identity required for ${actorId}. Sync users.upsertUser before agent-bound access checks.`
469
- );
470
- }
471
- return canonicalUser;
472
- }
473
- function requireCanonicalResolvedAgent(agent, actorId) {
474
- const resolved = agent;
475
- if (!resolved) {
476
- throw new Error(
477
- `[AccessControl] Agent "${actorId}" not found in agents or users table.`
478
- );
479
- }
480
- if (typeof resolved.principalId !== "string" || resolved.principalId.trim().length === 0) {
481
- throw new Error(
482
- `[AccessControl] Canonical agent principalId required for ${actorId}.`
483
- );
484
- }
485
- if (typeof resolved.tenantId !== "string" || resolved.tenantId.trim().length === 0) {
486
- throw new Error(
487
- `[AccessControl] Canonical home tenant required for ${actorId}.`
488
- );
489
- }
490
- if (typeof resolved.workspaceId !== "string" || resolved.workspaceId.trim().length === 0) {
491
- throw new Error(
492
- `[AccessControl] Canonical home workspace required for ${actorId}.`
493
- );
494
- }
495
- return {
496
- principalId: resolved.principalId.trim(),
497
- tenantId: resolved.tenantId.trim(),
498
- workspaceId: resolved.workspaceId.trim(),
499
- roles: uniqRoles(Array.isArray(resolved.roles) ? resolved.roles : []) ?? ["service_agent"],
500
- groupIds: normalizeGroupIds(resolved.groupIds)
501
- };
502
- }
503
- async function resolvePrincipalContext(ctx, actorId) {
504
- if (actorId.startsWith("agent:")) {
505
- const resolvers = resolveAccessControlAppResolvers();
506
- const resolvedAgent = await resolvers.getAgentByPrincipalId(ctx, actorId);
507
- if (resolvedAgent) {
508
- const agent = requireCanonicalResolvedAgent(
509
- resolvedAgent,
510
- actorId
511
- );
512
- return {
513
- principalId: agent.principalId,
514
- principalType: "service",
515
- clerkId: actorId,
516
- tenantId: agent.tenantId,
517
- workspaceId: agent.workspaceId,
518
- roles: agent.roles.length > 0 ? agent.roles : ["service_agent"],
519
- groupIds: agent.groupIds,
520
- isPlatformAdmin: false,
521
- isTenantAdmin: false,
522
- isWorkspaceAdmin: false,
523
- isSystemFallback: false
524
- };
525
- }
526
- const resolvedUser2 = await resolvers.getUserByClerkId(
527
- ctx,
528
- actorId
529
- );
530
- if (!resolvedUser2) {
531
- throw new Error(
532
- `[AccessControl] Agent "${actorId}" not found in agents or users table.`
533
- );
534
- }
535
- const user2 = requireServiceAgentUser(
536
- resolvedUser2,
537
- actorId
538
- );
539
- console.warn(
540
- `[AccessControl] Deprecated legacy service-agent fallback for ${actorId}; migrate this principal into identity.agents.`
541
- );
542
- return {
543
- principalId: user2.defaultPrincipalId,
544
- principalType: "service",
545
- clerkId: actorId,
546
- tenantId: user2.defaultTenantId,
547
- workspaceId: user2.defaultWorkspaceId,
548
- roles: ["service_agent"],
549
- groupIds: normalizeGroupIds(resolvedUser2?.principalGroupIds),
550
- isPlatformAdmin: false,
551
- isTenantAdmin: false,
552
- isWorkspaceAdmin: false,
553
- isSystemFallback: false
554
- };
555
- }
556
- const {
557
- resolvedUser,
558
- contextClerkId
559
- } = await resolveCanonicalUserRecord(ctx, actorId);
560
- const user = requireCanonicalResolvedUser(
561
- resolvedUser,
562
- contextClerkId
563
- );
564
- if (!user.defaultPrincipalId) {
565
- throw new Error(
566
- `[AccessControl] Canonical federated principal required for ${contextClerkId}. Re-sync Master Control identity before user-bound access checks.`
567
- );
568
- }
569
- if (user.mcRole === "service_agent") {
570
- return {
571
- principalId: user.defaultPrincipalId,
572
- principalType: "service",
573
- clerkId: contextClerkId,
574
- tenantId: user.defaultTenantId,
575
- workspaceId: user.defaultWorkspaceId,
576
- roles: ["service_agent"],
577
- groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
578
- isPlatformAdmin: false,
579
- isTenantAdmin: false,
580
- isWorkspaceAdmin: false,
581
- isSystemFallback: false
582
- };
583
- }
584
- const principalId = user.defaultPrincipalId;
585
- const effectiveRole = user.mcRole;
586
- const roles = effectiveRole === "platform_admin" ? ["platform_admin", "tenant_admin"] : effectiveRole === "tenant_admin" ? ["tenant_admin"] : [effectiveRole];
587
- const tenantId = user.defaultTenantId;
588
- const workspaceId = user.defaultWorkspaceId;
589
- const isPlatformAdmin = effectiveRole === "platform_admin";
590
- return {
591
- principalId,
592
- principalType: "user",
593
- clerkId: contextClerkId,
594
- tenantId,
595
- workspaceId,
596
- roles: uniqRoles(roles),
597
- groupIds: normalizeGroupIds(resolvedUser?.principalGroupIds),
598
- isPlatformAdmin,
599
- isTenantAdmin: isPlatformAdmin || effectiveRole === "tenant_admin",
600
- isWorkspaceAdmin: isPlatformAdmin || effectiveRole === "tenant_admin" || effectiveRole === "workspace_admin",
601
- isSystemFallback: false
602
- };
603
- }
604
-
605
- // ../access-control/src/access.ts
606
- function isTopicInPrincipalTenant(topic, principalTenantId) {
607
- if (!topic.tenantId) {
608
- return false;
609
- }
610
- if (!principalTenantId) {
611
- return false;
612
- }
613
- return String(topic.tenantId) === String(principalTenantId);
614
- }
615
- function isTopicInPrincipalWorkspace(topic, principalWorkspaceId) {
616
- if (!topic.workspaceId) {
617
- return false;
618
- }
619
- if (!principalWorkspaceId) {
620
- return false;
621
- }
622
- return String(topic.workspaceId) === String(principalWorkspaceId);
623
- }
624
- function isLegacyUnscopedTopic(topic) {
625
- return !topic.tenantId || !topic.workspaceId;
626
- }
627
- function isGrantScopeAlignedToTopic(topic, grant) {
628
- if (topic.tenantId && grant.tenantId && String(topic.tenantId) !== String(grant.tenantId)) {
629
- return false;
630
- }
631
- if (topic.workspaceId && grant.workspaceId && String(topic.workspaceId) !== String(grant.workspaceId)) {
632
- return false;
633
- }
634
- return true;
635
- }
636
- function isGrantSourceAllowedForVisibility(visibility, source) {
637
- if (source !== "external_share") {
638
- return true;
639
- }
640
- return visibility === "external" || visibility === "public";
641
- }
642
- function isGrantActive(grant) {
643
- if (grant.status !== "active") {
644
- return false;
645
- }
646
- if (grant.expiresAt !== void 0 && grant.expiresAt <= Date.now()) {
647
- return false;
648
- }
649
- return true;
650
- }
651
- async function hasPrincipalGrant(ctx, args) {
652
- const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndPrincipal(
653
- ctx,
654
- args.topic._id,
655
- args.principalId
656
- );
657
- if (grants.some(
658
- (grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
659
- args.topic.visibility,
660
- grant.source
661
- ) && (!args.principalIsExternal || args.topic.visibility === "public" || grant.source === "external_share")
662
- )) {
663
- return true;
664
- }
665
- return false;
666
- }
667
- async function hasGroupGrant(ctx, args) {
668
- if (args.groupIds.length === 0) {
669
- return false;
670
- }
671
- for (const groupId of args.groupIds) {
672
- const grants = await resolveAccessControlAppResolvers().listProjectGrantsByProjectAndGroup(ctx, args.topic._id, groupId);
673
- if (grants.some(
674
- (grant) => isGrantActive(grant) && isGrantScopeAlignedToTopic(args.topic, grant) && isGrantSourceAllowedForVisibility(
675
- args.topic.visibility,
676
- grant.source
677
- )
678
- )) {
679
- return true;
680
- }
681
- }
682
- return false;
683
- }
684
- function isExternalPrincipal(_ctx, _args) {
685
- return false;
686
- }
687
- async function evaluateTopicAccessDetailed(ctx, args) {
688
- if (args.legacyUserId) {
689
- return {
690
- hasAccess: true,
691
- isAdmin: false,
692
- isOwner: false,
693
- isShared: false,
694
- hasGrant: true,
695
- isFirmVisible: true,
696
- isExternalVisible: false,
697
- isPublicVisible: false,
698
- isTenantScopeMatch: true,
699
- isWorkspaceScopeMatch: true,
700
- isPrincipalExternal: false
701
- };
702
- }
703
- const topic = await resolveAccessControlAppResolvers().getProject(
704
- ctx,
705
- args.topicId
706
- );
707
- if (!topic) {
708
- return {
709
- hasAccess: false,
710
- isAdmin: false,
711
- isOwner: false,
712
- isShared: false,
713
- hasGrant: false,
714
- isFirmVisible: false,
715
- isExternalVisible: false,
716
- isPublicVisible: false,
717
- isTenantScopeMatch: false,
718
- isWorkspaceScopeMatch: false,
719
- isPrincipalExternal: false
720
- };
721
- }
722
- const { principalContext, legacyUserId } = args;
723
- const userIsAdmin = principalContext.isPlatformAdmin;
724
- const isOwner = topic.ownerId === legacyUserId;
725
- const isShared = (topic.sharedWith ?? []).includes(legacyUserId);
726
- const principalIsExternal = await isExternalPrincipal(ctx, {
727
- groupIds: principalContext.groupIds,
728
- topicTenantId: topic.tenantId,
729
- topicWorkspaceId: topic.workspaceId
730
- });
731
- const hasPrincipalGrantResult = await hasPrincipalGrant(ctx, {
732
- topic,
733
- principalId: principalContext.principalId,
734
- principalIsExternal
735
- });
736
- const hasGroupGrantResult = await hasGroupGrant(ctx, {
737
- topic,
738
- groupIds: principalContext.groupIds
739
- });
740
- const hasGrant = isShared || hasPrincipalGrantResult || hasGroupGrantResult;
741
- const legacyUnscoped = isLegacyUnscopedTopic(topic);
742
- const tenantScopeMatch = isTopicInPrincipalTenant(
743
- topic,
744
- principalContext.tenantId
745
- );
746
- const workspaceScopeMatch = isTopicInPrincipalWorkspace(
747
- topic,
748
- principalContext.workspaceId
749
- );
750
- const isPublicVisible = topic.visibility === "public";
751
- const isFirmVisible = topic.visibility === "firm" && !legacyUnscoped && tenantScopeMatch && workspaceScopeMatch && !principalIsExternal;
752
- const hasScopedGrant = hasGrant && (legacyUnscoped || tenantScopeMatch && workspaceScopeMatch);
753
- const isExternalVisible = topic.visibility === "external" && hasScopedGrant;
754
- const hasAccess = userIsAdmin || isOwner || hasScopedGrant || isPublicVisible || isFirmVisible;
755
- return {
756
- hasAccess,
757
- isAdmin: userIsAdmin,
758
- isOwner,
759
- isShared,
760
- hasGrant,
761
- isFirmVisible,
762
- isExternalVisible,
763
- isPublicVisible,
764
- isTenantScopeMatch: tenantScopeMatch,
765
- isWorkspaceScopeMatch: workspaceScopeMatch,
766
- isPrincipalExternal: principalIsExternal
767
- };
768
- }
769
- async function checkTopicAccessDetailed(ctx, topicId, userId) {
770
- const principalContext = await resolvePrincipalContext(ctx, userId);
771
- return evaluateTopicAccessDetailed(ctx, {
772
- topicId,
773
- legacyUserId: userId,
774
- principalContext
775
- });
776
- }
777
- async function checkTopicAccess(ctx, topicId, userId) {
778
- const result = await checkTopicAccessDetailed(ctx, topicId, userId);
779
- return result.hasAccess;
780
- }
781
- async function requireTopicAccess(ctx, topicId, userId) {
782
- const hasAccess = await checkTopicAccess(ctx, topicId, userId);
783
- if (!hasAccess) {
784
- throw new Error(
785
- "Access denied: You don't have permission to access this topic"
786
- );
787
- }
788
- }
789
- var checkProjectAccess = checkTopicAccess;
790
- var requireProjectAccess = requireTopicAccess;
791
- var LEGACY_SCOPE_FIELD2 = "graphScopeProjectId";
792
13
  function asMappedProjectId(topic) {
793
14
  if (!topic) {
794
15
  return;
795
16
  }
796
- const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD2]);
17
+ const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD]);
797
18
  if (directLegacyProjectId) {
798
19
  return directLegacyProjectId;
799
20
  }
800
21
  const metadata = topic.metadata || {};
801
- const candidate = metadata[LEGACY_SCOPE_FIELD2] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
22
+ const candidate = metadata[LEGACY_SCOPE_FIELD] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
802
23
  return candidate ? candidate : void 0;
803
24
  }
804
25
  function normalizeScopeValue(value) {
@@ -827,7 +48,7 @@ async function findTopicsByScopeAlias(ctx, scopeId) {
827
48
  try {
828
49
  return await ctx.db.query("topics").withIndex(
829
50
  "by_graph_scope_project",
830
- (q) => q.eq(LEGACY_SCOPE_FIELD2, scopeId)
51
+ (q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
831
52
  ).collect();
832
53
  } catch {
833
54
  const topics = await ctx.db.query("topics").collect();
@@ -1018,19 +239,6 @@ async function resolveScopeSoft(ctx, args) {
1018
239
  ...projectId ? { projectId } : {}
1019
240
  };
1020
241
  }
1021
- var permissiveReturn = v.optional(v.any());
1022
- var looseJsonObject = v.record(v.string(), v.any());
1023
- var looseJsonArray = v.array(v.any());
1024
- v.union(
1025
- v.string(),
1026
- v.number(),
1027
- v.boolean(),
1028
- v.null(),
1029
- looseJsonObject,
1030
- looseJsonArray
1031
- );
1032
-
1033
- // src/contradictions.ts
1034
242
  v.id("epistemicNodes");
1035
243
  v.id("epistemicNodes");
1036
244
  var DEFAULT_CONTRADICTION_SOURCE = "evidence_links";