Skip to content

feat(projects): SSH targets for project computer tasks - #1388

Merged
zhaozhaozz merged 4 commits into
agent-CI-Infra:mainfrom
zhaozhaozz:feat/issue-1379-remote-ssh
Nov 25, 2026
Merged

zhaozhaozz merged 4 commits into
agent-CI-Infra:mainfrom
zhaozhaozz:feat/issue-1379-remote-ssh

Conversation

@zhaozhaozz

@zhaozhaozz zhaozhaozz commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1379

Summary

Tasks on a project computer can use native ssh, scp, sftp and rsync against the SSH hosts registered for their project. Private keys stay sealed in the control plane, which signs per request; passwords are released only to OpenSSH's askpass helper for password targets; tunnel targets reach only their registered host:port through the control plane.

The branch has four commits:

  1. feat(projects): register SSH targets and protect stored command output: ssh_targets / ssh_target_projects, sealed credentials, console card, configuredRedactor() with exact (no minimum length) SSH secrets, rsync in the project-computer image.
  2. fix(console): clarify SSH inventory availability.
  3. feat(projects): verify registered SSH credentials from the console: a super admin's connectivity test (fingerprint check, one authentication, no session).
  4. feat(projects): let task commands use registered SSH targets: task-side SSH, described below.

How task-side SSH works

  • Grant. When the control plane starts processes for a run it attaches a one-time SSH grant: Pi start (workspace_agent_config last chunk / workspace_agent_start), Claude Code / Codex start (workspace_harness_start), and the project test command (src/tasks/request.ts before a PR, src/agent/engine.ts after an integration and before creating a PR, src/tasks/acceptance.ts in the QA gate). Usable targets are decided once at the start: the role is in allowed_roles, and the checkout was trusted (HarnessAccess.trustedCheckout; taskCheckoutContext for test commands) or the target has allow_untrusted_pr. Every hop of a proxy_jump chain must be usable too. The grant is never written to task operations, the manager ledger, trajectories or operation hashes; the manager's HTTP routing (manager/server.mjs) and a compute node's driver (manager/compute-driver.mjs) accept it only beside an execute; the manager forwards it only over the daemon socket and drops it on the one-shot docker exec path. src/tasks/pull-request.ts only hashes the policy; the PR-time test command runs in engine.ts.
  • Config. The manager writes a root-owned ssh_config (0640) and the targets' public keys into the computer's runner directory, which the image includes from /etc/ssh/ssh_config.d/ (OpenSSH reads the passwd home, never $HOME). Each alias gets Match originalhost <alias> exec "ssh-gate check"; a refused alias falls through to a Host <alias> block whose ProxyCommand prints ssh_target_denied and exits non-zero before any connection. There is no Host *, so git@github.com keeps its defaults. Host keys are accepted only through KnownHostsCommand against the registered SHA-256 fingerprint. Once the daemon answers its health check the manager seals the runner directory to 0750, so no process in the computer can replace runner.sock, ssh-agent.sock or the config.
  • Caller identity. All tasks share UID 10001, so neither environment nor socket names prove a run; run_token is not put in the task environment. The daemon serves one /run/bot-runner/ssh-agent.sock (0660); connecting grants nothing. Each request reads the caller's pid via SO_PEERCRED (through python3, Node has no getsockopt) and accepts only process groups the daemon registered synchronously at spawn (a command's detached shell), or groups whose leader is a direct child of a registered agent process (Pi run_command and native bash), checking leader pid and /proc start time. A process that leaves its group with setsid, or any other run's process, is refused.
  • Signatures. SIGN_REQUEST names only a key: the agent lists only the group's allowed public keys, and the control plane re-checks the run (run token and lease for Pi/Harness, task claim for test commands), the target (still bound, same identity), the role and trust before signing. Every target must have its own key: a key registered for one target is refused for another (ssh_key_reused). ed25519, RSA (rsa-sha2-256/512) and ECDSA P-256/384/521 are supported; encrypted and PKCS#8 keys are refused at registration.
  • Passwords. SSH_ASKPASS=/opt/runner/ssh-askpass.mjs and SSH_ASKPASS_REQUIRE=force are set only when the run may use a password target. The daemon answers only if the helper's parent is the image's /usr/bin/ssh (same device and inode), both are in the authorized group, and the prompt's user@host matches exactly one password target of the run; otherwise the helper writes nothing to stdout. The password goes only to the helper's stdout (and, by OpenSSH's readpass.c pipe hand-off, briefly to that ssh); never to files, env, argv, logs, trajectories or audit. Output and trajectories pass through configuredRedactor() with no minimum length and across chunk boundaries.
  • Tunnels. The ProxyCommand carries only the target id; daemon and control plane ignore any address the caller adds and connect only to the registered host:port. No HTTP CONNECT, SOCKS or arbitrary forwarding. Each direction ends on its own (half-close), so the remote's last answer still reaches the caller; only an error, a timeout or a revoked grant cuts both sides. Limits: 16 per run, 32 per computer, 10 min idle, 2 h total. Opens and refusals are audited (ssh_target.*, target id only). relay_node is a reserved field for [分布式部署] 支持总控与多 Linux Docker 计算节点分离:ClusterManager、node-agent 与节点管理 #1380 node relays; such a tunnel is refused with ssh_relay_unavailable until nodes relay.
  • Revocation. After a run ends, is cancelled, or a target is revoked or changed, new signatures, passwords and tunnels fail; closing the grant closes its tunnels (a watcher closes them within 30 s if the run ends first). Established direct / proxy_jump TCP sessions are held by the container and are not promised to be cut; this is written in docs/design/.
  • Not in scope / limits. No Feishu threads ([飞书渠道] 支持同群多 Thread 并发与解除助手电脑 owner_busy 阻塞(独立低优) #1381). On a remote compute node ([分布式部署] 支持总控与多 Linux Docker 计算节点分离:ClusterManager、node-agent 与节点管理 #1380, now on main) the grant reaches the node's Projects through its driver and check/sign/password go through the node's relayed gateway.sock; that relay does not carry HTTP Upgrade yet, so tunnel targets answer ssh_broker_unavailable on remote nodes. Local execution (BOT_LOCAL_EXECUTION) has no resident daemon, so no task-side SSH. Claude Code / Codex commands that the CLI starts in new process groups below its own process are not authorized. Isolation between same-UID processes relies on the host's Yama ptrace_scope >= 1.

Docs: docs/design/agent-and-harness-runtime.md (replaces the "task-side SSH not wired" gap with the actual boundary, sealed runner directory) and docs/features/console-and-workspace.md.

Verification

Head 32a77034, based on origin/main b31098e7 (includes #1380).

  • npm run check: exit 0.
  • BOT_TEST_JOBS=4 npm test: exit 1, 351 test sets, 2839 tests, 2838 passed. The one failure is distributed-nodes (test/st/src/cluster/distributed-nodes.test.mjs, from [分布式部署] 支持总控与多 Linux Docker 计算节点分离:ClusterManager、node-agent 与节点管理 #1380, unchanged here): node_agent_connect_timeout in its first test while four sets ran in parallel. Run alone on the same head (node test/framework/test-sets.mjs --jobs 1 test/st/src/cluster/distributed-nodes.test.mjs) it passes in 10.7 s.
  • New tests (loopback only, no Docker in UT/ST):
    • test/st/src/projects/ssh-access.test.mjs: real OpenSSH client against a loopback ssh2 server through the control-plane grant service and the agent broker: key auth via agent signing, password via askpass, tunnel to the registered address (a decoy address passed by the caller gets no connection), host-key mismatch, ssh_target_denied for a QA-only alias, an unregistered process, setsid, a revoked role and an ended run, reserved relay refusal, audit without secrets; short password (3 chars) redacted from trace events.
    • test/ut/runner/ssh-agent.test.mjs: agent protocol framing, identities/signing only for the registered group, setsid and release, agent child-group inheritance (direct children only), askpass parent binary / prompt / ambiguity checks, tunnel per-run limit, config rendering and grant validation, SSH env inherited only with BOT_SSH_BROKER.
    • test/ut/manager/ssh-grant.test.mjs: executes go through the real manager/server.mjs HTTP routing and the compute-node driver; grant forwarded to a real daemon but absent from the manager ledger, trajectory files and runner journal; config and key files written 0640; agent start registered; invalid grants refused; no grant on the one-shot exec path.
    • test/ut/src/projects/ssh-signature.test.mjs: agent signature format for ed25519, RSA (3 flags) and ECDSA (3 curves), independently verified.
    • test/ut/manager/runner-transport.test.mjs: runner directory sealed to 0750 after readiness and again after a restart.
  • Real image check (local only, not committed): the current bot-project-computer image with this branch's runner files, run read-only, cap_drop ALL, no-new-privileges, UID 10001, OpenSSH 9.2p1, against the real control-plane grant service and a loopback SSH server: ssh with key, password and tunnel targets, scp -O, sftp and rsync all exit 0 with files delivered; a role-denied alias, an unregistered process and setsid ssh exit 255 with ssh_target_denied; ssh -G git@github.com is unaffected.
  • Not run: a real remote host or embedded board.

Changes after the first review head (eab56b44)

  • Rebased onto main with [分布式部署] 支持总控与多 Linux Docker 计算节点分离:ClusterManager、node-agent 与节点管理 #1380 (conflicts in src/server.ts, docker/manager.Dockerfile and the deployment image table only).
  • manager/server.mjs and manager/compute-driver.mjs rejected any top-level key besides operation_id, generation and request, so an execute carrying a grant would have failed with invalid_input in a real deployment; the manager test called Projects.operate directly and missed it. Both now accept ssh beside an execute only, and the test goes through the HTTP server and the node driver (it fails with invalid_input without the fix).
  • Tunnels half-close in the daemon and the control plane (CI exposed a lost last answer; the new delayed-echo tests fail on the previous code).
  • Test shell scripts get paths through environment variables (CodeQL "shell command built from environment values"); the end-to-end test no longer requires an empty ssh stderr, since newer OpenSSH warns about the test server's key exchange.
  • A socket path the kernel would truncate is refused by the agent instead of being bound elsewhere; tests keep socket paths short in deep worktree TMPDIRs.

Screenshots

Machine settings → SSH remote hosts card, full viewports at 1440 and 390 CSS px (fixture data on loopback). The card note now says tasks can use the aliases; the connectivity result sits on its own line under the row's buttons.

Super admin, after a connectivity check (1440 / 390, and the row on 390):

Super admin, 1440 px
Super admin, 390 px
Super admin connectivity result, 390 px

Super admin, new project host form (1440 / 390):

New host form, 1440 px
New host form, 390 px

Project admin, read-only list without connectivity or edit actions (1440 / 390):

Project admin, 1440 px
Project admin, 390 px

Comment thread test/ut/runner/ssh-agent.test.mjs Fixed
Tasks on a project computer can now run native ssh, scp, sftp and rsync
against the aliases registered for their project:

- The control plane issues a per-run SSH grant when it starts a Pi or
  Claude Code/Codex process and when it runs the project test command
  (before a pull request, after an integration and in the QA gate). The
  usable targets follow the run's role and whether its checkout was
  trusted at the start (allow_untrusted_pr, default off). The grant is
  never recorded, hashed or replayed.
- The manager forwards the grant only over the daemon socket, writes a
  root-owned ssh_config with one gated Match block per alias and seals
  the runner directory once the daemon is ready.
- The runner daemon serves one ssh-agent.sock. It identifies callers by
  SO_PEERCRED and serves only process groups it registered for a run
  (or direct child groups of a registered agent), checked by leader pid
  and start time; refused aliases fail with ssh_target_denied.
- Signatures, passwords (SSH_ASKPASS for password targets only, parent
  must be /usr/bin/ssh) and tunnels to the registered host:port are
  decided by the control plane on every call, so ending, cancelling or
  revoking a run refuses the next one. Tunnels are bounded per run and
  per computer and audited.
- Every target needs its own unencrypted key; relay_node is reserved
  for compute-node relays and refused until they exist.
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head eab56b44 (worktree .worktrees/review-pr-1388, diff against origin/main 6c0c361a). The design is coherent and the tests cover a lot, but there are blocking issues: a required check fails on this head, and I found one UI layout defect and one hot-path regression.

Blocking

1. Required check unit fails (unit shard 2/2)

Two of the new test sets fail in CI on this head:

  • test/ut/runner/ssh-agent.test.mjs:155 "tunnels carry bytes for the registered target id only": stdout was '\n direct:1\n', i.e. the first tunneled command produced no bytes.
  • test/st/src/projects/ssh-access.test.mjs:133: assert.equal(direct.stderr, '') got the host OpenSSH post-quantum KEX warning.

The suite has to be deterministic in CI, not only locally.

a) runner/ssh-gate.mjs:19-21 and :36-39: the KnownHostsCommand key line and the tunneled byte stream are written to process.stdout, then the process calls process.exit() (known-host) / process.exit() on socket close (tunnel). Node does not flush pending pipe writes on process.exit(); a dropped key line makes ssh fail with "Host key verification failed", and a dropped tail breaks ssh/scp/sftp/rsync connections. Flush before exiting (e.g. process.stdout.end(..., () => process.exit(...)), as ssh-askpass.mjs already does) or let the process end naturally.

b) The tunnel test (and the daemon path it mirrors) also relies on the process group being registered before the child's first request. SshAgentBroker.register() is called right after spawn() (runner/daemon.mjs:190-196, runner/ssh-agent.mjs), and the comment "Called synchronously from the spawn of a detached leader, before its first instruction can reach this socket" is not a guarantee: the child runs concurrently, so a very early request can be denied with ssh_target_denied. The CI symptom is consistent with this or with (a). Please make the test deterministic (register before the gate can run, e.g. start the child behind a ready signal or exercise the real daemon/executor path) and make the product-side race explicit.

c) test/st/src/projects/ssh-access.test.mjs:133: do not require empty stderr from the host's ssh; assert the absence of the denial/secrets and the expected stdout instead (or pin the ssh flags). The project image is OpenSSH 9.2, but this test runs on the runner's ssh.

2. UI: the bind/unbind button overflows the card at 390 and the viewport at 320

static/project/ssh-targets.js:102 uses 解除本项目绑定 / 绑定到本项目 in a 4-column fixed-layout table (projectTable([...4 columns])). Headless-rendering the module with the console stylesheets (390 and 320 CSS px) gives an action column of ~79 px (390) / ~61 px (320) and button widths of 105 px and 92 px. At 390 the button's right edge is ~10 px past the card border (inside the viewport, so document.documentElement.scrollWidth <= innerWidth does not catch it); at 320 it ends 11 px past the viewport and causes page-level horizontal scroll. If the screenshot fixture has no bound global host, this state is not covered by the 390 shots. Please shorten the labels (e.g. 解除绑定 / 绑定) or give the action column room / stack the controls, and re-shoot 1440 and 390 including a bound host.

3. configuredRedactor() cache thrash on the trace path

src/core/redaction.ts:12-24 keeps a single-entry cache keyed only by the revision, but the PR now calls it with a project (src/tasks/trace.ts:105,131) while the trajectory callback (src/storage/database.ts:100) and many other hot paths still call it without one. For a project that has SSH targets the two revisions differ, so every trace event and trajectory append misses the cache and re-runs the UNION query and re-opens (decrypts) every sealed credential row — exactly the reuse the cache comment promises. Key the cache per project (Map) or at least by project + revision, and confirm the cost on a representative task. This is also an easy place to keep short SSH passwords from being applied to unrelated text (see below).

Please fix or document

  • Reattached Pi sessions cannot get a grant. The grant is only carried by the call that starts a process (src/agent/pi.ts:337-341), and workspace_agent_start on a still-running session answers {started:true} without spawning (runner/agent.mjs:181), so onSpawn never runs and the daemon never registers the new grant (runner/daemon.mjs:190-196). The old registration holds the previous run's secret, which a restarted control plane does not know. Result: a run recovered from an interruption (roleBackendWorkspace native_resume) gets ssh_target_denied even though the design doc says the grant follows the run. There is also no transport for updating a live session's grant: workspace_agent_continue is not in SSH_AGENT_TYPES (runner/ssh-config.mjs:21), so a grant attached to it would be rejected by the manager (manager/projects.mjs:1610). Either re-arm the live agent's grant (accept ssh on continue / register the existing pid) or state this limit in docs/design/agent-and-harness-runtime.md and cover it with a test.
  • Legacy single-executor entries never get an SSH context. src/agent/loop.ts:97,203 builds the Pi session without ssh/runAccess, so those entries cannot use aliases at all. Wire them up or document the boundary.
  • Short SSH passwords are accepted with no minimum length and applied literally (including from traceEvent and commandResult); a 1–2 character password will redact ordinary text across the project. Consider a minimum password length at registration (or a minimum for the exact matcher) instead of accepting it and garbling output.

Verified

  • Base origin/main 6c0c361a, head eab56b44, linear commits, no stray files, no single-run evidence in the branch; new tables/files are in the schema, Docker COPY lists and the console module manifest; docs are updated in both dimensions; screenshots are 1440×900/390×844 and distinct.
  • bot-control-plane, bot-computer-manager, bot-project-computer, bot-cloudflare-tunnel, e2e and the code analyses pass; unit shard 2/2 does not (above). I did not enqueue this head.

@zhaozhaozz
zhaozhaozz force-pushed the feat/issue-1379-remote-ssh branch from eab56b4 to 32a7703 Compare November 25, 2026 12:03
@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Re-reviewed head 32a77034 (rebased on b31098e7, includes #1380). All required checks are green (unit both shards, e2e, the four images, CodeQL/Analyze), and the fixes for the manager boundary and the tunnel are correct:

  • manager/server.mjs / manager/compute-driver.mjs now accept ssh beside execute, and the test goes through the real HTTP routing and the node driver. The previous head would have rejected every grant with invalid_input in a deployment; this was the missing blocker.
  • The half-close handling in runner/ssh-agent.mjs and src/projects/ssh-access.ts keeps the last answer after the caller ends its side, and the delayed-echo tests cover it. The stderr assertion is no longer tied to the host OpenSSH version.

I have enqueued 32a77034. With the required checks green and the security/functional contract of the covered paths unchanged, the remaining findings are non-blocking follow-ups:

  1. src/core/redaction.ts:12-24: the single-entry cache still alternates for an SSH-enabled project (trace events pass project, the trajectory callback and other hot paths pass null), so those writes rebuild the redactor and re-open all sealed credentials. Key the cache by project + revision (e.g. a Map) when convenient.
  2. The console card at narrow widths: the fixed 4-column table gives the action cell ~57 px at 320 and ~74.5 px at 390, while 解除本项目绑定 is 105 px (static/project/ssh-targets.js:102). In a faithful render of .project-page > .project-content > .machine-settings the button reaches the page edge at 390 and adds ~6 px of page-level horizontal scroll at 320 for a bound global host (the current screenshots show no bound host in the pool group). Shortening the label or giving the action column more room/stacking would close this.
  3. docs/design/agent-and-harness-runtime.md “限制”: a still-running Pi session resumed after a control-plane restart is started with started: true and no spawn (runner/agent.mjs:181), so no new grant is registered, and the legacy single-executor path (src/agent/loop.ts) has no ssh context. Worth a line in the limits (or wiring) so the boundary is explicit.

The candidate looks good; the above can land as a small follow-up rather than another review round.

Merged via the queue into agent-CI-Infra:main with commit 3534893 Nov 25, 2026
12 checks passed
zhaozhaozz added a commit to zhaozhaozz/bot that referenced this pull request Nov 25, 2026
…view

- ssh-gate exits only after stdout and stderr are flushed, so the
  KnownHostsCommand key line and the last tunnelled bytes reach ssh.
- A start, reattach or continue of a running Pi or harness session
  carries the run's grant; the daemon moves an agent it started itself
  to the new grant (a pid read from state.json is ignored), so resumed
  and continued sessions keep task-side SSH.
- The legacy single-executor entry gets an SSH context bound to the
  task claim.
- configuredRedactor() caches per project instead of one entry that
  alternating callers rebuilt on every trace event.
- Passwords need at least four characters; they are still redacted
  literally below eight.
- The SSH host table stacks each host below 650 px and uses shorter
  bind labels, so its actions stay inside the card at 390 and 320 px.
- Tests regenerate the ed25519 keys ssh2 occasionally produces with a
  missing leading zero byte.
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

This PR merged at 32a77034 while I was addressing the review points and the follow-ups (#1388 (comment), #1388 (comment)). The remaining fixes are in #1389: ssh-gate output flushing, grants for reattached and continued sessions, the legacy entry, the per-project redactor cache, a password minimum, the narrow-screen layout and a test key generator flake.

zhaozhaozz added a commit to zhaozhaozz/bot that referenced this pull request Nov 25, 2026
…view

- ssh-gate exits only after stdout and stderr are flushed, so the
  KnownHostsCommand key line and the last tunnelled bytes reach ssh.
- A start, reattach or continue of a running Pi or harness session
  carries the run's grant; the daemon moves an agent it started itself
  to the new grant (a pid read from state.json is ignored), so resumed
  and continued sessions keep task-side SSH.
- The legacy single-executor entry gets an SSH context bound to the
  task claim.
- configuredRedactor() caches per project instead of one entry that
  alternating callers rebuilt on every trace event.
- Passwords need at least four characters; they are still redacted
  literally below eight.
- The SSH host table stacks each host below 650 px and uses shorter
  bind labels, so its actions stay inside the card at 390 and 320 px.
- Tests regenerate the ed25519 keys ssh2 occasionally produces with a
  missing leading zero byte.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[远程执行/SSH] 虚拟电脑内轻量 SSH 远程连接:受控 SSH_AUTH_SOCK 签名代理、网络通路与远端验证

2 participants