Repository navigation
Description
Summary
App.xaml.cs is 4,617 lines and growing (was 4,148 when the architecture docs were written — 11% growth in the docs period). It is the sole owner of cached gateway state used by every page: _lastSessions, _lastChannels, _lastNodes, _lastUsage, _lastUsageStatus, _lastUsageCost, _lastGatewaySelf, _lastNodePairList, _lastDevicePairList, _lastModelsList, _lastPresence, _agentEventsCache, _lastUpdateInfo, _sessionActivities, _sessionPreviews, etc.
When the connection manager swaps the live OpenClawGatewayClient (reconnect, autopair, gateway switch), App.OnOperatorClientChanged re-subscribes ~28 gateway events at App.xaml.cs:2181-2268. Every page that needs gateway state reaches back through _hub.LastSessions etc., or via direct read of an App field.
Why it matters
Three concrete problems flow from this:
- Fragility on client swap. A subscription that doesn't get re-attached after a
OperatorClientChangedbecomes a silent stale-data bug. No test would catch a missing line inOnOperatorClientChanged. - Test surface is thin. Pages read state from
App, which is a singleton that can't be instantiated in a test without WinUI runtime initialization. That's whyOpenClaw.Tray.Testsresorts to<Compile Include="..\..\src\..."/>link-references rather than reusing the live App. - Concurrency. The 17 cached collections are guarded by ad-hoc patterns —
_sessionPreviewsLockis the only explicit lock. Other fields rely on dispatcher-thread invariants that are documented in code comments rather than enforced.
The single missing layer
A view-model / observable data-store layer between GatewayConnectionManager and the UI. Concretely:
GatewayConnectionManager ──events──► GatewayDataStore ──INotifyPropertyChanged──► Pages
Where GatewayDataStore:
- Subscribes to operator client events once in its constructor, re-subscribes internally when notified by
OperatorClientChanged. The 28-line re-subscription block moves out ofApp.xaml.cs. - Owns the 17 cached collections as observable properties /
ObservableCollection<T>. - Implements
INotifyPropertyChangedso pages bind toSessions,Channels,Usage, etc., instead of pollingApp.LastX. - Is injected into every page (constructor parameter) so tests can substitute a fake.
Proposed fix (phased)
This is a multi-PR refactor, not a single change.
- PR 1 — introduce
GatewayDataStorewith all 17 fields exposed as observable properties. HaveAppcontinue to fill it (one-way for now). Pages start reading from the store. - PR 2 — invert ownership.
GatewayDataStoresubscribes to gateway events directly via a constructor parameter.Appno longer subscribes. - PR 3 — migrate pages to constructor-injection of the store, removing the
_hub.Last*reads. - PR 4 — delete dead code from
App. Target: dropApp.xaml.csbelow 1,500 lines.
Severity
High — architectural keystone problem. Most of the other findings in this deep-review trace back here.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
shanselman commentedon May 18, 2026
Closing during the post-refactor deep-review cleanup. The original App.xaml.cs claim is obsolete after refactors (current file is much smaller than reported); remaining architecture debt is tracked in #372. This keeps the signal while reducing stale one-off issue noise.