Skip to content

Commit 2043866

Browse files
committed
fix(agents): refresh bootstrap snapshot when workspace files change
1 parent 199d5f7 commit 2043866

4 files changed

Lines changed: 101 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai
66

77
### Fixes
88

9+
- Agents/bootstrap: refresh cached workspace bootstrap snapshots on long-lived main-session turns when `AGENTS.md`, `SOUL.md`, `MEMORY.md`, or `TOOLS.md` change on disk, while preserving unchanged snapshot identity through the workspace file cache. (#64871; related #43901, #26497, #28594, #30896) Thanks @aimqwest and @mikejuyoon.
910
- Feishu: extract quoted/replied interactive-card text across schema 1.0, schema 2.0, i18n, template-variable, and post-format fallback shapes without carrying broad generated/config churn from related parser experiments. (#38776, #60383, #42218, #45936) Thanks @lishuaigit, @lskun, @just2gooo, and @Br1an67.
1011
- Exec approvals: accept a symlinked `OPENCLAW_HOME` as the trusted approvals root while still rejecting symlinked `.openclaw` path components below it. (#64663) Thanks @FunJim.
1112
- Logging: add top-level `hostname`, flattened `message`, and available `agent_id`, `session_id`, and `channel` fields to file-log JSONL records for multi-agent filtering without removing existing structured log arguments. Fixes #51075. Thanks @stevengonsalvez.

‎src/agents/bootstrap-cache.test.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,29 @@ describe("getOrLoadBootstrapFiles", () => {
4848
expect(mockLoad()).toHaveBeenCalledTimes(1);
4949
});
5050

51-
it("returns cached result on second call", async () => {
52-
await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "session-1" });
51+
it("refreshes from disk on second call while preserving unchanged object identity", async () => {
52+
const refreshedFiles = [makeFile("AGENTS.md", "# Agent"), makeFile("SOUL.md", "# Soul")];
53+
mockLoad().mockResolvedValueOnce(files).mockResolvedValueOnce(refreshedFiles);
54+
55+
const first = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "session-1" });
5356
const result = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "session-1" });
5457

55-
expect(result).toBe(files);
56-
expect(mockLoad()).toHaveBeenCalledTimes(1);
58+
expect(first).toBe(files);
59+
expect(result).toBe(first);
60+
expect(result).not.toBe(refreshedFiles);
61+
expect(mockLoad()).toHaveBeenCalledTimes(2);
62+
});
63+
64+
it("replaces cached result when workspace bootstrap contents change", async () => {
65+
const updatedFiles = [makeFile("AGENTS.md", "# Agent v2"), makeFile("SOUL.md", "# Soul")];
66+
mockLoad().mockResolvedValueOnce(files).mockResolvedValueOnce(updatedFiles);
67+
68+
const first = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "session-1" });
69+
const result = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "session-1" });
70+
71+
expect(first).toBe(files);
72+
expect(result).toBe(updatedFiles);
73+
expect(mockLoad()).toHaveBeenCalledTimes(2);
5774
});
5875

5976
it("different session keys get independent caches", async () => {
@@ -104,12 +121,13 @@ describe("clearBootstrapSnapshot", () => {
104121

105122
it("does not affect other sessions", async () => {
106123
await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "sk1" });
107-
await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "sk2" });
124+
const first = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "sk2" });
108125

109126
clearBootstrapSnapshot("sk1");
110127

111-
// sk2 should still be cached.
112-
await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "sk2" });
113-
expect(mockLoad()).toHaveBeenCalledTimes(2); // sk1 x1, sk2 x1
128+
// sk2 should still preserve its cached snapshot identity after refresh.
129+
const second = await getOrLoadBootstrapFiles({ workspaceDir: "/ws", sessionKey: "sk2" });
130+
expect(second).toBe(first);
131+
expect(mockLoad()).toHaveBeenCalledTimes(3); // sk1 x1, sk2 x2
114132
});
115133
});

‎src/agents/bootstrap-cache.ts‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
import { loadWorkspaceBootstrapFiles, type WorkspaceBootstrapFile } from "./workspace.js";
22

