Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/policy-review-fence-2507.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
19 changes: 17 additions & 2 deletions packages/ui-server/src/brain/policy-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, unknown> = {};
Expand Down
20 changes: 20 additions & 0 deletions packages/ui-server/tests/policy-proposal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions scripts/check-frontmatter-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
3 changes: 3 additions & 0 deletions tests/frontmatter-parse-gate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading