Skip to content

Commit 3218962

Browse files
committed
Fit a merge of two long revocation histories under the read limit, keeping what removes a row (#31)
Answers the round-21 Codex finding: two histories that each fit may not fit together. The merge then keeps every revocation that removes a row of the file read, then the file's own, then the newest others, and writes that, so a revoked app with a row in a restored backup stays revoked after a restart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent c3b8cf2 commit 3218962

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ final class AgentTrustStore {
177177
}
178178
var merged = loaded
179179
for denial in pending { Self.apply(denial, to: &merged) }
180+
if Self.encodedSize(merged) > Self.maxFileBytes {
181+
// Two histories each under the limit may not fit together. Keep every
182+
// revocation that removes a row of the file read (those apps would
183+
// otherwise connect), then the file's own, then the newest others.
184+
// What does not fit is no longer pending, so the merge is written.
185+
merged.revoked = Self.fitting(merged, mustKeep: Set(loaded.clients.map(\.keyID)), fileOwn: loaded.revoked)
186+
let kept = Set(merged.revoked)
187+
pending.removeAll { if case .revoke(let keyID) = $0 { return !kept.contains(keyID) }; return false }
188+
}
180189
guard merged != loaded else {
181190
// The file already says what the user asked for (or nothing is
182191
// pending), except one app's off for a row it does not have: that
@@ -195,6 +204,37 @@ final class AgentTrustStore {
195204
_ = commit(merged, byUser: false)
196205
}
197206

207+
private static func encodedSize(_ file: File) -> Int {
208+
(try? JSONEncoder().encode(file))?.count ?? Int.max
209+
}
210+
211+
/// The revocations of `file` that fit under the read limit, in their order.
212+
private static func fitting(_ file: File, mustKeep: Set<String>, fileOwn: [String]) -> [String] {
213+
let own = Set(fileOwn)
214+
let required = file.revoked.filter { mustKeep.contains($0) }
215+
let fromFile = file.revoked.filter { own.contains($0) && !mustKeep.contains($0) }
216+
let others = file.revoked.filter { !own.contains($0) && !mustKeep.contains($0) }.reversed()
217+
var empty = file
218+
empty.revoked = []
219+
var size = encodedSize(empty)
220+
var kept = Set<String>()
221+
for keyID in required + fromFile + others {
222+
// Each entry adds its quoted bytes and a comma.
223+
let cost = keyID.utf8.count + 3
224+
guard mustKeep.contains(keyID) || size + cost <= maxFileBytes - 64 else { continue }
225+
size += cost
226+
kept.insert(keyID)
227+
}
228+
var result = file.revoked.filter { kept.contains($0) }
229+
var check = file
230+
check.revoked = result
231+
while encodedSize(check) > maxFileBytes, let drop = result.lastIndex(where: { !mustKeep.contains($0) }) {
232+
result.remove(at: drop)
233+
check.revoked = result
234+
}
235+
return result
236+
}
237+
198238
private func close(failed: Bool) {
199239
var closed = File()
200240
closed.receiptChoice = .off

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,28 @@ 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+
// Two histories that each fit but not together: this instance knows 2,000
422+
// revocations ending with K, and a restored backup holds 2,000 others plus
423+
// K's row with receipt on. K stays revoked across a restart.
424+
let mergeURL = directory.appendingPathComponent("merge-limit.json")
425+
let merging = AgentTrustStore(url: mergeURL)
426+
let mergeApp = try merging.trust(publicKey: P256.Signing.PrivateKey().publicKey.x963Representation, label: "K", host: hostA)
427+
precondition(merging.setAutoReceive(true))
428+
var mine = try JSONDecoder().decode(AgentTrustStore.File.self, from: Data(contentsOf: mergeURL))
429+
var theirs = mine
430+
mine.revoked = (0..<1999).map { String(format: "a%063d", $0) }
431+
try AgentTrustStore.writePrivately(JSONEncoder().encode(mine), mergeURL)
432+
merging.adoptDisk()
433+
precondition(merging.forget(mergeApp.id) && merging.file.revoked.count == 2000)
434+
theirs.revoked = (0..<2000).map { String(format: "b%063d", $0) }
435+
try AgentTrustStore.writePrivately(JSONEncoder().encode(theirs), mergeURL)
436+
merging.adoptDisk()
437+
precondition(merging.clients.isEmpty && !merging.hasUnsavedDenials && !merging.changedOnDisk)
438+
let mergedAgain = AgentTrustStore(url: mergeURL)
439+
precondition(mergedAgain.clients.isEmpty && mergedAgain.lookup(keyID: mergeApp.keyID, host: hostA) == .revoked)
440+
for keyID in theirs.revoked { precondition(mergedAgain.lookup(keyID: keyID, host: hostA) == .revoked) }
441+
precondition(mergedAgain.file.revoked.contains(String(format: "a%063d", 1998)))
442+
421443
// Reconnect policy shared with the helper.
422444
precondition(AgentReconnect.next(after: "receiving_only", attempt: 3) == .done)
423445
precondition(AgentReconnect.next(after: "ready", attempt: 0) == .done)

0 commit comments

Comments
 (0)