@devries-cameron20 / t3code Mirror · read-only / commits / 6dc7fdc8e8
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

fix(opencode): owned servers no longer fail turns after five stalled event subscriptions (#39134)

Julius Marminge committed Nov 19, 2026 at 06:26 UTC 6dc7fdc8e8303c32e4b8a9c2d644fd51e02c6cc6
2 files changed +51 -10
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+34 -1
@@ -3602,6 +3602,38 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3602 }),
3603 );
3604
3605 + // regression: #39120 a live owned server failed after five stalled event subscriptions
3606 + it.effect("keeps owned work through more than five stalled event subscriptions", () =>
3607 + Effect.gen(function* () {
3608 + let subscriptions = 0;
3609 + const [exit, ...restored] = reconnected({}, "succeeded");
3610 + const { runtime, thread } = yield* resumed(
3611 + [
3612 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3613 + promptAccepted,
3614 + exit!,
3615 + ...Array.from({ length: 5 }, () => out("event.subscribe")),
3616 + ...restored,
3617 + ],
3618 + {
3619 + beforeResponse: (path) =>
3620 + path === "/api/event"
3621 + ? Effect.suspend(() => {
3622 + subscriptions += 1;
3623 + return subscriptions >= 2 && subscriptions <= 6 ? Effect.never : Effect.void;
3624 + })
3625 + : Effect.void,
3626 + },
3627 + );
3628 + const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3629 + yield* runtime.startTurn(turnInput(thread));
3630 + // Each stalled subscription waits out its 10s deadline before the next attempt.
3631 + for (let attempt = 0; attempt < 8; attempt++) yield* TestClock.adjust("15 seconds");
3632 + assert.equal((yield* Fiber.join(terminal))?.status, "completed");
3633 + assert.equal(subscriptions, 7);
3634 + }),
3635 + );
3636 +
3637 it.effect("reconciles a session deleted after the active snapshot", () =>
3638 Effect.gen(function* () {
3639 const otherId = "ses_other_stalled_session";
@@ -4769,7 +4801,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
4801 event("session.execution.succeeded", { sessionID: SESSION }),
4802 { type: "runtime_exit", status: "success" },
4803 ],
4772 - { borrowers },
4804 + // A live owned server is retried until it answers; only an external one runs out of attempts.
4805 + { borrowers, external: true },
4806 );
4807 const events = yield* runtime.events.pipe(Stream.runCollect, Effect.forkScoped);
4808 yield* runtime.startTurn(withLineage(thread));
packages/provider-opencode/src/server/v2/adapter.ts
+17 -9
@@ -652,6 +652,19 @@ const OWNED_RECOVERY_RETRY = {
652 schedule: OWNED_RECONNECT_SCHEDULE,
653 while: (error: unknown) => !isPermanentRecoveryError(error),
654 };
655 +// Retries an owned server's request until it answers or the process exits.
656 +const whileAlive = <A, E, R>(
657 + processExit: Effect.Effect<unknown> | undefined,
658 + effect: Effect.Effect<A, E, R>,
659 +) =>
660 + processExit === undefined
661 + ? effect
662 + : effect.pipe(
663 + Effect.retry(OWNED_RECOVERY_RETRY),
664 + Effect.raceFirst(
665 + processExit.pipe(Effect.andThen(Effect.fail(new OpenCode2ServerExitedError()))),
666 + ),
667 + );
668 class OpenCode2ServerExitedError extends Schema.TaggedError<OpenCode2ServerExitedError>()(
669 "OpenCode2ServerExitedError",
670 {},
@@ -3777,7 +3790,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3790 const scope = yield* Scope.make();
3791 const { next, stream } = yield* Effect.gen(function* () {
3792 const next = yield* borrow.pipe(Effect.provideService(Scope.Scope, scope));
3780 - return { next, stream: yield* next.events.pipe(Effect.provideService(Scope.Scope, scope)) };
3793 + const events = next.events.pipe(Effect.provideService(Scope.Scope, scope));
3794 + return { next, stream: yield* whileAlive(next.processExit, events) };
3795 }).pipe(Effect.onError(() => Scope.close(scope, Exit.void)));
3796 const previous = currentScope;
3797 connection = next;
@@ -3813,15 +3827,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3827 }
3828 // An owned process's exit, rather than a read deadline, bounds recovery; the bounded retry
3829 // below then borrows a restarted server. HTTP waits stay outside the event lock so Stop can still settle.
3816 - const exited = next.processExit;
3817 - yield* exited === undefined
3830 + yield* next.processExit === undefined
3831 ? reconcile().pipe(Effect.timeout(RECONCILE_TIMEOUT))
3819 - : reconcile().pipe(
3820 - Effect.retry(OWNED_RECOVERY_RETRY),
3821 - Effect.raceFirst(
3822 - exited.pipe(Effect.andThen(Effect.fail(new OpenCode2ServerExitedError()))),
3823 - ),
3824 - );
3832 + : whileAlive(next.processExit, reconcile());
3833 return stream;
3834 }).pipe(
3835 // Borrows and process exits stay bounded; only a live owned process is waited on above.