2026-03-13 17:40:39 -07:00
/**
2026-03-13 19:03:30 -07:00
* Universal Offload Orchestrator (Local)
2026-03-13 17:40:39 -07:00
*/
import { spawnSync } from 'child_process' ;
import path from 'path' ;
import fs from 'fs' ;
import { fileURLToPath } from 'url' ;
const __dirname = path . dirname ( fileURLToPath ( import . meta . url ) ) ;
const REPO_ROOT = path . resolve ( __dirname , '../../../..' ) ;
const q = ( str : string ) = > ` ' ${ str . replace ( /'/g , "'\\''" ) } ' ` ;
2026-03-13 18:45:06 -07:00
export async function runOrchestrator ( args : string [ ] , env : NodeJS.ProcessEnv = process . env ) {
const prNumber = args [ 0 ] ;
2026-03-13 19:03:30 -07:00
const action = args [ 1 ] || 'review' ; // Default action is review
2026-03-13 17:40:39 -07:00
if ( ! prNumber ) {
2026-03-13 19:03:30 -07:00
console . error ( 'Usage: npm run offload <PR_NUMBER> [action]' ) ;
2026-03-13 18:45:06 -07:00
return 1 ;
2026-03-13 17:40:39 -07:00
}
// Load Settings
const settingsPath = path . join ( REPO_ROOT , '.gemini/settings.json' ) ;
let settings : any = { } ;
if ( fs . existsSync ( settingsPath ) ) {
try { settings = JSON . parse ( fs . readFileSync ( settingsPath , 'utf8' ) ) ; } catch ( e ) { }
}
let config = settings . maintainer ? . deepReview ;
if ( ! config ) {
2026-03-13 19:03:30 -07:00
console . log ( '⚠️ Offload configuration not found. Launching setup...' ) ;
const setupResult = spawnSync ( 'npm' , [ 'run' , 'offload:setup' ] , { stdio : 'inherit' } ) ;
2026-03-13 17:40:39 -07:00
if ( setupResult . status !== 0 ) {
2026-03-13 19:03:30 -07:00
console . error ( '❌ Setup failed. Please run "npm run offload:setup" manually.' ) ;
2026-03-13 18:45:06 -07:00
return 1 ;
2026-03-13 17:40:39 -07:00
}
// Reload settings after setup
settings = JSON . parse ( fs . readFileSync ( settingsPath , 'utf8' ) ) ;
config = settings . maintainer . deepReview ;
}
2026-03-13 18:40:14 -07:00
const { remoteHost , remoteWorkDir , terminalType , syncAuth , geminiSetup , ghSetup } = config ;
2026-03-13 17:40:39 -07:00
console . log ( ` 🔍 Fetching metadata for PR # ${ prNumber } ... ` ) ;
const ghView = spawnSync ( 'gh' , [ 'pr' , 'view' , prNumber , '--json' , 'headRefName' , '-q' , '.headRefName' ] , { shell : true } ) ;
const branchName = ghView . stdout . toString ( ) . trim ( ) ;
if ( ! branchName ) {
console . error ( '❌ Failed to resolve PR branch.' ) ;
2026-03-13 18:45:06 -07:00
return 1 ;
2026-03-13 17:40:39 -07:00
}
const sessionName = ` ${ prNumber } - ${ branchName . replace ( /[^a-zA-Z0-9]/g , '_' ) } ` ;
2026-03-13 18:40:14 -07:00
// 2. Sync Configuration Mirror (Isolated Profiles)
const ISOLATED_GEMINI = geminiSetup === 'isolated' ? '~/.gemini-deep-review' : '~/.gemini' ;
const ISOLATED_GH = ghSetup === 'isolated' ? '~/.gh-deep-review' : '~/.config/gh' ;
const remotePolicyPath = ` ${ ISOLATED_GEMINI } /policies/deep-review-policy.toml ` ;
2026-03-13 17:40:39 -07:00
console . log ( ` 📡 Mirroring environment to ${ remoteHost } ... ` ) ;
2026-03-13 19:03:30 -07:00
spawnSync ( 'ssh' , [ remoteHost , ` mkdir -p ${ remoteWorkDir } /.gemini/skills/offload/scripts/ ${ ISOLATED_GEMINI } /policies/ ` ] ) ;
2026-03-13 17:40:39 -07:00
2026-03-13 18:40:14 -07:00
// Sync the policy file specifically
2026-03-13 19:03:30 -07:00
spawnSync ( 'rsync' , [ '-avz' , path . join ( REPO_ROOT , '.gemini/skills/offload/policy.toml' ) , ` ${ remoteHost } : ${ remotePolicyPath } ` ] ) ;
2026-03-13 18:40:14 -07:00
2026-03-13 19:03:30 -07:00
spawnSync ( 'rsync' , [ '-avz' , '--delete' , path . join ( REPO_ROOT , '.gemini/skills/offload/scripts/' ) , ` ${ remoteHost } : ${ remoteWorkDir } /.gemini/skills/offload/scripts/ ` ] ) ;
2026-03-13 17:40:39 -07:00
if ( syncAuth ) {
2026-03-13 18:45:06 -07:00
const homeDir = env . HOME || '' ;
2026-03-13 17:40:39 -07:00
const localGeminiDir = path . join ( homeDir , '.gemini' ) ;
const syncFiles = [ 'google_accounts.json' , 'settings.json' ] ;
for ( const f of syncFiles ) {
const lp = path . join ( localGeminiDir , f ) ;
2026-03-13 18:40:14 -07:00
if ( fs . existsSync ( lp ) ) spawnSync ( 'rsync' , [ '-avz' , lp , ` ${ remoteHost } : ${ ISOLATED_GEMINI } / ${ f } ` ] ) ;
2026-03-13 17:40:39 -07:00
}
const localPolicies = path . join ( localGeminiDir , 'policies/' ) ;
2026-03-13 18:40:14 -07:00
if ( fs . existsSync ( localPolicies ) ) spawnSync ( 'rsync' , [ '-avz' , '--delete' , localPolicies , ` ${ remoteHost } : ${ ISOLATED_GEMINI } /policies/ ` ] ) ;
2026-03-13 17:40:39 -07:00
const localEnv = path . join ( REPO_ROOT , '.env' ) ;
if ( fs . existsSync ( localEnv ) ) spawnSync ( 'rsync' , [ '-avz' , localEnv , ` ${ remoteHost } : ${ remoteWorkDir } /.env ` ] ) ;
}
2026-03-13 19:03:30 -07:00
// 3. Construct Clean Command
const envLoader = 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh"' ;
// Set FORCE_LOCAL_OPEN=1 to signal the worker to use OSC 1337 for links
const remoteWorker = ` export FORCE_LOCAL_OPEN=1 && export GEMINI_CLI_HOME= ${ ISOLATED_GEMINI } && export GH_CONFIG_DIR= ${ ISOLATED_GH } && ./node_modules/.bin/tsx .gemini/skills/offload/scripts/entrypoint.ts ${ prNumber } ${ branchName } ${ remotePolicyPath } ${ action } ` ;
const tmuxCmd = ` cd ${ remoteWorkDir } && ${ envLoader } && ${ remoteWorker } ; exec $ SHELL ` ;
const sshInternal = ` tmux attach-session -t ${ sessionName } 2>/dev/null || tmux new-session -s ${ sessionName } -n ${ q ( branchName ) } ${ q ( tmuxCmd ) } ` ;
const sshCmd = ` ssh -t ${ remoteHost } ${ q ( sshInternal ) } ` ;
// 4. Smart Context Execution
const isWithinGemini = ! ! env . GEMINI_SESSION_ID || ! ! env . GCLI_SESSION_ID ;
const forceBackground = args . includes ( '--background' ) ;
if ( isWithinGemini || forceBackground ) {
if ( process . platform === 'darwin' && terminalType !== 'none' && ! forceBackground ) {
// macOS: Use Window Automation
let appleScript = ` on run argv \ n set theCommand to item 1 of argv \ n tell application "iTerm" \ n set newWindow to (create window with default profile) \ n tell current session of newWindow \ n write text theCommand \ n end tell \ n activate \ n end tell \ n end run ` ;
if ( terminalType === 'terminal' ) {
2026-03-13 17:40:39 -07:00
appleScript = ` on run argv \ n set theCommand to item 1 of argv \ n tell application "Terminal" \ n do script theCommand \ n activate \ n end tell \ n end run ` ;
}
2026-03-13 19:03:30 -07:00
spawnSync ( 'osascript' , [ '-' , sshCmd ] , { input : appleScript } ) ;
console . log ( ` ✅ ${ terminalType . toUpperCase ( ) } window opened for verification. ` ) ;
return 0 ;
2026-03-13 17:40:39 -07:00
}
2026-03-13 19:03:30 -07:00
// Cross-Platform Background Mode
2026-03-13 17:40:39 -07:00
console . log ( ` 📡 Launching remote verification in background mode... ` ) ;
2026-03-13 19:03:30 -07:00
const logFile = path . join ( REPO_ROOT , ` .gemini/logs/offload- ${ prNumber } /background.log ` ) ;
2026-03-13 17:40:39 -07:00
fs . mkdirSync ( path . dirname ( logFile ) , { recursive : true } ) ;
2026-03-13 18:51:48 -07:00
const backgroundCmd = ` ssh ${ remoteHost } ${ q ( tmuxCmd ) } > ${ q ( logFile ) } 2>&1 & ` ;
2026-03-13 17:40:39 -07:00
spawnSync ( backgroundCmd , { shell : true } ) ;
console . log ( ` ⏳ Remote worker started in background. ` ) ;
2026-03-13 19:03:30 -07:00
console . log ( ` 📄 Tailing logs to: .gemini/logs/offload- ${ prNumber } /background.log ` ) ;
2026-03-13 18:45:06 -07:00
return 0 ;
2026-03-13 17:40:39 -07:00
}
// Direct Shell Mode: Execute SSH in-place
2026-03-13 19:03:30 -07:00
console . log ( ` 🚀 Launching offload session in current terminal... ` ) ;
2026-03-13 17:40:39 -07:00
const result = spawnSync ( sshCmd , { stdio : 'inherit' , shell : true } ) ;
2026-03-13 18:45:06 -07:00
return result . status || 0 ;
2026-03-13 17:40:39 -07:00
}
2026-03-13 18:45:06 -07:00
if ( import . meta . url === ` file:// ${ process . argv [ 1 ] } ` ) {
runOrchestrator ( process . argv . slice ( 2 ) ) . catch ( console . error ) ;
}