Repository: colbymchenry/codegraph (2026-07-20T05:07:28Z)

Issue:
Codex installer: findNextTableHeader skips past [[array-of-tables]], silently deleting sibling [[...]] blocks in ~/.codex/config.toml on install/upgrade/uninstall — verified
## Summary

The Codex installer's TOML block finder, `findNextTableHeader` in `src/installer/targets/toml.ts`, **skips past** `[[array-of-tables]]` headers instead of treating them as a boundary when locating the end of the `[mcp_servers.codegraph]` block. As a result, any `[[...]]` block located after the codegraph table in `~/.codex/config.toml` is treated as *part of* the codegraph block and is **silently deleted** — on uninstall (`removeTomlTable`), and on install/upgrade when the block is rewritten (`upsertTomlTable`'s `replaced` path). This contradicts the module's own doc comment ("Everything outside that block is preserved verbatim, byte-for-byte") and the intended invariant that sibling `[[array_of_tables]]` blocks survive. Verified against the real exported helpers that `codex.ts` calls.

## Root cause

```ts
// src/installer/targets/toml.ts:140-154
function findNextTableHeader(content: string, from: number): number {
  // Look for "\n[" but skip "\n[[" (array of tables).
  let i = from;
  while (i < content.length) {
    const nlIdx = content.indexOf('\n[', i);
    if (nlIdx === -1) return content.length;
    if (content[nlIdx + 2] === '[') {
      // [[...]] — keep searching past it.
      i = nlIdx + 2;      // <- skips the array-of-tables header instead of stopping at it
      continue;
    }
    return nlIdx + 1;
  }
  return content.length;
}
```

Because `\n[[` is skipped rather than treated as the end of the current block, `blockEnd` runs to EOF (or to the next single-bracket table), so everything from the codegraph header through the trailing `[[...]]` block is considered the codegraph block. `removeTomlTable` (`toml.ts:116-120`) and `upsertTomlTable`'s replace branch (`toml.ts:82-101`) then drop that range.

Both are on the real Codex install/uninstall path: `src/installer/targets/codex.ts:102` calls `removeTomlTable` on uninstall, and `codex.ts:155` calls `upsertTomlTable` on install/upgrade.

## Repro (executed against the real exported helpers)

```js
import { removeTomlTable, upsertTomlTable, buildTomlTable }
  from './dist/installer/targets/toml.js';

const file = `[mcp_servers.codegraph]
command = "codegraph"
args = ["serve", "--mcp"]

[[history]]
id = 1
note = "must survive"
`;

removeTomlTable(file, 'mcp_servers.codegraph');
// => { content: "", action: "removed" }        // the entire [[history]] block is gone

const newBlock = buildTomlTable('mcp_servers.codegraph',
  { command: 'codegraph', args: ['serve','--mcp','--v2'] });
upsertTomlTable(file, 'mcp_servers.codegraph', newBlock);
// => { content: '[mcp_servers.codegraph]\ncommand = "codegraph"\nargs = ["serve", "--mcp", "--v2"]\n',
//      action: "replaced" }                     // [[history]] dropped on the upgrade rewrite too
```

Expected: the `[[history]]` block is preserved (uninstall should leave `[[history]]` behind; upgrade should keep it below the rewritten codegraph table).

## Suggested direction

`findNextTableHeader` should treat a `\n[[` as a boundary too (stop there) — any new table header, single- or double-bracket, ends the current block. There is currently no test covering a sibling `[[array-of-tables]]` block in `installer-targets.test.ts`; a Codex fixture with a trailing `[[...]]` across install → upgrade-reinstall → uninstall would pin it.

## Environment

`main` @ `2d72891`, package `1.4.1`, Node 22.23.1 (Linux). Reproduces on a clean `npm ci && npm run build`.



Discussion:
Suggested label for triage (I lack permission to apply it myself): `bug`.
I am working on a focused fix for this: treat a trailing [[array-of-tables]] header as the end of the CodeGraph table, with regression coverage for rewrite and removal plus an Unreleased changelog entry.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e51d91235..f7c85dd85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ### Fixes
 
