fix(policies): use structured YAML parsing for policy preset merge#1055
fix(policies): use structured YAML parsing for policy preset merge#1055tommylin-signalpro wants to merge 2 commits intoNVIDIA:mainfrom
Conversation
Fixes NVIDIA#1010 The previous `mergePresetIntoPolicy()` used text-based string manipulation (regex + line splitting) to inject preset entries into the existing policy YAML. This produced invalid YAML when: - Preset entries were re-applied (duplicates) - Indentation varied between current policy and preset - network_policies appeared at unexpected positions Replace with structured YAML merge using the `yaml` package: - Parse both current policy and preset entries as YAML objects - Merge network_policies by name (preset overrides on collision) - Preserve all non-network sections (filesystem_policy, process, etc.) - Ensure version header exists Falls back to the text-based approach when preset entries use non-standard list format (backward compatibility with existing callers). Added 3 new tests: - Structured merge with realistic preset data - Deduplication on policy name collision - Preservation of non-network sections during merge Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRewrote merge logic in Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bin/lib/policies.js`:
- Around line 175-176: current.network_policies may be an array, so spreading it
into an object (const mergedNp = { ...existingNp, ...presetPolicies }) will
create numeric keys and corrupt the data; update the merge to guard for arrays:
check Array.isArray(existingNp) (and that presetPolicies is an object) and if
existingNp is an array, do not object-spread — either preserve the array
(mergedNp = existingNp) or convert the array into an object map first (e.g., by
mapping entries by a name/id field) before merging; apply this guard around the
existingNp/mergedNp logic so merges only happen when existingNp is a plain
object.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b7982780-9e9e-45a7-94bb-143e08c56f8b
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (3)
bin/lib/policies.jspackage.jsontest/policies.test.js
bin/lib/policies.js
Outdated
| const existingNp = current.network_policies || {}; | ||
| const mergedNp = { ...existingNp, ...presetPolicies }; |
There was a problem hiding this comment.
Potential issue: current.network_policies could be an array.
If the existing policy uses list-style network_policies (an array of objects) rather than named policies (an object/map), the spread operator { ...existingNp } will produce keys like "0", "1", etc., corrupting the merge.
Consider adding a guard similar to the preset check:
🛡️ Proposed defensive check
// Structured merge: preset entries override existing on name collision.
// This prevents duplicate policy groups that the text-based approach
// would create when re-applying the same preset.
- const existingNp = current.network_policies || {};
+ const existingNp = (current.network_policies && typeof current.network_policies === "object" && !Array.isArray(current.network_policies))
+ ? current.network_policies
+ : {};
const mergedNp = { ...existingNp, ...presetPolicies };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@bin/lib/policies.js` around lines 175 - 176, current.network_policies may be
an array, so spreading it into an object (const mergedNp = { ...existingNp,
...presetPolicies }) will create numeric keys and corrupt the data; update the
merge to guard for arrays: check Array.isArray(existingNp) (and that
presetPolicies is an object) and if existingNp is an array, do not object-spread
— either preserve the array (mergedNp = existingNp) or convert the array into an
object map first (e.g., by mapping entries by a name/id field) before merging;
apply this guard around the existingNp/mergedNp logic so merges only happen when
existingNp is a plain object.
Address CodeRabbit review: existing network_policies may be an array
in legacy policies. Spreading an array into an object produces numeric
keys ("0", "1") and corrupts the data. Now checks Array.isArray()
before merging — falls back to using preset entries only when existing
is not a plain object.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
✨ Thanks for submitting this fix with a detailed summary, it identifies a bug in the policy preset merge process and proposes a solution using structured YAML parsing, which could improve the stability and reliability of NemoClaw. |
Summary
Fixes #1010
mergePresetIntoPolicy()with structured YAML parsing via theyamlpackagenetwork_policiesby name: preset entries override existing on name collision (prevents duplicates on re-apply)filesystem_policy,process,landlock, etc.)Problem
The previous implementation used regex and line splitting to inject preset entries into existing policy YAML. This produced invalid YAML when:
network_policies:appeared at unexpected positions in the documentChanges
bin/lib/policies.js— RewritemergePresetIntoPolicy()to parse YAML, merge objects, serialize back. Addyamlas dependency.test/policies.test.js— Add 3 tests with realistic preset data: structured merge, name collision dedup, non-network section preservation. Relax existing string-format assertions to check correctness instead of exact formatting.Test plan
install-preflight.test.js)Summary by CodeRabbit
Bug Fixes
Tests
Chores