@@ -19,6 +19,7 @@ import {
1919 type OrchestrationV2ProviderTurn ,
2020 type OrchestrationV2RuntimeRequest ,
2121 type OrchestrationV2TurnItem ,
22+ type PromptSuggestion ,
2223 ProjectId ,
2324 type ProviderApprovalDecision ,
2425 ProviderInstanceId ,
@@ -41,6 +42,7 @@ import * as DateTime from "effect/DateTime";
4142import * as Deferred from "effect/Deferred" ;
4243import * as Crypto from "effect/Crypto" ;
4344import * as Effect from "effect/Effect" ;
45+ import * as Exit from "effect/Exit" ;
4446import * as Predicate from "effect/Predicate" ;
4547import * as FileSystem from "effect/FileSystem" ;
4648import { MCP_APP_OUTPUT_KEY , readMcpAppReference } from "@t3tools/shared/mcpApp" ;
@@ -52,7 +54,9 @@ import * as PlatformError from "effect/PlatformError";
5254import * as Queue from "effect/Queue" ;
5355import * as Ref from "effect/Ref" ;
5456import * as Schema from "effect/Schema" ;
57+ import * as Scope from "effect/Scope" ;
5558import * as Stream from "effect/Stream" ;
59+ import { FetchHttpClient } from "effect/http" ;
5660import { TestClock } from "effect/testing" ;
5761import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
5862
@@ -2005,6 +2009,12 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20052009 Effect . void ,
20062010 onRequest : ( method : string , params : unknown ) => Effect . Effect < void > = ( ) => Effect . void ,
20072011 readChildMetadata ?: Parameters < typeof withCodexReplayChildMetadata > [ 2 ] ,
2012+ adapterOptions : Partial <
2013+ Pick < CodexAdapterV2 . CodexAdapterV2Options , "settings" | "promptSuggestions" >
2014+ > = { } ,
2015+ decorateClient : (
2016+ client : CodexClient . CodexAppServerClient [ "Service" ] ,
2017+ ) => CodexClient . CodexAppServerClient [ "Service" ] = ( client ) => client ,
20082018 ) =>
20092019 Effect . gen ( function * ( ) {
20102020 const fileSystem = yield * FileSystem . FileSystem ;
@@ -2039,6 +2049,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20392049 } ) satisfies CodexClient . CodexAppServerClient [ "Service" ] ,
20402050 ) ,
20412051 Effect . provide ( context ) ,
2052+ Effect . map ( decorateClient ) ,
20422053 ) ,
20432054 ) ,
20442055 ) ,
@@ -2058,6 +2069,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
20582069 continuationRequests . push ( request ) ;
20592070 } ) ,
20602071 } ,
2072+ ...adapterOptions ,
20612073 } ) ;
20622074 const threadId = ThreadId . make ( `thread-${ transcript . scenario } ` ) ;
20632075 const runtime = yield * adapter . openSession ( {
@@ -2122,6 +2134,188 @@ describe("CodexAdapterV2 post-settle continuation", () => {
21222134 } ;
21232135 } ) ;
21242136
2137+ it . effect . each ( [
2138+ { action : "next turn" , late : false } ,
2139+ { action : "rollback" , late : false } ,
2140+ { action : "close" , late : false } ,
2141+ { action : "next turn" , late : true } ,
2142+ { action : "rollback" , late : true } ,
2143+ { action : "close" , late : true } ,
2144+ ] as const ) ( "revokes composer predictions on $action (late=$late)" , ( { action, late } ) =>
2145+ Effect . gen ( function * ( ) {
2146+ const nativeThreadId = "prediction-thread" ;
2147+ const nativeTurnId = "prediction-native-turn" ;
2148+ const prompt = "Implement" ;
2149+ const nextTurn : Array < CodexReplay . CodexAppServerReplayEntry > = [
2150+ {
2151+ type : "expect_outbound" ,
2152+ frame : {
2153+ id : 4 ,
2154+ method : "turn/start" ,
2155+ params : {
2156+ threadId : nativeThreadId ,
2157+ input : [ { type : "text" , text : "Next" } ] ,
2158+ cwd : "/workspace" ,
2159+ model : "gpt-5.4" ,
2160+ approvalPolicy : "never" ,
2161+ approvalsReviewer : "user" ,
2162+ sandboxPolicy : { type : "dangerFullAccess" } ,
2163+ summary : "detailed" ,
2164+ } ,
2165+ } ,
2166+ } ,
2167+ {
2168+ type : "emit_inbound" ,
2169+ frame : {
2170+ id : 4 ,
2171+ result : { turn : makeCodexReplayTurn ( { id : "next-turn" , status : "inProgress" } ) } ,
2172+ } ,
2173+ } ,
2174+ ] ;
2175+ const transcript = makeCodexReplayTranscript ( {
2176+ scenario : `prediction-${ action } -${ late } ` ,
2177+ entries : [
2178+ ...codexReplayPreamble ( { nativeThreadId, nativeTurnId, prompt } ) ,
2179+ {
2180+ type : "emit_inbound" ,
2181+ label : "completed" ,
2182+ frame : {
2183+ method : "turn/completed" ,
2184+ params : {
2185+ threadId : nativeThreadId ,
2186+ turn : makeCodexReplayTurn ( { id : nativeTurnId , status : "completed" } ) ,
2187+ } ,
2188+ } ,
2189+ } ,
2190+ ...( action === "next turn" ? nextTurn : [ ] ) ,
2191+ ] ,
2192+ } ) ;
2193+ const published : Array < PromptSuggestion | null > = [ ] ;
2194+ const suggested = yield * Deferred . make < void > ( ) ;
2195+ const requested = yield * Deferred . make < void > ( ) ;
2196+ const responseReturned = yield * Deferred . make < void > ( ) ;
2197+ let resolveResponse ! : ( response : Response ) => void ;
2198+ let signal : AbortSignal | null | undefined ;
2199+ const fetch : typeof globalThis . fetch = async ( _url , init ) => {
2200+ if ( init ?. method === "GET" ) return Response . json ( { is_enabled : true , prompt : "Predict" } ) ;
2201+ signal = init ?. signal ;
2202+ Deferred . doneUnsafe ( requested , Effect . void ) ;
2203+ if ( ! late )
2204+ return new Response (
2205+ 'data: {"type":"response.output_text.delta","delta":"{\\"suggestion\\":\\"Add tests\\"}"}\n\ndata: {"type":"response.completed"}\n\n' ,
2206+ ) ;
2207+ const response = await new Promise < Response > ( ( resolve ) => {
2208+ resolveResponse = resolve ;
2209+ } ) ;
2210+ Deferred . doneUnsafe ( responseReturned , Effect . void ) ;
2211+ return response ;
2212+ } ;
2213+ const sessionScope = yield * Scope . make ( ) ;
2214+ yield * Effect . addFinalizer ( ( ) => Scope . close ( sessionScope , Exit . void ) ) ;
2215+ const harness = yield * makeCodexReplayHarness (
2216+ transcript ,
2217+ undefined ,
2218+ undefined ,
2219+ undefined ,
2220+ {
2221+ settings : { ...DEFAULT_CODEX_SETTINGS , promptSuggestions : true } ,
2222+ promptSuggestions : {
2223+ publish : ( threadId , suggestion ) =>
2224+ Effect . sync ( ( ) => {
2225+ assert . equal ( threadId , ThreadId . make ( `thread-${ transcript . scenario } ` ) ) ;
2226+ published . push ( suggestion ) ;
2227+ } ) . pipe (
2228+ Effect . andThen ( suggestion ? Deferred . succeed ( suggested , undefined ) : Effect . void ) ,
2229+ ) ,
2230+ } ,
2231+ } ,
2232+ ( client ) => ( {
2233+ ...client ,
2234+ request : ( ( method : string , params : unknown ) => {
2235+ if ( method === "getAuthStatus" )
2236+ return Effect . succeed ( { authMethod : "chatgpt" , authToken : "token" } ) ;
2237+ if ( method === "config/read" ) return Effect . succeed ( { config : { } } ) ;
2238+ if ( method === "thread/read" ) return Effect . succeed ( { thread : { turns : [ ] } } ) ;
2239+ return client . raw . request ( method , params ) ;
2240+ } ) as CodexClient . CodexAppServerClient [ "Service" ] [ "request" ] ,
2241+ raw : {
2242+ ...client . raw ,
2243+ request : ( method , params ) =>
2244+ method === "account/read"
2245+ ? Effect . succeed ( {
2246+ workspaceRouting : {
2247+ backendOrigin : "https://chatgpt.example" ,
2248+ chatgptAccountId : "account" ,
2249+ } ,
2250+ } )
2251+ : client . raw . request ( method , params ) ,
2252+ } ,
2253+ } ) ,
2254+ ) . pipe (
2255+ Effect . provideService ( Scope . Scope , sessionScope ) ,
2256+ Effect . provideService ( FetchHttpClient . Fetch , fetch ) ,
2257+ ) ;
2258+ const turnInput = makeCodexTestTurnInput ( {
2259+ threadId : harness . threadId ,
2260+ providerThread : harness . providerThread ,
2261+ now : yield * DateTime . now ,
2262+ attemptId : RunAttemptId . make ( "prediction-attempt" ) ,
2263+ text : prompt ,
2264+ } ) ;
2265+ yield * harness . runtime
2266+ . startTurn ( turnInput )
2267+ . pipe ( Effect . provideService ( FetchHttpClient . Fetch , fetch ) ) ;
2268+ yield * harness . firstTerminal ;
2269+ yield * Deferred . await ( requested ) ;
2270+ if ( ! late ) {
2271+ yield * Deferred . await ( suggested ) ;
2272+ assert . equal ( published . at ( - 1 ) ?. text , "Add tests" ) ;
2273+ assert . equal ( published . at ( - 1 ) ?. runId , turnInput . runId ) ;
2274+ assert . match ( published . at ( - 1 ) ! . id , / ^ [ 0 - 9 a - f - ] { 36 } $ / ) ;
2275+ }
2276+ if ( action === "next turn" ) {
2277+ yield * harness . runtime . startTurn ( {
2278+ ...turnInput ,
2279+ message : { ...turnInput . message , text : "Next" } ,
2280+ runId : RunId . make ( "next-run" ) ,
2281+ attemptId : RunAttemptId . make ( "next-attempt" ) ,
2282+ } ) ;
2283+ } else if ( action === "rollback" ) {
2284+ yield * harness . runtime . rollbackThread ( {
2285+ providerThread : harness . providerThread ,
2286+ target : {
2287+ type : "thread_start" ,
2288+ checkpointId : CheckpointId . make ( "prediction-checkpoint" ) ,
2289+ appRunOrdinal : 0 ,
2290+ } ,
2291+ providerThreadTurns : [ ] ,
2292+ } ) ;
2293+ } else {
2294+ yield * Scope . close ( sessionScope , Exit . void ) ;
2295+ }
2296+ assert . isNull ( published . at ( - 1 ) ) ;
2297+ if ( late ) {
2298+ assert . isTrue ( signal ?. aborted ) ;
2299+ resolveResponse (
2300+ new Response (
2301+ 'data: {"type":"response.output_text.delta","delta":"{\\"suggestion\\":\\"Too late\\"}"}\n\ndata: {"type":"response.completed"}\n\n' ,
2302+ ) ,
2303+ ) ;
2304+ yield * Deferred . await ( responseReturned ) ;
2305+ yield * Effect . yieldNow ;
2306+ assert . isTrue (
2307+ published . every ( ( value ) => value === null ) ,
2308+ "Late results must not publish" ,
2309+ ) ;
2310+ }
2311+ } ) . pipe (
2312+ Effect . scoped ,
2313+ Effect . provide (
2314+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
2315+ ) ,
2316+ ) ,
2317+ ) ;
2318+
21252319 it . effect . each ( [ "supported" , "unsupported" , "invalid" ] as const ) (
21262320 "delivers native history with %s app-server protocol" ,
21272321 ( response ) =>
0 commit comments