Skip to content

server: AgentNotFoundError for custom agents when ServerProcess.start drains sessions before agent registry loads #59585

Closed as not planned

Description

@vietnq130593-code

Summary

When a ServerProcess starts, OpenCode drains pending/interrupted sessions before the
custom-agent registry (agents/*.md from config directories) is ready. Sessions whose
agent is a custom .md agent fail to drain with Session.AgentNotFoundError, and
subagent dispatches issued in this window fail with Agent not found: "<custom-agent>".
Built-in agents (e.g. general) resolve fine in the same window.

Environment

  • opencode version: 2.1.4 (update check reports current == latest)
  • OS: Windows_NT 10.0.19045 (win32 x64)
  • Terminal: VS Code integrated terminal (TERM_PROGRAM=vscode, TERM=xterm-256color)
  • Shell: cmd.exe host; agents run PowerShell via the shell tool
  • Install/channel: release (npm @opencode/cli)
  • Active plugins: one local plugin at ~/.config/opencode/plugin/memory.js (loads OK; unrelated)

Reproduction

  1. Define custom agents in ~/.config/opencode/agent/ (e.g. backend-dev.md, mode: subagent)
    plus a custom primary agent set as default_agent.
  2. Trigger a new ServerProcess start while sessions hold pending steps. Observed triggers:
    service restart; a subagent session killed by a provider error (free-quota exhaustion)
    leaving pending state; dispatching a subagent right after such an interruption.
  3. Dispatch the custom subagent immediately.
  4. Dispatch fails: Subagent failed (sessionID: ses_***): Agent not found: "backend-dev".
  5. Server log at the same moment (UTC):
    • Failed to drain Session cause=Session.AgentNotFoundError: Agent not found: "backend-dev"
      at SessionContext.select <- SessionRunner.prepareContext <- SessionRunner.drain <- ServerProcess.start
    • a second identical error ~40-60 ms later for the parent session's custom agent
      (Agent not found: "nemesis"; earlier days: "fullstack-lead")
    • only AFTER those errors: watcher stopped/watcher started lines for the config/agent
      directories - i.e. the agent registry loads after the drain has already run.
  6. Retry the same dispatch after the process is warm -> succeeds.

Expected Behavior

Drain at ServerProcess.start should wait for (or defer until) the custom agent registry
is loaded; custom agents should resolve like built-ins during drain.

Actual Behavior

Paired Session.AgentNotFoundError for every mid-flight session whose agent is a custom
.md agent; the subagent dispatch fails and the child session never runs a step. The parent
session recovers on new turns, but in-flight subagent work is lost.

Additional Context

  • Intermittent: 5 failure bursts over 5 days (Oct 22 x2, Oct 24, Oct 27 x3), stack identical
    every time (always ServerProcess.start -> drain).
  • Custom-agent dispatches succeed whenever the ServerProcess is warm; failures cluster right
    after service interruptions/idle-unload windows and after quota-killed subagent sessions.
  • Built-in general subagent succeeded in the same window in which custom agents failed -
    points at registry load ordering, not session corruption.
  • All agent definition files were present and parseable throughout (frontmatter verified).
  • Workarounds: retry after warm-up; route work to built-in general; set
    experimental.location_idle_timeout: false to reduce cold starts (just applied, unverified).

Activity

  1. opencode-agent commented on Oct 27, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I tried to reproduce this on 2.1.4 but couldn't get the error.

    What I tried, with a simulated model:

    • A custom primary agent backend-dev set as default_agent, defined in the global config dir (both agents/ and agent/).
    • Start a turn on it, kill the server while the turn is still running, then start it again.

    After the restart the session continued on backend-dev ("Continuing after restart"), and the server log had no Failed to drain Session or Session.AgentNotFoundError. I also added a local plugin with a slow setup to make the startup window longer, and still saw no drain errors. Those runs show less, though: the simulated model wasn't available after the restart, so I couldn't confirm the resumed turn actually ran.

    These would help narrow it down:

    1. The server log for the whole startup, from the first lines after the restart to the first Failed to drain Session, including any loading plugin lines and the directory / location values on the session and watcher lines. I'd like to see whether the failing sessions belong to a different project directory than the one whose config was loading.
    2. How the server runs. Is it the background service shared by several projects or VS Code windows, or one opencode per terminal? How many projects or directories does it usually have open?
    3. The exact folders. Are the agents in ~/.config/opencode/agent/ (singular) or agents/? Is default_agent set in the global config or in a project config? Does the project have its own .opencode/ directory?
    4. Which session fails. Is it always the parent and child pair from an interrupted subagent dispatch, or also a plain primary session restarted mid-turn?

    You can reply here or edit the issue. If you edit it, please also leave a comment so we know you're done. Related issues, in case one matches what you see:

  2. Tarquinen commented on Nov 2, 2026

    @Tarquinen
    Member

    Thanks for the report! We couldn't reproduce this and haven't heard back, so I'm closing this issue. If it still happens on the latest version, please open a new issue with your logs and steps to reproduce.

    — from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions