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

store.test.ts

816 lines23.8 KB

1import {
2 CheckpointRef,
3 DEFAULT_MODEL_BY_PROVIDER,
4 EventId,
5 MessageId,
6 ProjectId,
7 ThreadId,
8 TurnId,
9 type OrchestrationEvent,
10 type OrchestrationReadModel,
11} from "@t3tools/contracts";
12import { describe, expect, it } from "vitest";
13​
14import {
15 applyOrchestrationEvent,
16 applyOrchestrationEvents,
17 syncServerReadModel,
18 type AppState,
19} from "./store";
20import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, type Thread } from "./types";
21​
22function makeThread(overrides: Partial<Thread> = {}): Thread {
23 return {
24 id: ThreadId.makeUnsafe("thread-1"),
25 codexThreadId: null,
26 projectId: ProjectId.makeUnsafe("project-1"),
27 title: "Thread",
28 modelSelection: {
29 provider: "codex",
30 model: "gpt-5-codex",
31 },
32 runtimeMode: DEFAULT_RUNTIME_MODE,
33 interactionMode: DEFAULT_INTERACTION_MODE,
34 session: null,
35 messages: [],
36 turnDiffSummaries: [],
37 activities: [],
38 proposedPlans: [],
39 error: null,
40 createdAt: "2026-02-13T00:00:00.000Z",
41 archivedAt: null,
42 latestTurn: null,
43 branch: null,
44 worktreePath: null,
45 ...overrides,
46 };
48​
49function makeState(thread: Thread): AppState {
50 const threadIdsByProjectId: AppState["threadIdsByProjectId"] = {
51 [thread.projectId]: [thread.id],
52 };
53 return {
54 projects: [
55 {
56 id: ProjectId.makeUnsafe("project-1"),
57 name: "Project",
58 cwd: "/tmp/project",
59 defaultModelSelection: {
60 provider: "codex",
61 model: "gpt-5-codex",
62 },
63 scripts: [],
64 },
65 ],
66 threads: [thread],
67 sidebarThreadsById: {},
68 threadIdsByProjectId,
69 bootstrapComplete: true,
70 };
72​
73function makeEvent<T extends OrchestrationEvent["type"]>(
74 type: T,
75 payload: Extract<OrchestrationEvent, { type: T }>["payload"],
76 overrides: Partial<Extract<OrchestrationEvent, { type: T }>> = {},
77): Extract<OrchestrationEvent, { type: T }> {
78 const sequence = overrides.sequence ?? 1;
79 return {
80 sequence,
81 eventId: EventId.makeUnsafe(`event-${sequence}`),
82 aggregateKind: "thread",
83 aggregateId:
84 "threadId" in payload
85 ? payload.threadId
86 : "projectId" in payload
87 ? payload.projectId
88 : ProjectId.makeUnsafe("project-1"),
89 occurredAt: "2026-02-27T00:00:00.000Z",
90 commandId: null,
91 causationEventId: null,
92 correlationId: null,
93 metadata: {},
94 type,
95 payload,
96 ...overrides,
97 } as Extract<OrchestrationEvent, { type: T }>;
99​
100function makeReadModelThread(overrides: Partial<OrchestrationReadModel["threads"][number]>) {
101 return {
102 id: ThreadId.makeUnsafe("thread-1"),
103 projectId: ProjectId.makeUnsafe("project-1"),
104 title: "Thread",
105 modelSelection: {
106 provider: "codex",
107 model: "gpt-5.3-codex",
108 },
109 runtimeMode: DEFAULT_RUNTIME_MODE,
110 interactionMode: DEFAULT_INTERACTION_MODE,
111 branch: null,
112 worktreePath: null,
113 latestTurn: null,
114 createdAt: "2026-02-27T00:00:00.000Z",
115 updatedAt: "2026-02-27T00:00:00.000Z",
116 archivedAt: null,
117 deletedAt: null,
118 messages: [],
119 activities: [],
120 proposedPlans: [],
121 checkpoints: [],
122 session: null,
123 ...overrides,
124 } satisfies OrchestrationReadModel["threads"][number];
126​
127function makeReadModel(thread: OrchestrationReadModel["threads"][number]): OrchestrationReadModel {
128 return {
129 snapshotSequence: 1,
130 updatedAt: "2026-02-27T00:00:00.000Z",
131 projects: [
132 {
133 id: ProjectId.makeUnsafe("project-1"),
134 title: "Project",
135 workspaceRoot: "/tmp/project",
136 defaultModelSelection: {
137 provider: "codex",
138 model: "gpt-5.3-codex",
139 },
140 createdAt: "2026-02-27T00:00:00.000Z",
141 updatedAt: "2026-02-27T00:00:00.000Z",
142 deletedAt: null,
143 scripts: [],
144 },
145 ],
146 threads: [thread],
147 };
149​
150function makeReadModelProject(
151 overrides: Partial<OrchestrationReadModel["projects"][number]>,
152): OrchestrationReadModel["projects"][number] {
153 return {
154 id: ProjectId.makeUnsafe("project-1"),
155 title: "Project",
156 workspaceRoot: "/tmp/project",
157 defaultModelSelection: {
158 provider: "codex",
159 model: "gpt-5.3-codex",
160 },
161 createdAt: "2026-02-27T00:00:00.000Z",
162 updatedAt: "2026-02-27T00:00:00.000Z",
163 deletedAt: null,
164 scripts: [],
165 ...overrides,
166 };
168​
169describe("store read model sync", () => {
170 it("marks bootstrap complete after snapshot sync", () => {
171 const initialState: AppState = {
172 ...makeState(makeThread()),
173 bootstrapComplete: false,
174 };
175​
176 const next = syncServerReadModel(initialState, makeReadModel(makeReadModelThread({})));
177​
178 expect(next.bootstrapComplete).toBe(true);
179 });
180​
181 it("preserves claude model slugs without an active session", () => {
182 const initialState = makeState(makeThread());
183 const readModel = makeReadModel(
184 makeReadModelThread({
185 modelSelection: {
186 provider: "claudeAgent",
187 model: "claude-opus-4-6",
188 },
189 }),
190 );
191​
192 const next = syncServerReadModel(initialState, readModel);
193​
194 expect(next.threads[0]?.modelSelection.model).toBe("claude-opus-4-6");
195 });
196​
197 it("resolves claude aliases when session provider is claudeAgent", () => {
198 const initialState = makeState(makeThread());
199 const readModel = makeReadModel(
200 makeReadModelThread({
201 modelSelection: {
202 provider: "claudeAgent",
203 model: "sonnet",
204 },
205 session: {
206 threadId: ThreadId.makeUnsafe("thread-1"),
207 status: "ready",
208 providerName: "claudeAgent",
209 runtimeMode: "approval-required",
210 activeTurnId: null,
211 lastError: null,
212 updatedAt: "2026-02-27T00:00:00.000Z",
213 },
214 }),
215 );
216​
217 const next = syncServerReadModel(initialState, readModel);
218​
219 expect(next.threads[0]?.modelSelection.model).toBe("claude-sonnet-4-6");
220 });
221​
222 it("preserves project and thread updatedAt timestamps from the read model", () => {
223 const initialState = makeState(makeThread());
224 const readModel = makeReadModel(
225 makeReadModelThread({
226 updatedAt: "2026-02-27T00:05:00.000Z",
227 }),
228 );
229​
230 const next = syncServerReadModel(initialState, readModel);
231​
232 expect(next.projects[0]?.updatedAt).toBe("2026-02-27T00:00:00.000Z");
233 expect(next.threads[0]?.updatedAt).toBe("2026-02-27T00:05:00.000Z");
234 });
235​
236 it("maps archivedAt from the read model", () => {
237 const initialState = makeState(makeThread());
238 const archivedAt = "2026-02-28T00:00:00.000Z";
239 const next = syncServerReadModel(
240 initialState,
241 makeReadModel(
242 makeReadModelThread({
243 archivedAt,
244 }),
245 ),
246 );
247​
248 expect(next.threads[0]?.archivedAt).toBe(archivedAt);
249 });
250​
251 it("replaces projects using snapshot order during recovery", () => {
252 const project1 = ProjectId.makeUnsafe("project-1");
253 const project2 = ProjectId.makeUnsafe("project-2");
254 const project3 = ProjectId.makeUnsafe("project-3");
255 const initialState: AppState = {
256 projects: [
257 {
258 id: project2,
259 name: "Project 2",
260 cwd: "/tmp/project-2",
261 defaultModelSelection: {
262 provider: "codex",
263 model: DEFAULT_MODEL_BY_PROVIDER.codex,
264 },
265 scripts: [],
266 },
267 {
268 id: project1,
269 name: "Project 1",
270 cwd: "/tmp/project-1",
271 defaultModelSelection: {
272 provider: "codex",
273 model: DEFAULT_MODEL_BY_PROVIDER.codex,
274 },
275 scripts: [],
276 },
277 ],
278 threads: [],
279 sidebarThreadsById: {},
280 threadIdsByProjectId: {},
281 bootstrapComplete: true,
282 };
283 const readModel: OrchestrationReadModel = {
284 snapshotSequence: 2,
285 updatedAt: "2026-02-27T00:00:00.000Z",
286 projects: [
287 makeReadModelProject({
288 id: project1,
289 title: "Project 1",
290 workspaceRoot: "/tmp/project-1",
291 }),
292 makeReadModelProject({
293 id: project2,
294 title: "Project 2",
295 workspaceRoot: "/tmp/project-2",
296 }),
297 makeReadModelProject({
298 id: project3,
299 title: "Project 3",
300 workspaceRoot: "/tmp/project-3",
301 }),
302 ],
303 threads: [],
304 };
305​
306 const next = syncServerReadModel(initialState, readModel);
307​
308 expect(next.projects.map((project) => project.id)).toEqual([project1, project2, project3]);
309 });
310});
311​
312describe("incremental orchestration updates", () => {
313 it("does not mark bootstrap complete for incremental events", () => {
314 const state: AppState = {
315 ...makeState(makeThread()),
316 bootstrapComplete: false,
317 };
318​
319 const next = applyOrchestrationEvent(
320 state,
321 makeEvent("thread.meta-updated", {
322 threadId: ThreadId.makeUnsafe("thread-1"),
323 title: "Updated title",
324 updatedAt: "2026-02-27T00:00:01.000Z",
325 }),
326 );
327​
328 expect(next.bootstrapComplete).toBe(false);
329 });
330​
331 it("preserves state identity for no-op project and thread deletes", () => {
332 const thread = makeThread();
333 const state = makeState(thread);
334​
335 const nextAfterProjectDelete = applyOrchestrationEvent(
336 state,
337 makeEvent("project.deleted", {
338 projectId: ProjectId.makeUnsafe("project-missing"),
339 deletedAt: "2026-02-27T00:00:01.000Z",
340 }),
341 );
342 const nextAfterThreadDelete = applyOrchestrationEvent(
343 state,
344 makeEvent("thread.deleted", {
345 threadId: ThreadId.makeUnsafe("thread-missing"),
346 deletedAt: "2026-02-27T00:00:01.000Z",
347 }),
348 );
349​
350 expect(nextAfterProjectDelete).toBe(state);
351 expect(nextAfterThreadDelete).toBe(state);
352 });
353​
354 it("reuses an existing project row when project.created arrives with a new id for the same cwd", () => {
355 const originalProjectId = ProjectId.makeUnsafe("project-1");
356 const recreatedProjectId = ProjectId.makeUnsafe("project-2");
357 const state: AppState = {
358 projects: [
359 {
360 id: originalProjectId,
361 name: "Project",
362 cwd: "/tmp/project",
363 defaultModelSelection: {
364 provider: "codex",
365 model: DEFAULT_MODEL_BY_PROVIDER.codex,
366 },
367 scripts: [],
368 },
369 ],
370 threads: [],
371 sidebarThreadsById: {},
372 threadIdsByProjectId: {},
373 bootstrapComplete: true,
374 };
375​
376 const next = applyOrchestrationEvent(
377 state,
378 makeEvent("project.created", {
379 projectId: recreatedProjectId,
380 title: "Project Recreated",
381 workspaceRoot: "/tmp/project",
382 defaultModelSelection: {
383 provider: "codex",
384 model: DEFAULT_MODEL_BY_PROVIDER.codex,
385 },
386 scripts: [],
387 createdAt: "2026-02-27T00:00:01.000Z",
388 updatedAt: "2026-02-27T00:00:01.000Z",
389 }),
390 );
391​
392 expect(next.projects).toHaveLength(1);
393 expect(next.projects[0]?.id).toBe(recreatedProjectId);
394 expect(next.projects[0]?.cwd).toBe("/tmp/project");
395 expect(next.projects[0]?.name).toBe("Project Recreated");
396 });
397​
398 it("removes stale project index entries when thread.created recreates a thread under a new project", () => {
399 const originalProjectId = ProjectId.makeUnsafe("project-1");
400 const recreatedProjectId = ProjectId.makeUnsafe("project-2");
401 const threadId = ThreadId.makeUnsafe("thread-1");
402 const thread = makeThread({
403 id: threadId,
404 projectId: originalProjectId,
405 });
406 const state: AppState = {
407 projects: [
408 {
409 id: originalProjectId,
410 name: "Project 1",
411 cwd: "/tmp/project-1",
412 defaultModelSelection: {
413 provider: "codex",
414 model: DEFAULT_MODEL_BY_PROVIDER.codex,
415 },
416 scripts: [],
417 },
418 {
419 id: recreatedProjectId,
420 name: "Project 2",
421 cwd: "/tmp/project-2",
422 defaultModelSelection: {
423 provider: "codex",
424 model: DEFAULT_MODEL_BY_PROVIDER.codex,
425 },
426 scripts: [],
427 },
428 ],
429 threads: [thread],
430 sidebarThreadsById: {},
431 threadIdsByProjectId: {
432 [originalProjectId]: [threadId],
433 },
434 bootstrapComplete: true,
435 };
436​
437 const next = applyOrchestrationEvent(
438 state,
439 makeEvent("thread.created", {
440 threadId,
441 projectId: recreatedProjectId,
442 title: "Recovered thread",
443 modelSelection: {
444 provider: "codex",
445 model: DEFAULT_MODEL_BY_PROVIDER.codex,
446 },
447 runtimeMode: DEFAULT_RUNTIME_MODE,
448 interactionMode: DEFAULT_INTERACTION_MODE,
449 branch: null,
450 worktreePath: null,
451 createdAt: "2026-02-27T00:00:01.000Z",
452 updatedAt: "2026-02-27T00:00:01.000Z",
453 }),
454 );
455​
456 expect(next.threads).toHaveLength(1);
457 expect(next.threads[0]?.projectId).toBe(recreatedProjectId);
458 expect(next.threadIdsByProjectId[originalProjectId]).toBeUndefined();
459 expect(next.threadIdsByProjectId[recreatedProjectId]).toEqual([threadId]);
460 });
461​
462 it("updates only the affected thread for message events", () => {
463 const thread1 = makeThread({
464 id: ThreadId.makeUnsafe("thread-1"),
465 messages: [
466 {
467 id: MessageId.makeUnsafe("message-1"),
468 role: "assistant",
469 text: "hello",
470 turnId: TurnId.makeUnsafe("turn-1"),
471 createdAt: "2026-02-27T00:00:00.000Z",
472 completedAt: "2026-02-27T00:00:00.000Z",
473 streaming: false,
474 },
475 ],
476 });
477 const thread2 = makeThread({ id: ThreadId.makeUnsafe("thread-2") });
478 const state: AppState = {
479 ...makeState(thread1),
480 threads: [thread1, thread2],
481 };
482​
483 const next = applyOrchestrationEvent(
484 state,
485 makeEvent("thread.message-sent", {
486 threadId: thread1.id,
487 messageId: MessageId.makeUnsafe("message-1"),
488 role: "assistant",
489 text: " world",
490 turnId: TurnId.makeUnsafe("turn-1"),
491 streaming: true,
492 createdAt: "2026-02-27T00:00:01.000Z",
493 updatedAt: "2026-02-27T00:00:01.000Z",
494 }),
495 );
496​
497 expect(next.threads[0]?.messages[0]?.text).toBe("hello world");
498 expect(next.threads[0]?.latestTurn?.state).toBe("running");
499 expect(next.threads[1]).toBe(thread2);
500 });
501​
502 it("applies replay batches in sequence and updates session state", () => {
503 const thread = makeThread({
504 latestTurn: {
505 turnId: TurnId.makeUnsafe("turn-1"),
506 state: "running",
507 requestedAt: "2026-02-27T00:00:00.000Z",
508 startedAt: "2026-02-27T00:00:00.000Z",
509 completedAt: null,
510 assistantMessageId: null,
511 },
512 });
513 const state = makeState(thread);
514​
515 const next = applyOrchestrationEvents(state, [
516 makeEvent(
517 "thread.session-set",
518 {
519 threadId: thread.id,
520 session: {
521 threadId: thread.id,
522 status: "running",
523 providerName: "codex",
524 runtimeMode: "full-access",
525 activeTurnId: TurnId.makeUnsafe("turn-1"),
526 lastError: null,
527 updatedAt: "2026-02-27T00:00:02.000Z",
528 },
529 },
530 { sequence: 2 },
531 ),
532 makeEvent(
533 "thread.message-sent",
534 {
535 threadId: thread.id,
536 messageId: MessageId.makeUnsafe("assistant-1"),
537 role: "assistant",
538 text: "done",
539 turnId: TurnId.makeUnsafe("turn-1"),
540 streaming: false,
541 createdAt: "2026-02-27T00:00:03.000Z",
542 updatedAt: "2026-02-27T00:00:03.000Z",
543 },
544 { sequence: 3 },
545 ),
546 ]);
547​
548 expect(next.threads[0]?.session?.status).toBe("running");
549 expect(next.threads[0]?.latestTurn?.state).toBe("completed");
550 expect(next.threads[0]?.messages).toHaveLength(1);
551 });
552​
553 it("does not regress latestTurn when an older turn diff completes late", () => {
554 const state = makeState(
555 makeThread({
556 latestTurn: {
557 turnId: TurnId.makeUnsafe("turn-2"),
558 state: "running",
559 requestedAt: "2026-02-27T00:00:02.000Z",
560 startedAt: "2026-02-27T00:00:03.000Z",
561 completedAt: null,
562 assistantMessageId: null,
563 },
564 }),
565 );
566​
567 const next = applyOrchestrationEvent(
568 state,
569 makeEvent("thread.turn-diff-completed", {
570 threadId: ThreadId.makeUnsafe("thread-1"),
571 turnId: TurnId.makeUnsafe("turn-1"),
572 checkpointTurnCount: 1,
573 checkpointRef: CheckpointRef.makeUnsafe("checkpoint-1"),
574 status: "ready",
575 files: [],
576 assistantMessageId: MessageId.makeUnsafe("assistant-1"),
577 completedAt: "2026-02-27T00:00:04.000Z",
578 }),
579 );
580​
581 expect(next.threads[0]?.turnDiffSummaries).toHaveLength(1);
582 expect(next.threads[0]?.latestTurn).toEqual(state.threads[0]?.latestTurn);
583 });
584​
585 it("rebinds live turn diffs to the authoritative assistant message when it arrives later", () => {
586 const turnId = TurnId.makeUnsafe("turn-1");
587 const state = makeState(
588 makeThread({
589 latestTurn: {
590 turnId,
591 state: "completed",
592 requestedAt: "2026-02-27T00:00:00.000Z",
593 startedAt: "2026-02-27T00:00:00.000Z",
594 completedAt: "2026-02-27T00:00:02.000Z",
595 assistantMessageId: MessageId.makeUnsafe("assistant:turn-1"),
596 },
597 turnDiffSummaries: [
598 {
599 turnId,
600 completedAt: "2026-02-27T00:00:02.000Z",
601 status: "ready",
602 checkpointTurnCount: 1,
603 checkpointRef: CheckpointRef.makeUnsafe("checkpoint-1"),
604 assistantMessageId: MessageId.makeUnsafe("assistant:turn-1"),
605 files: [{ path: "src/app.ts", additions: 1, deletions: 0 }],
606 },
607 ],
608 }),
609 );
610​
611 const next = applyOrchestrationEvent(
612 state,
613 makeEvent("thread.message-sent", {
614 threadId: ThreadId.makeUnsafe("thread-1"),
615 messageId: MessageId.makeUnsafe("assistant-real"),
616 role: "assistant",
617 text: "final answer",
618 turnId,
619 streaming: false,
620 createdAt: "2026-02-27T00:00:03.000Z",
621 updatedAt: "2026-02-27T00:00:03.000Z",
622 }),
623 );
624​
625 expect(next.threads[0]?.turnDiffSummaries[0]?.assistantMessageId).toBe(
626 MessageId.makeUnsafe("assistant-real"),
627 );
628 expect(next.threads[0]?.latestTurn?.assistantMessageId).toBe(
629 MessageId.makeUnsafe("assistant-real"),
630 );
631 });
632​
633 it("reverts messages, plans, activities, and checkpoints by retained turns", () => {
634 const state = makeState(
635 makeThread({
636 messages: [
637 {
638 id: MessageId.makeUnsafe("user-1"),
639 role: "user",
640 text: "first",
641 turnId: TurnId.makeUnsafe("turn-1"),
642 createdAt: "2026-02-27T00:00:00.000Z",
643 completedAt: "2026-02-27T00:00:00.000Z",
644 streaming: false,
645 },
646 {
647 id: MessageId.makeUnsafe("assistant-1"),
648 role: "assistant",
649 text: "first reply",
650 turnId: TurnId.makeUnsafe("turn-1"),
651 createdAt: "2026-02-27T00:00:01.000Z",
652 completedAt: "2026-02-27T00:00:01.000Z",
653 streaming: false,
654 },
655 {
656 id: MessageId.makeUnsafe("user-2"),
657 role: "user",
658 text: "second",
659 turnId: TurnId.makeUnsafe("turn-2"),
660 createdAt: "2026-02-27T00:00:02.000Z",
661 completedAt: "2026-02-27T00:00:02.000Z",
662 streaming: false,
663 },
664 ],
665 proposedPlans: [
666 {
667 id: "plan-1",
668 turnId: TurnId.makeUnsafe("turn-1"),
669 planMarkdown: "plan 1",
670 implementedAt: null,
671 implementationThreadId: null,
672 createdAt: "2026-02-27T00:00:00.000Z",
673 updatedAt: "2026-02-27T00:00:00.000Z",
674 },
675 {
676 id: "plan-2",
677 turnId: TurnId.makeUnsafe("turn-2"),
678 planMarkdown: "plan 2",
679 implementedAt: null,
680 implementationThreadId: null,
681 createdAt: "2026-02-27T00:00:02.000Z",
682 updatedAt: "2026-02-27T00:00:02.000Z",
683 },
684 ],
685 activities: [
686 {
687 id: EventId.makeUnsafe("activity-1"),
688 tone: "info",
689 kind: "step",
690 summary: "one",
691 payload: {},
692 turnId: TurnId.makeUnsafe("turn-1"),
693 createdAt: "2026-02-27T00:00:00.000Z",
694 },
695 {
696 id: EventId.makeUnsafe("activity-2"),
697 tone: "info",
698 kind: "step",
699 summary: "two",
700 payload: {},
701 turnId: TurnId.makeUnsafe("turn-2"),
702 createdAt: "2026-02-27T00:00:02.000Z",
703 },
704 ],
705 turnDiffSummaries: [
706 {
707 turnId: TurnId.makeUnsafe("turn-1"),
708 completedAt: "2026-02-27T00:00:01.000Z",
709 status: "ready",
710 checkpointTurnCount: 1,
711 checkpointRef: CheckpointRef.makeUnsafe("ref-1"),
712 files: [],
713 },
714 {
715 turnId: TurnId.makeUnsafe("turn-2"),
716 completedAt: "2026-02-27T00:00:03.000Z",
717 status: "ready",
718 checkpointTurnCount: 2,
719 checkpointRef: CheckpointRef.makeUnsafe("ref-2"),
720 files: [],
721 },
722 ],
723 }),
724 );
725​
726 const next = applyOrchestrationEvent(
727 state,
728 makeEvent("thread.reverted", {
729 threadId: ThreadId.makeUnsafe("thread-1"),
730 turnCount: 1,
731 }),
732 );
733​
734 expect(next.threads[0]?.messages.map((message) => message.id)).toEqual([
735 "user-1",
736 "assistant-1",
737 ]);
738 expect(next.threads[0]?.proposedPlans.map((plan) => plan.id)).toEqual(["plan-1"]);
739 expect(next.threads[0]?.activities.map((activity) => activity.id)).toEqual([
740 EventId.makeUnsafe("activity-1"),
741 ]);
742 expect(next.threads[0]?.turnDiffSummaries.map((summary) => summary.turnId)).toEqual([
743 TurnId.makeUnsafe("turn-1"),
744 ]);
745 });
746​
747 it("clears pending source proposed plans after revert before a new session-set event", () => {
748 const thread = makeThread({
749 latestTurn: {
750 turnId: TurnId.makeUnsafe("turn-2"),
751 state: "completed",
752 requestedAt: "2026-02-27T00:00:02.000Z",
753 startedAt: "2026-02-27T00:00:02.000Z",
754 completedAt: "2026-02-27T00:00:03.000Z",
755 assistantMessageId: MessageId.makeUnsafe("assistant-2"),
756 sourceProposedPlan: {
757 threadId: ThreadId.makeUnsafe("thread-source"),
758 planId: "plan-2" as never,
759 },
760 },
761 pendingSourceProposedPlan: {
762 threadId: ThreadId.makeUnsafe("thread-source"),
763 planId: "plan-2" as never,
764 },
765 turnDiffSummaries: [
766 {
767 turnId: TurnId.makeUnsafe("turn-1"),
768 completedAt: "2026-02-27T00:00:01.000Z",
769 status: "ready",
770 checkpointTurnCount: 1,
771 checkpointRef: CheckpointRef.makeUnsafe("ref-1"),
772 files: [],
773 },
774 {
775 turnId: TurnId.makeUnsafe("turn-2"),
776 completedAt: "2026-02-27T00:00:03.000Z",
777 status: "ready",
778 checkpointTurnCount: 2,
779 checkpointRef: CheckpointRef.makeUnsafe("ref-2"),
780 files: [],
781 },
782 ],
783 });
784 const reverted = applyOrchestrationEvent(
785 makeState(thread),
786 makeEvent("thread.reverted", {
787 threadId: thread.id,
788 turnCount: 1,
789 }),
790 );
791​
792 expect(reverted.threads[0]?.pendingSourceProposedPlan).toBeUndefined();
793​
794 const next = applyOrchestrationEvent(
795 reverted,
796 makeEvent("thread.session-set", {
797 threadId: thread.id,
798 session: {
799 threadId: thread.id,
800 status: "running",
801 providerName: "codex",
802 runtimeMode: "full-access",
803 activeTurnId: TurnId.makeUnsafe("turn-3"),
804 lastError: null,
805 updatedAt: "2026-02-27T00:00:04.000Z",
806 },
807 }),
808 );
809​
810 expect(next.threads[0]?.latestTurn).toMatchObject({
811 turnId: TurnId.makeUnsafe("turn-3"),
812 state: "running",
813 });
814 expect(next.threads[0]?.latestTurn?.sourceProposedPlan).toBeUndefined();
815 });
816});