@@ -12,6 +12,8 @@ import {
1212 type OrchestrationThread ,
1313} from "@t3tools/contracts" ;
1414import * as Effect from "effect/Effect" ;
15+ import * as Deferred from "effect/Deferred" ;
16+ import * as Fiber from "effect/Fiber" ;
1517import * as Layer from "effect/Layer" ;
1618import * as Option from "effect/Option" ;
1719import * as Stream from "effect/Stream" ;
@@ -29,7 +31,10 @@ import * as ThreadBackgroundLiveness from "../orchestration/ThreadBackgroundLive
2931import * as ThreadPlanProgress from "../orchestration/ThreadPlanProgress.ts" ;
3032import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts" ;
3133import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts" ;
32- import { ProviderSessionDirectoryLive } from "../provider/Layers/ProviderSessionDirectory.ts" ;
34+ import {
35+ makeProviderSessionDirectoryLive ,
36+ ProviderSessionDirectoryLive ,
37+ } from "../provider/Layers/ProviderSessionDirectory.ts" ;
3338import { ProviderSessionDirectoryPersistenceError } from "../provider/Errors.ts" ;
3439import * as ProviderSessionDirectory from "../provider/Services/ProviderSessionDirectory.ts" ;
3540import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts" ;
@@ -428,6 +433,7 @@ const integrationLayer = Layer.mergeAll(
428433 Layer . provide ( OrchestrationProjectionPipelineLive ) ,
429434 ) ,
430435 OrchestrationProjectionSnapshotQueryLive ,
436+ integrationRuntimeRepository ,
431437 ProviderSessionDirectoryLive . pipe ( Layer . provide ( integrationRuntimeRepository ) ) ,
432438 Layer . succeed ( AgentSessionScanner . AgentSessionScanner , integrationScanner ) ,
433439) . pipe (
@@ -505,4 +511,95 @@ it.layer(integrationLayer)("AgentSessionImporter integration", (it) => {
505511 ) ;
506512 } ) ,
507513 ) ;
514+
515+ it . effect ( "keeps a binding created while an import is in progress" , ( ) =>
516+ Effect . gen ( function * ( ) {
517+ const engine = yield * OrchestrationEngine . OrchestrationEngineService ;
518+ const snapshots = yield * ProjectionSnapshotQuery . ProjectionSnapshotQuery ;
519+ const directory = yield * ProviderSessionDirectory . ProviderSessionDirectory ;
520+ const repository = yield * ProviderSessionRuntime . ProviderSessionRuntimeRepository ;
521+ const projectId = ProjectId . make ( "project-import-binding-race" ) ;
522+ const workspaceRoot = "/tmp/project-import-binding-race" ;
523+ const providerSessionId = "codex-binding-race" ;
524+ const threadId = ThreadId . make ( `import:codex:${ providerSessionId } ` ) ;
525+ const scanner = AgentSessionScanner . AgentSessionScanner . of ( {
526+ scan : Effect . die ( "unused" ) ,
527+ recentThreads : ( ) =>
528+ Stream . succeed ( { ...integrationThread , providerSessionId, title : "Binding race" } ) ,
529+ } ) ;
530+ const importerAtBindingWrite = yield * Deferred . make < void > ( ) ;
531+ const releaseImporter = yield * Deferred . make < void > ( ) ;
532+ const importerRepository = ProviderSessionRuntime . ProviderSessionRuntimeRepository . of ( {
533+ ...repository ,
534+ upsert : ( runtime , options ) =>
535+ options ?. onConflict === "ignore"
536+ ? Deferred . succeed ( importerAtBindingWrite , undefined ) . pipe (
537+ Effect . andThen ( Deferred . await ( releaseImporter ) ) ,
538+ Effect . andThen ( repository . upsert ( runtime , options ) ) ,
539+ )
540+ : repository . upsert ( runtime , options ) ,
541+ } ) ;
542+ const importerDirectory = yield * ProviderSessionDirectory . ProviderSessionDirectory . pipe (
543+ Effect . provide (
544+ makeProviderSessionDirectoryLive ( ) . pipe (
545+ Layer . provide (
546+ Layer . succeed (
547+ ProviderSessionRuntime . ProviderSessionRuntimeRepository ,
548+ importerRepository ,
549+ ) ,
550+ ) ,
551+ ) ,
552+ ) ,
553+ ) ;
554+
555+ yield * engine . dispatch ( {
556+ type : "project.create" ,
557+ commandId : CommandId . make ( "create-import-binding-race-project" ) ,
558+ projectId,
559+ title : "Binding race" ,
560+ workspaceRoot,
561+ defaultModelSelection : null ,
562+ createdAt : "2026-08-24T09:00:00.000Z" ,
563+ } ) ;
564+
565+ const importFiber = yield * importRecentAgentThreads ( { projectId } ) . pipe (
566+ Effect . provideService ( AgentSessionScanner . AgentSessionScanner , scanner ) ,
567+ Effect . provideService ( ProviderSessionDirectory . ProviderSessionDirectory , importerDirectory ) ,
568+ Effect . forkChild ,
569+ ) ;
570+
571+ yield * Effect . raceFirst (
572+ Deferred . await ( importerAtBindingWrite ) ,
573+ Fiber . join ( importFiber ) . pipe (
574+ Effect . flatMap ( ( result ) =>
575+ Effect . die (
576+ new Error ( `Import completed before the binding write: ${ JSON . stringify ( result ) } ` ) ,
577+ ) ,
578+ ) ,
579+ ) ,
580+ ) ;
581+ expect (
582+ Option . getOrThrow ( yield * snapshots . getThreadDetailById ( threadId ) ) . messages . map (
583+ ( message ) => message . text ,
584+ ) ,
585+ ) . toEqual ( integrationThread . messages . map ( ( message ) => message . text ) ) ;
586+
587+ yield * directory . upsert ( {
588+ threadId,
589+ provider : ProviderDriverKind . make ( "codex" ) ,
590+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
591+ status : "running" ,
592+ resumeCursor : { threadId : "active-client-session" } ,
593+ runtimePayload : { cwd : workspaceRoot , activeTurnId : "turn-active" } ,
594+ } ) ;
595+ yield * Deferred . succeed ( releaseImporter , undefined ) ;
596+
597+ expect ( yield * Fiber . join ( importFiber ) ) . toEqual ( { importedCount : 1 , skippedCount : 0 } ) ;
598+ expect ( Option . getOrThrow ( yield * directory . getBinding ( threadId ) ) ) . toMatchObject ( {
599+ status : "running" ,
600+ resumeCursor : { threadId : "active-client-session" } ,
601+ runtimePayload : { cwd : workspaceRoot , activeTurnId : "turn-active" } ,
602+ } ) ;
603+ } ) ,
604+ ) ;
508605} ) ;
0 commit comments