@foldrun/core 23.0.3 → 23.1.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.
@@ -61,7 +61,7 @@ import {
61
61
  type Manifest,
62
62
  type ManifestEntry,
63
63
  } from "./manifest.ts";
64
- import { mirrorOn, localDisk, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces, type MirrorBase } from "./mirror-base.ts";
64
+ import { mirrorOn, localDisk, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces, headsSeen, noteHeadsSeen, dropHeadsSeen, type MirrorBase } from "./mirror-base.ts";
65
65
  import { headsOf, loadManifest, manifestsAt, manifestsOf, publish, restore, requestDigest, stateOpId, BlobMissing, PublishError, type Deps, type Heads, type PublishResult } from "./publish.ts";
66
66
  import { OpIdReused, type Revision, type WorkspaceRef } from "./revision-store.ts";
67
67
  import { pruneStateRevisions, type StateRetention } from "./state-retention.ts";
@@ -665,6 +665,7 @@ export async function dropWorkspaceRevisions(tenant: string, workspace: string,
665
665
  await dropWorkspaceOutputs(tenant, workspace);
666
666
  marked.delete(k);
667
667
  verified.delete(k);
668
+ dropHeadsSeen(tenant, workspace); // a new workspace of this name starts unseen
668
669
  } finally {
669
670
  lifecycleBusy.delete(k);
670
671
  release();
@@ -735,6 +736,7 @@ export async function moveWorkspaceRevisions(tenant: string, from: string, to: s
735
736
  }
736
737
  }
737
738
  rekeyQueue(tenant, from, to);
