Hmbown/CodeWhale · error

context window must be greater than 0

Error message

context window must be greater than 0

What it means

save_provider_context_window_for_identity persists a provider's context window size into the TOML config file. Before touching the file it validates the incoming value with anyhow::ensure!, and throws this error when the supplied context_window is zero. A zero context window is meaningless for token budgeting, so the write is refused.

Solutions

  1. Check the caller and ensure a parsed, positive context_window u32 is passed before saving
  2. Validate/parse the UI input earlier so blank or 0 values are rejected or defaulted before reaching persistence
  3. If the provider genuinely has unknown context, use a sane positive default (e.g. 128000) rather than 0

Example fix

// before
save_provider_context_window_for_identity(&identity, &cfg, 0)?;
// after
let ctx: u32 = ctx_input.parse()?;
anyhow::ensure!(ctx > 0, "context window must be positive");
save_provider_context_window_for_identity(&identity, &cfg, ctx)?;
Defensive patterns

Strategy: validation

Validate before calling

fn valid_context_window(v: u32) -> bool { v > 0 }
anyhow::ensure!(valid_context_window(ctx), "context window must be greater than 0");

Try / catch

match save_provider_context_window_for_identity(&id, &cfg, ctx) {
    Err(e) if e.to_string().contains("context window must be greater than 0") => warn_user_and_reprompt(),
    other => other?,
}

Prevention

When it happens

Trigger: Calling save_provider_context_window_for_identity with context_window == 0, typically because the UI layer passed an unparsed/empty input that defaulted to 0, or a provider entry without a context-window value was saved.

Common situations: Saving provider settings from a form where the context-window field was left blank or non-numeric; programmatically constructing a ProviderIdentity snapshot without setting context_window; importing provider config where the field is absent.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/316c9b29d0230e29. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/config.rs:12753

    crate::config_persistence::mutate_config_document(&config_path, |doc| {
        crate::config_persistence::set_document_value(
            doc,
            &["providers", key_inside, "base_url"],
            base_url,
        )
    })
    .with_context(|| format!("Failed to write config to {}", config_path.display()))?;
    Ok(config_path)
}

/// Persist a guided-setup context-window choice without replacing the user's
/// surrounding TOML comments or formatting.
pub(crate) fn save_provider_context_window_for_identity(
    identity: &ProviderIdentity,
    _route_config: &Config,
    context_window: u32,
) -> Result<PathBuf> {
    anyhow::ensure!(context_window > 0, "context window must be greater than 0");
    let config_path = try_default_config_path()
        .context("Failed to resolve config path for provider context window.")?;
    ensure_parent_dir(&config_path)?;
    let key_inside = if identity.provider == ApiProvider::Custom {
        let key = identity.key.trim();
        anyhow::ensure!(!key.is_empty(), "custom provider id cannot be empty");
        key
    } else {
        provider_config_key(identity.provider).context("provider context window table")?
    };
    crate::config_persistence::mutate_config_document(&config_path, |doc| {
        crate::config_persistence::set_document_value(
            doc,
            &["providers", key_inside, "context_window"],
            i64::from(context_window),
        )
    })
    .with_context(|| format!("Failed to write config to {}", config_path.display()))?;

View on GitHub (pinned to 73e0f67d83)