Skip to content

Commit d2f8cb2

Browse files
committed
perf: collapse board.get store reads
1 parent 9310586 commit d2f8cb2

5 files changed

Lines changed: 131 additions & 33 deletions

File tree

‎src/boards/board-store.parity.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ describe.each([
8787
revision: 1,
8888
sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
8989
});
90+
expect(store.getSnapshotWithHtmlDocuments("agent:main:board")).toMatchObject({
91+
snapshot: { revision: 1 },
92+
htmlDocuments: new Map([
93+
[
94+
"weather",
95+
expect.objectContaining({
96+
html: "<p>one</p>",
97+
revision: 1,
98+
sha256: expect.stringMatching(/^[a-f0-9]{64}$/u),
99+
}),
100+
],
101+
]),
102+
});
90103

91104
// Legacy clients omit heightMode on resize; explicit user sizing must pin.
92105
const resized = store.applyOps("agent:main:board", [

‎src/boards/board-store.ts‎

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ export type BoardWidgetMcpAppDocument = {
3434
interactive: boolean;
3535
};
3636
export type BoardWidgetDocument = BoardWidgetHtmlDocument | BoardWidgetMcpAppDocument;
37+
export type BoardSnapshotWithHtmlDocuments = {
38+
snapshot: BoardSnapshot;
39+
htmlDocuments: ReadonlyMap<string, BoardWidgetHtmlDocument>;
40+
};
3741

3842
export interface BoardStore {
3943
getSnapshot(sessionKey: string): BoardSnapshot;
44+
getSnapshotWithHtmlDocuments(sessionKey: string): BoardSnapshotWithHtmlDocuments;
4045
applyOps(sessionKey: string, ops: readonly BoardOp[]): BoardSnapshot;
4146
putWidget(params: BoardWidgetMaterializedPutParams): BoardSnapshot;
4247
grant(
@@ -89,6 +94,22 @@ export function cloneBoardSnapshot(snapshot: BoardSnapshot): BoardSnapshot {
8994
};
9095
}
9196

97+
function cloneBoardWidgetHtmlDocument(document: BoardWidgetHtmlDocument): BoardWidgetHtmlDocument {
98+
return {
99+
...document,
100+
...(document.declared
101+
? {
102+
declared: {
103+
...(document.declared.netOrigins
104+
? { netOrigins: [...document.declared.netOrigins] }
105+
: {}),
106+
...(document.declared.tools ? { tools: [...document.declared.tools] } : {}),
107+
},
108+
}
109+
: {}),
110+
};
111+
}
112+
92113
function createBoardWidgetDocument(
93114
content: BoardWidgetMaterializedContent,
94115
revision: number,
@@ -333,6 +354,20 @@ export class InMemoryBoardStore implements BoardStore {
333354
);
334355
}
335356

357+
getSnapshotWithHtmlDocuments(sessionKey: string): BoardSnapshotWithHtmlDocuments {
358+
const stored = this.boards.get(sessionKey);
359+
const htmlDocuments = new Map<string, BoardWidgetHtmlDocument>();
360+
for (const [name, document] of stored?.documents ?? []) {
361+
if ("html" in document) {
362+
htmlDocuments.set(name, cloneBoardWidgetHtmlDocument(document));
363+
}
364+
}
365+
return {
366+
snapshot: cloneBoardSnapshot(stored?.snapshot ?? emptyBoardSnapshot(sessionKey)),
367+
htmlDocuments,
368+
};
369+
}
370+
336371
applyOps(sessionKey: string, ops: readonly BoardOp[]): BoardSnapshot {
337372
const current = this.boards.get(sessionKey);
338373
const snapshot = current?.snapshot ?? emptyBoardSnapshot(sessionKey);
@@ -430,21 +465,7 @@ export class InMemoryBoardStore implements BoardStore {
430465
if (!document) {
431466
return undefined;
432467
}
433-
return "html" in document
434-
? {
435-
...document,
436-
...(document.declared
437-
? {
438-
declared: {
439-
...(document.declared.netOrigins
440-
? { netOrigins: [...document.declared.netOrigins] }
441-
: {}),
442-
...(document.declared.tools ? { tools: [...document.declared.tools] } : {}),
443-
},
444-
}
445-
: {}),
446-
}
447-
: undefined;
468+
return "html" in document ? cloneBoardWidgetHtmlDocument(document) : undefined;
448469
}
449470

450471
readWidgetMcpApp(sessionKey: string, name: string): BoardWidgetMcpAppDocument | undefined {

‎src/boards/sqlite-board-store.ts‎

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
createBoardGrantSnapshot,
2929
createBoardWidgetPutSnapshot,
3030
type BoardStore,
31+
type BoardSnapshotWithHtmlDocuments,
3132
type BoardWidgetHtmlDocument,
3233
type BoardWidgetMcpAppDocument,
3334
} from "./board-store.js";
@@ -161,6 +162,27 @@ function readStoredBoard(database: BoardDatabaseHandle, sessionKey: string): Sto
161162
);
162163
}
163164

165+
function rowToHtmlDocument(
166+
row: Pick<
167+
SelectedBoardWidgetRow,
168+
"content_kind" | "html" | "revision" | "sha256" | "view_generation" | "grant_state" | "manifest"
169+
>,
170+
): BoardWidgetHtmlDocument | undefined {
171+
if (row.content_kind !== "html" || row.html === null || row.view_generation === null) {
172+
return undefined;
173+
}
174+
const manifest = parseManifest(row.manifest);
175+
const declared = manifest.declared;
176+
return {
177+
html: Buffer.from(row.html).toString("utf8"),
178+
revision: row.revision,
179+
sha256: row.sha256,
180+
viewGeneration: row.view_generation,
181+
grantState: effectiveGrantState(row.grant_state as BoardWidget["grantState"], manifest),
182+
...(declared ? { declared } : {}),
183+
};
184+
}
185+
164186
function upsertTabs(
165187
database: BoardDatabaseHandle,
166188
previous: StoredBoard,
@@ -375,6 +397,33 @@ export class SqliteBoardStore implements BoardStore {
375397
);
376398
}
377399

400+
getSnapshotWithHtmlDocuments(sessionKey: string): BoardSnapshotWithHtmlDocuments {
401+
const resolved = this.resolve(sessionKey);
402+
const result = withOpenClawAgentDatabaseReadOnly(
403+
(database) =>
404+
hasSession(database, resolved.sessionKey) && boardTablesPresent(database)
405+
? readStoredBoard(database, resolved.sessionKey)
406+
: undefined,
407+
{
408+
agentId: resolved.agentId,
409+
...(resolved.path ? { path: resolved.path } : {}),
410+
env: this.options.env,
411+
},
412+
);
413+
const stored = result.found ? result.value : undefined;
414+
const htmlDocuments = new Map<string, BoardWidgetHtmlDocument>();
415+
for (const row of stored?.widgetRows ?? []) {
416+
const document = rowToHtmlDocument(row);
417+
if (document) {
418+
htmlDocuments.set(row.name, document);
419+
}
420+
}
421+
return {
422+
snapshot: cloneBoardSnapshot(stored?.snapshot ?? emptyBoardSnapshot(resolved.sessionKey)),
423+
htmlDocuments,
424+
};
425+
}
426+
378427
applyOps(sessionKey: string, ops: readonly BoardOp[]): BoardSnapshot {
379428
if (ops.length === 0) {
380429
return this.getSnapshot(sessionKey);
@@ -587,19 +636,7 @@ export class SqliteBoardStore implements BoardStore {
587636
if (!row) {
588637
return undefined;
589638
}
590-
if (row.content_kind === "html" && row.html !== null && row.view_generation !== null) {
591-
const manifest = parseManifest(row.manifest);
592-
const declared = manifest.declared;
593-
return {
594-
html: Buffer.from(row.html).toString("utf8"),
595-
revision: row.revision,
596-
sha256: row.sha256,
597-
viewGeneration: row.view_generation,
598-
grantState: effectiveGrantState(row.grant_state as BoardWidget["grantState"], manifest),
599-
...(declared ? { declared } : {}),
600-
};
601-
}
602-
return undefined;
639+
return rowToHtmlDocument(row);
603640
},
604641
{
605642
agentId: resolved.agentId,

‎src/gateway/server-methods/board.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ describe("board gateway methods", () => {
200200
expect(snapshot.widgets[0]).toMatchObject({ sandboxPort: 18790 });
201201
});
202202

203+
it("prepares HTML widget documents with the snapshot instead of rereading the store", async () => {
204+
const { invoke, store } = createHarness();
205+
await invoke("board.widget.put", {
206+
sessionKey: "agent:main:main",
207+
name: "first",
208+
content: { kind: "html", html: "<p>first</p>" },
209+
});
210+
await invoke("board.widget.put", {
211+
sessionKey: "agent:main:main",
212+
name: "second",
213+
content: { kind: "html", html: "<p>second</p>" },
214+
});
215+
const preparedRead = vi.spyOn(store, "getSnapshotWithHtmlDocuments");
216+
const documentRead = vi.spyOn(store, "readWidgetHtml");
217+
218+
await invoke("board.get", { sessionKey: "agent:main:main" });
219+
220+
expect(preparedRead).toHaveBeenCalledOnce();
221+
expect(documentRead).not.toHaveBeenCalled();
222+
});
223+
203224
it("applies updates and broadcasts board.changed", async () => {
204225
const { invoke, broadcast } = createHarness();
205226
const response = await invoke("board.update", {

‎src/gateway/server-methods/board.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ export function createBoardHandlers(
135135
invalidParams("board.get", validateBoardGetParams.errors, respond);
136136
return;
137137
}
138-
const snapshot = store.getSnapshot(params.sessionKey);
138+
const { snapshot, htmlDocuments } = store.getSnapshotWithHtmlDocuments(params.sessionKey);
139139
let sandboxPort = context.getMcpAppSandboxPort?.();
140+
let sandboxOrigin: string | undefined;
141+
let sandboxOriginResolved = false;
140142
for (const widget of snapshot.widgets) {
141143
if (widget.grantState !== "none" && widget.grantState !== "granted") {
142144
continue;
143145
}
144-
const document = store.readWidgetHtml(snapshot.sessionKey, widget.name);
146+
const document = htmlDocuments.get(widget.name);
145147
if (!document || document.revision !== widget.revision) {
146148
continue;
147149
}
@@ -170,9 +172,13 @@ export function createBoardHandlers(
170172
if (sandboxPort !== undefined) {
171173
widget.sandboxUrl = buildBoardWidgetSandboxPath(document);
172174
widget.sandboxPort = sandboxPort;
173-
const configuredOrigin = context.getRuntimeConfig?.().mcp?.apps?.sandboxOrigin;
174-
if (configuredOrigin) {
175-
widget.sandboxOrigin = new URL(configuredOrigin).origin;
175+
if (!sandboxOriginResolved) {
176+
const configuredOrigin = context.getRuntimeConfig?.().mcp?.apps?.sandboxOrigin;
177+
sandboxOrigin = configuredOrigin ? new URL(configuredOrigin).origin : undefined;
178+
sandboxOriginResolved = true;
179+
}
180+
if (sandboxOrigin) {
181+
widget.sandboxOrigin = sandboxOrigin;
176182
}
177183
}
178184
}

0 commit comments

Comments
 (0)