@foldrun/core 2.5.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/src/agent-import.js +2 -2
  2. package/dist/src/agent-tools.js +2 -2
  3. package/dist/src/agents-md.d.ts +5 -1
  4. package/dist/src/agents-md.js +42 -18
  5. package/dist/src/capture.js +39 -1
  6. package/dist/src/completions.js +25 -3
  7. package/dist/src/deploy.js +72 -7
  8. package/dist/src/egress.d.ts +16 -0
  9. package/dist/src/evals.d.ts +22 -1
  10. package/dist/src/evals.js +43 -14
  11. package/dist/src/flow-lint.d.ts +14 -0
  12. package/dist/src/flow-lint.js +35 -0
  13. package/dist/src/flow-patterns.js +29 -14
  14. package/dist/src/frontmatter-fields.d.ts +27 -0
  15. package/dist/src/frontmatter-fields.js +51 -3
  16. package/dist/src/frontmatter-guard.d.ts +19 -0
  17. package/dist/src/frontmatter-guard.js +67 -0
  18. package/dist/src/frontmatter-schema.d.ts +2 -2
  19. package/dist/src/frontmatter-schema.js +30 -2
  20. package/dist/src/frontmatter-text.d.ts +31 -0
  21. package/dist/src/frontmatter-text.js +102 -0
  22. package/dist/src/judge-verdict.d.ts +19 -0
  23. package/dist/src/judge-verdict.js +54 -0
  24. package/dist/src/matter.d.ts +34 -0
  25. package/dist/src/matter.js +104 -0
  26. package/dist/src/network.d.ts +0 -1
  27. package/dist/src/network.js +10 -7
  28. package/dist/src/package.js +9 -2
  29. package/dist/src/provider-health.js +2 -2
  30. package/dist/src/run-container.d.ts +9 -0
  31. package/dist/src/run-container.js +34 -1
  32. package/dist/src/runner.js +193 -39
  33. package/dist/src/scan.d.ts +11 -0
  34. package/dist/src/scan.js +86 -8
  35. package/dist/src/secret-files.js +23 -2
  36. package/dist/src/secrets.d.ts +62 -2
  37. package/dist/src/secrets.js +206 -20
  38. package/dist/src/starter.js +23 -0
  39. package/dist/src/step-exec.d.ts +10 -2
  40. package/dist/src/step-exec.js +12 -9
  41. package/dist/src/store.d.ts +17 -3
  42. package/dist/src/store.js +14 -10
  43. package/package.json +1 -1
  44. package/src/agent-import.ts +2 -2
  45. package/src/agent-tools.ts +2 -2
  46. package/src/agents-md.ts +39 -14
  47. package/src/capture.ts +33 -1
  48. package/src/completions.ts +26 -3
  49. package/src/deploy.ts +59 -7
  50. package/src/egress.ts +15 -1
  51. package/src/evals.ts +45 -13
  52. package/src/flow-lint.ts +36 -0
  53. package/src/flow-patterns.ts +26 -13
  54. package/src/frontmatter-fields.ts +74 -3
  55. package/src/frontmatter-guard.ts +70 -0
  56. package/src/frontmatter-schema.ts +30 -4
  57. package/src/frontmatter-text.ts +109 -0
  58. package/src/judge-verdict.ts +52 -0
  59. package/src/matter.ts +112 -0
  60. package/src/network.ts +10 -6
  61. package/src/package.ts +8 -2
  62. package/src/provider-health.ts +2 -2
  63. package/src/run-container.ts +41 -1
  64. package/src/runner.ts +189 -40
  65. package/src/scan.ts +86 -8
  66. package/src/secret-files.ts +22 -2
  67. package/src/secrets.ts +237 -23
  68. package/src/starter.ts +23 -0
  69. package/src/step-exec.ts +21 -8
  70. package/src/store.ts +23 -10
@@ -25,6 +25,7 @@ import { joinGroup, splitToRail, type Groups } from "./arrange.ts";
25
25
  import { lintFlow } from "./flow-lint.ts";
26
26
  import { toLF, asRaw } from "./eol.ts";
27
27
  import { removalImpact, stepLabel, type RemovalImpact } from "./step-removal.ts";
28
+ import { keySpan, InlineBlockError } from "./frontmatter-text.ts";
28
29
 
29
30
  // ---------- flow files ----------
30
31
 
@@ -474,13 +475,15 @@ function splitComment(value: string): { value: string; comment: string } {
474
475
  return m ? { value: m[1], comment: m[2] } : { value, comment: "" };
475
476
  }
476
477
 
478
+ /** Where a key is written — at the top or inside its job block
479
+ * (frontmatter-text.ts) — with the text after its colon and its indent. */
477
480
  function keyLine(lines: string[], close: number, key: string) {
478
- const re = new RegExp(`^${key.replace(/[-]/g, "\\-")}:(.*)$`);
479
- for (let i = 1; i < close; i++) {
480
- const m = lines[i].match(re);
481
- if (m) return { index: i, rest: m[1] };
482
- }
483
- return null;
481
+ const where = keySpan(lines.slice(1, close), key);
482
+ if (!where) return null;
483
+ const index = where.at + 1;
484
+ // Quoted or not, as keySpan matched it.
485
+ const m = lines[index].slice(where.indent.length).match(new RegExp(`^["']?${key.replace(/[.*+?^${}()|[\]\\-]/g, "\\$&")}["']?\\s*:(.*)$`));
486
+ return m ? { index, rest: m[1], indent: where.indent } : null;
484
487
  }
