@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 1,043 lines 39 KB
Raw
1 import { assert, it, describe } from "@effect/vitest";
2 import { searchableMessageSegments } from "@t3tools/shared/threadFindText";
3 import {
4 EventId,
5 MessageId,
6 PlanId,
7 RunId,
8 RuntimeRequestId,
9 TurnItemId,
10 type OrchestrationV2TurnItem,
11 type OrchestrationV2Run,
12 type OrchestrationV2ThreadHistoryPage,
13 ProjectId,
14 ProviderInstanceId,
15 ThreadId,
16 type OrchestrationV2DomainEvent,
17 } from "@t3tools/contracts";
18 import * as DateTime from "effect/DateTime";
19 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 * as Tracer from "effect/Tracer";
25 import { vi } from "vite-plus/test";
26
27 import * as SqlitePersistence from "../persistence/Sqlite.ts";
28 import * as ProjectionStore from "./ProjectionStore.ts";
29 import * as ProjectStore from "./ProjectStore.ts";
30 import * as EventStore from "./EventStore.ts";
31 import {
32 encodeThreadHistoryCursor,
33 selectHistoryPageFromCursor,
34 THREAD_HISTORY_PAGE_POLICY,
35 } from "./threadHistoryPaging.ts";
36 import { projectTurnItemForWire } from "./WireProjection.ts";
37
38 vi.mock("@t3tools/shared/threadFindText", { spy: true });
39
40 const layerTest = Layer.mergeAll(EventStore.layer, ProjectionStore.layer, ProjectStore.layer).pipe(
41 Layer.provideMerge(SqlitePersistence.layerMemory),
42 );
43
44 const providerInstanceId = ProviderInstanceId.make("codex");
45 const at = (minute: number) => DateTime.makeUnsafe(Date.UTC(2026, 8, 27, 0, minute));
46
47 const createProject = (projectId: ProjectId) =>
48 Effect.flatMap(ProjectStore.ProjectStoreV2, (projects) =>
49 projects.apply({
50 sequence: 0,
51 eventId: EventId.make(`created:${projectId}`),
52 aggregateKind: "project",
53 aggregateId: projectId,
54 occurredAt: DateTime.formatIso(at(0)),
55 commandId: null,
56 causationEventId: null,
57 correlationId: null,
58 metadata: {},
59 type: "project.created",
60 payload: {
61 projectId,
62 title: projectId,
63 workspaceRoot: `/work/${projectId}`,
64 defaultModelSelection: null,
65 scripts: [],
66 createdAt: DateTime.formatIso(at(0)),
67 updatedAt: DateTime.formatIso(at(0)),
68 },
69 }),
70 );
71
72 const thread = (
73 threadId: ThreadId,
74 projectId: ProjectId,
75 overrides: { readonly archivedAt?: DateTime.Utc; readonly deletedAt?: DateTime.Utc } = {},
76 ): OrchestrationV2DomainEvent => ({
77 id: EventId.make(`created:${threadId}`),
78 type: "thread.created",
79 threadId,
80 providerInstanceId,
81 occurredAt: at(0),
82 payload: {
83 createdBy: "user",
84 creationSource: "web",
85 id: threadId,
86 projectId,
87 title: threadId,
88 providerInstanceId,
89 modelSelection: { instanceId: providerInstanceId, model: "gpt-5" },
90 runtimeMode: "full-access",
91 interactionMode: "default",
92 branch: null,
93 worktreePath: null,
94 activeProviderThreadId: null,
95 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
96 forkedFrom: null,
97 createdAt: at(0),
98 updatedAt: at(0),
99 archivedAt: overrides.archivedAt ?? null,
100 settledOverride: null,
101 settledAt: null,
102 lastVisitedAt: null,
103 deletedAt: overrides.deletedAt ?? null,
104 },
105 });
106
107 const threadId = ThreadId.make("thread:find");
108 const projectId = ProjectId.make("project:find");
109 const runId = RunId.make("run:find");
110
111 function item(
112 id: string,
113 ordinal: number,
114 text: string,
115 kind: "user_message" | "assistant_message" | "proposed_plan" = "assistant_message",
116 owner = threadId,
117 ): Extract<
118 OrchestrationV2TurnItem,
119 { type: "user_message" | "assistant_message" | "proposed_plan" }
120 > {
121 const base = {
122 id: TurnItemId.make(id),
123 threadId: owner,
124 runId,
125 nodeId: null,
126 providerThreadId: null,
127 providerTurnId: null,
128 nativeItemRef: null,
129 parentItemId: null,
130 ordinal,
131 status: "completed" as const,
132 title: null,
133 startedAt: at(100 - ordinal),
134 completedAt: at(100 - ordinal),
135 updatedAt: at(1),
136 };
137 if (kind === "proposed_plan")
138 return { ...base, type: kind, planId: PlanId.make(id), markdown: text, streaming: false };
139 if (kind === "user_message")
140 return {
141 ...base,
142 type: kind,
143 messageId: MessageId.make(id),
144 text,
145 inputIntent: "turn_start",
146 attachments: [],
147 createdBy: "user",
148 creationSource: "web",
149 };
150 return { ...base, type: kind, messageId: MessageId.make(id), text, streaming: false };
151 }
152
153 function run(
154 owner = threadId,
155 id = runId,
156 ordinal = 1,
157 status: OrchestrationV2Run["status"] = "completed",
158 ): Extract<OrchestrationV2DomainEvent, { type: "run.created" }> {
159 return {
160 id: EventId.make(`run:${owner}:${id}:${status}`),
161 threadId: owner,
162 occurredAt: at(1),
163 type: "run.created",
164 payload: {
165 id,
166 threadId: owner,
167 ordinal,
168 providerInstanceId,
169 modelSelection: { instanceId: providerInstanceId, model: "gpt-6" },
170 providerThreadId: null,
171 userMessageId: MessageId.make("prompt"),
172 rootNodeId: null,
173 activeAttemptId: null,
174 status,
175 requestedAt: at(1),
176 startedAt: at(1),
177 completedAt: at(2),
178 checkpointId: null,
179 contextHandoffId: null,
180 },
181 };
182 }
183
184 const commit = Effect.fn("ThreadFindTest.commit")(function* (
185 events: readonly OrchestrationV2DomainEvent[],
186 ) {
187 const store = yield* EventStore.EventStoreV2;
188 const projection = yield* ProjectionStore.ProjectionStoreV2;
189 yield* store.append({ events });
190 yield* Effect.forEach(events, projection.apply, { discard: true });
191 });
192 const putItems = (items: readonly OrchestrationV2TurnItem[], version = "initial") =>
193 commit(
194 items.map((payload) => ({
195 id: EventId.make(`${payload.threadId}:${payload.id}:${version}`),
196 threadId: payload.threadId,
197 occurredAt: at(1),
198 type: "turn-item.updated" as const,
199 payload,
200 })),
201 );
202 const setup = Effect.gen(function* () {
203 yield* createProject(projectId);
204 yield* commit([thread(threadId, projectId), run()]);
205 return yield* ProjectionStore.ProjectionStoreV2;
206 });
207
208 const tracePayloadLoads = () => {
209 let batches = 0;
210 const tracer = Tracer.make({
211 span(options) {
212 if (options.name === "ThreadFind.load") batches++;
213 return new Tracer.NativeSpan(options);
214 },
215 });
216 return { tracer, batches: () => batches };
217 };
218
219 describe("V2 thread find", () => {
220 it.effect.each(["rendered text", "source text"] as const)(
221 "does not retain rendered snapshots over the %s budget",
222 (limit) =>
223 Effect.gen(function* () {
224 const projection = yield* setup;
225 yield* putItems(
226 Array.from({ length: 65 }, (_, i) =>
227 item(
228 `message:${i}`,
229 i,
230 limit === "rendered text"
231 ? "needle ".repeat(2500)
232 : `needle ![image](data:image/png;base64,${"a".repeat(35000)})`,
233 ),
234 ),
235 );
236 const loads = tracePayloadLoads();
237 const first = yield* projection
238 .searchThread({ threadId, query: "needle" })
239 .pipe(Effect.withTracer(loads.tracer));
240 const initialBatches = loads.batches();
241 const second = yield* projection
242 .searchThread({ threadId, query: "need" })
243 .pipe(Effect.withTracer(loads.tracer));
244 assert.equal(second.totalMatches, first.totalMatches);
245 assert.equal(loads.batches(), initialBatches * 2);
246 }).pipe(Effect.provide(layerTest)),
247 );
248
249 it.effect("finishes one snapshot while later searches include newly inserted matches", () =>
250 Effect.gen(function* () {
251 const projection = yield* setup;
252 yield* putItems([
253 item("selected", 1, "snapshot needle"),
254 item("last", 2, "snapshot last needle"),
255 ]);
256 yield* projection.searchThread({ threadId, query: "snapshot" });
257 const frames = yield* projection.searchThreadStream({ threadId, query: "needle" }).pipe(
258 Stream.tap((frame) =>
259 frame.complete === false
260 ? putItems([item("inserted", 0, "snapshot inserted needle")], "during-search")
261 : Effect.void,
262 ),
263 Stream.runCollect,
264 );
265 assert.equal(frames.length, 2);
266 assert.equal(frames[1]!.totalMatches, 2);
267 assert.deepEqual(frames[1]!.match, frames[0]!.match);
268 const updated = yield* projection.searchThread({
269 threadId,
270 query: "needle",
271 start: { entryId: "selected", occurrence: 0 },
272 });
273 assert.equal(updated.totalMatches, 3);
274 assert.equal(updated.activeIndex, 1);
275 assert.equal(updated.match?.entryId, "selected");
276 }).pipe(Effect.provide(layerTest)),
277 );
278
279 it.effect("returns bounded navigation counts around the selection, including wraparound", () =>
280 Effect.gen(function* () {
281 const projection = yield* setup;
282 yield* putItems(
283 Array.from({ length: 30 }, (_, i) =>
284 item(`message:${i}`, i, "needle ".repeat((i % 3) + 1)),
285 ),
286 );
287 const current = yield* projection.searchThread({
288 threadId,
289 query: "needle",
290 start: { entryId: "message:14", occurrence: 1 },
291 });
292 assert.equal(current.totalMatches, 60);
293 assert.equal(current.activeIndex, 28);
294 assert.equal(current.navigation?.length, 17);
295 assert.deepEqual(
296 current.navigation?.map((entry) => entry.entryId),
297 Array.from({ length: 17 }, (_, i) => `message:${i + 6}`),
298 );
299 assert.deepEqual(
300 current.navigation?.find((entry) => entry.entryId === "message:14"),
301 {
302 entryId: "message:14",
303 runId,
304 startIndex: 27,
305 count: 3,
306 },
307 );
308 const first = yield* projection.searchThread({ threadId, query: "needle" });
309 assert.equal(first.navigation?.length, 17);
310 assert.include(first.navigation?.map((entry) => entry.entryId) ?? [], "message:29");
311 const empty = yield* projection.searchThread({ threadId, query: "absent" });
312 assert.deepEqual(empty.navigation, []);
313 }).pipe(Effect.provide(layerTest)),
314 );
315
316 it.effect("keeps navigation compact for a message with thousands of occurrences", () =>
317 Effect.gen(function* () {
318 const projection = yield* setup;
319 yield* putItems([item("many", 1, "needle ".repeat(5000))]);
320 const result = yield* projection.searchThread({ threadId, query: "needle", index: 2000 });
321 assert.equal(result.totalMatches, 5000);
322 assert.equal(result.match?.occurrence, 2000);
323 assert.deepEqual(result.navigation, [{ entryId: "many", runId, startIndex: 0, count: 5000 }]);
324 assert.isBelow(JSON.stringify(result).length, 500);
325 }).pipe(Effect.provide(layerTest)),
326 );
327
328 it.effect("cycles relative to the current match when new matches are inserted", () =>
329 Effect.gen(function* () {
330 const projection = yield* setup;
331 yield* putItems([
332 item("first", 10, "needle"),
333 item("current", 20, "needle"),
334 item("last", 30, "needle"),
335 ]);
336 const initial = yield* projection.searchThread({ threadId, query: "needle", index: 1 });
337 assert.equal(initial.match?.entryId, "current");
338 yield* putItems([
339 item("new-before", 15, "needle"),
340 item("new-after", 25, "needle"),
341 item("new-last", 40, "needle"),
342 ]);
343 const start = { entryId: "current", occurrence: 0 };
344 const refreshed = yield* projection.searchThread({ threadId, query: "needle", start });
345 assert.equal(refreshed.totalMatches, 6);
346 assert.equal(refreshed.activeIndex, 2);
347 assert.equal(refreshed.match?.entryId, "current");
348 const next = yield* projection.searchThread({ threadId, query: "needle", start, offset: 1 });
349 assert.equal(next.match?.entryId, "new-after");
350 const previous = yield* projection.searchThread({
351 threadId,
352 query: "needle",
353 start,
354 offset: -1,
355 });
356 assert.equal(previous.match?.entryId, "new-before");
357 const end = yield* projection.searchThread({
358 threadId,
359 query: "needle",
360 start: { entryId: "last", occurrence: 0 },
361 offset: 1,
362 });
363 assert.equal(end.match?.entryId, "new-last");
364 const wrapped = yield* projection.searchThread({
365 threadId,
366 query: "needle",
367 start: { entryId: "new-last", occurrence: 0 },
368 offset: 1,
369 });
370 assert.equal(wrapped.match?.entryId, "first");
371 const backwards = yield* projection.searchThread({
372 threadId,
373 query: "needle",
374 start: { entryId: "first", occurrence: 0 },
375 offset: -1,
376 });
377 assert.equal(backwards.match?.entryId, "new-last");
378 }).pipe(Effect.provide(layerTest)),
379 );
380
381 it.effect("searches visible rendered messages and plans in canonical item order", () =>
382 Effect.gen(function* () {
383 const projection = yield* setup;
384 yield* putItems([
385 item("user", 1, "Check **the build**", "user_message"),
386 item("assistant", 2, "check the build twice: check the build"),
387 item("plan-item", 3, "# Check the build\nShip it", "proposed_plan"),
388 item("url", 4, "[documentation](https://hidden.example/needle)"),
389 ]);
390 const matches = [];
391 for (let index = 0; index < 4; index++) {
392 const result = yield* projection.searchThread({
393 threadId,
394 query: "CHECK THE BUILD",
395 index,
396 });
397 assert.equal(result.totalMatches, 4);
398 matches.push(result.match);
399 }
400 assert.deepEqual(matches, [
401 { entryId: "user", runId, occurrence: 0 },
402 { entryId: "assistant", runId, occurrence: 0 },
403 { entryId: "assistant", runId, occurrence: 1 },
404 { entryId: "plan-item", runId, occurrence: 0 },
405 ]);
406 assert.equal(
407 (yield* projection.searchThread({ threadId, query: "hidden.example" })).totalMatches,
408 0,
409 );
410 assert.equal(
411 (yield* projection.searchThread({ threadId, query: "check", index: 99 })).activeIndex,
412 3,
413 );
414 }).pipe(Effect.provide(layerTest)),
415 );
416
417 it.effect("skips the empty-response fallback the timeline hides beside an HTML render", () =>
418 Effect.gen(function* () {
419 const projection = yield* setup;
420 const shownRunId = RunId.make("run:shown");
421 yield* commit([run(threadId, shownRunId, 2)]);
422 yield* putItems([
423 {
424 ...item("render", 0, ""),
425 type: "dynamic_tool",
426 toolName: "html_render",
427 input: {},
428 output: { htmlRender: { attachmentId: "visual", title: "Result", height: 120 } },
429 },
430 item("hidden", 1, ""),
431 { ...item("shown", 2, ""), runId: shownRunId },
432 ]);
433 const result = yield* projection.searchThread({ threadId, query: "empty" });
434 assert.equal(result.totalMatches, 1);
435 assert.equal(result.match?.entryId, "shown");
436 const changed = yield* projection.searchThread({ threadId, query: "response" });
437 assert.equal(changed.totalMatches, 1);
438 assert.equal(changed.match?.entryId, "shown");
439 }).pipe(Effect.provide(layerTest)),
440 );
441
442 it.effect("searches displayed skill labels and invalidates parsed text when labels change", () =>
443 Effect.gen(function* () {
444 const projection = yield* setup;
445 yield* putItems([item("skill", 1, "Use $test-t3-app now")]);
446 const input = {
447 threadId,
448 query: "T3 App Testing",
449 skills: [{ name: "test-t3-app", displayName: "T3 App Testing" }],
450 };
451 assert.equal((yield* projection.searchThread(input)).totalMatches, 1);
452 assert.equal((yield* projection.searchThread({ ...input, skills: [] })).totalMatches, 0);
453 yield* putItems(
454 [item("skill-plan", 2, "# Verify\nUse $test-t3-app now", "proposed_plan")],
455 "plan",
456 );
457 assert.equal((yield* projection.searchThread(input)).totalMatches, 1);
458 assert.equal(
459 (yield* projection.searchThread({ ...input, query: "$test-t3-app" })).totalMatches,
460 1,
461 );
462 assert.equal((yield* projection.searchThread({ ...input, query: "App" })).totalMatches, 2);
463 }).pipe(Effect.provide(layerTest)),
464 );
465
466 it.effect("searches only the picture file chips visible in the viewer's theme", () =>
467 Effect.gen(function* () {
468 const projection = yield* setup;
469 yield* putItems([
470 item(
471 "picture",
472 1,
473 '<picture><source media="(prefers-color-scheme: dark)" srcset="/workspace/repo/notes.ts#L42-L64"><img src="/workspace/repo/screenshot.png" alt="notes"></picture>',
474 ),
475 item(
476 "fallback",
477 2,
478 '<picture><source media="(prefers-color-scheme: dark)" srcset="/workspace/repo/dark.png"><img src="/workspace/repo/fallback.ts#L42-L64" alt="notes"></picture>',
479 ),
480 ]);
481 const dark = { threadId, query: "L42–64", theme: "dark" } as const;
482 const light = { ...dark, theme: "light" } as const;
483 const darkResult = yield* projection.searchThread(dark);
484 assert.equal(darkResult.totalMatches, 1);
485 assert.equal(darkResult.match?.entryId, "picture");
486 assert.equal(
487 (yield* projection.searchThread({ ...light, query: "notes.ts" })).totalMatches,
488 0,
489 );
490 const lightResult = yield* projection.searchThread(light);
491 assert.equal(lightResult.totalMatches, 1);
492 assert.equal(lightResult.match?.entryId, "fallback");
493 assert.equal((yield* projection.searchThread(dark)).match?.entryId, "picture");
494 }).pipe(Effect.provide(layerTest)),
495 );
496
497 it.effect("refreshes file labels when the project cwd changes without a thread event", () =>
498 Effect.gen(function* () {
499 const projection = yield* setup;
500 const sql = yield* SqlClient.SqlClient;
501 yield* putItems([item("paths", 1, "[local](src/main.ts) [other](/elsewhere/src/main.ts)")]);
502 assert.equal(
503 (yield* projection.searchThread({ threadId, query: "project:find/src" })).totalMatches,
504 1,
505 );
506 yield* sql`UPDATE projection_projects SET workspace_root = '/elsewhere'
507 WHERE project_id = ${projectId}`;
508 assert.equal(
509 (yield* projection.searchThread({ threadId, query: "project:find" })).totalMatches,
510 0,
511 );
512 assert.equal(
513 (yield* projection.searchThread({ threadId, query: "main.ts" })).totalMatches,
514 2,
515 );
516 }).pipe(Effect.provide(layerTest)),
517 );
518
519 it.effect("never mixes message versions when the searched thread changes during a scan", () =>
520 Effect.gen(function* () {
521 const projection = yield* setup;
522 const sql = yield* SqlClient.SqlClient;
523 const items = Array.from({ length: 260 }, (_, i) => item(`message:${i}`, i, "needle"));
524 yield* putItems(items);
525 const updated = items.map((item) =>
526 item.type === "assistant_message" ? { ...item, text: "needle needle" } : item,
527 );
528 const [during] = yield* Effect.all(
529 [
530 projection.searchThread({ threadId, query: "needle" }),
531 sql.withTransaction(putItems(updated, "concurrent")),
532 ],
533 { concurrency: "unbounded" },
534 );
535 assert.include([260, 520], during.totalMatches);
536 assert.equal(
537 (yield* projection.searchThread({ threadId, query: "needle" })).totalMatches,
538 520,
539 );
540 }).pipe(Effect.provide(layerTest)),
541 );
542
543 it.effect("starts at the reading position and wraps when no later match exists", () =>
544 Effect.gen(function* () {
545 const projection = yield* setup;
546 yield* putItems([
547 item("first", 1, "needle"),
548 item("reading", 2, "needle above, needle below"),
549 item("between", 3, "nothing"),
550 item("last", 4, "needle"),
551 item("end", 5, "nothing"),
552 ]);
553 const from = (entryId: string, occurrence = 0) =>
554 projection.searchThread({ threadId, query: "needle", start: { entryId, occurrence } });
555 assert.equal((yield* from("reading")).activeIndex, 1);
556 const below = yield* from("reading", 1);
557 assert.equal(below.activeIndex, 2);
558 assert.equal(below.match?.occurrence, 1);
559 assert.equal((yield* from("between")).match?.entryId, "last");
560 assert.equal((yield* from("reading", 2)).activeIndex, 3);
561 assert.equal((yield* from("end")).match?.entryId, "first");
562 assert.equal((yield* from("missing")).activeIndex, 0);
563 const explicit = yield* projection.searchThread({
564 threadId,
565 query: "needle",
566 start: { entryId: "last", occurrence: 0 },
567 index: 1,
568 });
569 assert.equal(explicit.match?.entryId, "reading");
570 }).pipe(Effect.provide(layerTest)),
571 );
572
573 it.effect("finds matches beyond the recent history window", () =>
574 Effect.gen(function* () {
575 const projection = yield* setup;
576 const items = Array.from({ length: 520 }, (_, i) =>
577 item(`message:${i}`, i + 1, i === 8 || i === 510 ? "sentinel" : "other message"),
578 );
579 yield* putItems(items);
580 const loads = tracePayloadLoads();
581 const first = yield* projection
582 .searchThread({ threadId, query: "sentinel" })
583 .pipe(Effect.withTracer(loads.tracer));
584 assert.equal(loads.batches(), 5);
585 const changed = yield* projection
586 .searchThread({ threadId, query: "other message" })
587 .pipe(Effect.withTracer(loads.tracer));
588 assert.equal(changed.totalMatches, 518);
589 assert.equal(changed.match?.entryId, "message:0");
590 assert.equal(loads.batches(), 5);
591 const last = yield* projection.searchThread({ threadId, query: "sentinel", index: 1 });
592 assert.equal(first.totalMatches, 2);
593 assert.equal(first.match?.entryId, "message:8");
594 assert.equal(last.match?.entryId, "message:510");
595 }).pipe(Effect.provide(layerTest)),
596 );
597
598 it.effect("keeps its caches while Find is in use and drops them after one idle minute", () =>
599 Effect.gen(function* () {
600 const projection = yield* setup;
601 yield* putItems([item("one", 1, "idle needle one"), item("two", 2, "idle needle two")]);
602 const parses = () =>
603 vi
604 .mocked(searchableMessageSegments)
605 .mock.calls.filter(([message]) => message.text.startsWith("idle needle")).length;
606 const search = (query: string) => projection.searchThread({ threadId, query });
607 // One idle minute passes inside this search. The later counts fail if it let the caches go.
608 yield* projection.searchThreadStream({ threadId, query: "needle" }).pipe(
609 Stream.tap((frame) =>
610 frame.complete === false ? TestClock.adjust("1 minute") : Effect.void,
611 ),
612 Stream.runDrain,
613 );
614 assert.equal(parses(), 2);
615 yield* TestClock.adjust("40 seconds");
616 yield* search("idle");
617 yield* TestClock.adjust("40 seconds");
618 // A repeated query is answered from the completed scan and still counts as use.
619 const repeated = yield* projection
620 .searchThreadStream({ threadId, query: "needle" })
621 .pipe(Stream.runCollect);
622 assert.equal(repeated.length, 1);
623 yield* TestClock.adjust("40 seconds");
624 yield* search("one");
625 assert.equal(parses(), 2);
626 yield* TestClock.adjust("1 minute");
627 yield* search("needle");
628 assert.equal(parses(), 4);
629 }).pipe(Effect.provide(layerTest)),
630 );
631
632 it.effect("invalidates cached counts when streamed text changes or a run rolls back", () =>
633 Effect.gen(function* () {
634 const projection = yield* setup;
635 const original = item("streaming", 1, "needle");
636 yield* putItems([original]);
637 assert.equal((yield* projection.searchThread({ threadId, query: "needle" })).totalMatches, 1);
638 if (original.type !== "assistant_message") return;
639 yield* putItems([{ ...original, text: "needle needle", streaming: true }], "streaming");
640 assert.equal((yield* projection.searchThread({ threadId, query: "needle" })).totalMatches, 2);
641 assert.equal((yield* projection.searchThread({ threadId, query: "need" })).totalMatches, 2);
642 const updated = run(threadId, runId, 1, "rolled_back");
643 yield* commit([{ ...updated, type: "run.updated" }]);
644 assert.equal((yield* projection.searchThread({ threadId, query: "needle" })).totalMatches, 0);
645 }).pipe(Effect.provide(layerTest)),
646 );
647
648 it.effect(
649 "does not search undispatched inputs, cancelled queue rows, or folded question answers",
650 () =>
651 Effect.gen(function* () {
652 const projection = yield* setup;
653 const queuedRunId = RunId.make("queued:cancelled");
654 yield* commit([run(threadId, queuedRunId, 2, "cancelled")]);
655 const queued = item("cancelled", 1, "needle", "user_message");
656 if (queued.type !== "user_message") return;
657 const answer = item("async-answer:question", 2, "needle", "user_message");
658 const question: OrchestrationV2TurnItem = {
659 ...item("question", 3, ""),
660 type: "user_input_request",
661 requestId: RuntimeRequestId.make("question"),
662 questions: [],
663 questionAnswer: { requestId: "question", answers: {}, attachmentsByQuestionId: {} },
664 };
665 yield* putItems([
666 { ...queued, runId: queuedRunId, inputIntent: "queued_turn" },
667 answer,
668 question,
669 ]);
670 const event: OrchestrationV2DomainEvent = {
671 id: EventId.make("undispatched"),
672 threadId,
673 occurredAt: at(2),
674 type: "message.updated",
675 payload: {
676 id: MessageId.make("undispatched"),
677 threadId,
678 runId: null,
679 nodeId: null,
680 role: "user",
681 text: "needle",
682 attachments: [],
683 streaming: false,
684 createdBy: "user",
685 creationSource: "web",
686 createdAt: at(2),
687 updatedAt: at(2),
688 },
689 };
690 yield* commit([event]);
691 const result = yield* projection.searchThread({ threadId, query: "needle" });
692 assert.equal(result.totalMatches, 0);
693 assert.isNull(result.match);
694 assert.equal((yield* projection.searchThread({ threadId, query: "need" })).totalMatches, 0);
695 yield* putItems([{ ...question, questionAnswer: undefined }], "unfolded");
696 assert.equal((yield* projection.searchThread({ threadId, query: "need" })).totalMatches, 1);
697 }).pipe(Effect.provide(layerTest)),
698 );
699
700 it.effect("finds an async answer that started a later run", () =>
701 Effect.gen(function* () {
702 yield* createProject(projectId);
703 const projection = yield* ProjectionStore.ProjectionStoreV2;
704 const queuedRun = RunId.make("run:queued");
705 const answerRun = RunId.make("run:answer");
706 yield* commit([
707 thread(threadId, projectId),
708 run(),
709 run(threadId, queuedRun, 2),
710 run(threadId, answerRun, 3),
711 ]);
712 const requestId = RuntimeRequestId.make("late");
713 const question: OrchestrationV2TurnItem = {
714 ...item("question", 1, ""),
715 type: "user_input_request",
716 requestId,
717 questions: [],
718 questionAnswer: { requestId, answers: {}, attachmentsByQuestionId: {} },
719 };
720 const items = [
721 question,
722 { ...item("queued", 2, "queued follow-up", "user_message"), runId: queuedRun },
723 { ...item(`async-answer:${requestId}`, 3, "needle", "user_message"), runId: answerRun },
724 ];
725 yield* commit(
726 items.map((payload) => ({
727 id: EventId.make(`${payload.id}:initial`),
728 threadId,
729 occurredAt: at(1),
730 type: "turn-item.updated" as const,
731 payload,
732 })),
733 );
734 const result = yield* projection.searchThread({ threadId, query: "needle" });
735 assert.equal(result.totalMatches, 1);
736 assert.deepEqual(result.match, {
737 entryId: `async-answer:${requestId}`,
738 runId: answerRun,
739 occurrence: 0,
740 });
741 }).pipe(Effect.provide(layerTest)),
742 );
743
744 it.effect(
745 "searches inherited fork history through the fork boundary, using source item identities",
746 () =>
747 Effect.gen(function* () {
748 const projection = yield* setup;
749 const parent = ThreadId.make("thread:parent");
750 const laterRun = RunId.make("run:later");
751 const parentRun = RunId.make("run:parent");
752 yield* commit([
753 thread(parent, projectId),
754 run(parent, parentRun),
755 run(parent, laterRun, 2),
756 ]);
757 yield* putItems([
758 { ...item("inherited", 1, "needle", "assistant_message", parent), runId: parentRun },
759 { ...item("after-fork", 2, "needle", "assistant_message", parent), runId: laterRun },
760 ]);
761 const child = thread(threadId, projectId);
762 if (child.type !== "thread.created") return;
763 yield* commit([
764 {
765 ...child,
766 id: EventId.make("child:fork"),
767 type: "thread.metadata-updated",
768 payload: {
769 ...child.payload,
770 forkedFrom: { type: "run", threadId: parent, runId: parentRun },
771 },
772 },
773 ]);
774 yield* putItems([item("local", 1, "needle")]);
775 const inherited = yield* projection.searchThread({ threadId, query: "needle" });
776 assert.equal(inherited.totalMatches, 2);
777 assert.equal(inherited.match?.entryId, "inherited");
778 assert.equal(
779 (yield* projection.searchThread({ threadId, query: "needle", index: 1 })).match?.entryId,
780 "local",
781 );
782 assert.equal((yield* projection.searchThread({ threadId, query: "need" })).totalMatches, 2);
783 const snapshot = yield* projection.getThreadSnapshot(threadId);
784 const history = yield* projection.getThreadHistoryPage(
785 threadId,
786 encodeThreadHistoryCursor({
787 snapshotSequence: snapshot.snapshotSequence,
788 sourceThreadId: threadId,
789 sourceItemId: "local",
790 position: 2,
791 }),
792 );
793 assert.deepEqual(
794 history.items,
795 snapshot.projection.visibleTurnItems
796 .slice(0, -1)
797 .map((row) => ({ ...row, item: projectTurnItemForWire(row.item) })),
798 );
799 assert.equal(history.items[0]?.sourceThreadId, parent);
800 assert.equal(history.items.at(-1)?.item.type, "fork");
801 yield* putItems(
802 [{ ...item("inherited", 1, "gone", "assistant_message", parent), runId: parentRun }],
803 "parent-update",
804 );
805 assert.equal(
806 (yield* projection.searchThread({ threadId, query: "needle" })).totalMatches,
807 1,
808 );
809 }).pipe(Effect.provide(layerTest)),
810 );
811
812 it.effect("rejects deleted threads and keeps results scoped to their thread", () =>
813 Effect.gen(function* () {
814 const projection = yield* setup;
815 const other = ThreadId.make("thread:other");
816 yield* commit([thread(other, projectId), run(other, RunId.make("run:other"))]);
817 yield* putItems([
818 item("shared-id", 1, "needle"),
819 item("other-id", 1, "other text", "assistant_message", other),
820 ]);
821 assert.equal((yield* projection.searchThread({ threadId, query: "needle" })).totalMatches, 1);
822 assert.equal(
823 (yield* projection.searchThread({ threadId: other, query: "needle" })).totalMatches,
824 0,
825 );
826 const deleted = thread(threadId, projectId, { deletedAt: at(3) });
827 if (deleted.type !== "thread.created") return;
828 yield* commit([{ ...deleted, id: EventId.make("deleted"), type: "thread.deleted" }]);
829 const error = yield* Effect.flip(projection.searchThread({ threadId, query: "needle" }));
830 assert.equal(error._tag, "ProjectionStoreThreadNotFoundError");
831 const historyError = yield* Effect.flip(
832 projection.getThreadHistoryPage(
833 threadId,
834 encodeThreadHistoryCursor({
835 snapshotSequence: 0,
836 sourceThreadId: threadId,
837 sourceItemId: "shared-id",
838 position: 0,
839 }),
840 ),
841 );
842 assert.equal(historyError._tag, "ProjectionStoreThreadNotFoundError");
843 }).pipe(Effect.provide(layerTest)),
844 );
845
846 it.effect("invalidates a cached fork search when its empty source gains its first message", () =>
847 Effect.gen(function* () {
848 const projection = yield* setup;
849 const parent = ThreadId.make("thread:empty-parent");
850 const parentRun = RunId.make("run:empty-parent");
851 yield* commit([thread(parent, projectId), run(parent, parentRun)]);
852 const child = thread(threadId, projectId);
853 if (child.type !== "thread.created") return;
854 yield* commit([
855 {
856 ...child,
857 id: EventId.make("child:empty-fork"),
858 type: "thread.metadata-updated",
859 payload: {
860 ...child.payload,
861 forkedFrom: { type: "run", threadId: parent, runId: parentRun },
862 },
863 },
864 ]);
865 yield* putItems([item("local", 1, "needle")]);
866 assert.equal((yield* projection.searchThread({ threadId, query: "needle" })).totalMatches, 1);
867 yield* putItems([
868 { ...item("inherited", 1, "needle", "assistant_message", parent), runId: parentRun },
869 ]);
870 const updated = yield* projection.searchThread({ threadId, query: "needle" });
871 assert.equal(updated.totalMatches, 2);
872 assert.equal(updated.match?.entryId, "inherited");
873 }).pipe(Effect.provide(layerTest)),
874 );
875 });
876
877 describe("V2 history materialization", () => {
878 it.effect(
879 "combines history pages through a match's message identity without skipping intervening turns",
880 () =>
881 Effect.gen(function* () {
882 const projection = yield* setup;
883 const items = Array.from({ length: 45 }, (_, turn) => [
884 item(`user:${turn}`, turn * 3, `turn ${turn}`, "user_message"),
885 {
886 ...item(`answer:${turn}`, turn * 3 + 1, `answer ${turn}`),
887 messageId: MessageId.make(`message:${turn}`),
888 },
889 {
890 ...item(`tool:${turn}`, turn * 3 + 2, ""),
891 type: "command_execution" as const,
892 input: "pwd",
893 output: "huge output".repeat(1000),
894 exitCode: 0,
895 },
896 ]).flat();
897 yield* putItems(items);
898 const snapshot = yield* projection.getThreadSnapshot(threadId);
899 const cursor = encodeThreadHistoryCursor({
900 snapshotSequence: snapshot.snapshotSequence,
901 sourceThreadId: threadId,
902 sourceItemId: "absent",
903 position: items.length,
904 });
905 const ordinary = yield* projection.getThreadHistoryPage(threadId, cursor);
906 assert.equal(ordinary.items[0]?.sourceItemId, "user:25");
907 const targeted = yield* projection.getThreadHistoryPage(threadId, cursor, "message:10");
908 assert.equal(targeted.items[0]?.sourceItemId, "user:10");
909 assert.deepEqual(
910 targeted.items.map((row) => row.sourceItemId),
911 items.slice(30).map((entry) => entry.id),
912 );
913 const conversation = yield* projection.getThreadHistoryPage(
914 threadId,
915 cursor,
916 "message:10",
917 true,
918 );
919 assert.equal(conversation.nextCursor, targeted.nextCursor);
920 assert.equal(conversation.hasMoreHistory, targeted.hasMoreHistory);
921 assert.deepEqual(
922 conversation.items.map((row) => row.sourceItemId),
923 targeted.items
924 .filter((row) => row.item.type !== "command_execution")
925 .map((row) => row.sourceItemId),
926 );
927 assert.equal(conversation.items.length, 70);
928 assert.equal(targeted.items.length, 105);
929 assert.isTrue(targeted.hasMoreHistory);
930 const earlier = yield* projection.getThreadHistoryPage(threadId, targeted.nextCursor!);
931 assert.deepEqual(
932 earlier.items.map((row) => row.sourceItemId),
933 items.slice(0, 30).map((entry) => entry.id),
934 );
935 assert.isFalse(earlier.hasMoreHistory);
936 const capped = yield* projection.getThreadHistoryPage(threadId, cursor, "message:0");
937 assert.equal(capped.items[0]?.sourceItemId, "user:5");
938 const absent = yield* projection.getThreadHistoryPage(
939 threadId,
940 cursor,
941 "another-thread-message",
942 );
943 assert.deepEqual(absent, ordinary);
944 }).pipe(Effect.provide(layerTest)),
945 );
946
947 it.effect("pages long turns without duplicating or skipping messages", () =>
948 Effect.gen(function* () {
949 const projection = yield* setup;
950 const items = Array.from({ length: 45 }, (_, turn) => [
951 item(`user:${turn}`, turn * 3, `turn ${turn}`, "user_message"),
952 {
953 ...item(`steer:${turn}`, turn * 3 + 1, "steer", "user_message"),
954 inputIntent: "steer" as const,
955 },
956 item(
957 `answer:${turn}`,
958 turn * 3 + 2,
959 turn === 40 ? "large ".repeat(200_000) : `answer ${turn}`,
960 ),
961 ]).flat();
962 yield* putItems(items);
963 const snapshot = yield* projection.getThreadSnapshot(threadId);
964 const rows = snapshot.projection.visibleTurnItems.map((row) => ({
965 ...row,
966 item: projectTurnItemForWire(row.item),
967 }));
968 let cursor: string | null = encodeThreadHistoryCursor({
969 snapshotSequence: snapshot.snapshotSequence,
970 sourceThreadId: threadId,
971 sourceItemId: "absent",
972 position: rows.length,
973 });
974 const collected: string[] = [];
975 while (cursor !== null) {
976 const page: OrchestrationV2ThreadHistoryPage = yield* projection.getThreadHistoryPage(
977 threadId,
978 cursor,
979 );
980 assert.deepEqual(page, {
981 snapshotSequence: snapshot.snapshotSequence,
982 ...selectHistoryPageFromCursor({
983 items: rows,
984 cursor,
985 snapshotSequence: snapshot.snapshotSequence,
986 }),
987 });
988 // A turn larger than the byte budget is split, but only a lone oversized item may exceed it.
989 assert.isTrue(
990 page.items.length === 1 ||
991 Buffer.byteLength(JSON.stringify(page.items), "utf8") <=
992 THREAD_HISTORY_PAGE_POLICY.maxEncodedBytes,
993 );
994 collected.unshift(...page.items.map((row) => String(row.sourceItemId)));
995 cursor = page.nextCursor;
996 }
997 assert.deepEqual(
998 collected,
999 items.map((entry) => String(entry.id)),
1000 );
1001 }).pipe(Effect.provide(layerTest)),
1002 );
1003
1004 it.effect("enforces fallback item and byte limits and always admits an oversized row", () =>
1005 Effect.gen(function* () {
1006 const projection = yield* setup;
1007 yield* putItems(
1008 Array.from({ length: 80 }, (_, ordinal) =>
1009 item(`row:${ordinal}`, ordinal, ordinal < 78 ? "small" : "large ".repeat(200_000)),
1010 ),
1011 );
1012 const snapshot = yield* projection.getThreadSnapshot(threadId);
1013 const rows = snapshot.projection.visibleTurnItems.map((row) => ({
1014 ...row,
1015 item: projectTurnItemForWire(row.item),
1016 }));
1017 let cursor: string | null = encodeThreadHistoryCursor({
1018 snapshotSequence: snapshot.snapshotSequence,
1019 sourceThreadId: threadId,
1020 sourceItemId: "absent",
1021 position: rows.length,
1022 });
1023 const sizes: number[] = [];
1024 while (cursor !== null) {
1025 const page: OrchestrationV2ThreadHistoryPage = yield* projection.getThreadHistoryPage(
1026 threadId,
1027 cursor,
1028 );
1029 assert.deepEqual(page, {
1030 snapshotSequence: snapshot.snapshotSequence,
1031 ...selectHistoryPageFromCursor({
1032 items: rows,
1033 cursor,
1034 snapshotSequence: snapshot.snapshotSequence,
1035 }),
1036 });
1037 sizes.push(page.items.length);
1038 cursor = page.nextCursor;
1039 }
1040 assert.deepEqual(sizes, [1, 1, 75, 3]);
1041 }).pipe(Effect.provide(layerTest)),
1042 );
1043 });