Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(subagents): read frontmatter from files saved with a UTF-8 BOM

Abhiram-745a49355872cd0
diff --git a/packages/agent-runtime/src/prompt-templates.test.ts b/packages/agent-runtime/src/prompt-templates.test.ts
index a14c917b2..2007963fe 100644
--- a/packages/agent-runtime/src/prompt-templates.test.ts
+++ b/packages/agent-runtime/src/prompt-templates.test.ts
@@ -133,6 +133,19 @@ describe("loadComposerTemplates", () => {
);
});
+ it("reads frontmatter from a file saved with a UTF-8 byte-order mark", async () => {
+ const bomDir = join(root, "bom-prompts");
+ await mkdir(bomDir, { recursive: true });
+ await writeFile(
+ join(bomDir, "summarize.md"),
+ "\uFEFF---\ndescription: Summarize a file\n---\nSummarize $1.",
+ );
+ const { templates } = await loadComposerTemplates(root, { user: bomDir });
+ expect(templates).toHaveLength(1);
+ expect(templates[0]!.description).toBe("Summarize a file");
+ expect(templates[0]!.content).toBe("Summarize $1.");
+ });
+
it("degrades to an empty list when directories are missing", async () => {
const { templates, diagnostics } = await loadComposerTemplates(root, {
project: join(root, "absent-a"),
diff --git a/packages/agent-runtime/src/prompt-templates.ts b/packages/agent-runtime/src/prompt-templates.ts
index 7bad1b54e..a35066c8c 100644
--- a/packages/agent-runtime/src/prompt-templates.ts
+++ b/packages/agent-runtime/src/prompt-templates.ts
@@ -78,7 +78,10 @@ function isRecord(value: unknown): value is Record<string, unknown> {
}
function parsePrompt(raw: string): ParsedPrompt {
- const normalized = raw.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
+ const normalized = raw
+ .replace(/^\uFEFF/, "")
+ .replace(/\r\n/g, "\n")
+ .replace(/\r/g, "\n");
if (!normalized.startsWith("---")) {
return { frontmatter: {}, body: normalized };
}
diff --git a/packages/shared/src/subagent-definition.test.ts b/packages/shared/src/subagent-definition.test.ts
index 5162bfb5c..76be42fd1 100644
--- a/packages/shared/src/subagent-definition.test.ts
+++ b/packages/shared/src/subagent-definition.test.ts
@@ -35,6 +35,18 @@ function definition(
}
describe("parseSubagentDefinition", () => {
+ it("reads frontmatter from a file saved with a UTF-8 byte-order mark", () => {
+ const result = parse(
+ "\uFEFF---\nname: code-reviewer\ndescription: Reviews a diff.\ntools: [Read, Grep]\n---\nReview it.",
+ );
+ expect(result.ok).toBe(true);
+ if (!result.ok) return;
+ expect(result.definition.name).toBe("code-reviewer");
+ expect(result.definition.description).toBe("Reviews a diff.");
+ expect(result.definition.tools).toEqual(["Read", "Grep"]);
+ expect(result.definition.prompt).toBe("Review it.");
+ });
+
it("reads the document a project would actually write", () => {
const result = parse(`---
name: code-reviewer
diff --git a/packages/shared/src/subagent-definition.ts b/packages/shared/src/subagent-definition.ts
index 795eb2207..f0937c51b 100644
--- a/packages/shared/src/subagent-definition.ts
+++ b/packages/shared/src/subagent-definition.ts
@@ -303,7 +303,10 @@ function unquote(value: string): string {
function splitFrontmatter(
raw: string,
): { frontmatter: Frontmatter; body: string } {
- const normalized = raw.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
+ const normalized = raw
+ .replace(/^\uFEFF/, "")
+ .replace(/\r\n/g, "\n")
+ .replace(/\r/g, "\n");
const frontmatter: Frontmatter = new Map();
if (!normalized.startsWith("---\n")) {
return { frontmatter, body: normalized.trim() };