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): keep the download progress across a project switch (K24)

Bernhard Jackiewiczf2f30e8751b3
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.handover.test.tsx b/graph-ui/src/browser-ai/BrowserChatDock.handover.test.tsx
index 002bb7e20..d3ec4da79 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.handover.test.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.handover.test.tsx
@@ -127,6 +127,30 @@ describe('the loaded model across an in-page project switch (K24)', () => {
expect(states.at(-1)).toBe('active');
});
+ it('shows the progress of a download still running at the switch, and its later progress, in the new project', async () => {
+ const { props, runtime, states } = fixture(); const loading = deferred<void>();
+ let report: (value: BrowserAiProgress) => void = () => {};
+ runtime.prepare.mockImplementationOnce(async (progress) => { report = progress; return loading.promise; });
+ props.isCached = vi.fn(async () => false);
+ await configure(props, 'django-demo');
+ await act(async () => button('Download & load').click());
+ await act(async () => report({ file: 'onnx/model_q4f16.onnx', progress: 40 }));
+ const bar = () => document.body.querySelector<HTMLProgressElement>('.cbm-chat-loading progress');
+ expect(bar()?.value).toBe(40);
+ await switchTo(props, 'cbm');
+ // The configuration of the new project shows how far the download got, not an empty bar.
+ await act(async () => root.render(dock({ ...props, settingsRequest: 1 }, 'cbm')));
+ expect(bar()?.value).toBe(40);
+ expect(document.body.querySelector('.cbm-chat-loading')?.textContent).toContain('onnx/model_q4f16.onnx');
+ await act(async () => report({ file: 'onnx/model_q4f16.onnx', progress: 75 }));
+ expect(bar()?.value).toBe(75);
+ await act(async () => loading.resolve());
+ expect(bar()).toBeNull();
+ expect(states.at(-1)).toBe('active');
+ expect(props.createRuntime).toHaveBeenCalledOnce();
+ expect(runtime.prepare).toHaveBeenCalledOnce();
+ });
+
it('unloads the model when no dock takes it, and on an unmount that is not a switch', async () => {
const first = fixture();
await load(first.props, 'django-demo');
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.tsx b/graph-ui/src/browser-ai/BrowserChatDock.tsx
index c50081bd6..60eb29500 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.tsx
@@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState, type FormEvent, type JSX, type Re
import type { BrowserAiProgress } from './browser-ai-controller';
import { createBrowserChatRuntime, type BrowserChatRuntime } from './browser-ai-runtime';
import { BROWSER_MODELS, getBrowserModel, isBrowserModelCached, removeBrowserModelCache, type BrowserModel } from './model-policy';
-import { keepAgent, offerAgent, peekAgent, takeAgent, type AgentHandover } from './agent-handover';
+import { keepAgent, loadProgress, offerAgent, peekAgent, takeAgent, type AgentHandover, type LoadProgress } from './agent-handover';
import { buildChatMessages, selectionLocation, snapshotAttachment, snapshotReaderContext, trimChatHistory, type BrowserChatAttachment, type BrowserChatContext, type BrowserChatReaderContext, type BrowserChatSource, type BrowserChatTurn } from './chat-model';
import { explanationInput, EXPLANATION_DELAY_MS, type ExplanationInput } from './proactive-selection';
import { prepareExplanationContext, selectionSummary, type PreparedExplanationContext } from './explanation-context';
@@ -185,7 +185,8 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
const [error, setError] = useState<string>();
const [runtimeFailed, setRuntimeFailed] = useState(false);
const [notice, setNotice] = useState<string>();
- const [progress, setProgress] = useState<BrowserAiProgress>();
+ // A download handed over unfinished shows how far it got from the first frame (K24).
+ const [progress, setProgress] = useState<BrowserAiProgress | undefined>(() => peekAgent(model.id)?.progress?.latest);
/** Models whose files are in the browser cache: checked on start, not remembered per session (K10). */
const [cached, setCached] = useState<ReadonlySet<string>>(() => new Set());
const [settingsOpen, setSettingsOpen] = useState(false);
@@ -201,8 +202,8 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
const runtime = useRef<BrowserChatRuntime | undefined>(undefined);
/** The model the worker was created for; the stored choice can change in another tab. */
const runtimeModel = useRef<string | undefined>(undefined);
- /** The model still loading, so a project switch can hand it over unfinished (K24). */
- const loading = useRef<Promise<void> | undefined>(undefined);
+ /** The model still loading and its progress, so a project switch can hand it over unfinished (K24). */
+ const loading = useRef<{ ready: Promise<void>; progress: LoadProgress } | undefined>(undefined);
const epoch = useRef(0);
const pending = useRef(false);
const stopRequested = useRef(false);
@@ -473,7 +474,7 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
});
};
const handoverOf = (current: BrowserChatRuntime, modelId: string): AgentHandover => ({ runtime: current, modelId,
- ...operationSettled.current ? { settled: operationSettled.current } : {}, ...loading.current ? { ready: loading.current } : {} });
+ ...operationSettled.current ? { settled: operationSettled.current } : {}, ...loading.current ? { ready: loading.current.ready, progress: loading.current.progress } : {} });
/** Gives the model to the dock of the next project (K24). What this dock was doing stops,
* and its late results land nowhere: its epoch has moved on. */
const handOver = (): AgentHandover | undefined => {
@@ -496,12 +497,15 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
if (!waiting) { setPhase('ready'); return; }
const ticket = epoch.current;
pending.current = true;
- if (handover.ready) { loading.current = handover.ready; setPhase('preparing'); }
- else { stopRequested.current = true; setStopping(true); setPhase('counting'); }
+ if (handover.ready) {
+ const progress = handover.progress ?? loadProgress();
+ loading.current = { ready: handover.ready, progress }; setPhase('preparing');
+ progress.follow(value => { if (epoch.current === ticket && runtime.current === adopted) setProgress(value); });
+ } else { stopRequested.current = true; setStopping(true); setPhase('counting'); }
void waiting.then(() => {
if (epoch.current !== ticket || runtime.current !== adopted) return;
pending.current = false; stopRequested.current = false; loading.current = undefined; setStopping(false);
- if (handover.ready) setCached(previous => new Set(previous).add(handover.modelId));
+ if (handover.ready) { setCached(previous => new Set(previous).add(handover.modelId)); setProgress(undefined); }
setPhase('ready');
}, (failure: unknown) => {
if (epoch.current !== ticket || runtime.current !== adopted) return;
@@ -535,8 +539,10 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
const nextRuntime = createRuntime(model.id);
runtime.current = nextRuntime; runtimeModel.current = model.id;
nextRuntime.setFatalHandler?.(failure => { if (runtime.current === nextRuntime) invalidateRuntime(failure); });
- const loaded = nextRuntime.prepare(value => { if (epoch.current === ticket) setProgress(value); }, cacheOnly ? { cacheOnly } : undefined);
- loading.current = loaded;
+ const progress = loadProgress();
+ progress.follow(value => { if (epoch.current === ticket) setProgress(value); });
+ const loaded = nextRuntime.prepare(value => progress.report(value), cacheOnly ? { cacheOnly } : undefined);
+ loading.current = { ready: loaded, progress };
await loaded;
if (epoch.current !== ticket) return;
loading.current = undefined;
diff --git a/graph-ui/src/browser-ai/agent-handover.ts b/graph-ui/src/browser-ai/agent-handover.ts
index 656884768..d5c0cc6a1 100644
--- a/graph-ui/src/browser-ai/agent-handover.ts
+++ b/graph-ui/src/browser-ai/agent-handover.ts
@@ -1,4 +1,4 @@
-import type { BrowserChatRuntime } from './browser-ai-runtime';
+import type { BrowserAiProgress, BrowserChatRuntime } from './browser-ai-runtime';
/** A project switch stays in the page (K24): the window of the old project unmounts and the
* window of the new one mounts in the same commit (app/project-windows.tsx). The chat dock
@@ -14,6 +14,28 @@ export interface AgentHandover {
settled?: Promise<void>;
/** A model that is still loading; the new dock finishes loading it. */
ready?: Promise<void>;
+ /** How far that load got, and its later progress, for the dock that shows it now. */
+ progress?: LoadProgress;
+}
+
+/** The progress of a model load, shown by one dock at a time. The worker reports to the dock
+ * that started the load; after a switch the dock of the new project follows it instead, from
+ * the last value on, so a download still running neither stalls nor starts over in the view. */
+export interface LoadProgress {
+ readonly latest?: BrowserAiProgress;
+ report(value: BrowserAiProgress): void;
+ /** The dock that shows the progress from now on; the one before stops hearing of it. */
+ follow(listener: (value: BrowserAiProgress) => void): void;
+}
+
+export function loadProgress(): LoadProgress {
+ let latest: BrowserAiProgress | undefined;
+ let listener: ((value: BrowserAiProgress) => void) | undefined;
+ return {
+ get latest() { return latest; },
+ report(value) { latest = value; listener?.(value); },
+ follow(next) { listener = next; if (latest) next(latest); },
+ };
}
let offered: (() => AgentHandover | undefined) | undefined;