Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadManagementService.test.ts

320 lines10.4 KB

1import { expect, it } from "@effect/vitest";
2import {
3 CommandId,
4 MessageId,
5 NodeId,
6 type OrchestrationV2Command,
7 type OrchestrationV2ThreadProjection,
8 ProjectId,
9 ProviderInstanceId,
10 RunId,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15​
16import { OrchestratorProjectionError, OrchestratorV2 } from "./Orchestrator.ts";
17import {
18 existingThreadIdsForCommand,
19 layer,
20 ThreadManagementDurableRunProjectionError,
21 ThreadManagementProjectThreadsListError,
22 ThreadManagementProjectionLoadError,
23 ThreadManagementRunNotFoundError,
24 ThreadManagementService,
25 ThreadManagementThreadNotFoundError,
26 ThreadManagementThreadNotInterruptibleError,
27 ThreadManagementThreadArchivedError,
28 ThreadManagementNoSteerableRunError,
29 withCreationProvenance,
30} from "./ThreadManagementService.ts";
31​
32it("stamps authoritative provenance on commands that create threads or messages", () => {
33 const command: OrchestrationV2Command = {
34 type: "thread.create",
35 createdBy: "agent",
36 creationSource: "mcp",
37 commandId: CommandId.make("command:thread-management:create"),
38 threadId: ThreadId.make("thread:thread-management:create"),
39 projectId: ProjectId.make("project:thread-management"),
40 title: "Thread management",
41 modelSelection: {
42 instanceId: ProviderInstanceId.make("codex"),
43 model: "gpt-5-codex",
44 },
45 runtimeMode: "full-access",
46 interactionMode: "default",
47 branch: null,
48 worktreePath: null,
49 };
50​
51 expect(
52 withCreationProvenance(command, {
53 createdBy: "user",
54 creationSource: "web",
55 }),
56 ).toMatchObject({
57 createdBy: "user",
58 creationSource: "web",
59 });
60});
61​
62it("leaves commands that do not create durable authored content unchanged", () => {
63 const command: OrchestrationV2Command = {
64 type: "run.interrupt",
65 commandId: CommandId.make("command:thread-management:interrupt"),
66 threadId: ThreadId.make("thread:thread-management:interrupt"),
67 runId: RunId.make("run:thread-management:interrupt"),
68 };
69​
70 expect(
71 withCreationProvenance(command, {
72 createdBy: "user",
73 creationSource: "web",
74 }),
75 ).toBe(command);
76});
77​
78it("identifies every existing thread that must be hydrated before dispatch", () => {
79 const sourceThreadId = ThreadId.make("thread:thread-management:source");
80 const targetThreadId = ThreadId.make("thread:thread-management:target");
81 const parentThreadId = ThreadId.make("thread:thread-management:parent");
82​
83 expect(
84 existingThreadIdsForCommand({
85 type: "thread.create",
86 createdBy: "user",
87 creationSource: "web",
88 commandId: CommandId.make("command:thread-management:create"),
89 threadId: targetThreadId,
90 projectId: ProjectId.make("project:thread-management"),
91 title: "Created thread",
92 modelSelection: {
93 instanceId: ProviderInstanceId.make("codex"),
94 model: "gpt-5-codex",
95 },
96 runtimeMode: "full-access",
97 interactionMode: "default",
98 branch: null,
99 worktreePath: null,
100 }),
101 ).toEqual([]);
102​
103 expect(
104 existingThreadIdsForCommand({
105 type: "thread.archive",
106 commandId: CommandId.make("command:thread-management:archive"),
107 threadId: targetThreadId,
108 }),
109 ).toEqual([targetThreadId]);
110​
111 // Read-state commands skip transcript hydration entirely: they fire on
112 // every activity bump while a thread is open and never touch messages.
113 expect(
114 existingThreadIdsForCommand({
115 type: "thread.visit",
116 commandId: CommandId.make("command:thread-management:visit"),
117 threadId: targetThreadId,
118 visitedAt: "2026-07-30T00:00:00.000Z",
119 }),
120 ).toEqual([]);
121​
122 expect(
123 existingThreadIdsForCommand({
124 type: "thread.mark-unread",
125 commandId: CommandId.make("command:thread-management:mark-unread"),
126 threadId: targetThreadId,
127 }),
128 ).toEqual([]);
129​
130 expect(
131 existingThreadIdsForCommand({
132 type: "thread.fork",
133 createdBy: "user",
134 creationSource: "web",
135 commandId: CommandId.make("command:thread-management:fork"),
136 sourceThreadId,
137 targetThreadId,
138 sourcePoint: {
139 type: "run",
140 runId: RunId.make("run:thread-management:source"),
141 },
142 }),
143 ).toEqual([sourceThreadId]);
144​
145 expect(
146 existingThreadIdsForCommand({
147 type: "thread.merge_back",
148 createdBy: "user",
149 creationSource: "web",
150 commandId: CommandId.make("command:thread-management:merge"),
151 sourceThreadId,
152 targetThreadId,
153 sourcePoint: {
154 type: "run",
155 runId: RunId.make("run:thread-management:source"),
156 },
157 }),
158 ).toEqual([sourceThreadId, targetThreadId]);
159​
160 expect(
161 existingThreadIdsForCommand({
162 type: "delegated_task.request",
163 createdBy: "agent",
164 creationSource: "provider",
165 commandId: CommandId.make("command:thread-management:delegate"),
166 parentThreadId,
167 parentRunId: RunId.make("run:thread-management:parent"),
168 parentNodeId: NodeId.make("node:thread-management:parent"),
169 task: "Inspect the migration",
170 modelSelection: {
171 instanceId: ProviderInstanceId.make("codex"),
172 model: "gpt-5-codex",
173 },
174 runtimeMode: "full-access",
175 interactionMode: "default",
176 }),
177 ).toEqual([parentThreadId]);
178​
179 expect(
180 existingThreadIdsForCommand({
181 type: "delegated_task.wake-policy",
182 commandId: CommandId.make("command:thread-management:wake-policy"),
183 parentThreadId,
184 taskId: NodeId.make("node:thread-management:delegated"),
185 completionWake: "always",
186 }),
187 ).toEqual([parentThreadId]);
188​
189 expect(
190 existingThreadIdsForCommand({
191 type: "thread.created.record",
192 commandId: CommandId.make("command:thread-management:record"),
193 parentThreadId,
194 parentRunId: RunId.make("run:thread-management:parent"),
195 parentNodeId: NodeId.make("node:thread-management:parent"),
196 targetThreadId,
197 targetRunId: null,
198 }),
199 ).toEqual([parentThreadId, targetThreadId]);
200});
201​
202it("derives thread management messages from structural error attributes", () => {
203 const projectId = ProjectId.make("project:thread-management:errors");
204 const threadId = ThreadId.make("thread:thread-management:errors");
205 const runId = RunId.make("run:thread-management:errors");
206 const messageId = MessageId.make("message:thread-management:errors");
207 const infrastructureCause = new Error("private sqlite detail");
208​
209 const threadNotFound = new ThreadManagementThreadNotFoundError({
210 projectId,
211 threadId,
212 });
213 expect(threadNotFound).toMatchObject({ projectId, threadId });
214 expect(threadNotFound.message).toBe(`Thread ${threadId} was not found in project ${projectId}.`);
215​
216 const runNotFound = new ThreadManagementRunNotFoundError({ threadId, runId });
217 expect(runNotFound).toMatchObject({ threadId, runId });
218 expect(runNotFound.message).toBe(`Run ${runId} does not belong to thread ${threadId}.`);
219​
220 const archived = new ThreadManagementThreadArchivedError({
221 threadId,
222 });
223 expect(archived).toMatchObject({ threadId });
224 expect(archived.message).toBe(`Thread ${threadId} is archived and cannot receive messages.`);
225​
226 const notSteerable = new ThreadManagementNoSteerableRunError({
227 threadId,
228 mode: "restart",
229 });
230 expect(notSteerable).toMatchObject({
231 threadId,
232 mode: "restart",
233 });
234 expect(notSteerable.message).toBe(
235 `Thread ${threadId} has no running turn that can be restarted.`,
236 );
237​
238 const notInterruptible = new ThreadManagementThreadNotInterruptibleError({
239 threadId,
240 runId,
241 });
242 expect(notInterruptible).toMatchObject({ threadId, runId });
243 expect(notInterruptible.message).toBe(`Run ${runId} is not currently interruptible.`);
244​
245 const listFailure = new ThreadManagementProjectThreadsListError({
246 projectId,
247 cause: infrastructureCause,
248 });
249 expect(listFailure).toMatchObject({ projectId, cause: infrastructureCause });
250 expect(listFailure.message).toBe(`Unable to list threads in project ${projectId}.`);
251 expect(listFailure.message).not.toContain(infrastructureCause.message);
252​
253 const durableProjectionFailure = new ThreadManagementDurableRunProjectionError({
254 threadId,
255 messageId,
256 });
257 expect(durableProjectionFailure).toMatchObject({ threadId, messageId });
258 expect(durableProjectionFailure.message).toBe(
259 `Message ${messageId} was accepted on thread ${threadId} without a durable run projection.`,
260 );
261});
262​
263it.effect("classifies projection infrastructure failures separately from a missing thread", () => {
264 const projectId = ProjectId.make("project:thread-management:projection-failure");
265 const threadId = ThreadId.make("thread:thread-management:projection-failure");
266 const infrastructureCause = new Error("sqlite read failed");
267 const projectionError = new OrchestratorProjectionError({
268 threadId,
269 cause: infrastructureCause,
270 });
271 const testLayer = layer.pipe(
272 Layer.provide(
273 Layer.mock(OrchestratorV2)({
274 getThreadProjection: () => Effect.fail(projectionError),
275 }),
276 ),
277 );
278​
279 return Effect.gen(function* () {
280 const service = yield* ThreadManagementService;
281 const error = yield* Effect.flip(service.getProjectThread({ projectId, threadId }));
282​
283 expect(error).toBeInstanceOf(ThreadManagementProjectionLoadError);
284 expect(error).toMatchObject({
285 projectId,
286 threadId,
287 cause: projectionError,
288 });
289 expect(error.message).toBe(`Unable to load thread ${threadId} in project ${projectId}.`);
290 }).pipe(Effect.provide(testLayer));
291});
292​
293it.effect("uses thread-not-found only after a projection loads outside the project", () => {
294 const projectId = ProjectId.make("project:thread-management:requested");
295 const otherProjectId = ProjectId.make("project:thread-management:other");
296 const threadId = ThreadId.make("thread:thread-management:wrong-project");
297 const projection = {
298 thread: {
299 id: threadId,
300 projectId: otherProjectId,
301 deletedAt: null,
302 },
303 } as OrchestrationV2ThreadProjection;
304 const testLayer = layer.pipe(
305 Layer.provide(
306 Layer.mock(OrchestratorV2)({
307 getThreadProjection: () => Effect.succeed(projection),
308 }),
309 ),
310 );
311​
312 return Effect.gen(function* () {
313 const service = yield* ThreadManagementService;
314 const error = yield* Effect.flip(service.getProjectThread({ projectId, threadId }));
315​
316 expect(error).toBeInstanceOf(ThreadManagementThreadNotFoundError);
317 expect(error).toMatchObject({ projectId, threadId });
318 expect("cause" in error).toBe(false);
319 }).pipe(Effect.provide(testLayer));
320});