Skip to content

Commit ee78d9c

Browse files
committed
Fit after an unreadable file by the last intact file, not by nothing (#31)
Answers the round-24 Codex finding: clearing onDisk also cleared what the fit keeps first, so replacing an unreadable file could push out revocations the last readable file held. lastGood keeps the last intact file for the fit; onDisk (cleared on missing/unreadable) only gates skipping a write. Test: replacing an unreadable near-limit file keeps every revocation it held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent b4d4c2a commit ee78d9c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ final class AgentTrustStore {
115115
case .loaded(let loaded, let stamp):
116116
file = loaded
117117
onDisk = loaded
118+
lastGood = loaded
118119
loadedExisting = true
119120
diskStamp = stamp
120121
case .missing:
@@ -169,6 +170,7 @@ final class AgentTrustStore {
169170
closedByDisk = false
170171
diskStamp = stamp
171172
onDisk = loaded
173+
lastGood = loaded
172174
needsAdopt = false
173175
// Revocations are never undone (a revoked key cannot be approved again),
174176
// so every revocation this run knows is kept, also over a restored older
@@ -462,7 +464,7 @@ final class AgentTrustStore {
462464
// effect here and pending.
463465
var toWrite = next
464466
if Self.encodedSize(next) > Self.maxFileBytes {
465-
guard let fitted = Self.fitting(next, onDisk: onDisk) else {
467+
guard let fitted = Self.fitting(next, onDisk: lastGood) else {
466468
if !pending.isEmpty { needsAdopt = true }
467469
return false
468470
}
@@ -490,6 +492,7 @@ final class AgentTrustStore {
490492
if case .loaded(let disk, let stamp) = Self.readDisk(url), disk == toWrite {
491493
diskStamp = stamp
492494
onDisk = disk
495+
lastGood = disk
493496
needsAdopt = false
494497
lastCommitSeen = true
495498
if !recreated { pending.removeAll { Self.holds(toWrite, $0) } }
@@ -501,6 +504,9 @@ final class AgentTrustStore {
501504

502505
/// The file as this instance last read or wrote it.
503506
private var onDisk: File?
507+
/// The last file read or written intact, kept while the file is missing or
508+
/// unreadable: what it held is still what the fit keeps first.
509+
private var lastGood: File?
504510

505511
/// The last write was read back unchanged.
506512
private var lastCommitSeen = false

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ import Foundation
426426
precondition(atLimit.setAutoReceive(true) && !atLimit.readFailed && atLimit.autoReceive)
427427
let replaced = AgentTrustStore(url: limitURL)
428428
precondition(!replaced.readFailed && replaced.autoReceive)
429+
// Replacing it keeps every revocation the last readable file held (the
430+
// oldest too); only what never fit is left out, and it stays pending.
431+
for keyID in nearLimit + (1...overflow).map({ String(format: "x%063d", $0) }) {
432+
precondition(replaced.lookup(keyID: keyID, host: hostA) == .revoked)
433+
}
434+
precondition(atLimit.hasUnsavedDenials)
429435

430436
// Two histories that each fit but not together: this instance knows 2,000
431437
// revocations ending with K, and a restored backup holds 2,000 others plus

0 commit comments

Comments
 (0)