opencode-goal-plugin icon indicating copy to clipboard operation
opencode-goal-plugin copied to clipboard

OpenCode Desktop V2: /goal >60s triggers false 'Timed out waiting for the server to respond' toast and restores input box while goal is running

Open wycocoyu opened this issue 1 month ago • 1 comments

Hi @danyel117! First of all, thank you so much for the super fast turnaround on #110 (PR #116) — that makes human-in-the-loop pauses on V2 so much smoother.

While running V2 /goal <objective> in OpenCode Desktop (macOS GUI / Web UI), we ran into a 100% reproducible bug where every /goal <objective> run that lasts longer than 60 seconds triggers a false-positive command failure toast and restores the /goal ... text back into the composer input box, even though the goal prompt was already admitted and the agent is actively executing tools in the background.

(Note: as with #110, this issue write-up and root-cause trace were drafted with the help of my AI coding assistant from our live session debugging, and I'm also opening a companion PR in case the proposed fix is helpful!)


Symptoms & Screenshots (OpenCode Desktop V2)

  1. At the 60-second mark after sending /goal <objective>, OpenCode Desktop pops up a red error toast: 发送命令失败: Timed out waiting for the server to respond (Failed to send command: Timed out waiting for the server to respond) 1-desktop-command-timeout-toast

  2. The composer restores /goal <objective> back into the input box as if the submission failed, while the session indicator right above it says 工作中 (Working...): 2-input-restored-while-goal-running

  3. Meanwhile, in the session transcript, the expanded /goal prompt was already admitted at T+0s and the agent has already executed get_goal, create_goal, Shell, etc.: 3-prompt-already-admitted-and-executing Because the /goal ... text is put back into the input box, users naturally think the message was intercepted/dropped and press Enter again, accidentally firing a duplicate /goal while the first one is mid-flight.


Root Cause (3-Layer Interaction between Plugin, OpenCode Server, and OpenCode Desktop)

We traced this across @prevalentware/opencode-goal-plugin (src/server.ts) and anomalyco/opencode (packages/app & packages/server):

1. Plugin layer (src/server.ts:3420-3438)

In setupV2, when pursue is true (/goal <objective>, /goal resume, or /resume_goal), command.execute first calls await context.session.prompt(...) (admitted = true), and then holds the command.execute Promise open across the entire multi-turn goal lifecycle via do { await context.session.wait({ sessionID: input.sessionID }, { signal }); ... } while (...).

  • As documented in README.md (Zed and ACP lifecycle boundaries) and PRs #65 / #67 (for #59) + anomalyco/opencode#53913, this was added so opencode acp keeps turn.commandFinished pending until all auto-continuations finish.

2. OpenCode Server layer (packages/server/src/handlers/session.ts:327-339)

POST /api/session/:sessionID/command awaits the plugin's command.execute callback before sending the 204 No Content response headers. (By contrast, OpenCode's built-in /init and /review commands only await session.prompt(...) and return 204 immediately.)

3. OpenCode Desktop / Web frontend layer (packages/app)

  • 60s response-headers timeout + queue slot exhaustion: packages/app/src/runtime/server/request-queue.ts:21, 150-158 wraps all non-SSE requests in a 4-slot queue (requestQueueLimit = 4) with a hard requestHeadersTimeoutMs = 60_000 (60 seconds) timer (only POST /api/worktree is exempted). While /goal is running, POST /api/session/:sessionID/command occupies 1 of the 4 browser request slots and aborts at 60s with new DOMException("Timed out waiting for the server to respond", "TimeoutError").
  • Why failSubmission restores the input box on /command but not /prompt: In packages/app/src/composer/submit.ts:229-231, 644-660:
    function failSubmission(input, session, kind, error, restore, messageID) {
      if (messageID && session.admitted(messageID)) return
      restore()
      input.notify.failed(kind, error)
    }
    
    For sendPrompt (POST /api/session/:sessionID/prompt), the client sends its generated id (messageID), so once SSE confirms the message, session.admitted(messageID) is true and any later timeout is ignored. However, sendCommand (POST /api/session/:sessionID/command) does not accept a client id in its protocol schema, and context.session.prompt(...) inside the plugin generates a brand-new server-side message ID. Therefore session.admitted(messageID) is always false for plugin commands, causing failSubmission to restore the composer input and show the failure toast at 60s.

4. Secondary hazard once anomalyco/opencode#53913 lands

Once anomalyco/opencode#53913 forwards the HTTP request abort signal to execution.signal:

  • At 60 seconds, request-queue.ts aborts the HTTP request with DOMException("Timed out waiting for the server to respond", "TimeoutError").
  • In src/server.ts:446-455, isTransportAbortReason(reason) matches /socket|disconnect|connection|closed|eof|reset|broken pipe|request aborted|client closed/i, which does not match "Timed out waiting for the server to respond" / "TimeoutError".
  • As a result, at the 60-second mark, the Desktop frontend timeout would be treated as an explicit user cancellation and kill the active goal!

Proposed Fix (Preserving Zed ACP 100% while fixing Desktop / Web / TUI)

Looking at how anomalyco/opencode spawns ACP vs Desktop/Web/TUI:

  1. opencode acp (packages/cli/src/commands/handlers/acp.ts:24-25) sets process.env.OPENCODE_CLIENT = "acp" at entry and always spawns an isolated serve --stdio --port 0 subprocess (packages/cli/src/services/standalone.ts:17-33). It never shares the background opencode serve --service daemon.
  2. Desktop (OPENCODE_CLIENT = "desktop"), Web, and TUI (OPENCODE_CLIENT = undefined or "desktop") subscribe to /api/event SSE streams directly and do not rely on holding POST /api/session/:sessionID/command open across auto-continuations.

So we can gate the command-level session.wait loop (while keeping the 1-second session.wait idle barrier inside runAutoContinue at src/server.ts:2714 intact for all clients):

function shouldHoldCommandExecution(options: GoalPluginOptions = {}): boolean {
  if (typeof options.wait_for_command === "boolean") {
    return options.wait_for_command
  }
  return process.env.OPENCODE_CLIENT === "acp"
}

And also include timeout|timed out|timeouterror in isTransportAbortReason so any proxy/client HTTP timeout never masquerades as a deliberate user cancellation.

I'll open a small PR shortly with this change and unit tests so you can inspect or tweak it however you see fit!

wycocoyu avatar Oct 26 '26 18:10 wycocoyu

Hi, I’m a student and an independent DIY developer who enjoys experimenting with OpenCode. I’d love to contribute to the design, bug fixing, and optimization of this project. While building and testing some ideas of my own, I encountered a few issues when using /goal. I’d really appreciate any advice or guidance you could provide. I’d be happy to work on a PR to fix these issues, and if possible, I’d also appreciate it if you could review my changes. The PR linked above was opened by my AI coding assistant based on our debugging session. Please feel free to take a look when you have time. Thank you very much!

If you notice any problems, please feel free to point them out. And if you feel that my implementation could be improved, or if you would prefer to implement the fix yourselves, that’s completely fine as well. You can close this PR and leave me a comment with your feedback.

wycocoyu avatar Oct 26 '26 18:10 wycocoyu