Skip to content

Commit dd17815

Browse files
authored
Merge branch 'main' into codex/autoqa-archive-undo
2 parents 5a5db64 + 0cd18e1 commit dd17815

194 files changed

Lines changed: 11017 additions & 1246 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/.i18n/native-source.json‎

Lines changed: 228 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/android/app/src/main/java/ai/openclaw/app/NodeRuntime.kt‎

Lines changed: 109 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import ai.openclaw.app.wear.WearProxyGatewayException
122122
import ai.openclaw.app.wear.WearProxyModel
123123
import ai.openclaw.app.wear.WearRealtimeAttemptOwner
124124
import ai.openclaw.app.wear.WearRealtimeTalkController
125+
import ai.openclaw.app.wear.projectWearAgentPulse
125126
import ai.openclaw.app.wear.wearConnectionFailure
126127
import ai.openclaw.wear.shared.WearMessage
127128
import ai.openclaw.wear.shared.WearRealtimeTalkCodec
@@ -141,6 +142,7 @@ import kotlinx.coroutines.Dispatchers
141142
import kotlinx.coroutines.Job
142143
import kotlinx.coroutines.NonCancellable
143144
import kotlinx.coroutines.SupervisorJob
145+
import kotlinx.coroutines.async
144146
import kotlinx.coroutines.channels.Channel
145147
import kotlinx.coroutines.coroutineScope
146148
import kotlinx.coroutines.delay
@@ -159,6 +161,7 @@ import kotlinx.coroutines.sync.Mutex
159161
import kotlinx.coroutines.sync.withLock
160162
import kotlinx.coroutines.withContext
161163
import kotlinx.coroutines.withTimeout
164+
import kotlinx.coroutines.withTimeoutOrNull
162165
import kotlinx.serialization.Serializable
163166
import kotlinx.serialization.json.Json
164167
import kotlinx.serialization.json.JsonArray
@@ -184,6 +187,39 @@ private const val CRON_JOBS_SNAPSHOT_MAX_ATTEMPTS = 3
184187
private const val OperatorAdminScope = "operator.admin"
185188
private 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+
187223
private fun execApprovalOutcomeUnknownMessage(): String = nativeText("Resolution outcome unknown. Actions stay disabled until the Gateway record is verified.").source
188224

189225
private 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
}

