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 archivedAt?: string | null;
20 readonly settledOverride?: "settled" | "active" | null;
21 readonly settledAt?: string | null;
22 readonly snoozedUntil?: string | null;
23 readonly snoozedAt?: string | null;
24}): OrchestrationReadModel {
25 return {
26 snapshotSequence: 0,
27 projects: [],
28 threads: [
29 {
30 id: ThreadId.make("thread-1"),
31 projectId: ProjectId.make("project-1"),
32 title: "Thread",
33 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
34 runtimeMode: "full-access",
35 interactionMode: "default",
36 branch: null,
37 worktreePath: null,
38 latestTurn: null,
39 createdAt: NOW,
40 updatedAt: NOW,
41 archivedAt: input.archivedAt ?? null,
42 settledOverride: input.settledOverride ?? null,
43 settledAt: input.settledAt ?? (input.settledOverride === "settled" ? NOW : null),
44 snoozedUntil: input.snoozedUntil ?? null,
45 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? PINNED_AT : null),
46 pinnedAt: input.pinnedAt ?? null,
47 deletedAt: null,
48 messages: [],
49 proposedPlans: [],
50 activities: [],
51 checkpoints: [],
52 session: null,
53 },
54 ],
55 updatedAt: NOW,
56 };
57}
58
59it.layer(NodeServices.layer)("pinned thread decider", (it) => {
60 it.effect("pins a thread, stamping pinnedAt and updatedAt together", () =>
61 Effect.gen(function* () {
62 const event = yield* decideOrchestrationCommand({
63 command: {
64 type: "thread.pin",
65 commandId: CommandId.make("cmd-pin"),
66 threadId: ThreadId.make("thread-1"),
67 },
68 readModel: makeReadModel({}),
69 });
70 const events = Array.isArray(event) ? event : [event];
71 expect(events).toHaveLength(1);
72 expect(events[0]?.type).toBe("thread.pinned");
73 if (events[0]?.type === "thread.pinned") {
74 expect(events[0].payload.pinnedAt).toBe(events[0].payload.updatedAt);
75 }
76 }),
77 );
78
79 it.effect("re-pinning preserves the original pinnedAt and updatedAt", () =>
80 Effect.gen(function* () {
81 const event = yield* decideOrchestrationCommand({
82 command: {
83 type: "thread.pin",
84 commandId: CommandId.make("cmd-pin-again"),
85 threadId: ThreadId.make("thread-1"),
86 },
87 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
88 });
89 const events = Array.isArray(event) ? event : [event];
90 expect(events[0]?.type).toBe("thread.pinned");
91 if (events[0]?.type === "thread.pinned") {
92 expect(events[0].payload.pinnedAt).toBe(PINNED_AT);
93 expect(events[0].payload.updatedAt).toBe(NOW);
94 }
95 }),
96 );
97
98 it.effect("unpins a pinned thread", () =>
99 Effect.gen(function* () {
100 const event = yield* decideOrchestrationCommand({
101 command: {
102 type: "thread.unpin",
103 commandId: CommandId.make("cmd-unpin"),
104 threadId: ThreadId.make("thread-1"),
105 },
106 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
107 });
108 const events = Array.isArray(event) ? event : [event];
109 expect(events[0]?.type).toBe("thread.unpinned");
110 if (events[0]?.type === "thread.unpinned") {
111 expect(events[0].payload.updatedAt).not.toBe(NOW);
112 }
113 }),
114 );
115
116 it.effect("unpinning an unpinned thread preserves updatedAt", () =>
117 Effect.gen(function* () {
118 const event = yield* decideOrchestrationCommand({
119 command: {
120 type: "thread.unpin",
121 commandId: CommandId.make("cmd-unpin-noop"),
122 threadId: ThreadId.make("thread-1"),
123 },
124 readModel: makeReadModel({}),
125 });
126 const events = Array.isArray(event) ? event : [event];
127 expect(events[0]?.type).toBe("thread.unpinned");
128 if (events[0]?.type === "thread.unpinned") {
129 expect(events[0].payload.updatedAt).toBe(NOW);
130 }
131 }),
132 );
133
134 it.effect("pinning a settled thread also un-settles it", () =>
135 Effect.gen(function* () {
136 const event = yield* decideOrchestrationCommand({
137 command: {
138 type: "thread.pin",
139 commandId: CommandId.make("cmd-pin-settled"),
140 threadId: ThreadId.make("thread-1"),
141 },
142 readModel: makeReadModel({ settledOverride: "settled" }),
143 });
144 const events = Array.isArray(event) ? event : [event];
145 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsettled"]);
146 const unsettled = events.find((entry) => entry.type === "thread.unsettled");
147 if (unsettled?.type === "thread.unsettled") {
148 expect(unsettled.payload.reason).toBe("user");
149 }
150 }),
151 );
152
153 it.effect("pinning a snoozed thread also wakes it", () =>
154 Effect.gen(function* () {
155 const event = yield* decideOrchestrationCommand({
156 command: {
157 type: "thread.pin",
158 commandId: CommandId.make("cmd-pin-snoozed"),
159 threadId: ThreadId.make("thread-1"),
160 },
161 readModel: makeReadModel({ snoozedUntil: "1970-01-02T09:00:00.000Z" }),
162 });
163 const events = Array.isArray(event) ? event : [event];
164 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsnoozed"]);
165 }),
166 );
167
168 it.effect("pinning an unparked thread emits only thread.pinned", () =>
169 Effect.gen(function* () {
170 const event = yield* decideOrchestrationCommand({
171 command: {
172 type: "thread.pin",
173 commandId: CommandId.make("cmd-pin-plain"),
174 threadId: ThreadId.make("thread-1"),
175 },
176 readModel: makeReadModel({}),
177 });
178 const events = Array.isArray(event) ? event : [event];
179 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned"]);
180 }),
181 );
182
183 it.effect("settling a pinned thread also unpins it", () =>
184 Effect.gen(function* () {
185 const event = yield* decideOrchestrationCommand({
186 command: {
187 type: "thread.settle",
188 commandId: CommandId.make("cmd-settle-pinned"),
189 threadId: ThreadId.make("thread-1"),
190 },
191 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
192 });
193 const events = Array.isArray(event) ? event : [event];
194 expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unpinned"]);
195 }),
196 );
197
198 it.effect("settling an unpinned thread emits no unpin event", () =>
199 Effect.gen(function* () {
200 const event = yield* decideOrchestrationCommand({
201 command: {
202 type: "thread.settle",
203 commandId: CommandId.make("cmd-settle-unpinned"),
204 threadId: ThreadId.make("thread-1"),
205 },
206 readModel: makeReadModel({}),
207 });
208 const events = Array.isArray(event) ? event : [event];
209 expect(events.map((entry) => entry.type)).toEqual(["thread.settled"]);
210 }),
211 );
212
213 it.effect("rejects pinning an archived thread", () =>
214 Effect.gen(function* () {
215 const error = yield* decideOrchestrationCommand({
216 command: {
217 type: "thread.pin",
218 commandId: CommandId.make("cmd-pin-archived"),
219 threadId: ThreadId.make("thread-1"),
220 },
221 readModel: makeReadModel({ archivedAt: NOW }),
222 }).pipe(Effect.flip);
223 expect(error._tag).toBe("OrchestrationCommandInvariantError");
224 }),
225 );
226});