@@ -12,6 +12,7 @@ import { countThreadSearchOccurrences } from "@t3tools/shared/threadSearch";
1212import * as Cache from "effect/Cache" ;
1313import * as Data from "effect/Data" ;
1414import * as Effect from "effect/Effect" ;
15+ import * as RcRef from "effect/RcRef" ;
1516import * as Schema from "effect/Schema" ;
1617import * as Stream from "effect/Stream" ;
1718import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
@@ -38,6 +39,14 @@ interface FindDocument extends FindRow {
3839 readonly runId : OrchestrationV2TurnItem [ "runId" ] ;
3940 readonly count : number ;
4041}
42+ type CompletedScans = Map <
43+ string ,
44+ {
45+ sequence : number ;
46+ documents : readonly FindDocument [ ] ;
47+ sourceThreadIds : readonly ThreadId [ ] ;
48+ }
49+ > ;
4150class TextKey extends Data . Class < {
4251 text : string ;
4352 role : "user" | "assistant" | "plan" ;
@@ -232,26 +241,21 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
232241 } ) ;
233242 } ) ;
234243 } ) ;
235- // Scans visit every message; a cache smaller than the thread churns without hits.
236- const textCache = yield * Cache . make ( {
237- capacity : 8192 ,
238- timeToLive : "1 minute" ,
239- lookup : ( key : TextKey ) => Effect . sync ( ( ) => parseText ( key ) ) ,
240- } ) ;
241- // Bounded separately because each entry can hold a large body; typing reuses the parse.
242- const largeTextCache = yield * Cache . make ( {
243- capacity : 32 ,
244- timeToLive : "1 minute" ,
245- lookup : ( key : TextKey ) => Effect . sync ( ( ) => parseText ( key ) ) ,
244+ const makeTextCache = ( capacity : number ) =>
245+ Cache . make ( { capacity, lookup : ( key : TextKey ) => Effect . sync ( ( ) => parseText ( key ) ) } ) ;
246+ // Text keys are message content, so entries never go stale. Cache TTL only expires on access
247+ // and would keep the last searched threads forever. Each search borrows this for its whole
248+ // stream, and all of it is dropped once Find is idle.
249+ const findCaches = yield * RcRef . make ( {
250+ acquire : Effect . all ( {
251+ // Scans visit every message; a cache smaller than the thread churns without hits.
252+ text : makeTextCache ( 8192 ) ,
253+ // Bounded separately because each entry can hold a large body; typing reuses the parse.
254+ largeText : makeTextCache ( 32 ) ,
255+ scans : Effect . sync ( ( ) : CompletedScans => new Map ( ) ) ,
256+ } ) ,
257+ idleTimeToLive : "1 minute" ,
246258 } ) ;
247- const scans = new Map <
248- string ,
249- {
250- sequence : number ;
251- documents : readonly FindDocument [ ] ;
252- sourceThreadIds : readonly ThreadId [ ] ;
253- }
254- > ( ) ;
255259 const readRevision = Effect . fn ( "ThreadFind.readRevision" ) ( function * (
256260 threadIds : readonly ThreadId [ ] ,
257261 ) {
@@ -275,7 +279,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
275279 return revision [ 0 ] ?. sequence ?? 0 ;
276280 } ) ;
277281 const snapshot = Effect . fn ( "ThreadFind.snapshot" ) (
278- function * ( input : OrchestrationV2SearchThreadInput ) {
282+ function * ( input : OrchestrationV2SearchThreadInput , scans : CompletedScans ) {
279283 const { threadId } = input ;
280284 const active = yield * sql < { cwd : string | null } > `
281285 SELECT COALESCE(json_extract(t.payload_json, '$.worktreePath'), p.workspace_root) AS cwd
@@ -326,8 +330,9 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
326330 const searchThreadStream = ( input : OrchestrationV2SearchThreadInput ) =>
327331 Stream . unwrap (
328332 Effect . gen ( function * ( ) {
333+ const { scans, ...textCache } = yield * RcRef . get ( findCaches ) ;
329334 // Release SQLite before decoding and parsing. Both frames describe one snapshot.
330- const data = yield * snapshot ( input ) ;
335+ const data = yield * snapshot ( input , scans ) ;
331336 if ( data . result ) return Stream . succeed ( data . result ) ;
332337 const { payloads, cwd, sequence, cacheKey, sourceThreadIds } = data ;
333338 const decode = ( row : FindPayload ) =>
@@ -357,7 +362,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
357362 const decoded = yield * decode ( row ) ;
358363 const key = textKey ( decoded . item , cwd , input . skills ?? [ ] ) ;
359364 const segments = yield * Cache . get (
360- key . text . length <= 32_768 ? textCache : largeTextCache ,
365+ key . text . length <= 32_768 ? textCache . text : textCache . largeText ,
361366 key ,
362367 ) ;
363368 const document = documentFor ( decoded , countSegments ( segments , input . query ) ) ;
0 commit comments