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

orchestration.test.ts

937 lines29.7 KB

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