




















@@ -2370,14 +2370,93 @@ async function expectWorkspaceSummaryEmptyForAgentsAlias(
23702370const outside = path.join(root, "outside-secret.txt");
23712371fs.writeFileSync(outside, "secret");
23722372createAlias(outside, path.join(root, "AGENTS.md"));
2373-await expect(readWorkspaceContextForSummary()).resolves.toBe("");
2373+await expect(readWorkspaceContextForSummary(["Session Startup", "Red Lines"])).resolves.toBe(
2374+"",
2375+);
23742376} finally {
23752377cwdSpy.mockRestore();
23762378fs.rmSync(root, { recursive: true, force: true });
23772379}
23782380}
2379238123802382describe("readWorkspaceContextForSummary", () => {
2383+async function withWorkspaceSummary(
2384+content: string,
2385+sectionNames: string[] | undefined,
2386+): Promise<string> {
2387+const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-compaction-summary-"));
2388+const cwdSpy = vi.spyOn(process, "cwd").mockReturnValue(root);
2389+try {
2390+fs.writeFileSync(path.join(root, "AGENTS.md"), content);
2391+return await readWorkspaceContextForSummary(sectionNames);
2392+} finally {
2393+cwdSpy.mockRestore();
2394+fs.rmSync(root, { recursive: true, force: true });
2395+}
2396+}
2397+2398+it("returns empty when post-compaction sections are not configured", async () => {
2399+const result = await withWorkspaceSummary(
2400+"## Session Startup\n\nRead AGENTS.md\n\n## Red Lines\n\nBe careful.\n",
2401+undefined,
2402+);
2403+2404+expect(result).toBe("");
2405+});
2406+2407+it("returns empty when post-compaction sections are explicitly disabled", async () => {
2408+const result = await withWorkspaceSummary("## Session Startup\n\nRead AGENTS.md\n", []);
2409+2410+expect(result).toBe("");
2411+});
2412+2413+it("injects workspace critical rules only for explicit section opt-in", async () => {
2414+const result = await withWorkspaceSummary(
2415+"## Session Startup\n\nRead AGENTS.md\n\n## Other\n\nIgnore me.\n",
2416+["Session Startup", "Red Lines"],
2417+);
2418+2419+expect(result).toContain("<workspace-critical-rules>");
2420+expect(result).toContain("## Session Startup");
2421+expect(result).toContain("Read AGENTS.md");
2422+expect(result).not.toContain("Ignore me");
2423+});
2424+2425+it("reads workspace context from the configured workspace instead of process cwd", async () => {
2426+const processRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-compaction-cwd-"));
2427+const workspaceRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-compaction-workspace-"));
2428+const cwdSpy = vi.spyOn(process, "cwd").mockReturnValue(processRoot);
2429+try {
2430+fs.writeFileSync(
2431+path.join(processRoot, "AGENTS.md"),
2432+"## Session Startup\n\nWrong cwd rules.\n",
2433+);
2434+fs.writeFileSync(
2435+path.join(workspaceRoot, "AGENTS.md"),
2436+"## Session Startup\n\nUse the run workspace rules.\n",
2437+);
2438+2439+const result = await readWorkspaceContextForSummary(["Session Startup"], workspaceRoot);
2440+2441+expect(result).toContain("Use the run workspace rules.");
2442+expect(result).not.toContain("Wrong cwd rules.");
2443+} finally {
2444+cwdSpy.mockRestore();
2445+fs.rmSync(processRoot, { recursive: true, force: true });
2446+fs.rmSync(workspaceRoot, { recursive: true, force: true });
2447+}
2448+});
2449+2450+it("preserves legacy fallback only for the explicit default section pair", async () => {
2451+const result = await withWorkspaceSummary(
2452+"## Every Session\n\nDo startup things.\n\n## Safety\n\nBe safe.\n",
2453+["Red Lines", "Session Startup"],
2454+);
2455+2456+expect(result).toContain("Do startup things");
2457+expect(result).toContain("Be safe");
2458+});
2459+23812460it.runIf(process.platform !== "win32")(
23822461"returns empty when AGENTS.md is a symlink escape",
23832462async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。