2026-04-21 15:59:07 -07:00
import { Command } from 'commander' ;
import fs from 'node:fs/promises' ;
import path from 'node:path' ;
import { fileURLToPath } from 'node:url' ;
2026-04-22 10:42:31 -07:00
import { execSync , spawn } from 'node:child_process' ;
2026-04-21 15:59:07 -07:00
const __dirname = path . dirname ( fileURLToPath ( import . meta . url ) ) ;
async function main() {
const program = new Command ( ) ;
program
2026-04-22 15:29:20 -07:00
. option ( '--investigate' , 'Run investigation and process-updater phase' , false )
2026-04-22 10:42:31 -07:00
. option ( '--create-pr' , 'Create a PR when updating processes' , false )
2026-04-22 11:07:42 -07:00
. option ( '--execute-actions' , 'Actually execute destructive or state-changing actions (e.g., closing issues, commenting)' , false )
2026-04-21 15:59:07 -07:00
. parse ( process . argv ) ;
const options = program . opts ( ) ;
console . log ( 'Optimizer1000 starting...' ) ;
console . log ( 'Options:' , options ) ;
const rootDir = path . resolve ( __dirname , '../..' ) ;
// Ensure history directory exists so agent doesn't fail listing it
await fs . mkdir ( path . join ( rootDir , 'history' ) , { recursive : true } ) ;
// 0. Fetch previous artifacts
try {
console . log ( 'Checking for previous artifacts...' ) ;
// Check if any run exists for the current branch
const branch = execSync ( 'git rev-parse --abbrev-ref HEAD' , { encoding : 'utf-8' } ) . trim ( ) ;
const runCheck = execSync ( ` gh run list --branch ${ branch } --limit 1 --json databaseId --jq '.[0].databaseId' || true ` , { encoding : 'utf-8' } ) . trim ( ) ;
if ( runCheck && runCheck !== '' ) {
console . log ( 'Attempting to fetch previous artifacts into history/ (timeout 30s)...' ) ;
await fs . mkdir ( path . join ( rootDir , 'history' ) , { recursive : true } ) ;
// Download will fail gracefully if the artifact name doesn't match
execSync ( ` gh run download --name optimizer-results --pattern "*.csv" --dir history > /dev/null 2>&1 || true ` , {
stdio : 'inherit' ,
timeout : 30000 ,
cwd : rootDir
} ) ;
} else {
console . log ( 'No previous runs found, skipping download.' ) ;
}
} catch ( err ) {
console . warn ( 'Artifact check/download skipped, proceeding with fresh state.' ) ;
}
2026-04-22 11:07:42 -07:00
const policyPath = options . executeActions ? undefined : path . join ( __dirname , 'policies' , 'readonly-gh.toml' ) ;
2026-04-21 15:59:07 -07:00
// 1. Initial Metrics
2026-04-22 11:07:42 -07:00
await runPhase ( 'metrics' , { PRE_RUN : 'true' } , options , policyPath ) ;
2026-04-21 15:59:07 -07:00
2026-04-22 15:29:20 -07:00
// 2. Investigation & Update Processes (Optional)
2026-04-21 15:59:07 -07:00
if ( options . investigate ) {
2026-04-22 15:29:20 -07:00
await runPhase ( 'investigations' , {
EXECUTE_ACTIONS : String ( options . executeActions ) ,
} , options , undefined ) ;
2026-04-22 11:07:42 -07:00
2026-04-22 15:29:20 -07:00
// 3. Critique Phase (Only runs if investigations ran)
await runPhase ( 'critique' , {
2026-04-22 11:07:42 -07:00
CREATE_PR : String ( options . createPr ) ,
EXECUTE_ACTIONS : String ( options . executeActions ) ,
2026-04-22 15:29:20 -07:00
} , options , undefined ) ;
2026-04-21 15:59:07 -07:00
}
2026-04-22 11:07:42 -07:00
// 4. Run Processes
2026-04-21 15:59:07 -07:00
await runPhase ( 'processes' , {
2026-04-22 11:07:42 -07:00
EXECUTE_ACTIONS : String ( options . executeActions ) ,
} , options , policyPath ) ;
2026-04-21 15:59:07 -07:00
2026-04-22 11:07:42 -07:00
// 5. Final Metrics
await runPhase ( 'metrics' , { PRE_RUN : 'false' } , options , policyPath ) ;
2026-04-21 15:59:07 -07:00
console . log ( '\nOptimizer1000 completed.' ) ;
}
2026-04-22 15:29:20 -07:00
async function runPhase ( phaseDir : string , env : Record < string , string > , options : any , policyPath? : string ) : Promise < string | undefined > {
2026-04-21 15:59:07 -07:00
console . log ( ` \ n--- Phase: ${ phaseDir } --- ` ) ;
const phasePath = path . join ( __dirname , phaseDir ) ;
let promptFile : string | undefined ;
try {
const files = await fs . readdir ( phasePath ) ;
promptFile = files . find ( f = > f . endsWith ( '-AGENT.md' ) ) ;
} catch ( err ) {
console . warn ( ` Directory ${ phaseDir } not found or inaccessible. ` ) ;
return ;
}
if ( ! promptFile ) {
console . warn ( ` No agent prompt found in ${ phaseDir } ` ) ;
return ;
}
const instructionsPath = path . join ( phasePath , promptFile ) ;
2026-04-22 10:42:31 -07:00
const instructionsContent = await fs . readFile ( instructionsPath , 'utf8' ) ;
2026-04-21 15:59:07 -07:00
const envString = Object . entries ( env ) . map ( ( [ k , v ] ) = > ` ${ k } = ${ v } ` ) . join ( '\n' ) ;
2026-04-22 10:42:31 -07:00
const userPrompt = ` Execution Context: \ n ${ envString } \ n \ n ${ instructionsContent } \ n \ nPlease proceed with the ${ phaseDir } tasks as defined in your instructions. Always output CSV files as requested. ` ;
2026-04-21 15:59:07 -07:00
console . log ( ` Running agent with prompt: ${ promptFile } ` ) ;
// Resolve root to call the CLI binary
const rootDir = path . resolve ( __dirname , '../..' ) ;
try {
2026-04-22 11:07:42 -07:00
// Run GCLI non-interactively. Use --yolo to auto-approve 'allow' rules,
// but policies can still 'deny' actions.
2026-04-21 15:59:07 -07:00
const cliPath = path . join ( rootDir , 'packages' , 'cli' ) ;
2026-04-22 11:07:42 -07:00
const args = [ '--prompt' , userPrompt , '--yolo' , '--model' , 'gemini-3-flash-preview' ] ;
if ( policyPath ) {
args . push ( '--admin-policy' , policyPath ) ;
}
2026-04-21 15:59:07 -07:00
2026-04-22 10:42:31 -07:00
await new Promise < void > ( ( resolve , reject ) = > {
2026-04-22 11:07:42 -07:00
const child = spawn ( 'node' , [ cliPath , . . . args ] , {
2026-04-22 10:42:31 -07:00
stdio : 'inherit' ,
cwd : rootDir ,
env : { . . . process . env , . . . env }
} ) ;
child . on ( 'close' , ( code ) = > {
if ( code === 0 ) {
resolve ( ) ;
} else {
reject ( new Error ( ` Exit code ${ code } ` ) ) ;
}
} ) ;
child . on ( 'error' , ( err ) = > {
reject ( err ) ;
} ) ;
2026-04-21 15:59:07 -07:00
} ) ;
2026-04-22 10:42:31 -07:00
} catch ( err : any ) {
2026-04-21 15:59:07 -07:00
console . error ( ` Error in phase ${ phaseDir } : ` , err . message ) ;
}
console . log ( ` \ n--- Finished Phase: ${ phaseDir } --- ` ) ;
}
main ( ) . catch ( err = > {
console . error ( 'Fatal error:' , err ) ;
process . exit ( 1 ) ;
} ) ;