Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 canDetachThreadProviderSession,
5 canForkProjectedAssistantItem,
6 deriveThreadQueueWorkflowState,
7 resolveLatestMergeBackRun,
8} from "./threadWorkflows.ts";
9
10const capabilities = (input?: {
11 readonly queued?: boolean;
12 readonly steer?: boolean;
13 readonly restartSteer?: boolean;
14 readonly nativeFork?: boolean;
15 readonly portableFork?: boolean;
16}) =>
17 ({
18 turns: {
19 supportsQueuedMessages: input?.queued ?? false,
20 supportsActiveSteering: input?.steer ?? false,
21 supportsSteeringByInterruptRestart: input?.restartSteer ?? false,
22 },
23 threads: {
24 canForkThread: input?.nativeFork ?? false,
25 canForkFromTurn: input?.nativeFork ?? false,
26 },
27 identity: { nativeThreadIds: input?.nativeFork ? "strong" : "none" },
28 context: { supportsFullThreadHandoff: input?.portableFork ?? false },
29 }) as never;
30
31describe("thread workflows", () => {
32 it("sorts queued messages and gates reorder and promotion from capabilities", () => {
33 const state = deriveThreadQueueWorkflowState({
34 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
35 runs: [
36 {
37 id: "active",
38 status: "running",
39 providerThreadId: "provider-thread",
40 activeAttemptId: "attempt-active",
41 ordinal: 1,
42 },
43 { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
44 {
45 id: "first",
46 status: "queued",
47 userMessageId: "message-first",
48 ordinal: 2,
49 queuePosition: 1,
50 },
51 ],
52 messages: [
53 { id: "message-first", text: "First" },
54 { id: "message-later", text: "Later" },
55 ],
56 providerTurns: [
57 {
58 id: "provider-turn-active",
59 runAttemptId: "attempt-active",
60 status: "running",
61 },
62 ],
63 providerThreads: [
64 {
65 id: "provider-thread",
66 appThreadId: "thread",
67 providerSessionId: "provider-session",
68 },
69 ],
70 providerSessions: [
71 {
72 id: "provider-session",
73 status: "running",
74 capabilities: capabilities({ queued: true, restartSteer: true }),
75 },
76 ],
77 } as never);
78
79 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
80 ["first", "First"],
81 ["later", "Later"],
82 ]);
83 expect(state.activeRun?.id).toBe("active");
84 expect(state.canReorder).toBe(true);
85 expect(state.canPromoteToSteer).toBe(true);
86 });
87
88 it("removes only the promoted head from the visible queue", () => {
89 const state = deriveThreadQueueWorkflowState({
90 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
91 runs: [
92 {
93 id: "promoted",
94 status: "starting",
95 userMessageId: "message-promoted",
96 providerThreadId: "provider-thread",
97 ordinal: 2,
98 queuePosition: null,
99 },
100 {
101 id: "still-queued",
102 status: "queued",
103 userMessageId: "message-still-queued",
104 providerThreadId: "provider-thread",
105 ordinal: 3,
106 queuePosition: 2,
107 },
108 ],
109 messages: [
110 { id: "message-promoted", text: "Run now" },
111 { id: "message-still-queued", text: "Wait longer" },
112 ],
113 providerTurns: [],
114 providerThreads: [],
115 providerSessions: [],
116 } as never);
117
118 expect(state.activeRun?.id).toBe("promoted");
119 expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
120 ["still-queued", "Wait longer"],
121 ]);
122 });
123
124 it.each(["preparing", "starting", "waiting"] as const)(
125 "does not promote queued work into a %s run",
126 (status) => {
127 const state = deriveThreadQueueWorkflowState({
128 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
129 runs: [
130 {
131 id: "active",
132 status,
133 providerThreadId: "provider-thread",
134 activeAttemptId: "attempt-active",
135 ordinal: 1,
136 },
137 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
138 ],
139 messages: [{ id: "message", text: "Queued" }],
140 providerTurns: [
141 {
142 id: "provider-turn-active",
143 runAttemptId: "attempt-active",
144 status: status === "waiting" ? "completed" : "starting",
145 },
146 ],
147 providerThreads: [
148 {
149 id: "provider-thread",
150 appThreadId: "thread",
151 providerSessionId: "provider-session",
152 },
153 ],
154 providerSessions: [
155 {
156 id: "provider-session",
157 status: "running",
158 capabilities: capabilities({ queued: true, steer: true }),
159 },
160 ],
161 } as never);
162
163 expect(state.canPromoteToSteer).toBe(false);
164 },
165 );
166
167 it("does not promote queued work until the running provider turn is projected", () => {
168 const state = deriveThreadQueueWorkflowState({
169 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
170 runs: [
171 {
172 id: "active",
173 status: "running",
174 providerThreadId: "provider-thread",
175 activeAttemptId: "attempt-active",
176 ordinal: 1,
177 },
178 { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
179 ],
180 messages: [{ id: "message", text: "Queued" }],
181 providerTurns: [],
182 providerThreads: [
183 {
184 id: "provider-thread",
185 appThreadId: "thread",
186 providerSessionId: "provider-session",
187 },
188 ],
189 providerSessions: [
190 {
191 id: "provider-session",
192 status: "running",
193 capabilities: capabilities({ queued: true, steer: true }),
194 },
195 ],
196 } as never);
197
198 expect(state.canPromoteToSteer).toBe(false);
199 });
200
201 it("does not expose known unsupported queue or fork actions", () => {
202 const projection = {
203 thread: { id: "thread", activeProviderThreadId: "provider-thread" },
204 runs: [{ id: "queued", status: "queued", userMessageId: "message", ordinal: 1 }],
205 messages: [],
206 providerThreads: [
207 {
208 id: "provider-thread",
209 appThreadId: "thread",
210 providerSessionId: "provider-session",
211 },
212 ],
213 providerSessions: [
214 {
215 id: "provider-session",
216 status: "ready",
217 capabilities: capabilities(),
218 },
219 ],
220 } as never;
221 const queue = deriveThreadQueueWorkflowState(projection);
222 const projectedItem = {
223 item: { type: "assistant_message", runId: "run", status: "completed" },
224 } as never;
225
226 expect(queue.canReorder).toBe(false);
227 expect(queue.canPromoteToSteer).toBe(false);
228 expect(canForkProjectedAssistantItem({ projectedItem, capabilities: capabilities() })).toBe(
229 false,
230 );
231 expect(canDetachThreadProviderSession(projection)).toBe(true);
232 });
233
234 it("allows native, portable, and capability-unknown exact-run forks", () => {
235 const projectedItem = {
236 item: { type: "assistant_message", runId: "run", status: "completed" },
237 } as never;
238
239 expect(
240 canForkProjectedAssistantItem({
241 projectedItem,
242 capabilities: capabilities({ nativeFork: true }),
243 }),
244 ).toBe(true);
245 expect(
246 canForkProjectedAssistantItem({
247 projectedItem,
248 capabilities: capabilities({ portableFork: true }),
249 }),
250 ).toBe(true);
251 expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
252 expect(
253 canForkProjectedAssistantItem({
254 projectedItem: {
255 item: { type: "assistant_message", runId: "run", status: "running" },
256 } as never,
257 }),
258 ).toBe(false);
259 });
260
261 it("merges the newest provider-finished run while checkpoint capture is pending", () => {
262 const projection = {
263 runs: [
264 { id: "newest-queued", status: "queued", ordinal: 3 },
265 { id: "older-completed", status: "completed", ordinal: 1 },
266 { id: "newest-finished", status: "waiting", ordinal: 2 },
267 ],
268 } as never;
269
270 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
271 });
272
273 it("does not let a stale completed run later in storage order hide the waiting checkpoint", () => {
274 const projection = {
275 runs: [
276 { id: "newest-finished", status: "waiting", ordinal: 2 },
277 { id: "older-completed", status: "completed", ordinal: 1 },
278 ],
279 } as never;
280
281 expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
282 });
283
284 it.each(["preparing", "starting", "running"] as const)(
285 "does not merge older history while a newer run is %s",
286 (status) => {
287 const projection = {
288 runs: [
289 { id: "older-completed", status: "completed", ordinal: 1 },
290 { id: "newer-active", status, ordinal: 2 },
291 ],
292 } as never;
293
294 expect(resolveLatestMergeBackRun(projection)).toBeNull();
295 },
296 );
297});