Skip to content

Commit 00be343

Browse files
committed
fix(opencode): stop owned recovery on failures that cannot clear
1 parent 75f86e4 commit 00be343

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,22 +2800,30 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28002800
}).pipe(Effect.scoped),
28012801
);
28022802

2803-
it.effect("bounds recovery when an external server keeps rejecting requests", () =>
2803+
it.effect.each([
2804+
{
2805+
server: "an external server keeps rejecting requests",
2806+
external: true,
2807+
status: 503,
2808+
tries: 5,
2809+
},
2810+
{ server: "an owned server rejects its credentials", external: false, status: 401, tries: 1 },
2811+
])("bounds recovery when $server", ({ external, status, tries }) =>
28042812
Effect.gen(function* () {
28052813
const attempts = yield* Queue.unbounded<void>();
28062814
const { runtime, thread } = yield* resumed(
28072815
[
28082816
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
28092817
promptAccepted,
28102818
{ type: "runtime_exit", status: "success" },
2811-
...Array.from({ length: 5 }, () => [
2819+
...Array.from({ length: tries }, () => [
28122820
out("event.subscribe"),
28132821
out("session.active"),
2814-
reply("session.active", { status: 503, body: { message: "Unavailable" } }),
2822+
reply("session.active", { status, body: { message: "Unavailable" } }),
28152823
]).flat(),
28162824
],
28172825
{
2818-
external: true,
2826+
external,
28192827
beforeResponse: (path) =>
28202828
path === "/api/session/active"
28212829
? Queue.offer(attempts, undefined).pipe(Effect.asVoid)
@@ -2824,7 +2832,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28242832
);
28252833
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
28262834
yield* runtime.startTurn(turnInput(thread));
2827-
for (let attempt = 0; attempt < 4; attempt += 1) {
2835+
for (let attempt = 1; attempt < tries; attempt += 1) {
28282836
yield* Queue.take(attempts);
28292837
yield* TestClock.adjust("2 seconds");
28302838
}

‎packages/provider-opencode/src/server/v2/OpenCode2Server.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const serverEnvironment = (
5050
return { ...rest, OPENCODE_PASSWORD: Redacted.value(password) };
5151
};
5252

53-
const httpFailureOf = (cause: unknown): HttpClientError.HttpClientError | undefined => {
53+
export const httpFailureOf = (cause: unknown): HttpClientError.HttpClientError | undefined => {
5454
if (HttpClientError.isHttpClientError(cause)) return cause;
5555
if (
5656
P.isTagged(cause, "ClientError") &&

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import * as Duration from "effect/Duration";
4545
import * as Effect from "effect/Effect";
4646
import * as Hex from "effect/encoding/Hex";
4747
import * as Exit from "effect/Exit";
48+
import * as P from "effect/Predicate";
4849
import * as FileSystem from "effect/FileSystem";
4950
import * as Queue from "effect/Queue";
5051
import * as Schedule from "effect/Schedule";
@@ -543,6 +544,27 @@ const OWNED_RECONNECT_SCHEDULE = Schedule.exponential(RECONNECT_DELAY).pipe(
543544
Effect.succeed(Duration.min(duration, Duration.seconds(30))),
544545
),
545546
);
547+
// A live owned server is retried until it answers; these failures repeat however long it waits.
548+
const isPermanentRecoveryError = (error: unknown) => {
549+
if (Schema.isSchemaError(error)) return true;
550+
if (
551+
P.isTagged(error, "UnauthorizedError") ||
552+
P.isTagged(error, "InvalidRequestError") ||
553+
P.isTagged(error, "InvalidCursorError")
554+
) {
555+
return true;
556+
}
557+
const http = OpenCode2Server.httpFailureOf(error);
558+
if (http !== undefined) {
559+
const status = http.response?.status;
560+
return status !== undefined && status >= 400 && status < 500;
561+
}
562+
return P.isTagged(error, "ClientError");
563+
};
564+
const OWNED_RECOVERY_RETRY = {
565+
schedule: OWNED_RECONNECT_SCHEDULE,
566+
while: (error: unknown) => !isPermanentRecoveryError(error),
567+
};
546568
const LOST_BACKGROUND =
547569
"T3 Code lost its connection to OpenCode while this subagent ran, so its result is not shown.";
548570
const INVENTORY_TIMEOUT = "5 seconds";
@@ -2731,16 +2753,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27312753
// HTTP waits stay outside the event lock so Stop can still settle.
27322754
yield* connection.processExit === undefined
27332755
? reconcile.pipe(Effect.timeout(RECONCILE_TIMEOUT))
2734-
: reconcile.pipe(Effect.retry({ schedule: OWNED_RECONNECT_SCHEDULE }));
2756+
: reconcile.pipe(Effect.retry(OWNED_RECOVERY_RETRY));
27352757
return stream;
27362758
}).pipe(
2737-
Effect.retry({
2738-
times: connection.processExit === undefined ? RECONNECT_ATTEMPTS - 1 : undefined,
2739-
schedule:
2740-
connection.processExit === undefined
2741-
? Schedule.spaced(RECONNECT_DELAY)
2742-
: OWNED_RECONNECT_SCHEDULE,
2743-
}),
2759+
Effect.retry(
2760+
connection.processExit === undefined
2761+
? { times: RECONNECT_ATTEMPTS - 1, schedule: Schedule.spaced(RECONNECT_DELAY) }
2762+
: OWNED_RECOVERY_RETRY,
2763+
),
27442764
);
27452765

27462766
let currentScope = initial.scope;

0 commit comments

Comments
 (0)