GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Completed session marked failed, work reported lost, when its agent is missing at drain (`Session.AgentNotFoundError`) · Issue #53500 · anomalyco/opencode

Completed session marked failed, work reported lost, when its agent is missing at drain (Session.AgentNotFoundError) #53500

New issue
Copy link
New issue
Copy link
Open
Open
Completed session marked failed, work reported lost, when its agent is missing at drain (Session.AgentNotFoundError)#53500
Copy link
Assignees
neriousy

Description

@nathwn12
nathwn12
opened on Oct 6, 2026
Issue body actions

Description

A session stores its agent id; SessionContext.select resolves it against the live registry on every drain. If the agent is absent - renamed/removed in config, or the registry mid-reload - the drain dies before any step runs:

  • packages/core/src/session/context.ts:129 - if (!agent.info) return yield* new AgentNotFoundError({ sessionID: session.id, agent: session.agent ?? agent.id })
  • packages/core/src/session/runner/llm.ts:198-199 - prepareContext calls context.select
  • packages/core/src/session/execution.ts:99-103 - caught and only Effect.logError("Failed to drain Session", cause); nothing surfaces
  • packages/core/src/session/execution.ts:138-145 - the terminal lifecycle is published as Execution.Failed

So a drain failure is a false death notice: the earlier turn already completed, yet the session is marked failed and a watcher reads the work as still open.

Evidence

~/.local/share/opencode/log/opencode.log, 2026-09-30..10-05: 22 message="Failed to drain Session" errors, 13 with cause="Session.AgentNotFoundError" (orchestrator x6, fleet x3, commander x2, vault x1, ops x1). Each is preceded in the same second by a plugin reload / watcher teardown / agent.updated storm, the named agent having just been renamed or removed. At 2026-10-01T19:27:27.926Z, GET /api/agent/orchestrator -> 404 precedes the drain failure for the same id.

Session ses_ef21d9befffe4cTcYTlZ4HYaWE (agent ops) ran 3 edit calls at 21:07:56-21:08:00Z plus a write at 21:08:27Z, all completed; its drain failed at 21:09:08.016Z with Agent not found: "ops". session.get shows 18 messages and outcome:"failed".

Steps to reproduce

  1. Start a session on a named agent; let a turn complete.
  2. Rename/remove that agent in config and trigger a reload (or save a plugin that reloads).
  3. Prompt the session again (or let a watcher resume it).
  4. Drain fails with Session.AgentNotFoundError; the session ends failed, no session.error.

Expected: the completed turn stays completed, and a missing agent surfaces as a recoverable session error (or the drain falls back to the default agent).
Actual: Failed to drain Session is logged and dropped; the session is marked failed.

OpenCode version

2.0.22 runtime (same failure in logs from 2.0.21). Source compared: v2.0.0-920-g54630586a7.

Operating System

Windows 11 Pro, build 26200 (AMD64)

Terminal

Windows Terminal (TERM=xterm-256color), PowerShell 7.6.6, bun 1.4.2

Suggested fix

  1. In context.ts:129, don't fail the drain when the stored agent is absent - fall back to the default agent or emit a durable recoverable session error.
  2. Surface the drain cause through to-session-error.ts:73 as a real session error, not only logError.
  3. Don't publish Execution.Failed when no step started; the prior turn already settled.

Related

  • Session permanently frozen: drain() failures are logged but never surfaced to the client (no session.error event) #49740 - drain failures logged but never surfaced (general catch path; this is the agent-resolution cause).
  • plugin: session-hook plugin activation triggers failed reload and wipes provider registry #52618 - plugin reload churn; mentions Session.AgentNotFoundError for the configured default agent.

Activity

  1. github-actions
    assigned
    neriousy
    on Oct 6, 2026
  2. github-actions commented on Oct 6, 2026

    @github-actions
    github-actionsBot
    on Oct 6, 2026 – with GitHub Actions
    Contributor
    More actions

    This issue might be a duplicate of an existing issue. Please check:

    • #49740: reports drain failures being logged without a corresponding user-visible session error; this issue details the missing-agent failure and its effect on the terminal session state.
  3. opencodejr commented on Oct 27, 2026

    @opencodejr
    opencodejrBot
    on Oct 27, 2026
    Contributor
    More actions

    Thanks for the detailed write-up and the log analysis, @nathwn12.

    There seem to be two separate causes here:

    • Agent briefly missing during a plugin or config reload. Most of your logged cases follow a reload or agent.updated storm in the same second. That's Agent not found for an existing agent while plugins reload after a plugin file changes #54172: during a reload, config-defined agents briefly disappear, and a step that starts in that window fails with AgentNotFoundError. It's fixed on dev by fix(core): wait for plugin activation before each runner step #51133 (each runner step now waits for plugin activation) and should be in an upcoming release.
    • Agent actually renamed or removed. In this case the failure is what the code is designed to do. Resolving the stored agent fails, and the run is reported through the session.execution.failed event, which carries the error (the TUI shows it as "Session failed"). The Failed to drain Session log line is extra diagnostics on top of that (see Session permanently frozen: drain() failures are logged but never surfaced to the client (no session.error event) #49740). The earlier, completed turn isn't lost: its messages and tool results stay in the session, as your session.get with 18 messages shows. After the failure, the session goes idle and can be prompted again.

    So what's left is a request to change that behaviour: when a session's stored agent no longer exists, fall back to the default agent, or report it as a recoverable error rather than marking the run failed. I've marked this as a feature request for maintainers to consider. This can't currently be done with a plugin, because plugins can't hook how a session's agent is resolved.

    Related:

    • Agent not found for an existing agent while plugins reload after a plugin file changes #54172: agent missing during a non-atomic plugin reload (fixed by fix(core): wait for plugin activation before each runner step #51133)
    • plugin: session-hook plugin activation triggers failed reload and wipes provider registry #52618: plugin reload churn mentioning AgentNotFoundError
    • server: AgentNotFoundError for custom agents when ServerProcess.start drains sessions before agent registry loads #59585: AgentNotFoundError when sessions drain at server start before custom agents load
    • Session permanently frozen: drain() failures are logged but never surfaced to the client (no session.error event) #49740: drain failures thought not to be surfaced (closed; they're reported via session.execution.failed)
  4. davdroman commented on Oct 31, 2026

    @davdroman
    davdroman
    on Oct 31, 2026
    More actions

    Another data point for the fallback. In the TUI the problem usually doesn't show, because before each submit the TUI calls session.switchAgent when the selected agent differs from the session's saved one (packages/tui/src/component/prompt/index.tsx:1287). Clients that prompt without switching first still hit AgentNotFoundError. That includes TUI plugins using ctx.client.session.prompt or ctx.client.session.synthetic, which take no agent field. So the same session works when you type into it, but a plugin's continuation prompt fails with only a generic toast.

    Reproduced on 2.2.4:

    1. In a project, define { "agents": { "foo": { "mode": "primary" } } } in .opencode/opencode.jsonc.
    2. POST /api/session with { "agent": "foo", "location": { "directory": "<project>" } }.
    3. Rename foo to bar and set "default_agent": "bar". After the reload, GET /api/agent lists only bar.
    4. POST /api/session/<id>/prompt with { "text": "hi" }. The message fails with Agent not found: "foo", and the session still stores agent: "foo".

    A new session on bar in the same project answers normally.

    Falling back to default_agent when the saved agent no longer resolves, and saving it on the session, would give every client the result the TUI already produces by switching.

  5. davdroman
    marked Agent not found for prompts sent without switching agents after the session's agent is removed #60756 as a duplicate of this issue on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @neriousy
    neriousy

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.