Skip to content

fix(run): set non-zero exit code on session error#26588

Open
cokekitten wants to merge 1 commit intoanomalyco:devfrom
cokekitten:fix/26509-exit-code-on-session-error
Open

fix(run): set non-zero exit code on session error#26588
cokekitten wants to merge 1 commit intoanomalyco:devfrom
cokekitten:fix/26509-exit-code-on-session-error

Conversation

@cokekitten
Copy link
Copy Markdown

@cokekitten cokekitten commented May 9, 2026

Issue for this PR

Closes #26509

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

opencode run -m <invalid-model> "<prompt>" printed the error but exited 0,
breaking shell scripts that key off $?. Without a prompt the same command
exits 1, so the bug only hits the prompt path.

In run.ts, the non-interactive path runs loop() as fire-and-forget. When a
session.error event arrives, the handler stores the message locally and prints
it — but never propagates it to the process exit code. After the session goes
idle the loop breaks, the event subscription closes, the event loop drains, and
Node exits 0.

Fix: set process.exitCode = 1 in the session.error branch. Same pattern used
elsewhere in the CLI for soft-failure exits (thread.ts:126,222,
attach.ts:54,81). Using exitCode rather than process.exit(1) lets the loop
finish, the subscription tear down, and stdout flush before Node exits — relevant
for --format json consumers.

Refs older duplicates that were never actually closed by a fix: #15558, #17854,
#2489. PR #15787 took a different approach (await loopDone then process.exit(1))
but was auto-closed by the template-compliance bot before any human review.

How did you verify your code works?

Manual repro on dev tree (also reproduces on installed 1.14.41):

```
$ bun --conditions=browser packages/opencode/src/index.ts run -m invalid-model "test"
Error: Model not found: invalid-model/
$ echo $?
1 # was 0 before the fix
```

No-prompt path still exits `1` (no regression):

```
$ bun --conditions=browser packages/opencode/src/index.ts run -m invalid-model </dev/null
Error: You must provide a message or a command
$ echo $?
1
```

`bun run typecheck` passes; `bunx oxlint` reports no new warnings.

Screenshots / recordings

N/A — CLI exit code change.

Checklist

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

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

Based on my search results, I found one related PR that addresses a similar issue:

Potential Related PR:

The current PR (26588) appears to be addressing a specific edge case where session.error events weren't propagating to the process exit code in the non-interactive/prompt path, whereas PR #14625 may have handled a broader set of error scenarios. These could be complementary fixes or addressing different aspects of the same underlying issue.

@cokekitten
Copy link
Copy Markdown
Author

For visibility: #14625 (open since 2026-02-22) takes a different approach to the same bug — it converts loop() into an awaitable, awaits it after the prompt is submitted, and then sets process.exitCode based on the outer error variable. Both fixes produce the same end behavior; this one is smaller (+1 line) and doesn't change the control flow. Happy to defer if maintainers prefer the other approach.

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.

opencode run exits with status 0 for invalid model when prompt is provided BUG: exit code when an invalid model is passed

1 participant