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-p2a): inject remote hook env on remote tmux + thread hookEndpointPath

enes528889bd857a
diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.ts
index 0319315d8..80130bb7b 100644
--- a/src/main/pty-manager.ts
+++ b/src/main/pty-manager.ts
@@ -14,6 +14,7 @@ import {
} from '../shared/types'
import { hookServer } from './agents/hook-server'
import {
+ remoteHookEnvArgs,
remoteTmuxHasSessionArgs,
remoteTmuxKillArgs,
remoteTmuxPtyArgs,
@@ -339,9 +340,14 @@ export class PtyManager {
// Agent hooks: each session carries the hook-server coordinates + its node/agent id.
// Our managed hook (installed globally in each agent's config, but a no-op without these
// vars) then posts state back to us for any agent run in this session.
- const hookEnv = options.persistKey
- ? hookServer.buildPtyEnv(options.persistKey, options.agentId ?? 'claude')
- : {}
+ // A REMOTE (ssh-project) session must NOT get the LOCAL hook env: it points at
+ // 127.0.0.1:<localPort>, which is useless (and misleading) on the remote host. The remote
+ // session's hook env is injected via the remote tmux `-e` below (from the reverse-tunnel
+ // endpoint file), so leave the local hook env out entirely here.
+ const hookEnv =
+ options.persistKey && !options.sshRemote
+ ? hookServer.buildPtyEnv(options.persistKey, options.agentId ?? 'claude')
+ : {}
for (const [k, v] of Object.entries(hookEnv)) env[k] = v
const settings = this.getSettings()
@@ -361,6 +367,16 @@ export class PtyManager {
const remoteSsh = options.sshRemote && options.persistKey ? findSsh() : null
if (options.sshRemote && options.persistKey && remoteSsh) {
file = remoteSsh
+ // When the project's reverse tunnel + remote endpoint file are set up (Task 2), inject the
+ // remote hook env into the remote tmux session so the installed hook script POSTs state back
+ // over the unix-socket tunnel. Fail-open: no hookEndpointPath → no hook env (Phase-1 status).
+ const hookExtraEnv = options.sshRemote.hookEndpointPath
+ ? remoteHookEnvArgs(
+ options.sshRemote.hookEndpointPath,
+ sessionName(options.persistKey),
+ hookServer.getVersion()
+ )
+ : []
args = remoteTmuxPtyArgs(
options.sshRemote.conn,
options.sshRemote.controlPath,
@@ -368,7 +384,8 @@ export class PtyManager {
options.sshRemote.remoteCwd,
// An agent preset may pass a remote program to run inside the remote tmux; usually undefined.
options.shell,
- options.shellArgs
+ options.shellArgs,
+ hookExtraEnv
)
} else if (this.tmuxPath && settings.tmuxEnabled && options.persistKey) {
// attach-or-create the persistent session for this node.
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 7d756ae8b..30fba4040 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -506,7 +506,9 @@ export function Canvas() {
if (project.ssh) {
window.nodeTerminal.sshProject
.connect(project.id, project.ssh.server)
- .then(({ controlPath }) => useSshConn.getState().setControlPath(project.id, controlPath))
+ .then(({ controlPath, hookEndpointPath }) =>
+ useSshConn.getState().setConn(project.id, { controlPath, hookEndpointPath })
+ )
.catch(() => {
/* status surfaced via onStatus → the connection banner */
})
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 3477cf4ae..439e6d28c 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -48,7 +48,10 @@ function escapeDroppedPath(p: string): string {
async function resolveSshRemote(
conn: SshConnection,
cwd: string | undefined
-): Promise<{ controlPath: string; conn: SshConnection; remoteCwd: string } | undefined> {
+): Promise<
+ | { controlPath: string; conn: SshConnection; remoteCwd: string; hookEndpointPath?: string }
+ | undefined
+> {
const projectId = useProjects.getState().activeProjectId
let controlPath = useSshConn.getState().getControlPath(projectId)
if (!controlPath) {
@@ -62,14 +65,17 @@ async function resolveSshRemote(
resolve(v)
}
const unsub = useSshConn.subscribe((s) => {
- const v = s.byProject[projectId]
+ const v = s.byProject[projectId]?.controlPath
if (v) finish(v)
})
const timer = setTimeout(() => finish(useSshConn.getState().getControlPath(projectId)), 20000)
})
}
if (!controlPath) return undefined
- return { controlPath, conn, remoteCwd: cwd || '~' }
+ // The remote hook endpoint (reverse tunnel + remote install) is set up alongside the master;
+ // pass it through so the remote tmux session carries the hook env. Optional (fail-open).
+ const hookEndpointPath = useSshConn.getState().getHookEndpointPath(projectId)
+ return { controlPath, conn, remoteCwd: cwd || '~', hookEndpointPath }
}
/**
diff --git a/src/renderer/state/sshConn.ts b/src/renderer/state/sshConn.ts
index 87d320dec..e62c8822d 100644
--- a/src/renderer/state/sshConn.ts
+++ b/src/renderer/state/sshConn.ts
@@ -1,25 +1,39 @@
import { create } from 'zustand'
+/** Live connection coordinates for one SSH project, returned by `sshProject.connect`. */
+export interface SshConnInfo {
+ /** ControlMaster socket path the project's terminals run their PTYs over. */
+ controlPath: string
+ /** Remote endpoint file the managed hook script sources (reverse-tunnel hook transport).
+ * Optional: absent when forwarding/remote install failed (fail-open → Phase-1 status). */
+ hookEndpointPath?: string
+}
+
/**
- * Transient map of SSH project id → the live ControlMaster `controlPath` returned by
- * `sshProject.connect`. Not persisted: it's re-established on every launch by Canvas's
- * active-project effect. A remote terminal node reads its project's controlPath here to pass
- * `sshRemote` into `transport.create`, so the PTY runs over the project's master.
+ * Transient map of SSH project id → its live connection info (ControlMaster `controlPath` +
+ * optional remote `hookEndpointPath`) returned by `sshProject.connect`. Not persisted: it's
+ * re-established on every launch by Canvas's active-project effect. A remote terminal node reads
+ * its project's entry here to pass `sshRemote` into `transport.create`, so the PTY runs over the
+ * project's master and (when present) the remote hook env is injected.
*/
interface SshConnState {
- byProject: Record<string, string>
- setControlPath(projectId: string, controlPath: string): void
+ byProject: Record<string, SshConnInfo>
+ setConn(projectId: string, info: SshConnInfo): void
getControlPath(projectId: string): string | undefined
+ getHookEndpointPath(projectId: string): string | undefined
clear(projectId: string): void
}
export const useSshConn = create<SshConnState>((set, get) => ({
byProject: {},
- setControlPath(projectId, controlPath) {
- set((s) => ({ byProject: { ...s.byProject, [projectId]: controlPath } }))
+ setConn(projectId, info) {
+ set((s) => ({ byProject: { ...s.byProject, [projectId]: info } }))
},
getControlPath(projectId) {
- return get().byProject[projectId]
+ return get().byProject[projectId]?.controlPath
+ },
+ getHookEndpointPath(projectId) {
+ return get().byProject[projectId]?.hookEndpointPath
},
clear(projectId) {
set((s) => {
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 226e461b6..926b9d64c 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -315,7 +315,10 @@ export type SshProjectStatus = 'connecting' | 'connected' | 'disconnected' | 're
export interface SshProjectApi {
/** Open (or reuse) the ControlMaster for an SSH project; resolves once connected. */
- connect(projectId: string, server: import('./ssh').SshConnection): Promise<{ controlPath: string }>
+ connect(
+ projectId: string,
+ server: import('./ssh').SshConnection
+ ): Promise<{ controlPath: string; hookEndpointPath?: string }>
/** Tear down the master (remote tmux is unaffected). */
disconnect(projectId: string): Promise<void>
/**