@@ -83,62 +83,37 @@ function buildPolicyGetCommand(sandboxName) {
8383 return `openshell policy get --full "${ sandboxName } " 2>/dev/null` ;
8484}
8585
86- function applyPreset ( sandboxName , presetName ) {
87- // Guard against truncated sandbox names — WSL can truncate hyphenated
88- // names during argument parsing, e.g. "my-assistant" → "m"
89- const isRfc1123Label = / ^ [ a - z 0 - 9 ] ( [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? $ / . test ( sandboxName ) ;
90- if ( ! sandboxName || sandboxName . length > 63 || ! isRfc1123Label ) {
91- throw new Error (
92- `Invalid or truncated sandbox name: '${ sandboxName } '. ` +
93- `Names must be 1-63 chars, lowercase alphanumeric, with optional internal hyphens.`
94- ) ;
95- }
96-
97- const presetContent = loadPreset ( presetName ) ;
98- if ( ! presetContent ) {
99- console . error ( ` Cannot load preset: ${ presetName } ` ) ;
100- return false ;
101- }
102-
103- const presetEntries = extractPresetEntries ( presetContent ) ;
104- if ( ! presetEntries ) {
105- console . error ( ` Preset ${ presetName } has no network_policies section.` ) ;
106- return false ;
86+ /**
87+ * Merge preset entries into existing policy YAML. Handles versionless policies
88+ * by ensuring the merged result has a version header when the current policy
89+ * has content but no version field. Pure function for testing.
90+ *
91+ * @param {string } currentPolicy - Existing policy YAML (may be versionless)
92+ * @param {string } presetEntries - Indented network_policies entries from preset
93+ * @returns {string } Merged YAML with version header when missing
94+ */
95+ function mergePresetIntoPolicy ( currentPolicy , presetEntries ) {
96+ if ( ! currentPolicy || ! presetEntries ) {
97+ return "version: 1\n\nnetwork_policies:\n" + ( presetEntries || "" ) ;
10798 }
10899
109- // Get current policy YAML from sandbox
110- let rawPolicy = "" ;
111- try {
112- rawPolicy = runCapture (
113- buildPolicyGetCommand ( sandboxName ) ,
114- { ignoreError : true }
115- ) ;
116- } catch { }
117-
118- let currentPolicy = parseCurrentPolicy ( rawPolicy ) ;
119-
120- // Merge: inject preset entries under the existing network_policies key
121100 let merged ;
122- if ( currentPolicy && currentPolicy . includes ( "network_policies:" ) ) {
123- // Find the network_policies: line and append the new entries after it
124- // We need to insert before the next top-level key or end of file
101+ if ( / ^ n e t w o r k _ p o l i c i e s \s * : / m. test ( currentPolicy ) ) {
125102 const lines = currentPolicy . split ( "\n" ) ;
126103 const result = [ ] ;
127104 let inNetworkPolicies = false ;
128105 let inserted = false ;
129106
130107 for ( const line of lines ) {
131- // Detect top-level keys (no leading whitespace, ends with colon)
132108 const isTopLevel = / ^ \S .* : / . test ( line ) ;
133109
134- if ( line . trim ( ) === " network_policies:" || line . trim ( ) . startsWith ( "network_policies:" ) ) {
110+ if ( / ^ n e t w o r k _ p o l i c i e s \s * : / . test ( line ) ) {
135111 inNetworkPolicies = true ;
136112 result . push ( line ) ;
137113 continue ;
138114 }
139115
140116 if ( inNetworkPolicies && isTopLevel && ! inserted ) {
141- // We hit the next top-level key — insert preset entries before it
142117 result . push ( presetEntries ) ;
143118 inserted = true ;
144119 inNetworkPolicies = false ;
@@ -147,25 +122,55 @@ function applyPreset(sandboxName, presetName) {
147122 result . push ( line ) ;
148123 }
149124
150- // If network_policies was the last section, append at end
151125 if ( inNetworkPolicies && ! inserted ) {
152126 result . push ( presetEntries ) ;
153127 }
154128
155129 merged = result . join ( "\n" ) ;
156- } else if ( currentPolicy ) {
157- // No network_policies section yet — append one
158- // Ensure version field exists
159- if ( ! currentPolicy . includes ( "version:" ) ) {
160- currentPolicy = "version: 1\n" + currentPolicy ;
161- }
162- merged = currentPolicy + "\n\nnetwork_policies:\n" + presetEntries ;
163130 } else {
164- // No current policy at all
165- merged = "version: 1\n\nnetwork_policies:\n" + presetEntries ;
131+ const withVersion = currentPolicy . includes ( "version:" )
132+ ? currentPolicy
133+ : "version: 1\n" + currentPolicy ;
134+ merged = withVersion + "\n\nnetwork_policies:\n" + presetEntries ;
166135 }
167136
168- // Write temp file and apply
137+ if ( ! merged . trimStart ( ) . startsWith ( "version:" ) ) {
138+ merged = "version: 1\n" + merged ;
139+ }
140+ return merged ;
141+ }
142+
143+ function applyPreset ( sandboxName , presetName ) {
144+ // Guard against truncated sandbox names — WSL can truncate hyphenated
145+ // names during argument parsing, e.g. "my-assistant" → "m"
146+ const isRfc1123Label = / ^ [ a - z 0 - 9 ] ( [ a - z 0 - 9 - ] * [ a - z 0 - 9 ] ) ? $ / . test ( sandboxName ) ;
147+ if ( ! sandboxName || sandboxName . length > 63 || ! isRfc1123Label ) {
148+ throw new Error (
149+ `Invalid or truncated sandbox name: '${ sandboxName } '. ` +
150+ `Names must be 1-63 chars, lowercase alphanumeric, with optional internal hyphens.`
151+ ) ;
152+ }
153+
154+ const presetContent = loadPreset ( presetName ) ;
155+ if ( ! presetContent ) {
156+ console . error ( ` Cannot load preset: ${ presetName } ` ) ;
157+ return false ;
158+ }
159+
160+ const presetEntries = extractPresetEntries ( presetContent ) ;
161+ if ( ! presetEntries ) {
162+ console . error ( ` Preset ${ presetName } has no network_policies section.` ) ;
163+ return false ;
164+ }
165+
166+ const rawPolicy = runCapture (
167+ buildPolicyGetCommand ( sandboxName ) ,
168+ { ignoreError : true }
169+ ) ;
170+
171+ const currentPolicy = parseCurrentPolicy ( rawPolicy ) ;
172+ const merged = mergePresetIntoPolicy ( currentPolicy , presetEntries ) ;
173+
169174 const tmpFile = path . join ( os . tmpdir ( ) , `nemoclaw-policy-${ Date . now ( ) } .yaml` ) ;
170175 fs . writeFileSync ( tmpFile , merged , "utf-8" ) ;
171176
@@ -177,7 +182,6 @@ function applyPreset(sandboxName, presetName) {
177182 fs . unlinkSync ( tmpFile ) ;
178183 }
179184
180- // Update registry
181185 const sandbox = registry . getSandbox ( sandboxName ) ;
182186 if ( sandbox ) {
183187 const pols = sandbox . policies || [ ] ;
@@ -204,6 +208,7 @@ module.exports = {
204208 parseCurrentPolicy,
205209 buildPolicySetCommand,
206210 buildPolicyGetCommand,
211+ mergePresetIntoPolicy,
207212 applyPreset,
208213 getAppliedPresets,
209214} ;
0 commit comments