v2.0.1: ACP dispatch + PTY fallback + worktree management
Phase 5 of v2.0. External agent dispatch via SSH to host. ACP dispatch (acp-dispatch.ts): spawns agent via SSH with JSON-RPC stdio pipe. Wraps opencode/goose in ACP mode. Captures structured events (file operations, tool calls) mapped to parts taxonomy. Falls back to PTY if ACP handshake fails. PTY dispatch (pty-dispatch.ts): raw SSH spawn for agents without ACP support (claude, pi). Captures stdout/stderr as plain text. Simpler but less structured than ACP. SSH helper (ssh.ts): shared spawn wrapper for SSH commands to samkintop@100.114.205.53 (Tailscale IP, same as booterm). Uses openssh-client installed in the runtime Dockerfile stage. Worktree management (worktrees.ts): createWorktree (git worktree add via SSH), diffWorktree (git diff HEAD...task-branch), cleanupWorktree (git worktree remove --force). One worktree per task at /tmp/booworktrees/<taskId>. Dispatcher updated: checks available_agents.supports_acp to pick transport. Path B flow: create worktree → dispatch agent → diff worktree → queue diff into pending_changes → cleanup worktree → mark task complete. Agent probe updated: probes via SSH to find host-installed agents (which opencode && opencode --version over SSH). Dockerfile: openssh-client added to runtime stage. Config: SSH_HOST env var (default 100.114.205.53). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
139
apps/coder/src/services/pty-dispatch.ts
Normal file
139
apps/coder/src/services/pty-dispatch.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* PTY dispatch — runs external agents on the host via SSH.
|
||||
*
|
||||
* For agents without ACP support (claude, pi), we pipe the task into their
|
||||
* non-interactive mode and capture stdout/stderr. The agent runs in a git
|
||||
* worktree so it can modify files freely.
|
||||
*
|
||||
* Supported agents:
|
||||
* - claude: `claude -p --model <model>` (print mode, reads task from stdin)
|
||||
* - opencode: `echo <task> | opencode` (stdin pipe — exact flags TBD)
|
||||
* - goose: stub (not yet supported)
|
||||
* - pi: stub (not yet supported)
|
||||
*/
|
||||
import type { FastifyBaseLogger } from 'fastify';
|
||||
import { sshSpawnWithStdin } from './ssh.js';
|
||||
|
||||
export interface DispatchResult {
|
||||
exitCode: number;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
export interface PtyDispatchOpts {
|
||||
agent: string;
|
||||
task: string;
|
||||
worktreePath: string;
|
||||
model?: string;
|
||||
signal?: AbortSignal;
|
||||
log: FastifyBaseLogger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the shell command that runs the agent non-interactively.
|
||||
* The command will be executed inside `cd <worktreePath> && ...`.
|
||||
*/
|
||||
function buildAgentCommand(agent: string, task: string, model?: string): string | null {
|
||||
// Escape the task for embedding in a shell command
|
||||
const escapedTask = task.replace(/'/g, "'\\''");
|
||||
|
||||
switch (agent) {
|
||||
case 'claude':
|
||||
// Claude Code's print mode: reads prompt from stdin, runs autonomously, prints result
|
||||
return model
|
||||
? `echo '${escapedTask}' | claude -p --model '${model}'`
|
||||
: `echo '${escapedTask}' | claude -p`;
|
||||
|
||||
case 'opencode':
|
||||
// opencode non-interactive: pipe task via stdin
|
||||
// NOTE: exact flags may vary — opencode may need --non-interactive or --pipe
|
||||
return model
|
||||
? `echo '${escapedTask}' | opencode --model '${model}'`
|
||||
: `echo '${escapedTask}' | opencode`;
|
||||
|
||||
case 'goose':
|
||||
// Not yet verified for non-interactive use
|
||||
return null;
|
||||
|
||||
case 'pi':
|
||||
// Not yet verified for non-interactive use
|
||||
return null;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a task to an external agent via SSH.
|
||||
*
|
||||
* The agent runs in the worktree directory on the host. stdout/stderr are
|
||||
* captured in full and returned. The SSH process is killed on abort signal.
|
||||
*/
|
||||
export async function dispatchViaPty(opts: PtyDispatchOpts): Promise<DispatchResult> {
|
||||
const { agent, task, worktreePath, model, signal, log } = opts;
|
||||
|
||||
const agentCmd = buildAgentCommand(agent, task, model);
|
||||
if (!agentCmd) {
|
||||
return {
|
||||
exitCode: 1,
|
||||
stdout: '',
|
||||
stderr: `Agent '${agent}' is not yet supported for PTY dispatch.`,
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap in cd to the worktree
|
||||
const fullCommand = `cd '${worktreePath.replace(/'/g, "'\\''")}' && ${agentCmd}`;
|
||||
|
||||
log.info({ agent, worktreePath }, 'pty-dispatch: starting');
|
||||
|
||||
return new Promise<DispatchResult>((resolve, reject) => {
|
||||
const child = sshSpawnWithStdin(fullCommand, '');
|
||||
// Note: sshSpawnWithStdin already closes stdin. For agents that read from
|
||||
// stdin via echo piping, the command itself handles the piping on the remote
|
||||
// side. We just need the SSH tunnel.
|
||||
|
||||
// Actually, re-think: sshSpawnWithStdin writes input and closes stdin on the
|
||||
// LOCAL ssh process. But the remote command is `echo '...' | agent`, which
|
||||
// provides its own stdin. So we should use sshSpawn (no local stdin needed)
|
||||
// or just let the empty stdin close — the remote shell handles piping internally.
|
||||
// This is fine as-is because the echo piping happens WITHIN the remote shell command.
|
||||
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
let killed = false;
|
||||
|
||||
child.stdout!.on('data', (chunk: Buffer) => { stdout += chunk.toString(); });
|
||||
child.stderr!.on('data', (chunk: Buffer) => { stderr += chunk.toString(); });
|
||||
|
||||
const cleanup = () => {
|
||||
if (!killed) {
|
||||
killed = true;
|
||||
child.kill('SIGTERM');
|
||||
// Give it a moment then force-kill
|
||||
setTimeout(() => child.kill('SIGKILL'), 5_000);
|
||||
}
|
||||
};
|
||||
|
||||
if (signal) {
|
||||
if (signal.aborted) {
|
||||
cleanup();
|
||||
resolve({ exitCode: 130, stdout: '', stderr: 'Aborted before start' });
|
||||
return;
|
||||
}
|
||||
signal.addEventListener('abort', cleanup, { once: true });
|
||||
}
|
||||
|
||||
child.on('close', (code) => {
|
||||
if (signal) signal.removeEventListener('abort', cleanup);
|
||||
log.info({ agent, exitCode: code }, 'pty-dispatch: completed');
|
||||
resolve({ exitCode: code ?? 1, stdout, stderr });
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
if (signal) signal.removeEventListener('abort', cleanup);
|
||||
log.error({ agent, err: err.message }, 'pty-dispatch: spawn error');
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user