Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(init): init edits only simple Codex TOML [A9,I1]

Corey Olson2b5188bf85af
diff --git a/src/init.js b/src/init.js
index 968a39bc..f30c9a79 100644
--- a/src/init.js
+++ b/src/init.js
@@ -553,32 +553,81 @@ export function codexHasMcpServer(contents, name) {
}
/**
- * Whether a Codex config.toml defines mcp_servers itself as an inline table (`mcp_servers = { ... }`). TOML never adds
- * to an inline table, so an appended [mcp_servers.<name>] table would make the whole file invalid (#961).
+ * The end of one simple TOML value starting at `at`: a basic or literal string, a number, a boolean, or a single-line
+ * array of those. -1 for anything else, such as an inline table, a date or a multi-line string (#961).
+ *
+ * @param {string} text
+ * @param {number} at
+ * @returns {number}
+ */
+function simpleTomlValueEnd(text, at) {
+ if (text.startsWith('"""', at) || text.startsWith("\'\'\'", at)) return -1;
+ if (text[at] === '"') {
+ const match = /"((?:[^"\\]|\\.)*)"/uy;
+ match.lastIndex = at;
+ const found = match.exec(text);
+ return found && tomlBasicString(found[1]) !== null ? match.lastIndex : -1;
+ }
+ if (text[at] === "\'") {
+ const close = text.indexOf("\'", at + 1);
+ return close === -1 ? -1 : close + 1;
+ }
+ if (text[at] === '[') {
+ let index = at + 1;
+ for (;;) {
+ while (text[index] === ' ' || text[index] === '\t') index += 1;
+ if (text[index] === ']') return index + 1;
+ const end = text[index] === '[' ? -1 : simpleTomlValueEnd(text, index);
+ if (end === -1) return -1;
+ index = end;
+ while (text[index] === ' ' || text[index] === '\t') index += 1;
+ if (text[index] === ',') index += 1;
+ else if (text[index] !== ']') return -1;
+ }
+ }
+ const scalar = /(?:true|false|[+-]?(?:inf|nan)|0x[0-9A-Fa-f_]+|0o[0-7_]+|0b[01_]+|[+-]?\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)(?=[\s,\]#]|$)/uy;
+ scalar.lastIndex = at;
+ return scalar.exec(text) ? scalar.lastIndex : -1;
+}
+
+/**
+ * Whether init may edit this Codex config.toml: every line is blank, a comment, a [table] or [a.b] header, or a
+ * single-line key = value whose value is a string, number, boolean or single-line array, and nothing assigns
+ * mcp_servers a value of its own. Outside that subset init writes nothing and prints the block for the person: a
+ * reviewer grades this subset and the fallback, not every TOML form (#961).
*
* @param {string} contents
* @returns {boolean}
*/
-export function codexMcpServersInline(contents) {
+export function codexConfigEditable(contents) {
let table = [];
- for (const { text: raw, inString } of tomlLines(contents)) {
- if (inString) continue;
- const line = withoutTomlComment(raw).trim();
- const header = /^\[\[?(.+?)\]\]?$/u.exec(line);
- if (header) { table = tomlKeyParts(header[1]) ?? []; continue; }
+ for (const raw of String(contents).split(/\r?\n/u)) {
+ const line = raw.trim();
+ if (!line || line.startsWith('#')) continue;
+ const header = /^\[([^[\]]+)\]\s*(?:#.*)?$/u.exec(line);
+ if (header) {
+ table = tomlKeyParts(header[1]);
+ if (!table) return false;
+ continue;
+ }
const equals = line.indexOf('=');
- if (equals < 1) continue;
- const path = [...table, ...(tomlKeyParts(line.slice(0, equals)) ?? [null])];
- if (path.length === 1 && path[0] === 'mcp_servers' && line.slice(equals + 1).trim().startsWith('{')) return true;
+ const key = equals > 0 ? tomlKeyParts(line.slice(0, equals)) : null;
+ if (!key) return false;
+ const path = [...table, ...key];
+ if (path.length === 1 && path[0] === 'mcp_servers') return false;
+ let at = equals + 1;
+ while (line[at] === ' ' || line[at] === '\t') at += 1;
+ const end = simpleTomlValueEnd(line, at);
+ if (end === -1 || !/^\s*(?:#.*)?$/u.test(line.slice(end))) return false;
}
- return false;
+ return true;
}
/** The one [mcp_servers.pullboard] block init writes, or prints for the person when it cannot write it (#961). */
export const CODEX_MCP_BLOCK = `[mcp_servers.pullboard]\ncommand = "sh"\nargs = ["-c", ${JSON.stringify(MCP_COMMAND)}]`;
/** How a note begins when Codex needs the person: an inline mcp_servers table that init leaves byte-identical. */
-export const CODEX_MANUAL_NOTE = 'kept .codex/config.toml as written: its mcp_servers is an inline table, which TOML cannot extend from outside it.';
+export const CODEX_MANUAL_NOTE = 'kept .codex/config.toml as written: it uses TOML beyond the simple lines init edits (such as an inline table or a multi-line string).';
/**
* How init left each client's MCP server, read from its notes: added, kept (already there), or manual (the person adds
@@ -623,13 +672,13 @@ function writeMcpConfig(root, onWrite) {
}
const codex = join(root, '.codex', 'config.toml');
const contents = existsSync(codex) ? readFileSync(codex, 'utf8') : '';
- if (codexHasMcpServer(contents, 'pullboard')) {
+ if (!codexConfigEditable(contents)) {
+ // Outside the simple subset init never rewrites what the person wrote: it prints the block for them to add (#961).
+ notes.push(`${CODEX_MANUAL_NOTE} To run pullboard mcp in Codex, add this server yourself, as this table or as a pullboard entry in your mcp_servers table:\n${CODEX_MCP_BLOCK}`);
+ } else if (codexHasMcpServer(contents, 'pullboard')) {
notes.push('kept the Codex MCP server in .codex/config.toml');
- } else if (codexMcpServersInline(contents)) {
- // Rewriting the person's inline table would change bytes they wrote; they add the entry where TOML allows it.
- notes.push(`${CODEX_MANUAL_NOTE} To run pullboard mcp in Codex, move those servers into [mcp_servers.<name>] tables, then add:\n${CODEX_MCP_BLOCK}`);
} else {
- // A new table at the end is valid TOML after any existing content, so nothing already there is rewritten.
+ // A new table at the end is valid TOML after any simple content, so nothing already there is rewritten.
const prefix = contents && !contents.endsWith('\n') ? `${contents}\n` : contents;
const table = `${prefix ? `${prefix}\n` : ''}${CODEX_MCP_BLOCK}\n`;
mkdirSync(join(root, '.codex'), { recursive: true });
diff --git a/test/init-permissions.test.js b/test/init-permissions.test.js
index eb08ebaf..f642fe0c 100644
--- a/test/init-permissions.test.js
+++ b/test/init-permissions.test.js
@@ -196,48 +196,51 @@ test('init grants the agent the permissions the Pullboard loop needs [I1,I2]', t
assert.equal(readFileSync(skipCodexPath, 'utf8'), skipCodexBytes, '--no-permissions preserves existing Codex rules byte-for-byte');
});
-test('init keeps an existing Codex pullboard server in any TOML form and adds no second one [A9,I1]', (t) => {
- const forms = {
- 'a trailing comment': '[mcp_servers.pullboard] # the team runs it here\ncommand = "pullboard"\nargs = ["mcp"]\n',
- 'a single-quoted key': "[mcp_servers.'pullboard']\ncommand = \"pullboard\"\nargs = [\"mcp\"]\n",
- 'an inline table': 'mcp_servers = { pullboard = { command = "pullboard", args = ["mcp"] } }\n',
- 'an inline table under [mcp_servers]': '[mcp_servers]\npullboard = { command = "pullboard", args = ["mcp"] }\n',
- 'dotted keys': 'mcp_servers.pullboard.command = "pullboard"\nmcp_servers.pullboard.args = ["mcp"]\n',
- };
- for (const [form, existing] of Object.entries(forms)) {
+test('init edits a simple Codex config and keeps a pullboard server already there [A9,I1]', (t) => {
+ // Every line is in the simple subset, so init appends the table once, or keeps the file when pullboard is there.
+ for (const [existing, codex] of [
+ ['[mcp_servers.pullboard] # the team runs it here\ncommand = "pullboard"\nargs = ["mcp"]\n', 'kept'],
+ ["[mcp_servers.'pullboard']\ncommand = \"pullboard\"\n", 'kept'],
+ ['mcp_servers.pullboard.command = "pullboard"\n', 'kept'],
+ ['[mcp_servers]\npullboard.command = "pullboard"\n', 'kept'],
+ ['# [mcp_servers.pullboard] was here once\n[mcp_servers.other]\ncommand = "pullboard"\n', 'added'],
+ ['[mcp_servers."\\U0001F600"]\ncommand = "smile"\n', 'added'],
+ ['model = "o3"\nlimits = [1, 2.5, true, "x"] # trailing\n', 'added'],
+ ]) {
const box = initFixture(t);
mkdirSync(join(box.root, '.codex'), { recursive: true });
writeFileSync(join(box.root, '.codex', 'config.toml'), existing);
const initialized = box.init();
- assert.equal(initialized.code, 0, `${form}: ${initialized.stderr}`);
- assert.equal(readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8'), existing, `${form}: the config is byte for byte as it was, with no duplicate server`);
- assert.ok(initialized.document.notes.includes('kept the Codex MCP server in .codex/config.toml'), `${form}: init says it kept the server`);
+ assert.equal(initialized.code, 0, `${existing}\n${initialized.stderr}`);
+ assert.equal(initialized.document.mcp.codex, codex, existing);
+ const after = readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8');
+ if (codex === 'kept') assert.equal(after, existing, 'a pullboard server already there is kept byte for byte');
+ else {
+ assert.ok(after.startsWith(existing), 'everything already there is kept');
+ assert.match(after.slice(existing.length), /^\n\[mcp_servers\.pullboard\]\ncommand = "sh"\nargs = \["-c", "[^\n]+"\]\n$/u, 'the pullboard table is appended once');
+ }
}
- // A server by another name, or pullboard named only inside a string or a comment, still gets the pullboard table.
- const box = initFixture(t);
- const other = '# [mcp_servers.pullboard] was here once\n[mcp_servers.other]\ncommand = "pullboard"\n';
- mkdirSync(join(box.root, '.codex'), { recursive: true });
- writeFileSync(join(box.root, '.codex', 'config.toml'), other);
- assert.equal(box.init().code, 0);
- const after = readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8');
- assert.ok(after.startsWith(other), 'the existing config is kept');
- assert.match(after.slice(other.length), /^\n\[mcp_servers\.pullboard\]\ncommand = "sh"\n/u, 'the pullboard server is appended once');
});
-test('init leaves an inline mcp_servers table as written, prints the exact block and marks Codex manual [A9,I1]', (t) => {
- // TOML never adds to an inline table from outside it, so init writes nothing and the person adds the block.
- for (const existing of ['mcp_servers = { other = { command = "other-server", args = ["serve"] } }\n', 'mcp_servers = {}\n']) {
+test('init leaves a Codex config outside the simple subset as written, prints the exact block and marks Codex manual [A9,I1]', (t) => {
+ // Outside the subset init never rewrites the person's TOML; it prints the one block for them to add.
+ for (const existing of [
+ 'mcp_servers = { other = { command = "other-server", args = ["serve"] } }\n',
+ 'mcp_servers = {}\n',
+ 'mcp_servers = { pullboard = { command = "pullboard" } }\n',
+ 'note = """\n[mcp_servers.pullboard]\n"""\n[mcp_servers.other]\ncommand = "other"\n',
+ 'released = 1979-05-27\n',
+ '[[profiles]]\nname = "x"\n',
+ ]) {
const box = initFixture(t);
mkdirSync(join(box.root, '.codex'), { recursive: true });
writeFileSync(join(box.root, '.codex', 'config.toml'), existing);
const initialized = box.init();
- assert.equal(initialized.code, 0, initialized.stderr);
- assert.equal(readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8'), existing, 'the inline table is byte for byte as it was');
- assert.equal(initialized.document.mcp.codex, 'manual', '--json marks Codex as manual');
- assert.equal(initialized.document.mcp.claude, 'added');
- const note = initialized.document.notes.find(entry => entry.startsWith('kept .codex/config.toml as written: its mcp_servers is an inline table'));
- assert.ok(note, 'init says why it kept the file');
- assert.match(note, /\n\[mcp_servers\.pullboard\]\ncommand = "sh"\nargs = \["-c", "[^\n]+"\]$/u, 'and prints the one exact block to add');
+ assert.equal(initialized.code, 0, `${existing}\n${initialized.stderr}`);
+ assert.equal(readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8'), existing, `${existing}: byte for byte as it was`);
+ assert.equal(initialized.document.mcp.codex, 'manual', `${existing}: --json marks Codex as manual`);
+ const note = initialized.document.notes.find(entry => entry.startsWith('kept .codex/config.toml as written:'));
+ assert.match(note ?? '', /\n\[mcp_servers\.pullboard\]\ncommand = "sh"\nargs = \["-c", "[^\n]+"\]$/u, `${existing}: the one exact block to add`);
}
// The plain command prints the block too, without --verbose.
const box = initFixture(t);
@@ -246,26 +249,4 @@ test('init leaves an inline mcp_servers table as written, prints the exact block
const plain = runFixtureChild(process.execPath, [BIN, 'init'], { cwd: box.root, env: box.env, encoding: 'utf8' });
assert.equal(plain.status, 0, plain.stderr);
assert.match(plain.stdout, /\[mcp_servers\.pullboard\]\ncommand = "sh"\nargs = \["-c", /u, 'the plain output shows the block');
- // A config init can extend says added.
- const ordinary = initFixture(t);
- assert.equal(ordinary.init().document.mcp.codex, 'added');
-});
-
-test('init reads no table inside a multi-line string and reads every TOML escape in a key [A9,I1]', (t) => {
- // A header written inside a multi-line string is text, so the pullboard server is still added; a \U escape never crashes.
- for (const existing of [
- 'note = """\n[mcp_servers.pullboard]\n"""\n[mcp_servers.other]\ncommand = "other"\n',
- "note = '''\n[mcp_servers.pullboard]\n'''\n",
- '[mcp_servers."\\U0001F600"]\ncommand = "smile"\n',
- ]) {
- const box = initFixture(t);
- mkdirSync(join(box.root, '.codex'), { recursive: true });
- writeFileSync(join(box.root, '.codex', 'config.toml'), existing);
- const initialized = box.init();
- assert.equal(initialized.code, 0, `${existing}\n${initialized.stderr}`);
- assert.equal(initialized.document.mcp.codex, 'added', existing);
- const after = readFileSync(join(box.root, '.codex', 'config.toml'), 'utf8');
- assert.ok(after.startsWith(existing), 'everything already there is kept');
- assert.match(after.slice(existing.length), /^\n\[mcp_servers\.pullboard\]\ncommand = "sh"\n/u, 'the pullboard table is appended once');
- }
});