Skip to content

fix(config): catch parse errors gracefully during startup#29208

Draft
HaleTom wants to merge 32 commits into
anomalyco:devfrom
HaleTom:json-errors
Draft

fix(config): catch parse errors gracefully during startup#29208
HaleTom wants to merge 32 commits into
anomalyco:devfrom
HaleTom:json-errors

Conversation

@HaleTom
Copy link
Copy Markdown

@HaleTom HaleTom commented May 25, 2026

Issue for this PR

Closes #29200

Closes #29041

Type of change

  • Bug fix

What does this PR do?

Invalid JSON or JSONC in opencode.jsonc produces "Unexpected server error" on startup. ConfigParse.jsonc() throws JsonError and ConfigParse.schema() throws InvalidError as plain exceptions, which become Effect defects inside Effect.gen and propagate as generic HTTP 500s.

Wraps the parsing/schema-validation steps in Effect.sync with Effect.catchCause (the correct operator for catching sync-thrown defects inside Effect.gen). On error, logs the cause server-side and returns an empty {} config via Schema.decodeSync, so the server starts with defaults.

How did you verify your code works?

  • 5 new tests: invalid JSON, invalid JSONC (unterminated), invalid schema fields, multi-file partial failure, agent config still loads
  • All existing config tests pass (83 pass, 0 fail)
  • Tested with copilot review across all changes

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

HaleTom added 2 commits May 25, 2026 18:41
Wrap ConfigParse.jsonc() and ConfigParse.effectSchema() calls in
loadConfig with Effect.catchCause to prevent sync-thrown JsonError
and InvalidError from becoming Effect defects. On parse/schema
failure, the bad file is skipped with a log.error and {} fallback
instead of crashing, matching the existing tui.jsonc error handling
pattern in tui.ts.

Without this, any invalid JSONC syntax or schema violation in
opencode.json/opencode.jsonc causes "4 of 6 requests failed:
Unexpected server error" on startup. Now the server starts with
default values and logs the config error details.

Fixes anomalyco#29200
Copilot AI review requested due to automatic review settings May 25, 2026 12:22
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 25, 2026
@HaleTom
Copy link
Copy Markdown
Author

HaleTom commented May 25, 2026

@gemini-code-review pls review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates config loading to be resilient to malformed config input (schema/JSON/JSONC) by logging errors and falling back to defaults, with accompanying test adjustments.

Changes:

  • Wrap config expansion/parsing/schema validation (and related steps) in error handling that logs and returns an empty config object.
  • Update tests to expect successful config resolution (no crash) for invalid schema and invalid JSON.
  • Add tests for invalid JSONC and for merging when one config file is bad.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/opencode/src/config/config.ts Adds catch-and-fallback behavior during config variable substitution, JSONC parsing, schema validation, plugin resolution, and schema injection/write-back.
packages/opencode/test/config/config.test.ts Reworks negative tests to assert non-crashing behavior and adds coverage for bad JSONC and multi-file merge behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/opencode/test/config/config.test.ts
Comment thread packages/opencode/src/config/config.ts
Comment thread packages/opencode/src/config/config.ts Outdated
Comment thread packages/opencode/test/config/config.test.ts Outdated
@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels May 25, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@HaleTom HaleTom requested a review from Copilot May 25, 2026 13:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread packages/opencode/src/config/config.ts Outdated
Comment thread packages/opencode/src/config/config.ts
Comment thread packages/opencode/test/config/config.test.ts
@HaleTom HaleTom requested a review from Copilot May 25, 2026 18:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment thread packages/opencode/src/config/config.ts
Comment thread packages/opencode/src/config/config.ts Outdated
Comment thread packages/opencode/src/config/config.ts
Comment thread packages/opencode/test/config/config.test.ts
HaleTom added 2 commits May 26, 2026 01:58
- Strip unknown config keys before schema validation to preserve valid fields
- Sanitize log.error calls to avoid leaking config content via cause
- Handle plugin resolution per-item to avoid partial mutation on failure
- Add test for plugin resolution failure
@HaleTom HaleTom marked this pull request as draft May 25, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid JSON/C causes "Unexpected server error" on startup webfetch permission with object syntax crashes server on startup

5 participants