README Source History Refs Reviews Compare Notes Search Review guide CI runs fix: search all retained memory in recall e60c1696a32892b0fa0b8d465121fb0af4ea7129
Oliver Jan Krylow <oliver@bugabinga.net > · 14 Sep 2026
SSH signature · not checked
Browse files Compare with HEAD
Push filtering into hot SQLite queries, scan every overlapping archive segment, and merge both tiers under one deterministic newest-first order. Keep Unicode case matching identical across hot and archived data.
Changed files (2) Diff layout Auto Unified Split
src/service/ops.int.test.ts Modified +136 / −0 text lines
47 47 assert.equal ((since[0 ]?.payload as { note: string }).note, 'payload-a' ) assert.equal ((since[0 ]?.payload as { note: string }).note, 'payload-a' )
50 + test ('recall searches beyond the newest page in hot data and archives' , async () => {51 + const { ops } = await freshOps ()52 + const recent = Array .from ({ length: 20 }, (_, i) => 53 + audit (`noise-${i}` , `2026-09-${ String (i + 1). padStart (2, '0')}T00:00:00Z` ),56 + audit ('cold-needle' , '2026-01-01T00:00:00Z' , 'forgotten-audit' ),57 + audit ('hot-needle' , '2026-08-01T00:00:00Z' , 'remembered-audit' ),62 + id: 'chat-cold-needle' ,66 + ts: '2024-01-01T00:00:00Z' ,67 + senderName: 'old friend' ,68 + text: 'remember this phrase' ,70 + ... recent.map ((_, i) => ({71 + id: `chat-noise-${i}` ,74 + direction: 'in' as const ,75 + ts: `2026-08-${ String (i + 1). padStart (2, '0')}T00:00:00Z` ,76 + text: 'routine chatter' ,80 + const hot = await ops.recall ({ kind: 'audit' , q: 'remembered-audit' , limit: 2 })82 + (hot.rows as { id: string }[]).map ((r) => r.id ),86 + await ops.archiveCycle ()87 + const coldAudit = await ops.recall ({ kind: 'audit' , q: 'forgotten-audit' , limit: 2 })89 + (coldAudit.rows as { id: string }[]).map ((r) => r.id ),92 + assert.equal (coldAudit.cold, true )93 + const coldChat = await ops.recall ({ kind: 'chat' , q: 'remember this phrase' , limit: 2 })95 + (coldChat.rows as { id: string }[]).map ((r) => r.id ),96 + ['chat-cold-needle' ],98 + assert.equal (coldChat.cold, true )101 + test ('recall orders overlapping archives and late hot rows globally' , async () => {102 + const { ops } = await freshOps ()104 + audit ('jan-01' , '2026-01-01T00:00:00Z' , 'shared-needle' ),105 + audit ('jan-30' , '2026-01-30T00:00:00Z' , 'shared-needle' ),107 + await ops.archiveCycle ()108 + ops.ingestAudit ([audit ('jan-15' , '2026-01-15T00:00:00Z' , 'shared-needle' )])109 + await ops.archiveCycle ()110 + ops.ingestAudit ([audit ('late-jan-02' , '2026-01-02T00:00:00Z' , 'shared-needle' )])112 + const result = await ops.recall ({ kind: 'audit' , q: 'shared-needle' , limit: 1 })113 + assert.equal ((result.rows [0 ] as { id: string }).id , 'jan-30' )114 + assert.equal (result.cold, true )117 + test ('recall has identical Unicode case matching before and after archival' , async () => {118 + const { ops } = await freshOps ()120 + audit ('kind-unicode' , '2026-01-01T00:00:00Z' , 'ÄPFEL' ),121 + audit ('ÖL' , '2026-01-02T00:00:00Z' ),129 + ts: '2024-01-01T00:00:00Z' ,135 + for (const query of [136 + { kind: 'audit' as const , q: 'äpfel' , id: 'kind-unicode' },137 + { kind: 'audit' as const , q: 'öl' , id: 'ÖL' },138 + { kind: 'chat' as const , q: 'änne' , id: 'chat-unicode' },139 + { kind: 'chat' as const , q: 'grüsse' , id: 'chat-unicode' },141 + const hot = await ops.recall ({ kind: query.kind, q: query.q, limit: 2 })142 + assert.equal ((hot.rows [0 ] as { id: string }).id , query.id )145 + await ops.archiveCycle ()146 + for (const query of [147 + { kind: 'audit' as const , q: 'äpfel' , id: 'kind-unicode' },148 + { kind: 'audit' as const , q: 'öl' , id: 'ÖL' },149 + { kind: 'chat' as const , q: 'änne' , id: 'chat-unicode' },150 + { kind: 'chat' as const , q: 'grüsse' , id: 'chat-unicode' },152 + const cold = await ops.recall ({ kind: query.kind, q: query.q, limit: 2 })153 + assert.equal ((cold.rows [0 ] as { id: string }).id , query.id )154 + assert.equal (cold.cold, true )158 + test ('recall tie ordering is limit-prefix consistent before and after archival' , async () => {159 + const { ops } = await freshOps ()160 + const ts = '2026-01-01T00:00:00Z' 161 + ops.ingestAudit ([audit ('z' , ts, 'tie' ), audit ('a' , ts, 'tie' )])163 + { id: 'z' , chatId: - 100 , messageId: 1 , direction: 'in' , ts, text: 'tie' },164 + { id: 'a' , chatId: - 100 , messageId: 2 , direction: 'in' , ts, text: 'tie' },167 + const assertOrder = async (kind: 'audit' | 'chat' ) => {168 + const one = await ops.recall ({ kind, q: 'tie' , limit: 1 })169 + const two = await ops.recall ({ kind, q: 'tie' , limit: 2 })171 + (one.rows as { id: string }[]).map ((r) => r.id ),175 + (two.rows as { id: string }[]).map ((r) => r.id ),179 + await assertOrder ('audit' )180 + await assertOrder ('chat' )181 + await ops.archiveCycle ()182 + await assertOrder ('audit' )183 + await assertOrder ('chat' )50 186 test ('chat ingest is idempotent and hide tombstones' , async () => {test ('chat ingest is idempotent and hide tombstones' , async () => {51 187 const { ops } = await freshOps () const { ops } = await freshOps ()52 188 const msg: ChatMessageIn = { const msg: ChatMessageIn = {src/service/ops.ts Modified +135 / −28 text lines
279 279 const coldFill = async <T >( const coldFill = async <T >(280 280 kind: 'audit' | 'chat' , kind: 'audit' | 'chat' ,281 - until: string | undefined ,281 + range: { since?: string ; until?: string },282 282 seen: Set <string >, seen: Set <string >,283 283 budget: number , budget: number ,284 284 mapRecord : (r: ArchivedRecord ) => T , mapRecord : (r: ArchivedRecord ) => T ,
285 + matches : (r: ArchivedRecord ) => boolean = () => true ,285 286 ): Promise <T []> => { ): Promise <T []> => {286 287 const segments = (await listSidecars (dirs.archiveDir, kind)).map ((s) => ({ const segments = (await listSidecars (dirs.archiveDir, kind)).map ((s) => ({287 288 ... s.sidecar, ... s.sidecar,288 289 zstPath: s.zstPath, zstPath: s.zstPath,290 - const overlapping = segmentsOverlapping (segments, until ? { until } : { } ).sort ((a, b) => 291 + const overlapping = segmentsOverlapping (segments, range ).sort ((a, b) => 291 292 b.firstTs.localeCompare (a.firstTs), b.firstTs.localeCompare (a.firstTs),293 294 const out: T [] = [] const out: T [] = []294 295 outer : for (const seg of overlapping) { outer : for (const seg of overlapping) {295 - const records = await readSegment (seg.zstPath, ( ) => true , BATCH_LIMIT)296 + const records = await readSegment (seg.zstPath, matches , BATCH_LIMIT)296 297 const mapped = records const mapped = records297 298 .map (mapRecord) .map (mapRecord)298 299 .sort ((a, b) => (b as { ts: string }).ts.localeCompare ((a as { ts: string }).ts)) .sort ((a, b) => (b as { ts: string }).ts.localeCompare ((a as { ts: string }).ts))307 308 return out return out
311 + const coldSearch = async <T extends { id: string ; ts: string }>(312 + kind: 'audit' | 'chat' ,313 + range: { since?: string ; until?: string },315 + mapRecord : (r: ArchivedRecord ) => T ,316 + matches : (r: ArchivedRecord ) => boolean ,317 + ): Promise <T []> => {318 + const segments = (await listSidecars (dirs.archiveDir, kind)).map ((s) => ({323 + for (const seg of segmentsOverlapping (segments, range)) {324 + for (const record of await readSegment (seg.zstPath, matches, BATCH_LIMIT)) {325 + if (seen.has (record.id )) continue 327 + out.push (mapRecord (record))330 + return out.sort ((a, b) => b.ts.localeCompare (a.ts) || b.id .localeCompare (a.id ))333 + const newestFirst = (a: { id: string ; ts: string }, b: { id: string ; ts: string }) => 334 + b.ts.localeCompare (a.ts) || Buffer .compare (Buffer .from (b.id ), Buffer .from (a.id ))336 + db.function ('unicode_lower' , { deterministic: true }, (value) => 337 + String (value ?? '' ).toLowerCase (),310 340 const ops = { const ops = {311 341 ingestAudit (events: AuditEvent []): IngestResult { ingestAudit (events: AuditEvent []): IngestResult {312 342 let accepted = 0 let accepted = 0 544 574 const until = hot.length > 0 ? hot[hot.length - 1 ]?.ts : undefined const until = hot.length > 0 ? hot[hot.length - 1 ]?.ts : undefined 545 575 const cold = await coldFill <TimelineRow >( const cold = await coldFill <TimelineRow >(577 + until === undefined ? {} : { until },548 578 new Set (hot.map ((r) => r.id )), new Set (hot.map ((r) => r.id )),549 579 limit - hot.length , limit - hot.length ,588 618 const until = hot.length > 0 ? hot[hot.length - 1 ]?.ts : undefined const until = hot.length > 0 ? hot[hot.length - 1 ]?.ts : undefined 589 619 const cold = await coldFill <ChatViewRow >( const cold = await coldFill <ChatViewRow >(621 + until === undefined ? {} : { until },592 622 new Set (hot.map ((r) => r.id )), new Set (hot.map ((r) => r.id )),593 623 limit - hot.length , limit - hot.length ,602 632 async recall (query: RecallQuery): Promise < RecallResponse> { async recall (query: RecallQuery): Promise < RecallResponse> {603 633 const limit = Math .min (query.limit ?? 50 , 500 ) const limit = Math .min (query.limit ?? 50 , 500 )604 - const q = query.q?.toLowerCase ()634 + const q = query.q?.toLowerCase () || undefined 635 + const range: { since?: string; until?: string } = {}636 + if (query.since !== undefined ) range.since = query.since637 + if (query.until !== undefined ) range.until = query.until605 638 switch (query.kind) { switch (query.kind) {606 639 case 'audit' : { case 'audit' : {607 - const page = await ops.timeline ({ limit: limit * 2 })608 - const rows = page.rows .filter (610 - (! query.since || r.ts >= query.since) && 611 - (! query.until || r.ts <= query.until) && 613 - r.kind.toLowerCase ().includes (q) || 614 - JSON .stringify (r.payload).toLowerCase ().includes (q)),643 + `SELECT * FROM audit_events 644 + WHERE (? IS NULL OR ts >= ?) AND (? IS NULL OR ts <= ?) 645 + AND (? IS NULL OR instr(unicode_lower(kind), ?) > 0 OR instr(unicode_lower(payload), ?) > 0) 646 + ORDER BY ts DESC, id DESC LIMIT ?` ,649 + query.since ?? null ,650 + query.since ?? null ,651 + query.until ?? null ,652 + query.until ?? null ,657 + ) as unknown as AuditRowDb []658 + ).map ((r) => auditDbToRow (r))659 + const cold = await coldSearch <TimelineRow >(662 + new Set (hot.map ((r) => r.id )),664 + const rec = r.record as {667 + provenance?: TimelineRow ['provenance' ]674 + sessionId: rec.sessionId,675 + provenance: rec.provenance,676 + payload: rec.payload,678 + } satisfies TimelineRow681 + const rec = r.record as { kind: string ; payload?: unknown }684 + rec.kind.toLowerCase ().includes (q) || 685 + JSON .stringify (rec.payload ?? null )688 + (! query.since || r.ts >= query.since) && 689 + (! query.until || r.ts <= query.until)616 - return { rows: rows.slice (0 , limit), cold: page.cold }693 + const rows = [... hot, ... cold].sort (newestFirst).slice (0 , limit)694 + return { rows, cold: rows.some ((r) => r.archived === true ) }618 696 case 'chat' : { case 'chat' : {619 - const page = await ops.chatPage (620 - query.until === undefined 621 - ? { limit: limit * 2 }622 - : { limit: limit * 2 , before: query.until },697 + const hidden = hiddenMessageIds ()701 + `SELECT * FROM chat_messages 702 + WHERE (? IS NULL OR ts >= ?) AND (? IS NULL OR ts <= ?) 703 + AND (? IS NULL OR instr(unicode_lower(text), ?) > 0 704 + OR instr(unicode_lower(sender_name), ?) > 0) 706 + SELECT 1 FROM tombstones WHERE tombstones.message_id = chat_messages.message_id 708 + ORDER BY ts DESC, id DESC LIMIT ?` ,711 + query.since ?? null ,712 + query.since ?? null ,713 + query.until ?? null ,714 + query.until ?? null ,719 + ) as unknown as ChatRowDb []720 + ).map ((r) => chatDbToRow (r, false ))721 + const cold = await coldSearch <ChatViewRow >(724 + new Set (hot.map ((r) => r.id )),725 + (r) => ({ ... (r.record as ChatViewRow ), archived: true , hidden: false }),727 + const rec = r.record as ChatViewRow 729 + ! hidden.has (rec.messageId) && 731 + (rec.text ?? '' ).toLowerCase ().includes (q) || 732 + (rec.senderName ?? '' ).toLowerCase ().includes (q)) && 733 + (! query.since || r.ts >= query.since) && 734 + (! query.until || r.ts <= query.until)624 - const rows = page.rows .filter (627 - (! query.since || r.ts >= query.since) && 629 - (r.text ?? '' ).toLowerCase ().includes (q) || 630 - (r.senderName ?? '' ).toLowerCase ().includes (q)),632 - return { rows: rows.slice (0 , limit), cold: page.cold }738 + const rows = [... hot, ... cold].sort (newestFirst).slice (0 , limit)739 + return { rows, cold: rows.some ((r) => r.archived === true ) }634 741 case 'deploy' : { case 'deploy' : {635 742 const rows = db const rows = db