Open History to see the last commit
1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationCommand,
7 type OrchestrationReadModel,
8 type OrchestrationThread,
9} from "@t3tools/contracts";
10import * as NodeServices from "@effect/platform-node/NodeServices";
11import { expect, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13
14import { decideOrchestrationCommand } from "./decider.ts";
15import { projectEvent } from "./projector.ts";
16
17const NOW = "2026-01-01T00:00:00.000Z";
18// The Effect test clock starts at the epoch.
19const BEFORE_NOW = "1969-12-30T00:00:00.000Z";
20const SNOOZED_AT = "1969-12-31T00:00:00.000Z";
21const FUTURE_WAKE = "1970-01-02T00:00:00.000Z";
22const THREAD_ID = ThreadId.make("thread-1");
23
24function makeReadModel(overrides: Partial<OrchestrationThread> = {}): OrchestrationReadModel {
25 return {
26 snapshotSequence: 0,
27 projects: [],
28 threads: [
29 {
30 id: THREAD_ID,
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 pullRequests: [],
37 branch: null,
38 worktreePath: null,
39 latestTurn: null,
40 createdAt: NOW,
41 updatedAt: NOW,
42 archivedAt: null,
43 settledOverride: null,
44 settledAt: null,
45 unsettledAt: null,
46 activeOrderKey: null,
47 snoozedUntil: null,
48 snoozedAt: null,
49 pinnedAt: null,
50 pinOrderKey: null,
51 deletedAt: null,
52 messages: [],
53 proposedPlans: [],
54 activities: [],
55 checkpoints: [],
56 session: null,
57 ...overrides,
58 },
59 ],
60 updatedAt: NOW,
61 };
62}
63
64const reorderCommand = {
65 type: "thread.active.reorder",
66 commandId: CommandId.make("cmd-active-reorder"),
67 threadId: THREAD_ID,
68 orderKey: "m",
69} as const;
70
71it.layer(NodeServices.layer)("active thread ordering", (it) => {
72 it.effect("persists changed and repeated slots without changing thread activity timestamps", () =>
73 Effect.gen(function* () {
74 let readModel = makeReadModel({ unsettledAt: BEFORE_NOW });
75 for (const orderKey of ["m", "m", "g"]) {
76 const decided = yield* decideOrchestrationCommand({
77 command: { ...reorderCommand, orderKey },
78 readModel,
79 });
80 const events = Array.isArray(decided) ? decided : [decided];
81 expect(events).toHaveLength(1);
82 expect(events[0]).toMatchObject({
83 type: "thread.meta-updated",
84 payload: { threadId: THREAD_ID, activeOrderKey: orderKey, updatedAt: NOW },
85 });
86 for (const event of events) {
87 readModel = yield* projectEvent(readModel, {
88 ...event,
89 sequence: readModel.snapshotSequence + 1,
90 });
91 }
92 expect(readModel.threads[0]).toMatchObject({
93 activeOrderKey: orderKey,
94 updatedAt: NOW,
95 createdAt: NOW,
96 unsettledAt: BEFORE_NOW,
97 });
98 }
99 }),
100 );
101
102 for (const [label, overrides] of [
103 ["archived", { archivedAt: NOW }],
104 ["deleted", { deletedAt: NOW }],
105 ["pinned", { pinnedAt: NOW }],
106 ["settled", { settledOverride: "settled", settledAt: NOW }],
107 ] satisfies ReadonlyArray<readonly [string, Partial<OrchestrationThread>]>) {
108 it.effect(`rejects reordering a ${label} thread`, () =>
109 Effect.gen(function* () {
110 const error = yield* decideOrchestrationCommand({
111 command: reorderCommand,
112 readModel: makeReadModel(overrides),
113 }).pipe(Effect.flip);
114 expect(error._tag).toBe("OrchestrationCommandInvariantError");
115 }),
116 );
117 }
118
119 it.effect("reorders a running thread without affecting its session", () =>
120 Effect.gen(function* () {
121 const readModel = makeReadModel({
122 session: {
123 threadId: THREAD_ID,
124 status: "running",
125 providerName: "codex",
126 runtimeMode: "full-access",
127 activeTurnId: null,
128 lastError: null,
129 updatedAt: NOW,
130 },
131 });
132 const decided = yield* decideOrchestrationCommand({ command: reorderCommand, readModel });
133 const events = Array.isArray(decided) ? decided : [decided];
134 expect(events).toHaveLength(1);
135 for (const event of events) {
136 const projected = yield* projectEvent(readModel, { ...event, sequence: 1 });
137 expect(projected.threads[0]).toEqual({ ...readModel.threads[0], activeOrderKey: "m" });
138 }
139 }),
140 );
141
142 it.effect(
143 "changes a snoozed thread's retained slot without waking it or changing timestamps",
144 () =>
145 Effect.gen(function* () {
146 const readModel = makeReadModel({
147 activeOrderKey: "g",
148 snoozedAt: SNOOZED_AT,
149 snoozedUntil: FUTURE_WAKE,
150 unsettledAt: BEFORE_NOW,
151 });
152 const decided = yield* decideOrchestrationCommand({ command: reorderCommand, readModel });
153 const events = Array.isArray(decided) ? decided : [decided];
154 expect(events).toHaveLength(1);
155 for (const event of events) {
156 const projected = yield* projectEvent(readModel, { ...event, sequence: 1 });
157 expect(projected.threads[0]).toEqual({ ...readModel.threads[0], activeOrderKey: "m" });
158 }
159 }),
160 );
161
162 it.effect("keeps placement through metadata, pin and snooze, then resets it on settlement", () =>
163 Effect.gen(function* () {
164 let readModel = makeReadModel();
165 const steps = [
166 [reorderCommand, "m"],
167 [{ type: "thread.meta.update", title: "Renamed" }, "m"],
168 [{ type: "thread.pin", orderKey: "g" }, "m"],
169 [{ type: "thread.snooze", snoozedUntil: FUTURE_WAKE }, "m"],
170 [{ type: "thread.unsnooze", reason: "user" }, "m"],
171 [{ type: "thread.unpin" }, "m"],
172 [{ type: "thread.settle" }, null],
173 [{ type: "thread.unsettle", reason: "user" }, null],
174 [{ type: "thread.active.reorder", orderKey: "s" }, "s"],
175 ] as const;
176 for (const [index, [step, expectedKey]] of steps.entries()) {
177 const command: OrchestrationCommand = {
178 ...step,
179 commandId: CommandId.make(`lifecycle-${index}`),
180 threadId: THREAD_ID,
181 };
182 const decided = yield* decideOrchestrationCommand({ command, readModel });
183 const events = Array.isArray(decided) ? decided : [decided];
184 for (const event of events) {
185 readModel = yield* projectEvent(readModel, {
186 ...event,
187 sequence: readModel.snapshotSequence + 1,
188 });
189 }
190 expect(readModel.threads[0]?.activeOrderKey, command.type).toBe(expectedKey);
191 }
192 expect(readModel.threads[0]).toMatchObject({
193 title: "Renamed",
194 settledOverride: "active",
195 settledAt: null,
196 snoozedUntil: null,
197 pinnedAt: null,
198 });
199 }),
200 );
201});