@@ -112,7 +112,9 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
112112 private var browserProfileImportOfferRetryPending = false
113113 private var hasLiveContent = false
114114 private var isShowingFailurePage = false
115+ private var navigationGeneration : UInt64 = 0
115116 private var pendingNativeCommands : [ DashboardNativeCommand ] = [ ]
117+ private var pendingNativeNavigation : DashboardNativeNavigation ?
116118 var onClosed : ( ( ) -> Void ) ?
117119
118120 init (
@@ -404,14 +406,6 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
404406 return window . isVisible || window . isMiniaturized
405407 }
406408
407- /// Commands are deliverable when a document is live or a load is in flight
408- /// (the queue flushes at `didFinish`). A failure page, or a terminally
409- /// cancelled load with no successor, needs a reload before dispatch —
410- /// otherwise queued ⌘N/⌘K would wait on a `didFinish` that never comes.
411- var canDeliverNativeCommands : Bool {
412- ! self . isShowingFailurePage && ( self . hasLiveContent || self . webView . isLoading )
413- }
414-
415409 func show ( ) {
416410 if let window {
417411 let frame = window . frame
@@ -442,9 +436,11 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
442436 func showFailure ( title : String , message : String , detail : String ? = nil ) {
443437 self . hasLiveContent = false
444438 self . isShowingFailurePage = true
439+ self . advanceNavigationGeneration ( )
445440 // Queued commands are moment-bound user intent; replaying them after a
446441 // later recovery reload would toggle or navigate unexpectedly.
447442 self . pendingNativeCommands = [ ]
443+ self . pendingNativeNavigation = nil
448444 self . currentURL = URL ( string : " about:blank " ) !
449445 self . auth = DashboardWindowAuth ( gatewayUrl : nil , token : nil , password : nil )
450446 self . setUpdateBridgeEnabled ( false )
@@ -1017,9 +1013,11 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
10171013 }
10181014 self . hasLiveContent = false
10191015 self . isShowingFailurePage = true
1016+ self . advanceNavigationGeneration ( )
10201017 // Same moment-bound rule as showFailure: a terminal load failure
10211018 // invalidates commands queued for the document that never arrived.
10221019 self . pendingNativeCommands = [ ]
1020+ self . pendingNativeNavigation = nil
10231021 dashboardWindowLogger . error (
10241022 """
10251023 dashboard load failed url= \( dashboardLogString ( for : self . currentURL ) , privacy : . public ) \
@@ -1035,6 +1033,20 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
10351033}
10361034
10371035extension DashboardWindowController {
1036+ /// Commands are deliverable when a document is live or a load is in flight
1037+ /// (the queue flushes at `didFinish`). A failure page, or a terminally
1038+ /// cancelled load with no successor, needs a reload before dispatch —
1039+ /// otherwise queued ⌘N/⌘K would wait on a `didFinish` that never comes.
1040+ var canDeliverNativeCommands : Bool {
1041+ ! self . isShowingFailurePage && ( self . hasLiveContent || self . webView . isLoading )
1042+ }
1043+
1044+ /// The canonical Dashboard mount URL supplied by the manager. WebKit route
1045+ /// loads and SPA history do not mutate it, so native fallbacks stay rooted.
1046+ var dashboardBaseURL : URL {
1047+ self . currentURL
1048+ }
1049+
10381050 func windowWillClose ( _ : Notification ) {
10391051 self . webView . stopLoading ( )
10401052 self . closeLinkBrowser ( focusDashboard : false )
@@ -1119,16 +1131,25 @@ extension DashboardWindowController {
11191131 }
11201132
11211133 func dispatchNativeCommand ( _ command : DashboardNativeCommand ) {
1134+ if command . supersedesPendingNavigation {
1135+ self . advanceNavigationGeneration ( )
1136+ }
11221137 guard self . hasLiveContent else {
11231138 // Ordered queue, duplicates included: two ⌘K presses while loading
11241139 // must toggle twice, and ⌘N followed by ⌘K must deliver both.
1140+ if command . supersedesPendingNavigation {
1141+ self . pendingNativeNavigation = nil
1142+ }
11251143 self . pendingNativeCommands . append ( command )
11261144 return
11271145 }
11281146 self . evaluateNativeCommand ( command )
11291147 }
11301148
11311149 private func evaluateNativeCommand ( _ command : DashboardNativeCommand ) {
1150+ if command . supersedesPendingNavigation {
1151+ self . advanceNavigationGeneration ( )
1152+ }
11321153 guard let fallback = command . legacyFallbackEventName else {
11331154 self . webView . evaluateJavaScript (
11341155 " window.dispatchEvent(new CustomEvent( \( Self . jsStringLiteral ( command . rawValue ) ) )) " )
@@ -1157,6 +1178,53 @@ extension DashboardWindowController {
11571178 self . evaluateNativeCommand ( command )
11581179 }
11591180 }
1181+
1182+ func dispatchNativeNavigation ( _ navigation : DashboardNativeNavigation ) {
1183+ self . advanceNavigationGeneration ( )
1184+ guard self . hasLiveContent else {
1185+ // Navigation is state selection, so only the newest destination matters while loading.
1186+ self . pendingNativeNavigation = navigation
1187+ return
1188+ }
1189+ self . evaluateNativeNavigation ( navigation )
1190+ }
1191+
1192+ private func evaluateNativeNavigation ( _ navigation : DashboardNativeNavigation ) {
1193+ let generation = self . navigationGeneration
1194+ let sourceURL = self . currentURL
1195+ let script =
1196+ """
1197+ (() => !window.dispatchEvent(new CustomEvent('openclaw:native-navigate', {
1198+ cancelable: true,
1199+ detail: {path: \( Self . jsStringLiteral ( navigation . path ) ) }
1200+ })))()
1201+ """
1202+ Task { @MainActor [ weak self ] in
1203+ guard let self else { return }
1204+ let result = try? await self . webView . evaluateJavaScript ( script )
1205+ let handled = result as? Bool
1206+ // Async completions may return after another route or New Session intent.
1207+ // Only the newest navigation intent may load its fallback URL.
1208+ guard handled != true ,
1209+ self . navigationFallbackIsCurrent ( generation : generation , sourceURL : sourceURL )
1210+ else { return }
1211+ self . load ( navigation . fallbackURL )
1212+ }
1213+ }
1214+
1215+ private func advanceNavigationGeneration ( ) {
1216+ self . navigationGeneration &+= 1
1217+ }
1218+
1219+ private func navigationFallbackIsCurrent ( generation : UInt64 , sourceURL : URL ) -> Bool {
1220+ self . navigationGeneration == generation && self . currentURL == sourceURL
1221+ }
1222+
1223+ private func flushPendingNativeNavigation ( ) {
1224+ guard let navigation = self . pendingNativeNavigation else { return }
1225+ self . pendingNativeNavigation = nil
1226+ self . evaluateNativeNavigation ( navigation )
1227+ }
11601228}
11611229
11621230/// WKNavigationDelegate policy lives in an extension to keep the class
@@ -1285,6 +1353,7 @@ extension DashboardWindowController {
12851353 self . publishNativeHistoryState ( )
12861354 // History state must reach the shell before a queued command can navigate it.
12871355 self . flushPendingNativeCommands ( )
1356+ self . flushPendingNativeNavigation ( )
12881357 }
12891358 }
12901359
@@ -1488,6 +1557,18 @@ extension DashboardWindowController {
14881557 self . pendingNativeCommands
14891558 }
14901559
1560+ var _testPendingNativeNavigation : DashboardNativeNavigation ? {
1561+ self . pendingNativeNavigation
1562+ }
1563+
1564+ var _testNavigationGeneration : UInt64 {
1565+ self . navigationGeneration
1566+ }
1567+
1568+ func _testNavigationFallbackIsCurrent ( generation : UInt64 , sourceURL : URL ) -> Bool {
1569+ self . navigationFallbackIsCurrent ( generation : generation , sourceURL : sourceURL )
1570+ }
1571+
14911572 var _testNavigationWebViewIdentity : ObjectIdentifier {
14921573 ObjectIdentifier ( self . activeNavigationWebView )
14931574 }
0 commit comments