fix(sight): preserve user config on schema migration#1505
Conversation
Replace the destructive overwrite in ensure_default_agents_config with a shallow merge: start from the embedded default, overlay all top-level keys the user has set (except schema_version), bump to CURRENT_SCHEMA_VERSION. This preserves user customizations (cmdline rules, https rules, codex_offsets, feature overrides) while adding any new sections from the default that the old config was missing. Fixes alibaba#1496
There was a problem hiding this comment.
- 合并逻辑中的二次 JSON 解析使用
expect,配置损坏会导致进程 panic,建议改为返回错误。 - 顶层“浅合并”目前对对象字段执行全覆盖,可能丢失新默认子字段,建议对
features/runtime_limits等采用子字段级 merge 并澄清语义。 - 备份文件按秒级时间戳命名且使用
copy,同秒多次迁移会静默覆盖旧备份,建议避免备份文件名冲突。
🤖 Generated by Qoder • View workflow run
|
Responding to the bot review: Point 1 (
Point 2 (shallow merge loses nested default keys): Acknowledged as follow-up, not a blocker. The shallow merge is intentional and documented (comment at line 537). Deep merge risks overwriting user intentional nested changes with defaults — the opposite of what #1496 set out to fix. New nested keys introduced by future schema bumps can be handled by a dedicated deep-merge enhancement if real-world breakage surfaces. Not blocking the current fix which is strictly better than the destructive overwrite it replaces. Point 3 (timestamp collision in backup naming): Acknowledged as cosmetic nit, not blocking. Requires two processes to start in the same second AND both find outdated schema_version (a once-per-upgrade event). Even if triggered, one valid backup still exists. Not worth the diff churn. |
chengshuyi
left a comment
There was a problem hiding this comment.
LGTM. 浅合并逻辑清晰正确,测试 discriminating,对 bot review 的回应到位。可以合入。
Summary
Replace the destructive config overwrite in
ensure_default_agents_configwith a shallow merge: start from the embedded default, overlay all top-level keys the user has set (exceptschema_version), bump toCURRENT_SCHEMA_VERSION.Fixes #1496.
Problem
When upgrading from schema v1 → v2,
ensure_default_agents_config(added in 98158d2) backed up the old config and overwrote it entirely with the embedded default. User customizations — cmdline rules, https rules, codex_offsets, feature overrides — were silently lost.Fix
serde_json::Valueschema_version), overwrite the default's corresponding keyschema_version = CURRENT, write back pretty-printed.bak) is still created (usingfs::copyinstead offs::renameso the merged file has a fresh inode)This preserves user data while adding any new sections (features, runtime_limits, etc.) from the default that the old config was missing.
Testing
Two discriminating tests added:
ensure_default_agents_config_preserves_user_cmdline_rules_on_merge: custom agent rule + https + deny rule survive migrationensure_default_agents_config_adds_missing_sections_on_merge: missingfeatures/runtime_limitsare pulled from defaultBoth tests fail if reverted to the old overwrite logic (discriminating).
Verification