@foldrun/core 0.2.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 (166) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +239 -0
  3. package/dist/index.d.ts +32 -0
  4. package/dist/index.js +43 -0
  5. package/dist/src/agent-tools.d.ts +31 -0
  6. package/dist/src/agent-tools.js +99 -0
  7. package/dist/src/agents-md.d.ts +9 -0
  8. package/dist/src/agents-md.js +83 -0
  9. package/dist/src/api-tools.d.ts +38 -0
  10. package/dist/src/api-tools.js +313 -0
  11. package/dist/src/approvals.d.ts +39 -0
  12. package/dist/src/approvals.js +103 -0
  13. package/dist/src/arrange.d.ts +8 -0
  14. package/dist/src/arrange.js +40 -0
  15. package/dist/src/catalog.d.ts +108 -0
  16. package/dist/src/catalog.js +255 -0
  17. package/dist/src/completions.d.ts +61 -0
  18. package/dist/src/completions.js +578 -0
  19. package/dist/src/confine.d.ts +31 -0
  20. package/dist/src/confine.js +298 -0
  21. package/dist/src/container.d.ts +30 -0
  22. package/dist/src/container.js +172 -0
  23. package/dist/src/context-tools.d.ts +81 -0
  24. package/dist/src/context-tools.js +347 -0
  25. package/dist/src/deploy.d.ts +100 -0
  26. package/dist/src/deploy.js +377 -0
  27. package/dist/src/egress.d.ts +75 -0
  28. package/dist/src/egress.js +133 -0
  29. package/dist/src/evals.d.ts +150 -0
  30. package/dist/src/evals.js +651 -0
  31. package/dist/src/flow-lint.d.ts +16 -0
  32. package/dist/src/flow-lint.js +177 -0
  33. package/dist/src/git-hook.d.ts +1 -0
  34. package/dist/src/git-hook.js +70 -0
  35. package/dist/src/git.d.ts +37 -0
  36. package/dist/src/git.js +101 -0
  37. package/dist/src/gitrepo.d.ts +95 -0
  38. package/dist/src/gitrepo.js +355 -0
  39. package/dist/src/history.d.ts +82 -0
  40. package/dist/src/history.js +259 -0
  41. package/dist/src/kinds.d.ts +95 -0
  42. package/dist/src/kinds.js +421 -0
  43. package/dist/src/library.d.ts +94 -0
  44. package/dist/src/library.js +341 -0
  45. package/dist/src/linkable.d.ts +20 -0
  46. package/dist/src/linkable.js +113 -0
  47. package/dist/src/model-probe.d.ts +21 -0
  48. package/dist/src/model-probe.js +81 -0
  49. package/dist/src/notify.d.ts +61 -0
  50. package/dist/src/notify.js +252 -0
  51. package/dist/src/oauth-presets.d.ts +13 -0
  52. package/dist/src/oauth-presets.js +39 -0
  53. package/dist/src/okf.d.ts +220 -0
  54. package/dist/src/okf.js +656 -0
  55. package/dist/src/openapi.d.ts +66 -0
  56. package/dist/src/openapi.js +342 -0
  57. package/dist/src/paths.d.ts +33 -0
  58. package/dist/src/paths.js +78 -0
  59. package/dist/src/platform.d.ts +32 -0
  60. package/dist/src/platform.js +48 -0
  61. package/dist/src/providers.d.ts +47 -0
  62. package/dist/src/providers.js +114 -0
  63. package/dist/src/refs.d.ts +20 -0
  64. package/dist/src/refs.js +78 -0
  65. package/dist/src/run-container.d.ts +227 -0
  66. package/dist/src/run-container.js +760 -0
  67. package/dist/src/runner.d.ts +241 -0
  68. package/dist/src/runner.js +3215 -0
  69. package/dist/src/runtime.d.ts +41 -0
  70. package/dist/src/runtime.js +357 -0
  71. package/dist/src/script-tools.d.ts +62 -0
  72. package/dist/src/script-tools.js +296 -0
  73. package/dist/src/secret-files.d.ts +12 -0
  74. package/dist/src/secret-files.js +92 -0
  75. package/dist/src/secrets.d.ts +147 -0
  76. package/dist/src/secrets.js +626 -0
  77. package/dist/src/starter.d.ts +21 -0
  78. package/dist/src/starter.js +399 -0
  79. package/dist/src/step-exec.d.ts +136 -0
  80. package/dist/src/step-exec.js +391 -0
  81. package/dist/src/storage.d.ts +123 -0
  82. package/dist/src/storage.js +640 -0
  83. package/dist/src/store.d.ts +1052 -0
  84. package/dist/src/store.js +2378 -0
  85. package/dist/src/tar.d.ts +30 -0
  86. package/dist/src/tar.js +111 -0
  87. package/dist/src/tool-names.d.ts +39 -0
  88. package/dist/src/tool-names.js +79 -0
  89. package/dist/src/tool-programs.d.ts +18 -0
  90. package/dist/src/tool-programs.js +52 -0
  91. package/dist/src/tool-test.d.ts +19 -0
  92. package/dist/src/tool-test.js +335 -0
  93. package/dist/src/translator.d.ts +159 -0
  94. package/dist/src/translator.js +986 -0
  95. package/dist/src/triggers.d.ts +60 -0
  96. package/dist/src/triggers.js +270 -0
  97. package/dist/src/webhook.d.ts +47 -0
  98. package/dist/src/webhook.js +148 -0
  99. package/index.ts +44 -0
  100. package/package.json +64 -0
  101. package/src/agent-tools.ts +138 -0
  102. package/src/agents-md.ts +81 -0
  103. package/src/api-tools.ts +389 -0
  104. package/src/approvals.ts +129 -0
  105. package/src/arrange.ts +42 -0
  106. package/src/catalog.ts +321 -0
  107. package/src/completions.ts +652 -0
  108. package/src/confine.ts +350 -0
  109. package/src/container.ts +220 -0
  110. package/src/context-tools.ts +435 -0
  111. package/src/deploy.ts +458 -0
  112. package/src/egress.ts +176 -0
  113. package/src/evals.ts +812 -0
  114. package/src/flow-lint.ts +215 -0
  115. package/src/git-hook.ts +73 -0
  116. package/src/git.ts +117 -0
  117. package/src/gitrepo.ts +391 -0
  118. package/src/history.ts +329 -0
  119. package/src/kinds.ts +507 -0
  120. package/src/library.ts +419 -0
  121. package/src/linkable.ts +123 -0
  122. package/src/model-probe.ts +112 -0
  123. package/src/notify.ts +281 -0
  124. package/src/oauth-presets.ts +54 -0
  125. package/src/okf.ts +768 -0
  126. package/src/openapi.ts +383 -0
  127. package/src/paths.ts +79 -0
  128. package/src/platform.ts +87 -0
  129. package/src/providers.ts +147 -0
  130. package/src/refs.ts +83 -0
  131. package/src/run-container.ts +883 -0
  132. package/src/runner.ts +3446 -0
  133. package/src/runtime.ts +404 -0
  134. package/src/script-tools.ts +383 -0
  135. package/src/secret-files.ts +101 -0
  136. package/src/secrets.ts +799 -0
  137. package/src/starter.ts +415 -0
  138. package/src/step-exec.ts +498 -0
  139. package/src/storage.ts +771 -0
  140. package/src/store.ts +2985 -0
  141. package/src/tar.ts +124 -0
  142. package/src/tool-names.ts +91 -0
  143. package/src/tool-programs.ts +65 -0
  144. package/src/tool-test.ts +401 -0
  145. package/src/translator.ts +1029 -0
  146. package/src/triggers.ts +287 -0
  147. package/src/webhook.ts +183 -0
  148. package/templates/blog-desk/AGENTS.md +13 -0
  149. package/templates/blog-desk/agents/researcher/agent.md +11 -0
  150. package/templates/blog-desk/agents/writer/agent.md +8 -0
  151. package/templates/blog-desk/evals/writer-quality.md +10 -0
  152. package/templates/blog-desk/flows/publish.md +10 -0
  153. package/templates/blog-desk/knowledge/house-style.md +9 -0
  154. package/templates/blog-desk/knowledge/index.md +9 -0
  155. package/templates/hello/AGENTS.md +18 -0
  156. package/templates/hello/agents/curator/agent.md +32 -0
  157. package/templates/hello/agents/notetaker/agent.md +24 -0
  158. package/templates/hello/evals/house-style.md +16 -0
  159. package/templates/hello/flows/memory-review.md +7 -0
  160. package/templates/hello/flows/note.md +7 -0
  161. package/templates/hello/knowledge/house-style.md +15 -0
  162. package/templates/hello/knowledge/index.md +9 -0
  163. package/templates/hello/skills/plain-english/SKILL.md +11 -0
  164. package/templates/hello/state/publishing.md +7 -0
  165. package/templates/hello/tools/wordcount/run.py +13 -0
  166. package/templates/hello/tools/wordcount/tool.md +22 -0
