Skip to content

Commit db5946c

Browse files
committed
fix(orchestration): delete the created thread when an import is interrupted
tapError never runs on fiber interruption, so a client disconnecting mid import left the thread in the sidebar and its provider session running. onExit compensates on any non-success exit. Error construction moves to each failure boundary, per the service conventions, so the message and cause stay visible where they originate.
1 parent b5250c8 commit db5946c

2 files changed

Lines changed: 120 additions & 44 deletions

File tree

‎apps/server/src/orchestration/importThread.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { describe, expect, it } from "@effect/vitest";
1212
import * as Crypto from "effect/Crypto";
1313
import * as DateTime from "effect/DateTime";
1414
import * as Effect from "effect/Effect";
15+
import * as Fiber from "effect/Fiber";
1516
import * as Option from "effect/Option";
1617

1718
import { makeImportThread } from "./importThread.ts";
@@ -61,6 +62,7 @@ function makeHarness(options?: {
6162
readonly driverKind?: string;
6263
readonly items?: ReadonlyArray<unknown>;
6364
readonly snapshotWorkspaceRoot?: string;
65+
readonly startSessionNeverSettles?: boolean;
6466
}) {
6567
const dispatched: OrchestrationCommand[] = [];
6668
const instanceId = options?.instanceId ?? codexInstanceId;
@@ -90,7 +92,10 @@ function makeHarness(options?: {
9092
continuationKey: `${driverKind}:instance:${instanceId}`,
9193
},
9294
}),
93-
startSession: (threadId) => Effect.succeed({ ...codexSession, threadId }),
95+
startSession: (threadId) =>
96+
options?.startSessionNeverSettles === true
97+
? Effect.never
98+
: Effect.succeed({ ...codexSession, threadId }),
9499
readThread: (threadId) =>
95100
Effect.succeed({
96101
threadId,
@@ -192,6 +197,24 @@ describe("importThread", () => {
192197
}),
193198
);
194199

200+
it.effect("deletes the thread it created when the import is interrupted", () =>
201+
Effect.gen(function* () {
202+
const harness = makeHarness({ items: codexTranscript, startSessionNeverSettles: true });
203+
204+
const fiber = yield* Effect.forkChild(
205+
harness.importSession.importThread({
206+
projectId,
207+
modelSelection: codexModelSelection,
208+
externalId: "codex-thread-interrupted",
209+
}),
210+
);
211+
yield* Effect.yieldNow;
212+
yield* Fiber.interrupt(fiber);
213+
214+
expect(dispatchedTypes(harness.dispatched)).toEqual(["thread.create", "thread.delete"]);
215+
}),
216+
);
217+
195218
it.effect("refuses to import into a project that no longer exists", () =>
196219
Effect.gen(function* () {
197220
const harness = makeHarness({ projectMissing: true });

‎apps/server/src/orchestration/importThread.ts‎

Lines changed: 96 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import * as Crypto from "effect/Crypto";
1616
import * as DateTime from "effect/DateTime";
1717
import * as Effect from "effect/Effect";
18+
import * as Exit from "effect/Exit";
1819
import * as Option from "effect/Option";
1920

2021
import type { ProviderServiceShape } from "../provider/Services/ProviderService.ts";
@@ -33,13 +34,14 @@ function isImportableDriver(driver: string): driver is ImportableDriver {
3334

3435
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
3536

36-
const importFailure = (message: string) => (cause: unknown) =>
37-
new OrchestrationImportThreadError({ message, cause });
38-
3937
const readClaudeSessionInfo = (externalId: string) =>
4038
Effect.tryPromise({
4139
try: () => getSessionInfo(externalId, {}),
42-
catch: importFailure(`Could not read Claude Code session '${externalId}'.`),
40+
catch: (cause) =>
41+
new OrchestrationImportThreadError({
42+
message: `Could not read Claude Code session '${externalId}'.`,
43+
cause,
44+
}),
4345
});
4446

4547
const claudeSessionNotFound = (externalId: string) =>
@@ -86,9 +88,15 @@ export const makeImportThread = (dependencies: {
8688
const requireImportableDriver = Effect.fnUntraced(function* (
8789
instanceId: ProviderInstanceId,
8890
): Effect.fn.Return<ImportableDriver, OrchestrationImportThreadError> {
89-
const instanceInfo = yield* providerService
90-
.getInstanceInfo(instanceId)
91-
.pipe(Effect.mapError(importFailure(`Provider instance '${instanceId}' is not available.`)));
91+
const instanceInfo = yield* providerService.getInstanceInfo(instanceId).pipe(
92+
Effect.mapError(
93+
(cause) =>
94+
new OrchestrationImportThreadError({
95+
message: `Provider instance '${instanceId}' is not available.`,
96+
cause,
97+
}),
98+
),
99+
);
92100
const driver = instanceInfo.driverKind;
93101
if (isImportableDriver(driver)) {
94102
return driver;
@@ -109,9 +117,11 @@ export const makeImportThread = (dependencies: {
109117
if (input.driver === "claudeAgent") {
110118
const sessionMessages = yield* Effect.tryPromise({
111119
try: () => getSessionMessages(input.externalId, {}),
112-
catch: importFailure(
113-
`Could not read the transcript for Claude Code session '${input.externalId}'.`,
114-
),
120+
catch: (cause) =>
121+
new OrchestrationImportThreadError({
122+
message: `Could not read the transcript for Claude Code session '${input.externalId}'.`,
123+
cause,
124+
}),
115125
});
116126
return mapClaudeSessionMessages({
117127
threadId: input.threadId,
@@ -120,13 +130,15 @@ export const makeImportThread = (dependencies: {
120130
});
121131
}
122132

123-
const snapshot = yield* providerService
124-
.readThread(input.threadId)
125-
.pipe(
126-
Effect.mapError(
127-
importFailure(`Could not read the transcript for Codex thread '${input.externalId}'.`),
128-
),
129-
);
133+
const snapshot = yield* providerService.readThread(input.threadId).pipe(
134+
Effect.mapError(
135+
(cause) =>
136+
new OrchestrationImportThreadError({
137+
message: `Could not read the transcript for Codex thread '${input.externalId}'.`,
138+
cause,
139+
}),
140+
),
141+
);
130142
const snapshotWorkspaceRoot = snapshot.workspaceRoot?.trim() ?? "";
131143
if (snapshotWorkspaceRoot.length > 0 && snapshotWorkspaceRoot !== input.workspaceRoot) {
132144
return yield* new OrchestrationImportThreadError({
@@ -140,24 +152,29 @@ export const makeImportThread = (dependencies: {
140152
});
141153
});
142154

143-
const deletingCreatedThreadOnFailure = <A, E, R>(
155+
const deletingCreatedThreadUnlessImported = <A, E, R>(
144156
threadId: ThreadId,
145157
effect: Effect.Effect<A, E, R>,
146158
) =>
147159
effect.pipe(
148-
Effect.tapError(() =>
149-
commandId("delete").pipe(
150-
Effect.flatMap((deleteCommandId) =>
151-
orchestrationEngine.dispatch({
152-
type: "thread.delete",
153-
commandId: deleteCommandId,
154-
threadId,
155-
}),
156-
),
157-
Effect.catchCause((cause) =>
158-
Effect.logWarning("failed to delete thread after a failed import", { threadId, cause }),
159-
),
160-
),
160+
Effect.onExit((exit) =>
161+
Exit.isSuccess(exit)
162+
? Effect.void
163+
: commandId("delete").pipe(
164+
Effect.flatMap((deleteCommandId) =>
165+
orchestrationEngine.dispatch({
166+
type: "thread.delete",
167+
commandId: deleteCommandId,
168+
threadId,
169+
}),
170+
),
171+
Effect.catchCause((cause) =>
172+
Effect.logWarning("failed to delete thread after an abandoned import", {
173+
threadId,
174+
cause,
175+
}),
176+
),
177+
),
161178
),
162179
);
163180

@@ -188,7 +205,13 @@ export const makeImportThread = (dependencies: {
188205
.getActiveProjectByWorkspaceRoot(workspaceRoot)
189206
.pipe(
190207
Effect.map(Option.getOrUndefined),
191-
Effect.mapError(importFailure(`Could not look up a project for '${workspaceRoot}'.`)),
208+
Effect.mapError(
209+
(cause) =>
210+
new OrchestrationImportThreadError({
211+
message: `Could not look up a project for '${workspaceRoot}'.`,
212+
cause,
213+
}),
214+
),
192215
);
193216

194217
return {
@@ -202,12 +225,16 @@ export const makeImportThread = (dependencies: {
202225
const importThread = Effect.fnUntraced(function* (
203226
input: OrchestrationImportThreadInput,
204227
): Effect.fn.Return<OrchestrationImportThreadResult, OrchestrationImportThreadError> {
205-
const project = yield* projectionSnapshotQuery
206-
.getProjectShellById(input.projectId)
207-
.pipe(
208-
Effect.map(Option.getOrUndefined),
209-
Effect.mapError(importFailure(`Could not read project '${input.projectId}'.`)),
210-
);
228+
const project = yield* projectionSnapshotQuery.getProjectShellById(input.projectId).pipe(
229+
Effect.map(Option.getOrUndefined),
230+
Effect.mapError(
231+
(cause) =>
232+
new OrchestrationImportThreadError({
233+
message: `Could not read project '${input.projectId}'.`,
234+
cause,
235+
}),
236+
),
237+
);
211238
if (project === undefined) {
212239
return yield* new OrchestrationImportThreadError({
213240
message: `Project '${input.projectId}' no longer exists.`,
@@ -248,9 +275,17 @@ export const makeImportThread = (dependencies: {
248275
worktreePath: null,
249276
createdAt,
250277
})
251-
.pipe(Effect.mapError(importFailure("Could not create a thread for the imported session.")));
278+
.pipe(
279+
Effect.mapError(
280+
(cause) =>
281+
new OrchestrationImportThreadError({
282+
message: "Could not create a thread for the imported session.",
283+
cause,
284+
}),
285+
),
286+
);
252287

253-
yield* deletingCreatedThreadOnFailure(
288+
yield* deletingCreatedThreadUnlessImported(
254289
threadId,
255290
Effect.gen(function* () {
256291
const session = yield* providerService
@@ -268,7 +303,11 @@ export const makeImportThread = (dependencies: {
268303
})
269304
.pipe(
270305
Effect.mapError(
271-
importFailure(`Could not resume session '${input.externalId}' for this thread.`),
306+
(cause) =>
307+
new OrchestrationImportThreadError({
308+
message: `Could not resume session '${input.externalId}' for this thread.`,
309+
cause,
310+
}),
272311
),
273312
);
274313

@@ -294,7 +333,15 @@ export const makeImportThread = (dependencies: {
294333
messages,
295334
createdAt: importedAt,
296335
})
297-
.pipe(Effect.mapError(importFailure("Could not store the imported transcript.")));
336+
.pipe(
337+
Effect.mapError(
338+
(cause) =>
339+
new OrchestrationImportThreadError({
340+
message: "Could not store the imported transcript.",
341+
cause,
342+
}),
343+
),
344+
);
298345

299346
yield* orchestrationEngine
300347
.dispatch({
@@ -314,7 +361,13 @@ export const makeImportThread = (dependencies: {
314361
createdAt: yield* nowIso,
315362
})
316363
.pipe(
317-
Effect.mapError(importFailure("Could not bind the resumed session to this thread.")),
364+
Effect.mapError(
365+
(cause) =>
366+
new OrchestrationImportThreadError({
367+
message: "Could not bind the resumed session to this thread.",
368+
cause,
369+
}),
370+
),
318371
);
319372
}),
320373
);

0 commit comments

Comments
 (0)