feat(offload): implement dynamic remote home resolution for reliable rsync

This commit is contained in:
mkorwel
2026-03-14 01:47:05 -07:00
parent af9dc5be58
commit b13fe22582
3 changed files with 56 additions and 39 deletions

View File

@@ -9,10 +9,12 @@
},
"maintainer": {
"deepReview": {
"remoteHost": "bg1",
"remoteWorkDir": "~/.offload/workspace",
"projectId": "gemini-cli-team-quota",
"zone": "us-west1-a",
"machineType": "n2-standard-8",
"terminalType": "iterm2",
"syncAuth": true,
"setupType": "isolated",
"geminiSetup": "isolated",
"ghSetup": "isolated"
}

View File

@@ -40,25 +40,23 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
console.log(`🔍 Connecting to offload worker: ${targetVM}...`);
// 2. Verify Worker is RUNNING
const statusCheck = spawnSync('gcloud', [
'compute', 'instances', 'describe', targetVM,
'--project', projectId,
'--zone', zone,
'--format', 'get(status)'
], { stdio: 'pipe' });
// 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';
const status = statusCheck.stdout.toString().trim();
if (status !== 'RUNNING') {
if (status === '') {
console.log(`⚠️ Worker ${targetVM} does not exist. Please run "npm run offload:fleet provision" first.`);
} else {
console.log(`⚠️ Worker ${targetVM} is ${status}. Starting it now...`);
spawnSync('gcloud', ['compute', 'instances', 'start', targetVM, '--project', projectId, '--zone', zone], { stdio: 'inherit' });
}
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' });
}
const remoteWorkDir = '~/.offload/workspace';
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`;
const sessionName = `offload-${prNumber}-${action}`;
@@ -71,20 +69,16 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
const ghView = spawnSync(ghCmd, { shell: true });
const metaName = ghView.stdout.toString().trim() || `task-${prNumber}`;
const branchName = action === 'implement' ? `impl-${prNumber}` : metaName;
console.log(`📡 Using worker: ${targetVM}`);
// 3. Mirror logic
const ISOLATED_GEMINI = '~/.offload/gemini-cli-config';
const ISOLATED_GH = '~/.offload/gh-cli-config';
const remotePolicyPath = `${ISOLATED_GEMINI}/policies/offload-policy.toml`;
console.log(`DEBUG: Branch name for session: ${branchName}`);
console.log(`📦 Synchronizing with ${targetVM}...`);
spawnSync(`gcloud compute ssh ${targetVM} --project ${projectId} --zone ${zone} --command "mkdir -p ${remoteWorkDir} ${ISOLATED_GEMINI}/policies/"`, { shell: true });
// Sync scripts and policy
spawnSync(`rsync -avz -e "gcloud compute ssh --project ${projectId} --zone ${zone}" .gemini/skills/offload/policy.toml ${targetVM}:${remotePolicyPath}`, { shell: true });
spawnSync(`rsync -avz --delete -e "gcloud compute ssh --project ${projectId} --zone ${zone}" .gemini/skills/offload/scripts/ ${targetVM}:${remoteWorkDir}/.gemini/skills/offload/scripts/`, { shell: true });
// 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 });
if (syncAuth) {
const homeDir = env.HOME || '';
@@ -99,22 +93,43 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
}
// 4. Construct Command
const envLoader = 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh"';
const remoteWorker = `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 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';
const sshInternal = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'offload' ${q(tmuxCmd)}`;
const finalSSH = `gcloud compute ssh ${targetVM} --project ${projectId} --zone ${zone} -- -t ${q(sshInternal)}`;
const finalSSH = `${gcloudPath} compute ssh ${targetVM} --project ${projectId} --zone ${zone} -- -t ${q(sshInternal)}`;
console.log(`DEBUG: Final SSH command: ${finalSSH}`);
// 5. Terminal Automation
const isWithinGemini = !!env.GEMINI_SESSION_ID || !!env.GCLI_SESSION_ID;
if (isWithinGemini) {
const appleScript = `on run argv\n tell application "iTerm"\n set newWindow to (create window with default profile)\n tell current session of newWindow\n write text (item 1 of argv)\n end tell\n activate\n end tell\n end run`;
spawnSync('osascript', ['-', finalSSH], { input: appleScript });
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 });
console.log(`✅ iTerm2 window opened on ${targetVM}.`);
return 0;
}
console.log('🚀 Launching interactive session...');
spawnSync(finalSSH, { stdio: 'inherit', shell: true });
return 0;
}

View File

@@ -35,11 +35,11 @@ describe('Offload Orchestration (GCE)', () => {
vi.spyOn(process, 'chdir').mockImplementation(() => {});
vi.spyOn(process, 'cwd').mockReturnValue('/test-cwd');
// Default mock for gcloud instance describe
// Default mock for gcloud instance info and describe
vi.mocked(spawnSync).mockImplementation((cmd: any, args: any) => {
const callInfo = JSON.stringify({ cmd, args });
if (callInfo.includes('compute') && callInfo.includes('describe')) {
return { status: 0, stdout: Buffer.from('RUNNING\n'), stderr: Buffer.from('') } as any;
if (callInfo.includes('gcloud') && callInfo.includes('ssh') && callInfo.includes('echo $HOME')) {
return { status: 0, stdout: Buffer.from('/home/testuser\nRUNNING\n'), stderr: Buffer.from('') } as any;
}
if (callInfo.includes('gh') && callInfo.includes('view')) {
return { status: 0, stdout: Buffer.from('test-meta\n'), stderr: Buffer.from('') } as any;