Skip to content

Commit 0a0d343

Browse files
steipeteampagent
andauthored
1 parent 426a3d1 commit 0a0d343

4 files changed

Lines changed: 113 additions & 11 deletions

File tree

‎ui/src/e2e/agent-file-lifecycle.e2e.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,74 @@ suite.define(() => {
212212
);
213213
});
214214

215+
it("refreshes the active file while preserving a dirty draft", async () => {
216+
await suite.withPage(
217+
{
218+
locale: "en-US",
219+
serviceWorkers: "block",
220+
viewport: { height: 900, width: 1440 },
221+
},
222+
async ({ page }) => {
223+
const gateway = await installMockGateway(page, {
224+
featureMethods: [
225+
"agents.files.get",
226+
"agents.files.list",
227+
"agents.files.set",
228+
"agents.list",
229+
],
230+
methodResponses: {
231+
"agents.list": {
232+
defaultId: "main",
233+
mainKey: "main",
234+
scope: "agent",
235+
agents: [{ id: "main", name: "Main" }],
236+
},
237+
"agents.files.get": fileGetResponses("server revision 1"),
238+
"agents.files.list": fileListResponses,
239+
},
240+
operatorScopes: ["operator.admin", "operator.read", "operator.write"],
241+
});
242+
243+
await page.goto(`${suite.server.baseUrl}settings/agents/main/files`);
244+
const editor = page.locator(".agent-file-textarea");
245+
const fileSection = page.locator(".settings-section").filter({
246+
has: page.getByRole("heading", { name: "Core files" }),
247+
});
248+
const refresh = fileSection.getByRole("button", { name: "Refresh" });
249+
const fileActions = page.locator(".agent-file-actions");
250+
const reset = fileActions.getByRole("button", { name: "Reset" });
251+
const save = fileActions.getByRole("button", { name: "Save" });
252+
await expect.poll(() => editor.inputValue()).toBe("server revision 1");
253+
expect(await gateway.getRequests("agents.files.get")).toHaveLength(1);
254+
255+
await gateway.setMethodResponse("agents.files.get", fileGetResponses("server revision 2"));
256+
await refresh.click();
257+
await expect
258+
.poll(async () => (await gateway.getRequests("agents.files.get")).length)
259+
.toBe(2);
260+
await expect.poll(() => editor.inputValue()).toBe("server revision 2");
261+
await expect.poll(() => editor.isEnabled()).toBe(true);
262+
await capture(page, "04-refresh-adopts-authoritative-content.png");
263+
264+
await editor.fill("local dirty draft");
265+
await gateway.setMethodResponse("agents.files.get", fileGetResponses("server revision 3"));
266+
await refresh.click();
267+
await expect
268+
.poll(async () => (await gateway.getRequests("agents.files.get")).length)
269+
.toBe(3);
270+
await expect.poll(() => editor.inputValue()).toBe("local dirty draft");
271+
await expect.poll(() => editor.isEnabled()).toBe(true);
272+
await expect.poll(() => reset.isEnabled()).toBe(true);
273+
await capture(page, "05-refresh-preserves-dirty-draft.png");
274+
await reset.click();
275+
await expect.poll(() => editor.inputValue()).toBe("server revision 3");
276+
await expect.poll(() => reset.isDisabled()).toBe(true);
277+
await expect.poll(() => save.isDisabled()).toBe(true);
278+
await capture(page, "06-reset-uses-refreshed-authoritative-content.png");
279+
},
280+
);
281+
});
282+
215283
it("reads and saves the selected agent workspace through an isolated Gateway", async () => {
216284
const port = await getFreePort();
217285
const state = await createOpenClawTestState({
@@ -293,7 +361,7 @@ suite.define(() => {
293361
await expect
294362
.poll(() => readFile(path.join(mainWorkspace, "AGENTS.md"), "utf8"))
295363
.toBe("# Saved through real Gateway\n");
296-
await capture(page, "04-real-gateway-main-save.png");
364+
await capture(page, "07-real-gateway-main-save.png");
297365
},
298366
);
299367
} finally {

‎ui/src/pages/agents/agent-file-lifecycle.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type TestAgentsPage = HTMLElement & {
1818
agentFilesError: string | null;
1919
agentFileActive: string | null;
2020
agentFileContents: Record<string, string>;
21+
agentFileDrafts: Record<string, string>;
2122
gateway: {
2223
applySnapshot: (
2324
snapshot: ApplicationGatewaySnapshot,
@@ -26,6 +27,7 @@ type TestAgentsPage = HTMLElement & {
2627
};
2728
selectDefaultAgentFile: (agentId: string) => Promise<void>;
2829
syncCurrentAgentFiles: (agents?: ApplicationContext["agents"]) => void;
30+
loadAgentFiles: (agentId: string, force?: boolean) => Promise<void>;
2931
saveSelectedAgentFile: (agentId: string, name: string, content: string) => void;
3032
};
3133

@@ -90,6 +92,41 @@ describe("agent file lifecycle", () => {
9092
expect(page.agentFileContents["AGENTS.md"]).toBe("# Instructions");
9193
});
9294

95+
it("refreshes the active file base without replacing a dirty draft", async () => {
96+
const list = fileList();
97+
let authoritativeContent = "server revision 1";
98+
const request = vi.fn(async () => ({
99+
file: {
100+
...list.files[0],
101+
content: authoritativeContent,
102+
},
103+
}));
104+
const refreshFiles = vi.fn(async () => list);
105+
const client = { request } as unknown as GatewayBrowserClient;
106+
const page = document.createElement("openclaw-agents-page") as TestAgentsPage;
107+
page.context = {
108+
gateway: gateway(snapshot(client)),
109+
agents: {
110+
files: () => ({ list: null, loading: false, error: null }),
111+
ensureFiles: vi.fn(async () => list),
112+
refreshFiles,
113+
},
114+
} as unknown as ApplicationContext;
115+
setPageGateway(page, client);
116+
page.agentsSelectedId = "main";
117+
118+
await page.loadAgentFiles("main");
119+
page.agentFileDrafts = { "AGENTS.md": "local draft" };
120+
authoritativeContent = "server revision 2";
121+
122+
await page.loadAgentFiles("main", true);
123+
124+
expect(refreshFiles).toHaveBeenCalledOnce();
125+
expect(request).toHaveBeenCalledTimes(2);
126+
expect(page.agentFileContents["AGENTS.md"]).toBe("server revision 2");
127+
expect(page.agentFileDrafts["AGENTS.md"]).toBe("local draft");
128+
});
129+
93130
it("keeps a rejected save visible without refreshing it away", async () => {
94131
const request = vi.fn(async () => {
95132
throw new Error("workspace write failed");

‎ui/src/pages/agents/agents-page.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,15 @@ class AgentsPage
336336
void this.selectDefaultAgentFile(agentId);
337337
}
338338

339-
private async selectDefaultAgentFile(agentId: string) {
339+
private async selectDefaultAgentFile(agentId: string, force = false) {
340340
const files = this.agentFilesList?.files ?? [];
341341
if (!this.agentFileActive || !files.some((file) => file.name === this.agentFileActive)) {
342342
this.agentFileActive = files.find((file) => file.name === "AGENTS.md")?.name ?? null;
343343
}
344344
if (this.agentFileActive) {
345-
await loadAgentFileContent(this, agentId, this.agentFileActive);
345+
await loadAgentFileContent(this, agentId, this.agentFileActive, {
346+
force,
347+
});
346348
}
347349
}
348350

@@ -676,7 +678,7 @@ class AgentsPage
676678
}
677679
}
678680
if (this.isCurrentRequest(client, generation, agentId, { agents })) {
679-
await this.selectDefaultAgentFile(agentId);
681+
await this.selectDefaultAgentFile(agentId, force);
680682
}
681683
}
682684

‎ui/src/pages/agents/files.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function loadAgentFileContent(
3939
state: AgentFilesState,
4040
agentId: string,
4141
name: string,
42-
opts?: { force?: boolean; preserveDraft?: boolean },
42+
opts?: { force?: boolean },
4343
): Promise<boolean> {
4444
const client = state.client;
4545
if (!client || !state.connected || state.agentFilesLoading) {
@@ -62,14 +62,9 @@ export async function loadAgentFileContent(
6262
const content = res.file.content ?? "";
6363
const previousBase = state.agentFileContents[name] ?? "";
6464
const currentDraft = state.agentFileDrafts[name];
65-
const preserveDraft = opts?.preserveDraft ?? true;
6665
state.agentFilesList = mergeFileEntry(state.agentFilesList, res.file);
6766
state.agentFileContents = { ...state.agentFileContents, [name]: content };
68-
if (
69-
!preserveDraft ||
70-
!Object.hasOwn(state.agentFileDrafts, name) ||
71-
currentDraft === previousBase
72-
) {
67+
if (!Object.hasOwn(state.agentFileDrafts, name) || currentDraft === previousBase) {
7368
state.agentFileDrafts = { ...state.agentFileDrafts, [name]: content };
7469
}
7570
return true;

0 commit comments

Comments
 (0)