@@ -17,6 +17,7 @@ import {
1717 type OrchestrationV2AppThread ,
1818 type OrchestrationV2ProviderThread ,
1919 type OrchestrationV2ProviderTurn ,
20+ type OrchestrationV2RuntimeRequest ,
2021 type OrchestrationV2TurnItem ,
2122 ProjectId ,
2223 type ProviderApprovalDecision ,
@@ -35,6 +36,7 @@ import { SpawnExecutableResolution } from "@t3tools/shared/shell";
3536import * as CodexClient from "effect-codex-app-server/client" ;
3637import * as CodexError from "effect-codex-app-server/errors" ;
3738import * as CodexReplay from "effect-codex-app-server/replay" ;
39+ import type * as CodexSchema from "effect-codex-app-server/schema" ;
3840import * as DateTime from "effect/DateTime" ;
3941import * as Deferred from "effect/Deferred" ;
4042import * as Crypto from "effect/Crypto" ;
@@ -47,6 +49,7 @@ import * as Fiber from "effect/Fiber";
4749import * as Layer from "effect/Layer" ;
4850import * as Path from "effect/Path" ;
4951import * as PlatformError from "effect/PlatformError" ;
52+ import * as Queue from "effect/Queue" ;
5053import * as Ref from "effect/Ref" ;
5154import * as Schema from "effect/Schema" ;
5255import * as Stream from "effect/Stream" ;
@@ -9533,4 +9536,214 @@ describe("CodexAdapterV2 post-settle continuation", () => {
95339536 ) ,
95349537 ) ;
95359538 } ) ;
9539+
9540+ describe ( "Codex file-change approval paths" , ( ) => {
9541+ const nativeThreadId = "file-approval-thread" ;
9542+ const nativeTurnId = "file-approval-turn" ;
9543+ const prompt = "Edit the files." ;
9544+ type FileUpdateChange = CodexSchema . V2ItemStartedNotification__FileUpdateChange ;
9545+ const addA : FileUpdateChange = { path : "/w/a.ts" , kind : { type : "add" } , diff : "export {};" } ;
9546+ const updateB : FileUpdateChange = {
9547+ path : "/w/b.ts" ,
9548+ kind : { type : "update" , move_path : null } ,
9549+ diff : "@@" ,
9550+ } ;
9551+ const renameC : FileUpdateChange = {
9552+ path : "/w/c.ts" ,
9553+ kind : { type : "update" , move_path : "/w/d.ts" } ,
9554+ diff : "@@" ,
9555+ } ;
9556+
9557+ const deleteE : FileUpdateChange = { path : "/w/e.ts" , kind : { type : "delete" } , diff : "gone" } ;
9558+
9559+ const fileChange = (
9560+ phase : "started" | "completed" ,
9561+ turnId : string ,
9562+ itemId : string ,
9563+ changes : ReadonlyArray < FileUpdateChange > ,
9564+ ) : CodexReplay . CodexAppServerReplayEntry => ( {
9565+ type : "emit_inbound" ,
9566+ label : `${ phase } ${ itemId } ` ,
9567+ frame : {
9568+ method : `item/${ phase } ` ,
9569+ params : {
9570+ threadId : nativeThreadId ,
9571+ turnId,
9572+ ...( phase === "started" ? { startedAtMs : 1 } : { completedAtMs : 3 } ) ,
9573+ item : {
9574+ type : "fileChange" ,
9575+ id : itemId ,
9576+ status : phase === "started" ? "inProgress" : "completed" ,
9577+ changes,
9578+ } ,
9579+ } ,
9580+ } ,
9581+ } ) ;
9582+
9583+ const approval = ( input : {
9584+ readonly id : number ;
9585+ readonly turnId : string ;
9586+ readonly itemId : string ;
9587+ } ) : Array < CodexReplay . CodexAppServerReplayEntry > => [
9588+ {
9589+ type : "emit_inbound" ,
9590+ label : `approval ${ input . itemId } ` ,
9591+ frame : {
9592+ id : input . id ,
9593+ method : "item/fileChange/requestApproval" ,
9594+ params : {
9595+ threadId : nativeThreadId ,
9596+ turnId : input . turnId ,
9597+ itemId : input . itemId ,
9598+ startedAtMs : 2 ,
9599+ reason : null ,
9600+ grantRoot : null ,
9601+ } ,
9602+ } ,
9603+ } ,
9604+ {
9605+ type : "expect_outbound" ,
9606+ label : `approval ${ input . itemId } ` ,
9607+ frame : { id : input . id , result : { decision : "accept" } } ,
9608+ } ,
9609+ ] ;
9610+
9611+ const turnCompleted = ( turnId : string ) : CodexReplay . CodexAppServerReplayEntry => ( {
9612+ type : "emit_inbound" ,
9613+ label : `complete ${ turnId } ` ,
9614+ frame : {
9615+ method : "turn/completed" ,
9616+ params : {
9617+ threadId : nativeThreadId ,
9618+ turn : makeCodexReplayTurn ( { id : turnId , status : "completed" } ) ,
9619+ } ,
9620+ } ,
9621+ } ) ;
9622+
9623+ const startApprovalTurn = (
9624+ scenario : string ,
9625+ entries : ReadonlyArray < CodexReplay . CodexAppServerReplayEntry > ,
9626+ ) =>
9627+ Effect . gen ( function * ( ) {
9628+ const pending = yield * Queue . unbounded < OrchestrationV2RuntimeRequest > ( ) ;
9629+ const harness = yield * makeCodexReplayHarness (
9630+ makeCodexReplayTranscript ( {
9631+ scenario,
9632+ entries : [ ...codexReplayPreamble ( { nativeThreadId, nativeTurnId, prompt } ) , ...entries ] ,
9633+ } ) ,
9634+ ( event ) =>
9635+ event . type === "runtime_request.updated" && event . runtimeRequest . status === "pending"
9636+ ? Queue . offer ( pending , event . runtimeRequest )
9637+ : Effect . void ,
9638+ ) ;
9639+ yield * harness . runtime . startTurn (
9640+ makeCodexTestTurnInput ( {
9641+ threadId : harness . threadId ,
9642+ providerThread : harness . providerThread ,
9643+ now : yield * DateTime . now ,
9644+ attemptId : RunAttemptId . make ( `${ scenario } -attempt` ) ,
9645+ text : prompt ,
9646+ } ) ,
9647+ ) ;
9648+ return {
9649+ harness,
9650+ answerNext : Queue . take ( pending ) . pipe (
9651+ Effect . tap ( ( request ) =>
9652+ harness . runtime . respondToRuntimeRequest ( {
9653+ requestId : request . id ,
9654+ decision : "accept" ,
9655+ } ) ,
9656+ ) ,
9657+ ) ,
9658+ } ;
9659+ } ) ;
9660+
9661+ const approvalItem = (
9662+ events : ReadonlyArray < ProviderAdapter . ProviderAdapterV2Event > ,
9663+ request : OrchestrationV2RuntimeRequest ,
9664+ ) =>
9665+ events . flatMap ( ( event ) =>
9666+ event . type === "turn_item.updated" &&
9667+ event . turnItem . type === "approval_request" &&
9668+ event . turnItem . requestId === request . id
9669+ ? [ event . turnItem ]
9670+ : [ ] ,
9671+ ) [ 0 ] ;
9672+
9673+ it . effect ( "answers without waiting for late paths or borrowing another item's paths" , ( ) =>
9674+ Effect . gen ( function * ( ) {
9675+ const { harness, answerNext } = yield * startApprovalTurn ( "file-approval-after" , [
9676+ fileChange ( "started" , nativeTurnId , "other-patch" , [ updateB ] ) ,
9677+ // Late details cannot arrive until the approval response has been sent.
9678+ ...approval ( { id : 0 , turnId : nativeTurnId , itemId : "patch-a" } ) ,
9679+ fileChange ( "started" , nativeTurnId , "patch-a" , [ addA ] ) ,
9680+ fileChange ( "completed" , nativeTurnId , "patch-a" , [ addA ] ) ,
9681+ fileChange ( "completed" , nativeTurnId , "other-patch" , [ updateB ] ) ,
9682+ turnCompleted ( nativeTurnId ) ,
9683+ ] ) ;
9684+ const request = yield * answerNext ;
9685+ yield * harness . firstTerminal ;
9686+
9687+ const items = harness . events . flatMap ( ( event ) =>
9688+ event . type === "turn_item.updated" &&
9689+ event . turnItem . type === "approval_request" &&
9690+ event . turnItem . requestId === request . id
9691+ ? [ event . turnItem ]
9692+ : [ ] ,
9693+ ) ;
9694+ assert . equal ( request . nativeRequestRef ?. nativeId , "patch-a" ) ;
9695+ assert . isNotEmpty ( items ) ;
9696+ for ( const item of items ) {
9697+ assert . equal ( item . requestKind , "file-change" ) ;
9698+ assert . isUndefined ( item . prompt ) ;
9699+ }
9700+ assert . deepEqual (
9701+ harness . terminalEvents ( ) . map ( ( event ) => event . status ) ,
9702+ [ "completed" ] ,
9703+ ) ;
9704+ } ) . pipe (
9705+ Effect . scoped ,
9706+ Effect . provide (
9707+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
9708+ ) ,
9709+ ) ,
9710+ ) ;
9711+
9712+ it . effect ( "matches file paths to items approved out of order" , ( ) =>
9713+ Effect . gen ( function * ( ) {
9714+ const { harness, answerNext } = yield * startApprovalTurn ( "file-approval-concurrent" , [
9715+ fileChange ( "started" , nativeTurnId , "patch-a" , [ renameC , deleteE , addA ] ) ,
9716+ fileChange ( "started" , nativeTurnId , "patch-b" , [ updateB ] ) ,
9717+ ...approval ( { id : 0 , turnId : nativeTurnId , itemId : "patch-b" } ) ,
9718+ fileChange ( "completed" , nativeTurnId , "patch-b" , [ updateB ] ) ,
9719+ ...approval ( { id : 1 , turnId : nativeTurnId , itemId : "patch-a" } ) ,
9720+ fileChange ( "completed" , nativeTurnId , "patch-a" , [ renameC , deleteE , addA ] ) ,
9721+ turnCompleted ( nativeTurnId ) ,
9722+ ] ) ;
9723+ const first = yield * answerNext ;
9724+ const second = yield * answerNext ;
9725+ yield * harness . firstTerminal ;
9726+
9727+ assert . deepEqual (
9728+ [ first , second ] . map ( ( request ) => [
9729+ request . nativeRequestRef ?. nativeId ,
9730+ approvalItem ( harness . events , request ) ?. prompt ,
9731+ ] ) ,
9732+ [
9733+ [ "patch-b" , "update /w/b.ts" ] ,
9734+ [ "patch-a" , "add /w/a.ts\nupdate /w/c.ts -> /w/d.ts\ndelete /w/e.ts" ] ,
9735+ ] ,
9736+ ) ;
9737+ assert . deepEqual (
9738+ harness . terminalEvents ( ) . map ( ( event ) => event . status ) ,
9739+ [ "completed" ] ,
9740+ ) ;
9741+ } ) . pipe (
9742+ Effect . scoped ,
9743+ Effect . provide (
9744+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
9745+ ) ,
9746+ ) ,
9747+ ) ;
9748+ } ) ;
95369749} ) ;
0 commit comments