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.

fix(ssh-p4): scope git routing to the active project + panel re-fetch on connect + findSsh login-shell probe (final-review)

enes8f2dda89fc39
diff --git a/src/main/index.ts b/src/main/index.ts
index bb1e3f04c..f8abddaa2 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -37,7 +37,7 @@ import { initLicense } from './license'
import { initRemoteHost } from './remote/host-service'
import { initRemoteClient } from './remote/client-service'
import { initSshProject } from './remote-ssh/ssh-project'
-import { setGitRemoteResolver } from './remote-ssh/remote-git'
+import { setGitRemoteResolver, type GitRemoteRef } from './remote-ssh/remote-git'
import { SshFs } from './ssh-fs'
// Dev-only: NT_MULTI lets a SECOND instance run (host + client testing on one machine) with an
@@ -67,6 +67,12 @@ const gitService = new GitService()
// Set once the app window is ready; used by the quit hooks to tear down SSH-project masters.
let sshProjectManager: ReturnType<typeof initSshProject> | undefined
+// Remote git routing is scoped to the ACTIVE project only (set via `git:set-active-remote`).
+// A global cwd-keyed match would misroute when two SSH projects share a remote path, or when a
+// LOCAL project's cwd equals a connected SSH project's remoteCwd. The renderer drives this on every
+// project switch: the active SSH project's ref, or null for a local project (→ all git runs local).
+let activeRemote: { cwd: string; ref: GitRemoteRef } | null = null
+
// The single app window — kept at module scope so IPC handlers (e.g. notifications)
// can check focus and route clicks back to the renderer.
let mainWin: BrowserWindow | null = null
@@ -534,9 +540,19 @@ app.whenReady().then(async () => {
initRemoteHost(win, ptyManager)
initRemoteClient(win, { isPackaged: app.isPackaged })
sshProjectManager = initSshProject(win)
- // Route git-service + commit-message git ops over the project's master when the cwd belongs to a
- // connected SSH project; returns undefined for local cwds so the local path is byte-identical.
- setGitRemoteResolver((cwd) => sshProjectManager?.refForRemoteCwd(cwd))
+ // Route git-service + commit-message git ops over the active SSH project's master only — and only
+ // for that project's exact remoteCwd. Any other cwd (a local project, or a different connected
+ // project) resolves to undefined, so the local path stays byte-identical.
+ setGitRemoteResolver((cwd) => (activeRemote && activeRemote.cwd === cwd ? activeRemote.ref : undefined))
+ // The renderer's active-project effect calls this on every switch: a non-null projectId of a
+ // connected SSH project (whose ref carries a remoteCwd) arms remote routing; null/local disarms it.
+ ipcMain.handle(IPC.gitSetActiveRemote, (_e, projectId: string | null) => {
+ const ref = projectId ? sshProjectManager?.refForProject(projectId) : undefined
+ activeRemote =
+ ref && ref.remoteCwd
+ ? { cwd: ref.remoteCwd, ref: { conn: ref.conn, controlPath: ref.controlPath } }
+ : null
+ })
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
diff --git a/src/main/remote-ssh/remote-git.ts b/src/main/remote-ssh/remote-git.ts
index 7d3dfa1b5..0be570470 100644
--- a/src/main/remote-ssh/remote-git.ts
+++ b/src/main/remote-ssh/remote-git.ts
@@ -23,6 +23,21 @@ export function remoteGitArgs(conn: SshConnection, controlPath: string, cwd: str
let sshPath: string | null | undefined
function findSsh(): string | null {
if (sshPath !== undefined) return sshPath
+ // GUI apps don't inherit the shell PATH, so probe a login shell first (mirrors
+ // commit-message.ts). Kept self-contained — importing ssh-project.ts would pull in electron and
+ // break this module's pure vitest tests.
+ try {
+ const out = execFileSync('/usr/bin/env', ['sh', '-lc', 'command -v ssh'], {
+ encoding: 'utf-8'
+ }).trim()
+ if (out) {
+ execFileSync(out, ['-V'], { stdio: 'ignore' })
+ sshPath = out
+ return out
+ }
+ } catch {
+ /* fall back to the hardcoded paths */
+ }
for (const p of ['/usr/bin/ssh', '/usr/local/bin/ssh', '/opt/homebrew/bin/ssh']) {
try {
execFileSync(p, ['-V'], { stdio: 'ignore' })
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
index 8c5c52620..088986c1c 100644
--- a/src/main/remote-ssh/ssh-project.ts
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -93,7 +93,8 @@ export class SshProjectManager {
const { code } = await this.r.run(checkMasterArgs(existing.conn, existing.controlPath))
if (code === 0) {
// Keep the remote git cwd current even on an idempotent reuse (the folder may have changed).
- existing.remoteCwd = remoteCwd
+ // Guard against a later connect without remoteCwd clearing a known cwd.
+ existing.remoteCwd = remoteCwd ?? existing.remoteCwd
return { controlPath: existing.controlPath, hookEndpointPath: existing.hookEndpointPath }
}
this.r.onStatus({ projectId, status: 'reconnecting' })
@@ -193,9 +194,11 @@ export class SshProjectManager {
* SshFs ops take). Returns `undefined` when the project isn't connected, so the `sshFs:*` IPC
* handlers can fail open (empty result) rather than throw.
*/
- refForProject(projectId: string): { conn: SshConnection; controlPath: string } | undefined {
+ refForProject(
+ projectId: string
+ ): { conn: SshConnection; controlPath: string; remoteCwd?: string } | undefined {
const c = this.conns.get(projectId)
- return c ? { conn: c.conn, controlPath: c.controlPath } : undefined
+ return c ? { conn: c.conn, controlPath: c.controlPath, remoteCwd: c.remoteCwd } : undefined
}
/**
diff --git a/src/preload/index.ts b/src/preload/index.ts
index ceff5766d..ebc1d0310 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -147,7 +147,8 @@ const api: NodeTerminalApi = {
worktreeMerge: (repoPath, branch, baseRef) =>
ipcRenderer.invoke(IPC.gitWorktreeMerge, repoPath, branch, baseRef),
worktreeRemove: (repoPath, wtPath, deleteBranch) =>
- ipcRenderer.invoke(IPC.gitWorktreeRemove, repoPath, wtPath, deleteBranch)
+ ipcRenderer.invoke(IPC.gitWorktreeRemove, repoPath, wtPath, deleteBranch),
+ setActiveRemote: (projectId) => ipcRenderer.invoke(IPC.gitSetActiveRemote, projectId)
},
clipboard: {
writeText: (text: string) => clipboard.writeText(text)
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index a11fd9c43..13fcb23e6 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -506,12 +506,18 @@ export function Canvas() {
if (project.ssh) {
window.nodeTerminal.sshProject
.connect(project.id, project.ssh.server, project.ssh.remoteCwd)
- .then(({ controlPath, hookEndpointPath }) =>
+ .then(async ({ controlPath, hookEndpointPath }) => {
+ // Arm remote git routing for the active project BEFORE the sshConn entry appears, so the
+ // Source Control panel's re-fetch (which keys off that entry) already hits the master.
+ await window.nodeTerminal.git.setActiveRemote(project.id)
useSshConn.getState().setConn(project.id, { controlPath, hookEndpointPath })
- )
+ })
.catch(() => {
/* status surfaced via onStatus → the connection banner */
})
+ } else {
+ // Local active project: ensure all git ops run local (no stale remote from a prior SSH tab).
+ void window.nodeTerminal.git.setActiveRemote(null)
}
loadingRef.current = true
const flow = nodeStatesToFlow(project.nodes)
diff --git a/src/renderer/components/SourceControlPanel.tsx b/src/renderer/components/SourceControlPanel.tsx
index 12ffa5e8b..5cde02ea9 100644
--- a/src/renderer/components/SourceControlPanel.tsx
+++ b/src/renderer/components/SourceControlPanel.tsx
@@ -3,6 +3,7 @@ import { createPortal } from 'react-dom'
import type { GitFileChange, GitResult, GitStatus } from '@shared/types'
import type { GitHistoryItem, GitHistoryResult } from '@shared/git-history'
import { useProjects } from '../state/projects'
+import { useSshConn } from '../state/sshConn'
import { useScmDraft } from '../state/scmDraft'
import { GitHistoryPanel } from './git-history/GitHistoryPanel'
import { buildCommitMenuItems } from './git-history/git-history-menu'
@@ -49,6 +50,10 @@ export function SourceControlPanel({
// Local projects are byte-identical (the SSH branch fires only when `project.ssh` is set).
const isSsh = !!project?.ssh
const cwd = project?.ssh?.remoteCwd ?? project?.cwd
+ // For an SSH project the master may still be connecting when the panel mounts; its controlPath
+ // appears once `setConn` runs (after `setActiveRemote` arms remote routing). Observing it lets the
+ // refresh re-run when the master connects. Local projects have no entry → undefined → no effect.
+ const sshControlPath = useSshConn((s) => (project?.id ? s.byProject[project.id]?.controlPath : undefined))
const [status, setStatus] = useState<GitStatus | null>(null)
// Commit message + AI-generate state live in a per-repo store (keyed by cwd), so closing the
// panel mid-generation neither discards the message nor abandons the run — reopening shows it.
@@ -83,7 +88,10 @@ export function SourceControlPanel({
const refresh = useCallback(async () => {
setStatus(cwd ? await git.status(cwd) : null)
- }, [cwd, git])
+ // `sshControlPath` is a dep so an SSH project whose master finishes connecting after the panel
+ // opened re-fetches once the connection is live (instead of staying "no repo"/empty).
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [cwd, git, sshControlPath])
const refreshHistory = useCallback(async () => {
if (!cwd) {
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index d2f4b5a4d..702ba2522 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -110,6 +110,7 @@ export const IPC = {
gitWorktreeAdd: 'git:worktree-add',
gitWorktreeMerge: 'git:worktree-merge',
gitWorktreeRemove: 'git:worktree-remove',
+ gitSetActiveRemote: 'git:set-active-remote',
shellOpenExternal: 'shell:open-external',
commitGenerate: 'commit:generate',
remoteHostStart: 'remote:host:start',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 348beacb5..4159def06 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -455,6 +455,9 @@ export interface GitApi {
worktreeAdd(repoPath: string, wtPath: string, branch: string, baseRef: string, isNew: boolean): Promise<GitResult>
worktreeMerge(repoPath: string, branch: string, baseRef: string): Promise<GitResult>
worktreeRemove(repoPath: string, wtPath: string, deleteBranch: boolean): Promise<GitResult>
+ /** Scope remote git routing to the active project: pass its id to route git over that SSH
+ * project's master, or null for a local project so all git ops run locally. */
+ setActiveRemote(projectId: string | null): Promise<void>
}
export interface UpdateInfo {