Open History to see the last commit
1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationReadModel,
7} from "@t3tools/contracts";
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import { expect, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11
12import { decideOrchestrationCommand } from "./decider.ts";
13
14const UPDATED_AT = "2026-01-01T00:00:00.000Z";
15
16const readModel: OrchestrationReadModel = {
17 snapshotSequence: 0,
18 projects: [],
19 threads: [
20 {
21 id: ThreadId.make("thread-1"),
22 projectId: ProjectId.make("project-1"),
23 title: "Manual title",
24 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
25 runtimeMode: "full-access",
26 interactionMode: "default",
27 branch: null,
28 worktreePath: null,
29 pullRequests: [],
30 latestTurn: null,
31 createdAt: UPDATED_AT,
32 updatedAt: UPDATED_AT,
33 archivedAt: null,
34 settledOverride: null,
35 settledAt: null,
36 snoozedUntil: null,
37 snoozedAt: null,
38 deletedAt: null,
39 messages: [],
40 proposedPlans: [],
41 activities: [],
42 checkpoints: [],
43 session: null,
44 },
45 ],
46 updatedAt: UPDATED_AT,
47};
48
49it.layer(NodeServices.layer)("title regeneration decider", (it) => {
50 it.effect("preserves updatedAt for a stale completion", () =>
51 Effect.gen(function* () {
52 const result = yield* decideOrchestrationCommand({
53 command: {
54 type: "thread.title.regeneration.complete",
55 commandId: CommandId.make("cmd-regeneration-complete"),
56 threadId: ThreadId.make("thread-1"),
57 requestId: CommandId.make("cmd-old-regeneration-request"),
58 title: "Generated title",
59 },
60 readModel,
61 });
62 const event = Array.isArray(result) ? result[0] : result;
63
64 expect(event.type).toBe("thread.meta-updated");
65 if (event.type === "thread.meta-updated") {
66 expect(event.payload).toEqual({
67 threadId: ThreadId.make("thread-1"),
68 updatedAt: UPDATED_AT,
69 });
70 }
71 }),
72 );
73
74 it.effect("rejects an initial result after a manual rename to the same text", () =>
75 Effect.gen(function* () {
76 const result = yield* decideOrchestrationCommand({
77 command: {
78 type: "thread.title.generate.complete",
79 commandId: CommandId.make("generated"),
80 threadId: ThreadId.make("thread-1"),
81 expectedTitle: "Manual title",
82 expectedVersion: null,
83 title: "Automatic title",
84 needsRefinement: true,
85 },
86 readModel: {
87 ...readModel,
88 threads: readModel.threads.map((thread) => ({
89 ...thread,
90 titleState: {
91 source: "manual" as const,
92 version: CommandId.make("manual"),
93 needsRefinement: false,
94 },
95 })),
96 },
97 });
98 const event = Array.isArray(result) ? result[0] : result;
99 expect(event.payload).toEqual({ threadId: ThreadId.make("thread-1"), updatedAt: UPDATED_AT });
100 }),
101 );
102
103 it.effect("records manual ownership even when the title text does not change", () =>
104 Effect.gen(function* () {
105 const result = yield* decideOrchestrationCommand({
106 command: {
107 type: "thread.meta.update",
108 commandId: CommandId.make("manual-rename"),
109 threadId: ThreadId.make("thread-1"),
110 title: "Manual title",
111 },
112 readModel,
113 });
114 const event = Array.isArray(result) ? result[0] : result;
115 expect(event.payload).toMatchObject({
116 titleState: { source: "manual", version: "manual-rename", needsRefinement: false },
117 });
118 }),
119 );
120});