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): start a returning file or selection fresh in the chat prompt

Bernhard Jackiewiczb7ee993af6a8
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
index 03148fae3..b5e5b524e 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.test.tsx
@@ -991,6 +991,28 @@ describe('graph answers and answer limits', () => {
expect(runtime.chat.mock.calls[2][0].map(message => message.content).join('\n')).not.toContain('flake8');
});
+ it('starts a file or selection fresh when the reader comes back to it, as the divider says (K17)', async () => {
+ const { props, runtime } = fixture();
+ runtime.chat.mockResolvedValueOnce('This runs the flake8 linter on Python files.');
+ const workflow = reader('name: New contributor message', 'file', '.github/workflows/new_contributor_pr.yml');
+ await render({ ...props, selectionScope: 'django-demo:explore', readerContext: workflow }); await click('Download & load');
+ await type('was kannst du mir über dieses File sagen'); await click('Send ↑');
+ await render({ ...props, selectionScope: 'django-demo:galaxy', proactiveSelection: jsonbAggEvidence() });
+ await type('What does JSONBAgg do?'); await click('Send ↑');
+ await render({ ...props, selectionScope: 'django-demo:explore', readerContext: workflow });
+ await type('Und was noch?'); await click('Send ↑');
+ const request = runtime.chat.mock.calls[2][0];
+ expect(request.filter(message => message.role === 'assistant')).toHaveLength(0);
+ expect(request.map(message => message.content).join('\n')).not.toContain('flake8');
+ expect(request.filter(message => message.role === 'user').map(message => message.content.split('\n').at(-1))).toEqual(['Und was noch?']);
+ expect([...container.querySelectorAll('.cbm-chat-topic-break')].map(item => item.textContent)).toEqual([
+ 'New topic: JSONBAgg. Earlier messages are not sent with these questions.',
+ 'New topic: .github/workflows/new_contributor_pr.yml. Earlier messages are not sent with these questions.']);
+ // Within the returned topic the conversation goes on.
+ await type('Welche Jobs?'); await click('Send ↑');
+ expect(runtime.chat.mock.calls[3][0].filter(message => message.role === 'user').map(message => message.content.split('\n').at(-1))).toEqual(['Und was noch?', 'Welche Jobs?']);
+ });
+
it('marks names in an answer that are not in the file or the graph facts (K12)', async () => {
const { props, runtime } = fixture();
runtime.chat.mockResolvedValueOnce('Dieses Script ruft `flake8` mit `subprocess.run` auf und prüft `pull_request_target`.');
diff --git a/graph-ui/src/browser-ai/BrowserChatDock.tsx b/graph-ui/src/browser-ai/BrowserChatDock.tsx
index b88af05ec..8031e1e59 100644
--- a/graph-ui/src/browser-ai/BrowserChatDock.tsx
+++ b/graph-ui/src/browser-ai/BrowserChatDock.tsx
@@ -15,7 +15,7 @@ import { isGpuRuntimeFailure, BrowserRuntimeFatalError } from './runtime-fault';
import ChatMarkdown from './ChatMarkdown';
import AgentSettingsDialog from './AgentSettingsDialog';
import { useChatHistory } from './use-chat-history';
-import { chatTopic, followedTopic, missingContextAnswer } from './chat-context';
+import { chatTopic, followedTopic, missingContextAnswer, topicHistory } from './chat-context';
import './browser-chat.css';
export type { BrowserChatAttachment, BrowserChatContext, BrowserChatReaderContext, BrowserChatSource } from './chat-model';
@@ -530,9 +530,9 @@ export default function BrowserChatDock({ proactiveSelection, selectionScope = "
consume();
return;
}
- // Answers about another file or selection stay out: an earlier wrong answer must not
- // become evidence for this one (K17).
- const earlier = (ask ? turns.filter(item => item.id !== retry.id) : turns).filter(item => item.topic?.key === topic?.key);
+ // Answers about another file or selection stay out, and so do those from before the
+ // last change of topic: an earlier wrong answer must not become evidence for this one (K17).
+ const earlier = topicHistory(ask ? turns.filter(item => item.id !== retry.id) : turns, topic);
let history: ChatTurn[] = earlier;
const makeRequest = () => buildChatMessages(history, prompt, source, extra, reader, currentGraph, packet ? formatExplanationEvidence(packet) : undefined);
const queued = { cancelled: false }; manualRequest.current = queued;
diff --git a/graph-ui/src/browser-ai/chat-context.test.ts b/graph-ui/src/browser-ai/chat-context.test.ts
index d20b72bb5..a8793cf1e 100644
--- a/graph-ui/src/browser-ai/chat-context.test.ts
+++ b/graph-ui/src/browser-ai/chat-context.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { chatTopic, followedTopic, missingContextAnswer, questionLanguage } from './chat-context';
+import { chatTopic, followedTopic, missingContextAnswer, questionLanguage, topicHistory } from './chat-context';
import type { BrowserChatTurn } from './chat-model';
import { jsonbAggEvidence } from './galaxy-evidence.fixture';
@@ -53,3 +53,22 @@ describe('what a question is about', () => {
expect(followedTopic([turn({ key: '{damaged', label: 'x', kind: 'attachment' })], 'p:galaxy')).toBeUndefined();
});
});
+
+describe('which earlier turns a question carries (K17)', () => {
+ const file = chatTopic('p:explore', { reader: { project: 'p', path: 'a.yml', status: 'ready', source: { ...attachment, path: 'a.yml', kind: 'file' } } })!;
+ const selected = chatTopic('p:galaxy', { graph: jsonbAggEvidence() })!;
+ it('carries only the turns since the topic last changed, as the divider says', () => {
+ const [first, second] = [turn(file), turn(file)];
+ expect(topicHistory([first, second], file)).toEqual([first, second]);
+ expect(topicHistory([first, turn(selected)], file)).toEqual([]);
+ // Coming back to a file after another topic starts it fresh: the old wrong answer stays out.
+ expect(topicHistory([first, turn(selected), second], file)).toEqual([second]);
+ expect(topicHistory([turn(selected), first, second], file)).toEqual([first, second]);
+ });
+
+ it('steps over replies without a topic, which never start one', () => {
+ const [first, second] = [turn(file), turn(file)];
+ expect(topicHistory([first, turn(undefined, { answeredFrom: 'local' }), second], file)).toEqual([first, second]);
+ expect(topicHistory([first], undefined)).toEqual([]);
+ });
+});
diff --git a/graph-ui/src/browser-ai/chat-context.ts b/graph-ui/src/browser-ai/chat-context.ts
index ee5e3c634..253e36999 100644
--- a/graph-ui/src/browser-ai/chat-context.ts
+++ b/graph-ui/src/browser-ai/chat-context.ts
@@ -21,6 +21,22 @@ export function chatTopic(scope: string, sources: { reader?: BrowserChatReaderCo
return undefined;
}
+/** The earlier turns a question about `topic` carries: those since the topic last changed.
+ * A file or selection the reader comes back to starts fresh, as the divider above it says,
+ * so an earlier wrong answer about it is not sent again (K17). Replies without a topic
+ * (no context) neither belong to nor end a topic. */
+export function topicHistory<T extends BrowserChatTurn>(turns: readonly T[], topic: ChatTopic | undefined): T[] {
+ if (!topic) return [];
+ const run: T[] = [];
+ for (let index = turns.length - 1; index >= 0; index--) {
+ const turn = turns[index];
+ if (!turn.topic) continue;
+ if (turn.topic.key !== topic.key) break;
+ run.unshift(turn);
+ }
+ return run;
+}
+
/** A question without its own context follows up on explicitly attached code or context
* in the same view. A selection or file that is gone leaves nothing to follow up on. */
export function followedTopic(turns: readonly BrowserChatTurn[], scope: string): ChatTopic | undefined {