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 3 commits intoNVIDIA:mainfrom
Conversation
|
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 ignored due to path filters (1)
📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReplaced fragile line/regex-based policy merging with structured YAML parsing/serialization using the 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
|
✨ 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. |
|
@tommylin-signalpro Thanks for working on this. The core fix direction looks good: replacing the regex/text-based merge with structured YAML parsing is the right approach for I’m not comfortable merging this branch as written for two concrete reasons:
That second point is an unrelated regression: even if If you update the branch so:
then this looks worth re-reviewing. |
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>
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>
…lexity Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
3a287af to
2d9dbac
Compare
|
@wscurran Thanks for the kind words! @kjw3 Thanks for the review. Both issues are addressed:
Ready for re-review. |
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