Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/opencode/src/cli/cmd/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ async function toolError(part: ToolPart) {
}
}

/** Retry delays longer than this are announced by a non-interactive run; shorter ones stay silent. */
const RUN_RETRY_NOTICE_MS = 10_000

export const RunCommand = effectCmd({
command: "run [message..]",
describe: "run silvercode with a message",
Expand Down Expand Up @@ -718,6 +721,7 @@ export const RunCommand = effectCmd({
let rootIdle = false
let lastText = ""
let error: string | undefined
let retryNoticed = 0

for await (const event of events.stream) {
if (
Expand Down Expand Up @@ -846,6 +850,28 @@ export const RunCommand = effectCmd({

if (event.type === "session.status" && event.properties.sessionID === sessionID) {
rootIdle = event.properties.status.type === "idle"
const status = event.properties.status
// Visibility only: a long provider-requested retry delay used to leave a
// headless run silent until killed (#555). Say that it is waiting, without
// changing the retry policy. Short retries stay silent.
// A later non-retry status (busy again after a successful step) ends the
// wait, so the next wait in the same run is announced too.
if (status.type !== "retry") retryNoticed = 0
if (status.type === "retry" && args.format !== "json") {
const wait = status.next - Date.now()
if (wait > RUN_RETRY_NOTICE_MS && status.attempt !== retryNoticed) {
retryNoticed = status.attempt
// A fixed category, never the provider's retry message: that text can carry raw
// response bodies, URLs or control characters.
const reason =
status.action?.reason === "free_tier_limit"
? "free usage limit reached"
: status.action?.reason === "account_rate_limit"
? "account rate limit"
: "retrying after a provider error"
UI.println(`waiting about ${Math.ceil(wait / 1000)}s before retrying (attempt ${status.attempt}): ${reason}`)
}
}
}
// A deferred/silent descendant delivery can arrive after the root's
// last idle event. Recheck on every event, not only another root
Expand Down
88 changes: 88 additions & 0 deletions packages/opencode/test/cli/run/run-retry-wait.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { cliIt } from "../../lib/cli-process"
import { reply } from "../../lib/llm-server"

// #555: a provider quota rejection that asks for a long retry delay must not
// leave a headless run silent until it is killed.
const quota = { error: { message: "Free usage limit reached", type: "quota_exceeded", code: "FreeUsageLimitError" } }
const config = (url: string) => ({
OPENCODE_CONFIG_CONTENT: JSON.stringify({
agent: { work: { finishTool: false } },
provider: {
opencode: {
npm: "@ai-sdk/openai-compatible",
options: { baseURL: url, apiKey: "public" },
models: { "local-quota-test": { name: "q", limit: { context: 128000, output: 4096 }, cost: { input: 0, output: 0 } } },
},
},
permission: { "*": "allow" },
}),
})

describe("headless run provider retry delay (#555)", () => {
const setup = (llm: { url: string }) => config(llm.url)

cliIt.live("announces a long provider retry wait and keeps the retry policy", ({ llm, opencode }) =>
Effect.gen(function* () {
const env = setup(llm)
yield* llm.text("initial session response")
const first = yield* opencode.run("initial request", { timeoutMs: 20_000, model: "opencode/local-quota-test", env })
expect(first.exitCode).toBe(0)
yield* llm.error(429, quota, { "retry-after": "12" })
yield* llm.text("recovered after the long wait")
const result = yield* opencode.run("quota rejection", {
timeoutMs: 40_000, extraArgs: ["--continue"], model: "opencode/local-quota-test", env,
})
// The CLI keeps its retry policy: it waits out the delay and then succeeds.
expect(result.exitCode).toBe(0)
expect(result.stdout).toContain("recovered after the long wait")
expect(result.stderr).toMatch(/waiting about \d+s before retrying \(attempt 1\)/)
expect(result.stderr).toContain("free usage limit reached")
expect(result.stderr).not.toContain("quota_exceeded")
}), 60_000)

cliIt.live("announces a long retry-after that is not a free-tier quota too", ({ llm, opencode }) =>
Effect.gen(function* () {
const env = setup(llm)
yield* llm.text("initial session response")
const first = yield* opencode.run("initial request", { timeoutMs: 20_000, model: "opencode/local-quota-test", env })
expect(first.exitCode).toBe(0)
yield* llm.error(429, { error: { message: "Too many requests, try again later", type: "rate_limit_error" } }, { "retry-after": "12" })
yield* llm.text("recovered after the long wait")
const result = yield* opencode.run("rate limited", {
timeoutMs: 40_000, extraArgs: ["--continue"], model: "opencode/local-quota-test", env,
})
expect(result.exitCode).toBe(0)
expect(result.stderr).toMatch(/waiting about \d+s before retrying \(attempt 1\): retrying after a provider error/)
expect(result.stderr).not.toContain("Too many requests")
}), 60_000)

cliIt.live("stays silent and still retries through a short provider delay", ({ llm, opencode }) =>
Effect.gen(function* () {
const env = setup(llm)
yield* llm.text("initial session response")
const first = yield* opencode.run("initial request", { timeoutMs: 20_000, model: "opencode/local-quota-test", env })
expect(first.exitCode).toBe(0)
yield* llm.error(429, quota, { "retry-after": "1" })
yield* llm.text("recovered after retry")
const result = yield* opencode.run("short retry", {
timeoutMs: 20_000, extraArgs: ["--continue"], model: "opencode/local-quota-test", env,
})
expect(result.exitCode).toBe(0)
expect(result.stdout).toContain("recovered after retry")
expect(result.stderr).not.toContain("waiting about")
}), 60_000)

cliIt.live("announces each long wait of a multi-step run, not only the first", ({ llm, opencode }) =>
Effect.gen(function* () {
yield* llm.error(429, quota, { "retry-after": "11" })
yield* llm.push(reply().text("first step").tool("bash", { command: "printf step", description: "First step" }))
yield* llm.error(429, quota, { "retry-after": "11" })
yield* llm.text("done after two waits")
const result = yield* opencode.run("two long waits", { timeoutMs: 50_000, extraArgs: ["--dangerously-skip-permissions"] })
expect(result.exitCode).toBe(0)
expect(result.stdout).toContain("done after two waits")
expect(result.stderr.match(/waiting about \d+s before retrying \(attempt 1\)/g)?.length).toBe(2)
}), 90_000)
})
Loading