Open History to see the last commit
1import {
2 CommandId,
3 MessageId,
4 ProjectId,
5 ProviderInstanceId,
6 ThreadId,
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 { OrchestrationCommandInvariantError } from "./Errors.ts";
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 a
19// window that still has time left must sit after 1970-01-01T00:00:00.000Z.
20const RESETS_AT = "1970-01-02T09:00:00.000Z";
21
22function makeThread(input: {
23 readonly sessionStatus?: OrchestrationThread["session"] extends null
24 ? never
25 : NonNullable<OrchestrationThread["session"]>["status"];
26 readonly lastErrorKind?: "usage_limit";
27 readonly lastErrorResetsAt?: string | null;
28 readonly usageLimitResumeAt?: string | null;
29}): OrchestrationThread {
30 const sessionError = input.sessionStatus === "error";
31 return {
32 id: ThreadId.make("thread-1"),
33 projectId: ProjectId.make("project-1"),
34 title: "Thread",
35 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
36 runtimeMode: "full-access",
37 interactionMode: "default",
38 branch: null,
39 worktreePath: null,
40 pullRequests: [],
41 latestTurn: null,
42 createdAt: NOW,
43 updatedAt: NOW,
44 archivedAt: null,
45 settledOverride: null,
46 settledAt: null,
47 snoozedUntil: null,
48 snoozedAt: null,
49 deletedAt: null,
50 messages: [],
51 proposedPlans: [],
52 activities: [],
53 checkpoints: [],
54 usageLimitResumeAt: input.usageLimitResumeAt ?? null,
55 session: input.sessionStatus
56 ? {
57 threadId: ThreadId.make("thread-1"),
58 status: input.sessionStatus,
59 providerName: "claude",
60 runtimeMode: "full-access",
61 activeTurnId: null,
62 lastError: sessionError ? "Claude usage limit reached." : null,
63 ...(input.lastErrorKind !== undefined ? { lastErrorKind: input.lastErrorKind } : {}),
64 ...(input.lastErrorResetsAt !== undefined
65 ? { lastErrorResetsAt: input.lastErrorResetsAt }
66 : {}),
67 updatedAt: NOW,
68 }
69 : null,
70 };
71}
72
73function makeReadModel(thread: OrchestrationThread): OrchestrationReadModel {
74 return {
75 snapshotSequence: 0,
76 projects: [],
77 threads: [thread],
78 updatedAt: NOW,
79 };
80}
81
82const armCommand = {
83 type: "thread.usage-resume.arm" as const,
84 commandId: CommandId.make("cmd-arm"),
85 threadId: ThreadId.make("thread-1"),
86 resumeAt: RESETS_AT,
87 createdAt: NOW,
88};
89
90it.layer(NodeServices.layer)("usage-limit resume decider", (it) => {
91 it.effect("arms a resume for a thread parked on a usage-limit failure", () =>
92 Effect.gen(function* () {
93 const event = yield* decideOrchestrationCommand({
94 command: armCommand,
95 readModel: makeReadModel(
96 makeThread({
97 sessionStatus: "error",
98 lastErrorKind: "usage_limit",
99 lastErrorResetsAt: RESETS_AT,
100 }),
101 ),
102 });
103 expect(event).toMatchObject({ type: "thread.usage-resume-armed" });
104 }),
105 );
106
107 it.effect("rejects arming a thread with no usage-limit failure", () =>
108 Effect.gen(function* () {
109 const error = yield* decideOrchestrationCommand({
110 command: armCommand,
111 readModel: makeReadModel(makeThread({ sessionStatus: "ready" })),
112 }).pipe(Effect.flip);
113 expect(error).toBeInstanceOf(OrchestrationCommandInvariantError);
114 }),
115 );
116
117 it.effect("rejects arming a failed thread whose error is not a usage limit", () =>
118 Effect.gen(function* () {
119 const error = yield* decideOrchestrationCommand({
120 command: armCommand,
121 readModel: makeReadModel(makeThread({ sessionStatus: "error" })),
122 }).pipe(Effect.flip);
123 expect(error).toBeInstanceOf(OrchestrationCommandInvariantError);
124 }),
125 );
126
127 it.effect("rejects a resume time that does not match the failure window", () =>
128 Effect.gen(function* () {
129 const error = yield* decideOrchestrationCommand({
130 command: { ...armCommand, resumeAt: "1970-01-03T09:00:00.000Z" },
131 readModel: makeReadModel(
132 makeThread({
133 sessionStatus: "error",
134 lastErrorKind: "usage_limit",
135 lastErrorResetsAt: RESETS_AT,
136 }),
137 ),
138 }).pipe(Effect.flip);
139 expect(error).toBeInstanceOf(OrchestrationCommandInvariantError);
140 }),
141 );
142
143 it.effect("judges the window against the server clock, not the command stamp", () =>
144 Effect.gen(function* () {
145 // RESETS_AT is in the future relative to the test clock (epoch), but a
146 // client with a stale command stamp must not sneak a past window by.
147 const error = yield* decideOrchestrationCommand({
148 command: { ...armCommand, resumeAt: "1969-12-31T00:00:00.000Z", createdAt: NOW },
149 readModel: makeReadModel(
150 makeThread({
151 sessionStatus: "error",
152 lastErrorKind: "usage_limit",
153 lastErrorResetsAt: "1969-12-31T00:00:00.000Z",
154 }),
155 ),
156 }).pipe(Effect.flip);
157 expect(error).toBeInstanceOf(OrchestrationCommandInvariantError);
158 }),
159 );
160
161 it.effect("disarms on a user turn start", () =>
162 Effect.gen(function* () {
163 const events = yield* decideOrchestrationCommand({
164 command: {
165 type: "thread.turn.start",
166 commandId: CommandId.make("cmd-turn"),
167 threadId: ThreadId.make("thread-1"),
168 message: {
169 messageId: MessageId.make("msg-1"),
170 role: "user",
171 text: "take over manually",
172 attachments: [],
173 },
174 runtimeMode: "full-access",
175 interactionMode: "default",
176 createdAt: NOW,
177 },
178 readModel: makeReadModel(
179 makeThread({
180 sessionStatus: "error",
181 lastErrorKind: "usage_limit",
182 lastErrorResetsAt: RESETS_AT,
183 usageLimitResumeAt: RESETS_AT,
184 }),
185 ),
186 });
187 const list = Array.isArray(events) ? events : [events];
188 expect(list.map((event) => event.type)).toContain("thread.usage-resume-disarmed");
189 }),
190 );
191});