fix: align AGENTS.md template section names with post-compaction extraction (#25029) (#25098)

Merged via squash.

Prepared head SHA: 8cd6cc8049
Co-authored-by: echoVic <16428813+echoVic@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
This commit is contained in:
青雲
2026-03-05 04:16:00 +08:00
committed by GitHub
parent 4242c5152f
commit 96021a2b17
6 changed files with 72 additions and 7 deletions

View File

@@ -226,4 +226,57 @@ Read WORKFLOW.md on startup.
expect(result).not.toBeNull();
expect(result).toContain("Current time:");
});
it("falls back to legacy section names (Every Session / Safety)", async () => {
const content = `# Rules
## Every Session
Read SOUL.md and USER.md.
## Safety
Don't exfiltrate private data.
## Other
Ignore this.
`;
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content);
const result = await readPostCompactionContext(tmpDir);
expect(result).not.toBeNull();
expect(result).toContain("Every Session");
expect(result).toContain("Read SOUL.md");
expect(result).toContain("Safety");
expect(result).toContain("Don't exfiltrate");
expect(result).not.toContain("Other");
});
it("prefers new section names over legacy when both exist", async () => {
const content = `# Rules
## Session Startup
New startup instructions.
## Every Session
Old startup instructions.
## Red Lines
New red lines.
## Safety
Old safety rules.
`;
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content);
const result = await readPostCompactionContext(tmpDir);
expect(result).not.toBeNull();
expect(result).toContain("New startup instructions");
expect(result).toContain("New red lines");
expect(result).not.toContain("Old startup instructions");
expect(result).not.toContain("Old safety rules");
});
});

View File

@@ -53,9 +53,14 @@ export async function readPostCompactionContext(
}
})();
// Extract "## Session Startup" and "## Red Lines" sections
// Extract "## Session Startup" and "## Red Lines" sections.
// Also accept legacy names "Every Session" and "Safety" for backward
// compatibility with older AGENTS.md templates.
// Each section ends at the next "## " heading or end of file
const sections = extractSections(content, ["Session Startup", "Red Lines"]);
let sections = extractSections(content, ["Session Startup", "Red Lines"]);
if (sections.length === 0) {
sections = extractSections(content, ["Every Session", "Safety"]);
}
if (sections.length === 0) {
return null;