Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(ssh-p4): remote-git builder + runner + resolver registry + refForRemoteCwd

enes7f68fb7ff91a
diff --git a/src/main/remote-ssh/remote-git.test.ts b/src/main/remote-ssh/remote-git.test.ts
new file mode 100644
index 000000000..8a5f1f2eb
--- /dev/null
+++ b/src/main/remote-ssh/remote-git.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest'
+import { remoteGitArgs, setGitRemoteResolver, resolveGitRemote } from './remote-git'
+
+const conn = { host: 'h', user: 'u' }
+
+describe('remoteGitArgs', () => {
+ it('composes `cd <tilde-cwd> && git <quoted args>` as a remote child command', () => {
+ const j = remoteGitArgs(conn, '/s.sock', '~/proj', ['commit', '-m', 'hi there']).join(' ')
+ expect(j).toContain(`cd ~/'proj' && git 'commit' '-m' 'hi there'`)
+ })
+ it('quotes args with shell metacharacters', () => {
+ const j = remoteGitArgs(conn, '/s.sock', '/r', ['log', '--format=%H;rm -rf /']).join(' ')
+ expect(j).toContain(`git 'log' '--format=%H;rm -rf /'`)
+ })
+})
+
+describe('git remote resolver registry', () => {
+ it('resolves via the set fn, undefined when unset/null', () => {
+ setGitRemoteResolver(null)
+ expect(resolveGitRemote('/r')).toBeUndefined()
+ setGitRemoteResolver((cwd) => (cwd === '/r' ? { conn, controlPath: '/s' } : undefined))
+ expect(resolveGitRemote('/r')).toEqual({ conn, controlPath: '/s' })
+ expect(resolveGitRemote('/other')).toBeUndefined()
+ setGitRemoteResolver(null)
+ })
+})
diff --git a/src/main/remote-ssh/remote-git.ts b/src/main/remote-ssh/remote-git.ts
new file mode 100644
index 000000000..7d3dfa1b5
--- /dev/null
+++ b/src/main/remote-ssh/remote-git.ts
@@ -0,0 +1,63 @@
+// Run git over an SSH project's ControlMaster. The pure builder composes `cd <cwd> && git <args>`
+// (cwd tilde-expands via quoteRemotePath; every arg posixQuote'd so a ref/branch/message can't
+// inject into the remote shell). runRemoteGit returns the SAME { ok, out, err } shape as the local
+// git() helper so callers are transport-agnostic. A module-level resolver registry lets both
+// git-service.ts and commit-message.ts route the same way without threading a ref through every op.
+import { execFile, execFileSync } from 'child_process'
+import { promisify } from 'util'
+import { childArgs } from './control-master'
+import { posixQuote, quoteRemotePath, type SshConnection } from '../../shared/ssh'
+
+const run = promisify(execFile)
+
+export interface GitRemoteRef {
+ conn: SshConnection
+ controlPath: string
+}
+
+export function remoteGitArgs(conn: SshConnection, controlPath: string, cwd: string, args: string[]): string[] {
+ const remote = `cd ${quoteRemotePath(cwd)} && git ${args.map(posixQuote).join(' ')}`
+ return childArgs(conn, controlPath, remote)
+}
+
+let sshPath: string | null | undefined
+function findSsh(): string | null {
+ if (sshPath !== undefined) return sshPath
+ for (const p of ['/usr/bin/ssh', '/usr/local/bin/ssh', '/opt/homebrew/bin/ssh']) {
+ try {
+ execFileSync(p, ['-V'], { stdio: 'ignore' })
+ sshPath = p
+ return p
+ } catch {
+ /* try next */
+ }
+ }
+ sshPath = null
+ return null
+}
+
+/** Run a git command on the remote over the master. Returns the same shape as the local git() helper. */
+export async function runRemoteGit(
+ ref: GitRemoteRef,
+ cwd: string,
+ args: string[],
+ maxBuffer: number
+): Promise<{ ok: boolean; out: string; err: string }> {
+ const ssh = findSsh()
+ if (!ssh) return { ok: false, out: '', err: 'ssh not found' }
+ try {
+ const { stdout } = await run(ssh, remoteGitArgs(ref.conn, ref.controlPath, cwd, args), { maxBuffer })
+ return { ok: true, out: stdout.replace(/\n$/, ''), err: '' }
+ } catch (e) {
+ const err = e as { stdout?: string; stderr?: string; message?: string }
+ return { ok: false, out: (err.stdout ?? '').trim(), err: (err.stderr || err.message || '').trim() }
+ }
+}
+
+let resolver: ((cwd: string) => GitRemoteRef | undefined) | null = null
+export function setGitRemoteResolver(fn: ((cwd: string) => GitRemoteRef | undefined) | null): void {
+ resolver = fn
+}
+export function resolveGitRemote(cwd: string): GitRemoteRef | undefined {
+ return resolver ? resolver(cwd) : undefined
+}
diff --git a/src/main/remote-ssh/ssh-project.test.ts b/src/main/remote-ssh/ssh-project.test.ts
index 9c4b3d996..56e9543d3 100644
--- a/src/main/remote-ssh/ssh-project.test.ts
+++ b/src/main/remote-ssh/ssh-project.test.ts
@@ -48,4 +48,11 @@ describe('SshProjectManager', () => {
expect(mgr.refForProject('p1')).toEqual({ conn, controlPath: controlPathFor('p1') })
expect(mgr.refForProject('nope')).toBeUndefined()
})
+
+ it('refForRemoteCwd resolves {conn, controlPath} by the connected project remote cwd', async () => {
+ const { mgr } = makeMgr()
+ await mgr.connect('p1', conn, '/srv/repo')
+ expect(mgr.refForRemoteCwd('/srv/repo')).toEqual({ conn, controlPath: controlPathFor('p1') })
+ expect(mgr.refForRemoteCwd('/nope')).toBeUndefined()
+ })
})
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
index 8ff6741e2..df7217a32 100644
--- a/src/main/remote-ssh/ssh-project.ts
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -38,6 +38,9 @@ interface Conn {
/** The remote `$HOME`, resolved at connect. Used (Phase 2b) to jail remote transcript reads
* under `<remoteHome>/.claude/projects`. Undefined if it couldn't be resolved (fail-open). */
remoteHome?: string
+ /** The project's remote repo cwd (Phase 4). Lets `refForRemoteCwd` route remote git ops to this
+ * connection's master. Undefined when the project has no folder selected. */
+ remoteCwd?: string
}
/**
@@ -79,7 +82,8 @@ export class SshProjectManager {
async connect(
projectId: string,
- conn: SshConnection
+ conn: SshConnection,
+ remoteCwd?: string
): Promise<{ controlPath: string; hookEndpointPath?: string }> {
const existing = this.conns.get(projectId)
if (existing) {
@@ -87,7 +91,11 @@ export class SshProjectManager {
// would otherwise leave us reusing a dead socket. If `-O check` fails, surface
// `reconnecting`, drop the stale entry, and fall through to re-establish.
const { code } = await this.r.run(checkMasterArgs(existing.conn, existing.controlPath))
- if (code === 0) return { controlPath: existing.controlPath, hookEndpointPath: existing.hookEndpointPath }
+ if (code === 0) {
+ // Keep the remote git cwd current even on an idempotent reuse (the folder may have changed).
+ existing.remoteCwd = remoteCwd
+ return { controlPath: existing.controlPath, hookEndpointPath: existing.hookEndpointPath }
+ }
this.r.onStatus({ projectId, status: 'reconnecting' })
existing.master.kill()
this.conns.delete(projectId)
@@ -102,7 +110,7 @@ export class SshProjectManager {
}
this.r.onStatus({ projectId, status: 'connecting' })
const master = this.r.spawnMaster(masterArgs(conn, controlPath))
- this.conns.set(projectId, { conn, controlPath, master })
+ this.conns.set(projectId, { conn, controlPath, master, remoteCwd })
// Wait until the master answers `-O check`, retrying briefly.
for (let i = 0; i < 50; i++) {
const { code } = await this.r.run(checkMasterArgs(conn, controlPath))
@@ -190,6 +198,18 @@ export class SshProjectManager {
return c ? { conn: c.conn, controlPath: c.controlPath } : undefined
}
+ /**
+ * Resolve the `{ conn, controlPath }` ref for the connected project whose remote repo cwd matches
+ * `cwd` (Phase 4). Backs the git-remote resolver registry so remote git ops route to the right
+ * master by working directory. Returns `undefined` when no connected project owns that cwd.
+ */
+ refForRemoteCwd(cwd: string): { conn: SshConnection; controlPath: string } | undefined {
+ for (const c of this.conns.values()) {
+ if (c.remoteCwd && c.remoteCwd === cwd) return { conn: c.conn, controlPath: c.controlPath }
+ }
+ return undefined
+ }
+
/** The resolved remote `$HOME` for a connected project, if known. */
remoteHomeFor(projectId: string): string | undefined {
return this.conns.get(projectId)?.remoteHome