Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(search): exact-lookup tier keeps identifier hits in keyword order (#7935)
garrytan692a11a3701d
diff --git a/src/core/search/exact-lookup.ts b/src/core/search/exact-lookup.tsindex d29575046c..e9767e7c77 100644--- a/src/core/search/exact-lookup.ts+++ b/src/core/search/exact-lookup.ts@@ -209,9 +209,10 @@ export async function structuralExactLookup(* Mirrors applyAliasHop's contract: hits already present are promoted to the* top (score = current-top + epsilon) and stamped; absent hits inject with* the same top-of-organic + epsilon score shape. Identity rows move to the- * front; every other row keeps its input order (never re-sorted by `score`,- * which would discard a reranked order). Returns a NEW array; fail-open- * returns `results` unchanged.+ * front in the hits' own order (#7935: the first hit ends at rank 1 with the+ * highest score); every other row keeps its input order (never re-sorted by+ * `score`, which would discard a reranked order). Returns a NEW array;+ * fail-open returns `results` unchanged.*/export async function applyExactLookupTier(engine: BrainEngine,@@ -241,7 +242,7 @@ export function applyExactLookupHits(results: SearchResult[], hits: readonly Seaconst topScore = out.reduce((m, r) => (Number.isFinite(r.score) && r.score > m ? r.score : m), 0);let injectScore = topScore > 0 ? topScore : 1.0;- for (const hit of hits) {+ for (const hit of [...hits].reverse()) {injectScore += 1e-6;const matchingIndexes: number[] = [];for (let i = 0; i < out.length; i++) {diff --git a/test/search-identifier-lookup.test.ts b/test/search-identifier-lookup.test.tsindex 9b3dc6d1c9..4ae0756178 100644--- a/test/search-identifier-lookup.test.ts+++ b/test/search-identifier-lookup.test.ts@@ -7,7 +7,8 @@ import { identifierKeywordQuery, parseIdentifier, textContainsIdentifier, type Iimport { PGLiteEngine } from '../src/core/pglite-engine.ts';import { resetPgliteState } from './helpers/reset-pglite.ts';import { seedPage } from './helpers/keyword-paging-fixture.ts';-import { structuralExactLookup } from '../src/core/search/exact-lookup.ts';+import { applyExactLookupTier, structuralExactLookup } from '../src/core/search/exact-lookup.ts';+import type { SearchResult } from '../src/core/types.ts';describe('parseIdentifier', () => {const cases: Array<[string, IdentifierKind | null]> = [@@ -98,4 +99,20 @@ describe('identifier probe on PGLite', () => {test('a plain-word query runs no identifier probe', async () => {expect(await slugs('billing team')).toEqual([]);});++ // #7935: the tier promoted its hits in reverse, so the best keyword match landed last of the promoted rows.+ test('promoted identifier hits keep the keyword order, best match first', async () => {+ await seedPage(engine, 'companies/acme-example', 'Acme Example owns acme-example.com. Mail [email protected] or visit acme-example.com and acme-example.com/support.');+ await seedPage(engine, 'people/alice-example', 'Alice Example works at acme-example.com.');+ await seedPage(engine, 'meetings/2026-04-03', 'Call notes; the agenda link was acme-example.com.');+ const hits = (await structuralExactLookup(engine, 'acme-example.com', {})).map(h => h.slug);+ expect(hits).toHaveLength(3);+ expect(hits[0]).toBe('companies/acme-example');+ const organic = [{ slug: 'notes/vendor-contact', source_id: 'default', score: 0.9, chunk_text: 'x' } as SearchResult,+ { slug: hits[2], source_id: 'default', score: 0.5, chunk_text: 'y' } as SearchResult];+ const out = await applyExactLookupTier(engine, organic, 'acme-example.com', {});+ expect(out.map(r => r.slug)).toEqual([...hits, 'notes/vendor-contact']);+ expect(out[0].score).toBeGreaterThan(out[1].score);+ expect(out[1].score).toBeGreaterThan(out[2].score);+ });});