Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(dashboard): refresh snapshots when lenses reopen (#995)

Divyam Talwarfe744cb0fe75
diff --git a/cli/beacon/internal/endpoint/dashboard/static/app.js b/cli/beacon/internal/endpoint/dashboard/static/app.js
index 78dc2c44..d4eeaec7 100644
--- a/cli/beacon/internal/endpoint/dashboard/static/app.js
+++ b/cli/beacon/internal/endpoint/dashboard/static/app.js
@@ -3195,7 +3195,6 @@ const lensHost = {
added: [],
active: "",
mount: null,
- dataPromise: null,
};
function readAddedLenses() {
@@ -3371,15 +3370,16 @@ function selectLens(id, { history = true } = {}) {
else unmountLens();
}
-function lensData() {
- if (!lensHost.dataPromise) {
- lensHost.dataPromise = getJSON(`/api/lens-data?session=${encodeURIComponent(lensHost.session)}`);
- // A failed load is not cached, so reopening the lens tries again.
- lensHost.dataPromise.catch(() => {
- lensHost.dataPromise = null;
+function lensData(mount) {
+ if (!mount.dataPromise) {
+ // A frame receives one snapshot. Reopening creates a new mount and fetches
+ // current data without changing the snapshot of a frame already on screen.
+ mount.dataPromise = getJSON(`/api/lens-data?session=${encodeURIComponent(lensHost.session)}`);
+ mount.dataPromise.catch(() => {
+ mount.dataPromise = null;
});
}
- return lensHost.dataPromise;
+ return mount.dataPromise;
}
function unmountLens() {
@@ -3405,7 +3405,7 @@ function mountLens(lens) {
frame.title = `${lens.title} lens`;
frame.src = `/lenses/frame/${encodeURIComponent(lens.id)}`;
- const mount = { lens, frame, port: null, loads: 0, delivered: false, rendered: false, timer: 0, settleTimer: 0 };
+ const mount = { lens, frame, dataPromise: null, port: null, loads: 0, delivered: false, rendered: false, timer: 0, settleTimer: 0 };
lensHost.mount = mount;
const fail = (reason) => {
if (lensHost.mount !== mount) return;
@@ -3450,7 +3450,7 @@ function mountLens(lens) {
mount.port.postMessage(payload);
return true;
};
- lensData().then(
+ lensData(mount).then(
(data) => reply({ type: "trace", data }),
(err) => reply({ type: "traceError", message: String(err.message || err) }),
).then((sent) => {
diff --git a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lens-snapshot.spec.ts b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lens-snapshot.spec.ts
new file mode 100644
index 00000000..4988700a
--- /dev/null
+++ b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/lens-snapshot.spec.ts
@@ -0,0 +1,83 @@
+import { expect, test, type Page } from '@playwright/test';
+
+const lens = (page: Page) => page.frameLocator('iframe.lens-frame');
+const count = (page: Page) => lens(page).locator('#root table').first().locator('tr').nth(1).locator('td').nth(1);
+const tab = (page: Page) => page.locator('.lens-tab[data-lens="activity"]');
+const bundle = (session: string, size: number) => ({
+ trace: { id: `session:${session}`, events: Array.from({ length: size }, (_, i) => ({
+ id: `${session}-${i}`, type: 'command', command: { command: `echo ${session}-${i}` },
+ })) },
+});
+
+async function fixture(page: Page, size = () => 1) {
+ const origin = new URL(test.info().project.use.baseURL!).origin;
+ await page.route('**/*', (route) => new URL(route.request().url()).origin === origin ? route.continue() : route.abort());
+ await page.route('**/api/status', (route) => route.fulfill({ json: {} }));
+ await page.route('**/api/session?*', (route) => {
+ const id = new URL(route.request().url()).searchParams.get('id')!;
+ return route.fulfill({ json: { session: { id }, events: Array.from({ length: size() }, (_, i) => ({
+ event: { id: `${id}-${i}`, action: 'command.executed' }, session: { id }, command: { command: `echo ${id}-${i}` },
+ })) } });
+ });
+}
+
+async function settled(page: Page) {
+ await page.evaluate(() => new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))));
+}
+
+// Exercise a repeated host-protocol request as well as the lens prelude's own
+// one-shot getTrace. The actual host callback must reuse this mount's snapshot.
+async function requestAgain(page: Page) {
+ await page.evaluate('lensHost.mount.port.onmessage({ data: { type: "getTrace" } })');
+ await settled(page);
+}
+
+test('a reopened lens sees the normal detail refresh without reloading the active frame', async ({ page }) => {
+ await page.clock.install();
+ let size = 1;
+ let requests = 0;
+ await fixture(page, () => size);
+ await page.route('**/api/lens-data?*', (route) => {
+ requests++;
+ return route.fulfill({ json: bundle('s1', size) });
+ });
+ await page.goto('/session.html?id=s1&lens=activity');
+ await expect(count(page)).toHaveText('1');
+ await expect(page.locator('#lens-status')).toBeHidden();
+ await page.locator('iframe.lens-frame').evaluate((frame) => { frame.setAttribute('data-original', 'yes'); });
+ const historyLength = await page.evaluate(() => history.length);
+ size = 2;
+ const refreshed = page.waitForResponse((response) => new URL(response.url()).pathname === '/api/session');
+ await page.clock.runFor(15_000);
+ await (await refreshed).finished();
+ await expect(page.locator('#result-meta')).toContainText('2 of 2 events');
+ await expect(count(page)).toHaveText('1');
+ await tab(page).click();
+ await expect(page.locator('iframe.lens-frame')).toHaveAttribute('data-original', 'yes');
+ expect(await page.evaluate(() => history.length)).toBe(historyLength);
+ await requestAgain(page);
+ expect(requests).toBe(1);
+
+ await page.locator('.lens-tab[data-lens=""]').click();
+ await expect(page.locator('iframe.lens-frame')).toHaveCount(0);
+ await tab(page).click();
+ await expect(count(page)).toHaveText('2');
+ expect(requests).toBe(2);
+ await requestAgain(page);
+ expect(requests).toBe(2);
+});
+
+test('a failed snapshot can be retried by reopening the lens', async ({ page }) => {
+ await fixture(page);
+ let requests = 0;
+ await page.route('**/api/lens-data?*', (route) => ++requests === 1
+ ? route.fulfill({ status: 503, json: { error: 'snapshot unavailable' } })
+ : route.fulfill({ json: bundle('s1', 2) }));
+ await page.goto('/session.html?id=s1&lens=activity');
+ await expect(lens(page).locator('#status')).toContainText('snapshot unavailable');
+ await page.locator('.lens-tab[data-lens=""]').click();
+ await expect(page.locator('iframe.lens-frame')).toHaveCount(0);
+ await tab(page).click();
+ await expect(count(page)).toHaveText('2');
+ expect(requests).toBe(2);
+});