@@ -79,8 +79,9 @@ final class AgentTrustStore {
7979
8080 static let maxClients = AgentPairing . slotCount
8181 /// The read and write limit. Revocations have no count limit; at about 70
82- /// bytes each, this holds some 240,000 of them. Only past it does a write
83- /// leave the oldest revocations of apps without an approval row out.
82+ /// bytes each, this holds some 240,000 of them. Near it, approvals are no
83+ /// longer saved, and a new revocation of an app without an approval row stays
84+ /// in effect for this run only rather than push a saved one out.
8485 static let defaultMaxFileBytes = 16 * 1024 * 1024
8586 let maxFileBytes : Int
8687 // Approval does not expire. An approved app stays approved until the user
@@ -200,28 +201,34 @@ final class AgentTrustStore {
200201 needsAdopt = false
201202 return
202203 }
203- _ = commit ( merged , byUser : false )
204+ _ = commit ( merged , byUser : false , denial : true )
204205 }
205206
206207 private static func encodedSize ( _ file : File ) -> Int {
207208 ( try? JSONEncoder ( ) . encode ( file ) ) ? . count ?? Int . max
208209 }
209210
210- /// Room kept below the limit for changes that are not revocations (one
211- /// app's receipt, the overall choice, an approval row), so saving them never
212- /// has to push a saved revocation out.
211+ /// Room kept below the limit for writes that are not denials (an approval,
212+ /// a key change, a reconnect time, turning receipt on). Past the limit less
213+ /// this, such a write is refused if it would make the file larger, so this
214+ /// room is only ever used by denials.
213215 static let reserveBytes = 8 * 1024
214-
215- /// What is written when `file` would pass the limit. A denial is always
216- /// written and a saved revocation is never pushed out by this app's own
217- /// writes:
216+ /// Room a denial that adds a revocation of an app without a row leaves free,
217+ /// for receipt offs (at most a few hundred bytes for all slots), so turning
218+ /// receipt off never has to push a saved revocation out.
219+ static let flagReserveBytes = 1024
220+
221+ /// What a denial writes when `file` would pass the limit less
222+ /// `flagReserveBytes`. A saved revocation is never pushed out by this app's
223+ /// own writes:
218224 /// - every revocation that removes a row of the last intact file;
219225 /// - that file's own revocations (newest first, up to the limit, which they
220226 /// only miss if that file came from elsewhere already at the limit);
221- /// - other revocations, newest first, only up to the limit less the reserve.
227+ /// - revocations the user asked for that are not saved yet, then any others,
228+ /// newest first, up to the limit less `flagReserveBytes`.
222229 /// What is left out stays in effect for this run. Nil only if the rows'
223230 /// revocations alone do not fit, which the client limit rules out.
224- private static func fitting ( _ file : File , lastGood : File ? , limit : Int ) -> File ? {
231+ private static func fitting ( _ file : File , lastGood : File ? , asked : Set < String > , limit : Int ) -> File ? {
225232 let rows = Set ( ( lastGood ? . clients ?? [ ] ) . map ( \. keyID ) )
226233 let own = Set ( lastGood ? . revoked ?? [ ] )
227234 let required = file . revoked . filter { rows . contains ( $0 ) }
@@ -233,11 +240,13 @@ final class AgentTrustStore {
233240 // Takes entries in order while the exact size stays within `cap`.
234241 func take ( _ entries : [ String ] , cap : Int ) {
235242 var size = encodedSize ( fitted )
243+ var empty = kept . isEmpty
236244 for keyID in entries {
237- // Each entry adds its quoted bytes and a comma.
238- let cost = keyID . utf8 . count + 3
245+ // Its quoted bytes, and a comma unless the list was empty .
246+ let cost = keyID . utf8 . count + 2 + ( empty ? 0 : 1 )
239247 guard size + cost <= cap else { break }
240248 size += cost
249+ empty = false
241250 kept . insert ( keyID )
242251 }
243252 fitted . revoked = build ( )
@@ -247,9 +256,13 @@ final class AgentTrustStore {
247256 fitted . revoked = build ( )
248257 }
249258 }
250- take ( Array ( file . revoked . filter { own . contains ( $0 ) && ! rows . contains ( $0 ) } . reversed ( ) ) , cap : limit )
251- take ( Array ( file . revoked . filter { ! own . contains ( $0 ) && ! rows . contains ( $0 ) } . reversed ( ) ) ,
252- cap : max ( encodedSize ( fitted ) , limit - reserveBytes ) )
259+ let rest = file . revoked . filter { ! rows . contains ( $0 ) }
260+ take ( Array ( rest . filter { own . contains ( $0 ) } . reversed ( ) ) , cap : limit )
261+ let small = limit - flagReserveBytes
262+ take ( Array ( rest . filter { ! own . contains ( $0 ) && asked . contains ( $0 ) } . reversed ( ) ) ,
263+ cap : max ( encodedSize ( fitted ) , small ) )
264+ take ( Array ( rest . filter { ! own . contains ( $0 ) && ! asked . contains ( $0 ) } . reversed ( ) ) ,
265+ cap : max ( encodedSize ( fitted ) , small ) )
253266 return fitted
254267 }
255268
@@ -377,7 +390,7 @@ final class AgentTrustStore {
377390 guard let index = file . clients . firstIndex ( where : { $0 . id == id } ) else { return }
378391 var next = file
379392 next . clients [ index ] . lastSeen = now ( )
380- _ = commit ( next , byUser : false )
393+ _ = commit ( next , byUser : false , denial : false )
381394 }
382395
383396 /// Forgetting tombstones the key, so the same credential cannot come back
@@ -433,7 +446,7 @@ final class AgentTrustStore {
433446 private func deny ( _ next : File , _ denial : Denial ) -> Bool {
434447 if ! pending . contains ( denial ) { pending . append ( denial ) }
435448 file = next
436- if ! closedByDisk { _ = commit ( next , byUser : false ) }
449+ if ! closedByDisk { _ = commit ( next , byUser : false , denial : true ) }
437450 guard pending . contains ( denial ) else { return true }
438451 // Try again at the next check, even if nothing else changes the file.
439452 if ! closedByDisk || readFailed { needsAdopt = true }
@@ -447,7 +460,7 @@ final class AgentTrustStore {
447460 /// on top) and the grant is refused.
448461 private func grant ( _ next : File , byUser : Bool ) -> Bool {
449462 let before = file
450- guard commit ( next , byUser : byUser ) else { return false }
463+ guard commit ( next , byUser : byUser , denial : false ) else { return false }
451464 guard lastCommitSeen else {
452465 // Nothing granted stays in memory, even if the file is now gone.
453466 file = before
@@ -461,7 +474,7 @@ final class AgentTrustStore {
461474 /// file shows up there, its own writes do not settle pending denials.
462475 private var recreated = false
463476
464- private func commit ( _ next : File , byUser : Bool ) -> Bool {
477+ private func commit ( _ next : File , byUser : Bool , denial : Bool ) -> Bool {
465478 lastCommitSeen = false
466479 // A file written where the last one vanished may be replaced by the
467480 // original again, so pending denials are not settled by it.
@@ -471,24 +484,42 @@ final class AgentTrustStore {
471484 needsAdopt = true
472485 return false
473486 }
474- // Never replace the file with one that could not be read back. Past the
487+ // Never replace the file with one that could not be read back. Near the
475488 // limit only the most important revocations are written; the rest stay in
476489 // effect here and pending.
477490 var toWrite = next
478- if Self . encodedSize ( next ) > maxFileBytes - Self . reserveBytes {
479- guard let fitted = Self . fitting ( next , lastGood : lastGood , limit : maxFileBytes ) else {
491+ if denial || readFailed {
492+ // A denial, or the user's turning receipt on over an unreadable file.
493+ if Self . encodedSize ( next ) > maxFileBytes - Self . flagReserveBytes {
494+ let asked = Set ( pending . compactMap { item -> String ? in
495+ if case . revoke ( let keyID ) = item { return keyID }
496+ return nil
497+ } )
498+ guard let fitted = Self . fitting ( next , lastGood : lastGood , asked : asked , limit : maxFileBytes ) else {
499+ if ! pending . isEmpty { needsAdopt = true }
500+ return false
501+ }
502+ toWrite = fitted
503+ }
504+ } else if Self . encodedSize ( next ) > maxFileBytes - Self . reserveBytes {
505+ // Not a denial: only the revocations the file holds are written, and
506+ // the file may not grow past the limit less the reserve.
507+ let base = onDisk ?? lastGood
508+ let keep = Set ( ( base ? . revoked ?? [ ] ) + ( base ? . clients ?? [ ] ) . map ( \. keyID ) )
509+ toWrite . revoked = next . revoked . filter { keep . contains ( $0 ) }
510+ let bound = min ( maxFileBytes , max ( maxFileBytes - Self . reserveBytes , base . map ( Self . encodedSize ) ?? 0 ) )
511+ guard Self . encodedSize ( toWrite ) <= bound else {
480512 if ! pending . isEmpty { needsAdopt = true }
481513 return false
482514 }
483- toWrite = fitted
484- if ! readFailed , ! closedByDisk , toWrite == onDisk , diskStamp != nil , Self . stamp ( url ) == diskStamp {
485- // Nothing more fits: the file already holds all it can.
486- file = next
487- needsAdopt = false
488- lastCommitSeen = true
489- if ! recreated { pending . removeAll { Self . holds ( toWrite , $0 ) } }
490- return true
491- }
515+ }
516+ if toWrite != next , ! readFailed , ! closedByDisk , toWrite == onDisk , diskStamp != nil , Self . stamp ( url ) == diskStamp {
517+ // Nothing more fits: the file already holds all it can.
518+ file = next
519+ needsAdopt = false
520+ lastCommitSeen = true
521+ if ! recreated { pending . removeAll { Self . holds ( toWrite , $0 ) } }
522+ return true
492523 }
493524 guard let bytes = try? JSONEncoder ( ) . encode ( toWrite ) , bytes . count <= maxFileBytes ,
494525 ( try? write ( bytes , url ) ) != nil else {
0 commit comments