@ontrails/topography 0.2.0
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/CHANGELOG.md +866 -0
- package/README.md +228 -0
- package/package.json +40 -0
- package/src/activation-report.ts +385 -0
- package/src/backend-support.ts +11 -0
- package/src/derive.ts +690 -0
- package/src/diff.ts +1088 -0
- package/src/forces.ts +125 -0
- package/src/hash.ts +55 -0
- package/src/index.ts +273 -0
- package/src/internal/topo-snapshots.ts +682 -0
- package/src/internal/topo-store-read.ts +1102 -0
- package/src/internal/topo-store.ts +1651 -0
- package/src/io.ts +280 -0
- package/src/library-derivation.ts +131 -0
- package/src/overlays.ts +155 -0
- package/src/permit.ts +19 -0
- package/src/source-fingerprint.ts +103 -0
- package/src/surface-bindings.ts +101 -0
- package/src/topo-store.ts +716 -0
- package/src/types.ts +749 -0
- package/src/versioning.ts +277 -0
- package/src/wayfind/error-facts.ts +363 -0
- package/src/wayfind/filters.ts +430 -0
- package/src/wayfind/loader.ts +443 -0
- package/src/wayfind/navigation.ts +265 -0
- package/src/wayfind/provenance.ts +152 -0
- package/src/wayfind/queries.ts +1656 -0
- package/src/wayfind/relations.ts +344 -0
- package/src/workspace-configured-aliases.ts +221 -0
- package/src/workspace-lock-census.ts +306 -0
- package/src/workspace-view-types.ts +104 -0
- package/src/workspace-view.ts +486 -0
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
import type { SQLQueryBindings } from 'bun:sqlite';
|
|
2
|
+
import { existsSync, statSync } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
NotFoundError,
|
|
6
|
+
Result,
|
|
7
|
+
deriveTrailsDbPath,
|
|
8
|
+
openReadTrailsDb,
|
|
9
|
+
openWriteTrailsDb,
|
|
10
|
+
resource,
|
|
11
|
+
} from '@ontrails/core';
|
|
12
|
+
import type { Topo, TrailsDbLocationOptions } from '@ontrails/core';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
TOPO_SCHEMA_VERSION,
|
|
16
|
+
ensureTopoSnapshotSchema,
|
|
17
|
+
listTopoSnapshots as listStoredTopoSnapshots,
|
|
18
|
+
pinTopoSnapshot as pinStoredTopoSnapshot,
|
|
19
|
+
unpinTopoSnapshot as unpinStoredTopoSnapshot,
|
|
20
|
+
} from './internal/topo-snapshots.js';
|
|
21
|
+
import type {
|
|
22
|
+
CreateTopoSnapshotInput,
|
|
23
|
+
ListTopoSnapshotsOptions,
|
|
24
|
+
TopoSnapshot,
|
|
25
|
+
} from './internal/topo-snapshots.js';
|
|
26
|
+
import type {
|
|
27
|
+
TopoStoreEntityRecord,
|
|
28
|
+
TopoStoreEntryKind,
|
|
29
|
+
TopoStoreExportRecord,
|
|
30
|
+
TopoStoreResourceRecord,
|
|
31
|
+
TopoStoreRef,
|
|
32
|
+
TopoStoreSignalDetailRecord,
|
|
33
|
+
TopoStoreSignalRecord,
|
|
34
|
+
TopoStoreTopoGraphEntryRecord,
|
|
35
|
+
TopoStoreTopoGraphRecord,
|
|
36
|
+
TopoStoreTrailDetailRecord,
|
|
37
|
+
TopoStoreTrailRecord,
|
|
38
|
+
} from './internal/topo-store-read.js';
|
|
39
|
+
import {
|
|
40
|
+
getTopoStoreEntity,
|
|
41
|
+
getTopoStoreEntry,
|
|
42
|
+
getTopoStoreExport,
|
|
43
|
+
getTopoStoreResource,
|
|
44
|
+
getTopoStoreSignal,
|
|
45
|
+
getTopoStoreTopoGraph,
|
|
46
|
+
getTopoStoreTrail,
|
|
47
|
+
listTopoStoreEntities,
|
|
48
|
+
listTopoStoreEntries,
|
|
49
|
+
listTopoStoreResources,
|
|
50
|
+
listTopoStoreSignals,
|
|
51
|
+
listTopoStoreSnapshots,
|
|
52
|
+
listTopoStoreTrails,
|
|
53
|
+
queryTopoStore,
|
|
54
|
+
readTopoStoreSnapshot,
|
|
55
|
+
} from './internal/topo-store-read.js';
|
|
56
|
+
import { createTopoSnapshot as storeTopoSnapshot } from './internal/topo-store.js';
|
|
57
|
+
|
|
58
|
+
interface MigratedDbIdentity {
|
|
59
|
+
readonly mtimeMs: number;
|
|
60
|
+
readonly size: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const migratedTopoDbPaths = new Map<string, MigratedDbIdentity>();
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Test-only instrumentation counters. Incremented by the read-path migration
|
|
67
|
+
* check to let tests assert that a current-schema store does not escalate to
|
|
68
|
+
* a write-mode open, and that cache invalidation re-runs the check when the
|
|
69
|
+
* underlying file is replaced.
|
|
70
|
+
*
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
export const __topoStoreMigrationStats = {
|
|
74
|
+
peekCalls: 0,
|
|
75
|
+
writeEscalations: 0,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const TOPO_STORE_SCHEMA_VERSION = TOPO_SCHEMA_VERSION;
|
|
79
|
+
|
|
80
|
+
const peekTopoSchemaVersion = (
|
|
81
|
+
options: TrailsDbLocationOptions | undefined
|
|
82
|
+
): number | undefined => {
|
|
83
|
+
let db: ReturnType<typeof openReadTrailsDb> | undefined;
|
|
84
|
+
try {
|
|
85
|
+
db = openReadTrailsDb(options);
|
|
86
|
+
const row = db
|
|
87
|
+
.query<{ version: number }, [string]>(
|
|
88
|
+
'SELECT version FROM meta_schema_versions WHERE subsystem = ?'
|
|
89
|
+
)
|
|
90
|
+
.get('topo');
|
|
91
|
+
return row?.version ?? 0;
|
|
92
|
+
} catch {
|
|
93
|
+
// Table missing or unexpected shape: caller will escalate to a write-mode
|
|
94
|
+
// open and run the migration, which rebuilds the schema.
|
|
95
|
+
return undefined;
|
|
96
|
+
} finally {
|
|
97
|
+
db?.close();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const statIdentity = (dbPath: string): MigratedDbIdentity | undefined => {
|
|
102
|
+
try {
|
|
103
|
+
const info = statSync(dbPath);
|
|
104
|
+
return { mtimeMs: info.mtimeMs, size: info.size };
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const identitiesEqual = (
|
|
111
|
+
a: MigratedDbIdentity,
|
|
112
|
+
b: MigratedDbIdentity
|
|
113
|
+
): boolean => a.mtimeMs === b.mtimeMs && a.size === b.size;
|
|
114
|
+
|
|
115
|
+
const runTopoMigrationEscalation = (
|
|
116
|
+
options: TrailsDbLocationOptions | undefined,
|
|
117
|
+
dbPath: string,
|
|
118
|
+
fallbackIdentity: MigratedDbIdentity
|
|
119
|
+
): void => {
|
|
120
|
+
__topoStoreMigrationStats.writeEscalations += 1;
|
|
121
|
+
const db = openWriteTrailsDb(options);
|
|
122
|
+
try {
|
|
123
|
+
ensureTopoSnapshotSchema(db);
|
|
124
|
+
} finally {
|
|
125
|
+
db.close();
|
|
126
|
+
}
|
|
127
|
+
// Re-stat after the migration so the cached identity matches the file we
|
|
128
|
+
// just touched, avoiding a spurious second escalation on the next read.
|
|
129
|
+
const postIdentity = statIdentity(dbPath) ?? fallbackIdentity;
|
|
130
|
+
migratedTopoDbPaths.set(dbPath, postIdentity);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const resolveIdentityIfFresh = (
|
|
134
|
+
dbPath: string
|
|
135
|
+
): MigratedDbIdentity | undefined => {
|
|
136
|
+
if (!existsSync(dbPath)) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const identity = statIdentity(dbPath);
|
|
140
|
+
if (identity === undefined) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const cached = migratedTopoDbPaths.get(dbPath);
|
|
144
|
+
if (cached !== undefined && identitiesEqual(cached, identity)) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
return identity;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Ensure the topo snapshot schema is at the current version before any
|
|
152
|
+
* read-only access. Peeks the version through a read-only handle first and
|
|
153
|
+
* only escalates to a write-mode open + migration when the store is stale.
|
|
154
|
+
*
|
|
155
|
+
* Memoized per resolved DB path keyed on file identity (mtime + size) so a
|
|
156
|
+
* long-running process that deletes and recreates `trails.db` re-runs the
|
|
157
|
+
* migration check against the fresh file.
|
|
158
|
+
*
|
|
159
|
+
* If the DB file does not yet exist, this is a no-op — the downstream read
|
|
160
|
+
* path will surface its own NotFoundError.
|
|
161
|
+
*/
|
|
162
|
+
const ensureTopoMigratedIfExists = (
|
|
163
|
+
options?: TrailsDbLocationOptions
|
|
164
|
+
): void => {
|
|
165
|
+
const dbPath = deriveTrailsDbPath(options);
|
|
166
|
+
const identity = resolveIdentityIfFresh(dbPath);
|
|
167
|
+
if (identity === undefined) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
__topoStoreMigrationStats.peekCalls += 1;
|
|
172
|
+
const version = peekTopoSchemaVersion(options);
|
|
173
|
+
if (version !== undefined && version >= TOPO_SCHEMA_VERSION) {
|
|
174
|
+
// Already current — record identity and skip the write-mode open entirely,
|
|
175
|
+
// preserving the read-only contract for callers whose filesystem mounts
|
|
176
|
+
// `trails.db` read-only.
|
|
177
|
+
migratedTopoDbPaths.set(dbPath, identity);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
runTopoMigrationEscalation(options, dbPath, identity);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export type {
|
|
185
|
+
TopoStoreActivationContextRecord,
|
|
186
|
+
TopoStoreEntityRecord,
|
|
187
|
+
TopoStoreEntryKind,
|
|
188
|
+
TopoStoreExportRecord,
|
|
189
|
+
TopoStoreResourceRecord,
|
|
190
|
+
TopoStoreRef,
|
|
191
|
+
TopoStoreSignalDetailRecord,
|
|
192
|
+
TopoStoreSignalRecord,
|
|
193
|
+
TopoStoreSurfaceDerivedRecord,
|
|
194
|
+
TopoStoreTopoGraphEntryRecord,
|
|
195
|
+
TopoStoreTopoGraphRecord,
|
|
196
|
+
TopoStoreTrailDetailRecord,
|
|
197
|
+
TopoStoreTrailRecord,
|
|
198
|
+
} from './internal/topo-store-read.js';
|
|
199
|
+
export type {
|
|
200
|
+
CreateTopoSnapshotInput,
|
|
201
|
+
ListTopoSnapshotsOptions,
|
|
202
|
+
TopoSnapshot,
|
|
203
|
+
} from './internal/topo-snapshots.js';
|
|
204
|
+
|
|
205
|
+
export interface ReadOnlyTopoStore {
|
|
206
|
+
readonly entities: {
|
|
207
|
+
get(
|
|
208
|
+
id: string,
|
|
209
|
+
options?: { readonly snapshot?: TopoStoreRef }
|
|
210
|
+
): TopoStoreEntityRecord | undefined;
|
|
211
|
+
list(options?: {
|
|
212
|
+
readonly snapshot?: TopoStoreRef;
|
|
213
|
+
}): readonly TopoStoreEntityRecord[];
|
|
214
|
+
};
|
|
215
|
+
readonly entries: {
|
|
216
|
+
get(
|
|
217
|
+
id: string,
|
|
218
|
+
options?: {
|
|
219
|
+
readonly kind?: TopoStoreEntryKind;
|
|
220
|
+
readonly snapshot?: TopoStoreRef;
|
|
221
|
+
}
|
|
222
|
+
): TopoStoreTopoGraphEntryRecord | undefined;
|
|
223
|
+
list(options?: {
|
|
224
|
+
readonly kind?: TopoStoreEntryKind;
|
|
225
|
+
readonly snapshot?: TopoStoreRef;
|
|
226
|
+
}): readonly TopoStoreTopoGraphEntryRecord[];
|
|
227
|
+
};
|
|
228
|
+
readonly exports: {
|
|
229
|
+
get(ref?: TopoStoreRef): TopoStoreExportRecord | undefined;
|
|
230
|
+
};
|
|
231
|
+
query<TRow extends Record<string, unknown>>(
|
|
232
|
+
sql: string,
|
|
233
|
+
bindings?: readonly SQLQueryBindings[]
|
|
234
|
+
): readonly TRow[];
|
|
235
|
+
readonly resources: {
|
|
236
|
+
get(
|
|
237
|
+
id: string,
|
|
238
|
+
options?: { readonly snapshot?: TopoStoreRef }
|
|
239
|
+
): TopoStoreResourceRecord | undefined;
|
|
240
|
+
list(options?: {
|
|
241
|
+
readonly snapshot?: TopoStoreRef;
|
|
242
|
+
}): readonly TopoStoreResourceRecord[];
|
|
243
|
+
};
|
|
244
|
+
readonly signals: {
|
|
245
|
+
get(
|
|
246
|
+
id: string,
|
|
247
|
+
options?: { readonly snapshot?: TopoStoreRef }
|
|
248
|
+
): TopoStoreSignalDetailRecord | undefined;
|
|
249
|
+
list(options?: {
|
|
250
|
+
readonly snapshot?: TopoStoreRef;
|
|
251
|
+
}): readonly TopoStoreSignalRecord[];
|
|
252
|
+
};
|
|
253
|
+
readonly snapshots: {
|
|
254
|
+
get(ref?: TopoStoreRef): TopoSnapshot | undefined;
|
|
255
|
+
latest(): TopoSnapshot | undefined;
|
|
256
|
+
list(options?: ListTopoSnapshotsOptions): readonly TopoSnapshot[];
|
|
257
|
+
};
|
|
258
|
+
readonly topoGraph: {
|
|
259
|
+
get(ref?: TopoStoreRef): TopoStoreTopoGraphRecord | undefined;
|
|
260
|
+
};
|
|
261
|
+
readonly trails: {
|
|
262
|
+
get(
|
|
263
|
+
id: string,
|
|
264
|
+
options?: { readonly snapshot?: TopoStoreRef }
|
|
265
|
+
): TopoStoreTrailDetailRecord | undefined;
|
|
266
|
+
list(options?: {
|
|
267
|
+
readonly intent?: TopoStoreTrailRecord['intent'];
|
|
268
|
+
readonly snapshot?: TopoStoreRef;
|
|
269
|
+
}): readonly TopoStoreTrailRecord[];
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface MockTopoStoreSeed {
|
|
274
|
+
readonly entities?: readonly TopoStoreEntityRecord[];
|
|
275
|
+
readonly entries?: readonly TopoStoreTopoGraphEntryRecord[];
|
|
276
|
+
readonly exports?: readonly TopoStoreExportRecord[];
|
|
277
|
+
readonly resources?: readonly TopoStoreResourceRecord[];
|
|
278
|
+
readonly signals?: readonly TopoStoreSignalDetailRecord[];
|
|
279
|
+
readonly snapshots?: readonly TopoSnapshot[];
|
|
280
|
+
readonly topoGraphs?: readonly TopoStoreTopoGraphRecord[];
|
|
281
|
+
readonly trails?: readonly TopoStoreTrailDetailRecord[];
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const missingStoreMessage =
|
|
285
|
+
'No saved topo state found. Populate trails.db first or run a topo-backed surface.';
|
|
286
|
+
|
|
287
|
+
const resolveStoreRootDir = (options?: TrailsDbLocationOptions): string =>
|
|
288
|
+
options?.rootDir ?? process.cwd();
|
|
289
|
+
|
|
290
|
+
const requireReadDb = (
|
|
291
|
+
options?: TrailsDbLocationOptions
|
|
292
|
+
): ReturnType<typeof openReadTrailsDb> => {
|
|
293
|
+
const dbPath = deriveTrailsDbPath(options);
|
|
294
|
+
if (!existsSync(dbPath)) {
|
|
295
|
+
throw new NotFoundError(missingStoreMessage);
|
|
296
|
+
}
|
|
297
|
+
ensureTopoMigratedIfExists(options);
|
|
298
|
+
return openReadTrailsDb(options);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const requireSavedTopoState = (
|
|
302
|
+
db: ReturnType<typeof openReadTrailsDb>
|
|
303
|
+
): void => {
|
|
304
|
+
if (readTopoStoreSnapshot(db) === undefined) {
|
|
305
|
+
throw new NotFoundError(missingStoreMessage);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const withStoredTopoState = <T>(
|
|
310
|
+
options: TrailsDbLocationOptions | undefined,
|
|
311
|
+
run: (db: ReturnType<typeof openReadTrailsDb>) => T
|
|
312
|
+
): T => {
|
|
313
|
+
const db = requireReadDb(options);
|
|
314
|
+
try {
|
|
315
|
+
requireSavedTopoState(db);
|
|
316
|
+
return run(db);
|
|
317
|
+
} finally {
|
|
318
|
+
db.close();
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const createSeedResolver = (seed?: MockTopoStoreSeed) => {
|
|
323
|
+
const snapshots = [...(seed?.snapshots ?? [])];
|
|
324
|
+
const trails = [...(seed?.trails ?? [])];
|
|
325
|
+
const resources = [...(seed?.resources ?? [])];
|
|
326
|
+
const signals = [...(seed?.signals ?? [])];
|
|
327
|
+
const exports = [...(seed?.exports ?? [])];
|
|
328
|
+
const topoGraphs = [
|
|
329
|
+
...(seed?.topoGraphs ??
|
|
330
|
+
exports.map((entry) => ({
|
|
331
|
+
snapshot: entry.snapshot,
|
|
332
|
+
topoGraph: entry.topoGraph,
|
|
333
|
+
}))),
|
|
334
|
+
];
|
|
335
|
+
const entries = [
|
|
336
|
+
...(seed?.entries ??
|
|
337
|
+
topoGraphs.flatMap(({ snapshot, topoGraph }) =>
|
|
338
|
+
topoGraph.entries.map((entry) => ({
|
|
339
|
+
...entry,
|
|
340
|
+
snapshotId: snapshot.id,
|
|
341
|
+
}))
|
|
342
|
+
)),
|
|
343
|
+
];
|
|
344
|
+
const entities = [
|
|
345
|
+
...(seed?.entities ??
|
|
346
|
+
entries
|
|
347
|
+
.filter((entry) => entry.kind === 'entity')
|
|
348
|
+
.map((entry) => entry as TopoStoreEntityRecord)),
|
|
349
|
+
];
|
|
350
|
+
|
|
351
|
+
const resolveSnapshot = (ref?: TopoStoreRef): TopoSnapshot | undefined => {
|
|
352
|
+
if (ref?.snapshotId !== undefined) {
|
|
353
|
+
return snapshots.find((snapshot) => snapshot.id === ref.snapshotId);
|
|
354
|
+
}
|
|
355
|
+
if (ref?.pin !== undefined) {
|
|
356
|
+
return snapshots.find((snapshot) => snapshot.pinnedAs === ref.pin);
|
|
357
|
+
}
|
|
358
|
+
return snapshots[0];
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
entities,
|
|
363
|
+
entries,
|
|
364
|
+
exports,
|
|
365
|
+
resolveSnapshot,
|
|
366
|
+
resources,
|
|
367
|
+
signals,
|
|
368
|
+
snapshots,
|
|
369
|
+
topoGraphs,
|
|
370
|
+
trails,
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export const createMockTopoStore = (
|
|
375
|
+
seed?: MockTopoStoreSeed
|
|
376
|
+
): ReadOnlyTopoStore => {
|
|
377
|
+
const resolved = createSeedResolver(seed);
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
entities: {
|
|
381
|
+
get(id, options) {
|
|
382
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
383
|
+
if (snapshot === undefined) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
return resolved.entities.find(
|
|
387
|
+
(entity) => entity.id === id && entity.snapshotId === snapshot.id
|
|
388
|
+
);
|
|
389
|
+
},
|
|
390
|
+
list(options) {
|
|
391
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
392
|
+
return snapshot === undefined
|
|
393
|
+
? []
|
|
394
|
+
: resolved.entities.filter(
|
|
395
|
+
(entity) => entity.snapshotId === snapshot.id
|
|
396
|
+
);
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
entries: {
|
|
400
|
+
get(id, options) {
|
|
401
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
402
|
+
if (snapshot === undefined) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
return resolved.entries.find(
|
|
406
|
+
(entry) =>
|
|
407
|
+
entry.id === id &&
|
|
408
|
+
(options?.kind === undefined || entry.kind === options.kind) &&
|
|
409
|
+
entry.snapshotId === snapshot.id
|
|
410
|
+
);
|
|
411
|
+
},
|
|
412
|
+
list(options) {
|
|
413
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
414
|
+
return snapshot === undefined
|
|
415
|
+
? []
|
|
416
|
+
: resolved.entries.filter(
|
|
417
|
+
(entry) =>
|
|
418
|
+
entry.snapshotId === snapshot.id &&
|
|
419
|
+
(options?.kind === undefined || entry.kind === options.kind)
|
|
420
|
+
);
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
exports: {
|
|
424
|
+
get(ref?: TopoStoreRef) {
|
|
425
|
+
const snapshot = resolved.resolveSnapshot(ref);
|
|
426
|
+
return snapshot === undefined
|
|
427
|
+
? undefined
|
|
428
|
+
: resolved.exports.find((entry) => entry.snapshot.id === snapshot.id);
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
query() {
|
|
432
|
+
throw new NotFoundError(
|
|
433
|
+
'Mock topoStore.query() is unsupported. Seed typed accessors instead.'
|
|
434
|
+
);
|
|
435
|
+
},
|
|
436
|
+
resources: {
|
|
437
|
+
get(id, options) {
|
|
438
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
439
|
+
if (snapshot === undefined) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
return resolved.resources.find(
|
|
443
|
+
(item) => item.id === id && item.snapshotId === snapshot.id
|
|
444
|
+
);
|
|
445
|
+
},
|
|
446
|
+
list(options) {
|
|
447
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
448
|
+
return snapshot === undefined
|
|
449
|
+
? []
|
|
450
|
+
: resolved.resources.filter(
|
|
451
|
+
(item) => item.snapshotId === snapshot.id
|
|
452
|
+
);
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
signals: {
|
|
456
|
+
get(id, options) {
|
|
457
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
458
|
+
if (snapshot === undefined) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
return resolved.signals.find(
|
|
462
|
+
(signal) => signal.id === id && signal.snapshotId === snapshot.id
|
|
463
|
+
);
|
|
464
|
+
},
|
|
465
|
+
list(options) {
|
|
466
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
467
|
+
if (snapshot === undefined) {
|
|
468
|
+
return [];
|
|
469
|
+
}
|
|
470
|
+
return resolved.signals.filter(
|
|
471
|
+
(signal) => signal.snapshotId === snapshot.id
|
|
472
|
+
);
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
snapshots: {
|
|
476
|
+
get(ref?: TopoStoreRef) {
|
|
477
|
+
return resolved.resolveSnapshot(ref);
|
|
478
|
+
},
|
|
479
|
+
latest() {
|
|
480
|
+
return resolved.snapshots[0];
|
|
481
|
+
},
|
|
482
|
+
list(options) {
|
|
483
|
+
let snapshots =
|
|
484
|
+
options?.pinned === undefined
|
|
485
|
+
? resolved.snapshots
|
|
486
|
+
: resolved.snapshots.filter((snapshot) =>
|
|
487
|
+
options.pinned
|
|
488
|
+
? snapshot.pinnedAs !== undefined
|
|
489
|
+
: snapshot.pinnedAs === undefined
|
|
490
|
+
);
|
|
491
|
+
if (options?.before !== undefined) {
|
|
492
|
+
const target = snapshots.find((s) => s.id === options.before);
|
|
493
|
+
if (target !== undefined) {
|
|
494
|
+
snapshots = snapshots.filter(
|
|
495
|
+
(s) =>
|
|
496
|
+
s.createdAt < target.createdAt ||
|
|
497
|
+
(s.createdAt === target.createdAt && s.id < target.id)
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (options?.limit !== undefined) {
|
|
502
|
+
snapshots = snapshots.slice(0, options.limit);
|
|
503
|
+
}
|
|
504
|
+
return snapshots;
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
topoGraph: {
|
|
508
|
+
get(ref?: TopoStoreRef) {
|
|
509
|
+
const snapshot = resolved.resolveSnapshot(ref);
|
|
510
|
+
return snapshot === undefined
|
|
511
|
+
? undefined
|
|
512
|
+
: resolved.topoGraphs.find(
|
|
513
|
+
(entry) => entry.snapshot.id === snapshot.id
|
|
514
|
+
);
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
trails: {
|
|
518
|
+
get(id, options) {
|
|
519
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
520
|
+
if (snapshot === undefined) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
return resolved.trails.find(
|
|
524
|
+
(trail) => trail.id === id && trail.snapshotId === snapshot.id
|
|
525
|
+
);
|
|
526
|
+
},
|
|
527
|
+
list(options) {
|
|
528
|
+
const snapshot = resolved.resolveSnapshot(options?.snapshot);
|
|
529
|
+
if (snapshot === undefined) {
|
|
530
|
+
return [];
|
|
531
|
+
}
|
|
532
|
+
return resolved.trails.filter(
|
|
533
|
+
(trail) =>
|
|
534
|
+
trail.snapshotId === snapshot.id &&
|
|
535
|
+
(options?.intent === undefined || trail.intent === options.intent)
|
|
536
|
+
);
|
|
537
|
+
},
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
export const createTopoStore = (
|
|
543
|
+
options?: TrailsDbLocationOptions
|
|
544
|
+
): ReadOnlyTopoStore => ({
|
|
545
|
+
entities: {
|
|
546
|
+
get(id, queryOptions) {
|
|
547
|
+
return withStoredTopoState(options, (db) =>
|
|
548
|
+
getTopoStoreEntity(db, id, queryOptions)
|
|
549
|
+
);
|
|
550
|
+
},
|
|
551
|
+
list(queryOptions) {
|
|
552
|
+
return withStoredTopoState(options, (db) =>
|
|
553
|
+
listTopoStoreEntities(db, queryOptions)
|
|
554
|
+
);
|
|
555
|
+
},
|
|
556
|
+
},
|
|
557
|
+
entries: {
|
|
558
|
+
get(id, queryOptions) {
|
|
559
|
+
return withStoredTopoState(options, (db) =>
|
|
560
|
+
getTopoStoreEntry(db, id, queryOptions)
|
|
561
|
+
);
|
|
562
|
+
},
|
|
563
|
+
list(queryOptions) {
|
|
564
|
+
return withStoredTopoState(options, (db) =>
|
|
565
|
+
listTopoStoreEntries(db, queryOptions)
|
|
566
|
+
);
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
exports: {
|
|
570
|
+
get(ref?: TopoStoreRef) {
|
|
571
|
+
return withStoredTopoState(options, (db) => getTopoStoreExport(db, ref));
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
query<TRow extends Record<string, unknown>>(
|
|
575
|
+
sql: string,
|
|
576
|
+
bindings?: readonly SQLQueryBindings[]
|
|
577
|
+
) {
|
|
578
|
+
return withStoredTopoState(options, (db) =>
|
|
579
|
+
queryTopoStore<TRow>(db, sql, bindings)
|
|
580
|
+
);
|
|
581
|
+
},
|
|
582
|
+
resources: {
|
|
583
|
+
get(id, queryOptions) {
|
|
584
|
+
return withStoredTopoState(options, (db) =>
|
|
585
|
+
getTopoStoreResource(db, id, queryOptions)
|
|
586
|
+
);
|
|
587
|
+
},
|
|
588
|
+
list(queryOptions) {
|
|
589
|
+
return withStoredTopoState(options, (db) =>
|
|
590
|
+
listTopoStoreResources(db, queryOptions)
|
|
591
|
+
);
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
signals: {
|
|
595
|
+
get(id, queryOptions) {
|
|
596
|
+
return withStoredTopoState(options, (db) =>
|
|
597
|
+
getTopoStoreSignal(db, id, queryOptions)
|
|
598
|
+
);
|
|
599
|
+
},
|
|
600
|
+
list(queryOptions) {
|
|
601
|
+
return withStoredTopoState(options, (db) =>
|
|
602
|
+
listTopoStoreSignals(db, queryOptions)
|
|
603
|
+
);
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
snapshots: {
|
|
607
|
+
get(ref?: TopoStoreRef) {
|
|
608
|
+
return withStoredTopoState(options, (db) =>
|
|
609
|
+
readTopoStoreSnapshot(db, ref)
|
|
610
|
+
);
|
|
611
|
+
},
|
|
612
|
+
latest() {
|
|
613
|
+
return withStoredTopoState(options, (db) => readTopoStoreSnapshot(db));
|
|
614
|
+
},
|
|
615
|
+
list(snapshotOptions) {
|
|
616
|
+
return withStoredTopoState(options, (db) =>
|
|
617
|
+
listTopoStoreSnapshots(db, snapshotOptions)
|
|
618
|
+
);
|
|
619
|
+
},
|
|
620
|
+
},
|
|
621
|
+
topoGraph: {
|
|
622
|
+
get(ref?: TopoStoreRef) {
|
|
623
|
+
return withStoredTopoState(options, (db) =>
|
|
624
|
+
getTopoStoreTopoGraph(db, ref)
|
|
625
|
+
);
|
|
626
|
+
},
|
|
627
|
+
},
|
|
628
|
+
trails: {
|
|
629
|
+
get(id, queryOptions) {
|
|
630
|
+
return withStoredTopoState(options, (db) =>
|
|
631
|
+
getTopoStoreTrail(db, id, queryOptions)
|
|
632
|
+
);
|
|
633
|
+
},
|
|
634
|
+
list(queryOptions) {
|
|
635
|
+
return withStoredTopoState(options, (db) =>
|
|
636
|
+
listTopoStoreTrails(db, queryOptions)
|
|
637
|
+
);
|
|
638
|
+
},
|
|
639
|
+
},
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
export const createTopoSnapshot = (
|
|
643
|
+
topo: Topo,
|
|
644
|
+
options?: TrailsDbLocationOptions & CreateTopoSnapshotInput
|
|
645
|
+
): Result<TopoSnapshot, Error> => {
|
|
646
|
+
const db = openWriteTrailsDb(options);
|
|
647
|
+
try {
|
|
648
|
+
return storeTopoSnapshot(db, topo, options);
|
|
649
|
+
} finally {
|
|
650
|
+
db.close();
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
export const listTopoSnapshots = (
|
|
655
|
+
options?: TrailsDbLocationOptions & ListTopoSnapshotsOptions
|
|
656
|
+
): readonly TopoSnapshot[] => {
|
|
657
|
+
const dbPath = deriveTrailsDbPath(options);
|
|
658
|
+
if (!existsSync(dbPath)) {
|
|
659
|
+
return [];
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
ensureTopoMigratedIfExists(options);
|
|
663
|
+
const db = openReadTrailsDb(options);
|
|
664
|
+
try {
|
|
665
|
+
return listStoredTopoSnapshots(db, options);
|
|
666
|
+
} finally {
|
|
667
|
+
db.close();
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
export const pinTopoSnapshot = (
|
|
672
|
+
id: string,
|
|
673
|
+
name: string,
|
|
674
|
+
options?: TrailsDbLocationOptions
|
|
675
|
+
): TopoSnapshot | undefined => {
|
|
676
|
+
const dbPath = deriveTrailsDbPath(options);
|
|
677
|
+
if (!existsSync(dbPath)) {
|
|
678
|
+
return undefined;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const db = openWriteTrailsDb(options);
|
|
682
|
+
try {
|
|
683
|
+
return pinStoredTopoSnapshot(db, { id, name });
|
|
684
|
+
} finally {
|
|
685
|
+
db.close();
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
export const unpinTopoSnapshot = (
|
|
690
|
+
nameOrId: string,
|
|
691
|
+
options?: TrailsDbLocationOptions
|
|
692
|
+
): TopoSnapshot | undefined => {
|
|
693
|
+
const dbPath = deriveTrailsDbPath(options);
|
|
694
|
+
if (!existsSync(dbPath)) {
|
|
695
|
+
return undefined;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const db = openWriteTrailsDb(options);
|
|
699
|
+
try {
|
|
700
|
+
return unpinStoredTopoSnapshot(db, nameOrId);
|
|
701
|
+
} finally {
|
|
702
|
+
db.close();
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
export const topoStore = resource('topo.store', {
|
|
707
|
+
create: (resourceCtx) =>
|
|
708
|
+
Result.ok(
|
|
709
|
+
createTopoStore({
|
|
710
|
+
rootDir:
|
|
711
|
+
resourceCtx.workspaceRoot ?? resourceCtx.cwd ?? resolveStoreRootDir(),
|
|
712
|
+
})
|
|
713
|
+
),
|
|
714
|
+
description: 'Read-only query access to saved topo state in trails.db',
|
|
715
|
+
mock: () => createMockTopoStore(),
|
|
716
|
+
});
|