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

orchestration.test.ts

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