Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

feat(main): provision/tear down per-agent worktree on spawn/kill

Chaitanya Giri3f1fce116bbf
diff --git a/src/main/index.ts b/src/main/index.ts
index b8771c77..e1e24487 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -9,7 +9,8 @@ import {
} from './config';
import { listDir, readFileText, writeFileText } from './fs';
import {
- getBranch, getStatus, getLog, getBranches, getAheadBehind, isRepo
+ getBranch, getStatus, getLog, getBranches, getAheadBehind, isRepo,
+ addWorktree, removeWorktree
} from './git';
import { HiveManager, type AgentMeta, type HiveMessage } from './hive';
import { HookServer } from './hooks';
@@ -32,6 +33,13 @@ let mainWindow: BrowserWindow | null = null;
/** When true, skip the quit interceptor (user already confirmed). */
let allowQuit = false;
+/** Agents spawned with `isolate: true` get a dedicated git worktree; this maps
+ * the agent/pty id → the worktree path so we can tear it down on kill. */
+const worktreePaths = new Map<string, string>();
+/** id → the original repo cwd the worktree was created from (needed to run
+ * `git worktree remove` from the parent tree, not the worktree itself). */
+const worktreeOrigins = new Map<string, string>();
+
/** The live renderer webContents, or null if the window is gone/destroyed.
* Anything that emits to the renderer from a timer/socket/child callback must
* route through here — during quit the window can be destroyed while those
@@ -95,10 +103,32 @@ function createWindow(): void {
}
// ─── IPC: pty lifecycle ─────────────────────────────────────────────────────
-ipcMain.handle('pty:spawn', (_evt, opts: SpawnOptions & { hive?: AgentMeta }) => {
+ipcMain.handle('pty:spawn', async (_evt, opts: SpawnOptions & { hive?: AgentMeta; isolate?: boolean }) => {
if (!opts || typeof opts.id !== 'string' || typeof opts.cwd !== 'string' || typeof opts.command !== 'string') {
return { ok: false, error: 'invalid SpawnOptions' };
}
+ // Git isolation: when requested and the cwd is a real repo, give this agent
+ // its own worktree on an `agent/<id>` branch so it can't clobber other agents'
+ // (or the user's) working tree. Best-effort — a failure falls back to the
+ // shared cwd rather than blocking the spawn.
+ if (opts.isolate === true && await isRepo(opts.cwd)) {
+ try {
+ const origCwd = opts.cwd;
+ const wtPath = join(readConfig().harnessHome ?? origCwd, 'worktrees', opts.hive?.id ?? opts.id);
+ const br = await getBranch(origCwd);
+ const baseBranch = 'current' in br && br.current ? br.current : 'main';
+ const wt = await addWorktree(origCwd, wtPath, baseBranch);
+ if (wt.ok) {
+ opts.cwd = wtPath;
+ worktreePaths.set(opts.id, wtPath);
+ worktreeOrigins.set(opts.id, origCwd);
+ } else {
+ console.error('[worktree] addWorktree failed:', wt.error);
+ }
+ } catch (e) {
+ console.error('[worktree] isolation failed:', e);
+ }
+ }
// If the agent carries hive metadata, provision its workspace and inject the
// identity + protocol (extra --append-system-prompt args + AGENT_* env).
if (opts.hive && hive.enabled()) {
@@ -124,7 +154,20 @@ ipcMain.handle('pty:resize', (_evt, id: string, cols: number, rows: number) => {
});
ipcMain.handle('pty:kill', (_evt, id: string) => {
if (typeof id !== 'string') return { ok: false, error: 'invalid id' };
- return ptyManager.kill(id);
+ const res = ptyManager.kill(id);
+ // Tear down the agent's isolated worktree, if any. Best-effort and non-blocking:
+ // we don't await it so the kill result returns immediately, and removal errors
+ // (e.g. uncommitted work) are logged rather than surfaced to the renderer.
+ const wtPath = worktreePaths.get(id);
+ if (wtPath) {
+ const origCwd = worktreeOrigins.get(id) ?? wtPath;
+ worktreePaths.delete(id);
+ worktreeOrigins.delete(id);
+ void removeWorktree(origCwd, wtPath)
+ .then(r => { if (!r.ok) console.error('[worktree] removeWorktree failed:', r.error); })
+ .catch(e => console.error('[worktree] removeWorktree threw:', e));
+ }
+ return res;
});
ipcMain.handle('pty:list', () => ptyManager.list());