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

Closed
opened 2026-02-16 18:11:02 -05:00 by yindo · 2 comments
Owner

Originally created by @ColeMurray on GitHub (Feb 8, 2026).

Originally assigned to: @thdxr on GitHub.

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)
Originally created by @ColeMurray on GitHub (Feb 8, 2026). Originally assigned to: @thdxr on GitHub. ## 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](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/session/prompt.ts#L324), the `loop()` function calls `Provider.getModel()` without a try/catch: ```typescript // 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](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/session/prompt.ts#L278) calls `cancel(sessionID)`, which [sets the session status to `"idle"` at line 259](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/session/prompt.ts#L259) — but no `session.error` event is published. Since `prompt_async` ([`packages/opencode/src/server/routes/session.ts` line 765](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/server/routes/session.ts#L765)) doesn't `await` the prompt, the exception becomes an unhandled promise rejection: ```typescript // 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](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/session/prompt.ts#L1710-L1721) correctly catches `ModelNotFoundError` and publishes `Session.Event.Error`: ```typescript // 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](https://github.com/anomalyco/opencode/blob/19b1222cd85060f7b0999b99586e93f84821b94b/packages/opencode/src/session/prompt.ts#L324) in a similar try/catch that publishes `Session.Event.Error` before re-throwing: ```typescript 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)
yindo closed this issue 2026-02-16 18:11:02 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 8, 2026):

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

  • #8460: API doesn't report invalid model errors via event stream (same root cause: Provider.getModel() error in loop not publishing session.error event)

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

@github-actions[bot] commented on GitHub (Feb 8, 2026): This issue might be a duplicate of existing issues. Please check: - #8460: API doesn't report invalid model errors via event stream (same root cause: Provider.getModel() error in loop not publishing session.error event) Feel free to ignore if none of these address your specific case.
Author
Owner

@ColeMurray commented on GitHub (Feb 8, 2026):

confirming dupe of: https://github.com/anomalyco/opencode/issues/8460

@ColeMurray commented on GitHub (Feb 8, 2026): confirming dupe of: https://github.com/anomalyco/opencode/issues/8460
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8858