Skip to content

Commit 11162a6

Browse files
committed
fix(macos): refresh config from gateway config.changed events
Addresses the review finding that no macOS consumer turned the gateway's hash-only config.changed broadcast into refreshed shared state, so a Control UI accent change never reached an open native chat window while the app ran. ChannelsStore now subscribes to gateway pushes and re-fetches config.get on config.changed, reconnect snapshots, and sequence gaps. The refresh applies non-force so an in-progress local settings draft wins (the gateway rejects stale-hash writes anyway). The in-flight reload queue gains a closed pending level (none/refresh/force) so a refresh arriving during a load is coalesced instead of dropped, and a requeued refresh cannot clobber a dirty draft the way the old boolean force-pending did.
1 parent b8da089 commit 11162a6

5 files changed

Lines changed: 72 additions & 14 deletions

File tree

‎apps/macos/Sources/OpenClaw/ChannelsStore+Config.swift‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ extension ChannelsStore {
6969
}
7070
}
7171

72-
func loadConfig(force: Bool = true) async {
72+
func loadConfig(force: Bool = true, refresh: Bool = false) async {
7373
let sourceKey = self.currentConfigCacheSourceKey()
7474
self.resetConfigCacheIfSourceChanged(sourceKey)
75-
if !force, self.configLoaded {
75+
if !force, !refresh, self.configLoaded {
7676
return
7777
}
78-
guard !self.queueConfigReloadIfLoading(sourceKey: sourceKey, force: force) else { return }
78+
guard !self.queueConfigReloadIfLoading(sourceKey: sourceKey, force: force, refresh: refresh)
79+
else { return }
7980
self.configLoading = true
8081
self.configLoadingSourceKey = sourceKey
8182
defer {
@@ -98,9 +99,9 @@ extension ChannelsStore {
9899
self.configStatus = error.localizedDescription
99100
}
100101

101-
guard self.configForceReloadPending else { break }
102-
self.configForceReloadPending = false
103-
requestForce = true
102+
guard self.configReloadPending != .none else { break }
103+
requestForce = self.configReloadPending == .force
104+
self.configReloadPending = .none
104105
requestSourceKey = self.currentConfigCacheSourceKey()
105106
self.resetConfigCacheIfSourceChanged(requestSourceKey)
106107
}
@@ -253,10 +254,12 @@ extension ChannelsStore {
253254
self.configSourceKey = sourceKey
254255
}
255256

256-
func queueConfigReloadIfLoading(sourceKey: String, force: Bool) -> Bool {
257+
func queueConfigReloadIfLoading(sourceKey: String, force: Bool, refresh: Bool = false) -> Bool {
257258
guard self.configLoading else { return false }
258259
if force || self.configLoadingSourceKey != sourceKey {
259-
self.configForceReloadPending = true
260+
self.configReloadPending = .force
261+
} else if refresh, self.configReloadPending == .none {
262+
self.configReloadPending = .refresh
260263
}
261264
return true
262265
}

‎apps/macos/Sources/OpenClaw/ChannelsStore+Lifecycle.swift‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import OpenClawKit
23
import OpenClawProtocol
34

45
func whatsappLoginWaitRequestTimeoutMs(
@@ -25,6 +26,9 @@ extension ChannelsStore {
2526
self.startCount += 1
2627
guard self.startCount == 1 else { return }
2728
guard self.pollTask == nil else { return }
29+
GatewayPushSubscription.restartTask(task: &self.gatewayPushTask) { [weak self] push in
30+
self?.handleGatewayPush(push)
31+
}
2832
self.pollTask = Task.detached { [weak self] in
2933
guard let self else { return }
3034
await self.refresh(probe: false)
@@ -44,6 +48,23 @@ extension ChannelsStore {
4448
guard self.startCount == 0 else { return }
4549
self.pollTask?.cancel()
4650
self.pollTask = nil
51+
self.gatewayPushTask?.cancel()
52+
self.gatewayPushTask = nil
53+
}
54+
55+
static func gatewayPushRequestsConfigRefresh(_ push: GatewayPush) -> Bool {
56+
switch push {
57+
case let .event(event):
58+
event.event == "config.changed"
59+
case .snapshot, .seqGap:
60+
true
61+
}
62+
}
63+
64+
private func handleGatewayPush(_ push: GatewayPush) {
65+
guard Self.gatewayPushRequestsConfigRefresh(push) else { return }
66+
// Change events contain only a hash; refetch without overwriting a dirty local draft.
67+
Task { await self.loadConfig(force: false, refresh: true) }
4768
}
4869

4970
func refresh(probe: Bool) async {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,18 @@ final class ChannelsStore {
300300
var configSchemaReloadPending = false
301301
var configLoading = false
302302
var configLoadingSourceKey: String?
303-
var configForceReloadPending = false
303+
/// Coalesced re-load request while a config fetch is in flight: `refresh`
304+
/// refetches without overwriting a dirty local draft; `force` overwrites it.
305+
enum ConfigReloadRequest { case none, refresh, force }
306+
var configReloadPending: ConfigReloadRequest = .none
304307
var configDraft: [String: Any] = [:]
305308
var configDirty = false
306309

307310
let interval: TimeInterval = 45
308311
let isPreview: Bool
309312
var startCount = 0
310313
var pollTask: Task<Void, Never>?
314+
var gatewayPushTask: Task<Void, Never>?
311315
var configRoot: [String: Any] = [:]
312316
var configLoaded = false
313317
var configSourceKey: String?

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct ChannelsSettingsSmokeTests {
100100
now: Date(timeInterval: 1.5, since: startedAt)) == nil)
101101
}
102102

103-
@Test func `cached config loads return without clearing dirty draft`() async {
103+
@Test func `cached config loads return without clearing dirty draft`() {
104104
let store = makeChannelsStore(channels: [:])
105105
store.configSchema = ConfigSchemaNode(raw: ["type": "object"])
106106
store.configSchemaSourceKey = "source-a"
@@ -194,14 +194,21 @@ struct ChannelsSettingsSmokeTests {
194194
store.configLoadingSourceKey = "source-a"
195195

196196
#expect(store.queueConfigReloadIfLoading(sourceKey: "source-a", force: false) == true)
197-
#expect(store.configForceReloadPending == false)
197+
#expect(store.configReloadPending == .none)
198+
199+
#expect(store.queueConfigReloadIfLoading(sourceKey: "source-a", force: false, refresh: true) == true)
200+
#expect(store.configReloadPending == .refresh)
198201

199202
#expect(store.queueConfigReloadIfLoading(sourceKey: "source-a", force: true) == true)
200-
#expect(store.configForceReloadPending == true)
203+
#expect(store.configReloadPending == .force)
204+
205+
// Force is sticky: a queued refresh must not downgrade a pending force reload.
206+
#expect(store.queueConfigReloadIfLoading(sourceKey: "source-a", force: false, refresh: true) == true)
207+
#expect(store.configReloadPending == .force)
201208

202-
store.configForceReloadPending = false
209+
store.configReloadPending = .none
203210
#expect(store.queueConfigReloadIfLoading(sourceKey: "source-b", force: false) == true)
204-
#expect(store.configForceReloadPending == true)
211+
#expect(store.configReloadPending == .force)
205212
}
206213

207214
@Test func `schema reload queues behind background load after source changes`() {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import OpenClawKit
12
import OpenClawProtocol
23
import Testing
34
@testable import OpenClaw
@@ -43,4 +44,26 @@ struct ChannelsStoreUIConfigTests {
4344

4445
#expect(AppStateStore.shared.seamColorHex == "#112233")
4546
}
47+
48+
@Test func `gateway pushes refresh config only when its snapshot may be stale`() {
49+
let hello = HelloOk(
50+
type: "hello-ok",
51+
_protocol: 3,
52+
server: [:],
53+
features: [:],
54+
snapshot: Snapshot(
55+
presence: [],
56+
health: [:],
57+
stateversion: StateVersion(presence: 0, health: 0),
58+
uptimems: 0),
59+
auth: [:],
60+
policy: [:])
61+
62+
#expect(ChannelsStore.gatewayPushRequestsConfigRefresh(
63+
.event(EventFrame(type: "event", event: "config.changed"))))
64+
#expect(ChannelsStore.gatewayPushRequestsConfigRefresh(.snapshot(hello)))
65+
#expect(ChannelsStore.gatewayPushRequestsConfigRefresh(.seqGap(expected: 1, received: 3)))
66+
#expect(!ChannelsStore.gatewayPushRequestsConfigRefresh(
67+
.event(EventFrame(type: "event", event: "presence"))))
68+
}
4669
}

0 commit comments

Comments
 (0)