Skip to content

fix(projects): address SSH follow-ups from the #1388 review - #1389

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

zhaozhaozz merged 2 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

Refs #1379. Follow-up to #1388, which merged at 32a77034 while these review points (#1388 (comment), follow-ups in #1388 (comment)) were being fixed. This PR carries the remaining fixes on top of main 3534893c.

Review points from #1388

1. Test determinism and helper output

  • a) runner/ssh-gate.mjs now exits only after stdout and stderr have handed every byte to their pipes (write('', cb) before process.exit). The tunnel pipes the socket into stdout with { end: false } and exits after the socket closes, once stdout is flushed.
  • b) Registration order. SshAgentBroker.register() runs from onSpawn, synchronously in the same turn in which spawn() returns. libuv returns from spawn only after the child has run setsid and exec (it waits on the exec status pipe). This process accepts the child's connection, and reads its data, only after that turn returns to the event loop, so the child's first request always finds its registration. This is now stated in runner/daemon.mjs and in register(). The first runner test starts the agent client as the shell's very first command, without a delay. Its registration happens in the same test process, so the test does not depend on timing.
    The CI failure on eab56b44 was a real half-close bug, fixed in feat(projects): SSH targets for project computer tasks #1388. When the caller half-closed, the agent server, created with allowHalfOpen false, ended its own side. The daemon then destroyed both sides, so the remote's last answer was lost. The control-plane tunnel had the same pattern. Now both sides end each direction on its own and destroy only on error, timeout or revocation. The tests use delayed echo servers (UT: fake upstream; ST: a tunnel target through daemon and control plane). Both fail deterministically on the previous code: I put the previous runner/ssh-agent.mjs and src/projects/ssh-access.ts back and ran them.
  • c) (Merged in feat(projects): SSH targets for project computer tasks #1388.) The ST no longer requires empty stderr.
  • d) ssh2.utils.generateKeyPairSync('ed25519') occasionally drops a leading zero byte of the public key (about 1 in 250, reproduced 12 in 3000) and then cannot parse its own key. ssh-keygen keys are unaffected, but ssh-signature failed once locally for this reason. The tests now draw again until both halves parse.

2. Narrow layout. The labels are now 解除绑定 / 绑定. Action buttons wrap. Below 650 px each host is one block: alias and route on the first line, address on the second, actions on their own line. The screenshot script checks every view at 1440, 390 and 320 px: no button of the card ends past the card's edge, and scrollWidth == innerWidth. The fixture now has a bound and an unbound global host. Screenshots are below.

3. configuredRedactor() cache. The cache is now per project (Map keyed by project, '' for deployment-wide callers, at most 256 entries). Measured on the fixture: 4 SSH targets plus 2 other sealed rows, 2000 calls alternating with and without a project as the trace and trajectory paths do. Before: 119 µs per call. After: 45 µs per call. What remains is the revision query that existed before this PR. The gap grows with the number of sealed rows, because each miss re-opens every one of them.

Follow-ups

  • Reattached sessions. A call that starts, reattaches or continues a session now carries the run's grant: workspace_agent_start / last workspace_agent_config chunk, workspace_agent_continue (added to SSH_AGENT_TYPES), and workspace_harness_start for a live instance. When the process is already running, runner/agent.mjs hands its pid to the daemon (onAttach). SshAgentBroker.rearm() then moves it to the new grant, but only if the daemon registered that pid as an agent itself. A pid read from state.json, which tasks can write, is ignored. Tests: re-arming switches a live agent's child groups to the new targets; an unregistered process cannot be re-armed; start and continue of a running session attach instead of spawning.
  • Legacy single-executor entries. src/agent/loop.ts now gets an SSH context too, role dev. Its liveness follows the task claim (taskSsh, shared with the test-command grants), because these entries have no run token.
  • Short passwords. Registration requires at least 4 characters (invalid_ssh_credential, detail password_too_short). Redaction stays literal with no 8-character minimum, as [远程执行/SSH] 虚拟电脑内轻量 SSH 远程连接:受控 SSH_AUTH_SOCK 签名代理、网络通路与远端验证 #1379 asks; 4 characters avoid wiping ordinary text.

