11import { createHash , randomUUID } from 'node:crypto' ;
2- import { chmodSync , closeSync , constants , fstatSync , lstatSync , mkdtempSync , openSync , readFileSync ,
2+ import { chmodSync , closeSync , constants , fstatSync , lstatSync , mkdtempSync , openSync , readSync ,
33 readdirSync , realpathSync , rmSync , writeFileSync } from 'node:fs' ;
44import { tmpdir } from 'node:os' ;
55import { join } from 'node:path' ;
@@ -39,18 +39,40 @@ interface FileIdentity {
3939 readonly digest : string ;
4040}
4141interface ProfileIdentity { readonly inputDirectory : string ; readonly schema : FileIdentity ; readonly auth ?: FileIdentity ;
42- readonly cleanupDirectory ?: string ; readonly filesystems : TaskFilesystems ; readonly clone : InvocationInput [ 'clone' ] }
42+ readonly cleanupDirectories : readonly string [ ] ; readonly filesystems : TaskFilesystems ;
43+ readonly clone : InvocationInput [ 'clone' ] }
4344type InputIdentity = Pick < ProfileIdentity , 'inputDirectory' | 'schema' > ;
45+ interface InputCapture extends InputIdentity { readonly content : Buffer }
4446const identities = new WeakMap < ContainerProfile , ProfileIdentity > ( ) ;
47+ const removeOwnedDirectory = ( directory : string ) => {
48+ if ( ! lstatSync ( directory , { throwIfNoEntry : false } ) ) return ;
49+ chmodSync ( directory , 0o700 ) ;
50+ rmSync ( directory , { recursive : true , force : true } ) ;
51+ } ;
52+ const removeOwnedDirectories = ( directories : readonly string [ ] ) => {
53+ const failures : unknown [ ] = [ ] ;
54+ for ( const directory of directories ) {
55+ try { removeOwnedDirectory ( directory ) ; } catch ( error ) { failures . push ( error ) ; }
56+ }
57+ if ( failures . length ) throw new AggregateError ( failures , 'Profile snapshot cleanup did not settle.' ) ;
58+ } ;
4559
4660const readCapturedFile = ( path : string , kind : string ) : { identity : FileIdentity ; content : Buffer } => {
4761 let fd : number | undefined ;
4862 try {
4963 fd = openSync ( path , constants . O_RDONLY | constants . O_NOFOLLOW ) ;
5064 const before = fstatSync ( fd ) ;
51- if ( ! before . isFile ( ) || before . nlink !== 1 || before . size > 1024 * 1024 )
65+ const maximum = 1024 * 1024 ;
66+ if ( ! before . isFile ( ) || before . nlink !== 1 || before . size > maximum )
5267 throw new Error ( `${ kind } must be a bounded, unlinked regular file.` ) ;
53- const content = readFileSync ( fd ) ;
68+ const bounded = Buffer . allocUnsafe ( maximum + 1 ) ;
69+ let length = 0 , count = 0 ;
70+ do {
71+ count = readSync ( fd , bounded , length , bounded . length - length , null ) ;
72+ length += count ;
73+ } while ( count > 0 && length < bounded . length ) ;
74+ if ( length > maximum ) throw new Error ( `${ kind } exceeds its maximum size.` ) ;
75+ const content = bounded . subarray ( 0 , length ) ;
5476 const after = fstatSync ( fd ) ;
5577 if ( before . dev !== after . dev || before . ino !== after . ino || before . size !== after . size
5678 || before . mtimeMs !== after . mtimeMs || before . ctimeMs !== after . ctimeMs )
@@ -65,17 +87,17 @@ const sameFile = (actual: FileIdentity, expected: FileIdentity) => actual.path =
6587 && actual . dev === expected . dev && actual . ino === expected . ino && actual . mode === expected . mode
6688 && actual . nlink === expected . nlink && actual . size === expected . size && actual . mtimeMs === expected . mtimeMs
6789 && actual . digest === expected . digest ;
68- const captureInput = ( directory : string ) : InputIdentity => {
90+ const captureInput = ( directory : string ) : InputCapture => {
6991 const stat = lstatSync ( directory ) ;
7092 if ( ! stat . isDirectory ( ) || stat . isSymbolicLink ( ) || ( stat . mode & 0o005 ) !== 0o005 )
7193 throw new Error ( 'Schema input directory must be a container-readable real directory.' ) ;
7294 const canonical = mountSource ( realpathSync ( directory ) , 'Schema input' ) ;
7395 const entries = readdirSync ( canonical ) ;
7496 if ( entries . length !== 1 || entries [ 0 ] !== 'schema.json' )
7597 throw new Error ( 'Schema input must contain only one bounded, unlinked regular schema.json file.' ) ;
76- const schema = captureFile ( `${ canonical } /schema.json` , 'Schema input' ) ;
98+ const captured = readCapturedFile ( `${ canonical } /schema.json` , 'Schema input' ) , schema = captured . identity ;
7799 if ( ( schema . mode & 0o004 ) === 0 ) throw new Error ( 'Schema input must be container-readable.' ) ;
78- return Object . freeze ( { inputDirectory : canonical , schema } ) ;
100+ return Object . freeze ( { inputDirectory : canonical , schema, content : captured . content } ) ;
79101} ;
80102
81103/** Internal authenticity and host-file revalidation used at every launch boundary. */
@@ -96,7 +118,7 @@ export function assertContainerProfile(profile: ContainerProfile): void {
96118export function disposeContainerProfile ( profile : ContainerProfile ) : void {
97119 const identity = identities . get ( profile ) ;
98120 if ( ! identity ) return ;
99- if ( identity . cleanupDirectory ) rmSync ( identity . cleanupDirectory , { recursive : true , force : true } ) ;
121+ removeOwnedDirectories ( identity . cleanupDirectories ) ;
100122 identities . delete ( profile ) ;
101123}
102124
@@ -119,8 +141,7 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
119141 throw new Error ( 'Container profile requires the immutable built image ID.' ) ;
120142 assertBuiltAgentImage ( options . imageId ) ;
121143 assertTaskFilesystems ( filesystems , invocation . clone ) ;
122- const inputIdentity = captureInput ( options . inputDirectory ) ;
123- const inputDirectory = inputIdentity . inputDirectory ;
144+ const sourceInput = captureInput ( options . inputDirectory ) ;
124145 if ( invocation . vendor === 'codex' && ( ! options . codexAuthFile || options . claudeToken ) )
125146 throw new Error ( 'Codex requires only its auth file.' ) ;
126147 if ( invocation . vendor === 'claude' && ( ! options . claudeToken || options . codexAuthFile ) )
@@ -132,47 +153,59 @@ export function createContainerProfile(options: ProfileOptions): ContainerProfil
132153 if ( options . codexAuthFile && ! lstatSync ( options . codexAuthFile ) . isFile ( ) )
133154 throw new Error ( 'Codex auth must be a direct regular file, not a link.' ) ;
134155 const sourceAuth = options . codexAuthFile ? readCapturedFile ( realpathSync ( options . codexAuthFile ) , 'Codex auth' ) : undefined ;
135- let cleanupDirectory : string | undefined , codexAuthFile : string | undefined , authIdentity : FileIdentity | undefined ;
136- if ( sourceAuth ) {
137- cleanupDirectory = mkdtempSync ( join ( tmpdir ( ) , 'codeboost-auth-' ) ) ;
138- try {
156+ const cleanupDirectories : string [ ] = [ ] ;
157+ let codexAuthFile : string | undefined , authIdentity : FileIdentity | undefined ;
158+ try {
159+ const inputDirectory = mkdtempSync ( join ( tmpdir ( ) , 'codeboost-input-' ) ) ;
160+ cleanupDirectories . push ( inputDirectory ) ;
161+ writeFileSync ( join ( inputDirectory , 'schema.json' ) , sourceInput . content ,
162+ { mode : 0o400 , flag : 'wx' } ) ;
163+ chmodSync ( join ( inputDirectory , 'schema.json' ) , 0o444 ) ;
164+ chmodSync ( inputDirectory , 0o555 ) ;
165+ const inputIdentity = captureInput ( inputDirectory ) ;
166+ if ( sourceAuth ) {
167+ const cleanupDirectory = mkdtempSync ( join ( tmpdir ( ) , 'codeboost-auth-' ) ) ;
168+ cleanupDirectories . push ( cleanupDirectory ) ;
139169 const stagedAuth = join ( cleanupDirectory , 'auth.json' ) ;
140170 writeFileSync ( stagedAuth , sourceAuth . content , { mode : 0o400 , flag : 'wx' } ) ;
141171 chmodSync ( stagedAuth , 0o444 ) ;
142172 codexAuthFile = mountSource ( realpathSync ( stagedAuth ) , 'Codex auth' ) ;
143173 authIdentity = captureFile ( codexAuthFile , 'Staged Codex auth' ) ;
144- } catch ( error ) {
145- rmSync ( cleanupDirectory , { recursive : true , force : true } ) ;
146- throw error ;
147174 }
175+ const name = `codeboost-agent-${ safeName ( invocation . attemptId ) } ` , ownershipId = randomUUID ( ) ;
176+ const readOnlyWork = [ 'planning' , 'questions' , 'review' ] . includes ( invocation . phase ) ;
177+ const args = [ 'create' , '--name' , name , '--read-only' , '--user' , '10001:10001' , '--cap-drop=ALL' ,
178+ '--security-opt=no-new-privileges' , '--pids-limit=128' , '--memory=512m' , '--memory-swap=512m' ,
179+ '--cpus=1' , '--shm-size=16m' ,
180+ '--network=none' , '--env' , 'HOME=/home/codeboost' , '--env' , `CODEBOOST_PHASE=${ invocation . phase } ` ,
181+ '--label' , `io.codeboost.invocation=${ ownershipId } ` ,
182+ '--env' , `CODEBOOST_VENDOR=${ invocation . vendor } ` , '--env' , 'npm_config_cache=/tmp/npm-cache' ,
183+ '--env' , `CODEBOOST_WORK_BYTES=${ filesystems . workBytes } ` , '--env' , `CODEBOOST_WORK_INODES=${ filesystems . workInodes } ` ,
184+ '--env' , `CODEBOOST_METADATA_BYTES=${ filesystems . metadataBytes } ` , '--env' , `CODEBOOST_METADATA_INODES=${ filesystems . metadataInodes } ` ,
185+ '--env' , 'XDG_CACHE_HOME=/tmp/xdg-cache' ,
186+ '--tmpfs' , '/tmp:rw,nosuid,nodev,size=33554432,nr_inodes=4096,mode=1777' ,
187+ '--tmpfs' , '/home/codeboost:rw,nosuid,nodev,size=1048576,nr_inodes=128,uid=10001,gid=10001,mode=0700' ,
188+ '--mount' , mount ( { type : 'volume' , source : filesystems . workVolume , target : '/work' , readonly : readOnlyWork } ) ,
189+ '--mount' , mount ( { type : 'volume' , source : filesystems . metadataVolume , target : '/work/.git' , readonly : true } ) ,
190+ '--mount' , mount ( { type : 'bind' , source : inputIdentity . inputDirectory , target : '/run/codeboost-input' , readonly : true } ) ] ;
191+ if ( invocation . vendor === 'codex' ) {
192+ args . push ( '--env' , 'CODEX_HOME=/run/codeboost-auth/codex' ,
193+ '--tmpfs' , '/run/codeboost-auth/codex:rw,nosuid,nodev,size=4194304,nr_inodes=256,uid=10001,gid=10001,mode=0700' ,
194+ '--mount' , mount ( { type : 'bind' , source : codexAuthFile ! , target : '/run/codeboost-auth/codex/auth.json' , readonly : true } ) ) ;
195+ } else args . push ( '--env' , 'CLAUDE_CODE_OAUTH_TOKEN' ) ;
196+ args . push ( options . imageId , ...options . command ) ;
197+ const capturedFilesystems = filesystems ;
198+ const profile = Object . freeze ( { name, args : Object . freeze ( args ) , expectedImage : options . imageId ,
199+ phase : invocation . phase , vendor : invocation . vendor ,
200+ filesystems : capturedFilesystems , inputDirectory : inputIdentity . inputDirectory , codexAuthFile,
201+ command : Object . freeze ( [ ...options . command ] ) , ownershipId } ) ;
202+ identities . set ( profile , Object . freeze ( { inputDirectory : inputIdentity . inputDirectory , schema : inputIdentity . schema ,
203+ auth : authIdentity ,
204+ cleanupDirectories : Object . freeze ( [ ...cleanupDirectories ] ) , filesystems, clone : invocation . clone } ) ) ;
205+ return profile ;
206+ } catch ( error ) {
207+ try { removeOwnedDirectories ( cleanupDirectories ) ; }
208+ catch ( cleanupError ) { throw new AggregateError ( [ error , cleanupError ] , 'Profile creation and cleanup both failed.' ) ; }
209+ throw error ;
148210 }
149- const name = `codeboost-agent-${ safeName ( invocation . attemptId ) } ` , ownershipId = randomUUID ( ) ;
150- const readOnlyWork = [ 'planning' , 'questions' , 'review' ] . includes ( invocation . phase ) ;
151- const args = [ 'create' , '--name' , name , '--read-only' , '--user' , '10001:10001' , '--cap-drop=ALL' ,
152- '--security-opt=no-new-privileges' , '--pids-limit=128' , '--memory=512m' , '--cpus=1' ,
153- '--network=none' , '--env' , 'HOME=/home/codeboost' , '--env' , `CODEBOOST_PHASE=${ invocation . phase } ` ,
154- '--label' , `io.codeboost.invocation=${ ownershipId } ` ,
155- '--env' , `CODEBOOST_VENDOR=${ invocation . vendor } ` , '--env' , 'npm_config_cache=/tmp/npm-cache' ,
156- '--env' , `CODEBOOST_WORK_BYTES=${ filesystems . workBytes } ` , '--env' , `CODEBOOST_WORK_INODES=${ filesystems . workInodes } ` ,
157- '--env' , `CODEBOOST_METADATA_BYTES=${ filesystems . metadataBytes } ` , '--env' , `CODEBOOST_METADATA_INODES=${ filesystems . metadataInodes } ` ,
158- '--env' , 'XDG_CACHE_HOME=/tmp/xdg-cache' ,
159- '--tmpfs' , '/tmp:rw,nosuid,nodev,size=33554432,nr_inodes=4096,mode=1777' ,
160- '--tmpfs' , '/home/codeboost:rw,nosuid,nodev,size=1048576,nr_inodes=128,uid=10001,gid=10001,mode=0700' ,
161- '--mount' , mount ( { type : 'volume' , source : filesystems . workVolume , target : '/work' , readonly : readOnlyWork } ) ,
162- '--mount' , mount ( { type : 'volume' , source : filesystems . metadataVolume , target : '/work/.git' , readonly : true } ) ,
163- '--mount' , mount ( { type : 'bind' , source : inputDirectory , target : '/run/codeboost-input' , readonly : true } ) ] ;
164- if ( invocation . vendor === 'codex' ) {
165- args . push ( '--env' , 'CODEX_HOME=/run/codeboost-auth/codex' ,
166- '--tmpfs' , '/run/codeboost-auth/codex:rw,nosuid,nodev,size=4194304,nr_inodes=256,uid=10001,gid=10001,mode=0700' ,
167- '--mount' , mount ( { type : 'bind' , source : codexAuthFile ! , target : '/run/codeboost-auth/codex/auth.json' , readonly : true } ) ) ;
168- } else args . push ( '--env' , 'CLAUDE_CODE_OAUTH_TOKEN' ) ;
169- args . push ( options . imageId , ...options . command ) ;
170- const capturedFilesystems = filesystems ;
171- const profile = Object . freeze ( { name, args : Object . freeze ( args ) , expectedImage : options . imageId ,
172- phase : invocation . phase , vendor : invocation . vendor ,
173- filesystems : capturedFilesystems , inputDirectory, codexAuthFile,
174- command : Object . freeze ( [ ...options . command ] ) , ownershipId } ) ;
175- identities . set ( profile , Object . freeze ( { ...inputIdentity , auth : authIdentity , cleanupDirectory,
176- filesystems, clone : invocation . clone } ) ) ;
177- return profile ;
178211}
0 commit comments