485
488
 
486
489
  /** Block-style item lines under a key: ` - name`, until anything else. */
@@ -502,7 +505,15 @@ export function frontmatterList(raw: string, key: string): string[] {
502
505
  const lines = raw.split(eolOf(raw));
503
506
  const b = frontBounds(lines);
504
507
  if (!b) return [];
505
- const k = keyLine(lines, b.close, key);
508
+ let k: ReturnType<typeof keyLine>;
509
+ try {
510
+ k = keyLine(lines, b.close, key);
511
+ } catch (err) {
512
+ // A block these text edits do not handle (frontmatter-text.ts) is the
513
+ // writer's to refuse; a read of it is empty, not an error.
514
+ if (err instanceof InlineBlockError) return [];
515
+ throw err;
516
+ }
506
517
  if (!k) return [];
507
518
  const v = splitComment(k.rest).value.trim();
508
519
  if (v.startsWith("[")) return v.replace(/^\[|\]$/g, "").split(",").map(itemName).filter(Boolean);
@@ -523,9 +534,11 @@ export function editFrontmatterList(raw: string, key: string, name: string, acti
523
534
  const lines = raw.split(eol);
524
535
  const b = frontBounds(lines);
525
536
  if (!b) return action === "remove" ? raw : ["---", `${key}: [${name}]`, "---", ...lines].join(eol);
537
+ // Located first, so a key in a block these edits do not handle is refused
538
+ // (InlineBlockError) rather than read as an empty list.
539
+ const k = keyLine(lines, b.close, key);
526
540
  const has = frontmatterList(raw, key).includes(name);
527
541
  if ((action === "add") === has) return raw;
528
- const k = keyLine(lines, b.close, key);
529
542
  if (!k) {
530
543
  lines.splice(b.close, 0, `${key}: [${name}]`);
531
544
  return lines.join(eol);
@@ -536,18 +549,18 @@ export function editFrontmatterList(raw: string, key: string, name: string, acti
536
549
  // Flow style, or a lone scalar (read as a one-item list): that one line.
537
550
  const tokens = v.startsWith("[") ? v.replace(/^\[|\]$/g, "").split(",").map((t) => t.trim()).filter(Boolean) : [v];
538
551
  const next = action === "add" ? [...tokens, name] : tokens.filter((t) => itemName(t) !== name);
539
- lines[k.index] = `${key}: [${next.join(", ")}]${comment}`;
552
+ lines[k.index] = `${k.indent}${key}: [${next.join(", ")}]${comment}`;
540
553
  return lines.join(eol);
541
554
  }
542
555
  const items = blockItems(lines, k.index, b.close);
543
556
  if (action === "add") {
544
- const indent = items.length ? lines[items[0]].match(/^(\s*)-/)![1] : " ";
557
+ const indent = items.length ? lines[items[0]].match(/^(\s*)-/)![1] : `${k.indent} `;
545
558
  lines.splice(items.length ? items[items.length - 1] + 1 : k.index + 1, 0, `${indent}- ${name}`);
546
559
  return lines.join(eol);
547
560
  }
548
561
  const drop = items.filter((i) => blockItemName(lines[i]) === name);
549
562
  for (const i of [...drop].reverse()) lines.splice(i, 1);
550
- if (drop.length === items.length) lines[k.index] = `${key}: []${comment}`;
563
+ if (drop.length === items.length) lines[k.index] = `${k.indent}${key}: []${comment}`;
551
564
  return lines.join(eol);
552
565
  }
553
566
 
@@ -577,9 +590,9 @@ export function setFrontmatterScalar(raw: string, key: string, value: string): s
577
590
  const { value: v, comment } = splitComment(k.rest);
578
591
  let end = k.index + 1;
579
592
  if (/^\s*[|>][-+]?\d*\s*$/.test(v) || !v.trim()) {
580
- while (end < b.close && /^\s+\S/.test(lines[end])) end++;
593
+ while (end < b.close && (lines[end].match(/^\s*/)?.[0].length ?? 0) > k.indent.length && /\S/.test(lines[end])) end++;
581
594
  }
582
- lines.splice(k.index, end - k.index, `${line}${comment}`);
595
+ lines.splice(k.index, end - k.index, `${k.indent}${line}${comment}`);
583
596
  return lines.join(eol);
584
597
  }
585
598
  const name = keyLine(lines, b.close, "name");
@@ -34,8 +34,33 @@ export interface FieldSpec {
34
34
  legacy?: boolean;
35
35
  /** One line: what it is. The completion's hint. */
36
36
  help: string;
37
+ /** Which of the four jobs it does, when it does one (set from JOB_FIELDS
38
+ * below, not by hand): the block it may also be written in. */
39
+ job?: Job;
37
40
  }
38
41
 
42
+ /**
43
+ * The four jobs of running agents — governance, guardrail, observability,
44
+ * evaluation (the owner's choice of words, 21 Oct 2026). A field that does
45
+ * one may be written flat, as always, or inside a block of that name:
46
+ *
47
+ * guardrail:
48
+ * limits: {repeat: 5}
49
+ * network: [api.example.com]
50
+ *
51
+ * reads exactly as `limits:` and `network:` at the top. matter.ts folds the
52
+ * blocks as it parses, so every reader sees flat keys; check reports a key
53
+ * set both ways (config/conflict) or in the wrong block.
54
+ */
55
+ export type Job = "governance" | "guardrail" | "observability" | "evaluation";
56
+
57
+ export const JOBS: readonly { name: Job; title: string; what: string }[] = [
58
+ { name: "governance", title: "Governance", what: "who answers for it and what it may spend, decide and ship" },
59
+ { name: "guardrail", title: "Guardrail", what: "what it may reach and call, and what stops it" },
60
+ { name: "observability", title: "Observability", what: "what is recorded, and who is told" },
61
+ { name: "evaluation", title: "Evaluation", what: "how its work is judged" },
62
+ ];
63
+
39
64
  export type SchemaKind = "agent" | "agents-md" | "flow" | "tool" | "skill" | "eval" | "hook";
40
65
 
41
66
  /** What can start a flow. store.ts re-exports it; parseFlow and the editor
@@ -109,6 +134,7 @@ const AGENTS_MD: FieldSpec[] = [
109
134
  { key: "network_enforce", type: "boolean", help: "true: a site off a step's network: list is refused, not only named in the trace. The account's or the workspace's true is enough" },
110
135
  { key: "telemetry", type: "object", validated: true, help: "send each run as an OpenTelemetry trace — otlp: <https …/v1/traces>, headers: { authorization: ${SECRET} }" },
111
136
  { key: "deploy_block", type: "string", values: ["none", "secrets", "security"], help: "refuse a deploy on scanner findings: secrets (a key written in) or security (any security warning)" },
137
+ { key: "judge", type: "object", validated: true, help: "the model every judge: here grades with — {model: fast | default | max, effort}; an eval's own model: wins" },
112
138
  { key: "schedule", type: ["string", "object"], validated: true, legacy: true, help: "refused: only a flow has a clock" },
113
139
  ];
114
140
 
@@ -209,10 +235,55 @@ const SAID_WHAT_IT_IS: FieldSpec[] = [
209
235
  { key: "type", type: "string", validated: true, legacy: true, help: "no longer read — the path says what this is" },
210
236
  ];
211
237
 
238
+ /** Which fields each job holds, per kind of file. One table: the blocks'
239
+ * help, the fold, the editor's completions and check all read it. */
240
+ const JOB_FIELDS: Partial<Record<SchemaKind, Partial<Record<Job, readonly string[]>>>> = {
241
+ agent: {
242
+ governance: ["owner", "budget"],
243
+ guardrail: ["limits", "network", "hooks", "disallowedTools", "permissionMode", "secrets"],
244
+ },
245
+ flow: {
246
+ governance: ["owner", "approvers", "approve_within", "budget", "priority"],
247
+ guardrail: ["throttle", "debounce", "overlap", "disable_after", "live"],
248
+ observability: ["issues", "sla"],
249
+ },
250
+ "agents-md": {
251
+ governance: ["owner", "budget", "priority", "concurrency", "deploy_block"],
252
+ guardrail: ["limits", "network", "network_enforce", "hooks"],
253
+ observability: ["alerts", "telemetry", "notify", "capture"],
254
+ evaluation: ["judge"],
255
+ },
256
+ };
257
+
258
+ /** The fields of one kind that do this job, in schema order. */
259
+ export function fieldsOfJob(kind: SchemaKind, job: Job): string[] {
260
+ return [...(JOB_FIELDS[kind]?.[job] ?? [])];
261
+ }
262
+
263
+ /** Every key any kind lets a block hold, per job — what matter.ts may lift
264
+ * out of a block without knowing which kind of file it is reading. */
265
+ export const FOLDABLE: Readonly<Record<Job, ReadonlySet<string>>> = Object.fromEntries(
266
+ JOBS.map(({ name }) => [name, new Set(Object.values(JOB_FIELDS).flatMap((byJob) => byJob?.[name] ?? []))]),
267
+ ) as unknown as Record<Job, ReadonlySet<string>>;
268
+
269
+ /** A kind's fields with their jobs set, and a block field per job it has. */
270
+ function withJobs(kind: SchemaKind, fields: FieldSpec[]): FieldSpec[] {
271
+ const byJob = JOB_FIELDS[kind] ?? {};
272
+ const jobOf = new Map<string, Job>();
273
+ for (const [job, keys] of Object.entries(byJob) as [Job, readonly string[]][]) for (const k of keys) jobOf.set(k, job);
274
+ const tagged = fields.map((f) => (jobOf.has(f.key) ? { ...f, job: jobOf.get(f.key)! } : f));
275
+ const blocks: FieldSpec[] = JOBS.filter((j) => (byJob[j.name]?.length ?? 0) > 0).map((j) => ({
276
+ key: j.name,
277
+ type: "object",
278
+ help: `${j.title} — ${j.what}: ${byJob[j.name]!.join(", ")}, as one block`,
279
+ }));
280
+ return [...tagged, ...blocks];
281
+ }
282
+
212
283
  export const FRONTMATTER: Readonly<Record<SchemaKind, readonly FieldSpec[]>> = {
213
- agent: [...AGENT, ...SAID_WHAT_IT_IS],
214
- "agents-md": AGENTS_MD,
215
- flow: [...FLOW, ...SAID_WHAT_IT_IS],
284
+ agent: withJobs("agent", [...AGENT, ...SAID_WHAT_IT_IS]),
285
+ "agents-md": withJobs("agents-md", AGENTS_MD),
286
+ flow: withJobs("flow", [...FLOW, ...SAID_WHAT_IT_IS]),
216
287
  tool: [...TOOL, ...SAID_WHAT_IT_IS.filter((f) => f.key !== "type")],
217
288
  skill: [...SKILL, ...SAID_WHAT_IT_IS],
218
289
  eval: [...EVAL, ...SAID_WHAT_IT_IS],
@@ -0,0 +1,70 @@
1
+ // The last word on every frontmatter edit made as text (agents-md.ts
2
+ // patchFrontmatter, network.ts setNetworkSetting, the canvas's list and
3
+ // scalar edits): the result is read back the way a run reads it, and the
4
+ // edit is refused — the file left as it was — unless it parses, set what it
5
+ // said it set, left everything else as it was, and made no key both flat and
6
+ // in its block. The text writers handle the shapes people write; this makes
7
+ // a shape they do not handle a refusal, never a broken or doubled file
8
+ // (Codex debate, 21 Oct 2026).
9
+
10
+ import { isDeepStrictEqual } from "node:util";
11
+ import { configMatter, jobFold } from "./matter.ts";
12
+
13
+ /** A write the guard refused. 409: the file is fine, this edit is not. */
14
+ export class UnsafeEditError extends Error {
15
+ status = 409;
16
+ constructor(why: string) {
17
+ super(`not saved — ${why}. Edit the file itself, or write its blocks one key per line`);
18
+ }
19
+ }
20
+
21
+ const front = (text: string) => configMatter(text).data as Record<string, unknown>;
22
+
23
+ /** What a changed key must read back as: a value (null: absent), or a test
24
+ * of what it reads as, for a writer whose output form is its own (a canvas
25
+ * list, a scalar YAML may type). */
26
+ export type Expect = unknown | ((value: unknown) => boolean);
27
+
28
+ /**
29
+ * `after` if it is `before` with only `changed` keys different and each
30
+ * `expect`ed key reading back as expected; throws UnsafeEditError otherwise.
31
+ * Checked on every call — an edit that changed nothing must still have done
32
+ * what it said — and refused when `before` cannot be read, since nothing
33
+ * then could be compared.
34
+ */
35
+ export function guardEdit(before: string, after: string, changed: readonly string[], expect: Record<string, Expect> = {}): string {
36
+ let was: Record<string, unknown>;
37
+ try {
38
+ was = front(before);
39
+ } catch {
40
+ throw new UnsafeEditError("the frontmatter does not parse as it is, so this edit could not be checked — fix the file first");
41
+ }
42
+ let now: Record<string, unknown>;
43
+ try {
44
+ now = front(after);
45
+ } catch (err) {
46
+ throw new UnsafeEditError(`the result would not parse (${err instanceof Error ? err.message.split("\n")[0] : String(err)})`);
47
+ }
48
+ const conflictsBefore = jobFold(was)?.conflicts ?? new Map();
49
+ for (const [k, block] of jobFold(now)?.conflicts ?? new Map()) {
50
+ if (!conflictsBefore.has(k)) throw new UnsafeEditError(`\`${k}\` would be written both at the top and inside \`${block}:\``);
51
+ }
52
+ for (const [k, want] of Object.entries(expect)) {
53
+ const ok = typeof want === "function" ? (want as (v: unknown) => boolean)(now[k]) : isDeepStrictEqual(now[k], want === null ? undefined : want);
54
+ if (!ok) throw new UnsafeEditError(`\`${k}\` would not read back as written`);
55
+ }
56
+ for (const k of new Set([...Object.keys(was), ...Object.keys(now)])) {
57
+ if (changed.includes(k)) continue;
58
+ if (!isDeepStrictEqual(was[k], now[k])) throw new UnsafeEditError(`\`${k}\` would change too`);
59
+ }
60
+ return after;
61
+ }
62
+
63
+ /** The front of `text` as a run reads it, or null when it does not parse. */
64
+ export function readFront(text: string): Record<string, unknown> | null {
65
+ try {
66
+ return front(text);
67
+ } catch {
68
+ return null;
69
+ }
70
+ }
@@ -2,7 +2,8 @@
2
2
  // the names. Node-side (it reads store's parsers); the browser takes the
3
3
  // field list alone from frontmatter-fields.ts.
4
4
 
5
- import { FRONTMATTER, type FieldType, type SchemaKind } from "./frontmatter-fields.ts";
5
+ import { FRONTMATTER, JOBS, type FieldType, type SchemaKind } from "./frontmatter-fields.ts";
6
+ import { jobFold } from "./matter.ts";
6
7
  import { resolveEffort, parseDuration, parseInstant, parseRate, STEP_OPTION_KEYS } from "./store.ts";
7
8
  import { budgetProblem } from "./budget.ts";
8
9
  import { cronProblem } from "./cron.ts";
@@ -65,8 +66,8 @@ const CHECKS: Partial<Record<SchemaKind, Record<string, (value: unknown) => stri
65
66
 
66
67
  export interface FieldProblem {
67
68
  key: string;
68
- rule: "config/unknown-field" | "config/wrong-type" | "config/invalid-value" | "config/missing-field";
69
- level: "warn" | "info";
69
+ rule: "config/unknown-field" | "config/wrong-type" | "config/invalid-value" | "config/missing-field" | "config/conflict" | "config/misplaced";
70
+ level: "error" | "warn" | "info";
70
71
  message: string;
71
72
  }
72
73
 
@@ -97,7 +98,31 @@ export function frontmatterProblems(
97
98
  const fields = FRONTMATTER[kind];
98
99
  const known = fields.map((f) => f.key);
99
100
  const out: FieldProblem[] = [];
101
+ // The four job blocks (matter.ts folds them): a key set both ways, and a
102
+ // key left in a block that does not hold it.
103
+ const fold = jobFold(data);
104
+ for (const [key, job] of fold?.conflicts ?? []) {
105
+ out.push({ key, rule: "config/conflict", level: "error", message: `\`${key}:\` is set twice — at the top and inside \`${job}:\`. Only the top one would be read; keep one` });
106
+ }
107
+ for (const { name: job } of JOBS) {
108
+ const block = data[job];
109
+ if (!block || typeof block !== "object" || Array.isArray(block)) continue;
110
+ for (const inner of Object.keys(block as Record<string, unknown>)) {
111
+ const spec = fields.find((f) => f.key === inner);
112
+ const message = spec?.job
113
+ ? `\`${inner}:\` is a ${spec.job} field, not a ${job} one — move it into \`${spec.job}:\` or to the top. Inside \`${job}:\` it is not read`
114
+ : spec
115
+ ? `\`${inner}:\` belongs to none of the four jobs — write it at the top. Inside \`${job}:\` it is not read`
116
+ : `\`${inner}:\` inside \`${job}:\` is not a field foldrun reads${belongsIn(inner, kind) ? ` here — it is read in ${belongsIn(inner, kind)}` : ""}; it is ignored`;
117
+ // A real setting in the wrong block is a setting silently not applied
118
+ // — a guardrail written under governance: guards nothing — so it is an
119
+ // error (Codex debate). A word foldrun does not read at all is a note.
120
+ out.push({ key: inner, rule: "config/misplaced", level: spec ? "error" : "info", message });
121
+ }
122
+ }
100
123
  for (const [key, value] of Object.entries(data)) {
124
+ // A block holding only misplaced keys is reported above, key by key.
125
+ if (JOBS.some((j) => j.name === key) && value && typeof value === "object" && !Array.isArray(value)) continue;
101
126
  const spec = fields.find((f) => f.key === key);
102
127
  const check = CHECKS[kind]?.[key];
103
128
  if (!spec) {
@@ -110,7 +135,8 @@ export function frontmatterProblems(
110
135
  // it is an option of a flow's step.
111
136
  out.push({ key, rule: "config/unknown-field", level: "warn", message: `\`${key}:\` is not read on an agent — it is a step option: indent it under the step in the flow (\` ${key}: ${shown(value)}\`). Here it is ignored` });
112
137
  } else if (home) {
113
- out.push({ key, rule: "config/unknown-field", level: "warn", message: `\`${key}:\` is read in ${home}, not in ${kind === "agents-md" ? "AGENTS.md" : kind === "eval" ? "an eval" : `a ${kind}`} — here it is ignored` });
138
+ const where = fold?.folded.get(key);
139
+ out.push({ key, rule: "config/unknown-field", level: "warn", message: `\`${key}:\`${where ? ` (inside \`${where}:\`)` : ""} is read in ${home}, not in ${kind === "agents-md" ? "AGENTS.md" : kind === "eval" ? "an eval" : `a ${kind}`} — here it is ignored` });
114
140
  } else {
115
141
  out.push({ key, rule: "config/unknown-field", level: "info", message: `\`${key}:\` is not a field foldrun reads; it is ignored` });
116
142
  }
@@ -0,0 +1,109 @@
1
+ // Frontmatter edited as text, where it is written: at the top or inside one
2
+ // of the four job blocks. No node imports — the flow canvas (flow-patterns.ts)
3
+ // runs in the browser and edits frontmatter with it too.
4
+
5
+ /** A block header on its own line — an anchor and a trailing `# comment`
6
+ * allowed. */
7
+ const JOB_BLOCK = /^["']?(governance|guardrail|observability|evaluation)["']?\s*:\s*(&\S+\s*)?(#.*)?$/;
8
+ /** Any other block header: an inline map, an alias, a tag — a form these
9
+ * writers do not edit. */
10
+ const JOB_OTHER = /^["']?(governance|guardrail|observability|evaluation)["']?\s*:\s*\S/;
11
+ /** An inline map closed on its own line: `guardrail: {network: [a.com]}`. */
12
+ const JOB_ONE_LINE = /^["']?(governance|guardrail|observability|evaluation)["']?\s*:\s*\{.*\}\s*(#.*)?$/;
13
+
14
+ const indentOf = (l: string) => l.match(/^[ \t]*/)?.[0].length ?? 0;
15
+
16
+ export interface KeySpan {
17
+ /** The key's line. */
18
+ at: number;
19
+ /** One past its last line. */
20
+ end: number;
21
+ /** What its line starts with. */
22
+ indent: string;
23
+ /** The block header's line, when it is inside one. */
24
+ block: number | null;
25
+ }
26
+
27
+ /** Thrown for a key a writer cannot edit in place: one that is, or may be,
28
+ * inside a block not written one key per line (`guardrail: {…}`, an alias).
29
+ * Rewriting it would mean re-serialising the person's YAML, and adding the
30
+ * key at the top beside it would be a config/conflict — so the writer says
31
+ * what to write instead. */
32
+ export class InlineBlockError extends Error {
33
+ status = 409;
34
+ constructor(key: string, block: string) {
35
+ super(`\`${key}\` may be inside \`${block}:\`, which is not written one key per line — write \`${block}:\` one key per line to change it here, or edit the file`);
36
+ }
37
+ }
38
+
39
+ /** A comment or blank line: neither ends a block nor is one of its keys. */
40
+ const filler = (l: string) => l.trim() === "" || /^\s*#/.test(l);
41
+
42
+ /**
43
+ * Where a key is written in frontmatter lines: at the top, or a direct child
44
+ * of one of the four job blocks (`guardrail:` and so on). Every writer that
45
+ * edits a key as text reads it here, so a file written in blocks is edited
46
+ * where it is — never given a second, flat copy that check would refuse as
47
+ * config/conflict. A key inside an inline block map throws InlineBlockError.
48
+ */
49
+ export function keySpan(lines: string[], key: string): KeySpan | null {
50
+ const k = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
51
+ const span = (at: number, indent: string, block: number | null): KeySpan => {
52
+ let end = at + 1;
53
+ // The key continues while lines are indented deeper, are an item of its
54
+ // list written at its own indent (YAML allows `key:\n- a`), or are blank
55
+ // lines and comments inside it.
56
+ while (
57
+ end < lines.length &&
58
+ (filler(lines[end]) ||
59
+ indentOf(lines[end]) > indent.length ||
60
+ (indentOf(lines[end]) === indent.length && /^[ \t]*-(\s|$)/.test(lines[end])))
61
+ ) end++;
62
+ // Blank lines and comments at its foot, no deeper than the key, belong
63
+ // to what follows.
64
+ while (end > at + 1 && filler(lines[end - 1]) && (lines[end - 1].trim() === "" || indentOf(lines[end - 1]) <= indent.length)) end--;
65
+ return { at, end, indent, block };
66
+ };
67
+ // A key may be quoted: `"network": none`.
68
+ const named = `["']?${k}["']?\\s*:`;
69
+ const top = lines.findIndex((l) => new RegExp(`^${named}`).test(l));
70
+ if (top !== -1) return span(top, "", null);
71
+ for (let h = 0; h < lines.length; h++) {
72
+ const other = !JOB_BLOCK.test(lines[h]) && JOB_OTHER.exec(lines[h]);
73
+ if (other) {
74
+ // One closed line can be read: refuse only if the key is in it. Any
75
+ // other form (a map over several lines, an alias) cannot be, so any
76
+ // key not found at the top is refused rather than added beside it.
77
+ if (!JOB_ONE_LINE.test(lines[h]) || new RegExp(`[{,]\\s*${named}`).test(lines[h])) throw new InlineBlockError(key, other[1]!);
78
+ continue;
79
+ }
80
+ if (!JOB_BLOCK.test(lines[h])) continue;
81
+ // Its children sit at the indent of its first indented line; a deeper
82
+ // line of the same name belongs to one of them, not to the block.
83
+ // Comments and blank lines, indented or not, do not end it.
84
+ let child = -1;
85
+ let merged = false;
86
+ for (let n = h + 1; n < lines.length && (/^[ \t]/.test(lines[n]) || filler(lines[n])); n++) {
87
+ if (filler(lines[n])) continue;
88
+ if (child === -1) child = indentOf(lines[n]);
89
+ if (indentOf(lines[n]) !== child) continue;
90
+ const m = new RegExp(`^([ \t]+)${named}`).exec(lines[n]);
91
+ if (m) return span(n, m[1]!, h);
92
+ if (/^[ \t]+<<\s*:/.test(lines[n])) merged = true;
93
+ }
94
+ // `<<: *policy` may bring the key in from elsewhere: not one this text
95
+ // edit can see, so it is refused rather than added beside it.
96
+ if (merged) throw new InlineBlockError(key, JOB_BLOCK.exec(lines[h])![1]!);
97
+ }
98
+ return null;
99
+ }
100
+
101
+ /** A block header left with nothing under it is removed with its last key.
102
+ * Blank lines and comments between it and its next key do not count as
103
+ * nothing: the header stays while any indented key follows them. */
104
+ export function dropEmptyBlock(lines: string[], header: number): string[] {
105
+ let n = header + 1;
106
+ while (n < lines.length && filler(lines[n])) n++;
107
+ const next = lines[n];
108
+ return next === undefined || !/^[ \t]+\S/.test(next) ? [...lines.slice(0, header), ...lines.slice(header + 1)] : lines;
109
+ }
@@ -0,0 +1,52 @@
1
+ // What a judge said: the PASS or FAIL alone on its last line, after its
2
+ // reasoning. Shared by the eval judge (evals.ts) and a flow step's
3
+ // `verify: judge:` (step-exec.ts), which ask for the same shape.
4
+ //
5
+ // The word comes last because, asked for it first, a judge commits before
6
+ // it has looked and then argues the other way — "FAIL … which meets the
7
+ // criterion" (vaza evals, 21 Oct 2026).
8
+
9
+ import { EFFORT_LEVELS } from "./frontmatter-fields.ts";
10
+
11
+ /** The line to end a judge prompt with. */
12
+ export const JUDGE_VERDICT_INSTRUCTION =
13
+ "Check each part against the text in a few short sentences. Then, on the last line and alone, write exactly PASS or FAIL — PASS only if every part holds.";
14
+
15
+ /**
16
+ * The judge's verdict: the PASS or FAIL alone on its last line, with the
17
+ * reasoning before it as the detail. An answer in the old shape (the word
18
+ * first) still reads; one with no verdict at all is a FAIL that says so.
19
+ */
20
+ export function judgeVerdict(text: string): { passed: boolean; detail: string } {
21
+ const lines = text.trim().split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
22
+ const word = (l: string | undefined) => /^[*_`#\s]*(PASS|FAIL)[*_`.\s]*$/i.exec(l ?? "")?.[1]?.toUpperCase();
23
+ let verdict = word(lines[lines.length - 1]);
24
+ let reason = lines.slice(0, -1).join(" ");
25
+ if (!verdict) {
26
+ const first = /^[*_`#\s]*(PASS|FAIL)\b[*_`:.\s-]*(.*)$/i.exec(lines[0] ?? "");
27
+ if (first) {
28
+ verdict = first[1]!.toUpperCase();
29
+ reason = [first[2], ...lines.slice(1)].join(" ").trim();
30
+ }
31
+ }
32
+ if (!verdict) return { passed: false, detail: `judge gave no PASS or FAIL: ${text.trim().slice(0, 300) || "nothing"}` };
33
+ // The end of the reasoning is the part that decided it.
34
+ const why = reason.length > 400 ? `…${reason.slice(-400)}` : reason;
35
+ return { passed: verdict === "PASS", detail: `${verdict}${why ? ` — ${why}` : ""}` };
36
+ }
37
+
38
+ /** `judge:` in AGENTS.md — the model and effort every judge in the workspace
39
+ * grades with, unless an eval names its own. `model` is a tier or alias as
40
+ * anywhere else; what is wrong with the block, in words, for check. */
41
+ export function parseJudge(raw: unknown): { model?: string; effort?: string; problems: string[] } {
42
+ if (raw === undefined || raw === null) return { problems: [] };
43
+ if (typeof raw !== "object" || Array.isArray(raw)) return { problems: ["judge: is a block — {model: fast | default | max, effort: low…max}"] };
44
+ const o = raw as Record<string, unknown>;
45
+ const problems: string[] = [];
46
+ for (const k of Object.keys(o)) if (k !== "model" && k !== "effort") problems.push(`judge: ${k} — not a key; the keys are model and effort`);
47
+ const model = typeof o.model === "string" && o.model.trim() ? o.model.trim() : undefined;
48
+ if (o.model !== undefined && !model) problems.push("judge: model — a tier (fast, default, max), an alias or a model id");
49
+ const effort = typeof o.effort === "string" && (EFFORT_LEVELS as readonly string[]).includes(o.effort.trim()) ? o.effort.trim() : undefined;
50
+ if (o.effort !== undefined && !effort) problems.push(`judge: effort ${String(o.effort)} — low, medium, high, xhigh or max`);
51
+ return { ...(model ? { model } : {}), ...(effort ? { effort } : {}), problems };
52
+ }
package/src/matter.ts CHANGED
@@ -21,13 +21,125 @@
21
21
  // it.
22
22
 
23
23
  import grayMatter from "gray-matter";
24
+ import { JOBS, FOLDABLE, type Job } from "./frontmatter-fields.ts";
24
25
 
25
26
  type Options = NonNullable<Parameters<typeof grayMatter>[1]>;
26
27
 
28
+ /** Where the four job blocks put things, on the parsed data — out of sight
29
+ * of every reader that spreads it or writes it out as JSON. */
30
+ const FOLD = Symbol.for("foldrun.jobBlocks");
31
+
32
+ export interface JobFold {
33
+ /** A key lifted out of a block, and the block it was in. */
34
+ folded: Map<string, Job>;
35
+ /** A key set both at the top and inside a block: the top one is kept, and
36
+ * check refuses the file (config/conflict). */
37
+ conflicts: Map<string, Job>;
38
+ }
39
+
40
+ /** What the fold did to this data, or null when it had no blocks. */
41
+ export function jobFold(data: unknown): JobFold | null {
42
+ return data && typeof data === "object" ? ((data as Record<symbol, JobFold | undefined>)[FOLD] ?? null) : null;
43
+ }
44
+
45
+ /**
46
+ * The four job blocks (`governance:`, `guardrail:`, `observability:`,
47
+ * `evaluation:`) read as the flat keys they hold (frontmatter-fields.ts
48
+ * JOB_FIELDS). Only a key some config kind lets that block hold is lifted;
49
+ * anything else stays in the block for check to report. A key set both ways
50
+ * keeps its top-level value and is recorded. Nothing parsed is changed: new
51
+ * objects are built (YAML aliases share nodes — deleting from one would
52
+ * delete from the other).
53
+ */
54
+ function foldJobs(data: Record<string, unknown>): Record<string, unknown> {
55
+ let fold: JobFold | null = null;
56
+ let out: Record<string, unknown> | null = null;
57
+ for (const { name } of JOBS) {
58
+ const block = data[name];
59
+ if (!block || typeof block !== "object" || Array.isArray(block) || block instanceof Date) continue;
60
+ const inner = block as Record<string, unknown>;
61
+ const lift = Object.keys(inner).filter((k) => FOLDABLE[name].has(k));
62
+ if (!lift.length) continue;
63
+ out ??= { ...data };
64
+ fold ??= { folded: new Map(), conflicts: new Map() };
65
+ const rest: Record<string, unknown> = {};
66
+ for (const [k, v] of Object.entries(inner)) if (!lift.includes(k)) rest[k] = v;
67
+ for (const k of lift) {
68
+ if (Object.prototype.hasOwnProperty.call(data, k)) fold.conflicts.set(k, name);
69
+ else {
70
+ out[k] = inner[k];
71
+ fold.folded.set(k, name);
72
+ }
73
+ }
74
+ if (Object.keys(rest).length) out[name] = rest;
75
+ else delete out[name];
76
+ }
77
+ if (!out || !fold) return data;
78
+ Object.defineProperty(out, FOLD, { value: fold, enumerable: false });
79
+ return out;
80
+ }
81
+
27
82
  function matter(input: string, options?: Options) {
28
83
  return grayMatter(input, options ?? {});
29
84
  }
30
85
  matter.stringify = grayMatter.stringify;
31
86
  matter.test = grayMatter.test;
32
87
 
88
+ /**
89
+ * matter(), for the three files that hold configuration — an agent, a flow,
90
+ * AGENTS.md — with the four job blocks read as flat keys. Every reader of
91
+ * those files goes through this (tests/jobs.test.ts holds them to it);
92
+ * knowledge, memory, skills and every other document keep matter(), so a
93
+ * note's own `governance:` is its own (Codex debate, 21 Oct 2026).
94
+ */
95
+ export function configMatter(input: string, options?: Options) {
96
+ const file = grayMatter(input, options ?? {});
97
+ if (file.data && typeof file.data === "object" && !Array.isArray(file.data)) {
98
+ (file as { data: Record<string, unknown> }).data = foldJobs(file.data as Record<string, unknown>);
99
+ }
100
+ return file;
101
+ }
102
+
33
103
  export default matter;
104
+
105
+ /** The first line of a YAML parse error, with where it is: what a person
106
+ * needs to fix the file, without the parser's excerpt. */
107
+ export function frontmatterErrorText(err: unknown): string {
108
+ const msg = err instanceof Error ? err.message : String(err);
109
+ return `frontmatter does not parse — ${msg.split("\n")[0].trim().replace(/:$/, "")}. Quote a value that contains a colon followed by a space.`;
110
+ }
111
+
112
+ /** The 1-based file line a YAML parse error points at, when it says. The
113
+ * text gray-matter hands the parser starts with the newline after the
114
+ * opening `---`, so the parser's 0-based line is the file's 1-based one. */
115
+ export function frontmatterErrorLine(err: unknown): number | undefined {
116
+ const line = (err as { mark?: { line?: unknown } } | null)?.mark?.line;
117
+ return typeof line === "number" ? line + 1 : undefined;
118
+ }
119
+
120
+ /** Frontmatter as the runtime reads a skill: strictly first, then once more
121
+ * with every unquoted value that holds a colon quoted (the commonest way a
122
+ * hand-written SKILL.md breaks, and one other skills clients forgive), and
123
+ * empty when even that fails. `lenient` says the second read was needed. */
124
+ export function parseLenient(raw: string): { data: Record<string, unknown>; lenient: boolean; failed: boolean } {
125
+ try {
126
+ return { data: matter(raw).data as Record<string, unknown>, lenient: false, failed: false };
127
+ } catch {
128
+ const fm = raw.match(/^\uFEFF?---\r?\n([\s\S]*?)\r?\n---/);
129
+ if (fm) {
130
+ const fixed = fm[1].replace(
131
+ /^([A-Za-z0-9_-]+):[ \t]+(?!["'>|])(.*:.*)$/gm,
132
+ (_m, k: string, v: string) => `${k}: ${JSON.stringify(v)}`,
133
+ );
134
+ try {
135
+ // A function replacer, not a string: a fixed block containing `$&` or
136
+ // `$$` (a value with those in it) would otherwise be treated as a
137
+ // replacement pattern and splice the whole frontmatter into itself.
138
+ return { data: matter(raw.replace(fm[1], () => fixed)).data as Record<string, unknown>, lenient: true, failed: false };
139
+ } catch {
140
+ /* fall through */
141
+ }
142
+ }
143
+ return { data: {}, lenient: false, failed: true };
144
+ }
145
+ }
package/src/network.ts CHANGED
@@ -28,6 +28,8 @@
28
28
  // proxy (foldrun-platform egress-proxy.ts) holds the rule per request.
29
29
 
30
30
  /** How wide a step's network is. */
31
+ import { keySpan, dropEmptyBlock } from "./frontmatter-text.ts";
32
+
31
33
  export type NetworkLevel = "open" | "trusted" | "none";
32
34
 
33
35
  export const NETWORK_LEVELS: readonly NetworkLevel[] = ["open", "trusted", "none"];
@@ -402,13 +404,15 @@ export function setNetworkSetting(raw: string, setting: { level?: NetworkLevel;
402
404
  if (lines[0]?.trim() !== "---") return line ? ["---", line, "---", ...lines].join(eol) : raw;
403
405
  const close = lines.findIndex((l, i) => i > 0 && l.trim() === "---");
404
406
  if (close < 0) return raw;
405
- const at = lines.findIndex((l, i) => i > 0 && i < close && /^network\s*:/.test(l));
406
- if (at < 0) {
407
+ // Where network: is written — at the top or inside `guardrail:` — is
408
+ // where it is set (frontmatter-text.ts); written nowhere, it goes last.
409
+ const where = keySpan(lines.slice(1, close), "network");
410
+ if (!where) {
407
411
  if (line) lines.splice(close, 0, line);
408
412
  return lines.join(eol);
409
413
  }
410
- let end = at + 1;
411
- while (end < close && (/^\s+\S/.test(lines[end]) || /^\s*-\s/.test(lines[end]))) end++;
412
- lines.splice(at, end - at, ...(line ? [line] : []));
413
- return lines.join(eol);
414
+ const front = lines.slice(1, close);
415
+ front.splice(where.at, where.end - where.at, ...(line ? [`${where.indent}${line}`] : []));
416
+ const edited = !line && where.block !== null ? dropEmptyBlock(front, where.block) : front;
417
+ return [lines[0], ...edited, ...lines.slice(close)].join(eol);
414
418
  }