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

orchestration.test.ts

620 lines19.3 KB

1import assert from "node:assert/strict";
2import { it } from "@effect/vitest";
3import { Effect, Schema } from "effect";
4​
5import {
6 DEFAULT_PROVIDER_INTERACTION_MODE,
7 DEFAULT_RUNTIME_MODE,
8 OrchestrationCommand,
9 OrchestrationEvent,
10 OrchestrationGetTurnDiffInput,
11 OrchestrationLatestTurn,
12 ProjectCreatedPayload,
13 ProjectMetaUpdatedPayload,
14 OrchestrationProposedPlan,
15 OrchestrationSession,
16 ProjectCreateCommand,
17 ThreadMetaUpdatedPayload,
18 ThreadTurnStartCommand,
19 ThreadCreatedPayload,
20 ThreadTurnDiff,
21 ThreadTurnStartRequestedPayload,
22} from "./orchestration.ts";
23​
24const decodeTurnDiffInput = Schema.decodeUnknownEffect(OrchestrationGetTurnDiffInput);
25const decodeThreadTurnDiff = Schema.decodeUnknownEffect(ThreadTurnDiff);
26const decodeProjectCreateCommand = Schema.decodeUnknownEffect(ProjectCreateCommand);
27const decodeProjectCreatedPayload = Schema.decodeUnknownEffect(ProjectCreatedPayload);
28const decodeProjectMetaUpdatedPayload = Schema.decodeUnknownEffect(ProjectMetaUpdatedPayload);
29const decodeThreadTurnStartCommand = Schema.decodeUnknownEffect(ThreadTurnStartCommand);
30const decodeThreadTurnStartRequestedPayload = Schema.decodeUnknownEffect(
31 ThreadTurnStartRequestedPayload,
32);
33const decodeOrchestrationLatestTurn = Schema.decodeUnknownEffect(OrchestrationLatestTurn);
34const decodeOrchestrationProposedPlan = Schema.decodeUnknownEffect(OrchestrationProposedPlan);
35const decodeOrchestrationSession = Schema.decodeUnknownEffect(OrchestrationSession);
36​
37function getOptionValue(
38 options: ReadonlyArray<{ id: string; value: unknown }> | undefined,
39 id: string,
40): unknown {
41 return options?.find((option) => option.id === id)?.value;
43const decodeThreadCreatedPayload = Schema.decodeUnknownEffect(ThreadCreatedPayload);
44const decodeOrchestrationCommand = Schema.decodeUnknownEffect(OrchestrationCommand);
45const decodeOrchestrationEvent = Schema.decodeUnknownEffect(OrchestrationEvent);
46const decodeThreadMetaUpdatedPayload = Schema.decodeUnknownEffect(ThreadMetaUpdatedPayload);
47​
48it.effect("parses turn diff input when fromTurnCount <= toTurnCount", () =>
49 Effect.gen(function* () {
50 const parsed = yield* decodeTurnDiffInput({
51 threadId: "thread-1",
52 fromTurnCount: 1,
53 toTurnCount: 2,
54 });
55 assert.strictEqual(parsed.fromTurnCount, 1);
56 assert.strictEqual(parsed.toTurnCount, 2);
57 }),
58);
59​
60it.effect("rejects turn diff input when fromTurnCount > toTurnCount", () =>
61 Effect.gen(function* () {
62 const result = yield* Effect.exit(
63 decodeTurnDiffInput({
64 threadId: "thread-1",
65 fromTurnCount: 3,
66 toTurnCount: 2,
67 }),
68 );
69 assert.strictEqual(result._tag, "Failure");
70 }),
71);
72​
73it.effect("rejects thread turn diff when fromTurnCount > toTurnCount", () =>
74 Effect.gen(function* () {
75 const result = yield* Effect.exit(
76 decodeThreadTurnDiff({
77 threadId: "thread-1",
78 fromTurnCount: 3,
79 toTurnCount: 2,
80 diff: "patch",
81 }),
82 );
83 assert.strictEqual(result._tag, "Failure");
84 }),
85);
86​
87it.effect("trims branded ids and command string fields at decode boundaries", () =>
88 Effect.gen(function* () {
89 const parsed = yield* decodeProjectCreateCommand({
90 type: "project.create",
91 commandId: " cmd-1 ",
92 projectId: " project-1 ",
93 title: " Project Title ",
94 workspaceRoot: " /tmp/workspace ",
95 defaultModelSelection: {
96 provider: "codex",
97 model: " gpt-5.2 ",
98 },
99 createdAt: "2026-01-01T00:00:00.000Z",
100 });
101 assert.strictEqual(parsed.commandId, "cmd-1");
102 assert.strictEqual(parsed.projectId, "project-1");
103 assert.strictEqual(parsed.title, "Project Title");
104 assert.strictEqual(parsed.workspaceRoot, "/tmp/workspace");
105 assert.strictEqual(parsed.createWorkspaceRootIfMissing, undefined);
106 assert.deepStrictEqual(parsed.defaultModelSelection, {
107 provider: "codex",
108 model: "gpt-5.2",
109 });
110 }),
111);
112​
113it.effect("decodes project.create with createWorkspaceRootIfMissing enabled", () =>
114 Effect.gen(function* () {
115 const parsed = yield* decodeProjectCreateCommand({
116 type: "project.create",
117 commandId: "cmd-1",
118 projectId: "project-1",
119 title: "Project Title",
120 workspaceRoot: "/tmp/workspace",
121 createWorkspaceRootIfMissing: true,
122 createdAt: "2026-01-01T00:00:00.000Z",
123 });
124​
125 assert.strictEqual(parsed.createWorkspaceRootIfMissing, true);
126 }),
127);
128​
129it.effect("decodes historical project.created payloads with a default provider", () =>
130 Effect.gen(function* () {
131 const parsed = yield* decodeProjectCreatedPayload({
132 projectId: "project-1",
133 title: "Project Title",
134 workspaceRoot: "/tmp/workspace",
135 defaultModelSelection: {
136 provider: "codex",
137 model: "gpt-5.4",
138 },
139 scripts: [],
140 createdAt: "2026-01-01T00:00:00.000Z",
141 updatedAt: "2026-01-01T00:00:00.000Z",
142 });
143 assert.strictEqual(parsed.defaultModelSelection?.provider, "codex");
144 }),
145);
146​
147it.effect("decodes project.meta-updated payloads with explicit default provider", () =>
148 Effect.gen(function* () {
149 const parsed = yield* decodeProjectMetaUpdatedPayload({
150 projectId: "project-1",
151 defaultModelSelection: {
152 provider: "claudeAgent",
153 model: "claude-opus-4-6",
154 },
155 updatedAt: "2026-01-01T00:00:00.000Z",
156 });
157 assert.strictEqual(parsed.defaultModelSelection?.provider, "claudeAgent");
158 }),
159);
160​
161it.effect("rejects command fields that become empty after trim", () =>
162 Effect.gen(function* () {
163 const result = yield* Effect.exit(
164 decodeProjectCreateCommand({
165 type: "project.create",
166 commandId: "cmd-1",
167 projectId: "project-1",
168 title: " ",
169 workspaceRoot: "/tmp/workspace",
170 createdAt: "2026-01-01T00:00:00.000Z",
171 }),
172 );
173 assert.strictEqual(result._tag, "Failure");
174 }),
175);
176​
177it.effect("decodes thread.turn.start defaults for provider and runtime mode", () =>
178 Effect.gen(function* () {
179 const parsed = yield* decodeThreadTurnStartCommand({
180 type: "thread.turn.start",
181 commandId: "cmd-turn-1",
182 threadId: "thread-1",
183 message: {
184 messageId: "msg-1",
185 role: "user",
186 text: "hello",
187 attachments: [],
188 },
189 createdAt: "2026-01-01T00:00:00.000Z",
190 });
191 assert.strictEqual(parsed.modelSelection, undefined);
192 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
193 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
194 }),
195);
196​
197it.effect("preserves explicit provider and runtime mode in thread.turn.start", () =>
198 Effect.gen(function* () {
199 const parsed = yield* decodeThreadTurnStartCommand({
200 type: "thread.turn.start",
201 commandId: "cmd-turn-2",
202 threadId: "thread-1",
203 message: {
204 messageId: "msg-2",
205 role: "user",
206 text: "hello",
207 attachments: [],
208 },
209 modelSelection: {
210 provider: "codex",
211 model: "gpt-5.4",
212 },
213 runtimeMode: "full-access",
214 createdAt: "2026-01-01T00:00:00.000Z",
215 });
216 assert.strictEqual(parsed.modelSelection?.provider, "codex");
217 assert.strictEqual(parsed.runtimeMode, "full-access");
218 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
219 }),
220);
221​
222it.effect("accepts bootstrap metadata in thread.turn.start", () =>
223 Effect.gen(function* () {
224 const parsed = yield* decodeThreadTurnStartCommand({
225 type: "thread.turn.start",
226 commandId: "cmd-turn-bootstrap",
227 threadId: "thread-1",
228 message: {
229 messageId: "msg-bootstrap",
230 role: "user",
231 text: "hello",
232 attachments: [],
233 },
234 bootstrap: {
235 createThread: {
236 projectId: "project-1",
237 title: "Bootstrap thread",
238 modelSelection: {
239 provider: "codex",
240 model: "gpt-5.4",
241 },
242 runtimeMode: "full-access",
243 interactionMode: "default",
244 branch: null,
245 worktreePath: null,
246 createdAt: "2026-01-01T00:00:00.000Z",
247 },
248 prepareWorktree: {
249 projectCwd: "/tmp/workspace",
250 baseBranch: "main",
251 branch: "t3code/example",
252 },
253 runSetupScript: true,
254 },
255 createdAt: "2026-01-01T00:00:00.000Z",
256 });
257 assert.strictEqual(parsed.bootstrap?.createThread?.projectId, "project-1");
258 assert.strictEqual(parsed.bootstrap?.prepareWorktree?.baseBranch, "main");
259 assert.strictEqual(parsed.bootstrap?.runSetupScript, true);
260 }),
261);
262​
263it.effect("decodes thread.created runtime mode for historical events", () =>
264 Effect.gen(function* () {
265 const parsed = yield* decodeThreadCreatedPayload({
266 threadId: "thread-1",
267 projectId: "project-1",
268 title: "Thread title",
269 modelSelection: {
270 provider: "codex",
271 model: "gpt-5.4",
272 },
273 interactionMode: "default",
274 branch: null,
275 worktreePath: null,
276 createdAt: "2026-01-01T00:00:00.000Z",
277 updatedAt: "2026-01-01T00:00:00.000Z",
278 });
279​
280 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
281 assert.strictEqual(parsed.modelSelection.provider, "codex");
282 }),
283);
284​
285it.effect("decodes thread.meta-updated payloads with explicit provider", () =>
286 Effect.gen(function* () {
287 const parsed = yield* decodeThreadMetaUpdatedPayload({
288 threadId: "thread-1",
289 modelSelection: {
290 provider: "claudeAgent",
291 model: "claude-opus-4-6",
292 },
293 updatedAt: "2026-01-01T00:00:00.000Z",
294 });
295 assert.strictEqual(parsed.modelSelection?.provider, "claudeAgent");
296 }),
297);
298​
299it.effect("decodes thread archive and unarchive commands", () =>
300 Effect.gen(function* () {
301 const archive = yield* decodeOrchestrationCommand({
302 type: "thread.archive",
303 commandId: "cmd-archive-1",
304 threadId: "thread-1",
305 });
306 const unarchive = yield* decodeOrchestrationCommand({
307 type: "thread.unarchive",
308 commandId: "cmd-unarchive-1",
309 threadId: "thread-1",
310 });
311​
312 assert.strictEqual(archive.type, "thread.archive");
313 assert.strictEqual(unarchive.type, "thread.unarchive");
314 }),
315);
316​
317it.effect("decodes thread archived and unarchived events", () =>
318 Effect.gen(function* () {
319 const archived = yield* decodeOrchestrationEvent({
320 sequence: 1,
321 eventId: "event-archive-1",
322 aggregateKind: "thread",
323 aggregateId: "thread-1",
324 type: "thread.archived",
325 occurredAt: "2026-01-01T00:00:00.000Z",
326 commandId: "cmd-archive-1",
327 causationEventId: null,
328 correlationId: "cmd-archive-1",
329 metadata: {},
330 payload: {
331 threadId: "thread-1",
332 archivedAt: "2026-01-01T00:00:00.000Z",
333 updatedAt: "2026-01-01T00:00:00.000Z",
334 },
335 });
336 const unarchived = yield* decodeOrchestrationEvent({
337 sequence: 2,
338 eventId: "event-unarchive-1",
339 aggregateKind: "thread",
340 aggregateId: "thread-1",
341 type: "thread.unarchived",
342 occurredAt: "2026-01-02T00:00:00.000Z",
343 commandId: "cmd-unarchive-1",
344 causationEventId: null,
345 correlationId: "cmd-unarchive-1",
346 metadata: {},
347 payload: {
348 threadId: "thread-1",
349 updatedAt: "2026-01-02T00:00:00.000Z",
350 },
351 });
352​
353 assert.strictEqual(archived.type, "thread.archived");
354 assert.strictEqual(archived.payload.archivedAt, "2026-01-01T00:00:00.000Z");
355 assert.strictEqual(unarchived.type, "thread.unarchived");
356 }),
357);
358​
359it.effect("accepts provider-scoped model options in thread.turn.start", () =>
360 Effect.gen(function* () {
361 const parsed = yield* decodeThreadTurnStartCommand({
362 type: "thread.turn.start",
363 commandId: "cmd-turn-options",
364 threadId: "thread-1",
365 message: {
366 messageId: "msg-options",
367 role: "user",
368 text: "hello",
369 attachments: [],
370 },
371 modelSelection: {
372 provider: "codex",
373 model: "gpt-5.3-codex",
374 options: [
375 { id: "reasoningEffort", value: "high" },
376 { id: "fastMode", value: true },
377 ],
378 },
379 createdAt: "2026-01-01T00:00:00.000Z",
380 });
381 assert.strictEqual(parsed.modelSelection?.provider, "codex");
382 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "reasoningEffort"), "high");
383 assert.strictEqual(getOptionValue(parsed.modelSelection?.options, "fastMode"), true);
384 }),
385);
386​
387it.effect("normalizes legacy object-shaped modelSelection.options on decode", () =>
388 Effect.gen(function* () {
389 const parsed = yield* decodeThreadCreatedPayload({
390 threadId: "thread-1",
391 projectId: "project-1",
392 title: "Legacy options thread",
393 modelSelection: {
394 provider: "claudeAgent",
395 model: "claude-opus-4-6",
396 options: {
397 effort: "max",
398 fastMode: true,
399 // Falsy/garbage entries are dropped, matching migration 026.
400 emptyStr: " ",
401 nullish: null,
402 nested: { foo: 1 },
403 },
404 },
405 branch: null,
406 worktreePath: null,
407 createdAt: "2026-01-01T00:00:00.000Z",
408 updatedAt: "2026-01-01T00:00:00.000Z",
409 });
410​
411 assert.strictEqual(parsed.modelSelection.provider, "claudeAgent");
412 assert.deepStrictEqual(parsed.modelSelection.options, [
413 { id: "effort", value: "max" },
414 { id: "fastMode", value: true },
415 ]);
416 }),
417);
418​
419it.effect("normalizes legacy object-shaped defaultModelSelection.options on decode", () =>
420 Effect.gen(function* () {
421 const parsed = yield* decodeProjectCreatedPayload({
422 projectId: "project-1",
423 title: "Legacy default project",
424 workspaceRoot: "/tmp/legacy",
425 defaultModelSelection: {
426 provider: "codex",
427 model: "gpt-5.4",
428 options: { reasoningEffort: "low" },
429 },
430 scripts: [],
431 createdAt: "2026-01-01T00:00:00.000Z",
432 updatedAt: "2026-01-01T00:00:00.000Z",
433 });
434​
435 assert.deepStrictEqual(parsed.defaultModelSelection?.options, [
436 { id: "reasoningEffort", value: "low" },
437 ]);
438 }),
439);
440​
441it.effect(
442 "normalizes legacy object-shaped options on decode and re-encodes as canonical array",
443 () =>
444 Effect.gen(function* () {
445 const decoded = yield* decodeThreadCreatedPayload({
446 threadId: "thread-1",
447 projectId: "project-1",
448 title: "Round trip thread",
449 modelSelection: {
450 provider: "codex",
451 model: "gpt-5.4",
452 options: { fastMode: true },
453 },
454 branch: null,
455 worktreePath: null,
456 createdAt: "2026-01-01T00:00:00.000Z",
457 updatedAt: "2026-01-01T00:00:00.000Z",
458 });
459​
460 const encoded = yield* Schema.encodeEffect(ThreadCreatedPayload)(decoded);
461 assert.deepStrictEqual(encoded.modelSelection.options, [{ id: "fastMode", value: true }]);
462 }),
463);
464​
465it.effect("accepts a title seed in thread.turn.start", () =>
466 Effect.gen(function* () {
467 const parsed = yield* decodeThreadTurnStartCommand({
468 type: "thread.turn.start",
469 commandId: "cmd-turn-title-seed",
470 threadId: "thread-1",
471 message: {
472 messageId: "msg-title-seed",
473 role: "user",
474 text: "hello",
475 attachments: [],
476 },
477 titleSeed: "Investigate reconnect failures",
478 createdAt: "2026-01-01T00:00:00.000Z",
479 });
480 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
481 }),
482);
483​
484it.effect("accepts a source proposed plan reference in thread.turn.start", () =>
485 Effect.gen(function* () {
486 const parsed = yield* decodeThreadTurnStartCommand({
487 type: "thread.turn.start",
488 commandId: "cmd-turn-source-plan",
489 threadId: "thread-2",
490 message: {
491 messageId: "msg-source-plan",
492 role: "user",
493 text: "implement this",
494 attachments: [],
495 },
496 sourceProposedPlan: {
497 threadId: "thread-1",
498 planId: "plan-1",
499 },
500 createdAt: "2026-01-01T00:00:00.000Z",
501 });
502 assert.deepStrictEqual(parsed.sourceProposedPlan, {
503 threadId: "thread-1",
504 planId: "plan-1",
505 });
506 }),
507);
508​
509it.effect(
510 "decodes thread.turn-start-requested defaults for provider, runtime mode, and interaction mode",
511 () =>
512 Effect.gen(function* () {
513 const parsed = yield* decodeThreadTurnStartRequestedPayload({
514 threadId: "thread-1",
515 messageId: "msg-1",
516 createdAt: "2026-01-01T00:00:00.000Z",
517 });
518 assert.strictEqual(parsed.modelSelection, undefined);
519 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
520 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
521 assert.strictEqual(parsed.sourceProposedPlan, undefined);
522 }),
523);
524​
525it.effect("decodes thread.turn-start-requested source proposed plan metadata when present", () =>
526 Effect.gen(function* () {
527 const parsed = yield* decodeThreadTurnStartRequestedPayload({
528 threadId: "thread-2",
529 messageId: "msg-2",
530 sourceProposedPlan: {
531 threadId: "thread-1",
532 planId: "plan-1",
533 },
534 createdAt: "2026-01-01T00:00:00.000Z",
535 });
536 assert.deepStrictEqual(parsed.sourceProposedPlan, {
537 threadId: "thread-1",
538 planId: "plan-1",
539 });
540 }),
541);
542​
543it.effect("decodes thread.turn-start-requested title seed when present", () =>
544 Effect.gen(function* () {
545 const parsed = yield* decodeThreadTurnStartRequestedPayload({
546 threadId: "thread-2",
547 messageId: "msg-2",
548 titleSeed: "Investigate reconnect failures",
549 createdAt: "2026-01-01T00:00:00.000Z",
550 });
551 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
552 }),
553);
554​
555it.effect("decodes latest turn source proposed plan metadata when present", () =>
556 Effect.gen(function* () {
557 const parsed = yield* decodeOrchestrationLatestTurn({
558 turnId: "turn-2",
559 state: "running",
560 requestedAt: "2026-01-01T00:00:00.000Z",
561 startedAt: "2026-01-01T00:00:01.000Z",
562 completedAt: null,
563 assistantMessageId: null,
564 sourceProposedPlan: {
565 threadId: "thread-1",
566 planId: "plan-1",
567 },
568 });
569 assert.deepStrictEqual(parsed.sourceProposedPlan, {
570 threadId: "thread-1",
571 planId: "plan-1",
572 });
573 }),
574);
575​
576it.effect("decodes orchestration session runtime mode defaults", () =>
577 Effect.gen(function* () {
578 const parsed = yield* decodeOrchestrationSession({
579 threadId: "thread-1",
580 status: "idle",
581 providerName: null,
582 providerSessionId: null,
583 providerThreadId: null,
584 activeTurnId: null,
585 lastError: null,
586 updatedAt: "2026-01-01T00:00:00.000Z",
587 });
588 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
589 }),
590);
591​
592it.effect("defaults proposed plan implementation metadata for historical rows", () =>
593 Effect.gen(function* () {
594 const parsed = yield* decodeOrchestrationProposedPlan({
595 id: "plan-1",
596 turnId: "turn-1",
597 planMarkdown: "# Plan",
598 createdAt: "2026-01-01T00:00:00.000Z",
599 updatedAt: "2026-01-01T00:00:00.000Z",
600 });
601 assert.strictEqual(parsed.implementedAt, null);
602 assert.strictEqual(parsed.implementationThreadId, null);
603 }),
604);
605​
606it.effect("preserves proposed plan implementation metadata when present", () =>
607 Effect.gen(function* () {
608 const parsed = yield* decodeOrchestrationProposedPlan({
609 id: "plan-2",
610 turnId: "turn-2",
611 planMarkdown: "# Plan",
612 implementedAt: "2026-01-02T00:00:00.000Z",
613 implementationThreadId: "thread-2",
614 createdAt: "2026-01-01T00:00:00.000Z",
615 updatedAt: "2026-01-02T00:00:00.000Z",
616 });
617 assert.strictEqual(parsed.implementedAt, "2026-01-02T00:00:00.000Z");
618 assert.strictEqual(parsed.implementationThreadId, "thread-2");
619 }),
620);