2026-03-13 17:40:39 -07:00
|
|
|
import path from 'path';
|
|
|
|
|
import fs from 'fs';
|
2026-03-17 13:23:15 -07:00
|
|
|
import { spawnSync } from 'child_process';
|
2026-03-13 17:40:39 -07:00
|
|
|
import { fileURLToPath } from 'url';
|
2026-03-16 15:33:40 -07:00
|
|
|
import { ProviderFactory } from './providers/ProviderFactory.ts';
|
2026-03-13 17:40:39 -07:00
|
|
|
|
|
|
|
|
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-18 11:18:53 -07:00
|
|
|
console.error('Usage: npm run workspace <PR_NUMBER> [action]');
|
2026-03-13 18:45:06 -07:00
|
|
|
return 1;
|
2026-03-13 17:40:39 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-14 09:23:40 -07:00
|
|
|
// 1. Load Settings
|
2026-03-18 11:18:53 -07:00
|
|
|
const settingsPath = path.join(REPO_ROOT, '.gemini/workspaces/settings.json');
|
2026-03-16 15:33:40 -07:00
|
|
|
if (!fs.existsSync(settingsPath)) {
|
2026-03-18 11:18:53 -07:00
|
|
|
console.error('❌ Settings not found. Run "npm run workspace:setup" first.');
|
2026-03-16 15:33:40 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
2026-03-14 00:43:53 -07:00
|
|
|
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
2026-03-18 11:18:53 -07:00
|
|
|
const config = settings.workspace;
|
2026-03-13 17:40:39 -07:00
|
|
|
if (!config) {
|
2026-03-18 11:23:29 -07:00
|
|
|
console.error('❌ Workspace configuration not found.');
|
2026-03-14 00:43:53 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 11:23:29 -07:00
|
|
|
const { projectId, zone, remoteWorkDir } = config;
|
2026-03-18 11:18:53 -07:00
|
|
|
const targetVM = `gcli-workspace-${env.USER || 'mattkorwel'}`;
|
2026-03-16 15:33:40 -07:00
|
|
|
const provider = ProviderFactory.getProvider({ projectId, zone, instanceName: targetVM });
|
2026-03-14 00:43:53 -07:00
|
|
|
|
2026-03-17 15:34:13 -07:00
|
|
|
// 2. Wake Worker & Verify Container
|
2026-03-16 15:33:40 -07:00
|
|
|
await provider.ensureReady();
|
2026-03-13 17:40:39 -07:00
|
|
|
|
2026-03-17 21:22:23 -07:00
|
|
|
// Use Absolute Container Paths
|
2026-03-17 15:34:13 -07:00
|
|
|
const containerHome = '/home/node';
|
2026-03-18 11:18:53 -07:00
|
|
|
const remotePolicyPath = `${containerHome}/.gemini/policies/workspace-policy.toml`;
|
2026-03-18 11:23:29 -07:00
|
|
|
const persistentScripts = `${containerHome}/.workspaces/scripts`;
|
2026-03-18 11:18:53 -07:00
|
|
|
const sessionName = `workspace-${prNumber}-${action}`;
|
2026-03-17 15:34:13 -07:00
|
|
|
const remoteWorktreeDir = `${containerHome}/dev/worktrees/${sessionName}`;
|
2026-03-14 00:43:53 -07:00
|
|
|
|
2026-03-17 21:22:23 -07:00
|
|
|
// 3. Remote Context Setup
|
|
|
|
|
console.log(`🚀 Preparing remote environment for ${action} on #${prNumber}...`);
|
2026-03-15 18:37:56 -07:00
|
|
|
|
2026-03-17 21:22:23 -07:00
|
|
|
const check = await provider.getExecOutput(`ls -d ${remoteWorktreeDir}/.git`, { wrapContainer: 'maintainer-worker' });
|
|
|
|
|
|
|
|
|
|
if (check.status !== 0) {
|
|
|
|
|
console.log(' - Provisioning isolated git worktree...');
|
2026-03-17 21:48:23 -07:00
|
|
|
await provider.exec(`sudo docker exec -u root maintainer-worker mkdir -p ${containerHome}/dev/worktrees && sudo docker exec -u root maintainer-worker chown -R node:node ${containerHome}/dev/worktrees`);
|
2026-03-17 21:22:23 -07:00
|
|
|
|
|
|
|
|
const setupCmd = `
|
2026-03-18 11:23:29 -07:00
|
|
|
git config --global --add safe.directory ${containerHome}/dev/main && \
|
2026-03-17 21:22:23 -07:00
|
|
|
mkdir -p ${containerHome}/dev/worktrees && \
|
2026-03-18 11:23:29 -07:00
|
|
|
cd ${containerHome}/dev/main && \
|
2026-03-17 21:22:23 -07:00
|
|
|
git fetch upstream pull/${prNumber}/head && \
|
|
|
|
|
git worktree add -f ${remoteWorktreeDir} FETCH_HEAD
|
|
|
|
|
`;
|
|
|
|
|
await provider.exec(setupCmd, { wrapContainer: 'maintainer-worker' });
|
2026-03-15 18:37:56 -07:00
|
|
|
} else {
|
2026-03-17 21:22:23 -07:00
|
|
|
console.log(' ✅ Remote worktree ready.');
|
2026-03-15 18:37:56 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 21:22:23 -07:00
|
|
|
// 4. Execution Logic
|
|
|
|
|
const remoteWorker = `tsx ${persistentScripts}/entrypoint.ts ${prNumber} . ${remotePolicyPath} ${action}`;
|
2026-03-18 11:23:29 -07:00
|
|
|
|
|
|
|
|
// tmux command inside container
|
2026-03-18 11:18:53 -07:00
|
|
|
const remoteTmuxCmd = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'workspace' 'cd ${remoteWorktreeDir} && ${remoteWorker}; exec $SHELL'`;
|
2026-03-15 16:20:31 -07:00
|
|
|
|
2026-03-18 11:23:29 -07:00
|
|
|
const terminalTarget = config.terminalTarget || 'tab';
|
2026-03-17 13:23:15 -07:00
|
|
|
const isWithinGemini = !!env.GEMINI_CLI || !!env.GEMINI_SESSION_ID || !!env.GCLI_SESSION_ID;
|
|
|
|
|
|
2026-03-18 11:23:29 -07:00
|
|
|
// Handle different UI targets
|
|
|
|
|
switch (terminalTarget) {
|
|
|
|
|
case 'background':
|
|
|
|
|
console.log(`📡 Job #${prNumber} starting in background (session: ${sessionName}).`);
|
|
|
|
|
// Remove -it for background launch
|
|
|
|
|
const bgWrap = `sudo docker exec maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
|
|
|
|
await provider.exec(bgWrap);
|
|
|
|
|
console.log(`✅ Job is running. Attach anytime with: npm run workspace:attach ${prNumber} ${action}`);
|
|
|
|
|
return 0;
|
2026-03-17 13:23:15 -07:00
|
|
|
|
2026-03-18 11:23:29 -07:00
|
|
|
case 'tab':
|
|
|
|
|
case 'window':
|
|
|
|
|
if (isWithinGemini && env.TERM_PROGRAM === 'iTerm.app') {
|
|
|
|
|
const containerWrap = `sudo docker exec -it maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
|
|
|
|
const finalSSH = provider.getRunCommand(containerWrap, { interactive: true });
|
|
|
|
|
const tempCmdPath = path.join(process.env.TMPDIR || '/tmp', `workspace-ssh-${prNumber}.sh`);
|
|
|
|
|
fs.writeFileSync(tempCmdPath, `#!/bin/bash\n${finalSSH}\nrm "$0"`, { mode: 0o755 });
|
2026-03-13 17:40:39 -07:00
|
|
|
|
2026-03-18 11:23:29 -07:00
|
|
|
const appleScript = terminalTarget === 'window' ? `
|
|
|
|
|
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
|
|
|
|
|
` : `
|
|
|
|
|
on run argv
|
|
|
|
|
tell application "iTerm"
|
|
|
|
|
tell current window
|
|
|
|
|
set newTab to (create tab with default profile)
|
|
|
|
|
tell current session of newTab
|
|
|
|
|
write text (item 1 of argv) & return
|
|
|
|
|
end tell
|
|
|
|
|
end tell
|
|
|
|
|
activate
|
|
|
|
|
end tell
|
|
|
|
|
end run
|
|
|
|
|
`;
|
|
|
|
|
spawnSync('osascript', ['-', tempCmdPath], { input: appleScript });
|
|
|
|
|
console.log(`✅ iTerm2 ${terminalTarget} opened for job #${prNumber}.`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// Fallthrough to foreground if not in iTerm
|
|
|
|
|
console.log(' ⚠️ iTerm2 not detected or not in Gemini. Falling back to foreground...');
|
|
|
|
|
|
|
|
|
|
case 'foreground':
|
|
|
|
|
default:
|
|
|
|
|
console.log(`📡 Connecting to session ${sessionName}...`);
|
|
|
|
|
const fgWrap = `sudo docker exec -it maintainer-worker sh -c ${q(remoteTmuxCmd)}`;
|
|
|
|
|
const fgSSH = provider.getRunCommand(fgWrap, { interactive: true });
|
|
|
|
|
spawnSync(fgSSH, { 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);
|
|
|
|
|
}
|