2026-03-13 17:40:39 -07:00
|
|
|
/**
|
2026-03-13 19:03:30 -07:00
|
|
|
* Universal Offload Orchestrator (Local)
|
2026-03-14 00:43:53 -07:00
|
|
|
*
|
|
|
|
|
* Automatically detects and connects to your dynamic GCE fleet.
|
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-14 00:43:53 -07:00
|
|
|
const action = args[1] || 'review';
|
2026-03-13 19:03:30 -07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
// 1. Load GCP Settings
|
2026-03-13 17:40:39 -07:00
|
|
|
const settingsPath = path.join(REPO_ROOT, '.gemini/settings.json');
|
2026-03-14 00:43:53 -07:00
|
|
|
if (!fs.existsSync(settingsPath)) {
|
|
|
|
|
console.error('❌ Settings not found. Run "npm run offload:setup" first.');
|
|
|
|
|
return 1;
|
2026-03-13 17:40:39 -07:00
|
|
|
}
|
2026-03-14 00:43:53 -07:00
|
|
|
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
|
|
|
const config = settings.maintainer?.deepReview;
|
2026-03-13 17:40:39 -07:00
|
|
|
if (!config) {
|
2026-03-14 00:43:53 -07:00
|
|
|
console.error('❌ Fleet settings not found. Run "npm run offload:setup" first.');
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { projectId, zone, terminalType, syncAuth } = config;
|
2026-03-14 00:55:18 -07:00
|
|
|
const targetVM = `gcli-offload-${env.USER || 'mattkorwel'}`;
|
2026-03-14 00:43:53 -07:00
|
|
|
|
2026-03-14 00:55:18 -07:00
|
|
|
console.log(`🔍 Connecting to offload worker: ${targetVM}...`);
|
2026-03-14 00:43:53 -07:00
|
|
|
|
2026-03-14 01:47:05 -07:00
|
|
|
// 1. Get remote HOME and Status
|
|
|
|
|
const infoCheck = spawnSync(`gcloud compute ssh ${targetVM} --project ${projectId} --zone ${zone} --command "echo \$HOME && gcloud compute instances describe ${targetVM} --project ${projectId} --zone ${zone} --format='get(status)'"`, { shell: true });
|
|
|
|
|
const infoOutput = infoCheck.stdout.toString().trim().split('\n');
|
|
|
|
|
const remoteHome = infoOutput[0] || '/home/ubuntu';
|
|
|
|
|
const status = infoOutput[infoOutput.length - 1] || 'RUNNING';
|
|
|
|
|
|
|
|
|
|
console.log(`DEBUG: Remote Home: ${remoteHome}, Status: ${status}`);
|
|
|
|
|
|
|
|
|
|
if (status !== 'RUNNING' && status !== 'PROVISIONING' && status !== 'STAGING') {
|
|
|
|
|
console.log(`⚠️ Worker ${targetVM} is ${status}. Starting it now...`);
|
|
|
|
|
spawnSync(`gcloud compute instances start ${targetVM} --project ${projectId} --zone ${zone}`, { shell: true, stdio: 'inherit' });
|
2026-03-13 17:40:39 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-14 01:47:05 -07:00
|
|
|
const remoteWorkDir = `${remoteHome}/.offload/workspace`;
|
|
|
|
|
const ISOLATED_GEMINI = `${remoteHome}/.offload/gemini-cli-config`;
|
|
|
|
|
const ISOLATED_GH = `${remoteHome}/.offload/gh-cli-config`;
|
|
|
|
|
const remotePolicyPath = `${ISOLATED_GEMINI}/policies/offload-policy.toml`;
|
2026-03-14 00:55:18 -07:00
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
const sessionName = `offload-${prNumber}-${action}`;
|
|
|
|
|
|
|
|
|
|
// Fetch Metadata (local)
|
2026-03-13 19:31:32 -07:00
|
|
|
console.log(`🔍 Fetching metadata for ${action === 'implement' ? 'Issue' : 'PR'} #${prNumber}...`);
|
2026-03-14 00:43:53 -07:00
|
|
|
const ghCmd = action === 'implement'
|
|
|
|
|
? `gh issue view ${prNumber} --json title -q .title`
|
|
|
|
|
: `gh pr view ${prNumber} --json headRefName -q .headRefName`;
|
2026-03-13 19:31:32 -07:00
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
const ghView = spawnSync(ghCmd, { shell: true });
|
|
|
|
|
const metaName = ghView.stdout.toString().trim() || `task-${prNumber}`;
|
2026-03-13 19:31:32 -07:00
|
|
|
const branchName = action === 'implement' ? `impl-${prNumber}` : metaName;
|
2026-03-14 01:47:05 -07:00
|
|
|
console.log(`DEBUG: Branch name for session: ${branchName}`);
|
2026-03-13 18:40:14 -07:00
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
console.log(`📦 Synchronizing with ${targetVM}...`);
|
|
|
|
|
spawnSync(`gcloud compute ssh ${targetVM} --project ${projectId} --zone ${zone} --command "mkdir -p ${remoteWorkDir} ${ISOLATED_GEMINI}/policies/"`, { shell: true });
|
|
|
|
|
|
2026-03-14 01:47:05 -07:00
|
|
|
// Sync manifests and scripts
|
|
|
|
|
const rsyncBase = `rsync -avz -e "gcloud compute ssh --project ${projectId} --zone ${zone}"`;
|
|
|
|
|
spawnSync(`${rsyncBase} package.json package-lock.json .gemini/skills/offload/policy.toml ${targetVM}:${remoteWorkDir}/`, { shell: true });
|
|
|
|
|
spawnSync(`${rsyncBase} .gemini/skills/offload/policy.toml ${targetVM}:${remotePolicyPath}`, { shell: true });
|
|
|
|
|
spawnSync(`${rsyncBase} --delete .gemini/skills/offload/scripts/ ${targetVM}:${remoteWorkDir}/.gemini/skills/offload/scripts/`, { shell: true });
|
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-14 00:43:53 -07:00
|
|
|
if (fs.existsSync(lp)) {
|
|
|
|
|
spawnSync(`rsync -avz -e "gcloud compute ssh --project ${projectId} --zone ${zone}" ${lp} ${targetVM}:${ISOLATED_GEMINI}/${f}`, { shell: true });
|
|
|
|
|
}
|
2026-03-13 17:40:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-13 19:03:30 -07:00
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
// 4. Construct Command
|
2026-03-14 01:47:05 -07:00
|
|
|
const remoteWorker = `export GEMINI_CLI_HOME=${ISOLATED_GEMINI} && export GH_CONFIG_DIR=${ISOLATED_GH} && [ -d node_modules ] || npm install --no-audit --no-fund && node_modules/.bin/tsx .gemini/skills/offload/scripts/entrypoint.ts ${prNumber} ${branchName} ${remotePolicyPath} ${action}`;
|
|
|
|
|
const tmuxCmd = `cd ${remoteWorkDir} && ${remoteWorker}; exec $SHELL`;
|
|
|
|
|
|
|
|
|
|
const gcloudPath = spawnSync('which', ['gcloud'], { stdio: 'pipe' }).stdout.toString().trim() || 'gcloud';
|
2026-03-14 00:43:53 -07:00
|
|
|
|
|
|
|
|
const sshInternal = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'offload' ${q(tmuxCmd)}`;
|
2026-03-14 01:47:05 -07:00
|
|
|
const finalSSH = `${gcloudPath} compute ssh ${targetVM} --project ${projectId} --zone ${zone} -- -t ${q(sshInternal)}`;
|
|
|
|
|
|
|
|
|
|
console.log(`DEBUG: Final SSH command: ${finalSSH}`);
|
2026-03-13 19:03:30 -07:00
|
|
|
|
2026-03-14 00:43:53 -07:00
|
|
|
// 5. Terminal Automation
|
2026-03-14 01:47:05 -07:00
|
|
|
const isWithinGemini = !!env.GEMINI_CLI || !!env.GEMINI_SESSION_ID || !!env.GCLI_SESSION_ID;
|
|
|
|
|
console.log(`DEBUG: isWithinGemini: ${isWithinGemini}`);
|
|
|
|
|
|
|
|
|
|
if (isWithinGemini && terminalType === 'iterm2') {
|
|
|
|
|
// Write the command to a temp file to avoid AppleScript/Shell mangling
|
|
|
|
|
const tempCmdPath = path.join(process.env.TMPDIR || '/tmp', `offload-ssh-${prNumber}.sh`);
|
|
|
|
|
fs.writeFileSync(tempCmdPath, `#!/bin/bash\n${finalSSH}\nrm "$0"`, { mode: 0o755 });
|
|
|
|
|
|
|
|
|
|
const appleScript = `
|
|
|
|
|
on run argv
|
|
|
|
|
tell application "iTerm"
|
|
|
|
|
set newWindow to (create window with default profile)
|
|
|
|
|
tell current session of newWindow
|
|
|
|
|
write text (item 1 of argv) & return
|
|
|
|
|
end tell
|
|
|
|
|
activate
|
|
|
|
|
end tell
|
|
|
|
|
end run
|
|
|
|
|
`;
|
|
|
|
|
spawnSync('osascript', ['-', tempCmdPath], { input: appleScript });
|
2026-03-14 00:43:53 -07:00
|
|
|
console.log(`✅ iTerm2 window opened on ${targetVM}.`);
|
2026-03-13 18:45:06 -07:00
|
|
|
return 0;
|
2026-03-13 17:40:39 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-14 01:47:05 -07:00
|
|
|
|
|
|
|
|
console.log('🚀 Launching interactive session...');
|
2026-03-14 00:43:53 -07:00
|
|
|
spawnSync(finalSSH, { stdio: 'inherit', shell: true });
|
|
|
|
|
return 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);
|
|
|
|
|
}
|