Open History to see the last commit
1import {
2 CommandId,
3 EventId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 type OrchestrationReadModel,
9 type OrchestrationThread,
10} from "@t3tools/contracts";
11import * as NodeServices from "@effect/platform-node/NodeServices";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14
15import { decideOrchestrationCommand } from "./decider.ts";
16
17const NOW = "2026-01-01T00:00:00.000Z";
18// The decider's clock is the Effect test clock, pinned to the epoch, so
19// "future" wake times are relative to 1970-01-01T00:00:00.000Z.
20const FUTURE_WAKE = "1970-01-02T09:00:00.000Z";
21const PAST_WAKE = "1969-12-31T09:00:00.000Z";
22const SNOOZED_AT = "1969-12-30T00:00:00.000Z";
23
24function makeReadModel(input: {
25 readonly snoozedUntil?: string | null;
26 readonly snoozedAt?: string | null;
27 readonly archivedAt?: string | null;
28 readonly activities?: OrchestrationThread["activities"];
29 readonly messages?: OrchestrationThread["messages"];
30}): OrchestrationReadModel {
31 return {
32 snapshotSequence: 0,
33 projects: [],
34 threads: [
35 {
36 id: ThreadId.make("thread-1"),
37 projectId: ProjectId.make("project-1"),
38 title: "Thread",
39 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
40 runtimeMode: "full-access",
41 interactionMode: "default",
42 branch: null,
43 worktreePath: null,
44 latestTurn: null,
45 createdAt: NOW,
46 updatedAt: NOW,
47 archivedAt: input.archivedAt ?? null,
48 settledOverride: null,
49 settledAt: null,
50 snoozedUntil: input.snoozedUntil ?? null,
51 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null),
52 deletedAt: null,
53 messages: input.messages ?? [],
54 proposedPlans: [],
55 activities: input.activities ?? [],
56 checkpoints: [],
57 session: null,
58 },
59 ],
60 updatedAt: NOW,
61 };
62}
63
64it.layer(NodeServices.layer)("snoozed thread decider", (it) => {
65 it.effect("snoozes a thread to a future wake time", () =>
66 Effect.gen(function* () {
67 const event = yield* decideOrchestrationCommand({
68 command: {
69 type: "thread.snooze",
70 commandId: CommandId.make("cmd-snooze"),
71 threadId: ThreadId.make("thread-1"),
72 snoozedUntil: FUTURE_WAKE,
73 },
74 readModel: makeReadModel({}),
75 });
76 const events = Array.isArray(event) ? event : [event];
77 expect(events).toHaveLength(1);
78 expect(events[0]?.type).toBe("thread.snoozed");
79 if (events[0]?.type === "thread.snoozed") {
80 expect(events[0].payload.snoozedUntil).toBe(FUTURE_WAKE);
81 expect(events[0].payload.snoozedAt).toBe(events[0].payload.updatedAt);
82 }
83 }),
84 );
85
86 it.effect("rejects a wake time that is not in the future", () =>
87 Effect.gen(function* () {
88 const error = yield* decideOrchestrationCommand({
89 command: {
90 type: "thread.snooze",
91 commandId: CommandId.make("cmd-snooze-past"),
92 threadId: ThreadId.make("thread-1"),
93 snoozedUntil: PAST_WAKE,
94 },
95 readModel: makeReadModel({}),
96 }).pipe(Effect.flip);
97 expect(error._tag).toBe("OrchestrationCommandInvariantError");
98 }),
99 );
100
101 it.effect("rejects an unparseable wake time", () =>
102 Effect.gen(function* () {
103 // IsoDateTime is structurally a string, so garbage can reach the
104 // decider; a NaN wake time must never persist as snooze state.
105 const error = yield* decideOrchestrationCommand({
106 command: {
107 type: "thread.snooze",
108 commandId: CommandId.make("cmd-snooze-garbage"),
109 threadId: ThreadId.make("thread-1"),
110 snoozedUntil: "not-a-date",
111 },
112 readModel: makeReadModel({}),
113 }).pipe(Effect.flip);
114 expect(error._tag).toBe("OrchestrationCommandInvariantError");
115 }),
116 );
117
118 it.effect("rejects snoozing blocked-on-you work", () =>
119 Effect.gen(function* () {
120 const requestActivity = {
121 id: EventId.make("activity-req-1"),
122 tone: "approval" as const,
123 kind: "approval.requested",
124 summary: "approval.requested",
125 payload: { requestId: "req-1" },
126 turnId: null,
127 createdAt: NOW,
128 } as OrchestrationThread["activities"][number];
129 const error = yield* decideOrchestrationCommand({
130 command: {
131 type: "thread.snooze",
132 commandId: CommandId.make("cmd-snooze-blocked"),
133 threadId: ThreadId.make("thread-1"),
134 snoozedUntil: FUTURE_WAKE,
135 },
136 readModel: makeReadModel({ activities: [requestActivity] }),
137 }).pipe(Effect.flip);
138 expect(error._tag).toBe("OrchestrationCommandInvariantError");
139 }),
140 );
141
142 it.effect("re-emits idempotently for a duplicate snooze to the same wake time", () =>
143 Effect.gen(function* () {
144 const reEmit = yield* decideOrchestrationCommand({
145 command: {
146 type: "thread.snooze",
147 commandId: CommandId.make("cmd-snooze-again"),
148 threadId: ThreadId.make("thread-1"),
149 snoozedUntil: FUTURE_WAKE,
150 },
151 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
152 });
153 const events = Array.isArray(reEmit) ? reEmit : [reEmit];
154 expect(events).toHaveLength(1);
155 if (events[0]?.type === "thread.snoozed") {
156 // Original snoozedAt preserved; updatedAt must not churn.
157 expect(events[0].payload.snoozedAt).toBe(SNOOZED_AT);
158 expect(events[0].payload.updatedAt).toBe(NOW);
159 }
160 }),
161 );
162
163 it.effect("re-snoozing to a DIFFERENT wake time stamps fresh", () =>
164 Effect.gen(function* () {
165 const event = yield* decideOrchestrationCommand({
166 command: {
167 type: "thread.snooze",
168 commandId: CommandId.make("cmd-snooze-extend"),
169 threadId: ThreadId.make("thread-1"),
170 snoozedUntil: "1970-01-03T09:00:00.000Z",
171 },
172 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
173 });
174 const events = Array.isArray(event) ? event : [event];
175 if (events[0]?.type === "thread.snoozed") {
176 expect(events[0].payload.snoozedUntil).toBe("1970-01-03T09:00:00.000Z");
177 expect(events[0].payload.updatedAt).not.toBe(NOW);
178 }
179 }),
180 );
181
182 it.effect("unsnoozes with reason user and re-emits idempotently when awake", () =>
183 Effect.gen(function* () {
184 const event = yield* decideOrchestrationCommand({
185 command: {
186 type: "thread.unsnooze",
187 commandId: CommandId.make("cmd-unsnooze"),
188 threadId: ThreadId.make("thread-1"),
189 reason: "user",
190 },
191 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
192 });
193 const events = Array.isArray(event) ? event : [event];
194 expect(events[0]?.type).toBe("thread.unsnoozed");
195 if (events[0]?.type === "thread.unsnoozed") {
196 expect(events[0].payload.reason).toBe("user");
197 expect(events[0].payload.updatedAt).not.toBe(NOW);
198 }
199
200 const awake = yield* decideOrchestrationCommand({
201 command: {
202 type: "thread.unsnooze",
203 commandId: CommandId.make("cmd-unsnooze-awake"),
204 threadId: ThreadId.make("thread-1"),
205 reason: "user",
206 },
207 readModel: makeReadModel({}),
208 });
209 const awakeEvents = Array.isArray(awake) ? awake : [awake];
210 expect(awakeEvents[0]?.type).toBe("thread.unsnoozed");
211 if (awakeEvents[0]?.type === "thread.unsnoozed") {
212 // No state change — keep the existing updatedAt.
213 expect(awakeEvents[0].payload.updatedAt).toBe(NOW);
214 }
215 }),
216 );
217
218 it.effect("rejects snoozing a thread with a queued turn start", () =>
219 Effect.gen(function* () {
220 // The decider clock is the Effect test clock pinned to the epoch: a
221 // user message 30s before it with no adopting turn is queued work.
222 const queuedMessage = {
223 id: MessageId.make("message-queued"),
224 role: "user",
225 text: "Continue",
226 turnId: null,
227 streaming: false,
228 createdAt: "1969-12-31T23:59:30.000Z",
229 updatedAt: "1969-12-31T23:59:30.000Z",
230 } as OrchestrationThread["messages"][number];
231 const error = yield* decideOrchestrationCommand({
232 command: {
233 type: "thread.snooze",
234 commandId: CommandId.make("cmd-snooze-queued"),
235 threadId: ThreadId.make("thread-1"),
236 snoozedUntil: FUTURE_WAKE,
237 },
238 readModel: makeReadModel({ messages: [queuedMessage] }),
239 }).pipe(Effect.flip);
240 expect(error._tag).toBe("OrchestrationCommandInvariantError");
241 }),
242 );
243
244 it.effect("rejects snoozing an archived thread", () =>
245 Effect.gen(function* () {
246 const error = yield* decideOrchestrationCommand({
247 command: {
248 type: "thread.snooze",
249 commandId: CommandId.make("cmd-snooze-archived"),
250 threadId: ThreadId.make("thread-1"),
251 snoozedUntil: FUTURE_WAKE,
252 },
253 readModel: makeReadModel({ archivedAt: NOW }),
254 }).pipe(Effect.flip);
255 expect(error._tag).toBe("OrchestrationCommandInvariantError");
256 }),
257 );
258
259 it.effect("a user message spends the snooze return ticket (activity wake)", () =>
260 Effect.gen(function* () {
261 const result = yield* decideOrchestrationCommand({
262 command: {
263 type: "thread.turn.start",
264 commandId: CommandId.make("cmd-turn-start"),
265 threadId: ThreadId.make("thread-1"),
266 message: {
267 messageId: MessageId.make("message-1"),
268 role: "user",
269 text: "Continue",
270 attachments: [],
271 },
272 runtimeMode: "full-access",
273 interactionMode: "default",
274 createdAt: NOW,
275 },
276 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
277 });
278 const events = Array.isArray(result) ? result : [result];
279 const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed");
280 expect(unsnoozed).toBeDefined();
281 if (unsnoozed?.type === "thread.unsnoozed") {
282 expect(unsnoozed.payload.reason).toBe("activity");
283 }
284 }),
285 );
286});