Skip to content
This repository was archived by the owner on Oct 16, 2026. It is now read-only.

Commit 0146e93

Browse files
committed
Fix opencode-go limit judgment: tunable thresholds + zen credit detection
opencode-go (opencode zen) exposes no usage/limit API — the local DB holds no quota state (see anomalyco/opencode#10448), and `cost` is a display-only token-price estimate, machine-local. The previous heuristic (sum local cost over sliding 5h/weekly/monthly windows vs hardcoded $12/$30/$60) has no correspondence to zen's real model (credit balance + monthly cap), so it under-reports "limited" and the provider gets selected while actually rate-limited. * Make the three USD thresholds configurable via env (`SEHER_OPENCODE_5H_LIMIT_USD` / `_WEEKLY_` / `_MONTHLY_`), default to the previous values. A value of `0` disables that window — lets the user drop the invented 5h/weekly windows and keep only a monthly cap matching their zen plan. * `OpencodeGoUsageWindow`: `limit_usd <= 0` now means *disabled* (never limited, 0% utilization), instead of always-limited / 100%. * Broaden `is_pi_limit` to catch zen's credit-exhaustion wording ("usage exceeded" / "add credits" / "insufficient credits" / "out of credits" / "credit balance"), so when zen is driven through pi's OpenAI-compatible endpoint the run-time error triggers retry-on-limit — the only reliable signal absent a usage API. Tests: +5 (disabled window, env default fallback, threshold limited, zen credit phrases). 146 total.
1 parent 9204765 commit 0146e93

3 files changed

Lines changed: 128 additions & 15 deletions

File tree

‎crates/seher-sdk/src/opencode_go/local.rs‎

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,65 @@ const FIVE_HOUR_LIMIT_USD: f64 = 12.0;
1111
const WEEKLY_LIMIT_USD: f64 = 30.0;
1212
const MONTHLY_LIMIT_USD: f64 = 60.0;
1313

14-
const WINDOW_SPECS: [WindowSpec; 3] = [
15-
WindowSpec {
14+
// Env overrides for the per-window USD limits. opencode zen exposes no usage
15+
// API (the local DB has no quota state — see docs/Issue #10448), so this spend
16+
// heuristic is only an *estimate* against thresholds that may not match the
17+
// user's actual plan. These let the user tune the thresholds to their zen
18+
// monthly usage limit, or set a window to `0` to disable it entirely.
19+
const ENV_FIVE_HOUR_LIMIT: &str = "SEHER_OPENCODE_5H_LIMIT_USD";
20+
const ENV_WEEKLY_LIMIT: &str = "SEHER_OPENCODE_WEEKLY_LIMIT_USD";
21+
const ENV_MONTHLY_LIMIT: &str = "SEHER_OPENCODE_MONTHLY_LIMIT_USD";
22+
23+
struct WindowDef {
24+
entry_type: &'static str,
25+
window_seconds: i64,
26+
default_limit_usd: f64,
27+
env_var: &'static str,
28+
}
29+
30+
const WINDOW_DEFS: [WindowDef; 3] = [
31+
WindowDef {
1632
entry_type: "five_hour_spend",
1733
window_seconds: 5 * 60 * 60,
18-
limit_usd: FIVE_HOUR_LIMIT_USD,
34+
default_limit_usd: FIVE_HOUR_LIMIT_USD,
35+
env_var: ENV_FIVE_HOUR_LIMIT,
1936
},
20-
WindowSpec {
37+
WindowDef {
2138
entry_type: "weekly_spend",
2239
window_seconds: 7 * 24 * 60 * 60,
23-
limit_usd: WEEKLY_LIMIT_USD,
40+
default_limit_usd: WEEKLY_LIMIT_USD,
41+
env_var: ENV_WEEKLY_LIMIT,
2442
},
25-
WindowSpec {
43+
WindowDef {
2644
entry_type: "monthly_spend",
2745
window_seconds: 30 * 24 * 60 * 60,
28-
limit_usd: MONTHLY_LIMIT_USD,
46+
default_limit_usd: MONTHLY_LIMIT_USD,
47+
env_var: ENV_MONTHLY_LIMIT,
2948
},
3049
];
3150

51+
/// Resolve the effective USD limit for a window: the value of `env_var` if it
52+
/// parses as a finite `f64`, otherwise `default`. A parsed value of `0` (or
53+
/// negative) disables the window (never limited).
54+
fn resolve_limit(env_var: &str, default: f64) -> f64 {
55+
std::env::var(env_var)
56+
.ok()
57+
.and_then(|v| v.trim().parse::<f64>().ok())
58+
.filter(|v| v.is_finite())
59+
.unwrap_or(default)
60+
}
61+
62+
fn window_specs() -> Vec<WindowSpec> {
63+
WINDOW_DEFS
64+
.iter()
65+
.map(|d| WindowSpec {
66+
entry_type: d.entry_type,
67+
window_seconds: d.window_seconds,
68+
limit_usd: resolve_limit(d.env_var, d.default_limit_usd),
69+
})
70+
.collect()
71+
}
72+
3273
const LIMIT_EPSILON: f64 = 1e-9;
3374

3475
#[derive(Debug, Error)]
@@ -207,9 +248,9 @@ impl OpencodeGoUsageStore {
207248
records: &[UsageRecord],
208249
credentials_available: bool,
209250
) -> OpencodeGoUsageSnapshot {
210-
let windows = WINDOW_SPECS
211-
.iter()
212-
.map(|spec| Self::window_from_records(now, records, *spec))
251+
let windows = window_specs()
252+
.into_iter()
253+
.map(|spec| Self::window_from_records(now, records, spec))
213254
.collect();
214255

215256
OpencodeGoUsageSnapshot {
@@ -236,7 +277,9 @@ impl OpencodeGoUsageStore {
236277
.map(|record| record.cost_usd)
237278
.sum::<f64>();
238279

239-
let resets_at = if spent_usd + LIMIT_EPSILON >= spec.limit_usd {
280+
// A non-positive limit means the window is disabled (never limited).
281+
let limited = spec.limit_usd > 0.0 && spent_usd + LIMIT_EPSILON >= spec.limit_usd;
282+
let resets_at = if limited {
240283
let mut remaining = spent_usd;
241284
active_records.iter().find_map(|record| {
242285
remaining -= record.cost_usd;
@@ -354,6 +397,51 @@ mod tests {
354397
);
355398
}
356399

400+
#[test]
401+
fn disabled_window_is_never_limited() {
402+
// limit_usd <= 0 means the window is disabled, even if spend is huge.
403+
let now = Utc
404+
.timestamp_millis_opt(10 * 60 * 60 * 1000)
405+
.single()
406+
.unwrap();
407+
let records = vec![usage_record(9 * 60 * 60 * 1000, 999.0)];
408+
let spec = WindowSpec {
409+
entry_type: "five_hour_spend",
410+
window_seconds: 5 * 60 * 60,
411+
limit_usd: 0.0,
412+
};
413+
let w = OpencodeGoUsageStore::window_from_records(now, &records, spec);
414+
assert!(!w.is_limited());
415+
assert_eq!(w.resets_at, None);
416+
assert_eq!(w.utilization(), 0.0);
417+
}
418+
419+
#[test]
420+
fn resolve_limit_uses_default_when_env_absent() {
421+
// Use a unique var name unlikely to be set in the environment.
422+
assert_eq!(
423+
resolve_limit("SEHER_OPENCODE_TEST_UNSET_LIMIT_XYZ", 42.0),
424+
42.0
425+
);
426+
}
427+
428+
#[test]
429+
fn limited_window_above_threshold() {
430+
let now = Utc
431+
.timestamp_millis_opt(10 * 60 * 60 * 1000)
432+
.single()
433+
.unwrap();
434+
let records = vec![usage_record(9 * 60 * 60 * 1000, 13.0)];
435+
let spec = WindowSpec {
436+
entry_type: "five_hour_spend",
437+
window_seconds: 5 * 60 * 60,
438+
limit_usd: 12.0,
439+
};
440+
let w = OpencodeGoUsageStore::window_from_records(now, &records, spec);
441+
assert!(w.is_limited());
442+
assert!(w.resets_at.is_some());
443+
}
444+
357445
#[test]
358446
fn reads_only_opencode_go_assistant_messages_from_sqlite() -> TestResult {
359447
let tmp = tempfile::NamedTempFile::new()?;

‎crates/seher-sdk/src/opencode_go/types.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ pub struct OpencodeGoUsageWindow {
1616
}
1717

1818
impl OpencodeGoUsageWindow {
19+
/// A `limit_usd <= 0` window is **disabled** (the user opted out of this
20+
/// window via env): never limited, regardless of spend.
1921
#[must_use]
2022
pub fn is_limited(&self) -> bool {
21-
self.spent_usd + LIMIT_EPSILON >= self.limit_usd
23+
self.limit_usd > 0.0 && self.spent_usd + LIMIT_EPSILON >= self.limit_usd
2224
}
2325

2426
#[must_use]
2527
pub fn utilization(&self) -> f64 {
2628
if self.limit_usd <= 0.0 {
27-
100.0
29+
0.0
2830
} else {
2931
self.spent_usd / self.limit_usd * 100.0
3032
}

‎crates/seher-sdk/src/sdk/pi_runner.rs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,22 @@ const PI_LIMIT_TOKENS: &[&str] = &[
2626
"quota",
2727
];
2828

29-
/// Phrases matched as a multi-word substring (lowercased).
30-
const PI_LIMIT_PHRASES: &[&str] = &["rate limit", "usage limit", "too many requests"];
29+
/// Phrases matched as a multi-word substring (lowercased). Includes opencode
30+
/// zen's credit-exhaustion wording ("usage exceeded" / "add credits" /
31+
/// "insufficient credits"), since zen has no usage API and only surfaces the
32+
/// limit as a run-time error when driven through pi's OpenAI-compatible path.
33+
const PI_LIMIT_PHRASES: &[&str] = &[
34+
"rate limit",
35+
"usage limit",
36+
"usage exceeded",
37+
"too many requests",
38+
"add credits",
39+
"insufficient credit",
40+
"insufficient credits",
41+
"out of credits",
42+
"no credits",
43+
"credit balance",
44+
];
3145

3246
fn is_pi_limit(msg: &str) -> bool {
3347
let lower = msg.to_lowercase();
@@ -256,4 +270,13 @@ mod tests {
256270
// bare numeric 429 inside HTTP status text still matches (separated by space)
257271
assert!(is_pi_limit("status 429 returned"));
258272
}
273+
274+
#[test]
275+
fn detects_opencode_zen_credit_exhaustion() {
276+
// opencode zen surfaces the limit as a credit error, not a "rate limit".
277+
assert!(is_pi_limit("Free usage exceeded. Add credits to continue."));
278+
assert!(is_pi_limit("Insufficient credits"));
279+
assert!(is_pi_limit("You are out of credits"));
280+
assert!(is_pi_limit("credit balance too low"));
281+
}
259282
}

0 commit comments

Comments
 (0)