@devries-cameron20 / t3code-1 / commits / 6e6b6a7365
Fork 0

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

fix(opencode): stopped sessions can fork after a lost end event (#24552)

Julius Marminge committed Nov 11, 2026 at 15:50 UTC 6e6b6a7365494016650a11457d913e574db1243b
2 files changed +101 -9
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+72
@@ -5672,6 +5672,78 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
5672 }).pipe(Effect.scoped),
5673 );
5674
5675 + // regression: #24549 a Stop whose interrupted event never arrives blocked the next fork
5676 + it.effect("forks a stopped session whose interrupted event never arrived", () =>
5677 + Effect.gen(function* () {
5678 + const FORK = "ses_f1484db83ffeLGtrRCFimo1H0e";
5679 + const { runtime, thread } = yield* resumed([
5680 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
5681 + promptAccepted,
5682 + event("session.execution.started", { sessionID: SESSION }),
5683 + out("session.interrupt", { sessionID: SESSION }),
5684 + reply("session.interrupt", { interrupted: true }),
5685 + out("session.active"),
5686 + reply("session.active", { data: {} }),
5687 + out("session.fork", { sessionID: SESSION }),
5688 + replyData("session.fork", sessionInfo({ id: FORK })),
5689 + out("session.update", { sessionID: FORK, permissions: "<any>" }),
5690 + reply("session.update", null),
5691 + out("session.active"),
5692 + reply("session.active", { data: {} }),
5693 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
5694 + promptAccepted,
5695 + event("session.execution.interrupted", { sessionID: SESSION }),
5696 + event("session.execution.started", { sessionID: SESSION }),
5697 + event("session.execution.failed", {
5698 + sessionID: SESSION,
5699 + error: { type: "provider", message: "second turn failed" },
5700 + }),
5701 + ]);
5702 + const ended = yield* terminals(runtime, 2);
5703 + yield* runtime.startTurn(turnInput(thread));
5704 + yield* runtime.interruptTurn({
5705 + providerThread: thread,
5706 + providerTurnId: yield* providerTurnId,
5707 + });
5708 + const forked = yield* runtime.forkThread({
5709 + sourceProviderThread: thread,
5710 + targetThreadId: ThreadId.make("thread:opencode2-adapter:fork"),
5711 + });
5712 + assert.equal(forked.nativeThreadRef?.nativeId, FORK);
5713 + // The lost end arriving late must not finish the source's next turn.
5714 + yield* runtime.startTurn(secondTurn(thread));
5715 + const [first, second] = yield* Fiber.join(ended);
5716 + assert.equal(first?.status, "interrupted");
5717 + assert.equal(second?.failure?.message, "second turn failed");
5718 + }).pipe(Effect.scoped),
5719 + );
5720 +
5721 + it.effect("refuses to fork a stopped session OpenCode still runs", () =>
5722 + Effect.gen(function* () {
5723 + const { runtime, thread } = yield* resumed([
5724 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
5725 + promptAccepted,
5726 + event("session.execution.started", { sessionID: SESSION }),
5727 + out("session.interrupt", { sessionID: SESSION }),
5728 + reply("session.interrupt", { interrupted: true }),
5729 + out("session.active"),
5730 + reply("session.active", { data: { [SESSION]: { type: "running" } } }),
5731 + ]);
5732 + yield* runtime.startTurn(turnInput(thread));
5733 + yield* runtime.interruptTurn({
5734 + providerThread: thread,
5735 + providerTurnId: yield* providerTurnId,
5736 + });
5737 + const refused = yield* runtime
5738 + .forkThread({
5739 + sourceProviderThread: thread,
5740 + targetThreadId: ThreadId.make("thread:opencode2-adapter:fork"),
5741 + })
5742 + .pipe(Effect.flip);
5743 + assert.equal(refused._tag, "ProviderAdapterProtocolError");
5744 + }).pipe(Effect.scoped),
5745 + );
5746 +
5747 it.effect("moves a fork into its target thread's worktree before the first prompt", () =>
5748 Effect.gen(function* () {
5749 const FORK = "ses_f1484db83ffeLGtrRCFimo1H0e";
packages/provider-opencode/src/server/v2/adapter.ts
+29 -9
@@ -331,6 +331,8 @@ interface ThreadState {
331 unsettled: boolean;
332 /** A late first start belongs to the failed prompt; only a subsequent start proves a lost end. */
333 unsettledStarted: boolean;
334 + /** Execution starts seen; lets a check that awaited OpenCode notice a run that began meanwhile. */
335 + starts: number;
336 directory: string;
337 agent: string;
338 title: string | undefined;
@@ -827,6 +829,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
829 model: undefined,
830 unsettled: false,
831 unsettledStarted: false,
832 + starts: 0,
833 directory,
834 agent: "build",
835 title: undefined,
@@ -3076,6 +3079,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3079 ) {
3080 // Executions are serial: a second start proves the unsettled run lost its end.
3081 const state = threads.get(sessionId);
3082 + if (state !== undefined) state.starts += 1;
3083 if (state?.unsettled === true) {
3084 if (state.unsettledStarted) state.unsettled = false;
3085 else state.unsettledStarted = true;
@@ -3825,21 +3829,37 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3829 verb: "roll back" | "fork",
3830 ) {
3831 const state = threads.get(sessionId);
3832 + // A stopped turn whose end was lost with the stream stays active here,
3833 + // so OpenCode's own list decides whether it still runs.
3834 + const stopped = state?.active?.interrupted === true ? state.active : undefined;
3835 + const starts = state?.starts;
3836 + const stillWorking = new ProviderAdapter.ProviderAdapterProtocolError({
3837 + driver: OPENCODE_PROVIDER,
3838 + detail: `Cannot ${verb} OpenCode session ${sessionId} while it is still working`,
3839 + });
3840 const busyHere =
3841 state !== undefined &&
3830 - (state.active !== undefined ||
3831 - busy.has(sessionId) ||
3842 + ((stopped === undefined && (state.active !== undefined || busy.has(sessionId))) ||
3843 (verb === "roll back" && hasBackground(state)));
3844 const busyThere =
3845 !busyHere &&
3835 - (state === undefined || state.unsettled) &&
3846 + (state === undefined || state.unsettled || stopped !== undefined) &&
3847 sessionId in (yield* client.session.active().pipe(Effect.timeout(ACTIVE_CHECK_TIMEOUT)));
3837 - if (busyHere || busyThere) {
3838 - return yield* new ProviderAdapter.ProviderAdapterProtocolError({
3839 - driver: OPENCODE_PROVIDER,
3840 - detail: `Cannot ${verb} OpenCode session ${sessionId} while it is still working`,
3841 - });
3842 - }
3848 + if (busyHere || busyThere) return yield* stillWorking;
3849 + if (state === undefined || stopped === undefined) return;
3850 + yield* lock.withPermit(
3851 + Effect.gen(function* () {
3852 + // A run that started during the check may postdate OpenCode's answer.
3853 + if (state.starts !== starts) return yield* stillWorking;
3854 + if (state.active !== stopped) return;
3855 + // Its end may still arrive late; unsettled swallows it and makes the next turn await its start.
3856 + // OpenCode reported it idle, so any later start is a new run.
3857 + state.unsettled = true;
3858 + state.unsettledStarted = true;
3859 + busy.delete(sessionId);
3860 + yield* finishTurn(state, { status: "interrupted" });
3861 + }),
3862 + );
3863 });
3864
3865 const cancelStrandedSteers = Effect.fnUntraced(function* (state: ThreadState) {