Verification on 8ebaae45

  • npm run check: exit 0.
  • BOT_TEST_JOBS=4 npm test: exit 1, 351 test sets, 2841 tests, 2840 passed. The one failure is codex-harness "Codex model reroute stops the run within the grace period even when the interrupt is ignored" (test/st/src/harness/codex-harness.test.mjs:395, a timing assertion under 4-way parallel load). That test and the Codex harness code are unchanged against main. Run alone on the same head (node test/framework/test-sets.mjs --jobs 1 test/st/src/harness/codex-harness.test.mjs) it passes in 309 s.
  • The SSH sets (ssh-agent, ssh-access, ssh-targets, ssh-grant, ssh-signature) pass, run through test/framework/test-sets.mjs. The rearm test, the attach test and the password minimum fail on main without this change.

Screenshots (1440 / 390 / 320)

Super admin, project hosts after a connectivity check:

1440
390
320

Super admin, global hosts with a bound (解除绑定) and an unbound (绑定) host:

1440
390
320

Super admin, new host form:

1440
390
320

Project admin, read-only list:

1440
390
320

…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.

@agent-ci-infra-bot agent-ci-infra-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit: 8ebaae4
Coverage: PR #1389; 19 changed file(s)

Comment thread runner/ssh-agent.mjs Resolved
@agent-ci-infra-bot

agent-ci-infra-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

第 2 次检视,更新于 2026-11-25 13:17 UTC,检视提交 5b0bf4c

Review of 5b0bf4cf (round 2; round 1 was on 8ebaae45)

F1 — fixed, verified. The new commit pins the re-arm to the task directory: register() now stores the workspace_id of the starting call, and rearm() refuses unless the call bringing the new grant names the same workspace (runner/ssh-agent.mjs:190-208, call site runner/daemon.mjs:202).

  • The comparison value cannot be forged by a task: both sides come from the control plane (Workspace.call/deliver add workspace_id: task.workspace_id, src/agent/workspace.ts:76,168), and workspace_id is also the directory the daemon spawns the agent in (runner/agent.mjs:239 dir = root + '/agent'), so the pin is exactly "the agent directory the daemon itself created".
  • The !workspace guard closes the null === null case, and non-agent pids are still refused.
  • I checked the regression coverage on both trees rather than taking the claim: exported 8ebaae45 with git archive and copied the two updated test files over it. On 5b0bf4cf both pass (10/10 in test/ut/runner/ssh-agent.test.mjs, 2/2 in test/ut/manager/ssh-grant.test.mjs); on 8ebaae45 the re-arm test fails (not ok 9) and the daemon-path test fails on the grant assertion. The new test/ut/manager/ssh-grant.test.mjs block is the valuable one: it drives the real daemon and manager, with task B's state.json naming task A's live agent.
  • docs/design/agent-and-harness-runtime.md now describes the same rule, including why the pid cannot be trusted.

Round-1 items (files unchanged in this commit, findings stand): stdout/stderr flush before exit in runner/ssh-gate.mjs — fixed; spawn/registration ordering — answered; registration-order test no longer delay-based — fixed; per-project configuredRedactor() cache — fixed; four-character password minimum plus console minLength and doc text — fixed; narrow card layout (labels 绑定/解除绑定, host-per-block below 650 px) — fixed, no horizontal overflow at 320/390 px; reattached sessions and the legacy src/agent/loop.ts entry — fixed.

New finding: F2 (suggestion, non-blocking) — the daemon drops rearm()'s boolean, so a refused attach still answers {started:true} / {written:true} and the run continues on the old grant. No security impact (a stale grant's secret is re-authorized against a run token/lease that is normally gone), but the F1 refusal becomes invisible and the run silently loses SSH for its remaining turns. Details inline.

