@foldrun/core 23.0.1 → 23.0.3

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.
@@ -14,6 +14,11 @@ export declare const CAPTURE_SLACK = 4096;
14
14
  * secret would redact half the language, and anything that short is not one.
15
15
  */
16
16
  export declare function redactionsFor(secrets: Record<string, string | undefined>): [string, string][];
17
+ /** A value's encoded forms, each long enough to be its own and nothing else's
18
+ * (12 characters or more). Base64: the characters that come from the value's
19
+ * bytes alone at each alignment, so the form matches inside a longer
20
+ * encoding whatever came before or after the value. */
21
+ export declare function encodedForms(v: string): string[];
17
22
  /** Masked, then cut to the cap. */
18
23
  export declare function maskCall(text: string, redactSecrets: (text: string) => string): {
19
24
  text: string;
@@ -23,10 +23,21 @@ export const CAPTURE_SLACK = 4096;
23
23
  */
24
24
  export function redactionsFor(secrets) {
25
25
  const out = new Map();
26
- const add = (v, name) => {
26
+ const put = (v, name) => {
27
27
  if (v.length >= 8 && !out.has(v))
28
28
  out.set(v, name);
29
29
  };
30
+ // Every value also as a tool prints it encoded (Kousha's review, 14 Nov
31
+ // 2026): hex, URL-encoded, and base64 — standard and URL-safe, at each of
32
+ // the three byte alignments it can take inside a longer encoded text
33
+ // (`base64 <<< "user:$TOKEN"`, a JWT segment, a Basic header the agent built).
34
+ const add = (v, name) => {
35
+ if (v.length < 8)
36
+ return;
37
+ put(v, name);
38
+ for (const e of encodedForms(v))
39
+ put(e, name);
40
+ };
30
41
  for (const [name, raw] of Object.entries(secrets)) {
31
42
  if (typeof raw !== "string")
32
43
  continue;
@@ -85,6 +96,31 @@ export function redactionsFor(secrets) {
85
96
  }
86
97
  return [...out.entries()].sort((a, b) => b[0].length - a[0].length);
87
98
  }
99
+ /** A value's encoded forms, each long enough to be its own and nothing else's
100
+ * (12 characters or more). Base64: the characters that come from the value's
101
+ * bytes alone at each alignment, so the form matches inside a longer
102
+ * encoding whatever came before or after the value. */
103
+ export function encodedForms(v) {
104
+ const bytes = Buffer.from(v, "utf8");
105
+ const forms = new Set([bytes.toString("hex"), bytes.toString("hex").toUpperCase()]);
106
+ try {
107
+ forms.add(encodeURIComponent(v));
108
+ }
109
+ catch {
110
+ // a lone surrogate: no URL form
111
+ }
112
+ for (let pad = 0; pad < 3; pad++) {
113
+ const b64 = Buffer.concat([Buffer.alloc(pad), bytes]).toString("base64").replace(/=+$/, "");
114
+ // character i holds bits [6i, 6i + 6); the value's are [8·pad, 8·(pad + n))
115
+ const first = Math.ceil((8 * pad) / 6);
116
+ const last = Math.floor((8 * (pad + bytes.length)) / 6); // exclusive
117
+ const part = b64.slice(first, last);
118
+ forms.add(part);
119
+ forms.add(part.replace(/\+/g, "-").replace(/\//g, "_"));
120
+ }
121
+ forms.delete(v);
122
+ return [...forms].filter((f) => f.length >= 12);
123
+ }
88
124
  // A private key's whole block — scan.ts names a key header and leaves the
89
125
  // body, which is right for a finding and wrong for a stored output.
90
126
  // Unterminated too: a key cut short, or printed without its END line, is
@@ -13,6 +13,13 @@ export interface MirrorBase {
13
13
  * (`revisions.mirror`). Off on a laptop, in the CLI and until switched on. */
14
14
  export declare function setMirrorGate(fn: (() => boolean) | null): void;
15
15
  export declare function mirrorOn(): boolean;
16
+ /** The platform says whether this pod's data root is its own disk
17
+ * (FOLDRUN_DATA_LOCAL=1, an emptyDir) rather than a volume every pod shares.
18
+ * On such a disk a folder that is missing or older than the heads says
19
+ * nothing about the workspace: it may simply not be synced here, or (mirror
20
+ * off) never will be. Off on a laptop, in the CLI and on the shared volume. */
21
+ export declare function setLocalDiskGate(fn: (() => boolean) | null): void;
22
+ export declare function localDisk(): boolean;
16
23
  export declare const mirrorBaseFile: (tenant: string, workspace: string) => string | null;
17
24
  export declare function readMirrorBase(tenant: string, workspace: string): MirrorBase | null;
18
25
  export declare function writeMirrorBase(tenant: string, workspace: string, base: MirrorBase): void;
@@ -26,6 +26,23 @@ export function mirrorOn() {
26
26
  return false;
27
27
  }
28
28
  }
29
+ const LOCAL = processState("local-disk-gate", () => ({ on: (() => false) }));
30
+ /** The platform says whether this pod's data root is its own disk
31
+ * (FOLDRUN_DATA_LOCAL=1, an emptyDir) rather than a volume every pod shares.
32
+ * On such a disk a folder that is missing or older than the heads says
33
+ * nothing about the workspace: it may simply not be synced here, or (mirror
34
+ * off) never will be. Off on a laptop, in the CLI and on the shared volume. */
35
+ export function setLocalDiskGate(fn) {
36
+ LOCAL.on = fn ?? (() => false);
37
+ }
38
+ export function localDisk() {
39
+ try {
40
+ return LOCAL.on();
41
+ }
42
+ catch {
43
+ return false;
44
+ }
45
+ }
29
46
  const SAFE = /^(?:@library|[A-Za-z0-9][A-Za-z0-9_.-]{0,127})$/;
30
47
  export const mirrorBaseFile = (tenant, workspace) => SAFE.test(tenant) && SAFE.test(workspace) ? path.join(dataRoot(), ".mirror", tenant, `${workspace}.json`) : null;
31
48
  export function readMirrorBase(tenant, workspace) {
@@ -43,7 +43,7 @@ import { opsError, opsLog } from "./ops-log.js";
43
43
  import { readAll, fileSource, BlobNotFound } from "./blob-store.js";
44
44
  import { fsJournalStore } from "./journal-store.js";
45
45
  import { AUTHORED_DIRS, AUTHORED_ROOT_FILES, STATE_DIRS, assertManifestPath, blobObjectKey, diffManifests, emptyManifest, entryId, EMPTY_MANIFEST_ID, MODE_EXEC, MODE_FILE, sha256Hex, sideOf, } from "./manifest.js";
46
- import { mirrorOn, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces } from "./mirror-base.js";
46
+ import { mirrorOn, localDisk, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces } from "./mirror-base.js";
47
47
  import { headsOf, loadManifest, manifestsAt, manifestsOf, publish, restore, requestDigest, stateOpId, BlobMissing, PublishError } from "./publish.js";
48
48
  import { OpIdReused } from "./revision-store.js";
49
49
  import { pruneStateRevisions } from "./state-retention.js";
@@ -323,10 +323,15 @@ async function drainOne(k) {
323
323
  while (q && q.length && S.backend) {
324
324
  const entry = q[0];
325
325
  // The workspace was deleted while this waited (or retried): nothing to
326
- // publish, and nothing for a later boot to replay.
326
+ // publish, and nothing for a later boot to replay. On this pod's own disk
327
+ // (FOLDRUN_DATA_LOCAL=1) a missing folder is only one not synced here: the
328
+ // entry leaves this pod's queue but stays in the shared store, for a pod
329
+ // that has the folder (Kousha's review, 14 Nov 2026). A real delete takes
330
+ // the store's entries with it (dropJournal).
327
331
  if (!fs.existsSync(folderOf(tenant, workspace))) {
328
332
  q.shift();
329
- forget(tenant, workspace, entry);
333
+ if (!localDisk())
334
+ forget(tenant, workspace, entry);
330
335
  continue;
331
336
  }
332
337
  try {
@@ -341,8 +346,10 @@ async function drainOne(k) {
341
346
  if (err instanceof PublishError && err.code === "gone") {
342
347
  // The folder is gone (deleted or renamed, here or on another pod): the
343
348
  // change has nowhere to go, and nothing should be recorded under the name.
349
+ // On this pod's own disk, kept in the store as above.
344
350
  q.shift();
345
- forget(tenant, workspace, entry);
351
+ if (!localDisk())
352
+ forget(tenant, workspace, entry);
346
353
  continue;
347
354
  }
348
355
  if (permanent(err)) {
@@ -801,9 +808,12 @@ export async function cleanOrphanedHistory(marks, opts) {
801
808
  const deps = S.backend;
802
809
  if (!deps)
803
810
  return out;
804
- // Folders are a copy of the heads (revisions.mirror): a missing one says
805
- // nothing about the workspace, so nothing is taken for orphaned.
806
- if (mirrorOn())
811
+ // Folders are a copy of the heads (revisions.mirror), or this pod's own
812
+ // disk (FOLDRUN_DATA_LOCAL=1, with the mirror on or off): a missing one says
813
+ // nothing about the workspace, so nothing is taken for orphaned. Until
814
+ // 14 Nov 2026 only the mirror was asked, so turning it off on a pod with its
815
+ // own empty disk would have dropped every workspace's history (Kousha's review).
816
+ if (mirrorOn() || localDisk())
807
817
  return out;
808
818
  const root = dataRoot();
809
819
  let rootEntries = 0;
@@ -889,7 +899,11 @@ export async function recoverJournal(opts = {}) {
889
899
  const k = key(tenant, workspace);
890
900
  const q = queues.get(k) ?? [];
891
901
  const held = new Set(q.filter((e) => e.store === store).map((e) => e.handle));
892
- const gone = !fs.existsSync(folderOf(tenant, workspace));
902
+ // A folder missing from this pod's own disk (FOLDRUN_DATA_LOCAL=1) is
903
+ // one not synced here yet, not a deleted workspace: the database store
904
+ // is shared, and those entries may be another live pod's (Kousha's
905
+ // review, 14 Nov 2026). Only a shared volume's absence is believed.
906
+ const gone = !localDisk() && !fs.existsSync(folderOf(tenant, workspace));
893
907
  const found = [];
894
908
  let names = [];
895
909
  try {
@@ -903,13 +917,16 @@ export async function recoverJournal(opts = {}) {
903
917
  for (const { handle, ageMs } of names) {
904
918
  if (held.has(handle))
905
919
  continue;
920
+ // Young entries are a live process's, whatever the folder says: the
921
+ // age is asked before anything is removed (until 14 Nov 2026 a missing
922
+ // folder removed even a fresh entry).
923
+ if (minAge > 0 && ageMs < minAge)
924
+ continue;
906
925
  try {
907
926
  if (gone) {
908
927
  store.remove(tenant, workspace, handle);
909
928
  continue;
910
929
  }
911
- if (minAge > 0 && ageMs < minAge)
912
- continue;
913
930
  const e = store.read(tenant, workspace, handle);
914
931
  if ((e.v !== 1 && e.v !== 2) || !Array.isArray(e.files))
915
932
  throw new Error("not a journal entry");
@@ -1311,6 +1328,13 @@ async function reconcileNow(tenant, workspace, by = "reconcile") {
1311
1328
  // sync to say what "changed here" means.
1312
1329
  if (mirrorOn() && !readMirrorBase(tenant, workspace))
1313
1330
  return null;
1331
+ // This pod's own disk with the mirror off: nothing brings the folder to the
1332
+ // heads, so it is not the truth about anything either — an emptyDir after a
1333
+ // restart would publish every file as deleted (Kousha's review, 14 Nov 2026).
1334
+ if (!mirrorOn() && localDisk()) {
1335
+ opsLog("warn", "revisions.reconcile_skipped", { tenant, workspace, reason: "local_disk_without_mirror" });
1336
+ return null;
1337
+ }
1314
1338
  const ws = { tenant, workspace };
1315
1339
  const k = key(tenant, workspace);
1316
1340
  // The mark goes BEFORE the folder is read, so one set after the read (a run
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "23.0.1",
3
+ "version": "23.0.3",
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/capture.ts CHANGED
@@ -27,9 +27,18 @@ export const CAPTURE_SLACK = 4096;
27
27
  */
28
28
  export function redactionsFor(secrets: Record<string, string | undefined>): [string, string][] {
29
29
  const out = new Map<string, string>();
30
- const add = (v: string, name: string) => {
30
+ const put = (v: string, name: string) => {
31
31
  if (v.length >= 8 && !out.has(v)) out.set(v, name);
32
32
  };
33
+ // Every value also as a tool prints it encoded (Kousha's review, 14 Nov
34
+ // 2026): hex, URL-encoded, and base64 — standard and URL-safe, at each of
35
+ // the three byte alignments it can take inside a longer encoded text
36
+ // (`base64 <<< "user:$TOKEN"`, a JWT segment, a Basic header the agent built).
37
+ const add = (v: string, name: string) => {
38
+ if (v.length < 8) return;
39
+ put(v, name);
40
+ for (const e of encodedForms(v)) put(e, name);
41
+ };
33
42
  for (const [name, raw] of Object.entries(secrets)) {
34
43
  if (typeof raw !== "string") continue;
35
44
  // An API connection's header values are each a secret in their own right
@@ -80,6 +89,31 @@ export function redactionsFor(secrets: Record<string, string | undefined>): [str
80
89
  return [...out.entries()].sort((a, b) => b[0].length - a[0].length);
81
90
  }
82
91
 
92
+ /** A value's encoded forms, each long enough to be its own and nothing else's
93
+ * (12 characters or more). Base64: the characters that come from the value's
94
+ * bytes alone at each alignment, so the form matches inside a longer
95
+ * encoding whatever came before or after the value. */
96
+ export function encodedForms(v: string): string[] {
97
+ const bytes = Buffer.from(v, "utf8");
98
+ const forms = new Set<string>([bytes.toString("hex"), bytes.toString("hex").toUpperCase()]);
99
+ try {
100
+ forms.add(encodeURIComponent(v));
101
+ } catch {
102
+ // a lone surrogate: no URL form
103
+ }
104
+ for (let pad = 0; pad < 3; pad++) {
105
+ const b64 = Buffer.concat([Buffer.alloc(pad), bytes]).toString("base64").replace(/=+$/, "");
106
+ // character i holds bits [6i, 6i + 6); the value's are [8·pad, 8·(pad + n))
107
+ const first = Math.ceil((8 * pad) / 6);
108
+ const last = Math.floor((8 * (pad + bytes.length)) / 6); // exclusive
109
+ const part = b64.slice(first, last);
110
+ forms.add(part);
111
+ forms.add(part.replace(/\+/g, "-").replace(/\//g, "_"));
112
+ }
113
+ forms.delete(v);
114
+ return [...forms].filter((f) => f.length >= 12);
115
+ }
116
+
83
117
  // A private key's whole block — scan.ts names a key header and leaves the
84
118
  // body, which is right for a finding and wrong for a stored output.
85
119
  // Unterminated too: a key cut short, or printed without its END line, is
@@ -39,6 +39,25 @@ export function mirrorOn(): boolean {
39
39
  }
40
40
  }
41
41
 
42
+ const LOCAL = processState("local-disk-gate", () => ({ on: (() => false) as () => boolean }));
43
+
44
+ /** The platform says whether this pod's data root is its own disk
45
+ * (FOLDRUN_DATA_LOCAL=1, an emptyDir) rather than a volume every pod shares.
46
+ * On such a disk a folder that is missing or older than the heads says
47
+ * nothing about the workspace: it may simply not be synced here, or (mirror
48
+ * off) never will be. Off on a laptop, in the CLI and on the shared volume. */
49
+ export function setLocalDiskGate(fn: (() => boolean) | null): void {
50
+ LOCAL.on = fn ?? (() => false);
51
+ }
52
+
53
+ export function localDisk(): boolean {
54
+ try {
55
+ return LOCAL.on();
56
+ } catch {
57
+ return false;
58
+ }
59
+ }
60
+
42
61
  const SAFE = /^(?:@library|[A-Za-z0-9][A-Za-z0-9_.-]{0,127})$/;
43
62
  export const mirrorBaseFile = (tenant: string, workspace: string): string | null =>
44
63
  SAFE.test(tenant) && SAFE.test(workspace) ? path.join(dataRoot(), ".mirror", tenant, `${workspace}.json`) : null;
@@ -61,7 +61,7 @@ import {
61
61
  type Manifest,
62
62
  type ManifestEntry,
63
63
  } from "./manifest.ts";
64
- import { mirrorOn, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces, type MirrorBase } from "./mirror-base.ts";
64
+ import { mirrorOn, localDisk, readMirrorBase, writeMirrorBase, dropMirrorBase, mirroredWorkspaces, 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";
@@ -437,10 +437,14 @@ async function drainOne(k: string): Promise<void> {
437
437
  while (q && q.length && S.backend) {
438
438
  const entry = q[0];
439
439
  // The workspace was deleted while this waited (or retried): nothing to
440
- // publish, and nothing for a later boot to replay.
440
+ // publish, and nothing for a later boot to replay. On this pod's own disk
441
+ // (FOLDRUN_DATA_LOCAL=1) a missing folder is only one not synced here: the
442
+ // entry leaves this pod's queue but stays in the shared store, for a pod
443
+ // that has the folder (Kousha's review, 14 Nov 2026). A real delete takes
444
+ // the store's entries with it (dropJournal).
441
445
  if (!fs.existsSync(folderOf(tenant, workspace))) {
442
446
  q.shift();
443
- forget(tenant, workspace, entry);
447
+ if (!localDisk()) forget(tenant, workspace, entry);
444
448
  continue;
445
449
  }
446
450
  try {
@@ -453,8 +457,9 @@ async function drainOne(k: string): Promise<void> {
453
457
  if (err instanceof PublishError && err.code === "gone") {
454
458
  // The folder is gone (deleted or renamed, here or on another pod): the
455
459
  // change has nowhere to go, and nothing should be recorded under the name.
460
+ // On this pod's own disk, kept in the store as above.
456
461
  q.shift();
457
- forget(tenant, workspace, entry);
462
+ if (!localDisk()) forget(tenant, workspace, entry);
458
463
  continue;
459
464
  }
460
465
  if (permanent(err)) {
@@ -905,9 +910,12 @@ export async function cleanOrphanedHistory(
905
910
  const out = { checked: 0, orphans: 0, marked: 0, dropped: 0, skipped: 0 };
906
911
  const deps = S.backend;
907
912
  if (!deps) return out;
908
- // Folders are a copy of the heads (revisions.mirror): a missing one says
909
- // nothing about the workspace, so nothing is taken for orphaned.
910
- if (mirrorOn()) return out;
913
+ // Folders are a copy of the heads (revisions.mirror), or this pod's own
914
+ // disk (FOLDRUN_DATA_LOCAL=1, with the mirror on or off): a missing one says
915
+ // nothing about the workspace, so nothing is taken for orphaned. Until
916
+ // 14 Nov 2026 only the mirror was asked, so turning it off on a pod with its
917
+ // own empty disk would have dropped every workspace's history (Kousha's review).
918
+ if (mirrorOn() || localDisk()) return out;
911
919
  const root = dataRoot();
912
920
  let rootEntries = 0;
913
921
  try {
@@ -983,7 +991,11 @@ export async function recoverJournal(opts: { minAgeMs?: number } = {}): Promise<
983
991
  const k = key(tenant, workspace);
984
992
  const q = queues.get(k) ?? [];
985
993
  const held = new Set(q.filter((e) => e.store === store).map((e) => e.handle));
986
- const gone = !fs.existsSync(folderOf(tenant, workspace));
994
+ // A folder missing from this pod's own disk (FOLDRUN_DATA_LOCAL=1) is
995
+ // one not synced here yet, not a deleted workspace: the database store
996
+ // is shared, and those entries may be another live pod's (Kousha's
997
+ // review, 14 Nov 2026). Only a shared volume's absence is believed.
998
+ const gone = !localDisk() && !fs.existsSync(folderOf(tenant, workspace));
987
999
  const found: Entry[] = [];
988
1000
  let names: { handle: string; ageMs: number }[] = [];
989
1001
  try {
@@ -995,12 +1007,15 @@ export async function recoverJournal(opts: { minAgeMs?: number } = {}): Promise<
995
1007
  }
996
1008
  for (const { handle, ageMs } of names) {
997
1009
  if (held.has(handle)) continue;
1010
+ // Young entries are a live process's, whatever the folder says: the
1011
+ // age is asked before anything is removed (until 14 Nov 2026 a missing
1012
+ // folder removed even a fresh entry).
1013
+ if (minAge > 0 && ageMs < minAge) continue;
998
1014
  try {
999
1015
  if (gone) {
1000
1016
  store.remove(tenant, workspace, handle);
1001
1017
  continue;
1002
1018
  }
1003
- if (minAge > 0 && ageMs < minAge) continue;
1004
1019
  const e = store.read(tenant, workspace, handle);
1005
1020
  if ((e.v !== 1 && e.v !== 2) || !Array.isArray(e.files)) throw new Error("not a journal entry");
1006
1021
  found.push({ id: e.id, files: e.files, by: e.by, message: e.message, at: e.at, restoredFrom: e.restoredFrom, baseRev: e.baseRev, store, handle });
@@ -1375,6 +1390,13 @@ async function reconcileNow(tenant: string, workspace: string, by = "reconcile")
1375
1390
  // own writes reach the head through the journal; the reconcile waits for a
1376
1391
  // sync to say what "changed here" means.
1377
1392
  if (mirrorOn() && !readMirrorBase(tenant, workspace)) return null;
1393
+ // This pod's own disk with the mirror off: nothing brings the folder to the
1394
+ // heads, so it is not the truth about anything either — an emptyDir after a
1395
+ // restart would publish every file as deleted (Kousha's review, 14 Nov 2026).
1396
+ if (!mirrorOn() && localDisk()) {
1397
+ opsLog("warn", "revisions.reconcile_skipped", { tenant, workspace, reason: "local_disk_without_mirror" });
1398
+ return null;
1399
+ }
1378
1400
  const ws: WorkspaceRef = { tenant, workspace };
1379
1401
  const k = key(tenant, workspace);
1380
1402
  // The mark goes BEFORE the folder is read, so one set after the read (a run