agent parser failed on wrangler.toml with duplicate top-level keys
Fixes agent tooling that crashes parsing wrangler.toml with duplicate top-level keys. Covers reproducing with a strict parser, merging duplicated sections, and making the agent's config editor parse-merge-validate on every write. Use when a TOML parser throws on wrangler.toml. Key trigger: parser error naming a duplicated key after an automated config edit.
TL;DR: Merge the duplicate sections - TOML tables are singletons, so a second [vars] block is a syntax error, not a merge. Agents hit this by appending config instead of merging into the existing table. Reproduce with a strict parser to name the duplicated key, consolidate into one section, re-parse to confirm, then re-run the agent's original parse. Make the agent's config editor parse-merge-validate on every write so it can't happen again.
agent parser failed on wrangler.toml with duplicate top-level keysSteps
- Reproduce with a strict parser: run
python3 -c "import tomllib; tomllib.load(open('wrangler.toml','rb'))". Expected: an error naming the duplicate key. That name is the whole diagnosis. - Search wrangler.toml for every occurrence of the duplicated section header or key. Expected: you see exactly which blocks collide - usually an appended section the agent added below the original.
- Merge the duplicates into a single section, keeping the intended values from both. Expected: one definition per key, nothing lost.
- Re-run the strict parse from step 1. Expected: clean parse, no errors.
- Re-run the agent's original parse operation that failed. Expected: it succeeds. If it still fails, the problem is the agent's parser, not the file.
- Fix the editor: the agent's config routine must parse the file first, merge into existing tables instead of appending, and re-validate before writing. Expected: duplicate keys become structurally impossible.
Use this when
- a TOML parser throws a duplicate-key error on wrangler.toml
- wrangler reports an invalid config naming a duplicated key
- an automated config edit just broke the file
- "duplicate top-level key" in any wrangler.toml tooling
Not for this skill when
- the file parses but wrangler rejects a value - that's validation, not syntax
- it's a JSON config with duplicate keys - different parser, different rules
- the error is a runtime binding problem - the config syntax is fine
- the duplication is intentional across environments - see edge cases
Variant phrasings
- tomllib duplicate key error on wrangler.toml
- agent appended a second vars section
- wrangler.toml parse error after automated edit
- invalid wrangler.toml duplicate key
Why it happens
TOML forbids duplicate keys, and agents that edit config by concatenating text append a second [vars] (or second top-level key) instead of merging into the existing one. The file looks fine to a human skimming it, but every strict parser rejects it.
Edge cases
[env.production.vars]and top-level[vars]are different tables, not duplicates. Don't "fix" those.- Comments and blank lines don't affect duplication. Only keys and table headers count.
- wrangler also accepts JSONC config, where duplicate keys are technically allowed but last-wins - still merge rather than rely on that.
- If two agents edit the file concurrently, merges collide. Serialize config writes.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_aFKD6UJAoIWnkcWM-OMhwA