Checks: no check runs on 5b0bf4cf yet (the push is a few minutes old), so merge evidence for this head is still the pending CI run; the round-1 command (8ebaae45) is fully green but does not certify this commit. Locally on 5b0bf4cf: node --check clean on the four touched .mjs files and the two updated suites, and the ssh-agent / ssh-grant suites pass as described above. I still cannot run npm run check or the full npm test here (node_modules is empty).

F1's inline thread is resolved. Nothing else in the 20-file diff is unaddressed; F2 is a diagnostic gap, not a blocker.

已关闭 1 条后续提交已修复的行内意见:runner/ssh-agent.mjs:203。

Reviewed commit: 5b0bf4c
Coverage: PR #1389; 20 changed file(s)

@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head 8ebaae45 (base origin/main 3534893c, one linear commit, no merge commits). All required checks are green on this head (unit both shards, e2e, the four images, CodeQL/Analyze). I enqueued 8ebaae45.

What I verified

  • Every follow-up from the feat(projects): SSH targets for project computer tasks #1388 review is addressed: ssh-gate waits for stdout/stderr before exiting and the tunnel no longer drops the last answer; the spawn/registration ordering claim matches how spawn() and the daemon event loop actually behave; the test key pairs are redrawn until both halves parse; the narrow card layout stacks one host per block with wrapped actions below 650 px; the redactor cache is per project (bounded to 256); reattached sessions carry the grant through workspace_agent_start/workspace_agent_continue/workspace_harness_start with the daemon re-arming only a pid it registered itself; the legacy src/agent/loop.ts entry gets an SSH context that follows the task claim; password registration requires at least four characters.
  • Independent UI check: I rendered the card with the console stylesheets at 1440/390/320 px for a super admin (bound and unbound global host) and a project admin (read-only). No button or table exceeds the card's content box, scrollWidth == innerWidth at every width, no grid cell overlaps, and buttons keep their 44 px height.
  • Screenshots: all 12 attachments are reachable, distinct, and have the stated viewport sizes (1440x900 / 390x844 / 320x568), including a bound and an unbound global host.
  • Branch hygiene: no stray files, no single-run evidence; the design and feature docs are updated consistently with the new behavior.
  • New tests (re-arm, attach instead of spawn, password minimum, deterministic key generation) live in the existing CI-discovered sets, and the unit/e2e checks pass on this head.

Non-blocking observation

  • runner/agent.mjs attaches a continued session only when its saved state is running. A review Pi session spends its wait between rounds in review_ready, so if a continue is ever delivered in that state the daemon would not re-arm it. PiConversation.resume is not set by the current callers, so this is latent rather than a defect today; if continue becomes the normal resume path, accepting review_ready/review_resuming here would close it.

The pid of a running session comes from its task's agent/state.json,
which every process of the computer can write. rearm() accepted any
agent the daemon had registered, so a task could point another task's
state.json at its own agent and receive that run's grant on the next
start or continue. The daemon now records the workspace an agent was
started for and moves an agent only for a call from the same workspace.
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

F1 is fixed in 5b0bf4cf (pushed on top of 8ebaae45).

  • The daemon records the task directory an agent was started for: the workspace_id of the starting call, as validated by the manager (runner/daemon.mjs takeSsh). SshAgentBroker.register() stores it, and rearm(pid, payload, workspace) refuses unless the call bringing the new grant names the same workspace (runner/ssh-agent.mjs). A pid that another task wrote into this task's state.json therefore cannot pull this run's grant onto that task's agent. A non-agent pid or a missing workspace is refused as before.
  • Regression tests:
    • test/ut/runner/ssh-agent.test.mjs: a rearm for another workspace (and for none) leaves the agent on its old grant; a rearm for its own workspace moves it.
    • test/ut/manager/ssh-grant.test.mjs: the real daemon path through manager/server.mjs. Task B's state.json names task A's running agent; B's workspace_agent_continue with B's grant leaves A's agent on A's grant, and A's own continue moves it.
    • Both tests fail on 8ebaae45: I put the previous runner/ssh-agent.mjs and runner/daemon.mjs back, and B's grant ended up on A's agent.
  • docs/design/agent-and-harness-runtime.md now names the starting task directory as what pins the transfer.