+- Codex installs, upgrades, and uninstalls now preserve TOML array-of-table sections that appear after CodeGraph's MCP configuration instead of accidentally removing them. (#1351)
 - TypeScript, TSX, and JavaScript files now parse with up-to-date grammars — modern syntax such as `using` declarations and import attributes no longer trips parse errors that could drop surrounding symbols. (The previously bundled grammars dated from 2023.)
 - Searching or exploring by field names now finds the code that defines them. A query made of object keys or API field names (`profileInfo isTrialEligible quotaInfo billingMethod`) used to return unrelated results while the defining files never appeared, because three retrieval steps each dropped multi-word camelCase terms: an internal case-comparison bug, a match step that only considered classes (never functions or methods), and exploration seeding that required exact symbol-name matches. All three are fixed — `codegraph_explore` with a bag of field names now surfaces the controllers and services that assemble those fields. (#1196)
 - `codegraph.json`'s `includeIgnored` works again for the "folder of repos" layout: when one `.gitignore` rule covers a parent directory (`/repos/`) holding several embedded git repositories, opting in the individual repos (`"includeIgnored": ["repos/a/"]` — the exact spelling `codegraph init`'s own hint suggests) previously matched nothing and indexed zero files, looping the same suggestion back at you. Both spellings now work — name the parent directory to opt in everything under it, or name individual repos to opt in just those — and the hint no longer re-suggests repos that are already configured. (#1295)
diff --git a/src/installer/targets/toml.ts b/src/installer/targets/toml.ts
index 29348a7c9..1dc086bf3 100644
--- a/src/installer/targets/toml.ts
+++ b/src/installer/targets/toml.ts
@@ -7,13 +7,14 @@
  *
  * Strategy: treat the file as text. Find the `[mcp_servers.codegraph]`
  * header line, splice it (and the lines that follow it until the next
- * `[...]` header or EOF) in or out. Everything outside that block is
- * preserved verbatim, byte-for-byte.
+ * `[...]` / `[[...]]` header or EOF) in or out. A small lexical scan keeps
+ * header-shaped text inside multiline values out of the boundary search.
+ * Everything outside that block is preserved verbatim, byte-for-byte.
  *
  * Limitations (acceptable for our narrow use):
- *   - Only handles top-level table headers; not array-of-tables or
- *     subtables nested inside `[mcp_servers]` itself (we always write
- *     the full dotted key `[mcp_servers.codegraph]`).
+ *   - Only writes a top-level table header. Array-of-tables and sibling
+ *     subtables are preserved as opaque blocks (we always write the full
+ *     dotted key `[mcp_servers.codegraph]`).
  *   - Doesn't validate sibling TOML — if the file is malformed
  *     elsewhere, our injection won't fix it but won't make it worse.
  *   - Quotes string values with double quotes; escapes `\` and `"`.
@@ -79,7 +80,7 @@ export function upsertTomlTable(
     };
   }
 
-  // Find the end of this block: next `[...]` header (at line start) or EOF.
+  // Find the end of this block: next table header or EOF.
   const blockEnd = findNextTableHeader(fileContent, headerIdx + headerLine.length);
   const existingBlock = fileContent.substring(headerIdx, blockEnd).replace(/\n+$/, '');
 
