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

orchestration.test.ts

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