npm run check passes on 5b0bf4cf; the full npm test result will be in the PR description.

@agent-ci-infra-bot agent-ci-infra-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit: 5b0bf4c
Coverage: PR #1389; 20 changed file(s)

Comment thread runner/daemon.mjs
onSpawn: pid => { onSpawn?.(pid); if (sshBroker.register(pid, taken.grant, taken.kind)) taken.spawned.push(pid); },
onSpawn: pid => { onSpawn?.(pid); if (sshBroker.register(pid, taken.grant, taken.kind, taken.workspace)) taken.spawned.push(pid); },
// The pid of a running session comes from a task-writable file: only an agent started for this workspace moves.
onAttach: pid => { if (taken.kind === 'agent') sshBroker.rearm(pid, taken.grant, taken.workspace); },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F2 (suggestion, non-blocking) — the rearm() result is discarded, so a refused attach still reports success.

This arrow function is the only consumer of the boolean, and it ignores it. When the workspace check refuses (the state.json names another task's agent, or a pid the daemon never registered as an agent), the operation still answers {written:true} from workspace_agent_continue (runner/agent.mjs:275) and {started:true} from startAgent (runner/agent.mjs:184) — both also ignore onAttach's return value. The session then keeps the previous run's grant for the rest of the run.

This is not an authority leak: the control plane re-checks a signature/askpass request against the run token and lease behind that grant, and the previous run's lease is normally gone, so the stale secret is refused there. What is lost is diagnosability — exactly the case F1 was about now looks like ssh_target_denied on every command, indistinguishable from a role that simply has no host, and nothing is logged.

Suggestion: keep the boolean, e.g. onAttach: pid => { if (taken.kind === 'agent' && !sshBroker.rearm(pid, taken.grant, taken.workspace)) console.error('[ssh-agent] rearm refused for pid ' + pid); }, and, if the control plane wants to react, surface it in the answer (ssh_rearmed: false from workspace_agent_continue) so a mismatch ends the run with a real error instead of a silently downgraded session. Either half closes the observability gap; the log line alone is enough to keep this from blocking.

Merged via the queue into agent-CI-Infra:main with commit d0ed9f7 Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Post-merge: main received 8ebaae45, not the reviewed 5b0bf4cf.

  • The merge queue entry was created at 13:09:37Z for 8ebaae45; 5b0bf4cf was pushed at 13:10:40Z, but the group built from the old head kept its checks and merged at 13:19:07Z as d0ed9f7e.
  • git diff --stat 8ebaae45 origin/main is empty; git diff 5b0bf4cf origin/main shows the five F1-fix files missing. 5b0bf4cf has zero check runs, so the merged tree was never the one under review.
  • Consequently the F1 fix is not in main: rearm() there still accepts an agent pid read from a task-writable agent/state.json, so one task can move another run's SSH grant onto its own process.

5b0bf4cf itself is fine: register() stores the starting call's workspace_id, rearm() requires the call to name the same workspace (and refuses a missing one), the added tests cover both the broker and the real manager/daemon path, and I re-ran the two updated suites on this head locally (10/10 in test/ut/runner/ssh-agent.test.mjs, 2/2 in test/ut/manager/ssh-grant.test.mjs). Since #1389 is closed as merged and 5b0bf4cf is not in main, the fix needs a new PR from the branch (only that commit is unmerged).

The F2 suggestion (log a refused re-arm) remains valid for that follow-up. Separately, the queue race is worth handling in the automation: when the head changes while a PR is queued, dequeue the stale entry or verify the entry's head against the current head before merging.

@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

This PR merged from the merge queue at 8ebaae45 (merge commit d0ed9f7e). That head was enqueued at 13:09:37, after the F1 must-fix finding and before the fix 5b0bf4cf was pushed, so main does not contain the F1 fix even though this page shows 5b0bf4cf as the head. The fix is in #1390, unchanged (same tree as 5b0bf4cf).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant