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

orchestration.test.ts

964 lines30.7 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 durable after-current delivery in thread.turn.start", () =>
256 Effect.gen(function* () {
257 const parsed = yield* decodeThreadTurnStartCommand({
258 type: "thread.turn.start",
259 commandId: "cmd-turn-after-current",
260 threadId: "thread-1",
261 message: {
262 messageId: "msg-after-current",
263 role: "user",
264 text: "run next",
265 attachments: [],
266 },
267 deliveryMode: "after-current",
268 createdAt: "2026-01-01T00:00:00.000Z",
269 });
270​
271 assert.strictEqual(parsed.deliveryMode, "after-current");
272 }),
273);
274​
275it.effect("accepts bootstrap metadata in thread.turn.start", () =>
276 Effect.gen(function* () {
277 const parsed = yield* decodeThreadTurnStartCommand({
278 type: "thread.turn.start",
279 commandId: "cmd-turn-bootstrap",
280 threadId: "thread-1",
281 message: {
282 messageId: "msg-bootstrap",
283 role: "user",
284 text: "hello",
285 attachments: [],
286 },
287 bootstrap: {
288 createThread: {
289 projectId: "project-1",
290 title: "Bootstrap thread",
291 modelSelection: {
292 provider: "codex",
293 model: "gpt-5.4",
294 },
295 runtimeMode: "full-access",
296 interactionMode: "default",
297 branch: null,
298 worktreePath: null,
299 createdAt: "2026-01-01T00:00:00.000Z",
300 },
301 prepareWorktree: {
302 projectCwd: "/tmp/workspace",
303 baseBranch: "main",
304 branch: "t3code/example",
305 startFromOrigin: true,
306 },
307 runSetupScript: true,
308 },
309 createdAt: "2026-01-01T00:00:00.000Z",
310 });
311 assert.strictEqual(parsed.bootstrap?.createThread?.projectId, "project-1");
312 assert.strictEqual(parsed.bootstrap?.prepareWorktree?.baseBranch, "main");
313 assert.strictEqual(parsed.bootstrap?.prepareWorktree?.startFromOrigin, true);
314 assert.strictEqual(parsed.bootstrap?.runSetupScript, true);
315 }),
316);
317​
318it.effect("decodes thread.created runtime mode for historical events", () =>
319 Effect.gen(function* () {
320 const parsed = yield* decodeThreadCreatedPayload({
321 threadId: "thread-1",
322 projectId: "project-1",
323 title: "Thread title",
324 modelSelection: {
325 provider: "codex",
326 model: "gpt-5.4",
327 },
328 interactionMode: "default",
329 branch: null,
330 worktreePath: null,
331 createdAt: "2026-01-01T00:00:00.000Z",
332 updatedAt: "2026-01-01T00:00:00.000Z",
333 });
334​
335 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
336 assert.strictEqual(parsed.modelSelection.instanceId, "codex");
337 }),
338);
339​
340it.effect("decodes thread.meta-updated payloads with explicit provider", () =>
341 Effect.gen(function* () {
342 const parsed = yield* decodeThreadMetaUpdatedPayload({
343 threadId: "thread-1",
344 regenerateTitle: true,
345 previousTitle: "Previous title",
346 titleRegeneration: {
347 requestId: "cmd-title-regenerate",
348 startedAt: "2026-01-01T00:00:00.000Z",
349 },
350 modelSelection: {
351 provider: "claudeAgent",
352 model: "claude-opus-4-6",
353 },
354 updatedAt: "2026-01-01T00:00:00.000Z",
355 });
356 assert.strictEqual(parsed.previousTitle, "Previous title");
357 assert.strictEqual(parsed.titleRegeneration?.requestId, "cmd-title-regenerate");
358 assert.strictEqual(parsed.modelSelection?.instanceId, "claudeAgent");
359 }),
360);
361​
362it.effect("decodes thread archive and unarchive commands", () =>
363 Effect.gen(function* () {
364 const archive = yield* decodeOrchestrationCommand({
365 type: "thread.archive",
366 commandId: "cmd-archive-1",
367 threadId: "thread-1",
368 });
369 const unarchive = yield* decodeOrchestrationCommand({
370 type: "thread.unarchive",
371 commandId: "cmd-unarchive-1",
372 threadId: "thread-1",
373 });
374​
375 assert.strictEqual(archive.type, "thread.archive");
376 assert.strictEqual(unarchive.type, "thread.unarchive");
377 }),
378);
379​
380it.effect("decodes thread settle and unsettle commands", () =>
381 Effect.gen(function* () {
382 const settle = yield* decodeOrchestrationCommand({
383 type: "thread.settle",
384 commandId: "cmd-settle-1",
385 threadId: "thread-1",
386 });
387 const unsettle = yield* decodeOrchestrationCommand({
388 type: "thread.unsettle",
389 commandId: "cmd-unsettle-1",
390 threadId: "thread-1",
391 reason: "user",
392 });
393​
394 assert.strictEqual(settle.type, "thread.settle");
395 assert.strictEqual(unsettle.type, "thread.unsettle");
396​
397 // "activity" is server-owned: it exists on the event, never on the
398 // command, so a client cannot forge the neutral reset.
399 const forged = yield* decodeOrchestrationCommand({
400 type: "thread.unsettle",
401 commandId: "cmd-unsettle-2",
402 threadId: "thread-1",
403 reason: "activity",
404 }).pipe(Effect.flip);
405 assert.ok(forged);
406 }),
407);
408​
409it.effect("defaults settled fields when decoding historical thread data", () =>
410 Effect.gen(function* () {
411 const common = {
412 id: "thread-1",
413 projectId: "project-1",
414 title: "Historical thread",
415 modelSelection: { provider: "codex", model: "gpt-5.4" },
416 runtimeMode: "full-access",
417 interactionMode: "default",
418 branch: null,
419 worktreePath: null,
420 latestTurn: null,
421 createdAt: "2026-01-01T00:00:00.000Z",
422 updatedAt: "2026-01-01T00:00:00.000Z",
423 archivedAt: null,
424 session: null,
425 };
426 const thread = yield* decodeOrchestrationThread({
427 ...common,
428 deletedAt: null,
429 messages: [],
430 proposedPlans: [],
431 activities: [],
432 checkpoints: [],
433 });
434 const shell = yield* decodeOrchestrationThreadShell({
435 ...common,
436 latestUserMessageAt: null,
437 hasPendingApprovals: false,
438 hasPendingUserInput: false,
439 hasActionableProposedPlan: false,
440 });
441​
442 assert.strictEqual(thread.settledOverride, null);
443 assert.strictEqual(thread.settledAt, null);
444 assert.strictEqual(shell.settledOverride, null);
445 assert.strictEqual(shell.settledAt, null);
446 }),
447);
448​
449it.effect("decodes thread archived and unarchived events", () =>
450 Effect.gen(function* () {
451 const archived = yield* decodeOrchestrationEvent({
452 sequence: 1,
453 eventId: "event-archive-1",
454 aggregateKind: "thread",
455 aggregateId: "thread-1",
456 type: "thread.archived",
457 occurredAt: "2026-01-01T00:00:00.000Z",
458 commandId: "cmd-archive-1",
459 causationEventId: null,
460 correlationId: "cmd-archive-1",
461 metadata: {},
462 payload: {
463 threadId: "thread-1",
464 archivedAt: "2026-01-01T00:00:00.000Z",
465 updatedAt: "2026-01-01T00:00:00.000Z",
466 },
467 });
468 const unarchived = yield* decodeOrchestrationEvent({
469 sequence: 2,
470 eventId: "event-unarchive-1",
471 aggregateKind: "thread",
472 aggregateId: "thread-1",
473 type: "thread.unarchived",
474 occurredAt: "2026-01-02T00:00:00.000Z",
475 commandId: "cmd-unarchive-1",
476 causationEventId: null,
477 correlationId: "cmd-unarchive-1",
478 metadata: {},
479 payload: {
480 threadId: "thread-1",
481 updatedAt: "2026-01-02T00:00:00.000Z",
482 },
483 });
484​
485 if (archived.type !== "thread.archived") {
486 assert.fail(`Expected thread.archived event, received ${archived.type}.`);
487 }
488 assert.strictEqual(archived.payload.archivedAt, "2026-01-01T00:00:00.000Z");
489 assert.strictEqual(unarchived.type, "thread.unarchived");
490 }),
491);
492​
493it.effect("decodes thread settled and unsettled events", () =>
494 Effect.gen(function* () {
495 const settled = yield* decodeOrchestrationEvent({
496 sequence: 1,
497 eventId: "event-settle-1",
498 aggregateKind: "thread",
499 aggregateId: "thread-1",
500 type: "thread.settled",
501 occurredAt: "2026-01-01T00:00:00.000Z",
502 commandId: "cmd-settle-1",
503 causationEventId: null,
504 correlationId: "cmd-settle-1",
505 metadata: {},
506 payload: {
507 threadId: "thread-1",
508 settledAt: "2026-01-01T00:00:00.000Z",
509 updatedAt: "2026-01-01T00:00:00.000Z",
510 },
511 });
512 const unsettled = yield* decodeOrchestrationEvent({
513 sequence: 2,
514 eventId: "event-unsettle-1",
515 aggregateKind: "thread",
516 aggregateId: "thread-1",
517 type: "thread.unsettled",
518 occurredAt: "2026-01-02T00:00:00.000Z",
519 commandId: "cmd-unsettle-1",
520 causationEventId: null,
521 correlationId: "cmd-unsettle-1",
522 metadata: {},
523 payload: {
524 threadId: "thread-1",
525 reason: "user",
526 updatedAt: "2026-01-02T00:00:00.000Z",
527 },
528 });
529​
530 assert.strictEqual(settled.type, "thread.settled");
531 assert.strictEqual(unsettled.type, "thread.unsettled");
532 }),
533);
534​
535it.effect("accepts provider-scoped model options in thread.turn.start", () =>
536 Effect.gen(function* () {
537 const parsed = yield* decodeThreadTurnStartCommand({
538 type: "thread.turn.start",
539 commandId: "cmd-turn-options",
540 threadId: "thread-1",
541 message: {
542 messageId: "msg-options",
543 role: "user",
544 text: "hello",
545 attachments: [],
546 },
547 modelSelection: {
548 provider: "codex",
549 model: "gpt-5.3-codex",
550 options: [
551 { id: "reasoningEffort", value: "high" },
552 { id: "fastMode", value: true },
553 ],
554 },
555 createdAt: "2026-01-01T00:00:00.000Z",
556 });
557 assert.strictEqual(parsed.modelSelection?.instanceId, "codex");
558 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "reasoningEffort"), "high");
559 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "fastMode"), true);
560 }),
561);
562​
563it.effect("normalizes legacy object-shaped modelSelection.options on decode", () =>
564 Effect.gen(function* () {
565 const parsed = yield* decodeThreadCreatedPayload({
566 threadId: "thread-1",
567 projectId: "project-1",
568 title: "Legacy options thread",
569 modelSelection: {
570 provider: "claudeAgent",
571 model: "claude-opus-4-6",
572 options: {
573 effort: "max",
574 fastMode: true,
575 // Falsy/garbage entries are dropped, matching migration 026.
576 emptyStr: " ",
577 nullish: null,
578 nested: { foo: 1 },
579 },
580 },
581 branch: null,
582 worktreePath: null,
583 createdAt: "2026-01-01T00:00:00.000Z",
584 updatedAt: "2026-01-01T00:00:00.000Z",
585 });
586​
587 assert.strictEqual(parsed.modelSelection.instanceId, ProviderInstanceId.make("claudeAgent"));
588 assert.deepStrictEqual(parsed.modelSelection.options, [
589 { id: "effort", value: "max" },
590 { id: "fastMode", value: true },
591 ]);
592 }),
593);
594​
595it.effect("normalizes legacy object-shaped defaultModelSelection.options on decode", () =>
596 Effect.gen(function* () {
597 const parsed = yield* decodeProjectCreatedPayload({
598 projectId: "project-1",
599 title: "Legacy default project",
600 workspaceRoot: "/tmp/legacy",
601 defaultModelSelection: {
602 provider: "codex",
603 model: "gpt-5.4",
604 options: { reasoningEffort: "low" },
605 },
606 scripts: [],
607 createdAt: "2026-01-01T00:00:00.000Z",
608 updatedAt: "2026-01-01T00:00:00.000Z",
609 });
610​
611 assert.deepStrictEqual(parsed.defaultModelSelection?.options, [
612 { id: "reasoningEffort", value: "low" },
613 ]);
614 }),
615);
616​
617it.effect(
618 "normalizes legacy object-shaped options on decode and re-encodes as canonical array",
619 () =>
620 Effect.gen(function* () {
621 const decoded = yield* decodeThreadCreatedPayload({
622 threadId: "thread-1",
623 projectId: "project-1",
624 title: "Round trip thread",
625 modelSelection: {
626 provider: "codex",
627 model: "gpt-5.4",
628 options: { fastMode: true },
629 },
630 branch: null,
631 worktreePath: null,
632 createdAt: "2026-01-01T00:00:00.000Z",
633 updatedAt: "2026-01-01T00:00:00.000Z",
634 });
635​
636 const encoded = yield* encodeThreadCreatedPayload(decoded);
637 assert.deepStrictEqual(encoded.modelSelection.options, [{ id: "fastMode", value: true }]);
638 }),
639);
640​
641it.effect("accepts a title seed in thread.turn.start", () =>
642 Effect.gen(function* () {
643 const parsed = yield* decodeThreadTurnStartCommand({
644 type: "thread.turn.start",
645 commandId: "cmd-turn-title-seed",
646 threadId: "thread-1",
647 message: {
648 messageId: "msg-title-seed",
649 role: "user",
650 text: "hello",
651 attachments: [],
652 },
653 titleSeed: "Investigate reconnect failures",
654 createdAt: "2026-01-01T00:00:00.000Z",
655 });
656 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
657 }),
658);
659​
660it.effect("accepts a title regeneration intent in thread.meta.update", () =>
661 Effect.gen(function* () {
662 const parsed = yield* decodeOrchestrationCommand({
663 type: "thread.meta.update",
664 commandId: "cmd-title-regenerate",
665 threadId: "thread-1",
666 regenerateTitle: true,
667 });
668 assert.strictEqual(parsed.type, "thread.meta.update");
669 if (parsed.type === "thread.meta.update") {
670 assert.strictEqual(parsed.regenerateTitle, true);
671 }
672 }),
673);
674​
675it.effect("accepts an internal title regeneration completion", () =>
676 Effect.gen(function* () {
677 const parsed = yield* decodeOrchestrationCommand({
678 type: "thread.title.regeneration.complete",
679 commandId: "cmd-title-regeneration-complete",
680 threadId: "thread-1",
681 requestId: "cmd-title-regenerate",
682 title: "Updated title",
683 });
684 assert.strictEqual(parsed.type, "thread.title.regeneration.complete");
685 if (parsed.type === "thread.title.regeneration.complete") {
686 assert.strictEqual(parsed.requestId, "cmd-title-regenerate");
687 assert.strictEqual(parsed.title, "Updated title");
688 }
689 }),
690);
691​
692it.effect("rejects an explicit title combined with title regeneration", () =>
693 Effect.gen(function* () {
694 const result = yield* Effect.exit(
695 decodeOrchestrationCommand({
696 type: "thread.meta.update",
697 commandId: "cmd-title-regenerate-with-title",
698 threadId: "thread-1",
699 title: "Explicit title",
700 regenerateTitle: true,
701 }),
702 );
703 assert.strictEqual(result._tag, "Failure");
704 }),
705);
706​
707it.effect("accepts a source proposed plan reference in thread.turn.start", () =>
708 Effect.gen(function* () {
709 const parsed = yield* decodeThreadTurnStartCommand({
710 type: "thread.turn.start",
711 commandId: "cmd-turn-source-plan",
712 threadId: "thread-2",
713 message: {
714 messageId: "msg-source-plan",
715 role: "user",
716 text: "implement this",
717 attachments: [],
718 },
719 sourceProposedPlan: {
720 threadId: "thread-1",
721 planId: "plan-1",
722 },
723 createdAt: "2026-01-01T00:00:00.000Z",
724 });
725 assert.deepStrictEqual(parsed.sourceProposedPlan, {
726 threadId: "thread-1",
727 planId: "plan-1",
728 });
729 }),
730);
731​
732it.effect(
733 "decodes thread.turn-start-requested defaults for provider, runtime mode, and interaction mode",
734 () =>
735 Effect.gen(function* () {
736 const parsed = yield* decodeThreadTurnStartRequestedPayload({
737 threadId: "thread-1",
738 messageId: "msg-1",
739 createdAt: "2026-01-01T00:00:00.000Z",
740 });
741 assert.strictEqual(parsed.modelSelection, undefined);
742 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
743 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
744 assert.strictEqual(parsed.sourceProposedPlan, undefined);
745 }),
746);
747​
748it.effect("decodes thread.turn-start-requested source proposed plan metadata when present", () =>
749 Effect.gen(function* () {
750 const parsed = yield* decodeThreadTurnStartRequestedPayload({
751 threadId: "thread-2",
752 messageId: "msg-2",
753 sourceProposedPlan: {
754 threadId: "thread-1",
755 planId: "plan-1",
756 },
757 createdAt: "2026-01-01T00:00:00.000Z",
758 });
759 assert.deepStrictEqual(parsed.sourceProposedPlan, {
760 threadId: "thread-1",
761 planId: "plan-1",
762 });
763 }),
764);
765​
766it.effect("decodes thread.turn-start-requested title seed when present", () =>
767 Effect.gen(function* () {
768 const parsed = yield* decodeThreadTurnStartRequestedPayload({
769 threadId: "thread-2",
770 messageId: "msg-2",
771 titleSeed: "Investigate reconnect failures",
772 createdAt: "2026-01-01T00:00:00.000Z",
773 });
774 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
775 }),
776);
777​
778it.effect("decodes latest turn source proposed plan metadata when present", () =>
779 Effect.gen(function* () {
780 const parsed = yield* decodeOrchestrationLatestTurn({
781 turnId: "turn-2",
782 state: "running",
783 requestedAt: "2026-01-01T00:00:00.000Z",
784 startedAt: "2026-01-01T00:00:01.000Z",
785 completedAt: null,
786 assistantMessageId: null,
787 sourceProposedPlan: {
788 threadId: "thread-1",
789 planId: "plan-1",
790 },
791 });
792 assert.deepStrictEqual(parsed.sourceProposedPlan, {
793 threadId: "thread-1",
794 planId: "plan-1",
795 });
796 }),
797);
798​
799it.effect("decodes orchestration session runtime mode defaults", () =>
800 Effect.gen(function* () {
801 const parsed = yield* decodeOrchestrationSession({
802 threadId: "thread-1",
803 status: "idle",
804 providerName: null,
805 providerSessionId: null,
806 providerThreadId: null,
807 activeTurnId: null,
808 lastError: null,
809 updatedAt: "2026-01-01T00:00:00.000Z",
810 });
811 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
812 }),
813);
814​
815it.effect("defaults proposed plan implementation metadata for historical rows", () =>
816 Effect.gen(function* () {
817 const parsed = yield* decodeOrchestrationProposedPlan({
818 id: "plan-1",
819 turnId: "turn-1",
820 planMarkdown: "# Plan",
821 createdAt: "2026-01-01T00:00:00.000Z",
822 updatedAt: "2026-01-01T00:00:00.000Z",
823 });
824 assert.strictEqual(parsed.implementedAt, null);
825 assert.strictEqual(parsed.implementationThreadId, null);
826 }),
827);
828​
829it.effect("preserves proposed plan implementation metadata when present", () =>
830 Effect.gen(function* () {
831 const parsed = yield* decodeOrchestrationProposedPlan({
832 id: "plan-2",
833 turnId: "turn-2",
834 planMarkdown: "# Plan",
835 implementedAt: "2026-01-02T00:00:00.000Z",
836 implementationThreadId: "thread-2",
837 createdAt: "2026-01-01T00:00:00.000Z",
838 updatedAt: "2026-01-02T00:00:00.000Z",
839 });
840 assert.strictEqual(parsed.implementedAt, "2026-01-02T00:00:00.000Z");
841 assert.strictEqual(parsed.implementationThreadId, "thread-2");
842 }),
843);
844​
845// ── ModelSelection: instance-keyed wire shape + legacy decoder ────────
846//
847// `ModelSelection` is routing-keyed on `instanceId` — never a driver kind.
848// Persisted and in-flight payloads from pre-instance builds carry a
849// `provider` field whose value was a driver kind; those payloads are migrated
850// at the wire boundary by
851// promoting `provider` to the default instance id for that driver
852// (built-in drivers use the driver kind slug as their default instance id, so
853// the migration is a 1:1 rename).
854//
855// These tests pin the rollback/fork tolerance invariant: legacy payloads
856// decode cleanly for fork-provided drivers, and the decoded form uses
857// `instanceId` uniformly regardless of origin.
858​
859const decodeModelSelection = Schema.decodeUnknownEffect(ModelSelection);
860const encodeModelSelection = Schema.encodeUnknownEffect(ModelSelection);
861​
862it.effect("ModelSelection migrates legacy `provider` field to `instanceId`", () =>
863 Effect.gen(function* () {
864 const parsed = yield* decodeModelSelection({
865 provider: "codex",
866 model: "gpt-5-codex",
867 options: [{ id: "reasoningEffort", value: "high" }],
868 });
869 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex"));
870 assert.strictEqual(parsed.model, "gpt-5-codex");
871 assert.deepStrictEqual(parsed.options, [{ id: "reasoningEffort", value: "high" }]);
872 }),
873);
874​
875it.effect("ModelSelection accepts an explicit instanceId routing key", () =>
876 Effect.gen(function* () {
877 const parsed = yield* decodeModelSelection({
878 instanceId: "codex_personal",
879 model: "gpt-5-codex",
880 });
881 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex_personal"));
882 }),
883);
884​
885it.effect("ModelSelection prefers explicit instanceId over legacy provider", () =>
886 Effect.gen(function* () {
887 const parsed = yield* decodeModelSelection({
888 provider: "codex",
889 instanceId: "codex_personal",
890 model: "gpt-5-codex",
891 });
892 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("codex_personal"));
893 }),
894);
895​
896it.effect(
897 "ModelSelection decodes unknown driver kinds via legacy provider (rollback / fork invariant)",
898 () =>
899 Effect.gen(function* () {
900 const parsed = yield* decodeModelSelection({
901 provider: "ollama",
902 model: "llama3:70b",
903 options: [{ id: "temperature", value: "0.4" }],
904 });
905 assert.strictEqual(parsed.instanceId, ProviderInstanceId.make("ollama"));
906 assert.strictEqual(parsed.model, "llama3:70b");
907 }),
908);
909​
910it.effect("ModelSelection encodes to the canonical instanceId wire form", () =>
911 Effect.gen(function* () {
912 const decoded = yield* decodeModelSelection({
913 provider: "ollama",
914 model: "llama3:70b",
915 options: [{ id: "temperature", value: "0.4" }],
916 });
917 const encoded = yield* encodeModelSelection(decoded);
918 assert.deepStrictEqual(encoded, {
919 instanceId: "ollama",
920 model: "llama3:70b",
921 options: [{ id: "temperature", value: "0.4" }],
922 });
923 }),
924);
925​
926it.effect("ModelSelection rejects malformed instance ids", () =>
927 Effect.gen(function* () {
928 const result = yield* Effect.exit(
929 decodeModelSelection({
930 instanceId: "1invalid", // must start with a letter
931 model: "x",
932 }),
933 );
934 assert.strictEqual(result._tag, "Failure");
935 }),
936);
937​
938it.effect("project favicon overrides accept only supported image files", () =>
939 Effect.gen(function* () {
940 const valid = yield* decodeOrchestrationCommand({
941 type: "project.meta.update",
942 commandId: "cmd-project-favicon",
943 projectId: "project-1",
944 faviconPath: "brand/icon.svg",
945 });
946 assert.strictEqual(valid.type, "project.meta.update");
947​
948 const invalid = yield* Effect.exit(
949 decodeOrchestrationCommand({
950 type: "project.meta.update",
951 commandId: "cmd-project-secret",
952 projectId: "project-1",
953 faviconPath: ".env",
954 }),
955 );
956 assert.strictEqual(invalid._tag, "Failure");
957 }),
958);
959​
960it("isProviderSendTurnSupportedImageMimeType accepts raster formats and rejects svg", () => {
961 assert.strictEqual(isProviderSendTurnSupportedImageMimeType("image/png"), true);
962 assert.strictEqual(isProviderSendTurnSupportedImageMimeType("IMAGE/JPEG"), true);
963 assert.strictEqual(isProviderSendTurnSupportedImageMimeType("image/svg+xml"), false);
964});