@devries-cameron20 / t3code-1 / commits / c90b67859a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): Find frees searched thread text after one idle minute (#34132)

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

Theo Browne committed Nov 10, 2026 at 19:00 UTC c90b67859a6d85fc957f9ffe25ba95113c6479d3
2 files changed +66 -22
apps/server/src/orchestration-v2/ThreadFind.test.ts
+39
@@ -1,4 +1,5 @@
1 import { assert, it, describe } from "@effect/vitest";
2 +import { searchableMessageSegments } from "@t3tools/shared/threadFindText";
3 import {
4 EventId,
5 MessageId,
@@ -19,6 +20,8 @@ import * as Effect from "effect/Effect";
20 import * as Layer from "effect/Layer";
21 import * as SqlClient from "effect/sql/SqlClient";
22 import * as Stream from "effect/Stream";
23 +import * as TestClock from "effect/testing/TestClock";
24 +import { vi } from "vite-plus/test";
25
26 import * as SqlitePersistence from "../persistence/Sqlite.ts";
27 import * as ProjectionStore from "./ProjectionStore.ts";
@@ -31,6 +34,8 @@ import {
34 } from "./threadHistoryPaging.ts";
35 import { projectTurnItemForWire } from "./WireProjection.ts";
36
37 +vi.mock("@t3tools/shared/threadFindText", { spy: true });
38 +
39 const layerTest = Layer.mergeAll(EventStore.layer, ProjectionStore.layer, ProjectStore.layer).pipe(
40 Layer.provideMerge(SqlitePersistence.layerMemory),
41 );
@@ -460,6 +465,40 @@ describe("V2 thread find", () => {
465 }).pipe(Effect.provide(layerTest)),
466 );
467
468 + it.effect("keeps its caches while Find is in use and drops them after one idle minute", () =>
469 + Effect.gen(function* () {
470 + const projection = yield* setup;
471 + yield* putItems([item("one", 1, "idle needle one"), item("two", 2, "idle needle two")]);
472 + const parses = () =>
473 + vi
474 + .mocked(searchableMessageSegments)
475 + .mock.calls.filter(([message]) => message.text.startsWith("idle needle")).length;
476 + const search = (query: string) => projection.searchThread({ threadId, query });
477 + // One idle minute passes inside this search. The later counts fail if it let the caches go.
478 + yield* projection.searchThreadStream({ threadId, query: "needle" }).pipe(
479 + Stream.tap((frame) =>
480 + frame.complete === false ? TestClock.adjust("1 minute") : Effect.void,
481 + ),
482 + Stream.runDrain,
483 + );
484 + assert.equal(parses(), 2);
485 + yield* TestClock.adjust("40 seconds");
486 + yield* search("idle");
487 + yield* TestClock.adjust("40 seconds");
488 + // A repeated query is answered from the completed scan and still counts as use.
489 + const repeated = yield* projection
490 + .searchThreadStream({ threadId, query: "needle" })
491 + .pipe(Stream.runCollect);
492 + assert.equal(repeated.length, 1);
493 + yield* TestClock.adjust("40 seconds");
494 + yield* search("one");
495 + assert.equal(parses(), 2);
496 + yield* TestClock.adjust("1 minute");
497 + yield* search("needle");
498 + assert.equal(parses(), 4);
499 + }).pipe(Effect.provide(layerTest)),
500 + );
501 +
502 it.effect("invalidates cached counts when streamed text changes or a run rolls back", () =>
503 Effect.gen(function* () {
504 const projection = yield* setup;
apps/server/src/orchestration-v2/ThreadFind.ts
+27 -22
@@ -12,6 +12,7 @@ import { countThreadSearchOccurrences } from "@t3tools/shared/threadSearch";
12 import * as Cache from "effect/Cache";
13 import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 +import * as RcRef from "effect/RcRef";
16 import * as Schema from "effect/Schema";
17 import * as Stream from "effect/Stream";
18 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
@@ -38,6 +39,14 @@ interface FindDocument extends FindRow {
39 readonly runId: OrchestrationV2TurnItem["runId"];
40 readonly count: number;
41 }
42 +type CompletedScans = Map<
43 + string,
44 + {
45 + sequence: number;
46 + documents: readonly FindDocument[];
47 + sourceThreadIds: readonly ThreadId[];
48 + }
49 +>;
50 class TextKey extends Data.Class<{
51 text: string;
52 role: "user" | "assistant" | "plan";
@@ -232,26 +241,21 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
241 });
242 });
243 });
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",
258 });
247 - const scans = new Map<
248 - string,
249 - {
250 - sequence: number;
251 - documents: readonly FindDocument[];
252 - sourceThreadIds: readonly ThreadId[];
253 - }
254 - >();
259 const readRevision = Effect.fn("ThreadFind.readRevision")(function* (
260 threadIds: readonly ThreadId[],
261 ) {
@@ -275,7 +279,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
279 return revision[0]?.sequence ?? 0;
280 });
281 const snapshot = Effect.fn("ThreadFind.snapshot")(
278 - function* (input: OrchestrationV2SearchThreadInput) {
282 + function* (input: OrchestrationV2SearchThreadInput, scans: CompletedScans) {
283 const { threadId } = input;
284 const active = yield* sql<{ cwd: string | null }>`
285 SELECT COALESCE(json_extract(t.payload_json, '$.worktreePath'), p.workspace_root) AS cwd
@@ -326,8 +330,9 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
330 const searchThreadStream = (input: OrchestrationV2SearchThreadInput) =>
331 Stream.unwrap(
332 Effect.gen(function* () {
333 + const { scans, ...textCache } = yield* RcRef.get(findCaches);
334 // Release SQLite before decoding and parsing. Both frames describe one snapshot.
330 - const data = yield* snapshot(input);
335 + const data = yield* snapshot(input, scans);
336 if (data.result) return Stream.succeed(data.result);
337 const { payloads, cwd, sequence, cacheKey, sourceThreadIds } = data;
338 const decode = (row: FindPayload) =>
@@ -357,7 +362,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
362 const decoded = yield* decode(row);
363 const key = textKey(decoded.item, cwd, input.skills ?? []);
364 const segments = yield* Cache.get(
360 - key.text.length <= 32_768 ? textCache : largeTextCache,
365 + key.text.length <= 32_768 ? textCache.text : textCache.largeText,
366 key,
367 );
368 const document = documentFor(decoded, countSegments(segments, input.query));