BigPizzaV3/CodexPlusPlus · error

上下文 id 不能为空

Error message

上下文 id 不能为空

What it means

upsert_context_entry_in_common_config 用于向公共配置(common config)的 mcp_servers/skills/plugins 表里插入或更新一个条目(按 id 为键)。函数首先对 id trim 非空校验,为空立即 bail——因为空 id 无法成为 TOML 键。

Source

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

) -> anyhow::Result<CodexContextEntries> {
    let normalized = normalize_duplicate_toml_text(common_config);
    let doc = parse_toml_document(&normalized)?;
    Ok(CodexContextEntries {
        mcp_servers: list_context_entries_for_table(&doc, "mcp_servers"),
        skills: list_context_entries_for_table(&doc, "skills"),
        plugins: list_context_entries_for_table(&doc, "plugins"),
    })
}

pub fn upsert_context_entry_in_common_config(
    common_config: &str,
    kind: &str,
    id: &str,
    toml_body: &str,
) -> anyhow::Result<String> {
    let id = id.trim();
    if id.is_empty() {
        anyhow::bail!("上下文 id 不能为空");
    }
    let table_name = context_table_name(kind)?;
    let body_doc = parse_toml_document(toml_body)?;
    let normalized = normalize_duplicate_toml_text(common_config);
    let mut doc = parse_toml_document(&normalized)?;
    if !doc.as_table().contains_key(table_name) {
        doc[table_name] = toml_edit::table();
    }
    if doc[table_name].as_table().is_none() {
        anyhow::bail!("{table_name} 必须是 TOML 表");
    }
    doc[table_name][id] = Item::Table(body_doc.as_table().clone());
    Ok(normalize_optional_toml(doc))
}

pub fn delete_context_entry_from_common_config(
    common_config: &str,
    kind: &str,

View on GitHub (pinned to 1f431ae49b)

Solutions

  1. 为条目填写非空 id(如 MCP server 名)再调用
  2. 调用前对 id.trim() 做表单校验
  3. 导入流程中对缺失 id 的条目跳过或生成默认 id

Example fix

// before
let cfg = upsert_context_entry_in_common_config(&common, "mcp", "", &body)?;

// after
let id = id.trim();
anyhow::ensure!(!id.is_empty(), "上下文 id 不能为空");
let cfg = upsert_context_entry_in_common_config(&common, "mcp", id, &body)?;
Defensive patterns

Strategy: validation

Validate before calling

let id = id.trim();
anyhow::ensure!(!id.is_empty(), "上下文 id 不能为空");
upsert_context_entry_in_common_config(&common, kind, id, toml_body)?;

Try / catch

match upsert_context_entry_in_common_config(&common, kind, id, body) {
    Ok(new_common) => common = new_common,
    Err(err) if err.to_string().contains("上下文 id 不能为空") => { /* 表单提示 */ }
    Err(err) => return Err(err),
}

Prevention

When it happens

Trigger: 调用 upsert_context_entry_in_common_config(common_config, kind, id, toml_body) 时 id.trim() 为空。kind 非法走的是另一个错误([72] 未知上下文类型),且本校验先于 kind 校验执行。

Common situations: MCP/Skill/Plugin 编辑器里 id/名称输入框为空就点保存;从导入数据里取 id 字段时缺失;用空格命名条目。

Related errors


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