@@ -133,22 +134,121 @@ function findHeaderIndex(content: string, headerLine: string): number {
 }
 
 /**
- * Find the byte index of the next top-level `[...]` table header
- * (excluding array-of-tables `[[...]]`) starting from `from`, or
- * return content length when none.
+ * Find the byte index of the next `[...]` or `[[...]]` table header
+ * starting from `from`, or return content length when none.
  */
 function findNextTableHeader(content: string, from: number): number {
-  // Look for "\n[" but skip "\n[[" (array of tables).
-  let i = from;
-  while (i < content.length) {
-    const nlIdx = content.indexOf('\n[', i);
-    if (nlIdx === -1) return content.length;
-    if (content[nlIdx + 2] === '[') {
-      // [[...]] — keep searching past it.
-      i = nlIdx + 2;
-      continue;
+  const state: TomlLexState = { multilineString: null, arrayDepth: 0, inlineTableDepth: 0 };
+  let lineStart = from;
+  let isHeaderRemainder = true;
+
+  while (lineStart < content.length) {
+    const newlineIdx = content.indexOf('\n', lineStart);
+    const lineEnd = newlineIdx === -1 ? content.length : newlineIdx;
+    const line = content.slice(lineStart, lineEnd);
+
+    if (
+      !isHeaderRemainder &&
+      state.multilineString === null &&
+      state.arrayDepth === 0 &&
+      state.inlineTableDepth === 0 &&
+      isTomlTableHeader(line)
+    ) {
+      return lineStart;
     }
-    return nlIdx + 1;
+
+    scanTomlLine(line, state);
+    if (newlineIdx === -1) break;
+    lineStart = newlineIdx + 1;
+    isHeaderRemainder = false;
   }
+
   return content.length;
 }
+
+type MultilineStringDelimiter = '"""' | "'''";
+
+interface TomlLexState {
+  multilineString: MultilineStringDelimiter | null;
+  arrayDepth: number;
+  inlineTableDepth: number;
+}
+
+const TOML_KEY_PART = String.raw`(?:[A-Za-z0-9_-]+|"(?:\\.|[^"\\])*"|'[^']*')`;
+const TOML_DOTTED_KEY = String.raw`${TOML_KEY_PART}(?:[ \t]*\.[ \t]*${TOML_KEY_PART})*`;
+const TOML_TABLE = String.raw`\[[ \t]*${TOML_DOTTED_KEY}[ \t]*\]`;
+const TOML_ARRAY_TABLE = String.raw`\[\[[ \t]*${TOML_DOTTED_KEY}[ \t]*\]\]`;
+const TOML_TABLE_HEADER = new RegExp(
+  String.raw`^[ \t]*(?:${TOML_TABLE}|${TOML_ARRAY_TABLE})[ \t]*(?:#.*)?\r?$`
+);
+
+function isTomlTableHeader(line: string): boolean {
+  return TOML_TABLE_HEADER.test(line);
+}
+
+/** Track value constructs that may legally span lines so bracket-shaped string
+ * content and nested arrays cannot be mistaken for sibling table headers. */
+function scanTomlLine(line: string, state: TomlLexState): void {
+  for (let i = 0; i < line.length;) {
+    if (state.multilineString !== null) {
+      const end = findMultilineStringEnd(line, i, state.multilineString);
+      if (end === -1) return;
+      i = end + state.multilineString.length;
+      state.multilineString = null;
+      continue;
+    }
+
+    if (line[i] === '#') return;
+
+    const multiline = line.startsWith('"""', i)
+      ? '"""'
+      : line.startsWith("'''", i)
+        ? "'''"
+        : null;
+    if (multiline !== null) {
+      state.multilineString = multiline;
+      i += multiline.length;
+      continue;
+    }
+
+    const ch = line[i]!;
+    if (ch === '"' || ch === "'") {
+      i = skipSingleLineString(line, i, ch);
+      continue;
+    }
+    if (ch === '[') state.arrayDepth++;
+    else if (ch === ']' && state.arrayDepth > 0) state.arrayDepth--;
+    else if (ch === '{') state.inlineTableDepth++;
+    else if (ch === '}' && state.inlineTableDepth > 0) state.inlineTableDepth--;
+    i++;
+  }
+}
+
+function findMultilineStringEnd(
+  line: string,
+  from: number,
+  delimiter: MultilineStringDelimiter,
+): number {
+  let end = line.indexOf(delimiter, from);
+  while (delimiter === '"""' && end !== -1 && isBackslashEscaped(line, end)) {
+    end = line.indexOf(delimiter, end + 1);
+  }
+  return end;
+}
+
+function isBackslashEscaped(line: string, index: number): boolean {
+  let backslashes = 0;
+  for (let i = index - 1; i >= 0 && line[i] === '\\'; i--) backslashes++;
+  return backslashes % 2 === 1;
+}
+
+function skipSingleLineString(line: string, start: number, quote: '"' | "'"): number {
+  for (let i = start + 1; i < line.length; i++) {
+    if (quote === '"' && line[i] === '\\') {
+      i++;
+      continue;
+    }
+    if (line[i] === quote) return i + 1;
+  }
+  return line.length;
+}


Test patch:
diff --git a/__tests__/installer-targets.test.ts b/__tests__/installer-targets.test.ts
index e11efd3cc..6db793d65 100644
--- a/__tests__/installer-targets.test.ts
+++ b/__tests__/installer-targets.test.ts
@@ -876,6 +876,48 @@ describe('Installer targets — partial-state idempotency', () => {
     expect(after).not.toContain('enabled = true');
   });
 
+  it('codex: install, re-install, and uninstall preserve trailing array-of-tables siblings', () => {
+    const codex = getTarget('codex')!;
+    const tomlPath = path.join(tmpHome, '.codex', 'config.toml');
+    fs.mkdirSync(path.dirname(tomlPath), { recursive: true });
+    const historyTables = [
+      '[[history]]',
+      'id = 1',
+      'note = "keep first"',
+      '',
+      '[[history]]',
+      'id = 2',
+      'note = "keep second"',
+      '',
+    ].join('\n');
+    fs.writeFileSync(tomlPath, [
+      '[mcp_servers.codegraph]',
+      'command = "old-codegraph"',
+      'args = ["old"]',
+      'description = """',
+      'header-shaped text inside a multiline string:',
+      '[[not-a-table]]',
+      'still part of the string',
+      '"""',
+      '',
+      historyTables,
+    ].join('\n'));
+
+    const first = codex.install('global', { autoAllow: false });
+    expect(first.files.find((f) => f.path === tomlPath)?.action).toBe('updated');
+    const afterInstall = fs.readFileSync(tomlPath, 'utf-8');
+    expect(afterInstall).toContain('command = "codegraph"');
+    expect(afterInstall).not.toContain('[[not-a-table]]');
+    expect(afterInstall.endsWith(historyTables)).toBe(true);
+
+    const second = codex.install('global', { autoAllow: false });
+    expect(second.files.find((f) => f.path === tomlPath)?.action).toBe('unchanged');
+    expect(fs.readFileSync(tomlPath, 'utf-8')).toBe(afterInstall);
+
+    codex.uninstall('global');
+    expect(fs.readFileSync(tomlPath, 'utf-8')).toBe(historyTables);
+  });
+
   it('claude: local install writes ./.mcp.json (project scope), not ./.claude.json', () => {
     const claude = getTarget('claude')!;
     const result = claude.install('local', { autoAllow: false });
@@ -1292,6 +1334,113 @@ describe('Installer targets — TOML serializer (Codex backbone)', () => {
     expect(content.match(/\[\[foo\]\]/g)?.length).toBe(2);
     expect(content).toContain('[mcp_servers.codegraph]');
   });
+
+  it('upsert replaces the managed table without consuming trailing array-of-tables siblings', () => {
+    const historyTables = [
+      '[[history]]',
+      'id = 1',
+      'note = "keep first"',
+      '',
+      '[[history]]',
+      'id = 2',
+      'note = "keep second"',
+      '',
+    ].join('\n');
+    const existing = [
+      '[mcp_servers.codegraph]',
+      'command = "old-codegraph"',
+      'args = ["old"]',
+      '',
+      historyTables,
+    ].join('\n');
+    const block = buildTomlTable('mcp_servers.codegraph', {
+      command: 'codegraph',
+      args: ['serve', '--mcp'],
+    });
+
+    const { content, action } = upsertTomlTable(existing, 'mcp_servers.codegraph', block);
+
+    expect(action).toBe('replaced');
+    expect(content).toBe(`${block}\n\n${historyTables}`);
+  });
+
+  it('remove preserves trailing array-of-tables siblings byte-for-byte', () => {
+    const historyTables = [
+      '[[history]]',
+      'id = 1',
+      'note = "keep first"',
+      '',
+      '[[history]]',
+      'id = 2',
+      'note = "keep second"',
+      '',
+    ].join('\n');
+    const existing = [
+      '[mcp_servers.codegraph]',
+      'command = "codegraph"',
+      'args = ["serve", "--mcp"]',
+      '',
+      historyTables,
+    ].join('\n');
+
+    const { content, action } = removeTomlTable(existing, 'mcp_servers.codegraph');
+
+    expect(action).toBe('removed');
+    expect(content).toBe(historyTables);
+  });
+
+  it.each([
+    ['table', '[ mcp_servers.other ]'],
+    ['array-of-tables', '[[ history ]]'],
+  ])('preserves a trailing %s header with inner whitespace', (_kind, siblingHeader) => {
+    const siblingTable = `${siblingHeader}\nvalue = "keep"\n`;
+    const existing = [
+      '[mcp_servers.codegraph]',
+      'command = "old-codegraph"',
+      'args = ["old"]',
+      '',
+      siblingTable,
+    ].join('\n');
+    const block = buildTomlTable('mcp_servers.codegraph', {
+      command: 'codegraph',
+      args: ['serve', '--mcp'],
+    });
+
+    const upserted = upsertTomlTable(existing, 'mcp_servers.codegraph', block);
+    const removed = removeTomlTable(existing, 'mcp_servers.codegraph');
+
+    expect(upserted.content).toBe(`${block}\n\n${siblingTable}`);
+    expect(removed.content).toBe(siblingTable);
+  });
+
+  it.each([
+    ['basic', '"""'],
+    ['literal', "'''"],
+  ])('ignores header-shaped text inside a multiline %s string', (_kind, delimiter) => {
+    const historyTable = '[[history]]\nid = 1\n';
+    const existing = [
+      '[mcp_servers.codegraph]',
+      'command = "old-codegraph"',
+      'args = [',
+      `  ${delimiter}first line`,
+      '[[not-a-table]]',
+      `last line${delimiter},`,
+      '  "serve",',
+      ']',
+      '',
+      historyTable,
+    ].join('\n');
+    const block = buildTomlTable('mcp_servers.codegraph', {
+      command: 'codegraph',
+      args: ['serve', '--mcp'],
+    });
+
+    const upserted = upsertTomlTable(existing, 'mcp_servers.codegraph', block);
+    const removed = removeTomlTable(existing, 'mcp_servers.codegraph');
+
+    expect(upserted.content).toBe(`${block}\n\n${historyTable}`);
+    expect(removed.content).toBe(historyTable);
+  });
 });
 
 describe('Installer — uninstallTargets sweep (codegraph uninstall)', () => {