@@ -1110,63 +1110,61 @@ function recorderThreadConfigMetadata(scenario: ReplayScenario) {
11101110 return keys . length === 0 ? { } : { recorderThreadConfigKeys : keys } ;
11111111}
11121112
1113- function makeRecorder ( {
1113+ const makeRecorder = Effect . fn ( "record-codex-app-server-replay-fixture.makeRecorder" ) ( function * ( {
11141114 outPath,
11151115 scenario,
11161116 checkout,
11171117} : {
11181118 readonly outPath : string ;
11191119 readonly scenario : ReplayScenario ;
11201120 readonly checkout : string ;
1121- } ) : Effect . Effect < Recorder , PlatformError . PlatformError , FileSystem . FileSystem > {
1122- return Effect . gen ( function * ( ) {
1123- const fs = yield * FileSystem . FileSystem ;
1124- let version = "unknown" ;
1125- const records : Array < Record < string , unknown > > = [ ] ;
1126- const setVersion = ( nextVersion : string ) =>
1127- Effect . sync ( ( ) => {
1128- version = nextVersion ;
1129- } ) ;
1130- const writeRecord = ( record : Record < string , unknown > ) =>
1131- Effect . sync ( ( ) => {
1132- records . push ( record ) ;
1133- } ) ;
1134- const flush = ( ) => {
1135- const outputRecords = codexReplayRecordingOutputRecords ( records , {
1136- workspace : process . cwd ( ) ,
1137- machine : {
1138- home : NodeOS . homedir ( ) ,
1139- hostname : NodeOS . hostname ( ) ,
1140- checkout,
1141- } ,
1142- } ) ;
1143- return fs . writeFileString (
1144- outPath ,
1145- `${ [
1146- {
1147- type : "transcript_start" ,
1148- provider : "codex" ,
1149- protocol : "codex.app-server" ,
1150- version,
1151- scenario : scenario . name ,
1152- metadata : {
1153- source : "record-codex-app-server-replay-fixture" ,
1154- fileName : scenario . fileName ,
1155- description : scenario . description ,
1156- model : scenarioModel ( scenario ) ,
1157- ...recorderThreadConfigMetadata ( scenario ) ,
1158- } ,
1121+ } ) : Effect . fn . Return < Recorder , PlatformError . PlatformError , FileSystem . FileSystem > {
1122+ const fs = yield * FileSystem . FileSystem ;
1123+ let version = "unknown" ;
1124+ const records : Array < Record < string , unknown > > = [ ] ;
1125+ const setVersion = ( nextVersion : string ) =>
1126+ Effect . sync ( ( ) => {
1127+ version = nextVersion ;
1128+ } ) ;
1129+ const writeRecord = ( record : Record < string , unknown > ) =>
1130+ Effect . sync ( ( ) => {
1131+ records . push ( record ) ;
1132+ } ) ;
1133+ const flush = ( ) => {
1134+ const outputRecords = codexReplayRecordingOutputRecords ( records , {
1135+ workspace : process . cwd ( ) ,
1136+ machine : {
1137+ home : NodeOS . homedir ( ) ,
1138+ hostname : NodeOS . hostname ( ) ,
1139+ checkout,
1140+ } ,
1141+ } ) ;
1142+ return fs . writeFileString (
1143+ outPath ,
1144+ `${ [
1145+ {
1146+ type : "transcript_start" ,
1147+ provider : "codex" ,
1148+ protocol : "codex.app-server" ,
1149+ version,
1150+ scenario : scenario . name ,
1151+ metadata : {
1152+ source : "record-codex-app-server-replay-fixture" ,
1153+ fileName : scenario . fileName ,
1154+ description : scenario . description ,
1155+ model : scenarioModel ( scenario ) ,
1156+ ...recorderThreadConfigMetadata ( scenario ) ,
11591157 } ,
1160- ...outputRecords ,
1161- ]
1162- . map ( ( record ) => JSON . stringify ( record ) )
1163- . join ( "\n" ) } \n`,
1164- ) ;
1165- } ;
1158+ } ,
1159+ ...outputRecords ,
1160+ ]
1161+ . map ( ( record ) => JSON . stringify ( record ) )
1162+ . join ( "\n" ) } \n`,
1163+ ) ;
1164+ } ;
11661165
1167- return { path : outPath , setVersion, writeRecord, flush } ;
1168- } ) ;
1169- }
1166+ return { path : outPath , setVersion, writeRecord, flush } ;
1167+ } ) ;
11701168
11711169function layerCodex ( { recorder } : { readonly recorder : Recorder } ) {
11721170 const clientRequestMethodById = new Map < string , string > ( ) ;
@@ -1348,16 +1346,16 @@ function installReplayHandlers({
13481346 ) ;
13491347}
13501348
1351- function runReplaySession ( {
1352- scenario ,
1353- run ,
1354- recorder ,
1355- } : {
1356- readonly scenario : ReplayScenario ;
1357- readonly run : ReplayRun ;
1358- readonly recorder : Recorder ;
1359- } ) {
1360- return Effect . gen ( function * ( ) {
1349+ const runReplaySession = Effect . fn ( "record-codex-app-server-replay-fixture.runReplaySession" ) (
1350+ function * ( {
1351+ scenario ,
1352+ run ,
1353+ recorder ,
1354+ } : {
1355+ readonly scenario : ReplayScenario ;
1356+ readonly run : ReplayRun ;
1357+ readonly recorder : Recorder ;
1358+ } ) {
13611359 const startedTurns = yield * makeReplayRecorderDeferredRegistry ( ) ;
13621360 const completedTurns = yield * makeReplayRecorderDeferredRegistry ( ) ;
13631361 const startedCommandExecutions = yield * makeReplayRecorderDeferredRegistry ( ) ;
@@ -1397,73 +1395,72 @@ function runReplaySession({
13971395 return client ;
13981396 } ) ;
13991397
1400- const runTurnStep = (
1398+ const runTurnStep = Effect . fn ( "record-codex-app-server-replay-fixture.runTurnStep" ) ( function * (
14011399 client : CodexClient . CodexAppServerClient [ "Service" ] ,
14021400 threadId : string ,
14031401 step : TurnReplayStep ,
1404- ) =>
1405- Effect . gen ( function * ( ) {
1406- const turnParams : TurnStartParams = {
1407- approvalPolicy : "never" ,
1408- sandboxPolicy : { type : "dangerFullAccess" } ,
1409- cwd : process . cwd ( ) ,
1410- model,
1411- summary : "detailed" ,
1412- approvalsReviewer : "user" ,
1413- ...( run . interactionMode === "plan"
1414- ? { collaborationMode : planCollaborationMode ( model ) }
1415- : { } ) ,
1416- ...run . turnDefaults ,
1417- ...step . turnOverrides ,
1418- input : turnInput ( step . prompt ) ,
1419- threadId,
1420- } ;
1421-
1422- if ( step . type === "steeredTurn" ) {
1423- approvalGate = yield * Deferred . make < void > ( ) ;
1424- }
1402+ ) {
1403+ const turnParams : TurnStartParams = {
1404+ approvalPolicy : "never" ,
1405+ sandboxPolicy : { type : "dangerFullAccess" } ,
1406+ cwd : process . cwd ( ) ,
1407+ model,
1408+ summary : "detailed" ,
1409+ approvalsReviewer : "user" ,
1410+ ...( run . interactionMode === "plan"
1411+ ? { collaborationMode : planCollaborationMode ( model ) }
1412+ : { } ) ,
1413+ ...run . turnDefaults ,
1414+ ...step . turnOverrides ,
1415+ input : turnInput ( step . prompt ) ,
1416+ threadId,
1417+ } ;
1418+
1419+ if ( step . type === "steeredTurn" ) {
1420+ approvalGate = yield * Deferred . make < void > ( ) ;
1421+ }
14251422
1426- const turn = yield * client . request ( "turn/start" , turnParams ) ;
1427- const turnId = getTurnId ( turn ) ;
1428- const started = yield * getStarted ( turnId ) ;
1429- yield * getCompletion ( turnId ) ;
1423+ const turn = yield * client . request ( "turn/start" , turnParams ) ;
1424+ const turnId = getTurnId ( turn ) ;
1425+ const started = yield * getStarted ( turnId ) ;
1426+ yield * getCompletion ( turnId ) ;
14301427
1431- if ( step . type === "steeredTurn" ) {
1432- yield * Deferred . await ( started ) ;
1433- yield * client . request ( "turn/steer" , {
1434- expectedTurnId : turnId ,
1435- input : turnInput ( step . steer ) ,
1436- threadId,
1437- } ) ;
1438- if ( approvalGate ) {
1439- yield * Deferred . succeed ( approvalGate , void 0 ) ;
1440- approvalGate = undefined ;
1441- }
1428+ if ( step . type === "steeredTurn" ) {
1429+ yield * Deferred . await ( started ) ;
1430+ yield * client . request ( "turn/steer" , {
1431+ expectedTurnId : turnId ,
1432+ input : turnInput ( step . steer ) ,
1433+ threadId,
1434+ } ) ;
1435+ if ( approvalGate ) {
1436+ yield * Deferred . succeed ( approvalGate , void 0 ) ;
1437+ approvalGate = undefined ;
14421438 }
1439+ }
14431440
1444- if ( step . type === "interruptedTurn" ) {
1445- if ( step . interruptAfterCommandExecutionStarted === true ) {
1446- const commandExecutionStarted = yield * getCommandExecutionStarted ( turnId ) ;
1447- yield * Deferred . await ( commandExecutionStarted ) ;
1448- } else {
1449- yield * Effect . sleep ( `${ step . interruptAfterMs ?? 1_500 } millis` ) ;
1450- }
1451- yield * client . request ( "turn/interrupt" , {
1441+ if ( step . type === "interruptedTurn" ) {
1442+ if ( step . interruptAfterCommandExecutionStarted === true ) {
1443+ const commandExecutionStarted = yield * getCommandExecutionStarted ( turnId ) ;
1444+ yield * Deferred . await ( commandExecutionStarted ) ;
1445+ } else {
1446+ yield * Effect . sleep ( `${ step . interruptAfterMs ?? 1_500 } millis` ) ;
1447+ }
1448+ yield * client . request ( "turn/interrupt" , {
1449+ threadId,
1450+ turnId,
1451+ } ) ;
1452+ for ( const processId of runningCommandProcessIds . get ( turnId ) ?. values ( ) ?? [ ] ) {
1453+ yield * client . raw . request ( "thread/backgroundTerminals/terminate" , {
14521454 threadId,
1453- turnId ,
1455+ processId ,
14541456 } ) ;
1455- for ( const processId of runningCommandProcessIds . get ( turnId ) ?. values ( ) ?? [ ] ) {
1456- yield * client . raw . request ( "thread/backgroundTerminals/terminate" , {
1457- threadId,
1458- processId,
1459- } ) ;
1460- }
14611457 }
1458+ }
14621459
1463- const completed = yield * getCompletion ( turnId ) ;
1464- yield * Deferred . await ( completed ) ;
1465- lastTurnIdByThread . set ( threadId , turnId ) ;
1466- } ) ;
1460+ const completed = yield * getCompletion ( turnId ) ;
1461+ yield * Deferred . await ( completed ) ;
1462+ lastTurnIdByThread . set ( threadId , turnId ) ;
1463+ } ) ;
14671464
14681465 if ( scenario . name === "provider_thread_resume" ) {
14691466 const firstStep = run . steps [ 0 ] ;
@@ -1611,36 +1608,34 @@ function runReplaySession({
16111608 } ) ,
16121609 ) ,
16131610 ) ;
1614- } ) ;
1615- }
1611+ } ,
1612+ ) ;
16161613
1617- function runScenario ( {
1614+ const runScenario = Effect . fn ( "record-codex-app-server-replay-fixture.runScenario" ) ( function * ( {
16181615 scenario,
16191616 outPath,
16201617} : {
16211618 readonly scenario : ReplayScenario ;
16221619 readonly outPath : string ;
16231620} ) {
1624- return Effect . gen ( function * ( ) {
1625- const fs = yield * FileSystem . FileSystem ;
1626- const path = yield * Path . Path ;
1627- const checkout = path . resolve ( yield * path . fromFileUrl ( new URL ( "../../.." , import . meta. url ) ) ) ;
1628- const recorder = yield * makeRecorder ( { outPath, scenario, checkout } ) ;
1621+ const fs = yield * FileSystem . FileSystem ;
1622+ const path = yield * Path . Path ;
1623+ const checkout = path . resolve ( yield * path . fromFileUrl ( new URL ( "../../.." , import . meta. url ) ) ) ;
1624+ const recorder = yield * makeRecorder ( { outPath, scenario, checkout } ) ;
16291625
1630- yield * fs . makeDirectory ( path . dirname ( outPath ) , { recursive : true } ) ;
1631- yield * Console . log ( `Writing ${ scenario . name } Codex replay events to ${ recorder . path } ` ) ;
1626+ yield * fs . makeDirectory ( path . dirname ( outPath ) , { recursive : true } ) ;
1627+ yield * Console . log ( `Writing ${ scenario . name } Codex replay events to ${ recorder . path } ` ) ;
16321628
1633- yield * Effect . forEach ( scenario . runs , ( run ) => runReplaySession ( { scenario, run, recorder } ) , {
1634- concurrency : 1 ,
1635- } ) ;
1629+ yield * Effect . forEach ( scenario . runs , ( run ) => runReplaySession ( { scenario, run, recorder } ) , {
1630+ concurrency : 1 ,
1631+ } ) ;
16361632
1637- yield * recorder . writeRecord ( {
1638- type : "runtime_exit" ,
1639- status : "success" ,
1640- } ) ;
1641- yield * recorder . flush ( ) ;
1633+ yield * recorder . writeRecord ( {
1634+ type : "runtime_exit" ,
1635+ status : "success" ,
16421636 } ) ;
1643- }
1637+ yield * recorder . flush ( ) ;
1638+ } ) ;
16441639
16451640const program = Effect . gen ( function * ( ) {
16461641 const path = yield * Path . Path ;
0 commit comments