[Bug] One malformed agents.config.json produces 18 error messages and silently falls back to the global settings
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
-
In
$NANOCODER_CONFIG_DIRcreateagents.config.jsonwith a providerglobal-one. -
In the project folder create
agents.config.jsonwith a trailing comma:{"nanocoder": {"providers": [{"name":"a","models":["m"],"baseUrl":"http://x"},]}} -
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 wholegetAppConfig()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).
Hi @addyCooks , I'd like to work on this issue! Could you please assign it to me?
Proposed Approach
- Shared Cached JSON Loader: Cache both parse results and parse errors per file path keyed by
mtimeMsandsize:- If a file fails to parse, record the error and an
errorNotifiedflag in the cache entry. - The first loader that hits the error emits a single, clear
notifyErrorindicating 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
nullwithout emitting duplicate error notifications.
- If a file fails to parse, record the error and an
- Unify Provider Config Loading: Route
loadProjectProviderConfigsandloadProviderConfigFromFileinmcp-config-loader.tsthrough the same cached reader so provider loading does not perform a separate un-cached read. - Cache Clearing: Ensure
clearAppConfig()andreloadAppConfig()invalidate this cache cleanly. - Testing: Add an AVA test using
setMessageQueueSinkto assert that an invalid project config emits exactly 1 error message (with file path, syntax error, and skip notice) and falls back cleanly. - 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
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 @akramcodez Please do review this PR, This is open for a long time now.