@@ -2,16 +2,23 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22import * as ModelManifest from "./ModelManifest.ts" ;
33import { expect , it } from "@effect/vitest" ;
44import * as HostProcess from "@t3tools/shared/HostProcess" ;
5+ import * as Clock from "effect/Clock" ;
56import * as Crypto from "effect/Crypto" ;
67import * as Deferred from "effect/Deferred" ;
78import * as Effect from "effect/Effect" ;
89import * as Hex from "effect/encoding/Hex" ;
910import * as FileSystem from "effect/FileSystem" ;
1011import * as Option from "effect/Option" ;
12+ import * as Path from "effect/Path" ;
13+ import * as PlatformError from "effect/PlatformError" ;
1114import * as Exit from "effect/Exit" ;
15+ import * as Queue from "effect/Queue" ;
1216import * as Scope from "effect/Scope" ;
17+ import * as Sink from "effect/Sink" ;
1318import * as Stream from "effect/Stream" ;
1419import { HttpClient , HttpClientResponse } from "effect/http" ;
20+ import * as TestClock from "effect/testing/TestClock" ;
21+ import { ChildProcessSpawner } from "effect/process" ;
1522import * as CodexInstallation from "./CodexInstallation.ts" ;
1623
1724const archive = Buffer . from (
@@ -31,6 +38,10 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
3138 body ?: Stream . Stream < Uint8Array > ;
3239 baseDir ?: string ;
3340 local ?: { version : string ; appServerFails ?: boolean ; versionFails ?: boolean } ;
41+ platform ?: NodeJS . Platform ;
42+ spawner ?: ChildProcessSpawner . ChildProcessSpawner [ "Service" ] ;
43+ // Fails this many renames of an extracted runtime before letting one through.
44+ runtimeRenameFailures ?: number ;
3445 } = { } ,
3546) {
3647 const fs = yield * FileSystem . FileSystem ;
@@ -50,6 +61,30 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
5061 let downloads = 0 ;
5162 const crypto = yield * Crypto . Crypto ;
5263 const sha256 = Hex . encode ( yield * crypto . digest ( "SHA-256" , archive ) . pipe ( Effect . orDie ) ) ;
64+ let runtimeRenameFailures = input . runtimeRenameFailures ?? 0 ;
65+ const failedRenames = yield * Queue . unbounded < void > ( ) ;
66+ const lockingFs : FileSystem . FileSystem = {
67+ ...fs ,
68+ // Suspended so each retry of the same effect is a fresh attempt, like the real rename.
69+ rename : ( from , to ) =>
70+ Effect . suspend ( ( ) =>
71+ / [ / \\ ] r u n t i m e $ / u. test ( from ) && runtimeRenameFailures -- > 0
72+ ? Queue . offer ( failedRenames , undefined ) . pipe (
73+ Effect . andThen (
74+ Effect . fail (
75+ PlatformError . systemError ( {
76+ _tag : "PermissionDenied" ,
77+ module : "FileSystem" ,
78+ method : "rename" ,
79+ pathOrDescriptor : from ,
80+ } ) ,
81+ ) ,
82+ ) ,
83+ )
84+ : fs . rename ( from , to ) ,
85+ ) ,
86+ } ;
87+ const spawner = input . spawner ?? ( yield * ChildProcessSpawner . ChildProcessSpawner ) ;
5388 const installation = yield * CodexInstallation . makeCodexInstallation ( {
5489 baseDir,
5590 releaseAsset : { ...asset , sha256 } ,
@@ -62,9 +97,14 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
6297 forceRefresh : Effect . succeed ( ModelManifest . BUNDLED_MODEL_MANIFEST ) ,
6398 refreshInBackground : Effect . void ,
6499 } ) ,
65- Effect . provideService ( HostProcess . Platform , "darwin" ) ,
100+ Effect . provideService ( HostProcess . Platform , input . platform ?? "darwin" ) ,
66101 Effect . provideService ( HostProcess . Architecture , "arm64" ) ,
67- Effect . provideService ( HostProcess . Environment , { PATH : input . local ? localDirectory : "" } ) ,
102+ Effect . provideService ( HostProcess . Environment , {
103+ PATH : input . local ? localDirectory : "" ,
104+ SystemRoot : "C:\\Windows" ,
105+ } ) ,
106+ Effect . provideService ( FileSystem . FileSystem , lockingFs ) ,
107+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
68108 Effect . provideService (
69109 HttpClient . HttpClient ,
70110 HttpClient . make ( ( request ) =>
@@ -81,7 +121,15 @@ const makeHarness = Effect.fn("test.makeCodexInstallation")(function* (
81121 ) ,
82122 ) ,
83123 ) ;
84- return { installation, fs, baseDir, localBinaryPath, probeLog, downloads : ( ) => downloads } ;
124+ return {
125+ installation,
126+ fs,
127+ baseDir,
128+ localBinaryPath,
129+ probeLog,
130+ failedRenames,
131+ downloads : ( ) => downloads ,
132+ } ;
85133} ) ;
86134const terminalState = ( installation : CodexInstallation . CodexInstallation [ "Service" ] ) =>
87135 installation . changes . pipe (
@@ -358,3 +406,112 @@ it("publishes complete packages for all supported platforms", () => {
358406 ) ;
359407 expect ( CodexInstallation . resolveCodexReleaseAsset ( "linux" , "riscv64" ) ) . toBeNull ( ) ;
360408} ) ;
409+ // Live clock: staleness compares real filesystem mtimes.
410+ it . live ( "startup removes only stale staging directories" , ( ) =>
411+ Effect . gen ( function * ( ) {
412+ const fs = yield * FileSystem . FileSystem ;
413+ const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-codex-install-test-" } ) ;
414+ const managed = `${ baseDir } /tools/codex` ;
415+ // utimes takes seconds when given numbers.
416+ const stale = ( ( yield * Clock . currentTimeMillis ) - 25 * 60 * 60_000 ) / 1000 ;
417+ for ( const name of [ ".install-stale" , ".active-stale" , ".install-live" ] ) {
418+ yield * fs . makeDirectory ( `${ managed } /${ name } ` , { recursive : true } ) ;
419+ }
420+ yield * fs . utimes ( `${ managed } /.install-stale` , stale , stale ) ;
421+ yield * fs . utimes ( `${ managed } /.active-stale` , stale , stale ) ;
422+ const h = yield * makeHarness ( { baseDir } ) ;
423+ expect ( yield * fs . readDirectory ( managed ) ) . toEqual ( [ ".install-live" ] ) ;
424+ expect ( ( yield * h . installation . state ) . canRemove ) . toBe ( true ) ;
425+
426+ yield * fs . remove ( `${ managed } /.install-live` , { recursive : true } ) ;
427+ yield * fs . makeDirectory ( `${ managed } /.install-stale` ) ;
428+ yield * fs . utimes ( `${ managed } /.install-stale` , stale , stale ) ;
429+ const fresh = yield * makeHarness ( { baseDir } ) ;
430+ expect ( yield * fs . readDirectory ( managed ) ) . toEqual ( [ ] ) ;
431+ expect ( ( yield * fresh . installation . state ) . canRemove ) . toBe ( false ) ;
432+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
433+ ) ;
434+
435+ const processHandle = ( stdout : string ) =>
436+ ChildProcessSpawner . makeHandle ( {
437+ pid : ChildProcessSpawner . ProcessId ( 1 ) ,
438+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
439+ isRunning : Effect . succeed ( false ) ,
440+ kill : ( ) => Effect . void ,
441+ unref : Effect . succeed ( Effect . void ) ,
442+ stdin : Sink . drain ,
443+ stdout : Stream . make ( new TextEncoder ( ) . encode ( stdout ) ) ,
444+ stderr : Stream . empty ,
445+ all : Stream . empty ,
446+ getInputFd : ( ) => Sink . drain ,
447+ getOutputFd : ( ) => Stream . empty ,
448+ } ) ;
449+ const windowsPackage : Record < string , string > = {
450+ "bin/codex.exe" : "codex" ,
451+ "bin/codex-code-mode-host.exe" : "host" ,
452+ "codex-path/rg.exe" : "rg" ,
453+ "codex-package.json" : JSON . stringify ( {
454+ layoutVersion : 1 ,
455+ version : asset . version ,
456+ target : asset . target ,
457+ entrypoint : "bin/codex.exe" ,
458+ } ) ,
459+ } ;
460+ // Stands in for Windows' bsdtar: CRLF listings, and extraction writes the Windows layout.
461+ const makeWindowsTar = ( fs : FileSystem . FileSystem , commands : Array < string > ) =>
462+ ChildProcessSpawner . make ( ( command ) =>
463+ Effect . gen ( function * ( ) {
464+ const { command : executable , args } = command as unknown as {
465+ readonly command : string ;
466+ readonly args : ReadonlyArray < string > ;
467+ } ;
468+ commands . push ( executable ) ;
469+ const names = Object . keys ( windowsPackage ) ;
470+ if ( args [ 0 ] === "-tzf" ) return processHandle ( `${ names . join ( "\r\n" ) } \r\n` ) ;
471+ if ( args [ 0 ] === "-tvzf" )
472+ return processHandle ( `${ names . map ( ( name ) => `-rwxr-xr-x 0 0 ${ name } ` ) . join ( "\r\n" ) } \r\n` ) ;
473+ const runtime = args [ 3 ] ?? "" ;
474+ for ( const [ name , contents ] of Object . entries ( windowsPackage ) ) {
475+ yield * fs . makeDirectory ( `${ runtime } /${ name . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) } ` , {
476+ recursive : true ,
477+ } ) ;
478+ yield * fs . writeFileString ( `${ runtime } /${ name } ` , contents ) ;
479+ }
480+ return processHandle ( "" ) ;
481+ } ) . pipe ( Effect . orDie ) ,
482+ ) ;
483+
484+ it . effect ( "installs on Windows via System32 tar with CRLF output and a locked runtime" , ( ) =>
485+ Effect . gen ( function * ( ) {
486+ const fs = yield * FileSystem . FileSystem ;
487+ const commands : Array < string > = [ ] ;
488+ const h = yield * makeHarness ( {
489+ platform : "win32" ,
490+ spawner : makeWindowsTar ( fs , commands ) ,
491+ runtimeRenameFailures : 3 ,
492+ } ) ;
493+ yield * h . installation . start ;
494+ // Each failed attempt has scheduled its retry before the test fiber resumes.
495+ for ( let attempt = 0 ; attempt < 3 ; attempt ++ ) {
496+ yield * Queue . take ( h . failedRenames ) ;
497+ yield * TestClock . adjust ( "100 millis" ) ;
498+ }
499+ expect ( yield * terminalState ( h . installation ) ) . toMatchObject ( { phase : "succeeded" } ) ;
500+ expect ( commands ) . toEqual ( [
501+ "C:\\Windows\\System32\\tar.exe" ,
502+ "C:\\Windows\\System32\\tar.exe" ,
503+ "C:\\Windows\\System32\\tar.exe" ,
504+ ] ) ;
505+ const path = yield * Path . Path ;
506+ expect ( ( yield * h . installation . resolve ( ) ) . executablePath ) . toBe (
507+ path . join ( h . baseDir , "tools" , "codex" , "0.156.1" , "bin" , "codex.exe" ) ,
508+ ) ;
509+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
510+ ) ;
511+ it . effect ( "does not retry a failed runtime rename outside Windows" , ( ) =>
512+ Effect . gen ( function * ( ) {
513+ const h = yield * makeHarness ( { runtimeRenameFailures : 1 } ) ;
514+ yield * h . installation . start ;
515+ expect ( ( yield * terminalState ( h . installation ) ) . phase ) . toBe ( "failed" ) ;
516+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
517+ ) ;
0 commit comments