‎apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ import java.util.concurrent.atomic.AtomicLong
5858
internal const val SESSION_LIST_FETCH_LIMIT = 200
5959
private val QUESTION_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L, 2_000L, 4_000L)
6060
private val SWARM_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L, 2_000L, 4_000L)
61+
private const val WEAR_AGENT_PULSE_SWARM_MAX_ROWS = 1_000
62+
private const val WEAR_AGENT_PULSE_SWARM_FETCH_LIMIT = WEAR_AGENT_PULSE_SWARM_MAX_ROWS + 1
63+
private const val WEAR_AGENT_PULSE_DIRECT_CHILDREN_GROUP = "__wear_agent_pulse_direct_children__"
6164
private const val SUBAGENT_ACTIVITY_RETENTION_MS = 60_000L
6265
private const val SESSION_EDITOR_MAX_BASE64_CHARS = ((OUTBOX_MAX_COMMAND_ATTACHMENT_BYTES + 2) / 3) * 4
6366
private val MANAGED_MEDIA_PATH_REGEX =
@@ -385,6 +388,116 @@ class ChatController internal constructor(
385388
private var swarmSessionKey: String? = null
386389
private var swarmEnabled = false
387390

391+
internal fun currentSwarmSnapshot(): ChatSwarmSnapshot =
392+
synchronized(swarmLock) {
393+
ChatSwarmSnapshot(
394+
sessionKey = swarmSessionKey,
395+
enabled = swarmEnabled,
396+
groups = _swarmGroups.value,
397+
)
398+
}
399+
400+
/** Reads Swarm state for a Wear-selected session without changing the Phone chat selection. */
401+
internal suspend fun readSwarmSnapshotFor(
402+
sessionKey: String,
403+
agentId: String,
404+
): ChatSwarmSnapshot? {
405+
val requestedSessionKey = sessionKey.trim().takeIf(String::isNotEmpty) ?: return null
406+
val requestedAgentId = agentId.trim().takeIf(String::isNotEmpty) ?: return null
407+
val scopedSessionKey = normalizeRequestedSessionKey(requestedSessionKey)
408+
val scopedAgentId = resolveAgentIdFromMainSessionKey(scopedSessionKey)
409+
val restrictToParentAgent =
410+
when {
411+
scopedAgentId == requestedAgentId -> false
412+
requestedSessionKey == "main" && scopedSessionKey == "main" && scopedAgentId == null -> true
413+
else -> return null
414+
}
415+
val requestCacheScope = currentCacheScope() ?: return null
416+
val enabled =
417+
try {
418+
val params =
419+
buildJsonObject {
420+
put("agentId", JsonPrimitive(requestedAgentId))
421+
}
422+
val root =
423+
json
424+
.parseToJsonElement(
425+
requestGatewayBound(
426+
requestCacheScope.gatewayId,
427+
"chat.metadata",
428+
params.toString(),
429+
),
430+
).asObjectOrNull()
431+
root?.get("swarmEnabled").asBooleanOrNull() == true
432+
} catch (err: CancellationException) {
433+
throw err
434+
} catch (_: Throwable) {
435+
return null
436+
}
437+
if (!enabled) {
438+
return ChatSwarmSnapshot(
439+
sessionKey = requestedSessionKey,
440+
enabled = false,
441+
groups = emptyList(),
442+
)
443+
}
444+
val rows =
445+
try {
446+
val params =
447+
buildJsonObject {
448+
put("includeGlobal", JsonPrimitive(false))
449+
put("includeUnknown", JsonPrimitive(false))
450+
put("configuredAgentsOnly", JsonPrimitive(true))
451+
if (restrictToParentAgent) {
452+
put("agentId", JsonPrimitive(requestedAgentId))
453+
}
454+
put("spawnedBy", JsonPrimitive(scopedSessionKey))
455+
put("limit", JsonPrimitive(WEAR_AGENT_PULSE_SWARM_FETCH_LIMIT))
456+
put("offset", JsonPrimitive(0))
457+
}
458+
val root =
459+
json
460+
.parseToJsonElement(
461+
requestGatewayBound(
462+
requestCacheScope.gatewayId,
463+
"sessions.list",
464+
params.toString(),
465+
),
466+
).asObjectOrNull()
467+
?: return null
468+
val sessionElements = root["sessions"] as? JsonArray ?: return null
469+
val parsedRows = sessionElements.mapNotNull { parseSessionEntry(it.asObjectOrNull()) }
470+
val truncated =
471+
root["hasMore"].asBooleanOrNull() == true ||
472+
root["totalCount"].asLongOrNull()?.let { total -> total > sessionElements.size.toLong() } == true ||
473+
sessionElements.size > WEAR_AGENT_PULSE_SWARM_MAX_ROWS
474+
if (truncated || parsedRows.size != sessionElements.size) {
475+
return null
476+
}
477+
parsedRows
478+
} catch (err: CancellationException) {
479+
throw err
480+
} catch (_: Throwable) {
481+
return null
482+
}
483+
val projectedRows =
484+
rows.map { row ->
485+
val hasExplicitGroup = !row.swarmGroupId.isNullOrBlank()
486+
val hasSubagentProvenance = row.subagentRunState != null || row.hasActiveSubagentRun != null
487+
if (!hasExplicitGroup && hasSubagentProvenance) {
488+
row.copy(swarmGroupId = WEAR_AGENT_PULSE_DIRECT_CHILDREN_GROUP)
489+
} else {
490+
row
491+
}
492+
}
493+
if (requestCacheScope != currentCacheScope()) return null
494+
return ChatSwarmSnapshot(
495+
sessionKey = requestedSessionKey,
496+
enabled = true,
497+
groups = buildChatSwarmGroups(projectedRows) { candidate -> sameOutboxSession(candidate, scopedSessionKey) },
498+
)
499+
}
500+
388501
private val _sessionBranches = MutableStateFlow<List<SessionBranch>>(emptyList())
389502
val sessionBranches: StateFlow<List<SessionBranch>> = _sessionBranches.asStateFlow()
390503

@@ -4238,6 +4351,7 @@ class ChatController internal constructor(
42384351
) {
42394352
return
42404353
}
4354+
swarmSessionKey = snapshot.parentKey
42414355
_swarmGroups.value = groups
42424356
}
42434357
}

‎apps/android/app/src/main/java/ai/openclaw/app/chat/ChatSwarmProgress.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ data class ChatSwarmGroup(
4343
val phases: List<ChatSwarmPhase>,
4444
)
4545

46+
internal data class ChatSwarmSnapshot(
47+
val sessionKey: String?,
48+
val enabled: Boolean,
49+
val groups: List<ChatSwarmGroup>,
50+
) {
51+
fun isAvailableFor(sessionKey: String?): Boolean = enabled && sessionKey != null && this.sessionKey == sessionKey
52+
}
53+
4654
internal data class ChatSwarmSessionPage(
4755
val sessions: List<ChatSessionEntry>,
4856
val totalCount: Int?,

0 commit comments

Comments
 (0)