Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
fix(search): normalize legacy timestamps in fusion ties (#6748)
Afloatf53f002b7f8e
diff --git a/src/shared/reciprocal-rank-fusion.ts b/src/shared/reciprocal-rank-fusion.tsindex b20157fb5..74f01692e 100644--- a/src/shared/reciprocal-rank-fusion.ts+++ b/src/shared/reciprocal-rank-fusion.ts@@ -10,6 +10,8 @@* worker's relevance search use this one k, so one query ranks the same notes* in both runtimes.*/+import { epochMs } from './epoch-ms.js';+export const RRF_K = 60;/**@@ -35,7 +37,7 @@ export function fuseByReciprocalRank<T extends { id: number; created_at_epoch: n}return Array.from(fused.values()).sort((a, b) => b.score - a.score- || b.row.created_at_epoch - a.row.created_at_epoch+ || epochMs(b.row.created_at_epoch) - epochMs(a.row.created_at_epoch)|| b.row.id - a.row.id).map(entry => entry.row);}diff --git a/tests/shared/reciprocal-rank-fusion.test.ts b/tests/shared/reciprocal-rank-fusion.test.tsindex f927c7e1c..b07f81093 100644--- a/tests/shared/reciprocal-rank-fusion.test.ts+++ b/tests/shared/reciprocal-rank-fusion.test.ts@@ -19,6 +19,19 @@ describe('fuseByReciprocalRank', () => {expect(fused.map(entry => entry.id)).toEqual([3, 2, 1]);});+ it('compares mixed legacy seconds and milliseconds by their actual instants', () => {+ const newer = row(1, Date.parse('2026-01-02T00:00:00Z') / 1000);+ const older = row(2, Date.parse('2025-01-01T00:00:00Z'));+ expect(fuseByReciprocalRank([[older], [newer]])).toEqual([newer, older]);+ });++ it('uses the id tiebreak when seconds and milliseconds describe the same instant', () => {+ const instant = Date.parse('2026-01-02T00:00:00Z');+ const seconds = row(2, instant / 1000);+ const milliseconds = row(1, instant);+ expect(fuseByReciprocalRank([[milliseconds], [seconds]])).toEqual([seconds, milliseconds]);+ });+it('returns the other list unchanged when one list is empty', () => {const keyword = [row(4, 1), row(9, 3), row(2, 2)];expect(fuseByReciprocalRank([[], keyword])).toEqual(keyword);diff --git a/tests/worker/search/rrf-legacy-timestamps.test.ts b/tests/worker/search/rrf-legacy-timestamps.test.tsnew file mode 100644index 000000000..8bb3b13b7--- /dev/null+++ b/tests/worker/search/rrf-legacy-timestamps.test.ts@@ -0,0 +1,54 @@+import { describe, expect, it } from 'bun:test';+import { SessionStore } from '../../../src/services/sqlite/SessionStore.js';+import { SessionSearch } from '../../../src/services/sqlite/SessionSearch.js';+import { SearchOrchestrator } from '../../../src/services/worker/search/SearchOrchestrator.js';+import { ChromaSync } from '../../../src/services/sync/ChromaSync.js';++describe('hybrid relevance with legacy observation timestamps', () => {+ it('keeps the newer memory on the first page when the fusion scores tie', async () => {+ const store = new SessionStore(':memory:');+ try {+ const session = store.createSDKSession('content', 'search-project', 'ask');+ store.ensureMemorySessionIdRegistered(session, 'memory');+ const newerEpoch = Date.parse('2026-01-02T00:00:00Z');+ const olderEpoch = Date.parse('2025-01-01T00:00:00Z');+ const save = (epoch: number) => store.storeObservation('memory', 'search-project', {+ type: 'discovery', title: `Authentication finding ${epoch}`, subtitle: null,+ narrative: 'Authentication investigation', facts: [], concepts: [], files_read: [], files_modified: [],+ }, 1, 0, epoch).id;+ const newer = save(newerEpoch);+ const older = save(olderEpoch);+ // Older installs left some epoch columns in seconds. Preserve the ISO+ // timestamp and content, changing only that historical storage unit.+ store.db.prepare('UPDATE observations SET created_at_epoch = ? WHERE id = ?')+ .run(newerEpoch / 1000, newer);+ const sqlite = new SessionSearch(store.db);+ const keyword = sqlite.searchObservations('Authentication', { project: 'search-project' });+ expect(keyword).toHaveLength(2);+ // Reversed rankings give both rows exactly 1/61 + 1/62, leaving recency+ // to decide which observation fills a one-result page.+ const semantic = [...keyword].reverse();+ const chroma = Object.create(ChromaSync.prototype) as ChromaSync;+ chroma.queryChroma = async () => ({+ ids: semantic.map(row => row.id), distances: [0.1, 0.2],+ metadatas: semantic.map(row => ({+ sqlite_id: row.id, doc_type: 'observation', project: 'search-project',+ platform_source: 'claude', created_at_epoch: row.created_at_epoch,+ })),+ });+ const search = new SearchOrchestrator(sqlite, store, chroma);+ const query = {+ query: 'Authentication', project: 'search-project', searchType: 'observations',+ orderBy: 'relevance', ignoreDefaultRecencyWindow: true, limit: 1,+ };+ const first = await search.search(query);+ const second = await search.search({ ...query, offset: 1 });+ expect(first.results.observations.map(row => row.id)).toEqual([newer]);+ expect(second.results.observations.map(row => row.id)).toEqual([older]);+ expect(store.getObservationById(newer)?.created_at_epoch).toBe(newerEpoch / 1000);+ expect(store.getObservationById(older)?.created_at_epoch).toBe(olderEpoch);+ } finally {+ store.close();+ }+ });+});