Skip to content

Commit 74c01a5

Browse files
committed
Record the unreadable state before takeover and close remaining denial gaps (#31)
- turnOnReceive reads readFailed before startPairing, whose takeover re-reads the file, so the first click that finds it unreadable never overwrites it. - Unsaved denials while the file is missing are written to a new file, or retried at the next check. - A user's later on removes an unsaved receipt-off (or that app's off) from the pending list, so it cannot be replayed over the on. - A denial whose re-read does not show it is reported as unsaved. - The unreadable notice clears once receipt is turned on. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent 8638e5b commit 74c01a5

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎Sources/App/AgentSessionController.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,9 @@ import Security
696696
}
697697

698698
private func turnOnReceive(byUser: Bool, reason: String) -> Bool {
699-
guard !locked, startPairing() else { return false }
699+
// Recorded before taking the socket over, which reads the file again.
700700
let wasUnreadable = trust.readFailed
701+
guard !locked, startPairing() else { return false }
701702
adoptIfChanged()
702703
// Turning receipt on over an unreadable file starts a new, empty list. If
703704
// this click is what found the file unreadable, show that first and let
@@ -708,6 +709,7 @@ import Security
708709
return false
709710
}
710711
guard setAutoReceive(true, byUser: byUser) else { return false }
712+
if byUser { status = "" }
711713
Log.agent.info("[AGENT-CONN] receipt choice=\(self.receiptChoice.rawValue, privacy: .public) set=on reason=\(reason, privacy: .public)")
712714
dropIfGone()
713715
for binding in bindings.values { _ = activateReceive(binding) }

‎Sources/Core/AgentTrustStore.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ final class AgentTrustStore {
148148
needsAdopt = false
149149
// A first launch that never had a file keeps its in-memory choice.
150150
if knew { close(failed: false) }
151+
// Unsaved denials go into a new file; if that fails too, try again later.
152+
if !pending.isEmpty { _ = commit(file, byUser: false) }
151153
return
152154
}
153155
readFailed = false
@@ -209,6 +211,8 @@ final class AgentTrustStore {
209211
// The tab never overrides the user's off.
210212
if !byUser, value, receiptChoice == .off { return false }
211213
let choice: AgentReceiptChoice = byUser ? (value ? .auto : .off) : receiptChoice
214+
// The user's later on supersedes an off that was never saved.
215+
if value, byUser { pending.removeAll { $0 == .receiptOff } }
212216
guard file.autoReceive != value || file.receiptChoice != choice || !pending.isEmpty || readFailed else { return true }
213217
var next = file
214218
next.autoReceive = value
@@ -257,6 +261,7 @@ final class AgentTrustStore {
257261
guard !enabled, closedByDisk else { return false }
258262
return commitDenial(file, .appReceiptOff(id))
259263
}
264+
if enabled { pending.removeAll { $0 == .appReceiptOff(id) } }
260265
guard file.clients[index].receiptEnabled != enabled || !pending.isEmpty else { return true }
261266
var next = file
262267
next.clients[index].receivesDrafts = enabled
@@ -340,7 +345,8 @@ final class AgentTrustStore {
340345
pending = []
341346
} else {
342347
needsAdopt = true
343-
if let denial, !pending.contains(denial) { pending.append(denial) }
348+
// A denial counts as saved only once the file is seen to hold it.
349+
if denial != nil { return false }
344350
}
345351
return true
346352
}

‎Tests/AgentTrustStoreTests.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ import Foundation
126126
precondition(!retrying.changedOnDisk)
127127
let retryReopened = AgentTrustStore(url: retryURL)
128128
precondition(!retryReopened.autoReceive && retryReopened.lookup(keyID: retried.keyID, host: hostA) == .revoked)
129+
// A later on by the user is not undone by an off that was never saved.
130+
precondition(retrying.setAutoReceive(true))
131+
spaceLeft = false
132+
precondition(!retrying.setAutoReceive(false))
133+
spaceLeft = true
134+
precondition(retrying.setAutoReceive(true) && retrying.autoReceive && !retrying.changedOnDisk)
135+
retrying.adoptDisk()
136+
precondition(retrying.autoReceive && AgentTrustStore(url: retryURL).autoReceive)
137+
// An unsaved denial while the file is gone goes into a new file.
138+
spaceLeft = false
139+
precondition(!retrying.setAutoReceive(false))
140+
precondition(unlink(retryURL.path) == 0)
141+
spaceLeft = true
142+
retrying.adoptDisk()
143+
precondition(!retrying.changedOnDisk && AgentTrustStore(url: retryURL).receiptChoice == .off)
129144

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

0 commit comments

Comments
 (0)