Skip to content

Commit 49e33b1

Browse files
committed
Replay unsaved denials instead of merging, and move approvals to their own file (#31)
The adoption merge (tombstone union, intersection with an unsaved denial, old-writer receipt guard) kept producing edge cases. It is replaced by two simpler rules: - Approvals live in agent-approvals.json. The #13 builds' agent-clients.json is imported once when the new file does not exist and renamed to .migrated. No released version ever wrote either file, so earlier builds running beside this one no longer share a file with it. - A user's denial (receipt off, one app's receipt off, revoke) that cannot be saved is kept as a pending operation and replayed on top of whatever the file holds when it is next read, then written. Only the user's own actions are replayed, so revocations at the 64 cap and apps approved elsewhere are kept. Also: - needsAdopt flag instead of a nil stamp, so a pending write-back still notices the file vanishing; a read error after open keeps no stamp. - Over an unreadable file only the user turning receipt on writes (a new, empty list); denials there stay pending. The tab notice says so. - disconnect/forget capture what was clicked before re-reading the file and record the denial even if the row is not loaded right now. - Opening the tab checks the file again. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7KnMPqFcfTZ8A1C2qh
1 parent 6603dfe commit 49e33b1

5 files changed

Lines changed: 218 additions & 153 deletions

File tree

‎Sources/App/AgentSessionController.swift‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ import Security
199199
selectionWaiting: Duration = .seconds(90),
200200
noSessionDelay: Duration = .seconds(5),
201201
trust: AgentTrustStore = AgentTrustStore(
202-
url: TTSRuntime.applicationSupport().appendingPathComponent("agent-clients.json"))) {
202+
url: TTSRuntime.applicationSupport().appendingPathComponent("agent-approvals.json"),
203+
legacyURL: TTSRuntime.applicationSupport().appendingPathComponent("agent-clients.json"))) {
203204
self.directory = directory
204205
self.bundledHelper = bundledHelper
205206
self.selectionWaiting = selectionWaiting
@@ -748,6 +749,7 @@ import Security
748749
private func connectForPresence() {
749750
let before = readySequence
750751
if pairingSocket == nil { _ = startPairing() }
752+
adoptIfChanged()
751753
if !autoReceive {
752754
if locked {
753755
Log.agent.info("[AGENT-CONN] receipt choice=\(self.receiptChoice.rawValue, privacy: .public) skip reason=locked")
@@ -885,12 +887,15 @@ import Security
885887

886888
@discardableResult
887889
func disconnect(_ client: UUID) -> Bool {
890+
// What the user clicked on, before re-reading the file can remove it.
891+
guard let row = connections.first(where: { $0.id == client }) else { return false }
892+
let remembered = row.persistent || trust.clients.contains { $0.id == client }
888893
guard ownsPolicy() else { return false }
889-
guard connections.contains(where: { $0.id == client }) else { return false }
890-
if let record = trust.clients.first(where: { $0.id == client }) {
891-
clearApprovals(for: record)
894+
if let record = trust.clients.first(where: { $0.id == client }) { clearApprovals(for: record) }
895+
if remembered {
896+
// Kept even while the file cannot be read, and saved once it can.
892897
if !trust.setReceiptEnabled(false, for: client) {
893-
status = "이 에이전트의 받기 설정을 저장하지 못했습니다."
898+
status = "이 에이전트의 받기 설정을 저장하지 못했습니다. 파일을 다시 쓸 수 있게 되면 저장합니다."
894899
}
895900
trusted = trust.clients
896901
}
@@ -949,18 +954,22 @@ import Security
949954

950955
@discardableResult
951956
func forget(_ client: UUID) -> Bool {
952-
guard ownsPolicy() else { return false }
957+
// What the user clicked on, before re-reading the file can remove it.
953958
guard connections.contains(where: { $0.id == client }) else { return false }
954-
if let record = trust.clients.first(where: { $0.id == client }) {
959+
let clicked = trust.clients.first { $0.id == client }
960+
let clickedPeer = bindings[client]?.pair.peer
961+
guard ownsPolicy() else { return false }
962+
if let record = clicked {
955963
clearApprovals(for: record)
956-
let saved = trust.forget(client)
964+
// The key is revoked here at once, even while the file cannot be read.
965+
let saved = trust.forget(keyID: record.keyID)
957966
if !saved {
958-
status = "잊은 내용을 저장하지 못했습니다. 앱을 다시 열면 이 에이전트가 다시 보일 수 있습니다."
967+
status = "잊은 내용을 아직 저장하지 못했습니다. 이 앱에서는 바로 철회되며, 파일을 다시 쓸 수 있게 되면 저장합니다."
959968
}
960969
Log.agent.info("[AGENT-CONN] revoke host=\(record.host, privacy: .public) client=\(client.uuidString, privacy: .public) result=\(saved ? "ok" : "unsaved", privacy: .public)")
961970
trusted = trust.clients
962971
}
963-
if let peer = bindings[client]?.pair.peer {
972+
if let peer = clickedPeer ?? bindings[client]?.pair.peer {
964973
block(peer)
965974
clearApprovals(for: peer)
966975
}

0 commit comments

Comments
 (0)