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

store.test.ts

837 lines24.3 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("updates the existing project title when project.meta-updated arrives", () => {
332 const projectId = ProjectId.makeUnsafe("project-1");
333 const state = makeState(
334 makeThread({
335 projectId,
336 }),
337 );
338​
339 const next = applyOrchestrationEvent(
340 state,
341 makeEvent("project.meta-updated", {
342 projectId,
343 title: "Renamed Project",
344 updatedAt: "2026-02-27T00:00:01.000Z",
345 }),
346 );
347​
348 expect(next.projects[0]?.name).toBe("Renamed Project");
349 expect(next.projects[0]?.updatedAt).toBe("2026-02-27T00:00:01.000Z");
350 });
351​
352 it("preserves state identity for no-op project and thread deletes", () => {
353 const thread = makeThread();
354 const state = makeState(thread);
355​
356 const nextAfterProjectDelete = applyOrchestrationEvent(
357 state,
358 makeEvent("project.deleted", {
359 projectId: ProjectId.makeUnsafe("project-missing"),
360 deletedAt: "2026-02-27T00:00:01.000Z",
361 }),
362 );
363 const nextAfterThreadDelete = applyOrchestrationEvent(
364 state,
365 makeEvent("thread.deleted", {
366 threadId: ThreadId.makeUnsafe("thread-missing"),
367 deletedAt: "2026-02-27T00:00:01.000Z",
368 }),
369 );
370​
371 expect(nextAfterProjectDelete).toBe(state);
372 expect(nextAfterThreadDelete).toBe(state);
373 });
374​
375 it("reuses an existing project row when project.created arrives with a new id for the same cwd", () => {
376 const originalProjectId = ProjectId.makeUnsafe("project-1");
377 const recreatedProjectId = ProjectId.makeUnsafe("project-2");
378 const state: AppState = {
379 projects: [
380 {
381 id: originalProjectId,
382 name: "Project",
383 cwd: "/tmp/project",
384 defaultModelSelection: {
385 provider: "codex",
386 model: DEFAULT_MODEL_BY_PROVIDER.codex,
387 },
388 scripts: [],
389 },
390 ],
391 threads: [],
392 sidebarThreadsById: {},
393 threadIdsByProjectId: {},
394 bootstrapComplete: true,
395 };
396​
397 const next = applyOrchestrationEvent(
398 state,
399 makeEvent("project.created", {
400 projectId: recreatedProjectId,
401 title: "Project Recreated",
402 workspaceRoot: "/tmp/project",
403 defaultModelSelection: {
404 provider: "codex",
405 model: DEFAULT_MODEL_BY_PROVIDER.codex,
406 },
407 scripts: [],
408 createdAt: "2026-02-27T00:00:01.000Z",
409 updatedAt: "2026-02-27T00:00:01.000Z",
410 }),
411 );
412​
413 expect(next.projects).toHaveLength(1);
414 expect(next.projects[0]?.id).toBe(recreatedProjectId);
415 expect(next.projects[0]?.cwd).toBe("/tmp/project");
416 expect(next.projects[0]?.name).toBe("Project Recreated");
417 });
418​
419 it("removes stale project index entries when thread.created recreates a thread under a new project", () => {
420 const originalProjectId = ProjectId.makeUnsafe("project-1");
421 const recreatedProjectId = ProjectId.makeUnsafe("project-2");
422 const threadId = ThreadId.makeUnsafe("thread-1");
423 const thread = makeThread({
424 id: threadId,
425 projectId: originalProjectId,
426 });
427 const state: AppState = {
428 projects: [
429 {
430 id: originalProjectId,
431 name: "Project 1",
432 cwd: "/tmp/project-1",
433 defaultModelSelection: {
434 provider: "codex",
435 model: DEFAULT_MODEL_BY_PROVIDER.codex,
436 },
437 scripts: [],
438 },
439 {
440 id: recreatedProjectId,
441 name: "Project 2",
442 cwd: "/tmp/project-2",
443 defaultModelSelection: {
444 provider: "codex",
445 model: DEFAULT_MODEL_BY_PROVIDER.codex,
446 },
447 scripts: [],
448 },
449 ],
450 threads: [thread],
451 sidebarThreadsById: {},
452 threadIdsByProjectId: {
453 [originalProjectId]: [threadId],
454 },
455 bootstrapComplete: true,
456 };
457​
458 const next = applyOrchestrationEvent(
459 state,
460 makeEvent("thread.created", {
461 threadId,
462 projectId: recreatedProjectId,
463 title: "Recovered thread",
464 modelSelection: {
465 provider: "codex",
466 model: DEFAULT_MODEL_BY_PROVIDER.codex,
467 },
468 runtimeMode: DEFAULT_RUNTIME_MODE,
469 interactionMode: DEFAULT_INTERACTION_MODE,
470 branch: null,
471 worktreePath: null,
472 createdAt: "2026-02-27T00:00:01.000Z",
473 updatedAt: "2026-02-27T00:00:01.000Z",
474 }),
475 );
476​
477 expect(next.threads).toHaveLength(1);
478 expect(next.threads[0]?.projectId).toBe(recreatedProjectId);
479 expect(next.threadIdsByProjectId[originalProjectId]).toBeUndefined();
480 expect(next.threadIdsByProjectId[recreatedProjectId]).toEqual([threadId]);
481 });
482​
483 it("updates only the affected thread for message events", () => {
484 const thread1 = makeThread({
485 id: ThreadId.makeUnsafe("thread-1"),
486 messages: [
487 {
488 id: MessageId.makeUnsafe("message-1"),
489 role: "assistant",
490 text: "hello",
491 turnId: TurnId.makeUnsafe("turn-1"),
492 createdAt: "2026-02-27T00:00:00.000Z",
493 completedAt: "2026-02-27T00:00:00.000Z",
494 streaming: false,
495 },
496 ],
497 });
498 const thread2 = makeThread({ id: ThreadId.makeUnsafe("thread-2") });
499 const state: AppState = {
500 ...makeState(thread1),
501 threads: [thread1, thread2],
502 };
503​
504 const next = applyOrchestrationEvent(
505 state,
506 makeEvent("thread.message-sent", {
507 threadId: thread1.id,
508 messageId: MessageId.makeUnsafe("message-1"),
509 role: "assistant",
510 text: " world",
511 turnId: TurnId.makeUnsafe("turn-1"),
512 streaming: true,
513 createdAt: "2026-02-27T00:00:01.000Z",
514 updatedAt: "2026-02-27T00:00:01.000Z",
515 }),
516 );
517​
518 expect(next.threads[0]?.messages[0]?.text).toBe("hello world");
519 expect(next.threads[0]?.latestTurn?.state).toBe("running");
520 expect(next.threads[1]).toBe(thread2);
521 });
522​
523 it("applies replay batches in sequence and updates session state", () => {
524 const thread = makeThread({
525 latestTurn: {
526 turnId: TurnId.makeUnsafe("turn-1"),
527 state: "running",
528 requestedAt: "2026-02-27T00:00:00.000Z",
529 startedAt: "2026-02-27T00:00:00.000Z",
530 completedAt: null,
531 assistantMessageId: null,
532 },
533 });
534 const state = makeState(thread);
535​
536 const next = applyOrchestrationEvents(state, [
537 makeEvent(
538 "thread.session-set",
539 {
540 threadId: thread.id,
541 session: {
542 threadId: thread.id,
543 status: "running",
544 providerName: "codex",
545 runtimeMode: "full-access",
546 activeTurnId: TurnId.makeUnsafe("turn-1"),
547 lastError: null,
548 updatedAt: "2026-02-27T00:00:02.000Z",
549 },
550 },
551 { sequence: 2 },
552 ),
553 makeEvent(
554 "thread.message-sent",
555 {
556 threadId: thread.id,
557 messageId: MessageId.makeUnsafe("assistant-1"),
558 role: "assistant",
559 text: "done",
560 turnId: TurnId.makeUnsafe("turn-1"),
561 streaming: false,
562 createdAt: "2026-02-27T00:00:03.000Z",
563 updatedAt: "2026-02-27T00:00:03.000Z",
564 },
565 { sequence: 3 },
566 ),
567 ]);
568​
569 expect(next.threads[0]?.session?.status).toBe("running");
570 expect(next.threads[0]?.latestTurn?.state).toBe("completed");
571 expect(next.threads[0]?.messages).toHaveLength(1);
572 });
573​
574 it("does not regress latestTurn when an older turn diff completes late", () => {
575 const state = makeState(
576 makeThread({
577 latestTurn: {
578 turnId: TurnId.makeUnsafe("turn-2"),
579 state: "running",
580 requestedAt: "2026-02-27T00:00:02.000Z",
581 startedAt: "2026-02-27T00:00:03.000Z",
582 completedAt: null,
583 assistantMessageId: null,
584 },
585 }),
586 );
587​
588 const next = applyOrchestrationEvent(
589 state,
590 makeEvent("thread.turn-diff-completed", {
591 threadId: ThreadId.makeUnsafe("thread-1"),
592 turnId: TurnId.makeUnsafe("turn-1"),
593 checkpointTurnCount: 1,
594 checkpointRef: CheckpointRef.makeUnsafe("checkpoint-1"),
595 status: "ready",
596 files: [],
597 assistantMessageId: MessageId.makeUnsafe("assistant-1"),
598 completedAt: "2026-02-27T00:00:04.000Z",
599 }),
600 );
601​
602 expect(next.threads[0]?.turnDiffSummaries).toHaveLength(1);
603 expect(next.threads[0]?.latestTurn).toEqual(state.threads[0]?.latestTurn);
604 });
605​
606 it("rebinds live turn diffs to the authoritative assistant message when it arrives later", () => {
607 const turnId = TurnId.makeUnsafe("turn-1");
608 const state = makeState(
609 makeThread({
610 latestTurn: {
611 turnId,
612 state: "completed",
613 requestedAt: "2026-02-27T00:00:00.000Z",
614 startedAt: "2026-02-27T00:00:00.000Z",
615 completedAt: "2026-02-27T00:00:02.000Z",
616 assistantMessageId: MessageId.makeUnsafe("assistant:turn-1"),
617 },
618 turnDiffSummaries: [
619 {
620 turnId,
621 completedAt: "2026-02-27T00:00:02.000Z",
622 status: "ready",
623 checkpointTurnCount: 1,
624 checkpointRef: CheckpointRef.makeUnsafe("checkpoint-1"),
625 assistantMessageId: MessageId.makeUnsafe("assistant:turn-1"),
626 files: [{ path: "src/app.ts", additions: 1, deletions: 0 }],
627 },
628 ],
629 }),
630 );
631​
632 const next = applyOrchestrationEvent(
633 state,
634 makeEvent("thread.message-sent", {
635 threadId: ThreadId.makeUnsafe("thread-1"),
636 messageId: MessageId.makeUnsafe("assistant-real"),
637 role: "assistant",
638 text: "final answer",
639 turnId,
640 streaming: false,
641 createdAt: "2026-02-27T00:00:03.000Z",
642 updatedAt: "2026-02-27T00:00:03.000Z",
643 }),
644 );
645​
646 expect(next.threads[0]?.turnDiffSummaries[0]?.assistantMessageId).toBe(
647 MessageId.makeUnsafe("assistant-real"),
648 );
649 expect(next.threads[0]?.latestTurn?.assistantMessageId).toBe(
650 MessageId.makeUnsafe("assistant-real"),
651 );
652 });
653​
654 it("reverts messages, plans, activities, and checkpoints by retained turns", () => {
655 const state = makeState(
656 makeThread({
657 messages: [
658 {
659 id: MessageId.makeUnsafe("user-1"),
660 role: "user",
661 text: "first",
662 turnId: TurnId.makeUnsafe("turn-1"),
663 createdAt: "2026-02-27T00:00:00.000Z",
664 completedAt: "2026-02-27T00:00:00.000Z",
665 streaming: false,
666 },
667 {
668 id: MessageId.makeUnsafe("assistant-1"),
669 role: "assistant",
670 text: "first reply",
671 turnId: TurnId.makeUnsafe("turn-1"),
672 createdAt: "2026-02-27T00:00:01.000Z",
673 completedAt: "2026-02-27T00:00:01.000Z",
674 streaming: false,
675 },
676 {
677 id: MessageId.makeUnsafe("user-2"),
678 role: "user",
679 text: "second",
680 turnId: TurnId.makeUnsafe("turn-2"),
681 createdAt: "2026-02-27T00:00:02.000Z",
682 completedAt: "2026-02-27T00:00:02.000Z",
683 streaming: false,
684 },
685 ],
686 proposedPlans: [
687 {
688 id: "plan-1",
689 turnId: TurnId.makeUnsafe("turn-1"),
690 planMarkdown: "plan 1",
691 implementedAt: null,
692 implementationThreadId: null,
693 createdAt: "2026-02-27T00:00:00.000Z",
694 updatedAt: "2026-02-27T00:00:00.000Z",
695 },
696 {
697 id: "plan-2",
698 turnId: TurnId.makeUnsafe("turn-2"),
699 planMarkdown: "plan 2",
700 implementedAt: null,
701 implementationThreadId: null,
702 createdAt: "2026-02-27T00:00:02.000Z",
703 updatedAt: "2026-02-27T00:00:02.000Z",
704 },
705 ],
706 activities: [
707 {
708 id: EventId.makeUnsafe("activity-1"),
709 tone: "info",
710 kind: "step",
711 summary: "one",
712 payload: {},
713 turnId: TurnId.makeUnsafe("turn-1"),
714 createdAt: "2026-02-27T00:00:00.000Z",
715 },
716 {
717 id: EventId.makeUnsafe("activity-2"),
718 tone: "info",
719 kind: "step",
720 summary: "two",
721 payload: {},
722 turnId: TurnId.makeUnsafe("turn-2"),
723 createdAt: "2026-02-27T00:00:02.000Z",
724 },
725 ],
726 turnDiffSummaries: [
727 {
728 turnId: TurnId.makeUnsafe("turn-1"),
729 completedAt: "2026-02-27T00:00:01.000Z",
730 status: "ready",
731 checkpointTurnCount: 1,
732 checkpointRef: CheckpointRef.makeUnsafe("ref-1"),
733 files: [],
734 },
735 {
736 turnId: TurnId.makeUnsafe("turn-2"),
737 completedAt: "2026-02-27T00:00:03.000Z",
738 status: "ready",
739 checkpointTurnCount: 2,
740 checkpointRef: CheckpointRef.makeUnsafe("ref-2"),
741 files: [],
742 },
743 ],
744 }),
745 );
746​
747 const next = applyOrchestrationEvent(
748 state,
749 makeEvent("thread.reverted", {
750 threadId: ThreadId.makeUnsafe("thread-1"),
751 turnCount: 1,
752 }),
753 );
754​
755 expect(next.threads[0]?.messages.map((message) => message.id)).toEqual([
756 "user-1",
757 "assistant-1",
758 ]);
759 expect(next.threads[0]?.proposedPlans.map((plan) => plan.id)).toEqual(["plan-1"]);
760 expect(next.threads[0]?.activities.map((activity) => activity.id)).toEqual([
761 EventId.makeUnsafe("activity-1"),
762 ]);
763 expect(next.threads[0]?.turnDiffSummaries.map((summary) => summary.turnId)).toEqual([
764 TurnId.makeUnsafe("turn-1"),
765 ]);
766 });
767​
768 it("clears pending source proposed plans after revert before a new session-set event", () => {
769 const thread = makeThread({
770 latestTurn: {
771 turnId: TurnId.makeUnsafe("turn-2"),
772 state: "completed",
773 requestedAt: "2026-02-27T00:00:02.000Z",
774 startedAt: "2026-02-27T00:00:02.000Z",
775 completedAt: "2026-02-27T00:00:03.000Z",
776 assistantMessageId: MessageId.makeUnsafe("assistant-2"),
777 sourceProposedPlan: {
778 threadId: ThreadId.makeUnsafe("thread-source"),
779 planId: "plan-2" as never,
780 },
781 },
782 pendingSourceProposedPlan: {
783 threadId: ThreadId.makeUnsafe("thread-source"),
784 planId: "plan-2" as never,
785 },
786 turnDiffSummaries: [
787 {
788 turnId: TurnId.makeUnsafe("turn-1"),
789 completedAt: "2026-02-27T00:00:01.000Z",
790 status: "ready",
791 checkpointTurnCount: 1,
792 checkpointRef: CheckpointRef.makeUnsafe("ref-1"),
793 files: [],
794 },
795 {
796 turnId: TurnId.makeUnsafe("turn-2"),
797 completedAt: "2026-02-27T00:00:03.000Z",
798 status: "ready",
799 checkpointTurnCount: 2,
800 checkpointRef: CheckpointRef.makeUnsafe("ref-2"),
801 files: [],
802 },
803 ],
804 });
805 const reverted = applyOrchestrationEvent(
806 makeState(thread),
807 makeEvent("thread.reverted", {
808 threadId: thread.id,
809 turnCount: 1,
810 }),
811 );
812​
813 expect(reverted.threads[0]?.pendingSourceProposedPlan).toBeUndefined();
814​
815 const next = applyOrchestrationEvent(
816 reverted,
817 makeEvent("thread.session-set", {
818 threadId: thread.id,
819 session: {
820 threadId: thread.id,
821 status: "running",
822 providerName: "codex",
823 runtimeMode: "full-access",
824 activeTurnId: TurnId.makeUnsafe("turn-3"),
825 lastError: null,
826 updatedAt: "2026-02-27T00:00:04.000Z",
827 },
828 }),
829 );
830​
831 expect(next.threads[0]?.latestTurn).toMatchObject({
832 turnId: TurnId.makeUnsafe("turn-3"),
833 state: "running",
834 });
835 expect(next.threads[0]?.latestTurn?.sourceProposedPlan).toBeUndefined();
836 });
837});