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

main
ts 12,065 lines 452 KB
Raw
1 import * as NodeSqlite from "node:sqlite";
2
3 import { assert, it, vi } from "@effect/vitest";
4 import {
5 EnvironmentId,
6 EventId,
7 CommandId,
8 CheckpointId,
9 ContextTransferId,
10 CheckpointRef,
11 CheckpointScopeId,
12 ContextHandoffId,
13 MessageId,
14 type ModelSelection,
15 type OrchestrationV2ProviderThread,
16 type OrchestrationV2TurnItem,
17 type OrchestrationV2ThreadHistoryPage,
18 type OrchestrationV2ThreadShellSnapshot,
19 NodeId,
20 PlanId,
21 ProjectId,
22 ProviderDriverKind,
23 ProviderInstanceId,
24 ProviderSessionId,
25 ProviderThreadId,
26 ProviderTurnId,
27 RuntimeRequestId,
28 RunAttemptId,
29 RunId,
30 ThreadId,
31 TurnItemId,
32 type ThreadPullRequestLink,
33 isLiveSubagentShell,
34 isListedThreadLineage,
35 } from "@t3tools/contracts";
36 import * as DateTime from "effect/DateTime";
37 import * as Effect from "effect/Effect";
38 import * as Deferred from "effect/Deferred";
39 import * as Fiber from "effect/Fiber";
40 import * as Layer from "effect/Layer";
41 import * as Option from "effect/Option";
42 import * as Schema from "effect/Schema";
43 import * as Tracer from "effect/Tracer";
44 import * as SqlClient from "effect/sql/SqlClient";
45 import * as Statement from "effect/sql/Statement";
46 import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";
47 import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp";
48
49 import * as SqlitePersistence from "../persistence/Sqlite.ts";
50 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
51 import * as ProjectionStore from "./ProjectionStore.ts";
52 import {
53 buildBoundedThreadProjection,
54 decodeThreadHistoryCursor,
55 encodeThreadHistoryCursor,
56 selectHistoryPageFromCursor,
57 THREAD_HISTORY_PAGE_POLICY,
58 THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
59 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
60 } from "./threadHistoryPaging.ts";
61 import { buildBoundedThreadStreamSnapshot } from "./ThreadStream.ts";
62 import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
63
64 import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";
65
66 declare module "node:sqlite" {
67 interface StatementSync {
68 // Node 26 has this; @types/node 24 does not declare it yet.
69 stat(counter: "vmStep"): number;
70 }
71 }
72
73 const layerTest = Layer.mergeAll(
74 ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
75 SqlitePersistence.layerMemory,
76 );
77 const modelSelection = {
78 instanceId: ProviderInstanceId.make("codex"),
79 model: "gpt-5.4",
80 } satisfies ModelSelection;
81 const driver = ProviderDriverKind.make("codex");
82 const providerInstanceId = modelSelection.instanceId;
83 const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
84
85 const traceSqlStatements = () => {
86 const statements: Array<string> = [];
87 const queryTransactions: Array<string | null> = [];
88 const tracer = Tracer.make({
89 span(options) {
90 const span = new Tracer.NativeSpan(options);
91 const end = span.end.bind(span);
92 span.end = (endTime, exit) => {
93 end(endTime, exit);
94 const query = span.attributes.get("db.query.text");
95 if (typeof query === "string") {
96 statements.push(query);
97 let parent = Option.getOrUndefined(span.parent);
98 while (parent?._tag === "Span" && parent.name !== "sql.transaction") {
99 parent = Option.getOrUndefined(parent.parent);
100 }
101 queryTransactions.push(parent?._tag === "Span" ? parent.spanId : null);
102 }
103 };
104 return span;
105 },
106 });
107 return { statements, tracer, queryTransactions };
108 };
109
110 /** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
111 const assertShellIndexCoversShells = Effect.fnUntraced(function* () {
112 const store = yield* ProjectionStore.ProjectionStoreV2;
113 const snapshot = yield* store.getShellSnapshot();
114 const shells = snapshot.threads;
115 const index = yield* store.listActiveShellIndex();
116 const { tracer, statements, queryTransactions } = traceSqlStatements();
117 const activity = yield* store.getAgentAwarenessSnapshot().pipe(Effect.withTracer(tracer));
118 assert.equal(activity.snapshotSequence, snapshot.snapshotSequence);
119 const awareness = (thread: Parameters<typeof projectThreadAwarenessV2>[0]["thread"]) =>
120 projectThreadAwarenessV2({
121 environmentId: EnvironmentId.make("test"),
122 project: { title: "Test" },
123 thread,
124 });
125 assert.deepEqual(
126 activity.threads.map(awareness),
127 shells.filter((thread) => isListedThreadLineage(thread.lineage)).map(awareness),
128 );
129 assert.deepEqual(
130 activity.threads.map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
131 shells
132 .filter((thread) => isListedThreadLineage(thread.lineage))
133 .map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
134 );
135 assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));
136 assert.isTrue(queryTransactions.every((transaction) => transaction !== null));
137 assert.equal(new Set(queryTransactions).size, 1);
138 for (const shell of [...shells, ...snapshot.archivedThreads]) {
139 const targeted = yield* store
140 .getAgentAwarenessSnapshot(shell.id)
141 .pipe(Effect.withTracer(tracer));
142 assert.deepEqual(targeted.threads.map(awareness), [awareness(shell)]);
143 assert.deepEqual(targeted.threads[0]?.archivedAt, shell.archivedAt);
144 }
145 assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));
146
147 const iso = (value: DateTime.Utc | null | undefined) =>
148 value == null ? null : DateTime.formatIso(value);
149 const trimmed = (
150 row: Pick<
151 ProjectionStore.ActiveShellIndexEntry,
152 | "id"
153 | "projectId"
154 | "lineage"
155 | "settledOverride"
156 | "settledAt"
157 | "pinnedAt"
158 | "pinOrderKey"
159 | "snoozedUntil"
160 >,
161 ) => ({
162 id: row.id,
163 projectId: row.projectId,
164 lineage: row.lineage,
165 settledOverride: row.settledOverride,
166 settledAt: iso(row.settledAt),
167 pinnedAt: iso(row.pinnedAt),
168 pinOrderKey: row.pinOrderKey ?? null,
169 snoozedUntil: iso(row.snoozedUntil),
170 });
171 assert.deepEqual(index.map(trimmed), shells.map(trimmed));
172 shells.forEach((shell, position) => {
173 if (isLiveSubagentShell(shell)) assert.isTrue(index[position]?.mayBeLive, shell.id);
174 });
175 });
176
177 const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate")(function* (
178 suffix: string,
179 ) {
180 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
181 const now = yield* DateTime.now;
182 const threadId = ThreadId.make(`thread:${suffix}:rolled-back`);
183 const runId = RunId.make(`run:${suffix}:rolled-back`);
184 const rootNodeId = NodeId.make(`node:${suffix}:rolled-back`);
185 const run = {
186 id: runId,
187 threadId,
188 ordinal: 1,
189 providerInstanceId,
190 modelSelection,
191 providerThreadId: null,
192 userMessageId: MessageId.make(`message:${suffix}:rolled-back`),
193 rootNodeId,
194 activeAttemptId: null,
195 status: "running" as const,
196 requestedAt: now,
197 startedAt: now,
198 completedAt: null,
199 checkpointId: null,
200 contextHandoffId: null,
201 };
202
203 yield* projectionStore.apply({
204 id: EventId.make(`event:${suffix}:thread-created`),
205 type: "thread.created",
206 threadId,
207 occurredAt: now,
208 payload: {
209 createdBy: "user",
210 creationSource: "web",
211 id: threadId,
212 projectId: ProjectId.make(`project:${suffix}`),
213 title: "Rolled-back recovery candidate",
214 providerInstanceId,
215 modelSelection,
216 runtimeMode: "full-access",
217 interactionMode: "default",
218 branch: null,
219 worktreePath: null,
220 activeProviderThreadId: null,
221 lineage: {
222 parentThreadId: null,
223 relationshipToParent: null,
224 rootThreadId: threadId,
225 },
226 forkedFrom: null,
227 createdAt: now,
228 updatedAt: now,
229 archivedAt: null,
230 settledOverride: null,
231 settledAt: null,
232 lastVisitedAt: null,
233 deletedAt: null,
234 },
235 });
236 yield* projectionStore.apply({
237 id: EventId.make(`event:${suffix}:run-created`),
238 type: "run.created",
239 threadId,
240 runId,
241 nodeId: rootNodeId,
242 driver,
243 providerInstanceId,
244 occurredAt: now,
245 payload: run,
246 });
247 yield* projectionStore.apply({
248 id: EventId.make(`event:${suffix}:item-running`),
249 type: "turn-item.updated",
250 threadId,
251 runId,
252 nodeId: rootNodeId,
253 driver,
254 occurredAt: now,
255 payload: {
256 id: TurnItemId.make(`item:${suffix}:rolled-back`),
257 threadId,
258 runId,
259 nodeId: rootNodeId,
260 providerThreadId: null,
261 providerTurnId: null,
262 nativeItemRef: null,
263 parentItemId: null,
264 ordinal: 1,
265 status: "running",
266 title: "abandoned command",
267 startedAt: now,
268 completedAt: null,
269 updatedAt: now,
270 type: "command_execution",
271 input: "sleep 60",
272 },
273 });
274 yield* projectionStore.apply({
275 id: EventId.make(`event:${suffix}:run-rolled-back`),
276 type: "run.updated",
277 threadId,
278 runId,
279 nodeId: rootNodeId,
280 driver,
281 occurredAt: now,
282 payload: { ...run, status: "rolled_back", completedAt: now },
283 });
284
285 return threadId;
286 });
287
288 it.effect("routes full projection reads to readers and retains uncommitted writer state", () =>
289 Effect.gen(function* () {
290 const store = yield* ProjectionStore.ProjectionStoreV2;
291 const sql = yield* SqlClient.SqlClient;
292 const parentId = yield* addRolledBackRecoveryCandidate("reader-routing");
293 const parent = yield* store.getThreadProjection(parentId);
294 const threadId = ThreadId.make("thread:reader-routing:fork");
295 yield* store.apply({
296 id: EventId.make("event:reader-routing:fork"),
297 type: "thread.created",
298 threadId,
299 occurredAt: parent.thread.createdAt,
300 payload: {
301 ...parent.thread,
302 id: threadId,
303 forkedFrom: { type: "run", threadId: parentId, runId: parent.runs[0]!.id },
304 },
305 });
306 const messageId = MessageId.make("message:reader-routing");
307 yield* store.apply({
308 id: EventId.make("event:reader-routing:message"),
309 type: "message.updated",
310 threadId,
311 occurredAt: parent.thread.createdAt,
312 payload: {
313 id: messageId,
314 threadId,
315 runId: null,
316 nodeId: null,
317 role: "user",
318 text: "Committed message",
319 attachments: [],
320 streaming: false,
321 createdBy: "user",
322 creationSource: "web",
323 createdAt: parent.thread.createdAt,
324 updatedAt: parent.thread.createdAt,
325 },
326 });
327 const { queries, trace } = traceSqlRouting();
328 yield* store.getAgentAwarenessSnapshot().pipe(trace);
329 const projection = yield* store.getThreadProjection(threadId).pipe(trace);
330 assert.equal(projection.thread.id, threadId);
331 assert.deepEqual(
332 projection.visibleTurnItems
333 .filter((row) => row.visibility === "inherited")
334 .map((row) => row.sourceItemId),
335 parent.turnItems.map((item) => item.id),
336 );
337 assert.equal(
338 (yield* store.getMessagesById(threadId, [messageId]).pipe(trace)).get(messageId)?.text,
339 "Committed message",
340 );
341 assert.isAbove(queries.length, 1);
342 assert.deepEqual([...new Set(queries.map((row) => row.connection))], ["reader"]);
343 const commitRequested = yield* Deferred.make<void>();
344 const writer = yield* Deferred.await(commitRequested).pipe(
345 Effect.andThen(
346 sql.withTransaction(
347 Effect.gen(function* () {
348 yield* sql`
349 UPDATE orchestration_v2_projection_threads
350 SET payload_json = json_set(payload_json, '$.title', 'Committed title')
351 WHERE thread_id = ${threadId}
352 `;
353 yield* sql`
354 UPDATE orchestration_v2_projection_turn_items
355 SET payload_json = json_set(payload_json, '$.title', 'Committed command')
356 WHERE thread_id = ${parentId}
357 `;
358 }),
359 ),
360 ),
361 Effect.forkChild,
362 );
363 let commitInjected = false;
364 const duringCommit = yield* store.getThreadProjection(threadId).pipe(
365 Effect.provideService(Statement.CurrentTransformer, (statement) => {
366 const [query] = statement.compile();
367 if (!commitInjected && query.includes("FROM orchestration_v2_projection_turn_items")) {
368 commitInjected = true;
369 return Deferred.succeed(commitRequested, undefined).pipe(
370 Effect.andThen(Fiber.join(writer).pipe(Effect.orDie)),
371 Effect.as(statement),
372 );
373 }
374 return Effect.succeed(statement);
375 }),
376 );
377 assert.isTrue(commitInjected);
378 assert.deepEqual(duringCommit, projection);
379 const afterCommit = yield* store.getThreadProjection(threadId);
380 assert.equal(afterCommit.thread.title, "Committed title");
381 assert.equal(
382 afterCommit.visibleTurnItems.find((row) => row.visibility === "inherited")?.item.title,
383 "Committed command",
384 );
385 yield* sql.withTransaction(
386 Effect.gen(function* () {
387 yield* sql`
388 UPDATE orchestration_v2_projection_threads
389 SET payload_json = json_set(payload_json, '$.title', 'Uncommitted title')
390 WHERE thread_id = ${threadId}
391 `;
392 yield* sql`
393 UPDATE orchestration_v2_projection_messages
394 SET payload_json = json_set(payload_json, '$.text', 'Uncommitted message')
395 WHERE message_id = ${messageId}
396 `;
397 assert.equal(
398 (yield* store.getMessagesById(threadId, [messageId])).get(messageId)?.text,
399 "Uncommitted message",
400 );
401 assert.equal(
402 (yield* store.getAgentAwarenessSnapshot()).threads.find(
403 (thread) => thread.id === threadId,
404 )?.title,
405 "Uncommitted title",
406 );
407 assert.equal(
408 (yield* store.getThreadProjection(threadId)).thread.title,
409 "Uncommitted title",
410 );
411 }),
412 );
413 }).pipe(Effect.provide(ProjectionStore.layer.pipe(Layer.provideMerge(layerReaderDatabase)))),
414 );
415
416 it.effect("record reads restrict provider histories to delivery identities", () =>
417 Effect.gen(function* () {
418 const store = yield* ProjectionStore.ProjectionStoreV2;
419 const threadId = yield* addRolledBackRecoveryCandidate("delivery-identities");
420 const sql = yield* SqlClient.SqlClient;
421 const now = yield* DateTime.now;
422 for (const suffix of ["selected", "history"]) {
423 const id = ProviderThreadId.make(`provider-thread:${suffix}`);
424 yield* store.apply({
425 id: EventId.make(`event:delivery-identities:${suffix}`),
426 type: "provider-thread.updated",
427 threadId,
428 occurredAt: now,
429 payload: {
430 id,
431 appThreadId: threadId,
432 driver,
433 providerInstanceId,
434 providerSessionId: null,
435 ownerNodeId: null,
436 nativeThreadRef: null,
437 nativeConversationHeadRef: null,
438 lastRunOrdinal: null,
439 handoffIds: [],
440 forkedFrom: null,
441 status: "active",
442 firstRunOrdinal: 1,
443 createdAt: now,
444 updatedAt: now,
445 },
446 });
447 }
448 for (const [suffix, attempt] of [
449 ["selected", "current"],
450 ["history", "old"],
451 ["runless", null],
452 ] as const) {
453 yield* store.apply({
454 id: EventId.make(`event:delivery-turn:${suffix}`),
455 type: "provider-turn.updated",
456 threadId,
457 occurredAt: now,
458 payload: {
459 id: ProviderTurnId.make(`turn:${suffix}`),
460 providerThreadId: ProviderThreadId.make("provider-thread:selected"),
461 nodeId: NodeId.make("node:delivery"),
462 runAttemptId: attempt === null ? null : RunAttemptId.make(attempt),
463 nativeTurnRef: null,
464 ordinal: suffix === "selected" ? 1 : suffix === "history" ? 2 : 3,
465 status: "completed",
466 startedAt: now,
467 completedAt: now,
468 },
469 });
470 }
471 assert.deepEqual(
472 (yield* store.getThreadRecords(threadId, ["providerTurns"], {
473 providerTurnAttemptIds: [RunAttemptId.make("current")],
474 })).providerTurns.map((turn) => turn.id),
475 ["turn:selected"],
476 );
477 assert.deepEqual(
478 (yield* store.getThreadRecords(threadId, ["providerTurns"], {
479 providerTurnAttemptIds: [],
480 })).providerTurns,
481 [],
482 );
483 const selected = yield* store.getThreadRecords(threadId, ["providerThreads"], {
484 providerThreadIds: [ProviderThreadId.make("provider-thread:selected")],
485 });
486 assert.deepEqual(
487 selected.providerThreads.map((row) => row.id),
488 ["provider-thread:selected"],
489 );
490 assert.deepEqual(
491 (yield* store.getThreadRecords(threadId, ["providerThreads"], {
492 providerThreadIds: [],
493 })).providerThreads,
494 [],
495 );
496 const ownerNodeId = NodeId.make("node:delivery-identities:owner");
497 yield* store.apply({
498 id: EventId.make("event:delivery-identities:owner-node"),
499 type: "node.updated",
500 threadId,
501 runId: RunId.make("run:delivery-identities:rolled-back"),
502 nodeId: ownerNodeId,
503 driver,
504 occurredAt: now,
505 payload: {
506 id: ownerNodeId,
507 threadId,
508 runId: RunId.make("run:delivery-identities:rolled-back"),
509 rootNodeId: NodeId.make("node:delivery-identities:rolled-back"),
510 parentNodeId: NodeId.make("node:delivery-identities:rolled-back"),
511 kind: "subagent",
512 status: "completed",
513 countsForRun: false,
514 providerThreadId: null,
515 providerTurnId: null,
516 nativeItemRef: null,
517 runtimeRequestId: null,
518 checkpointScopeId: null,
519 startedAt: now,
520 completedAt: now,
521 },
522 });
523 yield* store.apply({
524 id: EventId.make("event:delivery-identities:owned"),
525 type: "provider-thread.updated",
526 threadId,
527 occurredAt: now,
528 payload: {
529 id: ProviderThreadId.make("provider-thread:owned"),
530 appThreadId: ThreadId.make("thread:delivery-identities:child"),
531 driver,
532 providerInstanceId,
533 providerSessionId: null,
534 ownerNodeId,
535 nativeThreadRef: null,
536 nativeConversationHeadRef: null,
537 lastRunOrdinal: null,
538 handoffIds: [],
539 forkedFrom: null,
540 status: "idle",
541 firstRunOrdinal: 2,
542 createdAt: now,
543 updatedAt: now,
544 },
545 });
546 assert.deepEqual(
547 (yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads.map(
548 (row) => row.id,
549 ),
550 ["provider-thread:history", "provider-thread:selected", "provider-thread:owned"],
551 );
552 // Unselected history must not be decoded, even if its payload is unreadable.
553 yield* sql`UPDATE orchestration_v2_projection_provider_threads SET payload_json = '{}'
554 WHERE provider_thread_id = 'provider-thread:history'`;
555 assert.lengthOf(
556 (yield* store.getThreadRecords(threadId, ["providerThreads"], {
557 providerThreadIds: [ProviderThreadId.make("provider-thread:selected")],
558 })).providerThreads,
559 1,
560 );
561 }).pipe(Effect.provide(layerTest)),
562 );
563
564 it.effect("idle compaction filters keep the latest run, live work and the newest usage", () =>
565 Effect.gen(function* () {
566 const store = yield* ProjectionStore.ProjectionStoreV2;
567 // Its rolled-back run 1 still has a running item, which background work must ignore.
568 const threadId = yield* addRolledBackRecoveryCandidate("idle-compaction");
569 const now = yield* DateTime.now;
570 const minutes = (count: number) => DateTime.add(now, { minutes: count });
571 const selected = ProviderThreadId.make("provider-thread:idle-selected");
572 const nodeId = NodeId.make("node:idle-compaction");
573 for (const [ordinal, status] of [
574 [2, "completed"],
575 [3, "completed"],
576 [4, "failed"],
577 // A removed or steered queued message.
578 [5, "cancelled"],
579 ] as const) {
580 const runId = RunId.make(`run:idle-compaction:${ordinal}`);
581 yield* store.apply({
582 id: EventId.make(`event:idle-compaction:run-${ordinal}`),
583 type: "run.created",
584 threadId,
585 runId,
586 nodeId,
587 driver,
588 providerInstanceId,
589 occurredAt: now,
590 payload: {
591 id: runId,
592 threadId,
593 ordinal,
594 providerInstanceId,
595 modelSelection,
596 providerThreadId: selected,
597 userMessageId: MessageId.make(`message:idle-compaction:${ordinal}`),
598 rootNodeId: nodeId,
599 activeAttemptId: null,
600 status,
601 requestedAt: now,
602 startedAt: status === "cancelled" ? null : now,
603 completedAt: now,
604 checkpointId: null,
605 contextHandoffId: null,
606 },
607 });
608 }
609 for (const [id, providerThreadId, ordinal, usageAt] of [
610 ["turn:idle-old", selected, 1, minutes(1)],
611 ["turn:idle-newest", selected, 2, minutes(3)],
612 ["turn:idle-later-ordinal", selected, 3, minutes(2)],
613 ["turn:idle-no-usage", selected, 4, null],
614 [
615 "turn:idle-other-thread",
616 ProviderThreadId.make("provider-thread:idle-other"),
617 1,
618 minutes(9),
619 ],
620 ] as const) {
621 yield* store.apply({
622 id: EventId.make(`event:${id}`),
623 type: "provider-turn.updated",
624 threadId,
625 nodeId,
626 driver,
627 occurredAt: now,
628 payload: {
629 id: ProviderTurnId.make(id),
630 providerThreadId,
631 nodeId,
632 runAttemptId: null,
633 nativeTurnRef: null,
634 ordinal,
635 status: "completed",
636 startedAt: now,
637 completedAt: now,
638 ...(usageAt === null
639 ? {}
640 : { tokenUsage: { usedTokens: 150_000, updatedAt: DateTime.formatIso(usageAt) } }),
641 },
642 });
643 }
644 for (const status of ["pending", "resolved"] as const) {
645 yield* store.apply({
646 id: EventId.make(`event:idle-compaction:request-${status}`),
647 type: "runtime-request.updated",
648 threadId,
649 nodeId,
650 occurredAt: now,
651 payload: {
652 id: RuntimeRequestId.make(`request:idle-compaction:${status}`),
653 nodeId,
654 providerTurnId: null,
655 nativeRequestRef: null,
656 kind: "user_input",
657 status,
658 responseCapability: { type: "message" },
659 createdAt: now,
660 resolvedAt: status === "pending" ? null : now,
661 },
662 });
663 }
664 for (const status of ["pending", "consumed"] as const) {
665 yield* store.apply({
666 id: EventId.make(`event:idle-compaction:transfer-${status}`),
667 type: "context-transfer.created",
668 threadId,
669 occurredAt: now,
670 payload: {
671 id: ContextTransferId.make(`transfer:idle-compaction:${status}`),
672 type: "merge_back",
673 sourceThreadId: ThreadId.make("thread:idle-compaction:side"),
674 targetThreadId: threadId,
675 sourcePoint: { threadId: ThreadId.make("thread:idle-compaction:side") },
676 basePoint: null,
677 sourceProviderInstanceId: providerInstanceId,
678 targetProviderInstanceId: providerInstanceId,
679 targetRunId: null,
680 status,
681 resolution: null,
682 createdBy: "user",
683 error: null,
684 createdAt: now,
685 updatedAt: now,
686 consumedAt: status === "pending" ? null : now,
687 },
688 });
689 }
690
691 const records = yield* store.getThreadRecords(
692 threadId,
693 ["runs", "providerTurns", "runtimeRequests", "contextTransfers"],
694 {
695 runStatuses: ["preparing", "starting", "running", "rolled_back"],
696 includeLatestRun: true,
697 providerTurnThreadIds: [selected],
698 latestProviderTurnUsageOnly: true,
699 runtimeRequestStatuses: ["pending"],
700 contextTransferStatuses: ["pending"],
701 },
702 );
703 // The failed latest run stays, so a completed earlier run is not mistaken for the latest,
704 // and a cancelled queued run never counts as the latest.
705 assert.deepEqual(
706 records.runs.map((run) => run.id),
707 ["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
708 );
709 const { statements, tracer } = traceSqlStatements();
710 const statusOnly = yield* store
711 .getThreadRecords(threadId, ["runs"], {
712 runStatuses: ["preparing", "starting", "running", "rolled_back"],
713 })
714 .pipe(Effect.withTracer(tracer));
715 assert.deepEqual(
716 statusOnly.runs.map((run) => run.id),
717 ["run:idle-compaction:rolled-back"],
718 );
719 const runQuery = statements.find((statement) =>
720 statement.includes("FROM orchestration_v2_projection_runs"),
721 );
722 assert.isDefined(runQuery);
723 const sql = yield* SqlClient.SqlClient;
724 // Read only the matching runs, not every run in ordinal order.
725 const runPlan = (yield* sql.unsafe<{ readonly detail: string }>(
726 `EXPLAIN QUERY PLAN ${runQuery}`,
727 ))
728 .map((row) => row.detail)
729 .join("\n");
730 assert.notInclude(runPlan, "orchestration_v2_projection_runs_thread_ordinal_idx");
731 assert.match(runPlan, /SEARCH orchestration_v2_projection_runs USING INDEX \w*status\w*/);
732 // The newest runs by ordinal, whatever their status, or the run that ended last, plus older runs
733 // the statuses match.
734 for (const [filter, expected] of [
735 [{ latestRunLimit: 2 }, [4, 5]],
736 [{ latestRunLimit: 2, runStatuses: ["rolled_back"] as const }, [1, 4, 5]],
737 [{ includeLatestExecutedRun: true, runStatuses: ["rolled_back"] as const }, [1, 4]],
738 ] as const) {
739 const traced = traceSqlStatements();
740 const recent = yield* store
741 .getThreadRecords(threadId, ["runs"], filter)
742 .pipe(Effect.withTracer(traced.tracer));
743 assert.deepEqual(
744 recent.runs.map((run) => run.ordinal),
745 [...expected],
746 );
747 const recentQuery = traced.statements.find((statement) =>
748 statement.includes("FROM orchestration_v2_projection_runs"),
749 );
750 assert.isDefined(recentQuery);
751 // Fetches the chosen runs by key instead of walking every run of the thread.
752 assert.match(
753 (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${recentQuery}`))
754 .map((row) => row.detail)
755 .join("\n"),
756 /^SEARCH orchestration_v2_projection_runs USING INDEX \w+ \(run_id=\?\)/,
757 );
758 }
759 // An id filter reads by primary key, not every row of the thread.
760 const byId = traceSqlStatements();
761 const found = yield* store
762 .getThreadRecords(
763 threadId,
764 ["runs", "nodes", "subagents", "messages", "attempts", "providerTurns"],
765 {
766 runIds: [RunId.make("run:idle-compaction:4")],
767 nodeIds: [nodeId],
768 subagentIds: [nodeId],
769 messageIds: [MessageId.make("message:idle-compaction:4")],
770 attemptIds: [RunAttemptId.make("attempt:idle-compaction")],
771 providerTurnIds: [ProviderTurnId.make("turn:idle-old")],
772 },
773 )
774 .pipe(Effect.withTracer(byId.tracer));
775 assert.deepEqual(
776 found.runs.map((run) => run.id),
777 ["run:idle-compaction:4"],
778 );
779 assert.deepEqual(
780 found.providerTurns.map((turn) => turn.id),
781 ["turn:idle-old"],
782 );
783 // A task's turn item and a child thread's tasks are found through their own indexes.
784 yield* store
785 .getThreadRecords(threadId, ["subagents", "turnItems"], {
786 subagentChildThreadIds: [threadId],
787 turnItemNodeIds: [nodeId],
788 })
789 .pipe(Effect.withTracer(byId.tracer));
790 for (const [table, index] of [
791 ...["runs", "nodes", "subagents", "messages", "run_attempts", "provider_turns"].map(
792 (name) => [name, `sqlite_autoindex_orchestration_v2_projection_${name}_1`] as const,
793 ),
794 ["subagents", "orchestration_v2_projection_subagents_child_thread_idx"],
795 ["turn_items", "orchestration_v2_projection_turn_items_node_ordinal_idx"],
796 ] as const) {
797 const plans = yield* Effect.forEach(
798 byId.statements.filter((statement) =>
799 statement.includes(`FROM orchestration_v2_projection_${table}`),
800 ),
801 (query) => sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`),
802 );
803 assert.isTrue(
804 plans.some((plan) => plan.some((row) => row.detail.includes(`USING INDEX ${index}`))),
805 `${table} is not read through ${index}`,
806 );
807 }
808 // The filters every command reads with seek the rows they keep, not every row of the thread.
809 const command = traceSqlStatements();
810 yield* store
811 .getThreadRecords(threadId, ["nodes", "subagents", "turnItems", "contextTransfers"], {
812 turnItemTypes: ["user_message", "error"],
813 omitSettledLeafNodes: true,
814 omitSettledSubagents: true,
815 contextTransferStatuses: ["pending"],
816 })
817 .pipe(Effect.withTracer(command.tracer));
818 for (const [table, seeks] of [
819 ["turn_items", ["turn_items_thread_type_idx (thread_id=? AND type=?)"]],
820 ["nodes", ["nodes_settled_leaf_idx (thread_id=? AND <expr>=?)"]],
821 ["subagents", ["USING COVERING INDEX orchestration_v2_projection_subagents_settled_idx"]],
822 [
823 "context_transfers",
824 [
825 "source_thread_status_idx (source_thread_id=? AND status=?)",
826 "target_thread_idx (target_thread_id=? AND status=?)",
827 ],
828 ],
829 ] as const) {
830 const query = command.statements.find((statement) =>
831 statement.includes(`FROM orchestration_v2_projection_${table}`),
832 );
833 assert.isDefined(query);
834 const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
835 .map((row) => row.detail)
836 .join("\n");
837 for (const seek of seeks) assert.include(plan, seek);
838 }
839 assert.deepEqual(
840 records.providerTurns.map((turn) => turn.id),
841 ["turn:idle-newest"],
842 );
843 assert.deepEqual(
844 records.runtimeRequests.map((request) => request.id),
845 ["request:idle-compaction:pending"],
846 );
847 assert.deepEqual(
848 records.contextTransfers.map((transfer) => transfer.id),
849 ["transfer:idle-compaction:pending"],
850 );
851 const empty = yield* store.getThreadRecords(threadId, ["runs", "providerTurns"], {
852 runStatuses: [],
853 providerTurnThreadIds: [],
854 latestProviderTurnUsageOnly: true,
855 });
856 assert.deepEqual([empty.runs, empty.providerTurns], [[], []]);
857 }).pipe(Effect.provide(layerTest)),
858 );
859
860 // One thread holds every way a run can still matter after a compaction. A run is requested at
861 // its ordinal in minutes and ends 30 seconds later, unless the table says otherwise.
862 it.effect("a compacted thread reads every run, but records and prompts only for recent ones", () =>
863 Effect.gen(function* () {
864 const store = yield* ProjectionStore.ProjectionStoreV2;
865 const threadId = yield* addRolledBackRecoveryCandidate("compacted");
866 const created = yield* DateTime.now;
867 const at = (minutes: number) => DateTime.add(created, { minutes });
868 const runId = (ordinal: number) => RunId.make(`run:compacted:${ordinal}`);
869 const rootNodeId = (ordinal: number) => NodeId.make(`node:compacted:${ordinal}`);
870 const attemptId = (ordinal: number) => RunAttemptId.make(`attempt:compacted:${ordinal}`);
871 const providerThreadId = ProviderThreadId.make("provider-thread:compacted");
872 const messageId = (ordinal: number) => MessageId.make(`message:compacted:${ordinal}`);
873 const item = (
874 ordinal: number,
875 itemOrdinal: number,
876 status: "completed" | "failed" | "running",
877 ) => ({
878 threadId,
879 runId: runId(ordinal),
880 nodeId: rootNodeId(ordinal),
881 providerThreadId: null,
882 providerTurnId: null,
883 nativeItemRef: null,
884 parentItemId: null,
885 ordinal: itemOrdinal,
886 status,
887 title: null,
888 startedAt: at(ordinal),
889 completedAt: status === "running" ? null : at(ordinal),
890 updatedAt: at(ordinal),
891 });
892 const compaction = (ordinal: number, status: "completed" | "failed" | "running") =>
893 store.apply({
894 id: EventId.make(`event:compacted:compaction-${ordinal}`),
895 type: "turn-item.updated",
896 threadId,
897 runId: runId(ordinal),
898 nodeId: rootNodeId(ordinal),
899 driver,
900 occurredAt: at(ordinal),
901 payload: {
902 ...item(ordinal, ordinal * 10 + 1, status),
903 id: TurnItemId.make(`item:compacted:compaction-${ordinal}`),
904 type: "compaction",
905 driver,
906 },
907 });
908 for (const [ordinal, status, endedAt, fresh] of [
909 [2, "queued", null],
910 // A fresh-context run starts its own conversation, so its prompt is read for the schedule check.
911 [3, "completed", undefined, true],
912 // A held queue ran this old run after the compactions.
913 [4, "completed", at(11.5)],
914 [5, "completed"],
915 [6, "completed"],
916 [7, "completed"],
917 [8, "completed"],
918 [9, "completed"],
919 [10, "completed"],
920 [11, "completed"],
921 [12, "completed"],
922 [13, "rolled_back"],
923 [14, "completed"],
924 [15, "running", null],
925 ] as const) {
926 const text = `Prompt ${ordinal}`;
927 const authored = { createdBy: "user", creationSource: "web" } as const;
928 const nodeId = rootNodeId(ordinal);
929 const ended = endedAt === undefined ? at(ordinal + 0.5) : endedAt;
930 yield* store.apply({
931 id: EventId.make(`event:compacted:run-${ordinal}`),
932 type: "run.created",
933 threadId,
934 runId: runId(ordinal),
935 nodeId,
936 driver,
937 providerInstanceId,
938 occurredAt: at(ordinal),
939 payload: {
940 id: runId(ordinal),
941 threadId,
942 ordinal,
943 providerInstanceId,
944 modelSelection,
945 providerThreadId: null,
946 userMessageId: messageId(ordinal),
947 rootNodeId: nodeId,
948 activeAttemptId: attemptId(ordinal),
949 status,
950 requestedAt: at(ordinal),
951 startedAt: status === "queued" ? null : at(ordinal),
952 completedAt: ended,
953 checkpointId: null,
954 contextHandoffId: null,
955 ...(fresh === true ? { freshContext: true } : {}),
956 },
957 });
958 yield* store.apply({
959 id: EventId.make(`event:compacted:attempt-${ordinal}`),
960 type: "run-attempt.created",
961 threadId,
962 runId: runId(ordinal),
963 nodeId,
964 driver,
965 occurredAt: at(ordinal),
966 payload: {
967 id: attemptId(ordinal),
968 runId: runId(ordinal),
969 attemptOrdinal: 1,
970 rootNodeId: nodeId,
971 providerInstanceId,
972 providerThreadId,
973 providerTurnId: null,
974 reason: "initial",
975 status: ended === null ? "running" : "completed",
976 startedAt: at(ordinal),
977 completedAt: ended,
978 },
979 });
980 yield* store.apply({
981 id: EventId.make(`event:compacted:node-${ordinal}`),
982 type: "node.updated",
983 threadId,
984 runId: runId(ordinal),
985 nodeId,
986 driver,
987 occurredAt: at(ordinal),
988 payload: {
989 id: nodeId,
990 threadId,
991 runId: runId(ordinal),
992 rootNodeId: nodeId,
993 parentNodeId: null,
994 kind: "root_turn",
995 status: ended === null ? "running" : "completed",
996 countsForRun: true,
997 providerThreadId,
998 providerTurnId: null,
999 nativeItemRef: null,
1000 runtimeRequestId: null,
1001 checkpointScopeId: null,
1002 startedAt: at(ordinal),
1003 completedAt: ended,
1004 },
1005 });
1006 yield* store.apply({
1007 id: EventId.make(`event:compacted:turn-${ordinal}`),
1008 type: "provider-turn.updated",
1009 threadId,
1010 nodeId,
1011 driver,
1012 occurredAt: at(ordinal),
1013 payload: {
1014 id: ProviderTurnId.make(`turn:compacted:${ordinal}`),
1015 providerThreadId,
1016 nodeId,
1017 runAttemptId: attemptId(ordinal),
1018 nativeTurnRef: null,
1019 ordinal,
1020 status: ended === null ? "running" : "completed",
1021 startedAt: at(ordinal),
1022 completedAt: ended,
1023 },
1024 });
1025 yield* store.apply({
1026 id: EventId.make(`event:compacted:message-${ordinal}`),
1027 type: "message.updated",
1028 threadId,
1029 runId: runId(ordinal),
1030 occurredAt: at(ordinal),
1031 payload: {
1032 ...authored,
1033 id: messageId(ordinal),
1034 threadId,
1035 runId: runId(ordinal),
1036 nodeId,
1037 role: "user",
1038 text,
1039 attachments: [],
1040 streaming: false,
1041 createdAt: at(ordinal),
1042 updatedAt: at(ordinal),
1043 },
1044 });
1045 yield* store.apply({
1046 id: EventId.make(`event:compacted:prompt-${ordinal}`),
1047 type: "turn-item.updated",
1048 threadId,
1049 runId: runId(ordinal),
1050 nodeId,
1051 driver,
1052 occurredAt: at(ordinal),
1053 payload: {
1054 ...authored,
1055 ...item(ordinal, ordinal * 10, status === "queued" ? "running" : "completed"),
1056 ...(status === "queued" ? { status: "pending" as const } : {}),
1057 id: TurnItemId.make(`item:compacted:prompt-${ordinal}`),
1058 type: "user_message",
1059 messageId: messageId(ordinal),
1060 inputIntent: status === "queued" ? "queued_turn" : "turn_start",
1061 text,
1062 attachments: [],
1063 },
1064 });
1065 }
1066 // Each collection as the ordinals of the runs it was read for. Run 1 is the helper's.
1067 const ordinals = (ids: ReadonlyArray<string | null>) =>
1068 [...new Set(ids.map((id) => Number(id?.split(":").at(-1)) || 1))].toSorted((a, b) => a - b);
1069 const read = (keepRunIds: ReadonlyArray<RunId> = [], keepMessageId = messageId(0)) =>
1070 store
1071 .getThreadRecords(
1072 threadId,
1073 ["runs", "attempts", "nodes", "providerTurns", "messages", "turnItems"],
1074 {
1075 messageRoles: ["user"],
1076 omitSettledLeafNodes: true,
1077 sinceLastCompaction: { keepRunIds, keepMessageId },
1078 },
1079 )
1080 .pipe(
1081 Effect.map((records) => ({
1082 runs: records.runs.length,
1083 attempts: ordinals(records.attempts.map((attempt) => attempt.runId)),
1084 nodes: ordinals(records.nodes.map((node) => node.runId)),
1085 providerTurns: ordinals(records.providerTurns.map((turn) => turn.runAttemptId)),
1086 messages: ordinals(records.messages.map((message) => message.runId)),
1087 turnItems: ordinals(records.turnItems.map((row) => row.runId)),
1088 })),
1089 );
1090 const all = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
1091 // Every run is read. Turn items are the failed errors of every run, and the prompts of the
1092 // recent runs, of the fresh-context run 3 and of the run a model switch is measured from.
1093 // Run 1, the helper's, holds only a command item.
1094 const expected = (
1095 recent: ReadonlyArray<number>,
1096 prompts: ReadonlyArray<number> = [...recent, 3],
1097 ) => ({
1098 runs: 15,
1099 attempts: recent,
1100 nodes: recent,
1101 providerTurns: recent,
1102 messages: recent,
1103 turnItems: [...new Set([5, ...prompts])].toSorted((a, b) => a - b),
1104 });
1105
1106 // Run 5 failed, and nothing is compacted yet, so the whole history is current.
1107 yield* store.apply({
1108 id: EventId.make("event:compacted:error-5"),
1109 type: "turn-item.updated",
1110 threadId,
1111 runId: runId(5),
1112 nodeId: rootNodeId(5),
1113 driver,
1114 occurredAt: at(5),
1115 payload: {
1116 ...item(5, 52, "failed"),
1117 id: TurnItemId.make("item:compacted:error-5"),
1118 type: "error",
1119 failure: {
1120 class: "usage_limit",
1121 message: "Plan limit reached.",
1122 resetAt: "2099-01-01T00:00:00.000Z",
1123 code: "usageLimitExceeded",
1124 retryable: null,
1125 },
1126 },
1127 });
1128 assert.deepEqual(yield* read(), { ...expected(all), turnItems: [1, ...all] });
1129
1130 // The window starts at the compaction's run. Queued run 2 and held run 4 are older but had
1131 // not ended by then.
1132 yield* compaction(7, "completed");
1133 assert.deepEqual(yield* read(), expected([2, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15]));
1134
1135 // The latest compaction wins.
1136 yield* compaction(11, "completed");
1137 const latest = expected([2, 4, 11, 12, 13, 14, 15]);
1138 assert.deepEqual(yield* read(), latest);
1139
1140 // A compaction that was rolled back, failed or is still running does not move the window.
1141 yield* compaction(13, "completed");
1142 yield* compaction(14, "failed");
1143 yield* compaction(15, "running");
1144 assert.deepEqual(yield* read(), latest);
1145
1146 // A named run and the run that holds a re-sent message are read too.
1147 assert.deepEqual(
1148 yield* read([runId(3)], messageId(6)),
1149 expected([2, 3, 4, 6, 11, 12, 13, 14, 15]),
1150 );
1151
1152 // Only an unfiltered read with runs narrows its prompts; any other read keeps its filter.
1153 const itemRuns = (fields: ReadonlyArray<"runs" | "turnItems">, filter: object) =>
1154 store
1155 .getThreadRecords(threadId, fields, {
1156 sinceLastCompaction: { keepRunIds: [], keepMessageId: messageId(0) },
1157 ...filter,
1158 })
1159 .pipe(Effect.map((records) => ordinals(records.turnItems.map((row) => row.runId))));
1160 assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunIds: [runId(5)] }), [5]);
1161 assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunIds: [] }), []);
1162 assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { turnItemRunId: runId(5) }), [5]);
1163 assert.deepEqual(yield* itemRuns(["runs", "turnItems"], { runIds: [runId(15)] }), [1, ...all]);
1164 assert.deepEqual(yield* itemRuns(["turnItems"], {}), [1, ...all]);
1165
1166 // A command also skips the node and the provider thread of each settled task. Tasks ended on
1167 // both sides of the compaction; one ran in the provider thread the thread's own runs use.
1168 const addProviderThread = (id: ProviderThreadId, appThreadId: ThreadId) =>
1169 store.apply({
1170 id: EventId.make(`event:compacted:${id}`),
1171 type: "provider-thread.updated",
1172 threadId,
1173 occurredAt: created,
1174 payload: {
1175 id,
1176 appThreadId,
1177 driver,
1178 providerInstanceId,
1179 providerSessionId: null,
1180 ownerNodeId: null,
1181 nativeThreadRef: null,
1182 nativeConversationHeadRef: null,
1183 lastRunOrdinal: null,
1184 handoffIds: [],
1185 forkedFrom: null,
1186 status: "idle",
1187 firstRunOrdinal: null,
1188 createdAt: created,
1189 updatedAt: created,
1190 },
1191 });
1192 yield* addProviderThread(providerThreadId, threadId);
1193 for (const [name, ordinal, delivery] of [
1194 ["old-delivered", 5, "delivered"],
1195 ["old-pending", 5, "pending"],
1196 ["old-own-thread", 5, "delivered"],
1197 ["recent-delivered", 12, "delivered"],
1198 ["recent-pending", 12, "pending"],
1199 ] as const) {
1200 const id = NodeId.make(`node:compacted:task:${name}`);
1201 const taskProviderThreadId =
1202 name === "old-own-thread"
1203 ? providerThreadId
1204 : ProviderThreadId.make(`provider-thread:compacted:task:${name}`);
1205 if (taskProviderThreadId !== providerThreadId) {
1206 yield* addProviderThread(
1207 taskProviderThreadId,
1208 ThreadId.make(`thread:compacted:child:${name}`),
1209 );
1210 }
1211 const task = {
1212 id,
1213 threadId,
1214 runId: runId(ordinal),
1215 parentNodeId: rootNodeId(ordinal),
1216 status: "completed",
1217 startedAt: at(ordinal),
1218 completedAt: at(ordinal),
1219 } as const;
1220 yield* store.apply({
1221 id: EventId.make(`event:compacted:task:${name}`),
1222 type: "subagent.updated",
1223 threadId,
1224 runId: runId(ordinal),
1225 nodeId: id,
1226 driver,
1227 providerInstanceId,
1228 occurredAt: at(ordinal),
1229 payload: {
1230 ...task,
1231 origin: "app_owned",
1232 createdBy: "agent",
1233 driver,
1234 providerInstanceId,
1235 providerThreadId: taskProviderThreadId,
1236 childThreadId: null,
1237 nativeTaskRef: null,
1238 prompt: "Do the work",
1239 title: name,
1240 model: null,
1241 completionDelivery: { state: delivery, observedByRunId: null },
1242 result: null,
1243 updatedAt: at(ordinal),
1244 },
1245 });
1246 yield* store.apply({
1247 id: EventId.make(`event:compacted:task-node:${name}`),
1248 type: "node.updated",
1249 threadId,
1250 runId: runId(ordinal),
1251 nodeId: id,
1252 driver,
1253 occurredAt: at(ordinal),
1254 payload: {
1255 ...task,
1256 rootNodeId: rootNodeId(ordinal),
1257 kind: "subagent",
1258 countsForRun: false,
1259 providerThreadId: null,
1260 providerTurnId: null,
1261 nativeItemRef: null,
1262 runtimeRequestId: null,
1263 checkpointScopeId: null,
1264 },
1265 });
1266 }
1267 const names = (ids: ReadonlyArray<string>) => ids.map((id) => id.split(":").at(-1)).toSorted();
1268 const readWithTasks = (filter: ProjectionStore.ProjectionRecordFilter) =>
1269 store
1270 .getThreadRecords(
1271 threadId,
1272 [
1273 "runs",
1274 "attempts",
1275 "nodes",
1276 "providerThreads",
1277 "providerTurns",
1278 "messages",
1279 "turnItems",
1280 ],
1281 { messageRoles: ["user"], omitSettledLeafNodes: true, ...filter },
1282 )
1283 .pipe(
1284 Effect.map((records) => ({
1285 runs: names(records.runs.map((run) => run.id)),
1286 turnItems: names(records.turnItems.map((row) => row.id)),
1287 attempts: names(records.attempts.map((attempt) => attempt.id)),
1288 rootNodes: names(
1289 records.nodes.flatMap((node) => (node.kind === "root_turn" ? [node.id] : [])),
1290 ),
1291 taskNodes: names(
1292 records.nodes.flatMap((node) => (node.kind === "subagent" ? [node.id] : [])),
1293 ),
1294 providerThreads: names(records.providerThreads.map((thread) => thread.id)),
1295 providerTurns: names(records.providerTurns.map((turn) => turn.id)),
1296 messages: names(records.messages.map((message) => message.id)),
1297 })),
1298 );
1299 const window = {
1300 sinceLastCompaction: { keepRunIds: [runId(3)], keepMessageId: messageId(6) },
1301 };
1302 const windowed = yield* readWithTasks(window);
1303 const unsettled = yield* readWithTasks({ omitSettledSubagentRecords: true });
1304 const both = yield* readWithTasks({ ...window, omitSettledSubagentRecords: true });
1305 // Each filter alone keeps records the other one drops.
1306 assert.deepEqual(windowed.taskNodes, ["recent-delivered", "recent-pending"]);
1307 assert.lengthOf(windowed.providerThreads, 5);
1308 assert.deepEqual(unsettled.taskNodes, ["old-pending", "recent-pending"]);
1309 assert.deepEqual(unsettled.providerThreads, ["compacted", "old-pending", "recent-pending"]);
1310 assert.lengthOf(unsettled.rootNodes, all.length);
1311 assert.lengthOf(unsettled.runs, 15);
1312 // Together: every run and the provider thread the runs use stay, and the windowed records are
1313 // read for the recent and named runs, less each settled task's node.
1314 const recentRuns = ["11", "12", "13", "14", "15", "2", "3", "4", "6"];
1315 assert.deepEqual(both, {
1316 ...unsettled,
1317 turnItems: windowed.turnItems,
1318 attempts: recentRuns,
1319 rootNodes: recentRuns,
1320 taskNodes: ["recent-pending"],
1321 providerTurns: recentRuns,
1322 messages: recentRuns,
1323 });
1324 const keptUnsettled = new Map(Object.entries(unsettled));
1325 assert.deepEqual(
1326 Object.fromEntries(
1327 Object.entries(windowed).map(([key, kept]) => [
1328 key,
1329 kept.filter((name) => keptUnsettled.get(key)?.includes(name)),
1330 ]),
1331 ),
1332 both,
1333 );
1334
1335 // A prompt that clients do not show cannot anchor a model switch. With the recent ones, and
1336 // run 4's, folded into async answers, the switch is measured from run 10, so its prompt is
1337 // read, with run 9's from the same batch.
1338 const storedRuns = (yield* store.getThreadRecords(threadId, ["runs"])).runs;
1339 for (const ordinal of [4, 11, 12, 14, 15]) {
1340 yield* store.apply({
1341 id: EventId.make(`event:compacted:async-run-${ordinal}`),
1342 type: "run.updated",
1343 threadId,
1344 occurredAt: at(ordinal),
1345 payload: {
1346 ...storedRuns.find((run) => run.id === runId(ordinal))!,
1347 userMessageId: MessageId.make(`async-answer:${ordinal}`),
1348 },
1349 });
1350 yield* store.apply({
1351 id: EventId.make(`event:compacted:async-${ordinal}`),
1352 type: "turn-item.updated",
1353 threadId,
1354 runId: runId(ordinal),
1355 nodeId: rootNodeId(ordinal),
1356 driver,
1357 occurredAt: at(ordinal),
1358 payload: {
1359 createdBy: "user",
1360 creationSource: "web",
1361 ...item(ordinal, ordinal * 10, "completed"),
1362 id: TurnItemId.make(`item:compacted:prompt-${ordinal}`),
1363 type: "user_message",
1364 messageId: MessageId.make(`async-answer:${ordinal}`),
1365 inputIntent: "turn_start",
1366 text: `Answer ${ordinal}`,
1367 attachments: [],
1368 },
1369 });
1370 }
1371 assert.deepEqual(
1372 (yield* read()).turnItems,
1373 expected([2, 4, 11, 12, 13, 14, 15], [2, 3, 4, 9, 10, 11, 12, 13, 14, 15]).turnItems,
1374 );
1375 }).pipe(Effect.provide(layerTest)),
1376 );
1377
1378 it.effect("record reads filter notification sources without dropping matching message bodies", () =>
1379 Effect.gen(function* () {
1380 const store = yield* ProjectionStore.ProjectionStoreV2;
1381 const threadId = yield* addRolledBackRecoveryCandidate("notification-filter");
1382 const now = yield* DateTime.now;
1383 for (const [id, role, kind] of [
1384 ["ordinary", "user", null],
1385 ["monitor", "user", "monitor"],
1386 ["system", "user", "system"],
1387 ["assistant-monitor", "assistant", "monitor"],
1388 ["wake", "user", "wake"],
1389 ] as const) {
1390 yield* store.apply({
1391 id: EventId.make(`notification-filter:${id}`),
1392 type: "message.updated",
1393 threadId,
1394 occurredAt: now,
1395 payload: {
1396 id: MessageId.make(id),
1397 threadId,
1398 runId: null,
1399 nodeId: null,
1400 createdBy: "agent",
1401 creationSource: "server",
1402 role,
1403 text: `Complete body for ${id}`,
1404 attachments: [],
1405 streaming: false,
1406 createdAt: now,
1407 updatedAt: now,
1408 ...(kind === null
1409 ? {}
1410 : kind === "wake"
1411 ? {
1412 delegatedCompletion: {
1413 parentRunId: RunId.make("run:wake-parent"),
1414 generation: 1,
1415 taskIds: [],
1416 },
1417 }
1418 : {
1419 notification: {
1420 source: { kind },
1421 outcome: "updated",
1422 summary: "Background notification",
1423 },
1424 }),
1425 },
1426 });
1427 }
1428 const matching = yield* store.getThreadRecords(threadId, ["messages"], {
1429 messageRoles: ["user"],
1430 monitorNotificationsOnly: true,
1431 });
1432 assert.deepEqual(
1433 matching.messages.map((message) => [message.id, message.text]),
1434 [["monitor", "Complete body for monitor"]],
1435 );
1436 const delegatedTaskMessages = yield* store.getThreadRecords(threadId, ["messages"], {
1437 messageRoles: ["user"],
1438 delegatedTaskMessagesOnly: true,
1439 });
1440 assert.deepEqual(
1441 delegatedTaskMessages.messages.map((message) => message.id),
1442 ["monitor", "wake"],
1443 );
1444 const { statements, tracer } = traceSqlStatements();
1445 const userMessages = yield* store
1446 .getThreadRecords(threadId, ["messages"], { messageRoles: ["user"] })
1447 .pipe(Effect.withTracer(tracer));
1448 assert.lengthOf(userMessages.messages, 4);
1449 const ordinalLookup = statements.find((statement) => statement.includes("MIN(ordinal)"));
1450 // User-only reads must seek the message-ordinal index instead of scanning every turn item.
1451 yield* assertReadsThroughMessageOrdinalIndex(ordinalLookup, ["thread_id=?", "<expr>=?"]);
1452 // With every user item loaded, the items give the order and the lookup does not run.
1453 const withItems = traceSqlStatements();
1454 const ordered = yield* store
1455 .getThreadRecords(threadId, ["messages", "turnItems"], {
1456 messageRoles: ["user"],
1457 turnItemTypes: ["user_message"],
1458 })
1459 .pipe(Effect.withTracer(withItems.tracer));
1460 assert.deepEqual(ordered.messages, userMessages.messages);
1461 assert.isFalse(withItems.statements.some((statement) => statement.includes("MIN(ordinal)")));
1462 assert.lengthOf((yield* store.getThreadRecords(threadId, ["messages"])).messages, 5);
1463 assert.lengthOf(
1464 (yield* store.getThreadRecords(threadId, ["messages"], {
1465 monitorNotificationsOnly: true,
1466 })).messages,
1467 2,
1468 );
1469 assert.lengthOf(
1470 (yield* store.getThreadRecords(threadId, ["messages"], {
1471 monitorNotificationsOnly: false,
1472 })).messages,
1473 5,
1474 );
1475 }).pipe(Effect.provide(layerTest)),
1476 );
1477
1478 it.effect("reads a delegated child's recent, unfinished and task runs without its history", () =>
1479 Effect.gen(function* () {
1480 const store = yield* ProjectionStore.ProjectionStoreV2;
1481 const threadId = yield* addRolledBackRecoveryCandidate("delegated-child-runs");
1482 const first = (yield* store.getThreadRecords(threadId, ["runs"])).runs[0]!;
1483 const at = (minutes: number) => DateTime.add(first.requestedAt, { minutes: minutes + 60 });
1484 const id = (ordinal: number) => RunId.make(`run:delegated-child-runs:${ordinal}`);
1485 const unstartedCancel = { status: "cancelled", startedAt: null } as const;
1486 for (const [ordinal, patch] of [
1487 [3, { status: "running", completedAt: null }],
1488 // Unstarted cancelled runs are outside the window's index: one ends after the floor.
1489 [4, { ...unstartedCancel, completedAt: at(20) }],
1490 [5, { ...unstartedCancel, completedAt: at(1) }],
1491 ...[2, 6, 7, 8, 9, 10, 11, 12].map((ordinal) => [ordinal, {}] as const),
1492 ] as const) {
1493 yield* store.apply({
1494 id: EventId.make(`event:delegated-child-runs:${ordinal}`),
1495 type: "run.created",
1496 threadId,
1497 runId: id(ordinal),
1498 occurredAt: first.requestedAt,
1499 payload: {
1500 ...first,
1501 id: id(ordinal),
1502 ordinal,
1503 status: "completed",
1504 startedAt: at(ordinal),
1505 completedAt: at(ordinal),
1506 delegatedTaskId: NodeId.make(`node:delegated-child-runs:task:${ordinal}`),
1507 ...patch,
1508 },
1509 });
1510 }
1511 const taskIds = [NodeId.make("node:delegated-child-runs:task:2")];
1512 const bounded = yield* store.getDelegatedChildControls(threadId, { taskIds, windowSize: 3 });
1513 assert.equal(bounded.runsFloor?.id, id(11));
1514 assert.deepEqual(
1515 bounded.runs.map((run) => run.id),
1516 [2, 3, 4, 11, 12].map(id),
1517 );
1518 const whole = yield* store.getDelegatedChildControls(threadId, { taskIds, windowSize: 100 });
1519 assert.isUndefined(whole.runsFloor);
1520 assert.deepEqual(
1521 whole.runs.map((run) => run.id),
1522 (yield* store.getThreadRecords(threadId, ["runs"])).runs.map((run) => run.id),
1523 );
1524 }).pipe(Effect.provide(layerTest)),
1525 );
1526
1527 it.effect("shells flag a latest run triggered by a provider background wake", () =>
1528 Effect.gen(function* () {
1529 const store = yield* ProjectionStore.ProjectionStoreV2;
1530 const threadId = yield* addRolledBackRecoveryCandidate("background-wake");
1531 const initial = yield* store.getThreadProjection(threadId);
1532 const at = initial.updatedAt;
1533 let ordinal = 1;
1534 for (const [id, source, outcome, expected] of [
1535 ["background-task", { kind: "background_task" }, "updated", true],
1536 ["monitor", { kind: "monitor" }, "updated", true],
1537 ["command", { kind: "command" }, "updated", true],
1538 ["command-finished", { kind: "command" }, "completed", false],
1539 ["monitor-stopped", { kind: "monitor" }, "cancelled", false],
1540 ["subagent", { kind: "subagent" }, "updated", false],
1541 ["delegated", { kind: "delegated_task", taskIds: [] }, "updated", false],
1542 ["steered", { kind: "background_task" }, "updated", false],
1543 ] as const) {
1544 const messageId = MessageId.make(`message:background-wake:${id}`);
1545 yield* store.apply({
1546 id: EventId.make(`event:background-wake:${id}:message`),
1547 type: "message.updated",
1548 threadId,
1549 occurredAt: at,
1550 payload: {
1551 id: messageId,
1552 threadId,
1553 runId: null,
1554 nodeId: null,
1555 createdBy: "agent",
1556 creationSource: "server",
1557 role: "user",
1558 text: id,
1559 attachments: [],
1560 streaming: false,
1561 createdAt: at,
1562 updatedAt: at,
1563 notification: { source, outcome, summary: "Background activity updated" },
1564 ...(id === "delegated"
1565 ? {
1566 delegatedCompletion: {
1567 parentRunId: initial.runs[0]!.id,
1568 generation: 1,
1569 taskIds: [],
1570 },
1571 }
1572 : {}),
1573 },
1574 });
1575 ordinal += 1;
1576 yield* store.apply({
1577 id: EventId.make(`event:background-wake:${id}:run`),
1578 type: "run.created",
1579 threadId,
1580 occurredAt: at,
1581 payload: {
1582 ...initial.runs[0]!,
1583 id: RunId.make(`run:background-wake:${id}`),
1584 ordinal,
1585 userMessageId: messageId,
1586 status: "completed",
1587 completedAt: at,
1588 },
1589 });
1590 if (id === "steered") {
1591 yield* store.apply({
1592 id: EventId.make("event:background-wake:steered:follow-up"),
1593 type: "message.updated",
1594 threadId,
1595 occurredAt: at,
1596 payload: {
1597 id: MessageId.make("message:background-wake:steered:follow-up"),
1598 threadId,
1599 runId: RunId.make("run:background-wake:steered"),
1600 nodeId: null,
1601 createdBy: "user",
1602 creationSource: "web",
1603 role: "user",
1604 text: "also fix X",
1605 attachments: [],
1606 streaming: false,
1607 createdAt: at,
1608 updatedAt: at,
1609 },
1610 });
1611 }
1612 const shells = [
1613 yield* store.getThreadShell(threadId),
1614 ProjectionStore.threadShellFromProjection(yield* store.getThreadProjection(threadId)),
1615 ];
1616 assert.deepEqual(
1617 shells.map((shell) => shell?.latestRunBackgroundWake),
1618 [expected, expected],
1619 id,
1620 );
1621 yield* assertShellIndexCoversShells();
1622 }
1623 }).pipe(Effect.provide(layerTest)),
1624 );
1625
1626 it.effect("SQL queue readiness skips held runs without bypassing active work", () =>
1627 Effect.gen(function* () {
1628 const store = yield* ProjectionStore.ProjectionStoreV2;
1629 const threadId = yield* addRolledBackRecoveryCandidate("held-queue");
1630 const { runs } = yield* store.getThreadProjection(threadId);
1631 const run = runs[0]!;
1632 const now = yield* DateTime.now;
1633 const held = { ...run, status: "queued" as const, queueHeld: true };
1634 yield* store.apply({
1635 id: EventId.make("held-queue:held"),
1636 type: "run.updated",
1637 threadId,
1638 occurredAt: now,
1639 payload: held,
1640 });
1641 assert.isFalse(yield* store.canStartQueuedRun(threadId));
1642
1643 const wake = { ...run, id: RunId.make("held-queue:wake"), ordinal: 2 };
1644 for (const status of ["running", "queued", "completed"] as const) {
1645 yield* store.apply({
1646 id: EventId.make(`held-queue:wake:${status}`),
1647 type: "run.updated",
1648 threadId,
1649 occurredAt: now,
1650 payload: { ...wake, status },
1651 });
1652 assert.equal(yield* store.canStartQueuedRun(threadId), status === "queued");
1653 }
1654 assert.deepEqual((yield* store.getThreadProjection(threadId)).runs[0], held);
1655 }).pipe(Effect.provide(layerTest)),
1656 );
1657
1658 it.effect("SQL shells flag only an active turn blocked solely on T3 thread waits", () =>
1659 Effect.gen(function* () {
1660 const store = yield* ProjectionStore.ProjectionStoreV2;
1661 const threadId = yield* addRolledBackRecoveryCandidate("thread-wait");
1662 const initial = yield* store.getThreadProjection(threadId);
1663 const at = initial.updatedAt;
1664 const previousCommand = initial.turnItems.find((item) => item.type === "command_execution")!;
1665 const itemBase = {
1666 ...previousCommand,
1667 providerThreadId: previousCommand.providerThreadId ?? null,
1668 providerTurnId: previousCommand.providerTurnId ?? null,
1669 nativeItemRef: previousCommand.nativeItemRef ?? null,
1670 parentItemId: previousCommand.parentItemId ?? null,
1671 };
1672 yield* store.apply({
1673 id: EventId.make("event:thread-wait:previous-completed"),
1674 type: "run.updated",
1675 threadId,
1676 occurredAt: at,
1677 payload: { ...initial.runs[0]!, status: "completed", completedAt: at },
1678 });
1679 const run = {
1680 ...initial.runs[0]!,
1681 id: RunId.make("run:thread-wait:active"),
1682 ordinal: 2,
1683 status: "running" as const,
1684 completedAt: null,
1685 };
1686 yield* store.apply({
1687 id: EventId.make("event:thread-wait:active"),
1688 type: "run.created",
1689 threadId,
1690 occurredAt: at,
1691 payload: run,
1692 });
1693 let sequence = 0;
1694 const updateItem = (payload: OrchestrationV2TurnItem) =>
1695 store.apply({
1696 id: EventId.make(`event:thread-wait:item:${sequence++}`),
1697 type: "turn-item.updated",
1698 threadId,
1699 runId: payload.runId ?? undefined,
1700 driver,
1701 occurredAt: at,
1702 payload,
1703 });
1704 const assertBlocked = Effect.fnUntraced(function* (expected: boolean) {
1705 const listed = (yield* store.getShellSnapshot()).threads.find((row) => row.id === threadId);
1706 for (const shell of [yield* store.getThreadShell(threadId), listed]) {
1707 assert.equal(shell?.activeRunId, run.id);
1708 assert.equal(shell?.status, "running");
1709 assert.equal(shell?.blockedOnThreadWait ?? false, expected);
1710 }
1711 yield* assertShellIndexCoversShells();
1712 });
1713 const wait = {
1714 ...itemBase,
1715 id: TurnItemId.make("item:thread-wait:active"),
1716 runId: run.id,
1717 type: "dynamic_tool" as const,
1718 toolName: "mcp__t3_code__t3_thread_wait",
1719 input: { threadId: "child-thread" },
1720 status: "running" as const,
1721 };
1722
1723 yield* updateItem({
1724 ...wait,
1725 id: TurnItemId.make("item:thread-wait:stale"),
1726 runId: initial.runs[0]!.id,
1727 });
1728 yield* assertBlocked(false);
1729 const foreign = {
1730 ...wait,
1731 id: TurnItemId.make("item:thread-wait:foreign"),
1732 toolName: "weather.t3_thread_wait",
1733 };
1734 yield* updateItem(foreign);
1735 yield* assertBlocked(false);
1736 yield* updateItem({ ...foreign, status: "completed", completedAt: at });
1737 yield* updateItem({ ...wait, status: "pending", startedAt: null });
1738 yield* assertBlocked(false);
1739 yield* updateItem(wait);
1740 yield* assertBlocked(true);
1741
1742 yield* updateItem({
1743 ...wait,
1744 id: TurnItemId.make("item:thread-wait:plan"),
1745 type: "todo_list",
1746 planId: PlanId.make("plan:thread-wait"),
1747 steps: [{ id: "review", text: "Review the child result", status: "running" }],
1748 });
1749 yield* assertBlocked(true);
1750 yield* updateItem({
1751 ...wait,
1752 id: TurnItemId.make("item:thread-wait:proposed-plan"),
1753 type: "proposed_plan",
1754 planId: PlanId.make("plan:thread-wait:proposed"),
1755 markdown: "Review the child result",
1756 streaming: true,
1757 });
1758 yield* updateItem({
1759 ...wait,
1760 id: TurnItemId.make("item:thread-wait:named-message"),
1761 nativeItemRef: { driver, nativeId: "named-message", strength: "weak" },
1762 type: "assistant_message",
1763 messageId: MessageId.make("message:thread-wait:named"),
1764 text: "Waiting for the child result",
1765 streaming: true,
1766 });
1767 yield* updateItem({
1768 ...wait,
1769 id: TurnItemId.make("item:thread-wait:named-thought"),
1770 nativeItemRef: { driver, nativeId: "named-thought", strength: "weak" },
1771 type: "reasoning",
1772 text: "I need the child result",
1773 streaming: true,
1774 });
1775 yield* assertBlocked(true);
1776
1777 const command = {
1778 ...itemBase,
1779 id: TurnItemId.make("item:thread-wait:concurrent"),
1780 runId: run.id,
1781 };
1782 yield* updateItem(command);
1783 yield* assertBlocked(false);
1784 yield* updateItem({ ...wait, status: "waiting" });
1785 yield* assertBlocked(false);
1786 yield* updateItem({ ...command, status: "completed", completedAt: at });
1787 yield* assertBlocked(true);
1788 const fileChange = {
1789 ...command,
1790 id: TurnItemId.make("item:thread-wait:concurrent-edit"),
1791 type: "file_change" as const,
1792 fileName: "example.ts",
1793 };
1794 yield* updateItem(fileChange);
1795 yield* assertBlocked(false);
1796 yield* updateItem({ ...fileChange, status: "completed", completedAt: at });
1797 yield* assertBlocked(true);
1798 const compaction = {
1799 ...command,
1800 id: TurnItemId.make("item:thread-wait:compaction"),
1801 type: "compaction" as const,
1802 driver,
1803 };
1804 yield* updateItem(compaction);
1805 yield* assertBlocked(false);
1806 yield* updateItem({ ...compaction, status: "completed", completedAt: at });
1807 yield* assertBlocked(true);
1808 yield* updateItem({ ...wait, status: "completed", completedAt: at });
1809 yield* assertBlocked(false);
1810
1811 const delegation = {
1812 ...wait,
1813 id: TurnItemId.make("item:thread-wait:delegation"),
1814 toolName: "t3-code-thread_opencode2-adapter_delegate_task",
1815 input: { task: "Review", mode: "async" },
1816 };
1817 yield* updateItem(delegation);
1818 yield* assertBlocked(false);
1819 yield* updateItem({ ...delegation, status: "completed", completedAt: at });
1820 const blockingDelegation = {
1821 ...delegation,
1822 id: TurnItemId.make("item:thread-wait:blocking-delegation"),
1823 input: { task: "Review", mode: "wait" },
1824 };
1825 yield* updateItem({
1826 ...command,
1827 id: TurnItemId.make("item:thread-wait:child"),
1828 type: "subagent",
1829 subagentId: NodeId.make("node:thread-wait:child"),
1830 origin: "app_owned",
1831 driver,
1832 providerInstanceId,
1833 childThreadId: ThreadId.make("child-thread"),
1834 prompt: "Review",
1835 result: null,
1836 });
1837 yield* updateItem(blockingDelegation);
1838 yield* assertBlocked(true);
1839 yield* updateItem({ ...blockingDelegation, status: "completed", completedAt: at });
1840 yield* assertBlocked(false);
1841 }).pipe(Effect.provide(layerTest)),
1842 );
1843
1844 it.effect("SQL shells use the latest subagent owner for duplicate provider-thread ids", () =>
1845 Effect.gen(function* () {
1846 const store = yield* ProjectionStore.ProjectionStoreV2;
1847 const threadId = yield* addRolledBackRecoveryCandidate("duplicate-provider-thread-owner");
1848 const initial = yield* store.getThreadProjection(threadId);
1849 const at = initial.updatedAt;
1850 const otherThreadId = yield* addRolledBackRecoveryCandidate(
1851 "duplicate-provider-thread-owner:other",
1852 );
1853 const other = yield* store.getThreadProjection(otherThreadId);
1854 const otherRun = other.runs[0]!;
1855 const otherInstanceId = ProviderInstanceId.make("codex-other");
1856 yield* store.apply({
1857 id: EventId.make("event:duplicate-provider-thread-owner:other-completed"),
1858 type: "run.updated",
1859 threadId: otherThreadId,
1860 occurredAt: at,
1861 payload: { ...otherRun, status: "completed", completedAt: at },
1862 });
1863 yield* store.apply({
1864 id: EventId.make("event:duplicate-provider-thread-owner:other-switched"),
1865 type: "thread.provider-switched",
1866 threadId: otherThreadId,
1867 occurredAt: at,
1868 payload: {
1869 ...other.thread,
1870 providerInstanceId: otherInstanceId,
1871 modelSelection: { ...modelSelection, instanceId: otherInstanceId },
1872 },
1873 });
1874 const providerThreadId = ProviderThreadId.make("provider-thread:duplicate-owner");
1875 const nativeTaskId = "native-task:duplicate-provider-thread-owner";
1876 for (const [appThreadId, owner] of [
1877 [otherThreadId, otherInstanceId],
1878 [threadId, providerInstanceId],
1879 ] as const) {
1880 yield* store.apply({
1881 id: EventId.make(`event:duplicate-provider-thread-owner:${owner}`),
1882 type: "provider-thread.updated",
1883 threadId: appThreadId,
1884 occurredAt: at,
1885 payload: {
1886 id: providerThreadId,
1887 driver,
1888 providerInstanceId: owner,
1889 providerSessionId: null,
1890 appThreadId,
1891 ownerNodeId: null,
1892 nativeThreadRef: null,
1893 nativeConversationHeadRef: null,
1894 status: "idle",
1895 firstRunOrdinal: 1,
1896 lastRunOrdinal: 1,
1897 handoffIds: [],
1898 forkedFrom: null,
1899 createdAt: at,
1900 updatedAt: at,
1901 pendingBackgroundTasks:
1902 owner === otherInstanceId
1903 ? [{ taskId: nativeTaskId, kind: "subagent", description: "Review" }]
1904 : [],
1905 },
1906 });
1907 }
1908 yield* store.apply({
1909 id: EventId.make("event:duplicate-provider-thread-owner:completed"),
1910 type: "run.updated",
1911 threadId,
1912 occurredAt: at,
1913 payload: { ...initial.runs[0]!, status: "completed", completedAt: at },
1914 });
1915 const item = {
1916 ...initial.turnItems[0]!,
1917 id: TurnItemId.make("item:duplicate-provider-thread-owner"),
1918 providerThreadId,
1919 nativeItemRef: { driver, nativeId: nativeTaskId, strength: "strong" as const },
1920 type: "subagent" as const,
1921 status: "running" as const,
1922 subagentId: NodeId.make("node:duplicate-provider-thread-owner"),
1923 origin: "provider_native" as const,
1924 driver,
1925 providerInstanceId,
1926 childThreadId: null,
1927 title: "Review",
1928 prompt: "Review",
1929 result: null,
1930 };
1931 yield* store.apply({
1932 id: EventId.make("event:duplicate-provider-thread-owner:item"),
1933 type: "turn-item.updated",
1934 threadId,
1935 occurredAt: at,
1936 payload: item,
1937 });
1938 yield* store.apply({
1939 id: EventId.make("event:duplicate-provider-thread-owner:other-item"),
1940 type: "turn-item.updated",
1941 threadId: otherThreadId,
1942 occurredAt: at,
1943 payload: {
1944 ...item,
1945 id: TurnItemId.make("item:duplicate-provider-thread-owner:other"),
1946 threadId: otherThreadId,
1947 runId: otherRun.id,
1948 nodeId: otherRun.rootNodeId,
1949 subagentId: NodeId.make("node:duplicate-provider-thread-owner:other"),
1950 },
1951 });
1952 yield* store.apply({
1953 id: EventId.make("event:duplicate-provider-thread-owner:switched"),
1954 type: "thread.provider-switched",
1955 threadId,
1956 occurredAt: at,
1957 payload: {
1958 ...initial.thread,
1959 providerInstanceId: otherInstanceId,
1960 modelSelection: { ...modelSelection, instanceId: otherInstanceId },
1961 },
1962 });
1963 assert.deepEqual((yield* store.getThreadProjection(otherThreadId)).providerThreads, []);
1964 const assertOwners = Effect.fnUntraced(function* (
1965 targetThreadId: ThreadId,
1966 expected: ReadonlyArray<unknown>,
1967 ) {
1968 const listed = (yield* store.getShellSnapshot()).threads.find(
1969 (thread) => thread.id === targetThreadId,
1970 );
1971 yield* assertShellIndexCoversShells();
1972 for (const shell of [yield* store.getThreadShell(targetThreadId), listed]) {
1973 assert.equal(shell?.providerInstanceId, otherInstanceId);
1974 assert.equal(shell?.activeRunId, null);
1975 assert.deepEqual(
1976 shell?.pendingBackgroundTasks?.filter((task) => task.kind === "subagent"),
1977 expected,
1978 );
1979 }
1980 });
1981 const owned = [
1982 { taskId: nativeTaskId, description: item.title, kind: "subagent", providerInstanceId },
1983 ];
1984 yield* assertOwners(threadId, owned);
1985 yield* assertOwners(otherThreadId, owned);
1986 yield* store.apply({
1987 id: EventId.make("event:duplicate-provider-thread-owner:stopped"),
1988 type: "turn-item.updated",
1989 threadId,
1990 occurredAt: at,
1991 payload: { ...item, status: "cancelled", completedAt: at },
1992 });
1993 yield* assertOwners(threadId, []);
1994 const ownerSnapshot = (yield* store.getThreadProjection(threadId)).providerThreads[0]!;
1995 // Rollback writes its provider snapshot before the rolled-back run events.
1996 yield* store.apply({
1997 id: EventId.make("event:duplicate-provider-thread-owner:rollback-provider-thread"),
1998 type: "provider-thread.updated",
1999 threadId: otherThreadId,
2000 occurredAt: at,
2001 payload: ownerSnapshot,
2002 });
2003 yield* store.apply({
2004 id: EventId.make("event:duplicate-provider-thread-owner:other-rolled-back"),
2005 type: "run.updated",
2006 threadId: otherThreadId,
2007 occurredAt: at,
2008 payload: { ...otherRun, status: "rolled_back", completedAt: at },
2009 });
2010 const rolledBack = yield* store.getThreadProjection(otherThreadId);
2011 assert.deepEqual(rolledBack.providerThreads, []);
2012 assert.equal(
2013 (yield* store.getThreadProjection(threadId)).thread.activeProviderThreadId,
2014 providerThreadId,
2015 );
2016 assert.deepEqual(
2017 ProjectionStore.threadShellFromProjection(rolledBack).pendingBackgroundTasks,
2018 [],
2019 );
2020 }).pipe(Effect.provide(layerTest)),
2021 );
2022
2023 const addOrphanedRecoveryCandidate = Effect.fn("addOrphanedRecoveryCandidate")(function* (
2024 suffix: string,
2025 ) {
2026 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2027 const now = yield* DateTime.now;
2028 const threadId = ThreadId.make(`thread:${suffix}:orphaned`);
2029 const runId = RunId.make(`run:${suffix}:missing`);
2030 const rootNodeId = NodeId.make(`node:${suffix}:orphaned`);
2031
2032 yield* projectionStore.apply({
2033 id: EventId.make(`event:${suffix}:thread-created`),
2034 type: "thread.created",
2035 threadId,
2036 occurredAt: now,
2037 payload: {
2038 createdBy: "user",
2039 creationSource: "web",
2040 id: threadId,
2041 projectId: ProjectId.make(`project:${suffix}`),
2042 title: "Orphaned recovery candidate",
2043 providerInstanceId,
2044 modelSelection,
2045 runtimeMode: "full-access",
2046 interactionMode: "default",
2047 branch: null,
2048 worktreePath: null,
2049 activeProviderThreadId: null,
2050 lineage: {
2051 parentThreadId: null,
2052 relationshipToParent: null,
2053 rootThreadId: threadId,
2054 },
2055 forkedFrom: null,
2056 createdAt: now,
2057 updatedAt: now,
2058 archivedAt: null,
2059 settledOverride: null,
2060 settledAt: null,
2061 lastVisitedAt: null,
2062 deletedAt: null,
2063 },
2064 });
2065 yield* projectionStore.apply({
2066 id: EventId.make(`event:${suffix}:item-running`),
2067 type: "turn-item.updated",
2068 threadId,
2069 runId,
2070 nodeId: rootNodeId,
2071 driver,
2072 occurredAt: now,
2073 payload: {
2074 id: TurnItemId.make(`item:${suffix}:orphaned`),
2075 threadId,
2076 runId,
2077 nodeId: rootNodeId,
2078 providerThreadId: null,
2079 providerTurnId: null,
2080 nativeItemRef: null,
2081 parentItemId: null,
2082 ordinal: 1,
2083 status: "running",
2084 title: "orphaned command",
2085 startedAt: now,
2086 completedAt: null,
2087 updatedAt: now,
2088 type: "command_execution",
2089 input: "sleep 60",
2090 },
2091 });
2092
2093 return threadId;
2094 });
2095
2096 const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
2097 const store = yield* ProjectionStore.ProjectionStoreV2;
2098 const threadId = yield* addRolledBackRecoveryCandidate("restart-cancelled-work-stale-update");
2099 const run = (yield* store.getThreadProjection(threadId)).runs[0]!;
2100 const now = yield* DateTime.now;
2101 const work = [{ kind: "subagent" as const, label: "Background subagent test" }];
2102 yield* store.apply({
2103 id: EventId.make("event:restart-cancelled-work-stale-update:recorded"),
2104 type: "run.background-work-cancelled",
2105 threadId,
2106 runId: run.id,
2107 providerInstanceId,
2108 occurredAt: now,
2109 payload: { runId: run.id, restartCancelledBackgroundWork: work },
2110 });
2111 yield* store.apply({
2112 id: EventId.make("event:restart-cancelled-work-stale-update:completed"),
2113 type: "run.updated",
2114 threadId,
2115 runId: run.id,
2116 providerInstanceId,
2117 occurredAt: now,
2118 payload: { ...run, status: "completed", completedAt: now },
2119 });
2120 const updated = (yield* store.getThreadProjection(threadId)).runs[0];
2121 assert.equal(updated?.status, "completed");
2122 assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
2123 });
2124
2125 const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
2126 const store = yield* ProjectionStore.ProjectionStoreV2;
2127 const now = yield* DateTime.now;
2128 const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
2129 const threadId = ThreadId.make(id);
2130 const thread = {
2131 createdBy: "user" as const,
2132 creationSource: "web" as const,
2133 id: threadId,
2134 projectId: ProjectId.make("project:worktree-paths"),
2135 title: `Title for ${id}`,
2136 providerInstanceId,
2137 modelSelection,
2138 runtimeMode: "full-access" as const,
2139 interactionMode: "default" as const,
2140 branch: input.worktreePath === null ? null : id,
2141 worktreePath: input.worktreePath,
2142 activeProviderThreadId: null,
2143 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2144 forkedFrom: null,
2145 createdAt: now,
2146 updatedAt: now,
2147 archivedAt: input.archived === true ? now : null,
2148 settledOverride: null,
2149 settledAt: null,
2150 lastVisitedAt: null,
2151 deletedAt: null,
2152 };
2153 return store
2154 .apply({
2155 id: EventId.make(`event:${id}:created`),
2156 type: "thread.created",
2157 threadId,
2158 occurredAt: now,
2159 payload: thread,
2160 })
2161 .pipe(Effect.as(thread));
2162 };
2163 yield* create("thread:active", { worktreePath: "/worktrees/active" });
2164 yield* create("thread:archived", { worktreePath: "/worktrees/archived", archived: true });
2165 yield* create("thread:local", { worktreePath: null });
2166 const deleted = yield* create("thread:deleted", { worktreePath: "/worktrees/deleted" });
2167 yield* store.apply({
2168 id: EventId.make("event:thread:deleted:deleted"),
2169 type: "thread.deleted",
2170 threadId: deleted.id,
2171 occurredAt: now,
2172 payload: { ...deleted, deletedAt: now },
2173 });
2174
2175 assert.deepEqual(yield* store.getThreadWorktreePaths(), [
2176 { threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
2177 ]);
2178 assert.deepEqual(yield* store.getThreadWorktreePaths({ includeArchived: true }), [
2179 { threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
2180 { threadId: ThreadId.make("thread:archived"), worktreePath: "/worktrees/archived" },
2181 ]);
2182 assert.deepEqual(
2183 yield* store.getThreadLinkMetadata([
2184 deleted.id,
2185 ThreadId.make("thread:active"),
2186 ThreadId.make("thread:archived"),
2187 ThreadId.make("unknown"),
2188 ]),
2189 {
2190 [ThreadId.make("thread:active")]: {
2191 title: "Title for thread:active",
2192 projectId: ProjectId.make("project:worktree-paths"),
2193 },
2194 [ThreadId.make("thread:archived")]: {
2195 title: "Title for thread:archived",
2196 projectId: ProjectId.make("project:worktree-paths"),
2197 },
2198 },
2199 );
2200 assert.deepEqual(
2201 yield* store.getThreadTitles([
2202 deleted.id,
2203 ThreadId.make("thread:active"),
2204 ThreadId.make("thread:archived"),
2205 ]),
2206 {
2207 [ThreadId.make("thread:active")]: "Title for thread:active",
2208 [ThreadId.make("thread:archived")]: "Title for thread:archived",
2209 },
2210 );
2211 });
2212
2213 const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
2214 const store = yield* ProjectionStore.ProjectionStoreV2;
2215 const sql = yield* SqlClient.SqlClient;
2216 const sharedSessionId = ProviderSessionId.make("session:settled-subagents:shared");
2217 const now = yield* DateTime.now;
2218 const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
2219 const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
2220 const activeRun = {
2221 ...settledRun,
2222 id: RunId.make("run:settled-leaf-nodes:active"),
2223 ordinal: 2,
2224 userMessageId: MessageId.make("message:settled-leaf-nodes:active"),
2225 rootNodeId: NodeId.make("node:settled-leaf-nodes:active-root"),
2226 status: "running" as const,
2227 completedAt: null,
2228 };
2229 yield* store.apply({
2230 id: EventId.make("event:settled-leaf-nodes:active-run"),
2231 type: "run.created",
2232 threadId,
2233 runId: activeRun.id,
2234 nodeId: activeRun.rootNodeId,
2235 driver,
2236 providerInstanceId,
2237 occurredAt: now,
2238 payload: activeRun,
2239 });
2240 const nodes = [
2241 ["settled-root", settledRun, "root_turn", "completed"],
2242 ["settled-tool", settledRun, "tool_call", "completed"],
2243 ["settled-message", settledRun, "assistant_message", "failed"],
2244 ["settled-reasoning", settledRun, "reasoning", "cancelled"],
2245 ["settled-subagent", settledRun, "subagent", "completed"],
2246 ["settled-run-live-tool", settledRun, "tool_call", "running"],
2247 ["settled-run-todo", settledRun, "todo_list", "running"],
2248 ["active-run-done-tool", activeRun, "tool_call", "completed"],
2249 ["active-run-todo", activeRun, "todo_list", "running"],
2250 ] as const;
2251 for (const [name, run, kind, status] of nodes) {
2252 const id = NodeId.make(`node:settled-leaf-nodes:${name}`);
2253 yield* store.apply({
2254 id: EventId.make(`event:settled-leaf-nodes:${name}`),
2255 type: "node.updated",
2256 threadId,
2257 runId: run.id,
2258 nodeId: id,
2259 driver,
2260 occurredAt: now,
2261 payload: {
2262 id,
2263 threadId,
2264 runId: run.id,
2265 rootNodeId: kind === "root_turn" ? id : run.rootNodeId!,
2266 parentNodeId: kind === "root_turn" ? null : run.rootNodeId,
2267 kind,
2268 status,
2269 countsForRun: kind === "root_turn",
2270 providerThreadId: null,
2271 providerTurnId: null,
2272 nativeItemRef: null,
2273 runtimeRequestId: null,
2274 checkpointScopeId: null,
2275 startedAt: now,
2276 completedAt: status === "running" ? null : now,
2277 },
2278 });
2279 if (kind === "todo_list") {
2280 yield* store.apply({
2281 id: EventId.make(`event:settled-leaf-nodes:${name}:plan`),
2282 type: "plan.updated",
2283 threadId,
2284 runId: run.id,
2285 nodeId: id,
2286 driver,
2287 occurredAt: now,
2288 payload: {
2289 id: PlanId.make(`plan:settled-leaf-nodes:${name}`),
2290 threadId,
2291 runId: run.id,
2292 nodeId: id,
2293 status: "active",
2294 kind: "todo_list",
2295 steps: [],
2296 },
2297 });
2298 }
2299 }
2300 // No row or node of this run is in the window.
2301 const settledCheckpointId = CheckpointId.make("checkpoint:settled-leaf-nodes:earlier-run");
2302 yield* store.apply({
2303 id: EventId.make("event:settled-leaf-nodes:earlier-run-checkpoint"),
2304 type: "checkpoint.captured",
2305 threadId,
2306 occurredAt: now,
2307 payload: {
2308 id: settledCheckpointId,
2309 threadId,
2310 scopeId: CheckpointScopeId.make("scope:settled-leaf-nodes:root"),
2311 runId: RunId.make("run:settled-leaf-nodes:earlier"),
2312 nodeId: NodeId.make("node:settled-leaf-nodes:earlier-root"),
2313 parentCheckpointId: null,
2314 ordinalWithinScope: 1,
2315 appRunOrdinal: 1,
2316 ref: CheckpointRef.make("refs/t3/settled-leaf-nodes/1"),
2317 status: "ready",
2318 files: [],
2319 capturedAt: now,
2320 },
2321 });
2322 const nodeNames = (records: { readonly nodes: ReadonlyArray<{ readonly id: string }> }) =>
2323 records.nodes
2324 .map((node) => node.id.replace("node:settled-leaf-nodes:", ""))
2325 .filter((name) => nodes.some(([candidate]) => candidate === name))
2326 .toSorted();
2327
2328 assert.deepEqual(
2329 nodeNames(yield* store.getThreadRecords(threadId, ["nodes"], { omitSettledLeafNodes: true })),
2330 [
2331 "active-run-done-tool",
2332 "active-run-todo",
2333 "settled-root",
2334 "settled-run-live-tool",
2335 "settled-run-todo",
2336 "settled-subagent",
2337 ],
2338 );
2339 assert.deepEqual(
2340 nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])),
2341 nodes.map(([name]) => name).toSorted(),
2342 );
2343 // A todo list stays running after its run ends. Counted as live work, every old run of a
2344 // long thread would ride along with each window, with its nodes and subagents.
2345 const liveWindow = (yield* store.getThreadSnapshotWindow(threadId, { rowLimit: 1 })).projection;
2346 assert.deepEqual(nodeNames(liveWindow), ["active-run-todo", "settled-run-live-tool"]);
2347 assert.deepEqual(
2348 liveWindow.plans.map((plan) => plan.id),
2349 [PlanId.make("plan:settled-leaf-nodes:active-run-todo")],
2350 );
2351 // Clients show the diff of the last finished turn from this record.
2352 assert.deepEqual(
2353 liveWindow.checkpoints.map((checkpoint) => checkpoint.id),
2354 [settledCheckpointId],
2355 );
2356
2357 const subagents = [
2358 ["delivered", settledRun, "completed", "delivered"],
2359 ["acknowledged", settledRun, "failed", "acknowledged"],
2360 ["disposed", settledRun, "cancelled", "disposed"],
2361 ["pending-delivery", settledRun, "completed", "pending"],
2362 ["claimed", settledRun, "completed", "claimed"],
2363 ["no-delivery", settledRun, "interrupted", null],
2364 ["running", settledRun, "running", null],
2365 ["active-run-delivered", activeRun, "completed", "delivered"],
2366 ] as const;
2367 // A follow-up reuses its task's child thread.
2368 const childThreadIds: Partial<Record<(typeof subagents)[number][0], ThreadId>> = {
2369 delivered: ThreadId.make("thread:settled-subagents:child-a"),
2370 running: ThreadId.make("thread:settled-subagents:child-a"),
2371 "no-delivery": ThreadId.make("thread:settled-subagents:child-b"),
2372 };
2373 for (const [index, [name, run, status, delivery]] of subagents.entries()) {
2374 const id = NodeId.make(`node:settled-subagents:${name}`);
2375 const childThreadId = childThreadIds[name] ?? null;
2376 yield* store.apply({
2377 id: EventId.make(`event:settled-subagents:item:${name}`),
2378 type: "turn-item.updated",
2379 threadId,
2380 runId: run.id,
2381 nodeId: id,
2382 driver,
2383 occurredAt: now,
2384 payload: {
2385 id: TurnItemId.make(`item:settled-subagents:${name}`),
2386 type: "subagent",
2387 threadId,
2388 runId: run.id,
2389 nodeId: id,
2390 providerThreadId: null,
2391 providerTurnId: null,
2392 nativeItemRef: null,
2393 parentItemId: null,
2394 ordinal: 100 + index,
2395 status: status === "running" ? "running" : "completed",
2396 title: name,
2397 startedAt: now,
2398 completedAt: status === "running" ? null : now,
2399 updatedAt: now,
2400 subagentId: id,
2401 origin: "app_owned",
2402 driver,
2403 providerInstanceId,
2404 childThreadId,
2405 prompt: "Do the work",
2406 result: null,
2407 },
2408 });
2409 yield* store.apply({
2410 id: EventId.make(`event:settled-subagents:${name}`),
2411 type: "subagent.updated",
2412 threadId,
2413 runId: run.id,
2414 nodeId: id,
2415 driver,
2416 providerInstanceId,
2417 occurredAt: now,
2418 payload: {
2419 id,
2420 threadId,
2421 runId: run.id,
2422 parentNodeId: run.rootNodeId!,
2423 origin: "app_owned",
2424 createdBy: "agent",
2425 driver,
2426 providerInstanceId,
2427 providerThreadId: ProviderThreadId.make(`provider-thread:settled-subagents:${name}`),
2428 childThreadId,
2429 nativeTaskRef: null,
2430 prompt: "Do the work",
2431 title: name,
2432 model: null,
2433 ...(delivery === null
2434 ? {}
2435 : { completionDelivery: { state: delivery, observedByRunId: null } }),
2436 status,
2437 result: null,
2438 startedAt: now,
2439 completedAt: status === "running" ? null : now,
2440 updatedAt: now,
2441 },
2442 });
2443 // Each task has a node in this thread and a provider thread in its child thread.
2444 yield* store.apply({
2445 id: EventId.make(`event:settled-subagents:node:${name}`),
2446 type: "node.updated",
2447 threadId,
2448 runId: run.id,
2449 nodeId: id,
2450 driver,
2451 occurredAt: now,
2452 payload: {
2453 id,
2454 threadId,
2455 runId: run.id,
2456 rootNodeId: run.rootNodeId!,
2457 parentNodeId: run.rootNodeId,
2458 kind: "subagent",
2459 status,
2460 countsForRun: false,
2461 providerThreadId: null,
2462 providerTurnId: null,
2463 nativeItemRef: null,
2464 runtimeRequestId: null,
2465 checkpointScopeId: null,
2466 startedAt: now,
2467 completedAt: status === "running" ? null : now,
2468 },
2469 });
2470 yield* store.apply({
2471 id: EventId.make(`event:settled-subagents:provider-thread:${name}`),
2472 type: "provider-thread.updated",
2473 threadId,
2474 occurredAt: now,
2475 payload: {
2476 id: ProviderThreadId.make(`provider-thread:settled-subagents:${name}`),
2477 appThreadId: ThreadId.make(`thread:settled-subagents:child:${name}`),
2478 driver,
2479 providerInstanceId,
2480 // One settled task ran in a provider session this thread also uses.
2481 providerSessionId: name === "acknowledged" ? sharedSessionId : null,
2482 ownerNodeId: null,
2483 nativeThreadRef: null,
2484 nativeConversationHeadRef: null,
2485 lastRunOrdinal: null,
2486 handoffIds: [],
2487 forkedFrom: null,
2488 status: "idle",
2489 firstRunOrdinal: null,
2490 createdAt: now,
2491 updatedAt: now,
2492 },
2493 });
2494 }
2495 yield* sql`INSERT INTO orchestration_v2_projection_provider_session_bindings
2496 (provider_session_id, thread_id) VALUES (${sharedSessionId}, ${threadId})`;
2497 // A command drops the node and the provider thread of each task it drops. The provider thread
2498 // in the shared session stays: detaching that session probes it for background work.
2499 const kept = ["active-run-delivered", "claimed", "no-delivery", "pending-delivery", "running"];
2500 const commandTrace = traceSqlStatements();
2501 const commandRecords = yield* store
2502 .getThreadRecords(threadId, ["nodes", "providerThreads"], {
2503 omitSettledLeafNodes: true,
2504 omitSettledSubagentRecords: true,
2505 })
2506 .pipe(Effect.withTracer(commandTrace.tracer));
2507 assert.deepEqual(
2508 commandRecords.nodes
2509 .flatMap((node) => (node.kind === "subagent" ? [node.id.split(":").at(-1)] : []))
2510 .toSorted(),
2511 // A subagent node that no task record names stays.
2512 [...kept, "settled-subagent"],
2513 );
2514 assert.deepEqual(
2515 commandRecords.providerThreads.map((thread) => thread.id.split(":").at(-1)).toSorted(),
2516 ["acknowledged", ...kept],
2517 );
2518 assert.lengthOf(
2519 (yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads,
2520 subagents.length,
2521 );
2522 // Both reads stay inside their indexes: no scan of the thread's nodes or provider threads.
2523 for (const [table, seeks] of [
2524 [
2525 "nodes",
2526 [
2527 "SEARCH node USING INTEGER PRIMARY KEY",
2528 "SEARCH settled USING COVERING INDEX",
2529 "SEARCH settled_node USING COVERING INDEX",
2530 "subagents_settled_idx (thread_id=?)",
2531 ],
2532 ],
2533 [
2534 "provider_threads",
2535 ["MULTI-INDEX OR", "provider_threads_session_idx (provider_session_id=?)"],
2536 ],
2537 ] as const) {
2538 const query = commandTrace.statements.find((statement) =>
2539 statement.includes(`FROM orchestration_v2_projection_${table}`),
2540 );
2541 assert.isDefined(query);
2542 const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
2543 .map((row) => row.detail)
2544 .join("\n");
2545 for (const seek of seeks) assert.include(plan, seek);
2546 assert.notInclude(plan, `SCAN orchestration_v2_projection_${table}`);
2547 }
2548 const subagentNames = (records: {
2549 readonly subagents: ReadonlyArray<{ readonly title: string | null }>;
2550 }) => records.subagents.map((subagent) => subagent.title ?? "").toSorted();
2551 assert.deepEqual(
2552 subagentNames(
2553 yield* store.getThreadRecords(threadId, ["subagents"], { omitSettledSubagents: true }),
2554 ),
2555 ["active-run-delivered", "claimed", "no-delivery", "pending-delivery", "running"],
2556 );
2557 assert.deepEqual(
2558 subagentNames(yield* store.getThreadRecords(threadId, ["subagents"])),
2559 subagents.map(([name]) => name).toSorted(),
2560 );
2561 // A command about one settled task keeps it, and reads only its node and turn item.
2562 const deliveredId = NodeId.make("node:settled-subagents:delivered");
2563 const forTask = yield* store.getThreadRecords(threadId, ["subagents", "nodes", "turnItems"], {
2564 omitSettledSubagents: { except: [deliveredId] },
2565 nodeIds: [NodeId.make("node:settled-leaf-nodes:settled-tool")],
2566 turnItemNodeIds: [deliveredId],
2567 });
2568 assert.deepEqual(subagentNames(forTask), [
2569 "active-run-delivered",
2570 "claimed",
2571 "delivered",
2572 "no-delivery",
2573 "pending-delivery",
2574 "running",
2575 ]);
2576 assert.deepEqual(nodeNames(forTask), ["settled-tool"]);
2577 assert.deepEqual(
2578 forTask.turnItems.map((item) => item.title),
2579 ["delivered"],
2580 );
2581 assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);
2582 assert.deepEqual(
2583 [...(yield* store.listAppOwnedChildThreadIds(threadId))],
2584 [childThreadIds.delivered, childThreadIds["no-delivery"]],
2585 );
2586 });
2587
2588 const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
2589 const store = yield* ProjectionStore.ProjectionStoreV2;
2590 const createdAt = DateTime.makeUnsafe("2026-10-06T14:00:00.000Z");
2591 const threadId = ThreadId.make("thread:background-updates");
2592 const thread = {
2593 createdBy: "user" as const,
2594 creationSource: "web" as const,
2595 id: threadId,
2596 projectId: ProjectId.make("project:background-updates"),
2597 title: "Background updates",
2598 providerInstanceId,
2599 modelSelection,
2600 runtimeMode: "full-access" as const,
2601 interactionMode: "default" as const,
2602 branch: null,
2603 worktreePath: null,
2604 activeProviderThreadId: null,
2605 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2606 forkedFrom: null,
2607 createdAt,
2608 updatedAt: createdAt,
2609 archivedAt: null,
2610 settledOverride: null,
2611 settledAt: null,
2612 lastVisitedAt: null,
2613 deletedAt: null,
2614 };
2615 yield* store.apply({
2616 id: EventId.make("event:background-updates:created"),
2617 type: "thread.created",
2618 threadId,
2619 occurredAt: createdAt,
2620 payload: thread,
2621 });
2622 const readActivity = Effect.gen(function* () {
2623 const projection = yield* store.getThreadProjection(threadId);
2624 const thread = yield* store.getThread(threadId);
2625 const shell = yield* store.getThreadShell(threadId);
2626 const listed = (yield* store.getShellSnapshot()).threads.find((entry) => entry.id === threadId);
2627 assert.deepEqual(projection.thread.updatedAt, createdAt);
2628 assert.deepEqual(thread.updatedAt, createdAt);
2629 assert.deepEqual(shell?.updatedAt, createdAt);
2630 assert.deepEqual(listed?.updatedAt, createdAt);
2631 return projection;
2632 });
2633 const link = {
2634 host: "github.com",
2635 repository: "owner/repository",
2636 number: 42,
2637 url: "https://github.com/owner/repository/pull/42",
2638 source: "manual" as const,
2639 linkedAt: DateTime.formatIso(createdAt),
2640 snapshot: null,
2641 stack: null,
2642 };
2643 yield* store.apply({
2644 id: EventId.make("event:background-updates:pull-request-synced"),
2645 type: "thread.pull-request-synced",
2646 threadId,
2647 occurredAt: DateTime.add(createdAt, { minutes: 45 }),
2648 payload: { ...thread, pullRequests: [link] },
2649 });
2650 const synced = yield* readActivity;
2651 assert.deepEqual(synced.updatedAt, createdAt);
2652 assert.equal(synced.thread.pullRequests?.[0]?.number, 42);
2653
2654 const snapshot = {
2655 state: "open" as const,
2656 title: "Ship it",
2657 headBranch: "feature",
2658 baseBranch: "main",
2659 isDraft: false,
2660 updatedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 46 })),
2661 syncedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 47 })),
2662 closedAt: null,
2663 mergedAt: null,
2664 };
2665 yield* store.apply({
2666 id: EventId.make("event:background-updates:pull-request-link-updated"),
2667 type: "thread.pull-request-link-updated",
2668 threadId,
2669 occurredAt: DateTime.add(createdAt, { minutes: 48 }),
2670 payload: { ...link, snapshot },
2671 });
2672 const refreshed = yield* readActivity;
2673 assert.deepEqual(refreshed.updatedAt, createdAt);
2674 assert.deepEqual(refreshed.thread.pullRequests?.[0]?.snapshot, snapshot);
2675
2676 yield* store.apply({
2677 id: EventId.make("event:background-updates:active-reordered"),
2678 type: "thread.active-reordered",
2679 threadId,
2680 occurredAt: DateTime.add(createdAt, { minutes: 50 }),
2681 payload: { ...refreshed.thread, activeOrderKey: "a" },
2682 });
2683 const reordered = yield* readActivity;
2684 assert.deepEqual(reordered.updatedAt, createdAt);
2685 assert.equal(reordered.thread.activeOrderKey, "a");
2686
2687 // MCP pins read the stored pin key from the active shell index.
2688 yield* store.apply({
2689 id: EventId.make("event:background-updates:pinned"),
2690 type: "thread.pinned",
2691 threadId,
2692 occurredAt: DateTime.add(createdAt, { minutes: 52 }),
2693 payload: {
2694 ...reordered.thread,
2695 pinnedAt: DateTime.add(createdAt, { minutes: 52 }),
2696 pinOrderKey: "m",
2697 },
2698 });
2699 const pinned = yield* readActivity;
2700 yield* assertShellIndexCoversShells();
2701
2702 // An already-unpinned thread keeps its activity time even when replay's envelope advances.
2703 yield* store.apply({
2704 id: EventId.make("event:background-updates:already-unpinned"),
2705 type: "thread.unpinned",
2706 threadId,
2707 occurredAt: DateTime.add(createdAt, { minutes: 55 }),
2708 payload: { ...pinned.thread, pinnedAt: null, pinOrderKey: null },
2709 });
2710 yield* readActivity;
2711 });
2712
2713 it.effect("SQL projection keeps background thread updates out of updatedAt", () =>
2714 backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
2715 );
2716
2717 const runlessNativeSubagentNeedsRecovery = Effect.gen(function* () {
2718 const store = yield* ProjectionStore.ProjectionStoreV2;
2719 const threadId = yield* addRolledBackRecoveryCandidate("runless-native-recovery");
2720 const now = yield* DateTime.now;
2721 const id = NodeId.make("node:runless-native-recovery");
2722 assert.notInclude(yield* store.getRecoveryThreadIds("runtime"), threadId);
2723 for (const status of ["pending", "running", "waiting", "completed"] as const) {
2724 yield* store.apply({
2725 id: EventId.make(`event:runless-native-recovery:${status}`),
2726 type: "subagent.updated",
2727 threadId,
2728 nodeId: id,
2729 driver,
2730 providerInstanceId,
2731 occurredAt: now,
2732 payload: {
2733 id,
2734 threadId,
2735 runId: null,
2736 parentNodeId: NodeId.make("node:runless-native-parent"),
2737 origin: "provider_native",
2738 createdBy: "agent",
2739 driver,
2740 providerInstanceId,
2741 providerThreadId: null,
2742 childThreadId: null,
2743 nativeTaskRef: { driver, nativeId: "runless-native", strength: "strong" },
2744 prompt: "Do the work",
2745 title: "Workflow member",
2746 model: null,
2747 status,
2748 result: null,
2749 startedAt: now,
2750 completedAt: status === "completed" ? now : null,
2751 updatedAt: now,
2752 },
2753 });
2754 assert.equal(
2755 (yield* store.getRecoveryThreadIds("runtime")).includes(threadId),
2756 status !== "completed",
2757 );
2758 }
2759 });
2760
2761 const subagentRecordsSelectById = Effect.gen(function* () {
2762 const store = yield* ProjectionStore.ProjectionStoreV2;
2763 const threadId = yield* addRolledBackRecoveryCandidate("subagent-records-by-id");
2764 const now = yield* DateTime.now;
2765 const ids = [NodeId.make("node:subagent-by-id:a"), NodeId.make("node:subagent-by-id:b")];
2766 for (const id of ids) {
2767 yield* store.apply({
2768 id: EventId.make(`event:${id}`),
2769 type: "subagent.updated",
2770 threadId,
2771 nodeId: id,
2772 driver,
2773 providerInstanceId,
2774 occurredAt: now,
2775 payload: {
2776 id,
2777 threadId,
2778 runId: null,
2779 parentNodeId: NodeId.make("node:subagent-by-id:parent"),
2780 origin: "app_owned",
2781 createdBy: "agent",
2782 driver,
2783 providerInstanceId,
2784 providerThreadId: null,
2785 childThreadId: null,
2786 nativeTaskRef: null,
2787 prompt: "Do the work",
2788 title: "Delegated task",
2789 model: null,
2790 status: "running",
2791 result: null,
2792 startedAt: now,
2793 completedAt: null,
2794 updatedAt: now,
2795 },
2796 });
2797 }
2798 const read = (subagentIds?: ReadonlyArray<NodeId>) =>
2799 store
2800 .getThreadRecords(threadId, ["subagents"], subagentIds === undefined ? {} : { subagentIds })
2801 .pipe(Effect.map(({ subagents }) => subagents.map((task) => task.id)));
2802 assert.deepEqual(yield* read(), ids);
2803 assert.deepEqual(yield* read([ids[1]!]), [ids[1]]);
2804 assert.deepEqual(yield* read([]), []);
2805 });
2806
2807 const recordsAt = (second: number) => DateTime.makeUnsafe(Date.UTC(2026, 0, 1, 0, 0, second));
2808 const recordsThreadCreated = (threadId: ThreadId) =>
2809 ({
2810 id: EventId.make(`event:${threadId}:created`),
2811 type: "thread.created",
2812 threadId,
2813 providerInstanceId,
2814 occurredAt: recordsAt(0),
2815 payload: {
2816 createdBy: "user",
2817 creationSource: "web",
2818 id: threadId,
2819 projectId: ProjectId.make("project:records"),
2820 title: threadId,
2821 providerInstanceId,
2822 modelSelection,
2823 runtimeMode: "full-access",
2824 interactionMode: "default",
2825 branch: null,
2826 worktreePath: null,
2827 activeProviderThreadId: null,
2828 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2829 forkedFrom: null,
2830 createdAt: recordsAt(0),
2831 updatedAt: recordsAt(0),
2832 archivedAt: null,
2833 settledOverride: null,
2834 settledAt: null,
2835 lastVisitedAt: null,
2836 deletedAt: null,
2837 },
2838 }) as never;
2839
2840 const queryPlan = Effect.fn("queryPlan")(function* (statement: string | undefined) {
2841 assert.isDefined(statement);
2842 const sql = yield* SqlClient.SqlClient;
2843 const rows = yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${statement}`);
2844 return rows.map((row) => row.detail);
2845 });
2846
2847 // SQLite's plan wording changes between versions, so only the index search, the keys it seeks
2848 // and the absence of a temporary B-tree are pinned.
2849 const assertReadsThroughMessageOrdinalIndex = Effect.fn("assertReadsThroughMessageOrdinalIndex")(
2850 function* (statement: string | undefined, keys: ReadonlyArray<string>) {
2851 const plan = yield* queryPlan(statement);
2852 const search = plan.find((detail) =>
2853 detail.includes("orchestration_v2_projection_turn_items_message_ordinal_idx"),
2854 );
2855 assert.isDefined(search, plan.join("\n"));
2856 for (const key of keys) assert.include(search, key);
2857 assert.isFalse(
2858 plan.some((detail) => detail.includes("TEMP B-TREE")),
2859 plan.join("\n"),
2860 );
2861 },
2862 );
2863
2864 // Each message's first item orders it, not the stored message order (the assistant reply is stored
2865 // first); the message-ordinal statement reads those ordinals through migration 94's index.
2866 const messageOrdinalsReadThroughTheirIndex = Effect.gen(function* () {
2867 const store = yield* ProjectionStore.ProjectionStoreV2;
2868 const threadId = ThreadId.make("thread:message-ordinals");
2869 yield* store.apply(recordsThreadCreated(threadId));
2870 const conversation = (name: string, firstOrdinal: number) => {
2871 const runId = RunId.make(`run:${name}`);
2872 const nodeId = NodeId.make(`node:${name}:root`);
2873 const user = MessageId.make(`message:${name}:user`);
2874 const assistant = MessageId.make(`message:${name}:assistant`);
2875 const item = (index: number) => ({
2876 id: TurnItemId.make(`item:${name}:${index}`),
2877 threadId,
2878 runId,
2879 nodeId,
2880 providerThreadId: null,
2881 providerTurnId: null,
2882 nativeItemRef: null,
2883 parentItemId: null,
2884 ordinal: firstOrdinal + index,
2885 status: "completed",
2886 title: null,
2887 startedAt: recordsAt(2),
2888 completedAt: recordsAt(2),
2889 updatedAt: recordsAt(2),
2890 });
2891 const message = (id: MessageId, role: "user" | "assistant", second: number) => ({
2892 id: EventId.make(`event:${id}`),
2893 type: "message.updated",
2894 threadId,
2895 occurredAt: recordsAt(second),
2896 payload: {
2897 createdBy: role === "user" ? "user" : "agent",
2898 creationSource: role === "user" ? "web" : "provider",
2899 id,
2900 threadId,
2901 runId,
2902 nodeId,
2903 role,
2904 text: `${name} ${role}`,
2905 attachments: [],
2906 streaming: false,
2907 createdAt: recordsAt(second),
2908 updatedAt: recordsAt(second),
2909 },
2910 });
2911 const turnItem = (index: number, payload: object) => ({
2912 id: EventId.make(`event:item:${name}:${index}`),
2913 type: "turn-item.updated",
2914 threadId,
2915 occurredAt: recordsAt(2),
2916 payload: { ...item(index), ...payload },
2917 });
2918 const events = [
2919 message(assistant, "assistant", 1),
2920 message(user, "user", 2),
2921 turnItem(0, {
2922 type: "user_message",
2923 createdBy: "user",
2924 creationSource: "web",
2925 messageId: user,
2926 inputIntent: "turn_start",
2927 text: `${name} user`,
2928 attachments: [],
2929 }),
2930 // A message streamed over two items keeps its first ordinal.
2931 ...[1, 2].map((index) =>
2932 turnItem(index, {
2933 type: "assistant_message",
2934 messageId: assistant,
2935 text: `${name} assistant ${index}`,
2936 streaming: false,
2937 }),
2938 ),
2939 turnItem(3, {
2940 type: "dynamic_tool",
2941 toolName: "tool",
2942 input: {},
2943 output: { messageId: user },
2944 }),
2945 ];
2946 return { events, user, assistant };
2947 };
2948 const first = conversation("first", 1);
2949 const second = conversation("second", 10);
2950 for (const event of [...first.events, ...second.events]) yield* store.apply(event as never);
2951
2952 const { statements, tracer } = traceSqlStatements();
2953 const indexed = yield* store
2954 .getThreadRecords(threadId, ["messages"])
2955 .pipe(Effect.withTracer(tracer));
2956 yield* assertReadsThroughMessageOrdinalIndex(
2957 statements.find((statement) => statement.includes("MIN(ordinal) AS ordinal")),
2958 ["thread_id=?"],
2959 );
2960 assert.deepEqual(
2961 indexed.messages.map((message) => message.id),
2962 [first.user, first.assistant, second.user, second.assistant],
2963 );
2964 // A narrow read (ids, runs or one role) seeks only its own messages in the same index.
2965 const narrowReads = [
2966 {
2967 filter: { messageIds: [second.assistant, first.user] },
2968 loaded: [first.user, second.assistant],
2969 },
2970 { filter: { messageRunIds: [RunId.make("run:first")] }, loaded: [first.user, first.assistant] },
2971 { filter: { messageRoles: ["user" as const] }, loaded: [first.user, second.user] },
2972 ];
2973 for (const { filter, loaded } of narrowReads) {
2974 const narrow = traceSqlStatements();
2975 const narrowed = yield* store
2976 .getThreadRecords(threadId, ["messages"], filter)
2977 .pipe(Effect.withTracer(narrow.tracer));
2978 assert.deepEqual(
2979 narrowed.messages.map((message) => message.id),
2980 loaded,
2981 );
2982 const bounded = narrow.statements.find((statement) =>
2983 statement.includes("MIN(ordinal) AS ordinal"),
2984 );
2985 yield* assertReadsThroughMessageOrdinalIndex(bounded, ["thread_id=?", "<expr>=?"]);
2986 }
2987 });
2988
2989 it.effect("reads message ordinals through their index", () =>
2990 messageOrdinalsReadThroughTheirIndex.pipe(Effect.provide(layerTest)),
2991 );
2992
2993 const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(function* (
2994 suffix: string,
2995 ) {
2996 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2997 const now = yield* DateTime.now;
2998 const projectId = ProjectId.make(`project:${suffix}:a`);
2999 const otherProjectId = ProjectId.make(`project:${suffix}:b`);
3000 const activeThreadId = ThreadId.make(`thread:${suffix}:a-active`);
3001 const archivedThreadId = ThreadId.make(`thread:${suffix}:a-archived`);
3002 const forkThreadId = ThreadId.make(`thread:${suffix}:a-fork`);
3003 const sourceThreadId = ThreadId.make(`thread:${suffix}:b-source`);
3004 const otherThreadId = ThreadId.make(`thread:${suffix}:b-other`);
3005 const sourceRunId = RunId.make(`run:${suffix}:b-source`);
3006 const sourceNodeId = NodeId.make(`node:${suffix}:b-source`);
3007 const createThread = (input: {
3008 readonly threadId: ThreadId;
3009 readonly projectId: ProjectId;
3010 readonly archived?: boolean;
3011 readonly forkedFromRunOf?: ThreadId;
3012 }) =>
3013 projectionStore.apply({
3014 id: EventId.make(`event:${input.threadId}:created`),
3015 type: "thread.created",
3016 threadId: input.threadId,
3017 occurredAt: now,
3018 payload: {
3019 createdBy: "user",
3020 creationSource: "web",
3021 id: input.threadId,
3022 projectId: input.projectId,
3023 title: input.threadId,
3024 providerInstanceId,
3025 modelSelection,
3026 runtimeMode: "full-access",
3027 interactionMode: "default",
3028 branch: null,
3029 worktreePath: null,
3030 activeProviderThreadId: null,
3031 lineage:
3032 input.forkedFromRunOf === undefined
3033 ? { parentThreadId: null, relationshipToParent: null, rootThreadId: input.threadId }
3034 : {
3035 parentThreadId: input.forkedFromRunOf,
3036 relationshipToParent: "fork",
3037 rootThreadId: input.forkedFromRunOf,
3038 },
3039 forkedFrom:
3040 input.forkedFromRunOf === undefined
3041 ? null
3042 : { type: "run", threadId: input.forkedFromRunOf, runId: sourceRunId },
3043 createdAt: now,
3044 updatedAt: now,
3045 archivedAt: input.archived === true ? now : null,
3046 settledOverride: null,
3047 settledAt: null,
3048 lastVisitedAt: null,
3049 deletedAt: null,
3050 },
3051 });
3052
3053 yield* createThread({ threadId: sourceThreadId, projectId: otherProjectId, archived: true });
3054 yield* createThread({ threadId: otherThreadId, projectId: otherProjectId });
3055 yield* createThread({ threadId: activeThreadId, projectId });
3056 yield* createThread({ threadId: archivedThreadId, projectId, archived: true });
3057 yield* createThread({ threadId: forkThreadId, projectId, forkedFromRunOf: sourceThreadId });
3058 yield* projectionStore.apply({
3059 id: EventId.make(`event:${suffix}:source-run`),
3060 type: "run.created",
3061 threadId: sourceThreadId,
3062 runId: sourceRunId,
3063 nodeId: sourceNodeId,
3064 driver,
3065 occurredAt: now,
3066 payload: {
3067 id: sourceRunId,
3068 threadId: sourceThreadId,
3069 ordinal: 1,
3070 providerInstanceId,
3071 modelSelection,
3072 providerThreadId: null,
3073 userMessageId: MessageId.make(`message:${suffix}:source-user`),
3074 rootNodeId: sourceNodeId,
3075 activeAttemptId: null,
3076 status: "completed",
3077 requestedAt: now,
3078 startedAt: now,
3079 completedAt: now,
3080 checkpointId: null,
3081 contextHandoffId: null,
3082 },
3083 });
3084 yield* projectionStore.apply({
3085 id: EventId.make(`event:${suffix}:source-item`),
3086 type: "turn-item.updated",
3087 threadId: sourceThreadId,
3088 runId: sourceRunId,
3089 occurredAt: now,
3090 payload: {
3091 id: TurnItemId.make(`turn-item:${suffix}:source`),
3092 threadId: sourceThreadId,
3093 runId: sourceRunId,
3094 nodeId: null,
3095 providerThreadId: null,
3096 providerTurnId: null,
3097 nativeItemRef: null,
3098 parentItemId: null,
3099 ordinal: 1,
3100 status: "completed",
3101 title: null,
3102 startedAt: now,
3103 completedAt: now,
3104 updatedAt: now,
3105 type: "assistant_message",
3106 messageId: MessageId.make(`message:${suffix}:source-assistant`),
3107 text: "Source answer",
3108 streaming: false,
3109 },
3110 });
3111
3112 const unscoped = yield* projectionStore.getShellSnapshot();
3113 const inProject = (thread: { readonly projectId: ProjectId }) => thread.projectId === projectId;
3114 const active = yield* projectionStore.getShellSnapshot({ projectId, location: "active" });
3115 assert.deepEqual(active.threads, unscoped.threads.filter(inProject));
3116 assert.deepEqual(active.archivedThreads, []);
3117 assert.sameMembers(
3118 active.threads.map((thread) => thread.id),
3119 [activeThreadId, forkThreadId],
3120 );
3121 const fork = active.threads.find((thread) => thread.id === forkThreadId);
3122 assert.isAbove(fork?.visibleItemCount ?? 0, 0);
3123
3124 const all = yield* projectionStore.getShellSnapshot({ projectId });
3125 assert.deepEqual(all.threads, active.threads);
3126 assert.deepEqual(all.archivedThreads, unscoped.archivedThreads.filter(inProject));
3127 assert.sameMembers(
3128 all.archivedThreads.map((thread) => thread.id),
3129 [archivedThreadId],
3130 );
3131 const missingThreadId = ThreadId.make(`thread:${suffix}:missing`);
3132 const targeted = yield* projectionStore.getShellSnapshot({
3133 threadIds: [forkThreadId, archivedThreadId, missingThreadId],
3134 });
3135 for (const threadId of [forkThreadId, archivedThreadId, missingThreadId]) {
3136 assert.deepEqual(
3137 [...targeted.threads, ...targeted.archivedThreads].find((shell) => shell.id === threadId) ??
3138 null,
3139 yield* projectionStore.getThreadShell(threadId),
3140 );
3141 }
3142 return { projectId, activeThreadId, forkThreadId, otherThreadId };
3143 });
3144
3145 it.effect("SQL shells read a subagent family and a worktree's threads", () =>
3146 Effect.gen(function* () {
3147 const store = yield* ProjectionStore.ProjectionStoreV2;
3148 const now = yield* DateTime.now;
3149 const projectId = ProjectId.make("project:family");
3150 const worktreePath = "/repo/.t3/worktrees/family";
3151 const id = (name: string) => ThreadId.make(`thread:family:${name}`);
3152 const payloadOf = (input: {
3153 readonly name: string;
3154 readonly parent?: string;
3155 readonly root?: string;
3156 readonly worktreePath?: string;
3157 readonly archived?: boolean;
3158 }) =>
3159 ({
3160 createdBy: "user",
3161 creationSource: input.parent === undefined ? "web" : "mcp",
3162 id: id(input.name),
3163 projectId,
3164 title: input.name,
3165 providerInstanceId,
3166 modelSelection,
3167 runtimeMode: "full-access",
3168 interactionMode: "default",
3169 branch: null,
3170 worktreePath: input.worktreePath ?? null,
3171 activeProviderThreadId: null,
3172 lineage: {
3173 parentThreadId: input.parent === undefined ? null : id(input.parent),
3174 relationshipToParent: input.parent === undefined ? null : "subagent",
3175 rootThreadId: id(input.root ?? input.name),
3176 },
3177 forkedFrom: null,
3178 createdAt: now,
3179 updatedAt: now,
3180 archivedAt: input.archived === true ? now : null,
3181 settledOverride: null,
3182 settledAt: null,
3183 lastVisitedAt: null,
3184 deletedAt: null,
3185 }) as const;
3186 const createThread = (input: Parameters<typeof payloadOf>[0]) =>
3187 store.apply({
3188 id: EventId.make(`event:family:${input.name}`),
3189 type: "thread.created",
3190 threadId: id(input.name),
3191 occurredAt: now,
3192 payload: payloadOf(input),
3193 });
3194 yield* createThread({ name: "root", worktreePath });
3195 yield* createThread({ name: "child", parent: "root", root: "root", worktreePath });
3196 yield* createThread({ name: "grandchild", parent: "child", root: "root" });
3197 yield* createThread({ name: "archived-child", parent: "root", root: "root", archived: true });
3198 yield* createThread({ name: "other", worktreePath: "/repo" });
3199 yield* createThread({ name: "other-child", parent: "other", root: "other", worktreePath });
3200
3201 const family = (threadId: ThreadId) =>
3202 store
3203 .getShellSnapshot({ location: "active", familyOf: threadId })
3204 .pipe(Effect.map((snapshot) => snapshot.threads.map((thread) => thread.id)));
3205 // Any member, or the root itself, names the same active tree, root included.
3206 const tree = [id("root"), id("child"), id("grandchild")];
3207 assert.sameMembers([...(yield* family(id("root")))], tree);
3208 assert.sameMembers([...(yield* family(id("grandchild")))], tree);
3209 assert.deepEqual([...(yield* family(id("missing")))], []);
3210
3211 // Includes another root's idle child on the same path, which a shell may leave out.
3212 const { statements, tracer } = traceSqlStatements();
3213 const onWorktree = yield* store
3214 .getShellSnapshot({ location: "active", worktreePath })
3215 .pipe(Effect.withTracer(tracer));
3216 assert.sameMembers(
3217 onWorktree.threads.map((thread) => thread.id),
3218 [id("root"), id("child"), id("other-child")],
3219 );
3220 // A path names a few threads; scanning every thread parses every payload.
3221 const sql = yield* SqlClient.SqlClient;
3222 const threadQuery = statements.find((statement) =>
3223 statement.includes("AS latest_run_stop_requested"),
3224 );
3225 assert.isDefined(threadQuery);
3226 const threadPlan = (yield* sql.unsafe<{ readonly detail: string }>(
3227 `EXPLAIN QUERY PLAN ${threadQuery}`,
3228 [],
3229 )).map((row) => row.detail);
3230 assert.notInclude(threadPlan, "SCAN t", threadPlan.join("\n"));
3231 // A fork keeps its deleted source's root, so the deleted root still names the live tree.
3232 yield* store.apply({
3233 id: EventId.make("event:family:root-deleted"),
3234 type: "thread.deleted",
3235 threadId: id("root"),
3236 occurredAt: now,
3237 payload: { ...payloadOf({ name: "root", worktreePath }), deletedAt: now },
3238 });
3239 assert.sameMembers([...(yield* family(id("root")))], [id("child"), id("grandchild")]);
3240 }).pipe(Effect.provide(layerTest)),
3241 );
3242
3243 it.effect("SQL list older settled threads below a cursor", () =>
3244 Effect.gen(function* () {
3245 const store = yield* ProjectionStore.ProjectionStoreV2;
3246 const now = yield* DateTime.now;
3247 const id = (name: string) => ThreadId.make(`thread:older:${name}`);
3248 const at = (day: number) => DateTime.makeUnsafe(Date.UTC(2026, 9, day));
3249 const createThread = (input: {
3250 readonly name: string;
3251 readonly settledDay: number | null;
3252 readonly project?: string;
3253 readonly subagent?: boolean;
3254 readonly archived?: boolean;
3255 readonly snoozedUntilDay?: number;
3256 }) =>
3257 store.apply({
3258 id: EventId.make(`event:older:${input.name}`),
3259 type: "thread.created",
3260 threadId: id(input.name),
3261 occurredAt: now,
3262 payload: {
3263 createdBy: "user",
3264 creationSource: "web",
3265 id: id(input.name),
3266 projectId: ProjectId.make(input.project ?? "project:a"),
3267 title: input.name,
3268 providerInstanceId,
3269 modelSelection,
3270 runtimeMode: "full-access",
3271 interactionMode: "default",
3272 branch: null,
3273 worktreePath: null,
3274 activeProviderThreadId: null,
3275 lineage: {
3276 parentThreadId: input.subagent === true ? id("b") : null,
3277 relationshipToParent: input.subagent === true ? "subagent" : null,
3278 rootThreadId: input.subagent === true ? id("b") : id(input.name),
3279 },
3280 forkedFrom: null,
3281 createdAt: now,
3282 updatedAt: now,
3283 archivedAt: input.archived === true ? now : null,
3284 settledOverride: input.settledDay === null ? null : "settled",
3285 settledAt: input.settledDay === null ? null : at(input.settledDay),
3286 snoozedUntil: input.snoozedUntilDay === undefined ? null : at(input.snoozedUntilDay),
3287 lastVisitedAt: null,
3288 deletedAt: null,
3289 },
3290 });
3291 yield* createThread({ name: "a", settledDay: 5 });
3292 yield* createThread({ name: "b", settledDay: 4 });
3293 yield* createThread({ name: "c", settledDay: 4 });
3294 yield* createThread({ name: "d", settledDay: 3, project: "project:b" });
3295 yield* createThread({ name: "snoozed", settledDay: 2, snoozedUntilDay: 30 });
3296 yield* createThread({ name: "recent", settledDay: 20 });
3297 yield* createThread({ name: "unsettled", settledDay: null });
3298 yield* createThread({ name: "child", settledDay: 1, subagent: true });
3299 yield* createThread({ name: "archived", settledDay: 1, archived: true });
3300
3301 const list = (
3302 before: { readonly settledAt: string; readonly threadId?: ThreadId },
3303 limit: number,
3304 projectIds?: ReadonlyArray<ProjectId>,
3305 ) => store.listOlderSettledThreads({ before, projectIds, limit });
3306 const before = { settledAt: DateTime.formatIso(at(10)) };
3307 const first = yield* list(before, 2);
3308 assert.deepEqual(first, { threadIds: [id("a"), id("b")], counts: [] });
3309 assert.sameDeepMembers(
3310 [...(yield* list(before, 0)).counts],
3311 [
3312 { projectId: ProjectId.make("project:a"), count: 4 },
3313 { projectId: ProjectId.make("project:b"), count: 1 },
3314 ],
3315 );
3316 // A cursor without an id is strict, as the snapshot's cut is: the day-4 tie is not counted.
3317 assert.sameDeepMembers(
3318 [...(yield* list({ settledAt: DateTime.formatIso(at(4)) }, 0)).counts],
3319 [
3320 { projectId: ProjectId.make("project:a"), count: 1 },
3321 { projectId: ProjectId.make("project:b"), count: 1 },
3322 ],
3323 );
3324 // The cursor is the last row: the tie on day 4 continues by id.
3325 const next = yield* list({ settledAt: DateTime.formatIso(at(4)), threadId: id("b") }, 10);
3326 assert.deepEqual(next.threadIds, [id("c"), id("d"), id("snoozed")]);
3327 assert.deepEqual(yield* list(before, 25, []), { threadIds: [], counts: [] });
3328 yield* assertShellIndexCoversShells();
3329 const scoped = yield* list(before, 0, [ProjectId.make("project:b")]);
3330 assert.deepEqual(scoped, {
3331 threadIds: [],
3332 counts: [{ projectId: ProjectId.make("project:b"), count: 1 }],
3333 });
3334 }).pipe(Effect.provide(layerTest)),
3335 );
3336
3337 // regression: #25806 pending request cards outlive the history window that drops their turn
3338 it.effect("SQL snapshot windows keep cards for requests still pending", () =>
3339 Effect.gen(function* () {
3340 const store = yield* ProjectionStore.ProjectionStoreV2;
3341 const now = yield* DateTime.now;
3342 const threadId = ThreadId.make("thread:pending-request-window");
3343 yield* store.apply({
3344 id: EventId.make("event:pending-request-window:thread"),
3345 type: "thread.created",
3346 threadId,
3347 occurredAt: now,
3348 payload: {
3349 createdBy: "user",
3350 creationSource: "web",
3351 id: threadId,
3352 projectId: ProjectId.make("project:pending-request-window"),
3353 title: "Pending request window",
3354 providerInstanceId,
3355 modelSelection,
3356 runtimeMode: "full-access",
3357 interactionMode: "default",
3358 branch: null,
3359 worktreePath: null,
3360 activeProviderThreadId: null,
3361 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
3362 forkedFrom: null,
3363 createdAt: now,
3364 updatedAt: now,
3365 archivedAt: null,
3366 settledOverride: null,
3367 settledAt: null,
3368 lastVisitedAt: null,
3369 deletedAt: null,
3370 },
3371 });
3372 let ordinal = 0;
3373 const itemBase = (runId: RunId, nodeId: NodeId, id: string) => ({
3374 id: TurnItemId.make(`item:pending-request-window:${id}`),
3375 threadId,
3376 runId,
3377 nodeId,
3378 providerThreadId: null,
3379 providerTurnId: null,
3380 nativeItemRef: null,
3381 parentItemId: null,
3382 ordinal: ++ordinal,
3383 status: "completed" as const,
3384 title: null,
3385 startedAt: now,
3386 completedAt: now,
3387 updatedAt: now,
3388 });
3389 const request = (id: string, nodeId: NodeId, kind: "user_input" | "command") => ({
3390 id: RuntimeRequestId.make(`request:pending-request-window:${id}`),
3391 nodeId,
3392 providerTurnId: null,
3393 nativeRequestRef: null,
3394 kind,
3395 responseCapability: {
3396 type: "live" as const,
3397 providerSessionId: ProviderSessionId.make("session:pending-request-window"),
3398 },
3399 createdAt: now,
3400 });
3401 const turnCount = THREAD_HISTORY_PAGE_POLICY.maxUserTurns + 4;
3402 for (let turn = 1; turn <= turnCount; turn += 1) {
3403 const runId = RunId.make(`run:pending-request-window:${turn}`);
3404 const nodeId = NodeId.make(`node:pending-request-window:${turn}`);
3405 yield* store.apply({
3406 id: EventId.make(`event:pending-request-window:run:${turn}`),
3407 type: "run.created",
3408 threadId,
3409 runId,
3410 nodeId,
3411 driver,
3412 occurredAt: now,
3413 payload: {
3414 id: runId,
3415 threadId,
3416 ordinal: turn,
3417 providerInstanceId,
3418 modelSelection,
3419 providerThreadId: null,
3420 userMessageId: MessageId.make(`message:pending-request-window:${turn}`),
3421 rootNodeId: nodeId,
3422 activeAttemptId: null,
3423 status: "completed",
3424 requestedAt: now,
3425 startedAt: now,
3426 completedAt: now,
3427 checkpointId: null,
3428 contextHandoffId: null,
3429 },
3430 });
3431 const items: Array<OrchestrationV2TurnItem> = [
3432 {
3433 ...itemBase(runId, nodeId, `message:${turn}`),
3434 type: "user_message",
3435 createdBy: "user",
3436 creationSource: "web",
3437 messageId: MessageId.make(`message:pending-request-window:${turn}`),
3438 inputIntent: "turn_start",
3439 text: `Turn ${turn}`,
3440 attachments: [],
3441 },
3442 ];
3443 if (turn === 1) {
3444 const question = {
3445 id: "scope",
3446 header: "Scope",
3447 question: "Which scope?",
3448 options: [{ label: "All", description: "Everything" }],
3449 };
3450 items.push(
3451 {
3452 ...itemBase(runId, nodeId, "question:pending"),
3453 status: "running",
3454 type: "user_input_request",
3455 requestId: request("question:pending", nodeId, "user_input").id,
3456 questions: [question],
3457 },
3458 {
3459 ...itemBase(runId, nodeId, "approval:pending"),
3460 status: "running",
3461 type: "approval_request",
3462 requestId: request("approval:pending", nodeId, "command").id,
3463 requestKind: "command",
3464 prompt: "rm -rf build",
3465 },
3466 {
3467 ...itemBase(runId, nodeId, "question:resolved"),
3468 type: "user_input_request",
3469 requestId: request("question:resolved", nodeId, "user_input").id,
3470 questions: [question],
3471 },
3472 );
3473 for (const [id, kind, status] of [
3474 ["question:pending", "user_input", "pending"],
3475 ["approval:pending", "command", "pending"],
3476 ["question:resolved", "user_input", "resolved"],
3477 ] as const) {
3478 yield* store.apply({
3479 id: EventId.make(`event:pending-request-window:request:${id}`),
3480 type: "runtime-request.updated",
3481 threadId,
3482 nodeId,
3483 driver,
3484 occurredAt: now,
3485 payload: {
3486 ...request(id, nodeId, kind),
3487 status,
3488 resolvedAt: status === "pending" ? null : now,
3489 },
3490 });
3491 }
3492 }
3493 for (const item of items) {
3494 yield* store.apply({
3495 id: EventId.make(`event:pending-request-window:${item.id}`),
3496 type: "turn-item.updated",
3497 threadId,
3498 runId,
3499 nodeId,
3500 driver,
3501 occurredAt: now,
3502 payload: item,
3503 });
3504 }
3505 }
3506
3507 const pendingCardIds = [
3508 TurnItemId.make("item:pending-request-window:question:pending"),
3509 TurnItemId.make("item:pending-request-window:approval:pending"),
3510 ];
3511 const { statements, tracer } = traceSqlStatements();
3512 const snapshot = yield* store
3513 .getThreadSnapshotWindow(threadId, {
3514 rowLimit: THREAD_HISTORY_PAGE_POLICY.maxItems + 2,
3515 userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
3516 })
3517 .pipe(Effect.withTracer(tracer));
3518 const sql = yield* SqlClient.SqlClient;
3519 const query = statements.find(
3520 (statement) =>
3521 statement.includes("FROM orchestration_v2_projection_runtime_requests AS request") &&
3522 statement.includes("SELECT item.payload_json"),
3523 );
3524 assert.isDefined(query);
3525 const plan = (yield* sql.unsafe<{ readonly detail: string }>(
3526 `EXPLAIN QUERY PLAN ${query}`,
3527 )).map((row) => row.detail);
3528 assert.include(
3529 plan[0],
3530 "SEARCH request USING INDEX orchestration_v2_projection_runtime_requests_thread_status_idx",
3531 );
3532 assert.include(
3533 plan[1],
3534 "SEARCH item USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx",
3535 );
3536 const bounded = buildBoundedThreadProjection({
3537 projection: snapshot.projection,
3538 snapshotSequence: snapshot.snapshotSequence,
3539 });
3540 assert.isTrue(bounded.hasMoreHistory);
3541 assert.notInclude(
3542 bounded.projection.turnItems.map((item) => item.id),
3543 TurnItemId.make("item:pending-request-window:question:resolved"),
3544 );
3545 for (const projection of [snapshot.projection, bounded.projection]) {
3546 const visibleIds = projection.visibleTurnItems.map((row) => row.sourceItemId);
3547 assert.notInclude(visibleIds, TurnItemId.make("item:pending-request-window:message:1"));
3548 assert.includeMembers(
3549 projection.turnItems.map((item) => item.id),
3550 pendingCardIds,
3551 );
3552 for (const id of pendingCardIds) assert.notInclude(visibleIds, id);
3553 }
3554 }).pipe(Effect.provide(layerTest)),
3555 );
3556
3557 it.effect("SQL snapshot windows leave handoff bodies in SQLite without changing the wire", () =>
3558 Effect.gen(function* () {
3559 const store = yield* ProjectionStore.ProjectionStoreV2;
3560 const now = yield* DateTime.now;
3561 const threadId = ThreadId.make("thread:handoff-window");
3562 yield* store.apply({
3563 id: EventId.make("event:handoff-window:thread"),
3564 type: "thread.created",
3565 threadId,
3566 occurredAt: now,
3567 payload: {
3568 createdBy: "user",
3569 creationSource: "web",
3570 id: threadId,
3571 projectId: ProjectId.make("project:handoff-window"),
3572 title: "Handoff window",
3573 providerInstanceId,
3574 modelSelection,
3575 runtimeMode: "full-access",
3576 interactionMode: "default",
3577 branch: null,
3578 worktreePath: null,
3579 activeProviderThreadId: null,
3580 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
3581 forkedFrom: null,
3582 createdAt: now,
3583 updatedAt: now,
3584 archivedAt: null,
3585 settledOverride: null,
3586 settledAt: null,
3587 lastVisitedAt: null,
3588 deletedAt: null,
3589 },
3590 });
3591 const providerThreadId = ProviderThreadId.make("provider-thread:handoff-window");
3592 const earlierItemId = TurnItemId.make("item:handoff-window:earlier");
3593 const handoff = (id: string, status: "ready" | "superseded") => ({
3594 id: ContextHandoffId.make(`handoff:handoff-window:${id}`),
3595 transferId: null,
3596 threadId,
3597 targetRunId: RunId.make("run:handoff-window"),
3598 fromProviderThreadIds: [providerThreadId],
3599 toProviderThreadId: providerThreadId,
3600 coveredRunOrdinals: { from: 1, to: 1 },
3601 strategy: "full_thread_summary" as const,
3602 status,
3603 summaryMessageId: null,
3604 summaryText: `Summary ${id}`,
3605 history: {
3606 messages: [
3607 {
3608 role: "user" as const,
3609 text: "Earlier request ".repeat(1_000),
3610 threadId,
3611 runId: null,
3612 itemId: earlierItemId,
3613 providerThreadId,
3614 status: "completed",
3615 kind: "user_message",
3616 },
3617 ],
3618 coverage: "full",
3619 omittedItems: 0,
3620 },
3621 delivery: { nativeThreadId: "native", status: "injected" as const, itemIds: [earlierItemId] },
3622 createdByProviderInstanceId: providerInstanceId,
3623 createdAt: now,
3624 updatedAt: now,
3625 });
3626 // The window keeps a ready handoff and one its rows show; the third is outside it.
3627 const handoffs = [
3628 handoff("ready", "ready"),
3629 handoff("shown", "superseded"),
3630 handoff("old", "superseded"),
3631 ];
3632 for (const payload of handoffs) {
3633 yield* store.apply({
3634 id: EventId.make(`event:${payload.id}`),
3635 type: "context-handoff.updated",
3636 threadId,
3637 occurredAt: now,
3638 payload,
3639 });
3640 }
3641 yield* store.apply({
3642 id: EventId.make("event:handoff-window:item"),
3643 type: "turn-item.updated",
3644 threadId,
3645 occurredAt: now,
3646 payload: {
3647 id: TurnItemId.make("item:handoff-window:handoff"),
3648 threadId,
3649 runId: null,
3650 nodeId: null,
3651 providerThreadId: null,
3652 providerTurnId: null,
3653 nativeItemRef: null,
3654 parentItemId: null,
3655 ordinal: 1,
3656 status: "completed",
3657 title: null,
3658 startedAt: now,
3659 completedAt: now,
3660 updatedAt: now,
3661 type: "handoff",
3662 contextHandoffId: handoffs[1]!.id,
3663 fromProviderThreadIds: [providerThreadId],
3664 toProviderThreadId: providerThreadId,
3665 fromProviderInstanceIds: [providerInstanceId],
3666 toProviderInstanceId: providerInstanceId,
3667 strategy: "full_thread_summary",
3668 },
3669 });
3670
3671 const window = yield* store.getThreadSnapshotWindow(threadId, { rowLimit: 10 });
3672 const windowHandoffs = window.projection.contextHandoffs;
3673 assert.deepEqual(
3674 windowHandoffs.map((row) => row.id),
3675 [handoffs[0]!.id, handoffs[1]!.id],
3676 );
3677 for (const row of windowHandoffs) {
3678 assert.isUndefined(row.history);
3679 assert.isUndefined(row.delivery);
3680 }
3681 const full = yield* store.getThreadSnapshot(threadId);
3682 assert.deepEqual(full.projection.contextHandoffs, handoffs);
3683 assert.deepEqual(
3684 buildBoundedThreadStreamSnapshot(window),
3685 buildBoundedThreadStreamSnapshot({
3686 ...window,
3687 projection: { ...window.projection, contextHandoffs: handoffs.slice(0, 2) },
3688 }),
3689 );
3690 }).pipe(Effect.provide(layerTest)),
3691 );
3692
3693 it.effect("lists only active subagent IDs without loading shell data", () =>
3694 Effect.gen(function* () {
3695 const store = yield* ProjectionStore.ProjectionStoreV2;
3696 const sql = yield* SqlClient.SqlClient;
3697 const createdAt = "2026-01-05T00:00:00.000Z";
3698 for (const [id, relationship, archivedAt, deletedAt] of [
3699 ["parent", null, null, null],
3700 ["active-subagent", "subagent", null, null],
3701 ["archived-subagent", "subagent", createdAt, null],
3702 ["deleted-subagent", "subagent", null, createdAt],
3703 ] as const) {
3704 yield* sql`
3705 INSERT INTO orchestration_v2_projection_threads (
3706 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
3707 created_at, updated_at, archived_at, deleted_at, payload_json
3708 ) VALUES (
3709 ${id}, 'project:test', ${id}, 'codex', 'full-access', 'default',
3710 ${createdAt}, ${createdAt}, ${archivedAt}, ${deletedAt},
3711 ${JSON.stringify({ lineage: { relationshipToParent: relationship }, archivedAt })}
3712 )
3713 `;
3714 }
3715
3716 assert.sameMembers(
3717 [...(yield* store.listActiveSubagentThreadIds())],
3718 [ThreadId.make("active-subagent")],
3719 );
3720 }).pipe(Effect.provide(layerTest)),
3721 );
3722
3723 // regression: #25621 SQLite rejects statements binding more than 32,766 variables
3724 it.effect("lists every thread in the shell snapshot past SQLite's bound variable limit", () =>
3725 Effect.gen(function* () {
3726 const store = yield* ProjectionStore.ProjectionStoreV2;
3727 const sql = yield* SqlClient.SqlClient;
3728 const now = yield* DateTime.now;
3729 const templateId = ThreadId.make("thread:many:00000");
3730 yield* store.apply({
3731 id: EventId.make(`event:${templateId}:created`),
3732 type: "thread.created",
3733 threadId: templateId,
3734 occurredAt: now,
3735 payload: {
3736 createdBy: "user",
3737 creationSource: "web",
3738 id: templateId,
3739 projectId: ProjectId.make("project:many"),
3740 title: "Many",
3741 providerInstanceId,
3742 modelSelection,
3743 runtimeMode: "full-access",
3744 interactionMode: "default",
3745 branch: null,
3746 worktreePath: null,
3747 activeProviderThreadId: null,
3748 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: templateId },
3749 forkedFrom: null,
3750 createdAt: now,
3751 updatedAt: now,
3752 archivedAt: null,
3753 settledOverride: null,
3754 settledAt: null,
3755 lastVisitedAt: null,
3756 deletedAt: null,
3757 },
3758 });
3759 const threadCount = 32_767;
3760 yield* sql`
3761 CREATE TEMP TABLE many_threads AS
3762 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${threadCount - 1})
3763 SELECT t.*, n.i AS clone_index FROM orchestration_v2_projection_threads t, n
3764 WHERE t.thread_id = ${templateId}
3765 `;
3766 yield* sql`
3767 UPDATE many_threads SET
3768 thread_id = printf('thread:many:%05d', clone_index),
3769 payload_json = json_set(
3770 payload_json,
3771 '$.id', printf('thread:many:%05d', clone_index),
3772 '$.lineage.rootThreadId', printf('thread:many:%05d', clone_index)
3773 )
3774 `;
3775 yield* sql`ALTER TABLE many_threads DROP COLUMN clone_index`;
3776 yield* sql`INSERT INTO orchestration_v2_projection_threads SELECT * FROM many_threads`;
3777
3778 const [unscoped, active] = yield* Effect.all([
3779 store.getShellSnapshot(),
3780 store.getShellSnapshot({ location: "active" }),
3781 ]);
3782 assert.strictEqual(unscoped.threads.length, threadCount);
3783 assert.strictEqual(active.threads.length, threadCount);
3784 }).pipe(Effect.provide(layerTest)),
3785 );
3786
3787 it.layer(layerTest)("ProjectionStoreV2", (it) => {
3788 // regression: #21969 keep corrupt plan JSON in the typed read error
3789 it.effect("returns a typed read error for malformed stored plan JSON", () =>
3790 Effect.gen(function* () {
3791 const store = yield* ProjectionStore.ProjectionStoreV2;
3792 const sql = yield* SqlClient.SqlClient;
3793 const threadId = yield* addRolledBackRecoveryCandidate("corrupt-plan");
3794 const planId = PlanId.make("plan:corrupt");
3795 yield* sql`INSERT INTO orchestration_v2_projection_plans
3796 (plan_id, thread_id, node_id, kind, status, payload_json)
3797 VALUES (${planId}, ${threadId}, 'node:corrupt-plan', 'proposed_plan', 'active', '{broken')`;
3798
3799 const failure = yield* store
3800 .getPlan(threadId, planId)
3801 .pipe(
3802 Effect.flip,
3803 Effect.ensuring(
3804 Effect.orDie(
3805 sql`DELETE FROM orchestration_v2_projection_plans WHERE plan_id = ${planId}`,
3806 ),
3807 ),
3808 );
3809 assert.strictEqual(failure._tag, "ProjectionStoreReadError");
3810 }),
3811 );
3812
3813 // regression: #25157 streamed payloads are not re-parsed to read their indexed timestamps
3814 it.effect("indexes streamed payload timestamps without parsing the payload JSON", () =>
3815 Effect.gen(function* () {
3816 const store = yield* ProjectionStore.ProjectionStoreV2;
3817 const sql = yield* SqlClient.SqlClient;
3818 const threadId = yield* addRolledBackRecoveryCandidate("streamed-payload-parse");
3819 const runId = RunId.make("run:streamed-payload-parse:rolled-back");
3820 const createdAt = DateTime.makeUnsafe("2026-01-02T03:04:05.006Z");
3821 const updatedAt = DateTime.makeUnsafe("2026-01-02T03:04:06.007Z");
3822 const text = "x".repeat(128 * 1024);
3823 const messageId = MessageId.make("message:streamed-payload-parse");
3824 const itemId = TurnItemId.make("item:streamed-payload-parse");
3825
3826 const parsedPayloads = yield* Effect.acquireUseRelease(
3827 Effect.sync(() => vi.spyOn(JSON, "parse")),
3828 (parse) =>
3829 Effect.gen(function* () {
3830 yield* store.apply({
3831 id: EventId.make("event:streamed-payload-parse:message"),
3832 type: "message.updated",
3833 threadId,
3834 occurredAt: updatedAt,
3835 payload: {
3836 createdBy: "agent",
3837 creationSource: "server",
3838 id: messageId,
3839 threadId,
3840 runId,
3841 nodeId: null,
3842 role: "assistant",
3843 text,
3844 attachments: [],
3845 streaming: true,
3846 createdAt,
3847 updatedAt,
3848 },
3849 });
3850 yield* store.apply({
3851 id: EventId.make("event:streamed-payload-parse:item"),
3852 type: "turn-item.updated",
3853 threadId,
3854 runId,
3855 occurredAt: updatedAt,
3856 payload: {
3857 id: itemId,
3858 threadId,
3859 runId,
3860 nodeId: null,
3861 providerThreadId: null,
3862 providerTurnId: null,
3863 nativeItemRef: null,
3864 parentItemId: null,
3865 ordinal: 1,
3866 type: "assistant_message",
3867 status: "running",
3868 title: "Assistant",
3869 messageId,
3870 text,
3871 streaming: true,
3872 startedAt: createdAt,
3873 completedAt: null,
3874 updatedAt,
3875 },
3876 });
3877 return parse.mock.calls
3878 .filter(([json]) => typeof json === "string" && json.includes(text))
3879 .map(([json]) => (JSON.parse(json) as { readonly id: string }).id);
3880 }),
3881 (parse) => Effect.sync(() => parse.mockRestore()),
3882 );
3883
3884 assert.deepStrictEqual(parsedPayloads, []);
3885 assert.deepStrictEqual(
3886 yield* sql`
3887 SELECT created_at, updated_at FROM orchestration_v2_projection_messages
3888 WHERE message_id = ${messageId}
3889 `,
3890 [{ created_at: DateTime.formatIso(createdAt), updated_at: DateTime.formatIso(updatedAt) }],
3891 );
3892 assert.deepStrictEqual(
3893 yield* sql`
3894 SELECT updated_at FROM orchestration_v2_projection_turn_items
3895 WHERE turn_item_id = ${itemId}
3896 `,
3897 [{ updated_at: DateTime.formatIso(updatedAt) }],
3898 );
3899 }),
3900 );
3901
3902 it.effect(
3903 "SQL recovery selects unfinished runless native subagents",
3904 () => runlessNativeSubagentNeedsRecovery,
3905 );
3906 it.effect(
3907 "worktree path and title lookups skip deleted threads, and can include archived ones",
3908 () => threadWorktreePathsIncludeArchivedOnRequest,
3909 );
3910
3911 it.effect(
3912 "keeps restart-cancelled work through a stale run.updated",
3913 () => restartCancelledWorkSurvivesStaleRunUpdate,
3914 );
3915 it.effect("records restart-cancelled work without regressing a run that completed since", () =>
3916 Effect.gen(function* () {
3917 const store = yield* ProjectionStore.ProjectionStoreV2;
3918 const threadId = yield* addRolledBackRecoveryCandidate("restart-cancelled-work");
3919 const run = (yield* store.getThreadProjection(threadId)).runs[0]!;
3920 const now = yield* DateTime.now;
3921 yield* store.apply({
3922 id: EventId.make("event:restart-cancelled-work:completed"),
3923 type: "run.updated",
3924 threadId,
3925 runId: run.id,
3926 providerInstanceId,
3927 occurredAt: now,
3928 payload: { ...run, status: "completed", completedAt: now },
3929 });
3930 const work = [{ kind: "subagent" as const, label: "Background subagent test" }];
3931 yield* store.apply({
3932 id: EventId.make("event:restart-cancelled-work:recorded"),
3933 type: "run.background-work-cancelled",
3934 threadId,
3935 runId: run.id,
3936 providerInstanceId,
3937 occurredAt: now,
3938 payload: { runId: run.id, restartCancelledBackgroundWork: work },
3939 });
3940 const recorded = (yield* store.getThreadProjection(threadId)).runs[0];
3941 assert.equal(recorded?.status, "completed");
3942 assert.deepEqual(recorded?.restartCancelledBackgroundWork, work);
3943 const [recovery] = (yield* store.getThreadRecords(threadId, ["runs"], {
3944 runIds: [run.id],
3945 })).runs;
3946 assert.equal(recovery?.status, "completed");
3947 assert.deepEqual(recovery?.restartCancelledBackgroundWork, work);
3948 }),
3949 );
3950 it.effect("record reads select subagents by id", () => subagentRecordsSelectById);
3951 it.effect(
3952 "command reads skip finished leaf nodes of finished runs",
3953 () => commandReadsSkipSettledLeafNodes,
3954 );
3955 it.effect("preserves stored provider usage when a terminal update omits it", () =>
3956 Effect.gen(function* () {
3957 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3958 const now = yield* DateTime.now;
3959 const threadId = ThreadId.make("thread:provider-usage-reload");
3960 const providerThreadId = ProviderThreadId.make("provider-thread:provider-usage-reload");
3961 const providerTurnId = ProviderTurnId.make("provider-turn:provider-usage-reload");
3962 const nodeId = NodeId.make("node:provider-usage-reload");
3963 const initialUsage = {
3964 usedTokens: 12_000,
3965 maxTokens: 200_000,
3966 inputTokens: 11_000,
3967 outputTokens: 1_000,
3968 updatedAt: "2026-08-29T12:00:00.000Z",
3969 } as const;
3970 const replacementUsage = {
3971 usedTokens: 18_000,
3972 maxTokens: 200_000,
3973 inputTokens: 16_000,
3974 outputTokens: 2_000,
3975 updatedAt: "2026-08-29T12:00:01.000Z",
3976 } as const;
3977
3978 yield* projectionStore.apply({
3979 id: EventId.make("event:provider-usage-reload:thread"),
3980 type: "thread.created",
3981 threadId,
3982 occurredAt: now,
3983 payload: {
3984 createdBy: "user",
3985 creationSource: "web",
3986 id: threadId,
3987 projectId: ProjectId.make("project:provider-usage-reload"),
3988 title: "Provider usage reload",
3989 providerInstanceId,
3990 modelSelection,
3991 runtimeMode: "full-access",
3992 interactionMode: "default",
3993 branch: null,
3994 worktreePath: null,
3995 activeProviderThreadId: null,
3996 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
3997 forkedFrom: null,
3998 createdAt: now,
3999 updatedAt: now,
4000 archivedAt: null,
4001 settledOverride: null,
4002 settledAt: null,
4003 lastVisitedAt: null,
4004 deletedAt: null,
4005 },
4006 });
4007
4008 const providerTurn = {
4009 id: providerTurnId,
4010 providerThreadId,
4011 nodeId,
4012 runAttemptId: null,
4013 nativeTurnRef: null,
4014 ordinal: 1,
4015 status: "running" as const,
4016 startedAt: now,
4017 completedAt: null,
4018 };
4019 yield* projectionStore.apply({
4020 id: EventId.make("event:provider-usage-reload:running"),
4021 type: "provider-turn.updated",
4022 threadId,
4023 nodeId,
4024 driver,
4025 occurredAt: now,
4026 payload: { ...providerTurn, tokenUsage: initialUsage },
4027 });
4028 yield* projectionStore.apply({
4029 id: EventId.make("event:provider-usage-reload:completed"),
4030 type: "provider-turn.updated",
4031 threadId,
4032 nodeId,
4033 driver,
4034 occurredAt: now,
4035 payload: { ...providerTurn, status: "completed", completedAt: now },
4036 });
4037
4038 const reloaded = yield* projectionStore.getThreadProjection(threadId);
4039 assert.deepEqual(reloaded.providerTurns[0]?.tokenUsage, initialUsage);
4040 assert.strictEqual(reloaded.providerTurns[0]?.status, "completed");
4041
4042 yield* projectionStore.apply({
4043 id: EventId.make("event:provider-usage-reload:replacement"),
4044 type: "provider-turn.updated",
4045 threadId,
4046 nodeId,
4047 driver,
4048 occurredAt: now,
4049 payload: {
4050 ...providerTurn,
4051 status: "completed",
4052 completedAt: now,
4053 tokenUsage: replacementUsage,
4054 },
4055 });
4056
4057 const replaced = yield* projectionStore.getThreadProjection(threadId);
4058 assert.deepEqual(replaced.providerTurns[0]?.tokenUsage, replacementUsage);
4059 }),
4060 );
4061
4062 it.effect("reads a fixed SQL turn-item window for long histories and repeated clients", () =>
4063 Effect.gen(function* () {
4064 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
4065 const sql = yield* SqlClient.SqlClient;
4066 const now = yield* DateTime.now;
4067 const nowIso = DateTime.formatIso(now);
4068 const threadId = ThreadId.make("thread:bounded-sql-history");
4069 // The raw SQL edits below stand in for committed writes, which always append a thread event.
4070 let revision = 0;
4071 const committed = <A, E, R>(read: Effect.Effect<A, E, R>) =>
4072 sql`INSERT INTO orchestration_events (
4073 event_id, aggregate_kind, stream_id, stream_version, event_type,
4074 occurred_at, actor_kind, payload_json, metadata_json, application_event_version
4075 ) VALUES (
4076 ${`event:bounded-sql-history:revision:${++revision}`}, 'thread', ${threadId}, ${revision},
4077 'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
4078 )`.pipe(Effect.andThen(read));
4079 const historyPage = (...args: Parameters<typeof projectionStore.getThreadHistoryPage>) =>
4080 committed(projectionStore.getThreadHistoryPage(...args));
4081 yield* projectionStore.apply({
4082 id: EventId.make("event:bounded-sql-history:thread"),
4083 type: "thread.created",
4084 threadId,
4085 occurredAt: now,
4086 payload: {
4087 createdBy: "user",
4088 creationSource: "web",
4089 id: threadId,
4090 projectId: ProjectId.make("project:bounded-sql-history"),
4091 title: "Bounded SQL history",
4092 providerInstanceId,
4093 modelSelection,
4094 runtimeMode: "full-access",
4095 interactionMode: "default",
4096 branch: null,
4097 worktreePath: null,
4098 activeProviderThreadId: null,
4099 lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
4100 forkedFrom: null,
4101 createdAt: now,
4102 updatedAt: now,
4103 archivedAt: null,
4104 settledOverride: null,
4105 settledAt: null,
4106 lastVisitedAt: null,
4107 deletedAt: null,
4108 },
4109 });
4110
4111 for (let ordinal = 1; ordinal <= 1_000; ordinal += 1) {
4112 const id = `turn-item:bounded-sql-history:${ordinal}`;
4113 const runId = `run:bounded-sql-history:${ordinal}`;
4114 const message = ordinal === 1 || ordinal === 1_000;
4115 yield* sql`
4116 INSERT INTO orchestration_v2_projection_runs (
4117 run_id, thread_id, ordinal, provider, provider_thread_id, status,
4118 requested_at, completed_at, payload_json
4119 ) VALUES (
4120 ${runId}, ${threadId}, ${ordinal}, 'codex', NULL, 'completed', ${nowIso}, ${nowIso},
4121 ${encodeUnknownJsonString({
4122 id: runId,
4123 threadId,
4124 ordinal,
4125 providerInstanceId,
4126 modelSelection,
4127 providerThreadId: null,
4128 userMessageId: `message:bounded-sql-history:${ordinal}`,
4129 rootNodeId: `node:bounded-sql-history:${ordinal}`,
4130 activeAttemptId: null,
4131 status: "completed",
4132 requestedAt: nowIso,
4133 startedAt: nowIso,
4134 completedAt: nowIso,
4135 checkpointId: null,
4136 contextHandoffId: null,
4137 })}
4138 )
4139 `;
4140 yield* sql`
4141 INSERT INTO orchestration_v2_projection_turn_items (
4142 turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
4143 parent_item_id, ordinal, type, status, updated_at, payload_json
4144 ) VALUES (
4145 ${id}, ${threadId}, ${runId}, NULL, NULL, NULL, NULL, ${ordinal},
4146 ${message ? "user_message" : "command_execution"}, 'completed', ${nowIso},
4147 ${encodeUnknownJsonString({
4148 id,
4149 threadId,
4150 runId,
4151 nodeId: null,
4152 providerThreadId: null,
4153 providerTurnId: null,
4154 nativeItemRef: null,
4155 parentItemId: null,
4156 ordinal,
4157 status: "completed",
4158 title: `command ${ordinal}`,
4159 input: `echo ${ordinal}`,
4160 output: "ok",
4161 exitCode: 0,
4162 startedAt: nowIso,
4163 completedAt: nowIso,
4164 updatedAt: nowIso,
4165 ...(message
4166 ? {
4167 type: "user_message",
4168 createdBy: "user",
4169 creationSource: "web",
4170 messageId: `message:bounded-sql-history:${ordinal}`,
4171 inputIntent: "turn_start",
4172 text: `Turn ${ordinal}`,
4173 attachments: [],
4174 }
4175 : { type: "command_execution" }),
4176 })}
4177 )
4178 `;
4179 }
4180
4181 for (const ordinal of [1, 1_000]) {
4182 yield* projectionStore.apply({
4183 id: EventId.make(`event:bounded-sql-history:message:${ordinal}`),
4184 type: "message.updated",
4185 threadId,
4186 occurredAt: now,
4187 payload: {
4188 createdBy: "user",
4189 creationSource: "web",
4190 id: MessageId.make(`message:bounded-sql-history:${ordinal}`),
4191 threadId,
4192 runId: RunId.make(`run:bounded-sql-history:${ordinal}`),
4193 nodeId: NodeId.make(`node:bounded-sql-history:${ordinal}`),
4194 role: "user",
4195 text: `Turn ${ordinal}`,
4196 attachments: [],
4197 streaming: false,
4198 createdAt: now,
4199 updatedAt: now,
4200 },
4201 });
4202 }
4203 const scopeId = CheckpointScopeId.make("scope:bounded-sql-history:root");
4204 const baselineId = CheckpointId.make("checkpoint:bounded-sql-history:baseline");
4205 yield* projectionStore.apply({
4206 id: EventId.make("event:bounded-sql-history:scope"),
4207 type: "checkpoint-scope.created",
4208 threadId,
4209 occurredAt: now,
4210 payload: {
4211 id: scopeId,
4212 threadId,
4213 runId: RunId.make("run:bounded-sql-history:1000"),
4214 nodeId: NodeId.make("node:bounded-sql-history:1000"),
4215 parentScopeId: null,
4216 providerThreadId: null,
4217 kind: "root_run",
4218 ordinalWithinParent: 0,
4219 advancesAppRunCount: true,
4220 cwd: "/repo/worktree",
4221 createdAt: now,
4222 },
4223 });
4224 // A reused root scope can retain its original baseline on a node outside the window.
4225 yield* projectionStore.apply({
4226 id: EventId.make("event:bounded-sql-history:baseline"),
4227 type: "checkpoint.captured",
4228 threadId,
4229 occurredAt: now,
4230 payload: {
4231 id: baselineId,
4232 threadId,
4233 scopeId,
4234 runId: null,
4235 nodeId: NodeId.make("node:bounded-sql-history:1"),
4236 parentCheckpointId: null,
4237 ordinalWithinScope: 0,
4238 appRunOrdinal: null,
4239 ref: CheckpointRef.make("refs/t3/bounded-sql-history/0"),
4240 status: "ready",
4241 files: [],
4242 capturedAt: now,
4243 },
4244 });
4245
4246 const snapshots = yield* Effect.all(
4247 Array.from({ length: 4 }, () =>
4248 projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 }),
4249 ),
4250 { concurrency: 4 },
4251 );
4252 for (const snapshot of snapshots) {
4253 assert.lengthOf(snapshot.projection.turnItems, 76);
4254 assert.lengthOf(snapshot.projection.runs, 76);
4255 assert.lengthOf(snapshot.projection.visibleTurnItems, 76);
4256 assert.strictEqual(snapshot.projection.turnItems[0]?.ordinal, 925);
4257 assert.strictEqual(snapshot.projection.turnItems.at(-1)?.ordinal, 1_000);
4258 assert.deepEqual(
4259 snapshot.projection.messages.map((message) => message.id),
4260 [MessageId.make("message:bounded-sql-history:1000")],
4261 );
4262 assert.deepEqual(
4263 snapshot.projection.checkpointScopes.map((scope) => scope.id),
4264 [scopeId],
4265 );
4266 assert.deepEqual(
4267 snapshot.projection.checkpoints.map((checkpoint) => checkpoint.id),
4268 [baselineId],
4269 );
4270 }
4271 const humanWindow = () =>
4272 projectionStore.getThreadSnapshotWindow(threadId, {
4273 rowLimit: 77,
4274 userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
4275 });
4276 assert.lengthOf(
4277 (yield* humanWindow()).projection.turnItems,
4278 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
4279 );
4280 // With the newest user turn 1,000 rows back, the window pages from inside it.
4281 const setNewestTurnType = (type: "command_execution" | "user_message") => sql`
4282 UPDATE orchestration_v2_projection_turn_items
4283 SET type = ${type}, payload_json = json_set(payload_json, '$.type', ${type})
4284 WHERE thread_id = ${threadId} AND ordinal = 1000
4285 `;
4286 yield* setNewestTurnType("command_execution");
4287 assert.lengthOf((yield* humanWindow()).projection.turnItems, 77);
4288 yield* setNewestTurnType("user_message");
4289
4290 // Fewer than 75 rows still need a byte pass after materializing wire payloads.
4291 yield* sql`
4292 UPDATE orchestration_v2_projection_turn_items
4293 SET type = 'assistant_message', payload_json = json_set(payload_json,
4294 '$.type', 'assistant_message', '$.text', ${"x".repeat(40_000)},
4295 '$.messageId', turn_item_id, '$.streaming', json('false'))
4296 WHERE thread_id = ${threadId} AND ordinal BETWEEN 2 AND 70
4297 `;
4298 const byteCursor = encodeThreadHistoryCursor({
4299 snapshotSequence: 0,
4300 sourceThreadId: threadId,
4301 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:71"),
4302 position: 70,
4303 });
4304 const bytePage = yield* historyPage(threadId, byteCursor);
4305 assert.isAbove(bytePage.items.length, 0);
4306 assert.isBelow(bytePage.items.length, 70);
4307 assert.isAtMost(
4308 Buffer.byteLength(JSON.stringify(bytePage.items), "utf8"),
4309 THREAD_HISTORY_PAGE_POLICY.maxEncodedBytes,
4310 );
4311 const byteIds = bytePage.items.map((row) => String(row.sourceItemId));
4312 let byteNext = bytePage.nextCursor;
4313 for (let pages = 0; byteNext !== null; pages += 1) {
4314 assert.isBelow(pages, 70);
4315 const page = yield* historyPage(threadId, byteNext);
4316 assert.isAbove(page.items.length, 0);
4317 byteIds.unshift(...page.items.map((row) => String(row.sourceItemId)));
4318 byteNext = page.nextCursor;
4319 }
4320 assert.deepEqual(
4321 byteIds,
4322 Array.from({ length: 70 }, (_, index) => `turn-item:bounded-sql-history:${index + 1}`),
4323 );
4324
4325 // Selected message reads recover ordinals for only the loaded messages, while 69
4326 // unloaded assistant messages remain in the thread's history.
4327 const firstMessageId = MessageId.make("message:bounded-sql-history:1");
4328 const lastMessageId = MessageId.make("message:bounded-sql-history:1000");
4329 const insertMessageItem = (
4330 suffix: string,
4331 itemThreadId: string,
4332 ordinal: number,
4333 messageId: string | null | undefined,
4334 ) => sql`
4335 INSERT INTO orchestration_v2_projection_turn_items (
4336 turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
4337 parent_item_id, ordinal, type, status, updated_at, payload_json
4338 ) VALUES (
4339 ${`turn-item:bounded-sql-history:ordinal-${suffix}`}, ${itemThreadId}, NULL, NULL, NULL,
4340 NULL, NULL, ${ordinal}, 'user_message', 'completed', ${nowIso},
4341 ${encodeUnknownJsonString({ messageId })}
4342 )
4343 `;
4344 yield* insertMessageItem("duplicate", threadId, 0, lastMessageId);
4345 yield* insertMessageItem(
4346 "other-thread",
4347 "thread:bounded-sql-history:other",
4348 -1,
4349 firstMessageId,
4350 );
4351 yield* insertMessageItem("null-id", threadId, -2, null);
4352 yield* insertMessageItem("missing-id", threadId, -3, undefined);
4353 const absentMessageId = MessageId.make("message:bounded-sql-history:absent");
4354 const messageReadTrace = traceSqlStatements();
4355 const selected = yield* projectionStore
4356 .getMessagesById(threadId, [firstMessageId, lastMessageId, firstMessageId, absentMessageId])
4357 .pipe(Effect.withTracer(messageReadTrace.tracer));
4358 assert.isFalse(messageReadTrace.statements.some((query) => query.includes("turn_items")));
4359 assert.equal(selected.size, 2);
4360 assert.equal(selected.get(firstMessageId)?.id, firstMessageId);
4361 assert.equal(selected.get(lastMessageId)?.id, lastMessageId);
4362 assert.isFalse(selected.has(absentMessageId));
4363 assert.equal((yield* projectionStore.getMessagesById(threadId, [])).size, 0);
4364 assert.equal(
4365 (yield* projectionStore.getMessagesById(ThreadId.make("other-thread"), [firstMessageId]))
4366 .size,
4367 0,
4368 );
4369 const readMessageIds = (filter?: ProjectionStore.ProjectionRecordFilter) =>
4370 projectionStore
4371 .getThreadRecords(threadId, ["messages"], filter)
4372 .pipe(Effect.map(({ messages }) => messages.map((message) => message.id)));
4373 const canonical = yield* projectionStore.getThreadRecords(threadId, ["messages"]);
4374 for (const message of canonical.messages) assert.deepEqual(selected.get(message.id), message);
4375 assert.deepEqual(yield* readMessageIds(), [lastMessageId, firstMessageId]);
4376 assert.deepEqual(yield* readMessageIds({ messageRoles: ["user", "assistant"] }), [
4377 lastMessageId,
4378 firstMessageId,
4379 ]);
4380 for (const filter of [
4381 {
4382 messageIds: [
4383 firstMessageId,
4384 lastMessageId,
4385 MessageId.make("message:bounded-sql-history:absent"),
4386 ],
4387 },
4388 { messageRoles: ["user" as const] },
4389 {
4390 messageRunIds: [
4391 RunId.make("run:bounded-sql-history:1"),
4392 RunId.make("run:bounded-sql-history:1000"),
4393 ],
4394 },
4395 ]) {
4396 assert.deepEqual(yield* readMessageIds(filter), [lastMessageId, firstMessageId]);
4397 }
4398 assert.deepEqual(yield* readMessageIds({ messageIds: [] }), []);
4399 assert.deepEqual(yield* readMessageIds({ messageIds: [firstMessageId] }), [firstMessageId]);
4400 yield* sql`
4401 DELETE FROM orchestration_v2_projection_turn_items
4402 WHERE turn_item_id LIKE 'turn-item:bounded-sql-history:ordinal-%'
4403 `;
4404
4405 yield* sql`
4406 UPDATE orchestration_v2_projection_turn_items
4407 SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
4408 WHERE thread_id = ${threadId} AND ordinal BETWEEN 2 AND 70
4409 `;
4410
4411 // A targeted page that needs more than 20 human turns must still reach its target
4412 // after the byte pass re-bounds it.
4413 yield* sql`
4414 UPDATE orchestration_v2_projection_turn_items
4415 SET type = 'user_message', payload_json = json_set(payload_json,
4416 '$.type', 'user_message', '$.createdBy', 'user', '$.creationSource', 'web',
4417 '$.inputIntent', 'turn_start', '$.messageId', turn_item_id, '$.text', 'Turn',
4418 '$.attachments', json('[]'))
4419 WHERE thread_id = ${threadId} AND ordinal BETWEEN 600 AND 624
4420 `;
4421 const targetedPage = yield* historyPage(
4422 threadId,
4423 encodeThreadHistoryCursor({
4424 snapshotSequence: 0,
4425 sourceThreadId: threadId,
4426 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:625"),
4427 position: 624,
4428 }),
4429 "turn-item:bounded-sql-history:600",
4430 );
4431 assert.strictEqual(
4432 String(targetedPage.items[0]?.sourceItemId),
4433 "turn-item:bounded-sql-history:600",
4434 );
4435 yield* sql`
4436 UPDATE orchestration_v2_projection_turn_items
4437 SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
4438 WHERE thread_id = ${threadId} AND ordinal BETWEEN 600 AND 624
4439 `;
4440
4441 // A turn split by the row budget keeps an interrupt result's request on the same page.
4442 yield* sql`
4443 UPDATE orchestration_v2_projection_turn_items
4444 SET run_id = 'run:bounded-sql-history:500',
4445 type = CASE ordinal WHEN 500 THEN 'run_interrupt_request' ELSE 'run_interrupt_result' END,
4446 payload_json = json_set(payload_json,
4447 '$.runId', 'run:bounded-sql-history:500', '$.message', 'Stop',
4448 '$.type', CASE ordinal WHEN 500 THEN 'run_interrupt_request' ELSE 'run_interrupt_result' END)
4449 WHERE thread_id = ${threadId} AND ordinal IN (500, 580)
4450 `;
4451 const interruptPage = yield* historyPage(
4452 threadId,
4453 encodeThreadHistoryCursor({
4454 snapshotSequence: 0,
4455 sourceThreadId: threadId,
4456 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:590"),
4457 position: 589,
4458 }),
4459 );
4460 const interruptIds = interruptPage.items.map((row) => String(row.sourceItemId));
4461 assert.include(interruptIds, "turn-item:bounded-sql-history:580");
4462 assert.include(interruptIds, "turn-item:bounded-sql-history:500");
4463 assert.notInclude(interruptIds, "turn-item:bounded-sql-history:501");
4464 yield* sql`
4465 UPDATE orchestration_v2_projection_turn_items
4466 SET run_id = 'run:bounded-sql-history:' || ordinal, type = 'command_execution',
4467 payload_json = json_set(payload_json,
4468 '$.runId', 'run:bounded-sql-history:' || ordinal, '$.type', 'command_execution')
4469 WHERE thread_id = ${threadId} AND ordinal IN (500, 580)
4470 `;
4471
4472 // A settled subagent of an old run is outside the window; its page carries its record.
4473 const subagentId = NodeId.make("node:bounded-sql-history:subagent");
4474 yield* sql`
4475 UPDATE orchestration_v2_projection_turn_items
4476 SET type = 'subagent', payload_json = json_set(payload_json,
4477 '$.type', 'subagent', '$.subagentId', ${subagentId}, '$.origin', 'provider_native',
4478 '$.driver', ${driver}, '$.providerInstanceId', ${providerInstanceId},
4479 '$.childThreadId', json('null'), '$.prompt', 'Look', '$.result', 'Done')
4480 WHERE thread_id = ${threadId} AND ordinal = 580
4481 `;
4482 yield* projectionStore.apply({
4483 id: EventId.make("event:bounded-sql-history:subagent"),
4484 type: "subagent.updated",
4485 threadId,
4486 nodeId: subagentId,
4487 driver,
4488 providerInstanceId,
4489 occurredAt: now,
4490 payload: {
4491 id: subagentId,
4492 threadId,
4493 runId: RunId.make("run:bounded-sql-history:580"),
4494 parentNodeId: NodeId.make("node:bounded-sql-history:580"),
4495 origin: "provider_native",
4496 createdBy: "agent",
4497 driver,
4498 providerInstanceId,
4499 providerThreadId: null,
4500 childThreadId: null,
4501 nativeTaskRef: null,
4502 prompt: "Look",
4503 title: "Old child",
4504 model: "claude-haiku-4-5-20251001",
4505 status: "completed",
4506 result: "Done",
4507 startedAt: now,
4508 completedAt: now,
4509 updatedAt: now,
4510 },
4511 });
4512 assert.deepEqual(
4513 (yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 })).projection
4514 .subagents,
4515 [],
4516 );
4517 const subagentPage = yield* historyPage(
4518 threadId,
4519 encodeThreadHistoryCursor({
4520 snapshotSequence: 0,
4521 sourceThreadId: threadId,
4522 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:590"),
4523 position: 589,
4524 }),
4525 );
4526 assert.deepEqual(
4527 subagentPage.subagents?.map((agent) => [agent.id, agent.model]),
4528 [[subagentId, "claude-haiku-4-5-20251001"]],
4529 );
4530 yield* sql`DELETE FROM orchestration_v2_projection_subagents WHERE subagent_id = ${subagentId}`;
4531 yield* sql`
4532 UPDATE orchestration_v2_projection_turn_items
4533 SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
4534 WHERE thread_id = ${threadId} AND ordinal = 580
4535 `;
4536
4537 // Older pages and timeline pages read the rows near the page, not the whole history. A
4538 // full index that another read left cached gives the same page.
4539 const pageStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
4540 const recordPageStatements = <A, E, R>(read: Effect.Effect<A, E, R>) =>
4541 committed(
4542 read.pipe(
4543 Effect.provideService(Statement.CurrentTransformer, (statement) => {
4544 pageStatements.push(statement.compile());
4545 return Effect.succeed(statement);
4546 }),
4547 ),
4548 );
4549 const olderCursor = encodeThreadHistoryCursor({
4550 snapshotSequence: 0,
4551 sourceThreadId: threadId,
4552 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:990"),
4553 position: 989,
4554 });
4555 const ordinalsFrom = (first: number, length: number) =>
4556 Array.from({ length }, (_, index) => first + index);
4557 for (const workMode of [undefined, "deferred"] as const) {
4558 const read = (throughEntryId?: string) =>
4559 projectionStore.getThreadHistoryPage(
4560 threadId,
4561 olderCursor,
4562 throughEntryId,
4563 false,
4564 workMode,
4565 );
4566 const page = yield* recordPageStatements(read());
4567 assert.deepEqual(
4568 page.items.map((row) => row.item.ordinal),
4569 ordinalsFrom(915, 75),
4570 );
4571 assert.strictEqual(decodeThreadHistoryCursor(page.nextCursor!).p, 914);
4572 // A jump to an entry reads the full index and leaves it cached. With work, it looks for
4573 // inline output only in its page's runs, not in a run after its cursor.
4574 const scannedRuns: Array<string> = [];
4575 yield* read("turn-item:bounded-sql-history:950").pipe(
4576 Effect.provideService(Statement.CurrentTransformer, (statement) => {
4577 const [query, params] = statement.compile();
4578 if (query.includes("item.inline_work_output = 1")) scannedRuns.push(String(params[0]));
4579 return Effect.succeed(statement);
4580 }),
4581 );
4582 assert.deepEqual(
4583 [
4584 scannedRuns.length,
4585 scannedRuns.some((runs) => runs.includes('"run:bounded-sql-history:950"')),
4586 scannedRuns.some((runs) => runs.includes('"run:bounded-sql-history:995"')),
4587 ],
4588 workMode === undefined ? [0, false, false] : [1, true, false],
4589 );
4590 assert.deepEqual(yield* read(), page);
4591 }
4592 for (const [view, ordinals] of [
4593 ["activity", ordinalsFrom(902, 20)],
4594 ["messages", [1000]],
4595 ] as const) {
4596 const read = projectionStore.getTimelinePage(threadId, {
4597 afterPosition: 900,
4598 limit: 20,
4599 view,
4600 });
4601 const page = yield* recordPageStatements(read);
4602 assert.deepEqual(
4603 page.items.map((row) => [row.item.ordinal, row.position]),
4604 ordinals.map((ordinal) => [ordinal, ordinal - 1]),
4605 );
4606 assert.strictEqual(page.totalItems, 1000);
4607 // An attachment lookup reads the full index and leaves it cached.
4608 yield* projectionStore.getTimelinePage(threadId, { attachmentId: "absent", limit: 1 });
4609 assert.deepEqual(yield* read, page);
4610 }
4611 {
4612 const pagePath = "file:/history-page-steps?vfs=memdb";
4613 using copy = new NodeSqlite.DatabaseSync(pagePath);
4614 yield* sql`VACUUM INTO ${pagePath}`;
4615 // Only statements that count positions may grow with the history, and those read an
4616 // index alone. Every other statement costs the same when the rows below the page are gone.
4617 const countsPositions = ([query]: readonly [string, ReadonlyArray<unknown>]) =>
4618 /COUNT\(\*\)|OFFSET/.test(query);
4619 for (const [query, params] of pageStatements.filter(countsPositions)) {
4620 const plan = copy
4621 .prepare(`EXPLAIN QUERY PLAN ${query}`)
4622 .all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>))
4623 .map((row) => String(row.detail))
4624 .filter((detail) => detail.includes("orchestration_v2_projection_turn_items"));
4625 assert.isTrue(
4626 plan.every((detail) => detail.includes("COVERING INDEX")),
4627 `${query}\n${plan.join("\n")}`,
4628 );
4629 }
4630 const rowReads = pageStatements.filter((statement) => !countsPositions(statement));
4631 const rowReadSteps = () =>
4632 rowReads.reduce((steps, [query, params]) => {
4633 const statement = copy.prepare(query);
4634 statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
4635 return steps + statement.stat("vmStep");
4636 }, 0);
4637 const fullSteps = rowReadSteps();
4638 assert.isAbove(fullSteps, 0);
4639 copy.exec(
4640 "DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 400",
4641 );
4642 assert.isAtMost(fullSteps, rowReadSteps() * 1.1);
4643 }
4644 // Rows that a rolled-back run hides below the page still shift every position above them.
4645 const setOldRunStatus = (status: "rolled_back" | "completed") => sql`
4646 UPDATE orchestration_v2_projection_runs
4647 SET status = ${status}, payload_json = json_set(payload_json, '$.status', ${status})
4648 WHERE thread_id = ${threadId} AND ordinal BETWEEN 100 AND 109`;
4649 yield* setOldRunStatus("rolled_back");
4650 const shiftedPage = yield* historyPage(threadId, olderCursor);
4651 assert.strictEqual(decodeThreadHistoryCursor(shiftedPage.nextCursor!).p, 904);
4652 const shiftedTimeline = yield* committed(
4653 projectionStore.getTimelinePage(threadId, {
4654 afterPosition: 900,
4655 limit: 1,
4656 view: "activity",
4657 }),
4658 );
4659 assert.deepEqual(
4660 [shiftedTimeline.totalItems, shiftedTimeline.items[0]?.item.ordinal],
4661 [990, 912],
4662 );
4663 yield* setOldRunStatus("completed");
4664
4665 const retainedRequestPlan = yield* sql<{ readonly detail: string }>`
4666 EXPLAIN QUERY PLAN
4667 WITH selected AS (
4668 SELECT run_id, type
4669 FROM orchestration_v2_projection_turn_items
4670 WHERE thread_id = ${threadId}
4671 ORDER BY ordinal DESC, turn_item_id DESC
4672 LIMIT 77
4673 )
4674 SELECT request.payload_json
4675 FROM orchestration_v2_projection_turn_items AS request
4676 WHERE request.run_id IN (
4677 SELECT run_id FROM selected
4678 WHERE type = 'run_interrupt_result' AND run_id IS NOT NULL
4679 )
4680 AND request.type = 'run_interrupt_request'
4681 `;
4682 assert.isTrue(
4683 retainedRequestPlan.some((row) =>
4684 row.detail.includes("orchestration_v2_projection_turn_items_run_ordinal_idx"),
4685 ),
4686 );
4687
4688 const older = yield* projectionStore.getThreadSnapshotWindow(threadId, {
4689 rowLimit: 76,
4690 anchorItemId: TurnItemId.make("turn-item:bounded-sql-history:925"),
4691 });
4692 assert.lengthOf(older.projection.turnItems, 76);
4693 assert.lengthOf(older.projection.runs, 76);
4694 assert.strictEqual(older.projection.turnItems[0]?.ordinal, 850);
4695 assert.strictEqual(older.projection.turnItems.at(-1)?.ordinal, 925);
4696
4697 const itemId = TurnItemId.make("turn-item:bounded-sql-history:925");
4698 const stored = yield* projectionStore.getTurnItem({ threadId, itemId });
4699 assert.strictEqual(stored?.type === "command_execution" ? stored.output : undefined, "ok");
4700 assert.isNull(
4701 yield* projectionStore.getTurnItem({ threadId: ThreadId.make("thread:other"), itemId }),
4702 );
4703
4704 const sqlPageLimit = THREAD_HISTORY_PAGE_POLICY.maxItems + 2;
4705 const initialSnapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
4706 rowLimit: sqlPageLimit,
4707 userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
4708 });
4709 const initialPage = buildBoundedThreadProjection({
4710 projection: initialSnapshot.projection,
4711 snapshotSequence: initialSnapshot.snapshotSequence,
4712 });
4713 const loadedIds = initialPage.projection.visibleTurnItems.map((row) =>
4714 String(row.sourceItemId),
4715 );
4716 let cursor = initialPage.historyCursor;
4717 let pageCount = 1;
4718 while (cursor !== null) {
4719 const anchorItemId = TurnItemId.make(decodeThreadHistoryCursor(cursor).si);
4720 const snapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
4721 rowLimit: sqlPageLimit,
4722 userTurnLimit: 20,
4723 anchorItemId,
4724 });
4725 assert.isAtMost(
4726 snapshot.projection.visibleTurnItems.length,
4727 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
4728 );
4729 const page = selectHistoryPageFromCursor({
4730 items: snapshot.projection.visibleTurnItems,
4731 cursor,
4732 snapshotSequence: snapshot.snapshotSequence,
4733 });
4734 loadedIds.push(...page.items.map((row) => String(row.sourceItemId)));
4735 pageCount += 1;
4736 cursor = page.nextCursor;
4737 }
4738
4739 assert.isAtLeast(pageCount, 3);
4740 assert.lengthOf(loadedIds, 1_000);
4741 assert.strictEqual(new Set(loadedIds).size, 1_000);
4742 assert.deepEqual(
4743 loadedIds.toSorted(
4744 (left, right) => Number(left.split(":").at(-1)) - Number(right.split(":").at(-1)),
4745 ),
4746 Array.from({ length: 1_000 }, (_, index) => `turn-item:bounded-sql-history:${index + 1}`),
4747 );
4748
4749 yield* setNewestTurnType("command_execution");
4750 yield* sql`
4751 UPDATE orchestration_v2_projection_turn_items
4752 SET run_id = 'run:bounded-sql-history:1',
4753 payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:1')
4754 WHERE thread_id = ${threadId}
4755 `;
4756 yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 1000,
4757 payload_json = json_set(payload_json, '$.ordinal', 1000)
4758 WHERE thread_id = ${threadId} AND ordinal = 999`;
4759 const promptId = TurnItemId.make("turn-item:bounded-sql-history:1");
4760 const { statements: deferredStatements, tracer: deferredTracer } = traceSqlStatements();
4761 const deferred = yield* projectionStore
4762 .getThreadSnapshotWindow(threadId, {
4763 rowLimit: sqlPageLimit,
4764 userTurnLimit: 10,
4765 deferWork: true,
4766 })
4767 .pipe(Effect.withTracer(deferredTracer));
4768 const deferredQuery = deferredStatements.find((statement) =>
4769 statement.includes("blocked_runs AS MATERIALIZED"),
4770 );
4771 assert.isDefined(deferredQuery);
4772 const deferredPlan = yield* sql.unsafe<{
4773 readonly id: number;
4774 readonly parent: number;
4775 readonly detail: string;
4776 }>(`EXPLAIN QUERY PLAN ${deferredQuery}`);
4777 const blockedRunsPlan = deferredPlan.find((row) => row.detail === "MATERIALIZE blocked_runs");
4778 assert.isDefined(blockedRunsPlan);
4779 const blockedLookup = deferredPlan.find(
4780 (row) => row.parent === blockedRunsPlan!.id && row.detail.startsWith("SEARCH "),
4781 );
4782 assert.include(blockedLookup?.detail ?? "", "run_id=?");
4783 assert.lengthOf(deferred.projection.visibleTurnItems, 3);
4784 assert.strictEqual(deferred.projection.visibleTurnItems[0]?.sourceItemId, promptId);
4785 const anchor = deferred.projection.visibleTurnItems[1]!;
4786 assert.strictEqual(anchor.item.deferredWork?.itemCount, 999);
4787 const workIds = new Set<TurnItemId>();
4788 let workCursor: string | null = anchor.item.deferredWork!.cursor;
4789 for (let pages = 0; workCursor !== null; pages += 1) {
4790 assert.isBelow(pages, 20);
4791 const page: OrchestrationV2ThreadHistoryPage = yield* historyPage(
4792 threadId,
4793 workCursor,
4794 anchor.sourceItemId,
4795 false,
4796 "work",
4797 );
4798 assert.isAtMost(page.items.length, 75);
4799 for (const row of page.items) workIds.add(row.sourceItemId);
4800 workCursor = page.nextCursor;
4801 }
4802 assert.strictEqual(workIds.size, 999);
4803 const secondItemId = TurnItemId.make("turn-item:bounded-sql-history:2");
4804 const secondItem = {
4805 id: secondItemId,
4806 threadId,
4807 runId: (yield* projectionStore.getTurnItem({ threadId, itemId: secondItemId }))!.runId,
4808 nodeId: null,
4809 providerThreadId: null,
4810 providerTurnId: null,
4811 nativeItemRef: null,
4812 parentItemId: null,
4813 ordinal: 2,
4814 status: "completed" as const,
4815 title: "command 2",
4816 startedAt: now,
4817 completedAt: now,
4818 updatedAt: now,
4819 };
4820 const writeSecondItem = (item: OrchestrationV2TurnItem) =>
4821 projectionStore.apply({
4822 id: EventId.make(`event:bounded-sql-history:item-2:${item.type}`),
4823 type: "turn-item.updated",
4824 threadId,
4825 runId: secondItem.runId ?? undefined,
4826 driver,
4827 occurredAt: now,
4828 payload: item,
4829 });
4830 for (const inline of [
4831 { toolName: "t3-code.delegate_task" },
4832 { toolName: "mcp__t3_code__delegate_task" },
4833 { toolName: "mcp__docs__search", output: { next: "Delegate-Task" } },
4834 { toolName: "view_image", viewedImagePath: "/repo/screenshot.png" },
4835 { toolName: "mcp__docs__chart", output: { [MCP_APP_OUTPUT_KEY]: { uri: "ui://chart" } } },
4836 { toolName: "mcp__t3-code__send_to_user" },
4837 // OpenCode Code Mode reports a nested send_to_user call as one `execute` item.
4838 { toolName: "execute", output: JSON.stringify({ sentMessage: "Draft" }) },
4839 ]) {
4840 yield* writeSecondItem({
4841 ...secondItem,
4842 type: "dynamic_tool",
4843 input: {},
4844 ...inline,
4845 });
4846 const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
4847 rowLimit: sqlPageLimit,
4848 userTurnLimit: 10,
4849 deferWork: true,
4850 });
4851 assert.isUndefined(
4852 window.projection.visibleTurnItems.find((row) => row.item.ordinal === 2)?.item
4853 .deferredWork,
4854 );
4855 assert.strictEqual(
4856 window.projection.visibleTurnItems.find((row) => row.item.deferredWork)?.item.ordinal,
4857 3,
4858 );
4859 const newestCursor = encodeThreadHistoryCursor({
4860 snapshotSequence: 0,
4861 sourceThreadId: threadId,
4862 sourceItemId: "turn-item:bounded-sql-history:1000",
4863 position: 1000,
4864 });
4865 const page = yield* historyPage(threadId, newestCursor, undefined, false, "deferred");
4866 assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
4867 // A page that reads the full index, as a jump to an entry does, looks for inline output
4868 // only in its own runs.
4869 const pageRead = traceSqlStatements();
4870 yield* historyPage(
4871 threadId,
4872 newestCursor,
4873 "turn-item:bounded-sql-history:2",
4874 false,
4875 "deferred",
4876 ).pipe(Effect.withTracer(pageRead.tracer));
4877 const payloadScans = pageRead.statements.filter((statement) =>
4878 statement.includes("item.inline_work_output = 1"),
4879 );
4880 assert.lengthOf(payloadScans, 1);
4881 assert.include(payloadScans[0], "item.run_id IS json_extract(wanted.value, '$.id')");
4882 }
4883 // Until its run is scanned, a cursor on inline output is hidden inside a work group.
4884 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4885 payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'),
4886 inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 4`;
4887 const sharedRunId = "run:bounded-sql-history:1";
4888 // A work page of rows that have no run leaves their inline output out too.
4889 yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL
4890 WHERE thread_id = ${threadId} AND ordinal BETWEEN 3 AND 6`;
4891 const runlessWork = yield* historyPage(
4892 threadId,
4893 encodeThreadHistoryCursor({
4894 snapshotSequence: 0,
4895 sourceThreadId: threadId,
4896 sourceItemId: "turn-item:bounded-sql-history:6",
4897 position: 6,
4898 inclusive: true,
4899 }),
4900 "turn-item:bounded-sql-history:3",
4901 false,
4902 "work",
4903 );
4904 assert.deepEqual(
4905 runlessWork.items.map((row) => row.item.ordinal),
4906 [3, 5, 6],
4907 );
4908 yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId}
4909 WHERE thread_id = ${threadId} AND ordinal BETWEEN 3 AND 6`;
4910 // A stale position selects from the cursor row itself, or from more than a page after it
4911 // once the later rows are another run's and cannot fold.
4912 for (const [position, laterStatus, laterRun] of [
4913 [0, "completed", sharedRunId],
4914 [999, "running", "run:bounded-sql-history:300"],
4915 ] as const) {
4916 yield* sql`UPDATE orchestration_v2_projection_turn_items SET status = ${laterStatus},
4917 run_id = ${laterRun},
4918 payload_json = json_set(payload_json, '$.status', ${laterStatus}, '$.runId', ${laterRun})
4919 WHERE thread_id = ${threadId} AND ordinal >= 300`;
4920 const beforeInlineOutput = yield* historyPage(
4921 threadId,
4922 encodeThreadHistoryCursor({
4923 snapshotSequence: 0,
4924 sourceThreadId: threadId,
4925 sourceItemId: "turn-item:bounded-sql-history:4",
4926 position,
4927 }),
4928 undefined,
4929 false,
4930 "deferred",
4931 );
4932 assert.deepEqual(
4933 beforeInlineOutput.items.map((row) => row.item.ordinal),
4934 [1, 2, 3],
4935 );
4936 }
4937 // A cursor whose row is gone falls back to its position. Inline output between the page
4938 // and that position can belong to a run with no other row there.
4939 yield* sql`UPDATE orchestration_v2_projection_turn_items
4940 SET status = CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
4941 run_id = CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END,
4942 payload_json = json_set(payload_json,
4943 '$.status', CASE WHEN ordinal <= 700 THEN 'running' ELSE 'completed' END,
4944 '$.runId', CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END)
4945 WHERE thread_id = ${threadId} AND ordinal >= 100`;
4946 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4947 payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'),
4948 inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 720`;
4949 const beforeMissingCursor = yield* historyPage(
4950 threadId,
4951 encodeThreadHistoryCursor({
4952 snapshotSequence: 0,
4953 sourceThreadId: threadId,
4954 sourceItemId: "turn-item:bounded-sql-history:gone",
4955 position: 800,
4956 }),
4957 undefined,
4958 false,
4959 "deferred",
4960 );
4961 assert.strictEqual(beforeMissingCursor.items.at(-1)?.item.ordinal, 720);
4962 yield* sql`UPDATE orchestration_v2_projection_turn_items SET status = 'completed',
4963 run_id = ${sharedRunId},
4964 payload_json = json_set(payload_json, '$.status', 'completed', '$.runId', ${sharedRunId})
4965 WHERE thread_id = ${threadId} AND ordinal >= 100`;
4966 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
4967 payload_json = json_set(payload_json, '$.type', 'command_execution'), inline_work_output = 0
4968 WHERE thread_id = ${threadId} AND ordinal IN (4, 720)`;
4969 yield* writeSecondItem({
4970 ...secondItem,
4971 type: "command_execution",
4972 input: "echo 2",
4973 output: "ok",
4974 exitCode: 0,
4975 });
4976 for (const [ordinal, status] of [
4977 [0, "completed"],
4978 [1, "superseded"],
4979 [2, "running"],
4980 ] as const) {
4981 const attempt = {
4982 id: `attempt:bounded-sql-history:${ordinal}`,
4983 runId: sharedRunId,
4984 attemptOrdinal: ordinal + 1,
4985 rootNodeId: `node:bounded-sql-history:attempt-${ordinal === 0 ? 1 : ordinal}`,
4986 providerInstanceId,
4987 providerThreadId: "provider-thread:bounded-sql-history",
4988 providerTurnId: null,
4989 reason: ordinal === 1 ? "initial" : "steering_restart",
4990 status,
4991 startedAt: nowIso,
4992 completedAt: status === "superseded" ? nowIso : null,
4993 };
4994 yield* sql`INSERT INTO orchestration_v2_projection_run_attempts
4995 (attempt_id, thread_id, run_id, attempt_ordinal, root_node_id, provider, provider_instance_id, provider_thread_id, provider_turn_id, status, payload_json)
4996 VALUES (${attempt.id}, ${threadId}, ${sharedRunId}, ${attempt.attemptOrdinal}, ${attempt.rootNodeId}, ${providerInstanceId}, ${providerInstanceId}, ${attempt.providerThreadId}, NULL, ${status}, ${encodeUnknownJsonString(attempt)})`;
4997 }
4998 yield* sql`UPDATE orchestration_v2_projection_turn_items
4999 SET node_id = CASE WHEN ordinal < 990 AND ordinal <> 10 THEN 'node:bounded-sql-history:attempt-1' ELSE 'node:bounded-sql-history:attempt-2' END,
5000 payload_json = json_set(payload_json, '$.nodeId', CASE WHEN ordinal < 990 AND ordinal <> 10 THEN 'node:bounded-sql-history:attempt-1' ELSE 'node:bounded-sql-history:attempt-2' END)
Showing first 5,000 of 12,065 lines. View raw