@@ -15,6 +15,7 @@ import {
1515} from "@t3tools/contracts" ;
1616import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
1717import * as KeyedLock from "@t3tools/shared/KeyedLock" ;
18+ import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator" ;
1819import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
1920import * as Context from "effect/Context" ;
2021import * as DateTime from "effect/DateTime" ;
@@ -530,33 +531,80 @@ it.effect(
530531 } ) . pipe ( Effect . scoped , Effect . provide ( persistence ) ) ,
531532) ;
532533
533- it . effect ( "gives up on a native session that never answers and closes it " , ( ) =>
534+ it . effect ( "releases the ACP import lock when a native session never answers" , ( ) =>
534535 Effect . gen ( function * ( ) {
535536 const projections = yield * ProjectionStore . ProjectionStoreV2 ;
537+ const coordinator = yield * AcpRegistryRuntimeCoordinator . AcpRegistryRuntimeCoordinator ;
536538 const threadId = yield * seedOwner ( candidate ( CODEX_MODEL_SELECTION , "native-history-hang" ) ) ;
539+ const opened = yield * Deferred . make < void > ( ) ;
537540 let closed = false ;
538- // An agent that only resumes sessions and never answers session/resume.
541+ // An agent that only resumes sessions and never answers its first session/resume.
539542 const adapter = {
540543 openSession : ( ) =>
541- Effect . addFinalizer ( ( ) =>
542- Effect . sync ( ( ) => {
543- closed = true ;
544- } ) ,
545- ) . pipe ( Effect . andThen ( Effect . never ) ) ,
544+ Effect . suspend ( ( ) => {
545+ if ( ! closed ) {
546+ return Effect . addFinalizer ( ( ) =>
547+ Effect . sync ( ( ) => {
548+ closed = true ;
549+ } ) ,
550+ ) . pipe (
551+ Effect . andThen ( Deferred . succeed ( opened , undefined ) ) ,
552+ Effect . andThen ( Effect . never ) ,
553+ ) ;
554+ }
555+ return Effect . succeed ( {
556+ readThreadSnapshot : ( { providerThread } ) =>
557+ Effect . succeed ( {
558+ providerThread,
559+ providerTurns : [ ] ,
560+ runtimeRequests : [ ] ,
561+ messages : [
562+ {
563+ createdBy : "user" ,
564+ creationSource : "provider" ,
565+ id : MessageId . make ( "replayed:hang" ) ,
566+ threadId,
567+ runId : null ,
568+ nodeId : null ,
569+ role : "user" ,
570+ text : "Fix the build" ,
571+ attachments : [ ] ,
572+ streaming : false ,
573+ createdAt : now ,
574+ updatedAt : now ,
575+ } ,
576+ ] ,
577+ } ) ,
578+ } as Pick < ProviderAdapter . ProviderAdapterV2SessionRuntime , "readThreadSnapshot" > ) ;
579+ } ) ,
546580 } as unknown as ProviderAdapter . ProviderAdapterV2 [ "Service" ] ;
547581 const importer = yield * makeImporter ( [ ] , Effect . void , ( ) => Effect . succeed ( adapter ) ) ;
582+ const importHistory = coordinator . withSessionMutation (
583+ importer . importNativeHistory ( { threadId, workspaceRoot : "/workspace/project" } ) ,
584+ ) ;
548585
549- const importing = yield * importer
550- . importNativeHistory ( { threadId, workspaceRoot : "/workspace/project" } )
551- . pipe ( Effect . flip , Effect . forkChild ) ;
586+ const importing = yield * importHistory . pipe ( Effect . flip , Effect . forkChild ) ;
587+ yield * Deferred . await ( opened ) ;
588+ // Any other ACP import or delete waits for the same lock.
589+ const next = yield * coordinator
590+ . withSessionMutation ( Effect . succeed ( "next" ) )
591+ . pipe ( Effect . forkChild ) ;
552592 yield * TestClock . adjust ( "2 minutes" ) ;
553593
554594 expect ( ( yield * Fiber . join ( importing ) ) . _tag ) . toBe ( "TimeoutError" ) ;
555595 expect ( closed ) . toBe ( true ) ;
596+ expect ( yield * Fiber . join ( next ) ) . toBe ( "next" ) ;
556597 expect (
557598 ( yield * projections . getThreadRecords ( threadId , [ ] ) ) . thread . historyOrigin ,
558599 ) . toBeUndefined ( ) ;
559- } ) . pipe ( Effect . scoped , Effect . provide ( persistence ) ) ,
600+ expect ( yield * importHistory ) . toBe ( 1 ) ;
601+ expect ( ( yield * projections . getThreadRecords ( threadId , [ ] ) ) . thread . historyOrigin ) . toBe (
602+ "v1_import" ,
603+ ) ;
604+ } ) . pipe (
605+ Effect . scoped ,
606+ Effect . provide ( Layer . merge ( persistence , AcpRegistryRuntimeCoordinator . layer ) ) ,
607+ ) ,
560608) ;
561609
562610/**
0 commit comments