Skip to content

fix(ca): don't wake asleep agents on sleep; adopt OpenCode/Codex servers restarted after wake - #1370

Merged
codyde merged 2 commits into
masterfrom
fix/ca-sleep-and-desktop-reattach
Nov 17, 2026
Merged

codyde merged 2 commits into
masterfrom
fix/ca-sleep-and-desktop-reattach

Conversation

@codyde

@codyde codyde commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Fixes two Cloud Agent bugs: railway ca sleep woke agents that were already asleep and left them running, and OpenCode/Codex setup failed with "port occupied" after an agent woke.

Changes

  • ca sleep reads the agent's status first. RUNNING: flush and sleep as before. SLEEPING (backboard also reports STOPPING as SLEEPING): print "already asleep" and send nothing. Any other state: send the sleep request without SSH. ca sleep --all only acts on running agents.
  • The in-VM OpenCode and Codex scripts adopt the port's listener when the saved PID no longer matches but the listener accepts the saved credentials (OpenCode's server.json password, Codex's token): they find it in /proc, record its process group leader and start time, and continue. Setup, connect, discovery (on an unsaved copy) and OpenCode's stop all do this. A listener that rejects the saved credentials is still refused.

Why

  • Since monorepo #39843 any SSH connection wakes a sleeping agent, so the pre-sleep sync woke it, and the sleep request then arrived while it was starting and was refused. The sync stays for RUNNING agents only; since monorepo #37022 host-initiated stops sync the guest anyway.
  • Since monorepo #41919 express-agent restarts recorded listeners on the first boot after a power-off, so the server came back under a new PID and the scripts reported "Port 4096 is occupied by another process".

Verification

  • Rust: tests for the status gate (plan per status; SLEEPING sends no mutation; STARTING sends one without flushing) and for --all filtering. The sleep tests in tests/cloud_agent_lifecycle.rs now cover the new behavior, plus --all with nothing running.
  • Python (tests/opencode_desktop.py, tests/codex_server.py): a server relaunched under a new PID is adopted by setup, connect, discovery (state file unchanged) and OpenCode stop; a listener with other credentials is refused. The adopt tests fail on master. Both files pass on macOS and in a Linux container, which exercises the /proc lookup. Codex's stale-PID test asserted the old refusal and is replaced by the refuse case.
  • cargo fmt, build, test (1,981 passed, 6 ignored), clippy: no new warnings.

Related: #1369 (help text for idle sleep)

🤖 Generated with Claude Code

codyde and others added 2 commits November 17, 2026 09:04
`ca sleep` flushed the disk over SSH before requesting sleep. Any SSH
connection now wakes a sleeping agent, and the sleep request that follows
arrives while it is starting and is refused, so the command failed and left
the agent running. `ca sleep --all` did this to every sleeping agent.

Read the agent's status first. A RUNNING agent is flushed and slept as
before; a SLEEPING one (which includes the orchestrator's STOPPING) is
reported as already asleep without connecting; any other state still sends
the sleep request, but never over SSH. `--all` only acts on running agents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
express-agent now restarts recorded listeners on the first boot after a
power-off, so the OpenCode or Codex server this CLI started comes back
under a new PID. The in-VM scripts only treated a listener as their own
when the saved PID and start time matched, so rerunning desktop setup (or
`railway code --opencode/--codex` reconnecting) failed with "Port 4096 is
occupied by another process".

When the saved-PID check fails and the port's listener authenticates with
the saved credentials (OpenCode's server.json password, Codex's token), find
it through /proc, record its process group leader and start time, and
continue. Discovery does the same on a copy it never saves, and OpenCode's
stop adopts before stopping. A listener that rejects the saved credentials
is still refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codyde codyde added the release/patch Author patch release label Nov 17, 2026
@codyde codyde added release/skip Author no release and removed release/patch Author patch release labels Nov 17, 2026
@codyde
codyde merged commit a581e58 into master Nov 17, 2026
14 of 16 checks passed
@codyde
codyde deleted the fix/ca-sleep-and-desktop-reattach branch November 17, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release/skip Author no release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant