@@ -4,7 +4,7 @@ import { SessionV1 } from "@opencode-ai/core/v1/session" |
4 | 4 | import type { NamedError } from "@opencode-ai/core/util/error" |
5 | 5 | import { APICallError } from "ai" |
6 | 6 | import { setTimeout as sleep } from "node:timers/promises" |
7 | | -import { Effect, Schedule, Schema } from "effect" |
| 7 | +import { Effect, Fiber, Schedule, Schema } from "effect" |
8 | 8 | import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
9 | 9 | import { SessionRetry } from "../../src/session/retry" |
10 | 10 | import { MessageV2 } from "../../src/session/message-v2" |
@@ -117,6 +117,82 @@ describe("session.retry.delay", () => { |
117 | 117 | ) |
118 | 118 | }) |
119 | 119 |
|
| 120 | +describe("session.retry.policy.limits", () => { |
| 121 | + function policyStep(seen: { count: number; next?: number }, maxAttempts?: number) { |
| 122 | + return Schedule.toStepWithMetadata( |
| 123 | + SessionRetry.policy({ |
| 124 | + provider: "test", |
| 125 | + parse: Schema.decodeUnknownSync(SessionV1.APIError.Schema), |
| 126 | + set: (info) => |
| 127 | + Effect.sync(() => { |
| 128 | + seen.count++ |
| 129 | + seen.next = info.next |
| 130 | + }), |
| 131 | + maxAttempts, |
| 132 | + }), |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + // the schedule step sleeps its computed wait, so for long waits we fork the |
| 137 | + // step, assert the published status, and interrupt instead of sleeping |
| 138 | + function firstWait(seen: { count: number; next?: number }, error: SessionV1.APIError) { |
| 139 | + return Effect.gen(function* () { |
| 140 | + const step = yield* policyStep(seen) |
| 141 | + const before = Date.now() |
| 142 | + const fiber = yield* Effect.forkChild(step(error)) |
| 143 | + yield* Effect.promise(() => sleep(100)) |
| 144 | + yield* Fiber.interrupt(fiber) |
| 145 | + expect(seen.count).toBe(1) |
| 146 | + return seen.next! - before |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + it.instance("stops after RETRY_MAX_ATTEMPTS", () => |
| 151 | + Effect.gen(function* () { |
| 152 | + const error = apiError({ "retry-after-ms": "0" }) |
| 153 | + const seen = { count: 0 } |
| 154 | + const step = yield* policyStep(seen) |
| 155 | + // steps beyond the cap terminate the schedule (Cause.done surfaces as a |
| 156 | + // failure in the step Effect), so absorb with Effect.ignore |
| 157 | + for (let i = 0; i < SessionRetry.RETRY_MAX_ATTEMPTS + 2; i++) { |
| 158 | + yield* Effect.ignore(step(error)) |
| 159 | + } |
| 160 | + expect(seen.count).toBe(SessionRetry.RETRY_MAX_ATTEMPTS) |
| 161 | + }), |
| 162 | + ) |
| 163 | + |
| 164 | + it.instance("maxAttempts option overrides the default cap", () => |
| 165 | + Effect.gen(function* () { |
| 166 | + const error = apiError({ "retry-after-ms": "0" }) |
| 167 | + const seen = { count: 0 } |
| 168 | + const step = yield* policyStep(seen, 1) |
| 169 | + for (let i = 0; i < 3; i++) { |
| 170 | + yield* Effect.ignore(step(error)) |
| 171 | + } |
| 172 | + expect(seen.count).toBe(1) |
| 173 | + }), |
| 174 | + ) |
| 175 | + |
| 176 | + it.instance("caps individual waits at RETRY_MAX_WAIT", () => |
| 177 | + Effect.gen(function* () { |
| 178 | + // hard quota limits send retry-after of hours; never slumber the session |
| 179 | + const seen = { count: 0 } |
| 180 | + const wait = yield* firstWait(seen, apiError({ "retry-after": "21600" })) // 6 hours |
| 181 | + expect(wait).toBeLessThanOrEqual(SessionRetry.RETRY_MAX_WAIT + 1000) |
| 182 | + expect(wait).toBeGreaterThan(SessionRetry.RETRY_MAX_WAIT - 5000) |
| 183 | + }), |
| 184 | + ) |
| 185 | + |
| 186 | + it.instance("legitimate retry-after just above the cap is not aborted", () => |
| 187 | + Effect.gen(function* () { |
| 188 | + const seen = { count: 0 } |
| 189 | + const wait = yield* firstWait(seen, apiError({ "retry-after": "61" })) |
| 190 | + // capped to RETRY_MAX_WAIT, still retried (status update published) |
| 191 | + expect(wait).toBeLessThanOrEqual(SessionRetry.RETRY_MAX_WAIT + 1000) |
| 192 | + }), |
| 193 | + ) |
| 194 | +}) |
| 195 | + |
120 | 196 | describe("session.retry.retryable", () => { |
121 | 197 | test("maps too_many_requests json messages", () => { |
122 | 198 | const error = wrap(JSON.stringify({ type: "error", error: { type: "too_many_requests" } })) |
|