BigPizzaV3/CodexPlusPlus · error · anyhow::Error

未知上下文类型:{other}

Error message

未知上下文类型:{other}

What it means

context_table_name 把调用方传入的 kind 字符串映射为 common config 中的表名:mcp/mcpServer/mcpServers -> mcp_servers,skill/skills -> skills,plugin/plugins -> plugins。其他任何值都 bail「未知上下文类型:{other}」。upsert/delete/filter 等所有上下文条目操作都经过此映射。

Source

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

                toml_body: body,
                enabled: context_entry_enabled(table),
            })
        })
        .collect()
}

fn table_body_to_string(table: &Table) -> String {
    let mut doc = DocumentMut::new();
    merge_toml_table_like(doc.as_table_mut(), table);
    normalize_optional_toml(doc)
}

fn context_table_name(kind: &str) -> anyhow::Result<&'static str> {
    match kind {
        "mcp" | "mcpServer" | "mcpServers" => Ok("mcp_servers"),
        "skill" | "skills" => Ok("skills"),
        "plugin" | "plugins" => Ok("plugins"),
        other => anyhow::bail!("未知上下文类型:{other}"),
    }
}

fn context_kind_name(table: &str) -> &'static str {
    match table {
        "mcp_servers" => "mcp",
        "skills" => "skill",
        "plugins" => "plugin",
        _ => "unknown",
    }
}

fn context_entry_summary(body: &str) -> String {
    body.lines()
        .map(str::trim)
        .find(|line| !line.is_empty() && !line.starts_with('#'))
        .unwrap_or("")
        .chars()

View on GitHub (pinned to 1f431ae49b)

Solutions

  1. 改用受支持的字面量之一:mcp / mcpServer / mcpServers、skill / skills、plugin / plugins
  2. 在前端定义枚举并在序列化层约束取值,避免自由字符串透传
  3. 新增类型时同步修改 context_table_name 和 context_kind_name 两个映射

Example fix

// before
upsert_context_entry_in_common_config(&common, "mcp_servers", "fetch", &body)?;

// after
upsert_context_entry_in_common_config(&common, "mcp", "fetch", &body)?;
Defensive patterns

Strategy: type-guard

Validate before calling

const VALID_CONTEXT_KINDS: &[&str] = &["mcp", "mcpServer", "mcpServers", "skill", "skills", "plugin", "plugins"];
anyhow::ensure!(VALID_CONTEXT_KINDS.contains(&kind), "未知上下文类型:{kind}");
upsert_context_entry_in_common_config(&common, kind, id, body)?;

Type guard

fn is_valid_context_kind(kind: &str) -> bool {
    matches!(kind, "mcp" | "mcpServer" | "mcpServers" | "skill" | "skills" | "plugin" | "plugins")
}

Try / catch

match upsert_context_entry_in_common_config(&common, kind, id, body) {
    Ok(c) => common = c,
    Err(err) if err.to_string().contains("未知上下文类型") => { /* 修正 kind 后重试 */ }
    Err(err) => return Err(err),
}

Prevention

When it happens

Trigger: 调用 upsert_context_entry_in_common_config / delete_context_entry_from_common_config / list_context_entries_from_common_config 时 kind 传了受支持别名之外的字符串,例如 "mcp_servers"(下划线形式不在别名列表里)、"tools"、""。

Common situations: 前端把自己内部的枚举值(下划线命名)直接透传给后端;新增上下文类型时只改了前端没改这个 match;拼写大小写不一致(只有列出的精确别名被接受)。

Related errors


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