GitNexus/gitnexus/test/unit/text-generator.test.ts
Gergő Magyar a0586d967f
feat(embeddings): preserve AST chunk coverage (#3817)
* feat(embeddings): preserve AST chunk coverage

Index long callable tails without duplicating class bodies. Preserve unchanged input reuse and include local model identity in cache checks.

Refs #702

* fix(embeddings): clean up benchmark failures

Protect fixture setup and model initialization with cleanup. Remove
fixtures even when sidecar disposal fails, and cover failure paths.
Clarify the one-time refresh for legacy non-git indexes.

Refs #3817

* fix(embeddings): address projection regressions

Preserve Kotlin/C++ class declarations, strip property bodies, and
keep fallback File nodes out of source hydration. Bump the class
projection revision so stale cached inputs regenerate.

Keep clone fixtures byte-identical under Windows Git settings.
Run paired model benchmarks directly instead of adding skipped CI tests.

Refs #3817

Note: pre-existing failures in setup.test.ts, setup-skills.test.ts,
and auto-sync.test.ts are not addressed by this PR.

* fix(embeddings): preserve empty declarations

Empty direct-member declarations have no member start offset. Keep
the whole declaration and invalidate v2 class projections so corrected
inputs regenerate. Clarify named lookup and single-candidate fallback.

Refs #3817

* chore(bench): standardize chunking artifacts

Move the paired benchmark and record measured baselines under bench/.
Remove the implementation plan from the PR and update its references.

* ci: run embedding chunk benchmarks directly

Build the comparison revision and candidate in ci-tests, then retain
paired source-aware measurements. Remove the benchmark integration test.

* fix: close embedding review and CI gaps

Filter constructor-default and heritage lambdas plus C# expression
getters from class inputs. Bump the class fingerprint to invalidate
cached v3 projections and cover the cases with real parsers.

Install the optional embedding runtime before CI benchmarking and
serialize the native lifecycle test under lbug-db.

Refs #702

* fix(ci): isolate native benchmark revisions

Independent LadybugDB addons segfault at process exit when loaded together. Run each revision in its own child process, preserve paired sampling, and require clean teardown before writing results.

Give Kotlin regression fixtures unique names for the execution audit.

* feat(embeddings): adapt cAST splitting

Reuse existing parsers without adding a chunking dependency. Refresh
source-backed layout hashes even when retained snippets fit the budget.

Refs #702

* perf(embeddings): speed up AST chunking

* docs: remove chunking optimization report

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-05 14:08:43 +00:00

649 lines
25 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
generateEmbeddingText,
truncateDescription,
extractDeclarationOnly,
splitEmbeddingText,
withoutOverlappingContext,
} from '../../src/core/embeddings/text-generator.js';
import {
CHUNKING_RULES,
isChunkableLabel,
STRUCTURAL_TEXT_MODE_NONE,
} from '../../src/core/embeddings/types.js';
import type { EmbeddableNode } from '../../src/core/embeddings/types.js';
const baseNode: EmbeddableNode = {
id: 'Function:src/utils.ts:parseJSON',
name: 'parseJSON',
label: 'Function',
filePath: 'src/utils/parser.ts',
content: 'function parseJSON(text: string): Result<any> {\n return JSON.parse(text);\n}',
startLine: 10,
endLine: 12,
};
describe('text-generator', () => {
it('removes only the portion of preceding context already covered by a chunk', () => {
expect(withoutOverlappingContext('before shared', 100, 94)).toBe('before ');
expect(withoutOverlappingContext('shared', 100, 94)).toBe('');
expect(withoutOverlappingContext('shared', 100, 90)).toBe('');
expect(withoutOverlappingContext('before', 100, 100)).toBe('before');
expect(withoutOverlappingContext('before', 100, 101)).toBe('before');
expect(withoutOverlappingContext('before', undefined, 94)).toBe('before');
expect(withoutOverlappingContext('before', 100, undefined)).toBe('before');
});
it.each([2, 17, 4096])(
'preserves all final generated text within a %i character budget',
(budget) => {
const text = 'Header: 超长😀\n' + '中文😀abc'.repeat(3000);
const chunks = [...splitEmbeddingText(text, budget)];
expect(chunks.join('')).toBe(text);
expect(
chunks.every((chunk) => chunk.length <= budget && Buffer.from(chunk).toString() === chunk),
).toBe(true);
},
);
describe('generateEmbeddingText', () => {
it('leads with name and code, dropping verbose metadata lines (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
isExported: true,
repoName: 'backend-user-ms',
};
const text = generateEmbeddingText(node, node.content);
// Compact embedding header: name + code remain.
expect(text).toContain('Function: parseJSON');
expect(text).toContain('function parseJSON');
// Low-signal metadata lines are intentionally excluded from embedding text.
expect(text).not.toContain('Repo: backend-user-ms');
expect(text).not.toContain('Path: src/utils/parser.ts');
expect(text).not.toContain('Export: true');
});
it('excludes the Server line from embedding text even when serverName is set (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
repoName: 'backend-user-ms',
serverName: 'user-service',
};
const text = generateEmbeddingText(node, node.content);
expect(text).not.toContain('Server: user-service');
expect(text).not.toContain('Server:');
});
it('includes truncated description', () => {
const node: EmbeddableNode = {
...baseNode,
description: 'This function parses JSON text and returns a typed result object.',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('This function parses JSON text');
});
// #2333: short doc comments must not be diluted by metadata. The description
// is hoisted directly under the name, ahead of the code body, and the
// low-signal metadata lines are dropped from embedding text entirely.
it('hoists a short English description above the code body (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Method',
name: 'updateMaterialExpiryDate',
description: 'validate user',
isExported: false,
repoName: 'my-project',
content:
'function updateMaterialExpiryDate(paramMap) {\n // ... a long method body ...\n return doWork(paramMap);\n}',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('validate user');
// Description appears before the code body.
expect(text.indexOf('validate user')).toBeLessThan(text.indexOf('return doWork'));
// Metadata noise removed.
expect(text).not.toContain('Repo: my-project');
expect(text).not.toContain('Path:');
expect(text).not.toContain('Export:');
});
it('hoists a short CJK description above the code body (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Method',
name: 'updateMaterialExpiryDate',
description: '更新物料有效期',
isExported: false,
repoName: 'my-project',
content:
'function updateMaterialExpiryDate(paramMap) {\n // ... a long method body ...\n return doWork(paramMap);\n}',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('更新物料有效期');
expect(text.indexOf('更新物料有效期')).toBeLessThan(text.indexOf('return doWork'));
expect(text).not.toContain('Repo: my-project');
expect(text).not.toContain('Path:');
expect(text).not.toContain('Export:');
});
it('hoists description in short-label nodes too (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Const',
name: 'MAX_RETRIES',
description: 'retry ceiling',
content: 'const MAX_RETRIES = 5;',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Const: MAX_RETRIES');
expect(text).toContain('retry ceiling');
expect(text.indexOf('retry ceiling')).toBeLessThan(text.indexOf('const MAX_RETRIES = 5;'));
expect(text).not.toContain('Path:');
});
it('keeps structural Methods/Properties lines under the compact header (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Class',
name: 'Parser',
description: 'JSON parser',
repoName: 'my-project',
methodNames: ['parseJSON', 'validate'],
fieldNames: ['options', 'cache'],
content: `class Parser {
options: ParserOptions;
private cache: Map<string, any>;
parseJSON(text: string) { return JSON.parse(text); }
validate() { return true; }
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Class: Parser');
expect(text).toContain('JSON parser');
// Structural signal must survive the compact-header change.
expect(text).toContain('Methods: parseJSON, validate');
expect(text).toContain('Properties: options, cache');
// Description is hoisted ahead of the structural lines (ordering guard for
// the structural path, mirroring the function/method ordering checks).
expect(text.indexOf('JSON parser')).toBeLessThan(text.indexOf('Container:'));
expect(text.indexOf('JSON parser')).toBeLessThan(text.indexOf('Methods:'));
// Metadata noise still dropped.
expect(text).not.toContain('Repo: my-project');
});
it('emits no description line and no metadata when description is absent (#2333)', () => {
const node: EmbeddableNode = {
...baseNode,
isExported: true,
repoName: 'my-project',
description: undefined,
};
const text = generateEmbeddingText(node, node.content);
// Header is the name line, then the bounded location line, then a blank
// line, then the code body — no stray empty description line, no verbose
// metadata.
expect(
text.startsWith('Function: parseJSON\nLoc: utils/parser.ts\n\nfunction parseJSON'),
).toBe(true);
expect(text).not.toContain('Repo:');
expect(text).not.toContain('Export:');
// Only the bounded last-1-2 segments — never the verbose deep path.
expect(text).not.toContain('Path:');
expect(text).not.toContain('src/utils/parser.ts');
});
// U3 (#2333 PR #2334 tri-review): a BOUNDED location signal (last 1-2 path
// segments) is reinstated so path/service-qualified semantic search keeps a
// discriminator, since FTS does not index filePath.
it('emits a bounded location (last 2 segments), not the full deep path (#2333 U3)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Method',
name: 'updateMaterialExpiryDate',
filePath: 'src/main/java/com/example/service/MaterialServiceImpl.java',
content: 'function updateMaterialExpiryDate() { return doWork(); }',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Loc: service/MaterialServiceImpl.java');
// The deep prefix is dropped entirely.
expect(text).not.toContain('src/main/java/com/example');
});
it('emits just the basename for a root-level file (#2333 U3)', () => {
const node: EmbeddableNode = { ...baseNode, filePath: 'index.ts' };
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Loc: index.ts');
// No leading slash and no stray "undefined/" prefix from slicing one segment.
expect(text).not.toContain('Loc: /index.ts');
expect(text).not.toContain('undefined');
});
it('disambiguates same-named symbols in different service folders (#2333 U3)', () => {
const billing = generateEmbeddingText(
{ ...baseNode, name: 'handler', filePath: 'billing/handler.ts' },
'function handler() {}',
);
const identity = generateEmbeddingText(
{ ...baseNode, name: 'handler', filePath: 'identity/handler.ts' },
'function handler() {}',
);
expect(billing).toContain('Loc: billing/handler.ts');
expect(identity).toContain('Loc: identity/handler.ts');
// The two embedding texts differ — the regression the tri-review flagged
// (both collapsing to identical vectors) is fixed.
expect(billing).not.toBe(identity);
});
it('keeps the description ahead of the location signal (#2333 U3)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Method',
name: 'doThing',
description: 'batch import rows',
filePath: 'svc/importer.ts',
content: 'function doThing() { return run(); }',
};
const text = generateEmbeddingText(node, node.content);
// description leads, then the location line, then the code body.
expect(text.indexOf('batch import rows')).toBeLessThan(text.indexOf('Loc: svc/importer.ts'));
expect(text.indexOf('Loc: svc/importer.ts')).toBeLessThan(text.indexOf('return run()'));
});
it('normalizes Windows path separators in the location signal (#2333 U3)', () => {
const node: EmbeddableNode = { ...baseNode, filePath: 'src\\svc\\Foo.ts' };
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Loc: svc/Foo.ts');
expect(text).not.toContain('\\');
});
it('generates short node text for TypeAlias without chunking', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'TypeAlias',
name: 'Result',
content: 'type Result<T> = Success<T> | Error;',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('TypeAlias: Result');
expect(text).toContain('type Result<T> = Success<T> | Error;');
});
it('generates Class text with AST-extracted method/field names', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Class',
name: 'Parser',
methodNames: ['parseJSON', 'validate'],
fieldNames: ['options', 'cache'],
content: `class Parser {
options: ParserOptions;
private cache: Map<string, any>;
parseJSON(text: string) { return JSON.parse(text); }
validate() { return true; }
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Class: Parser');
expect(text).toContain('Methods: parseJSON, validate');
expect(text).toContain('Properties: options, cache');
// Method bodies should NOT appear in declaration section
expect(text).not.toContain('return JSON.parse');
expect(text).not.toContain('return true');
});
it('generates Class text without method names when not provided', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Class',
name: 'Parser',
content: `class Parser {
parse(input) { }
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Class: Parser');
expect(text).not.toContain('Methods:');
});
it('generates Interface text with structural names and signatures', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Interface',
name: 'Handler',
methodNames: ['handle', 'validate'],
fieldNames: ['name'],
content: `interface Handler {
handle(event: Event): void;
validate(input: string): boolean;
readonly name: string;
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Interface: Handler');
expect(text).toContain('Methods: handle, validate');
expect(text).toContain('Properties: name');
expect(text).toContain('handle(event: Event): void;');
expect(text).toContain('readonly name: string;');
});
it('includes only prepared Class fields, never unfiltered method source', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Class',
name: 'Parser',
methodNames: ['parseJSON', 'validate'],
fieldNames: ['options', 'cache'],
content: `class Parser {
options: ParserOptions;
cache: Map<string, any>;
parseJSON(text: string) { return JSON.parse(text); }
validate() { return true; }
}`,
};
const chunkBody = 'cache: Map<string, any>;';
const text = generateEmbeddingText(
{ ...node, classDeclaration: 'class Parser {' },
chunkBody,
);
expect(text).toContain('Class: Parser');
expect(text).toContain('Methods: parseJSON, validate');
expect(text).toContain('class Parser {');
expect(text).toContain('cache: Map<string, any>;');
expect(text).not.toContain('return JSON.parse');
expect(generateEmbeddingText(node, node.content)).not.toContain('return JSON.parse');
});
it('generates Struct text with structural metadata', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Struct',
name: 'User',
fieldNames: ['name', 'age'],
content: `struct User {
name: String,
age: u32,
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Struct: User');
expect(text).toContain('Properties: name, age');
expect(text).toContain('Container: struct User {');
expect(text).toContain('struct User {');
});
// U5 (#2333 PR #2334): Interface and Struct route through the same
// generateStructuralTypeText path as Class, so the description-forward
// ordering must hold for them too — guards against a future per-label
// specialization silently reordering the header.
it('keeps an Interface description ahead of its structural lines (#2333 U5)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Interface',
name: 'Handler',
description: 'event handler contract',
methodNames: ['handle', 'validate'],
fieldNames: ['name'],
content: `interface Handler {
handle(event: Event): void;
readonly name: string;
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Interface: Handler');
expect(text).toContain('event handler contract');
expect(text).toContain('Methods: handle, validate');
expect(text).toContain('Properties: name');
expect(text.indexOf('event handler contract')).toBeLessThan(text.indexOf('Container:'));
expect(text.indexOf('event handler contract')).toBeLessThan(text.indexOf('Methods:'));
expect(text).toContain('Loc: utils/parser.ts');
expect(text).not.toContain('Repo:');
});
it('keeps a Struct description ahead of its structural lines (#2333 U5)', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Struct',
name: 'User',
description: 'user record',
fieldNames: ['name', 'age'],
content: `struct User {
name: String,
age: u32,
}`,
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Struct: User');
expect(text).toContain('user record');
expect(text).toContain('Properties: name, age');
expect(text).toContain('Container: struct User {');
expect(text.indexOf('user record')).toBeLessThan(text.indexOf('Container:'));
expect(text.indexOf('user record')).toBeLessThan(text.indexOf('Properties:'));
expect(text).toContain('Loc: utils/parser.ts');
});
it('keeps compact container context on later structural chunks', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Class',
name: 'Parser',
methodNames: ['parseJSON', 'validate'],
fieldNames: ['options', 'cache'],
content: `class Parser {
options: ParserOptions;
cache: Map<string, any>;
parseJSON(text: string) { return JSON.parse(text); }
validate() { return true; }
}`,
};
const text = generateEmbeddingText(
{ ...node, classDeclaration: 'class Parser {' },
'cache: Map<string, any>;',
{},
1,
'parseJSON(text: string) { return JSON.parse(text); }',
);
expect(text).toContain('Class: Parser');
expect(text).toContain('Container: class Parser {');
expect(text).not.toContain('[preceding context]:');
expect(text).not.toContain('return JSON.parse');
expect(text).toContain('cache: Map<string, any>;');
expect(text).not.toContain('Methods: parseJSON, validate');
expect(text).not.toContain('Properties: options, cache');
});
it.each([0, 1])(
'preserves Unicode at the compact container cutoff (chunk %i)',
(chunkIndex) => {
const prefix = 'export interface Handler { value: "';
const declaration = `${prefix}${'x'.repeat(199 - prefix.length)}😀"; }`;
const node = { ...baseNode, label: 'Interface', name: 'Handler', content: declaration };
const text = generateEmbeddingText(node, declaration, {}, chunkIndex);
expect(text).toContain('Container: export interface Handler');
expect(text).toContain('😀');
expect(Buffer.from(text).toString('utf8')).toBe(text);
const chunks = [...splitEmbeddingText(text, 200)];
expect(chunks.join('')).toBe(text);
expect(
chunks.every(
(chunk) => chunk.length <= 200 && Buffer.from(chunk).toString('utf8') === chunk,
),
).toBe(true);
},
);
it('keeps a declaration-aware container on decorated and blank-led later chunks', () => {
for (const prefix of ['\n\n', '@Component({\n selector: "app-root",\n})\n']) {
const node = {
...baseNode,
label: 'Class',
name: 'Parser',
content: `${prefix}export class Parser {\n parse() { return 1; }\n}`,
classDeclaration: 'export class Parser {',
};
expect(generateEmbeddingText(node, 'field: string;', {}, 1)).toContain(
'Container: export class Parser {',
);
}
});
it.each([
`@Component({ description: "${'x'.repeat(500)}" })\n`,
`/* ${'comment '.repeat(80)} */\n`,
'\n'.repeat(250),
])('uses graph identity after a long prefix (case %#)', (prefix) => {
const node = {
...baseNode,
label: 'Class',
name: 'Parser',
content: `${prefix}export class Parser {\n parse() { return 1; }\n}`,
classDeclaration: 'export class Parser {',
};
expect(generateEmbeddingText(node, 'field: string;', {}, 0)).toContain(
'Container: export class Parser {',
);
expect(generateEmbeddingText(node, 'field: string;', {}, 1)).toContain(
'Container: export class Parser {',
);
});
it('adds preceding context to non-structural chunk text', () => {
const text = generateEmbeddingText(
baseNode,
'return JSON.parse(text);',
{},
1,
'function parseJSON(text: string): Result<any> {',
);
expect(text).toContain('Function: parseJSON');
expect(text).toContain('[preceding context]: ...function parseJSON');
expect(text).toContain('return JSON.parse(text);');
});
it.each([
{
label: 'Protocol' as const,
name: 'Worker',
content: ['@protocol Worker', '- (void)run;', '@end'].join('\n'),
chunk: '- (void)run;',
},
{
label: 'Category' as const,
name: 'Worker (Tracing)',
content: ['@interface Worker (Tracing)', '- (void)trace;', '@end'].join('\n'),
chunk: '- (void)trace;',
},
])(
'keeps Objective-C $label embedding text code-oriented',
({ label, name, content, chunk }) => {
const node: EmbeddableNode = {
...baseNode,
label,
name,
content,
};
const text = generateEmbeddingText(node, chunk, {}, 1, 'previous declaration');
expect(CHUNKING_RULES[label]?.structuralTextMode).toBe(STRUCTURAL_TEXT_MODE_NONE);
expect(text).toContain(chunk);
expect(text).toContain('[preceding context]: ...previous declaration');
expect(text).not.toContain('Container:');
},
);
});
describe('Constructor label', () => {
it('is recognized as chunkable', () => {
expect(isChunkableLabel('Constructor')).toBe(true);
});
it('is recognized as embeddable', () => {
const node: EmbeddableNode = {
...baseNode,
label: 'Constructor',
name: 'constructor',
content: 'constructor(private service: ApiClient) {\n this.service = service;\n}',
};
const text = generateEmbeddingText(node, node.content);
expect(text).toContain('Constructor: constructor');
expect(text).toContain('this.service = service');
});
});
describe('extractDeclarationOnly', () => {
it('strips method bodies from TS class', () => {
const content = `class Foo {
prop1: string;
method1() {
if (x) { nested }
}
method2() { return 1; }
}`;
const result = extractDeclarationOnly(content);
expect(result).toContain('class Foo {');
expect(result).toContain('prop1: string;');
expect(result).not.toContain('if (x)');
expect(result).not.toContain('return 1');
});
it('keeps single-line methods with semicolon (property initializers)', () => {
const content = `class Foo {
config = { timeout: 5000 };
count = 0;
}`;
const result = extractDeclarationOnly(content);
expect(result).toContain('config = { timeout: 5000 };');
expect(result).toContain('count = 0;');
});
it('returns empty for non-brace languages (Python)', () => {
const content = `class User:
def __init__(self, name):
self.name = name`;
const result = extractDeclarationOnly(content);
expect(result).toBe('');
});
it('preserves all fields in Rust struct', () => {
const content = `struct User {
name: String,
age: u32,
}`;
const result = extractDeclarationOnly(content);
expect(result).toContain('struct User {');
expect(result).toContain('name: String,');
expect(result).toContain('age: u32,');
});
it('preserves all lines in interface (no method bodies)', () => {
const content = `interface Handler {
handle(event: Event): void;
validate(input: string): boolean;
}`;
const result = extractDeclarationOnly(content);
expect(result).toContain('interface Handler {');
expect(result).toContain('handle(event: Event): void;');
expect(result).toContain('validate(input: string): boolean;');
});
});
describe('truncateDescription', () => {
it('returns short text unchanged', () => {
expect(truncateDescription('short text', 150)).toBe('short text');
});
it('truncates at sentence boundary', () => {
const text = 'First sentence. Second sentence. Third very long sentence that goes on and on.';
const result = truncateDescription(text, 40);
expect(result).toContain('First sentence');
expect(result.length).toBeLessThan(text.length);
});
it('truncates at word boundary when no sentence end', () => {
const text =
'this is a long description without any sentence ending punctuation marks at all';
const result = truncateDescription(text, 30);
expect(result.length).toBeLessThanOrEqual(30);
expect(result.length).toBeLessThan(text.length);
});
});
});