Repository navigation
Conversation
`opencode run` subscribes to /api/event and waits for the opening `server.connected` event before it sends the prompt. A proxy, tunnel or VPN that holds the subscription open without forwarding anything makes that wait unbounded: the session is created, no prompt is ever sent, and the command hangs with no output (issue anomalyco#62681). Bound the wait with `ACPConnection.ConnectedTimeout` (10s, overridable) and report it: - the turn fails with `ACPEventStreamUnavailableError`, which reaches the CLI as "The OpenCode server is reachable but sent no events within 10s. Something between this client and the server, such as a proxy, tunnel or VPN, is probably buffering the event stream." and no prompt is submitted; - the catalog and session attach only wait for event ordering, so they now continue after the deadline instead of blocking every command that needs a catalog.
There was a problem hiding this comment.
This doesn't fix #62681 yet. I ran the issue's reproduction (a proxy that sends the /api/event headers but holds back the body) with opencode run --server <proxy> from source. With this branch it behaves exactly as on dev: nothing is printed, no prompt is sent, and it hangs until killed (exit 124 after 30s, then "shutdown did not finish within 10s").
The reason is that opencode run doesn't go through the ACP code. commands/handlers/run.ts → run/run.ts → runNonInteractivePrompt, which waits on await stream.next() at packages/cli/src/run/noninteractive.ts:69 with no time limit. The same unbounded first-event wait exists in packages/tui/src/mini/stream-v2.transport.ts (connect(), nextEvent) for opencode mini. A fix for the issue should put a time limit on that wait (and probably the mini one) and exit non-zero with your message. A test should then drive runNonInteractivePrompt against a stream that never sends anything. As it stands, the PR only changes opencode acp, and its tests only cover the ACP wire fixture. If you also want the ACP change, please describe it as an ACP fix rather than Fixes #62681.
On the ACP part itself, see the inline comments: on a silent stream, every session attach waits the full 10s again, and catalog and sessions can miss events once they carry on past the deadline.
Public API: unchanged. The only addition is a new ACP error, errorName: "EventStreamUnavailable".
Verified with the issue's proxy reproduction on base and head, bun test test/acp (103 pass), and tsc -b (clean).
Related: #62682
| const connected = yield* take(turn.subscription) | ||
| // A buffered stream never delivers it, and submitting anyway would wait for events forever. | ||
| const timeout = yield* ACPConnection.ConnectedTimeout | ||
| const opened = yield* take(turn.subscription).pipe(Effect.timeoutOption(timeout)) |
There was a problem hiding this comment.
This only covers opencode acp. The command in #62681, opencode run, waits for the first event at packages/cli/src/run/noninteractive.ts:69 (await stream.next()), and that wait is still unbounded. With the issue's proxy reproduction, opencode run on this branch still hangs until killed. The fix for the issue needs to go there, and probably in packages/tui/src/mini/stream-v2.transport.ts connect() for mini.
There was a problem hiding this comment.
You are right, the issue was in the wrong place. Moved it:
packages/cli/src/run/noninteractive.ts: the openingstream.next()now goes throughfirstEvent(), which has a 10s deadline and rejects with "The OpenCode server accepted the event stream but sent nothing within 10s. Something between this client and the server, such as a proxy, tunnel or VPN, is probably buffering it." A test intest/run/noninteractive.test.tsreproduces the buffered body with anext()that never settles and asserts the run fails instead of hanging.packages/tui/src/mini/stream-v2.transport.ts: the same wait inconnect()is bounded throughnextEvent(stream, signal, stall), and aStreamStalledErrorfails the boot throughreadyRejectrather than looping onreconnectingevery 250ms. Covered intest/mini/stream-v2.transport.test.ts.
The ACP part is kept, but only as the in-protocol variant of the same failure.
| attach: Effect.fn("cli.acp.sessions.attach")(function* (session, cwd, mcpServers) { | ||
| yield* Deferred.await(connected) | ||
| // Attaching no longer blocks on a stream that never opens; the turn reports that failure. | ||
| yield* ACPConnection.awaitConnected(connected) |
There was a problem hiding this comment.
The result of awaitConnected is ignored here, and connected never completes on a silent stream. So every session/new or session/load waits the full 10s again, and so does each new cwd in catalog.ts. Each prompt then waits another 10s before failing. Once the code carries on, an event that arrives between loading and the late server.connected can also be missed, which is the ordering the wait was there to protect. It may be better to remember the timeout once, or to fail fast here with the same EventStreamUnavailableError, rather than silently continuing.
There was a problem hiding this comment.
Agreed, carrying on was the wrong call. awaitConnected now fails with EventStreamUnavailableError and remembers the deadline per connection (connectionDeadlines, keyed by the connected Deferred), so a caller that arrives after the stream was already declared silent fails at once instead of waiting the full timeout again.
sessions.attach and catalog.create therefore report the stall on the first request, and EventStreamUnavailableError is now part of CatalogError so the catalog channel carries it. The tests in test/acp/event-stream.test.ts cover both halves: session/new returns -32603 with EventStreamUnavailable, and a second session/new on the same connection fails in under 50ms instead of waiting another 100ms.
There was a problem hiding this comment.
Failing fast on a silent stream is fine, and the new tests cover that case. The problem is the healthy case, which they don't exercise. In awaitConnected (connection.ts:36–40), once Date.now() passes the stored deadline, remaining > 0 is false and it fails without checking connected, even when server.connected arrived long ago. I reproduced it on this commit with the wire fixture: startWire({ connectTimeout: "100 millis" }) with the normal (non-buffered) stream, newSession() succeeds, then after Bun.sleep(300) a second newSession() fails with EventStreamUnavailable. With the default 10s, any ACP client that opens another session, or a catalog for a new cwd, more than 10s after the first would be refused.
To fix it, check Deferred.isDone(connected) (or Deferred.poll) first and return when it's done; only fail fast when it's still pending after the deadline. A test with a healthy stream and a late second newSession() would cover it. The mini reconnect point (stream-v2.transport.ts:1559: only reject while state.initial, otherwise fall through to the reconnect path) is also still open.
There was a problem hiding this comment.
Fixed in 498cdd3. awaitConnected now returns at once when Deferred.poll(connected) shows the stream is already open, so the deadline only bounds a stream that never arrived. Reproduced your case as a test: startWire({ connectTimeout: "100 millis" }), newSession(), Bun.sleep(300), second newSession() succeeds.
The mini reconnect point is fixed too: the StreamStalledError branch now requires state.initial, so a stall after server.connected writes "reconnecting" and retries instead of resolving ready that has already resolved. New test drives that path with a stalled second stream and a healthy third one.
`opencode run` waited for the opening event with an unbounded `stream.next()`, so a proxy that answers the subscribe request and then holds the body open left the command hanging forever, with no session and no output (anomalyco#62681). The wait now has a 10s deadline and fails with a message that names the buffering middlebox. The same wait in the mini transport `connect()` is bounded too, and a stream that never opens fails the boot instead of looping on `reconnecting`. For `opencode acp`, `awaitConnected` now fails with EventStreamUnavailableError and remembers the deadline once per connection, so `session/new` reports the stall instead of every later request waiting another 10s, and an event that arrives between loading and a late `server.connected` can no longer be missed.
There was a problem hiding this comment.
opencode run is fixed now. With the issue's buffering-proxy reproduction it exits with code 1 after about 11s and prints the new error, instead of hanging until it is killed. My earlier concern about the wrong layer is resolved. The repeated 10s wait per attach is gone too, but the way it was fixed breaks ACP sessions on a healthy stream, which blocks the merge (inline comment on connection.ts). I reproduced it with the wire fixture: with a 100ms timeout and a working event stream, a second session/new 300ms after the first fails with EventStreamUnavailable. With the default 10s, any ACP client that opens a session, or a catalog for another cwd, more than 10s after the first would be refused.
There's also a smaller issue in the mini transport: a reconnect attempt that stalls now stops the event loop with no error (inline comment).
Checked with: the reproduction script, bun test test/acp test/run in packages/cli (130 pass), bun test test/mini/stream-v2.transport.test.ts in packages/tui (71 pass), and tsc -b in packages/cli (clean).
Public API: unchanged, apart from ACP session/new and session/load (and catalog requests) now being able to fail with errorName: "EventStreamUnavailable".
Related: #62682
| export const awaitConnected = (connected: Deferred.Deferred<void>) => | ||
| Effect.gen(function* () { | ||
| const timeout = yield* ConnectedTimeout | ||
| const deadline = connectionDeadlines.get(connected) ?? Date.now() + Duration.toMillis(timeout) |
There was a problem hiding this comment.
Once the deadline has passed, remaining > 0 is false, so this fails without checking connected. That happens even when the stream opened long ago. On a healthy connection, every attach or catalog create that comes more than 10s after the first call gets EventStreamUnavailable. I reproduced it: startWire({ connectTimeout: "100 millis" }), newSession() succeeds, then after Bun.sleep(300) a second newSession() fails. Check Deferred.isDone(connected) (or Deferred.poll) first and return if it's done; only fail fast when it's still pending after the deadline. Please also add a test for the healthy late-request case.
There was a problem hiding this comment.
Fixed in 498cdd3: awaitConnected polls the deferred first and returns when it is already done, so a request more than 10s into a healthy connection is served. Your reproduction is now a test in packages/cli/test/acp/event-stream.test.ts (a later request is served after the connect timeout has passed).
There was a problem hiding this comment.
Thanks, confirmed on 498cdd3: Deferred.poll returns early once the stream has opened, and a later request is served after the connect timeout has passed passes along with the rest of test/acp. This one is resolved.
The one open point is in packages/cli/src/run/noninteractive.ts (line 110): on SIGINT/SIGTERM the subscription is closed before cancel() finishes, and cancel() still needs the stream to see background jobs end.
There was a problem hiding this comment.
Fixed in f8f7ebd. The signal is no longer forwarded to the subscription, which is what was closing the stream: cancel() waits on state.ended, and only consume() resolves that, from the terminal events of the jobs it just stopped. Closing the stream on SIGINT/SIGTERM took those events away, so the loop sat on await ended until the shutdown timeout and the process exited 1.
The startup case still ends at once. firstEvent races stream.next() against the caller's signal itself, and the finally aborts the controller and returns the iterator, so a signal during the opening wait closes the subscription and returns in milliseconds. A run that is cancelled before it starts still opens no subscription at all, since if (input.signal.aborted) controller.abort() runs before subscribe.
New test, a cancelled run keeps the stream open so the jobs it stopped can finish, in packages/cli/test/run/noninteractive.test.ts. The mocked subscription closes on abort like the real client does, a background shell started after admission keeps cancel() waiting, and the terminal event pushed after the signal lets the run finish. It fails on 498cdd3 (hung after 2 s) and passes here.
Checks: bun test test/run test/acp gives 129 pass, 3 skip. additional-directories.subprocess.test.ts timed out once under parallel load on Windows and passes alone in 4 s. bun run typecheck in packages/cli is clean over 2,183 files, and oxlint plus prettier report nothing on the two changed files.
There was a problem hiding this comment.
Thanks. I checked f8f7ebd in the code: the caller's signal no longer closes the subscription, so consume() keeps getting events while cancel() waits on state.ended. The opening wait still stops on the signal through firstEvent, and the outer finally closes the stream. That resolves the cancellation point. I only read the diff and haven't rerun the tests here. Both of my earlier ACP and mini points were already resolved in 498cdd3.
| if (controller.signal.aborted || input.footer.isClosed) return | ||
| // A stream that never opens is not a dropped connection: reconnecting would only hide the | ||
| // buffering proxy behind a reconnect loop that never settles. | ||
| if (error instanceof StreamStalledError) return readyReject(error) |
There was a problem hiding this comment.
This also runs for reconnect attempts after the session was already running. ready has resolved by then, so readyReject does nothing and connect() simply returns. The session stops getting events with no error and no "reconnecting" status. Only reject here while state.initial is true; after that, a stall should fall through to the normal reconnect path, which the new time limit now makes possible.
There was a problem hiding this comment.
Fixed in 498cdd3: the branch is now if (error instanceof StreamStalledError && state.initial) return readyReject(error), so a reconnect attempt that stalls falls through to the normal reconnect path (status "reconnecting", 250ms wait, new subscribe). Covered by a new test in packages/tui/test/mini/stream-v2.transport.test.ts where the second stream stalls and the third resumes the session.
|
At
Please propagate macOS arm64, Bun 1.4.4; no prompt/provider requests. AI-assisted investigation. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
|
Both are fixed in 498cdd3. The subscription is now created inside the run's cleanup: it is opened under a controller that follows
Coverage added: |
There was a problem hiding this comment.
The issue's buffering-proxy reproduction is fixed: opencode run now exits with code 1 after about 10s and prints the new error. Both points from my last review are resolved. awaitConnected returns early once the stream has opened, and the new healthy late session/new test passes. In mini, a stalled stream only fails startup on the first connection; later stalls reconnect.
One new problem in this commit, found by reading the code: runNonInteractivePrompt now closes the event stream as soon as SIGINT or SIGTERM arrives, but cancel() still needs it (inline comment). Ctrl-C while background shells or subagents are still running can then hang until the 10s shutdown limit.
Checked with: the reproduction script, bun test test/acp test/run in packages/cli (132 pass), bun test test/mini/stream-v2.transport.test.ts in packages/tui (72 pass), and tsc -b in both (clean).
Public API: unchanged, apart from ACP session/new and session/load (and catalog requests) now failing with errorName: "EventStreamUnavailable" when the event stream never opens.
Related: #62682
| const controller = new AbortController() | ||
| // SIGINT and SIGTERM must interrupt the opening wait too, so the subscription is opened under the | ||
| // caller's signal and closed again whatever happens to the run. | ||
| const abort = () => controller.abort() |
There was a problem hiding this comment.
This closes the subscription as soon as input.signal aborts, but on dev it stayed open until cancel() finished, and cancel() still depends on it. After session.wait, if background shells or subagents are still running, it waits on state.ended, and only consume() resolves that from stream events. Subagents that start again while cancelling are also interrupted from the stream (session.execution.started while phase === "cancelling"). With the stream closed, consume() returns and neither happens, so Ctrl-C during background work can hang until the 10s shutdown limit. firstEvent already watches input.signal, so this listener isn't needed for the opening wait. Dropping it and keeping the controller.abort() in the outer finally keeps both the new fix and the old cancel behaviour.
|
Rechecked I reproduced the remaining cancellation concern with real CLI subprocesses against local HTTP fixtures:
macOS arm64, Bun 1.4.4; synthetic server/job fixtures, no provider calls. AI-assisted verification. |
… jobs Forwarding the caller's signal to the subscription closed the stream as soon as SIGINT or SIGTERM arrived, and `cancel()` waits for the terminal events of the jobs it just stopped, so a run with a background shell or subagent in flight sat out the shutdown timeout and exited 1 instead of 143 or 130. The opening wait already takes the signal itself, so the stream now stays open until the run is over and is closed in the finally either way.
|
@r266-tech the cancellation case you reproduced is fixed in f8f7ebd. You were right that the early
|
There was a problem hiding this comment.
The last open point is addressed. A cancelled opencode run keeps the event stream open until cancel() has seen its background jobs end, as on dev. The opening wait still stops on SIGINT/SIGTERM through firstEvent, and the outer finally closes the stream. The new test checks this against a mock stream that closes on abort, like the real client does. All earlier threads are resolved, and I found nothing new.
Checked with: the issue's buffering-proxy reproduction (opencode run exits with code 1 after about 10s with the new error, instead of hanging), bun test test/acp test/run in packages/cli (133 pass), and tsc -b in packages/cli (clean).
Public API: unchanged, apart from ACP session/new and session/load (and catalog requests) now failing with errorName: "EventStreamUnavailable" when the event stream never opens.
|
Rechecked
Both keep the subscription open while the stop is acknowledged but the job is still running, receive the terminal SSE event, read back an empty job list, and then close the subscription. Neither prints Startup cancellation before macOS arm64, Bun 1.4.4; synthetic local server/job data, no provider calls. AI-assisted verification. |
|
Thanks for running the fixtures against this head, that is exactly the signal I could not get from mocks. For completeness, the three earlier threads on the ACP side are also addressed at
All check runs on this head are green and the PR is mergeable. If nothing else is outstanding, the review threads above can be resolved. |
Issue for this PR
Closes #62681
Type of change
What does this PR do?
opencode runand the ACP client waited for the first event of/api/eventwith no limit, and the TUI reconnected forever when the event stream never opened. When a proxy, tunnel or VPN answers the subscribe request and then holds the body open, the client waits with no output until something else kills it.run/noninteractive.ts: the subscription is now opened under the caller's signal, so SIGINT and SIGTERM end the opening wait at once instead of after the deadline, and the wait is bounded (10s) with "The OpenCode server accepted the event stream but sent nothing within 10s". The iterator is closed in afinallywhatever happens, so a timeout no longer leaves the connection open.acp/connection.ts:awaitConnectedfails withEventStreamUnavailablewhen the stream never opens, and returns at once when it already opened, so a session or catalog request more than 10s into a healthy connection is still served.turn.ts,catalog.tsandsessions.tsuse it.tui/src/mini/stream-v2.transport.ts: the connect timeout only rejects while the session has not started. A stall afterserver.connectedis a dropped connection, so it writes "reconnecting" and retries.How did you verify your code works?
packages/cli:bun test test/run/noninteractive.test.ts test/acp-> 127 pass, 3 skip, 0 fail.packages/tui:bun test test/mini/stream-v2.transport.test.ts-> 72 pass.bun check --no-script -bclean in both packages, prettier and oxlint clean.New tests:
noninteractive.test.ts: a stream that never yields fails after 10s with the message and the subscription is closed; aborting during the opening wait returns immediately and closes it too.acp/event-stream.test.ts: on a healthy stream a session request after the connect timeout still succeeds; a silent stream still fails the turn with-32603 EventStreamUnavailable.stream-v2.transport.test.ts: a stalled second stream reconnects and a third stream resumes the session.Checklist
Added by the opencode bot (AI-generated).
Related: #62682