@@ -2169,4 +2169,173 @@ describe("AcpRegistryAdapterV2", () => {
21692169 assert . isFalse ( yield * queued . runtime . hasPendingBackgroundWork ! ) ;
21702170 } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
21712171 ) ;
2172+
2173+ it . effect . each ( [
2174+ {
2175+ failure : "a lost session" ,
2176+ error : {
2177+ code : - 32603 ,
2178+ message : "Internal error" ,
2179+ data : {
2180+ details :
2181+ "Request session.send failed with message: session event delivery failed: session not found: copilot-session" ,
2182+ } ,
2183+ } ,
2184+ restarts : true ,
2185+ } ,
2186+ {
2187+ failure : "a lost session reported in the message" ,
2188+ error : {
2189+ code : - 32603 ,
2190+ message :
2191+ "Request session.send failed with message: session event delivery failed: session not found: copilot-session" ,
2192+ } ,
2193+ restarts : true ,
2194+ } ,
2195+ {
2196+ failure : "a session-lock timeout" ,
2197+ error : {
2198+ code : - 32603 ,
2199+ message : "Internal error" ,
2200+ data : {
2201+ details :
2202+ "session lock unavailable during is_session_generation_current: acquisition timed out" ,
2203+ } ,
2204+ } ,
2205+ restarts : true ,
2206+ } ,
2207+ {
2208+ failure : "a session-lock timeout reported in the message" ,
2209+ error : {
2210+ code : - 32603 ,
2211+ message :
2212+ "Request session.send failed with message: session lock unavailable during is_session_generation_current: acquisition timed out" ,
2213+ } ,
2214+ restarts : true ,
2215+ } ,
2216+ {
2217+ failure : "an unrelated internal error" ,
2218+ error : {
2219+ code : - 32603 ,
2220+ message : "Request session.send failed with message: Session directory no longer exists" ,
2221+ } ,
2222+ restarts : false ,
2223+ } ,
2224+ ] as const ) ( "runs Copilot's next message after $failure" , ( { error, restarts } ) =>
2225+ Effect . gen ( function * ( ) {
2226+ const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
2227+ const fileSystem = yield * FileSystem . FileSystem ;
2228+ const path = yield * Path . Path ;
2229+ const replayDir = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-copilot-lost-" } ) ;
2230+ const scriptPath = yield * path . fromFileUrl (
2231+ new URL ( "../../../scripts/acp-replay-agent.ts" , import . meta. url ) ,
2232+ ) ;
2233+ const request = ( method : string , params : unknown = "<any>" ) => ( {
2234+ type : "expect_outbound" ,
2235+ frame : { kind : "request" , method, params } ,
2236+ } ) ;
2237+ const respond = ( method : string , response : object ) => ( {
2238+ type : "emit_inbound" ,
2239+ frame : { kind : "response" , method, ...response } ,
2240+ } ) ;
2241+ const start = ( sessionId : string ) => [
2242+ request ( "initialize" ) ,
2243+ respond ( "initialize" , {
2244+ result : { protocolVersion : 1 , agentCapabilities : { loadSession : true } } ,
2245+ } ) ,
2246+ request ( "session/new" ) ,
2247+ respond ( "session/new" , { result : { sessionId } } ) ,
2248+ ] ;
2249+ const failedPrompt = [
2250+ ...start ( "copilot-session" ) ,
2251+ request ( "session/prompt" ) ,
2252+ respond ( "session/prompt" , { error } ) ,
2253+ ] ;
2254+ const completedPrompt = [
2255+ request ( "session/prompt" ) ,
2256+ respond ( "session/prompt" , { result : { stopReason : "end_turn" } } ) ,
2257+ ] ;
2258+ const processes = restarts
2259+ ? [
2260+ failedPrompt ,
2261+ [
2262+ ...start ( "replacement-session" ) ,
2263+ request ( "session/load" , {
2264+ sessionId : "copilot-session" ,
2265+ cwd : "<workspace>" ,
2266+ mcpServers : "<any>" ,
2267+ } ) ,
2268+ respond ( "session/load" , { result : { } } ) ,
2269+ ...completedPrompt ,
2270+ ] ,
2271+ ]
2272+ : [ [ ...failedPrompt , ...completedPrompt ] ] ;
2273+ let spawned = 0 ;
2274+ const instanceId = ProviderInstanceId . make ( "copilot-lost-session" ) ;
2275+ const adapter = yield * makeAcpRegistryAdapterV2 ( {
2276+ selfInvocation : yield * resolveSelfInvocation ( ) ,
2277+ instanceId,
2278+ settings : yield * decodeAcpRegistryAdapterSettings ( { agentId : "github-copilot-cli" } ) ,
2279+ environment : { } ,
2280+ makeRuntime : ( runtimeInput ) =>
2281+ Effect . gen ( function * ( ) {
2282+ const index = spawned ++ ;
2283+ const transcript = yield * decodeAcpReplayTranscript (
2284+ {
2285+ provider : ACP_REGISTRY_PROVIDER ,
2286+ protocol : "acp.ndjson-jsonrpc" ,
2287+ version : "1" ,
2288+ scenario : `copilot-lost-session-${ index } ` ,
2289+ entries : processes [ index ] as never ,
2290+ } ,
2291+ ACP_REGISTRY_PROVIDER ,
2292+ ) . pipe ( Effect . orDie ) ;
2293+ return yield * makeAcpReplayRuntime ( {
2294+ transcript,
2295+ statusPath : path . join ( replayDir , `status-${ index } .json` ) ,
2296+ scriptPath,
2297+ childProcessSpawner,
2298+ fileSystem,
2299+ } ) ( runtimeInput ) ;
2300+ } ) ,
2301+ } ) . pipe ( Effect . provide ( layerInjectedRuntimeCatalog ) ) ;
2302+ const threadId = ThreadId . make ( "thread-copilot-lost-session" ) ;
2303+ const modelSelection = { instanceId, model : "default" } as const ;
2304+ const runtimePolicy = ProviderAdapter . ProviderAdapterV2RuntimePolicy . make ( {
2305+ runtimeMode : "approval-required" ,
2306+ interactionMode : "default" ,
2307+ cwd : replayDir ,
2308+ } ) ;
2309+ const runtime = yield * adapter . openSession ( {
2310+ threadId,
2311+ providerSessionId : ProviderSessionId . make ( "session-copilot-lost-session" ) ,
2312+ modelSelection,
2313+ runtimePolicy,
2314+ } ) ;
2315+ const providerThread = yield * runtime . ensureThread ( {
2316+ threadId,
2317+ modelSelection,
2318+ runtimePolicy,
2319+ } ) ;
2320+ const now = yield * DateTime . now ;
2321+ const runTurn = ( ordinal : number ) =>
2322+ runtime
2323+ . startTurn (
2324+ makeTurnInput ( { threadId, providerThread, instanceId, runtimePolicy, now, ordinal } ) ,
2325+ )
2326+ . pipe (
2327+ Effect . andThen (
2328+ runtime . events . pipe (
2329+ Stream . filter ( ( event ) => event . type === "turn.terminal" ) ,
2330+ Stream . take ( 1 ) ,
2331+ Stream . runCollect ,
2332+ ) ,
2333+ ) ,
2334+ Effect . map ( ( [ terminal ] ) => terminal ?. status ) ,
2335+ ) ;
2336+ assert . equal ( yield * runTurn ( 1 ) , "failed" ) ;
2337+ assert . equal ( yield * runTurn ( 2 ) , "completed" ) ;
2338+ assert . equal ( spawned , restarts ? 2 : 1 ) ;
2339+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
2340+ ) ;
21722341} ) ;
0 commit comments