From 4e359a5229a4abf481c2ec4cc78bda81ab22fa0d Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Fri, 6 Nov 2026 08:33:52 +0100 Subject: [PATCH] fix(ui-server): split the policy review where the parser ends frontmatter (#2507) (#3649) The policy proposal review found its fences with its own regex, which accepted a `...` closer and only a whole `---` line as either fence. It now asks frontmatterBlock, so the review's frontmatter diff shows the block the validator enforces. The closing line's trailing whitespace and line ending stay frontmatter. The file leaves the lint gate's fence allowlist. Co-authored-by: Claude Opus 5.5 --- .changeset/policy-review-fence-2507.md | 5 +++++ docs/decisions/governed-writable-workers.md | 4 ++-- .../ui-server/src/brain/policy-proposal.ts | 19 ++++++++++++++++-- .../ui-server/tests/policy-proposal.test.ts | 20 +++++++++++++++++++ scripts/check-frontmatter-parse.ts | 4 ---- tests/frontmatter-parse-gate.test.ts | 3 +++ 6 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 .changeset/policy-review-fence-2507.md diff --git a/.changeset/policy-review-fence-2507.md b/.changeset/policy-review-fence-2507.md new file mode 100644 index 0000000000..f931fff4d0 --- /dev/null +++ b/.changeset/policy-review-fence-2507.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-server": patch +--- + +A policy proposal's review now splits frontmatter from body where the policy parser ends the frontmatter (#2507). Before, the review found the fences with its own pattern, which could disagree with what is enforced. A block with no closing fence, or opened by a `---` line carrying more text, was shown entirely as body even though its rules apply. A `---x` line was skipped as a closer, so the lines after it were shown as frontmatter although the parser ignores them. A `...` line ended the frontmatter, though the parser does not end it there. The approval card's frontmatter diff and body diff now show exactly the enforced block. The closing line with its trailing whitespace and line ending still counts as frontmatter. Proposals that were accepted before are still accepted. diff --git a/docs/decisions/governed-writable-workers.md b/docs/decisions/governed-writable-workers.md index 79230c2bd9..bfaacd76c6 100644 --- a/docs/decisions/governed-writable-workers.md +++ b/docs/decisions/governed-writable-workers.md @@ -1194,7 +1194,7 @@ replacement and the deadline publish nothing and record no policy-updated event; a no-publication attempt records none either. Real observation and storage failures keep their existing handling, and a publication that committed before its report failed stays `unknown` and is never retried -(`const held`, `packages/ui-server/src/brain/policy-proposal.ts:167-256`). +(`const held`, `packages/ui-server/src/brain/policy-proposal.ts:150-239`). Every retained turn has one initially collapsed receipt. Known no-change is zero; legacy/unknown detail and unknown cost/limits remain explicitly unknown. @@ -1210,7 +1210,7 @@ principal authority remain the caller's responsibility. The policy publisher creates, writes, fsyncs and removes its operation-owned staging name entirely inside that synchronous callback, between the two serialized observations (`await options.observeUserPolicy`, -`packages/ui-server/src/brain/policy-proposal.ts:219-253`). First publication +`packages/ui-server/src/brain/policy-proposal.ts:202-236`). First publication also removes the temporary hardlink before the final observation. No filename prefix is ignored; a similarly named real agent change is still retained. Failed staging preserves the original policy, while failed recording after diff --git a/packages/ui-server/src/brain/policy-proposal.ts b/packages/ui-server/src/brain/policy-proposal.ts index dbf52281ed..35b18ef2d7 100644 --- a/packages/ui-server/src/brain/policy-proposal.ts +++ b/packages/ui-server/src/brain/policy-proposal.ts @@ -4,6 +4,7 @@ import { createHash } from "node:crypto"; import type { SessionHistoryMessage } from "@schlessera/brain-ui-sdk/protocol"; import type { Database } from "bun:sqlite"; import { parseFrontmatter } from "@schlessera/brain-common/internal/frontmatter"; +import { frontmatterBlock } from "@schlessera/brain-common/internal/frontmatter-block"; import { parseGovernancePolicy, GOVERNANCE_POLICY_PATH, GovernancePolicyReadError, openGovernancePolicyAnchored, verifyGovernancePolicyAnchored, type AnchoredGovernancePolicy, type GovernancePolicyReadRefusal } from "@schlessera/brain-common/internal/governance-policy"; import { BRAIN_LOCK_KEY, type KeyedLock } from "@schlessera/brain-ui-sdk/server"; @@ -42,9 +43,23 @@ function diff(before: string, after: string) { if (before === after) return ""; return before.split("\n").map(line => `- ${line}`).concat(after.split("\n").map(line => `+ ${line}`)).join("\n"); } +/** + * The review's frontmatter/body split ends the frontmatter where + * `parseFrontmatter`, and so the policy validator, ends it + * (`frontmatterBlock`, #2507). The approver then sees every enforced line as + * frontmatter and no unenforced line as frontmatter: an unclosed block is + * frontmatter to the end of the file, and a closing `---x` ends it after the + * dashes. A `...` line does not close it, because the parser does not close + * there. One rule is kept from the old splitter: when only whitespace follows + * the closing dashes, the whole closing line and its line ending are + * frontmatter rather than the start of the body. + */ function split(text: string) { - const match = /^(?:\uFEFF)?---[ \t]*\r?\n([\s\S]*?)\r?\n(?:---|\.\.\.)[ \t]*(?:\r?\n|$)/.exec(text); - return match ? { frontmatter: match[0], body: text.slice(match[0].length) } : { frontmatter: "", body: text }; + const block = frontmatterBlock(text); + if (!block) return { frontmatter: "", body: text }; + const rest = block.closed ? /^[ \t]*(?:\r?\n|$)/.exec(text.slice(block.innerEnd + 3)) : null; + const end = rest ? block.innerEnd + 3 + rest[0].length : block.end; + return { frontmatter: text.slice(0, end), body: text.slice(end) }; } function sections(before: string, after: string) { let a: Record = {}; diff --git a/packages/ui-server/tests/policy-proposal.test.ts b/packages/ui-server/tests/policy-proposal.test.ts index a5cc185eca..2b02b3aa6f 100644 --- a/packages/ui-server/tests/policy-proposal.test.ts +++ b/packages/ui-server/tests/policy-proposal.test.ts @@ -41,6 +41,26 @@ test("approval publishes exact bytes under the shared brain lock and the next ad expect(admitted.policy.profiles.interactive).toBe("governed"); expect(result.sections).toEqual(["profiles.interactive"]); expect(result.bodyChanged).toBe(true); }); +test("the review splits frontmatter where the policy parser ends it (#2507)", async () => { + // Unclosed: the parser reads every line as YAML, so the rules are frontmatter, not body. + const unclosed = "---\nschema_version: 1\nprofiles:\n interactive: strict\n"; + const f = fixture(), pending = f.service.propose(f.turn, "unclosed", { ...input, content: unclosed }); await f.service.settled(); + const review = f.service.read("odysseus", "policy-turn", "unclosed")!; + expect(review.frontmatterDiff).toContain("+ interactive: strict"); expect(review.bodyDiff).not.toContain("interactive"); + await f.answer(false); await pending; + // `---x` closes the block for the parser: the line after it is body, not frontmatter. + const dashed = "---\nschema_version: 1\nprofiles:\n interactive: governed\n---x\nlater: 1\n---\nOdysseus reviews the policy.\n"; + const g = fixture(), second = g.service.propose(g.turn, "dashed", { ...input, content: dashed }); await g.service.settled(); + const split = g.service.read("odysseus", "policy-turn", "dashed")!; + expect(split.frontmatterDiff).not.toContain("later"); expect(split.frontmatterDiff.endsWith("+ ---")).toBe(true); expect(split.bodyDiff).toContain("+ later: 1"); + await g.answer(false); await second; + // Whitespace after the closing dashes: the whole closing line stays frontmatter. + const spaced = proposed.replace("strict\n---\n", "strict\n--- \n"); + const h = fixture(), third = h.service.propose(h.turn, "spaced", { ...input, content: spaced }); await h.service.settled(); + const line = h.service.read("odysseus", "policy-turn", "spaced")!; + expect(line.frontmatterDiff).toContain("+ --- \n+ "); expect(line.bodyDiff).toBe("- Odysseus reviews the policy.\n- \n+ Odysseus approves the policy.\n+ "); + await h.answer(false); await third; +}); test("decline preserves bytes and retains the same request receipt", async () => { const f = fixture(), pending = f.service.propose(f.turn, "policy-1", input); await f.answer(false); expect((await pending).state).toBe("declined"); expect(readFileSync(f.path, "utf8")).toBe(original); expect(f.edits).toHaveLength(0); diff --git a/scripts/check-frontmatter-parse.ts b/scripts/check-frontmatter-parse.ts index 5ff903014c..b8ce26452d 100644 --- a/scripts/check-frontmatter-parse.ts +++ b/scripts/check-frontmatter-parse.ts @@ -84,10 +84,6 @@ export const FENCE_ALLOWED: ReadonlyArray<{ pattern: RegExp; reason: string }> = pattern: /^packages\/core\/src\/lib\/frontmatter\.ts$/, reason: "stringifyDocument post-processes the LF-only block gray-matter's stringify just emitted", }, - { - pattern: /^packages\/ui-server\/src\/brain\/policy-proposal\.ts$/, - reason: "pending: its review splitter still accepts a `...` closer; moving it onto frontmatterBlock is open work on #2507", - }, ]; /** Package source, where rule 3 applies. */ diff --git a/tests/frontmatter-parse-gate.test.ts b/tests/frontmatter-parse-gate.test.ts index 086b3445df..6e27895c7c 100644 --- a/tests/frontmatter-parse-gate.test.ts +++ b/tests/frontmatter-parse-gate.test.ts @@ -163,6 +163,9 @@ describe("rule 3: fences are found by frontmatterBlock (#2507)", () => { expect({ file, rules: rules(file, planted) }).toEqual({ file, rules: [3] }); } for (const entry of FENCE_ALLOWED) expect(entry.reason.length).toBeGreaterThan(20); + // The policy review splitter left the list once it moved onto frontmatterBlock (#2507). + const splitter = "const match = /^(?:\\uFEFF)?---[ \\t]*\\r?\\n([\\s\\S]*?)\\r?\\n(?:---|\\.\\.\\.)[ \\t]*(?:\\r?\\n|$)/.exec(text);"; + expect(rules("packages/ui-server/src/brain/policy-proposal.ts", splitter)).toEqual([3]); }); test("the tree passes, and the CLI fails on a planted detector", () => {