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

orchestration.test.ts

477 lines14.9 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";
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);
36const decodeThreadCreatedPayload = Schema.decodeUnknownEffect(ThreadCreatedPayload);
37const decodeOrchestrationCommand = Schema.decodeUnknownEffect(OrchestrationCommand);
38const decodeOrchestrationEvent = Schema.decodeUnknownEffect(OrchestrationEvent);
39const decodeThreadMetaUpdatedPayload = Schema.decodeUnknownEffect(ThreadMetaUpdatedPayload);
40​
41it.effect("parses turn diff input when fromTurnCount <= toTurnCount", () =>
42 Effect.gen(function* () {
43 const parsed = yield* decodeTurnDiffInput({
44 threadId: "thread-1",
45 fromTurnCount: 1,
46 toTurnCount: 2,
47 });
48 assert.strictEqual(parsed.fromTurnCount, 1);
49 assert.strictEqual(parsed.toTurnCount, 2);
50 }),
51);
52​
53it.effect("rejects turn diff input when fromTurnCount > toTurnCount", () =>
54 Effect.gen(function* () {
55 const result = yield* Effect.exit(
56 decodeTurnDiffInput({
57 threadId: "thread-1",
58 fromTurnCount: 3,
59 toTurnCount: 2,
60 }),
61 );
62 assert.strictEqual(result._tag, "Failure");
63 }),
64);
65​
66it.effect("rejects thread turn diff when fromTurnCount > toTurnCount", () =>
67 Effect.gen(function* () {
68 const result = yield* Effect.exit(
69 decodeThreadTurnDiff({
70 threadId: "thread-1",
71 fromTurnCount: 3,
72 toTurnCount: 2,
73 diff: "patch",
74 }),
75 );
76 assert.strictEqual(result._tag, "Failure");
77 }),
78);
79​
80it.effect("trims branded ids and command string fields at decode boundaries", () =>
81 Effect.gen(function* () {
82 const parsed = yield* decodeProjectCreateCommand({
83 type: "project.create",
84 commandId: " cmd-1 ",
85 projectId: " project-1 ",
86 title: " Project Title ",
87 workspaceRoot: " /tmp/workspace ",
88 defaultModelSelection: {
89 provider: "codex",
90 model: " gpt-5.2 ",
91 },
92 createdAt: "2026-01-01T00:00:00.000Z",
93 });
94 assert.strictEqual(parsed.commandId, "cmd-1");
95 assert.strictEqual(parsed.projectId, "project-1");
96 assert.strictEqual(parsed.title, "Project Title");
97 assert.strictEqual(parsed.workspaceRoot, "/tmp/workspace");
98 assert.deepStrictEqual(parsed.defaultModelSelection, {
99 provider: "codex",
100 model: "gpt-5.2",
101 });
102 }),
103);
104​
105it.effect("decodes historical project.created payloads with a default provider", () =>
106 Effect.gen(function* () {
107 const parsed = yield* decodeProjectCreatedPayload({
108 projectId: "project-1",
109 title: "Project Title",
110 workspaceRoot: "/tmp/workspace",
111 defaultModelSelection: {
112 provider: "codex",
113 model: "gpt-5.4",
114 },
115 scripts: [],
116 createdAt: "2026-01-01T00:00:00.000Z",
117 updatedAt: "2026-01-01T00:00:00.000Z",
118 });
119 assert.strictEqual(parsed.defaultModelSelection?.provider, "codex");
120 }),
121);
122​
123it.effect("decodes project.meta-updated payloads with explicit default provider", () =>
124 Effect.gen(function* () {
125 const parsed = yield* decodeProjectMetaUpdatedPayload({
126 projectId: "project-1",
127 defaultModelSelection: {
128 provider: "claudeAgent",
129 model: "claude-opus-4-6",
130 },
131 updatedAt: "2026-01-01T00:00:00.000Z",
132 });
133 assert.strictEqual(parsed.defaultModelSelection?.provider, "claudeAgent");
134 }),
135);
136​
137it.effect("rejects command fields that become empty after trim", () =>
138 Effect.gen(function* () {
139 const result = yield* Effect.exit(
140 decodeProjectCreateCommand({
141 type: "project.create",
142 commandId: "cmd-1",
143 projectId: "project-1",
144 title: " ",
145 workspaceRoot: "/tmp/workspace",
146 createdAt: "2026-01-01T00:00:00.000Z",
147 }),
148 );
149 assert.strictEqual(result._tag, "Failure");
150 }),
151);
152​
153it.effect("decodes thread.turn.start defaults for provider and runtime mode", () =>
154 Effect.gen(function* () {
155 const parsed = yield* decodeThreadTurnStartCommand({
156 type: "thread.turn.start",
157 commandId: "cmd-turn-1",
158 threadId: "thread-1",
159 message: {
160 messageId: "msg-1",
161 role: "user",
162 text: "hello",
163 attachments: [],
164 },
165 createdAt: "2026-01-01T00:00:00.000Z",
166 });
167 assert.strictEqual(parsed.modelSelection, undefined);
168 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
169 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
170 }),
171);
172​
173it.effect("preserves explicit provider and runtime mode in thread.turn.start", () =>
174 Effect.gen(function* () {
175 const parsed = yield* decodeThreadTurnStartCommand({
176 type: "thread.turn.start",
177 commandId: "cmd-turn-2",
178 threadId: "thread-1",
179 message: {
180 messageId: "msg-2",
181 role: "user",
182 text: "hello",
183 attachments: [],
184 },
185 modelSelection: {
186 provider: "codex",
187 model: "gpt-5.4",
188 },
189 runtimeMode: "full-access",
190 createdAt: "2026-01-01T00:00:00.000Z",
191 });
192 assert.strictEqual(parsed.modelSelection?.provider, "codex");
193 assert.strictEqual(parsed.runtimeMode, "full-access");
194 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
195 }),
196);
197​
198it.effect("decodes thread.created runtime mode for historical events", () =>
199 Effect.gen(function* () {
200 const parsed = yield* decodeThreadCreatedPayload({
201 threadId: "thread-1",
202 projectId: "project-1",
203 title: "Thread title",
204 modelSelection: {
205 provider: "codex",
206 model: "gpt-5.4",
207 },
208 interactionMode: "default",
209 branch: null,
210 worktreePath: null,
211 createdAt: "2026-01-01T00:00:00.000Z",
212 updatedAt: "2026-01-01T00:00:00.000Z",
213 });
214​
215 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
216 assert.strictEqual(parsed.modelSelection.provider, "codex");
217 }),
218);
219​
220it.effect("decodes thread.meta-updated payloads with explicit provider", () =>
221 Effect.gen(function* () {
222 const parsed = yield* decodeThreadMetaUpdatedPayload({
223 threadId: "thread-1",
224 modelSelection: {
225 provider: "claudeAgent",
226 model: "claude-opus-4-6",
227 },
228 updatedAt: "2026-01-01T00:00:00.000Z",
229 });
230 assert.strictEqual(parsed.modelSelection?.provider, "claudeAgent");
231 }),
232);
233​
234it.effect("decodes thread archive and unarchive commands", () =>
235 Effect.gen(function* () {
236 const archive = yield* decodeOrchestrationCommand({
237 type: "thread.archive",
238 commandId: "cmd-archive-1",
239 threadId: "thread-1",
240 });
241 const unarchive = yield* decodeOrchestrationCommand({
242 type: "thread.unarchive",
243 commandId: "cmd-unarchive-1",
244 threadId: "thread-1",
245 });
246​
247 assert.strictEqual(archive.type, "thread.archive");
248 assert.strictEqual(unarchive.type, "thread.unarchive");
249 }),
250);
251​
252it.effect("decodes thread archived and unarchived events", () =>
253 Effect.gen(function* () {
254 const archived = yield* decodeOrchestrationEvent({
255 sequence: 1,
256 eventId: "event-archive-1",
257 aggregateKind: "thread",
258 aggregateId: "thread-1",
259 type: "thread.archived",
260 occurredAt: "2026-01-01T00:00:00.000Z",
261 commandId: "cmd-archive-1",
262 causationEventId: null,
263 correlationId: "cmd-archive-1",
264 metadata: {},
265 payload: {
266 threadId: "thread-1",
267 archivedAt: "2026-01-01T00:00:00.000Z",
268 updatedAt: "2026-01-01T00:00:00.000Z",
269 },
270 });
271 const unarchived = yield* decodeOrchestrationEvent({
272 sequence: 2,
273 eventId: "event-unarchive-1",
274 aggregateKind: "thread",
275 aggregateId: "thread-1",
276 type: "thread.unarchived",
277 occurredAt: "2026-01-02T00:00:00.000Z",
278 commandId: "cmd-unarchive-1",
279 causationEventId: null,
280 correlationId: "cmd-unarchive-1",
281 metadata: {},
282 payload: {
283 threadId: "thread-1",
284 updatedAt: "2026-01-02T00:00:00.000Z",
285 },
286 });
287​
288 assert.strictEqual(archived.type, "thread.archived");
289 assert.strictEqual(archived.payload.archivedAt, "2026-01-01T00:00:00.000Z");
290 assert.strictEqual(unarchived.type, "thread.unarchived");
291 }),
292);
293​
294it.effect("accepts provider-scoped model options in thread.turn.start", () =>
295 Effect.gen(function* () {
296 const parsed = yield* decodeThreadTurnStartCommand({
297 type: "thread.turn.start",
298 commandId: "cmd-turn-options",
299 threadId: "thread-1",
300 message: {
301 messageId: "msg-options",
302 role: "user",
303 text: "hello",
304 attachments: [],
305 },
306 modelSelection: {
307 provider: "codex",
308 model: "gpt-5.3-codex",
309 options: {
310 reasoningEffort: "high",
311 fastMode: true,
312 },
313 },
314 createdAt: "2026-01-01T00:00:00.000Z",
315 });
316 assert.strictEqual(parsed.modelSelection?.provider, "codex");
317 assert.strictEqual(parsed.modelSelection?.options?.reasoningEffort, "high");
318 assert.strictEqual(parsed.modelSelection?.options?.fastMode, true);
319 }),
320);
321​
322it.effect("accepts a title seed in thread.turn.start", () =>
323 Effect.gen(function* () {
324 const parsed = yield* decodeThreadTurnStartCommand({
325 type: "thread.turn.start",
326 commandId: "cmd-turn-title-seed",
327 threadId: "thread-1",
328 message: {
329 messageId: "msg-title-seed",
330 role: "user",
331 text: "hello",
332 attachments: [],
333 },
334 titleSeed: "Investigate reconnect failures",
335 createdAt: "2026-01-01T00:00:00.000Z",
336 });
337 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
338 }),
339);
340​
341it.effect("accepts a source proposed plan reference in thread.turn.start", () =>
342 Effect.gen(function* () {
343 const parsed = yield* decodeThreadTurnStartCommand({
344 type: "thread.turn.start",
345 commandId: "cmd-turn-source-plan",
346 threadId: "thread-2",
347 message: {
348 messageId: "msg-source-plan",
349 role: "user",
350 text: "implement this",
351 attachments: [],
352 },
353 sourceProposedPlan: {
354 threadId: "thread-1",
355 planId: "plan-1",
356 },
357 createdAt: "2026-01-01T00:00:00.000Z",
358 });
359 assert.deepStrictEqual(parsed.sourceProposedPlan, {
360 threadId: "thread-1",
361 planId: "plan-1",
362 });
363 }),
364);
365​
366it.effect(
367 "decodes thread.turn-start-requested defaults for provider, runtime mode, and interaction mode",
368 () =>
369 Effect.gen(function* () {
370 const parsed = yield* decodeThreadTurnStartRequestedPayload({
371 threadId: "thread-1",
372 messageId: "msg-1",
373 createdAt: "2026-01-01T00:00:00.000Z",
374 });
375 assert.strictEqual(parsed.modelSelection, undefined);
376 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
377 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
378 assert.strictEqual(parsed.sourceProposedPlan, undefined);
379 }),
380);
381​
382it.effect("decodes thread.turn-start-requested source proposed plan metadata when present", () =>
383 Effect.gen(function* () {
384 const parsed = yield* decodeThreadTurnStartRequestedPayload({
385 threadId: "thread-2",
386 messageId: "msg-2",
387 sourceProposedPlan: {
388 threadId: "thread-1",
389 planId: "plan-1",
390 },
391 createdAt: "2026-01-01T00:00:00.000Z",
392 });
393 assert.deepStrictEqual(parsed.sourceProposedPlan, {
394 threadId: "thread-1",
395 planId: "plan-1",
396 });
397 }),
398);
399​
400it.effect("decodes thread.turn-start-requested title seed when present", () =>
401 Effect.gen(function* () {
402 const parsed = yield* decodeThreadTurnStartRequestedPayload({
403 threadId: "thread-2",
404 messageId: "msg-2",
405 titleSeed: "Investigate reconnect failures",
406 createdAt: "2026-01-01T00:00:00.000Z",
407 });
408 assert.strictEqual(parsed.titleSeed, "Investigate reconnect failures");
409 }),
410);
411​
412it.effect("decodes latest turn source proposed plan metadata when present", () =>
413 Effect.gen(function* () {
414 const parsed = yield* decodeOrchestrationLatestTurn({
415 turnId: "turn-2",
416 state: "running",
417 requestedAt: "2026-01-01T00:00:00.000Z",
418 startedAt: "2026-01-01T00:00:01.000Z",
419 completedAt: null,
420 assistantMessageId: null,
421 sourceProposedPlan: {
422 threadId: "thread-1",
423 planId: "plan-1",
424 },
425 });
426 assert.deepStrictEqual(parsed.sourceProposedPlan, {
427 threadId: "thread-1",
428 planId: "plan-1",
429 });
430 }),
431);
432​
433it.effect("decodes orchestration session runtime mode defaults", () =>
434 Effect.gen(function* () {
435 const parsed = yield* decodeOrchestrationSession({
436 threadId: "thread-1",
437 status: "idle",
438 providerName: null,
439 providerSessionId: null,
440 providerThreadId: null,
441 activeTurnId: null,
442 lastError: null,
443 updatedAt: "2026-01-01T00:00:00.000Z",
444 });
445 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
446 }),
447);
448​
449it.effect("defaults proposed plan implementation metadata for historical rows", () =>
450 Effect.gen(function* () {
451 const parsed = yield* decodeOrchestrationProposedPlan({
452 id: "plan-1",
453 turnId: "turn-1",
454 planMarkdown: "# Plan",
455 createdAt: "2026-01-01T00:00:00.000Z",
456 updatedAt: "2026-01-01T00:00:00.000Z",
457 });
458 assert.strictEqual(parsed.implementedAt, null);
459 assert.strictEqual(parsed.implementationThreadId, null);
460 }),
461);
462​
463it.effect("preserves proposed plan implementation metadata when present", () =>
464 Effect.gen(function* () {
465 const parsed = yield* decodeOrchestrationProposedPlan({
466 id: "plan-2",
467 turnId: "turn-2",
468 planMarkdown: "# Plan",
469 implementedAt: "2026-01-02T00:00:00.000Z",
470 implementationThreadId: "thread-2",
471 createdAt: "2026-01-01T00:00:00.000Z",
472 updatedAt: "2026-01-02T00:00:00.000Z",
473 });
474 assert.strictEqual(parsed.implementedAt, "2026-01-02T00:00:00.000Z");
475 assert.strictEqual(parsed.implementationThreadId, "thread-2");
476 }),
477);