Skip to content

Commit b4475eb

Browse files
committed
Reset rate allowance on manual re-enable and publish keys with an exclusive rename (#13)
Turning receipt back on (master switch or per-client resume) clears the client's rate state again, as the docs say and the earlier code did. The helper key file is published with renamex_np(RENAME_EXCL) instead of link()+unlink(), so a helper killed mid-creation can no longer leave a key with st_nlink == 2 that the reader rejects forever. Addresses Codex review on PR #15 (discussion_r4698613638, discussion_r4698613629). Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7MWqYoCZivtQ96kBhP
1 parent 15da9e9 commit b4475eb

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎Helpers/AgentCatalogMCP.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,12 @@ struct ClientKey {
628628
guard out >= 0 else { return nil }
629629
defer { unlink(temp.path) }
630630
let writer = FileHandle(fileDescriptor: out, closeOnDealloc: true)
631+
// Publish with an exclusive rename, not link()+unlink(): a helper killed
632+
// between those two calls would leave the key with st_nlink == 2, which
633+
// the read path above rejects forever. rename keeps one link at every
634+
// instant, and RENAME_EXCL never replaces a key that already exists.
631635
guard (try? writer.write(contentsOf: bytes)) != nil, (try? writer.synchronize()) != nil,
632-
link(temp.path, url.path) == 0 else { return nil }
636+
renamex_np(temp.path, url.path, UInt32(RENAME_EXCL)) == 0 else { return nil }
633637
return make(key, persistent: true)
634638
}
635639

‎Sources/App/AgentSessionController.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ import Security
633633
func enableReceive(current: @escaping @MainActor (String) -> AgentInstructionSnapshot?,
634634
workspace: ExplanationDraftWorkspace) -> Bool {
635635
guard !locked, startPairing(), setAutoReceive(true) else { return false }
636+
// Turning receipt back on by hand starts every client's allowance over.
637+
rates.removeAll()
636638
configure(current: current, workspace: workspace)
637639
dropIfGone()
638640
for binding in bindings.values { _ = activateReceive(binding) }
@@ -711,6 +713,8 @@ import Security
711713
trusted = trust.clients
712714
}
713715
defer { refreshConnections() }
716+
// Resuming this client by hand starts its allowance over.
717+
rates[client] = nil
714718
guard let binding = bindings[client] else { return row.persistent }
715719
binding.receiptEnabled = true
716720
return activateReceive(binding)

0 commit comments

Comments
 (0)