Skip to content

Commit de06632

Browse files
committed
fix: handle duplicate YAML keys in frontmatter and improve compaction prompt (anomalyco#12254, anomalyco#12191)
1 parent d23439d commit de06632

3 files changed

Lines changed: 160 additions & 24 deletions

File tree

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
You are a helpful AI assistant tasked with summarizing conversations.
1+
You are a conversation summarizer. Your ONLY job is to accurately summarize what actually happened in the conversation above.
22

3-
When asked to summarize, provide a detailed but concise summary of the conversation.
4-
Focus on information that would be helpful for continuing the conversation, including:
5-
- What was done
6-
- What is currently being worked on
7-
- Which files are being modified
8-
- What needs to be done next
3+
CRITICAL RULES:
4+
- ONLY include information that is explicitly present in the conversation
5+
- NEVER invent, assume, or infer information not directly stated
6+
- NEVER add topics, files, or actions that weren't discussed
7+
- If the conversation is unclear, say so rather than guessing
8+
9+
Focus your summary on:
10+
- What tasks were completed and their outcomes
11+
- What is currently being worked on (if anything is in progress)
12+
- Which specific files were read, created, or modified
13+
- What needs to be done next (only if explicitly stated)
914
- Key user requests, constraints, or preferences that should persist
10-
- Important technical decisions and why they were made
15+
- Important technical decisions and their stated rationale
1116

12-
Your summary should be comprehensive enough to provide context but concise enough to be quickly understood.
17+
Your summary should be comprehensive enough to provide context for continuing the conversation, but concise enough to be quickly understood. Use bullet points for clarity.

‎packages/opencode/src/config/markdown.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export namespace ConfigMarkdown {
2323
const frontmatter = match[1]
2424
const lines = frontmatter.split("\n")
2525
const result: string[] = []
26+
const seen = new Set<string>()
2627

2728
for (const line of lines) {
2829
// skip comments and empty lines
@@ -45,6 +46,11 @@ export namespace ConfigMarkdown {
4546
}
4647

4748
const key = kvMatch[1]
49+
50+
// skip duplicate top-level keys
51+
if (seen.has(key)) continue
52+
seen.add(key)
53+
4854
const value = kvMatch[2].trim()
4955

5056
// skip if value is empty, already quoted, or uses block scalar
@@ -69,22 +75,16 @@ export namespace ConfigMarkdown {
6975

7076
export async function parse(filePath: string) {
7177
const template = await Bun.file(filePath).text()
72-
7378
try {
74-
const md = matter(template)
75-
return md
76-
} catch {
77-
try {
78-
return matter(fallbackSanitization(template))
79-
} catch (err) {
80-
throw new FrontmatterError(
81-
{
82-
path: filePath,
83-
message: `${filePath}: Failed to parse YAML frontmatter: ${err instanceof Error ? err.message : String(err)}`,
84-
},
85-
{ cause: err },
86-
)
87-
}
79+
return matter(fallbackSanitization(template))
80+
} catch (err) {
81+
throw new FrontmatterError(
82+
{
83+
path: filePath,
84+
message: `${filePath}: Failed to parse YAML frontmatter: ${err instanceof Error ? err.message : String(err)}`,
85+
},
86+
{ cause: err },
87+
)
8888
}
8989
}
9090

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { expect, test, describe } from "bun:test"
2+
import { ConfigMarkdown } from "../../src/config/markdown"
3+
import path from "path"
4+
import os from "os"
5+
6+
describe("ConfigMarkdown: duplicate key deduplication", () => {
7+
test("should keep first occurrence of duplicate top-level keys", () => {
8+
const content = [
9+
"---",
10+
"name: first",
11+
"mode: subagent",
12+
"name: second",
13+
"---",
14+
"",
15+
"Body content",
16+
].join("\n")
17+
18+
const sanitized = ConfigMarkdown.fallbackSanitization(content)
19+
expect(sanitized).toContain("name: first")
20+
expect(sanitized).not.toContain("name: second")
21+
})
22+
23+
test("should deduplicate multiple duplicate keys", () => {
24+
const content = [
25+
"---",
26+
"pwd: allow",
27+
"mode: subagent",
28+
"pwd: deny",
29+
"description: test",
30+
"mode: all",
31+
"---",
32+
"",
33+
"Body",
34+
].join("\n")
35+
36+
const sanitized = ConfigMarkdown.fallbackSanitization(content)
37+
expect(sanitized).toContain("pwd: allow")
38+
expect(sanitized).not.toContain("pwd: deny")
39+
expect(sanitized).toContain("mode: subagent")
40+
expect(sanitized).not.toContain("mode: all")
41+
expect(sanitized).toContain("description: test")
42+
})
43+
44+
test("should preserve nested/indented duplicate keys", () => {
45+
const content = [
46+
"---",
47+
"tools:",
48+
" write: true",
49+
" read: true",
50+
" write: false",
51+
"mode: subagent",
52+
"---",
53+
"",
54+
"Body",
55+
].join("\n")
56+
57+
const sanitized = ConfigMarkdown.fallbackSanitization(content)
58+
// indented lines are preserved as-is (they're nested keys)
59+
const indented = sanitized.match(/ write: (true|false)/g)
60+
expect(indented).toHaveLength(2)
61+
})
62+
63+
test("should not affect content without duplicates", () => {
64+
const content = [
65+
"---",
66+
"name: test",
67+
"mode: subagent",
68+
"description: a description",
69+
"---",
70+
"",
71+
"Body content",
72+
].join("\n")
73+
74+
const sanitized = ConfigMarkdown.fallbackSanitization(content)
75+
expect(sanitized).toContain("name: test")
76+
expect(sanitized).toContain("mode: subagent")
77+
expect(sanitized).toContain("description: a description")
78+
expect(sanitized).toContain("Body content")
79+
})
80+
81+
test("should return content unchanged when no frontmatter", () => {
82+
const content = "Just some text without frontmatter"
83+
const sanitized = ConfigMarkdown.fallbackSanitization(content)
84+
expect(sanitized).toBe(content)
85+
})
86+
})
87+
88+
describe("ConfigMarkdown: parse with duplicate keys", () => {
89+
test("should parse file with duplicate keys keeping first value", async () => {
90+
const dir = path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2))
91+
const file = path.join(dir, "duplicate-keys.md")
92+
await Bun.write(
93+
file,
94+
[
95+
"---",
96+
"mode: subagent",
97+
"description: test agent",
98+
"mode: all",
99+
"---",
100+
"",
101+
"Agent instructions here",
102+
].join("\n"),
103+
)
104+
105+
const result = await ConfigMarkdown.parse(file)
106+
expect(result.data.mode).toBe("subagent")
107+
expect(result.data.description).toBe("test agent")
108+
expect(result.content).toContain("Agent instructions here")
109+
})
110+
111+
test("should parse file with no duplicates normally", async () => {
112+
const dir = path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2))
113+
const file = path.join(dir, "normal.md")
114+
await Bun.write(
115+
file,
116+
[
117+
"---",
118+
"mode: subagent",
119+
"description: normal agent",
120+
"---",
121+
"",
122+
"Normal content",
123+
].join("\n"),
124+
)
125+
126+
const result = await ConfigMarkdown.parse(file)
127+
expect(result.data.mode).toBe("subagent")
128+
expect(result.data.description).toBe("normal agent")
129+
expect(result.content).toContain("Normal content")
130+
})
131+
})

0 commit comments

Comments
 (0)