@foldrun/core 17.3.0 → 18.0.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/dist/src/approvals.js +3 -3
- package/dist/src/blob-cache.d.ts +58 -0
- package/dist/src/blob-cache.js +312 -0
- package/dist/src/blob-pins.d.ts +44 -0
- package/dist/src/blob-pins.js +61 -0
- package/dist/src/blob-store.d.ts +47 -0
- package/dist/src/blob-store.js +362 -0
- package/dist/src/blob-sweeper.d.ts +70 -0
- package/dist/src/blob-sweeper.js +282 -0
- package/dist/src/blob-types.d.ts +145 -0
- package/dist/src/blob-types.js +115 -0
- package/dist/src/evals.js +1 -1
- package/dist/src/history.d.ts +92 -9
- package/dist/src/history.js +168 -20
- package/dist/src/journal-store.d.ts +58 -0
- package/dist/src/journal-store.js +117 -0
- package/dist/src/manifest.d.ts +92 -0
- package/dist/src/manifest.js +239 -0
- package/dist/src/ops-log.js +26 -3
- package/dist/src/platform.d.ts +7 -0
- package/dist/src/platform.js +1 -0
- package/dist/src/publish.d.ts +134 -0
- package/dist/src/publish.js +434 -0
- package/dist/src/records.d.ts +72 -0
- package/dist/src/records.js +24 -0
- package/dist/src/revision-store.d.ts +161 -0
- package/dist/src/revision-store.js +378 -0
- package/dist/src/run-container.d.ts +20 -0
- package/dist/src/run-container.js +61 -0
- package/dist/src/runner.js +1 -1
- package/dist/src/s3-blob-store.d.ts +29 -0
- package/dist/src/s3-blob-store.js +531 -0
- package/dist/src/state-retention.d.ts +15 -0
- package/dist/src/state-retention.js +65 -0
- package/dist/src/storage.d.ts +29 -1
- package/dist/src/storage.js +331 -81
- package/dist/src/store.d.ts +41 -0
- package/dist/src/store.js +188 -5
- package/dist/src/trigger-log.js +17 -0
- package/dist/src/workspace-revisions.d.ts +269 -0
- package/dist/src/workspace-revisions.js +1382 -0
- package/package.json +1 -1
- package/src/approvals.ts +3 -3
- package/src/blob-cache.ts +357 -0
- package/src/blob-pins.ts +99 -0
- package/src/blob-store.ts +389 -0
- package/src/blob-sweeper.ts +336 -0
- package/src/blob-types.ts +216 -0
- package/src/evals.ts +1 -1
- package/src/history.ts +226 -19
- package/src/journal-store.ts +152 -0
- package/src/manifest.ts +274 -0
- package/src/ops-log.ts +25 -3
- package/src/platform.ts +8 -0
- package/src/publish.ts +520 -0
- package/src/records.ts +105 -0
- package/src/revision-store.ts +513 -0
- package/src/run-container.ts +62 -0
- package/src/runner.ts +1 -1
- package/src/s3-blob-store.ts +560 -0
- package/src/state-retention.ts +83 -0
- package/src/storage.ts +335 -81
- package/src/store.ts +178 -5
- package/src/trigger-log.ts +15 -0
- package/src/workspace-revisions.ts +1423 -0
package/src/history.ts
CHANGED
|
@@ -55,6 +55,64 @@ export function registerTreeReader(fn: TreeReader) {
|
|
|
55
55
|
readTree = (tenant, scope) => fn(tenant, scope) ?? prev(tenant, scope);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** The scope's tree as the registered readers see it (text files only), or null. */
|
|
59
|
+
export function currentTree(tenant: string, scope: string): RevisionFile[] | null {
|
|
60
|
+
return readTree(tenant, scope);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Where a committed change set is also handed (workspace-revisions.ts registers
|
|
65
|
+
* here: this module cannot import it, it imports the store which imports this).
|
|
66
|
+
* Called after the legacy record is written; never throws into the writer.
|
|
67
|
+
*/
|
|
68
|
+
type Journal = (tenant: string, scope: string, files: RevisionFile[], meta: { by: string; message: string; at: string }) => void;
|
|
69
|
+
let journal: Journal | null = null;
|
|
70
|
+
export function registerJournal(fn: Journal | null) {
|
|
71
|
+
journal = fn;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A workspace was deleted: whatever a registered journal still holds for it
|
|
75
|
+
* (queued changes, their files) goes too (FOL-126 phase 4). */
|
|
76
|
+
let onGone: ((tenant: string, workspace: string) => void) | null = null;
|
|
77
|
+
export function registerWorkspaceGone(fn: ((tenant: string, workspace: string) => void) | null) {
|
|
78
|
+
onGone = fn;
|
|
79
|
+
}
|
|
80
|
+
export function workspaceGone(tenant: string, workspace: string): void {
|
|
81
|
+
try {
|
|
82
|
+
onGone?.(tenant, workspace);
|
|
83
|
+
} catch {
|
|
84
|
+
// a journal that cannot be cleared now is cleared at the next boot (the replay discards a missing workspace)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* What follows a workspace through a delete or a rename (workspace-revisions.ts
|
|
90
|
+
* registers it): its journal and its history in the revision store. The async
|
|
91
|
+
* forms run the folder's own change INSIDE them (`remove`, `rename`), so that
|
|
92
|
+
* a store which cannot be reached stops the change before anything is removed
|
|
93
|
+
* or moved, and a writer racing the change is held off until it is done. The
|
|
94
|
+
* sync forms are for a caller that cannot wait: they do what can be done now
|
|
95
|
+
* and finish in the background.
|
|
96
|
+
*/
|
|
97
|
+
export interface WorkspaceLifecycle {
|
|
98
|
+
drop(tenant: string, workspace: string, remove: () => void): Promise<void>;
|
|
99
|
+
move(tenant: string, from: string, to: string, rename: () => void): Promise<void>;
|
|
100
|
+
moved(tenant: string, from: string, to: string): void;
|
|
101
|
+
}
|
|
102
|
+
let lifecycleHooks: WorkspaceLifecycle | null = null;
|
|
103
|
+
export function registerWorkspaceLifecycle(l: WorkspaceLifecycle | null) {
|
|
104
|
+
lifecycleHooks = l;
|
|
105
|
+
}
|
|
106
|
+
export const workspaceLifecycle = (): WorkspaceLifecycle | null => lifecycleHooks;
|
|
107
|
+
/** A workspace's folder was renamed (sync path): see WorkspaceLifecycle.moved. */
|
|
108
|
+
export function workspaceMoved(tenant: string, from: string, to: string): void {
|
|
109
|
+
try {
|
|
110
|
+
lifecycleHooks?.moved(tenant, from, to);
|
|
111
|
+
} catch {
|
|
112
|
+
// finished by the next reconcile
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
58
116
|
function assertSafeName(v: string, what: string) {
|
|
59
117
|
if (!/^[A-Za-z0-9][A-Za-z0-9._@-]{0,120}$/.test(v)) throw new Error(`illegal ${what}: ${v}`);
|
|
60
118
|
}
|
|
@@ -166,6 +224,11 @@ export function recordRevision(
|
|
|
166
224
|
// outside this call, such as a bundle's generated index.
|
|
167
225
|
snapshot(tenant, scope, rev.id);
|
|
168
226
|
fs.appendFileSync(path.join(dir, "index.jsonl"), `${JSON.stringify(line)}\n`);
|
|
227
|
+
try {
|
|
228
|
+
journal?.(tenant, scope, changed, { by: rev.by, message: rev.message, at: rev.at });
|
|
229
|
+
} catch {
|
|
230
|
+
// the revision store is not allowed to break a save; workspace-revisions logs and retries
|
|
231
|
+
}
|
|
169
232
|
return rev;
|
|
170
233
|
}
|
|
171
234
|
|
|
@@ -209,13 +272,13 @@ function snapshot(tenant: string, scope: string, id: string): boolean {
|
|
|
209
272
|
}
|
|
210
273
|
|
|
211
274
|
/** Whether a revision can be restored to: it has a snapshot. */
|
|
212
|
-
export function
|
|
275
|
+
export function hasSnapshotLocal(tenant: string, scope: string, id: string): boolean {
|
|
213
276
|
assertSafeName(id, "revision id");
|
|
214
277
|
return fs.existsSync(treePath(tenant, scope, id));
|
|
215
278
|
}
|
|
216
279
|
|
|
217
280
|
/** The whole tree at revision `id`, or null when it has no snapshot. */
|
|
218
|
-
export function
|
|
281
|
+
export function snapshotAtLocal(tenant: string, scope: string, id: string): { path: string; content: string }[] | null {
|
|
219
282
|
assertSafeName(id, "revision id");
|
|
220
283
|
let manifest: { v: number; files: Record<string, string> };
|
|
221
284
|
try {
|
|
@@ -239,7 +302,7 @@ function snapshotIds(tenant: string, scope: string): Set<string> {
|
|
|
239
302
|
}
|
|
240
303
|
|
|
241
304
|
/** The newest revision that can be restored to — what "now" is in history. */
|
|
242
|
-
export function
|
|
305
|
+
export function headRevisionLocal(tenant: string, scope: string): RevisionSummary | null {
|
|
243
306
|
const have = snapshotIds(tenant, scope);
|
|
244
307
|
return journalRevisions(tenant, scope, { limit: Infinity }).find((r) => have.has(r.id)) ?? null;
|
|
245
308
|
}
|
|
@@ -251,8 +314,8 @@ export function headRevision(tenant: string, scope: string): RevisionSummary | n
|
|
|
251
314
|
* by a path that never recorded a change. Returns the head's id, or null
|
|
252
315
|
* when there is nothing to snapshot.
|
|
253
316
|
*/
|
|
254
|
-
export function
|
|
255
|
-
const head =
|
|
317
|
+
export function ensureBaselineLocal(tenant: string, scope: string, by = "foldrun"): string | null {
|
|
318
|
+
const head = headRevisionLocal(tenant, scope);
|
|
256
319
|
if (head) return head.id;
|
|
257
320
|
const tree = readTree(tenant, scope);
|
|
258
321
|
if (!tree || tree.length === 0) return null;
|
|
@@ -265,7 +328,7 @@ export function ensureBaseline(tenant: string, scope: string, by = "foldrun"): s
|
|
|
265
328
|
try {
|
|
266
329
|
fs.writeFileSync(marker, id, { flag: "wx" });
|
|
267
330
|
} catch {
|
|
268
|
-
const held =
|
|
331
|
+
const held = headRevisionLocal(tenant, scope)?.id ?? null;
|
|
269
332
|
// A marker with no baseline behind it, minutes old, is a process that
|
|
270
333
|
// died writing one: clear it, and the next call writes the baseline.
|
|
271
334
|
try {
|
|
@@ -297,28 +360,46 @@ export function ensureBaseline(tenant: string, scope: string, by = "foldrun"): s
|
|
|
297
360
|
* moment. Null when history holds nothing that old. Throws (status 400) on
|
|
298
361
|
* something that is none of those.
|
|
299
362
|
*/
|
|
300
|
-
export function
|
|
363
|
+
export function resolveRevisionLocal(
|
|
301
364
|
tenant: string,
|
|
302
365
|
scope: string,
|
|
303
366
|
to: string,
|
|
304
367
|
now = Date.now(),
|
|
305
368
|
): { id: string; at: string; by: string; message: string } | null {
|
|
369
|
+
const have = snapshotIds(tenant, scope);
|
|
370
|
+
return resolveIn(journalRevisions(tenant, scope, { limit: Infinity }).filter((r) => have.has(r.id)), to, now);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The pick of resolveRevision over a newest-first list of restorable revisions. */
|
|
374
|
+
function resolveIn(all: RevisionSummary[], to: string, now: number): { id: string; at: string; by: string; message: string } | null {
|
|
375
|
+
return pick(all, target(to, now));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** What a restore's `to` names: a moment, or an id (or a commit). Throws 400
|
|
379
|
+
* on something that is neither. */
|
|
380
|
+
function target(to: string, now: number): { moment: number } | { id: string } {
|
|
306
381
|
const t = to.trim();
|
|
307
382
|
if (!t || t.startsWith("-")) throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
|
|
308
|
-
const have = snapshotIds(tenant, scope);
|
|
309
|
-
const all = journalRevisions(tenant, scope, { limit: Infinity }).filter((r) => have.has(r.id));
|
|
310
383
|
const rel = /^(\d+)\s*(m|h|d|w)$/.exec(t);
|
|
311
384
|
const moment = rel
|
|
312
385
|
? now - Number(rel[1]) * { m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }[rel[2] as "m" | "h" | "d" | "w"]
|
|
313
386
|
: /^\d{4}-\d{2}-\d{2}/.test(t)
|
|
314
387
|
? Date.parse(t)
|
|
315
388
|
: null;
|
|
316
|
-
let hit: RevisionSummary | undefined;
|
|
317
389
|
if (moment !== null) {
|
|
318
390
|
if (Number.isNaN(moment)) throw Object.assign(new Error(`not a date: ${to}`), { status: 400 });
|
|
319
|
-
|
|
391
|
+
return { moment };
|
|
392
|
+
}
|
|
393
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,120}$/.test(t)) throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
|
|
394
|
+
return { id: t };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function pick(all: RevisionSummary[], want: { moment: number } | { id: string }): { id: string; at: string; by: string; message: string } | null {
|
|
398
|
+
let hit: RevisionSummary | undefined;
|
|
399
|
+
if ("moment" in want) {
|
|
400
|
+
hit = all.find((r) => Date.parse(r.at) <= want.moment);
|
|
320
401
|
} else {
|
|
321
|
-
|
|
402
|
+
const t = want.id;
|
|
322
403
|
// An exact id first, then a commit: a commit deployed twice is recorded
|
|
323
404
|
// once under its own id and again under a fresh one naming it (30 Oct
|
|
324
405
|
// 2026, recordRevision), and newest-first with one predicate answered
|
|
@@ -347,7 +428,7 @@ function describe(files: RevisionFile[]): string {
|
|
|
347
428
|
}
|
|
348
429
|
|
|
349
430
|
/** Newest first. `forPath` narrows to revisions that touched one file. */
|
|
350
|
-
export function
|
|
431
|
+
export function listRevisionsLocal(
|
|
351
432
|
tenant: string,
|
|
352
433
|
scope: string,
|
|
353
434
|
opts: { forPath?: string; limit?: number } = {},
|
|
@@ -416,7 +497,7 @@ function rebuildIndex(dir: string): RevisionSummary[] {
|
|
|
416
497
|
return rows.map(({ written: _w, ...r }) => r).reverse();
|
|
417
498
|
}
|
|
418
499
|
|
|
419
|
-
export function
|
|
500
|
+
export function readRevisionLocal(tenant: string, scope: string, id: string, forPath?: string): Revision | null {
|
|
420
501
|
assertSafeName(id, "revision id");
|
|
421
502
|
const file = path.join(historyDir(tenant, scope), `${id}.json`);
|
|
422
503
|
if (!fs.existsSync(file)) return null;
|
|
@@ -427,11 +508,14 @@ export function readRevision(tenant: string, scope: string, id: string, forPath?
|
|
|
427
508
|
/** The files changed after revision `from` up to and including `to`, in a
|
|
428
509
|
* scope — what differs between the versions two runs ran (FOL-72). Null
|
|
429
510
|
* when either is unknown or `to` is not after `from`. */
|
|
430
|
-
export function
|
|
511
|
+
export function changedPathsBetweenLocal(tenant: string, scope: string, from: string | null, to: string | null): string[] | null {
|
|
431
512
|
if (!from || !to) return null;
|
|
432
513
|
if (from === to) return [];
|
|
433
|
-
|
|
434
|
-
|
|
514
|
+
return changedIn(listRevisionsLocal(tenant, scope, { limit: 500 }), from, to);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function changedIn(revs: RevisionSummary[], from: string, to: string): string[] | null {
|
|
518
|
+
// newest first. An exact id before a commit, as resolveRevision.
|
|
435
519
|
const at = (x: string) => {
|
|
436
520
|
const byId = revs.findIndex((r) => r.id === x);
|
|
437
521
|
return byId >= 0 ? byId : revs.findIndex((r) => r.commit === x);
|
|
@@ -444,8 +528,131 @@ export function changedPathsBetween(tenant: string, scope: string, from: string
|
|
|
444
528
|
|
|
445
529
|
/** The most recent revision id for a scope — what a change is attributed
|
|
446
530
|
* to when nothing more specific (a deployed commit) is on record. */
|
|
447
|
-
export function
|
|
448
|
-
return
|
|
531
|
+
export function latestRevisionIdLocal(tenant: string, scope: string): string | null {
|
|
532
|
+
return listRevisionsLocal(tenant, scope, { limit: 1 })[0]?.id ?? null;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
// ------------------------------------------------- history from revisions
|
|
537
|
+
//
|
|
538
|
+
// FOL-126 phase 3: where the install keeps revisions (workspace-revisions.ts
|
|
539
|
+
// registers a view), the history page, a revision's diff, the restore points
|
|
540
|
+
// and run-to-run compare are answered from them — the same shapes this file
|
|
541
|
+
// has always returned. Everything is async because a revision lives in a
|
|
542
|
+
// store, not on this disk. Where nothing is registered (a laptop, the CLI, an
|
|
543
|
+
// install with the journal off) or a workspace has no revisions yet, the
|
|
544
|
+
// local `.history/` answers, exactly as before; where both exist the list is
|
|
545
|
+
// the revisions followed by the older local entries, so turning revisions on
|
|
546
|
+
// loses no history. The account shelf ("@library") is never in revisions.
|
|
547
|
+
|
|
548
|
+
export interface RevisionView {
|
|
549
|
+
/** Newest first, or null when revisions do not serve this scope. Every page
|
|
550
|
+
* of the store up to `limit` (Infinity: all of it); `paths: false` leaves
|
|
551
|
+
* the changed paths out (rows say []), which is what makes "all" cheap. */
|
|
552
|
+
list(tenant: string, scope: string, opts: { forPath?: string; limit?: number; paths?: boolean }): Promise<RevisionSummary[] | null>;
|
|
553
|
+
read(tenant: string, scope: string, id: string, forPath?: string): Promise<Revision | null>;
|
|
554
|
+
snapshot(tenant: string, scope: string, id: string): Promise<{ path: string; content: string }[] | null>;
|
|
555
|
+
/** One revision by id, looked up rather than listed (paths []); null when
|
|
556
|
+
* the store has none. Optional: a view without it is asked through `list`. */
|
|
557
|
+
get?(tenant: string, scope: string, id: string): Promise<RevisionSummary | null>;
|
|
558
|
+
}
|
|
559
|
+
let view: RevisionView | null = null;
|
|
560
|
+
export function registerRevisionView(v: RevisionView | null) {
|
|
561
|
+
view = v;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Revisions first, then the local entries older than the oldest of them.
|
|
565
|
+
* `stored` is which rows came from the store (every one a restore point). */
|
|
566
|
+
async function combinedRows(
|
|
567
|
+
tenant: string,
|
|
568
|
+
scope: string,
|
|
569
|
+
opts: { forPath?: string; limit?: number; paths?: boolean },
|
|
570
|
+
restorableOnly = false,
|
|
571
|
+
): Promise<{ rows: RevisionSummary[]; stored: Set<string> }> {
|
|
572
|
+
const limit = opts.limit ?? 200;
|
|
573
|
+
// The whole of `limit`, every page of it: the store's first page is not all
|
|
574
|
+
// of history (FOL-152).
|
|
575
|
+
const mine = view && scope !== "@library" ? await view.list(tenant, scope, { ...opts, limit }) : null;
|
|
576
|
+
let local = listRevisionsLocal(tenant, scope, { forPath: opts.forPath, limit: Infinity });
|
|
577
|
+
if (restorableOnly) {
|
|
578
|
+
const have = snapshotIds(tenant, scope);
|
|
579
|
+
local = local.filter((r) => have.has(r.id));
|
|
580
|
+
}
|
|
581
|
+
if (!mine || !mine.length) return { rows: local.slice(0, limit), stored: new Set() };
|
|
582
|
+
const oldest = mine[mine.length - 1].at;
|
|
583
|
+
const seen = new Set(mine.map((r) => r.id));
|
|
584
|
+
return { rows: [...mine, ...local.filter((r) => !seen.has(r.id) && r.at < oldest)].slice(0, limit), stored: seen };
|
|
585
|
+
}
|
|
586
|
+
async function combined(tenant: string, scope: string, opts: { forPath?: string; limit?: number; paths?: boolean }, restorableOnly = false): Promise<RevisionSummary[]> {
|
|
587
|
+
return (await combinedRows(tenant, scope, opts, restorableOnly)).rows;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
export async function listRevisions(tenant: string, scope: string, opts: { forPath?: string; limit?: number } = {}): Promise<RevisionSummary[]> {
|
|
591
|
+
return combined(tenant, scope, opts);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** The list the history page shows: each row with whether it can be restored
|
|
595
|
+
* to. One listing answers both — every revision in the store is restorable;
|
|
596
|
+
* a local entry is when it has a snapshot — where asking per row would list
|
|
597
|
+
* the store once per row. */
|
|
598
|
+
export async function listRevisionsRestorable(tenant: string, scope: string, opts: { forPath?: string; limit?: number } = {}): Promise<(RevisionSummary & { restorable: boolean })[]> {
|
|
599
|
+
const { rows, stored } = await combinedRows(tenant, scope, opts);
|
|
600
|
+
const have = snapshotIds(tenant, scope);
|
|
601
|
+
return rows.map((r) => ({ ...r, restorable: have.has(r.id) || stored.has(r.id) }));
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
export async function readRevision(tenant: string, scope: string, id: string, forPath?: string): Promise<Revision | null> {
|
|
605
|
+
assertSafeName(id, "revision id");
|
|
606
|
+
const fromStore = view && scope !== "@library" ? await view.read(tenant, scope, id, forPath) : null;
|
|
607
|
+
return fromStore ?? readRevisionLocal(tenant, scope, id, forPath);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
export async function snapshotAt(tenant: string, scope: string, id: string): Promise<{ path: string; content: string }[] | null> {
|
|
611
|
+
assertSafeName(id, "revision id");
|
|
612
|
+
const fromStore = view && scope !== "@library" ? await view.snapshot(tenant, scope, id) : null;
|
|
613
|
+
return fromStore ?? snapshotAtLocal(tenant, scope, id);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export async function hasSnapshot(tenant: string, scope: string, id: string): Promise<boolean> {
|
|
617
|
+
assertSafeName(id, "revision id");
|
|
618
|
+
if (hasSnapshotLocal(tenant, scope, id)) return true;
|
|
619
|
+
if (!view || scope === "@library") return false;
|
|
620
|
+
// Looked up by id: a listing stops at a page, and a revision older than it
|
|
621
|
+
// was "not restorable" until 9 Nov 2026 (FOL-152).
|
|
622
|
+
if (view.get) return (await view.get(tenant, scope, id)) !== null;
|
|
623
|
+
return Boolean((await view.list(tenant, scope, { limit: Infinity, paths: false }))?.some((r) => r.id === id));
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
export async function headRevision(tenant: string, scope: string): Promise<RevisionSummary | null> {
|
|
627
|
+
return (await combined(tenant, scope, { limit: 1 }, true))[0] ?? null;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export async function resolveRevision(tenant: string, scope: string, to: string, now = Date.now()): Promise<{ id: string; at: string; by: string; message: string } | null> {
|
|
631
|
+
const want = target(to, now);
|
|
632
|
+
// An id in the store is one lookup; anything else (a date, a local id, a
|
|
633
|
+
// commit) is answered from ALL of history — every page of the store, without
|
|
634
|
+
// the changed paths, then the local entries.
|
|
635
|
+
if ("id" in want && view?.get && scope !== "@library") {
|
|
636
|
+
const hit = await view.get(tenant, scope, want.id);
|
|
637
|
+
if (hit) return { id: hit.id, at: hit.at, by: hit.by, message: hit.message };
|
|
638
|
+
}
|
|
639
|
+
return pick(await combined(tenant, scope, { limit: Infinity, paths: false }, true), want);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export async function changedPathsBetween(tenant: string, scope: string, from: string | null, to: string | null): Promise<string[] | null> {
|
|
643
|
+
if (!from || !to) return null;
|
|
644
|
+
if (from === to) return [];
|
|
645
|
+
return changedIn(await combined(tenant, scope, { limit: 500 }), from, to);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
export async function latestRevisionId(tenant: string, scope: string): Promise<string | null> {
|
|
649
|
+
return (await combined(tenant, scope, { limit: 1 }))[0]?.id ?? null;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** A scope with no revision at all gets a local baseline, as before. */
|
|
653
|
+
export async function ensureBaseline(tenant: string, scope: string, by = "foldrun"): Promise<string | null> {
|
|
654
|
+
const head = await headRevision(tenant, scope);
|
|
655
|
+
return head ? head.id : ensureBaselineLocal(tenant, scope, by);
|
|
449
656
|
}
|
|
450
657
|
|
|
451
658
|
// ------------------------------------------------------------------- diff
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Where the revision journal's entries are kept (FOL-126 phase 4, item 3).
|
|
2
|
+
//
|
|
3
|
+
// The journal (workspace-revisions.ts) writes every change down BEFORE it is
|
|
4
|
+
// queued for a commit, and removes it once committed. Until now "written down"
|
|
5
|
+
// meant a file under <data>/.revision-journal/ — on the shared volume, which
|
|
6
|
+
// the storage refactor retires. So the place is a seam:
|
|
7
|
+
//
|
|
8
|
+
// fsJournalStore the files, as before: the default, and the only one a
|
|
9
|
+
// laptop or a Compose install without a database ever has.
|
|
10
|
+
// A hosted install keeps it as a LEGACY source, replayed at
|
|
11
|
+
// boot and drained, so switching loses nothing.
|
|
12
|
+
// (platform) pgJournalStore (src/journal-pg.ts): a table, behind the
|
|
13
|
+
// same calls, registered where there is a database.
|
|
14
|
+
//
|
|
15
|
+
// The calls are synchronous (the writers that journal are), and an entry's
|
|
16
|
+
// `handle` is whatever the store needs to find it again.
|
|
17
|
+
|
|
18
|
+
import fs from "node:fs";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
|
|
21
|
+
/** What is stored: the whole entry (new content only, never the old text). */
|
|
22
|
+
export interface JournalRecord {
|
|
23
|
+
v: 1;
|
|
24
|
+
tenant: string;
|
|
25
|
+
workspace: string;
|
|
26
|
+
id: string;
|
|
27
|
+
files: { path: string; before: null; after: string | null; base?: string | null; handed?: string | null }[];
|
|
28
|
+
by: string;
|
|
29
|
+
message: string;
|
|
30
|
+
at: string;
|
|
31
|
+
restoredFrom?: string;
|
|
32
|
+
/** A run's copy-back: the revisions (workspace head, state head) its stage-in
|
|
33
|
+
* started from. Each file's base is read from these, not from the folder. */
|
|
34
|
+
baseRev?: { workspace: string | null; state: string | null };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface JournalStore {
|
|
38
|
+
/** Which store this is, for logs ("files", "database"). */
|
|
39
|
+
readonly kind: string;
|
|
40
|
+
/** Write the entry down; throws if it cannot. Returns its handle. Putting an
|
|
41
|
+
* id that is already there answers the existing handle. */
|
|
42
|
+
put(rec: JournalRecord): string;
|
|
43
|
+
/** Forget one entry. Safe to repeat. */
|
|
44
|
+
remove(tenant: string, workspace: string, handle: string): void;
|
|
45
|
+
/** Every workspace that has entries. */
|
|
46
|
+
workspaces(): { tenant: string; workspace: string }[];
|
|
47
|
+
/** One workspace's entries, oldest first, with how long each has been there. */
|
|
48
|
+
entries(tenant: string, workspace: string): { handle: string; ageMs: number }[];
|
|
49
|
+
/** Read one entry; throws for one that is torn or foreign. */
|
|
50
|
+
read(tenant: string, workspace: string, handle: string): JournalRecord;
|
|
51
|
+
/** Keep an unreadable entry out of every later replay, without destroying it. */
|
|
52
|
+
setAside(tenant: string, workspace: string, handle: string): void;
|
|
53
|
+
/** Housekeeping before a replay: debris of a write that died half way (files only). */
|
|
54
|
+
sweep(tenant: string, workspace: string, olderThanMs: number): void;
|
|
55
|
+
/** Remove everything of one workspace, or of the whole account. Safe to repeat. */
|
|
56
|
+
drop(tenant: string, workspace?: string): void;
|
|
57
|
+
/** A workspace was renamed: its entries not held by a live queue (a dead
|
|
58
|
+
* process's) now belong to the new name. Handles of entries that were
|
|
59
|
+
* moved are NOT promised to stay valid: a caller holding some forgets them
|
|
60
|
+
* first and puts them again under the new name. */
|
|
61
|
+
move(tenant: string, from: string, to: string): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The files: <root>/<tenant>/<workspace>/<time>-<seq>-<id>.json, tmp + rename. */
|
|
65
|
+
export function fsJournalStore(root: () => string): JournalStore {
|
|
66
|
+
let seq = 0;
|
|
67
|
+
const dirOf = (tenant: string, workspace: string) => path.join(root(), tenant, workspace);
|
|
68
|
+
return {
|
|
69
|
+
kind: "files",
|
|
70
|
+
put(rec) {
|
|
71
|
+
const dir = dirOf(rec.tenant, rec.workspace);
|
|
72
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
73
|
+
const name = `${Date.now().toString().padStart(14, "0")}-${String(seq++ % 1_000_000).padStart(6, "0")}-${rec.id}.json`;
|
|
74
|
+
const file = path.join(dir, name);
|
|
75
|
+
fs.writeFileSync(`${file}.tmp`, JSON.stringify(rec));
|
|
76
|
+
fs.renameSync(`${file}.tmp`, file);
|
|
77
|
+
return file;
|
|
78
|
+
},
|
|
79
|
+
remove(tenant, workspace, handle) {
|
|
80
|
+
try {
|
|
81
|
+
fs.rmSync(handle, { force: true });
|
|
82
|
+
fs.rmdirSync(dirOf(tenant, workspace)); // only when empty
|
|
83
|
+
} catch {
|
|
84
|
+
// not empty, or already gone
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
workspaces() {
|
|
88
|
+
const out: { tenant: string; workspace: string }[] = [];
|
|
89
|
+
let tenants: string[] = [];
|
|
90
|
+
try {
|
|
91
|
+
tenants = fs.readdirSync(root());
|
|
92
|
+
} catch {
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
for (const tenant of tenants) {
|
|
96
|
+
try {
|
|
97
|
+
for (const workspace of fs.readdirSync(path.join(root(), tenant))) out.push({ tenant, workspace });
|
|
98
|
+
} catch {
|
|
99
|
+
// not a directory
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
},
|
|
104
|
+
entries(tenant, workspace) {
|
|
105
|
+
const dir = dirOf(tenant, workspace);
|
|
106
|
+
let names: string[] = [];
|
|
107
|
+
try {
|
|
108
|
+
names = fs.readdirSync(dir).filter((n) => n.endsWith(".json")).sort();
|
|
109
|
+
} catch {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
const out: { handle: string; ageMs: number }[] = [];
|
|
113
|
+
for (const n of names) {
|
|
114
|
+
const file = path.join(dir, n);
|
|
115
|
+
try {
|
|
116
|
+
out.push({ handle: file, ageMs: Date.now() - fs.statSync(file).mtimeMs });
|
|
117
|
+
} catch {
|
|
118
|
+
// gone meanwhile
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
},
|
|
123
|
+
read(_t, _w, handle) {
|
|
124
|
+
return JSON.parse(fs.readFileSync(handle, "utf8")) as JournalRecord;
|
|
125
|
+
},
|
|
126
|
+
setAside(_t, _w, handle) {
|
|
127
|
+
fs.renameSync(handle, `${handle}.unreadable`);
|
|
128
|
+
},
|
|
129
|
+
sweep(tenant, workspace, olderThanMs) {
|
|
130
|
+
const dir = dirOf(tenant, workspace);
|
|
131
|
+
try {
|
|
132
|
+
for (const n of fs.readdirSync(dir)) {
|
|
133
|
+
// a write that died before its rename leaves a .tmp: nothing to replay
|
|
134
|
+
if (n.endsWith(".json.tmp") && olderThanMs > 0 && Date.now() - fs.statSync(path.join(dir, n)).mtimeMs > olderThanMs) fs.rmSync(path.join(dir, n), { force: true });
|
|
135
|
+
}
|
|
136
|
+
} catch {
|
|
137
|
+
// nothing there
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
drop(tenant, workspace) {
|
|
141
|
+
fs.rmSync(workspace === undefined ? path.join(root(), tenant) : dirOf(tenant, workspace), { recursive: true, force: true });
|
|
142
|
+
},
|
|
143
|
+
move(tenant, from, to) {
|
|
144
|
+
const src = dirOf(tenant, from);
|
|
145
|
+
if (!fs.existsSync(src)) return;
|
|
146
|
+
const dest = dirOf(tenant, to);
|
|
147
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
148
|
+
for (const n of fs.readdirSync(src)) fs.renameSync(path.join(src, n), path.join(dest, n));
|
|
149
|
+
fs.rmSync(src, { recursive: true, force: true });
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|