@@ -15,6 +15,7 @@ import {
1515import * as Crypto from "effect/Crypto" ;
1616import * as DateTime from "effect/DateTime" ;
1717import * as Effect from "effect/Effect" ;
18+ import * as Exit from "effect/Exit" ;
1819import * as Option from "effect/Option" ;
1920
2021import type { ProviderServiceShape } from "../provider/Services/ProviderService.ts" ;
@@ -33,13 +34,14 @@ function isImportableDriver(driver: string): driver is ImportableDriver {
3334
3435const nowIso = Effect . map ( DateTime . now , DateTime . formatIso ) ;
3536
36- const importFailure = ( message : string ) => ( cause : unknown ) =>
37- new OrchestrationImportThreadError ( { message, cause } ) ;
38-
3937const 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
4547const 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