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): compare named identifiers with the given text case-insensitively (C3)

Bernhard Jackiewicz26bfbf5865b5
diff --git a/graph-ui/src/browser-ai/explanation-response.test.ts b/graph-ui/src/browser-ai/explanation-response.test.ts
index 2ed57f95c..b89e23e34 100644
--- a/graph-ui/src/browser-ai/explanation-response.test.ts
+++ b/graph-ui/src/browser-ai/explanation-response.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { citedInterpretation, explanationMessages, formatExplanationEvidence, parseExplanationResponse } from './explanation-response';
+import { citedInterpretation, explanationMessages, explanationSentence, formatExplanationEvidence, namesNotIn, parseExplanationResponse } from './explanation-response';
import { prepareExplanationContext } from './explanation-context';
import { jsonbAggEvidence } from './galaxy-evidence.fixture';
const packet = { label: 'file.c', evidence: [{ id: 'E1', text: 'u.i = (uintptr_t)CBM_NOT_FOUND;', source: 'code' as const, location: { path: 'file.c', startLine: 103, startColumn: 5, endLine: 103, endColumn: 36, sourceVersion: 'v1' } }], limitations: [], fallback: 'Source excerpt', characterCount: 50 };
@@ -85,6 +85,30 @@ describe('generated explanation response', () => {
});
});
+describe('names an answer was not given (C3)', () => {
+ const source = 'class JSONBAgg(OrderableAggMixin, Aggregate):\n function = "JSONB_AGG"\n template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)"\n'
+ + 'def test_jsonb_agg_distinct_false(self):\ndef test_jsonb_agg_integerfield_order_by(self):\ndef test_jsonb_agg_key_index_transforms(self):\n';
+ const config = 'repos:\n - repo: https://github.com/PyCQA/flake8\n hooks:\n - id: flake8\n args: ["--rst-literal-block"]\n';
+
+ it('finds a name of the source in another case, as identifier words', () => {
+ expect(namesNotIn('It supports `DISTINCT`, `EXPRESSIONS` and `ORDER_BY` in the template.', source)).toEqual([]);
+ expect(namesNotIn('The `Flake8` hook of `PycQA` passes the `lITERAL` block option.', config)).toEqual([]);
+ expect(namesNotIn('`jsonbagg` is an aggregate.', source)).toEqual([]);
+ });
+
+ it('still marks mangled and invented names', () => {
+ expect(namesNotIn('`Jsonb_agg_distinct_false` and test_jsonb_agg_integer_field_order_by and `test_jsonb_agg_key_index_transformations`', source))
+ .toEqual(['Jsonb_agg_distinct_false', 'test_jsonb_agg_key_index_transformations', 'test_jsonb_agg_integer_field_order_by']);
+ expect(namesNotIn('It runs `subprocess.run` with `docs/*.txt$`.', config)).toEqual(['subprocess.run', 'docs/*.txt$']);
+ });
+
+ it('applies the same comparison to the sentence of an automatic explanation', () => {
+ const packet = { label: 'JSONBAgg', evidence: [{ id: 'source-1', text: source, source: 'code' as const }], limitations: [], fallback: '', characterCount: 0 };
+ expect(explanationSentence('`JSONBAGG` sets the `distinct` and `ORDER_BY` parts of its template.', packet)).toEqual({ sentence: '`JSONBAGG` sets the `distinct` and `ORDER_BY` parts of its template.' });
+ expect(explanationSentence('`Jsonb_agg_distinct_false` checks it.', packet)).toEqual({ dropped: 'unsupported' });
+ });
+});
+
describe('evidence sections in the prompt', () => {
it('heads sections with meaningful words, never numbered ids the model could echo as "Graph 1" (K4)', () => {
const reader = { project: 'django-demo', path: 'django/contrib/postgres/aggregates/general.py', status: 'ready' as const, source: { id: 'file:1', kind: 'file' as const,
diff --git a/graph-ui/src/browser-ai/explanation-response.ts b/graph-ui/src/browser-ai/explanation-response.ts
index bb6f1e22c..caac61425 100644
--- a/graph-ui/src/browser-ai/explanation-response.ts
+++ b/graph-ui/src/browser-ai/explanation-response.ts
@@ -58,11 +58,22 @@ const UNSUPPORTED_CLAIM = /\b(?:returns?|returning|list of|lists of|integers?|st
/** Identifier-shaped words: snake_case, camelCase, PascalCase with an inner capital, or letters with digits. */
const IDENTIFIER = /\b(?:[A-Za-z]+_\w+|[a-z]+[A-Z]\w*|[A-Z][a-z0-9]+[A-Z]\w*|[A-Za-z]+\d+\w*)\b/g;
+/** Whether `given` holds a name: a single identifier as one of its identifier words in any
+ * case ("DISTINCT" for "%(distinct)s", "Flake8" for "flake8"), anything longer as written,
+ * case aside. A mangled name ("jsonb_agg_distinct_false" for "test_jsonb_agg_distinct_false")
+ * is no identifier word of the text and stays unknown (C3). */
+function nameCheck(given: string): (name: string) => boolean {
+ const words = new Set((given.match(/[A-Za-z_][A-Za-z0-9_]*/g) ?? []).map(word => word.toLowerCase()));
+ const text = given.toLowerCase();
+ return name => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name) ? words.has(name.toLowerCase()) : text.includes(name.toLowerCase());
+}
+
/** Names an answer uses that the request it was given does not contain: written in
* backticks or shaped like an identifier (flake8, json_agg_helper). Shown under the answer (K12). */
export function namesNotIn(answer: string, given: string): string[] {
const named = [...answer.matchAll(/`([^`\n]+)`/g)].map(match => match[1].trim()).concat(answer.replace(/`[^`]*`/g, ' ').match(IDENTIFIER) ?? []);
- return [...new Set(named.filter(name => name && !given.includes(name)))];
+ const known = nameCheck(given);
+ return [...new Set(named.filter(name => name && !known(name)))];
}
/** The model's part of an automatic explanation: its first sentence (two for reader code),
@@ -82,12 +93,12 @@ export function explanationSentence(output: string, packet: PreparedExplanationC
const keep = mode === 'code' ? 2 : 1;
const sentence = (ends.length >= keep ? text.slice(0, ends[keep - 1]) : ends.length ? text.slice(0, ends.at(-1)) : text).trim();
// Everything the model was given counts: the evidence, its headings and the file kind.
- const evidence = [packet.label, ...packet.evidence.map(item => item.text), given].join('\n');
+ const known = nameCheck([packet.label, ...packet.evidence.map(item => item.text), given].join('\n'));
const named = [...sentence.matchAll(/`([^`]+)`/g)].map(match => match[1]).concat(sentence.replace(/`[^`]*`/g, ' ').match(IDENTIFIER) ?? []);
// Without source every type or input/output claim is a guess; with source only one the code itself shows.
const code = packet.evidence.filter(item => item.source === 'code').map(item => item.text).join('\n').toLowerCase();
const claims = [...sentence.matchAll(new RegExp(UNSUPPORTED_CLAIM.source, 'gi'))].map(match => match[0].toLowerCase().replace(/(?:s|ing)$/, ''));
- if (named.some(name => !evidence.includes(name)) || (mode !== 'code' && claims.some(claim => mode === 'graph' || !code.includes(claim.split(' ')[0])))) return { dropped: 'unsupported' };
+ if (named.some(name => !known(name)) ||(mode !== 'code' && claims.some(claim => mode === 'graph' || !code.includes(claim.split(' ')[0])))) return { dropped: 'unsupported' };
return { sentence };
}