@@ -122,6 +122,7 @@ import ai.openclaw.app.wear.WearProxyGatewayException
122122import ai.openclaw.app.wear.WearProxyModel
123123import ai.openclaw.app.wear.WearRealtimeAttemptOwner
124124import ai.openclaw.app.wear.WearRealtimeTalkController
125+ import ai.openclaw.app.wear.projectWearAgentPulse
125126import ai.openclaw.app.wear.wearConnectionFailure
126127import ai.openclaw.wear.shared.WearMessage
127128import ai.openclaw.wear.shared.WearRealtimeTalkCodec
@@ -141,6 +142,7 @@ import kotlinx.coroutines.Dispatchers
141142import kotlinx.coroutines.Job
142143import kotlinx.coroutines.NonCancellable
143144import kotlinx.coroutines.SupervisorJob
145+ import kotlinx.coroutines.async
144146import kotlinx.coroutines.channels.Channel
145147import kotlinx.coroutines.coroutineScope
146148import kotlinx.coroutines.delay
@@ -159,6 +161,7 @@ import kotlinx.coroutines.sync.Mutex
159161import kotlinx.coroutines.sync.withLock
160162import kotlinx.coroutines.withContext
161163import kotlinx.coroutines.withTimeout
164+ import kotlinx.coroutines.withTimeoutOrNull
162165import kotlinx.serialization.Serializable
163166import kotlinx.serialization.json.Json
164167import kotlinx.serialization.json.JsonArray
@@ -184,6 +187,39 @@ private const val CRON_JOBS_SNAPSHOT_MAX_ATTEMPTS = 3
184187private const val OperatorAdminScope = " operator.admin"
185188private const val OperatorPairingScope = " operator.pairing"
186189
190+ internal const val WEAR_AGENT_PULSE_PHONE_BUDGET_MILLIS = 8_000L
191+
192+ internal data class WearAgentPulseReads <Tasks , Swarm >(
193+ val tasks : Tasks ? ,
194+ val swarm : Swarm ? ,
195+ )
196+
197+ internal suspend fun <Tasks , Swarm > readWearAgentPulseConcurrently (
198+ readTasks : suspend () -> Tasks ,
199+ readSwarm : suspend () -> Swarm ,
200+ budgetMillis : Long = WEAR_AGENT_PULSE_PHONE_BUDGET_MILLIS ,
201+ ): WearAgentPulseReads <Tasks , Swarm > =
202+ coroutineScope {
203+ val tasks = async { readWearAgentPulseComponent(budgetMillis, readTasks) }
204+ val swarm = async { readWearAgentPulseComponent(budgetMillis, readSwarm) }
205+ WearAgentPulseReads (
206+ tasks = tasks.await(),
207+ swarm = swarm.await(),
208+ )
209+ }
210+
211+ private suspend fun <T > readWearAgentPulseComponent (
212+ budgetMillis : Long ,
213+ read : suspend () -> T ,
214+ ): T ? =
215+ try {
216+ withTimeoutOrNull(budgetMillis) { read() }
217+ } catch (err: CancellationException ) {
218+ throw err
219+ } catch (_: Throwable ) {
220+ null
221+ }
222+
187223private fun execApprovalOutcomeUnknownMessage (): String = nativeText(" Resolution outcome unknown. Actions stay disabled until the Gateway record is verified." ).source
188224
189225private fun execApprovalStillPendingMessage (): String = nativeText(" The Gateway still shows this approval as pending. Review it before trying again." ).source
@@ -1310,6 +1346,7 @@ class NodeRuntime private constructor(
13101346 val execApprovalsNotice: StateFlow <GatewayExecApprovalNotice ?> = _execApprovalsNotice .asStateFlow()
13111347 private val execApprovalsRefreshSeq = AtomicLong (0 )
13121348 private val execApprovalsStateLock = Any ()
1349+ private var execApprovalsSnapshotReady = false
13131350 private val resolvedExecApprovalIds = Collections .newSetFromMap(ConcurrentHashMap <String , Boolean >())
13141351 private val pendingExecApprovalWrites = mutableMapOf<String , PendingExecApprovalWrite >()
13151352
@@ -1518,9 +1555,7 @@ class NodeRuntime private constructor(
15181555 isGatewayConnected = operatorSession::isReady,
15191556 gatewayStatusText = { synchronized(gatewayStatusLock) { operatorStatusText } },
15201557 hasOperatorAdminScope = { OperatorAdminScope in _operatorScopes .value },
1521- activeAgentId = {
1522- resolveAgentIdFromMainSessionKey(mainSessionKey.value) ? : gatewayDefaultAgentId.value
1523- },
1558+ activeAgentId = ::currentWearAgentId,
15241559 activeSessionKey = { chatSessionKey.value },
15251560 selectedModelRef = { chatSelectedModelRef.value },
15261561 agents = {
@@ -1560,6 +1595,7 @@ class NodeRuntime private constructor(
15601595 },
15611596 connectGateway = { refreshGatewayConnection() },
15621597 disconnectGateway = { disconnect() },
1598+ loadAgentPulse = ::loadWearAgentPulse,
15631599 startRealtimeTalk = { nodeId, sessionKey, attemptId, language, attemptScopedAudio ->
15641600 if (startWearRealtimeTalk(nodeId, sessionKey, attemptId, language, attemptScopedAudio)) wearRealtimeTalkSnapshot.value else null
15651601 },
@@ -1569,6 +1605,67 @@ class NodeRuntime private constructor(
15691605 )
15701606 }
15711607
1608+ private fun currentWearAgentId (): String? = resolveAgentIdFromMainSessionKey(mainSessionKey.value) ? : gatewayDefaultAgentId.value
1609+
1610+ private suspend fun loadWearAgentPulse (requestedSessionKey : String? ): JsonObject {
1611+ val gatewayScope = captureGatewayDataScope()
1612+ val agentId = currentWearAgentId()
1613+ val connected = gatewayScope != null && operatorSession.isReady()
1614+ val reads =
1615+ if (connected && agentId != null ) {
1616+ readWearAgentPulseConcurrently(
1617+ readTasks = { listBackgroundTasks(agentId) },
1618+ readSwarm = {
1619+ requestedSessionKey?.let { sessionKey ->
1620+ chat.readSwarmSnapshotFor(sessionKey, agentId)
1621+ }
1622+ },
1623+ )
1624+ } else {
1625+ null
1626+ }
1627+ val tasks = reads?.tasks
1628+ val swarmSnapshot = reads?.swarm
1629+ // Capture every projection input before the final route check so a route
1630+ // change cannot mix a current task result with later-route aggregates.
1631+ val approvals = currentWearAgentPulseApprovals()
1632+ val routeStillCurrent =
1633+ gatewayScope?.let { capturedScope ->
1634+ connected &&
1635+ isGatewayDataScopeCurrent(capturedScope) &&
1636+ operatorSession.isReady() &&
1637+ currentWearAgentId() == agentId
1638+ } == true
1639+ val swarmAvailable =
1640+ routeStillCurrent &&
1641+ requestedSessionKey != null &&
1642+ swarmSnapshot?.isAvailableFor(requestedSessionKey) == true
1643+ return projectWearAgentPulse(
1644+ gatewayConnected = routeStillCurrent,
1645+ tasks = tasks.takeIf { routeStillCurrent },
1646+ swarmAvailable = swarmAvailable,
1647+ swarmGroups = if (swarmAvailable) swarmSnapshot.groups else emptyList(),
1648+ pendingApprovalCount = approvals.pendingCount,
1649+ approvalsAvailable = routeStillCurrent && approvals.available,
1650+ approvalsRefreshing = approvals.refreshing,
1651+ )
1652+ }
1653+
1654+ private fun currentWearAgentPulseApprovals (): WearAgentPulseApprovalSnapshot =
1655+ synchronized(execApprovalsStateLock) {
1656+ WearAgentPulseApprovalSnapshot (
1657+ pendingCount = _execApprovals .value.size,
1658+ available = execApprovalsSnapshotReady && _execApprovalsErrorText .value == null ,
1659+ refreshing = _execApprovalsRefreshing .value,
1660+ )
1661+ }
1662+
1663+ private data class WearAgentPulseApprovalSnapshot (
1664+ val pendingCount : Int ,
1665+ val available : Boolean ,
1666+ val refreshing : Boolean ,
1667+ )
1668+
15721669 internal suspend fun handleWearProxyRequest (
15731670 sourceNodeId : String ,
15741671 request : WearMessage .Request ,
@@ -1668,8 +1765,11 @@ class NodeRuntime private constructor(
16681765 }
16691766 invalidateExecApprovalRefreshes()
16701767 resolvedExecApprovalIds.clear()
1671- if (retirePendingCronRuns) {
1672- synchronized(execApprovalsStateLock) { pendingExecApprovalWrites.clear() }
1768+ synchronized(execApprovalsStateLock) {
1769+ execApprovalsSnapshotReady = false
1770+ if (retirePendingCronRuns) {
1771+ pendingExecApprovalWrites.clear()
1772+ }
16731773 }
16741774 _execApprovals .value = emptyList()
16751775 _execApprovalsRefreshing .value = false
@@ -7056,7 +7156,9 @@ class NodeRuntime private constructor(
70567156 val gatewayScope = captureGatewayDataScope() ? : return
70577157 val refreshGeneration =
70587158 synchronized(execApprovalsStateLock) {
7059- execApprovalsRefreshSeq.incrementAndGet()
7159+ val nextGeneration = execApprovalsRefreshSeq.incrementAndGet()
7160+ execApprovalsSnapshotReady = false
7161+ nextGeneration
70607162 }
70617163 publishGatewayData(gatewayScope) {
70627164 _execApprovalsRefreshing .value = true
@@ -7623,6 +7725,7 @@ class NodeRuntime private constructor(
76237725 resolvedExecApprovalIds.addAll(terminalIds)
76247726 terminalIds.forEach(pendingExecApprovalWrites::remove)
76257727 val nextRows = rows.filterNot { it.id in resolvedExecApprovalIds }.filterActiveExecApprovals()
7728+ execApprovalsSnapshotReady = true
76267729 _execApprovals .value = nextRows
76277730 scheduleExecApprovalExpiryPrune(nextRows)
76287731 }
0 commit comments