Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(ui): resume the agent once per page after a project switch

Bernhard Jackiewicz0c84b055b221
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
index 212f49680..256a7984d 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
@@ -1398,7 +1398,8 @@ describe('a cached model across reloads and project switches (K10, K24)', () =>
await render({ ...props, isCached: vi.fn(async () => true) });
expect(props.createRuntime).toHaveBeenCalledOnce();
expect(runtime.prepare.mock.calls[0][1]).toEqual({ cacheOnly: true });
- expect(window.sessionStorage.getItem('cbm-agent-resume')).toBeNull();
+ // A later reload of this tab starts with the agent off again.
+ expect(JSON.parse(window.sessionStorage.getItem('cbm-agent-resume')!).page).toBe(performance.timeOrigin);
});
});
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.tsx b/graph-ui/src/browser-ai/BrowserChatDock.tsx
index 1cdd8968d..7279f2abb 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.tsx
@@ -528,7 +528,10 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
const waitingEpoch = epoch.current;
// The selected symbol's source grounds a question as it grounds the explanation (K14).
const symbol = !retry && !reader && currentGraph.length ? await sourceFor(currentGraph[0]) : undefined;
- if (queued.cancelled || manualRequest.current !== queued || epoch.current !== waitingEpoch || runtime.current !== currentRuntime) return;
+ if (queued.cancelled || manualRequest.current !== queued || epoch.current !== waitingEpoch || runtime.current !== currentRuntime) {
+ if (manualRequest.current === queued) manualRequest.current = undefined;
+ return;
+ }
if (!retry && ((reader?.source?.text.length ?? 0) > 5000 || currentGraph.length)) packet = prepareExplanationContext(reader, currentGraph, chatEvidence, symbol);
let request = retry && !ask ? retry.request.map(message => ({ ...message })) : makeRequest();
if (automaticRun) {
diff --git a/graph-ui/src/browser-ai/agent-resume.test.ts b/graph-ui/src/browser-ai/agent-resume.test.ts
index e06db497f..0b9aa5a95 100644
--- a/graph-ui/src/browser-ai/agent-resume.test.ts
+++ b/graph-ui/src/browser-ai/agent-resume.test.ts
@@ -6,11 +6,14 @@ import { BROWSER_MODEL, browserModelBaseUrl, BROWSER_MODELS, isBrowserModelCache
afterEach(() => { vi.unstubAllGlobals(); window.sessionStorage.clear(); });
describe('keeping the agent across a project switch (K24)', () => {
- it('resumes once, in this tab, shortly after the switch', () => {
+ it('resumes on the first page after the switch, in this tab, shortly after it', () => {
requestAgentResume(1000);
expect(JSON.parse(window.sessionStorage.getItem(AGENT_RESUME_KEY)!)).toEqual({ at: 1000 });
- expect(takeAgentResume(1000 + AGENT_RESUME_WINDOW_MS - 1)).toBe(true);
- expect(takeAgentResume(1000)).toBe(false);
+ expect(takeAgentResume(1000 + AGENT_RESUME_WINDOW_MS - 1, 7)).toBe(true);
+ // The same page reads it again (React's development double effect); a later reload does not.
+ expect(takeAgentResume(1000, 7)).toBe(true);
+ expect(takeAgentResume(1000, 8)).toBe(false);
+ expect(window.sessionStorage.getItem(AGENT_RESUME_KEY)).toBeNull();
});
it('does not resume after the window, from malformed data or without storage', () => {
diff --git a/graph-ui/src/browser-ai/agent-resume.ts b/graph-ui/src/browser-ai/agent-resume.ts
index 89099eda4..30e61144a 100644
--- a/graph-ui/src/browser-ai/agent-resume.ts
+++ b/graph-ui/src/browser-ai/agent-resume.ts
@@ -5,16 +5,20 @@ export const AGENT_RESUME_KEY = 'cbm-agent-resume';
/** A switch is a navigation of a few seconds; a stale note must not load a model later. */
export const AGENT_RESUME_WINDOW_MS = 60_000;
+const thisPage = (): number => typeof performance === 'undefined' ? 0 : performance.timeOrigin;
+
export function requestAgentResume(now = Date.now()): void {
try { window.sessionStorage.setItem(AGENT_RESUME_KEY, JSON.stringify({ at: now })); } catch { /* Without storage the next page starts with the agent off. */ }
}
-/** True once for a fresh note; reading removes it. */
-export function takeAgentResume(now = Date.now()): boolean {
- let raw: string | null = null;
- try { raw = window.sessionStorage.getItem(AGENT_RESUME_KEY); window.sessionStorage.removeItem(AGENT_RESUME_KEY); } catch { return false; }
+/** True for a fresh note, for the first page that reads it only: a later reload does not
+ * resume again, while a second read on the same page (React's development double effect) does. */
+export function takeAgentResume(now = Date.now(), page = thisPage()): boolean {
try {
- const at = (JSON.parse(raw ?? 'null') as { at?: unknown } | null)?.at;
- return typeof at === 'number' && now >= at && now - at <= AGENT_RESUME_WINDOW_MS;
+ const note = JSON.parse(window.sessionStorage.getItem(AGENT_RESUME_KEY) ?? 'null') as { at?: unknown; page?: unknown } | null;
+ const fresh = typeof note?.at === 'number' && now >= note.at && now - note.at <= AGENT_RESUME_WINDOW_MS;
+ if (!fresh || (note!.page !== undefined && note!.page !== page)) { window.sessionStorage.removeItem(AGENT_RESUME_KEY); return false; }
+ window.sessionStorage.setItem(AGENT_RESUME_KEY, JSON.stringify({ at: note!.at, page }));
+ return true;
} catch { return false; }
}