package/src/store.ts ADDED
@@ -0,0 +1,2985 @@
1
+ // Filesystem storage. Three real directories, one per scope — the nesting IS
2
+ // the scope model, so "nearest wins" is literally walking up the tree:
3
+ //
4
+ // data/<account>/ the company: one bill, one identity
5
+ // ├── secrets.json account credentials
6
+ // ├── library/ shared by every workspace
7
+ // │ └── {skills,memory,knowledge,tools,scripts}/
8
+ // ├── .runtimes/ generated: container images, venvs
9
+ // └── workspaces/<workspace>/ a trust boundary
10
+ // ├── AGENTS.md shared context for this workspace
11
+ // ├── agents/<agent>/ one role
12
+ // │ └── {agent.md,skills,memory,knowledge,scripts,outputs}/
13
+ // ├── {flows,evals,skills,memory,knowledge,tools,scripts,state}/
14
+ // ├── secrets.json workspace credentials
15
+ // └── runs/ generated: the audit trail
16
+ //
17
+ // Anything generated is either dot-prefixed (.runtimes, .results) or named for
18
+ // what produced it (runs/, outputs/). Everything else is authored by a person
19
+ // or an agent, and belongs in git.
20
+ //
21
+ // An account is a named directory (per OKF: tenancy is a hosting concern).
22
+
23
+ import fs from "node:fs";
24
+ import path from "node:path";
25
+ import { recordRevision, registerTreeReader, type RevisionFile } from "./history.ts";
26
+ import { dataRoot, singleWorkspace } from "./paths.ts";
27
+ import matter from "gray-matter";
28
+
29
+ /**
30
+ * The frontmatter of one markdown file, as the runtime reads it. Exported
31
+ * for the CLI: `foldrun probe` used to import gray-matter itself, which is
32
+ * this package's dependency, not the CLI's — on an installed CLI the import
33
+ * failed, the catch swallowed it, and the workspace's provider block was
34
+ * silently ignored, so the probe went to Anthropic and reported a model
35
+ * that "may not exist". Reading through here, the CLI cannot drift from
36
+ * the runtime, and there is nothing to fail to import.
37
+ */
38
+ export function readFrontmatter(file: string): Record<string, unknown> {
39
+ return matter(fs.readFileSync(file, "utf8")).data as Record<string, unknown>;
40
+ }
41
+ import { ownToolNames, legacyUseNames } from "./tool-names.ts";
42
+ import { refNames } from "./refs.ts";
43
+ import {
44
+ readBundle, syncIndex, appendLog, provenanceMarks, syncWorkspaceBundles,
45
+ } from "./okf.ts";
46
+ import { readTransport, KINDS } from "./kinds.ts";
47
+ import { providerPreset, looksOpenAiShaped, PROTECTED_PARAMS, type WireFormat, type AuthShape } from "./providers.ts";
48
+ import { starterFiles, accountFiles } from "./starter.ts";
49
+
50
+ // Where workspaces live. The hosted app keeps many under data/; the CLI runs
51
+ // against one folder, which is what `foldrun run ./my-desk` has to mean.
52
+
53
+ /**
54
+ * Single-workspace mode. When set, the workspace IS this directory — no
55
+ * tenant, no projects/ nesting. That's the difference between a framework you
56
+ * run on a folder and a platform that hosts many, and it's the only thing the
57
+ * core needs to know about which one it's in.
58
+ */
59
+ export { singleWorkspace };
60
+
61
+ // The directories a workspace is made of. Declared once: this list had drifted
62
+ // into four separate copies, and the file tree quietly stopped showing
63
+ // knowledge/, evals/ and state/ because one of them was never updated.
64
+ export const WORKSPACE_DIRS = [
65
+ "agents",
66
+ "flows",
67
+ "evals",
68
+ "skills",
69
+ "memory",
70
+ "knowledge",
71
+ "tools",
72
+ "scripts",
73
+ "state",
74
+ ] as const;
75
+
76
+ const IN_WORKSPACE_DIR = new RegExp(`^(${WORKSPACE_DIRS.join("|")})/`);
77
+
78
+ // Where the file store mirrors its bytes for a run to read. Deliberately not
79
+ // in WORKSPACE_DIRS: that list is the workspace's *source*, and everything
80
+ // derived from it — the file tree, the git export, what a deploy may carry —
81
+ // treats membership as "this is reviewable text". Blobs are not. Declared
82
+ // here rather than in files.ts because saveWorkspace has to know the name to
83
+ // preserve it, and store.ts is the module files.ts depends on, not the
84
+ // reverse. See files.ts for the whole argument.
85
+ export const STORAGE_DIR = "storage";
86
+ /** What this directory was called before. Read-only: nothing writes here any
87
+ * more, but installs created before the rename have bytes under it, and a
88
+ * workspace whose files vanished on upgrade is a data-loss bug however
89
+ * cosmetic the cause. `adoptLegacyFilesDir` moves one across, once. */
90
+ export const LEGACY_FILES_DIR = "files";
91
+
92
+ /**
93
+ * Move a pre-rename `files/` directory to `storage/`, once, in place.
94
+ *
95
+ * Called on the paths that resolve the store and the run mirror, so an
96
+ * existing install upgrades the first time either is touched rather than
97
+ * needing a migration step someone has to remember to run. A no-op when
98
+ * `storage/` already exists (the normal case after the first call) or when
99
+ * there is nothing to move (a fresh install).
100
+ */
101
+ export function adoptLegacyFilesDir(parent: string): void {
102
+ const current = path.join(parent, STORAGE_DIR);
103
+ const legacy = path.join(parent, LEGACY_FILES_DIR);
104
+ if (fs.existsSync(current) || !fs.existsSync(legacy)) return;
105
+ try {
106
+ fs.renameSync(legacy, current);
107
+ } catch {
108
+ // A rename across devices, or a permission we do not have: copy instead,
109
+ // and leave the original alone rather than risk losing it.
110
+ try {
111
+ fs.cpSync(legacy, current, { recursive: true });
112
+ } catch {
113
+ /* the caller sees an absent directory, which is the pre-rename state */
114
+ }
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Rewrite a pre-rename `mdagent_version:` in an AGENTS.md to `foldrun_version:`,
120
+ * once, in place.
121
+ *
122
+ * The reader used to accept both spellings, which meant the old name lived on
123
+ * forever in files nobody would ever edit. Migrating on touch retires it: the
124
+ * first read of an account or workspace converts the file, and after that
125
+ * there is one name in the codebase and one name on disk.
126
+ *
127
+ * Only the key is touched — same line, same value, same everything else — so
128
+ * a file with comments or unusual formatting comes through unchanged.
129
+ */
130
+ export function adoptLegacyVersionKey(dir: string): void {
131
+ for (const name of ["AGENTS.md", "project.md"]) {
132
+ const file = path.join(dir, name);
133
+ let raw: string;
134
+ try {
135
+ raw = fs.readFileSync(file, "utf8");
136
+ } catch {
137
+ continue;
138
+ }
139
+ if (!/^mdagent_version:/m.test(raw)) continue;
140
+ try {
141
+ fs.writeFileSync(file, raw.replace(/^mdagent_version:/m, "foldrun_version:"));
142
+ } catch {
143
+ /* read-only checkout, or a source tree we do not own: the caller reads
144
+ the file either way, and a missing version is a warning, not a stop. */
145
+ }
146
+ }
147
+ }
148
+
149
+ // Where an account keeps its workspaces. Renamed from "projects" once the
150
+ // concept settled on *workspace* everywhere else; the old directory is still
151
+ // read so an existing install keeps working without a migration step.
152
+ const WORKSPACES = "workspaces";
153
+ const LEGACY_WORKSPACES = "projects";
154
+
155
+ function workspacesRoot(tenant: string) {
156
+ const current = path.join(dataRoot(), tenant, WORKSPACES);
157
+ if (fs.existsSync(current)) return current;
158
+ const legacy = path.join(dataRoot(), tenant, LEGACY_WORKSPACES);
159
+ return fs.existsSync(legacy) ? legacy : current;
160
+ }
161
+ const SAFE_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
162
+
163
+ export function assertSafeName(name: string, kind: string) {
164
+ if (!SAFE_NAME.test(name)) throw new Error(`invalid ${kind} "${name}" — kebab-case only`);
165
+ }
166
+
167
+ export function workspaceDir(tenant: string, workspace: string) {
168
+ const single = singleWorkspace();
169
+ if (single) return path.resolve(single);
170
+ assertSafeName(tenant, "tenant");
171
+ assertSafeName(workspace, "workspace");
172
+ // An existing workspace keeps its current home; a new one is created under
173
+ // the canonical name.
174
+ const legacy = path.join(dataRoot(), tenant, LEGACY_WORKSPACES, workspace);
175
+ if (fs.existsSync(legacy)) return legacy;
176
+ return path.join(dataRoot(), tenant, WORKSPACES, workspace);
177
+ }
178
+
179
+ /**
180
+ * The account root — where `library/`, account secrets and the account-wide
181
+ * AGENTS.md live.
182
+ *
183
+ * On a laptop there is no account, so it is the workspace's parent: `my-desk/`
184
+ * sits beside `library/` and an `AGENTS.md` covering everything there. Same
185
+ * rule libraryDir uses, so the two never disagree about where "up" is.
186
+ */
187
+ export function accountDir(tenant: string) {
188
+ const single = singleWorkspace();
189
+ if (single) return path.resolve(single, "..");
190
+ assertSafeName(tenant, "tenant");
191
+ return path.join(dataRoot(), tenant);
192
+ }
193
+
194
+ /**
195
+ * Create the account's AGENTS.md if it is missing. Returns what it wrote.
196
+ *
197
+ * Called from every path that brings a workspace into being, because there is
198
+ * no separate "create an account" step — the account root accumulates
199
+ * (`library/`, `secrets.json`) as things need it, and its AGENTS.md is one
200
+ * more of those things.
201
+ *
202
+ * Never overwrites, and treats the legacy `project.md` as already-present: the
203
+ * whole file is hand-authored config, and a deploy is not permission to touch
204
+ * a scope the deploy did not ship. `dir` is explicit for `foldrun init`, which
205
+ * writes a workspace to an arbitrary path before anything is pinned to it and
206
+ * so cannot ask accountDir where "up" is.
207
+ */
208
+ export function ensureAccountFiles(tenant: string, dir = accountDir(tenant)): string[] {
209
+ const written: string[] = [];
210
+ for (const f of accountFiles(path.basename(path.resolve(dir)))) {
211
+ if (fs.existsSync(path.join(dir, f.path))) continue;
212
+ if (f.path === "AGENTS.md" && fs.existsSync(path.join(dir, "project.md"))) continue;
213
+ fs.mkdirSync(dir, { recursive: true });
214
+ fs.writeFileSync(path.join(dir, f.path), f.content);
215
+ written.push(f.path);
216
+ }
217
+ return written;
218
+ }
219
+
220
+ export interface DeployFile {
221
+ path: string;
222
+ content: string;
223
+ }
224
+
225
+ // Files whose names are part of a standard, and must be spelled exactly.
226
+ // AGENTS.md is the Linux Foundation convention; SKILL.md is the Agent Skills
227
+ // standard. Case-insensitive filesystems (macOS, Windows)
228
+ // happily accept `Skill.md` locally and then it vanishes on a Linux runtime —
229
+ // so the wrong case is rejected here, loudly, with the right spelling.
230
+ const CANONICAL = ["AGENTS.md", "SKILL.md"];
231
+
232
+ /**
233
+ * The format version this build understands.
234
+ *
235
+ * A workspace declares what it targets with `foldrun_version` in AGENTS.md.
236
+ * Without it, the first breaking change to the flow grammar or frontmatter
237
+ * silently changes what everyone's existing files mean — the one failure a
238
+ * format cannot recover from, because there is no error to notice.
239
+ *
240
+ * Missing is treated as "current": nobody's existing workspace breaks by
241
+ * having been written before this existed.
242
+ */
243
+ export const FORMAT_VERSION = "0.1";
244
+
245
+ export interface VersionCheck {
246
+ declared: string | null;
247
+ supported: string;
248
+ /** Set when the workspace targets something this build cannot promise. */
249
+ warning: string | null;
250
+ }
251
+
252
+ export function checkFormatVersion(declared: unknown): VersionCheck {
253
+ const value = typeof declared === "string" ? declared.trim() : null;
254
+ if (!value) return { declared: null, supported: FORMAT_VERSION, warning: null };
255
+
256
+ const [major, minor = "0"] = value.split(".");
257
+ const [okMajor, okMinor] = FORMAT_VERSION.split(".");
258
+ if (major !== okMajor) {
259
+ return {
260
+ declared: value,
261
+ supported: FORMAT_VERSION,
262
+ warning:
263
+ `this workspace targets foldrun format ${value}, and this build understands ` +
264
+ `${FORMAT_VERSION}. A different major version may mean different behaviour — ` +
265
+ `read it as best-effort rather than correct.`,
266
+ };
267
+ }
268
+ if (Number(minor) > Number(okMinor)) {
269
+ return {
270
+ declared: value,
271
+ supported: FORMAT_VERSION,
272
+ warning:
273
+ `this workspace targets foldrun format ${value}; this build understands ` +
274
+ `${FORMAT_VERSION}. Newer fields will be ignored rather than rejected.`,
275
+ };
276
+ }
277
+ return { declared: value, supported: FORMAT_VERSION, warning: null };
278
+ }
279
+
280
+ export function assertCanonicalCase(rel: string) {
281
+ const base = rel.split("/").pop() ?? rel;
282
+ for (const want of CANONICAL) {
283
+ if (base !== want && base.toLowerCase() === want.toLowerCase()) {
284
+ throw new Error(
285
+ `"${base}" must be spelled exactly "${want}" — it is a standard filename and the case is ` +
286
+ `part of it. Rename it and try again.`,
287
+ );
288
+ }
289
+ }
290
+ }
291
+
292
+ /**
293
+ * What the platform owns inside a workspace directory, in one place.
294
+ *
295
+ * Three separate mechanisms need this exact list and drifted apart while it
296
+ * lived in each of them: deploys must preserve these files, and the run
297
+ * executors (docker and k8s) must not copy them into a sandbox. A platform
298
+ * file added to one list and not the others is either silently wiped by the
299
+ * next deploy or silently handed to every container — both invisible until
300
+ * someone is bitten.
301
+ */
302
+ export const PLATFORM_FILES = ["secrets.json", "hooks.json", "hook-deliveries.jsonl", ".repo.json"];
303
+
304
+ /** True for anything the platform, not the author or the agent, writes. */
305
+ export function isPlatformPath(rel: string): boolean {
306
+ const norm = rel.replaceAll("\\", "/");
307
+ if (PLATFORM_FILES.includes(norm)) return true;
308
+ return norm === "runs" || norm.startsWith("runs/") || norm === ".foldrun" || norm.startsWith(".foldrun/");
309
+ }
310
+
311
+ export function saveWorkspace(
312
+ tenant: string,
313
+ workspace: string,
314
+ files: DeployFile[],
315
+ /** Attribution for the revision this save records. A deploy passes the
316
+ * commit, which becomes the revision's id. */
317
+ meta: ChangeMeta & { commit?: string | null } = {},
318
+ ) {
319
+ if (files.length > 500) throw new Error("too many files");
320
+ for (const f of files) {
321
+ const norm = path.normalize(f.path);
322
+ if (norm.startsWith("..") || path.isAbsolute(norm) || norm.startsWith("runs")) {
323
+ throw new Error(`illegal file path: ${f.path}`);
324
+ }
325
+ assertCanonicalCase(norm);
326
+ if (!IN_WORKSPACE_DIR.test(norm) && norm !== "project.md" && norm !== "AGENTS.md") {
327
+ throw new Error(
328
+ `unexpected file: ${f.path} — a workspace holds AGENTS.md plus ` +
329
+ `${WORKSPACE_DIRS.map((d) => `${d}/`).join(", ")}`,
330
+ );
331
+ }
332
+ }
333
+
334
+ // Executable: an agent's own scripts/, or the workspace-level shared scripts/.
335
+ const isScript = (rel: string) => {
336
+ const norm = path.normalize(rel);
337
+ return (
338
+ /^agents\/[^/]+\/scripts\//.test(norm) || // an agent's own scripts
339
+ /^agents\/[^/]+\/skills\/[^/]+\/scripts\//.test(norm) || // scripts bundled in a skill
340
+ /^scripts\//.test(norm) // workspace-wide shared scripts
341
+ );
342
+ };
343
+
344
+ const dir = workspaceDir(tenant, workspace);
345
+
346
+ // The scope above has to exist before anything under it runs, and this is
347
+ // the only chokepoint every hosted workspace passes through — the dashboard's
348
+ // New button, the API, and `git push` deploys all land here.
349
+ ensureAccountFiles(tenant);
350
+
351
+ // A deploy replaces the workspace, but it must not destroy what it never
352
+ // owned. Git is authoritative for the files it ships; the platform owns
353
+ // everything an agent produced at run time. So:
354
+ //
355
+ // runs/ always kept — the audit trail
356
+ // state/ always kept — what an agent carries between runs
357
+ // storage/ always kept — the file store's run mirror, which no
358
+ // deploy can ship (a deploy carries source, and bytes
359
+ // are not source) and every run expects to find. Note
360
+ // the pre-rename `files/` is NOT protected here: a
361
+ // workspace still holding one loses it on deploy, which
362
+ // is why adoptLegacyFilesDir runs on the read paths.
363
+ // memory/*.md kept when the deploy doesn't ship that file
364
+ //
365
+ // That last rule is the subtle one. Memory is written from both sides: a
366
+ // human commits a fact, and an agent learns one. Keeping only the files the
367
+ // deploy *didn't* mention means deleting a memory in git deletes it on the
368
+ // server, while a memory an agent wrote survives a push that never knew
369
+ // about it. Before this, every deploy silently erased everything the agents
370
+ // had learned.
371
+ const shipped = new Set(files.map((f) => path.normalize(f.path)));
372
+ const isAgentOwned = (rel: string) =>
373
+ // Platform bookkeeping (the vault, hook rotation state, the delivery
374
+ // log, run history) — losing any of it on deploy silently breaks
375
+ // something: agents lose secrets, a rotated hook un-rotates.
376
+ isPlatformPath(rel) ||
377
+ /(^|\/)state\//.test(rel) ||
378
+ rel === STORAGE_DIR ||
379
+ rel.startsWith(`${STORAGE_DIR}/`) ||
380
+ (/(^|\/)memory\/[^/]+\.md$/.test(rel) && !shipped.has(rel));
381
+
382
+ // What is there now, for the revision: every editable file's content
383
+ // before this save replaces the tree.
384
+ const before = new Map<string, string>();
385
+ if (fs.existsSync(dir)) {
386
+ for (const rel of listWorkspaceFiles(tenant, workspace)) {
387
+ try {
388
+ before.set(rel, fs.readFileSync(path.join(dir, rel), "utf8"));
389
+ } catch {
390
+ // vanished mid-read; not part of the revision
391
+ }
392
+ }
393
+ }
394
+
395
+ const snapshot = fs.existsSync(dir) ? fs.mkdtempSync(path.join(dataRoot(), ".keep-")) : null;
396
+ const preserved: string[] = [];
397
+ if (snapshot) {
398
+ for (const entry of fs.readdirSync(dir, { recursive: true })) {
399
+ const rel = String(entry).split(path.sep).join("/");
400
+ const from = path.join(dir, rel);
401
+ try {
402
+ if (!fs.statSync(from).isFile() || !isAgentOwned(rel)) continue;
403
+ } catch {
404
+ continue; // a broken link or a file that vanished mid-deploy
405
+ }
406
+ const to = path.join(snapshot, rel);
407
+ fs.mkdirSync(path.dirname(to), { recursive: true });
408
+ fs.copyFileSync(from, to);
409
+ preserved.push(rel);
410
+ }
411
+ }
412
+
413
+ fs.rmSync(dir, { recursive: true, force: true });
414
+ fs.mkdirSync(dir, { recursive: true });
415
+
416
+ for (const f of files) {
417
+ const target = path.join(dir, path.normalize(f.path));
418
+ fs.mkdirSync(path.dirname(target), { recursive: true });
419
+ fs.writeFileSync(target, f.content);
420
+ // Anything under an agent's scripts/ is meant to be run.
421
+ if (isScript(f.path)) fs.chmodSync(target, 0o755);
422
+ }
423
+
424
+ // Restore after writing, so a deploy never overwrites preserved state.
425
+ if (snapshot) {
426
+ for (const rel of preserved) {
427
+ const to = path.join(dir, rel);
428
+ fs.mkdirSync(path.dirname(to), { recursive: true });
429
+ fs.copyFileSync(path.join(snapshot, rel), to);
430
+ }
431
+ fs.rmSync(snapshot, { recursive: true, force: true });
432
+ }
433
+
434
+ // The revision: shipped files against what they replaced, plus anything
435
+ // that was there and is not any more (and was not preserved).
436
+ const keep = new Set(preserved);
437
+ const changes: RevisionFile[] = files.map((f) => {
438
+ const rel = path.normalize(f.path).split(path.sep).join("/");
439
+ return { path: rel, before: before.get(rel) ?? null, after: f.content };
440
+ });
441
+ for (const [rel, content] of before) {
442
+ if (!shipped.has(rel) && !keep.has(rel)) changes.push({ path: rel, before: content, after: null });
443
+ }
444
+ recordRevision(tenant, workspace, changes, {
445
+ by: meta.by ?? (meta.commit ? "deploy" : "system"),
446
+ message: meta.message ?? (meta.commit ? `deployed ${meta.commit.slice(0, 7)}` : undefined),
447
+ commit: meta.commit ?? null,
448
+ });
449
+
450
+ // The files are conformant concepts the moment they land; the bundles around
451
+ // them are not bundles until their index.md exists to carry okf_version. A
452
+ // deploy writes straight to disk, so nothing else would generate them.
453
+ syncWorkspaceBundles(dir);
454
+
455
+ return { preserved: preserved.length };
456
+ }
457
+
458
+ // The prompt-facing index for an OKF bundle (memory/ or knowledge/).
459
+ //
460
+ // The on-disk artifact is index.md, written by syncIndex and conformant with
461
+ // the spec. This builds the same listing with paths the agent can actually
462
+ // open, plus the v0.2 signals that change how a fact should be treated: a
463
+ // stale price list or an unverified claim should not read like a confirmed
464
+ // one.
465
+ //
466
+ // There is no curated preamble. A hand-written MEMORY.md used to be read in
467
+ // ahead of this listing, and it was a reserved name we invented inside someone
468
+ // else's format — a consumer applying OKF's rules saw an untyped concept and
469
+ // rejected the bundle. Shared prose belongs in AGENTS.md, which is already the
470
+ // place for context every agent here gets.
471
+ export function buildMemoryIndex(dir: string, prefix = ""): string | null {
472
+ if (!fs.existsSync(dir)) return null;
473
+
474
+ const lines: string[] = [];
475
+ for (const doc of readBundle(dir)) {
476
+ const marks = [
477
+ doc.type && doc.type !== "Memory" ? doc.type : null,
478
+ doc.status !== "stable" ? doc.status : null,
479
+ doc.stale ? `STALE since ${doc.staleAfter} — confirm before relying on it` : null,
480
+ // Same signals as the on-disk index, one definition — see provenanceMarks.
481
+ // This is the copy a model reads mid-run, so it is the one that decides
482
+ // whether it treats its own earlier guess as a fact.
483
+ ...provenanceMarks(doc),
484
+ ].filter(Boolean);
485
+ lines.push(
486
+ `- [${doc.title}](${prefix}${doc.file})` +
487
+ (doc.description ? ` — ${doc.description}` : "") +
488
+ (marks.length ? ` _(${marks.join(", ")})_` : ""),
489
+ );
490
+ }
491
+
492
+ return lines.length ? lines.join("\n") : null;
493
+ }
494
+
495
+ // Workspace-scoped shared assets: skills/, memory/, tools/ sit beside agents/
496
+ // and are available to every agent in that workspace.
497
+ export function workspaceMemoryIndex(tenant: string, workspace: string): string | null {
498
+ return buildMemoryIndex(path.join(workspaceDir(tenant, workspace), "memory"), "../../memory/");
499
+ }
500
+
501
+ // Knowledge is what you *gave* an agent — stable reference material an agent
502
+ // reads and never rewrites. Memory is what it *learned* and authors itself.
503
+ // Keeping them apart matters: a price list an agent silently edits is a bug,
504
+ // and a fact it discovered that you have to maintain by hand is a chore.
505
+ export function knowledgeIndex(dir: string, prefix = ""): string | null {
506
+ return buildMemoryIndex(dir, prefix);
507
+ }
508
+
509
+ export function workspaceKnowledgeIndex(tenant: string, workspace: string): string | null {
510
+ return knowledgeIndex(path.join(workspaceDir(tenant, workspace), "knowledge"), "../../knowledge/");
511
+ }
512
+
513
+ // One noun for capability. A tools/<name>.md file declares either an HTTP
514
+ // endpoint or a script — `type:` says which, and it's inferred when omitted
515
+ // (a `base:` means http, a `run:` means script). Both are granted the same
516
+ // way, with `tools: [name]`, so "what can this agent do?" has one answer in one
517
+ // place instead of three (`tools:`, `scripts:`, `apis:`).
518
+ // An MCP server an agent can connect to. MCP is the cross-vendor standard for
519
+ // tools (Anthropic, OpenAI, Google), so accepting a server here is what lets
520
+ // someone reuse the ecosystem instead of re-describing an integration we
521
+ // already have a format for. Both transports the SDK supports are allowed:
522
+ // a local process (`command`) or a remote endpoint (`url`).
523
+ const asRecord = (v: unknown): Record<string, string> =>
524
+ v && typeof v === "object" && !Array.isArray(v)
525
+ ? Object.fromEntries(Object.entries(v as Record<string, unknown>).map(([k, x]) => [k, String(x)]))
526
+ : {};
527
+
528
+ export interface McpSpec {
529
+ name: string;
530
+ description: string;
531
+ command?: string;
532
+ args: string[];
533
+ env: Record<string, string>; // values may contain ${SECRET_NAME}
534
+ url?: string;
535
+ headers: Record<string, string>;
536
+ }
537
+
538
+ export type ToolDef =
539
+ | { kind: "http"; name: string; spec: ApiSpec }
540
+ | { kind: "script"; name: string; spec: Record<string, unknown> }
541
+ | { kind: "mcp"; name: string; spec: McpSpec };
542
+
543
+ /**
544
+ * The program inside a single-file script tool: the first fenced block whose
545
+ * language tag names a runnable language. By tag, not by position — a body
546
+ * legitimately opens with a \`\`\`yaml example of the tools: line, and running
547
+ * the documentation would be a memorable bug. Untagged fences never qualify:
548
+ * "which block is the program" must be answerable by reading, not guessing.
549
+ */
550
+ export function fencedCode(body: string): { code: string; ext: string } | null {
551
+ const block = fencedCodeBlock(body);
552
+ return block ? { code: block.code, ext: block.ext } : null;
553
+ }
554
+
555
+ /**
556
+ * The same block, with where it sits — for the migration that lifts a
557
+ * single-file tool's program out into a file beside it. Extracting the code
558
+ * and then removing it from the body are the same question asked twice, and
559
+ * a second copy of the language table is how they would come to disagree.
560
+ */
561
+ export function fencedCodeBlock(
562
+ body: string,
563
+ ): { code: string; ext: string; start: number; end: number } | null {
564
+ const EXT: Record<string, string> = {
565
+ js: ".mjs", mjs: ".mjs", javascript: ".mjs", ts: ".mjs",
566
+ python: ".py", py: ".py", bash: ".sh", sh: ".sh", ruby: ".rb", rb: ".rb",
567
+ };
568
+ for (const m of body.matchAll(/```([a-zA-Z0-9]+)\r?\n([\s\S]*?)```/g)) {
569
+ const ext = EXT[m[1].toLowerCase()];
570
+ if (ext && m[2].trim()) {
571
+ return { code: m[2], ext, start: m.index!, end: m.index! + m[0].length };
572
+ }
573
+ }
574
+ return null;
575
+ }
576
+
577
+ export function parseToolDef(data: Record<string, unknown>, fallbackName: string, body?: string): ToolDef | null {
578
+ const name = typeof data.name === "string" ? data.name : fallbackName;
579
+ // `transport:`, or a legacy `type: http|script|mcp` from before `type:` meant
580
+ // the document. `type: Tool` says what the file is and deliberately says
581
+ // nothing about how it connects, so it falls through to inference below.
582
+ const declared = readTransport(data);
583
+ // Inferred when neither is present, so simple files stay simple.
584
+ const kind =
585
+ declared ??
586
+ (typeof data.command === "string" || typeof data.url === "string"
587
+ ? "mcp"
588
+ : typeof data.run === "string"
589
+ ? "script"
590
+ : "http");
591
+
592
+ if (kind === "mcp") {
593
+ const command = typeof data.command === "string" ? data.command : undefined;
594
+ const url = typeof data.url === "string" ? data.url : undefined;
595
+ if (!command && !url) return null;
596
+ return {
597
+ kind: "mcp",
598
+ name,
599
+ spec: {
600
+ name,
601
+ description: typeof data.description === "string" ? data.description : "",
602
+ command,
603
+ args: Array.isArray(data.args) ? data.args.map(String) : [],
604
+ env: asRecord(data.env),
605
+ url,
606
+ headers: asRecord(data.headers),
607
+ },
608
+ };
609
+ }
610
+ if (kind === "script") {
611
+ // The program is a run: path, or the file's own fenced code block — the
612
+ // single-file form, which is what lets a script tool read and edit like
613
+ // every other markdown document.
614
+ if (typeof data.run === "string") return { kind: "script", name, spec: { ...data, name } };
615
+ const inline = body ? fencedCode(body) : null;
616
+ if (!inline) return null;
617
+ return { kind: "script", name, spec: { ...data, name, code: inline.code, codeExt: inline.ext } };
618
+ }
619
+ const [spec] = parseApis([{ ...data, name }]);
620
+ return spec ? { kind: "http", name, spec } : null;
621
+ }
622
+
623
+ /**
624
+ * Tools in a directory, in both shapes.
625
+ *
626
+ * tools/email.md flat — a definition and nothing else
627
+ * tools/bounce-verify/ folder — tool.md plus the code it runs
628
+ * tool.md
629
+ * run.mjs
630
+ *
631
+ * The folder is the shape to write. A script tool is a definition and an
632
+ * executable that cannot be separated — point `run:` at the wrong scope and
633
+ * the tool silently resolves to nothing — so keeping them in one directory
634
+ * is what stops the two from ever disagreeing. It is also how skills already
635
+ * work, which is the pattern people have seen.
636
+ *
637
+ * A folder tool's `run:` is relative to its own folder, and rewritten here
638
+ * to the scope-qualified path the runner resolves. That is the point of the
639
+ * shape: the definition never names the scope it lives in, so the same
640
+ * folder works copied into a workspace or installed at the account.
641
+ *
642
+ * Flat files keep working unchanged — the shape is a migration, not a break.
643
+ */
644
+ function readToolDir(dir: string, scope: "workspace" | "account" = "workspace"): Record<string, ToolDef> {
645
+ if (!fs.existsSync(dir)) return {};
646
+ const out: Record<string, ToolDef> = {};
647
+ const broken: string[] = [];
648
+
649
+ const add = (raw: string, fallbackName: string, folder: string | null) => {
650
+ try {
651
+ const { data, content } = matter(raw);
652
+ // A COPY, because the qualification below is a mutation and the object
653
+ // is not ours: gray-matter caches its result by the input string, so two
654
+ // byte-identical definitions at different scopes are handed the same
655
+ // `data` object. Mutating it in place stamped the first reader's scope
656
+ // onto the second — an account tool whose `run:` came back
657
+ // `workspace/tools/…`, resolving to a path in whichever workspace was
658
+ // read first, and therefore to nothing. Silent, and dependent on read
659
+ // order, which is the worst pair of properties a bug can have.
660
+ //
661
+ // Real case: the same `browser` tool installed at the account and in a
662
+ // workspace, which is exactly what installing one gallery tool twice
663
+ // produces.
664
+ const d = { ...(data as Record<string, unknown>) };
665
+ // A folder tool's code sits beside its definition; qualify the path so
666
+ // the runner can find it from an agent directory two levels down.
667
+ if (folder && typeof d.run === "string" && !/^(workspace|account|shared|library)\//.test(d.run)) {
668
+ d.run = `${scope}/tools/${folder}/${d.run.replace(/^\.\//, "")}`;
669
+ }
670
+ const def = parseToolDef(d, fallbackName, content);
671
+ if (def) out[def.name] = def;
672
+ else broken.push(`${fallbackName}: no base, run, command or fenced code block — nothing to call`);
673
+ } catch (err) {
674
+ // A malformed definition must not fail every run in the workspace. But
675
+ // it must not vanish either: dropped silently, a tool with a YAML typo
676
+ // is indistinguishable from one that was never created, and the agent
677
+ // that opted into it is told the tool "is not in the workspace" —
678
+ // which sends its author looking in the wrong place entirely.
679
+ broken.push(`${fallbackName}: ${err instanceof Error ? err.message.split("\n")[0] : "unreadable"}`);
680
+ }
681
+ };
682
+
683
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
684
+ if (entry.isDirectory()) {
685
+ const manifest = path.join(dir, entry.name, "tool.md");
686
+ if (fs.existsSync(manifest)) add(fs.readFileSync(manifest, "utf8"), entry.name, entry.name);
687
+ continue;
688
+ }
689
+ if (!entry.name.endsWith(".md")) continue;
690
+ add(fs.readFileSync(path.join(dir, entry.name), "utf8"), entry.name.replace(/\.md$/, ""), null);
691
+ }
692
+ // Reported through the definitions map so a caller that only wants tools
693
+ // can ignore it, and the runner can say what is actually wrong.
694
+ brokenTools.set(dir, broken);
695
+ return out;
696
+ }
697
+
698
+ /** Why a tool in this directory failed to load, keyed by directory. */
699
+ const brokenTools = new Map<string, string[]>();
700
+
701
+ /** Definitions that exist on disk but could not be read, for the scopes an
702
+ * agent draws on. Empty when everything parsed. */
703
+ export function brokenToolReport(tenant: string, workspace: string): string[] {
704
+ const dirs = [
705
+ path.join(workspaceDir(tenant, workspace), "tools"),
706
+ path.join(dataRoot(), tenant, "library", "tools"),
707
+ ];
708
+ return dirs.flatMap((d) => brokenTools.get(d) ?? []);
709
+ }
710
+
711
+ export function workspaceTools(tenant: string, workspace: string): Record<string, ToolDef> {
712
+ return readToolDir(path.join(workspaceDir(tenant, workspace), "tools"), "workspace");
713
+ }
714
+
715
+ export { readToolDir };
716
+
717
+ // Files under agents/<name>/scripts/ — the agent's own tooling, runnable
718
+ // with the bash tool.
719
+ export function agentScripts(tenant: string, workspace: string, agent: string): string[] {
720
+ const dir = path.join(workspaceDir(tenant, workspace), "agents", agent, "scripts");
721
+ if (!fs.existsSync(dir)) return [];
722
+ const out: string[] = [];
723
+ for (const entry of fs.readdirSync(dir, { recursive: true })) {
724
+ const rel = String(entry);
725
+ if (fs.statSync(path.join(dir, rel)).isFile()) out.push(rel);
726
+ }
727
+ return out.sort();
728
+ }
729
+
730
+ // An API an agent may call. The platform turns each one into a single tool
731
+ // scoped to `base`, resolving ${SECRET} placeholders server-side so the
732
+ // agent never sees credentials.
733
+ export interface ApiSpec {
734
+ name: string;
735
+ base: string;
736
+ description: string;
737
+ headers: Record<string, string>; // values may contain ${SECRET_NAME}
738
+ query: Record<string, string>; // always-appended query params (e.g. api keys)
739
+ methods: string[]; // allowed HTTP methods; default GET only
740
+ /** Seconds a call may take, when the tool.md says so. Unset: no clock. */
741
+ timeout?: number;
742
+ /** An OpenAPI 3.x document describing the API: a path relative to the
743
+ * workspace root (`tools/hubspot/openapi.json`, JSON or YAML) or an
744
+ * https URL. Each operation becomes its own typed tool. */
745
+ openapi?: string;
746
+ /** Which of the document's operations to expose — operationIds or
747
+ * `"METHOD /path"` strings. Absent: every operation, capped at 60.
748
+ * Present: only these, and the generic `call_<api>` tool is withheld. */
749
+ operations?: string[];
750
+ /** Calls per second this API may see from one step: `count` is the burst
751
+ * the bucket holds, `perSec` the refill rate. `"5/s"` → {5, 5};
752
+ * `"100/m"` → {100, 1.67}. A call over the limit waits, never fails. */
753
+ rate?: { count: number; perSec: number };
754
+ /** The operations the `openapi:` document yielded, filled in by
755
+ * attachOperations (openapi.ts) before the step is assembled. parseApis
756
+ * leaves it unset. Plain JSON: this crosses into the run container. */
757
+ resolvedOperations?: import("./openapi.ts").OperationSpec[];
758
+ }
759
+
760
+ export interface AgentInfo {
761
+ name: string;
762
+ description: string;
763
+ model: string;
764
+ /** The other half of model selection. listAgents has always returned it;
765
+ * the interface did not say so, so every consumer had to re-read the file
766
+ * to show it. */
767
+ effort: string | null;
768
+ tools: string[];
769
+ apis: ApiSpec[];
770
+ /** The author's own tools named in `tools:` — the subset that is not a
771
+ * built-in. Same list as `tools`, minus what the runtime provides. */
772
+ ownTools: string[];
773
+ /** Names still written under the removed `use:` key. Nothing is granted
774
+ * for them; `foldrun check` and the run log say what to write instead. */
775
+ legacyUse: string[];
776
+ /** Colleagues this agent may consult mid-run (`agents:`) — each becomes a
777
+ * consult_<name> tool. The team's edges, as the author drew them. */
778
+ consults: string[];
779
+ /** The `skills:` allowlist, or null when the field is absent — which is
780
+ * not the same thing: absent inherits every skill in scope, an empty
781
+ * list withholds all of them. */
782
+ skills: string[] | null;
783
+ secrets: string[];
784
+ }
785
+
786
+ // Recognised HTTP verbs a tool may declare. Deliberately NOT named
787
+ // "safe methods" — in HTTP that term means "no side effects" (GET, HEAD), and
788
+ // DELETE is the opposite. This is an allowlist: unknown or exotic verbs are
789
+ // dropped rather than passed to fetch, and a tool that declares none is
790
+ // read-only, because the least dangerous thing should happen when a file says
791
+ // nothing.
792
+ const ALLOWED_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
793
+
794
+ // Model selection. Tiers are the portable default — they keep working as
795
+ // models are renamed. An explicit alias or full model id is passed straight
796
+ // through for callers who want to pin one.
797
+ export const MODEL_TIERS: Record<string, string> = {
798
+ fast: "haiku",
799
+ default: "sonnet",
800
+ max: "opus",
801
+ };
802
+
803
+ // The same three tiers under every name someone reasonably reaches for.
804
+ // Nobody should have to learn our vocabulary to pick a model, and the
805
+ // alternative to accepting `small` is worse than pedantic: an unrecognised
806
+ // word is passed through as a model id, so `model: small` would reach the
807
+ // provider as a model called "small" and fail the step at run time, on a
808
+ // file that reads perfectly. A synonym table is cheaper than that error.
809
+ const MODEL_ALIASES: Record<string, keyof typeof MODEL_TIERS> = {
810
+ fast: "fast",
811
+ small: "fast",
812
+ cheap: "fast",
813
+ mini: "fast",
814
+ light: "fast",
815
+ low: "fast",
816
+ haiku: "fast",
817
+
818
+ default: "default",
819
+ standard: "default",
820
+ base: "default",
821
+ balanced: "default",
822
+ mid: "default",
823
+ medium: "default",
824
+ normal: "default",
825
+ sonnet: "default",
826
+
827
+ max: "max",
828
+ large: "max",
829
+ big: "max",
830
+ best: "max",
831
+ smart: "max",
832
+ high: "max",
833
+ deep: "max",
834
+ opus: "max",
835
+ };
836
+
837
+ export type Tier = keyof typeof MODEL_TIERS;
838
+
839
+ /** Which tier a word names, or null if it names none. Exported because the
840
+ * provider block keys a remap table by tier and should accept every word
841
+ * `model:` does — one vocabulary, not two. */
842
+ export function resolveTier(value: unknown): Tier | null {
843
+ if (typeof value !== "string" || !value.trim()) return null;
844
+ return MODEL_ALIASES[value.trim().toLowerCase()] ?? null;
845
+ }
846
+
847
+ export function resolveModel(value: unknown): string {
848
+ const raw = typeof value === "string" && value.trim() ? value.trim() : "default";
849
+ const tier = resolveTier(raw);
850
+ if (tier) return MODEL_TIERS[tier];
851
+ return raw; // "claude-opus-5" | a provider's own id | …
852
+ }
853
+
854
+ // Effort is the other half of model selection, and the orthogonal one: the
855
+ // model is which brain, effort is how long it thinks before answering. The
856
+ // five levels are the SDK's own union — we neither invent our own scale nor
857
+ // narrow theirs, so a level added upstream is one line here.
858
+ export const EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"] as const;
859
+ export type Effort = (typeof EFFORT_LEVELS)[number];
860
+
861
+ const EFFORT_ALIASES: Record<string, Effort> = {
862
+ low: "low",
863
+ min: "low",
864
+ minimal: "low",
865
+ lowest: "low",
866
+ fast: "low",
867
+ quick: "low",
868
+
869
+ medium: "medium",
870
+ med: "medium",
871
+ mid: "medium",
872
+ moderate: "medium",
873
+ normal: "medium",
874
+ balanced: "medium",
875
+
876
+ high: "high",
877
+ default: "high", // the SDK's default, said out loud
878
+ deep: "high",
879
+
880
+ xhigh: "xhigh",
881
+ "x-high": "xhigh",
882
+ "extra-high": "xhigh",
883
+ extrahigh: "xhigh",
884
+ "very-high": "xhigh",
885
+ veryhigh: "xhigh",
886
+ higher: "xhigh",
887
+
888
+ max: "max",
889
+ maximum: "max",
890
+ highest: "max",
891
+ full: "max",
892
+ };
893
+
894
+ /**
895
+ * `null` for both "not set" and "not a level we know" — unlike a model, the
896
+ * set is closed, so passing an unrecognised word through would be a 400 from
897
+ * the API rather than someone's pinned choice. Callers that can report say
898
+ * which it was with `isEffortWord`.
899
+ */
900
+ export function resolveEffort(value: unknown): Effort | null {
901
+ if (typeof value !== "string" || !value.trim()) return null;
902
+ return EFFORT_ALIASES[value.trim().toLowerCase()] ?? null;
903
+ }
904
+
905
+ /** True when a non-empty `effort:` was written but names no level — the
906
+ * one case worth a line in the run trace, because silence there looks
907
+ * exactly like the setting having been applied. */
908
+ export function isEffortWord(value: unknown): boolean {
909
+ return typeof value === "string" && value.trim().length > 0 && resolveEffort(value) === null;
910
+ }
911
+
912
+ const RATE_UNIT_SECONDS: Record<string, number> = {
913
+ s: 1, sec: 1, second: 1,
914
+ m: 60, min: 60, minute: 60,
915
+ h: 3600, hr: 3600, hour: 3600,
916
+ };
917
+
918
+ /** `"5/s"`, `"100/m"`, `"1000/h"` → a bucket size and refill rate. Null for
919
+ * anything else: an unreadable limit is ignored rather than guessed. */
920
+ export function parseRate(value: unknown): { count: number; perSec: number } | null {
921
+ if (typeof value === "number" && Number.isFinite(value) && value > 0) {
922
+ return { count: value, perSec: value };
923
+ }
924
+ if (typeof value !== "string") return null;
925
+ const m = /^\s*(\d+(?:\.\d+)?)\s*\/\s*([a-z]+)\s*$/i.exec(value);
926
+ if (!m) return null;
927
+ const count = Number(m[1]);
928
+ const unit = RATE_UNIT_SECONDS[m[2].toLowerCase()];
929
+ if (!(count > 0) || !unit) return null;
930
+ return { count, perSec: count / unit };
931
+ }
932
+
933
+ // apis: frontmatter:
934
+ // apis:
935
+ // - name: google_ads
936
+ // base: https://googleads.googleapis.com/v18
937
+ // description: Google Ads REST API. Use searchStream for metrics.
938
+ // methods: [GET, POST]
939
+ // headers:
940
+ // Authorization: Bearer ${GOOGLE_ADS_TOKEN}
941
+ // developer-token: ${GOOGLE_ADS_DEV_TOKEN}
942
+ // openapi: tools/google_ads/openapi.yaml # optional: typed tools
943
+ // operations: [searchStream, "GET /customers/{id}"]
944
+ // rate: 10/s # or 100/m, 1000/h
945
+
946
+ export function parseApis(raw: unknown): ApiSpec[] {
947
+ if (!Array.isArray(raw)) return [];
948
+ const out: ApiSpec[] = [];
949
+ for (const entry of raw) {
950
+ if (!entry || typeof entry !== "object") continue;
951
+ const e = entry as Record<string, unknown>;
952
+ const name = typeof e.name === "string" ? e.name : null;
953
+ const base = typeof e.base === "string" ? e.base : null;
954
+ if (!name || !base) continue;
955
+ const asRecordLocal = (v: unknown): Record<string, string> =>
956
+ v && typeof v === "object" && !Array.isArray(v)
957
+ ? Object.fromEntries(Object.entries(v as Record<string, unknown>).map(([k, x]) => [k, String(x)]))
958
+ : {};
959
+ const methods = Array.isArray(e.methods)
960
+ ? e.methods.map((m) => String(m).toUpperCase()).filter((m) => ALLOWED_METHODS.includes(m))
961
+ : ["GET"];
962
+ const rate = parseRate(e.rate);
963
+ const operations = Array.isArray(e.operations)
964
+ ? e.operations.map((o) => String(o).trim()).filter(Boolean)
965
+ : null;
966
+ out.push({
967
+ name: name.replace(/[^a-zA-Z0-9_]/g, "_"),
968
+ ...(Number(e.timeout) > 0 ? { timeout: Number(e.timeout) } : {}),
969
+ base: base.replace(/\/+$/, ""),
970
+ description: typeof e.description === "string" ? e.description : "",
971
+ headers: asRecordLocal(e.headers),
972
+ query: asRecordLocal(e.query),
973
+ methods: methods.length ? methods : ["GET"],
974
+ ...(typeof e.openapi === "string" && e.openapi.trim() ? { openapi: e.openapi.trim() } : {}),
975
+ // An explicit list, even an empty one, means "only these" — the
976
+ // generic tool is withheld. Absent means "everything, plus call_<api>".
977
+ ...(operations ? { operations } : {}),
978
+ ...(rate ? { rate } : {}),
979
+ });
980
+ }
981
+ return out;
982
+ }
983
+
984
+ export interface FlowStep {
985
+ agent: string;
986
+ instruction: string;
987
+ group: number; // steps sharing a group number run in parallel
988
+ optional: boolean; // "2?" — failure doesn't fail the flow
989
+ /** Set when the step targets another flow (`[[flow:name]]`) instead of an agent. */
990
+ subflow?: string;
991
+ /** Overrides the agent's own model for this step only. */
992
+ model?: string;
993
+ /** Overrides the agent's own effort for this step only — the expensive
994
+ * step in an otherwise cheap flow is exactly what this is for. */
995
+ effort?: string;
996
+ /** Pause for a human before running this step. */
997
+ approve?: boolean;
998
+ /** Run only if the previous results contain this text (case-insensitive).
999
+ * Independent: every matching `when:` step in a group runs. */
1000
+ when?: string;
1001
+ /** Exclusive routing: of a group's `case:` steps, only the FIRST whose
1002
+ * text appears in the previous results runs; the rest are routed past. */
1003
+ case?: string;
1004
+ /** The route of last resort: runs only when none of its group's `case:`
1005
+ * steps matched. */
1006
+ else?: boolean;
1007
+ /** Attempts on failure, beyond the first. */
1008
+ retry?: number;
1009
+ /** Abandon the step after this many seconds. */
1010
+ timeout?: number;
1011
+ /** Another agent takes the step over when it fails: same instruction, the
1012
+ * failure as context. Success continues the flow with the rescuer's
1013
+ * result; a second failure fails the step as it always did. */
1014
+ onFail?: string;
1015
+ /** Seconds to hold before this step runs — "wait: 3d" parses to 259200.
1016
+ * The run parks in the queue, not in a process. */
1017
+ waitSecs?: number;
1018
+ /** `wait: event` — hold until something outside POSTs the run's event
1019
+ * URL. Same parking as an approval, released by a machine instead of a
1020
+ * person; the body it sends reaches the step as its event payload. This
1021
+ * is what lets a flow send a quote, wait for the reply, then follow up,
1022
+ * without the platform becoming a chat product. */
1023
+ waitFor?: "event";
1024
+ /** For `each: rows` — the CSV whose rows fan the step out, agent-relative
1025
+ * like every other path an instruction uses. */
1026
+ eachPath?: string;
1027
+ /** A question for a human. The step parks like an approval; the answer
1028
+ * typed at the gate reaches the step's prompt. */
1029
+ ask?: string;
1030
+ /** What the gate shows before a person decides: paths under storage/,
1031
+ * globs allowed (`draft/*.mdx, draft/images/*.webp`). Declared in the
1032
+ * flow so the preview does not depend on the previous step remembering
1033
+ * to name its files. Resolved against storage/ when the run parks. */
1034
+ preview?: string[];
1035
+ /** Model-led delegation, bounded by declaration: this step's agent picks
1036
+ * which of THESE agents run next, one instruction each. The choice set
1037
+ * is the file's, the picks are the model's, the record shows both. */
1038
+ delegate?: string[];
1039
+ /** Evaluator loop: when this step's result lacks `until:`'s marker, wind
1040
+ * back to the previous group and go again — at most this many extra
1041
+ * cycles. Bounded by declaration, so the worst-case cost is still
1042
+ * readable off the file. */
1043
+ loop?: number;
1044
+ /** The marker (case-insensitive) whose presence in this step's result ends
1045
+ * the loop early. Required for `loop:` to mean anything. */
1046
+ until?: string;
1047
+ /** Fan-out: run one instance of this step per item of the previous
1048
+ * group's result. `lines` — one item per non-empty line of the prose;
1049
+ * `rows` — one per CSV row of a file; `items` — one per element of the
1050
+ * JSON array a previous `output: json` step returned. */
1051
+ each?: "lines" | "rows" | "items";
1052
+ /** Fan-out cap. Items beyond it are dropped, and the run log says so. */
1053
+ max?: number;
1054
+ /** A check run after the step; failing it fails the step. Either a shell
1055
+ * command (non-zero exit fails) or one of the eval assertions —
1056
+ * `contains: x`, `not-contains: x`, `matches: regex`, `file: path`,
1057
+ * `judge: sentence` — so a flow and an eval share one vocabulary. */
1058
+ verify?: string;
1059
+ /** The step returns data, not just prose: its reply must end with one JSON
1060
+ * value in a ```json block. The runner parses it, fails the step when it
1061
+ * cannot, and hands the value to the next group losslessly — the narrow
1062
+ * move the grammar ADR allowed instead of variables. */
1063
+ output?: "json";
1064
+ /** 1-indexed line in the flow file. Diagnostics without a line make you
1065
+ * search; every real linter emits file:line. */
1066
+ line?: number;
1067
+ }
1068
+
1069
+ export interface FlowInfo {
1070
+ name: string;
1071
+ file: string;
1072
+ trigger: string;
1073
+ schedule: string | null;
1074
+ timezone: string | null;
1075
+ /** The model every step in this flow runs on, unless the step names its
1076
+ * own. Nearest wins: step, then flow, then the agent's own frontmatter. */
1077
+ model: string | null;
1078
+ /** Same, for effort. */
1079
+ effort: string | null;
1080
+ /** What a new fire does while a run of this flow is still live:
1081
+ * "skip" doesn't start one, "queue" starts it but the worker holds it
1082
+ * until the live run finishes. Unset keeps today's behaviour (runs may
1083
+ * overlap) — a default that changed underfoot would surprise every flow
1084
+ * that legitimately runs in parallel. */
1085
+ overlap: "skip" | "queue" | null;
1086
+ /** The most one run of this flow may spend, in USD. A literal, never an
1087
+ * expression — the cap is readable off the file, which is the property
1088
+ * the whole grammar exists to keep. Checked between groups: the group
1089
+ * that crosses it is the last one that runs. Null: no cap of its own
1090
+ * (the workspace's monthly `budget:` still applies). */
1091
+ budget: number | null;
1092
+ /** `trigger: flow` — start when this other flow of the workspace finishes,
1093
+ * with `on:` saying which endings count. The finished run's summary and
1094
+ * result become this run's task. */
1095
+ after: string | null;
1096
+ on: "completed" | "failed" | "any";
1097
+ /** `trigger: once` — an ISO 8601 instant; fires the first tick at or after
1098
+ * it, once, then never again. */
1099
+ at: string | null;
1100
+ /** `trigger: watch` — a URL polled every `every:` seconds (default 15m);
1101
+ * fires when its content changes, with the new content as the task. */
1102
+ url: string | null;
1103
+ every: number | null;
1104
+ /** `trigger: webhook` may also demand a provider's signature on every
1105
+ * delivery: GitHub (X-Hub-Signature-256), Stripe (Stripe-Signature), Slack
1106
+ * (X-Slack-Signature, plus the url_verification handshake) or a plain
1107
+ * `hmac` (X-Signature, hex SHA-256 of the body). `signingSecret` is the
1108
+ * NAME of the vault secret holding the shared key, never its value. */
1109
+ signature: "github" | "stripe" | "slack" | "hmac" | null;
1110
+ signingSecret: string | null;
1111
+ /** `trigger: storage` — a path prefix under storage/; a file landing
1112
+ * there starts the flow with the path as its task. */
1113
+ path: string | null;
1114
+ steps: FlowStep[];
1115
+ }
1116
+
1117
+ export const FLOW_TRIGGERS = ["manual", "schedule", "webhook", "email", "flow", "once", "watch", "storage"] as const;
1118
+ export type FlowTrigger = (typeof FLOW_TRIGGERS)[number];
1119
+
1120
+ // Skills and memory files belonging to one agent — for the graph view and
1121
+ // [[ ]] autocomplete.
1122
+ export interface AgentAssets {
1123
+ skills: string[]; // skill names (frontmatter name or filename)
1124
+ memory: string[]; // memory file names under memory/
1125
+ }
1126
+
1127
+ export function agentAssets(tenant: string, workspace: string, agent: string): AgentAssets {
1128
+ const base = path.join(workspaceDir(tenant, workspace), "agents", agent);
1129
+ const nameOf = (file: string, fallback: string) => {
1130
+ try {
1131
+ return matter(fs.readFileSync(file, "utf8")).data.name ?? fallback;
1132
+ } catch {
1133
+ return fallback;
1134
+ }
1135
+ };
1136
+
1137
+ // Skills: folders with SKILL.md (Agent Skills standard) plus flat .md files.
1138
+ const skills: string[] = [];
1139
+ const skillsDir = path.join(base, "skills");
1140
+ if (fs.existsSync(skillsDir)) {
1141
+ for (const entry of fs.readdirSync(skillsDir).sort()) {
1142
+ const full = path.join(skillsDir, entry);
1143
+ if (fs.statSync(full).isDirectory()) {
1144
+ const skillMd = path.join(full, "SKILL.md");
1145
+ if (fs.existsSync(skillMd)) skills.push(nameOf(skillMd, entry));
1146
+ } else if (entry.endsWith(".md")) {
1147
+ skills.push(nameOf(full, entry.replace(/\.md$/, "")));
1148
+ }
1149
+ }
1150
+ }
1151
+
1152
+ const memory: string[] = [];
1153
+ const memoryDir = path.join(base, "memory");
1154
+ if (fs.existsSync(memoryDir)) {
1155
+ for (const f of fs.readdirSync(memoryDir).sort()) {
1156
+ if (f.endsWith(".md") && !["index.md", "log.md"].includes(f)) {
1157
+ memory.push(nameOf(path.join(memoryDir, f), f.replace(/\.md$/, "")));
1158
+ }
1159
+ }
1160
+ }
1161
+ return { skills, memory };
1162
+ }
1163
+
1164
+ export function listAgents(tenant: string, workspace: string): AgentInfo[] {
1165
+ const dir = path.join(workspaceDir(tenant, workspace), "agents");
1166
+ if (!fs.existsSync(dir)) return [];
1167
+ return fs
1168
+ .readdirSync(dir)
1169
+ .filter((n) => fs.existsSync(path.join(dir, n, "agent.md")))
1170
+ .sort()
1171
+ .map((name) => {
1172
+ const { data } = matter(fs.readFileSync(path.join(dir, name, "agent.md"), "utf8"));
1173
+ const tools = refNames(data.tools);
1174
+ return {
1175
+ name,
1176
+ description: data.description ?? "",
1177
+ model: data.model ?? "default",
1178
+ effort: data.effort ?? null,
1179
+ tools,
1180
+ apis: parseApis(data.apis),
1181
+ ownTools: ownToolNames(data),
1182
+ legacyUse: legacyUseNames(data),
1183
+ consults: refNames(data.agents),
1184
+ skills: data.skills === undefined ? null : refNames(data.skills),
1185
+ secrets: Array.isArray(data.secrets) ? data.secrets.map(String) : [],
1186
+ };
1187
+ });
1188
+ }
1189
+
1190
+ // A step targets an agent — `[[writer]]` — or another flow — `[[flow:digest]]`,
1191
+ // which is how one flow triggers another.
1192
+ const STEP_RE = /^\s*(\d+)([?!])?\.?\s+\[\[(flow:)?([a-z0-9-]+)\]\]\s*(?:[—–-]\s*)?(.*)$/;
1193
+
1194
+ const OPTION_RE = /^\s+([a-z-]+):\s*(.+)$/;
1195
+
1196
+ /** "90s", "30m", "4h", "3d" — or a bare number of seconds. Clamped to 30
1197
+ * days: a wait is a pause in a flow, not a second scheduler. */
1198
+ export function parseWait(value: string): number | undefined {
1199
+ const m = value.trim().match(/^(\d+(?:\.\d+)?)\s*([smhd]?)$/i);
1200
+ if (!m) return undefined;
1201
+ const mult = { "": 1, s: 1, m: 60, h: 3600, d: 86400 }[m[2].toLowerCase() as "" | "s" | "m" | "h" | "d"];
1202
+ const secs = Math.round(Number(m[1]) * mult);
1203
+ return secs > 0 ? Math.min(secs, 30 * 86400) : undefined;
1204
+ }
1205
+
1206
+ /** An ISO 8601 instant (YAML may already have parsed it into a Date), or
1207
+ * null for anything that is not one — a `once` flow with an unreadable
1208
+ * `at:` never fires, and `foldrun check` says so. */
1209
+ export function parseInstant(raw: unknown): string | null {
1210
+ if (!(typeof raw === "string" || raw instanceof Date)) return null;
1211
+ const d = new Date(raw);
1212
+ return Number.isNaN(d.getTime()) ? null : d.toISOString();
1213
+ }
1214
+
1215
+ /**
1216
+ * Strip the quotes a person wrapped a whole value in, and only those.
1217
+ *
1218
+ * `schedule: "0 8 * * *"` is quoted so YAML-minded readers are comfortable,
1219
+ * and the cron parser must not see the quotes. But the rule that did this
1220
+ * removed a leading OR a trailing quote independently, so a value that
1221
+ * merely *ended* with one lost it — and
1222
+ *
1223
+ * verify: test "$(cat ../../storage/draft/published.run)" = "$FOLDRUN_RUN_ID"
1224
+ *
1225
+ * reached bash unbalanced, which exited 2 with "unexpected EOF" on every
1226
+ * run. The step could never pass, whatever the agent did, and nothing said
1227
+ * why: the trace printed the command the parser had already broken.
1228
+ *
1229
+ * So a pair is only stripped when the value opens and closes with the same
1230
+ * quote AND contains no other instance of it — which is what "the whole
1231
+ * value is quoted" actually means. `"a" = "b"` keeps its quotes, because
1232
+ * they are two pairs, not one wrapper.
1233
+ */
1234
+ export function unquote(raw: string): string {
1235
+ const value = raw.trim();
1236
+ const q = value[0];
1237
+ if ((q !== '"' && q !== "'") || value.length < 2 || !value.endsWith(q)) return value;
1238
+ const inner = value.slice(1, -1);
1239
+ return inner.includes(q) ? value : inner;
1240
+ }
1241
+
1242
+ export function parseFlow(file: string, raw: string): FlowInfo {
1243
+ const { data, content } = matter(raw);
1244
+ const steps: FlowStep[] = [];
1245
+ // Frontmatter is stripped by matter(), so add it back to keep line numbers
1246
+ // pointing at the real file rather than the body.
1247
+ const offset = raw.slice(0, raw.length - content.length).split("\n").length - 1;
1248
+ let lineNo = 0;
1249
+ for (const line of content.split("\n")) {
1250
+ lineNo++;
1251
+ const m = line.match(STEP_RE);
1252
+ if (m) {
1253
+ steps.push({
1254
+ group: Number(m[1]),
1255
+ // Both "2? [[x]]" and an `approve: true` option are accepted; "!"
1256
+ // is shorthand for the common case of needing a human.
1257
+ optional: m[2] === "?",
1258
+ approve: m[2] === "!" || undefined,
1259
+ agent: m[4],
1260
+ subflow: m[3] ? m[4] : undefined,
1261
+ instruction: m[5].trim(),
1262
+ line: lineNo + offset,
1263
+ });
1264
+ continue;
1265
+ }
1266
+ // Indented options belong to the step above them.
1267
+ const opt = line.match(OPTION_RE);
1268
+ if (opt && steps.length) {
1269
+ const step = steps[steps.length - 1];
1270
+ const [, key, rawValue] = opt;
1271
+ const value = unquote(rawValue);
1272
+ if (key === "approve") step.approve = value !== "false";
1273
+ else if (key === "when") step.when = value;
1274
+ else if (key === "case") step.case = value;
1275
+ else if (key === "else") step.else = value !== "false";
1276
+ else if (key === "retry") step.retry = Math.min(5, Math.max(0, Number(value) || 0));
1277
+ else if (key === "timeout") step.timeout = Math.max(1, Number(value) || 0);
1278
+ else if (key === "verify") step.verify = value;
1279
+ else if (key === "output") step.output = /^json$/i.test(value) ? "json" : undefined;
1280
+ else if (key === "model") step.model = value;
1281
+ else if (key === "effort") step.effort = value;
1282
+ else if (key === "loop") step.loop = Math.min(5, Math.max(1, Number(value) || 0)) || undefined;
1283
+ else if (key === "until") step.until = value;
1284
+ else if (key === "each") {
1285
+ if (value === "lines") step.each = "lines";
1286
+ else if (value === "items") step.each = "items";
1287
+ else if (/^rows\b/.test(value)) {
1288
+ step.each = "rows";
1289
+ // "rows of ../../storage/leads.csv" — the path is the part after "of".
1290
+ step.eachPath = value.replace(/^rows(\s+of)?\s*/, "").trim() || undefined;
1291
+ }
1292
+ }
1293
+ else if (key === "on-fail" || key === "onfail") step.onFail = refNames(value)[0] ?? undefined;
1294
+ else if (key === "wait") {
1295
+ if (/^event$/i.test(value)) step.waitFor = "event";
1296
+ else step.waitSecs = parseWait(value);
1297
+ }
1298
+ else if (key === "ask") { step.ask = value; }
1299
+ else if (key === "preview") {
1300
+ step.preview = value.split(",").map((p) => p.trim().replace(/^(\.\.\/\.\.\/)?storage\//, "")).filter(Boolean);
1301
+ }
1302
+ else if (key === "delegate") step.delegate = refNames(value).slice(0, 5);
1303
+ else if (key === "max") step.max = Math.min(20, Math.max(1, Number(value) || 0)) || undefined;
1304
+ }
1305
+ }
1306
+ steps.sort((a, b) => a.group - b.group);
1307
+ return {
1308
+ name: data.name ?? file.replace(/\.md$/, ""),
1309
+ file,
1310
+ trigger: data.trigger ?? "manual",
1311
+ schedule: data.schedule ?? null,
1312
+ timezone: data.timezone ?? null,
1313
+ model: data.model ?? null,
1314
+ effort: data.effort ?? null,
1315
+ overlap: data.overlap === "skip" || data.overlap === "queue" ? data.overlap : null,
1316
+ budget: Number(data.budget) > 0 ? Number(data.budget) : null,
1317
+ // `after: [[flow:publish]]` — a link or a bare name, read the same way
1318
+ // as every other file-naming field (refs.ts).
1319
+ after: (refNames(data.after)[0] ?? "").replace(/^flow:/, "").trim() || null,
1320
+ on: data.on === "failed" || data.on === "any" ? data.on : "completed",
1321
+ at: parseInstant(data.at),
1322
+ url: typeof data.url === "string" ? data.url.trim() : null,
1323
+ every: typeof data.every === "string" || typeof data.every === "number" ? (parseWait(String(data.every)) ?? null) : null,
1324
+ signature: ["github", "stripe", "slack", "hmac"].includes(String(data.signature))
1325
+ ? (String(data.signature) as FlowInfo["signature"])
1326
+ : null,
1327
+ signingSecret:
1328
+ typeof data.signing_secret === "string"
1329
+ ? data.signing_secret.trim().replace(/^\$\{|\}$/g, "")
1330
+ : null,
1331
+ path: typeof data.path === "string" ? data.path.trim().replace(/^\/+/, "") : null,
1332
+ steps,
1333
+ };
1334
+ }
1335
+
1336
+ /** A live run of this flow — the fact overlap: decisions are made on. */
1337
+ export function flowHasLiveRun(tenant: string, workspace: string, flowName: string): boolean {
1338
+ return listRuns(tenant, workspace).some(
1339
+ (r) => r.flow === flowName && (r.status === "running" || r.status === "queued" || r.status === "awaiting-approval"),
1340
+ );
1341
+ }
1342
+
1343
+ // Rearranging a flow from the dashboard rewrites the numbers in the markdown
1344
+ // and nothing else. Working on raw text blocks rather than re-serialising the
1345
+ // parsed FlowInfo means instructions, option lines, frontmatter and any prose
1346
+ // survive a drag exactly as the author wrote them — the file stays theirs.
1347
+ //
1348
+ // `groups` is the new arrangement: an array of groups, each holding indices
1349
+ // into the flow's parsed `steps`. Steps sharing a group run in parallel.
1350
+ interface FlowBlock {
1351
+ group: number;
1352
+ lines: string[]; // the step line, plus its indented options and notes
1353
+ }
1354
+
1355
+ // Split a flow file into its frontmatter, any prose before the first step,
1356
+ // and one block per step. Blocks come back in the same order as parseFlow's
1357
+ // steps, so a block index and a step index mean the same thing.
1358
+ function splitFlowBlocks(raw: string) {
1359
+ const head = raw.match(/^(---\r?\n[\s\S]*?\r?\n---\r?\n?)/)?.[1] ?? "";
1360
+ const preamble: string[] = [];
1361
+ const blocks: FlowBlock[] = [];
1362
+ for (const line of raw.slice(head.length).split("\n")) {
1363
+ if (STEP_RE.test(line)) blocks.push({ group: Number(line.match(STEP_RE)![1]), lines: [line] });
1364
+ else if (blocks.length) blocks[blocks.length - 1].lines.push(line);
1365
+ else preamble.push(line);
1366
+ }
1367
+ // parseFlow sorts by group, and Array.prototype.sort is stable, so the same
1368
+ // sort here keeps block i aligned with steps[i].
1369
+ const ordered = blocks.map((b, i) => ({ ...b, i })).sort((a, b) => a.group - b.group || a.i - b.i);
1370
+ while (preamble.length && preamble[preamble.length - 1].trim() === "") preamble.pop();
1371
+ return { head, preamble, blocks: ordered as FlowBlock[] };
1372
+ }
1373
+
1374
+ function emitFlow(head: string, preamble: string[], groups: FlowBlock[][]): string {
1375
+ const out: string[] = [];
1376
+ groups.forEach((group, g) => {
1377
+ for (const block of group) {
1378
+ const [stepLine, ...rest] = block.lines;
1379
+ const m = stepLine.match(STEP_RE)!;
1380
+ const [, , marker = "", flowPrefix = "", target, instruction] = m;
1381
+ out.push(
1382
+ `${g + 1}${marker}. [[${flowPrefix}${target}]]${instruction.trim() ? ` — ${instruction.trim()}` : ""}`,
1383
+ );
1384
+ while (rest.length && rest[rest.length - 1].trim() === "") rest.pop();
1385
+ out.push(...rest);
1386
+ }
1387
+ });
1388
+ // Keep the blank line that conventionally follows frontmatter.
1389
+ const gap = preamble.length ? `${preamble.join("\n")}\n\n` : head ? "\n" : "";
1390
+ return `${head}${gap}${out.join("\n")}\n`;
1391
+ }
1392
+
1393
+ /**
1394
+ * Rewrite a flow's trigger in place — the dashboard's trigger picker.
1395
+ *
1396
+ * Line-level surgery on the frontmatter, not a YAML round-trip: everything
1397
+ * the author wrote (comments, key order, unrelated keys) survives verbatim,
1398
+ * and the diff is exactly the lines that changed. A flow with no
1399
+ * frontmatter gains the smallest one that can carry the trigger.
1400
+ */
1401
+ export function setFlowTrigger(
1402
+ raw: string,
1403
+ opts: { trigger: FlowTrigger; schedule?: string; timezone?: string },
1404
+ ): string {
1405
+ if (!(FLOW_TRIGGERS as readonly string[]).includes(opts.trigger)) {
1406
+ throw new Error(`unknown trigger "${opts.trigger}" — one of ${FLOW_TRIGGERS.join(", ")}`);
1407
+ }
1408
+ const wanted = new Map<string, string | null>();
1409
+ wanted.set("trigger", opts.trigger === "manual" ? null : opts.trigger); // manual is the default — say nothing
1410
+ if (opts.trigger === "schedule") {
1411
+ const cron = (opts.schedule ?? "").trim();
1412
+ // Shape only — the scheduler's parseCron is the authority, and the API
1413
+ // route runs it; importing it here would cycle store ↔ scheduler.
1414
+ const shaped = /^@\w+$/.test(cron) || cron.split(/\s+/).length === 5;
1415
+ if (!cron || !shaped) {
1416
+ throw new Error(`"${cron}" is not a cron expression (5 fields, or @daily-style)`);
1417
+ }
1418
+ wanted.set("schedule", `"${cron}"`);
1419
+ wanted.set("timezone", opts.timezone?.trim() ? opts.timezone.trim() : null);
1420
+ } else {
1421
+ wanted.set("schedule", null);
1422
+ wanted.set("timezone", null);
1423
+ }
1424
+
1425
+ const lines = raw.split("\n");
1426
+ const hasFront = lines[0]?.trim() === "---";
1427
+ const close = hasFront ? lines.indexOf("---", 1) : -1;
1428
+ const front = hasFront && close > 0 ? lines.slice(1, close) : [];
1429
+ const body = hasFront && close > 0 ? lines.slice(close + 1) : lines;
1430
+
1431
+ const kept = front.filter((line) => {
1432
+ const key = line.match(/^(\w+):/)?.[1];
1433
+ return !(key && wanted.has(key));
1434
+ });
1435
+ for (const [key, value] of wanted) {
1436
+ if (value !== null) kept.push(`${key}: ${value}`);
1437
+ }
1438
+
1439
+ if (kept.length === 0) return body.join("\n").replace(/^\n+/, "");
1440
+ return ["---", ...kept, "---", ...body].join("\n");
1441
+ }
1442
+
1443
+ /**
1444
+ * Rewrite one step's option lines — the board's step editor. Surgical, like
1445
+ * every flow rewrite: only the managed option lines of block `index` change;
1446
+ * the step line, its unmanaged options (approve stays the `!` marker's
1447
+ * business), other steps, comments and frontmatter survive verbatim.
1448
+ * `null` clears an option; values are clamped exactly as parseFlow reads
1449
+ * them, so what the editor writes is what the runner will do.
1450
+ */
1451
+ /**
1452
+ * Rewrite one step's instruction, and nothing else.
1453
+ *
1454
+ * The step line carries four things — the group number, the optional/approve
1455
+ * marker, the wikilink, and the instruction — and only the last is being
1456
+ * edited. Rebuilding the line from the parsed step would silently normalise
1457
+ * the other three (a "2?" becoming "2.", an en dash becoming an em dash), so
1458
+ * the prefix is taken verbatim from the file and only the tail replaced. A
1459
+ * flow's markdown stays the author's.
1460
+ */
1461
+ export function updateFlowStepInstruction(raw: string, index: number, instruction: string): string {
1462
+ const { head, preamble, blocks } = splitFlowBlocks(raw);
1463
+ if (!Number.isInteger(index) || index < 0 || index >= blocks.length) {
1464
+ throw new Error(`no step ${index}`);
1465
+ }
1466
+ const block = blocks[index];
1467
+ const line = block.lines[0];
1468
+ const m = line.match(STEP_RE);
1469
+ if (!m) throw new Error(`step ${index} is not a step line`);
1470
+ // Everything up to where the instruction starts, exactly as written.
1471
+ const prefix = m[5] ? line.slice(0, line.length - m[5].length) : `${line.replace(/\s+$/, "")} — `;
1472
+ const next = instruction.trim().replace(/\s*\n\s*/g, " ");
1473
+ block.lines = [`${prefix}${next}`.replace(/\s+$/, ""), ...block.lines.slice(1)];
1474
+ // Same regrouping as updateFlowStep: consecutive blocks sharing a number
1475
+ // are one parallel group, and emitFlow puts the file back together.
1476
+ const groups: FlowBlock[][] = [];
1477
+ for (const b of blocks) {
1478
+ const last = groups[groups.length - 1];
1479
+ if (last && last[0].group === b.group) last.push(b);
1480
+ else groups.push([b]);
1481
+ }
1482
+ return emitFlow(head, preamble, groups);
1483
+ }
1484
+
1485
+ export function updateFlowStep(
1486
+ raw: string,
1487
+ index: number,
1488
+ options: Partial<Record<"model" | "effort" | "retry" | "timeout" | "verify" | "when" | "case" | "else" | "loop" | "until" | "each" | "max", string | number | null>>,
1489
+ ): string {
1490
+ const { head, preamble, blocks } = splitFlowBlocks(raw);
1491
+ if (!Number.isInteger(index) || index < 0 || index >= blocks.length) {
1492
+ throw new Error(`no step ${index}`);
1493
+ }
1494
+
1495
+ const clamp: Record<string, (v: string) => string | null> = {
1496
+ model: (v) => v || null,
1497
+ effort: (v) => resolveEffort(v) ?? null,
1498
+ verify: (v) => v || null,
1499
+ when: (v) => v || null,
1500
+ case: (v) => v || null,
1501
+ else: (v) => (v === "true" || v === "1" ? "true" : null),
1502
+ until: (v) => v || null,
1503
+ retry: (v) => String(Math.min(5, Math.max(0, Number(v) || 0))),
1504
+ timeout: (v) => (Number(v) > 0 ? String(Math.max(1, Math.floor(Number(v)))) : null),
1505
+ loop: (v) => (Number(v) > 0 ? String(Math.min(5, Math.max(1, Math.floor(Number(v))))) : null),
1506
+ max: (v) => (Number(v) > 0 ? String(Math.min(20, Math.max(1, Math.floor(Number(v))))) : null),
1507
+ each: (v) => (v === "lines" ? "lines" : null),
1508
+ };
1509
+
1510
+ const block = blocks[index];
1511
+ const managed = new Set(Object.keys(options));
1512
+ const kept = block.lines.slice(1).filter((line) => {
1513
+ const key = line.match(OPTION_RE)?.[1];
1514
+ return !(key && managed.has(key));
1515
+ });
1516
+ const added: string[] = [];
1517
+ for (const [key, value] of Object.entries(options)) {
1518
+ if (value === null || value === undefined || !(key in clamp)) continue;
1519
+ const cleaned = clamp[key](String(value).trim());
1520
+ if (cleaned === null) continue;
1521
+ added.push(` ${key}: ${cleaned}`);
1522
+ }
1523
+ block.lines = [block.lines[0], ...added, ...kept];
1524
+
1525
+ // Reassemble the original grouping: consecutive blocks sharing a group.
1526
+ const groups: FlowBlock[][] = [];
1527
+ for (const b of blocks) {
1528
+ const last = groups[groups.length - 1];
1529
+ if (last && last[0].group === b.group) last.push(b);
1530
+ else groups.push([b]);
1531
+ }
1532
+ return emitFlow(head, preamble, groups);
1533
+ }
1534
+
1535
+ export function reorderFlowSteps(raw: string, groups: number[][]): string {
1536
+ const { head, preamble, blocks: ordered } = splitFlowBlocks(raw);
1537
+
1538
+ const seen = new Set<number>();
1539
+ for (const group of groups) {
1540
+ if (group.length === 0) throw new Error("a group must contain at least one step");
1541
+ for (const i of group) {
1542
+ if (!Number.isInteger(i) || i < 0 || i >= ordered.length) throw new Error(`no step ${i}`);
1543
+ if (seen.has(i)) throw new Error(`step ${i} appears twice`);
1544
+ seen.add(i);
1545
+ }
1546
+ }
1547
+ if (seen.size !== ordered.length) {
1548
+ throw new Error(`expected all ${ordered.length} steps, got ${seen.size}`);
1549
+ }
1550
+
1551
+ return emitFlow(head, preamble, groups.map((g) => g.map((i) => ordered[i])));
1552
+ }
1553
+
1554
+ /** Append a step as its own group at the end of the flow. */
1555
+ export function addFlowStep(
1556
+ raw: string,
1557
+ step: { target: string; subflow?: boolean; instruction?: string },
1558
+ ): string {
1559
+ assertSafeName(step.target, "step target");
1560
+ const { head, preamble, blocks } = splitFlowBlocks(raw);
1561
+ const instruction = (step.instruction ?? "").replace(/[\r\n]+/g, " ").trim();
1562
+ const line = `0. [[${step.subflow ? "flow:" : ""}${step.target}]]${instruction ? ` — ${instruction}` : ""}`;
1563
+ if (!STEP_RE.test(line)) throw new Error("could not build a valid step line");
1564
+ const groups = groupBlocks(blocks);
1565
+ groups.push([{ group: 0, lines: [line] }]);
1566
+ return emitFlow(head, preamble, groups);
1567
+ }
1568
+
1569
+ /** Drop one step, by its index in the flow's parsed steps. */
1570
+ export function removeFlowStep(raw: string, index: number): string {
1571
+ const { head, preamble, blocks } = splitFlowBlocks(raw);
1572
+ if (!Number.isInteger(index) || index < 0 || index >= blocks.length) {
1573
+ throw new Error(`no step ${index}`);
1574
+ }
1575
+ const kept = blocks.filter((_, i) => i !== index);
1576
+ return emitFlow(head, preamble, groupBlocks(kept));
1577
+ }
1578
+
1579
+ /** Blocks → the group structure they currently describe. */
1580
+ function groupBlocks(blocks: FlowBlock[]): FlowBlock[][] {
1581
+ const map = new Map<number, FlowBlock[]>();
1582
+ for (const b of blocks) map.set(b.group, [...(map.get(b.group) ?? []), b]);
1583
+ return [...map.entries()].sort((a, b) => a[0] - b[0]).map(([, v]) => v);
1584
+ }
1585
+
1586
+ export function listFlows(tenant: string, workspace: string): FlowInfo[] {
1587
+ const dir = path.join(workspaceDir(tenant, workspace), "flows");
1588
+ if (!fs.existsSync(dir)) return [];
1589
+ return fs
1590
+ .readdirSync(dir)
1591
+ .filter((f) => f.endsWith(".md"))
1592
+ .sort()
1593
+ .map((f) => parseFlow(f, fs.readFileSync(path.join(dir, f), "utf8")));
1594
+ }
1595
+
1596
+ export interface WorkspaceSummary {
1597
+ name: string;
1598
+ description: string;
1599
+ agents: number;
1600
+ flows: number;
1601
+ deployedAt: string;
1602
+ runCount: number;
1603
+ /** Set when this workspace is a branch's preview of another (preview.ts):
1604
+ * which one, which branch, which commit. Read from the marker beside the
1605
+ * directory here rather than through preview.ts, which imports this file. */
1606
+ preview?: { source: string; branch: string; commit: string; at: string };
1607
+ /** The workspace's picture, as a URL, when the page that lists it looked
1608
+ * one up (avatars.ts) — the store itself does not. */
1609
+ avatar?: string | null;
1610
+ }
1611
+
1612
+ /**
1613
+ * Every account this installation holds.
1614
+ *
1615
+ * A tenant is a directory under the data root, but not every directory there
1616
+ * is one — `.runtimes/` sits beside them, and so do keys.json and the secret
1617
+ * key. What makes a tenant is that it holds workspaces, so that is the test.
1618
+ *
1619
+ * Single-workspace mode has no accounts at all: the workspace *is* the whole
1620
+ * installation, and there is nothing above it to enumerate.
1621
+ */
1622
+ export function listTenants(): string[] {
1623
+ if (singleWorkspace()) return [];
1624
+ const root = dataRoot();
1625
+ if (!fs.existsSync(root)) return [];
1626
+ return fs
1627
+ .readdirSync(root)
1628
+ .filter((name) => SAFE_NAME.test(name))
1629
+ .filter((name) => {
1630
+ const dir = path.join(root, name);
1631
+ try {
1632
+ if (!fs.statSync(dir).isDirectory()) return false;
1633
+ } catch {
1634
+ return false;
1635
+ }
1636
+ return [WORKSPACES, LEGACY_WORKSPACES].some((w) => fs.existsSync(path.join(dir, w)));
1637
+ })
1638
+ .sort();
1639
+ }
1640
+
1641
+ export function listWorkspaces(tenant: string): WorkspaceSummary[] {
1642
+ assertSafeName(tenant, "tenant");
1643
+ const dir = workspacesRoot(tenant);
1644
+ if (!fs.existsSync(dir)) return [];
1645
+ return fs
1646
+ .readdirSync(dir)
1647
+ .filter((n) => fs.statSync(path.join(dir, n)).isDirectory())
1648
+ .map((name) => {
1649
+ const pDir = path.join(dir, name);
1650
+ // AGENTS.md is the workspace's config file; project.md is the name it
1651
+ // had before. Take the first NON-EMPTY description across both — not the
1652
+ // first file that merely exists. A workspace with both files but a
1653
+ // description only in project.md (AGENTS.md carrying just `notify:`, say)
1654
+ // otherwise shows blank, because the old code broke on file-exists.
1655
+ let description = "";
1656
+ for (const file of ["AGENTS.md", "project.md"]) {
1657
+ const p = path.join(pDir, file);
1658
+ if (!fs.existsSync(p)) continue;
1659
+ try {
1660
+ const d = matter(fs.readFileSync(p, "utf8")).data.description ?? "";
1661
+ if (d) { description = d; break; }
1662
+ } catch {
1663
+ // an unreadable file is not a description; try the next
1664
+ }
1665
+ }
1666
+ const runsDir = path.join(pDir, "runs");
1667
+ let preview: WorkspaceSummary["preview"];
1668
+ try {
1669
+ preview = JSON.parse(fs.readFileSync(path.join(dir, `.${name}.preview.json`), "utf8"));
1670
+ } catch {
1671
+ // not a preview
1672
+ }
1673
+ return {
1674
+ name,
1675
+ description,
1676
+ agents: listAgents(tenant, name).length,
1677
+ flows: listFlows(tenant, name).length,
1678
+ deployedAt: fs.statSync(pDir).mtime.toISOString(),
1679
+ runCount: fs.existsSync(runsDir) ? fs.readdirSync(runsDir).length : 0,
1680
+ ...(preview ? { preview } : {}),
1681
+ };
1682
+ })
1683
+ .sort((a, b) => b.deployedAt.localeCompare(a.deployedAt));
1684
+ }
1685
+
1686
+ // ---------- file management (dashboard editing) ----------
1687
+
1688
+ // Editable paths: project.md, .md files under agents/ or flows/, and an
1689
+ // agent's own scripts (text source files it runs with bash).
1690
+ const SCRIPT_EXT = /\.(py|sh|[mc]?js|ts|rb|sql|txt|json|ya?ml|toml|env|md)$/i;
1691
+
1692
+ /**
1693
+ * May this path be edited — and therefore, must it be listed?
1694
+ *
1695
+ * The listing and the writer used to answer this separately, and drifted
1696
+ * twice. First `state/notes.md` could be written and never appeared; that was
1697
+ * fixed by adding a state rule to the listing. Then folder tools arrived and
1698
+ * `tools/x/run.mjs` — the program half of every script tool — was writable,
1699
+ * readable, and invisible in the file tree, so the code you were told to edit
1700
+ * could not be found.
1701
+ *
1702
+ * Writable and invisible is the worst of both. One rule decides now: the
1703
+ * listing asks the writer, so a path that can be saved is a path that shows.
1704
+ */
1705
+ export function isEditablePath(rel: string): boolean {
1706
+ try {
1707
+ assertEditablePath(rel);
1708
+ return true;
1709
+ } catch {
1710
+ return false;
1711
+ }
1712
+ }
1713
+
1714
+ function assertEditablePath(rel: string) {
1715
+ const norm = path.normalize(rel);
1716
+ if (norm.startsWith("..") || path.isAbsolute(norm)) throw new Error(`illegal path: ${rel}`);
1717
+ assertCanonicalCase(norm);
1718
+ if (norm === "project.md" || norm === "AGENTS.md") return norm;
1719
+ if (norm.includes("outputs/")) throw new Error(`not an editable path: ${rel}`);
1720
+ if (/scripts\//.test(norm) && SCRIPT_EXT.test(norm)) return norm;
1721
+ if (/^agents\/[^/]+\/skills\/[^/]+\//.test(norm) && SCRIPT_EXT.test(norm)) return norm;
1722
+ if (/^(skills|memory|knowledge|tools|state)\//.test(norm) && SCRIPT_EXT.test(norm)) return norm;
1723
+ if (/^(agents|flows|evals)\//.test(norm) && norm.endsWith(".md")) return norm;
1724
+ throw new Error(`not an editable path: ${rel}`);
1725
+ }
1726
+
1727
+
1728
+ /** Files under the account directory that exist, are worth showing, and must
1729
+ * never be served. Listing them is honest — hiding them is what makes a file
1730
+ * tree disagree with the filesystem it claims to show — but their contents
1731
+ * are the vault and the money, so a reader gets the reason instead. */
1732
+ const ACCOUNT_SEALED = new Set(["secrets.json", "oauth-clients.json", "ledger.jsonl"]);
1733
+
1734
+ export function accountFileSealed(rel: string): string | null {
1735
+ const norm = rel.replaceAll("\\", "/");
1736
+ if (ACCOUNT_SEALED.has(norm)) {
1737
+ return norm === "secrets.json"
1738
+ ? "the vault — encrypted, and never served to a browser. Manage secrets in Settings."
1739
+ : norm === "oauth-clients.json"
1740
+ ? "OAuth client credentials — never served to a browser."
1741
+ : "the billing ledger — append-only, read it through Usage and Wallet.";
1742
+ }
1743
+ if (/^(billed|workspaces\/[^/]+\/runs)\//.test(norm)) {
1744
+ return "generated bookkeeping, not an authored file.";
1745
+ }
1746
+ if (/^workspaces\/[^/]+\/secrets\.json$/.test(norm)) {
1747
+ return "the workspace vault — encrypted, and never served to a browser.";
1748
+ }
1749
+ return null;
1750
+ }
1751
+
1752
+ /** Generated bookkeeping: nobody authored it, no agent reads it, and each has
1753
+ * a page that presents it properly. Listing it would add rows whose only
1754
+ * function is to refuse the person who clicked them, which is noise wearing
1755
+ * honesty's clothes. `accountFileSealed` still refuses these if a path is
1756
+ * constructed directly — the listing hides them, the reader guards them. */
1757
+ const ACCOUNT_BOOKKEEPING =
1758
+ /^(billed\/|ledger\.jsonl$|oauth-clients\.json$)|(^|\/)runs\/|(^|\/)secrets\.json$/;
1759
+
1760
+ /**
1761
+ * The account directory as it actually is on disk.
1762
+ *
1763
+ * A faithful walk, with one rule for what it leaves out: show what a person
1764
+ * authors or an agent reads. That keeps the path you see the path an agent
1765
+ * types, which is the whole point of the tree — while sparing you rows that
1766
+ * exist only to be refused.
1767
+ *
1768
+ * The vault is not an exception to that rule. Secrets are authored in Settings
1769
+ * and reach an agent as environment variables — never as a file, deliberately,
1770
+ * so that no single read can carry them all off. `secrets.json` is therefore
1771
+ * neither authored here nor read by an agent, and a row that can only refuse
1772
+ * the person who clicks it teaches nothing. The reader still guards it.
1773
+ */
1774
+ export function listAccountFiles(tenant: string, cap = 4000): string[] {
1775
+ const root = accountDir(tenant);
1776
+ if (!fs.existsSync(root)) return [];
1777
+ const out: string[] = [];
1778
+ const walk = (abs: string, rel: string, depth: number) => {
1779
+ if (out.length >= cap || depth > 8) return;
1780
+ let entries: fs.Dirent[];
1781
+ try {
1782
+ entries = fs.readdirSync(abs, { withFileTypes: true });
1783
+ } catch {
1784
+ return;
1785
+ }
1786
+ for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {
1787
+ if (out.length >= cap) return;
1788
+ const next = rel ? `${rel}/${e.name}` : e.name;
1789
+ if (e.isSymbolicLink()) continue;
1790
+ // Content-addressed blobs and a run's copied outputs are bytes under a
1791
+ // hash — thousands of names that mean nothing to a reader. The
1792
+ // revision store (.history), the runtime cache (.runtimes, with npm's
1793
+ // _cacache under it) and the file-store indexes (storage/) are the
1794
+ // platform's own bookkeeping: real paths, but not files anyone reads
1795
+ // here, and between them most of what the page used to list.
1796
+ if (/(^|\/)(blobs|outputs|\.results|node_modules|\.git|\.history|\.runtimes|\.npm-cache|_cacache|storage|previews|avatars)$/.test(next)) continue;
1797
+ if (ACCOUNT_BOOKKEEPING.test(next)) continue;
1798
+ if (e.isDirectory()) walk(path.join(abs, e.name), next, depth + 1);
1799
+ else out.push(next);
1800
+ }
1801
+ };
1802
+ walk(root, "", 0);
1803
+ return out;
1804
+ }
1805
+
1806
+ /** One file under the account directory, or the reason it will not be served. */
1807
+ export function readAccountFile(tenant: string, rel: string): { content: string } | { sealed: string } {
1808
+ const sealed = accountFileSealed(rel);
1809
+ if (sealed) return { sealed };
1810
+ const norm = rel.replaceAll("\\", "/");
1811
+ if (norm.includes("..")) return { sealed: "path escapes the account directory." };
1812
+ const file = path.join(accountDir(tenant), norm);
1813
+ try {
1814
+ if (fs.statSync(file).size > 512 * 1024) return { sealed: "too large to show here — download it instead." };
1815
+ return { content: fs.readFileSync(file, "utf8") };
1816
+ } catch {
1817
+ return { sealed: "could not be read." };
1818
+ }
1819
+ }
1820
+
1821
+ export function listWorkspaceFiles(tenant: string, workspace: string): string[] {
1822
+ const dir = workspaceDir(tenant, workspace);
1823
+ if (!fs.existsSync(dir)) return [];
1824
+ const out: string[] = [];
1825
+ for (const entry of fs.readdirSync(dir, { recursive: true })) {
1826
+ const rel = String(entry);
1827
+ if (/(^|\/)(runs|outputs)\//.test(rel)) continue;
1828
+ // Skip anything reached through a symlink — a stray link would otherwise
1829
+ // pull another workspace (or the whole library) into this listing.
1830
+ try {
1831
+ if (fs.lstatSync(path.join(dir, rel)).isSymbolicLink()) continue;
1832
+ } catch {
1833
+ continue;
1834
+ }
1835
+ // Generated, not authored — the same reason runs/ is hidden above.
1836
+ if (/(^|\/)\.results\//.test(rel)) continue;
1837
+
1838
+ // Directories are entries too, and a directory is not an editable file.
1839
+ try {
1840
+ if (!fs.statSync(path.join(dir, rel)).isFile()) continue;
1841
+ } catch {
1842
+ continue;
1843
+ }
1844
+ // One rule, asked of the writer — see isEditablePath. The four separate
1845
+ // predicates that used to live here disagreed with it twice, and both
1846
+ // times the symptom was a file you could save and could not see.
1847
+ if (isEditablePath(rel)) out.push(rel);
1848
+ }
1849
+ return out.sort();
1850
+ }
1851
+
1852
+ export function readWorkspaceFile(tenant: string, workspace: string, rel: string): string {
1853
+ const p = path.join(workspaceDir(tenant, workspace), assertEditablePath(rel));
1854
+ if (!fs.existsSync(p)) throw new Error(`file not found: ${rel}`);
1855
+ return fs.readFileSync(p, "utf8");
1856
+ }
1857
+
1858
+ /** Who made a change and why — recorded on its revision. */
1859
+ export interface ChangeMeta {
1860
+ by?: string;
1861
+ message?: string;
1862
+ }
1863
+
1864
+ export function writeWorkspaceFile(
1865
+ tenant: string,
1866
+ workspace: string,
1867
+ rel: string,
1868
+ content: string,
1869
+ meta: ChangeMeta = {},
1870
+ ) {
1871
+ if (content.length > 256 * 1024) throw new Error("file too large");
1872
+ matter(content); // reject files whose frontmatter doesn't parse
1873
+ const p = path.join(workspaceDir(tenant, workspace), assertEditablePath(rel));
1874
+ const existed = fs.existsSync(p);
1875
+ const before = existed ? fs.readFileSync(p, "utf8") : null;
1876
+ fs.mkdirSync(path.dirname(p), { recursive: true });
1877
+ // One trailing newline, never a run of them.
1878
+ //
1879
+ // Models end a document with blank lines — several, sometimes dozens — and
1880
+ // nothing trimmed them, so every save kept whatever the last writer left.
1881
+ // A reader sees it as a file that scrolls for a page after the text stops;
1882
+ // the editor's line gutter dutifully numbered all of it. Trailing blank
1883
+ // lines are not content in any format this holds (markdown, YAML
1884
+ // frontmatter, a script), and no author types thirty of them on purpose.
1885
+ fs.writeFileSync(p, `${content.replace(/\s+$/, "")}\n`);
1886
+ // Code is executable wherever it lives: the scripts shelf, a skill's
1887
+ // bundled scripts/, or a folder tool's own directory. A run.mjs written
1888
+ // without the bit fails at exec, which reads as "the tool is broken".
1889
+ const norm = rel.replaceAll("\\", "/");
1890
+ if (/^scripts\//.test(norm) || /(^|\/)scripts\//.test(norm) ||
1891
+ (/^tools\//.test(norm) && !norm.endsWith(".md"))) {
1892
+ fs.chmodSync(p, 0o755);
1893
+ }
1894
+ syncBundleFor(p, existed ? "Update" : "Creation");
1895
+ // The revision: what it was, what it is now. Identical content records
1896
+ // nothing — see history.ts.
1897
+ recordRevision(tenant, workspace, [{ path: norm, before, after: fs.readFileSync(p, "utf8") }], meta);
1898
+ }
1899
+
1900
+ // memory/ and knowledge/ are OKF bundles, so index.md has to stay current —
1901
+ // a generated index that drifts is the same class of bug as a memory index
1902
+ // naming files that don't exist.
1903
+ export function syncBundleFor(file: string, change?: "Creation" | "Update") {
1904
+ // Walk up to the bundle root — a concept may sit in a nested section.
1905
+ let dir = path.dirname(file);
1906
+ let hops = 0;
1907
+ while (hops < 6) {
1908
+ const base = path.basename(dir);
1909
+ if (base === "memory" || base === "knowledge") break;
1910
+ dir = path.dirname(dir);
1911
+ hops++;
1912
+ }
1913
+ const kind = path.basename(dir);
1914
+ if (kind !== "memory" && kind !== "knowledge") return;
1915
+
1916
+ // A workspace- or account-level bundle is a root; an agent's own is nested
1917
+ // inside one, and the spec permits okf_version only at a bundle root.
1918
+ const isRoot = path.basename(path.resolve(dir, "..", "..")) !== "agents";
1919
+ syncIndex(dir, kind === "memory" ? "Memory" : "Knowledge", isRoot);
1920
+ if (change) {
1921
+ appendLog(dir, path.relative(dir, file).split(path.sep).join("/"), change);
1922
+ }
1923
+ }
1924
+
1925
+ /**
1926
+ * Rename one editable file. Both ends pass the same gate as a write — a
1927
+ * rename must not be able to move a file somewhere a write couldn't create
1928
+ * it — and an existing target refuses rather than silently replacing
1929
+ * someone's work. Bundle indexes on both sides are re-synced, because a
1930
+ * memory file leaving a bundle is as much a change to its index as one
1931
+ * arriving.
1932
+ */
1933
+ /**
1934
+ * Why a path can never be moved or renamed, in a sentence — or null when it
1935
+ * can. One vocabulary for the tree, the API and the tests, because "not an
1936
+ * editable path" told the person *that* they were refused and made them
1937
+ * guess at *why*, when the why is the whole lesson: in this format,
1938
+ * location is meaning.
1939
+ */
1940
+ export function anchoredReason(rel: string): string | null {
1941
+ const norm = rel.replaceAll("\\", "/");
1942
+ const base = norm.split("/").pop()!;
1943
+ if (norm === "AGENTS.md" || norm === "project.md") {
1944
+ return `${base} is the workspace's own identity — it cannot move; rename or delete the workspace instead`;
1945
+ }
1946
+ if (base === "agent.md") {
1947
+ return "agent.md is the agent because of where it sits — to rename the agent, rename its folder";
1948
+ }
1949
+ if (base === "SKILL.md") {
1950
+ return "SKILL.md names its skill — to rename the skill, rename its folder";
1951
+ }
1952
+ if (base === "tool.md") {
1953
+ return "tool.md names its tool — to rename the tool, rename its folder";
1954
+ }
1955
+ if (/(^|\/)(knowledge|memory)\/(.*\/)?(index|log)\.md$/.test(norm)) {
1956
+ return `${base} is generated from the files around it — it is rebuilt in place on the next write`;
1957
+ }
1958
+ return null;
1959
+ }
1960
+
1961
+ export function renameWorkspaceFile(tenant: string, workspace: string, from: string, to: string, meta: ChangeMeta = {}) {
1962
+ // Refuse with the reason before any path validation: "not an editable
1963
+ // path" is true of AGENTS.md too, and the generic sentence buries the
1964
+ // specific one worth reading.
1965
+ const why = anchoredReason(from) ?? anchoredReason(to);
1966
+ if (why) throw new Error(why);
1967
+ const dir = workspaceDir(tenant, workspace);
1968
+ const src = path.join(dir, assertEditablePath(from));
1969
+ const dst = path.join(dir, assertEditablePath(to));
1970
+ if (!fs.existsSync(src)) throw new Error(`${from} does not exist`);
1971
+ if (fs.existsSync(dst)) throw new Error(`${to} already exists`);
1972
+ fs.mkdirSync(path.dirname(dst), { recursive: true });
1973
+ const moved = deletionsUnder(workspaceDir(tenant, workspace), path.normalize(from).replaceAll("\\", "/"));
1974
+ fs.renameSync(src, dst);
1975
+ // A rename is a deletion and a creation with the same bytes, which is
1976
+ // exactly how git records one and what lets "where did this go" be answered.
1977
+ const toNorm = path.normalize(to).replaceAll("\\", "/");
1978
+ recordRevision(
1979
+ tenant,
1980
+ workspace,
1981
+ [
1982
+ ...moved,
1983
+ ...moved.map((f) => ({
1984
+ path: f.path === path.normalize(from).replaceAll("\\", "/") ? toNorm : f.path.replace(path.normalize(from).replaceAll("\\", "/"), toNorm),
1985
+ before: null,
1986
+ after: f.before,
1987
+ })),
1988
+ ],
1989
+ { message: `renamed ${from} → ${to}`, ...meta },
1990
+ );
1991
+ syncBundleFor(src);
1992
+ syncBundleFor(dst, "Update");
1993
+ }
1994
+
1995
+ /** Every text file under a path, as revision entries recording deletion. */
1996
+ function deletionsUnder(dir: string, norm: string): RevisionFile[] {
1997
+ const abs = path.join(dir, norm);
1998
+ if (!fs.existsSync(abs)) return [];
1999
+ if (fs.statSync(abs).isFile()) {
2000
+ return [{ path: norm, before: fs.readFileSync(abs, "utf8"), after: null }];
2001
+ }
2002
+ return fs
2003
+ .readdirSync(abs, { recursive: true })
2004
+ .map(String)
2005
+ .filter((e) => {
2006
+ try {
2007
+ return fs.statSync(path.join(abs, e)).isFile();
2008
+ } catch {
2009
+ return false;
2010
+ }
2011
+ })
2012
+ .map((e) => ({
2013
+ path: `${norm}/${e.split(path.sep).join("/")}`,
2014
+ before: fs.readFileSync(path.join(abs, e), "utf8"),
2015
+ after: null,
2016
+ }));
2017
+ }
2018
+
2019
+ export function deleteWorkspacePath(tenant: string, workspace: string, rel: string, meta: ChangeMeta = {}) {
2020
+ const norm = path.normalize(rel).replaceAll("\\", "/");
2021
+ if (norm.startsWith("..") || path.isAbsolute(norm)) throw new Error(`illegal path: ${rel}`);
2022
+ const dir = workspaceDir(tenant, workspace);
2023
+ // The two files a workspace cannot exist without. assertEditablePath lets
2024
+ // them through — they are editable — but editable and deletable are
2025
+ // different claims, and deleting AGENTS.md is deleting the workspace's
2026
+ // own name.
2027
+ // The same sentences the tree and the rename path use — one vocabulary
2028
+ // for "this file is structure, not content". An identity file is only
2029
+ // deletable as its whole folder: agent.md alone leaves a folder that is
2030
+ // no longer an agent but still shadows the name.
2031
+ const anchored = anchoredReason(norm);
2032
+ if (anchored) {
2033
+ if (/(agent|SKILL|tool)\.md$/.test(norm)) {
2034
+ throw new Error(`${norm.split("/").pop()} is its folder's identity — delete the whole folder instead`);
2035
+ }
2036
+ throw new Error(anchored);
2037
+ }
2038
+ // Whole-folder units: an agent, a skill, a folder tool. Each is one thing
2039
+ // to its reader, so each is one thing to delete.
2040
+ if (/^agents\/[a-z0-9-]+$/.test(norm) ||
2041
+ /^(skills|tools)\/[a-z0-9-]+$/.test(norm) ||
2042
+ /^agents\/[a-z0-9-]+\/skills\/[a-z0-9-]+$/.test(norm)) {
2043
+ const gone = deletionsUnder(dir, norm);
2044
+ fs.rmSync(path.join(dir, norm), { recursive: true, force: true });
2045
+ recordRevision(tenant, workspace, gone, { message: `deleted ${norm}/`, ...meta });
2046
+ return;
2047
+ }
2048
+ const p = path.join(dir, assertEditablePath(norm));
2049
+ const gone = deletionsUnder(dir, norm);
2050
+ fs.rmSync(p, { force: true });
2051
+ recordRevision(tenant, workspace, gone, meta);
2052
+ // A bundle's index must stop naming what is gone.
2053
+ syncBundleFor(p);
2054
+ }
2055
+
2056
+ export function deleteWorkspace(tenant: string, workspace: string) {
2057
+ const dir = workspaceDir(tenant, workspace);
2058
+ if (!fs.existsSync(dir)) throw new Error(`workspace ${workspace} not found`);
2059
+ fs.rmSync(dir, { recursive: true, force: true });
2060
+ }
2061
+
2062
+ /**
2063
+ * What a rename would break, so the caller can say so before doing it.
2064
+ *
2065
+ * The workspace name is not just a directory: it is in every webhook URL *and*
2066
+ * in the token those URLs carry, which is derived from `tenant/workspace/flow`.
2067
+ * So a rename invalidates both halves — an old hook doesn't 404, it fails the
2068
+ * signature check, which reads like an attack rather than a rename. Anything
2069
+ * calling those URLs has to be re-pointed by hand.
2070
+ */
2071
+ export function renameImpact(tenant: string, workspace: string): { webhookFlows: string[] } {
2072
+ return {
2073
+ webhookFlows: listFlows(tenant, workspace)
2074
+ .filter((f) => f.trigger === "webhook")
2075
+ .map((f) => f.name),
2076
+ };
2077
+ }
2078
+
2079
+ /**
2080
+ * Rename a workspace: move the directory, then update the `name:` in AGENTS.md
2081
+ * so the file and its folder don't disagree.
2082
+ *
2083
+ * Runs, state and secrets travel with it — they live inside the directory, so
2084
+ * there is nothing to migrate and nothing to lose.
2085
+ */
2086
+ export function renameWorkspace(tenant: string, from: string, to: string) {
2087
+ assertSafeName(to, "workspace");
2088
+ if (from === to) return;
2089
+
2090
+ const src = workspaceDir(tenant, from);
2091
+ if (!fs.existsSync(src)) throw new Error(`workspace ${from} not found`);
2092
+
2093
+ // Deliberately not workspaceDir(): that returns the *legacy* path when one
2094
+ // exists, and renaming into `projects/` would resurrect the old layout.
2095
+ const dest = singleWorkspace()
2096
+ ? (() => { throw new Error("cannot rename in single-workspace mode"); })()
2097
+ : path.join(dataRoot(), tenant, WORKSPACES, to);
2098
+ if (fs.existsSync(dest)) throw new Error(`workspace ${to} already exists`);
2099
+
2100
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
2101
+ fs.renameSync(src, dest);
2102
+
2103
+ const agentsMd = path.join(dest, "AGENTS.md");
2104
+ if (fs.existsSync(agentsMd)) {
2105
+ const raw = fs.readFileSync(agentsMd, "utf8");
2106
+ fs.writeFileSync(agentsMd, raw.replace(/^name:.*$/m, `name: ${to}`));
2107
+ }
2108
+ }
2109
+
2110
+ /** Set a workspace's description, in AGENTS.md, without touching anything else. */
2111
+ export function setWorkspaceDescription(tenant: string, workspace: string, description: string) {
2112
+ const file = path.join(workspaceDir(tenant, workspace), "AGENTS.md");
2113
+ if (!fs.existsSync(file)) throw new Error("this workspace has no AGENTS.md");
2114
+
2115
+ const raw = fs.readFileSync(file, "utf8");
2116
+ const block = raw.match(/^(---\r?\n)([\s\S]*?)(\r?\n---)/);
2117
+ if (!block) throw new Error("AGENTS.md has no frontmatter");
2118
+
2119
+ const [, open, body, close] = block;
2120
+ const lines = body.split("\n");
2121
+ const at = lines.findIndex((l) => /^description:/.test(l));
2122
+ const line = `description: ${description}`;
2123
+ if (at === -1) lines.push(line);
2124
+ else lines[at] = line;
2125
+
2126
+ fs.writeFileSync(file, raw.replace(open + body + close, open + lines.join("\n") + close));
2127
+ }
2128
+
2129
+ /**
2130
+ * Starter files for "+ New workspace" in the dashboard.
2131
+ *
2132
+ * Kept as a name because the API route and its callers use it; the content
2133
+ * lives in starter.ts so this and `foldrun init` cannot drift. They did:
2134
+ * this copy still said `type: Agent` after the CLI's had moved to `kind:`,
2135
+ * so every workspace made from the dashboard was born in the old format.
2136
+ */
2137
+ export function templateFiles(workspace: string): DeployFile[] {
2138
+ // The starter minus what only matters on a laptop's disk: .gitignore
2139
+ // guards a local clone's secrets, but the hosted store never keeps
2140
+ // secrets in the tree and saveWorkspace rightly refuses files outside
2141
+ // the workspace's own vocabulary.
2142
+ return starterFiles(workspace).filter((f) => f.path === "AGENTS.md" || IN_WORKSPACE_DIR.test(f.path));
2143
+ }
2144
+
2145
+ // Kept as names because routes import them; the content lives in KINDS, which
2146
+ // is the single table every creation path reads.
2147
+ export const AGENT_TEMPLATE = (name: string) => KINDS.agents.template(name);
2148
+ export const FLOW_TEMPLATE = (name: string, firstAgent: string) =>
2149
+ KINDS.flows.template(name, { firstAgent });
2150
+
2151
+ // The orchestration patterns a new flow can start from. Each template ships
2152
+ // the pattern's syntax filled in with real agent names, because a working
2153
+ // example in your own workspace teaches loop:/until:/each: better than any
2154
+ // reference page — you rename the instructions, not learn the shape.
2155
+ export const FLOW_PATTERNS = ["pipeline", "review-loop", "fan-out", "debate", "router"] as const;
2156
+ export type FlowPattern = (typeof FLOW_PATTERNS)[number];
2157
+
2158
+ export function flowPatternTemplate(pattern: FlowPattern, name: string, agents: string[]): string {
2159
+ // Real agents where they exist, honest placeholders where they don't —
2160
+ // `foldrun check` will point at any placeholder left unrenamed.
2161
+ const a = (i: number, fallback: string) => agents[i] ?? agents[0] ?? fallback;
2162
+ const head = `---\nname: ${name}\n---\n\n`;
2163
+ switch (pattern) {
2164
+ case "review-loop":
2165
+ return (
2166
+ head +
2167
+ `1. [[${a(0, "writer")}]] — draft the deliverable\n` +
2168
+ `2. [[${a(1, "reviewer")}]] — review it against the brief; end your reply with APPROVED when it is ready\n` +
2169
+ ` loop: 3\n` +
2170
+ ` until: APPROVED\n`
2171
+ );
2172
+ case "fan-out":
2173
+ return (
2174
+ head +
2175
+ `1. [[${a(0, "lister")}]] — list the items to process, one per line\n` +
2176
+ `2. [[${a(1, "worker")}]] — handle this one item\n` +
2177
+ ` each: lines\n` +
2178
+ ` max: 10\n` +
2179
+ `3. [[${a(2, "summariser")}]] — pull every result into one summary\n`
2180
+ );
2181
+ case "router":
2182
+ return (
2183
+ head +
2184
+ `1. [[${a(0, "classifier")}]] — classify the request: reply with exactly one word, BUG or QUESTION\n` +
2185
+ `2. [[${a(1, "debugger")}]] — investigate and fix the bug\n` +
2186
+ ` case: BUG\n` +
2187
+ `2. [[${a(2, "writer")}]] — answer the question clearly\n` +
2188
+ ` case: QUESTION\n` +
2189
+ `2. [[${a(3, "triager")}]] — neither label fit; say what is missing\n` +
2190
+ ` else: true\n`
2191
+ );
2192
+ case "debate":
2193
+ return (
2194
+ head +
2195
+ `1. [[${a(0, "researcher")}]] — gather the facts\n` +
2196
+ `2. [[${a(1, "advocate")}]] — argue the strongest case for, from the facts\n` +
2197
+ `2. [[${a(2, "skeptic")}]] — argue the strongest case against, from the facts\n` +
2198
+ `3. [[${a(3, "judge")}]] — weigh both cases and give a verdict\n`
2199
+ );
2200
+ default:
2201
+ return FLOW_TEMPLATE(name, a(0, "my-agent"));
2202
+ }
2203
+ }
2204
+
2205
+ export interface RunEvent {
2206
+ t: string;
2207
+ type: "text" | "tool" | "error" | "info";
2208
+ text: string;
2209
+ /** Tool events come in pairs: the call, and its completion. Both carry
2210
+ * the provider's tool_use id so a reader can pair them; the completion
2211
+ * also carries how long the tool ran and whether it errored. The
2212
+ * timeline draws a span from the pair; the trace shows one line. Absent
2213
+ * on records from before this existed — then a tool is a tick, not a bar. */
2214
+ call?: string;
2215
+ ms?: number;
2216
+ err?: boolean;
2217
+ }
2218
+
2219
+ export interface StepRecord {
2220
+ agent: string;
2221
+ instruction: string;
2222
+ group: number;
2223
+ optional: boolean;
2224
+ /** Carried from the flow — see FlowStep. Both are resolved at run
2225
+ * time rather than stored resolved, so a record stays readable as
2226
+ * what its author wrote. */
2227
+ model?: string;
2228
+ effort?: string;
2229
+ approve?: boolean;
2230
+ when?: string;
2231
+ case?: string;
2232
+ else?: boolean;
2233
+ retry?: number;
2234
+ timeout?: number;
2235
+ verify?: string;
2236
+ /** Attempts made so far, for the run trace. */
2237
+ attempts?: number;
2238
+ /** When the step actually started and finished executing — not when the
2239
+ * run reached it. A step's events approximate this (first and last), but
2240
+ * a model can think for minutes before its first tool call, and a step
2241
+ * parked at a gate has events long before it runs. Kept across retries:
2242
+ * startedAt is the first attempt's, finishedAt the last's. Absent on
2243
+ * records from before this existed; readers fall back to the events. */
2244
+ startedAt?: string;
2245
+ finishedAt?: string;
2246
+ /** Why a step was skipped, shown in the trace. */
2247
+ skipReason?: string;
2248
+ /** Evaluator loop, carried from the flow — see FlowStep. `loopRemaining`
2249
+ * counts down on the record so a resumed run knows how many cycles are
2250
+ * left rather than starting the budget over. */
2251
+ loop?: number;
2252
+ until?: string;
2253
+ loopRemaining?: number;
2254
+ /** Fan-out, carried from the flow — see FlowStep. */
2255
+ each?: "lines" | "rows" | "items";
2256
+ eachPath?: string;
2257
+ max?: number;
2258
+ /** Carried from the flow — see FlowStep. */
2259
+ output?: "json";
2260
+ /** The parsed JSON an `output: json` step returned. Kept beside the prose
2261
+ * result rather than instead of it: the reply is still what a person
2262
+ * reads on the run page, and the data is what the next step computes on. */
2263
+ data?: unknown;
2264
+ /** Set on the instances a fan-out step expanded into: the item this one
2265
+ * works on. The template step itself becomes status "expanded". */
2266
+ item?: string;
2267
+ /** Carried from the flow — see FlowStep for each. */
2268
+ onFail?: string;
2269
+ waitSecs?: number;
2270
+ /** Stamped when the run reaches the step; the queue holds it until then.
2271
+ * On the record so a restart resumes the same deadline, not a new one. */
2272
+ waitUntil?: string;
2273
+ /** Carried from the flow — see FlowStep. */
2274
+ waitFor?: "event";
2275
+ /** What the outside world POSTed to release a `wait: event` step. Rides
2276
+ * into the step's prompt the way an approval note does. */
2277
+ eventPayload?: string;
2278
+ ask?: string;
2279
+ /** Carried from the flow — see FlowStep. */
2280
+ preview?: string[];
2281
+ /** `preview:` resolved against storage/ at the moment the run parked —
2282
+ * the files the gate renders, relative to storage/. */
2283
+ previewFiles?: string[];
2284
+ delegate?: string[];
2285
+ /** Set once a delegate step's picks have been expanded — the guard that
2286
+ * a loop rewind cannot fan the same choice out twice. */
2287
+ delegated?: boolean;
2288
+ /**
2289
+ * When a person approved this step. Set by the approval API and never
2290
+ * cleared, because approval is a fact about the past.
2291
+ *
2292
+ * Status alone cannot carry it: approving flips the step back to `pending`,
2293
+ * which is indistinguishable from never having been asked. A run that was
2294
+ * approved and then abandoned could not be resumed without asking the same
2295
+ * person the same question again.
2296
+ */
2297
+ approvedAt?: string;
2298
+ /** What the approver said while approving — carried into the step's prompt
2299
+ * as operator guidance. This turns the gate from a yes/no button into a
2300
+ * place to steer: "approve, but skip the Sydney batch". */
2301
+ approvalNote?: string;
2302
+ status:
2303
+ | "pending"
2304
+ | "awaiting-approval"
2305
+ | "running"
2306
+ | "completed"
2307
+ | "failed"
2308
+ | "skipped";
2309
+ events: RunEvent[];
2310
+ result: string | null;
2311
+ /** The model's final text block — its answer, without the narration that
2312
+ * preceded the tool calls. Absent on steps recorded before this existed,
2313
+ * which is why every reader falls back to `result`. */
2314
+ conclusion?: string | null;
2315
+ costUsd: number | null;
2316
+ /** Set on steps copied into a re-run from an earlier run: the run they
2317
+ * actually executed in. Carried steps provide context and are never
2318
+ * billed again — the original run's ledger line already paid for them. */
2319
+ carriedFrom?: string;
2320
+ /**
2321
+ * Sandbox seconds this step rented, and how many of them were cold start.
2322
+ * Set only on the isolated path — an in-process step rents nothing, so
2323
+ * `null` is the honest value and billing must read it as zero, not as
2324
+ * "unknown, charge something".
2325
+ *
2326
+ * Wall time is deliberately not a substitute: a step parked at an approval
2327
+ * gate has no pod, and billing a customer for a person taking the weekend
2328
+ * to click approve is charging them for their own deliberation.
2329
+ */
2330
+ computeSecs?: number | null;
2331
+ startupSecs?: number | null;
2332
+ /** Which reservation class the sandbox held (small | large), and the
2333
+ * actual limits it held, as numbers. Facts of the step: a price is per
2334
+ * core-second, so what was reserved must come from the record, never
2335
+ * from what the config says when the bill is computed. Absent on steps
2336
+ * from before sizes existed — billing falls back to the flat rate. */
2337
+ size?: "small" | "large" | "heavy";
2338
+ reserved?: { cpus: number; memGiB: number } | null;
2339
+ /** Token counts behind costUsd, kept so usage reports can say "how many"
2340
+ * and not only "how much" — the price of a token changes, the count is
2341
+ * the fact. Absent on steps recorded before this existed. */
2342
+ tokens?: { input: number; output: number } | null;
2343
+ /** What the sandbox actually touched, against what it reserved: CPU
2344
+ * busy-seconds and peak memory from the pod's own cgroup, bytes on the
2345
+ * wire from /proc/net/dev. Null per-field where the kernel interface
2346
+ * wasn't readable in the sandbox — never estimated. */
2347
+ actual?: {
2348
+ busyCpuSecs: number | null;
2349
+ peakMemBytes: number | null;
2350
+ rxBytes: number | null;
2351
+ txBytes: number | null;
2352
+ } | null;
2353
+ }
2354
+
2355
+ export interface RunRecord {
2356
+ id: string;
2357
+ flow: string; // flow name, or "adhoc:<agent>" for direct agent runs
2358
+ /** Tags supplied when the run started — skills can require one. */
2359
+ tags?: string[];
2360
+ status: "queued" | "running" | "awaiting-approval" | "completed" | "failed";
2361
+ startedAt: string;
2362
+ finishedAt: string | null;
2363
+ /** Set when a worker parked this run at an approval gate and returned its
2364
+ * slot to the queue. The approval API re-enqueues exactly when this is
2365
+ * set — without the marker it would also re-enqueue runs whose starting
2366
+ * process is still alive and polling, and two drivers would race. */
2367
+ parkedAt?: string | null;
2368
+ /** Set when a person stopped this run. Kept on the record rather than
2369
+ * expressed only as a status, because "failed" and "someone stopped it"
2370
+ * are different facts and the trace should not conflate them. */
2371
+ stopRequested?: boolean;
2372
+ /**
2373
+ * What this run concluded, in one line — the first line of the last step
2374
+ * that produced a result, captured when the run finishes.
2375
+ *
2376
+ * A run has always recorded its status and its cost but never its outcome,
2377
+ * so "what did this actually do" could only be answered by opening it and
2378
+ * reading. That does not scale past a couple of flows, and the sentence was
2379
+ * already sitting in the record: agents are asked to lead with it.
2380
+ *
2381
+ * Deliberately NOT a schema the author has to fill in. Nothing new goes in
2382
+ * the folder, nothing new is learned, and an agent that writes no headline
2383
+ * simply gets its first line — which is still better than a status.
2384
+ */
2385
+ summary?: string | null;
2386
+ /** The flow's per-run `budget:` at the time this run started, so the cap
2387
+ * a run was held to is on its record rather than read from a file that
2388
+ * may since have changed. Null: none. */
2389
+ budgetUsd?: number | null;
2390
+ /** Memory files this run created or changed, workspace-relative — what
2391
+ * the agents decided was worth remembering, listed so a person can review
2392
+ * the lesson without diffing the folder. */
2393
+ memoryWrites?: string[];
2394
+ steps: StepRecord[];
2395
+ }
2396
+
2397
+ export function runFilePath(tenant: string, workspace: string, runId: string) {
2398
+ assertSafeName(runId, "run id");
2399
+ return path.join(workspaceDir(tenant, workspace), "runs", `${runId}.json`);
2400
+ }
2401
+
2402
+ /**
2403
+ * Erase a run from history: its record and the outputs archived under its
2404
+ * id.
2405
+ *
2406
+ * The ledger line stays, and that is settled rather than provisional: if
2407
+ * deleting a run voided its charge, deletion would be a refund button — run
2408
+ * the expensive extraction, read the result, delete, never pay. Money is
2409
+ * not deleted because a trace was.
2410
+ *
2411
+ * What deletion *does* owe the books is an explanation. A charge pointing
2412
+ * at a run that no longer exists reads as unexplained forever, so the
2413
+ * deletion writes itself down: a zero-value adjustment naming the run, on
2414
+ * the same append-only file the charge lives in. The balance is untouched;
2415
+ * the story is complete.
2416
+ */
2417
+ export function deleteRun(tenant: string, workspace: string, runId: string): boolean {
2418
+ const file = runFilePath(tenant, workspace, runId);
2419
+ if (!fs.existsSync(file)) return false;
2420
+ fs.rmSync(file, { force: true });
2421
+ fs.rmSync(path.join(workspaceDir(tenant, workspace), "runs", runId), {
2422
+ recursive: true,
2423
+ force: true,
2424
+ });
2425
+ onRunDeleted?.(tenant, workspace, runId);
2426
+ return true;
2427
+ }
2428
+
2429
+ /** ledger.ts registers here — store cannot import it without a cycle, and
2430
+ * money must not depend on one. */
2431
+ let onRunDeleted: ((tenant: string, workspace: string, runId: string) => void) | undefined;
2432
+ export function registerRunDeletionListener(fn: typeof onRunDeleted) {
2433
+ onRunDeleted = fn;
2434
+ }
2435
+
2436
+ export interface RunOutputFile {
2437
+ agent: string;
2438
+ /** Relative to that agent's outputs/ — the name the agent wrote. */
2439
+ path: string;
2440
+ bytes: number;
2441
+ }
2442
+
2443
+ /**
2444
+ * What a run archived: every file its agents left in outputs/, copied under
2445
+ * runs/<id>/outputs/<agent>/ when the run finished (runner.ts, archive()).
2446
+ *
2447
+ * Per agent, not per step — the archive is a copy of each agent's folder,
2448
+ * and the record cannot say which step wrote which file. An agent that
2449
+ * steps twice in one run shows the same files against both steps.
2450
+ */
2451
+ export function listRunOutputs(tenant: string, workspace: string, runId: string): RunOutputFile[] {
2452
+ const root = path.join(workspaceDir(tenant, workspace), "runs", runId, "outputs");
2453
+ if (!fs.existsSync(root)) return [];
2454
+ const out: RunOutputFile[] = [];
2455
+ const walk = (agent: string, dir: string, rel: string) => {
2456
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
2457
+ const full = path.join(dir, e.name);
2458
+ const r = rel ? `${rel}/${e.name}` : e.name;
2459
+ if (e.isDirectory()) walk(agent, full, r);
2460
+ else if (e.isFile()) out.push({ agent, path: r, bytes: fs.statSync(full).size });
2461
+ }
2462
+ };
2463
+ for (const e of fs.readdirSync(root, { withFileTypes: true })) {
2464
+ if (e.isDirectory()) walk(e.name, path.join(root, e.name), "");
2465
+ }
2466
+ return out.sort((a, b) => a.agent.localeCompare(b.agent) || a.path.localeCompare(b.path));
2467
+ }
2468
+
2469
+ /** The absolute path of one archived output, or null when the name escapes
2470
+ * the archive or names nothing — the API serves only what this returns. */
2471
+ export function runOutputPath(
2472
+ tenant: string,
2473
+ workspace: string,
2474
+ runId: string,
2475
+ agent: string,
2476
+ rel: string,
2477
+ ): string | null {
2478
+ if (!agent || !rel || agent.includes("/") || agent.includes("\\") || agent.startsWith(".")) return null;
2479
+ const root = path.resolve(workspaceDir(tenant, workspace), "runs", runId, "outputs", agent);
2480
+ const full = path.resolve(root, rel);
2481
+ if (full !== root && !full.startsWith(root + path.sep)) return null;
2482
+ if (!fs.existsSync(full) || !fs.statSync(full).isFile()) return null;
2483
+ return full;
2484
+ }
2485
+
2486
+ export function readRun(tenant: string, workspace: string, runId: string): RunRecord | null {
2487
+ const p = runFilePath(tenant, workspace, runId);
2488
+ return fs.existsSync(p) ? JSON.parse(fs.readFileSync(p, "utf8")) : null;
2489
+ }
2490
+
2491
+ export function writeRun(tenant: string, workspace: string, run: RunRecord) {
2492
+ const p = runFilePath(tenant, workspace, run.id);
2493
+ fs.mkdirSync(path.dirname(p), { recursive: true });
2494
+ // Atomic write — runs are re-read while running (dashboard polls, CLI
2495
+ // tails), and a plain writeFileSync can be caught half-written.
2496
+ const tmp = `${p}.tmp`;
2497
+ fs.writeFileSync(tmp, JSON.stringify(run, null, 2));
2498
+ fs.renameSync(tmp, p);
2499
+ }
2500
+
2501
+ // Every run in an account, each tagged with the workspace it belongs to —
2502
+ // the observability surface.
2503
+ export interface RunWithWorkspace extends RunRecord {
2504
+ workspace: string;
2505
+ }
2506
+
2507
+ export function listAllRuns(tenant: string): RunWithWorkspace[] {
2508
+ return listWorkspaces(tenant)
2509
+ .flatMap((p) => listRuns(tenant, p.name).map((r) => ({ ...r, workspace: p.name })))
2510
+ .sort((a, b) => b.startedAt.localeCompare(a.startedAt));
2511
+ }
2512
+
2513
+ /**
2514
+ * The status a person should see. "failed" and "someone stopped it" are
2515
+ * different facts — the record keeps them apart (stopRequested), and the
2516
+ * display must too: a run a person chose to end is not a run that broke,
2517
+ * and showing it red teaches people that stopping things is dangerous.
2518
+ */
2519
+ export function runDisplayStatus(run: Pick<RunRecord, "status" | "stopRequested">): string {
2520
+ return run.status === "failed" && run.stopRequested ? "stopped" : run.status;
2521
+ }
2522
+
2523
+ export function runCost(run: RunRecord): number {
2524
+ return run.steps.reduce((sum, s) => sum + (s.costUsd ?? 0), 0);
2525
+ }
2526
+
2527
+ /**
2528
+ * What a finished run consumed, in the units the format itself has — the
2529
+ * shape ledger.ts prices. Structural, not an import, because ledger.ts
2530
+ * imports this module and money must not depend on a cycle.
2531
+ *
2532
+ * Only steps that actually ran count. A pending step never happened, a
2533
+ * skipped one was skipped by the flow's own `when`, and an `expanded`
2534
+ * fan-out template is a row in the trace rather than work — billing any of
2535
+ * them would charge for steps the customer can see never executed, which is
2536
+ * the fastest way to lose an argument about an invoice.
2537
+ */
2538
+ export function runMeter(run: RunRecord): {
2539
+ tokenCostUsd: number;
2540
+ steps: number;
2541
+ computeSecs: number;
2542
+ /** The subset of computeSecs held at the small reservation. Steps from
2543
+ * before sizes existed count as large — they held the large limits. */
2544
+ smallSecs: number;
2545
+ netBytes: number;
2546
+ } {
2547
+ let steps = 0;
2548
+ let computeSecs = 0;
2549
+ let smallSecs = 0;
2550
+ let netBytes = 0;
2551
+ for (const s of run.steps) {
2552
+ if (s.status !== "completed" && s.status !== "failed") continue;
2553
+ // A carried step ran — and was billed — in the run it was carried from.
2554
+ if (s.carriedFrom) continue;
2555
+ steps += 1;
2556
+ const secs = s.computeSecs ?? 0;
2557
+ computeSecs += secs;
2558
+ if (s.size === "small") smallSecs += secs;
2559
+ netBytes += (s.actual?.rxBytes ?? 0) + (s.actual?.txBytes ?? 0);
2560
+ }
2561
+ return { tokenCostUsd: runCost(run), steps, computeSecs, smallSecs, netBytes };
2562
+ }
2563
+
2564
+ /**
2565
+ * The compute meter for hold+work pricing: core-seconds and GiB-seconds
2566
+ * actually reserved (from each step's own recorded reservation), and CPU
2567
+ * seconds actually burned (from the sandbox's cgroup, where readable).
2568
+ * Steps with no recorded reservation contribute plain seconds only — the
2569
+ * flat-rate fallback prices those.
2570
+ */
2571
+ export function runComputeMeter(run: RunRecord): {
2572
+ coreSecs: number;
2573
+ gibSecs: number;
2574
+ busyCpuSecs: number;
2575
+ flatSecs: number;
2576
+ } {
2577
+ let coreSecs = 0, gibSecs = 0, busyCpuSecs = 0, flatSecs = 0;
2578
+ for (const s of run.steps) {
2579
+ if (s.status !== "completed" && s.status !== "failed") continue;
2580
+ if (s.carriedFrom) continue;
2581
+ const secs = s.computeSecs ?? 0;
2582
+ if (secs <= 0) continue;
2583
+ if (s.reserved) {
2584
+ coreSecs += secs * s.reserved.cpus;
2585
+ gibSecs += secs * s.reserved.memGiB;
2586
+ } else {
2587
+ flatSecs += secs;
2588
+ }
2589
+ busyCpuSecs += s.actual?.busyCpuSecs ?? 0;
2590
+ }
2591
+ return { coreSecs, gibSecs, busyCpuSecs, flatSecs };
2592
+ }
2593
+
2594
+ export function runDurationSecs(run: RunRecord): number | null {
2595
+ if (!run.finishedAt) return null;
2596
+ return (new Date(run.finishedAt).getTime() - new Date(run.startedAt).getTime()) / 1000;
2597
+ }
2598
+
2599
+ /**
2600
+ * Why a failed run failed, said in one line: the failing step and its last
2601
+ * error event. The journal has the whole story; a runs *list* needs the
2602
+ * sentence — "failed" with no reason makes every diagnosis start by opening
2603
+ * the run, and the answer was sitting in the record all along.
2604
+ */
2605
+ export function runFailure(run: RunRecord): { agent: string; reason: string } | null {
2606
+ if (run.status !== "failed") return null;
2607
+ const step = run.steps.find((s) => s.status === "failed");
2608
+ if (!step) return null;
2609
+ const errors = step.events.filter((e) => e.type === "error");
2610
+ const last = errors[errors.length - 1];
2611
+ return { agent: step.agent, reason: last?.text ?? "failed without an error event" };
2612
+ }
2613
+
2614
+ /**
2615
+ * The one-line outcome, pulled out of the reply that already says it.
2616
+ *
2617
+ * The last step with a result, because that is the one that concluded —
2618
+ * a reporter at the end of a flow, or on a failed run whatever got furthest,
2619
+ * which is the most informative line available either way.
2620
+ *
2621
+ * Markdown decoration is stripped rather than rendered: this lands in an
2622
+ * email subject and a table cell, where `## ` and `**` are noise. Sibling of
2623
+ * runFailure, which puts the reason for a failure on the list for the same
2624
+ * reason — the sentence was in the record all along.
2625
+ */
2626
+ export function runSummary(run: RunRecord): string | null {
2627
+ for (let i = run.steps.length - 1; i >= 0; i--) {
2628
+ const line = stepHeadline(run.steps[i]);
2629
+ if (line) return line;
2630
+ }
2631
+ return null;
2632
+ }
2633
+
2634
+ /**
2635
+ * What one step concluded, in a line.
2636
+ *
2637
+ * The model's ANSWER first, falling back to the whole reply. `result` is every
2638
+ * text block joined, and a model that narrates before calling a tool puts
2639
+ * "Now let me read the outputs…" at the top of it — so reading the joined
2640
+ * text reported the plan of every step that used a tool, which is most of
2641
+ * them. Steps recorded before `conclusion` existed have only `result`, and
2642
+ * for those this is exactly what it always was.
2643
+ */
2644
+ export function stepHeadline(step: StepRecord): string | null {
2645
+ return headlineOf(step.conclusion) ?? headlineOf(step.result);
2646
+ }
2647
+
2648
+ /**
2649
+ * The first line of a reply that a person would recognise as its point.
2650
+ *
2651
+ * Markdown decoration is stripped rather than skipped, so a reply that opens
2652
+ * with `## Done: 4 findings` still has a headline. A line that is only
2653
+ * punctuation — a bare `#`, a rule, a row of asterisks — is decoration, and a
2654
+ * code fence is the shape of an `output: json` reply, not its headline.
2655
+ *
2656
+ * Exported because a run's summary and one agent's step both want it, and two
2657
+ * copies would be two answers to the same question.
2658
+ */
2659
+ export function headlineOf(result: string | null | undefined): string | null {
2660
+ if (!result) return null;
2661
+ for (const raw of result.split("\n")) {
2662
+ const line = raw
2663
+ .replace(/^\s*#{1,6}\s+/, "") // heading
2664
+ .replace(/^\s*[-*+]\s+/, "") // bullet
2665
+ .replace(/^\s*>\s?/, "") // quote
2666
+ .replace(/\*\*/g, "")
2667
+ .replace(/\s+/g, " ")
2668
+ .trim();
2669
+ // A line that is only markdown punctuation — a bare `#`, a `---` rule,
2670
+ // a row of asterisks — is decoration, not a headline. Checked after the
2671
+ // strips above rather than folded into them, because `#{1,6}\s*` would
2672
+ // also eat the hash in a line that legitimately opens "#1 priority".
2673
+ // A code fence, or a line that is only brackets, is the shape of an
2674
+ // `output: json` reply, not its headline either.
2675
+ if (!line || /^[#>\-*_=\s`{}\[\],]*$/.test(line) || line.startsWith("```")) continue;
2676
+ return line.length > 200 ? `${line.slice(0, 197)}…` : line;
2677
+ }
2678
+ return null;
2679
+ }
2680
+
2681
+ /** One step this agent ran, with the run it belongs to. */
2682
+ export interface AgentStep {
2683
+ runId: string;
2684
+ flow: string;
2685
+ status: RunRecord["status"];
2686
+ startedAt: string;
2687
+ /** The step's own first line — what THIS agent concluded, which is not
2688
+ * the run's summary unless it happened to run last. */
2689
+ headline: string | null;
2690
+ instruction: string;
2691
+ stepStatus: StepRecord["status"];
2692
+ costUsd: number | null;
2693
+ /** Set on a fan-out instance: the item this one worked on. */
2694
+ item?: string;
2695
+ /** Path refusals in this step — see deniedPaths. */
2696
+ denied: number;
2697
+ }
2698
+
2699
+ /**
2700
+ * Everything one agent has done, newest first.
2701
+ *
2702
+ * The runs list answers "what ran"; it is keyed by flow, so an agent that
2703
+ * appears in four flows has its work scattered across four filters and an
2704
+ * agent used by none is invisible unless someone remembers the `adhoc:`
2705
+ * prefix. This is the other axis, and the one you want when the question is
2706
+ * "is this agent any good" rather than "did Monday's flow finish".
2707
+ */
2708
+ /** Path refusals in a step — see deniedPaths in the dashboard's
2709
+ * step-io-model, which is the pure copy the client can import. */
2710
+ export function deniedPaths(step: Pick<StepRecord, "events">): number {
2711
+ return (step.events ?? []).filter((e) => / was denied: /.test(e.text ?? "")).length;
2712
+ }
2713
+
2714
+ export function listAgentSteps(tenant: string, workspace: string, agent: string): AgentStep[] {
2715
+ const out: AgentStep[] = [];
2716
+ for (const run of listRuns(tenant, workspace)) {
2717
+ for (const step of run.steps) {
2718
+ if (step.agent !== agent) continue;
2719
+ // A fan-out template expands into instances and produces nothing
2720
+ // itself; listing it would be a row that never has a result.
2721
+ if (step.status === ("expanded" as StepRecord["status"])) continue;
2722
+ out.push({
2723
+ runId: run.id,
2724
+ flow: run.flow,
2725
+ status: run.status,
2726
+ startedAt: run.startedAt,
2727
+ headline: stepHeadline(step),
2728
+ instruction: step.instruction,
2729
+ stepStatus: step.status,
2730
+ costUsd: step.costUsd,
2731
+ item: step.item,
2732
+ denied: deniedPaths(step),
2733
+ });
2734
+ }
2735
+ }
2736
+ return out;
2737
+ }
2738
+
2739
+ export function listRuns(tenant: string, workspace: string): RunRecord[] {
2740
+ const dir = path.join(workspaceDir(tenant, workspace), "runs");
2741
+ if (!fs.existsSync(dir)) return [];
2742
+ return fs
2743
+ .readdirSync(dir)
2744
+ .filter((f) => f.endsWith(".json"))
2745
+ .map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")) as RunRecord)
2746
+ .sort((a, b) => b.startedAt.localeCompare(a.startedAt));
2747
+ }
2748
+
2749
+ // ------------------------------------------------------------------ provider
2750
+
2751
+ /**
2752
+ * `provider:` — which endpoint the model calls run against, and the two
2753
+ * knobs that make a non-Anthropic gateway usable rather than merely
2754
+ * reachable.
2755
+ *
2756
+ * provider:
2757
+ * base_url: https://openrouter.ai/api
2758
+ * token: ${OPENROUTER_API_KEY}
2759
+ * models:
2760
+ * fast: google/gemini-2.5-flash
2761
+ * max: anthropic/claude-opus-4.1
2762
+ * headers:
2763
+ * X-Title: foldrun
2764
+ *
2765
+ * `models:` is what makes tiers portable rather than merely renamed-proof:
2766
+ * without it `model: fast` means the literal string "haiku" on every
2767
+ * endpoint, which is only true on one of them. `headers:` is the escape
2768
+ * hatch for whatever a gateway wants that our schema will never have —
2769
+ * routing preferences, attribution, tags. Both are the gateway's business,
2770
+ * so neither grows a per-vendor branch in here.
2771
+ */
2772
+ export interface ProviderSpec {
2773
+ /** The preset this block was built from, when `name:` named one. */
2774
+ name: string | null;
2775
+ /** What the endpoint speaks. `anthropic` is reached directly; `openai`
2776
+ * through the runtime's own translator on localhost (translator.ts). */
2777
+ format: WireFormat;
2778
+ /** Which header carries the key on an Anthropic-format endpoint. */
2779
+ auth: AuthShape;
2780
+ baseUrl: string;
2781
+ /** May still contain `${SECRET}` — resolving needs a tenant. */
2782
+ token: string;
2783
+ /** Tier → the id this gateway calls that tier. Absent tiers keep ours. */
2784
+ models: Partial<Record<Tier, string>>;
2785
+ /** Header name → value; values may contain `${SECRET}`. */
2786
+ headers: Record<string, string>;
2787
+ /** Extra request fields for a translated (`format: openai`) endpoint,
2788
+ * merged into the outgoing body verbatim. `null` removes a field the
2789
+ * translation would otherwise send. The escape hatch for every knob this
2790
+ * format deliberately does not model. */
2791
+ params: Record<string, unknown>;
2792
+ /** Things wrong enough to say out loud but not to fail a run over. */
2793
+ warnings: string[];
2794
+ /** A second endpoint, tried once when the first refuses over money, auth
2795
+ * or limits (not when the work itself fails). Same shape, one level deep
2796
+ * — a fallback with its own fallback is a routing table, and that lives
2797
+ * in a gateway, not in frontmatter. */
2798
+ fallback?: ProviderSpec | null;
2799
+ }
2800
+
2801
+ // RFC 9110 field-name characters. Anything else is not a header, and a
2802
+ // gateway would reject it — better to say so here than to have the run fail
2803
+ // with someone else's error message.
2804
+ const HEADER_NAME_RE = /^[A-Za-z0-9!#$%&'*+.^_`|~-]+$/;
2805
+
2806
+ export function parseProvider(raw: unknown): ProviderSpec | null {
2807
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
2808
+ const block = raw as Record<string, unknown>;
2809
+ const warnings: string[] = [];
2810
+
2811
+ // A name resolves to defaults; anything spelled out beside it wins. An
2812
+ // unknown name is a warning and the block reads as if it were absent,
2813
+ // because guessing a URL from a name is how a key ends up at a stranger.
2814
+ const preset = providerPreset(block.name);
2815
+ if (block.name !== undefined && !preset) {
2816
+ warnings.push(`provider.name: "${String(block.name)}" is not a provider this runtime knows — spell out base_url, format and auth instead`);
2817
+ }
2818
+ const baseUrl = (typeof block.base_url === "string" ? block.base_url.trim() : "") || preset?.baseUrl || "";
2819
+ const token = typeof block.token === "string" ? block.token.trim() : "";
2820
+ const formatRaw = typeof block.format === "string" ? block.format.trim().toLowerCase() : "";
2821
+ let format: WireFormat = preset?.format ?? "anthropic";
2822
+ if (formatRaw === "anthropic" || formatRaw === "openai" || formatRaw === "responses") format = formatRaw;
2823
+ else if (formatRaw) warnings.push(`provider.format: "${formatRaw}" is not anthropic, openai or responses — using ${format}`);
2824
+ const authRaw = typeof block.auth === "string" ? block.auth.trim().toLowerCase() : "";
2825
+ let auth: AuthShape = preset?.auth ?? "bearer";
2826
+ if (authRaw === "bearer" || authRaw === "x-api-key") auth = authRaw;
2827
+ else if (authRaw === "api-key" || authRaw === "key" || authRaw === "header") auth = "x-api-key";
2828
+ else if (authRaw) warnings.push(`provider.auth: "${authRaw}" is not bearer or x-api-key — using ${auth}`);
2829
+ if (preset && !baseUrl) {
2830
+ warnings.push(`provider.name: ${preset.name} needs a base_url — ${preset.note ?? "its address is per account"}`);
2831
+ }
2832
+ // A pasted Chat-Completions URL with no format: the runtime would speak
2833
+ // Anthropic at it and get a 404 from the wrong path. Said here, at the
2834
+ // file, not there.
2835
+ if (format === "anthropic" && !preset && baseUrl && looksOpenAiShaped(baseUrl)) {
2836
+ warnings.push(`provider.base_url looks like a Chat-Completions endpoint — add \`format: openai\` to reach it through the translator`);
2837
+ }
2838
+
2839
+ const models: Partial<Record<Tier, string>> = {};
2840
+ if (block.models && typeof block.models === "object" && !Array.isArray(block.models)) {
2841
+ for (const [key, value] of Object.entries(block.models as Record<string, unknown>)) {
2842
+ const tier = resolveTier(key);
2843
+ if (!tier) {
2844
+ warnings.push(
2845
+ `provider.models: "${key}" is not a tier — use ${Object.keys(MODEL_TIERS).join(", ")}. Ignored.`,
2846
+ );
2847
+ continue;
2848
+ }
2849
+ const id = typeof value === "string" ? value.trim() : "";
2850
+ if (!id) {
2851
+ warnings.push(`provider.models.${key}: empty — ignored`);
2852
+ continue;
2853
+ }
2854
+ models[tier] = id;
2855
+ }
2856
+ } else if (block.models !== undefined) {
2857
+ warnings.push("provider.models: expected a map of tier → model id — ignored");
2858
+ }
2859
+
2860
+ // `params:` — the provider's own request fields, passed through as
2861
+ // written. Not validated against any vendor's schema: the endpoint is the
2862
+ // authority on what it accepts, and a list here would be stale in a month.
2863
+ const params: Record<string, unknown> = {};
2864
+ if (block.params && typeof block.params === "object" && !Array.isArray(block.params)) {
2865
+ for (const [key, value] of Object.entries(block.params as Record<string, unknown>)) {
2866
+ const name = key.trim();
2867
+ if (!name) continue;
2868
+ if ((PROTECTED_PARAMS as readonly string[]).includes(name)) {
2869
+ warnings.push(`provider.params.${name}: that is the conversation, not a setting — ignored`);
2870
+ continue;
2871
+ }
2872
+ params[name] = value;
2873
+ }
2874
+ } else if (block.params !== undefined) {
2875
+ warnings.push("provider.params: expected a map of field → value — ignored");
2876
+ }
2877
+ if (Object.keys(params).length && format === "anthropic") {
2878
+ warnings.push(
2879
+ "provider.params only reaches a `format: openai` or `format: responses` endpoint — an Anthropic-shaped one is spoken to directly, so set its knobs at the provider (an OpenRouter preset, say) instead",
2880
+ );
2881
+ }
2882
+
2883
+ const headers: Record<string, string> = {};
2884
+ if (block.headers && typeof block.headers === "object" && !Array.isArray(block.headers)) {
2885
+ for (const [name, value] of Object.entries(block.headers as Record<string, unknown>)) {
2886
+ const key = name.trim();
2887
+ if (!HEADER_NAME_RE.test(key)) {
2888
+ warnings.push(`provider.headers: "${name}" is not a valid header name — ignored`);
2889
+ continue;
2890
+ }
2891
+ const text = value == null ? "" : String(value);
2892
+ // A newline in a value would end the header and start another one —
2893
+ // the whole point of a separator-delimited blob is that nothing in it
2894
+ // may contain the separator.
2895
+ if (/[\r\n]/.test(text)) {
2896
+ warnings.push(`provider.headers.${key}: value contains a line break — ignored`);
2897
+ continue;
2898
+ }
2899
+ headers[key] = text.trim();
2900
+ }
2901
+ } else if (block.headers !== undefined) {
2902
+ warnings.push("provider.headers: expected a map of name → value — ignored");
2903
+ }
2904
+
2905
+ // One level only: the nested block's own `fallback:` is dropped, with a
2906
+ // word about it, rather than followed.
2907
+ let fallback: ProviderSpec | null = null;
2908
+ if (block.fallback !== undefined) {
2909
+ const inner = parseProvider(block.fallback);
2910
+ if (!inner || !inner.baseUrl) warnings.push("provider.fallback: needs a base_url — ignored");
2911
+ else {
2912
+ if (inner.fallback) warnings.push("provider.fallback.fallback: only one level of fallback is followed — ignored");
2913
+ fallback = { ...inner, fallback: null };
2914
+ warnings.push(...inner.warnings.map((w) => `fallback: ${w}`));
2915
+ }
2916
+ }
2917
+
2918
+ return { name: preset?.name ?? null, format, auth, baseUrl, token, models, headers, params, warnings, fallback };
2919
+ }
2920
+
2921
+ /** The env the SDK reads for a tier remap. Our tier names are ours; these
2922
+ * are the SDK's, and this function is the only place the two meet. */
2923
+ const TIER_ENV: Record<Tier, string> = {
2924
+ fast: "ANTHROPIC_DEFAULT_HAIKU_MODEL",
2925
+ default: "ANTHROPIC_DEFAULT_SONNET_MODEL",
2926
+ max: "ANTHROPIC_DEFAULT_OPUS_MODEL",
2927
+ };
2928
+
2929
+ /**
2930
+ * The whole of a provider block as the env the Agent SDK reads. The one
2931
+ * place our format meets the SDK's, so the mapping is testable without a
2932
+ * vault: the caller resolves `${SECRET}` first and passes values.
2933
+ *
2934
+ * The token goes to ANTHROPIC_AUTH_TOKEN (a bearer credential) and
2935
+ * ANTHROPIC_API_KEY is explicitly blanked. Both matter. A key left in the
2936
+ * environment is sent as `x-api-key` and treated as a direct-Anthropic
2937
+ * credential, so a gateway that wants `Authorization: Bearer` gets the
2938
+ * wrong header — and worse, an *unset* key lets the SDK fall back to
2939
+ * authenticating against Anthropic directly, which fails somewhere far from
2940
+ * the file that caused it. A gateway that genuinely wants the key header
2941
+ * asks for it by name: `headers: { x-api-key: ${TOKEN} }`.
2942
+ */
2943
+ export function providerEnvFor(spec: {
2944
+ baseUrl?: string;
2945
+ /** Already secret-substituted. */
2946
+ token?: string;
2947
+ /** Which header the key rides in. Bearer unless the endpoint says
2948
+ * otherwise: the SDK sends ANTHROPIC_API_KEY as x-api-key and
2949
+ * ANTHROPIC_AUTH_TOKEN as a bearer, and blanks the other so it cannot
2950
+ * fall back to Anthropic's own endpoint with the wrong credential. */
2951
+ auth?: AuthShape;
2952
+ models: Partial<Record<Tier, string>>;
2953
+ headers: Record<string, string>;
2954
+ }): Record<string, string> {
2955
+ const env: Record<string, string> = {};
2956
+ if (spec.baseUrl) env.ANTHROPIC_BASE_URL = spec.baseUrl;
2957
+ if (spec.token) {
2958
+ if (spec.auth === "x-api-key") {
2959
+ env.ANTHROPIC_API_KEY = spec.token;
2960
+ env.ANTHROPIC_AUTH_TOKEN = "";
2961
+ } else {
2962
+ env.ANTHROPIC_AUTH_TOKEN = spec.token;
2963
+ env.ANTHROPIC_API_KEY = "";
2964
+ }
2965
+ }
2966
+ for (const [tier, id] of Object.entries(spec.models)) {
2967
+ if (id) env[TIER_ENV[tier as Tier]] = id;
2968
+ }
2969
+ const lines = Object.entries(spec.headers).map(([k, v]) => `${k}: ${v}`);
2970
+ if (lines.length) env.ANTHROPIC_CUSTOM_HEADERS = lines.join("\n");
2971
+ return env;
2972
+ }
2973
+
2974
+ // The workspace tree, for history's first commit — see registerTreeReader.
2975
+ registerTreeReader((tenant, scope) => {
2976
+ if (scope === "@library" || !fs.existsSync(workspaceDir(tenant, scope))) return null;
2977
+ const dir = workspaceDir(tenant, scope);
2978
+ return listWorkspaceFiles(tenant, scope).flatMap((rel) => {
2979
+ try {
2980
+ return [{ path: rel, before: null, after: fs.readFileSync(path.join(dir, rel), "utf8") }];
2981
+ } catch {
2982
+ return [];
2983
+ }
2984
+ });
2985
+ });