Open History to see the last commit
1import {
2 CommandId,
3 DEFAULT_PROVIDER_INTERACTION_MODE,
4 EventId,
5 MessageId,
6 ProjectId,
7 ThreadId,
8 ProviderInstanceId,
9 type ProjectScript,
10} from "@t3tools/contracts";
11import { createModelSelection } from "@t3tools/shared/model";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14import * as NodeServices from "@effect/platform-node/NodeServices";
15
16import { decideOrchestrationCommand } from "./decider.ts";
17import { createEmptyReadModel, projectEvent } from "./projector.ts";
18
19const asEventId = (value: string): EventId => EventId.make(value);
20const asProjectId = (value: string): ProjectId => ProjectId.make(value);
21const asMessageId = (value: string): MessageId => MessageId.make(value);
22it.layer(NodeServices.layer)("decider project scripts", (it) => {
23 it.effect("emits empty scripts on project.create", () =>
24 Effect.gen(function* () {
25 const now = "2026-01-01T00:00:00.000Z";
26 const readModel = createEmptyReadModel(now);
27
28 const result = yield* decideOrchestrationCommand({
29 command: {
30 type: "project.create",
31 commandId: CommandId.make("cmd-project-create-scripts"),
32 projectId: asProjectId("project-scripts"),
33 title: "Scripts",
34 workspaceRoot: "/tmp/scripts",
35 createdAt: now,
36 },
37 readModel,
38 });
39
40 const event = Array.isArray(result) ? result[0] : result;
41 expect(event.type).toBe("project.created");
42 expect((event.payload as { scripts: unknown[] }).scripts).toEqual([]);
43 }),
44 );
45
46 it.effect("propagates scripts in project.meta.update payload", () =>
47 Effect.gen(function* () {
48 const now = "2026-01-01T00:00:00.000Z";
49 const initial = createEmptyReadModel(now);
50 const readModel = yield* projectEvent(initial, {
51 sequence: 1,
52 eventId: asEventId("evt-project-create-scripts"),
53 aggregateKind: "project",
54 aggregateId: asProjectId("project-scripts"),
55 type: "project.created",
56 occurredAt: now,
57 commandId: CommandId.make("cmd-project-create-scripts"),
58 causationEventId: null,
59 correlationId: CommandId.make("cmd-project-create-scripts"),
60 metadata: {},
61 payload: {
62 projectId: asProjectId("project-scripts"),
63 title: "Scripts",
64 workspaceRoot: "/tmp/scripts",
65 defaultModelSelection: null,
66 scripts: [],
67 createdAt: now,
68 updatedAt: now,
69 },
70 });
71
72 const scripts = [
73 {
74 id: "lint",
75 name: "Lint",
76 command: "bun run lint",
77 icon: "lint",
78 runOnWorktreeCreate: false,
79 },
80 ] as const;
81
82 const result = yield* decideOrchestrationCommand({
83 command: {
84 type: "project.meta.update",
85 commandId: CommandId.make("cmd-project-update-scripts"),
86 projectId: asProjectId("project-scripts"),
87 scripts: Array.from(scripts),
88 },
89 readModel,
90 });
91
92 const event = Array.isArray(result) ? result[0] : result;
93 expect(event.type).toBe("project.meta-updated");
94 expect((event.payload as { scripts?: unknown[] }).scripts).toEqual(scripts);
95 }),
96 );
97
98 const script = (id: string): ProjectScript => ({
99 id,
100 name: "Install dependencies",
101 command: "vp i",
102 icon: "configure",
103 runOnWorktreeCreate: false,
104 });
105
106 const projectWithScripts = (scripts: ReadonlyArray<ProjectScript>) => {
107 const now = "2026-01-01T00:00:00.000Z";
108 return projectEvent(createEmptyReadModel(now), {
109 sequence: 1,
110 eventId: asEventId("evt-legacy-scripts"),
111 aggregateKind: "project",
112 aggregateId: asProjectId("project-scripts"),
113 type: "project.created",
114 occurredAt: now,
115 commandId: CommandId.make("cmd-legacy-scripts"),
116 causationEventId: null,
117 correlationId: CommandId.make("cmd-legacy-scripts"),
118 metadata: {},
119 payload: {
120 projectId: asProjectId("project-scripts"),
121 title: "Scripts",
122 workspaceRoot: "/tmp/scripts",
123 defaultModelSelection: null,
124 scripts,
125 createdAt: now,
126 updatedAt: now,
127 },
128 });
129 };
130
131 for (const id of ["install-javascript-dependencies", "A", "a.b", "a b", "-a", "a".repeat(25)]) {
132 it.effect(`rejects a new script ID that cannot have a shortcut: ${id}`, () =>
133 Effect.gen(function* () {
134 const readModel = yield* projectWithScripts([]);
135 const failure = yield* Effect.flip(
136 decideOrchestrationCommand({
137 readModel,
138 command: {
139 type: "project.meta.update",
140 commandId: CommandId.make("cmd-invalid-script"),
141 projectId: asProjectId("project-scripts"),
142 scripts: [script("lint"), script(id)],
143 },
144 }),
145 );
146 expect(failure).toMatchObject({ _tag: "OrchestrationCommandInvariantError" });
147 expect(failure.message).toContain("Script ID");
148 expect(failure.message).toContain("24");
149 expect(readModel.projects[0]?.scripts).toEqual([]);
150 }),
151 );
152 }
153
154 it.effect("accepts a script ID at the shortcut length limit", () =>
155 Effect.gen(function* () {
156 const readModel = yield* projectWithScripts([]);
157 const scripts = [script("a".repeat(24))];
158 const result = yield* decideOrchestrationCommand({
159 readModel,
160 command: {
161 type: "project.meta.update",
162 commandId: CommandId.make("cmd-valid-script"),
163 projectId: asProjectId("project-scripts"),
164 scripts,
165 },
166 });
167 const event = Array.isArray(result) ? result[0] : result;
168 expect(event.payload).toMatchObject({ scripts });
169 }),
170 );
171
172 it.effect(
173 "keeps legacy scripts readable, editable and removable while allowing valid additions",
174 () =>
175 Effect.gen(function* () {
176 const legacy = script("install-javascript-dependencies");
177 const readModel = yield* projectWithScripts([legacy]);
178 expect(readModel.projects[0]?.scripts).toEqual([legacy]);
179 for (const scripts of [[{ ...legacy, command: "vp install" }, script("lint")], []]) {
180 const result = yield* decideOrchestrationCommand({
181 readModel,
182 command: {
183 type: "project.meta.update",
184 commandId: CommandId.make("cmd-repair-script"),
185 projectId: asProjectId("project-scripts"),
186 scripts,
187 },
188 });
189 const event = Array.isArray(result) ? result[0] : result;
190 expect(event.payload).toMatchObject({ scripts });
191 }
192 const failure = yield* Effect.flip(
193 decideOrchestrationCommand({
194 readModel,
195 command: {
196 type: "project.meta.update",
197 commandId: CommandId.make("cmd-new-invalid-script"),
198 projectId: asProjectId("project-scripts"),
199 scripts: [legacy, script("another.invalid.id")],
200 },
201 }),
202 );
203 expect(failure).toMatchObject({ _tag: "OrchestrationCommandInvariantError" });
204 }),
205 );
206
207 it.effect("propagates project icon metadata in project.meta.update", () =>
208 Effect.gen(function* () {
209 const now = "2026-01-01T00:00:00.000Z";
210 const readModel = yield* projectEvent(createEmptyReadModel(now), {
211 sequence: 1,
212 eventId: asEventId("evt-project-create-favicon"),
213 aggregateKind: "project",
214 aggregateId: asProjectId("project-favicon"),
215 type: "project.created",
216 occurredAt: now,
217 commandId: CommandId.make("cmd-project-create-favicon"),
218 causationEventId: null,
219 correlationId: CommandId.make("cmd-project-create-favicon"),
220 metadata: {},
221 payload: {
222 projectId: asProjectId("project-favicon"),
223 title: "Favicon",
224 workspaceRoot: "/tmp/favicon",
225 defaultModelSelection: null,
226 scripts: [],
227 createdAt: now,
228 updatedAt: now,
229 },
230 });
231
232 const result = yield* decideOrchestrationCommand({
233 command: {
234 type: "project.meta.update",
235 commandId: CommandId.make("cmd-project-update-favicon"),
236 projectId: asProjectId("project-favicon"),
237 faviconPath: "brand/icon.svg",
238 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
239 },
240 readModel,
241 });
242
243 const event = Array.isArray(result) ? result[0] : result;
244 expect(event.type).toBe("project.meta-updated");
245 expect((event.payload as { faviconPath?: string }).faviconPath).toBe("brand/icon.svg");
246 expect((event.payload as { projectIcon?: unknown }).projectIcon).toEqual({
247 kind: "lucide",
248 name: "alarm-clock",
249 color: "violet",
250 });
251
252 for (const text of ["T3", "e\u0301", "किखि", "क्ष्म", "\u1100\u1161\u11a8"]) {
253 const monogram = { kind: "monogram", text, color: "violet" } as const;
254 const result = yield* decideOrchestrationCommand({
255 command: {
256 type: "project.meta.update",
257 commandId: CommandId.make("cmd-monogram"),
258 projectId: asProjectId("project-favicon"),
259 projectIcon: monogram,
260 },
261 readModel,
262 });
263 const updated = Array.isArray(result) ? result[0] : result;
264 expect(updated.payload).toMatchObject({ projectIcon: monogram });
265 }
266 for (const text of ["ABC", "किखिगि"]) {
267 const failure = yield* Effect.flip(
268 decideOrchestrationCommand({
269 command: {
270 type: "project.meta.update",
271 commandId: CommandId.make("cmd-monogram-invalid"),
272 projectId: asProjectId("project-favicon"),
273 projectIcon: { kind: "monogram", text, color: "violet" },
274 },
275 readModel,
276 }),
277 );
278 expect(failure).toMatchObject({ _tag: "OrchestrationCommandInvariantError" });
279 }
280 }),
281 );
282
283 it.effect("rejects project.create for an active workspace root that already exists", () =>
284 Effect.gen(function* () {
285 const now = "2026-01-01T00:00:00.000Z";
286 const initial = createEmptyReadModel(now);
287 const readModel = yield* projectEvent(initial, {
288 sequence: 1,
289 eventId: asEventId("evt-project-create"),
290 aggregateKind: "project",
291 aggregateId: asProjectId("project-existing"),
292 type: "project.created",
293 occurredAt: now,
294 commandId: CommandId.make("cmd-project-create"),
295 causationEventId: null,
296 correlationId: CommandId.make("cmd-project-create"),
297 metadata: {},
298 payload: {
299 projectId: asProjectId("project-existing"),
300 title: "Project",
301 workspaceRoot: "/tmp/project",
302 defaultModelSelection: null,
303 scripts: [],
304 createdAt: now,
305 updatedAt: now,
306 },
307 });
308
309 const failure = yield* Effect.flip(
310 decideOrchestrationCommand({
311 command: {
312 type: "project.create",
313 commandId: CommandId.make("cmd-project-create-duplicate-root"),
314 projectId: asProjectId("project-duplicate-root"),
315 title: "Duplicate Project",
316 workspaceRoot: "/tmp/project/",
317 createdAt: now,
318 },
319 readModel,
320 }),
321 );
322
323 expect(failure.message).toContain(
324 "Active project 'project-existing' already exists for workspace root '/tmp/project'.",
325 );
326 }),
327 );
328
329 it.effect("rejects project.meta.update when moving onto another active workspace root", () =>
330 Effect.gen(function* () {
331 const now = "2026-01-01T00:00:00.000Z";
332 const initial = createEmptyReadModel(now);
333 const withFirstProject = yield* projectEvent(initial, {
334 sequence: 1,
335 eventId: asEventId("evt-project-create-first"),
336 aggregateKind: "project",
337 aggregateId: asProjectId("project-first"),
338 type: "project.created",
339 occurredAt: now,
340 commandId: CommandId.make("cmd-project-create-first"),
341 causationEventId: null,
342 correlationId: CommandId.make("cmd-project-create-first"),
343 metadata: {},
344 payload: {
345 projectId: asProjectId("project-first"),
346 title: "First",
347 workspaceRoot: "/tmp/project-first",
348 defaultModelSelection: null,
349 scripts: [],
350 createdAt: now,
351 updatedAt: now,
352 },
353 });
354 const readModel = yield* projectEvent(withFirstProject, {
355 sequence: 2,
356 eventId: asEventId("evt-project-create-second"),
357 aggregateKind: "project",
358 aggregateId: asProjectId("project-second"),
359 type: "project.created",
360 occurredAt: now,
361 commandId: CommandId.make("cmd-project-create-second"),
362 causationEventId: null,
363 correlationId: CommandId.make("cmd-project-create-second"),
364 metadata: {},
365 payload: {
366 projectId: asProjectId("project-second"),
367 title: "Second",
368 workspaceRoot: "/tmp/project-second",
369 defaultModelSelection: null,
370 scripts: [],
371 createdAt: now,
372 updatedAt: now,
373 },
374 });
375
376 const failure = yield* Effect.flip(
377 decideOrchestrationCommand({
378 command: {
379 type: "project.meta.update",
380 commandId: CommandId.make("cmd-project-update-duplicate-root"),
381 projectId: asProjectId("project-second"),
382 workspaceRoot: "/tmp/project-first",
383 },
384 readModel,
385 }),
386 );
387
388 expect(failure.message).toContain(
389 "Active project 'project-first' already exists for workspace root '/tmp/project-first'.",
390 );
391 }),
392 );
393
394 it.effect("emits user message and turn-start-requested events for thread.turn.start", () =>
395 Effect.gen(function* () {
396 const now = "2026-01-01T00:00:00.000Z";
397 const initial = createEmptyReadModel(now);
398 const withProject = yield* projectEvent(initial, {
399 sequence: 1,
400 eventId: asEventId("evt-project-create"),
401 aggregateKind: "project",
402 aggregateId: asProjectId("project-1"),
403 type: "project.created",
404 occurredAt: now,
405 commandId: CommandId.make("cmd-project-create"),
406 causationEventId: null,
407 correlationId: CommandId.make("cmd-project-create"),
408 metadata: {},
409 payload: {
410 projectId: asProjectId("project-1"),
411 title: "Project",
412 workspaceRoot: "/tmp/project",
413 defaultModelSelection: null,
414 scripts: [],
415 createdAt: now,
416 updatedAt: now,
417 },
418 });
419 const readModel = yield* projectEvent(withProject, {
420 sequence: 2,
421 eventId: asEventId("evt-thread-create"),
422 aggregateKind: "thread",
423 aggregateId: ThreadId.make("thread-1"),
424 type: "thread.created",
425 occurredAt: now,
426 commandId: CommandId.make("cmd-thread-create"),
427 causationEventId: null,
428 correlationId: CommandId.make("cmd-thread-create"),
429 metadata: {},
430 payload: {
431 threadId: ThreadId.make("thread-1"),
432 projectId: asProjectId("project-1"),
433 title: "Thread",
434 modelSelection: {
435 instanceId: ProviderInstanceId.make("codex"),
436 model: "gpt-5-codex",
437 },
438 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
439 runtimeMode: "approval-required",
440 branch: null,
441 worktreePath: null,
442 createdAt: now,
443 updatedAt: now,
444 },
445 });
446
447 const result = yield* decideOrchestrationCommand({
448 command: {
449 type: "thread.turn.start",
450 commandId: CommandId.make("cmd-turn-start"),
451 threadId: ThreadId.make("thread-1"),
452 message: {
453 messageId: asMessageId("message-user-1"),
454 role: "user",
455 text: "hello",
456 attachments: [],
457 },
458 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
459 { id: "reasoningEffort", value: "high" },
460 { id: "fastMode", value: true },
461 ]),
462 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
463 runtimeMode: "approval-required",
464 createdAt: now,
465 },
466 readModel,
467 });
468
469 expect(Array.isArray(result)).toBe(true);
470 const events = Array.isArray(result) ? result : [result];
471 expect(events).toHaveLength(2);
472 expect(events[0]?.type).toBe("thread.message-sent");
473 const turnStartEvent = events[1];
474 expect(turnStartEvent?.type).toBe("thread.turn-start-requested");
475 expect(turnStartEvent?.causationEventId).toBe(events[0]?.eventId ?? null);
476 if (turnStartEvent?.type !== "thread.turn-start-requested") {
477 return;
478 }
479 expect(turnStartEvent.payload).toMatchObject({
480 threadId: ThreadId.make("thread-1"),
481 messageId: asMessageId("message-user-1"),
482 modelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.3-codex", [
483 { id: "reasoningEffort", value: "high" },
484 { id: "fastMode", value: true },
485 ]),
486 runtimeMode: "approval-required",
487 });
488 }),
489 );
490
491 it.effect("emits thread.runtime-mode-set from thread.runtime-mode.set", () =>
492 Effect.gen(function* () {
493 const now = "2026-01-01T00:00:00.000Z";
494 const initial = createEmptyReadModel(now);
495 const withProject = yield* projectEvent(initial, {
496 sequence: 1,
497 eventId: asEventId("evt-project-create"),
498 aggregateKind: "project",
499 aggregateId: asProjectId("project-1"),
500 type: "project.created",
501 occurredAt: now,
502 commandId: CommandId.make("cmd-project-create"),
503 causationEventId: null,
504 correlationId: CommandId.make("cmd-project-create"),
505 metadata: {},
506 payload: {
507 projectId: asProjectId("project-1"),
508 title: "Project",
509 workspaceRoot: "/tmp/project",
510 defaultModelSelection: null,
511 scripts: [],
512 createdAt: now,
513 updatedAt: now,
514 },
515 });
516 const readModel = yield* projectEvent(withProject, {
517 sequence: 2,
518 eventId: asEventId("evt-thread-create"),
519 aggregateKind: "thread",
520 aggregateId: ThreadId.make("thread-1"),
521 type: "thread.created",
522 occurredAt: now,
523 commandId: CommandId.make("cmd-thread-create"),
524 causationEventId: null,
525 correlationId: CommandId.make("cmd-thread-create"),
526 metadata: {},
527 payload: {
528 threadId: ThreadId.make("thread-1"),
529 projectId: asProjectId("project-1"),
530 title: "Thread",
531 modelSelection: {
532 instanceId: ProviderInstanceId.make("codex"),
533 model: "gpt-5-codex",
534 },
535 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
536 runtimeMode: "full-access",
537 branch: null,
538 worktreePath: null,
539 createdAt: now,
540 updatedAt: now,
541 },
542 });
543
544 const result = yield* decideOrchestrationCommand({
545 command: {
546 type: "thread.runtime-mode.set",
547 commandId: CommandId.make("cmd-runtime-mode-set"),
548 threadId: ThreadId.make("thread-1"),
549 runtimeMode: "approval-required",
550 createdAt: now,
551 },
552 readModel,
553 });
554
555 const singleResult = Array.isArray(result) ? null : result;
556 if (singleResult === null) {
557 throw new Error("Expected a single runtime-mode-set event.");
558 }
559 expect(singleResult).toMatchObject({
560 type: "thread.runtime-mode-set",
561 payload: {
562 threadId: ThreadId.make("thread-1"),
563 runtimeMode: "approval-required",
564 },
565 });
566 }),
567 );
568
569 it.effect("emits thread.interaction-mode-set from thread.interaction-mode.set", () =>
570 Effect.gen(function* () {
571 const now = "2026-01-01T00:00:00.000Z";
572 const initial = createEmptyReadModel(now);
573 const withProject = yield* projectEvent(initial, {
574 sequence: 1,
575 eventId: asEventId("evt-project-create"),
576 aggregateKind: "project",
577 aggregateId: asProjectId("project-1"),
578 type: "project.created",
579 occurredAt: now,
580 commandId: CommandId.make("cmd-project-create"),
581 causationEventId: null,
582 correlationId: CommandId.make("cmd-project-create"),
583 metadata: {},
584 payload: {
585 projectId: asProjectId("project-1"),
586 title: "Project",
587 workspaceRoot: "/tmp/project",
588 defaultModelSelection: null,
589 scripts: [],
590 createdAt: now,
591 updatedAt: now,
592 },
593 });
594 const readModel = yield* projectEvent(withProject, {
595 sequence: 2,
596 eventId: asEventId("evt-thread-create"),
597 aggregateKind: "thread",
598 aggregateId: ThreadId.make("thread-1"),
599 type: "thread.created",
600 occurredAt: now,
601 commandId: CommandId.make("cmd-thread-create"),
602 causationEventId: null,
603 correlationId: CommandId.make("cmd-thread-create"),
604 metadata: {},
605 payload: {
606 threadId: ThreadId.make("thread-1"),
607 projectId: asProjectId("project-1"),
608 title: "Thread",
609 modelSelection: {
610 instanceId: ProviderInstanceId.make("codex"),
611 model: "gpt-5-codex",
612 },
613 interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
614 runtimeMode: "approval-required",
615 branch: null,
616 worktreePath: null,
617 createdAt: now,
618 updatedAt: now,
619 },
620 });
621
622 const result = yield* decideOrchestrationCommand({
623 command: {
624 type: "thread.interaction-mode.set",
625 commandId: CommandId.make("cmd-interaction-mode-set"),
626 threadId: ThreadId.make("thread-1"),
627 interactionMode: "plan",
628 createdAt: now,
629 },
630 readModel,
631 });
632
633 const singleResult = Array.isArray(result) ? null : result;
634 if (singleResult === null) {
635 throw new Error("Expected a single interaction-mode-set event.");
636 }
637 expect(singleResult).toMatchObject({
638 type: "thread.interaction-mode-set",
639 payload: {
640 threadId: ThreadId.make("thread-1"),
641 interactionMode: "plan",
642 },
643 });
644 }),
645 );
646});