@devries-cameron20 / t3code-1 / commits / e518866d28
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): OpenCode stop no longer hangs when a turn is interrupted before submission (#12003)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: shivam <91240327+shivamhwp@users.noreply.github.com>

Dara Adedeji committed Sep 28, 2026 at 21:12 UTC e518866d28603da273a9357516dd1aef919f1353
2 files changed +63 -1
apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
+51
@@ -2,6 +2,7 @@ import * as NodeAssert from "node:assert/strict";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import { it } from "@effect/vitest";
4 import * as Cause from "effect/Cause";
5 +import * as Clock from "effect/Clock";
6 import * as Context from "effect/Context";
7 import * as Crypto from "effect/Crypto";
8 import * as Deferred from "effect/Deferred";
@@ -781,6 +782,56 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
782 }),
783 );
784
785 + it.effect(
786 + "stopSession completes after sendTurn is interrupted before the prompt is submitted",
787 + () =>
788 + Effect.gen(function* () {
789 + const adapter = yield* OpenCodeAdapter;
790 + const threadId = asThreadId("thread-opencode-interrupted-before-submit");
791 + runtimeMock.state.createdSessionIds.push("ses_interrupted_before_submit");
792 + yield* adapter.startSession({
793 + provider: ProviderDriverKind.make("opencode"),
794 + threadId,
795 + runtimeMode: "full-access",
796 + });
797 +
798 + const baseClock = yield* Clock.clockWith(Effect.succeed);
799 + const reached = yield* Deferred.make<void>();
800 + const release = yield* Deferred.make<void>();
801 + let calls = 0;
802 + const gatedClock: Clock.Clock = {
803 + ...baseClock,
804 + currentTimeMillisUnsafe: () => baseClock.currentTimeMillisUnsafe(),
805 + currentTimeMillis: Effect.suspend(() => {
806 + calls += 1;
807 + return calls === 2
808 + ? Deferred.succeed(reached, undefined).pipe(
809 + Effect.andThen(Deferred.await(release)),
810 + Effect.andThen(baseClock.currentTimeMillis),
811 + )
812 + : baseClock.currentTimeMillis;
813 + }),
814 + };
815 +
816 + const sendFiber = yield* adapter
817 + .sendTurn({
818 + threadId,
819 + input: "This prompt must not be submitted",
820 + modelSelection: createModelSelection(
821 + ProviderInstanceId.make("opencode"),
822 + "opencode/kimi-k3",
823 + ),
824 + })
825 + .pipe(Effect.provideService(Clock.Clock, gatedClock), Effect.exit, Effect.forkChild);
826 + yield* Deferred.await(reached);
827 + yield* Fiber.interrupt(sendFiber);
828 +
829 + const stopExit = yield* adapter.stopSession(threadId).pipe(Effect.exit);
830 + NodeAssert.equal(Exit.isSuccess(stopExit), true);
831 + NodeAssert.equal(yield* adapter.hasSession(threadId), false);
832 + }),
833 + );
834 +
835 it.effect("aborts a held teardown request before closing the session scope", () =>
836 Effect.gen(function* () {
837 const adapter = yield* OpenCodeAdapter;
apps/server/src/provider/Layers/OpenCodeAdapter.ts
+12 -1
@@ -3531,7 +3531,18 @@ export function makeOpenCodeAdapter(
3531 ? { resumeCursor: context.session.resumeCursor }
3532 : {}),
3533 };
3534 - }),
3534 + }).pipe(
3535 + // stopSession waits on submissionSettled; an interrupted sendTurn
3536 + // must not leave it pending.
3537 + Effect.ensuring(
3538 + Effect.suspend(() => {
3539 + const admission = context.promptAdmission;
3540 + return admission
3541 + ? Deferred.succeed(admission.submissionSettled, undefined).pipe(Effect.ignore)
3542 + : Effect.void;
3543 + }),
3544 + ),
3545 + ),
3546 );
3547 });
3548