Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
rank priors keep relational pins and existing order; pinned rows take neighbour scores
garrytan0c7f51736a5a
diff --git a/src/core/search/rank-priors.ts b/src/core/search/rank-priors.tsindex 8b93fddd9f..4053f8ad34 100644--- a/src/core/search/rank-priors.ts+++ b/src/core/search/rank-priors.ts@@ -92,17 +92,23 @@ export function applyRankPriors(rows: SearchResult[], query: string): SearchResuconst image = isImageRow(r) ? { image_page: true as const } : {};return priors[i] === 1 && !image.image_page ? r : { ...r, ...image, ...(priors[i] !== 1 ? { rank_prior: priors[i] } : {}) };});- const scored = stamped.map((r, i) => (Number.isFinite(r.rerank_score) ? i : -1)).filter(i => i >= 0);- if (scored.length > 0) {- if (priors.every(p => p === 1)) return stamped;- for (const i of scored) stamped[i] = { ...stamped[i], rank_score: stamped[i].rerank_score! * priors[i] };- const reordered = scored.map(i => stamped[i]).map((r, n) => ({ r, n })).sort((a, b) => (b.r.rank_score! - a.r.rank_score!) || (a.n - b.n)).map(x => x.r);- scored.forEach((slot, n) => { stamped[slot] = reordered[n]; });- return stamped;- }if (priors.every(p => p === 1)) return stamped;- return stamped- .map((r, n) => ({ r: priors[n] === 1 ? r : { ...r, score: r.score * priors[n] }, n }))- .sort((a, b) => (b.r.score - a.r.score) || (a.n - b.n))- .map(x => x.r);+ const reranked = stamped.some(r => Number.isFinite(r.rerank_score));+ for (let i = 0; i < stamped.length; i++) {+ if (priors[i] === 1) continue;+ if (!reranked) stamped[i] = { ...stamped[i], score: stamped[i].score * priors[i] };+ else if (Number.isFinite(stamped[i].rerank_score)) stamped[i] = { ...stamped[i], rank_score: stamped[i].rerank_score! * priors[i] };+ }+ const value = (r: SearchResult) => (reranked ? r.rank_score ?? r.rerank_score! : r.score);+ // Pinned rows and (in a reranked list) rows without a rerank score keep their slots.+ const slots = stamped.map((r, i) => i).filter(i => !stamped[i].relational_pinned && (!reranked || Number.isFinite(stamped[i].rerank_score)));+ // Unchanged rows keep their relative order (feedback and any later adjustment included); a demoted row moves+ // down to before the first unchanged-or-placed row whose order value is below its own.+ const seq = slots.filter(i => priors[i] === 1).map(i => stamped[i]);+ for (const i of slots.filter(i => priors[i] !== 1)) {+ const at = seq.findIndex(r => value(r) < value(stamped[i]));+ seq.splice(at < 0 ? seq.length : at, 0, stamped[i]);+ }+ slots.forEach((slot, n) => { stamped[slot] = seq[n]; });+ return stamped;}diff --git a/src/core/search/score-contract.ts b/src/core/search/score-contract.tsindex 2b301a1afe..a936c7e300 100644--- a/src/core/search/score-contract.ts+++ b/src/core/search/score-contract.ts@@ -14,9 +14,9 @@ import type { SearchResult } from '../types.ts';const finite = (n: unknown): n is number => typeof n === 'number' && Number.isFinite(n);-export function scoresFollowRank<T extends Pick<SearchResult, 'score' | 'rerank_score' | 'rank_score' | 'fused_score'>>(rows: T[]): T[] {+export function scoresFollowRank<T extends Pick<SearchResult, 'score' | 'rerank_score' | 'rank_score' | 'fused_score' | 'relational_pinned'>>(rows: T[]): T[] {const reranked = rows.some(r => finite(r.rerank_score));- const order = rows.map(r => (reranked ? (finite(r.rank_score) ? r.rank_score : r.rerank_score) : r.score));+ const order = rows.map(r => (reranked ? (r.relational_pinned ? undefined : finite(r.rank_score) ? r.rank_score : r.rerank_score) : r.score));const first = order.find(finite);if (first === undefined) return rows;let prev = Infinity;diff --git a/test/search/rank-priors.test.ts b/test/search/rank-priors.test.tsindex e03aa7caa4..051e3a0a18 100644--- a/test/search/rank-priors.test.ts+++ b/test/search/rank-priors.test.ts@@ -103,3 +103,34 @@ describe('scoresFollowRank (#7156)', () => {expect(scoresFollowRank(rows)).toBe(rows);});});++describe('pins and feedback order survive the priors (review)', () => {+ const r = (slug: string, type: string, extra: Partial<SearchResult> = {}) => ({ slug, type, title: slug, chunk_text: slug, score: 0.5, page_id: 1, chunk_id: 1, chunk_index: 0, chunk_source: 'compiled_truth', stale: false, source_id: 'default', ...extra }) as SearchResult;+ test('a relational pin keeps rank 1 when an image row below it is demoted', () => {+ const rows = [+ r('people/alice-example', 'person', { relational_pinned: true, rerank_score: 0.05 }),+ r('images/badge.jpg', 'image', { rerank_score: 0.9, chunk_text: 'badge' }),+ r('notes/harbor-plan', 'note', { rerank_score: 0.7 }),+ ];+ const out = applyRankPriors(rows, 'who invested in harbor plan');+ expect(out[0].slug).toBe('people/alice-example');+ expect(out.map(x => x.slug)).toEqual(['people/alice-example', 'notes/harbor-plan', 'images/badge.jpg']);+ });+ test('unchanged rows keep their relative order even when it differs from rerank order', () => {+ const rows = [+ r('notes/b', 'note', { rerank_score: 0.6 }),+ r('notes/a', 'note', { rerank_score: 0.8 }),+ r('images/x.png', 'image', { rerank_score: 0.9, chunk_text: 'x' }),+ ];+ const out = applyRankPriors(rows, 'harbor depot rota');+ expect(out.map(x => x.slug)).toEqual(['notes/b', 'notes/a', 'images/x.png']);+ });+ test('a pinned row with a low rerank score does not flatten the column', () => {+ const out = scoresFollowRank([+ { score: 0.1, rerank_score: 0.1, relational_pinned: true },+ { score: 0.4, rerank_score: 0.9 },+ { score: 0.5, rerank_score: 0.7 },+ ]);+ expect(out.map(x => x.score)).toEqual([0.9, 0.9, 0.7]);+ });+});