BigPizzaV3/CodexPlusPlus · error · anyhow::Error

{label}必须大于 0

Error message

{label}必须大于 0

What it means

parse_optional_positive_u64 解析 profile 的上下文窗口 / 自动压缩阈值字符串:空串返回 None(表示不写入),非数字走「必须是正整数」错误,解析成功但值为 0 时走本错误「{label}必须大于 0」。label 是「上下文大小」或「压缩上下文大小」。结果最终写入 config.toml 的 model_context_window / model_auto_compact_token_limit。

Source

Thrown at crates/codex-plus-core/src/relay_config.rs:1544

fn validate_auth_json(auth_bytes: &[u8], path: &Path) -> anyhow::Result<()> {
    if auth_bytes.iter().all(|byte| byte.is_ascii_whitespace()) {
        return Ok(());
    }
    serde_json::from_slice::<Value>(auth_bytes)
        .with_context(|| format!("{} 不是有效 JSON", path.display()))?;
    Ok(())
}

fn parse_optional_positive_u64(value: &str, label: &str) -> anyhow::Result<Option<u64>> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Ok(None);
    }
    let parsed = trimmed
        .parse::<u64>()
        .with_context(|| format!("{label}必须是正整数"))?;
    if parsed == 0 {
        anyhow::bail!("{label}必须大于 0");
    }
    Ok(Some(parsed))
}

fn apply_context_limits_to_config(
    config_text: &str,
    context_window: &str,
    auto_compact_limit: &str,
) -> anyhow::Result<String> {
    let mut doc = parse_toml_document(config_text)?;
    if let Some(value) = parse_optional_positive_u64(context_window, "上下文大小")? {
        doc["model_context_window"] = toml_edit::value(value as i64);
    }
    if let Some(value) = parse_optional_positive_u64(auto_compact_limit, "压缩上下文大小")? {
        doc["model_auto_compact_token_limit"] = toml_edit::value(value as i64);
    }
    Ok(normalize_optional_toml(doc))
}

View on GitHub (pinned to 1f431ae49b)

Solutions

  1. 想关闭限制就把对应输入留空(空 = 不写入该键)
  2. 填 ≥1 的正整数(如 128000)
  3. UI 层用 min=1 的数字输入,把 0 视为空提交

Example fix

# before (profile settings)
context_window = "0"
auto_compact_limit = ""

# after
context_window = "128000"
auto_compact_limit = ""   # 留空 = 不写入 model_auto_compact_token_limit
Defensive patterns

Strategy: validation

Validate before calling

fn normalize_positive_u64_field(value: &str) -> String {
    let t = value.trim();
    if t.parse::<u64>().map(|v| v == 0).unwrap_or(false) {
        String::new() // 把 0 规范化为空 = 不限制
    } else {
        t.to_string()
    }
}
// 提交 profile 前对 context_window / auto_compact_limit 做该规范化

Try / catch

if let Err(err) = apply_relay_profile_to_home_with_switch_rules_and_computer_use_guard(&home, &profile, &common, guard) {
    if err.to_string().contains("必须大于 0") {
        // 提示:0 无效,留空表示不限制
    } else { return Err(err); }
}

Prevention

When it happens

Trigger: 调用链 apply_relay_profile_to_home_* -> apply_context_limits_to_config 时,context_window 或 auto_compact_limit 传了 "0"(或 "00" 等解析为 0 的串)。留空不会触发;非数字触发的是另一条消息。

Common situations: 用户想表达「不限制上下文」填 0(本实现里 0 非法,应留空);UI 数字输入框默认值 0 被原样提交;从旧配置迁移时把 0 语义带了过来。

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@1f431ae49b (2026-08-16). Data as JSON: /api/errors/1c51fce957320795. Report an issue: GitHub.