@foldrun/core 17.2.0 → 17.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.
Files changed (70) hide show
  1. package/dist/src/approvals.js +3 -3
  2. package/dist/src/blob-cache.d.ts +58 -0
  3. package/dist/src/blob-cache.js +309 -0
  4. package/dist/src/blob-pins.d.ts +44 -0
  5. package/dist/src/blob-pins.js +61 -0
  6. package/dist/src/blob-store.d.ts +47 -0
  7. package/dist/src/blob-store.js +357 -0
  8. package/dist/src/blob-sweeper.d.ts +70 -0
  9. package/dist/src/blob-sweeper.js +254 -0
  10. package/dist/src/blob-types.d.ts +145 -0
  11. package/dist/src/blob-types.js +115 -0
  12. package/dist/src/browse-egress.d.ts +31 -0
  13. package/dist/src/browse-egress.js +43 -0
  14. package/dist/src/egress.d.ts +9 -0
  15. package/dist/src/evals.js +1 -1
  16. package/dist/src/history.d.ts +86 -9
  17. package/dist/src/history.js +132 -16
  18. package/dist/src/journal-store.d.ts +58 -0
  19. package/dist/src/journal-store.js +117 -0
  20. package/dist/src/manifest.d.ts +92 -0
  21. package/dist/src/manifest.js +239 -0
  22. package/dist/src/ops-log.js +26 -3
  23. package/dist/src/platform.d.ts +7 -0
  24. package/dist/src/platform.js +1 -0
  25. package/dist/src/publish.d.ts +134 -0
  26. package/dist/src/publish.js +434 -0
  27. package/dist/src/records.d.ts +72 -0
  28. package/dist/src/records.js +24 -0
  29. package/dist/src/revision-store.d.ts +161 -0
  30. package/dist/src/revision-store.js +378 -0
  31. package/dist/src/run-container.d.ts +20 -0
  32. package/dist/src/run-container.js +61 -0
  33. package/dist/src/runner.js +22 -4
  34. package/dist/src/s3-blob-store.d.ts +29 -0
  35. package/dist/src/s3-blob-store.js +458 -0
  36. package/dist/src/state-retention.d.ts +15 -0
  37. package/dist/src/state-retention.js +65 -0
  38. package/dist/src/storage.d.ts +29 -1
  39. package/dist/src/storage.js +331 -81
  40. package/dist/src/store.d.ts +41 -0
  41. package/dist/src/store.js +188 -5
  42. package/dist/src/trigger-log.js +17 -0
  43. package/dist/src/workspace-revisions.d.ts +259 -0
  44. package/dist/src/workspace-revisions.js +1274 -0
  45. package/package.json +1 -1
  46. package/src/approvals.ts +3 -3
  47. package/src/blob-cache.ts +354 -0
  48. package/src/blob-pins.ts +99 -0
  49. package/src/blob-store.ts +385 -0
  50. package/src/blob-sweeper.ts +308 -0
  51. package/src/blob-types.ts +216 -0
  52. package/src/browse-egress.ts +49 -1
  53. package/src/egress.ts +6 -0
  54. package/src/evals.ts +1 -1
  55. package/src/history.ts +182 -16
  56. package/src/journal-store.ts +152 -0
  57. package/src/manifest.ts +274 -0
  58. package/src/ops-log.ts +25 -3
  59. package/src/platform.ts +8 -0
  60. package/src/publish.ts +520 -0
  61. package/src/records.ts +105 -0
  62. package/src/revision-store.ts +513 -0
  63. package/src/run-container.ts +62 -0
  64. package/src/runner.ts +21 -4
  65. package/src/s3-blob-store.ts +489 -0
  66. package/src/state-retention.ts +83 -0
  67. package/src/storage.ts +335 -81
  68. package/src/store.ts +178 -5
  69. package/src/trigger-log.ts +15 -0
  70. package/src/workspace-revisions.ts +1324 -0
