@devries-cameron20 / t3code Mirror · read-only / commits / a135972c7e
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, 18:30 UTC

fix(opencode): preserve owned turns during server recovery (#25596)

Co-authored-by: Tin Zeljar <35663757+Zheruel@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

maria committed Nov 12, 2026 at 22:50 UTC a135972c7e16b05ca3b6346bf65f90033c4fcd35
8 files changed +859 -147
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+543 -1
@@ -17,6 +17,7 @@ import {
17 ProviderTurnId,
18 RunAttemptId,
19 RunId,
20 + type RuntimeRequestId,
21 ThreadId,
22 type ModelSelection,
23 type OrchestrationV2AppThread,
@@ -33,6 +34,7 @@ import * as Fiber from "effect/Fiber";
34 import * as FileSystem from "effect/FileSystem";
35 import * as Layer from "effect/Layer";
36 import * as Option from "effect/Option";
37 +import * as Queue from "effect/Queue";
38 import * as Scope from "effect/Scope";
39 import * as Stream from "effect/Stream";
40 import * as Exit from "effect/Exit";
@@ -328,12 +330,14 @@ const resumed = (
330 readonly goal?: OrchestrationV2ProviderThread["goal"];
331 readonly external?: boolean;
332 readonly supervised?: boolean;
333 + readonly processExit?: Effect.Effect<void>;
334 + readonly beforeResponse?: (path: string) => Effect.Effect<void>;
335 + readonly borrowers?: { current: number };
336 readonly replayGate?: ProviderReplayGate;
337 readonly attachmentsDir?: string;
338 readonly resolveExtractedTextPath?: (path: string) => Effect.Effect<string | undefined>;
339 readonly fileSystem?: FileSystem.FileSystem;
340 readonly onPrompt?: (input: unknown) => void;
336 - readonly borrowers?: { current: number };
341 },
342 ) =>
343 Effect.gen(function* () {
@@ -372,6 +376,10 @@ const resumed = (
376 ...(options?.fileSystem === undefined ? {} : { fileSystem: options.fileSystem }),
377 ...(options?.onPrompt === undefined ? {} : { onPrompt: options.onPrompt }),
378 ...(options?.external === undefined ? {} : { external: options.external }),
379 + ...(options?.processExit === undefined ? {} : { processExit: options.processExit }),
380 + ...(options?.beforeResponse === undefined
381 + ? {}
382 + : { beforeResponse: options.beforeResponse }),
383 ...(options?.replayGate === undefined ? {} : { replayGate: options.replayGate }),
384 ...(options?.attachmentsDir === undefined
385 ? {}
@@ -3264,6 +3272,182 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3272 }).pipe(Effect.scoped),
3273 );
3274
3275 + it.effect("keeps owned work through a stalled reconciliation without releasing its server", () =>
3276 + Effect.gen(function* () {
3277 + const reading = yield* Deferred.make<void>();
3278 + const resume = yield* Deferred.make<void>();
3279 + const borrowers = { current: 0 };
3280 + const { runtime, thread } = yield* resumed(
3281 + [
3282 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3283 + promptAccepted,
3284 + ...reconnected({ [SESSION]: { type: "running" } }),
3285 + ...openRequests([]),
3286 + event("session.execution.succeeded", { sessionID: SESSION }),
3287 + ],
3288 + {
3289 + borrowers,
3290 + beforeResponse: (path) =>
3291 + path.endsWith("/message")
3292 + ? Deferred.succeed(reading, undefined).pipe(Effect.andThen(Deferred.await(resume)))
3293 + : Effect.void,
3294 + },
3295 + );
3296 + const collected: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
3297 + const ended = yield* runtime.events.pipe(
3298 + Stream.tap((event) => Effect.sync(() => collected.push(event))),
3299 + Stream.takeUntil((event) => event.type === "turn.terminal"),
3300 + Stream.runDrain,
3301 + Effect.forkScoped,
3302 + );
3303 + yield* runtime.startTurn(turnInput(thread));
3304 + yield* Deferred.await(reading);
3305 + yield* TestClock.adjust("3 minutes");
3306 + assert.isFalse(collected.some((event) => event.type === "turn.terminal"));
3307 + assert.equal(borrowers.current, 1);
3308 + yield* Deferred.succeed(resume, undefined);
3309 + yield* Fiber.join(ended);
3310 + assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
3311 + }).pipe(Effect.scoped),
3312 + );
3313 +
3314 + it.effect("reconciles a session deleted after the active snapshot", () =>
3315 + Effect.gen(function* () {
3316 + const otherId = "ses_other_stalled_session";
3317 + const otherPrompt = "msg_other_stalled_prompt";
3318 + const history = (sessionID: string, id: string) => [
3319 + out("message.list", { sessionID, order: "desc", limit: "50" }),
3320 + reply("message.list", {
3321 + data: [{ id, time: { created: 1 }, text: "hi", type: "user" }],
3322 + cursor: {},
3323 + }),
3324 + ];
3325 + const requests = (sessionID: string) => [
3326 + out("permission.list", { sessionID }),
3327 + replyData("permission.list", []),
3328 + out("session.form.list", { sessionID }),
3329 + replyData("session.form.list", []),
3330 + ];
3331 + const { runtime, thread } = yield* resumed([
3332 + out("session.get", { sessionID: otherId }),
3333 + replyData("session.get", sessionInfo({ id: otherId })),
3334 + ...requests(otherId),
3335 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3336 + promptAccepted,
3337 + out("session.instructions.entry.put", {
3338 + sessionID: otherId,
3339 + key: "t3-code",
3340 + value: "<any>",
3341 + }),
3342 + reply("session.instructions.entry.put", null),
3343 + out("session.prompt", { sessionID: otherId, id: otherPrompt, text: "<any>" }),
3344 + replyData("session.prompt", {
3345 + id: otherPrompt,
3346 + sessionID: otherId,
3347 + time: { created: 1 },
3348 + type: "user",
3349 + payload: { text: "hi" },
3350 + delivery: "steer",
3351 + }),
3352 + { type: "runtime_exit", status: "success" },
3353 + out("event.subscribe"),
3354 + out("session.active"),
3355 + replyData("session.active", {
3356 + [SESSION]: { type: "running" },
3357 + [otherId]: { type: "running" },
3358 + }),
3359 + out("message.list", { sessionID: SESSION, order: "desc", limit: "50" }),
3360 + reply("message.list", {
3361 + status: 404,
3362 + body: {
3363 + _tag: "SessionNotFoundError",
3364 + sessionID: SESSION,
3365 + message: "Deleted during recovery",
3366 + },
3367 + }),
3368 + ...history(otherId, otherPrompt),
3369 + ...requests(otherId),
3370 + event("session.execution.succeeded", { sessionID: otherId }),
3371 + ]);
3372 + const other = yield* runtime.resumeThread({
3373 + providerThread: {
3374 + ...providerThread(yield* DateTime.now),
3375 + id: ProviderThreadId.make("provider-thread:other"),
3376 + nativeThreadRef: { driver: OPENCODE_PROVIDER, nativeId: otherId, strength: "strong" },
3377 + },
3378 + threadId,
3379 + modelSelection: bigPickle,
3380 + runtimePolicy: policy(),
3381 + });
3382 + const terminals = yield* runtime.events.pipe(
3383 + Stream.filter((event) => event.type === "turn.terminal"),
3384 + Stream.take(2),
3385 + Stream.runCollect,
3386 + Effect.forkScoped,
3387 + );
3388 + yield* runtime.startTurn(turnInput(thread));
3389 + yield* runtime.startTurn({
3390 + ...turnInput(other),
3391 + runId: RunId.make("run:other"),
3392 + attemptId: RunAttemptId.make("attempt:other"),
3393 + });
3394 + const ended = yield* Fiber.join(terminals);
3395 + assert.deepEqual(
3396 + ended.map((event) => event.status),
3397 + ["interrupted", "completed"],
3398 + );
3399 + assert.equal(ended[0]?.threadDisposition, "broken");
3400 + assert.sameMembers(
3401 + ended.map((event) => event.providerThreadId),
3402 + [thread.id, other.id],
3403 + );
3404 + }).pipe(Effect.scoped),
3405 + );
3406 +
3407 + it.effect.each([503, 429])(
3408 + "recovers an owned server after more than five HTTP %s reconciliation replies",
3409 + (status) =>
3410 + Effect.gen(function* () {
3411 + const allowRecovery = yield* Deferred.make<void>();
3412 + const attempts = yield* Queue.unbounded<void>();
3413 + let attempt = 0;
3414 + const { runtime, thread } = yield* resumed(
3415 + [
3416 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3417 + promptAccepted,
3418 + { type: "runtime_exit", status: "success" },
3419 + out("event.subscribe"),
3420 + ...Array.from({ length: 6 }, () => [
3421 + out("session.active"),
3422 + reply("session.active", { status, body: { message: "Recovering" } }),
3423 + ]).flat(),
3424 + ...reconnected({ [SESSION]: { type: "running" } }).slice(2),
3425 + ...openRequests([]),
3426 + event("session.execution.succeeded", { sessionID: SESSION }),
3427 + ],
3428 + {
3429 + beforeResponse: (path) =>
3430 + path === "/api/session/active"
3431 + ? Effect.gen(function* () {
3432 + attempt += 1;
3433 + yield* Queue.offer(attempts, undefined);
3434 + if (attempt === 7) yield* Deferred.await(allowRecovery);
3435 + })
3436 + : Effect.void,
3437 + },
3438 + );
3439 + const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3440 + yield* runtime.startTurn(turnInput(thread));
3441 + for (let retry = 0; retry < 6; retry += 1) {
3442 + yield* Queue.take(attempts);
3443 + yield* TestClock.adjust("30 seconds");
3444 + }
3445 + yield* Queue.take(attempts);
3446 + yield* Deferred.succeed(allowRecovery, undefined);
3447 + assert.equal((yield* Fiber.join(terminal))?.status, "completed");
3448 + }).pipe(Effect.scoped),
3449 + );
3450 +
3451 it.effect(
3452 "keeps a $skill found on the first read when the reread after the scan comes back empty",
3453 () =>
@@ -3298,6 +3482,364 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
3482 }).pipe(Effect.scoped),
3483 );
3484
3485 + it.effect.each([
3486 + {
3487 + server: "an external server keeps rejecting requests",
3488 + external: true,
3489 + status: 503,
3490 + tries: 5,
3491 + },
3492 + { server: "an owned server rejects its credentials", external: false, status: 401, tries: 1 },
3493 + ])("bounds recovery when $server", ({ external, status, tries }) =>
3494 + Effect.gen(function* () {
3495 + const attempts = yield* Queue.unbounded<void>();
3496 + const { runtime, thread } = yield* resumed(
3497 + [
3498 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3499 + promptAccepted,
3500 + { type: "runtime_exit", status: "success" },
3501 + ...Array.from({ length: tries }, () => [
3502 + out("event.subscribe"),
3503 + out("session.active"),
3504 + reply("session.active", { status, body: { message: "Unavailable" } }),
3505 + ]).flat(),
3506 + ],
3507 + {
3508 + external,
3509 + beforeResponse: (path) =>
3510 + path === "/api/session/active"
3511 + ? Queue.offer(attempts, undefined).pipe(Effect.asVoid)
3512 + : Effect.void,
3513 + },
3514 + );
3515 + const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3516 + yield* runtime.startTurn(turnInput(thread));
3517 + for (let attempt = 1; attempt < tries; attempt += 1) {
3518 + yield* Queue.take(attempts);
3519 + yield* TestClock.adjust("2 seconds");
3520 + }
3521 + yield* Queue.take(attempts);
3522 + assert.equal((yield* Fiber.join(terminal))?.status, "failed");
3523 + }).pipe(Effect.scoped),
3524 + );
3525 +
3526 + it.effect.each(["lists", "stream"] as const)(
3527 + "restores approvals and questions from %s after slow recovery",
3528 + (source) =>
3529 + Effect.gen(function* () {
3530 + const gate = makeProviderReplayGate(["late-permission"]);
3531 + let holdReads = false;
3532 + const reading = yield* Deferred.make<void>();
3533 + const resume = yield* Deferred.make<void>();
3534 + const requests = yield* Queue.unbounded<RuntimeRequestId>();
3535 + const { runtime, thread } = yield* resumed(
3536 + [
3537 + out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
3538 + promptAccepted,
3539 + ...reconnected({ [SESSION]: { type: "running" } }),
3540 + ...openRequests(
3541 + source === "lists" ? [shellAsk.data] : [],
3542 + source === "lists" ? [colorForm] : [],
3543 + ),
3544 + ...(source === "stream"
3545 + ? [
3546 + event("permission.asked", shellAsk.data, "late-permission"),
3547 + event("form.created", { form: colorForm }),
3548 + ]
3549 + : []),
3550 + out("permission.reply", {
3551 + sessionID: SESSION,
3552 + requestID: shellAsk.data.id,
3553 + decision: "once",
3554 + }),
3555 + reply("permission.reply", null),
3556 + out("session.form.reply", {
3557 + sessionID: SESSION,
3558 + formID: colorForm.id,
3559 + answer: { q0: "Red" },
3560 + }),
3561 + reply("session.form.reply", null),
3562 + event("session.execution.succeeded", { sessionID: SESSION }),
3563 + ],
3564 + {
3565 + supervised: true,
3566 + replayGate: gate,
3567 + beforeResponse: (path) =>
3568 + holdReads && path.endsWith(source === "stream" ? "/form" : "/message")
3569 + ? Deferred.succeed(reading, undefined).pipe(Effect.andThen(Deferred.await(resume)))
3570 + : Effect.void,
3571 + },
3572 + );
3573 + const ended = yield* Deferred.make<string>();
3574 + yield* runtime.events.pipe(
3575 + Stream.runForEach((event) =>
3576 + Effect.gen(function* () {
3577 + if (
3578 + event.type === "runtime_request.updated" &&
3579 + event.runtimeRequest.status === "pending"
3580 + ) {
3581 + yield* Queue.offer(requests, event.runtimeRequest.id);
3582 + }
3583 + if (event.type === "turn.terminal") {
3584 + yield* Deferred.succeed(ended, event.status);
3585 + }
3586 + }),
3587 + ),
3588 + Effect.forkScoped,
3589 + );
3590 + holdReads = true;
3591 + yield* runtime.startTurn(turnInput(thread, bigPickle, "approval-required"));
3592 + yield* Deferred.await(reading);
3593 + if (source === "stream") {
3594 + yield* Effect.promise(() => gate.waitForReached("late-permission"));
3595 + gate.release("late-permission");
3596 + }
3597 + yield* TestClock.adjust("2 minutes");
3598 + assert.equal(yield* Queue.size(requests), 0);
3599 + yield* Deferred.succeed(resume, undefined);
3600 + yield* runtime.respondToRuntimeRequest({
3601 + requestId: yield* Queue.take(requests),
3602 + decision: "accept",
3603 + });
3604 + yield* runtime.respondToRuntimeRequest({
3605 + requestId: yield* Queue.take(requests),
3606 + answers: { q0: "Red" },
3607 + });
3608 + assert.equal(yield* Deferred.await(ended), "completed");
3609 + }).pipe(Effect.scoped),
3610 + );
3611 +
3612 + it.effect(
3613 + "recovers through a restarted server when the process exits during a history read",
3614 + () =>
3615 + Effect.gen(function* () {
3616 + const reading = yield* Deferred.make<void>();
3617 + const cancelled = yield* Deferred.make<void>();
3618 + let restarted = false;
3619 + let held = false;
3620 + const { runtime, thread } = yield* resumed(
3621 + [
3622 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3623 + promptAccepted,
3624 + ...reconnected({ [SESSION]: { type: "running" } }),
3625 + ...reconnected({}).slice(1),
3626 + ],
3627 + {
3628 + // The replay lends one connection; only the first borrow's process exits.
3629 + processExit: Effect.suspend(() =>
3630 + restarted
3631 + ? Effect.never
3632 + : Effect.sleep("1 second").pipe(
3633 + Effect.tap(() =>
3634 + Effect.sync(() => {
3635 + restarted = true;
3636 + }),
3637 + ),
3638 + ),
3639 + ),
3640 + beforeResponse: (path) =>
3641 + path.endsWith("/message") && !held
3642 + ? Effect.sync(() => (held = true)).pipe(
3643 + Effect.andThen(Deferred.succeed(reading, undefined)),
3644 + Effect.andThen(Effect.never),
3645 + Effect.onInterrupt(() => Deferred.succeed(cancelled, undefined)),
3646 + )
3647 + : Effect.void,
3648 + },
3649 + );
3650 + const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
3651 + yield* runtime.startTurn(turnInput(thread));
3652 + yield* Deferred.await(reading);
3653 + // The exit at 1s cuts the stalled read; the retry 2s later borrows the restarted server.
3654 + yield* TestClock.adjust("3 seconds");
3655 + yield* Deferred.await(cancelled);
3656 + assert.equal((yield* Fiber.join(terminal))?.status, "interrupted");
3657 + assert.equal(runtime.providerSession.status, "ready");
3658 + }).pipe(Effect.scoped),
3659 + );
3660 +
3661 + it.effect("releases a recovering session on shutdown without waiting for its HTTP read", () =>
3662 + Effect.gen(function* () {
3663 + const sessionScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
3664 + Scope.close(scope, Exit.void),
3665 + );
3666 + const reading = yield* Deferred.make<void>();
3667 + const cancelled = yield* Deferred.make<void>();
3668 + const borrowers = { current: 0 };
3669 + const { runtime, thread } = yield* resumed(
3670 + [
3671 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3672 + promptAccepted,
3673 + ...reconnected({ [SESSION]: { type: "running" } }),
3674 + ],
3675 + {
3676 + borrowers,
3677 + beforeResponse: (path) =>
3678 + path.endsWith("/message")
3679 + ? Deferred.succeed(reading, undefined).pipe(
3680 + Effect.andThen(Effect.never),
3681 + Effect.onInterrupt(() => Deferred.succeed(cancelled, undefined)),
3682 + )
3683 + : Effect.void,
3684 + },
3685 + ).pipe(Effect.provideService(Scope.Scope, sessionScope));
3686 + yield* runtime.startTurn(turnInput(thread));
3687 + yield* Deferred.await(reading);
3688 + assert.equal(borrowers.current, 1);
3689 + yield* Scope.close(sessionScope, Exit.void);
3690 + yield* Deferred.await(cancelled);
3691 + assert.equal(borrowers.current, 0);
3692 + }).pipe(Effect.scoped),
3693 + );
3694 +
3695 + it.effect("honors a confirmed Stop during recovery and ignores its late backfill", () =>
3696 + Effect.gen(function* () {
3697 + const recovered = yield* Deferred.make<void>();
3698 + const reading = yield* Deferred.make<void>();
3699 + const resume = yield* Deferred.make<void>();
3700 + const { runtime, thread } = yield* resumed(
3701 + [
3702 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3703 + promptAccepted,
3704 + ...reconnected({ [SESSION]: { type: "running" } }),
3705 + out("session.interrupt", { sessionID: SESSION }),
3706 + reply("session.interrupt", { interrupted: true }),
3707 + ...openRequests([]),
3708 + event("session.execution.interrupted", { sessionID: SESSION }),
3709 + event("session.model.selected", {
3710 + sessionID: SESSION,
3711 + model: { providerID: "opencode", id: "recovered" },
3712 + }),
3713 + ],
3714 + {
3715 + beforeResponse: (path) =>
3716 + path.endsWith("/message")
3717 + ? Deferred.succeed(reading, undefined).pipe(Effect.andThen(Deferred.await(resume)))
3718 + : Effect.void,
3719 + },
3720 + );
3721 + const collected: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
3722 + const stopped = yield* Deferred.make<void>();
3723 + yield* runtime.events.pipe(
3724 + Stream.runForEach((event) =>
3725 + Effect.gen(function* () {
3726 + collected.push(event);
3727 + if (
3728 + event.type === "provider_thread.updated" &&
3729 + event.providerThread.nativeMetadata?.modelSelection?.model === "opencode/recovered"
3730 + ) {
3731 + yield* Deferred.succeed(recovered, undefined);
3732 + }
3733 + if (event.type === "turn.terminal") yield* Deferred.succeed(stopped, undefined);
3734 + }),
3735 + ),
3736 + Effect.forkScoped,
3737 + );
3738 + yield* runtime.startTurn(turnInput(thread));
3739 + yield* Deferred.await(reading);
3740 + const stopping = yield* runtime
3741 + .interruptTurn({
3742 + providerThread: thread,
3743 + providerTurnId: yield* providerTurnId,
3744 + })
3745 + .pipe(Effect.forkScoped);
3746 + yield* Fiber.join(stopping);
3747 + assert.equal(runtime.providerSession.status, "ready");
3748 + yield* Deferred.await(stopped);
3749 + yield* Deferred.succeed(resume, undefined);
3750 + yield* Deferred.await(recovered);
3751 + assert.deepEqual(
3752 + collected.filter((event) => event.type === "turn.terminal").map((event) => event.status),
3753 + ["interrupted"],
3754 + );
3755 + assert.isFalse(
3756 + collected.some(
3757 + (event) =>
3758 + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message",
3759 + ),
3760 + );
3761 + }).pipe(Effect.scoped),
3762 + );
3763 +
3764 + it.effect("keeps a background child's requests when its parent stops during reconciliation", () =>
3765 + Effect.gen(function* () {
3766 + const reading = yield* Deferred.make<void>();
3767 + const resume = yield* Deferred.make<void>();
3768 + const recovered = yield* Deferred.make<void>();
3769 + const permission = { ...shellAsk.data, sessionID: CHILD };
3770 + const form = { ...colorForm, sessionID: CHILD };
3771 + const { runtime, thread } = yield* resumed(
3772 + [
3773 + ...backgroundLaunch(CHILD),
3774 + event("permission.asked", permission),
3775 + event("form.created", { form }),
3776 + ...reconnected({ [SESSION]: { type: "running" }, [CHILD]: { type: "running" } }),
3777 + ...openRequests([]),
3778 + out("permission.list", { sessionID: CHILD }),
3779 + replyData("permission.list", [permission]),
3780 + out("session.form.list", { sessionID: CHILD }),
3781 + replyData("session.form.list", [form]),
3782 + out("session.interrupt", { sessionID: SESSION }),
3783 + reply("session.interrupt", { interrupted: true }),
3784 + event("session.model.selected", {
3785 + sessionID: SESSION,
3786 + model: { providerID: "opencode", id: "recovered" },
3787 + }),
3788 + out("permission.reply", { sessionID: CHILD, requestID: permission.id, decision: "once" }),
3789 + reply("permission.reply", null),
3790 + out("session.form.reply", { sessionID: CHILD, formID: form.id, answer: { q0: "Red" } }),
3791 + reply("session.form.reply", null),
3792 + ],
3793 + {
3794 + beforeResponse: (path) =>
3795 + path === `/api/session/${CHILD}/form`
3796 + ? Deferred.succeed(reading, undefined).pipe(Effect.andThen(Deferred.await(resume)))
3797 + : Effect.void,
3798 + },
3799 + );
3800 + const requests = new Map<
3801 + string,
3802 + { readonly id: RuntimeRequestId; readonly status: string }
3803 + >();
3804 + yield* runtime.events.pipe(
3805 + Stream.runForEach((event) =>
3806 + Effect.gen(function* () {
3807 + if (event.type === "runtime_request.updated") {
3808 + const nativeId = event.runtimeRequest.nativeRequestRef?.nativeId;
3809 + if (nativeId != null) requests.set(nativeId, event.runtimeRequest);
3810 + }
3811 + if (
3812 + event.type === "provider_thread.updated" &&
3813 + event.providerThread.nativeMetadata?.modelSelection?.model === "opencode/recovered"
3814 + ) {
3815 + yield* Deferred.succeed(recovered, undefined);
3816 + }
3817 + }),
3818 + ),
3819 + Effect.forkScoped,
3820 + );
3821 + yield* runtime.startTurn(withLineage(thread));
3822 + yield* Deferred.await(reading);
3823 + const stopping = yield* runtime
3824 + .interruptTurn({ providerThread: thread, providerTurnId: yield* providerTurnId })
3825 + .pipe(Effect.forkScoped);
3826 + yield* Fiber.join(stopping);
3827 + yield* Deferred.succeed(resume, undefined);
3828 + yield* Deferred.await(recovered);
3829 + assert.isTrue(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3830 + assert.equal(requests.get(permission.id)?.status, "pending");
3831 + assert.equal(requests.get(form.id)?.status, "pending");
3832 + yield* runtime.respondToRuntimeRequest({
3833 + requestId: requests.get(permission.id)!.id,
3834 + decision: "accept",
3835 + });
3836 + yield* runtime.respondToRuntimeRequest({
3837 + requestId: requests.get(form.id)!.id,
3838 + answers: { q0: "Red" },
3839 + });
3840 + }).pipe(Effect.scoped),
3841 + );
3842 +
3843 it.effect("keeps a turn still running after a reconnect open for its next events", () =>
3844 Effect.gen(function* () {
3845 const { runtime, thread } = yield* resumed([
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts
+18 -2
@@ -152,6 +152,7 @@ const replayHttpClient = (
152 controller: OpenCodeReplayController,
153 replayGate: ProviderReplayGate | undefined,
154 onPrompt: ((input: unknown) => void) | undefined,
155 + beforeResponse: ((path: string) => Effect.Effect<void>) | undefined,
156 ) =>
157 HttpClient.make((request, url) =>
158 Effect.tryPromise({
@@ -210,13 +211,18 @@ const replayHttpClient = (
211 new HttpClientError.HttpClientError({
212 reason: new HttpClientError.TransportError({ request, cause }),
213 }),
213 - }).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response))),
214 + }).pipe(
215 + Effect.map((response) => HttpClientResponse.fromWeb(request, response)),
216 + Effect.tap(() => beforeResponse?.(url.pathname) ?? Effect.void),
217 + ),
218 );
219
220 export const replayServer = (
221 transcript: ProviderReplayTranscript,
222 options?: {
223 readonly external?: boolean;
224 + readonly processExit?: Effect.Effect<void>;
225 + readonly beforeResponse?: (path: string) => Effect.Effect<void>;
226 readonly onPrompt?: (input: unknown) => void;
227 readonly replayGate?: ProviderReplayGate;
228 readonly borrowers?: { current: number };
@@ -233,7 +239,12 @@ export const replayServer = (
239 const opencode = yield* OpenCode2Client.make.pipe(
240 Effect.provideService(
241 HttpClient.HttpClient,
236 - replayHttpClient(controller, options?.replayGate, options?.onPrompt),
242 + replayHttpClient(
243 + controller,
244 + options?.replayGate,
245 + options?.onPrompt,
246 + options?.beforeResponse,
247 + ),
248 ),
249 );
250 const connection = {
@@ -241,6 +252,7 @@ export const replayServer = (
252 url: BASE_URL,
253 version: transcript.version,
254 external: options?.external ?? false,
255 + ...(options?.external ? {} : { processExit: options?.processExit ?? Effect.never }),
256 };
257 const borrowers = options?.borrowers;
258 return OpenCode2Server.OpenCode2Server.of({
@@ -265,6 +277,8 @@ const makeReplayAdapter = (
277 transcript: ProviderReplayTranscript,
278 options?: {
279 readonly external?: boolean;
280 + readonly processExit?: Effect.Effect<void>;
281 + readonly beforeResponse?: (path: string) => Effect.Effect<void>;
282 readonly onPrompt?: (input: unknown) => void;
283 readonly replayGate?: ProviderReplayGate;
284 readonly borrowers?: { current: number };
@@ -318,6 +332,8 @@ export const openCode2ReplayRuntime = (
332 entries: ReadonlyArray<ProviderReplayEntry>,
333 options?: {
334 readonly external?: boolean;
335 + readonly processExit?: Effect.Effect<void>;
336 + readonly beforeResponse?: (path: string) => Effect.Effect<void>;
337 readonly version?: string;
338 readonly onPrompt?: (input: unknown) => void;
339 readonly borrowers?: { current: number };
docs/user/providers-opencode.md
+3 -1
@@ -55,7 +55,9 @@ reached through `localhost`. That server can still report the previous version
55 until you restart it. For a loopback URL, T3 Code shows that restart requirement
56 instead of treating the CLI update as a failure.
57
58 -After a lost connection, send another prompt to reconnect to the same OpenCode
58 +T3 Code waits for a local server it started to recover after a lost connection.
59 +You can stop the turn while it reconnects. If the server exits or an external
60 +connection cannot be restored, send another prompt to resume the same OpenCode
61 session.
62
63 ## Approvals
packages/provider-opencode/src/server/v2/OpenCode2Client.test.ts
+32
@@ -4,6 +4,8 @@ import * as Cause from "effect/Cause";
4 import * as Deferred from "effect/Deferred";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 +import * as Exit from "effect/Exit";
8 +import * as Scope from "effect/Scope";
9 import * as Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11 import * as Schema from "effect/Schema";
@@ -168,6 +170,36 @@ describe("OpenCode2Client message history", () => {
170 }),
171 );
172 });
173 +it.effect("closes an event subscription that recovery has not started reading", () =>
174 + Effect.gen(function* () {
175 + const signals: Array<AbortSignal> = [];
176 + const http = HttpClient.make((request, _url, signal) =>
177 + Effect.sync(() => {
178 + signals.push(signal);
179 + return HttpClientResponse.fromWeb(
180 + request,
181 + new Response(": connected\n\n", {
182 + headers: { "content-type": "text/event-stream" },
183 + }),
184 + );
185 + }),
186 + );
187 + const opencode = yield* OpenCode2Client.make.pipe(
188 + Effect.provideService(HttpClient.HttpClient, http),
189 + );
190 + const connection = yield* opencode.connect({
191 + baseUrl: "http://127.0.0.1:4096",
192 + password: "test",
193 + });
194 + const scope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
195 + Scope.close(scope, Exit.void),
196 + );
197 + yield* connection.events.pipe(Effect.provideService(Scope.Scope, scope), Effect.asVoid);
198 + assert.isFalse(signals[0]!.aborted);
199 + yield* Scope.close(scope, Exit.void);
200 + assert.isTrue(signals[0]!.aborted);
201 + }).pipe(Effect.scoped),
202 +);
203
204 describe("OpenCode2Client native image jobs", () => {
205 it.effect.each([
packages/provider-opencode/src/server/v2/OpenCode2Client.ts
+10 -4
@@ -9,6 +9,7 @@ import * as Option from "effect/Option";
9 import * as Redacted from "effect/Redacted";
10 import * as Result from "effect/Result";
11 import * as Schema from "effect/Schema";
12 +import type * as Scope from "effect/Scope";
13 import * as Stream from "effect/Stream";
14 import * as Sse from "effect/encoding/Sse";
15 import * as HttpClient from "effect/http/HttpClient";
@@ -113,7 +114,8 @@ export interface OpenCode2Api {
114 OpenCode2StreamEvent,
115 HttpClientError.HttpClientError | Sse.Retry | Sse.SseError | OpenCode2SilentStreamError
116 >,
116 - HttpClientError.HttpClientError
117 + HttpClientError.HttpClientError,
118 + Scope.Scope
119 >;
120 }
121
@@ -170,9 +172,13 @@ const undecodable = Effect.fnUntraced(function* (data: string) {
172 return Result.failVoid;
173 });
174
173 -const readEvents = HttpClient.get("/api/event", {
174 - headers: { accept: "text/event-stream" },
175 -}).pipe(
175 +const readEvents = HttpClient.HttpClient.pipe(
176 + // Recovery subscribes before reading history, so its stream may never be consumed.
177 + Effect.flatMap((client) =>
178 + HttpClient.withScope(client).get("/api/event", {
179 + headers: { accept: "text/event-stream" },
180 + }),
181 + ),
182 Effect.flatMap(HttpClientResponse.filterStatusOk),
183 Effect.map((response) =>
184 response.stream.pipe(
packages/provider-opencode/src/server/v2/OpenCode2Server.test.ts
+4
@@ -253,8 +253,12 @@ describe("OpenCode2Server spawned server", () => {
253 assert.strictEqual(first.version, "2.0.18");
254 assert.strictEqual(first.external, false);
255 assert.strictEqual(second.client, first.client);
256 + if (first.processExit === undefined)
257 + return yield* Effect.die("Missing owned process lifetime");
258 + return first.processExit;
259 }).pipe(
260 Effect.scoped,
261 + Effect.flatten,
262 Effect.provide(
263 Layer.mergeAll(
264 OpenCode2Client.layer,
packages/provider-opencode/src/server/v2/OpenCode2Server.ts
+4 -2
@@ -18,6 +18,8 @@ export interface OpenCode2Connection extends OpenCode2Client.OpenCode2Api {
18 readonly url: string;
19 readonly version: string;
20 readonly external: boolean;
21 + /** Only owned processes have an authoritative lifetime signal. */
22 + readonly processExit?: Effect.Effect<void>;
23 }
24
25 export class OpenCode2Server extends Context.Service<
@@ -50,7 +52,7 @@ export const serverEnvironment = (
52 return { ...rest, OPENCODE_PASSWORD: Redacted.value(password) };
53 };
54
53 -const httpFailureOf = (cause: unknown): HttpClientError.HttpClientError | undefined => {
55 +export const httpFailureOf = (cause: unknown): HttpClientError.HttpClientError | undefined => {
56 if (HttpClientError.isHttpClientError(cause)) return cause;
57 if (
58 P.isTagged(cause, "ClientError") &&
@@ -163,7 +165,7 @@ export const make = Effect.fn("OpenCode2Server.make")(function* (input: {
165 withConnection: Effect.fn("OpenCode2Server.withConnection")(function (use) {
166 return owner.withServer((server) =>
167 latest?.url === server.url
166 - ? use(latest)
168 + ? use({ ...latest, processExit: server.exitCode.pipe(Effect.asVoid) })
169 : Effect.die(new Error("OpenCode 2 server was lent before verification.")),
170 );
171 }),
packages/provider-opencode/src/server/v2/adapter.ts
+245 -137
@@ -48,9 +48,11 @@ import type * as Cause from "effect/Cause";
48 import * as DateTime from "effect/DateTime";
49 import * as Deferred from "effect/Deferred";
50 import * as Digest from "effect/crypto/Digest";
51 +import * as Duration from "effect/Duration";
52 import * as Effect from "effect/Effect";
53 import * as Hex from "effect/encoding/Hex";
54 import * as Exit from "effect/Exit";
55 +import * as P from "effect/Predicate";
56 import * as FileSystem from "effect/FileSystem";
57 import * as Queue from "effect/Queue";
58 import * as Schedule from "effect/Schedule";
@@ -589,6 +591,44 @@ const RECONNECT_ATTEMPTS = 5;
591 const RECONNECT_DELAY = "2 seconds";
592 const RECONCILE_TIMEOUT = "15 seconds";
593 const RECONNECT_WAIT = "30 seconds";
594 +const OWNED_RECONNECT_SCHEDULE = Schedule.exponential(RECONNECT_DELAY).pipe(
595 + Schedule.modifyDelay(({ duration }) =>
596 + Effect.succeed(Duration.min(duration, Duration.seconds(30))),
597 + ),
598 +);
599 +const TRANSIENT_HTTP_STATUSES: ReadonlySet<number> = new Set([408, 425, 429]);
600 +// A live owned server is retried until it answers; these failures repeat however long it waits.
601 +const isPermanentRecoveryError = (error: unknown) => {
602 + if (Schema.isSchemaError(error)) return true;
603 + if (
604 + P.isTagged(error, "UnauthorizedError") ||
605 + P.isTagged(error, "InvalidRequestError") ||
606 + P.isTagged(error, "InvalidCursorError")
607 + ) {
608 + return true;
609 + }
610 + const http = OpenCode2Server.httpFailureOf(error);
611 + if (http !== undefined) {
612 + const status = http.response?.status;
613 + return (
614 + status !== undefined && status >= 400 && status < 500 && !TRANSIENT_HTTP_STATUSES.has(status)
615 + );
616 + }
617 + // ClientError also wraps an SSE retry request, which clears.
618 + return (
619 + P.isTagged(error, "ClientError") &&
620 + P.hasProperty(error, "cause") &&
621 + Schema.isSchemaError(error.cause)
622 + );
623 +};
624 +const OWNED_RECOVERY_RETRY = {
625 + schedule: OWNED_RECONNECT_SCHEDULE,
626 + while: (error: unknown) => !isPermanentRecoveryError(error),
627 +};
628 +class OpenCode2ServerExitedError extends Schema.TaggedError<OpenCode2ServerExitedError>()(
629 + "OpenCode2ServerExitedError",
630 + {},
631 +) {}
632 const LOST_BACKGROUND =
633 "T3 Code lost its connection to OpenCode while this subagent ran, so its result is not shown.";
634 const INVENTORY_TIMEOUT = "5 seconds";
@@ -3337,90 +3377,98 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3377 ).pipe(
3378 Stream.takeUntil((message) => message.id === start),
3379 Stream.runCollect,
3380 + Effect.catchTags({ SessionNotFoundError: () => Effect.succeed(null) }),
3381 );
3341 - // Without this turn's start in history, all retrieved turns are older.
3342 - const found = start === null || read.at(-1)?.id === start;
3343 - if (!found) return undefined;
3344 - const recent = before !== undefined && before !== null ? read.slice(0, -1) : read;
3345 - for (const message of recent) {
3346 - if (message.type === "user" && turn.steers.delete(message.id)) {
3347 - turn.settledInbox.add(message.id);
3348 - }
3349 - }
3350 - const idle = recent.find((message) => message.type === "idle");
3351 - const assistant = recent.find((message) => message.type === "assistant");
3352 - if (assistant?.retry !== undefined) {
3353 - // Snapshots omit when the retry was scheduled, so only its remaining delay is known.
3354 - yield* updateRetry(
3355 - state,
3356 - turn,
3357 - assistant.retry,
3358 - DateTime.toEpochMillis(assistant.retry.at) - DateTime.toEpochMillis(yield* DateTime.now),
3359 - );
3360 - } else if (
3361 - turn.retry !== undefined &&
3362 - assistant !== undefined &&
3363 - assistant.error === undefined &&
3364 - assistant.content.length > 0
3365 - ) {
3366 - yield* emitRetry(state, turn, "completed");
3367 - turn.retry = undefined;
3368 - }
3369 - for (const message of recent.toReversed()) {
3370 - if (message.type !== "assistant" || state.active !== turn) continue;
3371 - const ordinals = { text: 0, reasoning: 0 };
3372 - for (const [index, part] of message.content.entries()) {
3373 - if (part.type === "text" || part.type === "reasoning") {
3374 - const block = { assistantMessageID: message.id, ordinal: ordinals[part.type]++ };
3375 - // OpenCode saves a block's text only when the block ends, so the last block of a
3376 - // response still streaming stays open and keeps the text that already arrived.
3377 - const open =
3378 - message.time.streamed === undefined && index === message.content.length - 1;
3379 - yield* emitText(
3382 + return yield* lock.withPermit(
3383 + Effect.gen(function* () {
3384 + if (state.active !== turn) return undefined;
3385 + if (read === null) return { outcome: "missing" as const };
3386 + // Without this turn's start in history, all retrieved turns are older.
3387 + const found = start === null || read.at(-1)?.id === start;
3388 + if (!found) return undefined;
3389 + const recent = before !== undefined && before !== null ? read.slice(0, -1) : read;
3390 + for (const message of recent) {
3391 + if (message.type === "user" && turn.steers.delete(message.id)) {
3392 + turn.settledInbox.add(message.id);
3393 + }
3394 + }
3395 + const idle = recent.find((message) => message.type === "idle");
3396 + const assistant = recent.find((message) => message.type === "assistant");
3397 + if (assistant?.retry !== undefined) {
3398 + // Snapshots omit when the retry was scheduled, so only its remaining delay is known.
3399 + yield* updateRetry(
3400 state,
3401 turn,
3382 - block,
3383 - part.type,
3384 - (text) => (open && !part.text.startsWith(text) ? text : part.text),
3385 - !open,
3402 + assistant.retry,
3403 + DateTime.toEpochMillis(assistant.retry.at) -
3404 + DateTime.toEpochMillis(yield* DateTime.now),
3405 );
3387 - continue;
3388 - }
3389 - if (part.type !== "tool") continue;
3390 - if (!turn.tools.has(part.id)) {
3391 - turn.tools.set(part.id, { name: part.name, input: {} });
3392 - turn.startedAt.set(part.id, yield* DateTime.now);
3393 - }
3394 - const tool = turn.tools.get(part.id)!;
3395 - if (typeof part.state.input === "object" && part.state.input !== null) {
3396 - tool.input = part.state.input as Record<string, unknown>;
3406 + } else if (
3407 + turn.retry !== undefined &&
3408 + assistant !== undefined &&
3409 + assistant.error === undefined &&
3410 + assistant.content.length > 0
3411 + ) {
3412 + yield* emitRetry(state, turn, "completed");
3413 + turn.retry = undefined;
3414 }
3398 - if (part.state.status === "completed") {
3399 - yield* emitTool(state, turn, part.id, "completed", {
3400 - output: textOf(part.state.content),
3401 - files: part.state.content.flatMap((content) =>
3402 - content.type === "file" ? [content] : [],
3403 - ),
3404 - metadata: part.state.metadata,
3405 - });
3406 - if (
3407 - ![...state.imageJobs.values()].some(
3408 - (job) => job.turn === turn && job.toolId === part.id,
3409 - )
3410 - )
3411 - turn.tools.delete(part.id);
3412 - } else if (part.state.status === "error") {
3413 - yield* emitTool(state, turn, part.id, "failed", {
3414 - output: part.state.error.message,
3415 - metadata: part.state.metadata,
3416 - });
3417 - turn.tools.delete(part.id);
3418 - } else {
3419 - yield* emitTool(state, turn, part.id, "running");
3415 + for (const message of recent.toReversed()) {
3416 + if (message.type !== "assistant" || state.active !== turn) continue;
3417 + const ordinals = { text: 0, reasoning: 0 };
3418 + for (const [index, part] of message.content.entries()) {
3419 + if (part.type === "text" || part.type === "reasoning") {
3420 + const block = { assistantMessageID: message.id, ordinal: ordinals[part.type]++ };
3421 + // OpenCode saves a block's text only when the block ends, so the last block of a
3422 + // response still streaming stays open and keeps the text that already arrived.
3423 + const open =
3424 + message.time.streamed === undefined && index === message.content.length - 1;
3425 + yield* emitText(
3426 + state,
3427 + turn,
3428 + block,
3429 + part.type,
3430 + (text) => (open && !part.text.startsWith(text) ? text : part.text),
3431 + !open,
3432 + );
3433 + continue;
3434 + }
3435 + if (part.type !== "tool") continue;
3436 + if (!turn.tools.has(part.id)) {
3437 + turn.tools.set(part.id, { name: part.name, input: {} });
3438 + turn.startedAt.set(part.id, yield* DateTime.now);
3439 + }
3440 + const tool = turn.tools.get(part.id)!;
3441 + if (typeof part.state.input === "object" && part.state.input !== null) {
3442 + tool.input = part.state.input as Record<string, unknown>;
3443 + }
3444 + if (part.state.status === "completed") {
3445 + yield* emitTool(state, turn, part.id, "completed", {
3446 + output: textOf(part.state.content),
3447 + files: part.state.content.flatMap((content) =>
3448 + content.type === "file" ? [content] : [],
3449 + ),
3450 + metadata: part.state.metadata,
3451 + });
3452 + if (
3453 + ![...state.imageJobs.values()].some(
3454 + (job) => job.turn === turn && job.toolId === part.id,
3455 + )
3456 + )
3457 + turn.tools.delete(part.id);
3458 + } else if (part.state.status === "error") {
3459 + yield* emitTool(state, turn, part.id, "failed", {
3460 + output: part.state.error.message,
3461 + metadata: part.state.metadata,
3462 + });
3463 + turn.tools.delete(part.id);
3464 + } else {
3465 + yield* emitTool(state, turn, part.id, "running");
3466 + }
3467 + }
3468 }
3421 - }
3422 - }
3423 - return idle === undefined ? undefined : { outcome: idle.outcome };
3469 + return idle === undefined ? undefined : { outcome: idle.outcome };
3470 + }),
3471 + );
3472 });
3473
3474 // Restarts retain the previous session.outcome; without an idle item, the lost execution was interrupted.
@@ -3472,7 +3520,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3520 ) {
3521 return;
3522 }
3475 - const active = yield* client.session.active();
3523 + const active = { ...(yield* client.session.active()) };
3524 for (const sessionId of busy) if (!(sessionId in active)) busy.delete(sessionId);
3525 for (const state of threads.values()) {
3526 if (state.unsettled && !(state.sessionId in active)) state.unsettledStarted = true;
@@ -3483,72 +3531,101 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3531 if (turn === undefined) continue;
3532 turn.missedEvents = true;
3533 const ended = yield* backfill(sessionId, state);
3486 - if (state.active !== turn) continue;
3487 - if (sessionId in active) {
3488 - turn.heldEnd = undefined;
3489 - continue;
3534 + if (ended?.outcome === "missing") {
3535 + delete active[Session.ID.make(sessionId)];
3536 + busy.delete(sessionId);
3537 }
3491 - const terminal: TurnTerminal =
3492 - ended?.outcome === "succeeded"
3493 - ? { status: turn.interrupted ? "interrupted" : "completed" }
3494 - : ended?.outcome === "failed"
3495 - ? {
3496 - status: "failed",
3497 - failure: makeProviderFailure({
3498 - message:
3499 - "OpenCode ended the turn with an error while T3 Code was reconnecting.",
3500 - class: "provider_error",
3501 - }),
3502 - }
3503 - : { status: "interrupted" };
3504 - yield* endExecution(state, turn, terminal);
3538 + yield* lock.withPermit(
3539 + Effect.gen(function* () {
3540 + if (state.active !== turn) return;
3541 + if (sessionId in active) {
3542 + turn.heldEnd = undefined;
3543 + return;
3544 + }
3545 + if (ended?.outcome === "missing") {
3546 + return yield* finishTurn(state, { status: "interrupted" }, "broken");
3547 + }
3548 + const terminal: TurnTerminal =
3549 + ended?.outcome === "succeeded"
3550 + ? { status: turn.interrupted ? "interrupted" : "completed" }
3551 + : ended?.outcome === "failed"
3552 + ? {
3553 + status: "failed",
3554 + failure: makeProviderFailure({
3555 + message:
3556 + "OpenCode ended the turn with an error while T3 Code was reconnecting.",
3557 + class: "provider_error",
3558 + }),
3559 + }
3560 + : { status: "interrupted" };
3561 + yield* endExecution(state, turn, terminal);
3562 + }),
3563 + );
3564 }
3565 for (const state of threads.values()) {
3507 - yield* recoverImages(state).pipe(
3508 - Effect.timeout(INVENTORY_TIMEOUT),
3509 - Effect.ignore({ log: true }),
3510 - );
3566 + yield* lock
3567 + .withPermit(recoverImages(state).pipe(Effect.timeout(INVENTORY_TIMEOUT)))
3568 + .pipe(Effect.ignore({ log: true }));
3569 }
3512 - for (const state of background) {
3513 - for (const call of runningCalls(state)) {
3514 - const child = call.child?.sessionId;
3515 - if (child === undefined || !(child in active)) {
3516 - yield* settleCall(call, "interrupted", LOST_BACKGROUND);
3570 + yield* lock.withPermit(
3571 + Effect.gen(function* () {
3572 + for (const state of background) {
3573 + for (const call of runningCalls(state)) {
3574 + const child = call.child?.sessionId;
3575 + if (child === undefined || !(child in active)) {
3576 + yield* settleCall(call, "interrupted", LOST_BACKGROUND);
3577 + }
3578 + }
3579 + const sessions = sessionsOf(state);
3580 + for (const session of sessions) {
3581 + discardWakes(session, session.wakes.splice(0));
3582 + session.reports.clear();
3583 + }
3584 + for (const session of sessions.toReversed()) yield* syncRoster(session);
3585 }
3518 - }
3519 - const sessions = sessionsOf(state);
3520 - for (const session of sessions) {
3521 - discardWakes(session, session.wakes.splice(0));
3522 - session.reports.clear();
3523 - }
3524 - for (const session of sessions.toReversed()) yield* syncRoster(session);
3525 - }
3526 - // Stopped off the lock this reconcile holds; one still running waits for a Stop.
3527 - for (const thread of new Set([...strays.values()].map(rootOf))) {
3528 - yield* Effect.forkIn(stopStrays(thread), sessionScope);
3529 - }
3586 + // Stopped off the lock this reconcile holds; one still running waits for a Stop.
3587 + for (const thread of new Set([...strays.values()].map(rootOf))) {
3588 + yield* Effect.forkIn(stopStrays(thread), sessionScope);
3589 + }
3590 + }),
3591 + );
3592 const listed = new Map<string, Set<string>>();
3593 for (const sessionId of Object.keys(active)) {
3532 - if (ownerOf(sessionId) === undefined) continue;
3594 + const owner = ownerOf(sessionId);
3595 + if (owner === undefined) continue;
3596 + const turn = requestTurn(sessionId)?.turn;
3597 + // Replies may settle during these reads; an older snapshot must not ask again.
3598 + const known = new Set([...pending.values()].map((entry) => entry.native.id));
3599 const sessionID = Session.ID.make(sessionId);
3600 const [permissions, forms] = yield* Effect.all([
3601 client.permission.list({ sessionID }),
3602 client.session.form.list({ sessionID }),
3603 ]);
3538 - listed.set(sessionId, new Set([...permissions, ...forms].map((request) => request.id)));
3539 - const shown = new Set([...pending.values()].map((entry) => entry.native.id));
3540 - for (const request of permissions) {
3541 - if (!shown.has(request.id)) yield* onAsked({ type: "permission", request });
3542 - }
3543 - for (const form of forms) {
3544 - if (!shown.has(form.id)) yield* onAsked({ type: "form", form });
3545 - }
3546 - }
3547 - for (const entry of Array.from(pending.values())) {
3548 - if (listed.get(entry.sessionId)?.has(entry.native.id) !== true) {
3549 - yield* settleRequest(entry, "cancelled");
3550 - }
3604 + yield* lock.withPermit(
3605 + Effect.gen(function* () {
3606 + if (ownerOf(sessionId) !== owner || requestTurn(sessionId)?.turn !== turn) return;
3607 + listed.set(sessionId, new Set([...permissions, ...forms].map((request) => request.id)));
3608 + const shown = new Set([...pending.values()].map((entry) => entry.native.id));
3609 + for (const request of permissions) {
3610 + if (!shown.has(request.id) && !known.has(request.id))
3611 + yield* onAsked({ type: "permission", request });
3612 + }
3613 + for (const form of forms) {
3614 + if (!shown.has(form.id) && !known.has(form.id))
3615 + yield* onAsked({ type: "form", form });
3616 + }
3617 + }),
3618 + );
3619 }
3620 + yield* lock.withPermit(
3621 + Effect.gen(function* () {
3622 + for (const entry of Array.from(pending.values())) {
3623 + if (listed.get(entry.sessionId)?.has(entry.native.id) !== true) {
3624 + yield* settleRequest(entry, "cancelled");
3625 + }
3626 + }
3627 + }),
3628 + );
3629 });
3630
3631 const reconnect = Effect.gen(function* () {
@@ -3556,7 +3633,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3633 const scope = yield* Scope.make();
3634 const { next, stream } = yield* Effect.gen(function* () {
3635 const next = yield* borrow.pipe(Effect.provideService(Scope.Scope, scope));
3559 - return { next, stream: yield* next.events };
3636 + return { next, stream: yield* next.events.pipe(Effect.provideService(Scope.Scope, scope)) };
3637 }).pipe(Effect.onError(() => Scope.close(scope, Exit.void)));
3638 const previous = currentScope;
3639 connection = next;
@@ -3590,10 +3667,27 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3667 for (const state of threads.values()) {
3668 yield* recoverShells(state).pipe(Effect.ignore({ log: true }));
3669 }
3593 - yield* lock.withPermit(reconcile()).pipe(Effect.timeout(RECONCILE_TIMEOUT));
3670 + // An owned process's exit, rather than a read deadline, bounds recovery; the bounded retry
3671 + // below then borrows a restarted server. HTTP waits stay outside the event lock so Stop can still settle.
3672 + const exited = next.processExit;
3673 + yield* exited === undefined
3674 + ? reconcile().pipe(Effect.timeout(RECONCILE_TIMEOUT))
3675 + : reconcile().pipe(
3676 + Effect.retry(OWNED_RECOVERY_RETRY),
3677 + Effect.raceFirst(
3678 + exited.pipe(Effect.andThen(Effect.fail(new OpenCode2ServerExitedError()))),
3679 + ),
3680 + );
3681 return stream;
3682 }).pipe(
3596 - Effect.retry({ times: RECONNECT_ATTEMPTS - 1, schedule: Schedule.spaced(RECONNECT_DELAY) }),
3683 + // Borrows and process exits stay bounded; only a live owned process is waited on above.
3684 + Effect.retry({
3685 + times: RECONNECT_ATTEMPTS - 1,
3686 + schedule: Schedule.spaced(RECONNECT_DELAY),
3687 + ...(connection.processExit === undefined
3688 + ? {}
3689 + : { while: (error: unknown) => !isPermanentRecoveryError(error) }),
3690 + }),
3691 );
3692
3693 let currentScope = initial.scope;
@@ -3638,7 +3732,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3732 Effect.option,
3733 );
3734 if (next._tag === "None") {
3641 - return yield* failAll("The OpenCode event stream was lost and could not reconnect.");
3735 + return yield* lock.withPermit(
3736 + failAll("The OpenCode event stream was lost and could not reconnect."),
3737 + );
3738 }
3739 streamFailure = undefined;
3740 const done = reconnected;
@@ -3649,7 +3745,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3745 ),
3746 );
3747 // Subscribed before any session or prompt call, so no event of theirs is missed.
3652 - yield* follow(yield* connection.events).pipe(Effect.forkScoped);
3748 + yield* follow(
3749 + yield* connection.events.pipe(Effect.provideService(Scope.Scope, currentScope)),
3750 + ).pipe(Effect.forkScoped);
3751
3752 yield* Effect.gen(function* () {
3753 const snapshots = yield* lock.withPermit(
@@ -4842,6 +4940,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
4940 timeoutMs: INTERRUPT_TIMEOUT,
4941 });
4942 }
4943 + // Recovery may hold the terminal event behind a blocked history read.
4944 + if (reply.value.interrupted && streamFailure !== undefined) {
4945 + return yield* lock.withPermit(
4946 + Effect.gen(function* () {
4947 + if (state.active !== turn) return;
4948 + markUnsettled(state);
4949 + yield* finishTurn(state, { status: "interrupted" });
4950 + }),
4951 + );
4952 + }
4953 // Stop between steer executions retracts pending steers and skips any run they already started.
4954 if (!reply.value.interrupted && state.active === turn && turn.heldEnd !== undefined) {
4955 markUnsettled(state);