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

orchestration.test.ts

1,142 lines36.9 KB

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