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

orchestration.test.ts

944 lines30.1 KB

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