fix(sight): don't auto-overwrite invalid JSON configs#1504
Merged
jfeng18 merged 1 commit intoJul 15, 2026
Conversation
ensure_default_agents_config (added by 98158d2 to fix alibaba#1427) treated invalid JSON files as "outdated schema_version" configs: extract_schema_version internally swallows the serde error and returns None, None < Some(CURRENT) is true, so the function backed up the file to .bak.<ts> and overwrote it with the embedded default. By the time load_from_file ran, the file was valid JSON, so parse_json_rules succeeded and the config_load_err warn branch in AgentSight::new (src/unified.rs:186) never fired. Net effect: `agentsight trace --config <invalid.json>` produced empty output — the documented "Failed to load config from {path:?}: {e}, using embedded defaults" WARN log was silently suppressed, and the user's file was rewritten without warning. Fix: probe JSON validity with serde_json::from_str::<Value> before extract_schema_version. If parse fails, return Ok without touching the file so load_from_file surfaces "JSON parse error: <serde error>" and the warn-fallback path fires. Auto-upgrade is only meaningful for *valid* JSON whose schema_version is outdated. Adds 2 regression tests: - ensure_default_agents_config_leaves_invalid_json_untouched - ensure_default_agents_config_leaves_truncated_json_untouched Verified locally: 6 config::tests::ensure_default_agents_config_* tests pass. On test env 47.239.61.17, the rebuilt agentsight binary now emits the expected WARN log for both invalid and truncated JSON configs, and leaves the original file untouched (no .bak backup). Closes alibaba#1502.
jfeng18
approved these changes
Jul 15, 2026
jfeng18
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. Fix is minimal and correct: invalid JSON → early return without touching the file, letting the downstream load_from_file warn-fallback fire as documented. Three discriminating tests cover invalid/truncated/binary cases. Consistent with our root-cause analysis of #1502.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & Why
Fixes #1502.
ensure_default_agents_config(added in 98158d2 to fix #1427) silently overwrote invalid JSON config files with the embedded default, suppressing the documentedWARN Failed to load config from {path:?}: {e}, using embedded defaultslog line.Root cause:
extract_schema_versioninternally doesserde_json::from_str::<VersionProbe>(json).ok()— for invalid JSON it returnsNone. ThenNone < Some(CURRENT_SCHEMA_VERSION)is true, so the function backed up the file to.bak.<ts>and overwrote it with the default. By the timeload_from_fileran, the file was valid JSON →parse_json_rulessucceeded → theconfig_load_errwarn branch inAgentSight::new(src/unified.rs:186) never fired.Reproduction (on 0.8.0 RPM, before this fix)
This breaks
agentic-os-tests/agentsight-tests/tests/test_config_validation.py::TestInvalidConfigHandling::test_invalid_json_config_returns_errorandTestConfigFileParsingErrors::test_config_truncated_json_error(both failing on the 2026-07-15 regression run).Fix
In
ensure_default_agents_config(src/agentsight/src/config.rs), probe JSON validity withserde_json::from_str::<serde_json::Value>before callingextract_schema_version. If the parse fails, returnOk(())without touching the file — this lets the caller'sload_from_file → parse_json_rulessurfaceJSON parse error: <serde error>and the warn-fallback path fire. Auto-upgrade is only meaningful for valid JSON whoseschema_versionis outdated.This preserves the #1427 auto-upgrade behavior for valid-but-stale configs (still backed up + overwritten when
schema_version < CURRENT_SCHEMA_VERSION) while restoring the parse-error warn log for invalid JSON.Tests
Added 2 regression tests in
config::tests:ensure_default_agents_config_leaves_invalid_json_untouched— writes"NOT_VALID_JSON_AT_ALL{{{", callsensure_default_agents_config, asserts file content unchanged and no.bakfile created.ensure_default_agents_config_leaves_truncated_json_untouched— same with a truncated-JSON variant.All existing
ensure_default_agents_config_*tests still pass:End-to-end verification on test env (47.239.61.17)
Built
agentsightrelease binary with this fix, scp'd to test env, ran manual repro:The expected
WARN ... JSON parse error: ... using embedded defaultslog line now fires, and the invalid file is left untouched (no.bakbackup). Same behavior verified for truncated JSON ({"cmdline": {"allow": [{"rule": ["*cosh*").Related
agentsight serveschema migration overwrites user customizations for valid v1 configs (different code path, same theme of schema-migration logic silently clobbering user state)