Skip to content

fix(opencode): report invalid agent/mode configs instead of crashing or dropping them#29635

Closed
EClinick wants to merge 1 commit into
anomalyco:devfrom
EClinick:fix/config-agent-mode-error-handling
Closed

fix(opencode): report invalid agent/mode configs instead of crashing or dropping them#29635
EClinick wants to merge 1 commit into
anomalyco:devfrom
EClinick:fix/config-agent-mode-error-handling

Conversation

@EClinick

Copy link
Copy Markdown

Fixes #27133

Both config loaders in packages/opencode/src/config/agent.ts handled an invalid file inconsistently:

  • load() (agents) called ConfigParse.schema(...), which throws. One bad file aborted the whole load, so no agents loaded and startup failed with ConfigInvalidError.
  • loadMode() (modes) used Schema.decodeUnknownExit and skipped invalid entries with no else, so a bad mode was silently dropped with no error.

Now both decode with decodeUnknownExit, and on failure they log, publish a Session.Event.Error, and skip just the bad file while loading the rest. I pulled the shared report-and-skip logic into a small helper, which also removes the duplicated .catch block the two functions had.

How I verified

  • bun typecheck (opencode package): clean.
  • oxlint on both changed files: no new warnings.
  • Added two regression tests in test/config/config.test.ts. Both fail on the current code (agent test throws ConfigInvalidError; mode test fails the "error was reported" assertion) and pass with the fix.
  • bun test test/config test/agent: passing.
  • Manually: ran bun dev <dir> against a dir with a valid + invalid file in both agent/ and mode/. Before, startup crashed; after, opencode starts, the valid entries load, and the invalid ones are reported instead of crashing or vanishing.

…or dropping them

An invalid file in agent/ threw ConfigInvalidError and aborted the whole
load (no agents loaded); the same file in mode/ was silently dropped with
no error. Both loaders now log, publish a session error, and skip just the
bad file while loading the rest.

Fixes anomalyco#27133
Copilot AI review requested due to automatic review settings May 27, 2026 22:58
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label May 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Found one potentially related PR:

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

This PR appears to address similar config parsing error handling during startup. It may be worth checking if it overlaps with PR #29635's approach to handling invalid agent/mode configs gracefully.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Improves error handling when loading agent and mode markdown configuration files so that invalid files are reported (via log + Bus event) instead of either crashing the entire load or being silently dropped.

Changes:

  • Extracted a shared report helper in agent.ts for surfacing load failures via log and Bus event.
  • Replaced ConfigParse.schema with Schema.decodeUnknownExit in load() and added failure reporting in both load() and loadMode().
  • Added two tests verifying that invalid agent/mode files are reported and don't block sibling valid files.

Reviewed changes

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

File Description
packages/opencode/src/config/agent.ts Centralizes error reporting and avoids crashing/silent-drop on invalid files.
packages/opencode/test/config/config.test.ts Adds test coverage for invalid agent/mode file reporting behavior.

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

Comment on lines +113 to +115
await Promise.resolve()
.then(() => Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() }))
.catch(() => {})
Comment on lines +139 to +144
const parsed = Schema.decodeUnknownExit(Info)(config, { errors: "all", propertyOrder: "original" })
if (Exit.isFailure(parsed)) {
await report("agent", item, `Failed to parse agent ${item}`, parsed.cause)
continue
}
result[config.name] = parsed.value
Comment on lines +923 to +929
Bus.subscribe(Session.Event.Error, (evt) => {
const data = evt.properties.error?.data
if (data && "message" in data && typeof data.message === "string") errors.push(data.message)
})
await Bun.sleep(10)
const config = await load()
await Bun.sleep(10)
// publish failure is swallowed — the log line is the durable record, the event is best-effort.
async function report(label: string, item: string, message: string, err: unknown) {
log.error(`failed to load ${label}`, { [label]: item, err })
const { Session } = await import("@/session/session")
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 28, 2026
@github-actions github-actions Bot closed this May 28, 2026
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.

[BUG]: Invalid agent/mode config files: agent crashes startup, mode silently disappears

2 participants