Skip to content

Commit a2b4f6d

Browse files
committed
Treat every denial as pending until a re-read file holds it (#31)
Closes the class "denial reported saved but held by neither the file nor the pending list" instead of single instances: - Every denial (receipt off, one app's off, revoke, and the key replaced by a rotation approval) is added to the pending list first and removed only when a write is read back unchanged and holds it. - Nothing is written in place of a file that vanished or cannot be read; denials made then apply when it is back. - An off for a row missing from a readable file stays pending as well. - The "nothing changed" shortcut applies only when memory reflects a file this instance read or wrote and nothing is pending. - A user's on removes a pending off only if the write was read back. - A failed rotation approval still revokes the replaced key in memory. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent 412d35a commit a2b4f6d

2 files changed

Lines changed: 66 additions & 28 deletions

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ import Foundation
142142
precondition(unlink(retryURL.path) == 0)
143143
spaceLeft = true
144144
retrying.adoptDisk()
145-
precondition(retrying.changedOnDisk && access(retryURL.path, F_OK) != 0)
145+
precondition(retrying.hasUnsavedDenials && access(retryURL.path, F_OK) != 0)
146146
try AgentTrustStore.writePrivately(beforeGone, retryURL)
147147
retrying.adoptDisk()
148148
precondition(!retrying.changedOnDisk && AgentTrustStore(url: retryURL).receiptChoice == .off)
@@ -178,6 +178,26 @@ import Foundation
178178
try AgentTrustStore.writePrivately(withRow2, rowURL)
179179
rows.adoptDisk()
180180
precondition(rows.clients.first { $0.id == rowApp2.id }?.receiptEnabled == false && !rows.hasUnsavedDenials)
181+
// Receipt off and a revocation clicked right after the file vanished are
182+
// kept and apply when it is back; nothing is written in its place.
183+
precondition(rows.setAutoReceive(true))
184+
let rowApp3 = try rows.trust(publicKey: P256.Signing.PrivateKey().publicKey.x963Representation, label: "C",
185+
host: AgentPairing.registeredHostPrefix + "team.ABCDE12345:com.example.agent-c")
186+
let beforeVanish = try Data(contentsOf: rowURL)
187+
precondition(unlink(rowURL.path) == 0)
188+
rows.adoptDisk()
189+
precondition(!rows.setAutoReceive(false) && !rows.forget(keyID: rowApp3.keyID) && access(rowURL.path, F_OK) != 0)
190+
try AgentTrustStore.writePrivately(beforeVanish, rowURL)
191+
rows.adoptDisk()
192+
precondition(!rows.autoReceive && rows.lookup(keyID: rowApp3.keyID, host: rowApp3.host) == .revoked && !rows.hasUnsavedDenials)
193+
// An app's off for a row missing from a readable file is kept as well.
194+
let rowFile = try JSONDecoder().decode(AgentTrustStore.File.self, from: Data(contentsOf: rowURL))
195+
var lacking = rowFile
196+
lacking.clients.removeAll { $0.id == rowApp.id }
197+
try AgentTrustStore.writePrivately(JSONEncoder().encode(lacking), rowURL)
198+
rows.adoptDisk()
199+
precondition(rows.setReceiptEnabled(true, for: rowApp2.id))
200+
precondition(!rows.setReceiptEnabled(false, for: rowApp.id) && rows.hasUnsavedDenials)
181201

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

0 commit comments

Comments
 (0)