fix(agents): strip markdown code spans from IDENTITY.md values and labels

This commit is contained in:
clawsweeper
2026-05-25 21:34:59 +00:00
parent 666bccbef6
commit 30c43defd6
2 changed files with 24 additions and 8 deletions

View File

@@ -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");
});
});

View File

@@ -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;
}