Skip to content

Commit b4d4c2a

Browse files
committed
Forget the last read file once it is missing or unreadable; never skip a write over it (#31)
Answers the round-23 reviews: the skip-identical-write branch compared with a file read before it became unreadable, so turning receipt on left the unreadable file in place. onDisk is cleared when the file is missing or unreadable and the skip needs a readable, open file. The restore test now puts back a copy without K's row before the one with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent 0dd7d66 commit b4d4c2a

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ final class AgentTrustStore {
145145
let known = file.revoked
146146
let disk = Self.readDisk(url)
147147
guard case .loaded(let loaded, let stamp) = disk else {
148+
// What was last read is no longer what the file holds.
149+
onDisk = nil
148150
if case .unreadable(let stamp) = disk {
149151
diskStamp = stamp
150152
needsAdopt = stamp == nil
@@ -465,7 +467,7 @@ final class AgentTrustStore {
465467
return false
466468
}
467469
toWrite = fitted
468-
if toWrite == onDisk, diskStamp != nil, Self.stamp(url) == diskStamp {
470+
if !readFailed, !closedByDisk, toWrite == onDisk, diskStamp != nil, Self.stamp(url) == diskStamp {
469471
// Nothing more fits: the file already holds all it can.
470472
file = next
471473
needsAdopt = false

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,15 @@ import Foundation
418418
let limitAfter = try Data(contentsOf: limitURL)
419419
precondition(limitAfter.count <= AgentTrustStore.maxFileBytes && AgentTrustStore(url: limitURL).lookup(keyID: String(format: "x%063d", overflow), host: hostA) == .revoked)
420420

421+
// An unreadable file is never taken for the last good one: turning receipt
422+
// on replaces it even when the fitted list equals what was read before.
423+
precondition(chmod(limitURL.path, 0o644) == 0)
424+
atLimit.adoptDisk()
425+
precondition(atLimit.readFailed)
426+
precondition(atLimit.setAutoReceive(true) && !atLimit.readFailed && atLimit.autoReceive)
427+
let replaced = AgentTrustStore(url: limitURL)
428+
precondition(!replaced.readFailed && replaced.autoReceive)
429+
421430
// Two histories that each fit but not together: this instance knows 2,000
422431
// revocations ending with K, and a restored backup holds 2,000 others plus
423432
// K's row with receipt on. K stays revoked across a restart.
@@ -437,9 +446,10 @@ import Foundation
437446
// What did not fit on disk stays revoked here and pending.
438447
precondition(merging.clients.isEmpty && merging.hasUnsavedDenials && !merging.changedOnDisk)
439448
precondition(merging.lookup(keyID: String(format: "a%063d", 0), host: hostA) == .revoked)
440-
// A second backup taken earlier, with K's row and none of these, still
441-
// does not bring K back in this run or after a restart.
449+
// Two more restores in a row: first a copy without K's row (so K need not
450+
// be written), then the copy with K's row. K is never trusted again.
442451
var earlier = theirs
452+
earlier.clients = []
443453
earlier.revoked = (0..<2000).map { String(format: "c%063d", $0) }
444454
try AgentTrustStore.writePrivately(JSONEncoder().encode(earlier), mergeURL)
445455
merging.adoptDisk()
@@ -448,6 +458,7 @@ import Foundation
448458
precondition(AgentTrustStore(url: mergeURL).clients.isEmpty)
449459
try AgentTrustStore.writePrivately(JSONEncoder().encode(theirs), mergeURL)
450460
merging.adoptDisk()
461+
precondition(merging.clients.isEmpty && merging.lookup(keyID: mergeApp.keyID, host: hostA) == .revoked)
451462
let mergedAgain = AgentTrustStore(url: mergeURL)
452463
precondition(mergedAgain.clients.isEmpty && mergedAgain.lookup(keyID: mergeApp.keyID, host: hostA) == .revoked)
453464
for keyID in theirs.revoked { precondition(mergedAgain.lookup(keyID: keyID, host: hostA) == .revoked) }

0 commit comments

Comments
 (0)