Skip to content

models.json cache corruption causes intermittent startup failure for config.providers and provider.list #30813

@AlexPawlicki

Description

@AlexPawlicki

Bug

opencode fails to start intermittently with:

Error: 2 of 5 requests failed: Unexpected server error. Check server logs for details.
Affected startup requests: config.providers, provider.list

Root Cause (from server logs)

The actual error in the server log is:

ERROR service=server error=JSON Parse error: Unexpected EOF cause=SyntaxError: JSON Parse error: Unexpected EOF
    at ModelsDev.populate
    at Provider.list
    at ConfigHttpApi.providers

~/.cache/opencode/models.json gets corrupted (truncated/empty) if opencode is terminated while writing an updated model catalog to that file. On the next launch, ModelsDev.populate fails to parse the truncated JSON, which causes both config.providers and provider.list to fail, producing the "Unexpected server error" shown to the user.

Steps to Reproduce

  1. While opencode is running, hard-kill the process (e.g. close terminal, kill PID, machine sleep/wake) during a model catalog refresh
  2. Re-launch opencode — it fails with the error above

Expected Behaviour

opencode should handle a corrupted/missing models.json gracefully by re-downloading the model catalog rather than failing startup.

Suggested Fix

Write the updated catalog to a temp file and atomically rename it to models.json, so a mid-write kill cannot leave a partially-written file. Also add a fallback: if readJson throws a parse error on the cache file, delete it and re-fetch rather than propagating the error up to the startup requests.

Environment

  • Platform: Windows 11 (win32)
  • Shell: PowerShell 5.1
  • opencode: 1.15.13

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions