Skip to content

Commit e1e5898

Browse files
committed
Identify the agent app off the main thread and once per process (#13)
Manual testing with Claude Code (a 366 MB signed binary) showed no approval request: validating the host signature takes about a second on the main thread, past the 750 ms pairing deadline, so hello answered unavailable. Compute the host identity on the socket thread before the main-thread hop and cache it per running parent process (pid, start time, executable path), so later checks, including the per-request delivery verifier, are cheap. Drop the redundant second validity check; process identity is re-checked instead. Log main-deadline timeouts. Agent: Claude Code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015LJG7MWqYoCZivtQ96kBhP
1 parent 6d48dfd commit e1e5898

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎Sources/App/AgentPeerSigning.swift‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ enum AgentPeerSigning {
6060
guard let child = process(pid), child.pbi_ppid > 1 else { return nil }
6161
let parentPID = pid_t(child.pbi_ppid)
6262
guard let parent = process(parentPID), let runningPath = executablePath(parentPID) else { return nil }
63+
// Validating a large agent binary takes about a second, and every request
64+
// re-checks its host. The answer is fixed for one running parent process,
65+
// so it is computed once per process instance.
66+
let cacheKey = "\(parentPID):\(parent.pbi_start_tvsec):\(parent.pbi_start_tvusec):\(runningPath)"
67+
if let cached = hostCache.value(cacheKey) { return cached }
68+
let host = validateHost(pid: pid, child: child, parentPID: parentPID, parent: parent, runningPath: runningPath)
69+
// Only cache when the same processes are still in place afterwards.
70+
if let currentChild = process(pid), sameProcess(child, currentChild),
71+
let currentParent = process(parentPID), sameProcess(parent, currentParent) {
72+
hostCache.store(cacheKey, host)
73+
}
74+
return host
75+
}
76+
77+
private final class HostCache: @unchecked Sendable {
78+
private let lock = NSLock()
79+
private var entries: [(key: String, host: String?)] = []
80+
func value(_ key: String) -> String?? {
81+
lock.lock(); defer { lock.unlock() }
82+
return entries.first(where: { $0.key == key }).map { $0.host }
83+
}
84+
func store(_ key: String, _ host: String?) {
85+
lock.lock(); defer { lock.unlock() }
86+
entries.removeAll { $0.key == key }
87+
entries.append((key, host))
88+
if entries.count > 32 { entries.removeFirst(entries.count - 32) }
89+
}
90+
}
91+
private static let hostCache = HostCache()
92+
93+
private static func validateHost(pid: pid_t, child: proc_bsdinfo, parentPID: pid_t, parent: proc_bsdinfo,
94+
runningPath: String) -> String? {
6395

6496
var guest: SecCode?
6597
var code: SecStaticCode?
@@ -80,8 +112,7 @@ enum AgentPeerSigning {
80112
let currentChild = process(pid), currentChild.pbi_ppid == child.pbi_ppid,
81113
sameProcess(child, currentChild),
82114
let currentParent = process(parentPID), sameProcess(parent, currentParent),
83-
executablePath(parentPID) == runningPath,
84-
SecCodeCheckValidity(guest, [], nil) == errSecSuccess else { return nil }
115+
executablePath(parentPID) == runningPath else { return nil }
85116
// A bundle must agree with its own signature about which app it is.
86117
if let plist = details[kSecCodeInfoPList as String] as? [String: Any],
87118
let bundleID = plist["CFBundleIdentifier"] as? String, bundleID != identifier { return nil }

‎Sources/App/AgentSessionController.swift‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ import Security
160160
guard let request = Self.decode(bytes) else { return Self.reply("invalid") }
161161
guard Self.isSchema(request["schema_version"], AgentPairing.schema) else { return Self.reply("upgrade_required") }
162162
let keys = Set(request.keys)
163+
// Identify the launching app here, off the main thread: validating a large
164+
// agent binary can take longer than the main-thread deadline below. The
165+
// main-thread checks then reuse the cached result.
166+
_ = AgentPeerSigning.registeredHost(pid: peer.pid)
163167
let response = Reply()
164168
let signal = DispatchSemaphore(value: 0)
165169
let deadline = DispatchTime.now() + .milliseconds(750)
@@ -183,7 +187,11 @@ import Security
183187
default: response.set("invalid")
184188
}
185189
}
186-
guard signal.wait(timeout: deadline) == .success else { return Self.reply("unavailable") }
190+
guard signal.wait(timeout: deadline) == .success else {
191+
let op = request["op"] as? String ?? "?"
192+
Log.agent.error("[AGENT-CONN] pairing op=\(op, privacy: .public) pid=\(peer.pid, privacy: .public) result=unavailable reason=main_deadline")
193+
return Self.reply("unavailable")
194+
}
187195
return response.data()
188196
}
189197
guard socket.start() else {

0 commit comments

Comments
 (0)