Skip to content

Commit 10029ad

Browse files
committed
fix(macos): converge approval cards after losing a resolution race
The status-menu queue and the modal prompter intentionally share the gateway approval event stream: the gateway resolves each approval exactly once, the resolved broadcast removes the card, and the modal stays the active presentation owner while the menu is the passive, ambient one. What was missing: when the menu's resolve loses the race (modal or another client answered first), the gateway rejection left a zombie card if the resolved event was dropped. Resolve failures now re-list from the authoritative queue. Regression test simulates the race at the socket boundary and fails pre-fix.
1 parent 426d0dd commit 10029ad

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎apps/macos/Sources/OpenClaw/ExecApprovalQueueStore.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ final class ExecApprovalQueueStore {
160160
self.removeRequest(id: request.id)
161161
} catch {
162162
self.logger.error("exec approval resolution failed \(error.localizedDescription, privacy: .public)")
163+
// A losing race (the modal prompter or another client resolved first)
164+
// surfaces here as a gateway rejection. Re-list instead of parsing
165+
// error text so the card converges to the authoritative queue.
166+
await self.refresh()
163167
}
164168
}
165169

‎apps/macos/Tests/OpenClawIPCTests/ExecApprovalQueueStoreTests.swift‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ private struct ApprovalGatewayRequest: Sendable {
4444
}
4545

4646
private actor ApprovalGatewayRequestLog {
47-
private let initialRequests: [ApprovalFixtureRequest]
47+
private var listedRequests: [ApprovalFixtureRequest]
4848
private var requests: [ApprovalGatewayRequest] = []
4949
private var nextSequence = 0
50+
private var resolveRejection: String?
5051

5152
init(initialRequests: [ApprovalFixtureRequest]) {
52-
self.initialRequests = initialRequests
53+
self.listedRequests = initialRequests
5354
}
5455

5556
func append(_ request: ApprovalGatewayRequest) {
@@ -61,7 +62,18 @@ private actor ApprovalGatewayRequestLog {
6162
}
6263

6364
func listResponse() -> String {
64-
String(decoding: try! JSONEncoder().encode(self.initialRequests), as: UTF8.self)
65+
String(decoding: try! JSONEncoder().encode(self.listedRequests), as: UTF8.self)
66+
}
67+
68+
/// Simulates another client (the modal prompter) winning the resolution
69+
/// race server-side: resolves reject and the authoritative list is empty.
70+
func markResolvedElsewhere(reason: String = "APPROVAL_ALREADY_RESOLVED") {
71+
self.resolveRejection = reason
72+
self.listedRequests = []
73+
}
74+
75+
func resolveRejectionReason() -> String? {
76+
self.resolveRejection
6577
}
6678

6779
func nextEventSequence() -> Int {
@@ -82,6 +94,14 @@ private final class ApprovalGatewayFixture: @unchecked Sendable {
8294
GatewayTestWebSocketTask(sendHook: { socket, message, sendIndex in
8395
guard sendIndex > 0, let request = Self.decodeRequest(message) else { return }
8496
await requestLog.append(request)
97+
if request.method.hasSuffix("approval.resolve"),
98+
let reason = await requestLog.resolveRejectionReason()
99+
{
100+
let response =
101+
#"{"type":"res","id":"\#(request.id)","ok":false,"error":{"message":"\#(reason)"}}"#
102+
socket.emitReceiveSuccess(.data(Data(response.utf8)))
103+
return
104+
}
85105
let payload = if request.method == "exec.approval.list" {
86106
await requestLog.listResponse()
87107
} else {
@@ -155,6 +175,25 @@ struct ExecApprovalQueueStoreTests {
155175
#expect(await fixture.requestLog.requests(method: "exec.approval.list").count == 1)
156176
}
157177

178+
@Test func `losing a resolution race re-syncs from the authoritative queue`() async throws {
179+
let fixture = ApprovalGatewayFixture(initialRequests: [
180+
ApprovalFixtureRequest(id: "contested"),
181+
])
182+
let store = ExecApprovalQueueStore(gateway: fixture.gateway)
183+
defer { store.stop() }
184+
185+
await store.refresh()
186+
let contested = try #require(store.requests.first)
187+
188+
// The modal prompter (a second presentation surface on the same event
189+
// stream) resolves first; the gateway rejects this store's attempt.
190+
await fixture.requestLog.markResolvedElsewhere()
191+
await store.resolve(request: contested, decision: .deny)
192+
193+
#expect(store.requests.isEmpty)
194+
#expect(await fixture.requestLog.requests(method: "exec.approval.list").count == 2)
195+
}
196+
158197
@Test func `requested and resolved events update the shared queue`() async throws {
159198
let fixture = ApprovalGatewayFixture()
160199
let store = ExecApprovalQueueStore(gateway: fixture.gateway)

0 commit comments

Comments
 (0)