Skip to content

Commit be73f63

Browse files
fix(opencode): report a rejected run prompt exactly once (#25)
A rejected `session.prompt` / `session.command` request and the `session.error` event the server publishes for it are the same failure arriving on two channels. The event loop is already draining when the request returns, so both could report it — or only one, depending on whether the SSE subscription attached before the server published. That made `--format json` stdout carry either one or two `error` records for a single rejected request, which is what flaked `--format json emits a pure error record for a rejected prompt request`. First reporter wins now: the loop skips a session error the request path already reported, and the request path stays silent if the loop got there first. Neither path waits, so anomalyco#27371's prompt nonzero exit is unchanged.
1 parent 2fedd66 commit be73f63

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"opencode": patch
3+
---
4+
5+
Report a rejected `opencode run` prompt exactly once. The request's own error and the `session.error` event the server publishes for it are the same failure on two channels, so the run could print it twice — or emit two `error` records on `--format json` stdout — depending on whether the event subscription attached before the server published. The first reporter now wins and the other stays silent.

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,27 @@ export const RunCommand = effectCmd({
690690
return false
691691
}
692692

693+
// A rejected `session.prompt` / `session.command` request and the
694+
// `session.error` event the server publishes for it are the same
695+
// failure arriving on two channels. The event loop is already draining
696+
// when the request returns, so without coordination the run reports the
697+
// failure twice — nondeterministically, because whether the event
698+
// subscription attaches before the server publishes decides it. First
699+
// reporter wins; the other stays silent, so the run emits exactly one
700+
// error record either way.
701+
let sessionErrorReported = false
702+
let requestErrorReported = false
703+
704+
// The request never got admitted, so the caller returns immediately
705+
// without draining to idle (see #27371 — waiting for an idle that
706+
// never arrives used to hang the process).
707+
function reportRequestError(error: unknown) {
708+
process.exitCode = 1
709+
if (sessionErrorReported) return
710+
requestErrorReported = true
711+
if (!emit("error", { error })) UI.error(formatRunError(error))
712+
}
713+
693714
// Consume one subscribed event stream for the active session and mirror it
694715
// to stdout/UI. `client` is passed explicitly because attach mode may
695716
// rebind the SDK to the session's directory after the subscription is
@@ -776,6 +797,8 @@ export const RunCommand = effectCmd({
776797
if (event.type === "session.error") {
777798
const props = event.properties
778799
if (props.sessionID !== sessionID || !props.error) continue
800+
if (requestErrorReported) continue
801+
sessionErrorReported = true
779802
let err = String(props.error.name)
780803
if ("data" in props.error && props.error.data && "message" in props.error.data) {
781804
err = String(props.error.data.message)
@@ -847,8 +870,7 @@ export const RunCommand = effectCmd({
847870
variant: args.variant,
848871
})
849872
if (result.error) {
850-
if (!emit("error", { error: result.error })) UI.error(formatRunError(result.error))
851-
process.exitCode = 1
873+
reportRequestError(result.error)
852874
return
853875
}
854876
await finish()
@@ -864,8 +886,7 @@ export const RunCommand = effectCmd({
864886
parts: [...files, { type: "text", text: message }],
865887
})
866888
if (result.error) {
867-
if (!emit("error", { error: result.error })) UI.error(formatRunError(result.error))
868-
process.exitCode = 1
889+
reportRequestError(result.error)
869890
return
870891
}
871892
await finish()

0 commit comments

Comments
 (0)