package/src/store.ts CHANGED
@@ -26,9 +26,10 @@ import { EFFORT_LEVELS, FLOW_TRIGGERS } from "./frontmatter-fields.ts";
26
26
  import type { BrowserPodOutcome } from "./browser-pod.ts";
27
27
  import fs from "node:fs";
28
28
  import path from "node:path";
29
- import { recordRevision, registerTreeReader, assertCommitId, type RevisionFile } from "./history.ts";
29
+ import { recordRevision, registerTreeReader, assertCommitId, workspaceGone, workspaceMoved, workspaceLifecycle, type RevisionFile } from "./history.ts";
30
30
  import { dataRoot, singleWorkspace, singleAccountRoot, entriesNoFollow } from "./paths.ts";
31
31
  import { platform } from "./platform.ts";
32
+ import { records } from "./records.ts";
32
33
  import { toLF, asRaw } from "./eol.ts";
33
34
  import type { TestEffect } from "./test-mode.ts";
34
35
  import type { OperatorEvent } from "./operator.ts";
@@ -2670,6 +2671,7 @@ export function listWorkspaces(tenant: string): WorkspaceSummary[] {
2670
2671
  assertSafeName(tenant, "tenant");
2671
2672
  const dir = workspacesRoot(tenant);
2672
2673
  if (!fs.existsSync(dir)) return [];
2674
+ const recordCounts = records()?.runCounts(tenant) ?? null;
2673
2675
  return fs
2674
2676
  .readdirSync(dir)
2675
2677
  .filter((n) => fs.statSync(path.join(dir, n)).isDirectory())
@@ -2698,7 +2700,7 @@ export function listWorkspaces(tenant: string): WorkspaceSummary[] {
2698
2700
  agents: listAgents(tenant, name).length,
2699
2701
  flows: listFlows(tenant, name).length,
2700
2702
  deployedAt: fs.statSync(pDir).mtime.toISOString(),
2701
- runCount: fs.existsSync(runsDir) ? fs.readdirSync(runsDir).filter((f) => f.endsWith(".json") && !f.startsWith(".")).length : 0,
2703
+ runCount: recordCounts && records(tenant, name) ? (recordCounts[name] ?? 0) : (fs.existsSync(runsDir) ? fs.readdirSync(runsDir).filter((f) => f.endsWith(".json") && !f.startsWith(".")).length : 0),
2702
2704
  };
2703
2705
  })
2704
2706
  .sort((a, b) => b.deployedAt.localeCompare(a.deployedAt));
