nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] One malformed agents.config.json produces 18 error messages and silently falls back to the global settings

Open addyCooks opened this issue 2 weeks ago • 3 comments

Description

If the project agents.config.json is not valid JSON, every config section is loaded separately and every one of them reports the same parse error. Then each section falls back to the global value, so project settings (including safety settings) are not applied and the user only sees a wall of errors.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at 5df35b3a)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. In $NANOCODER_CONFIG_DIR create agents.config.json with a provider global-one.

  2. In the project folder create agents.config.json with a trailing comma:

     {"nanocoder": {"providers": [{"name":"a","models":["m"],"baseUrl":"http://x"},]}}
    
  3. Run getAppConfig() from the project folder and collect the messages sent to the message queue.

Expected Behavior

One message that names the file and the JSON error. The user is told clearly that the project config was skipped.

Actual Behavior

18 error messages, one per section (providers, auto-compact, headless, retries, nanocoderTools, alwaysAllow, disabledTools, systemPrompt, hooks, formatters, verify, modeProviders, tune, sandbox, autoCommit, firewall, lspServers, ...). The providers come from the global file (["global-one"]).

Logs/Screenshots

error: Failed to load project provider config from <...>\agents.config.json: SyntaxError: Unexpected token ']', ..."ttp://x"},]}}" is not valid JSON
error: Failed to load auto-compact config from <...>\agents.config.json: SyntaxError: Unexpected token ']', ...
error: Failed to load headless config from <...>\agents.config.json: SyntaxError: ...
... (18 in total)

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/config/index.ts:97-117 (tryLoadConfig): cache the parse result (or the parse error) per file for the whole getAppConfig() call and report a failed file once.
  • Decide the fallback on purpose: either stop with a clear error when the project file is invalid, or load the global config and say so in the single message.
  • Add a test that counts the messages for one broken file.

Related: #1652 (closed).

addyCooks avatar Nov 16 '26 09:11 addyCooks

Hi @addyCooks , I'd like to work on this issue! Could you please assign it to me?

Proposed Approach

  1. Shared Cached JSON Loader: Cache both parse results and parse errors per file path keyed by mtimeMs and size:
    • If a file fails to parse, record the error and an errorNotified flag in the cache entry.
    • The first loader that hits the error emits a single, clear notifyError indicating the file path, the syntax error, and explicitly stating that the file is skipped and falling back to global configuration.
    • All subsequent section loaders (and provider loaders) hitting that broken file retrieve the cached failure and return null without emitting duplicate error notifications.
  2. Unify Provider Config Loading: Route loadProjectProviderConfigs and loadProviderConfigFromFile in mcp-config-loader.ts through the same cached reader so provider loading does not perform a separate un-cached read.
  3. Cache Clearing: Ensure clearAppConfig() and reloadAppConfig() invalidate this cache cleanly.
  4. Testing: Add an AVA test using setMessageQueueSink to assert that an invalid project config emits exactly 1 error message (with file path, syntax error, and skip notice) and falls back cleanly.
  5. Changeset: Add a patch changeset as required by repository guidelines.

I'm ready to open a clean PR with tests, types, and lint passing.h

pranav514 avatar Nov 16 '26 10:11 pranav514

Hi @pranav514, thanks for the clear plan. This issue is assigned to you.

A few facts from main that should help:

  • source/config/index.ts already has a cached reader (readCachedConfigFile, keyed by mtimeMs and size, cleared by reloadAppConfig() and clearAppConfig()). It only caches successful parses; a failed parse throws and each section's tryLoadConfig reports it separately, so remembering the failure in that same cache is likely the smallest change.
  • source/config/mcp-config-loader.ts reads the files directly with readFileSync/JSON.parse (loadProjectProviderConfigs, loadProviderConfigFromFile), so your point about routing the provider loaders through the shared reader applies.
  • The issue allows either stopping with a clear error or loading the global config and saying so. Your plan (one message, project file skipped, global config used) is one of those two options. Please make the message name the file and the JSON error and say the project config was skipped.

Please open the PR now that you're assigned, with "Closes #2250", a changeset and the test you describe.

addyCooks avatar Nov 17 '26 06:11 addyCooks

@addyCooks @akramcodez Please do review this PR, This is open for a long time now.

pranav514 avatar Nov 23 '26 07:11 pranav514