Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import {
4 CommandId,
5 EventId,
6 MessageId,
7 ProjectId,
8 ProviderInstanceId,
9 ThreadId,
10} from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12import * as TestClock from "effect/testing/TestClock";
13
14import { decideOrchestrationCommand } from "./decider.ts";
15import { createEmptyReadModel, projectEvent } from "./projector.ts";
16
17it.layer(NodeServices.layer)("thread history import", (it) => {
18 it.effect("marks imported thread creation without changing live creation", () =>
19 Effect.gen(function* () {
20 const createdAt = "2026-08-24T10:00:00.000Z";
21 const projectId = ProjectId.make("project-1");
22 const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {
23 sequence: 1,
24 eventId: EventId.make("event-project-created"),
25 aggregateKind: "project",
26 aggregateId: projectId,
27 type: "project.created",
28 occurredAt: createdAt,
29 commandId: CommandId.make("command-project-created"),
30 causationEventId: null,
31 correlationId: CommandId.make("command-project-created"),
32 metadata: {},
33 payload: {
34 projectId,
35 title: "Project",
36 workspaceRoot: "/tmp/project",
37 defaultModelSelection: null,
38 scripts: [],
39 createdAt,
40 updatedAt: createdAt,
41 },
42 });
43 const makeCreateCommand = (threadId: ThreadId) => ({
44 type: "thread.create" as const,
45 commandId: CommandId.make(`command-create-${threadId}`),
46 threadId,
47 projectId,
48 title: "Imported thread",
49 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
50 runtimeMode: "full-access" as const,
51 interactionMode: "default" as const,
52 branch: null,
53 worktreePath: null,
54 createdAt,
55 });
56
57 const imported = yield* decideOrchestrationCommand({
58 command: {
59 ...makeCreateCommand(ThreadId.make("import:codex:session-1")),
60 historyImport: true,
61 },
62 readModel,
63 });
64 const live = yield* decideOrchestrationCommand({
65 command: makeCreateCommand(ThreadId.make("live-thread")),
66 readModel,
67 });
68
69 expect(imported).toMatchObject({
70 type: "thread.created",
71 metadata: { historyImport: true },
72 });
73 expect(live).toMatchObject({ type: "thread.created" });
74 expect(live).not.toMatchObject({ metadata: { historyImport: true } });
75 }),
76 );
77
78 it.effect("settles imported messages at the latest absolute timestamp", () =>
79 Effect.gen(function* () {
80 const createdAt = "2026-08-24T10:30:00.000+02:00";
81 const threadId = ThreadId.make("import:codex:session-1");
82 const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {
83 sequence: 1,
84 eventId: EventId.make("event-thread-created"),
85 aggregateKind: "thread",
86 aggregateId: threadId,
87 type: "thread.created",
88 occurredAt: createdAt,
89 commandId: CommandId.make("command-thread-created"),
90 causationEventId: null,
91 correlationId: CommandId.make("command-thread-created"),
92 metadata: {},
93 payload: {
94 threadId,
95 projectId: ProjectId.make("project-1"),
96 title: "Imported thread",
97 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
98 runtimeMode: "full-access",
99 interactionMode: "default",
100 branch: null,
101 worktreePath: null,
102 createdAt,
103 updatedAt: createdAt,
104 },
105 });
106
107 const events = yield* decideOrchestrationCommand({
108 command: {
109 type: "thread.history.import",
110 commandId: CommandId.make("command-import-history"),
111 threadId,
112 messages: [
113 {
114 messageId: MessageId.make(`${threadId}:000000`),
115 role: "user",
116 text: "Fix the bug",
117 createdAt,
118 },
119 {
120 messageId: MessageId.make(`${threadId}:000001`),
121 role: "assistant",
122 text: "Fixed",
123 createdAt: "2026-08-24T09:00:00.000Z",
124 },
125 ],
126 },
127 readModel,
128 });
129
130 expect(events).toMatchObject([
131 {
132 type: "thread.message-sent",
133 metadata: { historyImport: true },
134 payload: { role: "user", text: "Fix the bug", turnId: null, streaming: false },
135 },
136 {
137 type: "thread.message-sent",
138 metadata: { historyImport: true },
139 payload: { role: "assistant", text: "Fixed", turnId: null, streaming: false },
140 },
141 {
142 type: "thread.settled",
143 metadata: { historyImport: true },
144 occurredAt: "2026-08-24T09:00:00.000Z",
145 payload: {
146 settledAt: "2026-08-24T09:00:00.000Z",
147 updatedAt: "2026-08-24T09:00:00.000Z",
148 },
149 },
150 ]);
151
152 let projected = readModel;
153 const plannedEvents = Array.isArray(events) ? events : [events];
154 for (const [index, event] of plannedEvents.entries()) {
155 projected = yield* projectEvent(projected, { ...event, sequence: index + 2 });
156 }
157 projected = yield* projectEvent(projected, {
158 sequence: 5,
159 eventId: EventId.make("event-import-reverted"),
160 aggregateKind: "thread",
161 aggregateId: threadId,
162 type: "thread.reverted",
163 occurredAt: "2026-08-24T10:02:00.000Z",
164 commandId: CommandId.make("command-import-reverted"),
165 causationEventId: null,
166 correlationId: CommandId.make("command-import-reverted"),
167 metadata: {},
168 payload: { threadId, turnCount: 0 },
169 });
170 expect(projected.threads[0]?.messages.map((message) => message.text)).toEqual([
171 "Fix the bug",
172 "Fixed",
173 ]);
174 }),
175 );
176
177 it.effect("allows a thread with a newly imported user message to be settled", () =>
178 Effect.gen(function* () {
179 const createdAt = "2026-08-24T10:00:00.000Z";
180 yield* TestClock.setTime(Date.parse("2026-08-24T10:00:30.000Z"));
181 const threadId = ThreadId.make("import:codex:session-1");
182 const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {
183 sequence: 1,
184 eventId: EventId.make("event-import-thread-created"),
185 aggregateKind: "thread",
186 aggregateId: threadId,
187 type: "thread.created",
188 occurredAt: createdAt,
189 commandId: CommandId.make("command-import-thread-created"),
190 causationEventId: null,
191 correlationId: CommandId.make("command-import-thread-created"),
192 metadata: {},
193 payload: {
194 threadId,
195 projectId: ProjectId.make("project-1"),
196 title: "Imported thread",
197 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
198 runtimeMode: "full-access",
199 interactionMode: "default",
200 branch: null,
201 worktreePath: null,
202 createdAt,
203 updatedAt: createdAt,
204 },
205 });
206 const readModel = yield* projectEvent(withThread, {
207 sequence: 2,
208 eventId: EventId.make("event-import-user-message"),
209 aggregateKind: "thread",
210 aggregateId: threadId,
211 type: "thread.message-sent",
212 occurredAt: createdAt,
213 commandId: CommandId.make("command-import-user-message"),
214 causationEventId: null,
215 correlationId: CommandId.make("command-import-user-message"),
216 metadata: { historyImport: true },
217 payload: {
218 threadId,
219 messageId: MessageId.make("import:codex:session-1:0"),
220 role: "user",
221 text: "Existing prompt",
222 turnId: null,
223 streaming: false,
224 createdAt,
225 updatedAt: createdAt,
226 },
227 });
228
229 const result = yield* decideOrchestrationCommand({
230 command: {
231 type: "thread.settle",
232 commandId: CommandId.make("command-settle-imported-thread"),
233 threadId,
234 },
235 readModel,
236 });
237
238 expect(result).toMatchObject({ type: "thread.settled" });
239 }),
240 );
241
242 it.effect("rejects history import after a client message reaches the thread", () =>
243 Effect.gen(function* () {
244 const createdAt = "2026-08-24T10:00:00.000Z";
245 const liveMessageAt = "2026-08-24T10:02:00.000Z";
246 const threadId = ThreadId.make("import:codex:client-race");
247 const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {
248 sequence: 1,
249 eventId: EventId.make("event-client-race-thread-created"),
250 aggregateKind: "thread",
251 aggregateId: threadId,
252 type: "thread.created",
253 occurredAt: createdAt,
254 commandId: CommandId.make("command-client-race-thread-created"),
255 causationEventId: null,
256 correlationId: CommandId.make("command-client-race-thread-created"),
257 metadata: {},
258 payload: {
259 threadId,
260 projectId: ProjectId.make("project-1"),
261 title: "Imported thread",
262 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
263 runtimeMode: "full-access",
264 interactionMode: "default",
265 branch: null,
266 worktreePath: null,
267 createdAt,
268 updatedAt: createdAt,
269 },
270 });
271 const readModel = yield* projectEvent(withThread, {
272 sequence: 2,
273 eventId: EventId.make("event-client-race-message"),
274 aggregateKind: "thread",
275 aggregateId: threadId,
276 type: "thread.message-sent",
277 occurredAt: liveMessageAt,
278 commandId: CommandId.make("command-client-race-message"),
279 causationEventId: null,
280 correlationId: CommandId.make("command-client-race-message"),
281 metadata: {},
282 payload: {
283 threadId,
284 messageId: MessageId.make("client-race-message"),
285 role: "user",
286 text: "Start live work",
287 turnId: null,
288 streaming: false,
289 createdAt: liveMessageAt,
290 updatedAt: liveMessageAt,
291 },
292 });
293
294 const error = yield* Effect.flip(
295 decideOrchestrationCommand({
296 command: {
297 type: "thread.history.import",
298 commandId: CommandId.make("command-client-race-import"),
299 threadId,
300 messages: [
301 {
302 messageId: MessageId.make(`${threadId}:000000`),
303 role: "user",
304 text: "Old work",
305 createdAt,
306 },
307 ],
308 },
309 readModel,
310 }),
311 );
312
313 expect(error._tag).toBe("OrchestrationCommandInvariantError");
314 expect(error.message).toContain("must be active and empty");
315 expect(readModel.threads[0]?.updatedAt).toBe(liveMessageAt);
316 }),
317 );
318
319 for (const requestKind of ["approval.requested", "user-input.requested"] as const) {
320 it.effect(`rejects history import with an open ${requestKind} activity`, () =>
321 Effect.gen(function* () {
322 const createdAt = "2026-08-24T10:00:00.000Z";
323 const threadId = ThreadId.make(`import:codex:${requestKind}`);
324 const withThread = yield* projectEvent(createEmptyReadModel(createdAt), {
325 sequence: 1,
326 eventId: EventId.make(`event-${requestKind}-thread-created`),
327 aggregateKind: "thread",
328 aggregateId: threadId,
329 type: "thread.created",
330 occurredAt: createdAt,
331 commandId: CommandId.make(`command-${requestKind}-thread-created`),
332 causationEventId: null,
333 correlationId: CommandId.make(`command-${requestKind}-thread-created`),
334 metadata: {},
335 payload: {
336 threadId,
337 projectId: ProjectId.make("project-1"),
338 title: "Imported thread",
339 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
340 runtimeMode: "full-access",
341 interactionMode: "default",
342 branch: null,
343 worktreePath: null,
344 createdAt,
345 updatedAt: createdAt,
346 },
347 });
348 const readModel = yield* projectEvent(withThread, {
349 sequence: 2,
350 eventId: EventId.make(`event-${requestKind}`),
351 aggregateKind: "thread",
352 aggregateId: threadId,
353 type: "thread.activity-appended",
354 occurredAt: createdAt,
355 commandId: CommandId.make(`command-${requestKind}`),
356 causationEventId: null,
357 correlationId: CommandId.make(`command-${requestKind}`),
358 metadata: {},
359 payload: {
360 threadId,
361 activity: {
362 id: EventId.make(`activity-${requestKind}`),
363 tone: "approval",
364 kind: requestKind,
365 summary: "Pending request",
366 payload: { requestId: "request-1" },
367 turnId: null,
368 createdAt,
369 },
370 },
371 });
372
373 const error = yield* Effect.flip(
374 decideOrchestrationCommand({
375 command: {
376 type: "thread.history.import",
377 commandId: CommandId.make(`command-import-${requestKind}`),
378 threadId,
379 messages: [
380 {
381 messageId: MessageId.make(`${threadId}:000000`),
382 role: "user",
383 text: "Old work",
384 createdAt,
385 },
386 ],
387 },
388 readModel,
389 }),
390 );
391
392 expect(error._tag).toBe("OrchestrationCommandInvariantError");
393 expect(error.message).toContain("must be active and empty");
394 }),
395 );
396 }
397
398 it.effect("rejects a live user message in the imported-session namespace", () =>
399 Effect.gen(function* () {
400 const createdAt = "2026-08-24T10:00:00.000Z";
401 const threadId = ThreadId.make("thread-live-message");
402 const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {
403 sequence: 1,
404 eventId: EventId.make("event-live-thread-created"),
405 aggregateKind: "thread",
406 aggregateId: threadId,
407 type: "thread.created",
408 occurredAt: createdAt,
409 commandId: CommandId.make("command-live-thread-created"),
410 causationEventId: null,
411 correlationId: CommandId.make("command-live-thread-created"),
412 metadata: {},
413 payload: {
414 threadId,
415 projectId: ProjectId.make("project-1"),
416 title: "Live thread",
417 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
418 runtimeMode: "full-access",
419 interactionMode: "default",
420 branch: null,
421 worktreePath: null,
422 createdAt,
423 updatedAt: createdAt,
424 },
425 });
426
427 const error = yield* Effect.flip(
428 decideOrchestrationCommand({
429 command: {
430 type: "thread.turn.start",
431 commandId: CommandId.make("command-live-import-id"),
432 threadId,
433 message: {
434 messageId: MessageId.make("import:forged-live-message"),
435 role: "user",
436 text: "Live work",
437 attachments: [],
438 },
439 runtimeMode: "full-access",
440 interactionMode: "default",
441 createdAt,
442 },
443 readModel,
444 }),
445 );
446
447 expect(error._tag).toBe("OrchestrationCommandInvariantError");
448 expect(error.message).toContain("reserved imported-session namespace");
449 }),
450 );
451
452 it.effect("rejects live assistant messages in the imported-session namespace", () =>
453 Effect.gen(function* () {
454 const createdAt = "2026-08-24T10:00:00.000Z";
455 const threadId = ThreadId.make("thread-live-assistant-message");
456 const readModel = yield* projectEvent(createEmptyReadModel(createdAt), {
457 sequence: 1,
458 eventId: EventId.make("event-live-assistant-thread-created"),
459 aggregateKind: "thread",
460 aggregateId: threadId,
461 type: "thread.created",
462 occurredAt: createdAt,
463 commandId: CommandId.make("command-live-assistant-thread-created"),
464 causationEventId: null,
465 correlationId: CommandId.make("command-live-assistant-thread-created"),
466 metadata: {},
467 payload: {
468 threadId,
469 projectId: ProjectId.make("project-1"),
470 title: "Live thread",
471 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
472 runtimeMode: "full-access",
473 interactionMode: "default",
474 branch: null,
475 worktreePath: null,
476 createdAt,
477 updatedAt: createdAt,
478 },
479 });
480
481 for (const commandType of [
482 "thread.message.assistant.delta",
483 "thread.message.assistant.complete",
484 ] as const) {
485 const command =
486 commandType === "thread.message.assistant.delta"
487 ? {
488 type: commandType,
489 commandId: CommandId.make("command-live-assistant-delta-import-id"),
490 threadId,
491 messageId: MessageId.make("import:forged-live-assistant-message"),
492 delta: "Live work",
493 createdAt,
494 }
495 : {
496 type: commandType,
497 commandId: CommandId.make("command-live-assistant-complete-import-id"),
498 threadId,
499 messageId: MessageId.make("import:forged-live-assistant-message"),
500 createdAt,
501 };
502 const error = yield* Effect.flip(decideOrchestrationCommand({ command, readModel }));
503
504 expect(error._tag).toBe("OrchestrationCommandInvariantError");
505 expect(error.message).toContain("reserved imported-session namespace");
506 }
507 }),
508 );
509});