@@ -150,7 +150,7 @@ final class AgentTrustStore {
150150 if knew { close ( failed : false ) }
151151 // Nothing is written in place of a missing file (it may be restored);
152152 // unsaved denials stay and apply to whatever appears there.
153- needsAdopt = ! pending . isEmpty
153+ needsAdopt = false
154154 return
155155 }
156156 readFailed = false
@@ -213,14 +213,17 @@ final class AgentTrustStore {
213213 // The tab never overrides the user's off.
214214 if ! byUser , value , receiptChoice == . off { return false }
215215 let choice : AgentReceiptChoice = byUser ? ( value ? . auto : . off ) : receiptChoice
216- guard file . autoReceive != value || file . receiptChoice != choice || ! pending . isEmpty || readFailed else { return true }
216+ // Memory matches a file this instance read or wrote, with nothing pending.
217+ let settled = ! closedByDisk && pending . isEmpty
218+ guard ! settled || file . autoReceive != value || file . receiptChoice != choice else { return true }
217219 var next = file
218220 next . autoReceive = value
219221 next . receiptChoice = choice
220- guard value else { return commitDenial ( next , . receiptOff ) }
222+ guard value else { return deny ( next , . receiptOff ) }
221223 guard commit ( next , byUser : byUser ) else { return false }
222- // A saved on supersedes an off that was never saved; a failed on does not.
223- if byUser { pending . removeAll { $0 == . receiptOff } }
224+ // An on seen in the file supersedes an off that was never saved; an on
225+ // that is not seen there does not.
226+ if byUser , lastCommitSeen { pending . removeAll { $0 == . receiptOff } }
224227 return true
225228 }
226229
@@ -238,12 +241,17 @@ final class AgentTrustStore {
238241 var next = file
239242 let time = now ( )
240243 let client : AgentTrustedClient
244+ var replaced : String ?
241245 if let existing = next . clients . first ( where : { $0 . keyID == keyID } ) , existing . host != host {
242246 throw Failure . hostChanged
243247 }
244248 if let index = next . clients . firstIndex ( where : { $0 . host == host } ) {
245249 let prior = next . clients [ index ]
246- if prior . keyID != keyID { Self . revoke ( prior . keyID , in : & next ) }
250+ if prior . keyID != keyID {
251+ // The replaced key is a denial like any other: kept until the file holds it.
252+ Self . revoke ( prior . keyID , in : & next )
253+ replaced = prior . keyID
254+ }
247255 client = AgentTrustedClient ( id : prior . id , keyID : keyID , publicKey : publicKey , label : label ,
248256 host : host , created : prior . created , lastSeen : time , receivesDrafts : true )
249257 next . clients [ index ] = client
@@ -253,7 +261,15 @@ final class AgentTrustStore {
253261 host : host , created : time , lastSeen : time , receivesDrafts : true )
254262 next . clients . append ( client )
255263 }
256- guard commit ( next , byUser : false ) else { throw Failure . unwritable }
264+ if let replaced , ! pending . contains ( . revoke ( replaced ) ) { pending . append ( . revoke ( replaced ) ) }
265+ guard commit ( next , byUser : false ) else {
266+ // The new key is not approved, but the replaced one stays revoked.
267+ if let replaced {
268+ Self . apply ( . revoke ( replaced ) , to : & file )
269+ needsAdopt = true
270+ }
271+ throw Failure . unwritable
272+ }
257273 return client
258274 }
259275
@@ -262,18 +278,19 @@ final class AgentTrustStore {
262278 @ discardableResult
263279 func setReceiptEnabled ( _ enabled : Bool , for id : UUID ) -> Bool {
264280 guard let index = file . clients . firstIndex ( where : { $0 . id == id } ) else {
265- // The row is not loaded right now ( the file vanished or cannot be
266- // read): nothing is written, and the off applies when the row is back.
267- guard ! enabled , closedByDisk else { return false }
281+ // The row is not in the file as read right now: nothing is written,
282+ // and the off applies when the row is back.
283+ guard ! enabled else { return false }
268284 if ! pending . contains ( . appReceiptOff ( id ) ) { pending . append ( . appReceiptOff ( id ) ) }
269285 return false
270286 }
271- guard file . clients [ index ] . receiptEnabled != enabled || ! pending . isEmpty else { return true }
287+ let settled = ! closedByDisk && pending . isEmpty
288+ guard ! settled || file . clients [ index ] . receiptEnabled != enabled else { return true }
272289 var next = file
273290 next . clients [ index ] . receivesDrafts = enabled
274- guard enabled else { return commitDenial ( next , . appReceiptOff ( id ) ) }
291+ guard enabled else { return deny ( next , . appReceiptOff ( id ) ) }
275292 guard commit ( next , byUser : false ) else { return false }
276- pending . removeAll { $0 == . appReceiptOff ( id ) }
293+ if lastCommitSeen { pending . removeAll { $0 == . appReceiptOff ( id ) } }
277294 return true
278295 }
279296
@@ -299,7 +316,7 @@ final class AgentTrustStore {
299316 func forget ( keyID : String ) -> Bool {
300317 var next = file
301318 Self . apply ( . revoke ( keyID ) , to : & next )
302- return commitDenial ( next , . revoke ( keyID ) )
319+ return deny ( next , . revoke ( keyID ) )
303320 }
304321
305322 private static func apply ( _ denial : Denial , to file : inout File ) {
@@ -333,20 +350,23 @@ final class AgentTrustStore {
333350 if file . revoked . count > maxRevoked { file . revoked . removeFirst ( file . revoked . count - maxRevoked ) }
334351 }
335352
336- /// The denial is in effect here at once; if it cannot be saved it stays
337- /// pending and is applied again to the file when it can be.
338- private func commitDenial ( _ next : File , _ denial : Denial ) -> Bool {
339- guard ! commit ( next , byUser : false , denial : denial ) else { return true }
340- file = next
353+ /// A denial is in effect here at once and stays pending until a re-read of
354+ /// the file shows it. Nothing is written in place of a file that vanished or
355+ /// could not be read; the denial applies to it when it is back.
356+ private func deny ( _ next : File , _ denial : Denial ) -> Bool {
341357 if ! pending . contains ( denial ) { pending . append ( denial ) }
358+ file = next
359+ if ! closedByDisk { _ = commit ( next , byUser : false ) }
360+ guard pending . contains ( denial ) else { return true }
342361 // Try again at the next check, even if nothing else changes the file.
343- needsAdopt = true
362+ if ! closedByDisk || readFailed { needsAdopt = true }
344363 return false
345364 }
346365
347366 /// Nothing is written over a file that could not be read, except a user's
348367 /// turning receipt on, which replaces it with a new list.
349- private func commit ( _ next : File , byUser : Bool , denial : Denial ? = nil ) -> Bool {
368+ private func commit ( _ next : File , byUser : Bool ) -> Bool {
369+ lastCommitSeen = false
350370 guard ! readFailed || byUser else {
351371 needsAdopt = true
352372 return false
@@ -364,19 +384,17 @@ final class AgentTrustStore {
364384 if case . loaded ( let disk , let stamp ) = Self . readDisk ( url ) , disk == next {
365385 diskStamp = stamp
366386 needsAdopt = false
387+ lastCommitSeen = true
367388 pending . removeAll { Self . holds ( next , $0 ) }
368- // A denial the written file does not record is not saved.
369- if let denial , ! Self . holds ( next , denial ) { return false }
370389 } else {
371390 needsAdopt = true
372- // A denial counts as saved only once the file is seen to hold it.
373- if let denial {
374- guard case . loaded ( let disk , _) = Self . readDisk ( url ) , Self . holds ( disk , denial ) else { return false }
375- }
376391 }
377392 return true
378393 }
379394
395+ /// The last write was read back unchanged.
396+ private var lastCommitSeen = false
397+
380398 /// Denials made here that the file does not hold yet.
381399 var hasUnsavedDenials : Bool { ! pending . isEmpty }
382400
0 commit comments