{"record":{"id":"316c9b29d0230e29","repo":"Hmbown/CodeWhale","slug":"context-window-must-be-greater-than-0-316c9b","errorCode":null,"errorMessage":"context window must be greater than 0","messagePattern":"context window must be greater than 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/config.rs","lineNumber":12753,"sourceCode":"    crate::config_persistence::mutate_config_document(&config_path, |doc| {\n        crate::config_persistence::set_document_value(\n            doc,\n            &[\"providers\", key_inside, \"base_url\"],\n            base_url,\n        )\n    })\n    .with_context(|| format!(\"Failed to write config to {}\", config_path.display()))?;\n    Ok(config_path)\n}\n\n/// Persist a guided-setup context-window choice without replacing the user's\n/// surrounding TOML comments or formatting.\npub(crate) fn save_provider_context_window_for_identity(\n    identity: &ProviderIdentity,\n    _route_config: &Config,\n    context_window: u32,\n) -> Result<PathBuf> {\n    anyhow::ensure!(context_window > 0, \"context window must be greater than 0\");\n    let config_path = try_default_config_path()\n        .context(\"Failed to resolve config path for provider context window.\")?;\n    ensure_parent_dir(&config_path)?;\n    let key_inside = if identity.provider == ApiProvider::Custom {\n        let key = identity.key.trim();\n        anyhow::ensure!(!key.is_empty(), \"custom provider id cannot be empty\");\n        key\n    } else {\n        provider_config_key(identity.provider).context(\"provider context window table\")?\n    };\n    crate::config_persistence::mutate_config_document(&config_path, |doc| {\n        crate::config_persistence::set_document_value(\n            doc,\n            &[\"providers\", key_inside, \"context_window\"],\n            i64::from(context_window),\n        )\n    })\n    .with_context(|| format!(\"Failed to write config to {}\", config_path.display()))?;","sourceCodeStart":12735,"sourceCodeEnd":12771,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/config.rs#L12735-L12771","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the caller and ensure a parsed, positive context_window u32 is passed before saving","Validate/parse the UI input earlier so blank or 0 values are rejected or defaulted before reaching persistence","If the provider genuinely has unknown context, use a sane positive default (e.g. 128000) rather than 0"],"exampleFix":"// before\nsave_provider_context_window_for_identity(&identity, &cfg, 0)?;\n// after\nlet ctx: u32 = ctx_input.parse()?;\nanyhow::ensure!(ctx > 0, \"context window must be positive\");\nsave_provider_context_window_for_identity(&identity, &cfg, ctx)?;","handlingStrategy":"validation","validationCode":"fn valid_context_window(v: u32) -> bool { v > 0 }\nanyhow::ensure!(valid_context_window(ctx), \"context window must be greater than 0\");","typeGuard":null,"tryCatchPattern":"match save_provider_context_window_for_identity(&id, &cfg, ctx) {\n    Err(e) if e.to_string().contains(\"context window must be greater than 0\") => warn_user_and_reprompt(),\n    other => other?,\n}","preventionTips":["Parse the context-window input as u32 at the UI boundary and reject 0 immediately","Default missing context windows to a positive sentinel rather than 0","Add a unit test asserting 0 is rejected"],"tags":["config","validation","context-window"],"backgroundTag":"invalid-config-value","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}