From 148090b7acdc60bef2947299cf58b404407967c4 Mon Sep 17 00:00:00 2001 From: sunerpy Date: Sat, 1 Aug 2026 23:47:11 +0800 Subject: [PATCH 1/3] fix(installer): treat array-of-tables as a TOML block boundary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit find_next_table_header skipped past a [[...]] line instead of stopping at it, so the header it was scanning for swallowed the array-of-tables block that followed. Every Codex mutation path shares that function, which means install, upgrade and uninstall all spliced out a user's trailing [[...]] block along with ours. A config carrying [[mcp_servers.other.env]] after our entry lost it on the next codegraph install, silently and irreversibly. Scan line by line instead, recognizing both [...] and [[...]] as boundaries through the dotted-key grammar upstream settled on, and carry a lexer state so a header-shaped line cannot be mistaken for a boundary while it sits inside a multiline string, a multi-line array or an inline table. The """ terminator honors backslash parity, so an escaped delimiter keeps the string open while a doubled backslash lets it close. toml_next_header_skips_array_of_tables asserted only that [a] went and [c] stayed, never that [[b]] survived — its own comment described the swallowing as intended. It is replaced by the preservation assertion it should always have made, alongside eleven cases covering both mutation paths, the three multi-line states, CRLF, quoted keys, non-ASCII bodies and both backslash-parity branches. The integration round-trip appends its trailing block only after the first install, because an absent header makes upsert append at end of file — a fixture written up front puts every sibling ahead of our block and never reaches the boundary this fixes. --- crates/codegraph-cli/src/installer/shared.rs | 309 +++++++++++++++++-- crates/codegraph-cli/tests/installer.rs | 26 ++ 2 files changed, 313 insertions(+), 22 deletions(-) diff --git a/crates/codegraph-cli/src/installer/shared.rs b/crates/codegraph-cli/src/installer/shared.rs index 6a284538..9877af0d 100644 --- a/crates/codegraph-cli/src/installer/shared.rs +++ b/crates/codegraph-cli/src/installer/shared.rs @@ -5,9 +5,11 @@ use std::fs; use std::path::Path; +use std::sync::OnceLock; use jsonc_parser::ParseOptions; use jsonc_parser::cst::{CstInputValue, CstRootNode}; +use regex::Regex; use serde_json::{Map, Value}; use super::types::{FileAction, FileWrite}; @@ -716,26 +718,151 @@ fn find_header_index(content: &str, header_line: &str) -> Option { content.find(&needle).map(|idx| idx + 1) } -/// Ports `findNextTableHeader` (toml.ts:140): next `\n[` skipping `\n[[`. +/// Find the next `[...]` or `[[...]]` TOML table header. Ports +/// `findNextTableHeader` (toml.ts:140). fn find_next_table_header(content: &str, from: usize) -> usize { - let bytes = content.as_bytes(); - let mut i = from; - while i < content.len() { - match content[i..].find("\n[") { - None => return content.len(), - Some(rel) => { - let nl_idx = i + rel; - if bytes.get(nl_idx + 2) == Some(&b'[') { - i = nl_idx + 2; - continue; - } - return nl_idx + 1; - } + let mut state = TomlLexState::default(); + let mut line_start = from; + let mut is_header_remainder = true; + + while line_start < content.len() { + let newline_idx = content[line_start..] + .find('\n') + .map(|relative| line_start + relative); + let line_end = newline_idx.unwrap_or(content.len()); + let line = &content[line_start..line_end]; + + if !is_header_remainder + && state.multiline_string.is_none() + && state.array_depth == 0 + && state.inline_table_depth == 0 + && is_toml_table_header(line) + { + return line_start; } + + scan_toml_line(line, &mut state); + let Some(newline_idx) = newline_idx else { + break; + }; + line_start = newline_idx + 1; + is_header_remainder = false; } + content.len() } +#[derive(Default)] +struct TomlLexState { + multiline_string: Option<&'static str>, + array_depth: usize, + inline_table_depth: usize, +} + +fn is_toml_table_header(line: &str) -> bool { + static TOML_TABLE_HEADER: OnceLock = OnceLock::new(); + TOML_TABLE_HEADER + .get_or_init(|| { + let key_part = r#"(?:[A-Za-z0-9_-]+|"(?:\\.|[^"\\])*"|'[^']*')"#; + let dotted_key = format!(r#"{key_part}(?:[ \t]*\.[ \t]*{key_part})*"#); + let table = format!(r#"\[[ \t]*{dotted_key}[ \t]*\]"#); + let array_table = format!(r#"\[\[[ \t]*{dotted_key}[ \t]*\]\]"#); + Regex::new(&format!( + r#"^[ \t]*(?:{table}|{array_table})[ \t]*(?:#.*)?\r?$"# + )) + .expect("static TOML table-header regex must compile") + }) + .is_match(line) +} + +/// Track value constructs that may legally span lines so bracket-shaped string +/// content and nested arrays cannot be mistaken for sibling table headers. +fn scan_toml_line(line: &str, state: &mut TomlLexState) { + let bytes = line.as_bytes(); + let mut i = 0; + while i < bytes.len() { + if let Some(delimiter) = state.multiline_string { + let Some(end) = find_multiline_string_end(bytes, i, delimiter) else { + return; + }; + i = end + delimiter.len(); + state.multiline_string = None; + continue; + } + + if bytes[i] == b'#' { + return; + } + + let multiline = if bytes[i..].starts_with(b"\"\"\"") { + Some("\"\"\"") + } else if bytes[i..].starts_with(b"'''") { + Some("'''") + } else { + None + }; + if let Some(delimiter) = multiline { + state.multiline_string = Some(delimiter); + i += delimiter.len(); + continue; + } + + match bytes[i] { + quote @ (b'\"' | b'\'') => { + i = skip_single_line_string(bytes, i, quote); + continue; + } + b'[' => state.array_depth += 1, + b']' if state.array_depth > 0 => state.array_depth -= 1, + b'{' => state.inline_table_depth += 1, + b'}' if state.inline_table_depth > 0 => state.inline_table_depth -= 1, + _ => {} + } + i += 1; + } +} + +fn find_multiline_string_end(line: &[u8], from: usize, delimiter: &'static str) -> Option { + let delimiter_bytes = delimiter.as_bytes(); + let mut cursor = from; + while cursor + delimiter_bytes.len() <= line.len() { + let relative = line[cursor..] + .windows(delimiter_bytes.len()) + .position(|window| window == delimiter_bytes)?; + let end = cursor + relative; + if delimiter != "\"\"\"" || !is_backslash_escaped(line, end) { + return Some(end); + } + cursor = end + 1; + } + None +} + +fn is_backslash_escaped(line: &[u8], index: usize) -> bool { + let mut backslashes = 0; + let mut cursor = index; + while cursor > 0 && line[cursor - 1] == b'\\' { + backslashes += 1; + cursor -= 1; + } + backslashes % 2 == 1 +} + +fn skip_single_line_string(line: &[u8], start: usize, quote: u8) -> usize { + let mut i = start + 1; + while i < line.len() { + if quote == b'\"' && line[i] == b'\\' { + i += 2; + continue; + } + if line[i] == quote { + return i + 1; + } + i += 1; + } + line.len() +} + #[cfg(test)] mod tests { use std::sync::atomic::{AtomicU64, Ordering}; @@ -981,14 +1108,152 @@ mod tests { } #[test] - fn toml_next_header_skips_array_of_tables() { - // Skipping `\n[[` folds the `[[b]]` block into `[a]`, so removing `[a]` - // also removes `[[b]]`, stopping at `[c]`. - let content = "[a]\nx = 1\n\n[[b]]\ny = 2\n\n[c]\nz = 3\n"; - let (removed, did) = remove_toml_table(content, "a"); - assert!(did); - assert!(!removed.contains("[a]")); - assert!(removed.contains("[c]")); + fn toml_remove_preserves_trailing_array_of_tables() { + let trailing = "[[mcp_servers.other.env]]\nname = \"KEEP_ME\"\nvalue = \"critical\"\n"; + let content = format!("[mcp_servers.codegraph]\ncommand = \"codegraph\"\n\n{trailing}"); + + let (removed, did_remove) = remove_toml_table(&content, "mcp_servers.codegraph"); + + assert!(did_remove); + assert_eq!(removed, trailing); + } + + #[test] + fn toml_upsert_preserves_trailing_array_of_tables() { + let trailing = "[[mcp_servers.other.env]]\nname = \"KEEP_ME\"\n"; + let content = format!("[mcp_servers.codegraph]\ncommand = \"old\"\n\n{trailing}"); + let block = "[mcp_servers.codegraph]\ncommand = \"codegraph\""; + + let (replaced, kind) = upsert_toml_table(&content, "mcp_servers.codegraph", block); + + assert_eq!(kind, TomlUpsert::Replaced); + assert!(replaced.contains(trailing)); + } + + #[test] + fn toml_header_shape_inside_multiline_string_is_not_a_boundary() { + let content = "[a]\ns = \"\"\"\n[not_a_header]\n\"\"\"\n[real]\nx = 1\n"; + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[real]").unwrap() + ); + } + + #[test] + fn toml_header_shape_inside_multiline_array_is_not_a_boundary() { + let content = "[a]\nvalues = [\n[nested]\n]\n[real]\nx = 1\n"; + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[real]").unwrap() + ); + } + + #[test] + fn toml_header_with_trailing_comment_is_a_boundary() { + let content = "[a]\nx = 1\n[foo] # comment\ny = 2\n"; + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[foo]").unwrap() + ); + } + + #[test] + fn toml_quoted_key_headers_are_boundaries() { + for header in ["[\"weird.key\"]", "['literal']"] { + let content = format!("[a]\nx = 1\n{header}\ny = 2\n"); + + assert_eq!( + find_next_table_header(&content, "[a]".len()), + content.find(header).unwrap(), + "failed to recognize {header}" + ); + } + } + + #[test] + fn toml_remove_preserves_plain_sibling_table() { + let sibling = "[mcp_servers.other]\ncommand = \"other\"\n"; + let content = format!("[mcp_servers.codegraph]\ncommand = \"codegraph\"\n\n{sibling}"); + + let (removed, did_remove) = remove_toml_table(&content, "mcp_servers.codegraph"); + + assert!(did_remove); + assert_eq!(removed, sibling); + } + + #[test] + fn toml_remove_preserves_crlf_array_of_tables_byte_for_byte() { + let trailing = "[[mcp_servers.other.env]]\r\nname = \"KEEP_ME\"\r\n"; + let content = + format!("[mcp_servers.codegraph]\r\ncommand = \"codegraph\"\r\n\r\n{trailing}"); + + let (removed, did_remove) = remove_toml_table(&content, "mcp_servers.codegraph"); + + assert!(did_remove); + assert_eq!(removed, trailing); + } + + #[test] + fn toml_header_shape_inside_multiline_inline_table_is_not_a_boundary() { + let content = concat!( + "[a]\n", + "x = { a = 1,\n", + "[not_a_header]\n", + " b = \"[not_a_header]\" }\n", + "[real]\n", + "x = 1\n", + ); + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[real]").unwrap() + ); + } + + #[test] + fn toml_odd_backslash_escaped_multiline_delimiter_stays_open() { + let content = r#"[a] +s = """text \""" still inside +[fake] +still inside""" +[real] +x = 1 +"#; + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[real]").unwrap() + ); + } + + #[test] + fn toml_non_ascii_content_is_char_boundary_safe() { + let trailing = "[[mcp_servers.other.env]]\nname = \"KEEP_ME\"\n"; + let content = format!("[mcp_servers.codegraph]\nname = \"配置项 🎯\"\n\n{trailing}"); + + let (removed, did_remove) = + std::panic::catch_unwind(|| remove_toml_table(&content, "mcp_servers.codegraph")) + .expect("non-ASCII TOML must not panic"); + + assert!(did_remove); + assert_eq!(removed, trailing); + } + + #[test] + fn toml_even_backslashes_allow_multiline_delimiter_to_close() { + let content = r#"[a] +s = """text \\""" +[real] +x = 1 +"#; + + assert_eq!( + find_next_table_header(content, "[a]".len()), + content.find("[real]").unwrap() + ); } #[test] diff --git a/crates/codegraph-cli/tests/installer.rs b/crates/codegraph-cli/tests/installer.rs index 1590958b..3d5969a3 100644 --- a/crates/codegraph-cli/tests/installer.rs +++ b/crates/codegraph-cli/tests/installer.rs @@ -186,6 +186,32 @@ fn codex_global_writes_toml_idempotent_then_uninstall() { assert!(content.contains("[mcp_servers.other]")); } +#[test] +fn codex_round_trip_preserves_trailing_array_of_tables() { + let fx = Fixture::new("codex-array-table"); + let toml = fx.home.join(".codex/config.toml"); + let trailing = "[[mcp_servers.other.env]]\nname = \"KEEP_ME\"\nvalue = \"critical\"\n"; + + fx.run(&["install", "--target=codex", "--global", "--yes"]); + let installed = fs::read_to_string(&toml).unwrap(); + assert!(installed.contains("[mcp_servers.codegraph]")); + + fs::write(&toml, format!("{installed}\n{trailing}")).unwrap(); + let before_reinstall = fs::read_to_string(&toml).unwrap(); + + fx.run(&["install", "--target=codex", "--global", "--yes"]); + assert_eq!( + fs::read_to_string(&toml).unwrap(), + before_reinstall, + "re-install must preserve the complete config byte-for-byte" + ); + + fx.run(&["uninstall", "--target=codex", "--global"]); + let uninstalled = fs::read_to_string(&toml).unwrap(); + assert_eq!(uninstalled, trailing); + assert!(!uninstalled.contains("[mcp_servers.codegraph]")); +} + #[test] fn opencode_local_uses_mcp_wrapper() { let fx = Fixture::new("opencode"); From c167412171f9e2453b2ad449974c40d56d2c626c Mon Sep 17 00:00:00 2001 From: sunerpy Date: Sun, 2 Aug 2026 00:18:09 +0800 Subject: [PATCH 2/3] fix(installer): find our TOML header with lexer state, not a substring scan The previous commit taught the block-END scan to respect multiline strings but left the block-START search a bare substring match, which turned out worse than the bug it followed. A line reading [mcp_servers.codegraph] at column zero inside a user's own """ or ''' string was accepted as our header, the end scan then started mid-string with fresh lexer state, read that string's closing delimiter as an opening one, and returned end-of-file. Everything from the decoy line onward was deleted and the string left unterminated. A config documenting the entry in a docstring lost 82 bytes and stopped parsing, while install reported success and exited 0. It reproduced identically three times and fires on a bare install with no target named. Scan line by line for the header too, sharing the lexer the end scan already uses, so both ends of the block agree on what a header is. A decoy that was already handled correctly stays that way: indented, inside a comment, inside a quoted array element, or sitting after the real header. The plan forbade touching find_header_index on the belief it was unaffected. Hands-on QA disproved that, so the constraint is superseded by evidence. Uninstall also rewrote a CRLF file's final newline as bare LF, since trim_end drops the carriage return; it now reuses whichever ending the file already had. --- crates/codegraph-cli/src/installer/shared.rs | 188 +++++++++++++++++- .../src/installer/targets/codex.rs | 42 +++- 2 files changed, 223 insertions(+), 7 deletions(-) diff --git a/crates/codegraph-cli/src/installer/shared.rs b/crates/codegraph-cli/src/installer/shared.rs index 9877af0d..0fc87838 100644 --- a/crates/codegraph-cli/src/installer/shared.rs +++ b/crates/codegraph-cli/src/installer/shared.rs @@ -709,13 +709,37 @@ pub fn remove_toml_table(content: &str, header: &str) -> (String, bool) { (format!("{before}{sep}{after}"), true) } -/// Ports `findHeaderIndex` (toml.ts:127): header at BOL or right after a newline. +/// Find our table header at a line start while ignoring header-shaped text inside +/// multiline values. This deliberately reuses the same lexer as the end scan so +/// block start and end cannot disagree about TOML state. fn find_header_index(content: &str, header_line: &str) -> Option { - if content.starts_with(header_line) { - return Some(0); + let mut state = TomlLexState::default(); + let mut line_start = 0; + + while line_start < content.len() { + let newline_idx = content[line_start..] + .find('\n') + .map(|relative| line_start + relative); + let line_end = newline_idx.unwrap_or(content.len()); + let line = &content[line_start..line_end]; + + if state.multiline_string.is_none() + && state.array_depth == 0 + && state.inline_table_depth == 0 + && line.starts_with(header_line) + && is_toml_table_header(line) + { + return Some(line_start); + } + + scan_toml_line(line, &mut state); + let Some(newline_idx) = newline_idx else { + break; + }; + line_start = newline_idx + 1; } - let needle = format!("\n{header_line}"); - content.find(&needle).map(|idx| idx + 1) + + None } /// Find the next `[...]` or `[[...]]` TOML table header. Ports @@ -1256,6 +1280,160 @@ x = 1 ); } + fn assert_decoy_upsert_preserves_user_data(content: &str) { + toml::from_str::(content).expect("test fixture must be valid TOML"); + let block = concat!( + "[mcp_servers.codegraph]\n", + "command = \"codegraph\"\n", + "args = [\"serve\", \"--mcp\"]", + ); + + let (updated, action) = upsert_toml_table(content, "mcp_servers.codegraph", block); + + if !updated.contains("important-user-tool") { + let validity = match toml::from_str::(&updated) { + Ok(_) => "resulting TOML unexpectedly remains valid".to_string(), + Err(error) => format!("resulting TOML is invalid: {error}"), + }; + panic!( + "upsert silently removed trailing user data: input={} bytes, output={} bytes; {validity}\n{updated}", + content.len(), + updated.len(), + ); + } + + assert_eq!(action, TomlUpsert::Inserted); + assert!(updated.starts_with(content.trim_end())); + toml::from_str::(&updated).expect("updated config must remain valid TOML"); + } + + #[test] + fn toml_upsert_ignores_column_zero_header_decoy_in_basic_multiline_string() { + let content = r#"model = "gpt-5" + +[settings] +instructions = """ +To wire up codegraph, add this to your config: + +[mcp_servers.codegraph] +command = "codegraph" +args = ["serve", "--mcp"] +""" + +[[mcp_servers.other.tools]] +name = "important-user-tool" +token = "keep-this" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_ignores_column_zero_header_decoy_in_literal_multiline_string() { + let content = r#"model = "gpt-5" + +[settings] +instructions = ''' +To wire up codegraph, add this to your config: + +[mcp_servers.codegraph] +command = "codegraph" +args = ["serve", "--mcp"] +''' + +[[mcp_servers.other.tools]] +name = "important-user-tool" +token = "keep-this" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_ignores_header_decoy_closed_on_same_content_line() { + let content = r#"model = "gpt-5" + +[settings] +instructions = """ +To wire up codegraph, add this to your config: +[mcp_servers.codegraph]""" + +[[mcp_servers.other.tools]] +name = "important-user-tool" +token = "keep-this" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_still_ignores_indented_header_decoy() { + let content = r#"[settings] +instructions = """ + [mcp_servers.codegraph] +""" + +[[mcp_servers.other.tools]] +name = "important-user-tool" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_still_ignores_header_decoy_in_comment() { + let content = r#"[settings] +# [mcp_servers.codegraph] +enabled = true + +[[mcp_servers.other.tools]] +name = "important-user-tool" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_still_ignores_header_decoy_as_quoted_array_element() { + let content = r#"[settings] +examples = [ + "[mcp_servers.codegraph]", +] + +[[mcp_servers.other.tools]] +name = "important-user-tool" +"#; + + assert_decoy_upsert_preserves_user_data(content); + } + + #[test] + fn toml_upsert_still_uses_real_header_before_multiline_decoy() { + let content = r#"[mcp_servers.codegraph] +command = "old" + +[settings] +instructions = """ +[mcp_servers.codegraph] +""" + +[[mcp_servers.other.tools]] +name = "important-user-tool" +"#; + let block = "[mcp_servers.codegraph]\ncommand = \"codegraph\""; + + let (updated, action) = upsert_toml_table(content, "mcp_servers.codegraph", block); + + assert_eq!(action, TomlUpsert::Replaced); + assert!(updated.contains("important-user-tool")); + assert!(updated.contains("instructions = \"\"\"\n[mcp_servers.codegraph]\n\"\"\"")); + let parsed = toml::from_str::(&updated).expect("updated TOML must stay valid"); + assert_eq!( + parsed["mcp_servers"]["codegraph"]["command"].as_str(), + Some("codegraph") + ); + } + #[test] fn read_outcome_missing_parsed_unparseable() { let dir = std::env::temp_dir().join(format!( diff --git a/crates/codegraph-cli/src/installer/targets/codex.rs b/crates/codegraph-cli/src/installer/targets/codex.rs index 93d4c459..61a98d5a 100644 --- a/crates/codegraph-cli/src/installer/targets/codex.rs +++ b/crates/codegraph-cli/src/installer/targets/codex.rs @@ -104,13 +104,20 @@ impl AgentTarget for CodexTarget { let mut files = Vec::new(); let toml_path = toml_config_path(ctx); if let Ok(content) = fs::read_to_string(&toml_path) { + let line_ending = if content.contains("\r\n") { + "\r\n" + } else { + "\n" + }; let (next_content, removed) = remove_toml_table(&content, TOML_HEADER); if removed { if next_content.trim().is_empty() { let _ = fs::remove_file(&toml_path); } else { - let _ = - atomic_write_file(&toml_path, &format!("{}\n", next_content.trim_end())); + let _ = atomic_write_file( + &toml_path, + &format!("{}{line_ending}", next_content.trim_end()), + ); } files.push(FileWrite { path: toml_path, @@ -361,6 +368,37 @@ mod tests { assert!(content.contains("[mcp_servers.other]")); } + #[test] + fn uninstall_preserves_crlf_line_endings() { + let fx = TempCodex::new("uninstall-crlf"); + let target = CodexTarget; + let toml = toml_config_path(&fx.ctx); + fs::create_dir_all(toml.parent().unwrap()).unwrap(); + fs::write( + &toml, + concat!( + "[mcp_servers.codegraph]\r\n", + "command = \"codegraph\"\r\n", + "args = [\"serve\", \"--mcp\"]\r\n", + "\r\n", + "[mcp_servers.other]\r\n", + "name = \"keep-crlf\"\r\n", + ), + ) + .unwrap(); + let expected = b"[mcp_servers.other]\r\nname = \"keep-crlf\"\r\n"; + + target.uninstall(&fx.ctx, Location::Global); + + let actual = fs::read(&toml).unwrap(); + assert_eq!( + actual, + expected, + "uninstall must preserve CRLF bytes; got {:?}", + String::from_utf8_lossy(&actual) + ); + } + #[test] fn print_config_global_shows_toml_block() { let fx = TempCodex::new("print"); From 374d4ff0efd390965741c029566896cacb48d656 Mon Sep 17 00:00:00 2001 From: sunerpy Date: Sun, 2 Aug 2026 00:38:26 +0800 Subject: [PATCH 3/3] fix(installer): match our TOML header when it is indented MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An indented [mcp_servers.codegraph] is valid TOML and idiomatic under a parent table, but the header search required column zero, so install appended a second entry and left a config Codex cannot load: duplicate key codegraph in table mcp_servers. The shipped 0.42.0 produces the same error, so this predates the batch — the old substring search never matched an indented header either. Ignore leading spaces and tabs when the lexer is at top level. The lexer gate is untouched, so an indented decoy inside a multiline string still does not match; only the column requirement relaxes. The header search now returns the matched line's real end alongside its start, because both callers derived the boundary scan's start from the bare header's length — arithmetic that silently pointed short of the line end once indentation was allowed. Handing back the actual end removes the assumption instead of patching it. A rewrite emits the canonical column-zero header. The block belongs to the installer, dropping indentation changes nothing semantically, and preserving it would add formatting state to carry. detect also reported already-configured from a plain substring test, which is what made the duplicate visible: it claimed configured, then appended anyway. It now shares the same lexer-aware search, so a decoy in a docstring no longer counts as an installation. --- crates/codegraph-cli/src/installer/shared.rs | 122 ++++++++++++++++-- .../src/installer/targets/codex.rs | 33 ++++- crates/codegraph-cli/tests/installer.rs | 40 ++++++ 3 files changed, 180 insertions(+), 15 deletions(-) diff --git a/crates/codegraph-cli/src/installer/shared.rs b/crates/codegraph-cli/src/installer/shared.rs index 0fc87838..259bfe63 100644 --- a/crates/codegraph-cli/src/installer/shared.rs +++ b/crates/codegraph-cli/src/installer/shared.rs @@ -669,19 +669,19 @@ pub enum TomlUpsert { /// `upsertTomlTable` (toml.ts:64). pub fn upsert_toml_table(content: &str, header: &str, block: &str) -> (String, TomlUpsert) { let header_line = format!("[{header}]"); - let Some(header_idx) = find_header_index(content, &header_line) else { + let Some(header_match) = find_header_index(content, &header_line) else { let trimmed = content.trim_end(); let sep = if trimmed.is_empty() { "" } else { "\n\n" }; return (format!("{trimmed}{sep}{block}\n"), TomlUpsert::Inserted); }; - let block_end = find_next_table_header(content, header_idx + header_line.len()); - let existing_block = content[header_idx..block_end].trim_end_matches('\n'); + let block_end = find_next_table_header(content, header_match.line_end); + let existing_block = content[header_match.start..block_end].trim_end_matches('\n'); if existing_block == block { return (content.to_string(), TomlUpsert::Unchanged); } - let before_clean = content[..header_idx].trim_end_matches('\n'); + let before_clean = content[..header_match.start].trim_end_matches('\n'); let after_clean = content[block_end..].trim_start_matches('\n'); let sep_before = if before_clean.is_empty() { "" } else { "\n\n" }; let sep_after = if after_clean.is_empty() { "\n" } else { "\n\n" }; @@ -695,11 +695,11 @@ pub fn upsert_toml_table(content: &str, header: &str, block: &str) -> (String, T /// (toml.ts:108). pub fn remove_toml_table(content: &str, header: &str) -> (String, bool) { let header_line = format!("[{header}]"); - let Some(header_idx) = find_header_index(content, &header_line) else { + let Some(header_match) = find_header_index(content, &header_line) else { return (content.to_string(), false); }; - let block_end = find_next_table_header(content, header_idx + header_line.len()); - let before = content[..header_idx].trim_end_matches('\n'); + let block_end = find_next_table_header(content, header_match.line_end); + let before = content[..header_match.start].trim_end_matches('\n'); let after = content[block_end..].trim_start_matches('\n'); let sep = if !before.is_empty() && !after.is_empty() { "\n\n" @@ -709,10 +709,17 @@ pub fn remove_toml_table(content: &str, header: &str) -> (String, bool) { (format!("{before}{sep}{after}"), true) } -/// Find our table header at a line start while ignoring header-shaped text inside +struct TomlHeaderMatch { + start: usize, + line_end: usize, +} + +/// Return our table header's line bounds while ignoring header-shaped text inside /// multiline values. This deliberately reuses the same lexer as the end scan so -/// block start and end cannot disagree about TOML state. -fn find_header_index(content: &str, header_line: &str) -> Option { +/// block start and end cannot disagree about TOML state. The line end lets callers +/// start the end scan after any leading indentation instead of assuming the bare +/// header's byte length. +fn find_header_index(content: &str, header_line: &str) -> Option { let mut state = TomlLexState::default(); let mut line_start = 0; @@ -726,10 +733,15 @@ fn find_header_index(content: &str, header_line: &str) -> Option { if state.multiline_string.is_none() && state.array_depth == 0 && state.inline_table_depth == 0 - && line.starts_with(header_line) + && line + .trim_start_matches(&[' ', '\t'][..]) + .starts_with(header_line) && is_toml_table_header(line) { - return Some(line_start); + return Some(TomlHeaderMatch { + start: line_start, + line_end, + }); } scan_toml_line(line, &mut state); @@ -742,6 +754,10 @@ fn find_header_index(content: &str, header_line: &str) -> Option { None } +pub(super) fn contains_toml_table(content: &str, header: &str) -> bool { + find_header_index(content, &format!("[{header}]")).is_some() +} + /// Find the next `[...]` or `[[...]]` TOML table header. Ports /// `findNextTableHeader` (toml.ts:140). fn find_next_table_header(content: &str, from: usize) -> usize { @@ -1124,6 +1140,88 @@ mod tests { assert!(unchanged_remove.contains("[other]")); } + #[test] + fn toml_upsert_replaces_indented_header_with_canonical_header() { + let content = concat!( + "# fixture — valid TOML\n", + "[[user.tools]]\n", + "name = \"keep\"\n", + "\n", + " [mcp_servers.codegraph]\n", + " command = \"old\"\n", + ); + let block = concat!( + "[mcp_servers.codegraph]\n", + "command = \"codegraph\"\n", + "args = [\"serve\", \"--mcp\"]", + ); + toml::from_str::(content).expect("fixture must be valid TOML"); + + let (updated, action) = upsert_toml_table(content, "mcp_servers.codegraph", block); + + toml::from_str::(&updated) + .expect("install replacement must not create a duplicate TOML table"); + assert_eq!(action, TomlUpsert::Replaced); + assert_eq!(updated.matches("[mcp_servers.codegraph]").count(), 1); + assert!( + updated + .lines() + .any(|line| line == "[mcp_servers.codegraph]") + ); + assert!( + !updated + .lines() + .any(|line| line == " [mcp_servers.codegraph]") + ); + assert!(updated.contains("name = \"keep\"")); + } + + #[test] + fn toml_remove_removes_indented_header() { + let content = concat!( + "[[user.tools]]\n", + "name = \"keep\"\n", + "\n", + " [mcp_servers.codegraph]\n", + " command = \"old\"\n", + ); + + let (updated, removed) = remove_toml_table(content, "mcp_servers.codegraph"); + + assert!(removed); + assert!(!updated.contains("[mcp_servers.codegraph]")); + assert!(updated.contains("name = \"keep\"")); + toml::from_str::(&updated).expect("uninstall result must be valid TOML"); + } + + #[test] + fn toml_upsert_replaces_tab_indented_header() { + let content = "[user]\nname = \"keep\"\n\n\t[mcp_servers.codegraph]\ncommand = \"old\"\n"; + let block = "[mcp_servers.codegraph]\ncommand = \"codegraph\""; + + let (updated, action) = upsert_toml_table(content, "mcp_servers.codegraph", block); + + assert_eq!(action, TomlUpsert::Replaced); + assert_eq!(updated.matches("[mcp_servers.codegraph]").count(), 1); + assert!( + updated + .lines() + .any(|line| line == "[mcp_servers.codegraph]") + ); + toml::from_str::(&updated).expect("updated TOML must stay valid"); + } + + #[test] + fn toml_upsert_still_replaces_column_zero_header() { + let content = "[mcp_servers.codegraph]\ncommand = \"old\"\n"; + let block = "[mcp_servers.codegraph]\ncommand = \"codegraph\""; + + let (updated, action) = upsert_toml_table(content, "mcp_servers.codegraph", block); + + assert_eq!(action, TomlUpsert::Replaced); + assert_eq!(updated, format!("{block}\n")); + } + #[test] fn toml_string_escaping_quotes_and_backslashes() { let block = build_toml_table("h", &[("path", TomlValue::Str("C:\\a\\\"b\""))]); diff --git a/crates/codegraph-cli/src/installer/targets/codex.rs b/crates/codegraph-cli/src/installer/targets/codex.rs index 61a98d5a..5c804d34 100644 --- a/crates/codegraph-cli/src/installer/targets/codex.rs +++ b/crates/codegraph-cli/src/installer/targets/codex.rs @@ -9,8 +9,8 @@ use std::path::PathBuf; use super::super::shared::{ self, CODEGRAPH_SECTION_END, CODEGRAPH_SECTION_START, TomlUpsert, TomlValue, atomic_write_file, - build_toml_table, mcp_server_config, remove_toml_table, upsert_instructions_entry, - upsert_toml_table, + build_toml_table, contains_toml_table, mcp_server_config, remove_toml_table, + upsert_instructions_entry, upsert_toml_table, }; use super::super::types::{ AgentTarget, DetectionResult, FileAction, FileWrite, InstallContext, InstallOptions, Location, @@ -67,7 +67,7 @@ impl AgentTarget for CodexTarget { } let toml_path = toml_config_path(ctx); let already_configured = fs::read_to_string(&toml_path) - .map(|c| c.contains(&format!("[{TOML_HEADER}]"))) + .map(|content| contains_toml_table(&content, TOML_HEADER)) .unwrap_or(false); DetectionResult { installed: config_dir(ctx).exists(), @@ -323,6 +323,33 @@ mod tests { assert_eq!(fs::read_to_string(&toml).unwrap(), first); } + #[test] + fn detect_uses_toml_lexer_for_real_and_decoy_headers() { + let fx = TempCodex::new("detect-toml-header"); + let target = CodexTarget; + let toml = toml_config_path(&fx.ctx); + fs::create_dir_all(toml.parent().unwrap()).unwrap(); + fs::write( + &toml, + concat!( + "[settings]\n", + "instructions = \"\"\"\n", + " [mcp_servers.codegraph]\n", + "\"\"\"\n", + ), + ) + .unwrap(); + + assert!(!target.detect(&fx.ctx, Location::Global).already_configured); + + fs::write( + &toml, + "[settings]\nenabled = true\n\n [mcp_servers.codegraph]\n command = \"old\"\n", + ) + .unwrap(); + assert!(target.detect(&fx.ctx, Location::Global).already_configured); + } + #[test] fn local_location_is_rejected() { let fx = TempCodex::new("local-reject"); diff --git a/crates/codegraph-cli/tests/installer.rs b/crates/codegraph-cli/tests/installer.rs index 3d5969a3..7eaef1ff 100644 --- a/crates/codegraph-cli/tests/installer.rs +++ b/crates/codegraph-cli/tests/installer.rs @@ -212,6 +212,46 @@ fn codex_round_trip_preserves_trailing_array_of_tables() { assert!(!uninstalled.contains("[mcp_servers.codegraph]")); } +#[test] +fn codex_replaces_indented_table_then_uninstalls_it() { + let fx = Fixture::new("codex-indented-table"); + let toml = fx.home.join(".codex/config.toml"); + let original = concat!( + "# fixture — valid TOML\n", + "[[user.tools]]\n", + "name = \"keep\"\n", + "\n", + " [mcp_servers.codegraph]\n", + " command = \"old\"\n", + ); + fs::create_dir_all(toml.parent().unwrap()).unwrap(); + fs::write(&toml, original).unwrap(); + toml::from_str::(original).expect("fixture must be valid TOML"); + + fx.run(&["install", "--target=codex", "--global", "--yes"]); + let installed = fs::read_to_string(&toml).unwrap(); + toml::from_str::(&installed) + .expect("install must replace the indented table without duplicating it"); + assert_eq!(installed.matches("[mcp_servers.codegraph]").count(), 1); + assert!( + installed + .lines() + .any(|line| line == "[mcp_servers.codegraph]") + ); + assert!( + !installed + .lines() + .any(|line| line == " [mcp_servers.codegraph]") + ); + assert!(installed.contains("name = \"keep\"")); + + fx.run(&["uninstall", "--target=codex", "--global"]); + let uninstalled = fs::read_to_string(&toml).unwrap(); + toml::from_str::(&uninstalled).expect("uninstall result must be valid TOML"); + assert!(!uninstalled.contains("[mcp_servers.codegraph]")); + assert!(uninstalled.contains("name = \"keep\"")); +} + #[test] fn opencode_local_uses_mcp_wrapper() { let fx = Fixture::new("opencode");