Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4
5import {
6 DEFAULT_PROVIDER_INTERACTION_MODE,
7 DEFAULT_RUNTIME_MODE,
8 ModelSelection,
9 OrchestrationCommand,
10 OrchestrationEvent,
11 OrchestrationGetFullThreadDiffInput,
12 OrchestrationGetTurnDiffInput,
13 OrchestrationLatestTurn,
14 ProjectCreatedPayload,
15 ProjectMetaUpdatedPayload,
16 OrchestrationProposedPlan,
17 OrchestrationSession,
18 OrchestrationThread,
19 OrchestrationThreadShell,
20 ProjectCreateCommand,
21 ThreadMetaUpdatedPayload,
22 ThreadTurnStartCommand,
23 ThreadCreatedPayload,
24 ThreadTurnDiff,
25 ThreadTurnStartRequestedPayload,
26} from "./orchestration.ts";
27import { ProviderInstanceId } from "./providerInstance.ts";
28
29const decodeTurnDiffInput = Schema.decodeUnknownEffect(OrchestrationGetTurnDiffInput);
30const decodeFullThreadDiffInput = Schema.decodeUnknownEffect(OrchestrationGetFullThreadDiffInput);
31const decodeThreadTurnDiff = Schema.decodeUnknownEffect(ThreadTurnDiff);
32const decodeProjectCreateCommand = Schema.decodeUnknownEffect(ProjectCreateCommand);
33const decodeProjectCreatedPayload = Schema.decodeUnknownEffect(ProjectCreatedPayload);
34const decodeProjectMetaUpdatedPayload = Schema.decodeUnknownEffect(ProjectMetaUpdatedPayload);
35const decodeThreadTurnStartCommand = Schema.decodeUnknownEffect(ThreadTurnStartCommand);
36const decodeThreadTurnStartRequestedPayload = Schema.decodeUnknownEffect(
37 ThreadTurnStartRequestedPayload,
38);
39const decodeOrchestrationLatestTurn = Schema.decodeUnknownEffect(OrchestrationLatestTurn);
40const decodeOrchestrationProposedPlan = Schema.decodeUnknownEffect(OrchestrationProposedPlan);
41const decodeOrchestrationSession = Schema.decodeUnknownEffect(OrchestrationSession);
42const decodeOrchestrationThread = Schema.decodeUnknownEffect(OrchestrationThread);
43const decodeOrchestrationThreadShell = Schema.decodeUnknownEffect(OrchestrationThreadShell);
44const encodeThreadCreatedPayload = Schema.encodeEffect(ThreadCreatedPayload);
45
46function getOptionValue(
47 options: ReadonlyArray<{ id: string; value: unknown }> | undefined,
48 id: string,
49): unknown {
50 return options?.find((option) => option.id === id)?.value;
51}
52const decodeThreadCreatedPayload = Schema.decodeUnknownEffect(ThreadCreatedPayload);
53const decodeOrchestrationCommand = Schema.decodeUnknownEffect(OrchestrationCommand);
54const decodeOrchestrationEvent = Schema.decodeUnknownEffect(OrchestrationEvent);
55const decodeThreadMetaUpdatedPayload = Schema.decodeUnknownEffect(ThreadMetaUpdatedPayload);
56
57it.effect("parses turn diff input when fromTurnCount <= toTurnCount", () =>
58 Effect.gen(function* () {
59 const parsed = yield* decodeTurnDiffInput({
60 threadId: "thread-1",
61 fromTurnCount: 1,
62 toTurnCount: 2,
63 });
64 assert.strictEqual(parsed.fromTurnCount, 1);
65 assert.strictEqual(parsed.toTurnCount, 2);
66 }),
67);
68
69it.effect("parses turn diff input with whitespace ignoring enabled", () =>
70 Effect.gen(function* () {
71 const parsed = yield* decodeTurnDiffInput({
72 threadId: "thread-1",
73 fromTurnCount: 1,
74 toTurnCount: 2,
75 ignoreWhitespace: true,
76 });
77 assert.strictEqual(parsed.ignoreWhitespace, true);
78 }),
79);
80
81it.effect("parses full thread diff input with whitespace ignoring enabled", () =>
82 Effect.gen(function* () {
83 const parsed = yield* decodeFullThreadDiffInput({
84 threadId: "thread-1",
85 toTurnCount: 2,
86 ignoreWhitespace: true,
87 });
88 assert.strictEqual(parsed.ignoreWhitespace, true);
89 }),
90);
91
92it.effect("rejects turn diff input when fromTurnCount > toTurnCount", () =>
93 Effect.gen(function* () {
94 const result = yield* Effect.exit(
95 decodeTurnDiffInput({
96 threadId: "thread-1",
97 fromTurnCount: 3,
98 toTurnCount: 2,
99 }),
100 );
101 assert.strictEqual(result._tag, "Failure");
102 }),
103);
104
105it.effect("rejects thread turn diff when fromTurnCount > toTurnCount", () =>
106 Effect.gen(function* () {
107 const result = yield* Effect.exit(
108 decodeThreadTurnDiff({
109 threadId: "thread-1",
110 fromTurnCount: 3,
111 toTurnCount: 2,
112 diff: "patch",
113 }),
114 );
115 assert.strictEqual(result._tag, "Failure");
116 }),
117);
118
119it.effect("trims branded ids and command string fields at decode boundaries", () =>
120 Effect.gen(function* () {
121 const parsed = yield* decodeProjectCreateCommand({
122 type: "project.create",
123 commandId: " cmd-1 ",
124 projectId: " project-1 ",
125 title: " Project Title ",
126 workspaceRoot: " /tmp/workspace ",
127 defaultModelSelection: {
128 provider: "codex",
129 model: " gpt-5.2 ",
130 },
131 createdAt: "2026-01-01T00:00:00.000Z",
132 });
133 assert.strictEqual(parsed.commandId, "cmd-1");
134 assert.strictEqual(parsed.projectId, "project-1");
135 assert.strictEqual(parsed.title, "Project Title");
136 assert.strictEqual(parsed.workspaceRoot, "/tmp/workspace");
137 assert.strictEqual(parsed.createWorkspaceRootIfMissing, undefined);
138 assert.deepStrictEqual(parsed.defaultModelSelection, {
139 instanceId: ProviderInstanceId.make("codex"),
140 model: "gpt-5.2",
141 });
142 }),
143);
144
145it.effect("decodes project.create with createWorkspaceRootIfMissing enabled", () =>
146 Effect.gen(function* () {
147 const parsed = yield* decodeProjectCreateCommand({
148 type: "project.create",
149 commandId: "cmd-1",
150 projectId: "project-1",
151 title: "Project Title",
152 workspaceRoot: "/tmp/workspace",
153 createWorkspaceRootIfMissing: true,
154 createdAt: "2026-01-01T00:00:00.000Z",
155 });
156
157 assert.strictEqual(parsed.createWorkspaceRootIfMissing, true);
158 }),
159);
160
161it.effect("decodes historical project.created payloads with a default provider", () =>
162 Effect.gen(function* () {
163 const parsed = yield* decodeProjectCreatedPayload({
164 projectId: "project-1",
165 title: "Project Title",
166 workspaceRoot: "/tmp/workspace",
167 defaultModelSelection: {
168 provider: "codex",
169 model: "gpt-5.4",
170 },
171 scripts: [],
172 createdAt: "2026-01-01T00:00:00.000Z",
173 updatedAt: "2026-01-01T00:00:00.000Z",
174 });
175 assert.strictEqual(parsed.defaultModelSelection?.instanceId, "codex");
176 }),
177);
178
179it.effect("decodes project.meta-updated payloads with explicit default provider", () =>
180 Effect.gen(function* () {
181 const parsed = yield* decodeProjectMetaUpdatedPayload({
182 projectId: "project-1",
183 defaultModelSelection: {
184 provider: "claudeAgent",
185 model: "claude-opus-4-6",
186 },
187 updatedAt: "2026-01-01T00:00:00.000Z",
188 });
189 assert.strictEqual(parsed.defaultModelSelection?.instanceId, "claudeAgent");
190 }),
191);
192
193it.effect("rejects command fields that become empty after trim", () =>
194 Effect.gen(function* () {
195 const result = yield* Effect.exit(
196 decodeProjectCreateCommand({
197 type: "project.create",
198 commandId: "cmd-1",
199 projectId: "project-1",
200 title: " ",
201 workspaceRoot: "/tmp/workspace",
202 createdAt: "2026-01-01T00:00:00.000Z",
203 }),
204 );
205 assert.strictEqual(result._tag, "Failure");
206 }),
207);
208
209it.effect("decodes thread.turn.start defaults for provider and runtime mode", () =>
210 Effect.gen(function* () {
211 const parsed = yield* decodeThreadTurnStartCommand({
212 type: "thread.turn.start",
213 commandId: "cmd-turn-1",
214 threadId: "thread-1",
215 message: {
216 messageId: "msg-1",
217 role: "user",
218 text: "hello",
219 attachments: [],
220 },
221 createdAt: "2026-01-01T00:00:00.000Z",
222 });
223 assert.strictEqual(parsed.modelSelection, undefined);
224 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
225 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
226 }),
227);
228
229it.effect("preserves explicit provider and runtime mode in thread.turn.start", () =>
230 Effect.gen(function* () {
231 const parsed = yield* decodeThreadTurnStartCommand({
232 type: "thread.turn.start",
233 commandId: "cmd-turn-2",
234 threadId: "thread-1",
235 message: {
236 messageId: "msg-2",
237 role: "user",
238 text: "hello",
239 attachments: [],
240 },
241 modelSelection: {
242 provider: "codex",
243 model: "gpt-5.4",
244 },
245 runtimeMode: "full-access",
246 createdAt: "2026-01-01T00:00:00.000Z",
247 });
248 assert.strictEqual(parsed.modelSelection?.instanceId, "codex");
249 assert.strictEqual(parsed.runtimeMode, "full-access");
250 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
251 }),
252);
253
254it.effect("accepts bootstrap metadata in thread.turn.start", () =>
255 Effect.gen(function* () {
256 const parsed = yield* decodeThreadTurnStartCommand({
257 type: "thread.turn.start",
258 commandId: "cmd-turn-bootstrap",
259 threadId: "thread-1",
260 message: {
261 messageId: "msg-bootstrap",
262 role: "user",
263 text: "hello",
264 attachments: [],
265 },
266 bootstrap: {
267 createThread: {
268 projectId: "project-1",
269 title: "Bootstrap thread",
270 modelSelection: {
271 provider: "codex",
272 model: "gpt-5.4",
273 },
274 runtimeMode: "full-access",
275 interactionMode: "default",
276 branch: null,
277 worktreePath: null,
278 createdAt: "2026-01-01T00:00:00.000Z",
279 },
280 prepareWorktree: {
281 projectCwd: "/tmp/workspace",
282 baseBranch: "main",
283 branch: "t3code/example",
284 startFromOrigin: true,
285 },
286 runSetupScript: true,
287 },
288 createdAt: "2026-01-01T00:00:00.000Z",
289 });
290 assert.strictEqual(parsed.bootstrap?.createThread?.projectId, "project-1");
291 assert.strictEqual(parsed.bootstrap?.prepareWorktree?.baseBranch, "main");
292 assert.strictEqual(parsed.bootstrap?.prepareWorktree?.startFromOrigin, true);
293 assert.strictEqual(parsed.bootstrap?.runSetupScript, true);
294 }),
295);
296
297it.effect("decodes thread.created runtime mode for historical events", () =>
298 Effect.gen(function* () {
299 const parsed = yield* decodeThreadCreatedPayload({
300 threadId: "thread-1",
301 projectId: "project-1",
302 title: "Thread title",
303 modelSelection: {
304 provider: "codex",
305 model: "gpt-5.4",
306 },
307 interactionMode: "default",
308 branch: null,
309 worktreePath: null,
310 createdAt: "2026-01-01T00:00:00.000Z",
311 updatedAt: "2026-01-01T00:00:00.000Z",
312 });
313
314 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
315 assert.strictEqual(parsed.modelSelection.instanceId, "codex");
316 }),
317);
318
319it.effect("decodes thread.meta-updated payloads with explicit provider", () =>
320 Effect.gen(function* () {
321 const parsed = yield* decodeThreadMetaUpdatedPayload({
322 threadId: "thread-1",
323 modelSelection: {
324 provider: "claudeAgent",
325 model: "claude-opus-4-6",
326 },
327 updatedAt: "2026-01-01T00:00:00.000Z",
328 });
329 assert.strictEqual(parsed.modelSelection?.instanceId, "claudeAgent");
330 }),
331);
332
333it.effect("decodes thread archive and unarchive commands", () =>
334 Effect.gen(function* () {
335 const archive = yield* decodeOrchestrationCommand({
336 type: "thread.archive",
337 commandId: "cmd-archive-1",
338 threadId: "thread-1",
339 });
340 const unarchive = yield* decodeOrchestrationCommand({
341 type: "thread.unarchive",
342 commandId: "cmd-unarchive-1",
343 threadId: "thread-1",
344 });
345
346 assert.strictEqual(archive.type, "thread.archive");
347 assert.strictEqual(unarchive.type, "thread.unarchive");
348 }),
349);
350
351it.effect("decodes thread settle and unsettle commands", () =>
352 Effect.gen(function* () {
353 const settle = yield* decodeOrchestrationCommand({
354 type: "thread.settle",
355 commandId: "cmd-settle-1",
356 threadId: "thread-1",
357 });
358 const unsettle = yield* decodeOrchestrationCommand({
359 type: "thread.unsettle",
360 commandId: "cmd-unsettle-1",
361 threadId: "thread-1",
362 reason: "user",
363 });
364
365 assert.strictEqual(settle.type, "thread.settle");
366 assert.strictEqual(unsettle.type, "thread.unsettle");
367
368 // "activity" is server-owned: it exists on the event, never on the
369 // command, so a client cannot forge the neutral reset.
370 const forged = yield* decodeOrchestrationCommand({
371 type: "thread.unsettle",
372 commandId: "cmd-unsettle-2",
373 threadId: "thread-1",
374 reason: "activity",
375 }).pipe(Effect.flip);
376 assert.ok(forged);
377 }),
378);
379
380it.effect("defaults settled fields when decoding historical thread data", () =>
381 Effect.gen(function* () {
382 const common = {
383 id: "thread-1",
384 projectId: "project-1",
385 title: "Historical thread",
386 modelSelection: { provider: "codex", model: "gpt-5.4" },
387 runtimeMode: "full-access",
388 interactionMode: "default",
389 branch: null,
390 worktreePath: null,
391 latestTurn: null,
392 createdAt: "2026-01-01T00:00:00.000Z",
393 updatedAt: "2026-01-01T00:00:00.000Z",
394 archivedAt: null,
395 session: null,
396 };
397 const thread = yield* decodeOrchestrationThread({
398 ...common,
399 deletedAt: null,
400 messages: [],
401 proposedPlans: [],
402 activities: [],
403 checkpoints: [],
404 });
405 const shell = yield* decodeOrchestrationThreadShell({
406 ...common,
407 latestUserMessageAt: null,
408 hasPendingApprovals: false,
409 hasPendingUserInput: false,
410 hasActionableProposedPlan: false,
411 });
412
413 assert.strictEqual(thread.settledOverride, null);
414 assert.strictEqual(thread.settledAt, null);
415 assert.strictEqual(shell.settledOverride, null);
416 assert.strictEqual(shell.settledAt, null);
417 }),
418);
419
420it.effect("decodes thread archived and unarchived events", () =>
421 Effect.gen(function* () {
422 const archived = yield* decodeOrchestrationEvent({
423 sequence: 1,
424 eventId: "event-archive-1",
425 aggregateKind: "thread",
426 aggregateId: "thread-1",
427 type: "thread.archived",
428 occurredAt: "2026-01-01T00:00:00.000Z",
429 commandId: "cmd-archive-1",
430 causationEventId: null,
431 correlationId: "cmd-archive-1",
432 metadata: {},
433 payload: {
434 threadId: "thread-1",
435 archivedAt: "2026-01-01T00:00:00.000Z",
436 updatedAt: "2026-01-01T00:00:00.000Z",
437 },
438 });
439 const unarchived = yield* decodeOrchestrationEvent({
440 sequence: 2,
441 eventId: "event-unarchive-1",
442 aggregateKind: "thread",
443 aggregateId: "thread-1",
444 type: "thread.unarchived",
445 occurredAt: "2026-01-02T00:00:00.000Z",
446 commandId: "cmd-unarchive-1",
447 causationEventId: null,
448 correlationId: "cmd-unarchive-1",
449 metadata: {},
450 payload: {
451 threadId: "thread-1",
452 updatedAt: "2026-01-02T00:00:00.000Z",
453 },
454 });
455
456 if (archived.type !== "thread.archived") {
457 assert.fail(`Expected thread.archived event, received ${archived.type}.`);
458 }
459 assert.strictEqual(archived.payload.archivedAt, "2026-01-01T00:00:00.000Z");
460 assert.strictEqual(unarchived.type, "thread.unarchived");
461 }),
462);
463
464it.effect("decodes thread settled and unsettled events", () =>
465 Effect.gen(function* () {
466 const settled = yield* decodeOrchestrationEvent({
467 sequence: 1,
468 eventId: "event-settle-1",
469 aggregateKind: "thread",
470 aggregateId: "thread-1",
471 type: "thread.settled",
472 occurredAt: "2026-01-01T00:00:00.000Z",
473 commandId: "cmd-settle-1",
474 causationEventId: null,
475 correlationId: "cmd-settle-1",
476 metadata: {},
477 payload: {
478 threadId: "thread-1",
479 settledAt: "2026-01-01T00:00:00.000Z",
480 updatedAt: "2026-01-01T00:00:00.000Z",
481 },
482 });
483 const unsettled = yield* decodeOrchestrationEvent({
484 sequence: 2,
485 eventId: "event-unsettle-1",
486 aggregateKind: "thread",
487 aggregateId: "thread-1",
488 type: "thread.unsettled",
489 occurredAt: "2026-01-02T00:00:00.000Z",
490 commandId: "cmd-unsettle-1",
491 causationEventId: null,
492 correlationId: "cmd-unsettle-1",
493 metadata: {},
494 payload: {
495 threadId: "thread-1",
496 reason: "user",
497 updatedAt: "2026-01-02T00:00:00.000Z",
498 },
499 });
500
501 assert.strictEqual(settled.type, "thread.settled");
502 assert.strictEqual(unsettled.type, "thread.unsettled");
503 }),
504);
505
506it.effect("accepts provider-scoped model options in thread.turn.start", () =>
507 Effect.gen(function* () {
508 const parsed = yield* decodeThreadTurnStartCommand({
509 type: "thread.turn.start",
510 commandId: "cmd-turn-options",
511 threadId: "thread-1",
512 message: {
513 messageId: "msg-options",
514 role: "user",
515 text: "hello",
516 attachments: [],
517 },
518 modelSelection: {
519 provider: "codex",
520 model: "gpt-5.3-codex",
521 options: [
522 { id: "reasoningEffort", value: "high" },
523 { id: "fastMode", value: true },
524 ],
525 },
526 createdAt: "2026-01-01T00:00:00.000Z",
527 });
528 assert.strictEqual(parsed.modelSelection?.instanceId, "codex");
529 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "reasoningEffort"), "high");
530 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "fastMode"), true);
531 }),
532);
533
534it.effect("normalizes legacy object-shaped modelSelection.options on decode", () =>
535 Effect.gen(function* () {
536 const parsed = yield* decodeThreadCreatedPayload({
537 threadId: "thread-1",
538 projectId: "project-1",
539 title: "Legacy options thread",
540 modelSelection: {
541 provider: "claudeAgent",
542 model: "claude-opus-4-6",
543 options: {
544 effort: "max",
545 fastMode: true,
546 // Falsy/garbage entries are dropped, matching migration 026.
547 emptyStr: " ",
548 nullish: null,
549 nested: { foo: 1 },
550 },
551 },
552 branch: null,
553 worktreePath: null,
554 createdAt: "2026-01-01T00:00:00.000Z",
555 updatedAt: "2026-01-01T00:00:00.000Z",
556 });
557
558 assert.strictEqual(parsed.modelSelection.instanceId, ProviderInstanceId.make("claudeAgent"));
559 assert.deepStrictEqual(parsed.modelSelection.options, [
560 { id: "effort", value: "max" },
561 { id: "fastMode", value: true },
562 ]);
563 }),
564);
565
566it.effect("normalizes legacy object-shaped defaultModelSelection.options on decode", () =>
567 Effect.gen(function* () {
568 const parsed = yield* decodeProjectCreatedPayload({
569 projectId: "project-1",
570 title: "Legacy default project",
571 workspaceRoot: "/tmp/legacy",
572 defaultModelSelection: {
573 provider: "codex",
574 model: "gpt-5.4",
575 options: { reasoningEffort: "low" },
576 },
577 scripts: [],
578 createdAt: "2026-01-01T00:00:00.000Z",
579 updatedAt: "2026-01-01T00:00:00.000Z",
580 });
581
582 assert.deepStrictEqual(parsed.defaultModelSelection?.options, [
583 { id: "reasoningEffort", value: "low" },
584 ]);
585 }),
586);
587
588it.effect(
589 "normalizes legacy object-shaped options on decode and re-encodes as canonical array",
590 () =>
591 Effect.gen(function* () {
592 const decoded = yield* decodeThreadCreatedPayload({
593 threadId: "thread-1",
594 projectId: "project-1",
595 title: "Round trip thread",
596 modelSelection: {
597 provider: "codex",
598 model: "gpt-5.4",
599 options: { fastMode: true },
600 },
601 branch: null,
602 worktreePath: null,
603 createdAt: "2026-01-01T00:00:00.000Z",
604 updatedAt: "2026-01-01T00:00:00.000Z",
605 });
606
607 const encoded = yield* encodeThreadCreatedPayload(decoded);
608 assert.deepStrictEqual(encoded.modelSelection.options, [{ id: "fastMode", value: true }]);
609 }),
610);
611
612it.effect("accepts a title seed in thread.turn.start", () =>
613 Effect.gen(function* () {
614 const parsed = yield* decodeThreadTurnStartCommand({
615 type: "thread.turn.start",
616 commandId: "cmd-turn-title-seed",
617 threadId: "thread-1",
618 message: {
619 messageId: "msg-title-seed",
620 role: "user",
621 text: "hello",
622 attachments: [],
623 },
624 titleSeed: "Investigate reconnect failures",
625 createdAt: "2026-01-01T00:00:00.000Z",
626 });
627 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
628 }),
629);
630
631it.effect("accepts a source proposed plan reference in thread.turn.start", () =>
632 Effect.gen(function* () {
633 const parsed = yield* decodeThreadTurnStartCommand({
634 type: "thread.turn.start",
635 commandId: "cmd-turn-source-plan",
636 threadId: "thread-2",
637 message: {
638 messageId: "msg-source-plan",
639 role: "user",
640 text: "implement this",
641 attachments: [],
642 },
643 sourceProposedPlan: {
644 threadId: "thread-1",
645 planId: "plan-1",
646 },
647 createdAt: "2026-01-01T00:00:00.000Z",
648 });
649 assert.deepStrictEqual(parsed.sourceProposedPlan, {
650 threadId: "thread-1",
651 planId: "plan-1",
652 });
653 }),
654);
655
656it.effect(
657 "decodes thread.turn-start-requested defaults for provider, runtime mode, and interaction mode",
658 () =>
659 Effect.gen(function* () {
660 const parsed = yield* decodeThreadTurnStartRequestedPayload({
661 threadId: "thread-1",
662 messageId: "msg-1",
663 createdAt: "2026-01-01T00:00:00.000Z",
664 });
665 assert.strictEqual(parsed.modelSelection, undefined);
666 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
667 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
668 assert.strictEqual(parsed.sourceProposedPlan, undefined);
669 }),
670);
671
672it.effect("decodes thread.turn-start-requested source proposed plan metadata when present", () =>
673 Effect.gen(function* () {
674 const parsed = yield* decodeThreadTurnStartRequestedPayload({
675 threadId: "thread-2",
676 messageId: "msg-2",
677 sourceProposedPlan: {
678 threadId: "thread-1",
679 planId: "plan-1",
680 },
681 createdAt: "2026-01-01T00:00:00.000Z",
682 });
683 assert.deepStrictEqual(parsed.sourceProposedPlan, {
684 threadId: "thread-1",
685 planId: "plan-1",
686 });
687 }),
688);
689
690it.effect("decodes thread.turn-start-requested title seed when present", () =>
691 Effect.gen(function* () {
692 const parsed = yield* decodeThreadTurnStartRequestedPayload({
693 threadId: "thread-2",
694 messageId: "msg-2",
695 titleSeed: "Investigate reconnect failures",
696 createdAt: "2026-01-01T00:00:00.000Z",
697 });
698 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
699 }),
700);
701
702it.effect("decodes latest turn source proposed plan metadata when present", () =>
703 Effect.gen(function* () {
704 const parsed = yield* decodeOrchestrationLatestTurn({
705 turnId: "turn-2",
706 state: "running",
707 requestedAt: "2026-01-01T00:00:00.000Z",
708 startedAt: "2026-01-01T00:00:01.000Z",
709 completedAt: null,
710 assistantMessageId: null,
711 sourceProposedPlan: {
712 threadId: "thread-1",
713 planId: "plan-1",
714 },
715 });
716 assert.deepStrictEqual(parsed.sourceProposedPlan, {
717 threadId: "thread-1",
718 planId: "plan-1",
719 });
720 }),
721);
722
723it.effect("decodes orchestration session runtime mode defaults", () =>
724 Effect.gen(function* () {
725 const parsed = yield* decodeOrchestrationSession({
726 threadId: "thread-1",
727 status: "idle",
728 providerName: null,
729 providerSessionId: null,
730 providerThreadId: null,
731 activeTurnId: null,
732 lastError: null,
733 updatedAt: "2026-01-01T00:00:00.000Z",
734 });
735 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
736 }),
737);
738
739it.effect("defaults proposed plan implementation metadata for historical rows", () =>
740 Effect.gen(function* () {
741 const parsed = yield* decodeOrchestrationProposedPlan({
742 id: "plan-1",
743 turnId: "turn-1",
744 planMarkdown: "# Plan",
745 createdAt: "2026-01-01T00:00:00.000Z",
746 updatedAt: "2026-01-01T00:00:00.000Z",
747 });
748 assert.strictEqual(parsed.implementedAt, null);
749 assert.strictEqual(parsed.implementationThreadId, null);
750 }),
751);
752
753it.effect("preserves proposed plan implementation metadata when present", () =>
754 Effect.gen(function* () {
755 const parsed = yield* decodeOrchestrationProposedPlan({
756 id: "plan-2",
757 turnId: "turn-2",
758 planMarkdown: "# Plan",
759 implementedAt: "2026-01-02T00:00:00.000Z",
760 implementationThreadId: "thread-2",
761 createdAt: "2026-01-01T00:00:00.000Z",
762 updatedAt: "2026-01-02T00:00:00.000Z",
763 });
764 assert.strictEqual(parsed.implementedAt, "2026-01-02T00:00:00.000Z");
765 assert.strictEqual(parsed.implementationThreadId, "thread-2");
766 }),
767);
768
769// ── ModelSelection: instance-keyed wire shape + legacy decoder ────────
770//
771// `ModelSelection` is routing-keyed on `instanceId` — never a driver kind.
772// Persisted and in-flight payloads from pre-instance builds carry a
773// `provider` field whose value was a driver kind; those payloads are migrated
774// at the wire boundary by
775// promoting `provider` to the default instance id for that driver
776// (built-in drivers use the driver kind slug as their default instance id, so
777// the migration is a 1:1 rename).
778//
779// These tests pin the rollback/fork tolerance invariant: legacy payloads
780// decode cleanly for fork-provided drivers, and the decoded form uses
781// `instanceId` uniformly regardless of origin.
782
783const decodeModelSelection = Schema.decodeUnknownEffect(ModelSelection);
784const encodeModelSelection = Schema.encodeUnknownEffect(ModelSelection);
785
786it.effect("ModelSelection migrates legacy `provider` field to `instanceId`", () =>
787 Effect.gen(function* () {
788 const parsed = yield* decodeModelSelection({
789 provider: "codex",
790 model: "gpt-5-codex",
791 options: [{ id: "reasoningEffort", value: "high" }],
792 });
793 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex"));
794 assert.strictEqual(parsed.model, "gpt-5-codex");
795 assert.deepStrictEqual(parsed.options, [{ id: "reasoningEffort", value: "high" }]);
796 }),
797);
798
799it.effect("ModelSelection accepts an explicit instanceId routing key", () =>
800 Effect.gen(function* () {
801 const parsed = yield* decodeModelSelection({
802 instanceId: "codex_personal",
803 model: "gpt-5-codex",
804 });
805 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex_personal"));
806 }),
807);
808
809it.effect("ModelSelection prefers explicit instanceId over legacy provider", () =>
810 Effect.gen(function* () {
811 const parsed = yield* decodeModelSelection({
812 provider: "codex",
813 instanceId: "codex_personal",
814 model: "gpt-5-codex",
815 });
816 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex_personal"));
817 }),
818);
819
820it.effect(
821 "ModelSelection decodes unknown driver kinds via legacy provider (rollback / fork invariant)",
822 () =>
823 Effect.gen(function* () {
824 const parsed = yield* decodeModelSelection({
825 provider: "ollama",
826 model: "llama3:70b",
827 options: [{ id: "temperature", value: "0.4" }],
828 });
829 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("ollama"));
830 assert.strictEqual(parsed.model, "llama3:70b");
831 }),
832);
833
834it.effect("ModelSelection encodes to the canonical instanceId wire form", () =>
835 Effect.gen(function* () {
836 const decoded = yield* decodeModelSelection({
837 provider: "ollama",
838 model: "llama3:70b",
839 options: [{ id: "temperature", value: "0.4" }],
840 });
841 const encoded = yield* encodeModelSelection(decoded);
842 assert.deepStrictEqual(encoded, {
843 instanceId: "ollama",
844 model: "llama3:70b",
845 options: [{ id: "temperature", value: "0.4" }],
846 });
847 }),
848);
849
850it.effect("ModelSelection rejects malformed instance ids", () =>
851 Effect.gen(function* () {
852 const result = yield* Effect.exit(
853 decodeModelSelection({
854 instanceId: "1invalid", // must start with a letter
855 model: "x",
856 }),
857 );
858 assert.strictEqual(result._tag, "Failure");
859 }),
860);