@@ -105,50 +105,26 @@ describe("legacy exec approvals migration", () => {
105105 ) ;
106106 } ) ;
107107
108- it ( "imports, verifies, receipts, and removes valid legacy policy" , async ( ) => {
108+ it . each ( [
109+ { name : "absent" , usage : { } } ,
110+ { name : "historical null" , usage : { lastUsedAt : null , lastUsedCommand : null } } ,
111+ ] ) ( "imports $name usage metadata and releases the runtime gate" , async ( { usage } ) => {
109112 const { env, stateDir, sourcePath } = useStateDir ( ) ;
110113 const expected = {
111114 version : 1 as const ,
112115 socket : { path : "/tmp/approvals.sock" , token : "secret" } ,
113116 defaults : { security : "allowlist" as const , ask : "on-miss" as const } ,
114- agents : { main : { allowlist : [ { pattern : "/usr/bin/rg" } ] } } ,
115- } ;
116- await writeLegacy ( sourcePath , expected ) ;
117-
118- const result = await migrate ( { env, stateDir } ) ;
119-
120- expect ( result . warnings ) . toEqual ( [ ] ) ;
121- expect ( result . changes ) . toEqual ( [ "Imported legacy exec approvals into shared SQLite state." ] ) ;
122- const importedRaw = readExecApprovalsConfigRow ( database ( env ) ) ?. raw_json ;
123- expect ( importedRaw ) . toContain ( '"pattern": "/usr/bin/rg"' ) ;
124- expect ( importedRaw ) . toContain ( '"id":' ) ;
125- expect ( fs . existsSync ( sourcePath ) ) . toBe ( false ) ;
126- expect ( receipt ( env ) ) . toMatchObject ( {
127- removed_source : 1 ,
128- source_record_count : 1 ,
129- status : "completed" ,
130- target_table : "exec_approvals_config" ,
131- } ) ;
132- expect ( runReceipt ( env ) ) . toMatchObject ( { status : "completed" } ) ;
133- } ) ;
134-
135- it ( "imports historical null allowlist usage metadata and releases the runtime gate" , async ( ) => {
136- const { env, stateDir, sourcePath } = useStateDir ( ) ;
137- await writeLegacy ( sourcePath , {
138- version : 1 ,
139- defaults : { security : "allowlist" } ,
140117 agents : {
141- main : {
118+ main : { allowlist : [ { pattern : "/usr/bin/rg" , ...usage } ] } ,
119+ "*" : {
142120 allowlist : [
143- {
144- pattern : "/usr/bin/rg" ,
145- lastUsedAt : null ,
146- lastUsedCommand : null ,
147- } ,
121+ { pattern : "/usr/bin/unused" , ...usage } ,
122+ { pattern : "/usr/bin/used" , lastUsedAt : 0 , lastUsedCommand : "" } ,
148123 ] ,
149124 } ,
150125 } ,
151- } ) ;
126+ } ;
127+ await writeLegacy ( sourcePath , expected ) ;
152128 setTestEnvValue ( "OPENCLAW_STATE_DIR" , stateDir ) ;
153129 execApprovalsStoreTesting . reset ( ) ;
154130 expect ( ( ) => loadExecApprovals ( ) ) . toThrow ( ExecApprovalsMigrationRequiredError ) ;
@@ -161,13 +137,23 @@ describe("legacy exec approvals migration", () => {
161137 "Removed retired exec approvals JSON after recording its migration decision." ,
162138 ] ) ;
163139 const imported = loadExecApprovals ( ) ;
140+ expect ( imported . defaults ) . toMatchObject ( expected . defaults ) ;
141+ expect ( imported . socket ) . toEqual ( expected . socket ) ;
164142 expect ( imported . agents ?. main ?. allowlist ) . toEqual ( [
165- expect . objectContaining ( { pattern : "/usr/bin/rg" } ) ,
143+ { id : expect . any ( String ) , pattern : "/usr/bin/rg" } ,
144+ ] ) ;
145+ expect ( imported . agents ?. [ "*" ] ?. allowlist ) . toEqual ( [
146+ { id : expect . any ( String ) , pattern : "/usr/bin/unused" } ,
147+ { id : expect . any ( String ) , pattern : "/usr/bin/used" , lastUsedAt : 0 , lastUsedCommand : "" } ,
166148 ] ) ;
167- expect ( imported . agents ?. main ?. allowlist ?. [ 0 ] ) . not . toHaveProperty ( "lastUsedAt" ) ;
168- expect ( imported . agents ?. main ?. allowlist ?. [ 0 ] ) . not . toHaveProperty ( "lastUsedCommand" ) ;
169149 expect ( fs . existsSync ( sourcePath ) ) . toBe ( false ) ;
170- expect ( receipt ( env ) ) . toMatchObject ( { removed_source : 1 , status : "completed" } ) ;
150+ expect ( receipt ( env ) ) . toMatchObject ( {
151+ removed_source : 1 ,
152+ source_record_count : 1 ,
153+ status : "completed" ,
154+ target_table : "exec_approvals_config" ,
155+ } ) ;
156+ expect ( runReceipt ( env ) ) . toMatchObject ( { status : "completed" } ) ;
171157 } ) ;
172158
173159 it ( "is idempotent after successful source removal" , async ( ) => {
@@ -179,37 +165,42 @@ describe("legacy exec approvals migration", () => {
179165 expect ( receipt ( env ) ) . toMatchObject ( { removed_source : 1 } ) ;
180166 } ) ;
181167
182- it ( "preserves malformed bytes and records a non-removal receipt" , async ( ) => {
168+ it . each ( [
169+ { name : "invalid JSON" , raw : "{malformed-secret-marker" } ,
170+ ...[
171+ { lastUsedAt : "now" , lastUsedCommand : null } ,
172+ { lastUsedAt : null , lastUsedCommand : 42 } ,
173+ { lastUsedAt : null , argPattern : null } ,
174+ { lastUsedCommand : null , lastResolvedPath : null } ,
175+ ] . map ( ( metadata ) => ( {
176+ name : JSON . stringify ( metadata ) ,
177+ raw : JSON . stringify ( {
178+ version : 1 ,
179+ agents : { main : { allowlist : [ { pattern : "secret-marker" , ...metadata } ] } } ,
180+ } ) ,
181+ } ) ) ,
182+ {
183+ name : "null policy" ,
184+ raw : JSON . stringify ( {
185+ version : 1 ,
186+ defaults : { security : null } ,
187+ agents : { main : { allowlist : [ { pattern : "secret-marker" , lastUsedAt : null } ] } } ,
188+ } ) ,
189+ } ,
190+ ] ) ( "preserves malformed $name and records a non-removal receipt" , async ( { raw } ) => {
183191 const { env, stateDir, sourcePath } = useStateDir ( ) ;
184- await fsp . writeFile ( sourcePath , "{malformed-secret-marker" , "utf8" ) ;
192+ await fsp . writeFile ( sourcePath , raw , "utf8" ) ;
185193
186194 const result = await migrate ( { env, stateDir } ) ;
187195
188196 expect ( result . changes ) . toEqual ( [ ] ) ;
189197 expect ( result . warnings [ 0 ] ) . toContain ( "Preserved malformed legacy exec approvals" ) ;
190198 expect ( JSON . stringify ( result ) ) . not . toContain ( "secret-marker" ) ;
191- expect ( fs . readFileSync ( sourcePath , "utf8" ) ) . toContain ( "secret-marker" ) ;
199+ expect ( fs . readFileSync ( sourcePath , "utf8" ) ) . toBe ( raw ) ;
192200 expect ( receipt ( env ) ) . toMatchObject ( { removed_source : 0 , source_record_count : 0 } ) ;
193201 expect ( readExecApprovalsConfigRow ( database ( env ) ) ) . toBeUndefined ( ) ;
194202 } ) ;
195203
196- it ( "keeps null metadata outside the historical fields invalid" , async ( ) => {
197- const { env, stateDir, sourcePath } = useStateDir ( ) ;
198- await writeLegacy ( sourcePath , {
199- version : 1 ,
200- agents : {
201- main : { allowlist : [ { pattern : "/usr/bin/rg" , lastResolvedPath : null } ] } ,
202- } ,
203- } ) ;
204-
205- const result = await migrate ( { env, stateDir } ) ;
206-
207- expect ( result . changes ) . toEqual ( [ ] ) ;
208- expect ( result . warnings [ 0 ] ) . toContain ( "Preserved malformed legacy exec approvals" ) ;
209- expect ( fs . existsSync ( sourcePath ) ) . toBe ( true ) ;
210- expect ( readExecApprovalsConfigRow ( database ( env ) ) ) . toBeUndefined ( ) ;
211- } ) ;
212-
213204 it ( "removes a byte-identical source when canonical state already exists" , async ( ) => {
214205 const { env, stateDir, sourcePath } = useStateDir ( ) ;
215206 const file = { version : 1 as const , defaults : { security : "deny" as const } , agents : { } } ;
@@ -300,9 +291,12 @@ describe("legacy exec approvals migration", () => {
300291 expect ( receipt ( env ) ) . toBeUndefined ( ) ;
301292 } ) ;
302293
303- it ( "retains the claim after cleanup failure and converges on retry" , async ( ) => {
294+ it ( "retains the null-metadata source claim after cleanup failure and converges on retry" , async ( ) => {
304295 const { env, stateDir, sourcePath } = useStateDir ( ) ;
305- await writeLegacy ( sourcePath , { version : 1 , agents : { } } ) ;
296+ await writeLegacy ( sourcePath , {
297+ version : 1 ,
298+ agents : { main : { allowlist : [ { pattern : "/usr/bin/rg" , lastUsedAt : null } ] } } ,
299+ } ) ;
306300 const first = await migrate ( {
307301 env,
308302 stateDir,
0 commit comments