Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionStore.test.ts

869 lines27.0 KB

1import { assert, it } from "@effect/vitest";
2import {
3 EventId,
4 MessageId,
5 type ModelSelection,
6 NodeId,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ProviderSessionId,
11 ProviderThreadId,
12 ProviderTurnId,
13 RunAttemptId,
14 RunId,
15 ThreadId,
16 TurnItemId,
17} from "@t3tools/contracts";
18import * as DateTime from "effect/DateTime";
19import * as Effect from "effect/Effect";
20import * as Layer from "effect/Layer";
21import * as Schema from "effect/Schema";
22import * as SqlClient from "effect/unstable/sql/SqlClient";
23​
24import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts";
25import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
26import { ProjectionStoreV2, layer as projectionStoreLayer } from "./ProjectionStore.ts";
27​
28const TestLayer = Layer.mergeAll(
29 projectionStoreLayer.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
30 SqlitePersistenceMemory,
31);
32const modelSelection = {
33 instanceId: ProviderInstanceId.make("codex"),
34 model: "gpt-5.4",
35} satisfies ModelSelection;
36const driver = ProviderDriverKind.make("codex");
37const providerInstanceId = modelSelection.instanceId;
38const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
39​
40it.layer(TestLayer)("ProjectionStoreV2", (it) => {
41 it.effect("projects one shared provider session into multiple thread bindings", () =>
42 Effect.gen(function* () {
43 const projectionStore = yield* ProjectionStoreV2;
44 const now = yield* DateTime.now;
45 const projectId = ProjectId.make("project:projection-shared-provider-session");
46 const firstThreadId = ThreadId.make("thread:projection-shared-provider-session:first");
47 const secondThreadId = ThreadId.make("thread:projection-shared-provider-session:second");
48 const providerSessionId = ProviderSessionId.make(
49 "provider-session:projection-shared-provider-session",
50 );
51 const makeThread = (threadId: ThreadId) => ({
52 createdBy: "user" as const,
53 creationSource: "web" as const,
54 id: threadId,
55 projectId,
56 title: "Shared provider session",
57 providerInstanceId,
58 modelSelection,
59 runtimeMode: "full-access" as const,
60 interactionMode: "default" as const,
61 branch: null,
62 worktreePath: null,
63 activeProviderThreadId: null,
64 lineage: {
65 parentThreadId: null,
66 relationshipToParent: null,
67 rootThreadId: threadId,
68 },
69 forkedFrom: null,
70 createdAt: now,
71 updatedAt: now,
72 archivedAt: null,
73 deletedAt: null,
74 });
75 const session = {
76 id: providerSessionId,
77 driver,
78 providerInstanceId,
79 status: "ready" as const,
80 cwd: "/workspace",
81 model: modelSelection.model,
82 capabilities: CodexProviderCapabilitiesV2,
83 createdAt: now,
84 updatedAt: now,
85 lastError: null,
86 };
87​
88 yield* projectionStore.apply({
89 id: EventId.make("event:projection-shared-provider-session:first-thread"),
90 type: "thread.created",
91 threadId: firstThreadId,
92 occurredAt: now,
93 payload: makeThread(firstThreadId),
94 });
95 yield* projectionStore.apply({
96 id: EventId.make("event:projection-shared-provider-session:second-thread"),
97 type: "thread.created",
98 threadId: secondThreadId,
99 occurredAt: now,
100 payload: makeThread(secondThreadId),
101 });
102 for (const [threadId, suffix] of [
103 [firstThreadId, "first"],
104 [secondThreadId, "second"],
105 ] as const) {
106 yield* projectionStore.apply({
107 id: EventId.make(`event:projection-shared-provider-session:${suffix}-binding`),
108 type: "provider-session.attached",
109 threadId,
110 driver,
111 providerInstanceId,
112 occurredAt: now,
113 payload: session,
114 });
115 }
116​
117 assert.deepEqual(
118 (yield* projectionStore.getThreadProjection(firstThreadId)).providerSessions.map(
119 (value) => value.id,
120 ),
121 [providerSessionId],
122 );
123 assert.deepEqual(
124 (yield* projectionStore.getThreadProjection(secondThreadId)).providerSessions.map(
125 (value) => value.id,
126 ),
127 [providerSessionId],
128 );
129​
130 yield* projectionStore.apply({
131 id: EventId.make("event:projection-shared-provider-session:first-detached"),
132 type: "provider-session.detached",
133 threadId: firstThreadId,
134 driver,
135 providerInstanceId,
136 occurredAt: now,
137 payload: { providerSessionId, detachedAt: now },
138 });
139​
140 assert.lengthOf(
141 (yield* projectionStore.getThreadProjection(firstThreadId)).providerSessions,
142 0,
143 );
144 assert.lengthOf(
145 (yield* projectionStore.getThreadProjection(secondThreadId)).providerSessions,
146 1,
147 );
148 }),
149 );
150​
151 it.effect("builds shell snapshots without decoding full turn item payloads", () =>
152 Effect.gen(function* () {
153 const projectionStore = yield* ProjectionStoreV2;
154 const sql = yield* SqlClient.SqlClient;
155 const now = yield* DateTime.now;
156 const nowIso = DateTime.formatIso(now);
157 const threadId = ThreadId.make("thread:projection-shell-stale-item");
158 const projectId = ProjectId.make("project:projection-shell");
159​
160 yield* projectionStore.apply({
161 id: EventId.make("event:projection-shell-thread-created"),
162 type: "thread.created",
163 threadId,
164 occurredAt: now,
165 payload: {
166 createdBy: "user",
167 creationSource: "web",
168 id: threadId,
169 projectId,
170 title: "Projection shell",
171 providerInstanceId,
172 modelSelection: modelSelection,
173 runtimeMode: "full-access",
174 interactionMode: "default",
175 branch: null,
176 worktreePath: null,
177 activeProviderThreadId: null,
178 lineage: {
179 parentThreadId: null,
180 relationshipToParent: null,
181 rootThreadId: threadId,
182 },
183 forkedFrom: null,
184 createdAt: now,
185 updatedAt: now,
186 archivedAt: null,
187 deletedAt: null,
188 },
189 });
190​
191 yield* sql`
192 INSERT INTO orchestration_v2_projection_turn_items (
193 turn_item_id,
194 thread_id,
195 run_id,
196 node_id,
197 provider_thread_id,
198 provider_turn_id,
199 parent_item_id,
200 ordinal,
201 type,
202 status,
203 updated_at,
204 payload_json
205 )
206 VALUES (
207 ${"turn-item:stale-user-message"},
208 ${threadId},
209 ${null},
210 ${null},
211 ${null},
212 ${null},
213 ${null},
214 ${0},
215 ${"user_message"},
216 ${"completed"},
217 ${nowIso},
218 ${encodeUnknownJsonString({
219 id: "turn-item:stale-user-message",
220 threadId,
221 runId: null,
222 nodeId: null,
223 providerThreadId: null,
224 providerTurnId: null,
225 nativeItemRef: null,
226 parentItemId: null,
227 ordinal: 0,
228 status: "completed",
229 title: null,
230 startedAt: nowIso,
231 completedAt: nowIso,
232 updatedAt: nowIso,
233 type: "user_message",
234 messageId: "message:stale-user-message",
235 text: "stale user message",
236 attachments: [],
237 })}
238 )
239 `;
240​
241 const shell = yield* projectionStore.getShellSnapshot();
242 const fullProjectionExit = yield* Effect.exit(projectionStore.getThreadProjection(threadId));
243​
244 assert.deepEqual(
245 shell.threads
246 .filter((thread) => thread.id === threadId)
247 .map((thread) => ({
248 id: thread.id,
249 itemCount: thread.itemCount,
250 visibleItemCount: thread.visibleItemCount,
251 status: thread.status,
252 })),
253 [
254 {
255 id: threadId,
256 itemCount: 1,
257 visibleItemCount: 1,
258 status: "idle",
259 },
260 ],
261 );
262 assert.equal(fullProjectionExit._tag, "Failure");
263 }),
264 );
265​
266 it.effect("removes rolled back runs from the active visible projection", () =>
267 Effect.gen(function* () {
268 const projectionStore = yield* ProjectionStoreV2;
269 const now = yield* DateTime.now;
270 const threadId = ThreadId.make("thread:projection-rollback-prune");
271 const projectId = ProjectId.make("project:projection-rollback-prune");
272 const runId = RunId.make("run:projection-rollback-prune");
273 const attemptId = RunAttemptId.make("attempt:projection-rollback-prune");
274 const rootNodeId = NodeId.make("node:projection-rollback-prune:root");
275 const assistantNodeId = NodeId.make("node:projection-rollback-prune:assistant");
276 const providerThreadId = ProviderThreadId.make("provider-thread:projection-rollback-prune");
277 const providerTurnId = ProviderTurnId.make("provider-turn:projection-rollback-prune");
278 const userMessageId = MessageId.make("message:projection-rollback-prune:user");
279 const assistantMessageId = MessageId.make("message:projection-rollback-prune:assistant");
280 const userTurnItemId = TurnItemId.make("turn-item:projection-rollback-prune:user");
281 const assistantTurnItemId = TurnItemId.make("turn-item:projection-rollback-prune:assistant");
282​
283 yield* projectionStore.apply({
284 id: EventId.make("event:projection-rollback-prune:thread-created"),
285 type: "thread.created",
286 threadId,
287 occurredAt: now,
288 payload: {
289 createdBy: "user",
290 creationSource: "web",
291 id: threadId,
292 projectId,
293 title: "Projection rollback prune",
294 providerInstanceId,
295 modelSelection,
296 runtimeMode: "full-access",
297 interactionMode: "default",
298 branch: null,
299 worktreePath: null,
300 activeProviderThreadId: providerThreadId,
301 lineage: {
302 parentThreadId: null,
303 relationshipToParent: null,
304 rootThreadId: threadId,
305 },
306 forkedFrom: null,
307 createdAt: now,
308 updatedAt: now,
309 archivedAt: null,
310 deletedAt: null,
311 },
312 });
313 yield* projectionStore.apply({
314 id: EventId.make("event:projection-rollback-prune:provider-thread"),
315 type: "provider-thread.updated",
316 threadId,
317 driver,
318 occurredAt: now,
319 payload: {
320 id: providerThreadId,
321 driver,
322 providerInstanceId,
323 providerSessionId: null,
324 appThreadId: threadId,
325 ownerNodeId: null,
326 nativeThreadRef: null,
327 nativeConversationHeadRef: null,
328 status: "active",
329 firstRunOrdinal: 1,
330 lastRunOrdinal: 1,
331 handoffIds: [],
332 forkedFrom: null,
333 createdAt: now,
334 updatedAt: now,
335 },
336 });
337 yield* projectionStore.apply({
338 id: EventId.make("event:projection-rollback-prune:run-created"),
339 type: "run.created",
340 threadId,
341 runId,
342 nodeId: rootNodeId,
343 driver,
344 occurredAt: now,
345 payload: {
346 id: runId,
347 threadId,
348 ordinal: 1,
349 providerInstanceId,
350 modelSelection,
351 providerThreadId,
352 userMessageId,
353 rootNodeId,
354 activeAttemptId: attemptId,
355 status: "completed",
356 requestedAt: now,
357 startedAt: now,
358 completedAt: now,
359 checkpointId: null,
360 contextHandoffId: null,
361 },
362 });
363 yield* projectionStore.apply({
364 id: EventId.make("event:projection-rollback-prune:attempt-created"),
365 type: "run-attempt.created",
366 threadId,
367 runId,
368 nodeId: rootNodeId,
369 driver,
370 occurredAt: now,
371 payload: {
372 id: attemptId,
373 runId,
374 attemptOrdinal: 1,
375 rootNodeId,
376 providerInstanceId,
377 providerThreadId,
378 providerTurnId,
379 reason: "initial",
380 status: "completed",
381 startedAt: now,
382 completedAt: now,
383 },
384 });
385 yield* projectionStore.apply({
386 id: EventId.make("event:projection-rollback-prune:root-node"),
387 type: "node.updated",
388 threadId,
389 runId,
390 nodeId: rootNodeId,
391 driver,
392 occurredAt: now,
393 payload: {
394 id: rootNodeId,
395 threadId,
396 runId,
397 parentNodeId: null,
398 rootNodeId,
399 kind: "root_turn",
400 status: "completed",
401 countsForRun: true,
402 providerThreadId,
403 providerTurnId: null,
404 nativeItemRef: null,
405 runtimeRequestId: null,
406 checkpointScopeId: null,
407 startedAt: now,
408 completedAt: now,
409 },
410 });
411 yield* projectionStore.apply({
412 id: EventId.make("event:projection-rollback-prune:assistant-node"),
413 type: "node.updated",
414 threadId,
415 runId,
416 nodeId: assistantNodeId,
417 driver,
418 occurredAt: now,
419 payload: {
420 id: assistantNodeId,
421 threadId,
422 runId,
423 parentNodeId: rootNodeId,
424 rootNodeId,
425 kind: "assistant_message",
426 status: "completed",
427 countsForRun: false,
428 providerThreadId,
429 providerTurnId,
430 nativeItemRef: null,
431 runtimeRequestId: null,
432 checkpointScopeId: null,
433 startedAt: now,
434 completedAt: now,
435 },
436 });
437 yield* projectionStore.apply({
438 id: EventId.make("event:projection-rollback-prune:provider-turn"),
439 type: "provider-turn.updated",
440 threadId,
441 runId,
442 nodeId: rootNodeId,
443 driver,
444 occurredAt: now,
445 payload: {
446 id: providerTurnId,
447 providerThreadId,
448 nodeId: rootNodeId,
449 runAttemptId: attemptId,
450 nativeTurnRef: null,
451 ordinal: 1,
452 status: "completed",
453 startedAt: now,
454 completedAt: now,
455 },
456 });
457 yield* projectionStore.apply({
458 id: EventId.make("event:projection-rollback-prune:user-message"),
459 type: "message.updated",
460 threadId,
461 runId,
462 nodeId: rootNodeId,
463 driver,
464 occurredAt: now,
465 payload: {
466 createdBy: "user",
467 creationSource: "web",
468 id: userMessageId,
469 threadId,
470 runId,
471 nodeId: rootNodeId,
472 role: "user",
473 text: "rolled back user",
474 attachments: [],
475 streaming: false,
476 createdAt: now,
477 updatedAt: now,
478 },
479 });
480 yield* projectionStore.apply({
481 id: EventId.make("event:projection-rollback-prune:assistant-message"),
482 type: "message.updated",
483 threadId,
484 runId,
485 nodeId: assistantNodeId,
486 driver,
487 occurredAt: now,
488 payload: {
489 createdBy: "agent",
490 creationSource: "provider",
491 id: assistantMessageId,
492 threadId,
493 runId,
494 nodeId: assistantNodeId,
495 role: "assistant",
496 text: "rolled back assistant",
497 attachments: [],
498 streaming: false,
499 createdAt: now,
500 updatedAt: now,
501 },
502 });
503 yield* projectionStore.apply({
504 id: EventId.make("event:projection-rollback-prune:user-item"),
505 type: "turn-item.updated",
506 threadId,
507 runId,
508 nodeId: rootNodeId,
509 driver,
510 occurredAt: now,
511 payload: {
512 createdBy: "user",
513 creationSource: "web",
514 id: userTurnItemId,
515 threadId,
516 runId,
517 nodeId: rootNodeId,
518 providerThreadId,
519 providerTurnId: null,
520 nativeItemRef: null,
521 parentItemId: null,
522 ordinal: 100,
523 status: "completed",
524 title: null,
525 startedAt: now,
526 completedAt: now,
527 updatedAt: now,
528 type: "user_message",
529 messageId: userMessageId,
530 inputIntent: "turn_start",
531 text: "rolled back user",
532 attachments: [],
533 },
534 });
535 yield* projectionStore.apply({
536 id: EventId.make("event:projection-rollback-prune:assistant-item"),
537 type: "turn-item.updated",
538 threadId,
539 runId,
540 nodeId: assistantNodeId,
541 driver,
542 occurredAt: now,
543 payload: {
544 id: assistantTurnItemId,
545 threadId,
546 runId,
547 nodeId: assistantNodeId,
548 providerThreadId,
549 providerTurnId,
550 nativeItemRef: null,
551 parentItemId: null,
552 ordinal: 101,
553 status: "completed",
554 title: null,
555 startedAt: now,
556 completedAt: now,
557 updatedAt: now,
558 type: "assistant_message",
559 messageId: assistantMessageId,
560 text: "rolled back assistant",
561 streaming: false,
562 },
563 });
564 yield* projectionStore.apply({
565 id: EventId.make("event:projection-rollback-prune:run-rolled-back"),
566 type: "run.updated",
567 threadId,
568 runId,
569 nodeId: rootNodeId,
570 driver,
571 occurredAt: now,
572 payload: {
573 id: runId,
574 threadId,
575 ordinal: 1,
576 providerInstanceId,
577 modelSelection,
578 providerThreadId,
579 userMessageId,
580 rootNodeId,
581 activeAttemptId: attemptId,
582 status: "rolled_back",
583 requestedAt: now,
584 startedAt: now,
585 completedAt: now,
586 checkpointId: null,
587 contextHandoffId: null,
588 },
589 });
590 yield* projectionStore.apply({
591 id: EventId.make("event:projection-rollback-prune:root-rolled-back"),
592 type: "node.updated",
593 threadId,
594 runId,
595 nodeId: rootNodeId,
596 driver,
597 occurredAt: now,
598 payload: {
599 id: rootNodeId,
600 threadId,
601 runId,
602 parentNodeId: null,
603 rootNodeId,
604 kind: "root_turn",
605 status: "rolled_back",
606 countsForRun: true,
607 providerThreadId,
608 providerTurnId: null,
609 nativeItemRef: null,
610 runtimeRequestId: null,
611 checkpointScopeId: null,
612 startedAt: now,
613 completedAt: now,
614 },
615 });
616​
617 const projection = yield* projectionStore.getThreadProjection(threadId);
618​
619 assert.deepEqual(
620 projection.runs.map((run) => run.status),
621 ["rolled_back"],
622 );
623 assert.deepEqual(
624 projection.nodes.map((node) => [node.id, node.status]),
625 [
626 [assistantNodeId, "completed"],
627 [rootNodeId, "rolled_back"],
628 ],
629 );
630 assert.lengthOf(projection.providerTurns, 1);
631 assert.lengthOf(projection.messages, 2);
632 assert.lengthOf(projection.turnItems, 2);
633 assert.lengthOf(projection.visibleTurnItems, 0);
634 }),
635 );
636​
637 it.effect("keeps fork visible items stable after a source run is rolled back", () =>
638 Effect.gen(function* () {
639 const projectionStore = yield* ProjectionStoreV2;
640 const now = yield* DateTime.now;
641 const projectId = ProjectId.make("project:projection-fork-source-rollback");
642 const sourceThreadId = ThreadId.make("thread:projection-fork-source-rollback:source");
643 const targetThreadId = ThreadId.make("thread:projection-fork-source-rollback:target");
644 const sourceProviderThreadId = ProviderThreadId.make(
645 "provider-thread:projection-fork-source-rollback:source",
646 );
647 const targetProviderThreadId = ProviderThreadId.make(
648 "provider-thread:projection-fork-source-rollback:target",
649 );
650 const sourceRun1Id = RunId.make("run:projection-fork-source-rollback:source:1");
651 const sourceRun2Id = RunId.make("run:projection-fork-source-rollback:source:2");
652 const sourceRun1NodeId = NodeId.make("node:projection-fork-source-rollback:source:1");
653 const sourceRun2NodeId = NodeId.make("node:projection-fork-source-rollback:source:2");
654​
655 yield* projectionStore.apply({
656 id: EventId.make("event:projection-fork-source-rollback:source-thread"),
657 type: "thread.created",
658 threadId: sourceThreadId,
659 occurredAt: now,
660 payload: {
661 createdBy: "user",
662 creationSource: "web",
663 id: sourceThreadId,
664 projectId,
665 title: "Projection fork source rollback source",
666 providerInstanceId,
667 modelSelection,
668 runtimeMode: "full-access",
669 interactionMode: "default",
670 branch: null,
671 worktreePath: null,
672 activeProviderThreadId: sourceProviderThreadId,
673 lineage: {
674 parentThreadId: null,
675 relationshipToParent: null,
676 rootThreadId: sourceThreadId,
677 },
678 forkedFrom: null,
679 createdAt: now,
680 updatedAt: now,
681 archivedAt: null,
682 deletedAt: null,
683 },
684 });
685 yield* projectionStore.apply({
686 id: EventId.make("event:projection-fork-source-rollback:target-thread"),
687 type: "thread.created",
688 threadId: targetThreadId,
689 occurredAt: now,
690 payload: {
691 createdBy: "user",
692 creationSource: "web",
693 id: targetThreadId,
694 projectId,
695 title: "Projection fork source rollback target",
696 providerInstanceId,
697 modelSelection,
698 runtimeMode: "full-access",
699 interactionMode: "default",
700 branch: null,
701 worktreePath: null,
702 activeProviderThreadId: targetProviderThreadId,
703 lineage: {
704 parentThreadId: sourceThreadId,
705 relationshipToParent: "fork",
706 rootThreadId: sourceThreadId,
707 },
708 forkedFrom: {
709 type: "run",
710 threadId: sourceThreadId,
711 runId: sourceRun2Id,
712 },
713 createdAt: now,
714 updatedAt: now,
715 archivedAt: null,
716 deletedAt: null,
717 },
718 });
719​
720 for (const [ordinal, runId, nodeId, promptText, responseText] of [
721 [1, sourceRun1Id, sourceRun1NodeId, "source one", "one"],
722 [2, sourceRun2Id, sourceRun2NodeId, "source two", "two"],
723 ] as const) {
724 yield* projectionStore.apply({
725 id: EventId.make(`event:projection-fork-source-rollback:run-${ordinal}`),
726 type: "run.created",
727 threadId: sourceThreadId,
728 runId,
729 nodeId,
730 driver,
731 occurredAt: now,
732 payload: {
733 id: runId,
734 threadId: sourceThreadId,
735 ordinal,
736 providerInstanceId,
737 modelSelection,
738 providerThreadId: sourceProviderThreadId,
739 userMessageId: MessageId.make(
740 `message:projection-fork-source-rollback:user:${ordinal}`,
741 ),
742 rootNodeId: nodeId,
743 activeAttemptId: null,
744 status: "completed",
745 requestedAt: now,
746 startedAt: now,
747 completedAt: now,
748 checkpointId: null,
749 contextHandoffId: null,
750 },
751 });
752 yield* projectionStore.apply({
753 id: EventId.make(`event:projection-fork-source-rollback:user-item-${ordinal}`),
754 type: "turn-item.updated",
755 threadId: sourceThreadId,
756 runId,
757 nodeId,
758 driver,
759 occurredAt: now,
760 payload: {
761 createdBy: "user",
762 creationSource: "web",
763 id: TurnItemId.make(`turn-item:projection-fork-source-rollback:user:${ordinal}`),
764 threadId: sourceThreadId,
765 runId,
766 nodeId,
767 providerThreadId: sourceProviderThreadId,
768 providerTurnId: null,
769 nativeItemRef: null,
770 parentItemId: null,
771 ordinal: ordinal * 100,
772 status: "completed",
773 title: null,
774 startedAt: now,
775 completedAt: now,
776 updatedAt: now,
777 type: "user_message",
778 messageId: MessageId.make(`message:projection-fork-source-rollback:user:${ordinal}`),
779 inputIntent: "turn_start",
780 text: promptText,
781 attachments: [],
782 },
783 });
784 yield* projectionStore.apply({
785 id: EventId.make(`event:projection-fork-source-rollback:assistant-item-${ordinal}`),
786 type: "turn-item.updated",
787 threadId: sourceThreadId,
788 runId,
789 nodeId,
790 driver,
791 occurredAt: now,
792 payload: {
793 id: TurnItemId.make(`turn-item:projection-fork-source-rollback:assistant:${ordinal}`),
794 threadId: sourceThreadId,
795 runId,
796 nodeId,
797 providerThreadId: sourceProviderThreadId,
798 providerTurnId: null,
799 nativeItemRef: null,
800 parentItemId: null,
801 ordinal: ordinal * 100 + 1,
802 status: "completed",
803 title: null,
804 startedAt: now,
805 completedAt: now,
806 updatedAt: now,
807 type: "assistant_message",
808 messageId: MessageId.make(
809 `message:projection-fork-source-rollback:assistant:${ordinal}`,
810 ),
811 text: responseText,
812 streaming: false,
813 },
814 });
815 }
816​
817 const targetBeforeRollback = yield* projectionStore.getThreadProjection(targetThreadId);
818 assert.deepEqual(
819 targetBeforeRollback.visibleTurnItems.map((row) => row.item.type),
820 ["user_message", "assistant_message", "user_message", "assistant_message", "fork"],
821 );
822​
823 yield* projectionStore.apply({
824 id: EventId.make("event:projection-fork-source-rollback:run-2-rolled-back"),
825 type: "run.updated",
826 threadId: sourceThreadId,
827 runId: sourceRun2Id,
828 nodeId: sourceRun2NodeId,
829 driver,
830 occurredAt: now,
831 payload: {
832 id: sourceRun2Id,
833 threadId: sourceThreadId,
834 ordinal: 2,
835 providerInstanceId,
836 modelSelection,
837 providerThreadId: sourceProviderThreadId,
838 userMessageId: MessageId.make("message:projection-fork-source-rollback:user:2"),
839 rootNodeId: sourceRun2NodeId,
840 activeAttemptId: null,
841 status: "rolled_back",
842 requestedAt: now,
843 startedAt: now,
844 completedAt: now,
845 checkpointId: null,
846 contextHandoffId: null,
847 },
848 });
849​
850 const targetAfterRollback = yield* projectionStore.getThreadProjection(targetThreadId);
851 assert.deepEqual(
852 targetAfterRollback.visibleTurnItems.map((row) => [
853 row.visibility,
854 row.item.type,
855 row.item.type === "user_message" || row.item.type === "assistant_message"
856 ? row.item.text
857 : row.item.title,
858 ]),
859 [
860 ["inherited", "user_message", "source one"],
861 ["inherited", "assistant_message", "one"],
862 ["inherited", "user_message", "source two"],
863 ["inherited", "assistant_message", "two"],
864 ["synthetic", "fork", "Forked from conversation"],
865 ],
866 );
867 }),
868 );
869});