Skip to content

fix(acp): retry session setup while the agent is still starting - #1190

Open
ker2xu wants to merge 3 commits into
spacering-net:mainfrom
ker2xu:fix/opencode-startup-retry
Open

ker2xu wants to merge 3 commits into
spacering-net:mainfrom
ker2xu:fix/opencode-startup-retry

Conversation

@ker2xu

@ker2xu ker2xu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

OpenCode 2.x answers session/resume and session/new with its ServiceUnavailableError (HTTP 503, "Location plugins did not finish activating") for the first seconds after initialize. Its ACP bridge maps that to Internal service failure and keeps only the error name, so codeg surfaced a generic protocol error even though the same request succeeds a moment later.

This retries session/resume, session/load and session/new when the agent reports that specific startup condition, backing off from 500 ms to 4 s inside one 20 s budget shared by the whole resume → load → new chain. A rejected establishment request created nothing, so repeating it is safe. Every other failure (Method not found, Authentication required, session_busy, …) stays terminal, and handshake cancellation stops the wait immediately.

The classifier reads the ServiceUnavailableError name from the rendered message and from the error data, and also accepts the older "OpenCode service failure" wording.

Tests cover the classifier, a retry that succeeds on the third attempt, a non-retryable failure that is attempted once, the deadline bound, and cancellation. Verified with the server-feature test binary (--no-default-features --features server-bin --bin codeg-server --lib) and with cargo check; clippy on this tree still reports the existing unrelated lints in work_task/engine.rs, parsers/codex.rs, web/browser_tunnel/mod.rs, and acp/codex_catalog_commit/mod.rs.

ker2xu and others added 3 commits November 18, 2026 23:14
OpenCode 2.x answers session/resume and session/new with its
ServiceUnavailableError (HTTP 503, "Location plugins did not finish
activating") for the first seconds after initialize. Its ACP bridge maps
that to "Internal service failure" and keeps only the error name, so
codeg saw a generic protocol error. The same request succeeds a moment
later.

Retry resume, load and new on that specific wording, backing off from
500 ms to 4 s inside one 20 s budget shared by the whole resume -> load ->
new chain. A rejected establishment request created nothing, so repeating
it is safe; every other failure, including Method not found and
Authentication required, stays terminal. Handshake cancellation still
stops the wait immediately.

Recognize the current ServiceUnavailableError name in both the rendered
message and the error data, plus the older "OpenCode service failure"
wording. Unit tests cover the classifier, the retry, the deadline, and
cancellation.
…n setup

The startup-retry classifier also matched "OpenCode service failure" and a
bare "service unavailable" phrase. The former is OpenCode 1.x wording, which
codeg refuses to launch, and was 1.x's catch-all for permanent failures too
(a proxy misrouting its loopback server), so it would stall a doomed
handshake for the full 20 s budget; the latter matches any agent's text.
OpenCode 2.x reports the plugin-activation 503 as
`data.errorName = "ServiceUnavailableError"`, so match that name only.

Tests: the OpenCode fixture now has the wire shape (message
"Internal service failure", object data), the classifier rejects the 1.x and
bare-phrase wordings, and a new test cancels the handshake during the backoff
sleep rather than before the first attempt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…past the budget

A session/load that still answers ServiceUnavailableError once the startup
budget is spent fell through to the generic session/new fallback. With the
retries delaying that fallback, the new session tends to land just after
OpenCode finishes activating, binding the reopened conversation to an empty
session while the original one is intact. End the handshake with the
agent's error instead, as for a Claude Code refusing to start, so a reconnect
opens the original session.

The handshake budget is shortened under cfg(test) so a handshake test can
spend it in real time; the reopen harness gets a configurable quiet gap so
the backoff is not mistaken for the end of the handshake. The backoff-cancel
test now also asserts the sleep is cut short.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xintaofei

Copy link
Copy Markdown
Collaborator

Verdict: the approach holds. I checked it against the OpenCode v2.3.2 source. Until plugins finish activating, packages/server/src/location.ts (awaitPlugins, 5 s timeout) fails the catalog reads with ServiceUnavailableError. The ACP bridge reports that as Internal service failure with data.errorName set to the error's name, and for an Effect TaggedError that name is ServiceUnavailableError. newSession loads the catalog before creating a session, and a failed catalog load isn't cached, so repeating the request is safe and does help. Retrying inside the handshake with one shared budget and cancellation is the right place for it.

Two problems, fixed on top of the PR:

  1. Classifier too broad (src-tauri/src/acp/connection.rs, session_establishment_is_retryable). Important.
    • "opencode service failure" is OpenCode 1.x wording, and codeg refuses to launch 1.x (src-tauri/src/acp/opencode_launch.rs).
    • In 1.x it was a catch-all that also covered permanent failures. src-tauri/src/network/proxy.rs documents one: a proxy that misroutes the loopback server, which would stall for the whole 20 s before failing.
    • The bare "service unavailable" phrase matched any agent's text.
    • Now only the ServiceUnavailableError name counts, in the rendered error or in data. The test fixture now has the shape the error actually has on the wire.
  2. Exhausted startup error fell back to session/new (session/load error branch in run_connection). Important.
    • The retries push the session/new fallback later, so it tends to arrive right after OpenCode finishes starting. It then succeeds and binds the reopened conversation to an empty session while the original is intact.
    • Now a startup error that still fails session/load after the budget ends the handshake with the agent's error, the same way the Claude-refused-to-start path does, so a reconnect opens the original session.
    • New handshake test: resume and load keep answering the startup error. It asserts resume is retried and no session/new is sent, and it fails without the fix. To let it run in real time, the handshake budget is 2 s under cfg(test).

Also added: a test that cancels during the backoff sleep and asserts the sleep is cut short. The existing cancellation test cancels before the first attempt.

Verification: server lib tests and the full desktop cargo test --features test-utils pass, and desktop, server and mcp clippy with -D warnings are clean. Not tested against a live OpenCode binary.

With these changes it looks ready to me.

The changes were pushed to this pull request (1 file, +162/-29).


🤖 Generated with Codeg

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.

2 participants