@lucern/graph-primitives 0.3.0-alpha.0 → 0.3.0-alpha.10
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.
- package/dist/{beliefDecay-Q_26RTc-.d.ts → beliefDecay-DZ6tkLYq.d.ts} +1 -1
- package/dist/beliefDecay.d.ts +1 -1
- package/dist/beliefDecay.js +188 -1144
- package/dist/beliefDecay.js.map +1 -1
- package/dist/{beliefEvidenceLinks-42FlR48t.d.ts → beliefEvidenceLinks-CWOXxxJg.d.ts} +1 -1
- package/dist/beliefEvidenceLinks.d.ts +1 -1
- package/dist/beliefEvidenceLinks.js +186 -871
- package/dist/beliefEvidenceLinks.js.map +1 -1
- package/dist/{beliefLifecycle-C-AehZgF.d.ts → beliefLifecycle-y8WLXqQj.d.ts} +1 -1
- package/dist/beliefLifecycle.d.ts +1 -1
- package/dist/confidencePropagationDispatch.d.ts +4 -4
- package/dist/confidencePropagationDispatch.js +31 -311
- package/dist/confidencePropagationDispatch.js.map +1 -1
- package/dist/{contradictions-Hdwl7zid.d.ts → contradictions-51VLsESq.d.ts} +1 -1
- package/dist/contradictions.d.ts +1 -1
- package/dist/contradictions.js +67 -800
- package/dist/contradictions.js.map +1 -1
- package/dist/debug.d.ts +4 -0
- package/dist/debug.js +34 -0
- package/dist/debug.js.map +1 -0
- package/dist/edges/contradicts.js +1 -122
- package/dist/edges/contradicts.js.map +1 -1
- package/dist/edges/dependsOn.js +14 -172
- package/dist/edges/dependsOn.js.map +1 -1
- package/dist/edges/elaborates.js +1 -49
- package/dist/edges/elaborates.js.map +1 -1
- package/dist/edges/index.js +15 -280
- package/dist/edges/index.js.map +1 -1
- package/dist/edges/informs.js +2 -65
- package/dist/edges/informs.js.map +1 -1
- package/dist/edges/propagationTypes.d.ts +2 -2
- package/dist/edges/propagationTypes.js.map +1 -1
- package/dist/edges/refutes.js +2 -65
- package/dist/edges/refutes.js.map +1 -1
- package/dist/edges/supports.js +1 -122
- package/dist/edges/supports.js.map +1 -1
- package/dist/edges/utils.d.ts +7 -7
- package/dist/edges/utils.js +2 -133
- package/dist/edges/utils.js.map +1 -1
- package/dist/embeddingTrigger.js +21 -1
- package/dist/embeddingTrigger.js.map +1 -1
- package/dist/entityBridge.js +3 -18
- package/dist/entityBridge.js.map +1 -1
- package/dist/{entityLifecycle-BkhRJ-XI.d.ts → entityLifecycle-CvgSK5FV.d.ts} +1 -1
- package/dist/entityLifecycle.d.ts +1 -1
- package/dist/entityLifecycle.js +193 -892
- package/dist/entityLifecycle.js.map +1 -1
- package/dist/{epistemicAnswers-DSP1slZ9.d.ts → epistemicAnswers-C5ib4z6_.d.ts} +1 -1
- package/dist/epistemicAnswers.d.ts +1 -1
- package/dist/epistemicAnswers.js +73 -810
- package/dist/epistemicAnswers.js.map +1 -1
- package/dist/{epistemicBeliefs-DtFVTp-k.d.ts → epistemicBeliefs-DzKjZAeC.d.ts} +3 -3
- package/dist/epistemicBeliefs.d.ts +2 -2
- package/dist/epistemicBeliefs.js +404 -1698
- package/dist/epistemicBeliefs.js.map +1 -1
- package/dist/epistemicContractHelpers.js +1 -318
- package/dist/epistemicContractHelpers.js.map +1 -1
- package/dist/epistemicContracts.d.ts +1 -1
- package/dist/epistemicContracts.js +417 -1980
- package/dist/epistemicContracts.js.map +1 -1
- package/dist/{epistemicEdges-DcA8ErUG.d.ts → epistemicEdges-CD5vxmlH.d.ts} +3 -3
- package/dist/epistemicEdges.d.ts +1 -1
- package/dist/epistemicEdges.js +248 -919
- package/dist/epistemicEdges.js.map +1 -1
- package/dist/{epistemicEvidence-Bo638XDP.d.ts → epistemicEvidence-xw6UUrwh.d.ts} +1 -1
- package/dist/epistemicEvidence.d.ts +1 -1
- package/dist/epistemicEvidence.js +229 -1087
- package/dist/epistemicEvidence.js.map +1 -1
- package/dist/{epistemicHelpers-Bd9xbaib.d.ts → epistemicHelpers-DevrYgPN.d.ts} +1 -1
- package/dist/epistemicHelpers.d.ts +1 -1
- package/dist/{epistemicLinking-CyeLOIzN.d.ts → epistemicLinking-CfE00tHJ.d.ts} +1 -1
- package/dist/epistemicLinking.d.ts +1 -1
- package/dist/epistemicLinking.js +3 -786
- package/dist/epistemicLinking.js.map +1 -1
- package/dist/{epistemicNodes-BpD6Koud.d.ts → epistemicNodes-NBrPW7fk.d.ts} +2 -2
- package/dist/epistemicNodes.d.ts +1 -1
- package/dist/epistemicNodes.js +172 -899
- package/dist/epistemicNodes.js.map +1 -1
- package/dist/{epistemicQuestions-CmEeY6zQ.d.ts → epistemicQuestions-B_nUclrH.d.ts} +1 -1
- package/dist/epistemicQuestions.d.ts +1 -1
- package/dist/epistemicQuestions.js +369 -1125
- package/dist/epistemicQuestions.js.map +1 -1
- package/dist/{epistemicSources-ZazxHOK1.d.ts → epistemicSources-dlKj58Jp.d.ts} +1 -1
- package/dist/epistemicSources.d.ts +1 -1
- package/dist/epistemicSources.js +86 -886
- package/dist/epistemicSources.js.map +1 -1
- package/dist/evaluators/index.js +417 -1980
- package/dist/evaluators/index.js.map +1 -1
- package/dist/evaluators/lintCheckerEvaluator.js.map +1 -1
- package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -1
- package/dist/evaluators/shared.js +20 -1
- package/dist/evaluators/shared.js.map +1 -1
- package/dist/evaluators/testRunnerEvaluator.js +20 -1
- package/dist/evaluators/testRunnerEvaluator.js.map +1 -1
- package/dist/evaluators/tscCheckerEvaluator.js.map +1 -1
- package/dist/index.d.ts +20 -20
- package/dist/index.js +965 -3004
- package/dist/index.js.map +1 -1
- package/dist/{ontology-matching-Buhu23ss.d.ts → ontology-matching-C6rrz2VP.d.ts} +1 -1
- package/dist/ontology-matching.d.ts +1 -1
- package/dist/ontology-matching.js +1 -344
- package/dist/ontology-matching.js.map +1 -1
- package/dist/{ontologyApproval-Ba0Jjk1k.d.ts → ontologyApproval-CFYmqKmk.d.ts} +1 -1
- package/dist/ontologyApproval.d.ts +1 -1
- package/dist/ontologyApproval.js +1 -13
- package/dist/ontologyApproval.js.map +1 -1
- package/dist/ontologyDefinitions.js +6 -20
- package/dist/ontologyDefinitions.js.map +1 -1
- package/dist/ontologyHelpers.d.ts +1 -1
- package/dist/ontologyHelpers.js +4 -3
- package/dist/ontologyHelpers.js.map +1 -1
- package/dist/ontologyRegistry.js +2 -17
- package/dist/ontologyRegistry.js.map +1 -1
- package/dist/{projectionReconciliation-CxrXYGaB.d.ts → projectionReconciliation-jww2fBI0.d.ts} +1 -1
- package/dist/projectionReconciliation.d.ts +1 -1
- package/dist/projectionReconciliation.js +16 -37
- package/dist/projectionReconciliation.js.map +1 -1
- package/dist/{projectionStaleness-CAdpIsaW.d.ts → projectionStaleness-CmdbpjVK.d.ts} +1 -1
- package/dist/projectionStaleness.d.ts +1 -1
- package/dist/{questionEvidenceLinks-BdQD0TkM.d.ts → questionEvidenceLinks-DFlyPpAj.d.ts} +1 -1
- package/dist/questionEvidenceLinks.d.ts +1 -1
- package/dist/questionEvidenceLinks.js +199 -881
- package/dist/questionEvidenceLinks.js.map +1 -1
- package/dist/resolvers.js +86 -37
- package/dist/resolvers.js.map +1 -1
- package/dist/scopeResolverCompat.js +64 -7
- package/dist/scopeResolverCompat.js.map +1 -1
- package/dist/{text-matching-CMn2WnVD.d.ts → text-matching-DNg4M5Wd.d.ts} +1 -1
- package/dist/text-matching.d.ts +1 -1
- package/dist/text-matching.js +1 -244
- package/dist/text-matching.js.map +1 -1
- package/dist/topicProjectOverlay.js +56 -13
- package/dist/topicProjectOverlay.js.map +1 -1
- package/dist/topicScope.js +55 -6
- package/dist/topicScope.js.map +1 -1
- package/dist/workflowBridge.d.ts +27 -0
- package/dist/workflowBridge.js +352 -0
- package/dist/workflowBridge.js.map +1 -0
- package/dist/workspaceIsolation.js +56 -57
- package/dist/workspaceIsolation.js.map +1 -1
- package/package.json +6 -5
package/dist/contradictions.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { v } from 'convex/values';
|
|
2
|
-
import { componentsGeneric,
|
|
2
|
+
import { componentsGeneric, anyApi, queryGeneric, 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,31 @@ componentsGeneric();
|
|
|
7
9
|
var internal = anyApi;
|
|
8
10
|
var mutation = mutationGeneric;
|
|
9
11
|
var query = queryGeneric;
|
|
10
|
-
var api2 = anyApi;
|
|
11
|
-
componentsGeneric();
|
|
12
12
|
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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));
|
|
13
|
+
// src/debug.ts
|
|
14
|
+
function isGraphPrimitiveDebugEnabled() {
|
|
15
|
+
const env = globalThis.process?.env;
|
|
16
|
+
return env?.LUCERN_COMPAT_FALLBACK_DEBUG === "1" || env?.LUCERN_GRAPH_DEBUG === "1";
|
|
27
17
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
function readLegacyProjectId(value) {
|
|
32
|
-
if (!value) {
|
|
18
|
+
function debugGraphPrimitiveFallback(message, context) {
|
|
19
|
+
if (!isGraphPrimitiveDebugEnabled()) {
|
|
33
20
|
return;
|
|
34
21
|
}
|
|
35
|
-
|
|
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
|
-
};
|
|
22
|
+
console.debug(message, context ?? {});
|
|
603
23
|
}
|
|
604
24
|
|
|
605
|
-
//
|
|
606
|
-
|
|
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";
|
|
25
|
+
// src/topicScope.ts
|
|
26
|
+
var LEGACY_SCOPE_FIELD = "graphScopeProjectId";
|
|
792
27
|
function asMappedProjectId(topic) {
|
|
793
28
|
if (!topic) {
|
|
794
29
|
return;
|
|
795
30
|
}
|
|
796
|
-
const directLegacyProjectId = normalizeScopeValue(topic[
|
|
31
|
+
const directLegacyProjectId = normalizeScopeValue(topic[LEGACY_SCOPE_FIELD]);
|
|
797
32
|
if (directLegacyProjectId) {
|
|
798
33
|
return directLegacyProjectId;
|
|
799
34
|
}
|
|
800
35
|
const metadata = topic.metadata || {};
|
|
801
|
-
const candidate = metadata[
|
|
36
|
+
const candidate = metadata[LEGACY_SCOPE_FIELD] || metadata.legacyProjectId || metadata.projectId || metadata.scopeProjectId;
|
|
802
37
|
return candidate ? candidate : void 0;
|
|
803
38
|
}
|
|
804
39
|
function normalizeScopeValue(value) {
|
|
@@ -827,9 +62,16 @@ async function findTopicsByScopeAlias(ctx, scopeId) {
|
|
|
827
62
|
try {
|
|
828
63
|
return await ctx.db.query("topics").withIndex(
|
|
829
64
|
"by_graph_scope_project",
|
|
830
|
-
(q) => q.eq(
|
|
65
|
+
(q) => q.eq(LEGACY_SCOPE_FIELD, scopeId)
|
|
831
66
|
).collect();
|
|
832
|
-
} catch {
|
|
67
|
+
} catch (error) {
|
|
68
|
+
debugGraphPrimitiveFallback(
|
|
69
|
+
"[topicScope] Failed to resolve scope alias via index",
|
|
70
|
+
{
|
|
71
|
+
error,
|
|
72
|
+
scopeId
|
|
73
|
+
}
|
|
74
|
+
);
|
|
833
75
|
const topics = await ctx.db.query("topics").collect();
|
|
834
76
|
return topics.filter((topic) => {
|
|
835
77
|
const normalizedGlobalId = normalizeScopeValue(topic.globalId);
|
|
@@ -846,7 +88,14 @@ async function tryResolveHostTopicById(ctx, topicId) {
|
|
|
846
88
|
return await ctx.runQuery(api.topics.get, {
|
|
847
89
|
id: topicId
|
|
848
90
|
}) ?? null;
|
|
849
|
-
} catch {
|
|
91
|
+
} catch (error) {
|
|
92
|
+
debugGraphPrimitiveFallback(
|
|
93
|
+
"[topicScope] Failed to resolve topic by host query",
|
|
94
|
+
{
|
|
95
|
+
error,
|
|
96
|
+
topicId
|
|
97
|
+
}
|
|
98
|
+
);
|
|
850
99
|
return null;
|
|
851
100
|
}
|
|
852
101
|
}
|
|
@@ -858,7 +107,14 @@ async function tryResolveHostTopicByLegacyScope(ctx, legacyScopeId) {
|
|
|
858
107
|
return await ctx.runQuery(api.topics.getByLegacyScopeId, {
|
|
859
108
|
projectId: legacyScopeId
|
|
860
109
|
}) ?? null;
|
|
861
|
-
} catch {
|
|
110
|
+
} catch (error) {
|
|
111
|
+
debugGraphPrimitiveFallback(
|
|
112
|
+
"[topicScope] Failed to resolve topic by legacy scope",
|
|
113
|
+
{
|
|
114
|
+
error,
|
|
115
|
+
legacyScopeId
|
|
116
|
+
}
|
|
117
|
+
);
|
|
862
118
|
return null;
|
|
863
119
|
}
|
|
864
120
|
}
|
|
@@ -887,8 +143,17 @@ async function resolveTopicProjectScope(ctx, args) {
|
|
|
887
143
|
if (args.topicId) {
|
|
888
144
|
let topic = null;
|
|
889
145
|
try {
|
|
890
|
-
topic = await ctx.db.get(
|
|
891
|
-
|
|
146
|
+
topic = await ctx.db.get(
|
|
147
|
+
args.topicId
|
|
148
|
+
);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
debugGraphPrimitiveFallback(
|
|
151
|
+
"[topicScope] Failed to load topic by direct id",
|
|
152
|
+
{
|
|
153
|
+
error,
|
|
154
|
+
topicId: args.topicId
|
|
155
|
+
}
|
|
156
|
+
);
|
|
892
157
|
}
|
|
893
158
|
if (!topic) {
|
|
894
159
|
topic = await tryResolveHostTopicById(ctx, String(args.topicId));
|
|
@@ -925,7 +190,14 @@ async function resolveTopicProjectScope(ctx, args) {
|
|
|
925
190
|
directTopic = await ctx.db.get(
|
|
926
191
|
args.projectId
|
|
927
192
|
);
|
|
928
|
-
} catch {
|
|
193
|
+
} catch (error) {
|
|
194
|
+
debugGraphPrimitiveFallback(
|
|
195
|
+
"[topicScope] Failed to load direct project topic",
|
|
196
|
+
{
|
|
197
|
+
error,
|
|
198
|
+
projectId: args.projectId
|
|
199
|
+
}
|
|
200
|
+
);
|
|
929
201
|
}
|
|
930
202
|
if (directTopic) {
|
|
931
203
|
const inherited = await resolveInheritedWorkspaceScope(ctx, directTopic);
|
|
@@ -995,7 +267,15 @@ async function resolveScope(ctx, args) {
|
|
|
995
267
|
topicId,
|
|
996
268
|
projectId
|
|
997
269
|
});
|
|
998
|
-
} catch {
|
|
270
|
+
} catch (error) {
|
|
271
|
+
debugGraphPrimitiveFallback(
|
|
272
|
+
"[scopeResolverCompat] Failed to resolve scope",
|
|
273
|
+
{
|
|
274
|
+
error,
|
|
275
|
+
topicId,
|
|
276
|
+
projectId
|
|
277
|
+
}
|
|
278
|
+
);
|
|
999
279
|
return null;
|
|
1000
280
|
}
|
|
1001
281
|
}
|
|
@@ -1018,19 +298,6 @@ async function resolveScopeSoft(ctx, args) {
|
|
|
1018
298
|
...projectId ? { projectId } : {}
|
|
1019
299
|
};
|
|
1020
300
|
}
|
|
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
301
|
v.id("epistemicNodes");
|
|
1035
302
|
v.id("epistemicNodes");
|
|
1036
303
|
var DEFAULT_CONTRADICTION_SOURCE = "evidence_links";
|