@unson/brainbase-mcp 0.50.31 → 0.51.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.
- package/contracts/canonical-task/store-schema.sql +3 -0
- package/dist/antigravity-judgment-hook.d.ts +5 -0
- package/dist/antigravity-judgment-hook.js +344 -27
- package/dist/automation-run.js +3 -2
- package/dist/canonical-task-postgres.js +3 -0
- package/dist/canonical-task-service.d.ts +6 -0
- package/dist/canonical-task-service.js +40 -1
- package/dist/company-os-learning-adoption.d.ts +9 -0
- package/dist/company-os-learning-adoption.js +50 -1
- package/dist/durable-waits.d.ts +30 -0
- package/dist/durable-waits.js +100 -94
- package/dist/foundation-public-provider.d.ts +3 -1
- package/dist/foundation-public-provider.js +2 -2
- package/dist/graph-corrections.d.ts +46 -1
- package/dist/graph-corrections.js +172 -1
- package/dist/judgment-event-outbox.d.ts +24 -2
- package/dist/judgment-event-outbox.js +92 -10
- package/dist/judgment-frame.js +2 -2
- package/dist/judgment-host.d.ts +4 -1
- package/dist/judgment-host.js +126 -14
- package/dist/judgment-task-progress.d.ts +41 -0
- package/dist/judgment-task-progress.js +18 -0
- package/dist/knowledge-continuation.js +7 -1
- package/dist/knowledge-lookup.js +43 -58
- package/dist/mcp-runtime.d.ts +9 -0
- package/dist/mcp-runtime.js +12 -4
- package/dist/meeting-automation-runtime.d.ts +52 -0
- package/dist/meeting-automation-runtime.js +46 -0
- package/dist/queue-task-tools.js +2 -2
- package/dist/routine-liveness.js +0 -1
- package/dist/routine-runtime.js +1 -1
- package/dist/server.d.ts +59 -85
- package/dist/server.js +44 -36
- package/dist/ssot.js +1 -1
- package/dist/testing/queue-task-tools-contract.js +1 -1
- package/dist/world-model-impact-index.d.ts +132 -0
- package/dist/world-model-impact-index.js +901 -0
- package/package.json +6 -18
- package/ui/judgment-history.css +13 -0
- package/ui/objective-editor.css +19 -2
- package/ui/objective-editor.js +6 -3
- package/ui/project-workspace.css +33 -26
- package/ui/workspace-kit.js +9 -7
- package/ui/world/world-view.css +65 -46
- package/ui/world/world-view.js +27 -18
- package/dist/execution-authority-http.d.ts +0 -30
- package/dist/execution-authority-http.js +0 -454
- package/dist/execution-authority.d.ts +0 -282
- package/dist/execution-authority.js +0 -867
- package/dist/resource-reservation-http.d.ts +0 -31
- package/dist/resource-reservation-http.js +0 -371
- package/dist/resource-reservations.d.ts +0 -221
- package/dist/resource-reservations.js +0 -577
|
@@ -0,0 +1,901 @@
|
|
|
1
|
+
import { findLatestFoundationRecord, foundationKey } from './foundation-catalog.js';
|
|
2
|
+
/**
|
|
3
|
+
* Deterministic reverse-dependency indexer for Brainbase world models.
|
|
4
|
+
*
|
|
5
|
+
* Traverses `FoundationCatalog` definitions and relations (`uses_as_input`,
|
|
6
|
+
* `predicts`, `evaluated_by`, `used_as_basis`, `applies_to`, `contributes_to`,
|
|
7
|
+
* `execution_depends_on`, `time_condition`), `JudgmentProblemSnapshot` bundles,
|
|
8
|
+
* and active `ImpactReviewPlan` entries to enumerate models, objectives,
|
|
9
|
+
* snapshots, and plans that need review when a variable, model, objective,
|
|
10
|
+
* constraint, or method changes or is refuted.
|
|
11
|
+
*
|
|
12
|
+
* World models are allowed to contain feedback cycles; traversal tracks active
|
|
13
|
+
* path ancestry and visited nodes so cyclic graphs always terminate cleanly
|
|
14
|
+
* with deterministic shortest dependency paths.
|
|
15
|
+
*/
|
|
16
|
+
export const WORLD_MODEL_IMPACT_INDEX_VERSION = 'world-model-impact-index.v1';
|
|
17
|
+
export const REDACTED_WORLD_MODEL_REF_LABEL = '<redacted>';
|
|
18
|
+
export const REDACTED_WORLD_MODEL_RELATION = 'redacted';
|
|
19
|
+
const DEFAULT_MAX_DEPTH = 64;
|
|
20
|
+
const FOUNDATION_TYPE_SET = new Set(['objective', 'variable', 'model', 'constraint']);
|
|
21
|
+
const CANONICAL_REVISION_PATTERN = /^[1-9]\d*$/u;
|
|
22
|
+
const TYPED_APPLIES_TO_PATTERN = /^(objective|variable|model|constraint|judgment_problem|plan)\/([^@\s]+)(?:@([^\s]+))?$/u;
|
|
23
|
+
const SNAPSHOT_INDEXABLE_KINDS = new Set([
|
|
24
|
+
'objective',
|
|
25
|
+
'variable',
|
|
26
|
+
'model',
|
|
27
|
+
'constraint',
|
|
28
|
+
'dag',
|
|
29
|
+
]);
|
|
30
|
+
/**
|
|
31
|
+
* Checks whether a principal can read a Foundation resource according to its ACL.
|
|
32
|
+
* Fails closed when `principal` is omitted for non-public resources.
|
|
33
|
+
*/
|
|
34
|
+
export function canReadFoundationAcl(acl, principal) {
|
|
35
|
+
if (acl.visibility === 'public')
|
|
36
|
+
return true;
|
|
37
|
+
if (!principal || principal.trim() === '')
|
|
38
|
+
return false;
|
|
39
|
+
return acl.ownerId === principal
|
|
40
|
+
|| acl.readerIds.includes(principal)
|
|
41
|
+
|| acl.writerIds.includes(principal);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Redacts unauthorized intermediate nodes and their incident relation labels
|
|
45
|
+
* from a dependency path, collapsing consecutive redacted nodes into a single
|
|
46
|
+
* `<redacted>` hop so neither private IDs/kinds/revisions nor hidden hop counts
|
|
47
|
+
* are exposed.
|
|
48
|
+
*/
|
|
49
|
+
export function redactWorldModelDependencyPath(path, isReadable) {
|
|
50
|
+
if (path.length === 0)
|
|
51
|
+
return [];
|
|
52
|
+
const redactedRef = { kind: 'redacted', id: REDACTED_WORLD_MODEL_REF_LABEL };
|
|
53
|
+
const result = [];
|
|
54
|
+
let prevRef = isReadable(path[0].from) ? path[0].from : redactedRef;
|
|
55
|
+
let prevRedacted = prevRef.kind === 'redacted';
|
|
56
|
+
for (const step of path) {
|
|
57
|
+
const toReadable = isReadable(step.to);
|
|
58
|
+
const nextRef = toReadable ? step.to : redactedRef;
|
|
59
|
+
const nextRedacted = !toReadable;
|
|
60
|
+
if (prevRedacted && nextRedacted) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const stepRedacted = prevRedacted || nextRedacted;
|
|
64
|
+
result.push({
|
|
65
|
+
from: prevRef,
|
|
66
|
+
to: nextRef,
|
|
67
|
+
relation: stepRedacted ? REDACTED_WORLD_MODEL_RELATION : step.relation,
|
|
68
|
+
domain: stepRedacted ? 'world_model' : step.domain,
|
|
69
|
+
});
|
|
70
|
+
prevRef = nextRef;
|
|
71
|
+
prevRedacted = nextRedacted;
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Formats a dependency path into a deterministic, human-readable trail suitable
|
|
77
|
+
* for review reasons and `ImpactReviewPlanMatch.evidence`.
|
|
78
|
+
*/
|
|
79
|
+
export function formatWorldModelDependencyPath(path, options = {}) {
|
|
80
|
+
const effectivePath = options.isReadable
|
|
81
|
+
? redactWorldModelDependencyPath(path, options.isReadable)
|
|
82
|
+
: path;
|
|
83
|
+
if (effectivePath.length === 0)
|
|
84
|
+
return '';
|
|
85
|
+
const parts = [formatRef(effectivePath[0].from)];
|
|
86
|
+
for (const step of effectivePath) {
|
|
87
|
+
parts.push(`--[${step.relation}]-->`, formatRef(step.to));
|
|
88
|
+
}
|
|
89
|
+
return parts.join(' ');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Pure deterministic reverse-impact traversal over foundation definitions,
|
|
93
|
+
* explicit foundation relations, judgment problem snapshots, and plans.
|
|
94
|
+
*/
|
|
95
|
+
export function analyzeWorldModelImpact(input) {
|
|
96
|
+
const maxDepth = normalizeMaxDepth(input.maxDepth);
|
|
97
|
+
const changedRefs = normalizeChangedRefs(input.changedRefs);
|
|
98
|
+
if (changedRefs.length === 0) {
|
|
99
|
+
return {
|
|
100
|
+
version: WORLD_MODEL_IMPACT_INDEX_VERSION,
|
|
101
|
+
changedRefs: [],
|
|
102
|
+
affectedItems: [],
|
|
103
|
+
affectedModels: [],
|
|
104
|
+
affectedObjectives: [],
|
|
105
|
+
affectedSnapshots: [],
|
|
106
|
+
affectedPlans: [],
|
|
107
|
+
cyclesDetected: 0,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const { adjacency, knownRevisions } = buildImpactGraph(input);
|
|
111
|
+
const affectedItems = [];
|
|
112
|
+
let cyclesDetected = 0;
|
|
113
|
+
for (const seed of changedRefs) {
|
|
114
|
+
const seedKey = graphNodeKey(seed);
|
|
115
|
+
const seedRef = enrichRef(seed, knownRevisions);
|
|
116
|
+
const visited = new Set([seedKey]);
|
|
117
|
+
const queue = [{ key: seedKey, path: [], pathKeys: new Set([seedKey]) }];
|
|
118
|
+
let head = 0;
|
|
119
|
+
while (head < queue.length) {
|
|
120
|
+
const current = queue[head];
|
|
121
|
+
head += 1;
|
|
122
|
+
if (current.path.length >= maxDepth)
|
|
123
|
+
continue;
|
|
124
|
+
const outgoing = adjacency.get(current.key);
|
|
125
|
+
if (!outgoing || outgoing.length === 0)
|
|
126
|
+
continue;
|
|
127
|
+
for (const edge of outgoing) {
|
|
128
|
+
const nextKey = graphNodeKey(edge.to);
|
|
129
|
+
if (current.pathKeys.has(nextKey)) {
|
|
130
|
+
cyclesDetected += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (visited.has(nextKey)) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
visited.add(nextKey);
|
|
137
|
+
const step = {
|
|
138
|
+
from: current.path.length === 0 ? seedRef : enrichRef(edge.from, knownRevisions),
|
|
139
|
+
to: enrichRef(edge.to, knownRevisions),
|
|
140
|
+
relation: edge.relation,
|
|
141
|
+
domain: edge.domain,
|
|
142
|
+
};
|
|
143
|
+
const nextPath = [...current.path, step];
|
|
144
|
+
const affectedRef = step.to;
|
|
145
|
+
affectedItems.push({
|
|
146
|
+
status: 'needs_review',
|
|
147
|
+
affectedRef,
|
|
148
|
+
sourceRef: seedRef,
|
|
149
|
+
dependencyPath: nextPath,
|
|
150
|
+
depth: nextPath.length,
|
|
151
|
+
reason: `needs_review: ${formatWorldModelDependencyPath(nextPath)}`,
|
|
152
|
+
});
|
|
153
|
+
const nextPathKeys = new Set(current.pathKeys);
|
|
154
|
+
nextPathKeys.add(nextKey);
|
|
155
|
+
queue.push({ key: nextKey, path: nextPath, pathKeys: nextPathKeys });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
affectedItems.sort(compareAffectedItems);
|
|
160
|
+
return {
|
|
161
|
+
version: WORLD_MODEL_IMPACT_INDEX_VERSION,
|
|
162
|
+
changedRefs,
|
|
163
|
+
affectedItems,
|
|
164
|
+
affectedModels: affectedItems.filter((item) => item.affectedRef.kind === 'model'),
|
|
165
|
+
affectedObjectives: affectedItems.filter((item) => item.affectedRef.kind === 'objective'),
|
|
166
|
+
affectedSnapshots: affectedItems.filter((item) => item.affectedRef.kind === 'judgment_problem'),
|
|
167
|
+
affectedPlans: affectedItems.filter((item) => item.affectedRef.kind === 'plan'),
|
|
168
|
+
cyclesDetected,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Concrete OSS implementation of `ImpactReviewIndexPort` backed by the
|
|
173
|
+
* deterministic world-model reverse-dependency graph.
|
|
174
|
+
*/
|
|
175
|
+
export class WorldModelImpactIndex {
|
|
176
|
+
loadSource;
|
|
177
|
+
canReadPlan;
|
|
178
|
+
maxDepth;
|
|
179
|
+
constructor(options) {
|
|
180
|
+
if (!options || !options.source) {
|
|
181
|
+
throw new TypeError('WorldModelImpactIndex requires a source');
|
|
182
|
+
}
|
|
183
|
+
const sourceOption = options.source;
|
|
184
|
+
this.loadSource = typeof sourceOption === 'function' ? sourceOption : () => sourceOption;
|
|
185
|
+
this.canReadPlan = options.canReadPlan ?? defaultCanReadPlan;
|
|
186
|
+
this.maxDepth = normalizeMaxDepth(options.maxDepth);
|
|
187
|
+
}
|
|
188
|
+
async analyze(changedRefs, access) {
|
|
189
|
+
const source = await this.loadSource();
|
|
190
|
+
const raw = analyzeWorldModelImpact({
|
|
191
|
+
catalog: source.catalog,
|
|
192
|
+
relations: source.relations,
|
|
193
|
+
snapshots: source.snapshots,
|
|
194
|
+
plans: source.plans,
|
|
195
|
+
changedRefs,
|
|
196
|
+
maxDepth: this.maxDepth,
|
|
197
|
+
});
|
|
198
|
+
if (!access)
|
|
199
|
+
return raw;
|
|
200
|
+
const snapshots = source.snapshots ?? [];
|
|
201
|
+
const snapshotByKey = new Map();
|
|
202
|
+
for (const snapshot of snapshots) {
|
|
203
|
+
snapshotByKey.set(snapshotRevisionKey(snapshot.problem_id, snapshot.revision), snapshot);
|
|
204
|
+
}
|
|
205
|
+
const authorizedPlans = source.plans.filter((plan) => this.canReadPlan(plan, access, snapshotByKey.get(snapshotRevisionKey(plan.problem_snapshot.problem_id, plan.problem_snapshot.revision))));
|
|
206
|
+
const authorizedSnapshots = snapshots.filter((snapshot) => canReadFoundationAcl(snapshot.read_policy, access.principal));
|
|
207
|
+
const isReadable = createNodeReadabilityChecker({
|
|
208
|
+
catalog: source.catalog,
|
|
209
|
+
principal: access.principal,
|
|
210
|
+
authorizedPlans,
|
|
211
|
+
authorizedSnapshots,
|
|
212
|
+
allSnapshots: snapshots,
|
|
213
|
+
callerAuthorizedRefs: [],
|
|
214
|
+
});
|
|
215
|
+
const filteredItems = [];
|
|
216
|
+
for (const item of raw.affectedItems) {
|
|
217
|
+
if (!isReadable(item.affectedRef))
|
|
218
|
+
continue;
|
|
219
|
+
const redactedPath = redactWorldModelDependencyPath(item.dependencyPath, isReadable);
|
|
220
|
+
const sourceRef = isReadable(item.sourceRef)
|
|
221
|
+
? item.sourceRef
|
|
222
|
+
: { kind: 'redacted', id: REDACTED_WORLD_MODEL_REF_LABEL };
|
|
223
|
+
filteredItems.push({
|
|
224
|
+
status: 'needs_review',
|
|
225
|
+
affectedRef: item.affectedRef,
|
|
226
|
+
sourceRef,
|
|
227
|
+
dependencyPath: redactedPath,
|
|
228
|
+
depth: redactedPath.length,
|
|
229
|
+
reason: `needs_review: ${formatWorldModelDependencyPath(redactedPath)}`,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
version: raw.version,
|
|
234
|
+
changedRefs: raw.changedRefs.map((ref) => isReadable(ref) ? ref : { kind: 'redacted', id: REDACTED_WORLD_MODEL_REF_LABEL }),
|
|
235
|
+
affectedItems: filteredItems,
|
|
236
|
+
affectedModels: filteredItems.filter((item) => item.affectedRef.kind === 'model'),
|
|
237
|
+
affectedObjectives: filteredItems.filter((item) => item.affectedRef.kind === 'objective'),
|
|
238
|
+
affectedSnapshots: filteredItems.filter((item) => item.affectedRef.kind === 'judgment_problem'),
|
|
239
|
+
affectedPlans: filteredItems.filter((item) => item.affectedRef.kind === 'plan'),
|
|
240
|
+
cyclesDetected: raw.cyclesDetected,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async findAffectedPlans(input) {
|
|
244
|
+
const source = await this.loadSource();
|
|
245
|
+
const snapshots = source.snapshots ?? [];
|
|
246
|
+
const snapshotByKey = new Map();
|
|
247
|
+
for (const snapshot of snapshots) {
|
|
248
|
+
snapshotByKey.set(snapshotRevisionKey(snapshot.problem_id, snapshot.revision), snapshot);
|
|
249
|
+
}
|
|
250
|
+
const authorizedPlans = source.plans.filter((plan) => {
|
|
251
|
+
const snapshot = snapshotByKey.get(snapshotRevisionKey(plan.problem_snapshot.problem_id, plan.problem_snapshot.revision));
|
|
252
|
+
return this.canReadPlan(plan, input.access, snapshot);
|
|
253
|
+
});
|
|
254
|
+
if (authorizedPlans.length === 0 || input.changes.length === 0) {
|
|
255
|
+
return [];
|
|
256
|
+
}
|
|
257
|
+
const authorizedSnapshots = snapshots.filter((snapshot) => canReadFoundationAcl(snapshot.read_policy, input.access.principal));
|
|
258
|
+
const callerAuthorizedRefs = [
|
|
259
|
+
...input.changes.map((change) => ({
|
|
260
|
+
kind: change.current.kind,
|
|
261
|
+
id: change.current.id,
|
|
262
|
+
revision: change.current.revision,
|
|
263
|
+
})),
|
|
264
|
+
...input.current_references.map((ref) => ({
|
|
265
|
+
kind: ref.kind,
|
|
266
|
+
id: ref.id,
|
|
267
|
+
revision: ref.revision,
|
|
268
|
+
})),
|
|
269
|
+
];
|
|
270
|
+
const isReadable = createNodeReadabilityChecker({
|
|
271
|
+
catalog: source.catalog,
|
|
272
|
+
principal: input.access.principal,
|
|
273
|
+
authorizedPlans,
|
|
274
|
+
authorizedSnapshots,
|
|
275
|
+
allSnapshots: snapshots,
|
|
276
|
+
callerAuthorizedRefs,
|
|
277
|
+
});
|
|
278
|
+
const planById = new Map(authorizedPlans.map((plan) => [plan.plan_id, plan]));
|
|
279
|
+
const matchesByPlan = new Map();
|
|
280
|
+
for (const change of input.changes) {
|
|
281
|
+
const analysis = analyzeWorldModelImpact({
|
|
282
|
+
catalog: source.catalog,
|
|
283
|
+
relations: source.relations,
|
|
284
|
+
snapshots: authorizedSnapshots,
|
|
285
|
+
plans: authorizedPlans,
|
|
286
|
+
changedRefs: [{
|
|
287
|
+
kind: change.current.kind,
|
|
288
|
+
id: change.current.id,
|
|
289
|
+
revision: change.current.revision,
|
|
290
|
+
}],
|
|
291
|
+
maxDepth: this.maxDepth,
|
|
292
|
+
});
|
|
293
|
+
for (const item of analysis.affectedPlans) {
|
|
294
|
+
const plan = planById.get(item.affectedRef.id);
|
|
295
|
+
if (!plan)
|
|
296
|
+
continue;
|
|
297
|
+
const redactedPath = redactWorldModelDependencyPath(item.dependencyPath, isReadable);
|
|
298
|
+
const { domain, relation } = classifyPlanMatch(redactedPath);
|
|
299
|
+
const evidence = formatWorldModelDependencyPath(redactedPath) || REDACTED_WORLD_MODEL_REF_LABEL;
|
|
300
|
+
const match = {
|
|
301
|
+
change_id: change.change_id,
|
|
302
|
+
relation,
|
|
303
|
+
domain,
|
|
304
|
+
evidence,
|
|
305
|
+
};
|
|
306
|
+
const existing = matchesByPlan.get(plan.plan_id);
|
|
307
|
+
if (existing)
|
|
308
|
+
existing.push(match);
|
|
309
|
+
else
|
|
310
|
+
matchesByPlan.set(plan.plan_id, [match]);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const results = [];
|
|
314
|
+
for (const [planId, matches] of matchesByPlan) {
|
|
315
|
+
const plan = planById.get(planId);
|
|
316
|
+
if (!plan)
|
|
317
|
+
continue;
|
|
318
|
+
results.push({
|
|
319
|
+
plan: cloneJson(plan),
|
|
320
|
+
matches: deduplicateMatches(matches),
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return results.sort((left, right) => left.plan.plan_id.localeCompare(right.plan.plan_id, 'en'));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
export function createWorldModelImpactIndex(options) {
|
|
327
|
+
return new WorldModelImpactIndex(options);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Resolves the latest record for `(type, id)` from a `FoundationCatalog` using
|
|
331
|
+
* `catalog.latest` as the authoritative pointer and falling back to natural
|
|
332
|
+
* revision ordering only when no `latest` pointer exists.
|
|
333
|
+
*/
|
|
334
|
+
export function resolveLatestCatalogRecord(catalog, type, id) {
|
|
335
|
+
const key = foundationKey({ type, id });
|
|
336
|
+
const pointer = catalog.latest?.[key];
|
|
337
|
+
if (pointer && pointer.type === type && pointer.id === id && typeof pointer.revision === 'string' && pointer.revision.length > 0) {
|
|
338
|
+
if (CANONICAL_REVISION_PATTERN.test(pointer.revision)) {
|
|
339
|
+
const found = findLatestFoundationRecord(catalog, type, id);
|
|
340
|
+
if (found)
|
|
341
|
+
return found;
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
const exact = catalog.records.find((record) => record.definition.type === type
|
|
345
|
+
&& record.definition.id === id
|
|
346
|
+
&& record.definition.revision === pointer.revision);
|
|
347
|
+
if (exact)
|
|
348
|
+
return exact;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
let best;
|
|
352
|
+
for (const record of catalog.records) {
|
|
353
|
+
if (record.definition.type !== type || record.definition.id !== id)
|
|
354
|
+
continue;
|
|
355
|
+
if (!best || compareRevisionStrings(record.definition.revision, best.definition.revision) > 0) {
|
|
356
|
+
best = record;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return best;
|
|
360
|
+
}
|
|
361
|
+
function createNodeReadabilityChecker(options) {
|
|
362
|
+
const authorizedPlanIds = new Set(options.authorizedPlans.map((plan) => plan.plan_id));
|
|
363
|
+
const authorizedSnapshotKeys = new Set();
|
|
364
|
+
const authorizedSnapshotProblemIds = new Set();
|
|
365
|
+
const deniedSnapshotKeys = new Set();
|
|
366
|
+
for (const snapshot of options.allSnapshots) {
|
|
367
|
+
const key = snapshotRevisionKey(snapshot.problem_id, snapshot.revision);
|
|
368
|
+
if (!canReadFoundationAcl(snapshot.read_policy, options.principal)) {
|
|
369
|
+
deniedSnapshotKeys.add(key);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
for (const snapshot of options.authorizedSnapshots) {
|
|
373
|
+
authorizedSnapshotKeys.add(snapshotRevisionKey(snapshot.problem_id, snapshot.revision));
|
|
374
|
+
authorizedSnapshotProblemIds.add(snapshot.problem_id);
|
|
375
|
+
}
|
|
376
|
+
for (const plan of options.authorizedPlans) {
|
|
377
|
+
const key = snapshotRevisionKey(plan.problem_snapshot.problem_id, plan.problem_snapshot.revision);
|
|
378
|
+
if (!deniedSnapshotKeys.has(key)) {
|
|
379
|
+
authorizedSnapshotKeys.add(key);
|
|
380
|
+
authorizedSnapshotProblemIds.add(plan.problem_snapshot.problem_id);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const callerAuthorizedLogical = new Set(options.callerAuthorizedRefs.map((ref) => nodeKey(ref.kind, ref.id)));
|
|
384
|
+
const authorizedPlanRefLogical = new Set();
|
|
385
|
+
for (const plan of options.authorizedPlans) {
|
|
386
|
+
for (const ref of plan.references ?? []) {
|
|
387
|
+
authorizedPlanRefLogical.add(nodeKey(ref.kind, ref.id));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const foundationReadableCache = new Map();
|
|
391
|
+
const catalog = options.catalog;
|
|
392
|
+
return (ref) => {
|
|
393
|
+
if (ref.kind === 'redacted')
|
|
394
|
+
return false;
|
|
395
|
+
if (ref.kind === 'plan')
|
|
396
|
+
return authorizedPlanIds.has(ref.id);
|
|
397
|
+
if (ref.kind === 'judgment_problem') {
|
|
398
|
+
if (ref.revision)
|
|
399
|
+
return authorizedSnapshotKeys.has(snapshotRevisionKey(ref.id, ref.revision));
|
|
400
|
+
return authorizedSnapshotProblemIds.has(ref.id);
|
|
401
|
+
}
|
|
402
|
+
if (FOUNDATION_TYPE_SET.has(ref.kind)) {
|
|
403
|
+
const logical = nodeKey(ref.kind, ref.id);
|
|
404
|
+
const cached = foundationReadableCache.get(logical);
|
|
405
|
+
if (cached !== undefined)
|
|
406
|
+
return cached;
|
|
407
|
+
let allowed = false;
|
|
408
|
+
if (catalog) {
|
|
409
|
+
const latest = resolveLatestCatalogRecord(catalog, ref.kind, ref.id);
|
|
410
|
+
if (latest) {
|
|
411
|
+
allowed = canReadFoundationAcl(latest.definition.acl, options.principal);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
// Not present in catalog records: trust only caller-authorized seeds/references.
|
|
415
|
+
allowed = callerAuthorizedLogical.has(logical);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
allowed = callerAuthorizedLogical.has(logical);
|
|
420
|
+
}
|
|
421
|
+
foundationReadableCache.set(logical, allowed);
|
|
422
|
+
return allowed;
|
|
423
|
+
}
|
|
424
|
+
if (ref.kind === 'method' || ref.kind === 'dag') {
|
|
425
|
+
const logical = nodeKey(ref.kind, ref.id);
|
|
426
|
+
return callerAuthorizedLogical.has(logical) || authorizedPlanRefLogical.has(logical);
|
|
427
|
+
}
|
|
428
|
+
// Non-foundation relation endpoints (decision, story, project, execution, etc.)
|
|
429
|
+
// do not carry an ACL in FoundationCatalog; fail closed to avoid leaking IDs.
|
|
430
|
+
return false;
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function defaultCanReadPlan(plan, access, snapshot) {
|
|
434
|
+
if (!canReadFoundationAcl(plan.read_policy, access.principal))
|
|
435
|
+
return false;
|
|
436
|
+
if (snapshot && !canReadFoundationAcl(snapshot.read_policy, access.principal))
|
|
437
|
+
return false;
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
function buildImpactGraph(input) {
|
|
441
|
+
const edgeMap = new Map();
|
|
442
|
+
const knownRevisions = new Map();
|
|
443
|
+
const authoritativeLatestKeys = new Set();
|
|
444
|
+
const recordRevision = (ref, authoritative = false) => {
|
|
445
|
+
if (!ref.revision)
|
|
446
|
+
return;
|
|
447
|
+
const key = graphNodeKey(ref);
|
|
448
|
+
if (authoritative) {
|
|
449
|
+
knownRevisions.set(key, ref.revision);
|
|
450
|
+
authoritativeLatestKeys.add(key);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (authoritativeLatestKeys.has(key))
|
|
454
|
+
return;
|
|
455
|
+
const existing = knownRevisions.get(key);
|
|
456
|
+
if (!existing || compareRevisionStrings(ref.revision, existing) > 0) {
|
|
457
|
+
knownRevisions.set(key, ref.revision);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
const addEdge = (edge) => {
|
|
461
|
+
recordRevision(edge.from);
|
|
462
|
+
recordRevision(edge.to);
|
|
463
|
+
const dedupeKey = [
|
|
464
|
+
graphNodeKey(edge.from),
|
|
465
|
+
graphNodeKey(edge.to),
|
|
466
|
+
edge.relation,
|
|
467
|
+
edge.domain,
|
|
468
|
+
].join('\u0000');
|
|
469
|
+
if (!edgeMap.has(dedupeKey)) {
|
|
470
|
+
edgeMap.set(dedupeKey, edge);
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
const snapshots = input.snapshots ?? [];
|
|
474
|
+
const plans = input.plans ?? [];
|
|
475
|
+
const pinnedRecordKeys = collectPinnedFoundationKeys(snapshots, plans);
|
|
476
|
+
const catalog = input.catalog;
|
|
477
|
+
const latestRecordsByLogical = new Map();
|
|
478
|
+
if (catalog) {
|
|
479
|
+
const logicalIdentities = new Map();
|
|
480
|
+
for (const record of catalog.records) {
|
|
481
|
+
const def = record.definition;
|
|
482
|
+
const logical = nodeKey(def.type, def.id);
|
|
483
|
+
if (!logicalIdentities.has(logical)) {
|
|
484
|
+
logicalIdentities.set(logical, { type: def.type, id: def.id });
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
for (const [logical, identity] of logicalIdentities) {
|
|
488
|
+
const latest = resolveLatestCatalogRecord(catalog, identity.type, identity.id);
|
|
489
|
+
if (!latest)
|
|
490
|
+
continue;
|
|
491
|
+
latestRecordsByLogical.set(logical, latest);
|
|
492
|
+
recordRevision({ kind: latest.definition.type, id: latest.definition.id, revision: latest.definition.revision }, true);
|
|
493
|
+
}
|
|
494
|
+
const activeRecords = selectActiveCatalogRecords(catalog, latestRecordsByLogical, pinnedRecordKeys);
|
|
495
|
+
const snapshotsByProblemId = new Map();
|
|
496
|
+
for (const snapshot of snapshots) {
|
|
497
|
+
const list = snapshotsByProblemId.get(snapshot.problem_id);
|
|
498
|
+
if (list)
|
|
499
|
+
list.push(snapshot);
|
|
500
|
+
else
|
|
501
|
+
snapshotsByProblemId.set(snapshot.problem_id, [snapshot]);
|
|
502
|
+
}
|
|
503
|
+
const planIds = new Set(plans.map((plan) => plan.plan_id));
|
|
504
|
+
for (const record of activeRecords) {
|
|
505
|
+
const def = record.definition;
|
|
506
|
+
if (def.type === 'model') {
|
|
507
|
+
addModelDefinitionEdges(def, addEdge);
|
|
508
|
+
}
|
|
509
|
+
else if (def.type === 'objective') {
|
|
510
|
+
addObjectiveDefinitionEdges(def, addEdge);
|
|
511
|
+
}
|
|
512
|
+
else if (def.type === 'constraint') {
|
|
513
|
+
addConstraintDefinitionEdges(def, latestRecordsByLogical, snapshotsByProblemId, planIds, addEdge);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const allRelations = [
|
|
518
|
+
...(catalog?.relations ?? []),
|
|
519
|
+
...(input.relations ?? []),
|
|
520
|
+
];
|
|
521
|
+
for (const relation of allRelations) {
|
|
522
|
+
const edge = foundationRelationToImpactEdge(relation);
|
|
523
|
+
if (edge)
|
|
524
|
+
addEdge(edge);
|
|
525
|
+
}
|
|
526
|
+
for (const snapshot of snapshots) {
|
|
527
|
+
const snapshotRef = {
|
|
528
|
+
kind: 'judgment_problem',
|
|
529
|
+
id: snapshot.problem_id,
|
|
530
|
+
revision: snapshot.revision,
|
|
531
|
+
};
|
|
532
|
+
recordRevision(snapshotRef, true);
|
|
533
|
+
for (const ref of snapshot.references) {
|
|
534
|
+
if (!SNAPSHOT_INDEXABLE_KINDS.has(ref.kind))
|
|
535
|
+
continue;
|
|
536
|
+
addEdge({
|
|
537
|
+
from: {
|
|
538
|
+
kind: ref.kind,
|
|
539
|
+
id: ref.id,
|
|
540
|
+
revision: ref.revision,
|
|
541
|
+
},
|
|
542
|
+
to: snapshotRef,
|
|
543
|
+
relation: 'snapshot_reference',
|
|
544
|
+
domain: 'world_model',
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
for (const plan of plans) {
|
|
549
|
+
const planRef = { kind: 'plan', id: plan.plan_id };
|
|
550
|
+
addEdge({
|
|
551
|
+
from: {
|
|
552
|
+
kind: 'judgment_problem',
|
|
553
|
+
id: plan.problem_snapshot.problem_id,
|
|
554
|
+
revision: plan.problem_snapshot.revision,
|
|
555
|
+
},
|
|
556
|
+
to: planRef,
|
|
557
|
+
relation: 'plan_reference',
|
|
558
|
+
domain: 'world_model',
|
|
559
|
+
});
|
|
560
|
+
if (plan.references) {
|
|
561
|
+
for (const ref of plan.references) {
|
|
562
|
+
const isMethod = ref.kind === 'method';
|
|
563
|
+
addEdge({
|
|
564
|
+
from: { kind: ref.kind, id: ref.id, revision: ref.revision },
|
|
565
|
+
to: planRef,
|
|
566
|
+
relation: isMethod ? 'selected_method' : 'adopted',
|
|
567
|
+
domain: isMethod ? 'execution' : 'world_model',
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const adjacency = new Map();
|
|
573
|
+
for (const edge of edgeMap.values()) {
|
|
574
|
+
const fromKey = graphNodeKey(edge.from);
|
|
575
|
+
const list = adjacency.get(fromKey);
|
|
576
|
+
if (list)
|
|
577
|
+
list.push(edge);
|
|
578
|
+
else
|
|
579
|
+
adjacency.set(fromKey, [edge]);
|
|
580
|
+
}
|
|
581
|
+
for (const list of adjacency.values()) {
|
|
582
|
+
list.sort(compareEdges);
|
|
583
|
+
}
|
|
584
|
+
return { adjacency, knownRevisions };
|
|
585
|
+
}
|
|
586
|
+
function collectPinnedFoundationKeys(snapshots, plans) {
|
|
587
|
+
const keys = new Set();
|
|
588
|
+
for (const snapshot of snapshots) {
|
|
589
|
+
for (const ref of snapshot.references) {
|
|
590
|
+
if (ref.kind === 'objective' || ref.kind === 'variable' || ref.kind === 'model' || ref.kind === 'constraint') {
|
|
591
|
+
keys.add(`${nodeKey(ref.kind, ref.id)}@${ref.revision}`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
for (const plan of plans) {
|
|
596
|
+
for (const ref of plan.references ?? []) {
|
|
597
|
+
if (ref.kind === 'objective' || ref.kind === 'model' || ref.kind === 'constraint') {
|
|
598
|
+
keys.add(`${nodeKey(ref.kind, ref.id)}@${ref.revision}`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return keys;
|
|
603
|
+
}
|
|
604
|
+
function selectActiveCatalogRecords(catalog, latestByLogical, pinnedRecordKeys) {
|
|
605
|
+
const selected = new Map();
|
|
606
|
+
for (const record of latestByLogical.values()) {
|
|
607
|
+
const key = `${nodeKey(record.definition.type, record.definition.id)}@${record.definition.revision}`;
|
|
608
|
+
selected.set(key, record);
|
|
609
|
+
}
|
|
610
|
+
for (const record of catalog.records) {
|
|
611
|
+
const key = `${nodeKey(record.definition.type, record.definition.id)}@${record.definition.revision}`;
|
|
612
|
+
if (pinnedRecordKeys.has(key)) {
|
|
613
|
+
selected.set(key, record);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return [...selected.values()];
|
|
617
|
+
}
|
|
618
|
+
function addModelDefinitionEdges(model, addEdge) {
|
|
619
|
+
const modelRef = { kind: 'model', id: model.id, revision: model.revision };
|
|
620
|
+
for (const inputVar of model.inputVariableRefs) {
|
|
621
|
+
addEdge({
|
|
622
|
+
from: { kind: 'variable', id: inputVar.id, revision: inputVar.revision },
|
|
623
|
+
to: modelRef,
|
|
624
|
+
relation: 'uses_as_input',
|
|
625
|
+
domain: 'world_model',
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
for (const outputVar of model.outputVariableRefs) {
|
|
629
|
+
addEdge({
|
|
630
|
+
from: modelRef,
|
|
631
|
+
to: { kind: 'variable', id: outputVar.id, revision: outputVar.revision },
|
|
632
|
+
relation: 'predicts',
|
|
633
|
+
domain: 'world_model',
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
function addObjectiveDefinitionEdges(objective, addEdge) {
|
|
638
|
+
const objectiveRef = { kind: 'objective', id: objective.id, revision: objective.revision };
|
|
639
|
+
for (const criterion of objective.criteria) {
|
|
640
|
+
addEdge({
|
|
641
|
+
from: { kind: 'variable', id: criterion.variableRef.id, revision: criterion.variableRef.revision },
|
|
642
|
+
to: objectiveRef,
|
|
643
|
+
relation: 'evaluated_by',
|
|
644
|
+
domain: 'world_model',
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
function addConstraintDefinitionEdges(constraint, latestByLogical, snapshotsByProblemId, planIds, addEdge) {
|
|
649
|
+
const constraintRef = { kind: 'constraint', id: constraint.id, revision: constraint.revision };
|
|
650
|
+
for (const rawTarget of constraint.appliesTo) {
|
|
651
|
+
const typedMatch = TYPED_APPLIES_TO_PATTERN.exec(rawTarget);
|
|
652
|
+
if (typedMatch) {
|
|
653
|
+
const targetKind = typedMatch[1];
|
|
654
|
+
const targetId = typedMatch[2];
|
|
655
|
+
const targetRevision = typedMatch[3];
|
|
656
|
+
if (targetKind === 'constraint' && targetId === constraint.id)
|
|
657
|
+
continue;
|
|
658
|
+
if (targetKind === 'judgment_problem') {
|
|
659
|
+
const matchingSnapshots = (snapshotsByProblemId.get(targetId) ?? []).filter((snapshot) => targetRevision === undefined || snapshot.revision === targetRevision);
|
|
660
|
+
for (const snapshot of matchingSnapshots) {
|
|
661
|
+
addEdge({
|
|
662
|
+
from: constraintRef,
|
|
663
|
+
to: { kind: 'judgment_problem', id: snapshot.problem_id, revision: snapshot.revision },
|
|
664
|
+
relation: 'applies_to',
|
|
665
|
+
domain: 'world_model',
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (targetKind === 'plan') {
|
|
671
|
+
if (planIds.has(targetId)) {
|
|
672
|
+
addEdge({
|
|
673
|
+
from: constraintRef,
|
|
674
|
+
to: { kind: 'plan', id: targetId },
|
|
675
|
+
relation: 'applies_to',
|
|
676
|
+
domain: 'world_model',
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
const record = latestByLogical.get(nodeKey(targetKind, targetId));
|
|
682
|
+
if (record && (targetRevision === undefined || record.definition.revision === targetRevision)) {
|
|
683
|
+
addEdge({
|
|
684
|
+
from: constraintRef,
|
|
685
|
+
to: { kind: record.definition.type, id: record.definition.id, revision: record.definition.revision },
|
|
686
|
+
relation: 'applies_to',
|
|
687
|
+
domain: 'world_model',
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
// Bare untyped ID: collect candidate targets by kind and bind only when unambiguous.
|
|
693
|
+
const candidatesByKind = new Map();
|
|
694
|
+
for (const record of latestByLogical.values()) {
|
|
695
|
+
if (record.definition.id !== rawTarget)
|
|
696
|
+
continue;
|
|
697
|
+
if (record.definition.type === 'constraint' && record.definition.id === constraint.id)
|
|
698
|
+
continue;
|
|
699
|
+
const list = candidatesByKind.get(record.definition.type) ?? [];
|
|
700
|
+
list.push({
|
|
701
|
+
kind: record.definition.type,
|
|
702
|
+
id: record.definition.id,
|
|
703
|
+
revision: record.definition.revision,
|
|
704
|
+
});
|
|
705
|
+
candidatesByKind.set(record.definition.type, list);
|
|
706
|
+
}
|
|
707
|
+
const matchingSnapshots = snapshotsByProblemId.get(rawTarget);
|
|
708
|
+
if (matchingSnapshots && matchingSnapshots.length > 0) {
|
|
709
|
+
candidatesByKind.set('judgment_problem', matchingSnapshots.map((snapshot) => ({
|
|
710
|
+
kind: 'judgment_problem',
|
|
711
|
+
id: snapshot.problem_id,
|
|
712
|
+
revision: snapshot.revision,
|
|
713
|
+
})));
|
|
714
|
+
}
|
|
715
|
+
if (planIds.has(rawTarget)) {
|
|
716
|
+
candidatesByKind.set('plan', [{ kind: 'plan', id: rawTarget }]);
|
|
717
|
+
}
|
|
718
|
+
// Do not cross-bind an untyped ID across multiple distinct resource kinds.
|
|
719
|
+
if (candidatesByKind.size !== 1)
|
|
720
|
+
continue;
|
|
721
|
+
const [onlyTargets] = candidatesByKind.values();
|
|
722
|
+
for (const target of onlyTargets) {
|
|
723
|
+
addEdge({
|
|
724
|
+
from: constraintRef,
|
|
725
|
+
to: target,
|
|
726
|
+
relation: 'applies_to',
|
|
727
|
+
domain: 'world_model',
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
function foundationRelationToImpactEdge(relation) {
|
|
733
|
+
const source = {
|
|
734
|
+
kind: relation.source.type,
|
|
735
|
+
id: relation.source.id,
|
|
736
|
+
...(relation.source.revision ? { revision: relation.source.revision } : {}),
|
|
737
|
+
};
|
|
738
|
+
const target = {
|
|
739
|
+
kind: relation.target.type,
|
|
740
|
+
id: relation.target.id,
|
|
741
|
+
...(relation.target.revision ? { revision: relation.target.revision } : {}),
|
|
742
|
+
};
|
|
743
|
+
switch (relation.relation) {
|
|
744
|
+
// Model uses Variable as input => Variable change impacts Model.
|
|
745
|
+
case 'uses_as_input':
|
|
746
|
+
return { from: target, to: source, relation: 'uses_as_input', domain: 'world_model' };
|
|
747
|
+
// Model predicts Variable => Model change/refutation impacts predicted Variable.
|
|
748
|
+
case 'predicts':
|
|
749
|
+
return { from: source, to: target, relation: 'predicts', domain: 'world_model' };
|
|
750
|
+
// Objective is evaluated by Variable => Variable change impacts Objective.
|
|
751
|
+
case 'evaluated_by':
|
|
752
|
+
return { from: target, to: source, relation: 'evaluated_by', domain: 'world_model' };
|
|
753
|
+
// Constraint applies to Target => Constraint change impacts Target.
|
|
754
|
+
case 'applies_to':
|
|
755
|
+
return { from: source, to: target, relation: 'applies_to', domain: 'world_model' };
|
|
756
|
+
// Decision / JudgmentProblem used Target as basis => Target change impacts Decision / JudgmentProblem.
|
|
757
|
+
case 'used_as_basis':
|
|
758
|
+
return { from: target, to: source, relation: 'used_as_basis', domain: 'world_model' };
|
|
759
|
+
// Story / Objective contributes to Objective => Objective change impacts contributing Story / Objective.
|
|
760
|
+
case 'contributes_to':
|
|
761
|
+
return { from: target, to: source, relation: 'contributes_to', domain: 'world_model' };
|
|
762
|
+
// Story execution depends on Story / Objective => Target change impacts Story.
|
|
763
|
+
case 'execution_depends_on':
|
|
764
|
+
return { from: target, to: source, relation: 'execution_depends_on', domain: 'execution' };
|
|
765
|
+
// Story time condition tied to Objective => Objective change impacts Story schedule.
|
|
766
|
+
case 'time_condition':
|
|
767
|
+
return { from: target, to: source, relation: 'time_condition', domain: 'execution' };
|
|
768
|
+
default:
|
|
769
|
+
return null;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
function classifyPlanMatch(path) {
|
|
773
|
+
const unredactedSteps = path.filter((step) => step.relation !== REDACTED_WORLD_MODEL_RELATION
|
|
774
|
+
&& step.from.kind !== 'redacted'
|
|
775
|
+
&& step.to.kind !== 'redacted');
|
|
776
|
+
if (unredactedSteps.some((step) => step.relation === 'selected_method')) {
|
|
777
|
+
return { domain: 'execution', relation: 'selected_method' };
|
|
778
|
+
}
|
|
779
|
+
if (unredactedSteps.some((step) => step.relation === 'execution_depends_on')) {
|
|
780
|
+
return { domain: 'execution', relation: 'execution_depends_on' };
|
|
781
|
+
}
|
|
782
|
+
for (const step of unredactedSteps) {
|
|
783
|
+
if (step.relation === 'predicts' || step.relation === 'uses_as_input' || step.relation === 'applies_to') {
|
|
784
|
+
return { domain: 'world_model', relation: step.relation };
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return { domain: 'world_model', relation: 'adopted' };
|
|
788
|
+
}
|
|
789
|
+
function deduplicateMatches(matches) {
|
|
790
|
+
const map = new Map();
|
|
791
|
+
for (const match of matches) {
|
|
792
|
+
const key = `${match.change_id}\u0000${match.domain}\u0000${match.relation}\u0000${match.evidence}`;
|
|
793
|
+
if (!map.has(key))
|
|
794
|
+
map.set(key, match);
|
|
795
|
+
}
|
|
796
|
+
return [...map.values()].sort((left, right) => `${left.change_id}\u0000${left.domain}\u0000${left.relation}\u0000${left.evidence}`.localeCompare(`${right.change_id}\u0000${right.domain}\u0000${right.relation}\u0000${right.evidence}`, 'en'));
|
|
797
|
+
}
|
|
798
|
+
function normalizeChangedRefs(refs) {
|
|
799
|
+
const map = new Map();
|
|
800
|
+
for (const ref of refs) {
|
|
801
|
+
if (!ref || typeof ref.id !== 'string' || ref.id.trim() === '' || typeof ref.kind !== 'string' || ref.kind.trim() === '') {
|
|
802
|
+
continue;
|
|
803
|
+
}
|
|
804
|
+
const normalized = {
|
|
805
|
+
kind: ref.kind,
|
|
806
|
+
id: ref.id.trim(),
|
|
807
|
+
...(ref.revision && ref.revision.trim() !== '' ? { revision: ref.revision.trim() } : {}),
|
|
808
|
+
};
|
|
809
|
+
const key = `${nodeKey(normalized.kind, normalized.id)}@${normalized.revision ?? ''}`;
|
|
810
|
+
if (!map.has(key))
|
|
811
|
+
map.set(key, normalized);
|
|
812
|
+
}
|
|
813
|
+
return [...map.values()].sort(compareRefs);
|
|
814
|
+
}
|
|
815
|
+
function normalizeMaxDepth(value) {
|
|
816
|
+
if (value === undefined)
|
|
817
|
+
return DEFAULT_MAX_DEPTH;
|
|
818
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
819
|
+
throw new RangeError('maxDepth must be a positive integer');
|
|
820
|
+
}
|
|
821
|
+
return value;
|
|
822
|
+
}
|
|
823
|
+
function enrichRef(ref, knownRevisions) {
|
|
824
|
+
if (ref.kind === 'redacted') {
|
|
825
|
+
return { kind: 'redacted', id: REDACTED_WORLD_MODEL_REF_LABEL };
|
|
826
|
+
}
|
|
827
|
+
if (ref.revision) {
|
|
828
|
+
return { kind: ref.kind, id: ref.id, revision: ref.revision };
|
|
829
|
+
}
|
|
830
|
+
const known = knownRevisions.get(graphNodeKey(ref));
|
|
831
|
+
return known ? { kind: ref.kind, id: ref.id, revision: known } : { kind: ref.kind, id: ref.id };
|
|
832
|
+
}
|
|
833
|
+
function nodeKey(kind, id) {
|
|
834
|
+
return JSON.stringify([kind, id]);
|
|
835
|
+
}
|
|
836
|
+
function graphNodeKey(ref) {
|
|
837
|
+
if (ref.kind === 'judgment_problem' && ref.revision) {
|
|
838
|
+
return JSON.stringify([ref.kind, ref.id, ref.revision]);
|
|
839
|
+
}
|
|
840
|
+
return JSON.stringify([ref.kind, ref.id]);
|
|
841
|
+
}
|
|
842
|
+
function snapshotRevisionKey(problemId, revision) {
|
|
843
|
+
return JSON.stringify([problemId, revision]);
|
|
844
|
+
}
|
|
845
|
+
function formatRef(ref) {
|
|
846
|
+
if (ref.kind === 'redacted')
|
|
847
|
+
return REDACTED_WORLD_MODEL_REF_LABEL;
|
|
848
|
+
return ref.revision ? `${ref.kind}/${ref.id}@${ref.revision}` : `${ref.kind}/${ref.id}`;
|
|
849
|
+
}
|
|
850
|
+
function compareRefs(left, right) {
|
|
851
|
+
return left.kind.localeCompare(right.kind, 'en')
|
|
852
|
+
|| left.id.localeCompare(right.id, 'en')
|
|
853
|
+
|| compareRevisionStrings(left.revision ?? '', right.revision ?? '');
|
|
854
|
+
}
|
|
855
|
+
function compareEdges(left, right) {
|
|
856
|
+
return compareRefs(left.to, right.to)
|
|
857
|
+
|| left.relation.localeCompare(right.relation, 'en')
|
|
858
|
+
|| left.domain.localeCompare(right.domain, 'en');
|
|
859
|
+
}
|
|
860
|
+
function compareAffectedItems(left, right) {
|
|
861
|
+
return compareRefs(left.affectedRef, right.affectedRef)
|
|
862
|
+
|| (left.depth - right.depth)
|
|
863
|
+
|| compareRefs(left.sourceRef, right.sourceRef)
|
|
864
|
+
|| left.reason.localeCompare(right.reason, 'en');
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Natural revision comparison so numeric revisions (`9` vs `10`) and prefixed
|
|
868
|
+
* revisions (`r9` vs `r10`, `v1.9` vs `v1.10`) sort in true numeric order.
|
|
869
|
+
*/
|
|
870
|
+
export function compareRevisionStrings(left, right) {
|
|
871
|
+
if (left === right)
|
|
872
|
+
return 0;
|
|
873
|
+
if (/^\d+$/u.test(left) && /^\d+$/u.test(right)) {
|
|
874
|
+
const l = BigInt(left);
|
|
875
|
+
const r = BigInt(right);
|
|
876
|
+
return l < r ? -1 : l > r ? 1 : 0;
|
|
877
|
+
}
|
|
878
|
+
const leftParts = left.split(/(\d+)/u);
|
|
879
|
+
const rightParts = right.split(/(\d+)/u);
|
|
880
|
+
const length = Math.min(leftParts.length, rightParts.length);
|
|
881
|
+
for (let index = 0; index < length; index += 1) {
|
|
882
|
+
const lPart = leftParts[index];
|
|
883
|
+
const rPart = rightParts[index];
|
|
884
|
+
if (lPart === rPart)
|
|
885
|
+
continue;
|
|
886
|
+
if (/^\d+$/u.test(lPart) && /^\d+$/u.test(rPart)) {
|
|
887
|
+
const l = BigInt(lPart);
|
|
888
|
+
const r = BigInt(rPart);
|
|
889
|
+
if (l !== r)
|
|
890
|
+
return l < r ? -1 : 1;
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
const cmp = lPart.localeCompare(rPart, 'en');
|
|
894
|
+
if (cmp !== 0)
|
|
895
|
+
return cmp;
|
|
896
|
+
}
|
|
897
|
+
return leftParts.length - rightParts.length;
|
|
898
|
+
}
|
|
899
|
+
function cloneJson(value) {
|
|
900
|
+
return JSON.parse(JSON.stringify(value));
|
|
901
|
+
}
|