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