Skip to content

Commit aa1d0b3

Browse files
committed
fix(opencode): keep owned recovery retrying on rate limits and SSE retry
1 parent 00be343 commit aa1d0b3

2 files changed

Lines changed: 51 additions & 41 deletions

File tree

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

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,46 +2724,48 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
27242724
}).pipe(Effect.scoped),
27252725
);
27262726

2727-
it.effect("recovers an owned server after more than five failed reconciliation requests", () =>
2728-
Effect.gen(function* () {
2729-
const allowRecovery = yield* Deferred.make<void>();
2730-
const attempts = yield* Queue.unbounded<void>();
2731-
let attempt = 0;
2732-
const { runtime, thread } = yield* resumed(
2733-
[
2734-
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2735-
promptAccepted,
2736-
{ type: "runtime_exit", status: "success" },
2737-
out("event.subscribe"),
2738-
...Array.from({ length: 6 }, () => [
2739-
out("session.active"),
2740-
reply("session.active", { status: 503, body: { message: "Recovering" } }),
2741-
]).flat(),
2742-
...reconnected({ [SESSION]: { type: "running" } }).slice(2),
2743-
...openRequests([]),
2744-
event("session.execution.succeeded", { sessionID: SESSION }),
2745-
],
2746-
{
2747-
beforeResponse: (path) =>
2748-
path === "/api/session/active"
2749-
? Effect.gen(function* () {
2750-
attempt += 1;
2751-
yield* Queue.offer(attempts, undefined);
2752-
if (attempt === 7) yield* Deferred.await(allowRecovery);
2753-
})
2754-
: Effect.void,
2755-
},
2756-
);
2757-
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2758-
yield* runtime.startTurn(turnInput(thread));
2759-
for (let retry = 0; retry < 6; retry += 1) {
2727+
it.effect.each([503, 429])(
2728+
"recovers an owned server after more than five HTTP %s reconciliation replies",
2729+
(status) =>
2730+
Effect.gen(function* () {
2731+
const allowRecovery = yield* Deferred.make<void>();
2732+
const attempts = yield* Queue.unbounded<void>();
2733+
let attempt = 0;
2734+
const { runtime, thread } = yield* resumed(
2735+
[
2736+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2737+
promptAccepted,
2738+
{ type: "runtime_exit", status: "success" },
2739+
out("event.subscribe"),
2740+
...Array.from({ length: 6 }, () => [
2741+
out("session.active"),
2742+
reply("session.active", { status, body: { message: "Recovering" } }),
2743+
]).flat(),
2744+
...reconnected({ [SESSION]: { type: "running" } }).slice(2),
2745+
...openRequests([]),
2746+
event("session.execution.succeeded", { sessionID: SESSION }),
2747+
],
2748+
{
2749+
beforeResponse: (path) =>
2750+
path === "/api/session/active"
2751+
? Effect.gen(function* () {
2752+
attempt += 1;
2753+
yield* Queue.offer(attempts, undefined);
2754+
if (attempt === 7) yield* Deferred.await(allowRecovery);
2755+
})
2756+
: Effect.void,
2757+
},
2758+
);
2759+
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
2760+
yield* runtime.startTurn(turnInput(thread));
2761+
for (let retry = 0; retry < 6; retry += 1) {
2762+
yield* Queue.take(attempts);
2763+
yield* TestClock.adjust("30 seconds");
2764+
}
27602765
yield* Queue.take(attempts);
2761-
yield* TestClock.adjust("30 seconds");
2762-
}
2763-
yield* Queue.take(attempts);
2764-
yield* Deferred.succeed(allowRecovery, undefined);
2765-
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2766-
}).pipe(Effect.scoped),
2766+
yield* Deferred.succeed(allowRecovery, undefined);
2767+
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
2768+
}).pipe(Effect.scoped),
27672769
);
27682770

27692771
it.effect(

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ const OWNED_RECONNECT_SCHEDULE = Schedule.exponential(RECONNECT_DELAY).pipe(
544544
Effect.succeed(Duration.min(duration, Duration.seconds(30))),
545545
),
546546
);
547+
const TRANSIENT_HTTP_STATUSES: ReadonlySet<number> = new Set([408, 425, 429]);
547548
// A live owned server is retried until it answers; these failures repeat however long it waits.
548549
const isPermanentRecoveryError = (error: unknown) => {
549550
if (Schema.isSchemaError(error)) return true;
@@ -557,9 +558,16 @@ const isPermanentRecoveryError = (error: unknown) => {
557558
const http = OpenCode2Server.httpFailureOf(error);
558559
if (http !== undefined) {
559560
const status = http.response?.status;
560-
return status !== undefined && status >= 400 && status < 500;
561+
return (
562+
status !== undefined && status >= 400 && status < 500 && !TRANSIENT_HTTP_STATUSES.has(status)
563+
);
561564
}
562-
return P.isTagged(error, "ClientError");
565+
// ClientError also wraps an SSE retry request, which clears.
566+
return (
567+
P.isTagged(error, "ClientError") &&
568+
P.hasProperty(error, "cause") &&
569+
Schema.isSchemaError(error.cause)
570+
);
563571
};
564572
const OWNED_RECOVERY_RETRY = {
565573
schedule: OWNED_RECONNECT_SCHEDULE,

0 commit comments

Comments
 (0)