@foldrun/core 22.3.0 → 22.4.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.
@@ -152,6 +152,9 @@ export interface RevisionView {
152
152
  /** One revision by id, looked up rather than listed (paths []); null when
153
153
  * the store has none. Optional: a view without it is asked through `list`. */
154
154
  get?(tenant: string, scope: string, id: string): Promise<RevisionSummary | null>;
155
+ /** Whether the store serves history right now (its journal and reads on).
156
+ * A view without it is taken as serving. */
157
+ on?(): boolean;
155
158
  }
156
159
  export declare function registerRevisionView(v: RevisionView | null): void;
157
160
  export declare function listRevisions(tenant: string, scope: string, opts?: {
@@ -38,6 +38,7 @@ import { processState } from "./process-state.js";
38
38
  import path from "node:path";
39
39
  import crypto from "node:crypto";
40
40
  import { dataRoot } from "./paths.js";
41
+ import { mirrorOn } from "./mirror-base.js";
41
42
  /** What is registered here is one per PROCESS (process-state.ts): the boot
42
43
  * registers the journal and the view, and a route handler compiled apart from
43
44
  * it must see them — until 13 Nov 2026 it did not, and a dashboard save was
@@ -132,6 +133,24 @@ export function recordRevision(tenant, scope, files, meta = {}) {
132
133
  const changed = files.filter((f) => f.before !== f.after);
133
134
  if (changed.length === 0)
134
135
  return null;
136
+ // A pod whose folders are a copy of the heads (revisions.mirror): the
137
+ // revisions store is the history, and this disk is a cache that goes with
138
+ // the pod. A local record and snapshot per save only filled it (FOL-126,
139
+ // 13 Nov 2026) — nothing reads one that is newer than the store's rows.
140
+ if (mirrorOn() && storeServes()) {
141
+ if (meta.commit)
142
+ assertCommitId(meta.commit);
143
+ const at = new Date().toISOString();
144
+ const by = meta.by ?? "system";
145
+ const message = meta.message ?? describe(changed);
146
+ try {
147
+ REG.journal(tenant, scope, changed, { by, message, at });
148
+ }
149
+ catch {
150
+ // the revision store is not allowed to break a save; workspace-revisions logs and retries
151
+ }
152
+ return { id: meta.id ?? newRevisionId(), at, by, message, commit: meta.commit ?? null, files: changed.map((f) => ({ path: f.path, before: clip(f.before), after: clip(f.after) })) };
153
+ }
135
154
  const dir = historyDir(tenant, scope);
136
155
  fs.mkdirSync(dir, { recursive: true });
137
156
  // A commit is the revision's id only the first time it is recorded. A
@@ -467,16 +486,70 @@ function changedIn(revs, from, to) {
467
486
  export function latestRevisionIdLocal(tenant, scope) {
468
487
  return listRevisionsLocal(tenant, scope, { limit: 1 })[0]?.id ?? null;
469
488
  }
489
+ /** The store is the history: registered, journalling and read (Codex review). */
490
+ function storeServes() {
491
+ if (!REG.view || !REG.journal)
492
+ return false;
493
+ try {
494
+ return REG.view.on ? REG.view.on() === true : true;
495
+ }
496
+ catch {
497
+ return false;
498
+ }
499
+ }
470
500
  export function registerRevisionView(v) {
471
501
  REG.view = v;
472
502
  }
503
+ // The account shelf ("@library") in the store: its folder is the account's,
504
+ // so its revisions name `library/<kind>/<file>` and the account's AGENTS.md
505
+ // beside them; its own history names `<kind>/<file>` (library.ts). Read
506
+ // through this, the shelf's history is the store's like any workspace's
507
+ // (FOL-126, 13 Nov 2026 — until then it was each pod's own .history, which
508
+ // a pod on its own disk does not keep). AGENTS.md is not on the shelf: it is
509
+ // left out, and a revision that changed only it is not a shelf revision.
510
+ const SHELF = "library/";
511
+ const onShelf = (p) => p.startsWith(SHELF);
512
+ const shelfPath = (p) => p.slice(SHELF.length);
513
+ function viewFor(scope) {
514
+ const v = REG.view;
515
+ if (!v || scope !== "@library")
516
+ return v;
517
+ return {
518
+ async list(tenant, s, opts) {
519
+ const want = opts.limit ?? 200;
520
+ const withPaths = opts.paths !== false || !!opts.forPath;
521
+ // A revision that changed only the account's AGENTS.md is dropped
522
+ // after the store's limit was spent on it: ask for more until `want`
523
+ // shelf revisions are found or history ends (Codex review).
524
+ for (let ask = want;; ask = Math.min(ask * 4, Number.MAX_SAFE_INTEGER)) {
525
+ const rows = await v.list(tenant, s, { ...opts, limit: ask, ...(opts.forPath ? { forPath: SHELF + opts.forPath } : {}) });
526
+ if (!rows)
527
+ return null;
528
+ const mine = rows.map((r) => ({ ...r, paths: r.paths.filter(onShelf).map(shelfPath) })).filter((r) => !withPaths || r.paths.length > 0);
529
+ if (mine.length >= want || rows.length < ask || !Number.isFinite(want) || ask >= Number.MAX_SAFE_INTEGER)
530
+ return mine.slice(0, want);
531
+ }
532
+ },
533
+ async read(tenant, s, id, forPath) {
534
+ const r = await v.read(tenant, s, id, forPath === undefined ? undefined : SHELF + forPath);
535
+ return r && { ...r, files: r.files.filter((f) => onShelf(f.path)).map((f) => ({ ...f, path: shelfPath(f.path) })) };
536
+ },
537
+ async snapshot(tenant, s, id) {
538
+ const files = await v.snapshot(tenant, s, id);
539
+ return files && files.filter((f) => onShelf(f.path)).map((f) => ({ ...f, path: shelfPath(f.path) }));
540
+ },
541
+ ...(v.get ? { get: (tenant, s, id) => v.get(tenant, s, id) } : {}),
542
+ ...(v.on ? { on: () => v.on() } : {}),
543
+ };
544
+ }
473
545
  /** Revisions first, then the local entries older than the oldest of them.
474
546
  * `stored` is which rows came from the store (every one a restore point). */
475
547
  async function combinedRows(tenant, scope, opts, restorableOnly = false) {
476
548
  const limit = opts.limit ?? 200;
477
549
  // The whole of `limit`, every page of it: the store's first page is not all
478
550
  // of history (FOL-152).
479
- const mine = REG.view && scope !== "@library" ? await REG.view.list(tenant, scope, { ...opts, limit }) : null;
551
+ const view = viewFor(scope);
552
+ const mine = view ? await view.list(tenant, scope, { ...opts, limit }) : null;
480
553
  let local = listRevisionsLocal(tenant, scope, { forPath: opts.forPath, limit: Infinity });
481
554
  if (restorableOnly) {
482
555
  const have = snapshotIds(tenant, scope);
@@ -505,25 +578,28 @@ export async function listRevisionsRestorable(tenant, scope, opts = {}) {
505
578
  }
506
579
  export async function readRevision(tenant, scope, id, forPath) {
507
580
  assertSafeName(id, "revision id");
508
- const fromStore = REG.view && scope !== "@library" ? await REG.view.read(tenant, scope, id, forPath) : null;
581
+ const view = viewFor(scope);
582
+ const fromStore = view ? await view.read(tenant, scope, id, forPath) : null;
509
583
  return fromStore ?? readRevisionLocal(tenant, scope, id, forPath);
510
584
  }
511
585
  export async function snapshotAt(tenant, scope, id) {
512
586
  assertSafeName(id, "revision id");
513
- const fromStore = REG.view && scope !== "@library" ? await REG.view.snapshot(tenant, scope, id) : null;
587
+ const view = viewFor(scope);
588
+ const fromStore = view ? await view.snapshot(tenant, scope, id) : null;
514
589
  return fromStore ?? snapshotAtLocal(tenant, scope, id);
515
590
  }
516
591
  export async function hasSnapshot(tenant, scope, id) {
517
592
  assertSafeName(id, "revision id");
518
593
  if (hasSnapshotLocal(tenant, scope, id))
519
594
  return true;
520
- if (!REG.view || scope === "@library")
595
+ const view = viewFor(scope);
596
+ if (!view)
521
597
  return false;
522
598
  // Looked up by id: a listing stops at a page, and a revision older than it
523
599
  // was "not restorable" until 9 Nov 2026 (FOL-152).
524
- if (REG.view.get)
525
- return (await REG.view.get(tenant, scope, id)) !== null;
526
- return Boolean((await REG.view.list(tenant, scope, { limit: Infinity, paths: false }))?.some((r) => r.id === id));
600
+ if (view.get)
601
+ return (await view.get(tenant, scope, id)) !== null;
602
+ return Boolean((await view.list(tenant, scope, { limit: Infinity, paths: false }))?.some((r) => r.id === id));
527
603
  }
528
604
  export async function headRevision(tenant, scope) {
529
605
  return (await combined(tenant, scope, { limit: 1 }, true))[0] ?? null;
@@ -533,8 +609,9 @@ export async function resolveRevision(tenant, scope, to, now = Date.now()) {
533
609
  // An id in the store is one lookup; anything else (a date, a local id, a
534
610
  // commit) is answered from ALL of history — every page of the store, without
535
611
  // the changed paths, then the local entries.
536
- if ("id" in want && REG.view?.get && scope !== "@library") {
537
- const hit = await REG.view.get(tenant, scope, want.id);
612
+ const view = viewFor(scope);
613
+ if ("id" in want && view?.get) {
614
+ const hit = await view.get(tenant, scope, want.id);
538
615
  if (hit)
539
616
  return { id: hit.id, at: hit.at, by: hit.by, message: hit.message };
540
617
  }
@@ -553,6 +630,10 @@ export async function latestRevisionId(tenant, scope) {
553
630
  /** A scope with no revision at all gets a local baseline, as before. */
554
631
  export async function ensureBaseline(tenant, scope, by = "foldrun") {
555
632
  const head = await headRevision(tenant, scope);
633
+ // A mirrored pod's history is the store's: a baseline on its own disk
634
+ // would be gone with the pod (FOL-126).
635
+ if (!head && mirrorOn() && storeServes())
636
+ return null;
556
637
  return head ? head.id : ensureBaselineLocal(tenant, scope, by);
557
638
  }
558
639
  /**
@@ -1917,6 +1917,9 @@ const view = {
1917
1917
  read: (t, w, id, p) => guarded("read", t, w, () => readRevisionDetail(t, w, id, p)),
1918
1918
  snapshot: (t, w, id) => guarded("snapshot", t, w, () => snapshotFiles(t, w, id)),
1919
1919
  get: (t, w, id) => guarded("get", t, w, () => revisionSummary(t, w, id)),
1920
+ // guarded() answers null whenever the journal is off; reads too, for a
1921
+ // pod that keeps no history of its own (history.ts storeServes).
1922
+ on: () => revisionReadsOn(),
1920
1923
  };
1921
1924
  // ------------------------------------------------------------- reads through heads
1922
1925
  /** One workspace file, from its head when reads through revisions are on and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "22.3.0",
3
+ "version": "22.4.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/history.ts CHANGED
@@ -39,6 +39,7 @@ import { processState } from "./process-state.ts";
39
39
  import path from "node:path";
40
40
  import crypto from "node:crypto";
41
41
  import { dataRoot } from "./paths.ts";
42
+ import { mirrorOn } from "./mirror-base.ts";
42
43
 
43
44
  /**
44
45
  * The current tree of a scope, so the first commit can be a full import.
@@ -203,6 +204,23 @@ export function recordRevision(
203
204
  const changed = files.filter((f) => f.before !== f.after);
204
205
  if (changed.length === 0) return null;
205
206
 
207
+ // A pod whose folders are a copy of the heads (revisions.mirror): the
208
+ // revisions store is the history, and this disk is a cache that goes with
209
+ // the pod. A local record and snapshot per save only filled it (FOL-126,
210
+ // 13 Nov 2026) — nothing reads one that is newer than the store's rows.
211
+ if (mirrorOn() && storeServes()) {
212
+ if (meta.commit) assertCommitId(meta.commit);
213
+ const at = new Date().toISOString();
214
+ const by = meta.by ?? "system";
215
+ const message = meta.message ?? describe(changed);
216
+ try {
217
+ REG.journal!(tenant, scope, changed, { by, message, at });
218
+ } catch {
219
+ // the revision store is not allowed to break a save; workspace-revisions logs and retries
220
+ }
221
+ return { id: meta.id ?? newRevisionId(), at, by, message, commit: meta.commit ?? null, files: changed.map((f) => ({ path: f.path, before: clip(f.before), after: clip(f.after) })) };
222
+ }
223
+
206
224
  const dir = historyDir(tenant, scope);
207
225
  fs.mkdirSync(dir, { recursive: true });
208
226
  // A commit is the revision's id only the first time it is recorded. A
@@ -559,7 +577,7 @@ export function latestRevisionIdLocal(tenant: string, scope: string): string | n
559
577
  // install with the journal off) or a workspace has no revisions yet, the
560
578
  // local `.history/` answers, exactly as before; where both exist the list is
561
579
  // the revisions followed by the older local entries, so turning revisions on
562
- // loses no history. The account shelf ("@library") is never in revisions.
580
+ // loses no history. The account shelf ("@library") is read through viewFor.
563
581
 
564
582
  export interface RevisionView {
565
583
  /** Newest first, or null when revisions do not serve this scope. Every page
@@ -571,11 +589,63 @@ export interface RevisionView {
571
589
  /** One revision by id, looked up rather than listed (paths []); null when
572
590
  * the store has none. Optional: a view without it is asked through `list`. */
573
591
  get?(tenant: string, scope: string, id: string): Promise<RevisionSummary | null>;
592
+ /** Whether the store serves history right now (its journal and reads on).
593
+ * A view without it is taken as serving. */
594
+ on?(): boolean;
595
+ }
596
+ /** The store is the history: registered, journalling and read (Codex review). */
597
+ function storeServes(): boolean {
598
+ if (!REG.view || !REG.journal) return false;
599
+ try {
600
+ return REG.view.on ? REG.view.on() === true : true;
601
+ } catch {
602
+ return false;
603
+ }
574
604
  }
575
605
  export function registerRevisionView(v: RevisionView | null) {
576
606
  REG.view = v;
577
607
  }
578
608
 
609
+ // The account shelf ("@library") in the store: its folder is the account's,
610
+ // so its revisions name `library/<kind>/<file>` and the account's AGENTS.md
611
+ // beside them; its own history names `<kind>/<file>` (library.ts). Read
612
+ // through this, the shelf's history is the store's like any workspace's
613
+ // (FOL-126, 13 Nov 2026 — until then it was each pod's own .history, which
614
+ // a pod on its own disk does not keep). AGENTS.md is not on the shelf: it is
615
+ // left out, and a revision that changed only it is not a shelf revision.
616
+ const SHELF = "library/";
617
+ const onShelf = (p: string) => p.startsWith(SHELF);
618
+ const shelfPath = (p: string) => p.slice(SHELF.length);
619
+ function viewFor(scope: string): RevisionView | null {
620
+ const v = REG.view;
621
+ if (!v || scope !== "@library") return v;
622
+ return {
623
+ async list(tenant, s, opts) {
624
+ const want = opts.limit ?? 200;
625
+ const withPaths = opts.paths !== false || !!opts.forPath;
626
+ // A revision that changed only the account's AGENTS.md is dropped
627
+ // after the store's limit was spent on it: ask for more until `want`
628
+ // shelf revisions are found or history ends (Codex review).
629
+ for (let ask = want; ; ask = Math.min(ask * 4, Number.MAX_SAFE_INTEGER)) {
630
+ const rows = await v.list(tenant, s, { ...opts, limit: ask, ...(opts.forPath ? { forPath: SHELF + opts.forPath } : {}) });
631
+ if (!rows) return null;
632
+ const mine = rows.map((r) => ({ ...r, paths: r.paths.filter(onShelf).map(shelfPath) })).filter((r) => !withPaths || r.paths.length > 0);
633
+ if (mine.length >= want || rows.length < ask || !Number.isFinite(want) || ask >= Number.MAX_SAFE_INTEGER) return mine.slice(0, want);
634
+ }
635
+ },
636
+ async read(tenant, s, id, forPath) {
637
+ const r = await v.read(tenant, s, id, forPath === undefined ? undefined : SHELF + forPath);
638
+ return r && { ...r, files: r.files.filter((f) => onShelf(f.path)).map((f) => ({ ...f, path: shelfPath(f.path) })) };
639
+ },
640
+ async snapshot(tenant, s, id) {
641
+ const files = await v.snapshot(tenant, s, id);
642
+ return files && files.filter((f) => onShelf(f.path)).map((f) => ({ ...f, path: shelfPath(f.path) }));
643
+ },
644
+ ...(v.get ? { get: (tenant: string, s: string, id: string) => v.get!(tenant, s, id) } : {}),
645
+ ...(v.on ? { on: () => v.on!() } : {}),
646
+ };
647
+ }
648
+
579
649
  /** Revisions first, then the local entries older than the oldest of them.
580
650
  * `stored` is which rows came from the store (every one a restore point). */
581
651
  async function combinedRows(
@@ -587,7 +657,8 @@ async function combinedRows(
587
657
  const limit = opts.limit ?? 200;
588
658
  // The whole of `limit`, every page of it: the store's first page is not all
589
659
  // of history (FOL-152).
590
- const mine = REG.view && scope !== "@library" ? await REG.view.list(tenant, scope, { ...opts, limit }) : null;
660
+ const view = viewFor(scope);
661
+ const mine = view ? await view.list(tenant, scope, { ...opts, limit }) : null;
591
662
  let local = listRevisionsLocal(tenant, scope, { forPath: opts.forPath, limit: Infinity });
592
663
  if (restorableOnly) {
593
664
  const have = snapshotIds(tenant, scope);
@@ -618,24 +689,27 @@ export async function listRevisionsRestorable(tenant: string, scope: string, opt
618
689
 
619
690
  export async function readRevision(tenant: string, scope: string, id: string, forPath?: string): Promise<Revision | null> {
620
691
  assertSafeName(id, "revision id");
621
- const fromStore = REG.view && scope !== "@library" ? await REG.view.read(tenant, scope, id, forPath) : null;
692
+ const view = viewFor(scope);
693
+ const fromStore = view ? await view.read(tenant, scope, id, forPath) : null;
622
694
  return fromStore ?? readRevisionLocal(tenant, scope, id, forPath);
623
695
  }
624
696
 
625
697
  export async function snapshotAt(tenant: string, scope: string, id: string): Promise<{ path: string; content: string }[] | null> {
626
698
  assertSafeName(id, "revision id");
627
- const fromStore = REG.view && scope !== "@library" ? await REG.view.snapshot(tenant, scope, id) : null;
699
+ const view = viewFor(scope);
700
+ const fromStore = view ? await view.snapshot(tenant, scope, id) : null;
628
701
  return fromStore ?? snapshotAtLocal(tenant, scope, id);
629
702
  }
630
703
 
631
704
  export async function hasSnapshot(tenant: string, scope: string, id: string): Promise<boolean> {
632
705
  assertSafeName(id, "revision id");
633
706
  if (hasSnapshotLocal(tenant, scope, id)) return true;
634
- if (!REG.view || scope === "@library") return false;
707
+ const view = viewFor(scope);
708
+ if (!view) return false;
635
709
  // Looked up by id: a listing stops at a page, and a revision older than it
636
710
  // was "not restorable" until 9 Nov 2026 (FOL-152).
637
- if (REG.view.get) return (await REG.view.get(tenant, scope, id)) !== null;
638
- return Boolean((await REG.view.list(tenant, scope, { limit: Infinity, paths: false }))?.some((r) => r.id === id));
711
+ if (view.get) return (await view.get(tenant, scope, id)) !== null;
712
+ return Boolean((await view.list(tenant, scope, { limit: Infinity, paths: false }))?.some((r) => r.id === id));
639
713
  }
640
714
 
641
715
  export async function headRevision(tenant: string, scope: string): Promise<RevisionSummary | null> {
@@ -647,8 +721,9 @@ export async function resolveRevision(tenant: string, scope: string, to: string,
647
721
  // An id in the store is one lookup; anything else (a date, a local id, a
648
722
  // commit) is answered from ALL of history — every page of the store, without
649
723
  // the changed paths, then the local entries.
650
- if ("id" in want && REG.view?.get && scope !== "@library") {
651
- const hit = await REG.view.get(tenant, scope, want.id);
724
+ const view = viewFor(scope);
725
+ if ("id" in want && view?.get) {
726
+ const hit = await view.get(tenant, scope, want.id);
652
727
  if (hit) return { id: hit.id, at: hit.at, by: hit.by, message: hit.message };
653
728
  }
654
729
  return pick(await combined(tenant, scope, { limit: Infinity, paths: false }, true), want);
@@ -667,6 +742,9 @@ export async function latestRevisionId(tenant: string, scope: string): Promise<s
667
742
  /** A scope with no revision at all gets a local baseline, as before. */
668
743
  export async function ensureBaseline(tenant: string, scope: string, by = "foldrun"): Promise<string | null> {
669
744
  const head = await headRevision(tenant, scope);
745
+ // A mirrored pod's history is the store's: a baseline on its own disk
746
+ // would be gone with the pod (FOL-126).
747
+ if (!head && mirrorOn() && storeServes()) return null;
670
748
  return head ? head.id : ensureBaselineLocal(tenant, scope, by);
671
749
  }
672
750
 
@@ -1922,6 +1922,9 @@ const view: RevisionView = {
1922
1922
  read: (t, w, id, p) => guarded("read", t, w, () => readRevisionDetail(t, w, id, p)),
1923
1923
  snapshot: (t, w, id) => guarded("snapshot", t, w, () => snapshotFiles(t, w, id)),
1924
1924
  get: (t, w, id) => guarded("get", t, w, () => revisionSummary(t, w, id)),
1925
+ // guarded() answers null whenever the journal is off; reads too, for a
1926
+ // pod that keeps no history of its own (history.ts storeServes).
1927
+ on: () => revisionReadsOn(),
1925
1928
  };
1926
1929
 
1927
1930
  // ------------------------------------------------------------- reads through heads