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(ide): main-process git:diff IPC + preload bridge; add Monaco deps

Chaitanya Giri7fc34876a539
diff --git a/package.json b/package.json
index 8f66617d..bc428d0b 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"@codemirror/state": "^6.6.0",
"@codemirror/view": "^6.43.0",
"@lezer/highlight": "^1.2.3",
+ "@monaco-editor/react": "^4.7.0",
"@openai/agents-realtime": "^0.11.8",
"@uiw/react-codemirror": "^4.25.10",
"@xterm/addon-fit": "^0.10.0",
@@ -45,6 +46,7 @@
"better-sqlite3": "^11.10.0",
"commit-graph": "^2.4.0",
"localtunnel": "^2.0.2",
+ "monaco-editor": "^0.52.2",
"node-pty": "^1.0.0",
"pixi.js": "^8.5.1",
"react": "^18.3.1",
diff --git a/src/main/fs.ts b/src/main/fs.ts
index a465c44a..bd837b13 100644
--- a/src/main/fs.ts
+++ b/src/main/fs.ts
@@ -4,8 +4,12 @@ import { isAbsolute, join, normalize, relative, resolve } from 'node:path';
/**
* Confines `path` inside `root` to prevent path-traversal escapes.
* Returns the resolved absolute path on success, or null on violation.
+ *
+ * Exported so other main-process modules (e.g. git.ts) validate caller-supplied
+ * relative paths against a workspace root with the SAME guard — there is exactly
+ * one path-escape policy in the app and it lives here.
*/
-function safeJoin(root: string, rel: string): string | null {
+export function safeJoin(root: string, rel: string): string | null {
const absRoot = resolve(root);
const absPath = isAbsolute(rel) ? normalize(rel) : resolve(absRoot, rel);
const rel2 = relative(absRoot, absPath);
diff --git a/src/main/git.ts b/src/main/git.ts
index 6a3d5a3d..ec182d6c 100644
--- a/src/main/git.ts
+++ b/src/main/git.ts
@@ -1,4 +1,6 @@
import { spawn } from 'node:child_process';
+import { readFile, stat } from 'node:fs/promises';
+import { safeJoin } from './fs';
/** Run git in `cwd` with `args`. Returns stdout text or an error. */
function runGit(cwd: string, args: string[], timeoutMs = 8000): Promise<{
@@ -142,6 +144,71 @@ export async function isRepo(cwd: string): Promise<boolean> {
return res.ok && res.stdout.trim() === 'true';
}
+const MAX_DIFF_BYTES = 2 * 1024 * 1024; // 2 MB — keep the diff view responsive
+
+/** A file's two sides for a working-tree-vs-HEAD diff. `head` is the committed
+ * version ('' for a new/untracked file); `working` is the on-disk version ('' for
+ * a file deleted from the working tree). The renderer feeds these straight into
+ * Monaco's DiffEditor (original = head, modified = working). */
+export interface GitDiff {
+ ok: true;
+ path: string; // resolved absolute path (display only)
+ relPath: string;
+ head: string;
+ working: string;
+ headExists: boolean; // file is tracked at HEAD
+ workingExists: boolean; // file is present in the working tree
+ isBinary: boolean; // either side is binary → not text-diffable
+}
+
+/**
+ * Diff a single file: its committed (HEAD) content vs its current working-tree
+ * content. `relPath` MUST be repo-root-relative and is path-validated against
+ * `cwd` with the shared fs guard (no escaping the workspace). All git/fs access
+ * stays in the main process — the renderer only ever receives the two text sides.
+ */
+export async function getDiff(
+ cwd: string, relPath: string
+): Promise<GitDiff | { ok: false; error: string }> {
+ const abs = safeJoin(cwd, relPath);
+ if (!abs) return { ok: false, error: 'path escapes repository root' };
+
+ // HEAD side: `git show HEAD:<path>` — errors (untracked / new file) → no head version.
+ let head = '';
+ let headExists = false;
+ const show = await runGit(cwd, ['show', `HEAD:${relPath}`]);
+ if (show.ok) { head = show.stdout; headExists = true; }
+
+ // Working side: read the on-disk file. ENOENT → deleted from the working tree.
+ let working = '';
+ let workingExists = false;
+ let workingBinary = false;
+ try {
+ const s = await stat(abs);
+ if (s.size > MAX_DIFF_BYTES) {
+ return { ok: false, error: `file too large to diff (${(s.size / 1048576).toFixed(1)} MB)` };
+ }
+ const buf = await readFile(abs);
+ workingExists = true;
+ if (buf.includes(0)) workingBinary = true;
+ else working = buf.toString('utf8');
+ } catch {
+ workingExists = false;
+ }
+
+ const isBinary = workingBinary || head.includes('\0');
+ return {
+ ok: true,
+ path: abs,
+ relPath,
+ head: isBinary ? '' : head,
+ working: isBinary ? '' : working,
+ headExists,
+ workingExists,
+ isBinary
+ };
+}
+
/** Derive a safe `agent/<id>` branch name from a worktree path's basename. */
function agentBranchFor(wtPath: string): string {
const base = wtPath.split(/[\\/]/).filter(Boolean).pop() ?? 'agent';
diff --git a/src/main/index.ts b/src/main/index.ts
index 31271bb0..d33bf5e0 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -11,7 +11,7 @@ import {
} from './config';
import { listDir, readFileText, writeFileText } from './fs';
import {
- getBranch, getStatus, getLog, getBranches, getAheadBehind, isRepo,
+ getBranch, getStatus, getLog, getBranches, getAheadBehind, isRepo, getDiff,
addWorktree, removeWorktree, worktreeHasUnintegratedWork, worktreeIsGcSafe
} from './git';
import { HiveManager, type AgentMeta, type HiveMessage, type HiveTask } from './hive';
@@ -2323,6 +2323,12 @@ ipcMain.handle('git:aheadBehind', (_evt, cwd: unknown) => {
if (typeof cwd !== 'string') return { error: 'invalid cwd' };
return getAheadBehind(cwd);
});
+ipcMain.handle('git:diff', (_evt, cwd: unknown, relPath: unknown) => {
+ if (typeof cwd !== 'string' || typeof relPath !== 'string') {
+ return { ok: false, error: 'invalid args' };
+ }
+ return getDiff(cwd, relPath);
+});
// ─── IPC: hive (multi-agent coordination) ───────────────────────────────────
ipcMain.handle('hive:registry', () => hive.registry());
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 58fc2501..a6b4735f 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -353,6 +353,17 @@ export interface GitCommit {
}
export interface GitStatusEntry { path: string; index: string; worktree: string }
export interface GitStatus { staged: GitStatusEntry[]; unstaged: GitStatusEntry[]; untracked: string[] }
+/** A single file's two sides for a working-tree-vs-HEAD diff (see main git.getDiff). */
+export interface GitDiff {
+ ok: true;
+ path: string;
+ relPath: string;
+ head: string;
+ working: string;
+ headExists: boolean;
+ workingExists: boolean;
+ isBinary: boolean;
+}
/** Real token usage + estimated USD cost summed from an agent's Claude Code
* transcripts under ~/.claude/projects. Reconciler/fallback path — now priced
@@ -582,6 +593,11 @@ const api = {
ipcRenderer.invoke('git:branches', cwd) as Promise<{ local: string[]; remote: string[]; current: string | null } | { error: string }>,
gitAheadBehind: (cwd: string) =>
ipcRenderer.invoke('git:aheadBehind', cwd) as Promise<{ ahead: number; behind: number; upstream: string | null } | { error: string }>,
+ /** Diff one repo-root-relative file: its HEAD content vs its working-tree content.
+ * Path-validated main-side against `cwd`; the renderer only ever gets the two
+ * text sides. Backs the IDE's git-diff (Monaco DiffEditor) view. */
+ gitDiff: (cwd: string, relPath: string) =>
+ ipcRenderer.invoke('git:diff', cwd, relPath) as Promise<GitDiff | { ok: false; error: string }>,
// ─── Hive (multi-agent coordination) ─────────────────────────────────────
hiveRegistry: (): Promise<HiveRegistry> => ipcRenderer.invoke('hive:registry'),