mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 21:03:05 -07:00
feat(offload): unify host/container paths and mounts for seamless orchestration
This commit is contained in:
@@ -52,7 +52,9 @@ async function provisionWorker() {
|
|||||||
'--container-image', imageUri,
|
'--container-image', imageUri,
|
||||||
'--container-name', 'gemini-sandbox',
|
'--container-name', 'gemini-sandbox',
|
||||||
'--container-restart-policy', 'always',
|
'--container-restart-policy', 'always',
|
||||||
'--container-mount-host-path', `mount-path=/home/node/dev,host-path=/home/$(whoami)/dev,mode=rw`,
|
'--container-mount-host-path', 'host-path=/home/$(whoami)/dev,mount-path=/home/node/dev,mode=rw',
|
||||||
|
'--container-mount-host-path', 'host-path=/home/$(whoami)/.gemini,mount-path=/home/node/.gemini,mode=rw',
|
||||||
|
'--container-mount-host-path', 'host-path=/home/$(whoami)/.offload,mount-path=/home/node/.offload,mode=rw',
|
||||||
'--boot-disk-size', '50GB',
|
'--boot-disk-size', '50GB',
|
||||||
'--labels', `owner=${USER.replace(/[^a-z0-9_-]/g, '_')},type=offload-worker`,
|
'--labels', `owner=${USER.replace(/[^a-z0-9_-]/g, '_')},type=offload-worker`,
|
||||||
'--tags', `gcli-offload-${USER}`,
|
'--tags', `gcli-offload-${USER}`,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { projectId, zone, remoteHost, remoteHome, remoteWorkDir, useContainer } = config;
|
const { projectId, zone, remoteHost, remoteWorkDir, useContainer } = config;
|
||||||
const targetVM = `gcli-offload-${env.USER || 'mattkorwel'}`;
|
const targetVM = `gcli-offload-${env.USER || 'mattkorwel'}`;
|
||||||
|
|
||||||
// 2. Wake Worker
|
// 2. Wake Worker
|
||||||
@@ -43,16 +43,16 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
|||||||
spawnSync(`gcloud compute instances start ${targetVM} --project ${projectId} --zone ${zone}`, { shell: true, stdio: 'inherit' });
|
spawnSync(`gcloud compute instances start ${targetVM} --project ${projectId} --zone ${zone}`, { shell: true, stdio: 'inherit' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const remotePolicyPath = `${remoteHome}/.gemini/policies/offload-policy.toml`;
|
const remotePolicyPath = `~/.gemini/policies/offload-policy.toml`;
|
||||||
const persistentScripts = `${remoteHome}/.offload/scripts`;
|
const persistentScripts = `~/.offload/scripts`;
|
||||||
const sessionName = `offload-${prNumber}-${action}`;
|
const sessionName = `offload-${prNumber}-${action}`;
|
||||||
|
|
||||||
// 3. Remote Context Setup (Parallel Worktree)
|
// 3. Remote Context Setup (Parallel Worktree)
|
||||||
console.log(`🚀 Provisioning clean worktree for ${action} on PR #${prNumber}...`);
|
console.log(`🚀 Provisioning clean worktree for ${action} on PR #${prNumber}...`);
|
||||||
const remoteWorktreeDir = `${remoteHome}/dev/worktrees/offload-${prNumber}-${action}`;
|
const remoteWorktreeDir = `~/dev/worktrees/offload-${prNumber}-${action}`;
|
||||||
|
|
||||||
const setupCmd = `
|
const setupCmd = `
|
||||||
mkdir -p ${remoteHome}/dev/worktrees && \
|
mkdir -p ~/dev/worktrees && \
|
||||||
cd ${remoteWorkDir} && \
|
cd ${remoteWorkDir} && \
|
||||||
git fetch upstream pull/${prNumber}/head && \
|
git fetch upstream pull/${prNumber}/head && \
|
||||||
git worktree add -f ${remoteWorktreeDir} FETCH_HEAD
|
git worktree add -f ${remoteWorktreeDir} FETCH_HEAD
|
||||||
@@ -70,10 +70,7 @@ export async function runOrchestrator(args: string[], env: NodeJS.ProcessEnv = p
|
|||||||
|
|
||||||
let tmuxCmd = `cd ${remoteWorktreeDir} && ${remoteWorker}; exec $SHELL`;
|
let tmuxCmd = `cd ${remoteWorktreeDir} && ${remoteWorker}; exec $SHELL`;
|
||||||
if (useContainer) {
|
if (useContainer) {
|
||||||
// Inside container, we need to ensure the environment is loaded
|
|
||||||
tmuxCmd = `docker exec -it -w ${remoteWorktreeDir} gemini-sandbox sh -c "${remoteWorker}; exec $SHELL"`;
|
tmuxCmd = `docker exec -it -w ${remoteWorktreeDir} gemini-sandbox sh -c "${remoteWorker}; exec $SHELL"`;
|
||||||
} else {
|
|
||||||
tmuxCmd = `cd ${remoteWorktreeDir} && ${tmuxCmd}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sshInternal = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'offload' ${q(tmuxCmd)}`;
|
const sshInternal = `tmux attach-session -t ${sessionName} 2>/dev/null || tmux new-session -s ${sessionName} -n 'offload' ${q(tmuxCmd)}`;
|
||||||
|
|||||||
@@ -81,73 +81,47 @@ Host ${sshAlias}
|
|||||||
if (!currentConfig.includes(`Host ${sshAlias}`)) {
|
if (!currentConfig.includes(`Host ${sshAlias}`)) {
|
||||||
fs.appendFileSync(sshConfigPath, sshEntry);
|
fs.appendFileSync(sshConfigPath, sshEntry);
|
||||||
console.log(` ✅ Added '${sshAlias}' alias to ~/.ssh/config`);
|
console.log(` ✅ Added '${sshAlias}' alias to ~/.ssh/config`);
|
||||||
} else {
|
|
||||||
console.log(` ℹ️ '${sshAlias}' alias already exists in ~/.ssh/config`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1b. Security Fork Management
|
// 1b. Security Fork Management
|
||||||
console.log('\n🍴 Configuring Security Fork...');
|
console.log('\n🍴 Configuring Security Fork...');
|
||||||
const upstreamRepo = 'google-gemini/gemini-cli';
|
const upstreamRepo = 'google-gemini/gemini-cli';
|
||||||
|
|
||||||
const forksQuery = spawnSync('gh', ['api', 'user/repos', '--paginate', '-q', `.[] | select(.fork == true and .parent.full_name == "${upstreamRepo}") | .full_name`], { stdio: 'pipe' });
|
const forksQuery = spawnSync('gh', ['api', 'user/repos', '--paginate', '-q', `.[] | select(.fork == true and .parent.full_name == "${upstreamRepo}") | .full_name`], { stdio: 'pipe' });
|
||||||
const existingForks = forksQuery.stdout.toString().trim().split('\n').filter(Boolean);
|
const existingForks = forksQuery.stdout.toString().trim().split('\n').filter(Boolean);
|
||||||
|
|
||||||
let userFork = '';
|
let userFork = '';
|
||||||
if (existingForks.length > 0) {
|
if (existingForks.length > 0) {
|
||||||
console.log(` 🔍 Found existing fork(s):`);
|
if (await confirm(` Found existing fork ${existingForks[0]}. Use it?`)) {
|
||||||
existingForks.forEach((f, i) => console.log(` ${i + 1}. ${f}`));
|
|
||||||
|
|
||||||
if (existingForks.length === 1) {
|
|
||||||
if (await confirm(` Use existing fork ${existingForks[0]}?`)) {
|
|
||||||
userFork = existingForks[0];
|
userFork = existingForks[0];
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
const choice = await prompt(` Select fork (1-${existingForks.length}) or type 'new'`, '1');
|
|
||||||
if (choice !== 'new') userFork = existingForks[parseInt(choice) - 1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userFork) {
|
if (!userFork) {
|
||||||
console.log(` 🔍 No fork selected or detected.`);
|
console.log(` 🔍 Creating a fresh personal fork...`);
|
||||||
if (await confirm(' Create a fresh personal fork?')) {
|
|
||||||
spawnSync('gh', ['repo', 'fork', upstreamRepo, '--clone=false'], { stdio: 'inherit' });
|
spawnSync('gh', ['repo', 'fork', upstreamRepo, '--clone=false'], { stdio: 'inherit' });
|
||||||
const user = spawnSync('gh', ['api', 'user', '-q', '.login'], { stdio: 'pipe' }).stdout.toString().trim();
|
const user = spawnSync('gh', ['api', 'user', '-q', '.login'], { stdio: 'pipe' }).stdout.toString().trim();
|
||||||
userFork = `${user}/gemini-cli`;
|
userFork = `${user}/gemini-cli`;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!userFork) {
|
|
||||||
console.error('❌ A personal fork is required for autonomous offload tasks.');
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
console.log(` ✅ Using fork: ${userFork}`);
|
console.log(` ✅ Using fork: ${userFork}`);
|
||||||
|
|
||||||
// Resolve Paths
|
// Resolve Paths (Simplified with Tilde)
|
||||||
const remoteHost = sshAlias;
|
const remoteHost = sshAlias;
|
||||||
// Standard home is /home/node inside our maintainer container
|
const remoteWorkDir = `~/dev/main`;
|
||||||
const remoteHome = useContainer ? '/home/node' : spawnSync(`ssh ${remoteHost} "pwd"`, { shell: true }).stdout.toString().trim();
|
const persistentScripts = `~/.offload/scripts`;
|
||||||
const remoteWorkDir = `${remoteHome}/dev/main`;
|
|
||||||
const persistentScripts = `${remoteHome}/.offload/scripts`;
|
|
||||||
|
|
||||||
console.log(`\n📦 Performing One-Time Synchronization...`);
|
console.log(`\n📦 Performing One-Time Synchronization...`);
|
||||||
// If in container mode, we use the host mount logic
|
spawnSync(`ssh ${remoteHost} "mkdir -p ${remoteWorkDir} ~/.gemini/policies ${persistentScripts}"`, { shell: true });
|
||||||
const mkdirCmd = useContainer
|
|
||||||
? `docker exec gemini-sandbox mkdir -p ${remoteWorkDir} ${remoteHome}/.gemini/policies ${persistentScripts}`
|
|
||||||
: `mkdir -p ${remoteWorkDir} ${remoteHome}/.gemini/policies ${persistentScripts}`;
|
|
||||||
|
|
||||||
spawnSync(`ssh ${remoteHost} ${JSON.stringify(mkdirCmd)}`, { shell: true });
|
|
||||||
|
|
||||||
const rsyncBase = `rsync -avz -e "ssh"`;
|
const rsyncBase = `rsync -avz -e "ssh"`;
|
||||||
|
|
||||||
// 2. Sync Settings & Policies
|
// 2. Sync Settings & Policies
|
||||||
if (await confirm('Sync local settings and security policies?')) {
|
if (await confirm('Sync local settings and security policies?')) {
|
||||||
const localSettings = path.join(REPO_ROOT, '.gemini/settings.json');
|
const localSettings = path.join(REPO_ROOT, '.gemini/settings.json');
|
||||||
const remoteDest = useContainer ? `${remoteHost}:~/dev/.gemini/` : `${remoteHost}:${remoteHome}/.gemini/`;
|
|
||||||
if (fs.existsSync(localSettings)) {
|
if (fs.existsSync(localSettings)) {
|
||||||
spawnSync(`${rsyncBase} ${localSettings} ${remoteDest}`, { shell: true });
|
spawnSync(`${rsyncBase} ${localSettings} ${remoteHost}:~/.gemini/`, { shell: true });
|
||||||
}
|
}
|
||||||
const policyDest = useContainer ? `${remoteHost}:~/dev/.gemini/policies/offload-policy.toml` : `${remoteHost}:${remoteHome}/.gemini/policies/offload-policy.toml`;
|
spawnSync(`${rsyncBase} .gemini/skills/offload/policy.toml ${remoteHost}:~/.gemini/policies/offload-policy.toml`, { shell: true });
|
||||||
spawnSync(`${rsyncBase} .gemini/skills/offload/policy.toml ${policyDest}`, { shell: true });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Sync Auth (Gemini)
|
// 3. Sync Auth (Gemini)
|
||||||
@@ -155,26 +129,21 @@ Host ${sshAlias}
|
|||||||
const homeDir = env.HOME || '';
|
const homeDir = env.HOME || '';
|
||||||
const lp = path.join(homeDir, '.gemini/google_accounts.json');
|
const lp = path.join(homeDir, '.gemini/google_accounts.json');
|
||||||
if (fs.existsSync(lp)) {
|
if (fs.existsSync(lp)) {
|
||||||
console.log(` - Syncing .gemini/google_accounts.json...`);
|
spawnSync(`${rsyncBase} ${lp} ${remoteHost}:~/.gemini/google_accounts.json`, { shell: true });
|
||||||
const authDest = useContainer ? `${remoteHost}:~/dev/.gemini/google_accounts.json` : `${remoteHost}:${remoteHome}/.gemini/google_accounts.json`;
|
|
||||||
spawnSync(`${rsyncBase} ${lp} ${authDest}`, { shell: true });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Scoped Token Onboarding
|
// 4. Scoped Token Onboarding
|
||||||
if (await confirm('Generate a scoped, secure token for the autonomous agent? (Recommended)')) {
|
if (await confirm('Generate a scoped, secure token for the autonomous agent? (Recommended)')) {
|
||||||
const magicLink = `https://github.com/settings/tokens/beta/new?description=Offload-${env.USER}&repositories[]=${encodeURIComponent(upstreamRepo)}&repositories[]=${encodeURIComponent(userFork)}&permissions[contents]=write&permissions[pull_requests]=write&permissions[metadata]=read`;
|
const magicLink = `https://github.com/settings/tokens/beta/new?description=Offload-${env.USER}&repositories[]=${encodeURIComponent(upstreamRepo)}&repositories[]=${encodeURIComponent(userFork)}&permissions[contents]=write&permissions[pull_requests]=write&permissions[metadata]=read`;
|
||||||
console.log(`\n🔐 SECURITY: Open this Magic Link to create a token:\n\x1b[34m${magicLink}\x1b[0m`);
|
console.log(`\n🔐 SECURITY: Create a token here:\n\x1b[34m${magicLink}\x1b[0m`);
|
||||||
const scopedToken = await prompt('\nPaste Scoped Token', '');
|
const scopedToken = await prompt('\nPaste Scoped Token', '');
|
||||||
if (scopedToken) {
|
if (scopedToken) {
|
||||||
const tokenCmd = useContainer
|
spawnSync(`ssh ${remoteHost} "mkdir -p ~/.offload && echo ${scopedToken} > ~/.offload/.gh_token && chmod 600 ~/.offload/.gh_token"`, { shell: true });
|
||||||
? `docker exec gemini-sandbox sh -c "mkdir -p ~/.offload && echo ${scopedToken} > ~/.offload/.gh_token && chmod 600 ~/.offload/.gh_token"`
|
|
||||||
: `mkdir -p ~/.offload && echo ${scopedToken} > ~/.offload/.gh_token && chmod 600 ~/.offload/.gh_token`;
|
|
||||||
spawnSync(`ssh ${remoteHost} ${JSON.stringify(tokenCmd)}`, { shell: true });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Global Tooling & Clone
|
// 5. Tooling & Clone
|
||||||
if (await confirm('Initialize tools and clone repository?')) {
|
if (await confirm('Initialize tools and clone repository?')) {
|
||||||
if (!useContainer) {
|
if (!useContainer) {
|
||||||
spawnSync(`ssh ${remoteHost} "sudo npm install -g tsx vitest"`, { shell: true, stdio: 'inherit' });
|
spawnSync(`ssh ${remoteHost} "sudo npm install -g tsx vitest"`, { shell: true, stdio: 'inherit' });
|
||||||
@@ -182,10 +151,7 @@ Host ${sshAlias}
|
|||||||
|
|
||||||
console.log(`🚀 Cloning fork ${userFork} on worker...`);
|
console.log(`🚀 Cloning fork ${userFork} on worker...`);
|
||||||
const repoUrl = `https://github.com/${userFork}.git`;
|
const repoUrl = `https://github.com/${userFork}.git`;
|
||||||
const cloneCmd = useContainer
|
const cloneCmd = `[ -d ${remoteWorkDir}/.git ] || (git clone --filter=blob:none ${repoUrl} ${remoteWorkDir} && cd ${remoteWorkDir} && git remote add upstream https://github.com/${upstreamRepo}.git && git fetch upstream)`;
|
||||||
? `docker exec gemini-sandbox sh -c "[ -d ${remoteWorkDir}/.git ] || (git clone --filter=blob:none ${repoUrl} ${remoteWorkDir} && cd ${remoteWorkDir} && git remote add upstream https://github.com/${upstreamRepo}.git && git fetch upstream)"`
|
|
||||||
: `[ -d ${remoteWorkDir}/.git ] || (git clone --filter=blob:none ${repoUrl} ${remoteWorkDir} && cd ${remoteWorkDir} && git remote add upstream https://github.com/${upstreamRepo}.git && git fetch upstream)`;
|
|
||||||
|
|
||||||
spawnSync(`ssh ${remoteHost} ${JSON.stringify(cloneCmd)}`, { shell: true, stdio: 'inherit' });
|
spawnSync(`ssh ${remoteHost} ${JSON.stringify(cloneCmd)}`, { shell: true, stdio: 'inherit' });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +163,8 @@ Host ${sshAlias}
|
|||||||
}
|
}
|
||||||
settings.maintainer = settings.maintainer || {};
|
settings.maintainer = settings.maintainer || {};
|
||||||
settings.maintainer.deepReview = {
|
settings.maintainer.deepReview = {
|
||||||
projectId, zone, remoteHost, remoteHome, remoteWorkDir, userFork, upstreamRepo,
|
projectId, zone, remoteHost,
|
||||||
|
remoteWorkDir, userFork, upstreamRepo,
|
||||||
useContainer,
|
useContainer,
|
||||||
terminalType: 'iterm2'
|
terminalType: 'iterm2'
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user