From 30c43defd6325d64332acadeae1dac92ad0d0440 Mon Sep 17 00:00:00 2001 From: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Date: Mon, 25 May 2026 21:34:59 +0000 Subject: [PATCH] fix(agents): strip markdown code spans from IDENTITY.md values and labels --- src/agents/identity-file.test.ts | 8 ++++++++ src/agents/identity-file.ts | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/agents/identity-file.test.ts b/src/agents/identity-file.test.ts index be8437c7349..eb63370b57f 100644 --- a/src/agents/identity-file.test.ts +++ b/src/agents/identity-file.test.ts @@ -99,4 +99,12 @@ Fluent in over six million error messages. expect(merged).toContain("- Name: New Name"); expect(merged).toContain("- Emoji: 🦀"); }); + + it("updates code-span-wrapped writable labels instead of inserting duplicates", () => { + const merged = mergeIdentityMarkdownContent("- **`Name`**: Old Name\n", { + name: "New Name", + }); + + expect(merged).toBe("- Name: New Name\n"); + }); }); diff --git a/src/agents/identity-file.ts b/src/agents/identity-file.ts index 3915881d5f9..bafd2022411 100644 --- a/src/agents/identity-file.ts +++ b/src/agents/identity-file.ts @@ -39,6 +39,10 @@ function normalizeIdentityValue(value: string): string { return normalizeLowercaseStringOrEmpty(normalized.replace(/\s+/g, " ")); } +function normalizeIdentityLabel(label: string): string { + return normalizeLowercaseStringOrEmpty(label.replace(/[*_`]/g, "")); +} + function isIdentityPlaceholder(value: string): boolean { const normalized = normalizeIdentityValue(value); return IDENTITY_PLACEHOLDER_VALUES.has(normalized); @@ -53,9 +57,7 @@ export function parseIdentityMarkdown(content: string): AgentIdentityFile { if (colonIndex === -1) { continue; } - const label = normalizeLowercaseStringOrEmpty( - cleaned.slice(0, colonIndex).replace(/[*_`]/g, ""), - ); + const label = normalizeIdentityLabel(cleaned.slice(0, colonIndex)); const value = cleaned .slice(colonIndex + 1) .replace(/^[*_`\s]+|[*_`\s]+$/g, "") @@ -104,8 +106,16 @@ function buildIdentityLine(label: string, value: string): string { } function matchesIdentityLabel(line: string, label: string): boolean { - const escaped = label.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); - return new RegExp(`^\\s*-\\s*(?:\\*\\*)?${escaped}(?:\\*\\*)?\\s*:`, "i").test(line.trim()); + const trimmed = line.trim(); + if (!trimmed.startsWith("-")) { + return false; + } + const cleaned = trimmed.replace(/^\s*-\s*/, ""); + const colonIndex = cleaned.indexOf(":"); + if (colonIndex === -1) { + return false; + } + return normalizeIdentityLabel(cleaned.slice(0, colonIndex)) === normalizeIdentityLabel(label); } function normalizeIdentityContent(content: string | undefined): string[] { @@ -123,9 +133,7 @@ function resolveIdentityInsertIndex(lines: string[]): number { if (colonIndex === -1) { continue; } - const label = normalizeLowercaseStringOrEmpty( - cleaned.slice(0, colonIndex).replace(/[*_]/g, ""), - ); + const label = normalizeIdentityLabel(cleaned.slice(0, colonIndex)); if (RICH_IDENTITY_LABELS.has(label)) { lastIdentityIndex = index; }