Skip to content

Commit a7aeb83

Browse files
committed
Keep this instance's revocations when adopting an older writer's file (#31)
Codex bot P1 on #34: a stale file written by an older app instance could remove a tombstone this instance had saved, letting the forgotten key reconnect. Adoption now always unions the local revoked keys, drops their clients and writes the tombstones back. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent f3ddb9a commit a7aeb83

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,20 @@ final class AgentTrustStore {
103103
func adoptDisk() {
104104
defer { diskStamp = Self.stamp(url) }
105105
if let loaded = Self.read(url) {
106-
guard hasUnsavedDenial else { file = loaded; return }
106+
// A key this instance revoked stays revoked, even if an older writer
107+
// saved a list that predates the revocation.
107108
var merged = loaded
108-
merged.revoked = loaded.revoked + file.revoked.filter { !loaded.revoked.contains($0) }
109-
merged.clients = loaded.clients.compactMap { disk in
109+
let localOnly = file.revoked.filter { !loaded.revoked.contains($0) }
110+
merged.revoked = loaded.revoked + localOnly
111+
if merged.revoked.count > Self.maxRevoked { merged.revoked.removeFirst(merged.revoked.count - Self.maxRevoked) }
112+
merged.clients.removeAll { merged.revoked.contains($0.keyID) || file.revoked.contains($0.keyID) }
113+
guard hasUnsavedDenial else {
114+
file = merged
115+
// Write the tombstones back so the older writer's list does not stand.
116+
if !localOnly.isEmpty { _ = commit(merged) }
117+
return
118+
}
119+
merged.clients = merged.clients.compactMap { disk in
110120
guard !merged.revoked.contains(disk.keyID),
111121
let mine = file.clients.first(where: { $0.id == disk.id }) else { return nil }
112122
var client = disk

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ import Foundation
7171
precondition(!holder.changedOnDisk && standby.changedOnDisk)
7272
standby.adoptDisk()
7373
precondition(standby.clients.map(\.id) == [readd.id] && !standby.changedOnDisk)
74+
// An older writer saving a list from before a revocation does not bring the key back.
75+
let staleCopy = try Data(contentsOf: sharedURL)
76+
precondition(standby.forget(readd.id))
77+
try AgentTrustStore.writePrivately(staleCopy, sharedURL)
78+
precondition(standby.changedOnDisk)
79+
standby.adoptDisk()
80+
precondition(standby.clients.isEmpty && standby.lookup(keyID: readd.keyID, host: hostB) == .revoked)
81+
precondition(AgentTrustStore(url: sharedURL).lookup(keyID: readd.keyID, host: hostB) == .revoked)
7482
precondition(unlink(sharedURL.path) == 0 && standby.changedOnDisk)
7583
standby.adoptDisk()
7684
precondition(standby.clients.isEmpty && standby.receiptChoice == .off && !standby.autoReceive)

0 commit comments

Comments
 (0)