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 NOW = "2026-01-01T00:00:00.000Z";
15const PINNED_AT = "1969-12-30T00:00:00.000Z";
16
17function makeReadModel(input: {
18 readonly pinnedAt?: string | null;
19 readonly pinOrderKey?: string | null;
20 readonly archivedAt?: string | null;
21 readonly settledOverride?: "settled" | "active" | null;
22 readonly settledAt?: string | null;
23 readonly snoozedUntil?: string | null;
24 readonly snoozedAt?: string | null;
25}): OrchestrationReadModel {
26 return {
27 snapshotSequence: 0,
28 projects: [],
29 threads: [
30 {
31 id: ThreadId.make("thread-1"),
32 projectId: ProjectId.make("project-1"),
33 title: "Thread",
34 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
35 runtimeMode: "full-access",
36 interactionMode: "default",
37 branch: null,
38 worktreePath: null,
39 latestTurn: null,
40 createdAt: NOW,
41 updatedAt: NOW,
42 archivedAt: input.archivedAt ?? null,
43 settledOverride: input.settledOverride ?? null,
44 settledAt: input.settledAt ?? (input.settledOverride === "settled" ? NOW : null),
45 snoozedUntil: input.snoozedUntil ?? null,
46 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? PINNED_AT : null),
47 pinnedAt: input.pinnedAt ?? null,
48 pinOrderKey: input.pinOrderKey ?? null,
49 deletedAt: null,
50 messages: [],
51 proposedPlans: [],
52 activities: [],
53 checkpoints: [],
54 session: null,
55 },
56 ],
57 updatedAt: NOW,
58 };
59}
60
61it.layer(NodeServices.layer)("pinned thread decider", (it) => {
62 it.effect("pins a thread, stamping pinnedAt and updatedAt together", () =>
63 Effect.gen(function* () {
64 const event = yield* decideOrchestrationCommand({
65 command: {
66 type: "thread.pin",
67 commandId: CommandId.make("cmd-pin"),
68 threadId: ThreadId.make("thread-1"),
69 },
70 readModel: makeReadModel({}),
71 });
72 const events = Array.isArray(event) ? event : [event];
73 expect(events).toHaveLength(1);
74 expect(events[0]?.type).toBe("thread.pinned");
75 if (events[0]?.type === "thread.pinned") {
76 expect(events[0].payload.pinnedAt).toBe(events[0].payload.updatedAt);
77 }
78 }),
79 );
80
81 it.effect("re-pinning preserves the original pinnedAt and updatedAt", () =>
82 Effect.gen(function* () {
83 const event = yield* decideOrchestrationCommand({
84 command: {
85 type: "thread.pin",
86 commandId: CommandId.make("cmd-pin-again"),
87 threadId: ThreadId.make("thread-1"),
88 },
89 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
90 });
91 const events = Array.isArray(event) ? event : [event];
92 expect(events[0]?.type).toBe("thread.pinned");
93 if (events[0]?.type === "thread.pinned") {
94 expect(events[0].payload.pinnedAt).toBe(PINNED_AT);
95 expect(events[0].payload.updatedAt).toBe(NOW);
96 }
97 }),
98 );
99
100 it.effect("unpins a pinned thread", () =>
101 Effect.gen(function* () {
102 const event = yield* decideOrchestrationCommand({
103 command: {
104 type: "thread.unpin",
105 commandId: CommandId.make("cmd-unpin"),
106 threadId: ThreadId.make("thread-1"),
107 },
108 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
109 });
110 const events = Array.isArray(event) ? event : [event];
111 expect(events[0]?.type).toBe("thread.unpinned");
112 if (events[0]?.type === "thread.unpinned") {
113 expect(events[0].payload.updatedAt).not.toBe(NOW);
114 }
115 }),
116 );
117
118 it.effect("unpinning an unpinned thread preserves updatedAt", () =>
119 Effect.gen(function* () {
120 const event = yield* decideOrchestrationCommand({
121 command: {
122 type: "thread.unpin",
123 commandId: CommandId.make("cmd-unpin-noop"),
124 threadId: ThreadId.make("thread-1"),
125 },
126 readModel: makeReadModel({}),
127 });
128 const events = Array.isArray(event) ? event : [event];
129 expect(events[0]?.type).toBe("thread.unpinned");
130 if (events[0]?.type === "thread.unpinned") {
131 expect(events[0].payload.updatedAt).toBe(NOW);
132 }
133 }),
134 );
135
136 it.effect("pinning a settled thread also un-settles it", () =>
137 Effect.gen(function* () {
138 const event = yield* decideOrchestrationCommand({
139 command: {
140 type: "thread.pin",
141 commandId: CommandId.make("cmd-pin-settled"),
142 threadId: ThreadId.make("thread-1"),
143 },
144 readModel: makeReadModel({ settledOverride: "settled" }),
145 });
146 const events = Array.isArray(event) ? event : [event];
147 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsettled"]);
148 const unsettled = events.find((entry) => entry.type === "thread.unsettled");
149 if (unsettled?.type === "thread.unsettled") {
150 expect(unsettled.payload.reason).toBe("user");
151 }
152 }),
153 );
154
155 it.effect("pinning a snoozed thread also wakes it", () =>
156 Effect.gen(function* () {
157 const event = yield* decideOrchestrationCommand({
158 command: {
159 type: "thread.pin",
160 commandId: CommandId.make("cmd-pin-snoozed"),
161 threadId: ThreadId.make("thread-1"),
162 },
163 readModel: makeReadModel({ snoozedUntil: "1970-01-02T09:00:00.000Z" }),
164 });
165 const events = Array.isArray(event) ? event : [event];
166 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsnoozed"]);
167 }),
168 );
169
170 it.effect("pinning an unparked thread emits only thread.pinned", () =>
171 Effect.gen(function* () {
172 const event = yield* decideOrchestrationCommand({
173 command: {
174 type: "thread.pin",
175 commandId: CommandId.make("cmd-pin-plain"),
176 threadId: ThreadId.make("thread-1"),
177 },
178 readModel: makeReadModel({}),
179 });
180 const events = Array.isArray(event) ? event : [event];
181 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned"]);
182 }),
183 );
184
185 it.effect("settling a pinned thread also unpins it", () =>
186 Effect.gen(function* () {
187 const event = yield* decideOrchestrationCommand({
188 command: {
189 type: "thread.settle",
190 commandId: CommandId.make("cmd-settle-pinned"),
191 threadId: ThreadId.make("thread-1"),
192 },
193 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
194 });
195 const events = Array.isArray(event) ? event : [event];
196 expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unpinned"]);
197 }),
198 );
199
200 it.effect("settling an unpinned thread emits no unpin event", () =>
201 Effect.gen(function* () {
202 const event = yield* decideOrchestrationCommand({
203 command: {
204 type: "thread.settle",
205 commandId: CommandId.make("cmd-settle-unpinned"),
206 threadId: ThreadId.make("thread-1"),
207 },
208 readModel: makeReadModel({}),
209 });
210 const events = Array.isArray(event) ? event : [event];
211 expect(events.map((entry) => entry.type)).toEqual(["thread.settled"]);
212 }),
213 );
214
215 it.effect("rejects pinning an archived thread", () =>
216 Effect.gen(function* () {
217 const error = yield* decideOrchestrationCommand({
218 command: {
219 type: "thread.pin",
220 commandId: CommandId.make("cmd-pin-archived"),
221 threadId: ThreadId.make("thread-1"),
222 },
223 readModel: makeReadModel({ archivedAt: NOW }),
224 }).pipe(Effect.flip);
225 expect(error._tag).toBe("OrchestrationCommandInvariantError");
226 }),
227 );
228
229 it.effect("a fresh pin carries the client's order key", () =>
230 Effect.gen(function* () {
231 const event = yield* decideOrchestrationCommand({
232 command: {
233 type: "thread.pin",
234 commandId: CommandId.make("cmd-pin-keyed"),
235 threadId: ThreadId.make("thread-1"),
236 orderKey: "g",
237 },
238 readModel: makeReadModel({}),
239 });
240 const events = Array.isArray(event) ? event : [event];
241 expect(events[0]?.type).toBe("thread.pinned");
242 if (events[0]?.type === "thread.pinned") {
243 expect(events[0].payload.pinOrderKey).toBe("g");
244 }
245 }),
246 );
247
248 it.effect(
249 "re-pinning ignores the incoming order key so raced pins cannot move a placed thread",
250 () =>
251 Effect.gen(function* () {
252 const event = yield* decideOrchestrationCommand({
253 command: {
254 type: "thread.pin",
255 commandId: CommandId.make("cmd-pin-keyed-again"),
256 threadId: ThreadId.make("thread-1"),
257 orderKey: "t",
258 },
259 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
260 });
261 const events = Array.isArray(event) ? event : [event];
262 expect(events[0]?.type).toBe("thread.pinned");
263 if (events[0]?.type === "thread.pinned") {
264 expect(events[0].payload.pinOrderKey).toBeUndefined();
265 }
266 }),
267 );
268
269 it.effect("reorders a pinned thread, stamping the new key", () =>
270 Effect.gen(function* () {
271 const event = yield* decideOrchestrationCommand({
272 command: {
273 type: "thread.pin.reorder",
274 commandId: CommandId.make("cmd-reorder"),
275 threadId: ThreadId.make("thread-1"),
276 orderKey: "m",
277 },
278 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
279 });
280 const events = Array.isArray(event) ? event : [event];
281 expect(events[0]?.type).toBe("thread.pin-reordered");
282 if (events[0]?.type === "thread.pin-reordered") {
283 expect(events[0].payload.orderKey).toBe("m");
284 // A real move stamps the command time (the test clock), not the
285 // thread's previous updatedAt.
286 expect(events[0].payload.updatedAt).not.toBe(NOW);
287 }
288 }),
289 );
290
291 it.effect("reordering onto the same key preserves updatedAt", () =>
292 Effect.gen(function* () {
293 const event = yield* decideOrchestrationCommand({
294 command: {
295 type: "thread.pin.reorder",
296 commandId: CommandId.make("cmd-reorder-noop"),
297 threadId: ThreadId.make("thread-1"),
298 orderKey: "g",
299 },
300 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
301 });
302 const events = Array.isArray(event) ? event : [event];
303 expect(events[0]?.type).toBe("thread.pin-reordered");
304 if (events[0]?.type === "thread.pin-reordered") {
305 expect(events[0].payload.updatedAt).toBe(NOW);
306 }
307 }),
308 );
309
310 it.effect("rejects reordering an unpinned thread", () =>
311 Effect.gen(function* () {
312 const error = yield* decideOrchestrationCommand({
313 command: {
314 type: "thread.pin.reorder",
315 commandId: CommandId.make("cmd-reorder-unpinned"),
316 threadId: ThreadId.make("thread-1"),
317 orderKey: "m",
318 },
319 readModel: makeReadModel({}),
320 }).pipe(Effect.flip);
321 expect(error._tag).toBe("OrchestrationCommandInvariantError");
322 }),
323 );
324});