Skip to content

session.error not emitted when Provider.getModel fails in main prompt loop #12738

Closed

Description

@ColeMurray

Description

When Provider.getModel() throws a ModelNotFoundError during the main prompt loop, no session.error event is published to the bus. The session silently transitions to idle, leaving SSE consumers with no indication that an error occurred.

Reproduction

Configure a model with a provider that isn't loaded (e.g., "openai/gpt-5.2" without OPENAI_API_KEY), then send a prompt via prompt_async. The session goes idle with no assistant response and no error event.

Root cause

In packages/opencode/src/session/prompt.ts line 324, the loop() function calls Provider.getModel() without a try/catch:

// line 324
const model = await Provider.getModel(lastUser.model.providerID, lastUser.model.modelID)

When this throws, the exception propagates out of the while(true) loop. The defer at line 278 calls cancel(sessionID), which sets the session status to "idle" at line 259 — but no session.error event is published.

Since prompt_async (packages/opencode/src/server/routes/session.ts line 765) doesn't await the prompt, the exception becomes an unhandled promise rejection:

// line 765
SessionPrompt.prompt({ ...body, sessionID })  // fire-and-forget, no await

The pattern already exists elsewhere in the same file

The command handler at lines 1710–1721 correctly catches ModelNotFoundError and publishes Session.Event.Error:

// lines 1710-1721
try {
  await Provider.getModel(taskModel.providerID, taskModel.modelID)
} catch (e) {
  if (Provider.ModelNotFoundError.isInstance(e)) {
    const { providerID, modelID, suggestions } = e.data
    const hint = suggestions?.length ? ` Did you mean: ${suggestions.join(", ")}?` : ""
    Bus.publish(Session.Event.Error, {
      sessionID: input.sessionID,
      error: new NamedError.Unknown({ message: `Model not found: ${providerID}/${modelID}.${hint}` }).toObject(),
    })
  }
  throw e
}

Suggested fix

Wrap the Provider.getModel call at line 324 in a similar try/catch that publishes Session.Event.Error before re-throwing:

let model: Awaited<ReturnType<typeof Provider.getModel>>
try {
  model = await Provider.getModel(lastUser.model.providerID, lastUser.model.modelID)
} catch (e) {
  if (Provider.ModelNotFoundError.isInstance(e)) {
    const { providerID, modelID, suggestions } = e.data
    const hint = suggestions?.length ? ` Did you mean: ${suggestions.join(", ")}?` : ""
    Bus.publish(Session.Event.Error, {
      sessionID,
      error: new NamedError.Unknown({ message: `Model not found: ${providerID}/${modelID}.${hint}` }).toObject(),
    })
  }
  throw e
}

Impact

  • Headless/API consumers relying on SSE events see session.idle with no error, making failures indistinguishable from success
  • The TUI is unaffected since it catches the unhandled rejection separately
  • Only prompt_async callers are impacted (the synchronous prompt endpoint awaits the result)

Activity

  1. github-actions commented on Feb 8, 2026

    @github-actions
    Contributor

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

    Feel free to ignore if none of these address your specific case.

  2. ColeMurray commented on Feb 8, 2026

    @ColeMurray
    Author

    confirming dupe of: #8460

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

Metadata

Metadata

Assignees

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