739
+ dropHeadsSeen(tenant, from);
738
740
  if (rename) {
739
741
  // A commit on another pod that passed its guard between the move and the
740
742
  // folder rename left rows under the OLD name; its content is in the
@@ -1235,6 +1237,24 @@ async function baseManifests(deps: Deps, tenant: string, workspace: string, base
1235
1237
  return out;
1236
1238
  }
1237
1239
 
1240
+ /** On a pod's own disk (the mirror, or FOLDRUN_DATA_LOCAL) a folder with no
1241
+ * heads is either a workspace made here (no first revision yet) or the stale
1242
+ * copy of one deleted elsewhere. The marker tells them apart: once this pod
1243
+ * has seen heads for the name, their absence is a delete, and the folder is
1244
+ * not published as a first revision. The sync removes it, as it always did. */
1245
+ function isStale(tenant: string, workspace: string): boolean {
1246
+ return (mirrorOn() || localDisk()) && headsSeen(tenant, workspace);
1247
+ }
1248
+ function noteHeads(tenant: string, workspace: string, heads: Heads): void {
1249
+ if (heads.workspace || heads.state) noteHeadsSeen(tenant, workspace);
1250
+ }
1251
+ function staleFolder(tenant: string, workspace: string, heads: Heads): void {
1252
+ if (!heads.workspace && !heads.state && isStale(tenant, workspace)) {
1253
+ throw new PublishError(`${workspace} was deleted elsewhere; this pod's folder is a stale copy`, "gone");
1254
+ }
1255
+ noteHeads(tenant, workspace, heads);
1256
+ }
1257
+
1238
1258
  /** One journal entry → one publish. The first commit of a workspace is its
1239
1259
  * whole tree. */
1240
1260
  async function commitEntry(deps: Deps, tenant: string, workspace: string, entry: Entry): Promise<PublishResult | null> {
@@ -1246,6 +1266,7 @@ async function commitEntry(deps: Deps, tenant: string, workspace: string, entry:
1246
1266
  let releaseLarge: () => Promise<void> = async () => {};
1247
1267
  const changes: Changes = {};
1248
1268
  const baseline: { authored: boolean; state: boolean } = { authored: !heads.workspace, state: !heads.state };
1269
+ staleFolder(tenant, workspace, heads);
1249
1270
 
1250
1271
  // A run's copy-back is pinned to the revisions its stage-in started from: a
1251
1272
  // path's base is what THAT revision held, so a head that moved since (another
@@ -1410,6 +1431,8 @@ async function reconcileNow(tenant: string, workspace: string, by = "reconcile")
1410
1431
  // would diff a stale copy against a newer head, and with that head as each
1411
1432
  // path's base the publish would undo the newer write without a conflict.
1412
1433
  const heads = await headsOf(deps, ws);
1434
+ if (!heads.workspace && !heads.state && isStale(tenant, workspace)) return null;
1435
+ noteHeads(tenant, workspace, heads);
1413
1436
  const scan = await scanFolder(tenant, workspace);
1414
1437
  if (!scan) return null;
1415
1438
  const head = await manifestsAt(deps, tenant, heads);
@@ -1471,6 +1494,7 @@ async function reconcileNow(tenant: string, workspace: string, by = "reconcile")
1471
1494
  * conflict copy beside it), so this pod takes the head's version too — unless
1472
1495
  * the file changed here again since. Returns the result unchanged. */
1473
1496
  async function afterPublish(deps: Deps, ws: WorkspaceRef, changes: Changes, r: PublishResult): Promise<PublishResult> {
1497
+ noteHeadsSeen(ws.tenant, ws.workspace); // it has heads now, whether or not the mirror is on
1474
1498
  if (!mirrorOn()) return r;
1475
1499
  try {
1476
1500
  const { tenant, workspace } = ws;
@@ -1600,6 +1624,7 @@ async function syncNow(tenant: string, workspace: string): Promise<"unchanged" |
1600
1624
  if (workspace !== "@library") notifyWorkspaceChanged(tenant, workspace, "delete");
1601
1625
  return "removed";
1602
1626
  }
1627
+ noteHeadsSeen(tenant, workspace);
1603
1628
  const ids = { workspaceHead: heads.workspace?.id ?? null, stateHead: heads.state?.id ?? null };
1604
1629
  if (base && base.workspaceHead === ids.workspaceHead && base.stateHead === ids.stateHead && fs.existsSync(dir)) return "unchanged";
1605
1630
  const m = await manifestsAt(deps, tenant, heads);
@@ -1756,6 +1781,43 @@ function throughLink(dir: string, rel: string): boolean {
1756
1781
 
1757
1782
  /** Every workspace with heads, and every copy here whose heads are gone. One
1758
1783
  * failure does not stop the rest. */
1784
+ /**
1785
+ * One account's folders brought level with the heads, now — for a request
1786
+ * that must see what another pod just wrote (14 Nov 2026: a workspace made
1787
+ * on one web pod answered 404 on the other for up to the mirror's poll; a
1788
+ * deleted one stayed listed ~10 s). The same per-workspace sync the loop
1789
+ * runs, for this tenant only: its heads, and its mirrored folders with no
1790
+ * head (deleted elsewhere). A failure is logged and counted, never thrown —
1791
+ * the caller answers from the copy it has.
1792
+ */
1793
+ export async function syncTenantFromHeads(tenant: string): Promise<{ synced: number; removed: number; failed: number }> {
1794
+ const deps = S.backend;
1795
+ const out = { synced: 0, removed: 0, failed: 0 };
1796
+ if (!deps?.revisions.allHeads || !mirrorOn()) return out;
1797
+ let mine: { tenant: string; workspace: string }[];
1798
+ try {
1799
+ mine = (await deps.revisions.allHeads()).filter((h) => h.tenant === tenant);
1800
+ } catch (err) {
1801
+ // Fail open, as documented: the caller answers from the copy it has (Codex).
1802
+ opsError("revisions.mirror_sync_failed", err, { tenant }, "warn");
1803
+ out.failed++;
1804
+ return out;
1805
+ }
1806
+ const seen = new Set(mine.map((h) => h.workspace));
1807
+ const todo = [...mine.map((h) => h.workspace), ...mirroredWorkspaces().filter((w) => w.tenant === tenant && !seen.has(w.workspace)).map((w) => w.workspace)];
1808
+ for (const workspace of todo) {
1809
+ try {
1810
+ const r = await syncFromHeads(tenant, workspace);
1811
+ if (r === "synced") out.synced++;
1812
+ else if (r === "removed") out.removed++;
1813
+ } catch (err) {
1814
+ out.failed++;
1815
+ opsError("revisions.mirror_sync_failed", err, { tenant, workspace }, "warn");
1816
+ }
1817
+ }
1818
+ return out;
1819
+ }
1820
+
1759
1821
  export async function syncAllFromHeads(): Promise<{ synced: number; removed: number; failed: number }> {
1760
1822
  const deps = S.backend;
1761
1823
  const out = { synced: 0, removed: 0, failed: 0 };
@@ -1776,6 +1838,24 @@ export async function syncAllFromHeads(): Promise<{ synced: number; removed: num
1776
1838
  return out;
1777
1839
  }
1778
1840
 
1841
+ /** Every account that still has revision state: a head in the store, a journal
1842
+ * entry in any journal store, or changes queued in this process. A pod that is
1843
+ * about to remove an account's folder asks first — such an account has work
1844
+ * not yet in a head, or a history somebody can still read. Throws when a store
1845
+ * cannot be read: the caller must then remove nothing. */
1846
+ export async function tenantsWithRevisionState(): Promise<Set<string>> {
1847
+ const out = new Set<string>();
1848
+ const deps = S.backend;
1849
+ if (deps) {
1850
+ if (!deps.revisions.allHeads) throw new Error("revisions: the store cannot list its heads");
1851
+ for (const h of await deps.revisions.allHeads()) out.add(h.tenant);
1852
+ }
1853
+ for (const st of journalStores()) for (const w of st.workspaces()) out.add(w.tenant);
1854
+ for (const [k, q] of queues) if (q.length) out.add(k.split("\0")[0]);
1855
+ for (const k of marked) out.add(k.split("\0")[0]);
1856
+ return out;
1857
+ }
1858
+
1779
1859
  // ------------------------------------------------------------- reading
1780
1860
 
1781
1861
  async function blobBytes(deps: Deps, tenant: string, sha: string): Promise<Buffer | null> {