mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 21:41:36 +00:00
feat: inject post-compaction workspace context as system event (#18023)
This commit is contained in:
99
src/auto-reply/reply/post-compaction-context.test.ts
Normal file
99
src/auto-reply/reply/post-compaction-context.test.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { readPostCompactionContext } from "./post-compaction-context.js";
|
||||
|
||||
describe("readPostCompactionContext", () => {
|
||||
const tmpDir = path.join("/tmp", "test-post-compaction-" + Date.now());
|
||||
|
||||
beforeEach(() => {
|
||||
fs.mkdirSync(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("returns null when no AGENTS.md exists", async () => {
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when AGENTS.md has no relevant sections", async () => {
|
||||
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), "# My Agent\n\nSome content.\n");
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("extracts Session Startup section", async () => {
|
||||
const content = `# Agent Rules
|
||||
|
||||
## Session Startup
|
||||
|
||||
Read these files:
|
||||
1. WORKFLOW_AUTO.md
|
||||
2. memory/today.md
|
||||
|
||||
## Other Section
|
||||
|
||||
Not relevant.
|
||||
`;
|
||||
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content);
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toContain("Session Startup");
|
||||
expect(result).toContain("WORKFLOW_AUTO.md");
|
||||
expect(result).toContain("Post-compaction context refresh");
|
||||
expect(result).not.toContain("Other Section");
|
||||
});
|
||||
|
||||
it("extracts Red Lines section", async () => {
|
||||
const content = `# Rules
|
||||
|
||||
## Red Lines
|
||||
|
||||
Never do X.
|
||||
Never do Y.
|
||||
|
||||
## Other
|
||||
|
||||
Stuff.
|
||||
`;
|
||||
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content);
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toContain("Red Lines");
|
||||
expect(result).toContain("Never do X");
|
||||
});
|
||||
|
||||
it("extracts both sections", async () => {
|
||||
const content = `# Rules
|
||||
|
||||
## Session Startup
|
||||
|
||||
Do startup things.
|
||||
|
||||
## Red Lines
|
||||
|
||||
Never break things.
|
||||
|
||||
## Other
|
||||
|
||||
Ignore this.
|
||||
`;
|
||||
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), content);
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toContain("Session Startup");
|
||||
expect(result).toContain("Red Lines");
|
||||
expect(result).not.toContain("Other");
|
||||
});
|
||||
|
||||
it("truncates when content exceeds limit", async () => {
|
||||
const longContent = "## Session Startup\n\n" + "A".repeat(4000) + "\n\n## Other\n\nStuff.";
|
||||
fs.writeFileSync(path.join(tmpDir, "AGENTS.md"), longContent);
|
||||
const result = await readPostCompactionContext(tmpDir);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toContain("[truncated]");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user