nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] A UTF-8 BOM makes agents.config.json, preferences and command frontmatter silently unreadable

Open addyCooks opened this issue 1 month ago • 4 comments

Summary

Files that start with a UTF-8 byte order mark are not parsed. The config and preferences files become "unreadable", and a command's frontmatter is treated as part of the prompt body.

Steps to reproduce

  1. Write agents.config.json with a leading BOM (\uFEFF), for example with Windows PowerShell 5.1 Set-Content -Encoding UTF8. Set nanocoder.retries.maxEmptyTurns to 7.
  2. Run nanocoder config show nanocoder.retries.maxEmptyTurns and nanocoder config diff.
  3. Create .nanocoder/commands/bomcmd.md that starts with a BOM, then ---, description: BOM command, aliases: with - bc, then --- and a body.
  4. Load it with the command parser.

Expected behaviour

BOM files load like the same file without a BOM (value 7; description and aliases read from the frontmatter).

Actual behaviour

nanocoder.retries.maxEmptyTurns
  value    2            <- default; 7 expected
  layer    default

config diff:  global ...\agents.config.json   unreadable: Unexpected token '', "{"nanocod"... is not valid JSON

The BOM command parses with no metadata, and its content is ---\ndescription: BOM command\naliases:\n - bc\n---\nDo the thing. A spec calling splitFrontmatter('\uFEFF---\ndescription: Hello\n---\nBody') fails:

t.true(r.hasFrontmatter)  ->  Value is not `true`: false

nanocoder-preferences.json with a BOM is also reported as unreadable.

Environment

Windows 11, Node v22.22.3, Nanocoder 1.31.0 (main at 948a4e74)

Suggested fix

  • source/config/index.ts:81: strip a leading \uFEFF before JSON.parse. Do the same in the preferences reader.
  • source/utils/frontmatter.ts:22-27: strip it at the start of splitFrontmatter. source/custom-commands/parser.ts:209 is one caller.
  • source/skills/install.ts:262 already strips a BOM for the skills index. Move that into one shared read helper and use it everywhere.

addyCooks avatar Nov 02 '26 16:11 addyCooks

Hi @bfs2021 @Mahdi-Hasan , thanks for your contributions! Please don’t open a PR without first commenting on the issue and being assigned to it. That keeps us from duplicate work and makes sure the approach is agreed on before you spend time on it. Thanks for understanding!

addyCooks avatar Nov 06 '26 11:11 addyCooks

Hi @addyCooks, noted. Just to clarify, I don't have a PR for this one, and I'll leave it to #1946. Going forward I'll comment and wait for assignment before opening anything.

Mahdi-Hasan avatar Nov 06 '26 11:11 Mahdi-Hasan

Thanks @Mahdi-Hasan, appreciated. And thanks for picking up the "comment and wait for assignment" flow.

@bfs2021 your PR #1946 is the only one for this issue, so we'll keep it. It currently has a merge conflict with main, so please merge main into your branch, and I'll take another look. If you'd like to be assigned to this issue, say so here.

Thanks !

addyCooks avatar Nov 06 '26 11:11 addyCooks

Hi! I'd like to work on this.

Plan, similar to what #1946 sketched:

  1. Add a shared stripUtf8Bom() helper and run file contents through it before JSON.parse at the agents.config.json, nanocoder-preferences.json and .mcp.json read sites in source/config/, and before frontmatter splitting in source/utils/frontmatter.ts so custom commands, subagents, tools and skills all benefit.
  2. Update the parseCommandFile handles file with BOM spec (it currently asserts the broken behaviour) and add regression specs for the config and preferences paths.
  3. Patch changeset.

Could you assign it to me? Happy to adjust the approach if you'd rather strip at a different layer.

Seyamalam avatar Nov 16 '26 03:11 Seyamalam