Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 type OrchestrationV2DomainEvent,
4 type OrchestrationV2Run,
5 type OrchestrationV2ThreadProjection,
6 type OrchestrationV2TurnItem,
7 MessageId,
8 ProjectId,
9 ProviderInstanceId,
10 NodeId,
11 ProviderThreadId,
12 ProviderTurnId,
13 RunId,
14 ThreadId,
15 TurnItemId,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18
19import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
20
21const now = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
22const threadId = ThreadId.make("thread-reducer");
23const runId = RunId.make("run-reducer");
24const run = {
25 id: runId,
26 threadId,
27 ordinal: 1,
28 providerInstanceId: ProviderInstanceId.make("codex"),
29 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
30 providerThreadId: null,
31 userMessageId: MessageId.make("message-reducer"),
32 rootNodeId: null,
33 activeAttemptId: null,
34 status: "completed",
35 requestedAt: now,
36 startedAt: now,
37 completedAt: now,
38 checkpointId: null,
39 contextHandoffId: null,
40} satisfies OrchestrationV2Run;
41
42function commandItem(id: string, output = "done", ordinal = 1): OrchestrationV2TurnItem {
43 return {
44 id: TurnItemId.make(id),
45 threadId,
46 runId,
47 nodeId: null,
48 providerThreadId: null,
49 providerTurnId: null,
50 nativeItemRef: null,
51 parentItemId: null,
52 ordinal,
53 status: "completed",
54 title: null,
55 startedAt: now,
56 completedAt: now,
57 updatedAt: now,
58 type: "command_execution",
59 input: "pwd",
60 output,
61 exitCode: 0,
62 };
63}
64const emptyProjection = {
65 thread: {
66 id: threadId,
67 projectId: ProjectId.make("project-reducer"),
68 title: "Reducer",
69 providerInstanceId: ProviderInstanceId.make("codex"),
70 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
71 runtimeMode: "full-access",
72 interactionMode: "default",
73 branch: null,
74 worktreePath: null,
75 activeProviderThreadId: null,
76 lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
77 forkedFrom: null,
78 createdBy: "user",
79 creationSource: "web",
80 createdAt: now,
81 updatedAt: now,
82 archivedAt: null,
83 settledOverride: null,
84 settledAt: null,
85 lastVisitedAt: null,
86 deletedAt: null,
87 },
88 runs: [],
89 attempts: [],
90 nodes: [],
91 subagents: [],
92 providerSessions: [],
93 providerThreads: [],
94 providerTurns: [],
95 runtimeRequests: [],
96 messages: [],
97 plans: [],
98 turnItems: [],
99 checkpointScopes: [],
100 checkpoints: [],
101 contextHandoffs: [],
102 contextTransfers: [],
103 visibleTurnItems: [],
104 updatedAt: now,
105} as OrchestrationV2ThreadProjection;
106
107describe("applyOrchestrationV2ProjectionEvent", () => {
108 it("keeps live token usage when the terminal provider turn omits it", () => {
109 const providerTurnId = ProviderTurnId.make("provider-turn-reducer");
110 const running = {
111 id: providerTurnId,
112 providerThreadId: ProviderThreadId.make("provider-thread-reducer"),
113 nodeId: NodeId.make("provider-node-reducer"),
114 runAttemptId: null,
115 nativeTurnRef: null,
116 ordinal: 1,
117 status: "running" as const,
118 startedAt: now,
119 completedAt: null,
120 tokenUsage: {
121 usedTokens: 50_000,
122 maxTokens: 200_000,
123 updatedAt: "2026-08-29T00:00:00.000Z",
124 },
125 };
126 const projection = { ...emptyProjection, providerTurns: [running] };
127 const event = {
128 id: "event-provider-turn-terminal",
129 type: "provider-turn.updated",
130 threadId,
131 driver: "codex",
132 occurredAt: now,
133 payload: {
134 ...running,
135 status: "completed",
136 completedAt: now,
137 tokenUsage: undefined,
138 },
139 } as OrchestrationV2DomainEvent;
140
141 const next = applyOrchestrationV2ProjectionEvent(projection, event);
142
143 expect(next?.providerTurns[0]?.status).toBe("completed");
144 expect(next?.providerTurns[0]?.tokenUsage).toEqual(running.tokenUsage);
145 });
146
147 it("applies thread lifecycle payloads instead of leaving stale metadata", () => {
148 const archivedAt = DateTime.makeUnsafe("2026-06-20T01:00:00.000Z");
149 const event = {
150 id: "event-archive",
151 type: "thread.archived",
152 threadId,
153 occurredAt: archivedAt,
154 payload: { ...emptyProjection.thread, archivedAt, updatedAt: archivedAt },
155 } as OrchestrationV2DomainEvent;
156
157 const next = applyOrchestrationV2ProjectionEvent(emptyProjection, event);
158 expect(next?.thread.archivedAt).toEqual(archivedAt);
159 expect(next?.updatedAt).toEqual(archivedAt);
160 });
161
162 it("ignores events for another thread", () => {
163 const event = {
164 id: "event-other",
165 type: "thread.deleted",
166 threadId: ThreadId.make("thread-other"),
167 occurredAt: now,
168 payload: { ...emptyProjection.thread, id: ThreadId.make("thread-other"), deletedAt: now },
169 } as OrchestrationV2DomainEvent;
170
171 expect(applyOrchestrationV2ProjectionEvent(emptyProjection, event)).toBe(emptyProjection);
172 });
173
174 it("preserves visible row identity when run updates do not change membership", () => {
175 const item = commandItem("item-stable");
176 const visibleTurnItems = [
177 {
178 position: 0,
179 visibility: "local" as const,
180 sourceThreadId: threadId,
181 sourceItemId: item.id,
182 item,
183 },
184 ];
185 const projection = {
186 ...emptyProjection,
187 runs: [run],
188 turnItems: [item],
189 visibleTurnItems,
190 };
191 const event = {
192 id: "event-run-update",
193 type: "run.updated",
194 threadId,
195 runId,
196 occurredAt: now,
197 payload: { ...run, status: "completed" },
198 } as OrchestrationV2DomainEvent;
199
200 const next = applyOrchestrationV2ProjectionEvent(projection, event);
201 expect(next?.visibleTurnItems).toBe(visibleTurnItems);
202 expect(next?.visibleTurnItems[0]).toBe(visibleTurnItems[0]);
203 });
204
205 it("replaces only the updated visible item when membership is unchanged", () => {
206 const first = commandItem("item-first", "first");
207 const second = commandItem("item-second", "second");
208 const firstRow = {
209 position: 0,
210 visibility: "local" as const,
211 sourceThreadId: threadId,
212 sourceItemId: first.id,
213 item: first,
214 };
215 const secondRow = {
216 position: 1,
217 visibility: "local" as const,
218 sourceThreadId: threadId,
219 sourceItemId: second.id,
220 item: second,
221 };
222 const updated = commandItem("item-first", "streamed output");
223 const projection = {
224 ...emptyProjection,
225 runs: [run],
226 turnItems: [first, second],
227 visibleTurnItems: [firstRow, secondRow],
228 };
229 const event = {
230 id: "event-item-update",
231 type: "turn-item.updated",
232 threadId,
233 runId,
234 occurredAt: now,
235 payload: updated,
236 } as OrchestrationV2DomainEvent;
237
238 const next = applyOrchestrationV2ProjectionEvent(projection, event);
239 expect(next?.visibleTurnItems).not.toBe(projection.visibleTurnItems);
240 expect(next?.visibleTurnItems[0]).not.toBe(firstRow);
241 expect(next?.visibleTurnItems[0]?.item).toBe(updated);
242 expect(next?.visibleTurnItems[1]).toBe(secondRow);
243 });
244
245 it("inserts live turn items by authoritative ordinal", () => {
246 const queuedFuture = commandItem("item-queued-future", "queued", 300);
247 const activeAssistant = commandItem("item-active-assistant", "done", 201);
248 const queuedRow = {
249 position: 0,
250 visibility: "local" as const,
251 sourceThreadId: threadId,
252 sourceItemId: queuedFuture.id,
253 item: queuedFuture,
254 };
255 const projection = {
256 ...emptyProjection,
257 runs: [run],
258 turnItems: [queuedFuture],
259 visibleTurnItems: [queuedRow],
260 };
261 const event = {
262 id: "event-active-assistant",
263 type: "turn-item.updated",
264 threadId,
265 runId,
266 occurredAt: now,
267 payload: activeAssistant,
268 } as OrchestrationV2DomainEvent;
269
270 const next = applyOrchestrationV2ProjectionEvent(projection, event);
271 expect(next?.visibleTurnItems.map((row) => row.item.id)).toEqual([
272 activeAssistant.id,
273 queuedFuture.id,
274 ]);
275 expect(next?.visibleTurnItems.map((row) => row.position)).toEqual([0, 1]);
276 });
277
278 it("removes only hidden local items while preserving inherited rows", () => {
279 const inherited = commandItem("item-inherited");
280 const local = commandItem("item-local");
281 const inheritedRow = {
282 position: 0,
283 visibility: "inherited" as const,
284 sourceThreadId: ThreadId.make("thread-source"),
285 sourceItemId: inherited.id,
286 item: inherited,
287 };
288 const localRow = {
289 position: 1,
290 visibility: "local" as const,
291 sourceThreadId: threadId,
292 sourceItemId: local.id,
293 item: local,
294 };
295 const projection = {
296 ...emptyProjection,
297 runs: [run],
298 turnItems: [local],
299 visibleTurnItems: [inheritedRow, localRow],
300 };
301 const event = {
302 id: "event-run-rollback",
303 type: "run.updated",
304 threadId,
305 runId,
306 occurredAt: now,
307 payload: { ...run, status: "rolled_back" },
308 } as OrchestrationV2DomainEvent;
309
310 const next = applyOrchestrationV2ProjectionEvent(projection, event);
311 expect(next?.visibleTurnItems).toEqual([inheritedRow]);
312 expect(next?.visibleTurnItems[0]).toBe(inheritedRow);
313 });
314});