Skip to content

Commit e15d532

Browse files
authored
fix(memory): scope Dream Diary requests to agents (#125936)
1 parent 5301fb5 commit e15d532

6 files changed

Lines changed: 130 additions & 68 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/NodeRuntime.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7809,9 +7809,11 @@ class NodeRuntime private constructor(
78097809
disconnectedSummary = GatewayDreamingSummary(),
78107810
failureText = nativeText("Could not load dreaming."),
78117811
) { gatewayScope ->
7812-
val statusResponse = requestGatewayData(gatewayScope, "doctor.memory.status", "{}")
7812+
val agentId = resolveActiveAgentId().takeIf { it.isNotEmpty() } ?: error("No active agent")
7813+
val paramsJson = buildJsonObject { put("agentId", JsonPrimitive(agentId)) }.toString()
7814+
val statusResponse = requestGatewayData(gatewayScope, "doctor.memory.status", paramsJson)
78137815
val statusRoot = json.parseToJsonElement(statusResponse).asObjectOrNull()
7814-
val diaryResponse = requestGatewayData(gatewayScope, "doctor.memory.dreamDiary", "{}")
7816+
val diaryResponse = requestGatewayData(gatewayScope, "doctor.memory.dreamDiary", paramsJson)
78157817
val diaryRoot = json.parseToJsonElement(diaryResponse).asObjectOrNull()
78167818
parseDreamingSummary(dreaming = statusRoot?.get("dreaming").asObjectOrNull(), diary = diaryRoot)
78177819
}

‎apps/ios/Sources/Design/AgentProTab+GatewayData.swift‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ extension AgentProTab {
133133
async let presence = self.requestOptional([PresenceEntry].self, method: "system-presence")
134134
async let cronStatus = self.requestOptional(CronStatusLite.self, method: "cron.status")
135135
async let cronJobs = self.requestAllCronJobs()
136-
async let dreaming = self.requestOptional(DreamingStatusEnvelope.self, method: "doctor.memory.status")
137-
async let dreamDiary = self.requestOptional(DreamDiaryLite.self, method: "doctor.memory.dreamDiary")
136+
async let dreaming = self.requestOptional(
137+
DreamingStatusEnvelope.self,
138+
method: "doctor.memory.status",
139+
paramsJSON: skillsParams)
140+
async let dreamDiary = self.requestOptional(
141+
DreamDiaryLite.self,
142+
method: "doctor.memory.dreamDiary",
143+
paramsJSON: skillsParams)
138144
async let usage = self.requestOptional(
139145
CostUsageSummaryLite.self,
140146
method: "usage.cost",

‎ui/src/pages/agents/memory/dreaming.test.ts‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ afterAll(() => {
7979
function createState(): { state: DreamingState; request: ReturnType<typeof vi.fn<TestRequest>> } {
8080
const request = vi.fn<TestRequest>();
8181
const state: DreamingState = {
82-
...createDreamingState(),
82+
...createDreamingState({ selectedAgentId: "main" }),
8383
client: {
8484
request,
8585
} as unknown as DreamingState["client"],
@@ -247,7 +247,7 @@ describe("dreaming controller", () => {
247247

248248
await loadDreamingStatus(state);
249249

250-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
250+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
251251
const status = state.dreamingStatus;
252252
expect(status).toBe(payload.dreaming);
253253
expect(status?.enabled).toBe(true);
@@ -290,6 +290,25 @@ describe("dreaming controller", () => {
290290
});
291291
});
292292

293+
it("does not request agent-scoped resources or actions without a selected agent", async () => {
294+
const { state, request } = createState();
295+
state.selectedAgentId = null;
296+
state.hello = gatewayHelloForMethods(
297+
["wiki.importInsights", "wiki.overview", "doctor.memory.backfillDreamDiary"],
298+
["operator.write"],
299+
);
300+
301+
await Promise.all([
302+
loadDreamingStatus(state),
303+
loadDreamDiary(state),
304+
loadWikiImportInsights(state),
305+
loadWikiOverview(state),
306+
]);
307+
308+
await expect(backfillDreamDiary(state)).resolves.toBe(false);
309+
expect(request).not.toHaveBeenCalled();
310+
});
311+
293312
it("starts a new selected-agent status load and ignores stale completions", async () => {
294313
const { state, request } = createState();
295314
const agentA = createDeferred<unknown>();
@@ -473,7 +492,7 @@ describe("dreaming controller", () => {
473492

474493
await loadWikiImportInsights(state);
475494

476-
expect(request).toHaveBeenCalledWith("wiki.importInsights", {});
495+
expect(request).toHaveBeenCalledWith("wiki.importInsights", { agentId: "main" });
477496
expect(state.wikiImportInsights?.totalItems).toBe(2);
478497
expect(state.wikiImportInsights?.totalClusters).toBe(1);
479498
expect(state.wikiImportInsights?.clusters).toHaveLength(1);
@@ -548,7 +567,7 @@ describe("dreaming controller", () => {
548567

549568
await loadWikiImportInsights(state);
550569

551-
expect(request).toHaveBeenCalledWith("wiki.importInsights", {});
570+
expect(request).toHaveBeenCalledWith("wiki.importInsights", { agentId: "main" });
552571
expect(state.wikiImportInsights?.totalItems).toBe(1);
553572
expect(state.wikiImportInsights?.totalClusters).toBe(1);
554573
expect(state.wikiImportInsightsError).toBeNull();
@@ -666,7 +685,7 @@ describe("dreaming controller", () => {
666685

667686
await loadWikiOverview(state);
668687

669-
expect(request).toHaveBeenCalledWith("wiki.overview", {});
688+
expect(request).toHaveBeenCalledWith("wiki.overview", { agentId: "main" });
670689
expect(state.wikiOverview?.totalItems).toBe(1);
671690
expect(state.wikiOverview?.totalPages).toBe(2);
672691
expect(state.wikiOverview?.pageCounts.source).toBe(1);
@@ -749,7 +768,7 @@ describe("dreaming controller", () => {
749768

750769
await loadWikiOverview(state);
751770

752-
expect(request).toHaveBeenCalledWith("wiki.overview", {});
771+
expect(request).toHaveBeenCalledWith("wiki.overview", { agentId: "main" });
753772
expect(state.wikiOverview?.totalItems).toBe(0);
754773
expect(state.wikiOverview?.totalPages).toBe(0);
755774
expect(state.wikiOverview?.pageCounts).toEqual({
@@ -1074,7 +1093,7 @@ describe("dreaming controller", () => {
10741093

10751094
await loadDreamDiary(state);
10761095

1077-
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1096+
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
10781097
expect(state.dreamDiaryPath).toBe("DREAMS.md");
10791098
expect(state.dreamDiaryContent).toBe("## Dream Diary\n- recurring glacier thoughts");
10801099
expect(state.dreamDiaryError).toBeNull();
@@ -1253,9 +1272,9 @@ describe("dreaming controller", () => {
12531272
const ok = await backfillDreamDiary(state);
12541273

12551274
expect(ok).toBe(true);
1256-
expect(request).toHaveBeenCalledWith("doctor.memory.backfillDreamDiary", {});
1257-
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1258-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
1275+
expect(request).toHaveBeenCalledWith("doctor.memory.backfillDreamDiary", { agentId: "main" });
1276+
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
1277+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
12591278
expect(state.dreamDiaryContent).toBe("backfilled diary");
12601279
expect(state.dreamDiaryActionLoading).toBe(false);
12611280
});
@@ -1318,9 +1337,9 @@ describe("dreaming controller", () => {
13181337
const ok = await resetDreamDiary(state);
13191338

13201339
expect(ok).toBe(true);
1321-
expect(request).toHaveBeenCalledWith("doctor.memory.resetDreamDiary", {});
1322-
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1323-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
1340+
expect(request).toHaveBeenCalledWith("doctor.memory.resetDreamDiary", { agentId: "main" });
1341+
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
1342+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
13241343
expect(state.dreamDiaryContent).toBeNull();
13251344
expect(state.dreamDiaryActionLoading).toBe(false);
13261345
});
@@ -1345,9 +1364,11 @@ describe("dreaming controller", () => {
13451364
const ok = await resetGroundedShortTerm(state);
13461365

13471366
expect(ok).toBe(true);
1348-
expect(request).toHaveBeenCalledWith("doctor.memory.resetGroundedShortTerm", {});
1349-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
1350-
expect(request).not.toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1367+
expect(request).toHaveBeenCalledWith("doctor.memory.resetGroundedShortTerm", {
1368+
agentId: "main",
1369+
});
1370+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
1371+
expect(request).not.toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
13511372
expect(state.dreamDiaryContent).toBe("keep existing diary");
13521373
expect(state.dreamDiaryActionLoading).toBe(false);
13531374
});
@@ -1378,9 +1399,11 @@ describe("dreaming controller", () => {
13781399
const ok = await repairDreamingArtifacts(state);
13791400

13801401
expect(ok).toBe(true);
1381-
expect(request).toHaveBeenCalledWith("doctor.memory.repairDreamingArtifacts", {});
1382-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
1383-
expect(request).not.toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1402+
expect(request).toHaveBeenCalledWith("doctor.memory.repairDreamingArtifacts", {
1403+
agentId: "main",
1404+
});
1405+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
1406+
expect(request).not.toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
13841407
expect(state.dreamDiaryContent).toBe("keep existing diary");
13851408
expect(state.dreamDiaryActionMessage).toEqual({
13861409
kind: "success",
@@ -1415,9 +1438,9 @@ describe("dreaming controller", () => {
14151438
const ok = await dedupeDreamDiary(state);
14161439

14171440
expect(ok).toBe(true);
1418-
expect(request).toHaveBeenCalledWith("doctor.memory.dedupeDreamDiary", {});
1419-
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", {});
1420-
expect(request).toHaveBeenCalledWith("doctor.memory.status", {});
1441+
expect(request).toHaveBeenCalledWith("doctor.memory.dedupeDreamDiary", { agentId: "main" });
1442+
expect(request).toHaveBeenCalledWith("doctor.memory.dreamDiary", { agentId: "main" });
1443+
expect(request).toHaveBeenCalledWith("doctor.memory.status", { agentId: "main" });
14211444
expect(state.dreamDiaryContent).toBe("deduped diary");
14221445
expect(state.dreamDiaryActionMessage).toEqual({
14231446
kind: "success",

‎ui/src/pages/agents/memory/dreaming.ts‎

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export type WikiOverview = {
105105
};
106106

107107
type DreamingResourceKey = "dreamingStatus" | "dreamDiary" | "wikiImportInsights" | "wikiOverview";
108-
type DreamingResourceRequest = { agentId: string | null };
108+
type DreamingResourceRequest = { agentId: string };
109109

110110
export type DreamingState = {
111111
client: GatewayBrowserClient | null;
@@ -292,18 +292,6 @@ function resolveSelectedAgentId(state: DreamingState): string | null {
292292
return normalizeTrimmedString(state.selectedAgentId) ?? null;
293293
}
294294

295-
function buildSelectedAgentPayloadForAgentId(
296-
agentId: string | null,
297-
): { agentId: string } | Record<string, never> {
298-
return agentId ? { agentId } : {};
299-
}
300-
301-
function buildSelectedAgentPayload(
302-
state: DreamingState,
303-
): { agentId: string } | Record<string, never> {
304-
return buildSelectedAgentPayloadForAgentId(resolveSelectedAgentId(state));
305-
}
306-
307295
export function resolveConfiguredDreaming(configValue: Record<string, unknown> | null): {
308296
pluginId: string;
309297
enabled: boolean;
@@ -389,15 +377,17 @@ async function loadDreamingResource<Key extends DreamingResourceKey>(
389377
key: Key,
390378
spec: DreamingResourceSpec<Key> = DREAMING_RESOURCE_SPECS[key],
391379
): Promise<void> {
392-
const client = state.client;
393-
if (!client || !state.connected) {
394-
return;
395-
}
396-
397380
const agentId = resolveSelectedAgentId(state);
398381
const loadingKey = `${key}Loading` as const;
399382
const errorKey = `${key}Error` as const;
400383
const agentKey = `${key}AgentId` as const;
384+
if (!agentId) {
385+
return;
386+
}
387+
const client = state.client;
388+
if (!client || !state.connected) {
389+
return;
390+
}
401391
if (state[agentKey] !== agentId) {
402392
spec.clear(state);
403393
}
@@ -423,10 +413,7 @@ async function loadDreamingResource<Key extends DreamingResourceKey>(
423413
state[loadingKey] = true;
424414
state[errorKey] = null;
425415
try {
426-
const payload = await client.request<DreamingResourcePayloads[Key]>(
427-
spec.method,
428-
buildSelectedAgentPayloadForAgentId(agentId),
429-
);
416+
const payload = await client.request<DreamingResourcePayloads[Key]>(spec.method, { agentId });
430417
if (state.resourceRequests[key] !== request || resolveSelectedAgentId(state) !== agentId) {
431418
return;
432419
}
@@ -473,8 +460,10 @@ async function runDreamDiaryAction(
473460
},
474461
): Promise<boolean> {
475462
const client = state.client;
463+
const agentId = resolveSelectedAgentId(state);
476464
if (
477465
!client ||
466+
!agentId ||
478467
!canCallDreamingMethod(state, method, "operator.write") ||
479468
state.dreamDiaryActionLoading
480469
) {
@@ -486,10 +475,7 @@ async function runDreamDiaryAction(
486475
state.dreamDiaryActionMessage = null;
487476
state.dreamDiaryActionArchivePath = null;
488477
try {
489-
const payload = await client.request<DoctorMemoryDreamActionPayload>(
490-
method,
491-
buildSelectedAgentPayload(state),
492-
);
478+
const payload = await client.request<DoctorMemoryDreamActionPayload>(method, { agentId });
493479
if (options?.reloadDiary !== false) {
494480
await loadDreamDiary(state);
495481
}

‎ui/src/pages/agents/memory/memory-panel.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function contextWithGateway(
102102
},
103103
runtimeConfig: {
104104
state: { configForm, configSnapshot: null },
105+
ensureLoaded: vi.fn(async () => undefined),
105106
refresh: vi.fn(async () => undefined),
106107
removeFormValue: vi.fn(),
107108
waitForPendingWrites: vi.fn(async () => undefined),
@@ -134,6 +135,40 @@ afterEach(() => {
134135
});
135136

136137
describe("AgentMemoryPanel gateway lifecycle", () => {
138+
it("waits for a committed agent before loading agent-scoped memory", async () => {
139+
const request = vi.fn(async (method: string) => {
140+
if (method === "doctor.memory.status") {
141+
return { dreaming: null };
142+
}
143+
if (method === "doctor.memory.dreamDiary") {
144+
return { found: false, path: "DREAMS.md" };
145+
}
146+
return {};
147+
});
148+
const page = document.createElement("openclaw-agent-memory-panel") as TestMemoryPanel;
149+
page.context = contextWithGateway({ request } as unknown as GatewayBrowserClient, true);
150+
151+
document.body.append(page);
152+
await page.updateComplete;
153+
await page.updateComplete;
154+
await Promise.resolve();
155+
await expect(page.openWikiPage("unowned.md")).resolves.toBeNull();
156+
157+
expect(request).not.toHaveBeenCalled();
158+
159+
page.agentId = "support";
160+
await page.updateComplete;
161+
await vi.waitFor(() => {
162+
expect(request.mock.calls.filter(([method]) => method === "doctor.memory.status")).toEqual([
163+
["doctor.memory.status", { agentId: "support" }],
164+
]);
165+
expect(
166+
request.mock.calls.filter(([method]) => method === "doctor.memory.dreamDiary"),
167+
).toEqual([["doctor.memory.dreamDiary", { agentId: "support" }]]);
168+
expect(request).toHaveBeenCalledTimes(2);
169+
});
170+
});
171+
137172
it("does not run a confirmed dreaming action after the selected agent changes", async () => {
138173
const confirmation = deferred<boolean>();
139174
vi.mocked(showConfirmDialog).mockReturnValueOnce(confirmation.promise);
@@ -177,6 +212,13 @@ describe("AgentMemoryPanel gateway lifecycle", () => {
177212
expect(page.dreaming).not.toBe(previousState);
178213
expect(page.dreaming.selectedAgentId).toBe("support");
179214
expect(page.dreaming.dreamDiaryContent).toBeNull();
215+
216+
page.dreaming.dreamDiaryContent = "support-only";
217+
page.agentId = "";
218+
await page.updateComplete;
219+
220+
expect(page.dreaming.selectedAgentId).toBeNull();
221+
expect(page.dreaming.dreamDiaryContent).toBeNull();
180222
});
181223

182224
it("resets provider and modal state when the gateway source changes", async () => {

0 commit comments

Comments
 (0)