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-p3): ssh-fs ops over the master (list/read/readBinary/write, fail-open)

enes7a21f0a2013e
diff --git a/src/main/ssh-fs.test.ts b/src/main/ssh-fs.test.ts
new file mode 100644
index 000000000..d02feda4a
--- /dev/null
+++ b/src/main/ssh-fs.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, it, vi } from 'vitest'
+import { sshListArgs, sshReadArgs, sshReadBinaryArgs, sshWriteArgs, parseLsEntries, SshFs } from './ssh-fs'
+
+const conn = { host: 'h', user: 'u' }
+const ref = { conn, controlPath: '/s.sock' }
+
+describe('ssh-fs arg builders', () => {
+ it('list runs ls -Ap1 on the quoted path', () => {
+ expect(sshListArgs(conn, '/s.sock', '/a b/c').join(' ')).toContain(`ls -Ap1 '/a b/c'`)
+ })
+ it('read cats the quoted path', () => {
+ expect(sshReadArgs(conn, '/s.sock', "/x'y").join(' ')).toContain(`cat '/x'\\''y'`)
+ })
+ it('readBinary base64s the quoted path', () => {
+ expect(sshReadBinaryArgs(conn, '/s.sock', '/i.png').join(' ')).toContain(`base64 '/i.png'`)
+ })
+ it('write mkdir -p the dirname then cat > the quoted path (content via stdin, not interpolated)', () => {
+ const j = sshWriteArgs(conn, '/s.sock', '/d/e/f.txt').join(' ')
+ expect(j).toContain(`mkdir -p '/d/e'`)
+ expect(j).toContain(`cat > '/d/e/f.txt'`)
+ })
+})
+
+describe('parseLsEntries', () => {
+ it('folders-first alphabetical, .git hidden, trailing-slash → dir', () => {
+ expect(parseLsEntries('zeta.txt\nsrc/\n.git/\nalpha/\nb.md\n')).toEqual([
+ { name: 'alpha', dir: true, ignored: false },
+ { name: 'src', dir: true, ignored: false },
+ { name: 'b.md', dir: false, ignored: false },
+ { name: 'zeta.txt', dir: false, ignored: false }
+ ])
+ })
+})
+
+describe('SshFs (injected runner)', () => {
+ it('readText returns stdout, empty on failure', async () => {
+ expect(await new SshFs(async () => ({ code: 0, stdout: 'hi' })).readText(ref, '/x')).toBe('hi')
+ expect(await new SshFs(async () => ({ code: 1, stdout: '' })).readText(ref, '/x')).toBe('')
+ })
+ it('writeText feeds content on stdin and returns true on code 0 / false otherwise', async () => {
+ const run = vi.fn(async (_args: string[], _stdin?: string) => ({ code: 0, stdout: '' }))
+ expect(await new SshFs(run).writeText(ref, '/d/f.txt', 'BODY')).toBe(true)
+ expect(run.mock.calls[0][1]).toBe('BODY') // stdin
+ expect(await new SshFs(async () => ({ code: 1, stdout: '' })).writeText(ref, '/x', 'b')).toBe(false)
+ })
+ it('listDir parses ls output and flags ignored from check-ignore', async () => {
+ const run = vi.fn(async (args: string[]) =>
+ args.join(' ').includes('check-ignore') ? { code: 0, stdout: 'node_modules\n' } : { code: 0, stdout: 'node_modules/\nsrc/\n' }
+ )
+ const out = await new SshFs(run).listDir(ref, '/p')
+ expect(out).toEqual([
+ { name: 'node_modules', dir: true, ignored: true },
+ { name: 'src', dir: true, ignored: false }
+ ])
+ })
+ it('fail-open: listDir → [] when the ls run fails', async () => {
+ expect(await new SshFs(async () => ({ code: 1, stdout: '' })).listDir(ref, '/p')).toEqual([])
+ })
+})
diff --git a/src/main/ssh-fs.ts b/src/main/ssh-fs.ts
new file mode 100644
index 000000000..d5917c154
--- /dev/null
+++ b/src/main/ssh-fs.ts
@@ -0,0 +1,96 @@
+// SSH filesystem ops over a project's ControlMaster. The remote analog of fs-ops.ts: same
+// fail-open contract ([]/''/false on any error), reused by the renderer's sshFs(projectId) FsApi.
+// Every path is posixQuote'd; write content goes on stdin (never interpolated).
+import { childArgs } from './remote-ssh/control-master'
+import { posixQuote, type SshConnection } from '../shared/ssh'
+import type { DirEntry } from '../shared/types'
+
+export interface SshFsRef {
+ conn: SshConnection
+ controlPath: string
+}
+type Runner = (args: string[], stdin?: string) => Promise<{ code: number; stdout: string }>
+
+function dirname(p: string): string {
+ const i = p.replace(/\/+$/, '').lastIndexOf('/')
+ return i <= 0 ? '/' : p.slice(0, i)
+}
+
+export function sshListArgs(conn: SshConnection, cp: string, path: string): string[] {
+ return childArgs(conn, cp, `ls -Ap1 ${posixQuote(path)}`)
+}
+export function sshReadArgs(conn: SshConnection, cp: string, path: string): string[] {
+ return childArgs(conn, cp, `cat ${posixQuote(path)}`)
+}
+export function sshReadBinaryArgs(conn: SshConnection, cp: string, path: string): string[] {
+ return childArgs(conn, cp, `base64 ${posixQuote(path)}`)
+}
+export function sshWriteArgs(conn: SshConnection, cp: string, path: string): string[] {
+ return childArgs(conn, cp, `mkdir -p ${posixQuote(dirname(path))} && cat > ${posixQuote(path)}`)
+}
+export function sshCheckIgnoreArgs(conn: SshConnection, cp: string, dir: string, names: string[]): string[] {
+ return childArgs(conn, cp, `git -C ${posixQuote(dir)} check-ignore -- ${names.map(posixQuote).join(' ')}`)
+}
+
+/** Parse `ls -Ap1` output → DirEntry[]: trailing-slash = dir, hide .git, folders-first alpha. */
+export function parseLsEntries(stdout: string): DirEntry[] {
+ const entries: DirEntry[] = stdout
+ .split('\n')
+ .map((l) => l.replace(/\r$/, ''))
+ .filter((l) => l && l !== './' && l !== '../')
+ .map((l) => (l.endsWith('/') ? { name: l.slice(0, -1), dir: true, ignored: false } : { name: l, dir: false, ignored: false }))
+ .filter((e) => e.name !== '.git')
+ entries.sort((a, b) => (a.dir === b.dir ? a.name.localeCompare(b.name) : a.dir ? -1 : 1))
+ return entries
+}
+
+export class SshFs {
+ constructor(private run: Runner) {}
+
+ async listDir(ref: SshFsRef, path: string): Promise<DirEntry[]> {
+ try {
+ const { code, stdout } = await this.run(sshListArgs(ref.conn, ref.controlPath, path))
+ if (code !== 0) return []
+ const entries = parseLsEntries(stdout)
+ if (entries.length) {
+ try {
+ const ci = await this.run(sshCheckIgnoreArgs(ref.conn, ref.controlPath, path, entries.map((e) => e.name)))
+ const set = new Set(ci.stdout.split('\n').map((s) => s.trim().replace(/\/$/, '')).filter(Boolean))
+ for (const e of entries) if (set.has(e.name)) e.ignored = true
+ } catch {
+ /* check-ignore best-effort */
+ }
+ }
+ return entries
+ } catch {
+ return []
+ }
+ }
+
+ async readText(ref: SshFsRef, path: string): Promise<string> {
+ try {
+ const { code, stdout } = await this.run(sshReadArgs(ref.conn, ref.controlPath, path))
+ return code === 0 ? stdout : ''
+ } catch {
+ return ''
+ }
+ }
+
+ async readBinary(ref: SshFsRef, path: string): Promise<string> {
+ try {
+ const { code, stdout } = await this.run(sshReadBinaryArgs(ref.conn, ref.controlPath, path))
+ return code === 0 ? stdout.replace(/\s+/g, '') : ''
+ } catch {
+ return ''
+ }
+ }
+
+ async writeText(ref: SshFsRef, path: string, content: string): Promise<boolean> {
+ try {
+ const { code } = await this.run(sshWriteArgs(ref.conn, ref.controlPath, path), content)
+ return code === 0
+ } catch {
+ return false
+ }
+ }
+}