fix(server): make OpenCode Stop button actually stop the session #8813
main fromrefs/pull/8813/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- apps/server/src/provider/Layers/OpenCodeAdapter.ts line 1563OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1560,3 +1560,4 @@15601560 const interruptTurn: OpenCodeAdapterShape["interruptTurn"] = Effect.fn("interruptTurn")(15611561 function* (threadId, turnId) {15621562 const context = yield* ensureSessionContext(sessions, threadId);1563+ // Best-effort abort: even if the remote abort fails we still want to
- apps/server/src/provider/Layers/OpenCodeAdapter.ts lines 1570 to 1570OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1560,7 +1560,11 @@15601560 const interruptTurn: OpenCodeAdapterShape["interruptTurn"] = Effect.fn("interruptTurn")(15611561 function* (threadId, turnId) {15621562 const context = yield* ensureSessionContext(sessions, threadId);1563+ // Best-effort abort: even if the remote abort fails we still want to1564+ // clear the local active-turn state so the session does not stay1565+ // stuck in "running" (observed: turn "interrupted" but session still1566+ // "running" with same activeTurnId, leaving Stop unusable).15631567 yield* runOpenCodeSdk("session.abort", () =>15641568 context.client.session.abort({ sessionID: context.openCodeSessionId }),1565- ).pipe(Effect.mapError(toRequestError));1566- if (turnId ?? context.activeTurnId) {1569+ ).pipe(1570+ Effect.catchAll((cause) =>
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -2937,7 +2898,14 @@2937- new ProviderAdapterRequestError({2938- provider: PROVIDER,2939- method: "session.abort",2940- detail: "OpenCode session abort did not complete within 10 seconds.",2941- cause,2942- }),2943- ),2898+ // Snapshot the active turn before the first yield: if a new turn2899+ // starts while session.abort is awaiting, we must not emit/abort or2900+ // clear that newer turn (observed race: turn "interrupted" but session2901+ // still "running" with same activeTurnId left Stop unusable).2902+ const snapshotActiveTurnId = context.activeTurnId;2903+ const snapshotActiveAgent = context.activeAgent;2904+ const snapshotActiveVariant = context.activeVariant;2905+ void snapshotActiveAgent;2906+ void snapshotActiveVariant;2907+ const targetTurnId = turnId ?? snapshotActiveTurnId;2908+ // Best-effort abort: even if the remote abort fails we still want to2909+ // clear the local active-turn state so the session does not stay2910+ // stuck in "running".2911+ yield* runOpenCodeSdk("session.abort", () =>@@ -2984,9 +2924,13 @@2924+ threadId,2925+ turnId: targetTurnId,2926+ })),2927+ type: "turn.aborted",2928+ payload: {2929+ reason: "Interrupted by user.",2930+ },2931+ });29842932 }2985-2986- if (context.cancellation === cancellation) {2987- if (cancellation.turnId !== undefined) {2988- yield* interruptOpenCodeTurn(context, cancellation.turnId);2989- } else {2990- context.cancellation = undefined;2991- context.reconcileIdleStatus = true;2992- }2933+ // Clear only if the active turn still matches the snapshot we2934+ // interrupted — a newer turn may have started while abort was in2935+ // flight and must not be cleared.2936+ if (context.activeTurnId !== undefined && context.activeTurnId === snapshotActiveTurnId) {
- apps/server/src/provider/Layers/OpenCodeAdapter.ts lines 2911 to 2912OutdatedResolvedby Mina-Sayed(GitHub profile, opens in a new tab)@@ -2938,6 +2898,15 @@2938- provider: PROVIDER,2939- method: "session.abort",2940- detail: "OpenCode session abort did not complete within 10 seconds.",2941- cause,2942- }),2943- ),2898+ // Snapshot the active turn before the first yield: if a new turn2899+ // starts while session.abort is awaiting, we must not emit/abort or2900+ // clear that newer turn (observed race: turn "interrupted" but session2901+ // still "running" with same activeTurnId left Stop unusable).2902+ const snapshotActiveTurnId = context.activeTurnId;2903+ const snapshotActiveAgent = context.activeAgent;2904+ const snapshotActiveVariant = context.activeVariant;2905+ void snapshotActiveAgent;2906+ void snapshotActiveVariant;2907+ const targetTurnId = turnId ?? snapshotActiveTurnId;2908+ // Best-effort abort: even if the remote abort fails we still want to2909+ // clear the local active-turn state so the session does not stay2910+ // stuck in "running".2911+ yield* runOpenCodeSdk("session.abort", () =>2912+ context.client.session.abort({ sessionID: context.openCodeSessionId }),@@ -247,5 +243,5 @@247243 },248244 abort: async ({ sessionID }: { sessionID: string }, options?: { signal?: AbortSignal }) => {249245 runtimeMock.state.abortCalls.push(sessionID);250- if (options?.signal) {251- runtimeMock.state.abortSignals.push(options.signal);246+ if (runtimeMock.state.abortError) {247+ throw runtimeMock.state.abortError;@@ -65,7 +65,4 @@6565 createdSessionIds: [] as string[],6666 authHeaders: [] as Array<string | null>,6767 abortCalls: [] as string[],68- abortSignals: [] as AbortSignal[],69- abortImplementation: null as70- | ((sessionID: string, signal?: AbortSignal) => Promise<void>)71- | null,68+ abortError: null as Error | null,@@ -2932,12 +2898,9 @@2932- Effect.timeout("10 seconds"),2933- Effect.catchTags({2934- OpenCodeRuntimeError: (cause) => Effect.fail(toRequestError(cause)),2935- TimeoutError: (cause) =>2936- Effect.fail(2937- new ProviderAdapterRequestError({2938- provider: PROVIDER,2939- method: "session.abort",2940- detail: "OpenCode session abort did not complete within 10 seconds.",2941- cause,2942- }),2943- ),2898+ // Snapshot the active turn before the first yield: if a new turn2899+ // starts while session.abort is awaiting, we must not emit/abort or2900+ // clear that newer turn (observed race: turn "interrupted" but session2901+ // still "running" with same activeTurnId left Stop unusable).2902+ const snapshotActiveTurnId = context.activeTurnId;2903+ const snapshotActiveAgent = context.activeAgent;2904+ const snapshotActiveVariant = context.activeVariant;2905+ void snapshotActiveAgent;2906+ void snapshotActiveVariant;
- apps/server/src/provider/Layers/OpenCodeAdapter.ts line 2941OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2921,21 +2897,0 @@2921- promptAdmission.cancelled = true;2922- if (promptAdmission.promptFiber) {2923- yield* Fiber.interrupt(promptAdmission.promptFiber);2924- }2925- yield* Deferred.await(promptAdmission.submissionSettled);2926- }2927-2928- const abortOutcome = yield* Effect.raceFirst(2929- runOpenCodeSdk("session.abort", (signal) =>2930- context.client.session.abort({ sessionID: context.openCodeSessionId }, { signal }),2931- ).pipe(2932- Effect.timeout("10 seconds"),2933- Effect.catchTags({2934- OpenCodeRuntimeError: (cause) => Effect.fail(toRequestError(cause)),2935- TimeoutError: (cause) =>2936- Effect.fail(2937- new ProviderAdapterRequestError({2938- provider: PROVIDER,2939- method: "session.abort",2940- detail: "OpenCode session abort did not complete within 10 seconds.",2941- cause,apps/server/src/provider/Layers/OpenCodeAdapter.ts line 2940OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2984,9 +2928,13 @@2928+ payload: {2929+ reason: "Interrupted by user.",2930+ },2931+ });29842932 }2985-2986- if (context.cancellation === cancellation) {2987- if (cancellation.turnId !== undefined) {2988- yield* interruptOpenCodeTurn(context, cancellation.turnId);2989- } else {2990- context.cancellation = undefined;2991- context.reconcileIdleStatus = true;2992- }2933+ // Clear only if the active turn still matches the snapshot we2934+ // interrupted — a newer turn may have started while abort was in2935+ // flight and must not be cleared.2936+ if (context.activeTurnId !== undefined && context.activeTurnId === snapshotActiveTurnId) {2937+ context.activeTurnId = undefined;2938+ context.activeAgent = undefined;2939+ context.activeVariant = undefined;2940+ yield* updateProviderSession(context, { status: "ready" }, { clearActiveTurnId: true });
Mina-Sayed(GitHub profile, opens in a new tab) closed this
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
Mina-Sayed(GitHub profile, opens in a new tab) reopened this
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XS
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
85b5cf0a-81f7-4a95-bff0-8031dc3f175cYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Warning
Your free Security trial is over. An organization admin can activate Security or dismiss this notice.
Comment `@coderabbitai help` to get the list of available commands.