Skip to content

Commit 57c258c

Browse files
committed
Keep one app's unsaved off until a file with that app's row records it (#31)
Pending denials are removed only when the file holds them (receipt off, the app's row present and off, the key revoked and absent). An app's off for a row the file lacks stays pending and applies when the row comes back. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent 5459c95 commit 57c258c

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ final class AgentTrustStore {
164164
var merged = loaded
165165
for denial in pending { Self.apply(denial, to: &merged) }
166166
guard merged != loaded else {
167-
// The file already says what the user asked for.
167+
// The file already says what the user asked for, except one app's off
168+
// for a row it does not have: that stays for when the row comes back.
168169
file = loaded
169-
pending = []
170+
pending.removeAll { Self.holds(loaded, $0) }
170171
return
171172
}
172173
file = merged
@@ -311,6 +312,19 @@ final class AgentTrustStore {
311312
}
312313
}
313314

315+
/// The file records this denial. One app's off is recorded only by a file
316+
/// that has that app's row.
317+
private static func holds(_ file: File, _ denial: Denial) -> Bool {
318+
switch denial {
319+
case .receiptOff:
320+
return !file.autoReceive && file.receiptChoice == .off
321+
case .appReceiptOff(let id):
322+
return file.clients.first(where: { $0.id == id }).map { !$0.receiptEnabled } ?? false
323+
case .revoke(let keyID):
324+
return file.revoked.contains(keyID) && !file.clients.contains { $0.keyID == keyID }
325+
}
326+
}
327+
314328
private static func revoke(_ keyID: String, in file: inout File) {
315329
file.revoked.append(keyID)
316330
if file.revoked.count > maxRevoked { file.revoked.removeFirst(file.revoked.count - maxRevoked) }
@@ -347,18 +361,12 @@ final class AgentTrustStore {
347361
if case .loaded(let disk, let stamp) = Self.readDisk(url), disk == next {
348362
diskStamp = stamp
349363
needsAdopt = false
350-
pending = []
364+
pending.removeAll { Self.holds(next, $0) }
351365
} else {
352366
needsAdopt = true
353367
// A denial counts as saved only once the file is seen to hold it.
354-
if let denial, case .loaded(var disk, _) = Self.readDisk(url) {
355-
// One app's off is held only by a file that has that app's row.
356-
if case .appReceiptOff(let id) = denial, !disk.clients.contains(where: { $0.id == id }) { return false }
357-
let seen = disk
358-
Self.apply(denial, to: &disk)
359-
if disk != seen { return false }
360-
} else if denial != nil {
361-
return false
368+
if let denial {
369+
guard case .loaded(let disk, _) = Self.readDisk(url), Self.holds(disk, denial) else { return false }
362370
}
363371
}
364372
return true

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ import Foundation
146146
try AgentTrustStore.writePrivately(beforeGone, retryURL)
147147
retrying.adoptDisk()
148148
precondition(!retrying.changedOnDisk && AgentTrustStore(url: retryURL).receiptChoice == .off)
149+
// One app's unsaved off survives a file without that app's row, and
150+
// applies when the row comes back.
151+
let rowURL = directory.appendingPathComponent("row.json")
152+
var rowWritable = true
153+
let rows = AgentTrustStore(url: rowURL, write: { bytes, url in
154+
guard rowWritable else { throw POSIXError(.ENOSPC) }
155+
try AgentTrustStore.writePrivately(bytes, url)
156+
})
157+
let rowApp = try rows.trust(publicKey: P256.Signing.PrivateKey().publicKey.x963Representation, label: "A", host: hostA)
158+
let withRow = try Data(contentsOf: rowURL)
159+
rowWritable = false
160+
precondition(!rows.setReceiptEnabled(false, for: rowApp.id))
161+
var withoutRow = try JSONDecoder().decode(AgentTrustStore.File.self, from: withRow)
162+
withoutRow.clients = []
163+
try AgentTrustStore.writePrivately(JSONEncoder().encode(withoutRow), rowURL)
164+
rowWritable = true
165+
rows.adoptDisk()
166+
precondition(rows.clients.isEmpty && rows.hasUnsavedDenials)
167+
try AgentTrustStore.writePrivately(withRow, rowURL)
168+
rows.adoptDisk()
169+
precondition(rows.clients.map(\.receiptEnabled) == [false] && !rows.hasUnsavedDenials)
170+
precondition(AgentTrustStore(url: rowURL).clients.map(\.receiptEnabled) == [false])
149171

150172
// A denial that could not be saved is applied again on top of the file:
151173
// with a full revoked list, it is the disk's oldest entry that goes.

0 commit comments

Comments
 (0)