3-
const cache = new Map<string, WorkspaceBootstrapFile[]>();
3+
type BootstrapSnapshot = {
4+
workspaceDir: string;
5+
files: WorkspaceBootstrapFile[];
6+
};
7+
8+
const cache = new Map<string, BootstrapSnapshot>();
9+
10+
function bootstrapFilesEqual(
11+
previous: WorkspaceBootstrapFile[],
12+
next: WorkspaceBootstrapFile[],
13+
): boolean {
14+
if (previous.length !== next.length) {
15+
return false;
16+
}
17+
18+
return previous.every((file, index) => {
19+
const updated = next[index];
20+
return (
21+
updated !== undefined &&
22+
file.name === updated.name &&
23+
file.path === updated.path &&
24+
file.content === updated.content &&
25+
file.missing === updated.missing
26+
);
27+
});
28+
}
429

530
export async function getOrLoadBootstrapFiles(params: {
631
workspaceDir: string;
732
sessionKey: string;
833
}): Promise<WorkspaceBootstrapFile[]> {
934
const existing = cache.get(params.sessionKey);
10-
if (existing) {
11-
return existing;
35+
// Refresh per turn so long-lived sessions pick up edits; loadWorkspaceBootstrapFiles
36+
// handles unchanged file content through its guarded inode/mtime cache.
37+
const files = await loadWorkspaceBootstrapFiles(params.workspaceDir);
38+
if (
39+
existing &&
40+
existing.workspaceDir === params.workspaceDir &&
41+
bootstrapFilesEqual(existing.files, files)
42+
) {
43+
return existing.files;
1244
}
1345

14-
const files = await loadWorkspaceBootstrapFiles(params.workspaceDir);
15-
cache.set(params.sessionKey, files);
46+
cache.set(params.sessionKey, { workspaceDir: params.workspaceDir, files });
1647
return files;
1748
}
1849

‎src/agents/workspace.bootstrap-cache.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
3-
import { describe, expect, it, beforeEach } from "vitest";
3+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
44
import { makeTempWorkspace, writeWorkspaceFile } from "../test-helpers/workspace.js";
5+
import { clearAllBootstrapSnapshots, getOrLoadBootstrapFiles } from "./bootstrap-cache.js";
56
import { loadWorkspaceBootstrapFiles, DEFAULT_AGENTS_FILENAME } from "./workspace.js";
67

78
describe("workspace bootstrap file caching", () => {
89
let workspaceDir: string;
910

1011
beforeEach(async () => {
12+
clearAllBootstrapSnapshots();
1113
workspaceDir = await makeTempWorkspace("openclaw-bootstrap-cache-test-");
1214
});
1315

16+
afterEach(() => {
17+
clearAllBootstrapSnapshots();
18+
});
19+
1420
const loadAgentsFile = async (dir: string) => {
1521
const result = await loadWorkspaceBootstrapFiles(dir);
1622
return result.find((f) => f.name === DEFAULT_AGENTS_FILENAME);
1723
};
1824

25+
const loadSessionAgentsFile = async (dir: string, sessionKey: string) => {
26+
const result = await getOrLoadBootstrapFiles({ workspaceDir: dir, sessionKey });
27+
return result.find((f) => f.name === DEFAULT_AGENTS_FILENAME);
28+
};
29+
1930
const expectAgentsContent = (
2031
agentsFile: Awaited<ReturnType<typeof loadAgentsFile>>,
2132
content: string,
@@ -74,6 +85,32 @@ describe("workspace bootstrap file caching", () => {
7485
expectAgentsContent(agentsFile2, content2);
7586
});
7687

88+
it("refreshes session bootstrap snapshots after workspace file changes", async () => {
89+
const content1 = "# Initial content";
90+
const content2 = "# Updated content";
91+
const filePath = path.join(workspaceDir, DEFAULT_AGENTS_FILENAME);
92+
93+
await writeWorkspaceFile({
94+
dir: workspaceDir,
95+
name: DEFAULT_AGENTS_FILENAME,
96+
content: content1,
97+
});
98+
99+
const agentsFile1 = await loadSessionAgentsFile(workspaceDir, "agent:main:main");
100+
expectAgentsContent(agentsFile1, content1);
101+
102+
await writeWorkspaceFile({
103+
dir: workspaceDir,
104+
name: DEFAULT_AGENTS_FILENAME,
105+
content: content2,
106+
});
107+
const bumpedTime = new Date(Date.now() + 1_000);
108+
await fs.utimes(filePath, bumpedTime, bumpedTime);
109+
110+
const agentsFile2 = await loadSessionAgentsFile(workspaceDir, "agent:main:main");
111+
expectAgentsContent(agentsFile2, content2);
112+
});
113+
77114
it("invalidates cache when inode changes with same mtime", async () => {
78115
if (process.platform === "win32") {
79116
return;

0 commit comments

Comments
 (0)