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): explain a cut answer and offer the way to a longer one

Bernhard Jackiewicz7bc0e70d3a81
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
index 24852c865..324be00d2 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
@@ -945,13 +945,51 @@ describe('graph answers and answer limits', () => {
await render({ ...props, proactiveSelection: longCallers() }); await click('Download & load');
await type('Explain this class'); await click('Send ↑');
const notes = [...container.querySelectorAll('.cbm-chat-answer-note')].map(item => item.textContent);
- expect(notes).toContain('Shortened (token limit)');
+ expect(container.querySelector('.cbm-chat-limit-note > summary')?.textContent).toBe('Token limit reached: the answer was cut short');
const capacity = notes.map(note => /^55 nodes \/ 40 edges: too large for the local Qwen2\.5 Coder 0\.5B model; showing (\d+)$/.exec(note ?? '')).find(Boolean);
expect(Number(capacity?.[1])).toBeGreaterThan(0);
expect(Number(capacity?.[1])).toBeLessThan(24);
expect(runtime.chat.mock.calls[0][0].at(-1)!.content).toMatch(/\+\d+ more/);
});
+ it('explains a cut answer: current limits, a way to the output limit and larger models with their download (K1)', async () => {
+ window.localStorage.setItem(AGENT_PREFERENCES_KEY, JSON.stringify({ version: 1, preferences: { modelId: BROWSER_MODELS[0].id, automatic: true,
+ limits: { [BROWSER_MODELS[0].id]: { inputTokens: 2048, outputTokens: 256 } } } }));
+ const { props, runtime } = fixture();
+ runtime.chat.mockImplementationOnce(async (_messages, _onToken, options) => {
+ options?.onComplete?.({ stopReason: 'length' }); return 'A long answer that';
+ });
+ await render({ ...props, attachment: selection }); await click('Download & load');
+ await type('Explain this part'); await click('Send ↑');
+ const note = container.querySelector<HTMLDetailsElement>('.cbm-chat-turn details.cbm-chat-limit-note');
+ expect(note?.querySelector('summary')?.textContent).toBe('Token limit reached: the answer was cut short');
+ expect(note?.textContent).toContain('all 256 output tokens');
+ expect(note?.textContent).toContain('2,048 tokens');
+ expect(note?.textContent).toContain('up to 512 tokens');
+ for (const [name, size] of [['Qwen3 0.6B', '579 MB'], ['LFM2.5 1.2B', '764 MB'], ['Qwen3.5 2B', '1.40 GB']]) expect(note?.textContent).toContain(`${name} · ${size} download`);
+ expect(note?.textContent).not.toContain(`${BROWSER_MODELS[0].displayName} ·`);
+ expect(document.querySelector('dialog')).toBeNull();
+ await act(async () => button('Change the output limit').click());
+ expect(document.querySelector('dialog')).not.toBeNull();
+ expect(document.activeElement?.id).toBe('cbm-chat-output-tokens');
+ });
+
+ it('says why an automatic explanation stops early and that a question may answer longer (K1)', async () => {
+ vi.useFakeTimers();
+ try {
+ const { props, runtime } = fixture();
+ runtime.chat.mockImplementationOnce(async (_messages, _onToken, options) => {
+ options?.onComplete?.({ stopReason: 'length' }); return 'Defines the aggregate and lists the tests that';
+ });
+ await render({ ...props, proactive: true, proactiveSelection: jsonbAggEvidence() }); await click('Download & load');
+ await act(async () => { await vi.advanceTimersByTimeAsync(650); });
+ const note = container.querySelector<HTMLDetailsElement>('.cbm-chat-explanation details.cbm-chat-limit-note');
+ expect(note?.querySelector('summary')?.textContent).toBe('Token limit reached: the answer was cut short');
+ expect(note?.textContent).toContain('Automatic explanations stop after 128 output tokens');
+ expect(note?.textContent).toContain('up to 512 output tokens');
+ } finally { vi.useRealTimers(); }
+ });
+
it('does not blame the model when only the snapshot bounds the names', async () => {
const { props, runtime } = fixture();
const many = Array.from({ length: 40 }, (_, index) => ({ source: 2000 + index, target: 32360, type: 'CALLS' }));
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.tsx b/graph-ui/src/browser-ai/BrowserChatDock.tsx
index a94c7dc92..bfe61d165 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.tsx
@@ -1,13 +1,13 @@
import { useEffect, useMemo, useRef, useState, type FormEvent, type JSX, type ReactNode } from 'react';
import type { BrowserAiProgress } from './browser-ai-controller';
import { createBrowserChatRuntime, type BrowserChatRuntime } from './browser-ai-runtime';
-import { BROWSER_MODELS, removeBrowserModelCache } from './model-policy';
+import { BROWSER_MODELS, removeBrowserModelCache, type BrowserModel } from './model-policy';
import { buildChatMessages, selectionLocation, snapshotAttachment, snapshotReaderContext, trimChatHistory, type BrowserChatAttachment, type BrowserChatContext, type BrowserChatReaderContext, type BrowserChatTurn } from './chat-model';
import { explanationInput, EXPLANATION_DELAY_MS, type ExplanationInput } from './proactive-selection';
import { prepareExplanationContext, type PreparedExplanationContext } from './explanation-context';
import { AUTO_INPUT_TOKENS, AUTO_OUTPUT_TOKENS, citedInterpretation, parseExplanationResponse, explanationMessages, formatExplanationEvidence } from './explanation-response';
import { relationshipAnswer } from './relationship-answer';
-import { clampTokenLimits, tokenLimitBounds, tokenLimitsFor, useAgentPreferences } from './agent-preferences';
+import { clampTokenLimits, tokenLimitBounds, tokenLimitsFor, useAgentPreferences, type TokenLimits } from './agent-preferences';
import { browserChatText } from './strings';
import { isGpuRuntimeFailure, BrowserRuntimeFatalError } from './runtime-fault';
import ChatMarkdown from './ChatMarkdown';
@@ -42,7 +42,7 @@ export interface BrowserChatDockProps {
type Phase = 'off' | 'preparing' | 'ready' | 'counting' | 'generating' | 'removing';
type ChatTurn = BrowserChatTurn & { evidence?: PreparedExplanationContext };
-type Explanation = { key: string; label: string; answer: string; status: string; error?: string; packet?: PreparedExplanationContext; citation?: ReturnType<typeof citedInterpretation>; mode?: 'interpretation'; shortened?: boolean; evidence?: string };
+type Explanation = { key: string; label: string; answer: string; status: string; error?: string; packet?: PreparedExplanationContext; citation?: ReturnType<typeof citedInterpretation>; mode?: 'interpretation'; shortened?: boolean; limit?: TokenLimits; evidence?: string };
/** Finished explanations per selection, so returning to one does not run the model again. */
const EXPLANATION_CACHE_SIZE = 32;
const initialModel = BROWSER_MODELS.find(model => model.availability === 'available')!;
@@ -64,11 +64,26 @@ function SourceDisclosure({ children, title }: { children?: ReactNode; title?: s
</details> : <span className="cbm-chat-speaker" title={title}>Agent</span>;
}
+/** Where an answer stopped and what can be changed: the limits it ran into, the way to
+ * the output limit and the larger models with their download. */
+interface LimitNote { limit: TokenLimits; automatic?: boolean; chatOutput: number; model: BrowserModel; onChangeOutput: () => void }
+function TokenLimitNote({ limit, automatic, chatOutput, model, onChangeOutput }: LimitNote): JSX.Element {
+ const larger = BROWSER_MODELS.filter(candidate => candidate.availability === 'available' && candidate.bytes > model.bytes);
+ return <details className="cbm-chat-limit-note">
+ <summary>{browserChatText.shortened}</summary>
+ <p>{automatic ? browserChatText.limitAutomatic(limit.outputTokens, chatOutput) : browserChatText.limitReached(limit.inputTokens, limit.outputTokens)}</p>
+ <p>{browserChatText.outputRoom(chatOutput, model.maxOutputTokens)}</p>
+ <button type="button" onClick={onChangeOutput}>{browserChatText.changeOutputLimit}</button>
+ {larger.length > 0 && <><p>{browserChatText.largerModels}</p>
+ <ul>{larger.map(candidate => <li key={candidate.id}>{browserChatText.modelDownload(candidate.displayName, sizeLabel(candidate.bytes))}</li>)}</ul></>}
+ </details>;
+}
+
/** How an answer was bounded: cut at the output limit, scope too large, history left out. */
-function AnswerNotes({ shortened, packet, model, historyOmitted }: { shortened?: boolean; packet?: PreparedExplanationContext; model: string; historyOmitted?: number }): JSX.Element {
+function AnswerNotes({ shortened, packet, model, historyOmitted }: { shortened?: LimitNote; packet?: PreparedExplanationContext; model: string; historyOmitted?: number }): JSX.Element {
const capacity = packet?.capacity;
return <>
- {shortened && <small className="cbm-chat-answer-note">{browserChatText.shortened}</small>}
+ {shortened && <TokenLimitNote {...shortened} />}
{capacity && <small className="cbm-chat-answer-note">{browserChatText.capacity(capacity.nodes, capacity.edges, model, capacity.shown)}</small>}
{!!historyOmitted && <small className="cbm-chat-answer-note">{browserChatText.historyTrimmed(historyOmitted)}</small>}
</>;
@@ -139,6 +154,8 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
const [progress, setProgress] = useState<BrowserAiProgress>();
const [downloaded, setDownloaded] = useState<Set<string>>(() => new Set());
const [settingsOpen, setSettingsOpen] = useState(false);
+ /** The token-limit note opens the configuration at its output limit. */
+ const focusOutputLimit = useRef(false);
const [newOutput, setNewOutput] = useState(false);
const [stopping, setStopping] = useState(false);
const [selectedContext, setSelectedContext] = useState<BrowserChatContext[]>([]);
@@ -206,6 +223,16 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
useEffect(() => {
if (settingsSeen.current !== settingsRequest) { settingsSeen.current = settingsRequest; setSettingsOpen(true); }
}, [settingsRequest]);
+ useEffect(() => {
+ // Runs after the dialog's own showModal, which focuses its first control.
+ if (!settingsOpen || !focusOutputLimit.current) return;
+ focusOutputLimit.current = false;
+ const field = document.getElementById('cbm-chat-output-tokens');
+ if (field instanceof HTMLInputElement) { field.focus(); field.select(); field.scrollIntoView?.({ block: 'center' }); }
+ }, [settingsOpen]);
+ const openOutputLimit = (): void => { focusOutputLimit.current = true; setSettingsOpen(true); };
+ const limitNote = (shortened: boolean | undefined, limit: TokenLimits | undefined, automatic?: boolean): LimitNote | undefined => shortened
+ ? { limit: limit ?? (automatic ? { inputTokens: autoInput, outputTokens: autoOutput } : limits), automatic, chatOutput: limits.outputTokens, model, onChangeOutput: openOutputLimit } : undefined;
useEffect(() => {
const run = autoRun.current;
if (run && !run.cancelled && (run.key !== selected?.key || !automatic || !proactive)) {
@@ -276,7 +303,7 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
if (!followExplanation.current) setNewExplanation(true);
if (result.status === 'generated') {
const complete: Explanation = { key: snapshot.key, label: snapshot.label, answer: result.markdown, status: 'complete', mode: 'interpretation', packet, citation: result.citation,
- ...shortened ? { shortened } : {}, ...snapshot.evidence ? { evidence: snapshot.evidence } : {} };
+ ...shortened ? { shortened, limit: { inputTokens: autoInput, outputTokens: autoOutput } } : {}, ...snapshot.evidence ? { evidence: snapshot.evidence } : {} };
remember(complete); setExplanation(complete);
} else setExplanation({ key: snapshot.key, label: snapshot.label, answer: '', status: 'error', packet, error: result.reason });
}
@@ -454,7 +481,7 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
}, { maxOutputTokens: limits.outputTokens, onComplete: ({ stopReason }) => { shortened = stopReason === 'length'; } });
if (epoch.current !== ticket) return;
setTurns(previous => previous.map(item => item.id === id ? { ...item, answer: stopRequested.current ? item.answer : output, status: stopRequested.current ? 'stopped' : 'complete',
- ...shortened && !stopRequested.current ? { shortened } : {} } : item));
+ ...shortened && !stopRequested.current ? { shortened, limit: { inputTokens: limits.inputTokens, outputTokens: limits.outputTokens } } : {} } : item));
} catch (failure) {
if (epoch.current !== ticket) return;
if (isGpuRuntimeFailure(failure)) { invalidateRuntime(failure); return; }
@@ -551,7 +578,7 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
</SourceDisclosure>
{explanation?.key === selected.key ? <>
<ChatMarkdown text={explanation.answer || (explanation.status === 'generating' ? 'Explaining selection…' : explanation.status === 'stopped' ? 'Explanation stopped.' : '')} />
- {explanation.status !== 'generating' && <AnswerNotes shortened={explanation.shortened} packet={explanation.packet} model={model.displayName} />}
+ {explanation.status !== 'generating' && <AnswerNotes shortened={limitNote(explanation.shortened, explanation.limit, true)} packet={explanation.packet} model={model.displayName} />}
{explanation.error && <p className="cbm-chat-turn-error" role="alert">{explanation.error}</p>}
{explanation.status !== 'generating' && <button type="button" className="cbm-chat-retry" disabled={phase !== 'ready' || !!selected.waiting} onClick={() => {
lastAttempt.current = undefined; explanations.current.delete(selected.key); setRetryExplanation(value => value + 1);
@@ -572,7 +599,7 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
</> : null}
</SourceDisclosure><div className="cbm-chat-answer-text"><ChatMarkdown text={turn.answer || (turn.status === 'generating' ? 'Thinking…' : turn.status === 'stopped' ? 'Stopped before an answer.' : '')} /></div>
{turn.status === 'stopped' && turn.answer && <small>Stopped · partial answer</small>}
- {turn.status !== 'generating' && turn.answeredFrom !== 'graph' && <AnswerNotes shortened={turn.shortened} packet={turn.evidence} model={BROWSER_MODELS.find(candidate => candidate.id === turn.modelId)?.displayName ?? turn.modelId} historyOmitted={turn.historyOmitted} />}
+ {turn.status !== 'generating' && turn.answeredFrom !== 'graph' && <AnswerNotes shortened={limitNote(turn.shortened, turn.limit)} packet={turn.evidence} model={BROWSER_MODELS.find(candidate => candidate.id === turn.modelId)?.displayName ?? turn.modelId} historyOmitted={turn.historyOmitted} />}
{turn.status === 'error' && <p className="cbm-chat-turn-error" role="alert">{turn.error}</p>}
{index === turns.length - 1 && turn.status !== 'generating' && <button type="button" className="cbm-chat-retry" disabled={phase !== 'ready'} onClick={() => { void send(turn); }}>{turn.answeredFrom === 'graph' ? browserChatText.askModel : 'Retry'}</button>}
</div>
diff --git a/graph-ui/src/browser-ai/browser-chat.css b/graph-ui/src/browser-ai/browser-chat.css
index 2c2370adf..9d6470594 100644
--- a/graph-ui/src/browser-ai/browser-chat.css
+++ b/graph-ui/src/browser-ai/browser-chat.css
@@ -120,3 +120,8 @@
.cbm-chat-evidence-note { font-size: 11px; color: var(--chat-muted); margin: 8px 0 !important; }
.cbm-chat-evidence-excerpt { max-height: 150px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px; border: 1px solid var(--chat-border); border-radius: 6px; font-size: 11px; }
.cbm-chat-dock .cbm-chat-answer-note { display: block; margin-top: 7px; color: #d6bf81; font-size: 11px; overflow-wrap: anywhere; }
+.cbm-chat-limit-note { margin-top: 7px; font-size: 11px; color: var(--chat-muted); overflow-wrap: anywhere; }
+.cbm-chat-limit-note > summary { width: fit-content; color: #d6bf81; cursor: pointer; }
+.cbm-chat-limit-note > p { margin: 6px 0 !important; }
+.cbm-chat-limit-note > ul { margin: 4px 0 0; padding-left: 18px; }
+.cbm-chat-dock .cbm-chat-limit-note > button { margin: 2px 0 4px; padding: 3px 8px; font-size: 11px; }
diff --git a/graph-ui/src/browser-ai/chat-model.ts b/graph-ui/src/browser-ai/chat-model.ts
index ff3104498..72533d534 100644
--- a/graph-ui/src/browser-ai/chat-model.ts
+++ b/graph-ui/src/browser-ai/chat-model.ts
@@ -39,6 +39,8 @@ export interface BrowserChatTurn {
error?: string;
/** The answer ended at the output token limit. */
shortened?: boolean;
+ /** The limits that answer ran into. */
+ limit?: { inputTokens: number; outputTokens: number };
/** Listed from the loaded graph without the model. */
answeredFrom?: 'graph';
/** Earlier turns left out of this request to fit the input limit. */
diff --git a/graph-ui/src/browser-ai/strings.ts b/graph-ui/src/browser-ai/strings.ts
index 1005e1347..82e9afcb8 100644
--- a/graph-ui/src/browser-ai/strings.ts
+++ b/graph-ui/src/browser-ai/strings.ts
@@ -114,8 +114,17 @@ export const relationshipWords: { en: RelationshipWords; de: RelationshipWords }
};
/** Notes of the local chat dock about how an answer was produced or bounded. */
+const tokens = (value: number) => value.toLocaleString('en-US');
export const browserChatText = {
- shortened: 'Shortened (token limit)',
+ shortened: 'Token limit reached: the answer was cut short',
+ /** What the expanded token-limit note says, with the limits the answer ran into. */
+ limitReached: (input: number, output: number) => `This answer used all ${tokens(output)} output tokens it was allowed. The input limit is ${tokens(input)} tokens for the question, its source and earlier messages.`,
+ limitAutomatic: (automatic: number, output: number) => `Automatic explanations stop after ${tokens(automatic)} output tokens so they stay short. A question in the chat may answer with up to ${tokens(output)} output tokens.`,
+ outputRoom: (output: number, max: number) => output < max ? `You can raise the output limit up to ${tokens(max)} tokens in the agent configuration.`
+ : `The output limit is at the maximum of ${tokens(max)} tokens for this model.`,
+ changeOutputLimit: 'Change the output limit',
+ largerModels: 'A larger model may stay closer to the question. Each needs a one-time download, and its memory use is higher than the download:',
+ modelDownload: (name: string, size: string) => `${name} · ${size} download`,
/** Offered under a listed answer, which the model did not write. */
askModel: 'Ask the model',
capacity: (nodes: number, edges: number, model: string, shown: number) =>