From d0b33f23eb6a0a74190d399beb5da1cdc8b20baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=B7=E7=86=99?= Date: Mon, 16 Feb 2026 20:48:24 +0800 Subject: [PATCH] fix: improve section extraction robustness (case-insensitive, H3, code blocks) --- .../reply/post-compaction-context.test.ts | 70 +++++++++++++++++++ .../reply/post-compaction-context.ts | 61 ++++++++++++---- 2 files changed, 116 insertions(+), 15 deletions(-) diff --git a/src/auto-reply/reply/post-compaction-context.test.ts b/src/auto-reply/reply/post-compaction-context.test.ts index 92cbc1df0..003da9deb 100644 --- a/src/auto-reply/reply/post-compaction-context.test.ts +++ b/src/auto-reply/reply/post-compaction-context.test.ts @@ -96,4 +96,74 @@ Ignore this. expect(result).not.toBeNull(); expect(result).toContain("[truncated]"); }); + + it("matches section names case-insensitively", async () => { + const content = `# Rules + +## session startup + +Read WORKFLOW_AUTO.md + +## Other +`; + fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content); + const result = await readPostCompactionContext(tmpDir); + expect(result).not.toBeNull(); + expect(result).toContain("WORKFLOW_AUTO.md"); + }); + + it("matches H3 headings", async () => { + const content = `# Rules + +### Session Startup + +Read these files. + +### Other +`; + fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content); + const result = await readPostCompactionContext(tmpDir); + expect(result).not.toBeNull(); + expect(result).toContain("Read these files"); + }); + + it("skips sections inside code blocks", async () => { + const content = `# Rules + +\`\`\`markdown +## Session Startup +This is inside a code block and should NOT be extracted. +\`\`\` + +## Red Lines + +Real red lines here. + +## Other +`; + fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content); + const result = await readPostCompactionContext(tmpDir); + expect(result).not.toBeNull(); + expect(result).toContain("Real red lines here"); + expect(result).not.toContain("inside a code block"); + }); + + it("includes sub-headings within a section", async () => { + const content = `## Red Lines + +### Rule 1 +Never do X. + +### Rule 2 +Never do Y. + +## Other Section +`; + fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content); + const result = await readPostCompactionContext(tmpDir); + expect(result).not.toBeNull(); + expect(result).toContain("Rule 1"); + expect(result).toContain("Rule 2"); + expect(result).not.toContain("Other Section"); + }); }); diff --git a/src/auto-reply/reply/post-compaction-context.ts b/src/auto-reply/reply/post-compaction-context.ts index ec27a2a04..7811f732f 100644 --- a/src/auto-reply/reply/post-compaction-context.ts +++ b/src/auto-reply/reply/post-compaction-context.ts @@ -44,8 +44,10 @@ export async function readPostCompactionContext(workspaceDir: string): Promise