Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(search): two-pass code walk follows the symbol resolver's outcome (#5574)

garrytan2eac6d6e5fb0
diff --git a/src/core/chunkers/symbol-resolver.ts b/src/core/chunkers/symbol-resolver.ts
index 0a8eee3d62..8348f15645 100644
--- a/src/core/chunkers/symbol-resolver.ts
+++ b/src/core/chunkers/symbol-resolver.ts
@@ -291,11 +291,12 @@ async function processChunkBatch(
/**
* Read the resolution outcome from a single edge's metadata, if any.
- * Returns null when the edge hasn't been processed by the resolver yet.
+ * 'unresolved' also covers edges the resolver hasn't processed yet (no
+ * outcome keys written) — the two are indistinguishable by design.
*
- * Public helper for downstream code (two-pass.ts, code_blast op) that
- * wants to use the resolver's output without parsing edge_metadata JSON
- * directly.
+ * Public helper for downstream code that wants to use the resolver's
+ * output without parsing edge_metadata JSON directly. Consumer: the
+ * two-pass structural walk (src/core/search/two-pass.ts).
*/
export type EdgeResolution =
| { kind: 'resolved'; chunk_id: number }
diff --git a/src/core/search/two-pass.ts b/src/core/search/two-pass.ts
index cec572e392..b0382e64a4 100644
--- a/src/core/search/two-pass.ts
+++ b/src/core/search/two-pass.ts
@@ -24,6 +24,7 @@
import type { BrainEngine } from '../engine.ts';
import type { SearchResult } from '../types.ts';
import { currentTextProjectionFilter } from './safe-chunks.ts';
+import { readEdgeResolution } from '../chunkers/symbol-resolver.ts';
const MAX_WALK_DEPTH = 2;
const NEIGHBOR_CAP_PER_HOP = 50;
@@ -129,14 +130,25 @@ export async function expandAnchors(
continue;
}
- // Two kinds of neighbors to visit:
+ // Three kinds of neighbors to visit:
// 1. Resolved edges with to_chunk_id: direct chunk follow.
- // 2. Unresolved edges (code_edges_symbol): resolve by
+ // 2. Symbol edges the within-file resolver already answered:
+ // follow the resolved chunk_id / ambiguous candidates directly
+ // (same page as the caller). The name lookup below would pull
+ // in EVERY file defining the symbol, the aliasing the resolver
+ // exists to prevent.
+ // 3. Genuinely unresolved edges: resolve by
// symbol_name_qualified = to_symbol_qualified, then follow.
const directChunkIds: number[] = [];
const unresolvedTargets: string[] = [];
for (const e of edges) {
- if (e.to_chunk_id != null) directChunkIds.push(e.to_chunk_id);
+ if (e.to_chunk_id != null) {
+ directChunkIds.push(e.to_chunk_id);
+ continue;
+ }
+ const res = readEdgeResolution(e.edge_metadata);
+ if (res.kind === 'resolved') directChunkIds.push(res.chunk_id);
+ else if (res.kind === 'ambiguous') directChunkIds.push(...res.candidate_chunk_ids);
else if (e.to_symbol_qualified) unresolvedTargets.push(e.to_symbol_qualified);
}
// Resolve unresolved edges by looking up chunks whose
diff --git a/test/two-pass.test.ts b/test/two-pass.test.ts
index 2df08946a1..e14e4a88f4 100644
--- a/test/two-pass.test.ts
+++ b/test/two-pass.test.ts
@@ -15,7 +15,8 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { installFixtureChunks } from './helpers/page-projection.ts';
import { expandAnchors, hydrateChunks } from '../src/core/search/two-pass.ts';
import { hybridSearch } from '../src/core/search/hybrid.ts';
-import { importFromContent } from '../src/core/import-file.ts';
+import { importCodeFile, importFromContent } from '../src/core/import-file.ts';
+import { resolveSymbolEdgesIncremental } from '../src/core/chunkers/symbol-resolver.ts';
import { configureGateway, resetGateway } from '../src/core/ai/gateway.ts';
import { LEGACY_EMBEDDING_CONFIG } from './helpers/legacy-embedding-config.ts';
@@ -192,6 +193,168 @@ describe('Layer 7 (A2) — expandAnchors', () => {
});
});
+describe('Layer 7 (A2) — resolver outcome consumption', () => {
+ let engine: PGLiteEngine;
+ let chunkX: number;
+ let chunkBmain: number;
+ let chunkBalias: number;
+
+ beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+
+ // Caller page.
+ await engine.putPage('src-x-ts', {
+ type: 'code', page_kind: 'code',
+ title: 'src/x.ts (typescript)',
+ compiled_truth: 'export function x() { return b(); }',
+ timeline: '',
+ });
+ await installFixtureChunks(engine, 'src-x-ts', [{
+ chunk_index: 0,
+ chunk_text: 'export function x() { return b(); }',
+ chunk_source: 'compiled_truth',
+ language: 'typescript',
+ symbol_name: 'x', symbol_type: 'function',
+ symbol_name_qualified: 'x',
+ }]);
+
+ // The qualified name 'b' is defined in TWO files — the aliasing case the
+ // resolver exists to disambiguate.
+ for (const slug of ['src-bmain-ts', 'src-balias-ts']) {
+ await engine.putPage(slug, {
+ type: 'code', page_kind: 'code',
+ title: `${slug} (typescript)`,
+ compiled_truth: 'export function b() { return 1; }',
+ timeline: '',
+ });
+ await installFixtureChunks(engine, slug, [{
+ chunk_index: 0,
+ chunk_text: 'export function b() { return 1; }',
+ chunk_source: 'compiled_truth',
+ language: 'typescript',
+ symbol_name: 'b', symbol_type: 'function',
+ symbol_name_qualified: 'b',
+ }]);
+ }
+
+ chunkX = (await engine.getChunks('src-x-ts'))[0]!.id;
+ chunkBmain = (await engine.getChunks('src-bmain-ts'))[0]!.id;
+ chunkBalias = (await engine.getChunks('src-balias-ts'))[0]!.id;
+ });
+
+ afterAll(async () => {
+ await engine.disconnect();
+ }, 30_000);
+
+ const anchor = (chunkId: number) => [{
+ slug: 'src-x-ts', page_id: 0, title: 'x', type: 'code',
+ chunk_text: '', chunk_source: 'compiled_truth', chunk_id: chunkId,
+ chunk_index: 0, score: 1.0, stale: false, source_id: 'default',
+ } as never];
+
+ test('resolved_chunk_id in edge_metadata wins over the name lookup', async () => {
+ await engine.addCodeEdges([{
+ from_chunk_id: chunkX, to_chunk_id: null,
+ from_symbol_qualified: 'x', to_symbol_qualified: 'b',
+ edge_type: 'calls',
+ edge_metadata: { resolved_chunk_id: chunkBmain },
+ }]);
+ const expanded = await expandAnchors(engine, anchor(chunkX), { walkDepth: 1 });
+ const ids = expanded.map(e => e.chunk_id);
+ expect(ids).toContain(chunkBmain);
+ // The pre-fix behavior re-looked-up 'b' by name across ALL files and
+ // pulled in the alias file too — exactly what the resolver prevents.
+ expect(ids).not.toContain(chunkBalias);
+ });
+
+ test('ambiguous candidates are followed directly', async () => {
+ // A fresh caller whose only edge is ambiguous: no chunk is named
+ // 'ambig_target', so only the candidate list can reach chunkBalias.
+ await engine.putPage('src-z-ts', {
+ type: 'code', page_kind: 'code',
+ title: 'src/z.ts (typescript)',
+ compiled_truth: 'export function z() { return ambig_target(); }',
+ timeline: '',
+ });
+ await installFixtureChunks(engine, 'src-z-ts', [{
+ chunk_index: 0,
+ chunk_text: 'export function z() { return ambig_target(); }',
+ chunk_source: 'compiled_truth',
+ language: 'typescript',
+ symbol_name: 'z', symbol_type: 'function',
+ symbol_name_qualified: 'z',
+ }]);
+ const chunkZ = (await engine.getChunks('src-z-ts'))[0]!.id;
+ await engine.addCodeEdges([{
+ from_chunk_id: chunkZ, to_chunk_id: null,
+ from_symbol_qualified: 'z', to_symbol_qualified: 'ambig_target',
+ edge_type: 'calls',
+ edge_metadata: { ambiguous: true, candidates: [chunkBalias] },
+ }]);
+ const expanded = await expandAnchors(engine, anchor(chunkZ), { walkDepth: 1 });
+ const ids = expanded.map(e => e.chunk_id);
+ expect(ids).toContain(chunkBalias);
+ expect(ids).not.toContain(chunkBmain);
+ });
+
+ test('unresolved edges (no resolver outcome) still match by qualified name', async () => {
+ // A second caller chunk y → 'b' with NO edge_metadata: the name lookup
+ // fans out to every definition, both files.
+ await engine.putPage('src-y-ts', {
+ type: 'code', page_kind: 'code',
+ title: 'src/y.ts (typescript)',
+ compiled_truth: 'export function y() { return b(); }',
+ timeline: '',
+ });
+ await installFixtureChunks(engine, 'src-y-ts', [{
+ chunk_index: 0,
+ chunk_text: 'export function y() { return b(); }',
+ chunk_source: 'compiled_truth',
+ language: 'typescript',
+ symbol_name: 'y', symbol_type: 'function',
+ symbol_name_qualified: 'y',
+ }]);
+ const chunkY = (await engine.getChunks('src-y-ts'))[0]!.id;
+ await engine.addCodeEdges([{
+ from_chunk_id: chunkY, to_chunk_id: null,
+ from_symbol_qualified: 'y', to_symbol_qualified: 'b',
+ edge_type: 'calls',
+ }]);
+ const expanded = await expandAnchors(engine, anchor(chunkY), { walkDepth: 1 });
+ const ids = expanded.map(e => e.chunk_id);
+ expect(ids).toContain(chunkBmain);
+ expect(ids).toContain(chunkBalias);
+ });
+
+ test('a real resolver pass keeps the walk inside the caller file', async () => {
+ await importCodeFile(engine, 'src/local.ts',
+ 'export function helperz(): number {\n return 1;\n}\n\nexport function callerz(): number {\n return helperz() + 1;\n}\n',
+ { noEmbed: true });
+ await importCodeFile(engine, 'src/other.ts',
+ 'export function helperz(): number {\n return 2;\n}\n', { noEmbed: true });
+ const stats = await resolveSymbolEdgesIncremental(engine, { sourceId: 'default' });
+ expect(stats.edges_resolved).toBeGreaterThanOrEqual(1);
+
+ const chunksOf = async (path: string) => (await engine.executeRaw<{ id: number; symbol: string }>(
+ `SELECT cc.id, cc.symbol_name_qualified AS symbol FROM content_chunks cc
+ JOIN pages p ON p.id = cc.page_id
+ WHERE p.slug = $1 OR p.slug = $2`,
+ [path, path.replace(/[/.]/g, '-')],
+ ));
+ const local = await chunksOf('src/local.ts');
+ const other = await chunksOf('src/other.ts');
+ const caller = local.find(c => c.symbol === 'callerz')!.id;
+ const localHelper = local.find(c => c.symbol === 'helperz')!.id;
+ const otherHelper = other.find(c => c.symbol === 'helperz')!.id;
+
+ const ids = (await expandAnchors(engine, anchor(caller), { walkDepth: 1 })).map(e => e.chunk_id);
+ expect(ids).toContain(localHelper);
+ expect(ids).not.toContain(otherHelper);
+ });
+});
+
describe('Layer 7 (A2) — query operation schema', () => {
test('query op exposes near_symbol + walk_depth params', async () => {
const { operations } = await import('../src/core/operations.ts');