@@ -3191,7 +3193,14 @@ export function deleteWorkspacePath(tenant: string, workspace: string, rel: stri
3191
3193
  export function deleteWorkspace(tenant: string, workspace: string) {
3192
3194
  const dir = workspaceDir(tenant, workspace);
3193
3195
  if (!fs.existsSync(dir)) throw new Error(`workspace ${workspace} not found`);
3196
+ // Its records in the database (runs, trigger log, file index) go BEFORE the
3197
+ // folder: if that throws, nothing was removed and the delete can be retried;
3198
+ // after the folder is gone a retry would answer "not found" with rows left.
3199
+ records()?.workspaceDrop(tenant, workspace);
3194
3200
  fs.rmSync(dir, { recursive: true, force: true });
3201
+ // Its revision journal: queued changes must not commit (or replay at the
3202
+ // next boot) for a workspace that is gone (FOL-126 phase 4).
3203
+ workspaceGone(tenant, workspace);
3195
3204
  // Its file store too, kept by name beside the folder (storage.ts). Until
3196
3205
  // 30 Oct 2026 the store outlived the delete: its files stayed downloadable
3197
3206
  // and shared, and the next workspace given this name inherited them. The
@@ -3204,6 +3213,20 @@ export function deleteWorkspace(tenant: string, workspace: string) {
3204
3213
  workspaceChanged(tenant, workspace, "delete");
3205
3214
  }
3206
3215
 
3216
+ /**
3217
+ * deleteWorkspace, with the workspace's revisions: its history is dropped
3218
+ * BEFORE the folder goes (a store that cannot be reached refuses the delete
3219
+ * while it can still be retried) and once more after, for whatever a racing
3220
+ * write made in between. A new workspace of the same name starts empty.
3221
+ * Where nothing records revisions this is deleteWorkspace.
3222
+ */
3223
+ export async function deleteWorkspaceAsync(tenant: string, workspace: string): Promise<void> {
3224
+ const lc = workspaceLifecycle();
3225
+ if (!lc) return deleteWorkspace(tenant, workspace);
3226
+ if (!fs.existsSync(workspaceDir(tenant, workspace))) throw new Error(`workspace ${workspace} not found`);
3227
+ await lc.drop(tenant, workspace, () => deleteWorkspace(tenant, workspace));
3228
+ }
3229
+
3207
3230
  /**
3208
3231
  * What a rename would break, so the caller can say so before doing it.
3209
3232
  *
@@ -3244,6 +3267,16 @@ export function renameWorkspace(tenant: string, from: string, to: string) {
3244
3267
 
3245
3268
  fs.mkdirSync(path.dirname(dest), { recursive: true });
3246
3269
  fs.renameSync(src, dest);
3270
+ // Its runs and trigger log in the database follow the name; if they cannot,
3271
+ // the folder goes back, so the two never disagree about where the workspace is.
3272
+ try {
3273
+ records()?.workspaceMove(tenant, from, to);
3274
+ } catch (err) {
3275
+ fs.renameSync(dest, src);
3276
+ throw err;
3277
+ }
3278
+ // Its queued revisions and its history follow the name (workspace-revisions.ts).
3279
+ workspaceMoved(tenant, from, to);
3247
3280
 
3248
3281
  const agentsMd = path.join(dest, "AGENTS.md");
3249
3282
  if (fs.existsSync(agentsMd)) {
@@ -3252,6 +3285,25 @@ export function renameWorkspace(tenant: string, from: string, to: string) {
3252
3285
  }
3253
3286
  }
3254
3287
 
3288
+ /**
3289
+ * renameWorkspace, with the workspace's revisions: queued changes are held
3290
+ * while its history is re-keyed to the new name, then the folder moves; if the
3291
+ * folder cannot move the history goes back. A store that cannot be reached
3292
+ * refuses the rename before anything moved. Where nothing records revisions
3293
+ * this is renameWorkspace.
3294
+ */
3295
+ export async function renameWorkspaceAsync(tenant: string, from: string, to: string): Promise<void> {
3296
+ const lc = workspaceLifecycle();
3297
+ if (!lc || from === to) return renameWorkspace(tenant, from, to);
3298
+ assertSafeName(to, "workspace");
3299
+ if (!fs.existsSync(workspaceDir(tenant, from))) throw new Error(`workspace ${from} not found`);
3300
+ // The checks renameWorkspace makes, BEFORE the history moves: a name that is
3301
+ // taken must never have its history replaced by this one's.
3302
+ if (singleWorkspace()) return renameWorkspace(tenant, from, to); // throws
3303
+ if (fs.existsSync(workspaceDir(tenant, to)) || fs.existsSync(path.join(dataRoot(), tenant, WORKSPACES, to))) throw new Error(`workspace ${to} already exists`);
3304
+ await lc.move(tenant, from, to, () => renameWorkspace(tenant, from, to));
3305
+ }
3306
+
3255
3307
  /** Set a workspace's description, in AGENTS.md, without touching anything else.
3256
3308
  *
3257
3309
  * 30 Oct 2026: the value was spliced in as `description: ${text}`, so a
@@ -4020,15 +4072,19 @@ export function runFilePath(tenant: string, workspace: string, runId: string) {
4020
4072
  export function deleteRun(tenant: string, workspace: string, runId: string, opts: { pruned?: boolean } = {}): boolean {
4021
4073
  const file = runFilePath(tenant, workspace, runId);
4022
4074
  const dir = path.join(workspaceDir(tenant, workspace), "runs", runId);
4023
- const had = fs.existsSync(file);
4075
+ const port = records(tenant, workspace);
4076
+ const had = port ? port.runGet(tenant, workspace, runId) !== null : fs.existsSync(file);
4024
4077
  if (!had && !fs.existsSync(dir)) return false;
4025
4078
  // The folder first (archived outputs, captured calls), the record last:
4026
4079
  // a delete that fails halfway leaves the record, so a retry finds the run
4027
4080
  // and finishes — the other order left the content with nothing pointing
4028
4081
  // at it (Codex review, FOL-124).
4029
4082
  fs.rmSync(dir, { recursive: true, force: true });
4030
- fs.rmSync(file, { force: true });
4031
- noteRunDeleted(path.dirname(file), runId);
4083
+ if (port) port.runDelete(tenant, workspace, runId);
4084
+ else {
4085
+ fs.rmSync(file, { force: true });
4086
+ noteRunDeleted(path.dirname(file), runId);
4087
+ }
4032
4088
  if (!had) return true;
4033
4089
  // The retention window's prune (`pruned`) is policy, not a person erasing
4034
4090
  // history: no per-run ledger note. The charge stands either way.
@@ -4096,6 +4152,8 @@ export function runOutputPath(
4096
4152
 
4097
4153
  export function readRun(tenant: string, workspace: string, runId: string): RunRecord | null {
4098
4154
  const p = runFilePath(tenant, workspace, runId);
4155
+ const port = records(tenant, workspace);
4156
+ if (port) return port.runGet(tenant, workspace, runId);
4099
4157
  // EFS/NFS: writeRun() replaces this file by rename, so an open()+read()
4100
4158
  // here can land on the just-unlinked inode and come back ESTALE (errno
4101
4159
  // -116, surfaced by Node as "Unknown system error -116", syscall read).
@@ -4115,8 +4173,36 @@ export function readRun(tenant: string, workspace: string, runId: string): RunRe
4115
4173
  }
4116
4174
  }
4117
4175
 
4176
+ /** readRun without holding the event loop: asked of the database directly
4177
+ * where the port has async reads, otherwise the synchronous read. */
4178
+ export async function readRunAsync(tenant: string, workspace: string, runId: string): Promise<RunRecord | null> {
4179
+ runFilePath(tenant, workspace, runId); // the same name checks as readRun
4180
+ const port = records(tenant, workspace);
4181
+ return port?.async ? port.async.runGet(tenant, workspace, runId) : readRun(tenant, workspace, runId);
4182
+ }
4183
+
4184
+ /** writeRun without holding the event loop (FOL-126 phase 4): for the callers
4185
+ * that are async anyway. Where the port writes asynchronously the record is
4186
+ * written when this resolves; everywhere else (files, a port without the
4187
+ * async write) it IS writeRun. The sync callers (the runner's save) are
4188
+ * untouched. */
4189
+ export async function writeRunAsync(tenant: string, workspace: string, run: RunRecord): Promise<void> {
4190
+ const port = records(tenant, workspace);
4191
+ if (port?.async?.runPut) {
4192
+ runFilePath(tenant, workspace, run.id); // the same name checks as writeRun
4193
+ await port.async.runPut(tenant, workspace, run, summarizeRun(run), LIVE_STATUSES.has(run.status));
4194
+ return;
4195
+ }
4196
+ writeRun(tenant, workspace, run);
4197
+ }
4198
+
4118
4199
  export function writeRun(tenant: string, workspace: string, run: RunRecord) {
4119
4200
  const p = runFilePath(tenant, workspace, run.id);
4201
+ const port = records(tenant, workspace);
4202
+ if (port) {
4203
+ port.runPut(tenant, workspace, run, summarizeRun(run), LIVE_STATUSES.has(run.status));
4204
+ return;
4205
+ }
4120
4206
  fs.mkdirSync(path.dirname(p), { recursive: true });
4121
4207
  // Atomic write — runs are re-read while running (dashboard polls, CLI
4122
4208
  // tails), and a plain writeFileSync can be caught half-written.
@@ -4126,6 +4212,75 @@ export function writeRun(tenant: string, workspace: string, run: RunRecord) {
4126
4212
  noteRunWritten(path.dirname(p), run, p);
4127
4213
  }
4128
4214
 
4215
+ /** When a run's record last changed, as a string that differs whenever it
4216
+ * does — what a poller compares instead of a file's stat. Null: no such run
4217
+ * (or, for a file, gone for a moment — read to find out). */
4218
+ export function runStamp(tenant: string, workspace: string, runId: string): string | null {
4219
+ const port = records(tenant, workspace);
4220
+ if (port) {
4221
+ const at = port.runStamp(tenant, workspace, runId);
4222
+ return at === null ? null : String(at);
4223
+ }
4224
+ try {
4225
+ return fileStamp(fs.statSync(runFilePath(tenant, workspace, runId)));
4226
+ } catch {
4227
+ return null;
4228
+ }
4229
+ }
4230
+
4231
+ /** When a run's record was last written (epoch ms), or null when there is none. */
4232
+ export function runUpdatedMs(tenant: string, workspace: string, runId: string): number | null {
4233
+ const port = records(tenant, workspace);
4234
+ if (port) {
4235
+ const us = port.runStamp(tenant, workspace, runId);
4236
+ return us === null ? null : us / 1000;
4237
+ }
4238
+ try {
4239
+ return fs.statSync(runFilePath(tenant, workspace, runId)).mtimeMs;
4240
+ } catch {
4241
+ return null;
4242
+ }
4243
+ }
4244
+
4245
+ /** A run's record as the JSON text a stream sends, or null when there is none. */
4246
+ export function readRunText(tenant: string, workspace: string, runId: string): string | null {
4247
+ if (records(tenant, workspace)) {
4248
+ const run = readRun(tenant, workspace, runId);
4249
+ return run ? JSON.stringify(run, null, 2) : null;
4250
+ }
4251
+ try {
4252
+ return fs.readFileSync(runFilePath(tenant, workspace, runId), "utf8");
4253
+ } catch {
4254
+ return null;
4255
+ }
4256
+ }
4257
+
4258
+ /** Runs of one workspace that started at or after `sinceMs`, whole, newest
4259
+ * first. Off the records where there are any; the files otherwise read only
4260
+ * the names that say when they began. */
4261
+ export function runsStartedSince(tenant: string, workspace: string, sinceMs: number): RunRecord[] {
4262
+ const port = records(tenant, workspace);
4263
+ if (port) return port.runsSince(tenant, workspace, sinceMs);
4264
+ const dir = path.join(workspaceDir(tenant, workspace), "runs");
4265
+ let names: string[];
4266
+ try {
4267
+ names = fs.readdirSync(dir);
4268
+ } catch {
4269
+ return [];
4270
+ }
4271
+ const out: RunRecord[] = [];
4272
+ for (const f of names) {
4273
+ if (!f.endsWith(".json") || f.startsWith(".")) continue;
4274
+ try {
4275
+ const run = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as RunRecord;
4276
+ if (Date.parse(run.startedAt) >= sinceMs) out.push(run);
4277
+ } catch {
4278
+ // mid-write or foreign: not a run
4279
+ }
4280
+ }
4281
+ return out.sort((a, b) => b.startedAt.localeCompare(a.startedAt));
4282
+ }
4283
+
4129
4284
  /**
4130
4285
  * The workspace's live runs — queued, running, or parked at a gate — read
4131
4286
  * whole, newest first. Off the run index: only the live records are opened,
@@ -4133,7 +4288,15 @@ export function writeRun(tenant: string, workspace: string, run: RunRecord) {
4133
4288
  * periodic sweeps want (reconcile, the watchdog), and on a box that keeps
4134
4289
  * ninety days of runs that is a handful of files instead of thousands.
4135
4290
  */
4291
+ /** liveRuns without holding the event loop (see readRunAsync). */
4292
+ export async function liveRunsAsync(tenant: string, workspace: string): Promise<RunRecord[]> {
4293
+ const port = records(tenant, workspace);
4294
+ return port?.async ? port.async.runLive(tenant, workspace) : liveRuns(tenant, workspace);
4295
+ }
4296
+
4136
4297
  export function liveRuns(tenant: string, workspace: string): RunRecord[] {
4298
+ const port = records(tenant, workspace);
4299
+ if (port) return port.runLive(tenant, workspace);
4137
4300
  const out: RunRecord[] = [];
4138
4301
  for (const s of listRunSummaries(tenant, workspace)) {
4139
4302
  if (!LIVE_STATUSES.has(s.status)) continue;
@@ -4428,7 +4591,15 @@ function noteRunDeleted(dir: string, runId: string) {
4428
4591
  * are re-read, and the index is rewritten if anything changed. A missing
4429
4592
  * or unreadable index is a full scan that leaves one behind.
4430
4593
  */
4594
+ /** listRunSummaries without holding the event loop (see readRunAsync). */
4595
+ export async function listRunSummariesAsync(tenant: string, workspace: string): Promise<RunSummary[]> {
4596
+ const port = records(tenant, workspace);
4597
+ return port?.async ? port.async.runSummaries(tenant, workspace) : listRunSummaries(tenant, workspace);
4598
+ }
4599
+
4431
4600
  export function listRunSummaries(tenant: string, workspace: string): RunSummary[] {
4601
+ const port = records(tenant, workspace);
4602
+ if (port) return port.runSummaries(tenant, workspace);
4432
4603
  const dir = path.join(workspaceDir(tenant, workspace), "runs");
4433
4604
  if (!fs.existsSync(dir)) return [];
4434
4605
  const index: RunIndex = readRunIndex(dir) ?? { v: 1, runs: {} };
@@ -4799,6 +4970,8 @@ export function listAgentSteps(tenant: string, workspace: string, agent: string)
4799
4970
  /** Every run of the workspace, whole, newest first. Parses every record;
4800
4971
  * a list that needs only what a list needs reads listRunSummaries. */
4801
4972
  export function listRuns(tenant: string, workspace: string): RunRecord[] {
4973
+ const port = records(tenant, workspace);
4974
+ if (port) return port.runAll(tenant, workspace);
4802
4975
  const dir = path.join(workspaceDir(tenant, workspace), "runs");
4803
4976
  if (!fs.existsSync(dir)) return [];
4804
4977
  return fs
@@ -28,6 +28,7 @@
28
28
  import fs from "node:fs";
29
29
  import path from "node:path";
30
30
  import { workspaceDir } from "./store.ts";
31
+ import { records } from "./records.ts";
31
32
 
32
33
  /** What became of one trigger firing. `started` is here too: without it the
33
34
  * log answers "why did nothing run" and not "how often does anything",
@@ -84,6 +85,12 @@ const KEEP = 500;
84
85
  * diagnostic: the whole point is to be reading it on the bad morning. */
85
86
  export function recordTriggerEvent(tenant: string, workspace: string, event: TriggerEvent): void {
86
87
  try {
88
+ const port = records(tenant, workspace);
89
+ if (port) {
90
+ // Only a workspace that is there: a delivery for one that is not leaves no row.
91
+ if (fs.existsSync(workspaceDir(tenant, workspace))) port.triggerAppend(tenant, workspace, event);
92
+ return;
93
+ }
87
94
  const file = logFile(tenant, workspace);
88
95
  if (!fs.existsSync(path.dirname(file))) return;
89
96
  fs.appendFileSync(file, JSON.stringify(event) + "\n");
@@ -99,6 +106,14 @@ export function recordTriggerEvent(tenant: string, workspace: string, event: Tri
99
106
  }
100
107
 
101
108
  export function readTriggerEvents(tenant: string, workspace: string, limit = 200): TriggerEvent[] {
109
+ const port = records(tenant, workspace);
110
+ if (port) {
111
+ try {
112
+ return port.triggerRead(tenant, workspace, limit);
113
+ } catch {
114
+ return [];
115
+ }
116
+ }
102
117
  let lines: string[];
103
118
  try {
104
119
  lines = fs.readFileSync(logFile(tenant, workspace), "utf8").split("\n").filter(Boolean);