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): offer the selection for a misspelled caller question and read "wo" as a question word (K16)

Bernhard Jackiewiczecf8618bd2bc
diff --git a/graph-ui/src/browser-ai/relationship-answer.test.ts b/graph-ui/src/browser-ai/relationship-answer.test.ts
index 3511d96c4..3d5b400ad 100644
--- a/graph-ui/src/browser-ai/relationship-answer.test.ts
+++ b/graph-ui/src/browser-ai/relationship-answer.test.ts
@@ -211,3 +211,95 @@ describe('tolerant caller and callee questions (K16)', () => {
expect(relationshipSuggestion('BaseCommand aufrufe', [jsonbAggEvidence()])).toBeUndefined();
});
});
+
+describe('caller questions with a misspelled selection or a free word order (K16)', () => {
+ const evidence = () => [jsonbAggEvidence()];
+ const listedAnswer = (prompt: string) => relationshipAnswer(prompt, evidence())?.markdown ?? '';
+
+ // The order of the words is free in German: "wo" asks where, it is never the subject.
+ it.each([
+ ['jsonbagg wird wo aufgerufen', 'de'],
+ ['jsonbagg wird wo aufgerufen?', 'de'],
+ ['JSONBAgg wird wo überall aufgerufen', 'de'],
+ ['wo wird jsonbagg aufgerufen', 'de'],
+ ['wo wird jsonbagg überall aufgerufen?', 'de'],
+ ['wo überall wird jsonbagg aufgerufen', 'de'],
+ ['von wo wird jsonbagg aufgerufen', 'de'],
+ ['von wo aus wird jsonbagg aufgerufen', 'de'],
+ ['woher wird jsonbagg aufgerufen', 'de'],
+ ['jsonbagg wird von wo aufgerufen', 'de'],
+ ['jsonbagg wird von wem aufgerufen', 'de'],
+ ['jsonbagg wird aufgerufen von wem?', 'de'],
+ ['wird jsonbagg irgendwo aufgerufen', 'de'],
+ ['wer ruft jsonbagg auf', 'de'],
+ ['Wer ruft JSONBAgg eigentlich auf?', 'de'],
+ ['welche tests rufen jsonbagg auf', 'de'],
+ ['aufrufer von jsonbagg?', 'de'],
+ ['where is jsonbagg called', 'en'],
+ ['where is jsonbagg called from?', 'en'],
+ ['jsonbagg is called by whom?', 'en'],
+ ['jsonbagg is called from where', 'en'],
+ ['who calls jsonbagg', 'en'],
+ ['what calls jsonbagg?', 'en'],
+ ['callers of jsonbagg', 'en'],
+ ['list the callers of JSONBAgg', 'en'],
+ ['who uses jsonbagg', 'en'],
+ ] as const)('lists the callers for %s', (prompt, language) => {
+ const question = relationshipQuestion(prompt);
+ expect(question?.sides).toEqual(['incoming']);
+ expect(question?.language).toBe(language);
+ expect(question?.subject?.toLowerCase()).not.toMatch(/^(?:wo|woher|irgendwo|überall|where|whom)$/);
+ expect(listedAnswer(prompt)).toContain(language === 'de' ? 'Aufrufer von `JSONBAgg` im geladenen Graphen' : 'Callers of `JSONBAgg` in the loaded graph');
+ });
+
+ // A typo in the selected name, up to two edits and in any case: never straight to the model.
+ it.each([
+ ['wer ruft JSONBAg auf', 'de', 'incoming'],
+ ['wer ruft jsonbgg auf?', 'de', 'incoming'],
+ ['wer ruft jsnbagg auf', 'de', 'incoming'],
+ ['wer ruft JSONBAGG auf', 'de', 'listed'],
+ ['wer ruft jsonbaggg auf', 'de', 'incoming'],
+ ['wer ruft jsonbga auf', 'de', 'incoming'],
+ ['wer ruft jsonag auf', 'de', 'incoming'],
+ ['wo wird jsonbag aufgerufen', 'de', 'incoming'],
+ ['jsonbag wird wo aufgerufen', 'de', 'incoming'],
+ ['von wem wird jsnbagg aufgerufen', 'de', 'incoming'],
+ ['aufrufer von jsonbgg', 'de', 'incoming'],
+ ['was ruft jsonbgg auf', 'de', 'outgoing'],
+ ['who calls jsonbag', 'en', 'incoming'],
+ ['callers of jsonbag', 'en', 'incoming'],
+ ['who calls JSONBAg?', 'en', 'incoming'],
+ ['Who calls jsonbagh', 'en', 'incoming'],
+ ['where is jsonbgg called', 'en', 'incoming'],
+ ['what does jsonbag call?', 'en', 'outgoing'],
+ ['callees of jsonbga', 'en', 'outgoing'],
+ ['jsonbag aufrufe?', 'de', 'incoming'],
+ ['jsonbgg calls', 'en', 'outgoing'],
+ ] as const)('answers or suggests the selection for %s', (prompt, language, expected) => {
+ const listed = relationshipAnswer(prompt, evidence());
+ const suggestion = relationshipSuggestion(prompt, evidence());
+ if (expected === 'listed') {
+ expect(listed?.markdown).toContain(language === 'de' ? 'Aufrufer von `JSONBAgg`' : 'Callers of `JSONBAgg`');
+ return;
+ }
+ expect(listed).toBeUndefined();
+ expect(suggestion?.language).toBe(language);
+ expect(suggestion?.markdown).toContain(language === 'de'
+ ? expected === 'incoming' ? 'Meintest du: Aufrufer von `JSONBAgg`?' : 'Meintest du: von `JSONBAgg` aufgerufene Symbole?'
+ : expected === 'incoming' ? 'Did you mean: callers of `JSONBAgg`?' : 'Did you mean: what `JSONBAgg` calls?');
+ // The uncertain part is the name, and the reply says so.
+ if (relationshipQuestion(prompt)) expect(suggestion?.markdown).toContain(language === 'de' ? 'ist nicht genau der Name der Auswahl' : 'is not exactly the name of the selection');
+ // The suggested question lists the selection when chosen.
+ expect(relationshipAnswer(suggestion!.question, evidence())?.markdown).toContain(expected === 'incoming'
+ ? language === 'de' ? 'Aufrufer von `JSONBAgg` im geladenen Graphen' : 'Callers of `JSONBAgg` in the loaded graph'
+ : language === 'de' ? 'Von `JSONBAgg` aufgerufen' : 'Called by `JSONBAgg`');
+ });
+
+ it.each([
+ 'who calls jsonb', 'wer ruft json auf', 'who calls Aggregate', 'who calls OrderableAggMixin', 'Wer ruft BaseCommand auf?',
+ 'who calls test_jsonb_agg', 'wo wird BaseCommand aufgerufen', 'BaseCommand wird wo aufgerufen', 'Who calls JSONBAgg and why?',
+ ])('neither lists nor suggests the selection for %s, which names another symbol or asks why', prompt => {
+ expect(relationshipAnswer(prompt, evidence())).toBeUndefined();
+ expect(relationshipSuggestion(prompt, evidence())).toBeUndefined();
+ });
+});
diff --git a/graph-ui/src/browser-ai/relationship-answer.ts b/graph-ui/src/browser-ai/relationship-answer.ts
index 8d0662f51..2b6c4c86a 100644
--- a/graph-ui/src/browser-ai/relationship-answer.ts
+++ b/graph-ui/src/browser-ai/relationship-answer.ts
@@ -17,6 +17,10 @@ const FILLER = 'eigentlich|denn|überhaupt|genau|alles|actually|really|exactly';
/** Where a pattern says {name}: optional fillers, an optional article and kind noun, the name as typed, an optional kind noun. */
const NAME = String.raw`(?:(?:${FILLER})\s+)*(?:(?:the|a|an|der|die|das|den|dem|des)\s+)?(?:(?:${KIND})\s+)?[@\x60'"]?([A-Za-z_][\w.:/]*)[\x60'"]?(?:\s+(?:${KIND})\b)?`;
const pattern = (source: string) => new RegExp(source.replace('{name}', NAME), 'iu');
+/** One word between the name and the verb ("wird JSONBAgg überall aufgerufen"), umlauts included. */
+const WORD = String.raw`(?:[\p{L}\w]+\s+)?`;
+/** Where a German question asks from where; never its subject. */
+const WHERE = String.raw`(?:wo|woher|von\s+wo(?:\s+aus)?|von\s+wem)`;
/** Phrasings that fix the direction and, where it is written, the subject. */
const PHRASINGS: { side: Side; pattern: RegExp }[] = [
@@ -29,13 +33,16 @@ const PHRASINGS: { side: Side; pattern: RegExp }[] = [
String.raw`\bwhere\s+(?:is|are|does|do)\s+{name}\s+(?:\w+\s+)?(?:called|invoked|used)\b`,
String.raw`\b(?:is|are)\s+{name}\s+(?:called|invoked|used)\s+by\b`,
String.raw`\bwer\s+(?:ruft|rufen|benutzt|benutzen|verwendet|verwenden|nutzt|nutzen)\s+{name}`,
- String.raw`\bwer\s+(?:hat|hatte)\s+{name}\s+(?:\w+\s+)?(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
- String.raw`^\s*{name}\s+(?:wird|werden)\s+von\s+wem\s+(?:\w+\s+)?(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
+ String.raw`\bwer\s+(?:hat|hatte)\s+{name}\s+${WORD}(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
+ // The name first, the question word after it: "JSONBAgg wird wo (überall) aufgerufen", "... aufgerufen von wem".
+ String.raw`^\s*{name}\s+(?:wird|werden)\s+(?:überall\s+)?${WHERE}(?:\s+überall)?\s+${WORD}(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
+ String.raw`^\s*{name}\s+(?:wird|werden)\s+${WORD}(?:aufgerufen|benutzt|verwendet|genutzt)\s+${WHERE}`,
String.raw`\bwelche\w*\s+[\p{L}\w]+\s+(?:rufen|benutzen|verwenden|nutzen)\s+{name}`,
- String.raw`\b(?:wo|woher|von\s+wo)(?:\s+überall)?\s+wird\s+{name}\s+(?:\w+\s+)?(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
+ String.raw`\b${WHERE}(?:\s+überall)?\s+(?:wird|werden)\s+{name}\s+${WORD}(?:aufgerufen|benutzt|verwendet|genutzt)\b`,
String.raw`\bvon\s+wem\s+(?:wird|werden)\s+{name}`,
String.raw`\baufrufer\s+(?:von|für)\s+{name}`,
- String.raw`\bwird\s+{name}\s+(?:\w+\s+)?aufgerufen\b`,
+ String.raw`\bwird\s+{name}\s+${WORD}aufgerufen\b`,
+ String.raw`^\s*{name}\s+(?:is|are|gets|get)\s+(?:called|invoked|used)\s+(?:by\s+(?:whom|who|what|which)|from\s+where|where)\b`,
].map(source => ({ side: 'incoming' as const, pattern: pattern(source) })),
...[
String.raw`\bwhat\s+(?:does|do|did|will|can)\s+{name}\s+(?:call|invoke|use)\b`,
@@ -61,8 +68,8 @@ const SELF = new Set(['it', 'this', 'that', 'these', 'those', 'here', 'es', 'die
'function', 'method', 'class', 'symbol', 'file', 'module', 'selection', 'funktion', 'methode', 'klasse', 'datei', 'modul', 'auswahl']);
/** Words a {name} can land on that are not a subject at all; the match is discarded. */
const NOT_A_NAME = new Set(['in', 'into', 'from', 'at', 'on', 'to', 'for', 'with', 'of', 'and', 'or', 'by', 'anything', 'anyone', 'something', 'someone',
- 'what', 'which', 'who', 'whom', 'all', 'every', 'auf', 'von', 'im', 'aus', 'und', 'oder', 'irgendwo', 'überhaupt', 'etwas', 'jemand',
- 'was', 'wer', 'wem', 'wen', 'alle', 'jede', 'jeder', 'jedes']);
+ 'what', 'which', 'who', 'whom', 'where', 'when', 'all', 'every', 'auf', 'von', 'im', 'aus', 'und', 'oder', 'irgendwo', 'überhaupt', 'überall', 'etwas', 'jemand',
+ 'was', 'wer', 'wem', 'wen', 'wo', 'woher', 'wohin', 'wann', 'alle', 'jede', 'jeder', 'jedes']);
/** Relation words a typo is corrected to. */
const RELATION_WORDS = ['ruft', 'rufen', 'aufgerufen', 'aufrufer', 'benutzt', 'verwendet', 'calls', 'call', 'callers', 'caller', 'called', 'calling',
@@ -74,9 +81,9 @@ const OWN_WORDS = new Set(['falls', 'fall', 'cells', 'cell', 'halls', 'hall', 'w
'calms', 'calmed', 'falling', 'calming', 'rust', 'raft', 'rift', 'luft', 'duft', 'ruht', 'auf', 'aufruf', 'aufrufe',
'aufrufen', 'rufen', 'gerufen', 'cache', 'caches']);
-/** Optimal string alignment distance, stopped once it exceeds one. */
-function withinOneEdit(left: string, right: string): boolean {
- if (Math.abs(left.length - right.length) > 1) return false;
+/** Optimal string alignment distance of at most `limit`. */
+function withinEdits(left: string, right: string, limit: number): boolean {
+ if (Math.abs(left.length - right.length) > limit) return false;
const rows = [Array.from({ length: right.length + 1 }, (_, index) => index)];
for (let i = 1; i <= left.length; i++) {
const row = [i];
@@ -87,8 +94,9 @@ function withinOneEdit(left: string, right: string): boolean {
}
rows.push(row);
}
- return rows[left.length][right.length] <= 1;
+ return rows[left.length][right.length] <= limit;
}
+const withinOneEdit = (left: string, right: string) => withinEdits(left, right, 1);
/** "wer ruf X auf", "who cals X", "aufgerufn": a relation word with one typo reads as the word. */
export function correctRelationWords(prompt: string): string {
@@ -122,10 +130,25 @@ export function relationshipQuestion(typed: string): RelationshipQuestion | unde
...subject ? { subject } : {} };
}
+/** The names the selection goes by: its label, the last part of a path, its root symbols. */
+const selectionNames = (evidence: GalaxyEvidence): string[] => [...new Set([evidence.label, evidence.label.split('/').pop()!, ...evidence.roots.map(root => root.name)])];
+/** Names of the other symbols in the loaded scope: a word that is one of them names that symbol, not a typo. */
+const neighbourNames = (evidence: GalaxyEvidence): Set<string> => new Set([...evidence.relationships.incoming, ...evidence.relationships.outgoing]
+ .flatMap(group => group.files.flatMap(file => file.symbols.map(symbol => symbol.name.toLowerCase()))));
+/** How many typos a name of this length may carry and still mean it: two from seven letters on, one from four. */
+const typoBudget = (name: string) => name.length >= 7 ? 2 : name.length >= 4 ? 1 : 0;
+/** "jsonbgg", "JSONBAg": the selection's name with up to two typos, in any case (K16). An exact
+ * name of another symbol in the scope is that symbol. */
+function nearSelection(word: string, evidence: GalaxyEvidence): boolean {
+ const last = word.toLowerCase().split(/[.:]/).pop()!;
+ if (neighbourNames(evidence).has(last) || NOT_A_NAME.has(last) || SELF.has(last) || RELATION_WORDS.includes(last) || OWN_WORDS.has(last)) return false;
+ return selectionNames(evidence).some(name => { const own = name.toLowerCase(); return own !== last && withinEdits(last, own, typoBudget(own)); });
+}
+
/** A question about another symbol is not about this selection. Without a written
* subject, any symbol-shaped word in the question still counts as naming one. */
function aboutSelection(prompt: string, question: RelationshipQuestion, evidence: GalaxyEvidence): boolean {
- const selected = new Set([evidence.label, evidence.label.split('/').pop()!, ...evidence.roots.map(root => root.name)].map(name => name.toLowerCase()));
+ const selected = new Set(selectionNames(evidence).map(name => name.toLowerCase()));
const matches = (name: string) => selected.has(name.toLowerCase()) || selected.has(name.split(/[.:]/).pop()!.toLowerCase());
if (question.subject) return matches(question.subject);
const snake = prompt.match(/\b[A-Za-z]\w*_\w+\b/g) ?? [];
@@ -163,10 +186,35 @@ function listed(question: RelationshipQuestion, evidence: GalaxyEvidence): strin
/** Words that sound like a relationship question even where no phrasing is recognized. */
const RELATION_HINT = /\b(?:ruft|ruf|rufen|aufruf\w*|aufgerufen|calls?|callers?|called|callees?|invokes?|invoked)\b/iu;
+export interface RelationshipSuggestion { markdown: string; question: string; context: BrowserChatContext; language: 'en' | 'de' }
+
+/** "Did you mean: callers of JSONBAgg?" with the question that lists them. */
+function suggestionFor(sides: readonly Side[], language: 'en' | 'de', evidence: GalaxyEvidence, context: BrowserChatContext, typedName?: string): RelationshipSuggestion {
+ const name = evidence.label;
+ const both = sides.length > 1, outgoing = !both && sides[0] === 'outgoing';
+ const question = language === 'de' ? both ? `Wer ruft ${name} auf und was ruft ${name} auf?` : outgoing ? `Was ruft ${name} auf?` : `Wer ruft ${name} auf?`
+ : both ? `Who calls ${name} and what does ${name} call?` : outgoing ? `What does ${name} call?` : `Who calls ${name}?`;
+ const text = relationshipWords[language];
+ const heading = both ? text.didYouMeanBoth(quote(name)) : text.didYouMean(outgoing ? 'outgoing' : 'incoming', quote(name));
+ return { markdown: `${heading}\n\n_${typedName ? text.uncertainName(quote(typedName)) : text.uncertain}_`, question, context, language };
+}
+
/** A question that sounds like callers or callees of the selection, but not certainly:
- * the listed answer is offered, never given in its place. The selection must be named. */
-export function relationshipSuggestion(typed: string, contexts: readonly BrowserChatContext[]): { markdown: string; question: string; context: BrowserChatContext; language: 'en' | 'de' } | undefined {
- if (REASONING.test(typed) || relationshipQuestion(typed)) return undefined;
+ * the listed answer is offered, never given in its place. The selection must be named,
+ * exactly or with up to two typos ("wer ruft jsonbgg auf?", K16). */
+export function relationshipSuggestion(typed: string, contexts: readonly BrowserChatContext[]): RelationshipSuggestion | undefined {
+ if (REASONING.test(typed)) return undefined;
+ const recognized = relationshipQuestion(typed);
+ if (recognized) {
+ // The question is certain, its subject is not: a misspelled selection is offered, not listed.
+ const subject = recognized.subject;
+ if (!subject) return undefined;
+ for (const context of contexts) {
+ const evidence = readGalaxyEvidence(context.text);
+ if (evidence && !aboutSelection(typed, recognized, evidence) && nearSelection(subject, evidence)) return suggestionFor(recognized.sides, recognized.language, evidence, context, subject);
+ }
+ return undefined;
+ }
const prompt = correctRelationWords(typed);
if (!RELATION_HINT.test(prompt)) return undefined;
const words = prompt.toLowerCase().match(/[\p{L}\w.:/]+/gu) ?? [];
@@ -174,7 +222,7 @@ export function relationshipSuggestion(typed: string, contexts: readonly Browser
const evidence = readGalaxyEvidence(context.text);
if (!evidence) continue;
const names = new Set([evidence.label, ...evidence.roots.map(root => root.name)].map(name => name.toLowerCase()));
- const selection = (word: string) => names.has(word) || names.has(word.split(/[.:]/).pop()!) || SELF.has(word);
+ const selection = (word: string) => names.has(word) || names.has(word.split(/[.:]/).pop()!) || SELF.has(word) || nearSelection(word, evidence);
const at = words.findIndex(selection);
if (at < 0) continue;
// The selection may take several words ("this class"); what follows them is the verb.
@@ -186,11 +234,7 @@ export function relationshipSuggestion(typed: string, contexts: readonly Browser
const before = relation >= 0 && relation < at ? words[relation + 1] : undefined;
const outgoing = !/\b(?:wer|who|aufrufer|callers?|aufrufe)\b/i.test(prompt)
&& (/^(?:ruft|calls?|invokes?)$/.test(words[after] ?? '') || /^(?:in|inside|within|from|made)$/.test(before ?? ''));
- const language = GERMAN.test(prompt) ? 'de' : 'en';
- const name = evidence.label;
- const question = language === 'de' ? outgoing ? `Was ruft ${name} auf?` : `Wer ruft ${name} auf?` : outgoing ? `What does ${name} call?` : `Who calls ${name}?`;
- const text = relationshipWords[language];
- return { markdown: `${text.didYouMean(outgoing ? 'outgoing' : 'incoming', quote(name))}\n\n_${text.uncertain}_`, question, context, language };
+ return suggestionFor([outgoing ? 'outgoing' : 'incoming'], GERMAN.test(prompt) ? 'de' : 'en', evidence, context);
}
return undefined;
}
diff --git a/graph-ui/src/browser-ai/strings.ts b/graph-ui/src/browser-ai/strings.ts
index 78ab503d4..eb02c72a4 100644
--- a/graph-ui/src/browser-ai/strings.ts
+++ b/graph-ui/src/browser-ai/strings.ts
@@ -70,7 +70,9 @@ const englishRelationshipWords = {
stillLoading: 'The scope is still loading; this list can grow.',
listedFromGraph: 'Listed from the indexed graph; not generated by the model.',
didYouMean: (side: 'incoming' | 'outgoing', name: string) => side === 'incoming' ? `Did you mean: callers of ${name}?` : `Did you mean: what ${name} calls?`,
+ didYouMeanBoth: (name: string) => `Did you mean: callers of ${name} and what it calls?`,
uncertain: 'The question was not recognized for certain. Show the list from the indexed graph, or ask the model instead.',
+ uncertainName: (typed: string) => `${typed} is not exactly the name of the selection. Show the list from the indexed graph, or ask the model instead.`,
showList: 'Show the list',
/** Offered under a listed answer or a suggestion, which the model did not write. */
askModel: 'Ask the model',
@@ -116,7 +118,9 @@ export const relationshipWords: { en: RelationshipWords; de: RelationshipWords }
stillLoading: 'Der Ausschnitt lädt noch; die Liste kann wachsen.',
listedFromGraph: 'Aus dem indizierten Graphen gelistet, nicht vom Modell erzeugt.',
didYouMean: (side: 'incoming' | 'outgoing', name: string) => side === 'incoming' ? `Meintest du: Aufrufer von ${name}?` : `Meintest du: von ${name} aufgerufene Symbole?`,
+ didYouMeanBoth: (name: string) => `Meintest du: Aufrufer von ${name} und was ${name} aufruft?`,
uncertain: 'Die Frage wurde nicht sicher erkannt. Zeige die Liste aus dem indizierten Graphen oder frage stattdessen das Modell.',
+ uncertainName: (typed: string) => `${typed} ist nicht genau der Name der Auswahl. Zeige die Liste aus dem indizierten Graphen oder frage stattdessen das Modell.`,
showList: 'Liste anzeigen',
askModel: 'Modell fragen',
},