From a0fa283839741256806b23f6cc6f4f6e220a36a2 Mon Sep 17 00:00:00 2001 From: Nimrod Gutman Date: Wed, 25 Feb 2026 10:16:00 +0200 Subject: [PATCH 001/160] fix(discord): prevent stuck typing indicator --- src/channels/typing.test.ts | 24 +++++++++++++++++++ src/channels/typing.ts | 5 ++++ .../monitor/message-handler.process.ts | 16 +++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/channels/typing.test.ts b/src/channels/typing.test.ts index c1f314183b84..b68a1976491e 100644 --- a/src/channels/typing.test.ts +++ b/src/channels/typing.test.ts @@ -85,4 +85,28 @@ describe("createTypingCallbacks", () => { expect(stop).toHaveBeenCalledTimes(1); }); + + it("does not restart keepalive after idle cleanup", async () => { + vi.useFakeTimers(); + try { + const start = vi.fn().mockResolvedValue(undefined); + const stop = vi.fn().mockResolvedValue(undefined); + const onStartError = vi.fn(); + const callbacks = createTypingCallbacks({ start, stop, onStartError }); + + await callbacks.onReplyStart(); + expect(start).toHaveBeenCalledTimes(1); + + callbacks.onIdle?.(); + await flushMicrotasks(); + + await callbacks.onReplyStart(); + await vi.advanceTimersByTimeAsync(9_000); + + expect(start).toHaveBeenCalledTimes(1); + expect(stop).toHaveBeenCalledTimes(1); + } finally { + vi.useRealTimers(); + } + }); }); diff --git a/src/channels/typing.ts b/src/channels/typing.ts index b701dfb72cd7..531c2fdc485b 100644 --- a/src/channels/typing.ts +++ b/src/channels/typing.ts @@ -17,6 +17,7 @@ export function createTypingCallbacks(params: { const stop = params.stop; const keepaliveIntervalMs = params.keepaliveIntervalMs ?? 3_000; let stopSent = false; + let closed = false; const fireStart = async () => { try { @@ -32,6 +33,9 @@ export function createTypingCallbacks(params: { }); const onReplyStart = async () => { + if (closed) { + return; + } stopSent = false; keepaliveLoop.stop(); await fireStart(); @@ -39,6 +43,7 @@ export function createTypingCallbacks(params: { }; const fireStop = () => { + closed = true; keepaliveLoop.stop(); if (!stop || stopSent) { return; diff --git a/src/discord/monitor/message-handler.process.ts b/src/discord/monitor/message-handler.process.ts index 00124709c740..b6a73bc18a24 100644 --- a/src/discord/monitor/message-handler.process.ts +++ b/src/discord/monitor/message-handler.process.ts @@ -723,12 +723,18 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext) dispatchError = true; throw err; } finally { - // Must stop() first to flush debounced content before clear() wipes state - await draftStream?.stop(); - if (!finalizedViaPreviewMessage) { - await draftStream?.clear(); + try { + // Must stop() first to flush debounced content before clear() wipes state. + await draftStream?.stop(); + if (!finalizedViaPreviewMessage) { + await draftStream?.clear(); + } + } catch (err) { + // Draft cleanup should never keep typing alive. + logVerbose(`discord: draft cleanup failed: ${String(err)}`); + } finally { + markDispatchIdle(); } - markDispatchIdle(); if (statusReactionsEnabled) { if (dispatchError) { await statusReactions.setError(); From 56b8c69487e127d9b88a300a8279e8334c874093 Mon Sep 17 00:00:00 2001 From: Nimrod Gutman Date: Wed, 25 Feb 2026 10:21:32 +0200 Subject: [PATCH 002/160] docs(changelog): add discord typing fix entry (#26295) (thanks @ngutman) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6e1a71e0c9b..9eeac161fdf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Docs: https://docs.openclaw.ai - Telegram/Markdown spoilers: keep valid `||spoiler||` pairs while leaving unmatched trailing `||` delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin. - Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. - Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. +- Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. ## 2026.2.24 From 2e3c05d9dae4edff5a96061bbf42ae670a49ba97 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 13:34:08 +0530 Subject: [PATCH 003/160] feat(android): make QR scanning first-class onboarding --- apps/android/app/build.gradle.kts | 1 + .../android/ui/GatewayConfigResolver.kt | 48 ++- .../ai/openclaw/android/ui/OnboardingFlow.kt | 403 +++++++++++------- .../android/ui/GatewayConfigResolverTest.kt | 52 +++ 4 files changed, 339 insertions(+), 165 deletions(-) create mode 100644 apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt diff --git a/apps/android/app/build.gradle.kts b/apps/android/app/build.gradle.kts index ffe7d1d77c30..dda17320625e 100644 --- a/apps/android/app/build.gradle.kts +++ b/apps/android/app/build.gradle.kts @@ -137,6 +137,7 @@ dependencies { implementation("androidx.camera:camera-lifecycle:1.5.2") implementation("androidx.camera:camera-video:1.5.2") implementation("androidx.camera:camera-view:1.5.2") + implementation("com.journeyapps:zxing-android-embedded:4.3.0") // Unicast DNS-SD (Wide-Area Bonjour) for tailnet discovery domains. implementation("dnsjava:dnsjava:3.6.4") diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt index 5036c6290d3f..c9b545e0ce99 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt @@ -1,9 +1,13 @@ package ai.openclaw.android.ui -import android.util.Base64 import androidx.core.net.toUri +import java.util.Base64 import java.util.Locale -import org.json.JSONObject +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive internal data class GatewayEndpointConfig( val host: String, @@ -26,6 +30,8 @@ internal data class GatewayConnectConfig( val password: String, ) +private val gatewaySetupJson = Json { ignoreUnknownKeys = true } + internal fun resolveGatewayConnectConfig( useSetupCode: Boolean, setupCode: String, @@ -94,14 +100,31 @@ internal fun decodeGatewaySetupCode(rawInput: String): GatewaySetupCode? { } return try { - val decoded = String(Base64.decode(padded, Base64.DEFAULT), Charsets.UTF_8) - val obj = JSONObject(decoded) - val url = obj.optString("url").trim() + val decoded = String(Base64.getDecoder().decode(padded), Charsets.UTF_8) + val obj = parseJsonObject(decoded) ?: return null + val url = jsonField(obj, "url").orEmpty() if (url.isEmpty()) return null - val token = obj.optString("token").trim().ifEmpty { null } - val password = obj.optString("password").trim().ifEmpty { null } + val token = jsonField(obj, "token") + val password = jsonField(obj, "password") GatewaySetupCode(url = url, token = token, password = password) - } catch (_: Throwable) { + } catch (_: IllegalArgumentException) { + null + } +} + +internal fun resolveScannedSetupCode(rawInput: String): String? { + val trimmed = rawInput.trim() + if (trimmed.isEmpty()) return null + + if (decodeGatewaySetupCode(trimmed) != null) { + return trimmed + } + + val obj = parseJsonObject(trimmed) ?: return null + val setupCode = jsonField(obj, "setupCode") ?: return null + return if (decodeGatewaySetupCode(setupCode) != null) { + setupCode + } else { null } } @@ -113,3 +136,12 @@ internal fun composeGatewayManualUrl(hostInput: String, portInput: String, tls: val scheme = if (tls) "https" else "http" return "$scheme://$host:$port" } + +private fun parseJsonObject(input: String): JsonObject? { + return runCatching { gatewaySetupJson.parseToJsonElement(input).jsonObject }.getOrNull() +} + +private fun jsonField(obj: JsonObject, key: String): String? { + val value = obj[key]?.jsonPrimitive?.contentOrNull?.trim().orEmpty() + return value.ifEmpty { null } +} diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt index 8c732d9c3603..d9f3ec44fa66 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt @@ -6,6 +6,7 @@ import android.content.pm.PackageManager import android.os.Build import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement @@ -51,6 +52,8 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack +import androidx.compose.material.icons.filled.ExpandLess +import androidx.compose.material.icons.filled.ExpandMore import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -74,6 +77,8 @@ import androidx.core.content.ContextCompat import ai.openclaw.android.LocationMode import ai.openclaw.android.MainViewModel import ai.openclaw.android.R +import com.journeyapps.barcodescanner.ScanContract +import com.journeyapps.barcodescanner.ScanOptions private enum class OnboardingStep(val index: Int, val label: String) { Welcome(1, "Welcome"), @@ -192,6 +197,7 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { var gatewayUrl by rememberSaveable { mutableStateOf("") } var gatewayPassword by rememberSaveable { mutableStateOf("") } var gatewayInputMode by rememberSaveable { mutableStateOf(GatewayInputMode.SetupCode) } + var gatewayAdvancedOpen by rememberSaveable { mutableStateOf(false) } var manualHost by rememberSaveable { mutableStateOf("10.0.2.2") } var manualPort by rememberSaveable { mutableStateOf("18789") } var manualTls by rememberSaveable { mutableStateOf(false) } @@ -246,6 +252,23 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { step = OnboardingStep.FinalCheck } + val qrScanLauncher = + rememberLauncherForActivityResult(ScanContract()) { result -> + val contents = result.contents?.trim().orEmpty() + if (contents.isEmpty()) { + return@rememberLauncherForActivityResult + } + val scannedSetupCode = resolveScannedSetupCode(contents) + if (scannedSetupCode == null) { + gatewayError = "QR code did not contain a valid setup code." + return@rememberLauncherForActivityResult + } + setupCode = scannedSetupCode + gatewayInputMode = GatewayInputMode.SetupCode + gatewayError = null + attemptedConnect = false + } + if (pendingTrust != null) { val prompt = pendingTrust!! AlertDialog( @@ -316,6 +339,7 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { OnboardingStep.Gateway -> GatewayStep( inputMode = gatewayInputMode, + advancedOpen = gatewayAdvancedOpen, setupCode = setupCode, manualHost = manualHost, manualPort = manualPort, @@ -323,6 +347,18 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { gatewayToken = persistedGatewayToken, gatewayPassword = gatewayPassword, gatewayError = gatewayError, + onScanQrClick = { + gatewayError = null + qrScanLauncher.launch( + ScanOptions().apply { + setDesiredBarcodeFormats(ScanOptions.QR_CODE) + setPrompt("Scan OpenClaw onboarding QR") + setBeepEnabled(false) + setOrientationLocked(false) + }, + ) + }, + onAdvancedOpenChange = { gatewayAdvancedOpen = it }, onInputModeChange = { gatewayInputMode = it gatewayError = null @@ -367,7 +403,7 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { remoteAddress = remoteAddress, attemptedConnect = attemptedConnect, enabledPermissions = enabledPermissionSummary, - methodLabel = if (gatewayInputMode == GatewayInputMode.SetupCode) "Setup Code" else "Manual", + methodLabel = if (gatewayInputMode == GatewayInputMode.SetupCode) "QR / Setup Code" else "Manual", ) } } @@ -429,7 +465,7 @@ fun OnboardingFlow(viewModel: MainViewModel, modifier: Modifier = Modifier) { if (gatewayInputMode == GatewayInputMode.SetupCode) { val parsedSetup = decodeGatewaySetupCode(setupCode) if (parsedSetup == null) { - gatewayError = "Invalid setup code." + gatewayError = "Scan QR code first, or use Advanced setup." return@Button } val parsedGateway = parseGatewayEndpoint(parsedSetup.url) @@ -607,6 +643,7 @@ private fun WelcomeStep() { @Composable private fun GatewayStep( inputMode: GatewayInputMode, + advancedOpen: Boolean, setupCode: String, manualHost: String, manualPort: String, @@ -614,6 +651,8 @@ private fun GatewayStep( gatewayToken: String, gatewayPassword: String, gatewayError: String?, + onScanQrClick: () -> Unit, + onAdvancedOpenChange: (Boolean) -> Unit, onInputModeChange: (GatewayInputMode) -> Unit, onSetupCodeChange: (String) -> Unit, onManualHostChange: (String) -> Unit, @@ -626,175 +665,225 @@ private fun GatewayStep( val manualResolvedEndpoint = remember(manualHost, manualPort, manualTls) { composeGatewayManualUrl(manualHost, manualPort, manualTls)?.let { parseGatewayEndpoint(it)?.displayUrl } } StepShell(title = "Gateway Connection") { - GuideBlock(title = "Get setup code + gateway URL") { + GuideBlock(title = "Scan onboarding QR") { Text("Run these on the gateway host:", style = onboardingCalloutStyle, color = onboardingTextSecondary) - CommandBlock("openclaw qr --setup-code-only") - CommandBlock("openclaw qr --json") - Text( - "`--json` prints `setupCode` and `gatewayUrl`.", - style = onboardingCalloutStyle, - color = onboardingTextSecondary, - ) - Text( - "Auto URL discovery is not wired yet. Android emulator uses `10.0.2.2`; real devices need LAN/Tailscale host.", - style = onboardingCalloutStyle, - color = onboardingTextSecondary, - ) + CommandBlock("openclaw qr") + Text("Then scan with this device.", style = onboardingCalloutStyle, color = onboardingTextSecondary) + } + Button( + onClick = onScanQrClick, + modifier = Modifier.fillMaxWidth().height(48.dp), + shape = RoundedCornerShape(12.dp), + colors = + ButtonDefaults.buttonColors( + containerColor = onboardingAccent, + contentColor = Color.White, + ), + ) { + Text("Scan QR code", style = onboardingHeadlineStyle.copy(fontWeight = FontWeight.Bold)) + } + if (!resolvedEndpoint.isNullOrBlank()) { + Text("QR captured. Review endpoint below.", style = onboardingCalloutStyle, color = onboardingSuccess) + ResolvedEndpoint(endpoint = resolvedEndpoint) } - GatewayModeToggle(inputMode = inputMode, onInputModeChange = onInputModeChange) - - if (inputMode == GatewayInputMode.SetupCode) { - Text("SETUP CODE", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) - OutlinedTextField( - value = setupCode, - onValueChange = onSetupCodeChange, - placeholder = { Text("Paste code from `openclaw qr --setup-code-only`", color = onboardingTextTertiary, style = onboardingBodyStyle) }, - modifier = Modifier.fillMaxWidth(), - minLines = 3, - maxLines = 5, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), - textStyle = onboardingBodyStyle.copy(fontFamily = FontFamily.Monospace, color = onboardingText), - shape = RoundedCornerShape(14.dp), - colors = - OutlinedTextFieldDefaults.colors( - focusedContainerColor = onboardingSurface, - unfocusedContainerColor = onboardingSurface, - focusedBorderColor = onboardingAccent, - unfocusedBorderColor = onboardingBorder, - focusedTextColor = onboardingText, - unfocusedTextColor = onboardingText, - cursorColor = onboardingAccent, - ), - ) - if (!resolvedEndpoint.isNullOrBlank()) { - ResolvedEndpoint(endpoint = resolvedEndpoint) - } - } else { - Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - QuickFillChip(label = "Android Emulator", onClick = { - onManualHostChange("10.0.2.2") - onManualPortChange("18789") - onManualTlsChange(false) - }) - QuickFillChip(label = "Localhost", onClick = { - onManualHostChange("127.0.0.1") - onManualPortChange("18789") - onManualTlsChange(false) - }) - } - - Text("HOST", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) - OutlinedTextField( - value = manualHost, - onValueChange = onManualHostChange, - placeholder = { Text("10.0.2.2", color = onboardingTextTertiary, style = onboardingBodyStyle) }, - modifier = Modifier.fillMaxWidth(), - singleLine = true, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), - textStyle = onboardingBodyStyle.copy(color = onboardingText), - shape = RoundedCornerShape(14.dp), - colors = - OutlinedTextFieldDefaults.colors( - focusedContainerColor = onboardingSurface, - unfocusedContainerColor = onboardingSurface, - focusedBorderColor = onboardingAccent, - unfocusedBorderColor = onboardingBorder, - focusedTextColor = onboardingText, - unfocusedTextColor = onboardingText, - cursorColor = onboardingAccent, - ), - ) - - Text("PORT", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) - OutlinedTextField( - value = manualPort, - onValueChange = onManualPortChange, - placeholder = { Text("18789", color = onboardingTextTertiary, style = onboardingBodyStyle) }, - modifier = Modifier.fillMaxWidth(), - singleLine = true, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), - textStyle = onboardingBodyStyle.copy(fontFamily = FontFamily.Monospace, color = onboardingText), - shape = RoundedCornerShape(14.dp), - colors = - OutlinedTextFieldDefaults.colors( - focusedContainerColor = onboardingSurface, - unfocusedContainerColor = onboardingSurface, - focusedBorderColor = onboardingAccent, - unfocusedBorderColor = onboardingBorder, - focusedTextColor = onboardingText, - unfocusedTextColor = onboardingText, - cursorColor = onboardingAccent, - ), - ) + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(12.dp), + color = onboardingSurface, + border = androidx.compose.foundation.BorderStroke(1.dp, onboardingBorderStrong), + onClick = { onAdvancedOpenChange(!advancedOpen) }, + ) { Row( - modifier = Modifier.fillMaxWidth(), + modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 10.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Column(verticalArrangement = Arrangement.spacedBy(2.dp)) { - Text("Use TLS", style = onboardingHeadlineStyle, color = onboardingText) - Text("Switch to secure websocket (`wss`).", style = onboardingCalloutStyle.copy(lineHeight = 18.sp), color = onboardingTextSecondary) + Text("Advanced setup", style = onboardingHeadlineStyle, color = onboardingText) + Text("Paste setup code or enter host/port manually.", style = onboardingCaption1Style, color = onboardingTextSecondary) } - Switch( - checked = manualTls, - onCheckedChange = onManualTlsChange, - colors = - SwitchDefaults.colors( - checkedTrackColor = onboardingAccent, - uncheckedTrackColor = onboardingBorderStrong, - checkedThumbColor = Color.White, - uncheckedThumbColor = Color.White, - ), + Icon( + imageVector = if (advancedOpen) Icons.Default.ExpandLess else Icons.Default.ExpandMore, + contentDescription = if (advancedOpen) "Collapse advanced setup" else "Expand advanced setup", + tint = onboardingTextSecondary, ) } + } - Text("TOKEN (OPTIONAL)", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) - OutlinedTextField( - value = gatewayToken, - onValueChange = onTokenChange, - placeholder = { Text("token", color = onboardingTextTertiary, style = onboardingBodyStyle) }, - modifier = Modifier.fillMaxWidth(), - singleLine = true, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), - textStyle = onboardingBodyStyle.copy(color = onboardingText), - shape = RoundedCornerShape(14.dp), - colors = - OutlinedTextFieldDefaults.colors( - focusedContainerColor = onboardingSurface, - unfocusedContainerColor = onboardingSurface, - focusedBorderColor = onboardingAccent, - unfocusedBorderColor = onboardingBorder, - focusedTextColor = onboardingText, - unfocusedTextColor = onboardingText, - cursorColor = onboardingAccent, - ), - ) + AnimatedVisibility(visible = advancedOpen) { + Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + GuideBlock(title = "Manual setup commands") { + Text("Run these on the gateway host:", style = onboardingCalloutStyle, color = onboardingTextSecondary) + CommandBlock("openclaw qr --setup-code-only") + CommandBlock("openclaw qr --json") + Text( + "`--json` prints `setupCode` and `gatewayUrl`.", + style = onboardingCalloutStyle, + color = onboardingTextSecondary, + ) + Text( + "Auto URL discovery is not wired yet. Android emulator uses `10.0.2.2`; real devices need LAN/Tailscale host.", + style = onboardingCalloutStyle, + color = onboardingTextSecondary, + ) + } + GatewayModeToggle(inputMode = inputMode, onInputModeChange = onInputModeChange) - Text("PASSWORD (OPTIONAL)", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) - OutlinedTextField( - value = gatewayPassword, - onValueChange = onPasswordChange, - placeholder = { Text("password", color = onboardingTextTertiary, style = onboardingBodyStyle) }, - modifier = Modifier.fillMaxWidth(), - singleLine = true, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), - textStyle = onboardingBodyStyle.copy(color = onboardingText), - shape = RoundedCornerShape(14.dp), - colors = - OutlinedTextFieldDefaults.colors( - focusedContainerColor = onboardingSurface, - unfocusedContainerColor = onboardingSurface, - focusedBorderColor = onboardingAccent, - unfocusedBorderColor = onboardingBorder, - focusedTextColor = onboardingText, - unfocusedTextColor = onboardingText, - cursorColor = onboardingAccent, - ), - ) + if (inputMode == GatewayInputMode.SetupCode) { + Text("SETUP CODE", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) + OutlinedTextField( + value = setupCode, + onValueChange = onSetupCodeChange, + placeholder = { Text("Paste code from `openclaw qr --setup-code-only`", color = onboardingTextTertiary, style = onboardingBodyStyle) }, + modifier = Modifier.fillMaxWidth(), + minLines = 3, + maxLines = 5, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), + textStyle = onboardingBodyStyle.copy(fontFamily = FontFamily.Monospace, color = onboardingText), + shape = RoundedCornerShape(14.dp), + colors = + OutlinedTextFieldDefaults.colors( + focusedContainerColor = onboardingSurface, + unfocusedContainerColor = onboardingSurface, + focusedBorderColor = onboardingAccent, + unfocusedBorderColor = onboardingBorder, + focusedTextColor = onboardingText, + unfocusedTextColor = onboardingText, + cursorColor = onboardingAccent, + ), + ) + if (!resolvedEndpoint.isNullOrBlank()) { + ResolvedEndpoint(endpoint = resolvedEndpoint) + } + } else { + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + QuickFillChip(label = "Android Emulator", onClick = { + onManualHostChange("10.0.2.2") + onManualPortChange("18789") + onManualTlsChange(false) + }) + QuickFillChip(label = "Localhost", onClick = { + onManualHostChange("127.0.0.1") + onManualPortChange("18789") + onManualTlsChange(false) + }) + } + + Text("HOST", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) + OutlinedTextField( + value = manualHost, + onValueChange = onManualHostChange, + placeholder = { Text("10.0.2.2", color = onboardingTextTertiary, style = onboardingBodyStyle) }, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), + textStyle = onboardingBodyStyle.copy(color = onboardingText), + shape = RoundedCornerShape(14.dp), + colors = + OutlinedTextFieldDefaults.colors( + focusedContainerColor = onboardingSurface, + unfocusedContainerColor = onboardingSurface, + focusedBorderColor = onboardingAccent, + unfocusedBorderColor = onboardingBorder, + focusedTextColor = onboardingText, + unfocusedTextColor = onboardingText, + cursorColor = onboardingAccent, + ), + ) + + Text("PORT", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) + OutlinedTextField( + value = manualPort, + onValueChange = onManualPortChange, + placeholder = { Text("18789", color = onboardingTextTertiary, style = onboardingBodyStyle) }, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + textStyle = onboardingBodyStyle.copy(fontFamily = FontFamily.Monospace, color = onboardingText), + shape = RoundedCornerShape(14.dp), + colors = + OutlinedTextFieldDefaults.colors( + focusedContainerColor = onboardingSurface, + unfocusedContainerColor = onboardingSurface, + focusedBorderColor = onboardingAccent, + unfocusedBorderColor = onboardingBorder, + focusedTextColor = onboardingText, + unfocusedTextColor = onboardingText, + cursorColor = onboardingAccent, + ), + ) - if (!manualResolvedEndpoint.isNullOrBlank()) { - ResolvedEndpoint(endpoint = manualResolvedEndpoint) + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Column(verticalArrangement = Arrangement.spacedBy(2.dp)) { + Text("Use TLS", style = onboardingHeadlineStyle, color = onboardingText) + Text("Switch to secure websocket (`wss`).", style = onboardingCalloutStyle.copy(lineHeight = 18.sp), color = onboardingTextSecondary) + } + Switch( + checked = manualTls, + onCheckedChange = onManualTlsChange, + colors = + SwitchDefaults.colors( + checkedTrackColor = onboardingAccent, + uncheckedTrackColor = onboardingBorderStrong, + checkedThumbColor = Color.White, + uncheckedThumbColor = Color.White, + ), + ) + } + + Text("TOKEN (OPTIONAL)", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) + OutlinedTextField( + value = gatewayToken, + onValueChange = onTokenChange, + placeholder = { Text("token", color = onboardingTextTertiary, style = onboardingBodyStyle) }, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), + textStyle = onboardingBodyStyle.copy(color = onboardingText), + shape = RoundedCornerShape(14.dp), + colors = + OutlinedTextFieldDefaults.colors( + focusedContainerColor = onboardingSurface, + unfocusedContainerColor = onboardingSurface, + focusedBorderColor = onboardingAccent, + unfocusedBorderColor = onboardingBorder, + focusedTextColor = onboardingText, + unfocusedTextColor = onboardingText, + cursorColor = onboardingAccent, + ), + ) + + Text("PASSWORD (OPTIONAL)", style = onboardingCaption1Style.copy(letterSpacing = 0.9.sp), color = onboardingTextSecondary) + OutlinedTextField( + value = gatewayPassword, + onValueChange = onPasswordChange, + placeholder = { Text("password", color = onboardingTextTertiary, style = onboardingBodyStyle) }, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii), + textStyle = onboardingBodyStyle.copy(color = onboardingText), + shape = RoundedCornerShape(14.dp), + colors = + OutlinedTextFieldDefaults.colors( + focusedContainerColor = onboardingSurface, + unfocusedContainerColor = onboardingSurface, + focusedBorderColor = onboardingAccent, + unfocusedBorderColor = onboardingBorder, + focusedTextColor = onboardingText, + unfocusedTextColor = onboardingText, + cursorColor = onboardingAccent, + ), + ) + + if (!manualResolvedEndpoint.isNullOrBlank()) { + ResolvedEndpoint(endpoint = manualResolvedEndpoint) + } + } } } diff --git a/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt new file mode 100644 index 000000000000..421f0b3ad6d5 --- /dev/null +++ b/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt @@ -0,0 +1,52 @@ +package ai.openclaw.android.ui + +import java.util.Base64 +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class GatewayConfigResolverTest { + @Test + fun resolveScannedSetupCodeAcceptsRawSetupCode() { + val setupCode = encodeSetupCode("""{"url":"wss://gateway.example:18789","token":"token-1"}""") + + val resolved = resolveScannedSetupCode(setupCode) + + assertEquals(setupCode, resolved) + } + + @Test + fun resolveScannedSetupCodeAcceptsQrJsonPayload() { + val setupCode = encodeSetupCode("""{"url":"wss://gateway.example:18789","password":"pw-1"}""") + val qrJson = + """ + { + "setupCode": "$setupCode", + "gatewayUrl": "wss://gateway.example:18789", + "auth": "password", + "urlSource": "gateway.remote.url" + } + """.trimIndent() + + val resolved = resolveScannedSetupCode(qrJson) + + assertEquals(setupCode, resolved) + } + + @Test + fun resolveScannedSetupCodeRejectsInvalidInput() { + val resolved = resolveScannedSetupCode("not-a-valid-setup-code") + assertNull(resolved) + } + + @Test + fun resolveScannedSetupCodeRejectsJsonWithInvalidSetupCode() { + val qrJson = """{"setupCode":"invalid"}""" + val resolved = resolveScannedSetupCode(qrJson) + assertNull(resolved) + } + + private fun encodeSetupCode(payloadJson: String): String { + return Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.toByteArray(Charsets.UTF_8)) + } +} From f894c23e640fa98931d1a3918f4d2d02ffa6e8fb Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 13:35:24 +0530 Subject: [PATCH 004/160] docs(android): update README for native Android workflow --- apps/android/README.md | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/apps/android/README.md b/apps/android/README.md index 5e4d32359e0a..799109c0a0f8 100644 --- a/apps/android/README.md +++ b/apps/android/README.md @@ -34,6 +34,56 @@ cd apps/android `gradlew` auto-detects the Android SDK at `~/Library/Android/sdk` (macOS default) if `ANDROID_SDK_ROOT` / `ANDROID_HOME` are unset. +## Run on a Real Android Phone (USB) + +1) On phone, enable **Developer options** + **USB debugging**. +2) Connect by USB and accept the debugging trust prompt on phone. +3) Verify ADB can see the device: + +```bash +adb devices -l +``` + +4) Install + launch debug build: + +```bash +pnpm android:install +pnpm android:run +``` + +If `adb devices -l` shows `unauthorized`, re-plug and accept the trust prompt again. + +### USB-only gateway testing (no LAN dependency) + +Use `adb reverse` so Android `localhost:18789` tunnels to your laptop `localhost:18789`. + +Terminal A (gateway): + +```bash +pnpm openclaw gateway --port 18789 --verbose +``` + +Terminal B (USB tunnel): + +```bash +adb reverse tcp:18789 tcp:18789 +``` + +Then in app **Connect → Manual**: + +- Host: `127.0.0.1` +- Port: `18789` +- TLS: off + +## Hot Reload / Fast Iteration + +This app is native Kotlin + Jetpack Compose. + +- For Compose UI edits: use Android Studio **Live Edit** on a debug build (works on physical devices; project `minSdk=31` already meets API requirement). +- For many non-structural code/resource changes: use Android Studio **Apply Changes**. +- For structural/native/manifest/Gradle changes: do full reinstall (`pnpm android:run`). +- Canvas web content already supports live reload when loaded from Gateway `__openclaw__/canvas/` (see `docs/platforms/android.md`). + ## Connect / Pair 1) Start the gateway (on your main machine): From 959cbafcdb78b6388cf691d77404c384556c293a Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 13:35:33 +0530 Subject: [PATCH 005/160] fix(android): stabilize chat composer ime and tab layout --- apps/android/app/src/main/AndroidManifest.xml | 1 + .../java/ai/openclaw/android/MainActivity.kt | 24 -------- .../openclaw/android/ui/PostOnboardingTabs.kt | 30 ++++++---- .../openclaw/android/ui/chat/ChatComposer.kt | 29 +++++++--- .../android/ui/chat/ChatSheetContent.kt | 57 ++++++++++--------- 5 files changed, 70 insertions(+), 71 deletions(-) diff --git a/apps/android/app/src/main/AndroidManifest.xml b/apps/android/app/src/main/AndroidManifest.xml index facdbf301b42..c9814c00b4f0 100644 --- a/apps/android/app/src/main/AndroidManifest.xml +++ b/apps/android/app/src/main/AndroidManifest.xml @@ -50,6 +50,7 @@ diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt index cafe0958f86a..aa85ab7efbcc 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt @@ -9,9 +9,6 @@ import androidx.activity.compose.setContent import androidx.activity.viewModels import androidx.compose.material3.Surface import androidx.compose.ui.Modifier -import androidx.core.view.WindowCompat -import androidx.core.view.WindowInsetsCompat -import androidx.core.view.WindowInsetsControllerCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle @@ -28,7 +25,6 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) val isDebuggable = (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0 WebView.setWebContentsDebuggingEnabled(isDebuggable) - applyImmersiveMode() NodeForegroundService.start(this) permissionRequester = PermissionRequester(this) screenCaptureRequester = ScreenCaptureRequester(this) @@ -59,18 +55,6 @@ class MainActivity : ComponentActivity() { } } - override fun onResume() { - super.onResume() - applyImmersiveMode() - } - - override fun onWindowFocusChanged(hasFocus: Boolean) { - super.onWindowFocusChanged(hasFocus) - if (hasFocus) { - applyImmersiveMode() - } - } - override fun onStart() { super.onStart() viewModel.setForeground(true) @@ -80,12 +64,4 @@ class MainActivity : ComponentActivity() { viewModel.setForeground(false) super.onStop() } - - private fun applyImmersiveMode() { - WindowCompat.setDecorFitsSystemWindows(window, false) - val controller = WindowInsetsControllerCompat(window, window.decorView) - controller.systemBarsBehavior = - WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE - controller.hide(WindowInsetsCompat.Type.systemBars()) - } } diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt index b68c06ff2ff9..b79b8cb4d80e 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt @@ -5,14 +5,13 @@ import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.WindowInsetsSides import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn -import androidx.compose.foundation.layout.isImeVisible +import androidx.compose.foundation.layout.ime import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.only import androidx.compose.foundation.layout.offset @@ -41,6 +40,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import ai.openclaw.android.MainViewModel @@ -65,10 +65,8 @@ private enum class StatusVisual { } @Composable -@OptIn(ExperimentalLayoutApi::class) fun PostOnboardingTabs(viewModel: MainViewModel, modifier: Modifier = Modifier) { var activeTab by rememberSaveable { mutableStateOf(HomeTab.Connect) } - val imeVisible = WindowInsets.isImeVisible val statusText by viewModel.statusText.collectAsState() val isConnected by viewModel.isConnected.collectAsState() @@ -96,19 +94,29 @@ fun PostOnboardingTabs(viewModel: MainViewModel, modifier: Modifier = Modifier) ) }, bottomBar = { - if (!imeVisible) { - BottomTabBar( - activeTab = activeTab, - onSelect = { activeTab = it }, - ) - } + BottomTabBar( + activeTab = activeTab, + onSelect = { activeTab = it }, + ) }, ) { innerPadding -> + val density = LocalDensity.current + val imeVisible = WindowInsets.ime.getBottom(density) > 0 + val contentBottomPadding = + if (activeTab == HomeTab.Chat && imeVisible) { + 0.dp + } else { + innerPadding.calculateBottomPadding() + } + Box( modifier = Modifier .fillMaxSize() - .padding(innerPadding) + .padding( + top = innerPadding.calculateTopPadding(), + bottom = contentBottomPadding, + ) .background(mobileBackgroundGradient), ) { when (activeTab) { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatComposer.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatComposer.kt index 7f71995906bd..22099500ebf1 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatComposer.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatComposer.kt @@ -5,6 +5,7 @@ import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth @@ -161,6 +162,7 @@ fun ChatComposer( label = "Refresh", icon = Icons.Default.Refresh, enabled = true, + compact = true, onClick = onRefresh, ) @@ -168,6 +170,7 @@ fun ChatComposer( label = "Abort", icon = Icons.Default.Stop, enabled = pendingRunCount > 0, + compact = true, onClick = onAbort, ) } @@ -196,7 +199,12 @@ fun ChatComposer( Icon(Icons.AutoMirrored.Filled.Send, contentDescription = null, modifier = Modifier.size(16.dp)) } Spacer(modifier = Modifier.width(8.dp)) - Text("Send", style = mobileHeadline.copy(fontWeight = FontWeight.Bold)) + Text( + text = "Send", + style = mobileHeadline.copy(fontWeight = FontWeight.Bold), + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) } } } @@ -207,12 +215,13 @@ private fun SecondaryActionButton( label: String, icon: androidx.compose.ui.graphics.vector.ImageVector, enabled: Boolean, + compact: Boolean = false, onClick: () -> Unit, ) { Button( onClick = onClick, enabled = enabled, - modifier = Modifier.height(44.dp), + modifier = if (compact) Modifier.size(44.dp) else Modifier.height(44.dp), shape = RoundedCornerShape(14.dp), colors = ButtonDefaults.buttonColors( @@ -222,15 +231,17 @@ private fun SecondaryActionButton( disabledContentColor = mobileTextTertiary, ), border = BorderStroke(1.dp, mobileBorderStrong), - contentPadding = ButtonDefaults.ContentPadding, + contentPadding = if (compact) PaddingValues(0.dp) else ButtonDefaults.ContentPadding, ) { Icon(icon, contentDescription = label, modifier = Modifier.size(14.dp)) - Spacer(modifier = Modifier.width(5.dp)) - Text( - text = label, - style = mobileCallout.copy(fontWeight = FontWeight.SemiBold), - color = if (enabled) mobileTextSecondary else mobileTextTertiary, - ) + if (!compact) { + Spacer(modifier = Modifier.width(5.dp)) + Text( + text = label, + style = mobileCallout.copy(fontWeight = FontWeight.SemiBold), + color = if (enabled) mobileTextSecondary else mobileTextTertiary, + ) + } } } diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatSheetContent.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatSheetContent.kt index d1c2743ef041..12e13ab365ab 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatSheetContent.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/chat/ChatSheetContent.kt @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape @@ -122,33 +123,35 @@ fun ChatSheetContent(viewModel: MainViewModel) { modifier = Modifier.weight(1f, fill = true), ) - ChatComposer( - healthOk = healthOk, - thinkingLevel = thinkingLevel, - pendingRunCount = pendingRunCount, - attachments = attachments, - onPickImages = { pickImages.launch("image/*") }, - onRemoveAttachment = { id -> attachments.removeAll { it.id == id } }, - onSetThinkingLevel = { level -> viewModel.setChatThinkingLevel(level) }, - onRefresh = { - viewModel.refreshChat() - viewModel.refreshChatSessions(limit = 200) - }, - onAbort = { viewModel.abortChat() }, - onSend = { text -> - val outgoing = - attachments.map { att -> - OutgoingAttachment( - type = "image", - mimeType = att.mimeType, - fileName = att.fileName, - base64 = att.base64, - ) - } - viewModel.sendChat(message = text, thinking = thinkingLevel, attachments = outgoing) - attachments.clear() - }, - ) + Row(modifier = Modifier.fillMaxWidth().imePadding()) { + ChatComposer( + healthOk = healthOk, + thinkingLevel = thinkingLevel, + pendingRunCount = pendingRunCount, + attachments = attachments, + onPickImages = { pickImages.launch("image/*") }, + onRemoveAttachment = { id -> attachments.removeAll { it.id == id } }, + onSetThinkingLevel = { level -> viewModel.setChatThinkingLevel(level) }, + onRefresh = { + viewModel.refreshChat() + viewModel.refreshChatSessions(limit = 200) + }, + onAbort = { viewModel.abortChat() }, + onSend = { text -> + val outgoing = + attachments.map { att -> + OutgoingAttachment( + type = "image", + mimeType = att.mimeType, + fileName = att.fileName, + base64 = att.base64, + ) + } + viewModel.sendChat(message = text, thinking = thinkingLevel, attachments = outgoing) + attachments.clear() + }, + ) + } } } From 7725c0b9b317a07fb9ad9baf58bffcfe5b7a2e50 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 13:45:42 +0530 Subject: [PATCH 006/160] fix(android): stabilize chat ime insets and tab bar --- apps/android/app/src/main/AndroidManifest.xml | 2 +- .../java/ai/openclaw/android/MainActivity.kt | 2 ++ .../openclaw/android/ui/PostOnboardingTabs.kt | 30 ++++++++----------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/apps/android/app/src/main/AndroidManifest.xml b/apps/android/app/src/main/AndroidManifest.xml index c9814c00b4f0..6b8dd7eedbaf 100644 --- a/apps/android/app/src/main/AndroidManifest.xml +++ b/apps/android/app/src/main/AndroidManifest.xml @@ -50,7 +50,7 @@ diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt index aa85ab7efbcc..21d0f15ff7ab 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt @@ -7,6 +7,7 @@ import android.webkit.WebView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels +import androidx.core.view.WindowCompat import androidx.compose.material3.Surface import androidx.compose.ui.Modifier import androidx.lifecycle.Lifecycle @@ -23,6 +24,7 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + WindowCompat.setDecorFitsSystemWindows(window, false) val isDebuggable = (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0 WebView.setWebContentsDebuggingEnabled(isDebuggable) NodeForegroundService.start(this) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt index b79b8cb4d80e..a556bc82c436 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawing import androidx.compose.foundation.layout.windowInsetsPadding +import androidx.compose.foundation.layout.consumeWindowInsets import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ScreenShare @@ -83,6 +84,10 @@ fun PostOnboardingTabs(viewModel: MainViewModel, modifier: Modifier = Modifier) } } + val density = LocalDensity.current + val imeVisible = WindowInsets.ime.getBottom(density) > 0 + val hideBottomTabBar = activeTab == HomeTab.Chat && imeVisible + Scaffold( modifier = modifier, containerColor = Color.Transparent, @@ -94,29 +99,20 @@ fun PostOnboardingTabs(viewModel: MainViewModel, modifier: Modifier = Modifier) ) }, bottomBar = { - BottomTabBar( - activeTab = activeTab, - onSelect = { activeTab = it }, - ) + if (!hideBottomTabBar) { + BottomTabBar( + activeTab = activeTab, + onSelect = { activeTab = it }, + ) + } }, ) { innerPadding -> - val density = LocalDensity.current - val imeVisible = WindowInsets.ime.getBottom(density) > 0 - val contentBottomPadding = - if (activeTab == HomeTab.Chat && imeVisible) { - 0.dp - } else { - innerPadding.calculateBottomPadding() - } - Box( modifier = Modifier .fillMaxSize() - .padding( - top = innerPadding.calculateTopPadding(), - bottom = contentBottomPadding, - ) + .padding(innerPadding) + .consumeWindowInsets(innerPadding) .background(mobileBackgroundGradient), ) { when (activeTab) { From 9c1c083d98e5487ed899d8387c1cc82fa5be2b6e Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 13:47:55 +0530 Subject: [PATCH 007/160] fix(android): remove tab bar gap above system nav --- .../ai/openclaw/android/ui/PostOnboardingTabs.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt index a556bc82c436..b0135734e4c1 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt @@ -14,7 +14,6 @@ import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.ime import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.only -import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawing import androidx.compose.foundation.layout.windowInsetsPadding @@ -269,18 +268,21 @@ private fun BottomTabBar( Box( modifier = Modifier - .fillMaxWidth() - .windowInsetsPadding(safeInsets), + .fillMaxWidth(), ) { Surface( - modifier = Modifier.fillMaxWidth().offset(y = (-4).dp), + modifier = Modifier.fillMaxWidth(), color = Color.White.copy(alpha = 0.97f), shape = RoundedCornerShape(topStart = 24.dp, topEnd = 24.dp), border = BorderStroke(1.dp, mobileBorder), shadowElevation = 6.dp, ) { Row( - modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp, vertical = 10.dp), + modifier = + Modifier + .fillMaxWidth() + .windowInsetsPadding(safeInsets) + .padding(horizontal = 10.dp, vertical = 10.dp), horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically, ) { From 036e3e633eb3bdd1e6c22c35c2843cd739882370 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 14:01:06 +0530 Subject: [PATCH 008/160] fix(android): harden scanned setup code parsing --- .../android/ui/GatewayConfigResolver.kt | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt index c9b545e0ce99..4421a82be4b6 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/GatewayConfigResolver.kt @@ -5,9 +5,9 @@ import java.util.Base64 import java.util.Locale import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.contentOrNull import kotlinx.serialization.json.jsonObject -import kotlinx.serialization.json.jsonPrimitive internal data class GatewayEndpointConfig( val host: String, @@ -113,20 +113,8 @@ internal fun decodeGatewaySetupCode(rawInput: String): GatewaySetupCode? { } internal fun resolveScannedSetupCode(rawInput: String): String? { - val trimmed = rawInput.trim() - if (trimmed.isEmpty()) return null - - if (decodeGatewaySetupCode(trimmed) != null) { - return trimmed - } - - val obj = parseJsonObject(trimmed) ?: return null - val setupCode = jsonField(obj, "setupCode") ?: return null - return if (decodeGatewaySetupCode(setupCode) != null) { - setupCode - } else { - null - } + val setupCode = resolveSetupCodeCandidate(rawInput) ?: return null + return setupCode.takeIf { decodeGatewaySetupCode(it) != null } } internal fun composeGatewayManualUrl(hostInput: String, portInput: String, tls: Boolean): String? { @@ -141,7 +129,14 @@ private fun parseJsonObject(input: String): JsonObject? { return runCatching { gatewaySetupJson.parseToJsonElement(input).jsonObject }.getOrNull() } +private fun resolveSetupCodeCandidate(rawInput: String): String? { + val trimmed = rawInput.trim() + if (trimmed.isEmpty()) return null + val qrSetupCode = parseJsonObject(trimmed)?.let { jsonField(it, "setupCode") } + return qrSetupCode ?: trimmed +} + private fun jsonField(obj: JsonObject, key: String): String? { - val value = obj[key]?.jsonPrimitive?.contentOrNull?.trim().orEmpty() + val value = (obj[key] as? JsonPrimitive)?.contentOrNull?.trim().orEmpty() return value.ifEmpty { null } } From ed34129637bcbc2dbb53e715af5478a07b1451fa Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 14:01:10 +0530 Subject: [PATCH 009/160] test(android): cover non-string setupCode QR payload --- .../ai/openclaw/android/ui/GatewayConfigResolverTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt index 421f0b3ad6d5..7dc2dd1a239b 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/ui/GatewayConfigResolverTest.kt @@ -46,6 +46,13 @@ class GatewayConfigResolverTest { assertNull(resolved) } + @Test + fun resolveScannedSetupCodeRejectsJsonWithNonStringSetupCode() { + val qrJson = """{"setupCode":{"nested":"value"}}""" + val resolved = resolveScannedSetupCode(qrJson) + assertNull(resolved) + } + private fun encodeSetupCode(payloadJson: String): String { return Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.toByteArray(Charsets.UTF_8)) } From b3f46f0e2891621467061e4c24851882609b2cbd Mon Sep 17 00:00:00 2001 From: Nimrod Gutman Date: Wed, 25 Feb 2026 12:16:17 +0200 Subject: [PATCH 010/160] fix(test): stabilize low-mem parallel runner and cron session mock (#26324) * fix(test): stabilize low-mem parallel lane and cron session mock * feat(android): make QR scanning first-class onboarding * docs(android): update README for native Android workflow * fix(android): stabilize chat composer ime and tab layout * fix(android): stabilize chat ime insets and tab bar * fix(android): remove tab bar gap above system nav * fix(android): harden scanned setup code parsing * test(android): cover non-string setupCode QR payload * fix(test): add changelog note for low-mem test runner (#26324) (thanks @ngutman) --------- Co-authored-by: Ayaan Zaidi --- CHANGELOG.md | 1 + scripts/test-parallel.mjs | 29 ++++++++++++------- .../isolated-agent/run.skill-filter.test.ts | 1 + 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eeac161fdf2..7106390f08ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai - Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. - Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. - Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. +- Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. ## 2026.2.24 diff --git a/scripts/test-parallel.mjs b/scripts/test-parallel.mjs index 0ec8d2fdc5f3..35afef83c3f8 100644 --- a/scripts/test-parallel.mjs +++ b/scripts/test-parallel.mjs @@ -88,14 +88,20 @@ const isCI = process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true"; const isMacOS = process.platform === "darwin" || process.env.RUNNER_OS === "macOS"; const isWindows = process.platform === "win32" || process.env.RUNNER_OS === "Windows"; const isWindowsCi = isCI && isWindows; +const hostCpuCount = os.cpus().length; +const hostMemoryGiB = Math.floor(os.totalmem() / 1024 ** 3); +// Keep aggressive local defaults for high-memory workstations (Mac Studio class). +const highMemLocalHost = !isCI && hostMemoryGiB >= 96; +const lowMemLocalHost = !isCI && hostMemoryGiB < 64; const nodeMajor = Number.parseInt(process.versions.node.split(".")[0] ?? "", 10); // vmForks is a big win for transform/import heavy suites, but Node 24 had -// regressions with Vitest's vm runtime in this repo. Keep it opt-out via +// regressions with Vitest's vm runtime in this repo, and low-memory local hosts +// are more likely to hit per-worker V8 heap ceilings. Keep it opt-out via // OPENCLAW_TEST_VM_FORKS=0, and let users force-enable with =1. const supportsVmForks = Number.isFinite(nodeMajor) ? nodeMajor !== 24 : true; const useVmForks = process.env.OPENCLAW_TEST_VM_FORKS === "1" || - (process.env.OPENCLAW_TEST_VM_FORKS !== "0" && !isWindows && supportsVmForks); + (process.env.OPENCLAW_TEST_VM_FORKS !== "0" && !isWindows && supportsVmForks && !lowMemLocalHost); const disableIsolation = process.env.OPENCLAW_TEST_NO_ISOLATE === "1"; const runs = [ ...(useVmForks @@ -176,11 +182,6 @@ const testProfile = const overrideWorkers = Number.parseInt(process.env.OPENCLAW_TEST_WORKERS ?? "", 10); const resolvedOverride = Number.isFinite(overrideWorkers) && overrideWorkers > 0 ? overrideWorkers : null; -const hostCpuCount = os.cpus().length; -const hostMemoryGiB = Math.floor(os.totalmem() / 1024 ** 3); -// Keep aggressive local defaults for high-memory workstations (Mac Studio class). -const highMemLocalHost = !isCI && hostMemoryGiB >= 96; -const lowMemLocalHost = !isCI && hostMemoryGiB < 64; const parallelGatewayEnabled = process.env.OPENCLAW_TEST_PARALLEL_GATEWAY === "1" || (!isCI && highMemLocalHost); // Keep gateway serial by default except when explicitly requested or on high-memory local hosts. @@ -206,7 +207,7 @@ const defaultWorkerBudget = ? { unit: 2, unitIsolated: 1, - extensions: 1, + extensions: 4, gateway: 1, } : testProfile === "serial" @@ -236,7 +237,7 @@ const defaultWorkerBudget = // Sub-64 GiB local hosts are prone to OOM with large vmFork runs. unit: 2, unitIsolated: 1, - extensions: 1, + extensions: 4, gateway: 1, } : { @@ -335,9 +336,15 @@ const runOnce = (entry, extraArgs = []) => new Promise((resolve) => { const maxWorkers = maxWorkersForRun(entry.name); const reporterArgs = buildReporterArgs(entry, extraArgs); + // vmForks with a single worker has shown cross-file leakage in extension suites. + // Fall back to process forks when we intentionally clamp that lane to one worker. + const entryArgs = + entry.name === "extensions" && maxWorkers === 1 && entry.args.includes("--pool=vmForks") + ? entry.args.map((arg) => (arg === "--pool=vmForks" ? "--pool=forks" : arg)) + : entry.args; const args = maxWorkers ? [ - ...entry.args, + ...entryArgs, "--maxWorkers", String(maxWorkers), ...silentArgs, @@ -345,7 +352,7 @@ const runOnce = (entry, extraArgs = []) => ...windowsCiArgs, ...extraArgs, ] - : [...entry.args, ...silentArgs, ...reporterArgs, ...windowsCiArgs, ...extraArgs]; + : [...entryArgs, ...silentArgs, ...reporterArgs, ...windowsCiArgs, ...extraArgs]; const nodeOptions = process.env.NODE_OPTIONS ?? ""; const nextNodeOptions = WARNING_SUPPRESSION_FLAGS.reduce( (acc, flag) => (acc.includes(flag) ? acc : `${acc} ${flag}`.trim()), diff --git a/src/cron/isolated-agent/run.skill-filter.test.ts b/src/cron/isolated-agent/run.skill-filter.test.ts index f1f5ac9d6931..02d986819d98 100644 --- a/src/cron/isolated-agent/run.skill-filter.test.ts +++ b/src/cron/isolated-agent/run.skill-filter.test.ts @@ -112,6 +112,7 @@ vi.mock("../../cli/outbound-send-deps.js", () => ({ vi.mock("../../config/sessions.js", () => ({ resolveAgentMainSessionKey: vi.fn().mockReturnValue("main:default"), resolveSessionTranscriptPath: vi.fn().mockReturnValue("/tmp/transcript.jsonl"), + setSessionRuntimeModel: vi.fn(), updateSessionStore: vi.fn().mockResolvedValue(undefined), })); From 97eb5542e816cb76fb38f07abfb87693159ca55c Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 25 Feb 2026 09:03:25 +0000 Subject: [PATCH 011/160] fix(typing): guard fireStart against post-close invocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing `closed` flag in `createTypingCallbacks` guards `onReplyStart` but not `fireStart` itself. If a keepalive tick is already in-flight when `fireStop` sets `closed = true` and calls `keepaliveLoop.stop()`, the running `onTick → fireStart` callback still completes and sends a stale `sendChatAction('typing')` after the reply message has been delivered. On Telegram (which has no cancel-typing API), this causes the typing indicator to linger ~5 seconds after the bot's message appears. Add a `closed` early-return in `fireStart` as defense-in-depth so that even an in-flight tick is suppressed once cleanup has started. --- src/channels/typing.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/channels/typing.ts b/src/channels/typing.ts index 531c2fdc485b..a7e5c4a35d32 100644 --- a/src/channels/typing.ts +++ b/src/channels/typing.ts @@ -20,6 +20,7 @@ export function createTypingCallbacks(params: { let closed = false; const fireStart = async () => { + if (closed) return; try { await params.start(); } catch (err) { From ae658aa84c298f51ef490cdcc0dde29d788ec152 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 25 Feb 2026 11:21:46 +0000 Subject: [PATCH 012/160] style: add curly braces to satisfy eslint(curly) --- src/channels/typing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/channels/typing.ts b/src/channels/typing.ts index a7e5c4a35d32..beb983dbd38b 100644 --- a/src/channels/typing.ts +++ b/src/channels/typing.ts @@ -20,7 +20,7 @@ export function createTypingCallbacks(params: { let closed = false; const fireStart = async () => { - if (closed) return; + if (closed) { return; } try { await params.start(); } catch (err) { From a182afcf976e7456711f10c83789cae89ef4ea20 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 25 Feb 2026 11:25:02 +0000 Subject: [PATCH 013/160] style: expand curly braces per oxfmt --- src/channels/typing.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/channels/typing.ts b/src/channels/typing.ts index beb983dbd38b..c3dc765ff59e 100644 --- a/src/channels/typing.ts +++ b/src/channels/typing.ts @@ -20,7 +20,9 @@ export function createTypingCallbacks(params: { let closed = false; const fireStart = async () => { - if (closed) { return; } + if (closed) { + return; + } try { await params.start(); } catch (err) { From 3607b733cb439681c2d9c77e1b39d8ca0f99cff1 Mon Sep 17 00:00:00 2001 From: Nimrod Gutman Date: Wed, 25 Feb 2026 14:48:21 +0200 Subject: [PATCH 014/160] fix(changelog): add typing firestart guard note (#26325) (thanks @win4r) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7106390f08ea..6b3dacb2e263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai - Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. - Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. - Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. +- Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. From 90ddb3f271c8180760eac30cf0375c9a6edc7513 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 16:25:02 +0530 Subject: [PATCH 015/160] fix(android): stabilize gateway operator reconnect --- .../java/ai/openclaw/android/MainViewModel.kt | 4 --- .../java/ai/openclaw/android/NodeRuntime.kt | 30 +++++++++++-------- .../android/gateway/GatewaySession.kt | 26 ++++++---------- .../openclaw/android/ui/ConnectTabScreen.kt | 14 ++++----- 4 files changed, 31 insertions(+), 43 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt index 7076f09a292f..176032d1fc23 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt @@ -144,10 +144,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) { runtime.setTalkEnabled(enabled) } - fun logGatewayDebugSnapshot(source: String = "manual") { - runtime.logGatewayDebugSnapshot(source) - } - fun refreshGatewayConnection() { runtime.refreshGatewayConnection() } diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index 3e804ec8a076..da27d9dbdbda 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -328,13 +328,20 @@ class NodeRuntime(context: Context) { private fun updateStatus() { _isConnected.value = operatorConnected + val operator = operatorStatusText.trim() + val node = nodeStatusText.trim() _statusText.value = when { operatorConnected && _nodeConnected.value -> "Connected" operatorConnected && !_nodeConnected.value -> "Connected (node offline)" - !operatorConnected && _nodeConnected.value -> "Connected (operator offline)" - operatorStatusText.isNotBlank() && operatorStatusText != "Offline" -> operatorStatusText - else -> nodeStatusText + !operatorConnected && _nodeConnected.value -> + if (operator.isNotEmpty() && operator != "Offline") { + "Connected (operator: $operator)" + } else { + "Connected (operator offline)" + } + operator.isNotBlank() && operator != "Offline" -> operator + else -> node } } @@ -614,17 +621,14 @@ class NodeRuntime(context: Context) { prefs.setTalkEnabled(value) } - fun logGatewayDebugSnapshot(source: String = "manual") { - val flowToken = gatewayToken.value.trim() - val loadedToken = prefs.loadGatewayToken().orEmpty() - Log.i( - "OpenClawGatewayDebug", - "source=$source manualEnabled=${manualEnabled.value} host=${manualHost.value} port=${manualPort.value} tls=${manualTls.value} flowTokenLen=${flowToken.length} loadTokenLen=${loadedToken.length} connected=${isConnected.value} status=${statusText.value}", - ) - } - fun refreshGatewayConnection() { - val endpoint = connectedEndpoint ?: return + val endpoint = + connectedEndpoint ?: run { + _statusText.value = "Failed: no cached gateway endpoint" + return + } + operatorStatusText = "Connecting…" + updateStatus() val token = prefs.loadGatewayToken() val password = prefs.loadGatewayPassword() val tls = connectionManager.resolveTlsParams(endpoint) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt index 4e210de8fb99..92acf9689547 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt @@ -62,6 +62,11 @@ class GatewaySession( private val onInvoke: (suspend (InvokeRequest) -> InvokeResult)? = null, private val onTlsFingerprint: ((stableId: String, fingerprint: String) -> Unit)? = null, ) { + private companion object { + // Keep connect timeout above observed gateway unauthorized close on lower-end devices. + private const val CONNECT_RPC_TIMEOUT_MS = 12_000L + } + data class InvokeRequest( val id: String, val nodeId: String, @@ -302,26 +307,13 @@ class GatewaySession( val identity = identityStore.loadOrCreate() val storedToken = deviceAuthStore.loadToken(identity.deviceId, options.role) val trimmedToken = token?.trim().orEmpty() - val authToken = if (storedToken.isNullOrBlank()) trimmedToken else storedToken + // QR/setup/manual shared token must take precedence; stale role tokens can survive re-onboarding. + val authToken = if (trimmedToken.isNotBlank()) trimmedToken else storedToken.orEmpty() val payload = buildConnectParams(identity, connectNonce, authToken, password?.trim()) - var res = request("connect", payload, timeoutMs = 8_000) + val res = request("connect", payload, timeoutMs = CONNECT_RPC_TIMEOUT_MS) if (!res.ok) { val msg = res.error?.message ?: "connect failed" - val hasStoredToken = !storedToken.isNullOrBlank() - val canRetryWithShared = hasStoredToken && trimmedToken.isNotBlank() - if (canRetryWithShared) { - val sharedPayload = buildConnectParams(identity, connectNonce, trimmedToken, password?.trim()) - val sharedRes = request("connect", sharedPayload, timeoutMs = 8_000) - if (!sharedRes.ok) { - val retryMsg = sharedRes.error?.message ?: msg - throw IllegalStateException(retryMsg) - } - // Stored device token was bypassed successfully; clear stale token for future connects. - deviceAuthStore.clearToken(identity.deviceId, options.role) - res = sharedRes - } else { - throw IllegalStateException(msg) - } + throw IllegalStateException(msg) } handleConnectSuccess(res, identity.deviceId) connectDeferred.complete(Unit) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/ConnectTabScreen.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/ConnectTabScreen.kt index 9f7cf2211a16..875b82796d3e 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/ConnectTabScreen.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/ConnectTabScreen.kt @@ -168,6 +168,11 @@ fun ConnectTabScreen(viewModel: MainViewModel) { validationText = null return@Button } + if (statusText.contains("operator offline", ignoreCase = true)) { + validationText = null + viewModel.refreshGatewayConnection() + return@Button + } val config = resolveGatewayConnectConfig( @@ -397,15 +402,6 @@ fun ConnectTabScreen(viewModel: MainViewModel) { HorizontalDivider(color = mobileBorder) - Text( - "Debug snapshot: mode=${if (inputMode == ConnectInputMode.SetupCode) "setup" else "manual"}, manualEnabled=$manualEnabled, tokenLen=${gatewayToken.trim().length}", - style = mobileCaption1, - color = mobileTextSecondary, - ) - TextButton(onClick = { viewModel.logGatewayDebugSnapshot(source = "connect_tab") }) { - Text("Log gateway debug snapshot", style = mobileCallout.copy(fontWeight = FontWeight.SemiBold), color = mobileAccent) - } - TextButton(onClick = { viewModel.setOnboardingCompleted(false) }) { Text("Run onboarding again", style = mobileCallout.copy(fontWeight = FontWeight.SemiBold), color = mobileAccent) } From 3d29233babf9906a24a981afd5c1eec550a05b01 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 17:52:36 +0530 Subject: [PATCH 016/160] feat(android): add single-path mic capture runtime manager --- .../java/ai/openclaw/android/MainViewModel.kt | 31 +- .../openclaw/android/NodeForegroundService.kt | 20 +- .../java/ai/openclaw/android/NodeRuntime.kt | 170 +++----- .../android/voice/MicCaptureManager.kt | 362 ++++++++++++++++++ 4 files changed, 438 insertions(+), 145 deletions(-) create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt index 176032d1fc23..7276ad1eed83 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt @@ -45,14 +45,13 @@ class MainViewModel(app: Application) : AndroidViewModel(app) { val locationMode: StateFlow = runtime.locationMode val locationPreciseEnabled: StateFlow = runtime.locationPreciseEnabled val preventSleep: StateFlow = runtime.preventSleep - val wakeWords: StateFlow> = runtime.wakeWords - val voiceWakeMode: StateFlow = runtime.voiceWakeMode - val voiceWakeStatusText: StateFlow = runtime.voiceWakeStatusText - val voiceWakeIsListening: StateFlow = runtime.voiceWakeIsListening - val talkEnabled: StateFlow = runtime.talkEnabled - val talkStatusText: StateFlow = runtime.talkStatusText - val talkIsListening: StateFlow = runtime.talkIsListening - val talkIsSpeaking: StateFlow = runtime.talkIsSpeaking + val micEnabled: StateFlow = runtime.micEnabled + val micStatusText: StateFlow = runtime.micStatusText + val micLiveTranscript: StateFlow = runtime.micLiveTranscript + val micIsListening: StateFlow = runtime.micIsListening + val micQueuedMessages: StateFlow> = runtime.micQueuedMessages + val micInputLevel: StateFlow = runtime.micInputLevel + val micIsSending: StateFlow = runtime.micIsSending val manualEnabled: StateFlow = runtime.manualEnabled val manualHost: StateFlow = runtime.manualHost val manualPort: StateFlow = runtime.manualPort @@ -128,20 +127,8 @@ class MainViewModel(app: Application) : AndroidViewModel(app) { runtime.setCanvasDebugStatusEnabled(value) } - fun setWakeWords(words: List) { - runtime.setWakeWords(words) - } - - fun resetWakeWordsDefaults() { - runtime.resetWakeWordsDefaults() - } - - fun setVoiceWakeMode(mode: VoiceWakeMode) { - runtime.setVoiceWakeMode(mode) - } - - fun setTalkEnabled(enabled: Boolean) { - runtime.setTalkEnabled(enabled) + fun setMicEnabled(enabled: Boolean) { + runtime.setMicEnabled(enabled) } fun refreshGatewayConnection() { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeForegroundService.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeForegroundService.kt index ee7c8e006747..a6a79dc9c4a9 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeForegroundService.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeForegroundService.kt @@ -39,22 +39,22 @@ class NodeForegroundService : Service() { runtime.statusText, runtime.serverName, runtime.isConnected, - runtime.voiceWakeMode, - runtime.voiceWakeIsListening, - ) { status, server, connected, voiceMode, voiceListening -> - Quint(status, server, connected, voiceMode, voiceListening) - }.collect { (status, server, connected, voiceMode, voiceListening) -> + runtime.micEnabled, + runtime.micIsListening, + ) { status, server, connected, micEnabled, micListening -> + Quint(status, server, connected, micEnabled, micListening) + }.collect { (status, server, connected, micEnabled, micListening) -> val title = if (connected) "OpenClaw Node · Connected" else "OpenClaw Node" - val voiceSuffix = - if (voiceMode == VoiceWakeMode.Always) { - if (voiceListening) " · Voice Wake: Listening" else " · Voice Wake: Paused" + val micSuffix = + if (micEnabled) { + if (micListening) " · Mic: Listening" else " · Mic: Pending" } else { "" } - val text = (server?.let { "$status · $it" } ?: status) + voiceSuffix + val text = (server?.let { "$status · $it" } ?: status) + micSuffix val requiresMic = - voiceMode == VoiceWakeMode.Always && hasRecordAudioPermission() + micEnabled && hasRecordAudioPermission() startForegroundWithTypes( notification = buildNotification(title = title, text = text), requiresMic = requiresMic, diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index da27d9dbdbda..f6563bd4bf13 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -19,8 +19,7 @@ import ai.openclaw.android.gateway.GatewaySession import ai.openclaw.android.gateway.probeGatewayTlsFingerprint import ai.openclaw.android.node.* import ai.openclaw.android.protocol.OpenClawCanvasA2UIAction -import ai.openclaw.android.voice.TalkModeManager -import ai.openclaw.android.voice.VoiceWakeManager +import ai.openclaw.android.voice.MicCaptureManager import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -37,6 +36,7 @@ import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject +import java.util.UUID import java.util.concurrent.atomic.AtomicLong class NodeRuntime(context: Context) { @@ -54,40 +54,6 @@ class NodeRuntime(context: Context) { private val externalAudioCaptureActive = MutableStateFlow(false) - private val voiceWake: VoiceWakeManager by lazy { - VoiceWakeManager( - context = appContext, - scope = scope, - onCommand = { command -> - nodeSession.sendNodeEvent( - event = "agent.request", - payloadJson = - buildJsonObject { - put("message", JsonPrimitive(command)) - put("sessionKey", JsonPrimitive(resolveMainSessionKey())) - put("thinking", JsonPrimitive(chatThinkingLevel.value)) - put("deliver", JsonPrimitive(false)) - }.toString(), - ) - }, - ) - } - - val voiceWakeIsListening: StateFlow - get() = voiceWake.isListening - - val voiceWakeStatusText: StateFlow - get() = voiceWake.statusText - - val talkStatusText: StateFlow - get() = talkMode.statusText - - val talkIsListening: StateFlow - get() = talkMode.isListening - - val talkIsSpeaking: StateFlow - get() = talkMode.isSpeaking - private val discovery = GatewayDiscovery(appContext, scope = scope) val gateways: StateFlow> = discovery.gateways val discoveryStatusText: StateFlow = discovery.statusText @@ -146,7 +112,7 @@ class NodeRuntime(context: Context) { prefs = prefs, cameraEnabled = { cameraEnabled.value }, locationMode = { locationMode.value }, - voiceWakeMode = { voiceWakeMode.value }, + voiceWakeMode = { VoiceWakeMode.Off }, smsAvailable = { sms.canSendSms() }, hasRecordAudioPermission = { hasRecordAudioPermission() }, manualTls = { manualTls.value }, @@ -172,8 +138,6 @@ class NodeRuntime(context: Context) { onCanvasA2uiReset = { _canvasA2uiHydrated.value = false }, ) - private lateinit var gatewayEventHandler: GatewayEventHandler - data class GatewayTrustPrompt( val endpoint: GatewayEndpoint, val fingerprintSha256: String, @@ -242,8 +206,8 @@ class NodeRuntime(context: Context) { _seamColorArgb.value = DEFAULT_SEAM_COLOR_ARGB applyMainSessionKey(mainSessionKey) updateStatus() + micCapture.onGatewayConnectionChanged(true) scope.launch { refreshBrandingFromGateway() } - scope.launch { gatewayEventHandler.refreshWakeWordsFromGateway() } }, onDisconnected = { message -> operatorConnected = false @@ -254,11 +218,10 @@ class NodeRuntime(context: Context) { if (!isCanonicalMainSessionKey(_mainSessionKey.value)) { _mainSessionKey.value = "main" } - val mainKey = resolveMainSessionKey() - talkMode.setMainSessionKey(mainKey) - chat.applyMainSessionKey(mainKey) + chat.applyMainSessionKey(resolveMainSessionKey()) chat.onDisconnected(message) updateStatus() + micCapture.onGatewayConnectionChanged(false) }, onEvent = { event, payloadJson -> handleGatewayEvent(event, payloadJson) @@ -307,22 +270,52 @@ class NodeRuntime(context: Context) { json = json, supportsChatSubscribe = false, ) - private val talkMode: TalkModeManager by lazy { - TalkModeManager( + private val micCapture: MicCaptureManager by lazy { + MicCaptureManager( context = appContext, scope = scope, - session = operatorSession, - supportsChatSubscribe = false, - isConnected = { operatorConnected }, + sendToGateway = { message -> + val idempotencyKey = UUID.randomUUID().toString() + val params = + buildJsonObject { + put("sessionKey", JsonPrimitive(resolveMainSessionKey())) + put("message", JsonPrimitive(message)) + put("thinking", JsonPrimitive(chatThinkingLevel.value)) + put("timeoutMs", JsonPrimitive(30_000)) + put("idempotencyKey", JsonPrimitive(idempotencyKey)) + } + val response = operatorSession.request("chat.send", params.toString()) + parseChatSendRunId(response) ?: idempotencyKey + }, ) } + val micStatusText: StateFlow + get() = micCapture.statusText + + val micLiveTranscript: StateFlow + get() = micCapture.liveTranscript + + val micIsListening: StateFlow + get() = micCapture.isListening + + val micEnabled: StateFlow + get() = micCapture.micEnabled + + val micQueuedMessages: StateFlow> + get() = micCapture.queuedMessages + + val micInputLevel: StateFlow + get() = micCapture.inputLevel + + val micIsSending: StateFlow + get() = micCapture.isSending + private fun applyMainSessionKey(candidate: String?) { val trimmed = normalizeMainKey(candidate) ?: return if (isCanonicalMainSessionKey(_mainSessionKey.value)) return if (_mainSessionKey.value == trimmed) return _mainSessionKey.value = trimmed - talkMode.setMainSessionKey(trimmed) chat.applyMainSessionKey(trimmed) } @@ -424,9 +417,6 @@ class NodeRuntime(context: Context) { val locationMode: StateFlow = prefs.locationMode val locationPreciseEnabled: StateFlow = prefs.locationPreciseEnabled val preventSleep: StateFlow = prefs.preventSleep - val wakeWords: StateFlow> = prefs.wakeWords - val voiceWakeMode: StateFlow = prefs.voiceWakeMode - val talkEnabled: StateFlow = prefs.talkEnabled val manualEnabled: StateFlow = prefs.manualEnabled val manualHost: StateFlow = prefs.manualHost val manualPort: StateFlow = prefs.manualPort @@ -453,50 +443,13 @@ class NodeRuntime(context: Context) { val pendingRunCount: StateFlow = chat.pendingRunCount init { - gatewayEventHandler = GatewayEventHandler( - scope = scope, - prefs = prefs, - json = json, - operatorSession = operatorSession, - isConnected = { _isConnected.value }, - ) - - scope.launch { - combine( - voiceWakeMode, - isForeground, - externalAudioCaptureActive, - wakeWords, - ) { mode, foreground, externalAudio, words -> - Quad(mode, foreground, externalAudio, words) - }.distinctUntilChanged() - .collect { (mode, foreground, externalAudio, words) -> - voiceWake.setTriggerWords(words) - - val shouldListen = - when (mode) { - VoiceWakeMode.Off -> false - VoiceWakeMode.Foreground -> foreground - VoiceWakeMode.Always -> true - } && !externalAudio - - if (!shouldListen) { - voiceWake.stop(statusText = if (mode == VoiceWakeMode.Off) "Off" else "Paused") - return@collect - } - - if (!hasRecordAudioPermission()) { - voiceWake.stop(statusText = "Microphone permission required") - return@collect - } - - voiceWake.start() - } + if (prefs.voiceWakeMode.value != VoiceWakeMode.Off) { + prefs.setVoiceWakeMode(VoiceWakeMode.Off) } scope.launch { - talkEnabled.collect { enabled -> - talkMode.setEnabled(enabled) + prefs.talkEnabled.collect { enabled -> + micCapture.setMicEnabled(enabled) externalAudioCaptureActive.value = enabled } } @@ -604,20 +557,7 @@ class NodeRuntime(context: Context) { prefs.setCanvasDebugStatusEnabled(value) } - fun setWakeWords(words: List) { - prefs.setWakeWords(words) - gatewayEventHandler.scheduleWakeWordsSyncIfNeeded() - } - - fun resetWakeWordsDefaults() { - setWakeWords(SecurePrefs.defaultWakeWords) - } - - fun setVoiceWakeMode(mode: VoiceWakeMode) { - prefs.setVoiceWakeMode(mode) - } - - fun setTalkEnabled(value: Boolean) { + fun setMicEnabled(value: Boolean) { prefs.setTalkEnabled(value) } @@ -801,15 +741,19 @@ class NodeRuntime(context: Context) { } private fun handleGatewayEvent(event: String, payloadJson: String?) { - if (event == "voicewake.changed") { - gatewayEventHandler.handleVoiceWakeChangedEvent(payloadJson) - return - } - - talkMode.handleGatewayEvent(event, payloadJson) + micCapture.handleGatewayEvent(event, payloadJson) chat.handleGatewayEvent(event, payloadJson) } + private fun parseChatSendRunId(response: String): String? { + return try { + val root = json.parseToJsonElement(response).asObjectOrNull() ?: return null + root["runId"].asStringOrNull() + } catch (_: Throwable) { + null + } + } + private suspend fun refreshBrandingFromGateway() { if (!_isConnected.value) return try { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt new file mode 100644 index 000000000000..26f250013ef4 --- /dev/null +++ b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt @@ -0,0 +1,362 @@ +package ai.openclaw.android.voice + +import android.Manifest +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.speech.RecognitionListener +import android.speech.RecognizerIntent +import android.speech.SpeechRecognizer +import androidx.core.content.ContextCompat +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive + +class MicCaptureManager( + private val context: Context, + private val scope: CoroutineScope, + private val sendToGateway: suspend (String) -> String?, +) { + companion object { + private const val speechMinSessionMs = 30_000L + private const val speechCompleteSilenceMs = 1_500L + private const val speechPossibleSilenceMs = 900L + } + + private val mainHandler = Handler(Looper.getMainLooper()) + private val json = Json { ignoreUnknownKeys = true } + + private val _micEnabled = MutableStateFlow(false) + val micEnabled: StateFlow = _micEnabled + + private val _isListening = MutableStateFlow(false) + val isListening: StateFlow = _isListening + + private val _statusText = MutableStateFlow("Mic off") + val statusText: StateFlow = _statusText + + private val _liveTranscript = MutableStateFlow(null) + val liveTranscript: StateFlow = _liveTranscript + + private val _queuedMessages = MutableStateFlow>(emptyList()) + val queuedMessages: StateFlow> = _queuedMessages + + private val _inputLevel = MutableStateFlow(0f) + val inputLevel: StateFlow = _inputLevel + + private val _isSending = MutableStateFlow(false) + val isSending: StateFlow = _isSending + + private val messageQueue = ArrayDeque() + private val sessionSegments = mutableListOf() + private var lastFinalSegment: String? = null + private var pendingRunId: String? = null + private var gatewayConnected = false + + private var recognizer: SpeechRecognizer? = null + private var restartJob: Job? = null + private var stopRequested = false + + fun setMicEnabled(enabled: Boolean) { + if (_micEnabled.value == enabled) return + _micEnabled.value = enabled + if (enabled) { + start() + } else { + stop() + flushSessionToQueue() + sendQueuedIfIdle() + } + } + + fun onGatewayConnectionChanged(connected: Boolean) { + gatewayConnected = connected + if (connected) { + if (!_micEnabled.value) { + sendQueuedIfIdle() + } + return + } + if (!_micEnabled.value && messageQueue.isNotEmpty()) { + _statusText.value = "Queued ${messageQueue.size} message(s) · waiting for gateway" + } + } + + fun handleGatewayEvent(event: String, payloadJson: String?) { + if (event != "chat") return + val runId = pendingRunId ?: return + if (payloadJson.isNullOrBlank()) return + val obj = + try { + json.parseToJsonElement(payloadJson).asObjectOrNull() + } catch (_: Throwable) { + null + } ?: return + val eventRunId = obj["runId"].asStringOrNull() ?: return + if (eventRunId != runId) return + val state = obj["state"].asStringOrNull() ?: return + if (state != "final") return + + if (messageQueue.isNotEmpty()) { + messageQueue.removeFirst() + publishQueue() + } + pendingRunId = null + _isSending.value = false + sendQueuedIfIdle() + } + + private fun start() { + stopRequested = false + if (!SpeechRecognizer.isRecognitionAvailable(context)) { + _statusText.value = "Speech recognizer unavailable" + _micEnabled.value = false + return + } + if (!hasMicPermission()) { + _statusText.value = "Microphone permission required" + _micEnabled.value = false + return + } + + mainHandler.post { + try { + if (recognizer == null) { + recognizer = SpeechRecognizer.createSpeechRecognizer(context).also { it.setRecognitionListener(listener) } + } + startListeningSession() + } catch (err: Throwable) { + _statusText.value = "Start failed: ${err.message ?: err::class.simpleName}" + _micEnabled.value = false + } + } + } + + private fun stop() { + stopRequested = true + restartJob?.cancel() + restartJob = null + _isListening.value = false + _statusText.value = if (_isSending.value) "Mic off · sending queued messages…" else "Mic off" + _inputLevel.value = 0f + mainHandler.post { + recognizer?.cancel() + recognizer?.destroy() + recognizer = null + } + } + + private fun startListeningSession() { + val r = recognizer ?: return + val intent = + Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { + putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) + putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true) + putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3) + putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.packageName) + putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, speechMinSessionMs) + putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, speechCompleteSilenceMs) + putExtra( + RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, + speechPossibleSilenceMs, + ) + } + _statusText.value = if (_isSending.value) "Listening · queueing while gateway replies" else "Listening" + _isListening.value = true + r.startListening(intent) + } + + private fun scheduleRestart(delayMs: Long = 350L) { + if (stopRequested) return + if (!_micEnabled.value) return + restartJob?.cancel() + restartJob = + scope.launch { + delay(delayMs) + mainHandler.post { + if (stopRequested || !_micEnabled.value) return@post + try { + startListeningSession() + } catch (_: Throwable) { + // onError will retry + } + } + } + } + + private fun publishQueue() { + _queuedMessages.value = messageQueue.toList() + } + + private fun flushSessionToQueue() { + val message = sessionSegments.joinToString("\n").trim() + sessionSegments.clear() + _liveTranscript.value = null + lastFinalSegment = null + if (message.isEmpty()) return + messageQueue.addLast(message) + publishQueue() + } + + private fun sendQueuedIfIdle() { + if (_micEnabled.value) return + if (_isSending.value) return + if (messageQueue.isEmpty()) { + _statusText.value = "Mic off" + return + } + if (!gatewayConnected) { + _statusText.value = "Queued ${messageQueue.size} message(s) · waiting for gateway" + return + } + val next = messageQueue.first() + _isSending.value = true + _statusText.value = "Sending ${messageQueue.size} queued message(s)…" + scope.launch { + try { + val runId = sendToGateway(next) + pendingRunId = runId + if (runId == null) { + if (messageQueue.isNotEmpty()) { + messageQueue.removeFirst() + publishQueue() + } + _isSending.value = false + sendQueuedIfIdle() + } + } catch (err: Throwable) { + _isSending.value = false + _statusText.value = + if (!gatewayConnected) { + "Queued ${messageQueue.size} message(s) · waiting for gateway" + } else { + "Send failed: ${err.message ?: err::class.simpleName}" + } + } + } + } + + private fun disableMic(status: String) { + stopRequested = true + restartJob?.cancel() + restartJob = null + _micEnabled.value = false + _isListening.value = false + _inputLevel.value = 0f + _statusText.value = status + mainHandler.post { + recognizer?.cancel() + recognizer?.destroy() + recognizer = null + } + } + + private fun onFinalTranscript(text: String) { + val trimmed = text.trim() + if (trimmed.isEmpty()) return + _liveTranscript.value = trimmed + if (lastFinalSegment == trimmed) return + lastFinalSegment = trimmed + sessionSegments.add(trimmed) + } + + private fun hasMicPermission(): Boolean { + return ( + ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED + ) + } + + private val listener = + object : RecognitionListener { + override fun onReadyForSpeech(params: Bundle?) { + _isListening.value = true + } + + override fun onBeginningOfSpeech() {} + + override fun onRmsChanged(rmsdB: Float) { + val level = ((rmsdB + 2f) / 12f).coerceIn(0f, 1f) + _inputLevel.value = level + } + + override fun onBufferReceived(buffer: ByteArray?) {} + + override fun onEndOfSpeech() { + _inputLevel.value = 0f + scheduleRestart() + } + + override fun onError(error: Int) { + if (stopRequested) return + _isListening.value = false + _inputLevel.value = 0f + val status = + when (error) { + SpeechRecognizer.ERROR_AUDIO -> "Audio error" + SpeechRecognizer.ERROR_CLIENT -> "Client error" + SpeechRecognizer.ERROR_NETWORK -> "Network error" + SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network timeout" + SpeechRecognizer.ERROR_NO_MATCH -> "Listening" + SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "Recognizer busy" + SpeechRecognizer.ERROR_SERVER -> "Server error" + SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "Listening" + SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS -> "Microphone permission required" + SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED -> "Language not supported on this device" + SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE -> "Language unavailable on this device" + SpeechRecognizer.ERROR_SERVER_DISCONNECTED -> "Speech service disconnected" + SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> "Speech requests limited; retrying" + else -> "Speech error ($error)" + } + _statusText.value = status + + if ( + error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS || + error == SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED || + error == SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE + ) { + disableMic(status) + return + } + val restartDelayMs = + when (error) { + SpeechRecognizer.ERROR_NO_MATCH, + SpeechRecognizer.ERROR_SPEECH_TIMEOUT, + -> 1_500L + SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> 2_500L + else -> 600L + } + scheduleRestart(delayMs = restartDelayMs) + } + + override fun onResults(results: Bundle?) { + val text = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).orEmpty().firstOrNull() + if (!text.isNullOrBlank()) onFinalTranscript(text) + scheduleRestart() + } + + override fun onPartialResults(partialResults: Bundle?) { + val text = partialResults?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).orEmpty().firstOrNull() + if (!text.isNullOrBlank()) { + _liveTranscript.value = text.trim() + } + } + + override fun onEvent(eventType: Int, params: Bundle?) {} + } +} + +private fun kotlinx.serialization.json.JsonElement?.asObjectOrNull(): JsonObject? = + this as? JsonObject + +private fun kotlinx.serialization.json.JsonElement?.asStringOrNull(): String? = + (this as? JsonPrimitive)?.takeIf { it.isString }?.content From 6798330c24cad83a3688379b43afca2bc602931f Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 17:52:39 +0530 Subject: [PATCH 017/160] feat(android): replace voice placeholder with mic transcript UI --- .../openclaw/android/ui/PostOnboardingTabs.kt | 21 +- .../ai/openclaw/android/ui/VoiceTabScreen.kt | 364 ++++++++++++++++++ 2 files changed, 365 insertions(+), 20 deletions(-) create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt index b0135734e4c1..1345d8e3cb9f 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/PostOnboardingTabs.kt @@ -117,7 +117,7 @@ fun PostOnboardingTabs(viewModel: MainViewModel, modifier: Modifier = Modifier) when (activeTab) { HomeTab.Connect -> ConnectTabScreen(viewModel = viewModel) HomeTab.Chat -> ChatSheet(viewModel = viewModel) - HomeTab.Voice -> ComingSoonTabScreen(label = "VOICE", title = "Coming soon", description = "Voice mode is coming soon.") + HomeTab.Voice -> VoiceTabScreen(viewModel = viewModel) HomeTab.Screen -> ScreenTabScreen(viewModel = viewModel) HomeTab.Settings -> SettingsSheet(viewModel = viewModel) } @@ -318,22 +318,3 @@ private fun BottomTabBar( } } } - -@Composable -private fun ComingSoonTabScreen( - label: String, - title: String, - description: String, -) { - Box(modifier = Modifier.fillMaxSize().padding(horizontal = 22.dp, vertical = 18.dp)) { - Column( - modifier = Modifier.align(Alignment.Center), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(10.dp), - ) { - Text(label, style = mobileCaption1.copy(fontWeight = FontWeight.Bold), color = mobileAccent) - Text(title, style = mobileTitle1, color = mobileText) - Text(description, style = mobileBody, color = mobileTextSecondary) - } - } -} diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt new file mode 100644 index 000000000000..d431bedcbc47 --- /dev/null +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt @@ -0,0 +1,364 @@ +package ai.openclaw.android.ui + +import android.Manifest +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.provider.Settings +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.animation.core.LinearEasing +import androidx.compose.animation.core.RepeatMode +import androidx.compose.animation.core.animateFloat +import androidx.compose.animation.core.infiniteRepeatable +import androidx.compose.animation.core.rememberInfiniteTransition +import androidx.compose.animation.core.tween +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.WindowInsetsSides +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.imePadding +import androidx.compose.foundation.layout.only +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.safeDrawing +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.windowInsetsPadding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.compose.LocalLifecycleOwner +import ai.openclaw.android.MainViewModel +import kotlin.math.PI +import kotlin.math.max +import kotlin.math.sin + +@Composable +fun VoiceTabScreen(viewModel: MainViewModel) { + val context = LocalContext.current + val lifecycleOwner = LocalLifecycleOwner.current + val activity = remember(context) { context.findActivity() } + val listState = rememberLazyListState() + + val isConnected by viewModel.isConnected.collectAsState() + val gatewayStatus by viewModel.statusText.collectAsState() + val micEnabled by viewModel.micEnabled.collectAsState() + val micStatusText by viewModel.micStatusText.collectAsState() + val liveTranscript by viewModel.micLiveTranscript.collectAsState() + val queuedMessages by viewModel.micQueuedMessages.collectAsState() + val micInputLevel by viewModel.micInputLevel.collectAsState() + val micIsSending by viewModel.micIsSending.collectAsState() + + var hasMicPermission by remember { mutableStateOf(context.hasRecordAudioPermission()) } + var pendingMicEnable by remember { mutableStateOf(false) } + + DisposableEffect(lifecycleOwner, context) { + val observer = + LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_RESUME) { + hasMicPermission = context.hasRecordAudioPermission() + } + } + lifecycleOwner.lifecycle.addObserver(observer) + onDispose { lifecycleOwner.lifecycle.removeObserver(observer) } + } + + val requestMicPermission = + rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> + hasMicPermission = granted + if (granted && pendingMicEnable) { + viewModel.setMicEnabled(true) + } + pendingMicEnable = false + } + + LazyColumn( + state = listState, + modifier = + Modifier + .fillMaxWidth() + .imePadding() + .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Bottom)), + contentPadding = PaddingValues(horizontal = 20.dp, vertical = 16.dp), + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + item { + Column(verticalArrangement = Arrangement.spacedBy(6.dp)) { + Text( + "VOICE", + style = mobileCaption1.copy(fontWeight = FontWeight.Bold, letterSpacing = 1.sp), + color = mobileAccent, + ) + Text("Mic capture", style = mobileTitle2, color = mobileText) + Text( + if (isConnected) { + "Mic on captures speech continuously. Mic off sends the full transcript queue." + } else { + "Gateway offline. Mic off will keep messages queued until reconnect." + }, + style = mobileCallout, + color = mobileTextSecondary, + ) + } + } + + item { + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(16.dp), + color = Color.White, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, + ) { + Text("Gateway", style = mobileCaption1, color = mobileTextSecondary) + Text(gatewayStatus, style = mobileCaption1, color = mobileText) + } + Text(micStatusText, style = mobileHeadline, color = mobileText) + Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + Button( + onClick = { + if (micEnabled) { + viewModel.setMicEnabled(false) + return@Button + } + if (hasMicPermission) { + viewModel.setMicEnabled(true) + } else { + pendingMicEnable = true + requestMicPermission.launch(Manifest.permission.RECORD_AUDIO) + } + }, + shape = RoundedCornerShape(12.dp), + colors = + ButtonDefaults.buttonColors( + containerColor = if (micEnabled) mobileDanger else mobileAccent, + contentColor = Color.White, + ), + ) { + Text( + if (micEnabled) "Mic off" else "Mic on", + style = mobileCallout.copy(fontWeight = FontWeight.Bold), + ) + } + if (!hasMicPermission) { + Button( + onClick = { openAppSettings(context) }, + shape = RoundedCornerShape(12.dp), + colors = + ButtonDefaults.buttonColors( + containerColor = mobileSurfaceStrong, + contentColor = mobileText, + ), + ) { + Text("Open settings", style = mobileCallout.copy(fontWeight = FontWeight.SemiBold)) + } + } + } + if (!hasMicPermission) { + val showRationale = + if (activity == null) { + false + } else { + ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO) + } + Text( + if (showRationale) { + "Microphone permission required to capture speech." + } else { + "Microphone is blocked. Enable it in app settings." + }, + style = mobileCallout, + color = mobileWarning, + ) + } + } + } + } + + item { + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(16.dp), + color = Color.White, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + ) { + Text("Mic waveform", style = mobileHeadline, color = mobileText) + MicWaveform(level = micInputLevel, active = micEnabled) + } + } + } + + item { + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(16.dp), + color = Color.White, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + ) { + Text("Live transcript", style = mobileHeadline, color = mobileText) + Text( + liveTranscript?.trim().takeUnless { it.isNullOrEmpty() } ?: "Waiting for speech…", + style = mobileCallout, + color = if (liveTranscript.isNullOrBlank()) mobileTextTertiary else mobileText, + ) + } + } + } + + item { + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(16.dp), + color = Color.White, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + ) { + Text("Queued messages", style = mobileHeadline, color = mobileText) + Text( + if (queuedMessages.isEmpty()) { + "No queued transcripts." + } else { + "${queuedMessages.size} queued${if (micIsSending) " · sending…" else ""}" + }, + style = mobileCallout, + color = mobileTextSecondary, + ) + if (queuedMessages.isNotEmpty()) { + HorizontalDivider(color = mobileBorder) + } + if (queuedMessages.isEmpty()) { + Text("Turn mic off to flush captured speech into the queue.", style = mobileCallout, color = mobileTextTertiary) + } else { + queuedMessages.forEachIndexed { index, item -> + Surface( + modifier = Modifier.fillMaxWidth().padding(bottom = if (index == queuedMessages.lastIndex) 0.dp else 8.dp), + shape = RoundedCornerShape(12.dp), + color = mobileSurface, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column(modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp, vertical = 8.dp)) { + Text("Message ${index + 1}", style = mobileCaption1, color = mobileTextSecondary) + Text(item, style = mobileCallout, color = mobileText) + } + } + } + } + } + } + } + + item { Spacer(modifier = Modifier.height(24.dp)) } + } +} + +@Composable +private fun MicWaveform(level: Float, active: Boolean) { + val transition = rememberInfiniteTransition(label = "wave") + val phase by + transition.animateFloat( + initialValue = 0f, + targetValue = 1f, + animationSpec = infiniteRepeatable(animation = tween(1_200, easing = LinearEasing), repeatMode = RepeatMode.Restart), + label = "wavePhase", + ) + val effective = if (active) level.coerceIn(0f, 1f) else 0f + val base = max(effective, if (active) 0.08f else 0f) + Row( + modifier = Modifier.fillMaxWidth().heightIn(min = 60.dp), + horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.CenterHorizontally), + verticalAlignment = Alignment.CenterVertically, + ) { + repeat(22) { index -> + val pulse = + if (!active) { + 0f + } else { + ((sin(((phase * 2f * PI) + (index * 0.5f)).toDouble()) + 1.0) * 0.5).toFloat() + } + val barHeight = 8.dp + (52.dp * (base * pulse)) + Box( + modifier = + Modifier + .width(6.dp) + .height(barHeight) + .background(if (active) mobileAccent else mobileBorderStrong, RoundedCornerShape(999.dp)), + ) + } + } +} + +private fun Context.hasRecordAudioPermission(): Boolean { + return ( + ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED + ) +} + +private fun Context.findActivity(): Activity? = + when (this) { + is Activity -> this + is ContextWrapper -> baseContext.findActivity() + else -> null + } + +private fun openAppSettings(context: Context) { + val intent = + Intent( + Settings.ACTION_APPLICATION_DETAILS_SETTINGS, + Uri.fromParts("package", context.packageName, null), + ) + context.startActivity(intent) +} From 73677f2707e1478859ee838f2352b84bbecfc94b Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 17:52:42 +0530 Subject: [PATCH 018/160] refactor(android): remove legacy voice wake controls from settings --- .../ai/openclaw/android/ui/OnboardingFlow.kt | 2 +- .../ai/openclaw/android/ui/SettingsSheet.kt | 181 +++++------------- 2 files changed, 47 insertions(+), 136 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt index d9f3ec44fa66..4c9e064e6afb 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/OnboardingFlow.kt @@ -1053,7 +1053,7 @@ private fun PermissionsStep( } PermissionToggleRow( title = "Microphone", - subtitle = "Talk mode + voice features", + subtitle = "Voice tab transcription", checked = enableMicrophone, granted = isPermissionGranted(context, Manifest.permission.RECORD_AUDIO), onCheckedChange = onMicrophoneChange, diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt index 2a6219578c70..00d55b7c74c9 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt @@ -9,7 +9,6 @@ import android.os.Build import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Box @@ -32,8 +31,6 @@ import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState -import androidx.compose.foundation.text.KeyboardActions -import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material3.Button @@ -48,7 +45,6 @@ import androidx.compose.material3.RadioButton import androidx.compose.material3.Switch import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -56,11 +52,8 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha -import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.graphics.Color -import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.sp @@ -69,8 +62,6 @@ import androidx.core.content.ContextCompat import ai.openclaw.android.BuildConfig import ai.openclaw.android.LocationMode import ai.openclaw.android.MainViewModel -import ai.openclaw.android.VoiceWakeMode -import ai.openclaw.android.WakeWords @Composable fun SettingsSheet(viewModel: MainViewModel) { @@ -81,16 +72,9 @@ fun SettingsSheet(viewModel: MainViewModel) { val locationMode by viewModel.locationMode.collectAsState() val locationPreciseEnabled by viewModel.locationPreciseEnabled.collectAsState() val preventSleep by viewModel.preventSleep.collectAsState() - val wakeWords by viewModel.wakeWords.collectAsState() - val voiceWakeMode by viewModel.voiceWakeMode.collectAsState() - val voiceWakeStatusText by viewModel.voiceWakeStatusText.collectAsState() - val isConnected by viewModel.isConnected.collectAsState() val canvasDebugStatusEnabled by viewModel.canvasDebugStatusEnabled.collectAsState() val listState = rememberLazyListState() - val (wakeWordsText, setWakeWordsText) = remember { mutableStateOf("") } - val focusManager = LocalFocusManager.current - var wakeWordsHadFocus by remember { mutableStateOf(false) } val deviceModel = remember { listOfNotNull(Build.MANUFACTURER, Build.MODEL) @@ -116,14 +100,6 @@ fun SettingsSheet(viewModel: MainViewModel) { leadingIconColor = mobileTextSecondary, ) - LaunchedEffect(wakeWords) { setWakeWordsText(wakeWords.joinToString(", ")) } - val commitWakeWords = { - val parsed = WakeWords.parseIfChanged(wakeWordsText, wakeWords) - if (parsed != null) { - viewModel.setWakeWords(parsed) - } - } - val permissionLauncher = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { perms -> val cameraOk = perms[Manifest.permission.CAMERA] == true @@ -165,9 +141,16 @@ fun SettingsSheet(viewModel: MainViewModel) { } } + var micPermissionGranted by + remember { + mutableStateOf( + ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED, + ) + } val audioPermissionLauncher = - rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { _ -> - // Status text is handled by NodeRuntime. + rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> + micPermissionGranted = granted } val smsPermissionAvailable = @@ -302,7 +285,7 @@ fun SettingsSheet(viewModel: MainViewModel) { item { HorizontalDivider(color = mobileBorder) } - // Voice + // Voice item { Text( "VOICE", @@ -310,120 +293,48 @@ fun SettingsSheet(viewModel: MainViewModel) { color = mobileAccent, ) } - item { - val enabled = voiceWakeMode != VoiceWakeMode.Off - ListItem( - modifier = settingsRowModifier(), - colors = listItemColors, - headlineContent = { Text("Voice Wake", style = mobileHeadline) }, - supportingContent = { Text(voiceWakeStatusText, style = mobileCallout) }, - trailingContent = { - Switch( - checked = enabled, - onCheckedChange = { on -> - if (on) { - val micOk = - ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == - PackageManager.PERMISSION_GRANTED - if (!micOk) audioPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) - viewModel.setVoiceWakeMode(VoiceWakeMode.Foreground) + item { + ListItem( + modifier = settingsRowModifier(), + colors = listItemColors, + headlineContent = { Text("Microphone permission", style = mobileHeadline) }, + supportingContent = { + Text( + if (micPermissionGranted) { + "Granted. Use the Voice tab mic button to capture transcript." } else { - viewModel.setVoiceWakeMode(VoiceWakeMode.Off) - } - }, - ) - }, - ) - } - item { - AnimatedVisibility(visible = voiceWakeMode != VoiceWakeMode.Off) { - Column(verticalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth()) { - ListItem( - modifier = settingsRowModifier(), - colors = listItemColors, - headlineContent = { Text("Foreground Only", style = mobileHeadline) }, - supportingContent = { Text("Listens only while OpenClaw is open.", style = mobileCallout) }, - trailingContent = { - RadioButton( - selected = voiceWakeMode == VoiceWakeMode.Foreground, - onClick = { - val micOk = - ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == - PackageManager.PERMISSION_GRANTED - if (!micOk) audioPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) - viewModel.setVoiceWakeMode(VoiceWakeMode.Foreground) - }, - ) - }, - ) - ListItem( - modifier = settingsRowModifier(), - colors = listItemColors, - headlineContent = { Text("Always", style = mobileHeadline) }, - supportingContent = { Text("Keeps listening in the background (shows a persistent notification).", style = mobileCallout) }, - trailingContent = { - RadioButton( - selected = voiceWakeMode == VoiceWakeMode.Always, - onClick = { - val micOk = - ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == - PackageManager.PERMISSION_GRANTED - if (!micOk) audioPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) - viewModel.setVoiceWakeMode(VoiceWakeMode.Always) - }, + "Required for Voice tab transcription." + }, + style = mobileCallout, + ) + }, + trailingContent = { + Button( + onClick = { + if (micPermissionGranted) { + openAppSettings(context) + } else { + audioPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) + } + }, + colors = settingsPrimaryButtonColors(), + shape = RoundedCornerShape(14.dp), + ) { + Text( + if (micPermissionGranted) "Manage" else "Grant", + style = mobileCallout.copy(fontWeight = FontWeight.Bold), ) - }, - ) - } - } - } - item { - OutlinedTextField( - value = wakeWordsText, - onValueChange = setWakeWordsText, - label = { Text("Wake Words (comma-separated)", style = mobileCaption1, color = mobileTextSecondary) }, - modifier = - Modifier.fillMaxWidth().onFocusChanged { focusState -> - if (focusState.isFocused) { - wakeWordsHadFocus = true - } else if (wakeWordsHadFocus) { - wakeWordsHadFocus = false - commitWakeWords() } }, - singleLine = true, - keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), - keyboardActions = - KeyboardActions( - onDone = { - commitWakeWords() - focusManager.clearFocus() - }, - ), - textStyle = mobileBody.copy(color = mobileText), - colors = settingsTextFieldColors(), - ) - } + ) + } item { - Button( - onClick = viewModel::resetWakeWordsDefaults, - colors = settingsPrimaryButtonColors(), - shape = RoundedCornerShape(14.dp), - ) { - Text("Reset defaults", style = mobileCallout.copy(fontWeight = FontWeight.Bold)) - } + Text( + "Voice wake and talk modes were removed. Voice now uses one mic on/off flow in the Voice tab.", + style = mobileCallout, + color = mobileTextSecondary, + ) } - item { - Text( - if (isConnected) { - "Any node can edit wake words. Changes sync via the gateway." - } else { - "Connect to a gateway to sync wake words globally." - }, - style = mobileCallout, - color = mobileTextSecondary, - ) - } item { HorizontalDivider(color = mobileBorder) } From 434dc46531a50a9ec0ca3a781fb680627a5f04dd Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:08:20 +0530 Subject: [PATCH 019/160] feat(android): stream voice turns from mic manager events --- .../android/voice/MicCaptureManager.kt | 234 ++++++++++++++---- 1 file changed, 182 insertions(+), 52 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt index 26f250013ef4..603cd3d324be 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt @@ -11,6 +11,7 @@ import android.speech.RecognitionListener import android.speech.RecognizerIntent import android.speech.SpeechRecognizer import androidx.core.content.ContextCompat +import java.util.UUID import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.delay @@ -18,9 +19,22 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive +enum class VoiceConversationRole { + User, + Assistant, +} + +data class VoiceConversationEntry( + val id: String, + val role: VoiceConversationRole, + val text: String, + val isStreaming: Boolean = false, +) + class MicCaptureManager( private val context: Context, private val scope: CoroutineScope, @@ -30,8 +44,13 @@ class MicCaptureManager( private const val speechMinSessionMs = 30_000L private const val speechCompleteSilenceMs = 1_500L private const val speechPossibleSilenceMs = 900L + private const val maxConversationEntries = 40 } + private data class QueuedUtterance( + val text: String, + ) + private val mainHandler = Handler(Looper.getMainLooper()) private val json = Json { ignoreUnknownKeys = true } @@ -50,16 +69,20 @@ class MicCaptureManager( private val _queuedMessages = MutableStateFlow>(emptyList()) val queuedMessages: StateFlow> = _queuedMessages + private val _conversation = MutableStateFlow>(emptyList()) + val conversation: StateFlow> = _conversation + private val _inputLevel = MutableStateFlow(0f) val inputLevel: StateFlow = _inputLevel private val _isSending = MutableStateFlow(false) val isSending: StateFlow = _isSending - private val messageQueue = ArrayDeque() + private val messageQueue = ArrayDeque() private val sessionSegments = mutableListOf() private var lastFinalSegment: String? = null private var pendingRunId: String? = null + private var pendingAssistantEntryId: String? = null private var gatewayConnected = false private var recognizer: SpeechRecognizer? = null @@ -71,6 +94,7 @@ class MicCaptureManager( _micEnabled.value = enabled if (enabled) { start() + sendQueuedIfIdle() } else { stop() flushSessionToQueue() @@ -81,38 +105,54 @@ class MicCaptureManager( fun onGatewayConnectionChanged(connected: Boolean) { gatewayConnected = connected if (connected) { - if (!_micEnabled.value) { - sendQueuedIfIdle() - } + sendQueuedIfIdle() return } - if (!_micEnabled.value && messageQueue.isNotEmpty()) { - _statusText.value = "Queued ${messageQueue.size} message(s) · waiting for gateway" + if (messageQueue.isNotEmpty()) { + _statusText.value = queuedWaitingStatus() } } fun handleGatewayEvent(event: String, payloadJson: String?) { if (event != "chat") return - val runId = pendingRunId ?: return if (payloadJson.isNullOrBlank()) return - val obj = + val payload = try { json.parseToJsonElement(payloadJson).asObjectOrNull() } catch (_: Throwable) { null } ?: return - val eventRunId = obj["runId"].asStringOrNull() ?: return + + val runId = pendingRunId ?: return + val eventRunId = payload["runId"].asStringOrNull() ?: return if (eventRunId != runId) return - val state = obj["state"].asStringOrNull() ?: return - if (state != "final") return - if (messageQueue.isNotEmpty()) { - messageQueue.removeFirst() - publishQueue() + when (payload["state"].asStringOrNull()) { + "delta" -> { + val deltaText = parseAssistantText(payload) + if (!deltaText.isNullOrBlank()) { + upsertPendingAssistant(text = deltaText.trim(), isStreaming = true) + } + } + "final" -> { + val finalText = parseAssistantText(payload)?.trim().orEmpty() + if (finalText.isNotEmpty()) { + upsertPendingAssistant(text = finalText, isStreaming = false) + } else if (pendingAssistantEntryId != null) { + updateConversationEntry(pendingAssistantEntryId!!, text = null, isStreaming = false) + } + completePendingTurn() + } + "error" -> { + val errorMessage = payload["errorMessage"].asStringOrNull()?.trim().orEmpty().ifEmpty { "Voice request failed" } + upsertPendingAssistant(text = errorMessage, isStreaming = false) + completePendingTurn() + } + "aborted" -> { + upsertPendingAssistant(text = "Response aborted", isStreaming = false) + completePendingTurn() + } } - pendingRunId = null - _isSending.value = false - sendQueuedIfIdle() } private fun start() { @@ -146,7 +186,7 @@ class MicCaptureManager( restartJob?.cancel() restartJob = null _isListening.value = false - _statusText.value = if (_isSending.value) "Mic off · sending queued messages…" else "Mic off" + _statusText.value = if (_isSending.value) "Mic off · sending…" else "Mic off" _inputLevel.value = 0f mainHandler.post { recognizer?.cancel() @@ -156,7 +196,7 @@ class MicCaptureManager( } private fun startListeningSession() { - val r = recognizer ?: return + val recognizerInstance = recognizer ?: return val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) @@ -170,12 +210,17 @@ class MicCaptureManager( speechPossibleSilenceMs, ) } - _statusText.value = if (_isSending.value) "Listening · queueing while gateway replies" else "Listening" + _statusText.value = + when { + _isSending.value -> "Listening · sending queued voice" + messageQueue.isNotEmpty() -> "Listening · ${messageQueue.size} queued" + else -> "Listening" + } _isListening.value = true - r.startListening(intent) + recognizerInstance.startListening(intent) } - private fun scheduleRestart(delayMs: Long = 350L) { + private fun scheduleRestart(delayMs: Long = 300L) { if (stopRequested) return if (!_micEnabled.value) return restartJob?.cancel() @@ -187,57 +232,68 @@ class MicCaptureManager( try { startListeningSession() } catch (_: Throwable) { - // onError will retry + // retry through onError } } } } - private fun publishQueue() { - _queuedMessages.value = messageQueue.toList() - } - private fun flushSessionToQueue() { - val message = sessionSegments.joinToString("\n").trim() + val message = sessionSegments.joinToString(" ").trim() sessionSegments.clear() _liveTranscript.value = null lastFinalSegment = null if (message.isEmpty()) return - messageQueue.addLast(message) + + appendConversation( + role = VoiceConversationRole.User, + text = message, + ) + messageQueue.addLast(QueuedUtterance(text = message)) publishQueue() } + private fun publishQueue() { + _queuedMessages.value = messageQueue.map { it.text } + } + private fun sendQueuedIfIdle() { - if (_micEnabled.value) return if (_isSending.value) return if (messageQueue.isEmpty()) { - _statusText.value = "Mic off" + if (_micEnabled.value) { + _statusText.value = "Listening" + } else { + _statusText.value = "Mic off" + } return } if (!gatewayConnected) { - _statusText.value = "Queued ${messageQueue.size} message(s) · waiting for gateway" + _statusText.value = queuedWaitingStatus() return } + val next = messageQueue.first() _isSending.value = true - _statusText.value = "Sending ${messageQueue.size} queued message(s)…" + _statusText.value = if (_micEnabled.value) "Listening · sending queued voice" else "Sending queued voice" + scope.launch { try { - val runId = sendToGateway(next) + val runId = sendToGateway(next.text) pendingRunId = runId if (runId == null) { - if (messageQueue.isNotEmpty()) { - messageQueue.removeFirst() - publishQueue() - } + messageQueue.removeFirst() + publishQueue() _isSending.value = false + pendingAssistantEntryId = null sendQueuedIfIdle() } } catch (err: Throwable) { _isSending.value = false + pendingRunId = null + pendingAssistantEntryId = null _statusText.value = if (!gatewayConnected) { - "Queued ${messageQueue.size} message(s) · waiting for gateway" + queuedWaitingStatus() } else { "Send failed: ${err.message ?: err::class.simpleName}" } @@ -245,6 +301,69 @@ class MicCaptureManager( } } + private fun completePendingTurn() { + if (messageQueue.isNotEmpty()) { + messageQueue.removeFirst() + publishQueue() + } + pendingRunId = null + pendingAssistantEntryId = null + _isSending.value = false + sendQueuedIfIdle() + } + + private fun queuedWaitingStatus(): String { + return "${messageQueue.size} queued · waiting for gateway" + } + + private fun appendConversation( + role: VoiceConversationRole, + text: String, + isStreaming: Boolean = false, + ): String { + val id = UUID.randomUUID().toString() + _conversation.value = + (_conversation.value + VoiceConversationEntry(id = id, role = role, text = text, isStreaming = isStreaming)) + .takeLast(maxConversationEntries) + return id + } + + private fun updateConversationEntry(id: String, text: String?, isStreaming: Boolean) { + val current = _conversation.value + _conversation.value = + current.map { entry -> + if (entry.id == id) { + val updatedText = text ?: entry.text + entry.copy(text = updatedText, isStreaming = isStreaming) + } else { + entry + } + } + } + + private fun upsertPendingAssistant(text: String, isStreaming: Boolean) { + val currentId = pendingAssistantEntryId + if (currentId == null) { + pendingAssistantEntryId = + appendConversation( + role = VoiceConversationRole.Assistant, + text = text, + isStreaming = isStreaming, + ) + return + } + updateConversationEntry(id = currentId, text = text, isStreaming = isStreaming) + } + + private fun onFinalTranscript(text: String) { + val trimmed = text.trim() + if (trimmed.isEmpty()) return + _liveTranscript.value = trimmed + if (lastFinalSegment == trimmed) return + lastFinalSegment = trimmed + sessionSegments.add(trimmed) + } + private fun disableMic(status: String) { stopRequested = true restartJob?.cancel() @@ -260,15 +379,6 @@ class MicCaptureManager( } } - private fun onFinalTranscript(text: String) { - val trimmed = text.trim() - if (trimmed.isEmpty()) return - _liveTranscript.value = trimmed - if (lastFinalSegment == trimmed) return - lastFinalSegment = trimmed - sessionSegments.add(trimmed) - } - private fun hasMicPermission(): Boolean { return ( ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == @@ -276,6 +386,21 @@ class MicCaptureManager( ) } + private fun parseAssistantText(payload: JsonObject): String? { + val message = payload["message"].asObjectOrNull() ?: return null + if (message["role"].asStringOrNull() != "assistant") return null + val content = message["content"] as? JsonArray ?: return null + + val parts = + content.mapNotNull { item -> + val obj = item.asObjectOrNull() ?: return@mapNotNull null + if (obj["type"].asStringOrNull() != "text") return@mapNotNull null + obj["text"].asStringOrNull()?.trim()?.takeIf { it.isNotEmpty() } + } + if (parts.isEmpty()) return null + return parts.joinToString("\n") + } + private val listener = object : RecognitionListener { override fun onReadyForSpeech(params: Bundle?) { @@ -321,17 +446,18 @@ class MicCaptureManager( if ( error == SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS || - error == SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED || - error == SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE + error == SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED || + error == SpeechRecognizer.ERROR_LANGUAGE_UNAVAILABLE ) { disableMic(status) return } + val restartDelayMs = when (error) { SpeechRecognizer.ERROR_NO_MATCH, SpeechRecognizer.ERROR_SPEECH_TIMEOUT, - -> 1_500L + -> 1_200L SpeechRecognizer.ERROR_TOO_MANY_REQUESTS -> 2_500L else -> 600L } @@ -340,7 +466,11 @@ class MicCaptureManager( override fun onResults(results: Bundle?) { val text = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).orEmpty().firstOrNull() - if (!text.isNullOrBlank()) onFinalTranscript(text) + if (!text.isNullOrBlank()) { + onFinalTranscript(text) + flushSessionToQueue() + sendQueuedIfIdle() + } scheduleRestart() } From f9c3fdba458ebf8aac9502df11afa50f5cd0b046 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:08:25 +0530 Subject: [PATCH 020/160] refactor(android): expose voice conversation state to viewmodel --- .../app/src/main/java/ai/openclaw/android/MainViewModel.kt | 2 ++ .../app/src/main/java/ai/openclaw/android/NodeRuntime.kt | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt index 7276ad1eed83..e0d68c77e69c 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainViewModel.kt @@ -8,6 +8,7 @@ import ai.openclaw.android.node.CameraCaptureManager import ai.openclaw.android.node.CanvasController import ai.openclaw.android.node.ScreenRecordManager import ai.openclaw.android.node.SmsManager +import ai.openclaw.android.voice.VoiceConversationEntry import kotlinx.coroutines.flow.StateFlow class MainViewModel(app: Application) : AndroidViewModel(app) { @@ -50,6 +51,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app) { val micLiveTranscript: StateFlow = runtime.micLiveTranscript val micIsListening: StateFlow = runtime.micIsListening val micQueuedMessages: StateFlow> = runtime.micQueuedMessages + val micConversation: StateFlow> = runtime.micConversation val micInputLevel: StateFlow = runtime.micInputLevel val micIsSending: StateFlow = runtime.micIsSending val manualEnabled: StateFlow = runtime.manualEnabled diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index f6563bd4bf13..e9c930135e12 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -20,6 +20,7 @@ import ai.openclaw.android.gateway.probeGatewayTlsFingerprint import ai.openclaw.android.node.* import ai.openclaw.android.protocol.OpenClawCanvasA2UIAction import ai.openclaw.android.voice.MicCaptureManager +import ai.openclaw.android.voice.VoiceConversationEntry import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -305,6 +306,9 @@ class NodeRuntime(context: Context) { val micQueuedMessages: StateFlow> get() = micCapture.queuedMessages + val micConversation: StateFlow> + get() = micCapture.conversation + val micInputLevel: StateFlow get() = micCapture.inputLevel From 10a1593e0c61cfa3242c9205e098ec17c872fca2 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:08:29 +0530 Subject: [PATCH 021/160] feat(android): redesign voice mode layout for full-height conversation --- .../ai/openclaw/android/ui/VoiceTabScreen.kt | 417 +++++++++++------- 1 file changed, 252 insertions(+), 165 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt index d431bedcbc47..9149a0f08864 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/VoiceTabScreen.kt @@ -23,9 +23,9 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.WindowInsetsSides +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn @@ -33,18 +33,25 @@ import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.only import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.safeDrawing +import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Mic +import androidx.compose.material.icons.filled.MicOff import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Icon import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -52,9 +59,11 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.app.ActivityCompat @@ -63,6 +72,8 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.compose.LocalLifecycleOwner import ai.openclaw.android.MainViewModel +import ai.openclaw.android.voice.VoiceConversationEntry +import ai.openclaw.android.voice.VoiceConversationRole import kotlin.math.PI import kotlin.math.max import kotlin.math.sin @@ -78,11 +89,15 @@ fun VoiceTabScreen(viewModel: MainViewModel) { val gatewayStatus by viewModel.statusText.collectAsState() val micEnabled by viewModel.micEnabled.collectAsState() val micStatusText by viewModel.micStatusText.collectAsState() - val liveTranscript by viewModel.micLiveTranscript.collectAsState() - val queuedMessages by viewModel.micQueuedMessages.collectAsState() + val micLiveTranscript by viewModel.micLiveTranscript.collectAsState() + val micQueuedMessages by viewModel.micQueuedMessages.collectAsState() + val micConversation by viewModel.micConversation.collectAsState() val micInputLevel by viewModel.micInputLevel.collectAsState() val micIsSending by viewModel.micIsSending.collectAsState() + val hasStreamingAssistant = micConversation.any { it.role == VoiceConversationRole.Assistant && it.isStreaming } + val showThinkingBubble = micIsSending && !hasStreamingAssistant + var hasMicPermission by remember { mutableStateOf(context.hasRecordAudioPermission()) } var pendingMicEnable by remember { mutableStateOf(false) } @@ -106,233 +121,305 @@ fun VoiceTabScreen(viewModel: MainViewModel) { pendingMicEnable = false } - LazyColumn( - state = listState, + LaunchedEffect(micConversation.size, showThinkingBubble) { + val total = micConversation.size + if (showThinkingBubble) 1 else 0 + if (total > 0) { + listState.animateScrollToItem(total - 1) + } + } + + Column( modifier = Modifier - .fillMaxWidth() + .fillMaxSize() + .background(mobileBackgroundGradient) .imePadding() - .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Bottom)), - contentPadding = PaddingValues(horizontal = 20.dp, vertical = 16.dp), + .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Bottom)) + .padding(horizontal = 20.dp, vertical = 14.dp), verticalArrangement = Arrangement.spacedBy(10.dp), ) { - item { - Column(verticalArrangement = Arrangement.spacedBy(6.dp)) { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, + ) { + Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { Text( "VOICE", style = mobileCaption1.copy(fontWeight = FontWeight.Bold, letterSpacing = 1.sp), color = mobileAccent, ) - Text("Mic capture", style = mobileTitle2, color = mobileText) + Text("Voice mode", style = mobileTitle2, color = mobileText) + } + Surface( + shape = RoundedCornerShape(999.dp), + color = if (isConnected) mobileAccentSoft else mobileSurfaceStrong, + border = BorderStroke(1.dp, if (isConnected) mobileAccent.copy(alpha = 0.25f) else mobileBorderStrong), + ) { Text( - if (isConnected) { - "Mic on captures speech continuously. Mic off sends the full transcript queue." - } else { - "Gateway offline. Mic off will keep messages queued until reconnect." - }, - style = mobileCallout, - color = mobileTextSecondary, + if (isConnected) "Connected" else "Offline", + modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp), + style = mobileCaption1, + color = if (isConnected) mobileAccent else mobileTextSecondary, ) } } - item { - Surface( - modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(16.dp), - color = Color.White, - border = BorderStroke(1.dp, mobileBorder), - ) { - Column( - modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), - verticalArrangement = Arrangement.spacedBy(10.dp), - ) { - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically, + LazyColumn( + state = listState, + modifier = Modifier.fillMaxWidth().weight(1f), + contentPadding = PaddingValues(vertical = 4.dp), + verticalArrangement = Arrangement.spacedBy(10.dp), + ) { + if (micConversation.isEmpty() && !showThinkingBubble) { + item { + Column( + modifier = Modifier.fillMaxWidth().padding(top = 12.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), ) { - Text("Gateway", style = mobileCaption1, color = mobileTextSecondary) - Text(gatewayStatus, style = mobileCaption1, color = mobileText) - } - Text(micStatusText, style = mobileHeadline, color = mobileText) - Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - Button( - onClick = { - if (micEnabled) { - viewModel.setMicEnabled(false) - return@Button - } - if (hasMicPermission) { - viewModel.setMicEnabled(true) - } else { - pendingMicEnable = true - requestMicPermission.launch(Manifest.permission.RECORD_AUDIO) - } - }, - shape = RoundedCornerShape(12.dp), - colors = - ButtonDefaults.buttonColors( - containerColor = if (micEnabled) mobileDanger else mobileAccent, - contentColor = Color.White, - ), - ) { - Text( - if (micEnabled) "Mic off" else "Mic on", - style = mobileCallout.copy(fontWeight = FontWeight.Bold), - ) - } - if (!hasMicPermission) { - Button( - onClick = { openAppSettings(context) }, - shape = RoundedCornerShape(12.dp), - colors = - ButtonDefaults.buttonColors( - containerColor = mobileSurfaceStrong, - contentColor = mobileText, - ), - ) { - Text("Open settings", style = mobileCallout.copy(fontWeight = FontWeight.SemiBold)) - } - } - } - if (!hasMicPermission) { - val showRationale = - if (activity == null) { - false - } else { - ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO) - } Text( - if (showRationale) { - "Microphone permission required to capture speech." - } else { - "Microphone is blocked. Enable it in app settings." - }, + "Tap the mic and speak. Each pause sends a turn automatically.", style = mobileCallout, - color = mobileWarning, + color = mobileTextSecondary, ) } } } - } - item { - Surface( - modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(16.dp), - color = Color.White, - border = BorderStroke(1.dp, mobileBorder), - ) { - Column( - modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), - verticalArrangement = Arrangement.spacedBy(8.dp), - ) { - Text("Mic waveform", style = mobileHeadline, color = mobileText) - MicWaveform(level = micInputLevel, active = micEnabled) + items(items = micConversation, key = { it.id }) { entry -> + VoiceTurnBubble(entry = entry) + } + + if (showThinkingBubble) { + item { + VoiceThinkingBubble() } } } - item { - Surface( - modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(16.dp), - color = Color.White, - border = BorderStroke(1.dp, mobileBorder), + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(20.dp), + color = Color.White, + border = BorderStroke(1.dp, mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(8.dp), ) { - Column( - modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), - verticalArrangement = Arrangement.spacedBy(6.dp), + Surface( + shape = RoundedCornerShape(999.dp), + color = mobileSurface, + border = BorderStroke(1.dp, mobileBorder), ) { - Text("Live transcript", style = mobileHeadline, color = mobileText) + val queueCount = micQueuedMessages.size + val stateText = + when { + queueCount > 0 -> "$queueCount queued" + micIsSending -> "Sending" + micEnabled -> "Listening" + else -> "Mic off" + } Text( - liveTranscript?.trim().takeUnless { it.isNullOrEmpty() } ?: "Waiting for speech…", - style = mobileCallout, - color = if (liveTranscript.isNullOrBlank()) mobileTextTertiary else mobileText, + "$gatewayStatus · $stateText", + modifier = Modifier.padding(horizontal = 12.dp, vertical = 7.dp), + style = mobileCaption1, + color = mobileTextSecondary, ) } - } - } - item { - Surface( - modifier = Modifier.fillMaxWidth(), - shape = RoundedCornerShape(16.dp), - color = Color.White, - border = BorderStroke(1.dp, mobileBorder), - ) { - Column( - modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp), - verticalArrangement = Arrangement.spacedBy(8.dp), + if (!micLiveTranscript.isNullOrBlank()) { + Surface( + modifier = Modifier.fillMaxWidth(), + shape = RoundedCornerShape(14.dp), + color = mobileAccentSoft, + border = BorderStroke(1.dp, mobileAccent.copy(alpha = 0.2f)), + ) { + Text( + micLiveTranscript!!.trim(), + modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp), + style = mobileCallout, + color = mobileText, + ) + } + } + + MicWaveform(level = micInputLevel, active = micEnabled) + + Button( + onClick = { + if (micEnabled) { + viewModel.setMicEnabled(false) + return@Button + } + if (hasMicPermission) { + viewModel.setMicEnabled(true) + } else { + pendingMicEnable = true + requestMicPermission.launch(Manifest.permission.RECORD_AUDIO) + } + }, + shape = CircleShape, + contentPadding = PaddingValues(0.dp), + modifier = Modifier.size(86.dp), + colors = + ButtonDefaults.buttonColors( + containerColor = if (micEnabled) mobileDanger else mobileAccent, + contentColor = Color.White, + ), ) { - Text("Queued messages", style = mobileHeadline, color = mobileText) + Icon( + imageVector = if (micEnabled) Icons.Default.MicOff else Icons.Default.Mic, + contentDescription = if (micEnabled) "Turn microphone off" else "Turn microphone on", + modifier = Modifier.size(30.dp), + ) + } + + Text( + if (micEnabled) "Tap to stop" else "Tap to speak", + style = mobileCallout, + color = mobileTextSecondary, + ) + + if (!hasMicPermission) { + val showRationale = + if (activity == null) { + false + } else { + ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO) + } Text( - if (queuedMessages.isEmpty()) { - "No queued transcripts." + if (showRationale) { + "Microphone permission is required for voice mode." } else { - "${queuedMessages.size} queued${if (micIsSending) " · sending…" else ""}" + "Microphone blocked. Open app settings to enable it." }, - style = mobileCallout, - color = mobileTextSecondary, + style = mobileCaption1, + color = mobileWarning, + textAlign = TextAlign.Center, ) - if (queuedMessages.isNotEmpty()) { - HorizontalDivider(color = mobileBorder) - } - if (queuedMessages.isEmpty()) { - Text("Turn mic off to flush captured speech into the queue.", style = mobileCallout, color = mobileTextTertiary) - } else { - queuedMessages.forEachIndexed { index, item -> - Surface( - modifier = Modifier.fillMaxWidth().padding(bottom = if (index == queuedMessages.lastIndex) 0.dp else 8.dp), - shape = RoundedCornerShape(12.dp), - color = mobileSurface, - border = BorderStroke(1.dp, mobileBorder), - ) { - Column(modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp, vertical = 8.dp)) { - Text("Message ${index + 1}", style = mobileCaption1, color = mobileTextSecondary) - Text(item, style = mobileCallout, color = mobileText) - } - } - } + Button( + onClick = { openAppSettings(context) }, + shape = RoundedCornerShape(12.dp), + colors = ButtonDefaults.buttonColors(containerColor = mobileSurfaceStrong, contentColor = mobileText), + ) { + Text("Open settings", style = mobileCallout.copy(fontWeight = FontWeight.SemiBold)) } } + + Text( + micStatusText, + style = mobileCaption1, + color = mobileTextTertiary, + ) } } + } +} - item { Spacer(modifier = Modifier.height(24.dp)) } +@Composable +private fun VoiceTurnBubble(entry: VoiceConversationEntry) { + val isUser = entry.role == VoiceConversationRole.User + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = if (isUser) Arrangement.End else Arrangement.Start, + ) { + Surface( + modifier = Modifier.fillMaxWidth(0.90f), + shape = RoundedCornerShape(14.dp), + color = if (isUser) mobileAccentSoft else mobileSurface, + border = BorderStroke(1.dp, if (isUser) mobileAccent.copy(alpha = 0.2f) else mobileBorder), + ) { + Column( + modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 10.dp), + verticalArrangement = Arrangement.spacedBy(6.dp), + ) { + Text( + if (isUser) "You" else "OpenClaw", + style = mobileCaption1.copy(fontWeight = FontWeight.SemiBold), + color = mobileTextSecondary, + ) + Text( + if (entry.isStreaming && entry.text.isBlank()) "Listening response…" else entry.text, + style = mobileCallout, + color = mobileText, + ) + } + } + } +} + +@Composable +private fun VoiceThinkingBubble() { + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Start) { + Surface( + modifier = Modifier.fillMaxWidth(0.68f), + shape = RoundedCornerShape(14.dp), + color = mobileSurface, + border = BorderStroke(1.dp, mobileBorder), + ) { + Row( + modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp), + horizontalArrangement = Arrangement.spacedBy(8.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + ThinkingDots(color = mobileTextSecondary) + Text("OpenClaw is thinking…", style = mobileCallout, color = mobileTextSecondary) + } + } + } +} + +@Composable +private fun ThinkingDots(color: Color) { + Row(horizontalArrangement = Arrangement.spacedBy(5.dp), verticalAlignment = Alignment.CenterVertically) { + ThinkingDot(alpha = 0.38f, color = color) + ThinkingDot(alpha = 0.62f, color = color) + ThinkingDot(alpha = 0.90f, color = color) } } +@Composable +private fun ThinkingDot(alpha: Float, color: Color) { + Surface( + modifier = Modifier.size(6.dp).alpha(alpha), + shape = CircleShape, + color = color, + ) {} +} + @Composable private fun MicWaveform(level: Float, active: Boolean) { - val transition = rememberInfiniteTransition(label = "wave") + val transition = rememberInfiniteTransition(label = "voiceWave") val phase by transition.animateFloat( initialValue = 0f, targetValue = 1f, - animationSpec = infiniteRepeatable(animation = tween(1_200, easing = LinearEasing), repeatMode = RepeatMode.Restart), - label = "wavePhase", + animationSpec = infiniteRepeatable(animation = tween(1_000, easing = LinearEasing), repeatMode = RepeatMode.Restart), + label = "voiceWavePhase", ) + val effective = if (active) level.coerceIn(0f, 1f) else 0f - val base = max(effective, if (active) 0.08f else 0f) + val base = max(effective, if (active) 0.05f else 0f) + Row( - modifier = Modifier.fillMaxWidth().heightIn(min = 60.dp), + modifier = Modifier.fillMaxWidth().heightIn(min = 40.dp), horizontalArrangement = Arrangement.spacedBy(4.dp, Alignment.CenterHorizontally), verticalAlignment = Alignment.CenterVertically, ) { - repeat(22) { index -> + repeat(16) { index -> val pulse = if (!active) { 0f } else { - ((sin(((phase * 2f * PI) + (index * 0.5f)).toDouble()) + 1.0) * 0.5).toFloat() + ((sin(((phase * 2f * PI) + (index * 0.55f)).toDouble()) + 1.0) * 0.5).toFloat() } - val barHeight = 8.dp + (52.dp * (base * pulse)) + val barHeight = 6.dp + (24.dp * (base * pulse)) Box( modifier = Modifier - .width(6.dp) + .width(5.dp) .height(barHeight) .background(if (active) mobileAccent else mobileBorderStrong, RoundedCornerShape(999.dp)), ) From f729cc7b078f17ca4137e92a8d19d65a899bdeca Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:10:20 +0530 Subject: [PATCH 022/160] fix(android): stop auto canvas rehydrate on node connect --- .../android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index e9c930135e12..d36374b3256f 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -243,7 +243,6 @@ class NodeRuntime(context: Context) { _canvasRehydrateErrorText.value = null updateStatus() maybeNavigateToA2uiOnConnect() - requestCanvasRehydrate(source = "node_connect", force = false) }, onDisconnected = { message -> _nodeConnected.value = false From 285a0f48e57a3f04da39fa2829b2da6650c38b12 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:18:58 +0530 Subject: [PATCH 023/160] fix(android): sync mic manager on toggle --- .../app/src/main/java/ai/openclaw/android/NodeRuntime.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index d36374b3256f..15d99ffb9312 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -562,6 +562,8 @@ class NodeRuntime(context: Context) { fun setMicEnabled(value: Boolean) { prefs.setTalkEnabled(value) + micCapture.setMicEnabled(value) + externalAudioCaptureActive.value = value } fun refreshGatewayConnection() { From 2b7db53d0686d18ac5040995f2b84f2dafee3514 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:19:01 +0530 Subject: [PATCH 024/160] fix(android): recover stuck voice sends after missing finals --- .../android/voice/MicCaptureManager.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt index 603cd3d324be..c28e523a1821 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/voice/MicCaptureManager.kt @@ -45,6 +45,7 @@ class MicCaptureManager( private const val speechCompleteSilenceMs = 1_500L private const val speechPossibleSilenceMs = 900L private const val maxConversationEntries = 40 + private const val pendingRunTimeoutMs = 45_000L } private data class QueuedUtterance( @@ -87,6 +88,7 @@ class MicCaptureManager( private var recognizer: SpeechRecognizer? = null private var restartJob: Job? = null + private var pendingRunTimeoutJob: Job? = null private var stopRequested = false fun setMicEnabled(enabled: Boolean) { @@ -274,6 +276,8 @@ class MicCaptureManager( val next = messageQueue.first() _isSending.value = true + pendingRunTimeoutJob?.cancel() + pendingRunTimeoutJob = null _statusText.value = if (_micEnabled.value) "Listening · sending queued voice" else "Sending queued voice" scope.launch { @@ -281,13 +285,19 @@ class MicCaptureManager( val runId = sendToGateway(next.text) pendingRunId = runId if (runId == null) { + pendingRunTimeoutJob?.cancel() + pendingRunTimeoutJob = null messageQueue.removeFirst() publishQueue() _isSending.value = false pendingAssistantEntryId = null sendQueuedIfIdle() + } else { + armPendingRunTimeout(runId) } } catch (err: Throwable) { + pendingRunTimeoutJob?.cancel() + pendingRunTimeoutJob = null _isSending.value = false pendingRunId = null pendingAssistantEntryId = null @@ -301,7 +311,28 @@ class MicCaptureManager( } } + private fun armPendingRunTimeout(runId: String) { + pendingRunTimeoutJob?.cancel() + pendingRunTimeoutJob = + scope.launch { + delay(pendingRunTimeoutMs) + if (pendingRunId != runId) return@launch + pendingRunId = null + pendingAssistantEntryId = null + _isSending.value = false + _statusText.value = + if (gatewayConnected) { + "Voice reply timed out; retrying queued turn" + } else { + queuedWaitingStatus() + } + sendQueuedIfIdle() + } + } + private fun completePendingTurn() { + pendingRunTimeoutJob?.cancel() + pendingRunTimeoutJob = null if (messageQueue.isNotEmpty()) { messageQueue.removeFirst() publishQueue() From b12216af93c97c7eac947670d5ad38105d69f5dc Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 18:19:03 +0530 Subject: [PATCH 025/160] fix(android): refresh settings permissions on resume --- .../ai/openclaw/android/ui/SettingsSheet.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt b/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt index 00d55b7c74c9..6de3151a7f19 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/ui/SettingsSheet.kt @@ -45,6 +45,7 @@ import androidx.compose.material3.RadioButton import androidx.compose.material3.Switch import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -59,6 +60,9 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.dp import androidx.core.content.ContextCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.compose.LocalLifecycleOwner import ai.openclaw.android.BuildConfig import ai.openclaw.android.LocationMode import ai.openclaw.android.MainViewModel @@ -66,6 +70,7 @@ import ai.openclaw.android.MainViewModel @Composable fun SettingsSheet(viewModel: MainViewModel) { val context = LocalContext.current + val lifecycleOwner = LocalLifecycleOwner.current val instanceId by viewModel.instanceId.collectAsState() val displayName by viewModel.displayName.collectAsState() val cameraEnabled by viewModel.cameraEnabled.collectAsState() @@ -170,6 +175,22 @@ fun SettingsSheet(viewModel: MainViewModel) { viewModel.refreshGatewayConnection() } + DisposableEffect(lifecycleOwner, context) { + val observer = + LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_RESUME) { + micPermissionGranted = + ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == + PackageManager.PERMISSION_GRANTED + smsPermissionGranted = + ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) == + PackageManager.PERMISSION_GRANTED + } + } + lifecycleOwner.lifecycle.addObserver(observer) + onDispose { lifecycleOwner.lifecycle.removeObserver(observer) } + } + fun setCameraEnabledChecked(checked: Boolean) { if (!checked) { viewModel.setCameraEnabled(false) From 975c9f4b5457afbdf54c86ece31dcf80e4a585e0 Mon Sep 17 00:00:00 2001 From: Shadow Date: Wed, 25 Feb 2026 09:45:39 -0600 Subject: [PATCH 026/160] Agents: emphasize config.schema usage --- CHANGELOG.md | 1 + src/agents/system-prompt.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b3dacb2e263..6efa7d35cb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Docs: https://docs.openclaw.ai - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. +- Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. ### Fixes diff --git a/src/agents/system-prompt.ts b/src/agents/system-prompt.ts index d052daf5f7d9..c8b229a198a4 100644 --- a/src/agents/system-prompt.ts +++ b/src/agents/system-prompt.ts @@ -462,6 +462,7 @@ export function buildAgentSystemPrompt(params: { ? [ "Get Updates (self-update) is ONLY allowed when the user explicitly asks for it.", "Do not run config.apply or update.run unless the user explicitly requests an update or config change; if it's not explicit, ask first.", + "Use config.schema to fetch the current JSON Schema (includes plugins/channels) before making config changes or answering config-field questions; avoid guessing field names/types.", "Actions: config.get, config.schema, config.apply (validate + write full config, then restart), update.run (update deps or git, then restart).", "After restart, OpenClaw pings the last active session automatically.", ].join("\n") From 15240bdbfe05e6a34e570334248ec7e74498f5ba Mon Sep 17 00:00:00 2001 From: Bill Wang Date: Tue, 3 Feb 2026 23:16:56 +1100 Subject: [PATCH 027/160] feature/OPENCLAW_IMAGE --- docker-setup.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docker-setup.sh b/docker-setup.sh index 8c67dc0962d7..7f112c6053ed 100755 --- a/docker-setup.sh +++ b/docker-setup.sh @@ -247,12 +247,17 @@ upsert_env "$ENV_FILE" \ OPENCLAW_HOME_VOLUME \ OPENCLAW_DOCKER_APT_PACKAGES -echo "==> Building Docker image: $IMAGE_NAME" -docker build \ - --build-arg "OPENCLAW_DOCKER_APT_PACKAGES=${OPENCLAW_DOCKER_APT_PACKAGES}" \ - -t "$IMAGE_NAME" \ - -f "$ROOT_DIR/Dockerfile" \ - "$ROOT_DIR" +if [ "$IMAGE_NAME" == "openclaw:local" ]; then + echo "==> Building Docker image: $IMAGE_NAME" + docker build \ + --build-arg "OPENCLAW_DOCKER_APT_PACKAGES=${OPENCLAW_DOCKER_APT_PACKAGES}" \ + -t "$IMAGE_NAME" \ + -f "$ROOT_DIR/Dockerfile" \ + "$ROOT_DIR" +else + echo "==> Pulling Docker image: $IMAGE_NAME" + docker pull "$IMAGE_NAME" +fi echo "" echo "==> Onboarding (interactive)" From c7f88e85b7a3b72ac0039b9b884afe49092e0402 Mon Sep 17 00:00:00 2001 From: Bill Wang Date: Tue, 3 Feb 2026 23:27:34 +1100 Subject: [PATCH 028/160] feature/OPENCLAW_IMAGE --- docker-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-setup.sh b/docker-setup.sh index 7f112c6053ed..a9d79aac833c 100755 --- a/docker-setup.sh +++ b/docker-setup.sh @@ -247,7 +247,7 @@ upsert_env "$ENV_FILE" \ OPENCLAW_HOME_VOLUME \ OPENCLAW_DOCKER_APT_PACKAGES -if [ "$IMAGE_NAME" == "openclaw:local" ]; then +if [[ "$IMAGE_NAME" == "openclaw:local" ]]; then echo "==> Building Docker image: $IMAGE_NAME" docker build \ --build-arg "OPENCLAW_DOCKER_APT_PACKAGES=${OPENCLAW_DOCKER_APT_PACKAGES}" \ From 98292331d50949bfb880b341e4ebd1f275da0358 Mon Sep 17 00:00:00 2001 From: Bill Wang Date: Wed, 4 Feb 2026 10:43:30 +1100 Subject: [PATCH 029/160] Update docker-setup.sh Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- docker-setup.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docker-setup.sh b/docker-setup.sh index a9d79aac833c..f9b20949db0c 100755 --- a/docker-setup.sh +++ b/docker-setup.sh @@ -254,6 +254,17 @@ if [[ "$IMAGE_NAME" == "openclaw:local" ]]; then -t "$IMAGE_NAME" \ -f "$ROOT_DIR/Dockerfile" \ "$ROOT_DIR" +else + echo "==> Pulling Docker image: $IMAGE_NAME" + if ! docker pull "$IMAGE_NAME"; then + echo "ERROR: Failed to pull image $IMAGE_NAME. Please check the image name and your access permissions." >&2 + exit 1 + fi +fi + --build-arg "OPENCLAW_DOCKER_APT_PACKAGES=${OPENCLAW_DOCKER_APT_PACKAGES}" \ + -t "$IMAGE_NAME" \ + -f "$ROOT_DIR/Dockerfile" \ + "$ROOT_DIR" else echo "==> Pulling Docker image: $IMAGE_NAME" docker pull "$IMAGE_NAME" From a898acbd55c1ef267f1f4cf858d03ee76129d0f1 Mon Sep 17 00:00:00 2001 From: Bill Wang Date: Wed, 4 Feb 2026 10:48:28 +1100 Subject: [PATCH 030/160] feature/OPENCLAW_IMAGE --- docker-setup.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/docker-setup.sh b/docker-setup.sh index f9b20949db0c..c0cd925c4c3e 100755 --- a/docker-setup.sh +++ b/docker-setup.sh @@ -261,14 +261,6 @@ else exit 1 fi fi - --build-arg "OPENCLAW_DOCKER_APT_PACKAGES=${OPENCLAW_DOCKER_APT_PACKAGES}" \ - -t "$IMAGE_NAME" \ - -f "$ROOT_DIR/Dockerfile" \ - "$ROOT_DIR" -else - echo "==> Pulling Docker image: $IMAGE_NAME" - docker pull "$IMAGE_NAME" -fi echo "" echo "==> Onboarding (interactive)" From 8f3310000a8b0c11eced054c2cdb6fb27803511a Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:17:03 +0100 Subject: [PATCH 031/160] refactor(macos): remove anthropic oauth onboarding flow --- CHANGELOG.md | 1 + .../OpenClaw/AnthropicAuthControls.swift | 234 ----------- .../Sources/OpenClaw/AnthropicOAuth.swift | 383 ------------------ .../OpenClaw/AnthropicOAuthCodeState.swift | 59 --- apps/macos/Sources/OpenClaw/Onboarding.swift | 27 -- .../OpenClaw/OnboardingView+Actions.swift | 66 --- .../OpenClaw/OnboardingView+Layout.swift | 2 - .../OpenClaw/OnboardingView+Monitoring.swift | 78 ---- .../OpenClaw/OnboardingView+Pages.swift | 166 -------- .../OpenClaw/OnboardingView+Testing.swift | 9 - .../AnthropicAuthControlsSmokeTests.swift | 29 -- .../AnthropicAuthResolverTests.swift | 52 --- .../AnthropicOAuthCodeStateTests.swift | 31 -- .../OpenClawOAuthStoreTests.swift | 97 ----- docs/start/onboarding.md | 4 +- 15 files changed, 3 insertions(+), 1235 deletions(-) delete mode 100644 apps/macos/Sources/OpenClaw/AnthropicAuthControls.swift delete mode 100644 apps/macos/Sources/OpenClaw/AnthropicOAuth.swift delete mode 100644 apps/macos/Sources/OpenClaw/AnthropicOAuthCodeState.swift delete mode 100644 apps/macos/Tests/OpenClawIPCTests/AnthropicAuthControlsSmokeTests.swift delete mode 100644 apps/macos/Tests/OpenClawIPCTests/AnthropicAuthResolverTests.swift delete mode 100644 apps/macos/Tests/OpenClawIPCTests/AnthropicOAuthCodeStateTests.swift delete mode 100644 apps/macos/Tests/OpenClawIPCTests/OpenClawOAuthStoreTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 6efa7d35cb39..21d78689220b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai ### Changes +- macOS/Onboarding: remove Anthropic OAuth sign-in from the Mac onboarding UI and keep Anthropic subscription auth setup-token-only (legacy `oauth.json` OAuth onboarding path removed). - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. diff --git a/apps/macos/Sources/OpenClaw/AnthropicAuthControls.swift b/apps/macos/Sources/OpenClaw/AnthropicAuthControls.swift deleted file mode 100644 index 06f107d6c6e3..000000000000 --- a/apps/macos/Sources/OpenClaw/AnthropicAuthControls.swift +++ /dev/null @@ -1,234 +0,0 @@ -import AppKit -import Combine -import SwiftUI - -@MainActor -struct AnthropicAuthControls: View { - let connectionMode: AppState.ConnectionMode - - @State private var oauthStatus: OpenClawOAuthStore.AnthropicOAuthStatus = OpenClawOAuthStore.anthropicOAuthStatus() - @State private var pkce: AnthropicOAuth.PKCE? - @State private var code: String = "" - @State private var busy = false - @State private var statusText: String? - @State private var autoDetectClipboard = true - @State private var autoConnectClipboard = true - @State private var lastPasteboardChangeCount = NSPasteboard.general.changeCount - - private static let clipboardPoll: AnyPublisher = { - if ProcessInfo.processInfo.isRunningTests { - return Empty(completeImmediately: false).eraseToAnyPublisher() - } - return Timer.publish(every: 0.4, on: .main, in: .common) - .autoconnect() - .eraseToAnyPublisher() - }() - - var body: some View { - VStack(alignment: .leading, spacing: 10) { - if self.connectionMode != .local { - Text("Gateway isn’t running locally; OAuth must be created on the gateway host.") - .font(.footnote) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } - - HStack(spacing: 10) { - Circle() - .fill(self.oauthStatus.isConnected ? Color.green : Color.orange) - .frame(width: 8, height: 8) - Text(self.oauthStatus.shortDescription) - .font(.footnote.weight(.semibold)) - .foregroundStyle(.secondary) - Spacer() - Button("Reveal") { - NSWorkspace.shared.activateFileViewerSelecting([OpenClawOAuthStore.oauthURL()]) - } - .buttonStyle(.bordered) - .disabled(!FileManager().fileExists(atPath: OpenClawOAuthStore.oauthURL().path)) - - Button("Refresh") { - self.refresh() - } - .buttonStyle(.bordered) - } - - Text(OpenClawOAuthStore.oauthURL().path) - .font(.caption.monospaced()) - .foregroundStyle(.secondary) - .lineLimit(1) - .truncationMode(.middle) - .textSelection(.enabled) - - HStack(spacing: 12) { - Button { - self.startOAuth() - } label: { - if self.busy { - ProgressView().controlSize(.small) - } else { - Text(self.oauthStatus.isConnected ? "Re-auth (OAuth)" : "Open sign-in (OAuth)") - } - } - .buttonStyle(.borderedProminent) - .disabled(self.connectionMode != .local || self.busy) - - if self.pkce != nil { - Button("Cancel") { - self.pkce = nil - self.code = "" - self.statusText = nil - } - .buttonStyle(.bordered) - .disabled(self.busy) - } - } - - if self.pkce != nil { - VStack(alignment: .leading, spacing: 8) { - Text("Paste `code#state`") - .font(.footnote.weight(.semibold)) - .foregroundStyle(.secondary) - - TextField("code#state", text: self.$code) - .textFieldStyle(.roundedBorder) - .disabled(self.busy) - - Toggle("Auto-detect from clipboard", isOn: self.$autoDetectClipboard) - .font(.footnote) - .foregroundStyle(.secondary) - .disabled(self.busy) - - Toggle("Auto-connect when detected", isOn: self.$autoConnectClipboard) - .font(.footnote) - .foregroundStyle(.secondary) - .disabled(self.busy) - - Button("Connect") { - Task { await self.finishOAuth() } - } - .buttonStyle(.bordered) - .disabled(self.busy || self.connectionMode != .local || self.code - .trimmingCharacters(in: .whitespacesAndNewlines) - .isEmpty) - } - } - - if let statusText, !statusText.isEmpty { - Text(statusText) - .font(.footnote) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } - } - .onAppear { - self.refresh() - } - .onReceive(Self.clipboardPoll) { _ in - self.pollClipboardIfNeeded() - } - } - - private func refresh() { - let imported = OpenClawOAuthStore.importLegacyAnthropicOAuthIfNeeded() - self.oauthStatus = OpenClawOAuthStore.anthropicOAuthStatus() - if imported != nil { - self.statusText = "Imported existing OAuth credentials." - } - } - - private func startOAuth() { - guard self.connectionMode == .local else { return } - guard !self.busy else { return } - self.busy = true - defer { self.busy = false } - - do { - let pkce = try AnthropicOAuth.generatePKCE() - self.pkce = pkce - let url = AnthropicOAuth.buildAuthorizeURL(pkce: pkce) - NSWorkspace.shared.open(url) - self.statusText = "Browser opened. After approving, paste the `code#state` value here." - } catch { - self.statusText = "Failed to start OAuth: \(error.localizedDescription)" - } - } - - @MainActor - private func finishOAuth() async { - guard self.connectionMode == .local else { return } - guard !self.busy else { return } - guard let pkce = self.pkce else { return } - self.busy = true - defer { self.busy = false } - - guard let parsed = AnthropicOAuthCodeState.parse(from: self.code) else { - self.statusText = "OAuth failed: missing or invalid code/state." - return - } - - do { - let creds = try await AnthropicOAuth.exchangeCode( - code: parsed.code, - state: parsed.state, - verifier: pkce.verifier) - try OpenClawOAuthStore.saveAnthropicOAuth(creds) - self.refresh() - self.pkce = nil - self.code = "" - self.statusText = "Connected. OpenClaw can now use Claude via OAuth." - } catch { - self.statusText = "OAuth failed: \(error.localizedDescription)" - } - } - - private func pollClipboardIfNeeded() { - guard self.connectionMode == .local else { return } - guard self.pkce != nil else { return } - guard !self.busy else { return } - guard self.autoDetectClipboard else { return } - - let pb = NSPasteboard.general - let changeCount = pb.changeCount - guard changeCount != self.lastPasteboardChangeCount else { return } - self.lastPasteboardChangeCount = changeCount - - guard let raw = pb.string(forType: .string), !raw.isEmpty else { return } - guard let parsed = AnthropicOAuthCodeState.parse(from: raw) else { return } - guard let pkce = self.pkce, parsed.state == pkce.verifier else { return } - - let next = "\(parsed.code)#\(parsed.state)" - if self.code != next { - self.code = next - self.statusText = "Detected `code#state` from clipboard." - } - - guard self.autoConnectClipboard else { return } - Task { await self.finishOAuth() } - } -} - -#if DEBUG -extension AnthropicAuthControls { - init( - connectionMode: AppState.ConnectionMode, - oauthStatus: OpenClawOAuthStore.AnthropicOAuthStatus, - pkce: AnthropicOAuth.PKCE? = nil, - code: String = "", - busy: Bool = false, - statusText: String? = nil, - autoDetectClipboard: Bool = true, - autoConnectClipboard: Bool = true) - { - self.connectionMode = connectionMode - self._oauthStatus = State(initialValue: oauthStatus) - self._pkce = State(initialValue: pkce) - self._code = State(initialValue: code) - self._busy = State(initialValue: busy) - self._statusText = State(initialValue: statusText) - self._autoDetectClipboard = State(initialValue: autoDetectClipboard) - self._autoConnectClipboard = State(initialValue: autoConnectClipboard) - self._lastPasteboardChangeCount = State(initialValue: NSPasteboard.general.changeCount) - } -} -#endif diff --git a/apps/macos/Sources/OpenClaw/AnthropicOAuth.swift b/apps/macos/Sources/OpenClaw/AnthropicOAuth.swift deleted file mode 100644 index f594cc04c311..000000000000 --- a/apps/macos/Sources/OpenClaw/AnthropicOAuth.swift +++ /dev/null @@ -1,383 +0,0 @@ -import CryptoKit -import Foundation -import OSLog -import Security - -struct AnthropicOAuthCredentials: Codable { - let type: String - let refresh: String - let access: String - let expires: Int64 -} - -enum AnthropicAuthMode: Equatable { - case oauthFile - case oauthEnv - case apiKeyEnv - case missing - - var shortLabel: String { - switch self { - case .oauthFile: "OAuth (OpenClaw token file)" - case .oauthEnv: "OAuth (env var)" - case .apiKeyEnv: "API key (env var)" - case .missing: "Missing credentials" - } - } - - var isConfigured: Bool { - switch self { - case .missing: false - case .oauthFile, .oauthEnv, .apiKeyEnv: true - } - } -} - -enum AnthropicAuthResolver { - static func resolve( - environment: [String: String] = ProcessInfo.processInfo.environment, - oauthStatus: OpenClawOAuthStore.AnthropicOAuthStatus = OpenClawOAuthStore - .anthropicOAuthStatus()) -> AnthropicAuthMode - { - if oauthStatus.isConnected { return .oauthFile } - - if let token = environment["ANTHROPIC_OAUTH_TOKEN"]?.trimmingCharacters(in: .whitespacesAndNewlines), - !token.isEmpty - { - return .oauthEnv - } - - if let key = environment["ANTHROPIC_API_KEY"]?.trimmingCharacters(in: .whitespacesAndNewlines), - !key.isEmpty - { - return .apiKeyEnv - } - - return .missing - } -} - -enum AnthropicOAuth { - private static let logger = Logger(subsystem: "ai.openclaw", category: "anthropic-oauth") - - private static let clientId = "9d1c250a-e61b-44d9-88ed-5944d1962f5e" - private static let authorizeURL = URL(string: "https://claude.ai/oauth/authorize")! - private static let tokenURL = URL(string: "https://console.anthropic.com/v1/oauth/token")! - private static let redirectURI = "https://console.anthropic.com/oauth/code/callback" - private static let scopes = "org:create_api_key user:profile user:inference" - - struct PKCE { - let verifier: String - let challenge: String - } - - static func generatePKCE() throws -> PKCE { - var bytes = [UInt8](repeating: 0, count: 32) - let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes) - guard status == errSecSuccess else { - throw NSError(domain: NSOSStatusErrorDomain, code: Int(status)) - } - let verifier = Data(bytes).base64URLEncodedString() - let hash = SHA256.hash(data: Data(verifier.utf8)) - let challenge = Data(hash).base64URLEncodedString() - return PKCE(verifier: verifier, challenge: challenge) - } - - static func buildAuthorizeURL(pkce: PKCE) -> URL { - var components = URLComponents(url: self.authorizeURL, resolvingAgainstBaseURL: false)! - components.queryItems = [ - URLQueryItem(name: "code", value: "true"), - URLQueryItem(name: "client_id", value: self.clientId), - URLQueryItem(name: "response_type", value: "code"), - URLQueryItem(name: "redirect_uri", value: self.redirectURI), - URLQueryItem(name: "scope", value: self.scopes), - URLQueryItem(name: "code_challenge", value: pkce.challenge), - URLQueryItem(name: "code_challenge_method", value: "S256"), - // Match legacy flow: state is the verifier. - URLQueryItem(name: "state", value: pkce.verifier), - ] - return components.url! - } - - static func exchangeCode( - code: String, - state: String, - verifier: String) async throws -> AnthropicOAuthCredentials - { - let payload: [String: Any] = [ - "grant_type": "authorization_code", - "client_id": self.clientId, - "code": code, - "state": state, - "redirect_uri": self.redirectURI, - "code_verifier": verifier, - ] - let body = try JSONSerialization.data(withJSONObject: payload, options: []) - - var request = URLRequest(url: self.tokenURL) - request.httpMethod = "POST" - request.httpBody = body - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - - let (data, response) = try await URLSession.shared.data(for: request) - guard let http = response as? HTTPURLResponse else { - throw URLError(.badServerResponse) - } - guard (200..<300).contains(http.statusCode) else { - let text = String(data: data, encoding: .utf8) ?? "" - throw NSError( - domain: "AnthropicOAuth", - code: http.statusCode, - userInfo: [NSLocalizedDescriptionKey: "Token exchange failed: \(text)"]) - } - - let decoded = try JSONSerialization.jsonObject(with: data) as? [String: Any] - let access = decoded?["access_token"] as? String - let refresh = decoded?["refresh_token"] as? String - let expiresIn = decoded?["expires_in"] as? Double - guard let access, let refresh, let expiresIn else { - throw NSError(domain: "AnthropicOAuth", code: 0, userInfo: [ - NSLocalizedDescriptionKey: "Unexpected token response.", - ]) - } - - // Match legacy flow: expiresAt = now + expires_in - 5 minutes. - let expiresAtMs = Int64(Date().timeIntervalSince1970 * 1000) - + Int64(expiresIn * 1000) - - Int64(5 * 60 * 1000) - - self.logger.info("Anthropic OAuth exchange ok; expiresAtMs=\(expiresAtMs, privacy: .public)") - return AnthropicOAuthCredentials(type: "oauth", refresh: refresh, access: access, expires: expiresAtMs) - } - - static func refresh(refreshToken: String) async throws -> AnthropicOAuthCredentials { - let payload: [String: Any] = [ - "grant_type": "refresh_token", - "client_id": self.clientId, - "refresh_token": refreshToken, - ] - let body = try JSONSerialization.data(withJSONObject: payload, options: []) - - var request = URLRequest(url: self.tokenURL) - request.httpMethod = "POST" - request.httpBody = body - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - - let (data, response) = try await URLSession.shared.data(for: request) - guard let http = response as? HTTPURLResponse else { - throw URLError(.badServerResponse) - } - guard (200..<300).contains(http.statusCode) else { - let text = String(data: data, encoding: .utf8) ?? "" - throw NSError( - domain: "AnthropicOAuth", - code: http.statusCode, - userInfo: [NSLocalizedDescriptionKey: "Token refresh failed: \(text)"]) - } - - let decoded = try JSONSerialization.jsonObject(with: data) as? [String: Any] - let access = decoded?["access_token"] as? String - let refresh = (decoded?["refresh_token"] as? String) ?? refreshToken - let expiresIn = decoded?["expires_in"] as? Double - guard let access, let expiresIn else { - throw NSError(domain: "AnthropicOAuth", code: 0, userInfo: [ - NSLocalizedDescriptionKey: "Unexpected token response.", - ]) - } - - let expiresAtMs = Int64(Date().timeIntervalSince1970 * 1000) - + Int64(expiresIn * 1000) - - Int64(5 * 60 * 1000) - - self.logger.info("Anthropic OAuth refresh ok; expiresAtMs=\(expiresAtMs, privacy: .public)") - return AnthropicOAuthCredentials(type: "oauth", refresh: refresh, access: access, expires: expiresAtMs) - } -} - -enum OpenClawOAuthStore { - static let oauthFilename = "oauth.json" - private static let providerKey = "anthropic" - private static let openclawOAuthDirEnv = "OPENCLAW_OAUTH_DIR" - private static let legacyPiDirEnv = "PI_CODING_AGENT_DIR" - - enum AnthropicOAuthStatus: Equatable { - case missingFile - case unreadableFile - case invalidJSON - case missingProviderEntry - case missingTokens - case connected(expiresAtMs: Int64?) - - var isConnected: Bool { - if case .connected = self { return true } - return false - } - - var shortDescription: String { - switch self { - case .missingFile: "OpenClaw OAuth token file not found" - case .unreadableFile: "OpenClaw OAuth token file not readable" - case .invalidJSON: "OpenClaw OAuth token file invalid" - case .missingProviderEntry: "No Anthropic entry in OpenClaw OAuth token file" - case .missingTokens: "Anthropic entry missing tokens" - case .connected: "OpenClaw OAuth credentials found" - } - } - } - - static func oauthDir() -> URL { - if let override = ProcessInfo.processInfo.environment[self.openclawOAuthDirEnv]? - .trimmingCharacters(in: .whitespacesAndNewlines), - !override.isEmpty - { - let expanded = NSString(string: override).expandingTildeInPath - return URL(fileURLWithPath: expanded, isDirectory: true) - } - let home = FileManager().homeDirectoryForCurrentUser - return home.appendingPathComponent(".openclaw", isDirectory: true) - .appendingPathComponent("credentials", isDirectory: true) - } - - static func oauthURL() -> URL { - self.oauthDir().appendingPathComponent(self.oauthFilename) - } - - static func legacyOAuthURLs() -> [URL] { - var urls: [URL] = [] - let env = ProcessInfo.processInfo.environment - if let override = env[self.legacyPiDirEnv]?.trimmingCharacters(in: .whitespacesAndNewlines), - !override.isEmpty - { - let expanded = NSString(string: override).expandingTildeInPath - urls.append(URL(fileURLWithPath: expanded, isDirectory: true).appendingPathComponent(self.oauthFilename)) - } - - let home = FileManager().homeDirectoryForCurrentUser - urls.append(home.appendingPathComponent(".pi/agent/\(self.oauthFilename)")) - urls.append(home.appendingPathComponent(".claude/\(self.oauthFilename)")) - urls.append(home.appendingPathComponent(".config/claude/\(self.oauthFilename)")) - urls.append(home.appendingPathComponent(".config/anthropic/\(self.oauthFilename)")) - - var seen = Set() - return urls.filter { url in - let path = url.standardizedFileURL.path - if seen.contains(path) { return false } - seen.insert(path) - return true - } - } - - static func importLegacyAnthropicOAuthIfNeeded() -> URL? { - let dest = self.oauthURL() - guard !FileManager().fileExists(atPath: dest.path) else { return nil } - - for url in self.legacyOAuthURLs() { - guard FileManager().fileExists(atPath: url.path) else { continue } - guard self.anthropicOAuthStatus(at: url).isConnected else { continue } - guard let storage = self.loadStorage(at: url) else { continue } - do { - try self.saveStorage(storage) - return url - } catch { - continue - } - } - - return nil - } - - static func anthropicOAuthStatus() -> AnthropicOAuthStatus { - self.anthropicOAuthStatus(at: self.oauthURL()) - } - - static func hasAnthropicOAuth() -> Bool { - self.anthropicOAuthStatus().isConnected - } - - static func anthropicOAuthStatus(at url: URL) -> AnthropicOAuthStatus { - guard FileManager().fileExists(atPath: url.path) else { return .missingFile } - - guard let data = try? Data(contentsOf: url) else { return .unreadableFile } - guard let json = try? JSONSerialization.jsonObject(with: data, options: []) else { return .invalidJSON } - guard let storage = json as? [String: Any] else { return .invalidJSON } - guard let rawEntry = storage[self.providerKey] else { return .missingProviderEntry } - guard let entry = rawEntry as? [String: Any] else { return .invalidJSON } - - let refresh = self.firstString(in: entry, keys: ["refresh", "refresh_token", "refreshToken"]) - let access = self.firstString(in: entry, keys: ["access", "access_token", "accessToken"]) - guard refresh?.isEmpty == false, access?.isEmpty == false else { return .missingTokens } - - let expiresAny = entry["expires"] ?? entry["expires_at"] ?? entry["expiresAt"] - let expiresAtMs: Int64? = if let ms = expiresAny as? Int64 { - ms - } else if let number = expiresAny as? NSNumber { - number.int64Value - } else if let ms = expiresAny as? Double { - Int64(ms) - } else { - nil - } - - return .connected(expiresAtMs: expiresAtMs) - } - - static func loadAnthropicOAuthRefreshToken() -> String? { - let url = self.oauthURL() - guard let storage = self.loadStorage(at: url) else { return nil } - guard let rawEntry = storage[self.providerKey] as? [String: Any] else { return nil } - let refresh = self.firstString(in: rawEntry, keys: ["refresh", "refresh_token", "refreshToken"]) - return refresh?.trimmingCharacters(in: .whitespacesAndNewlines) - } - - private static func firstString(in dict: [String: Any], keys: [String]) -> String? { - for key in keys { - if let value = dict[key] as? String { return value } - } - return nil - } - - private static func loadStorage(at url: URL) -> [String: Any]? { - guard let data = try? Data(contentsOf: url) else { return nil } - guard let json = try? JSONSerialization.jsonObject(with: data, options: []) else { return nil } - return json as? [String: Any] - } - - static func saveAnthropicOAuth(_ creds: AnthropicOAuthCredentials) throws { - let url = self.oauthURL() - let existing: [String: Any] = self.loadStorage(at: url) ?? [:] - - var updated = existing - updated[self.providerKey] = [ - "type": creds.type, - "refresh": creds.refresh, - "access": creds.access, - "expires": creds.expires, - ] - - try self.saveStorage(updated) - } - - private static func saveStorage(_ storage: [String: Any]) throws { - let dir = self.oauthDir() - try FileManager().createDirectory( - at: dir, - withIntermediateDirectories: true, - attributes: [.posixPermissions: 0o700]) - - let url = self.oauthURL() - let data = try JSONSerialization.data( - withJSONObject: storage, - options: [.prettyPrinted, .sortedKeys]) - try data.write(to: url, options: [.atomic]) - try FileManager().setAttributes([.posixPermissions: 0o600], ofItemAtPath: url.path) - } -} - -extension Data { - fileprivate func base64URLEncodedString() -> String { - self.base64EncodedString() - .replacingOccurrences(of: "+", with: "-") - .replacingOccurrences(of: "/", with: "_") - .replacingOccurrences(of: "=", with: "") - } -} diff --git a/apps/macos/Sources/OpenClaw/AnthropicOAuthCodeState.swift b/apps/macos/Sources/OpenClaw/AnthropicOAuthCodeState.swift deleted file mode 100644 index 2a88898c34df..000000000000 --- a/apps/macos/Sources/OpenClaw/AnthropicOAuthCodeState.swift +++ /dev/null @@ -1,59 +0,0 @@ -import Foundation - -enum AnthropicOAuthCodeState { - struct Parsed: Equatable { - let code: String - let state: String - } - - /// Extracts a `code#state` payload from arbitrary text. - /// - /// Supports: - /// - raw `code#state` - /// - OAuth callback URLs containing `code=` and `state=` query params - /// - surrounding text/backticks from instructions pages - static func extract(from raw: String) -> String? { - let text = raw.trimmingCharacters(in: .whitespacesAndNewlines) - .trimmingCharacters(in: CharacterSet(charactersIn: "`")) - if text.isEmpty { return nil } - - if let fromURL = self.extractFromURL(text) { return fromURL } - if let fromToken = self.extractFromToken(text) { return fromToken } - return nil - } - - static func parse(from raw: String) -> Parsed? { - guard let extracted = self.extract(from: raw) else { return nil } - let parts = extracted.split(separator: "#", maxSplits: 1).map(String.init) - let code = parts.first ?? "" - let state = parts.count > 1 ? parts[1] : "" - guard !code.isEmpty, !state.isEmpty else { return nil } - return Parsed(code: code, state: state) - } - - private static func extractFromURL(_ text: String) -> String? { - // Users might copy the callback URL from the browser address bar. - guard let components = URLComponents(string: text), - let items = components.queryItems, - let code = items.first(where: { $0.name == "code" })?.value, - let state = items.first(where: { $0.name == "state" })?.value, - !code.isEmpty, !state.isEmpty - else { return nil } - - return "\(code)#\(state)" - } - - private static func extractFromToken(_ text: String) -> String? { - // Base64url-ish tokens; keep this fairly strict to avoid false positives. - let pattern = #"([A-Za-z0-9._~-]{8,})#([A-Za-z0-9._~-]{8,})"# - guard let re = try? NSRegularExpression(pattern: pattern) else { return nil } - - let range = NSRange(text.startIndex..? @State var needsBootstrap = false @State var didAutoKickoff = false @State var showAdvancedConnection = false @@ -104,19 +87,9 @@ struct OnboardingView: View { let pageWidth: CGFloat = Self.windowWidth let contentHeight: CGFloat = 460 let connectionPageIndex = 1 - let anthropicAuthPageIndex = 2 let wizardPageIndex = 3 let onboardingChatPageIndex = 8 - static let clipboardPoll: AnyPublisher = { - if ProcessInfo.processInfo.isRunningTests { - return Empty(completeImmediately: false).eraseToAnyPublisher() - } - return Timer.publish(every: 0.4, on: .main, in: .common) - .autoconnect() - .eraseToAnyPublisher() - }() - let permissionsPageIndex = 5 static func pageOrder( for mode: AppState.ConnectionMode, diff --git a/apps/macos/Sources/OpenClaw/OnboardingView+Actions.swift b/apps/macos/Sources/OpenClaw/OnboardingView+Actions.swift index bcd5bd6d44d2..a521926ddb99 100644 --- a/apps/macos/Sources/OpenClaw/OnboardingView+Actions.swift +++ b/apps/macos/Sources/OpenClaw/OnboardingView+Actions.swift @@ -78,70 +78,4 @@ extension OnboardingView { self.copied = true DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { self.copied = false } } - - func startAnthropicOAuth() { - guard !self.anthropicAuthBusy else { return } - self.anthropicAuthBusy = true - defer { self.anthropicAuthBusy = false } - - do { - let pkce = try AnthropicOAuth.generatePKCE() - self.anthropicAuthPKCE = pkce - let url = AnthropicOAuth.buildAuthorizeURL(pkce: pkce) - NSWorkspace.shared.open(url) - self.anthropicAuthStatus = "Browser opened. After approving, paste the `code#state` value here." - } catch { - self.anthropicAuthStatus = "Failed to start OAuth: \(error.localizedDescription)" - } - } - - @MainActor - func finishAnthropicOAuth() async { - guard !self.anthropicAuthBusy else { return } - guard let pkce = self.anthropicAuthPKCE else { return } - self.anthropicAuthBusy = true - defer { self.anthropicAuthBusy = false } - - guard let parsed = AnthropicOAuthCodeState.parse(from: self.anthropicAuthCode) else { - self.anthropicAuthStatus = "OAuth failed: missing or invalid code/state." - return - } - - do { - let creds = try await AnthropicOAuth.exchangeCode( - code: parsed.code, - state: parsed.state, - verifier: pkce.verifier) - try OpenClawOAuthStore.saveAnthropicOAuth(creds) - self.refreshAnthropicOAuthStatus() - self.anthropicAuthStatus = "Connected. OpenClaw can now use Claude." - } catch { - self.anthropicAuthStatus = "OAuth failed: \(error.localizedDescription)" - } - } - - func pollAnthropicClipboardIfNeeded() { - guard self.currentPage == self.anthropicAuthPageIndex else { return } - guard self.anthropicAuthPKCE != nil else { return } - guard !self.anthropicAuthBusy else { return } - guard self.anthropicAuthAutoDetectClipboard else { return } - - let pb = NSPasteboard.general - let changeCount = pb.changeCount - guard changeCount != self.anthropicAuthLastPasteboardChangeCount else { return } - self.anthropicAuthLastPasteboardChangeCount = changeCount - - guard let raw = pb.string(forType: .string), !raw.isEmpty else { return } - guard let parsed = AnthropicOAuthCodeState.parse(from: raw) else { return } - guard let pkce = self.anthropicAuthPKCE, parsed.state == pkce.verifier else { return } - - let next = "\(parsed.code)#\(parsed.state)" - if self.anthropicAuthCode != next { - self.anthropicAuthCode = next - self.anthropicAuthStatus = "Detected `code#state` from clipboard." - } - - guard self.anthropicAuthAutoConnectClipboard else { return } - Task { await self.finishAnthropicOAuth() } - } } diff --git a/apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift b/apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift index ce87e211ce44..9b0e45e205c6 100644 --- a/apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift +++ b/apps/macos/Sources/OpenClaw/OnboardingView+Layout.swift @@ -53,7 +53,6 @@ extension OnboardingView { .onDisappear { self.stopPermissionMonitoring() self.stopDiscovery() - self.stopAuthMonitoring() Task { await self.onboardingWizard.cancelIfRunning() } } .task { @@ -61,7 +60,6 @@ extension OnboardingView { self.refreshCLIStatus() await self.loadWorkspaceDefaults() await self.ensureDefaultWorkspace() - self.refreshAnthropicOAuthStatus() self.refreshBootstrapStatus() self.preferredGatewayID = GatewayDiscoveryPreferences.preferredStableID() } diff --git a/apps/macos/Sources/OpenClaw/OnboardingView+Monitoring.swift b/apps/macos/Sources/OpenClaw/OnboardingView+Monitoring.swift index dfbdf91d44d8..efe37f31673c 100644 --- a/apps/macos/Sources/OpenClaw/OnboardingView+Monitoring.swift +++ b/apps/macos/Sources/OpenClaw/OnboardingView+Monitoring.swift @@ -47,7 +47,6 @@ extension OnboardingView { func updateMonitoring(for pageIndex: Int) { self.updatePermissionMonitoring(for: pageIndex) self.updateDiscoveryMonitoring(for: pageIndex) - self.updateAuthMonitoring(for: pageIndex) self.maybeKickoffOnboardingChat(for: pageIndex) } @@ -63,33 +62,6 @@ extension OnboardingView { self.gatewayDiscovery.stop() } - func updateAuthMonitoring(for pageIndex: Int) { - let shouldMonitor = pageIndex == self.anthropicAuthPageIndex && self.state.connectionMode == .local - if shouldMonitor, !self.monitoringAuth { - self.monitoringAuth = true - self.startAuthMonitoring() - } else if !shouldMonitor, self.monitoringAuth { - self.stopAuthMonitoring() - } - } - - func startAuthMonitoring() { - self.refreshAnthropicOAuthStatus() - self.authMonitorTask?.cancel() - self.authMonitorTask = Task { - while !Task.isCancelled { - await MainActor.run { self.refreshAnthropicOAuthStatus() } - try? await Task.sleep(nanoseconds: 1_000_000_000) - } - } - } - - func stopAuthMonitoring() { - self.monitoringAuth = false - self.authMonitorTask?.cancel() - self.authMonitorTask = nil - } - func installCLI() async { guard !self.installingCLI else { return } self.installingCLI = true @@ -125,54 +97,4 @@ extension OnboardingView { expected: expected) } } - - func refreshAnthropicOAuthStatus() { - _ = OpenClawOAuthStore.importLegacyAnthropicOAuthIfNeeded() - let previous = self.anthropicAuthDetectedStatus - let status = OpenClawOAuthStore.anthropicOAuthStatus() - self.anthropicAuthDetectedStatus = status - self.anthropicAuthConnected = status.isConnected - - if previous != status { - self.anthropicAuthVerified = false - self.anthropicAuthVerificationAttempted = false - self.anthropicAuthVerificationFailed = false - self.anthropicAuthVerifiedAt = nil - } - } - - @MainActor - func verifyAnthropicOAuthIfNeeded(force: Bool = false) async { - guard self.state.connectionMode == .local else { return } - guard self.anthropicAuthDetectedStatus.isConnected else { return } - if self.anthropicAuthVerified, !force { return } - if self.anthropicAuthVerifying { return } - if self.anthropicAuthVerificationAttempted, !force { return } - - self.anthropicAuthVerificationAttempted = true - self.anthropicAuthVerifying = true - self.anthropicAuthVerificationFailed = false - defer { self.anthropicAuthVerifying = false } - - guard let refresh = OpenClawOAuthStore.loadAnthropicOAuthRefreshToken(), !refresh.isEmpty else { - self.anthropicAuthStatus = "OAuth verification failed: missing refresh token." - self.anthropicAuthVerificationFailed = true - return - } - - do { - let updated = try await AnthropicOAuth.refresh(refreshToken: refresh) - try OpenClawOAuthStore.saveAnthropicOAuth(updated) - self.refreshAnthropicOAuthStatus() - self.anthropicAuthVerified = true - self.anthropicAuthVerifiedAt = Date() - self.anthropicAuthVerificationFailed = false - self.anthropicAuthStatus = "OAuth detected and verified." - } catch { - self.anthropicAuthVerified = false - self.anthropicAuthVerifiedAt = nil - self.anthropicAuthVerificationFailed = true - self.anthropicAuthStatus = "OAuth verification failed: \(error.localizedDescription)" - } - } } diff --git a/apps/macos/Sources/OpenClaw/OnboardingView+Pages.swift b/apps/macos/Sources/OpenClaw/OnboardingView+Pages.swift index ed40bd2ed58b..4f942dfe8a4f 100644 --- a/apps/macos/Sources/OpenClaw/OnboardingView+Pages.swift +++ b/apps/macos/Sources/OpenClaw/OnboardingView+Pages.swift @@ -12,8 +12,6 @@ extension OnboardingView { self.welcomePage() case 1: self.connectionPage() - case 2: - self.anthropicAuthPage() case 3: self.wizardPage() case 5: @@ -340,170 +338,6 @@ extension OnboardingView { .buttonStyle(.plain) } - func anthropicAuthPage() -> some View { - self.onboardingPage { - Text("Connect Claude") - .font(.largeTitle.weight(.semibold)) - Text("Give your model the token it needs!") - .font(.body) - .foregroundStyle(.secondary) - .multilineTextAlignment(.center) - .frame(maxWidth: 540) - .fixedSize(horizontal: false, vertical: true) - Text("OpenClaw supports any model — we strongly recommend Opus 4.6 for the best experience.") - .font(.callout) - .foregroundStyle(.secondary) - .multilineTextAlignment(.center) - .frame(maxWidth: 540) - .fixedSize(horizontal: false, vertical: true) - - self.onboardingCard(spacing: 12, padding: 16) { - HStack(alignment: .center, spacing: 10) { - Circle() - .fill(self.anthropicAuthVerified ? Color.green : Color.orange) - .frame(width: 10, height: 10) - Text( - self.anthropicAuthConnected - ? (self.anthropicAuthVerified - ? "Claude connected (OAuth) — verified" - : "Claude connected (OAuth)") - : "Not connected yet") - .font(.headline) - Spacer() - } - - if self.anthropicAuthConnected, self.anthropicAuthVerifying { - Text("Verifying OAuth…") - .font(.caption) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } else if !self.anthropicAuthConnected { - Text(self.anthropicAuthDetectedStatus.shortDescription) - .font(.caption) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } else if self.anthropicAuthVerified, let date = self.anthropicAuthVerifiedAt { - Text("Detected working OAuth (\(date.formatted(date: .abbreviated, time: .shortened))).") - .font(.caption) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } - - Text( - "This lets OpenClaw use Claude immediately. Credentials are stored at " + - "`~/.openclaw/credentials/oauth.json` (owner-only).") - .font(.subheadline) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - - HStack(spacing: 12) { - Text(OpenClawOAuthStore.oauthURL().path) - .font(.caption) - .foregroundStyle(.secondary) - .lineLimit(1) - .truncationMode(.middle) - - Spacer() - - Button("Reveal") { - NSWorkspace.shared.activateFileViewerSelecting([OpenClawOAuthStore.oauthURL()]) - } - .buttonStyle(.bordered) - - Button("Refresh") { - self.refreshAnthropicOAuthStatus() - } - .buttonStyle(.bordered) - } - - Divider().padding(.vertical, 2) - - HStack(spacing: 12) { - if !self.anthropicAuthVerified { - if self.anthropicAuthConnected { - Button("Verify") { - Task { await self.verifyAnthropicOAuthIfNeeded(force: true) } - } - .buttonStyle(.borderedProminent) - .disabled(self.anthropicAuthBusy || self.anthropicAuthVerifying) - - if self.anthropicAuthVerificationFailed { - Button("Re-auth (OAuth)") { - self.startAnthropicOAuth() - } - .buttonStyle(.bordered) - .disabled(self.anthropicAuthBusy || self.anthropicAuthVerifying) - } - } else { - Button { - self.startAnthropicOAuth() - } label: { - if self.anthropicAuthBusy { - ProgressView() - } else { - Text("Open Claude sign-in (OAuth)") - } - } - .buttonStyle(.borderedProminent) - .disabled(self.anthropicAuthBusy) - } - } - } - - if !self.anthropicAuthVerified, self.anthropicAuthPKCE != nil { - VStack(alignment: .leading, spacing: 8) { - Text("Paste the `code#state` value") - .font(.headline) - TextField("code#state", text: self.$anthropicAuthCode) - .textFieldStyle(.roundedBorder) - - Toggle("Auto-detect from clipboard", isOn: self.$anthropicAuthAutoDetectClipboard) - .font(.caption) - .foregroundStyle(.secondary) - .disabled(self.anthropicAuthBusy) - - Toggle("Auto-connect when detected", isOn: self.$anthropicAuthAutoConnectClipboard) - .font(.caption) - .foregroundStyle(.secondary) - .disabled(self.anthropicAuthBusy) - - Button("Connect") { - Task { await self.finishAnthropicOAuth() } - } - .buttonStyle(.bordered) - .disabled( - self.anthropicAuthBusy || - self.anthropicAuthCode.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) - } - .onReceive(Self.clipboardPoll) { _ in - self.pollAnthropicClipboardIfNeeded() - } - } - - self.onboardingCard(spacing: 8, padding: 12) { - Text("API key (advanced)") - .font(.headline) - Text( - "You can also use an Anthropic API key, but this UI is instructions-only for now " + - "(GUI apps don’t automatically inherit your shell env vars like `ANTHROPIC_API_KEY`).") - .font(.subheadline) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } - .shadow(color: .clear, radius: 0) - .background(Color.clear) - - if let status = self.anthropicAuthStatus { - Text(status) - .font(.caption) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - } - } - } - .task { await self.verifyAnthropicOAuthIfNeeded() } - } - func permissionsPage() -> some View { self.onboardingPage { Text("Grant permissions") diff --git a/apps/macos/Sources/OpenClaw/OnboardingView+Testing.swift b/apps/macos/Sources/OpenClaw/OnboardingView+Testing.swift index cf8c3d0c78f6..2bd9c525ad4a 100644 --- a/apps/macos/Sources/OpenClaw/OnboardingView+Testing.swift +++ b/apps/macos/Sources/OpenClaw/OnboardingView+Testing.swift @@ -37,18 +37,9 @@ extension OnboardingView { view.cliStatus = "Installed" view.workspacePath = "/tmp/openclaw" view.workspaceStatus = "Saved workspace" - view.anthropicAuthPKCE = AnthropicOAuth.PKCE(verifier: "verifier", challenge: "challenge") - view.anthropicAuthCode = "code#state" - view.anthropicAuthStatus = "Connected" - view.anthropicAuthDetectedStatus = .connected(expiresAtMs: 1_700_000_000_000) - view.anthropicAuthConnected = true - view.anthropicAuthAutoDetectClipboard = false - view.anthropicAuthAutoConnectClipboard = false - view.state.connectionMode = .local _ = view.welcomePage() _ = view.connectionPage() - _ = view.anthropicAuthPage() _ = view.wizardPage() _ = view.permissionsPage() _ = view.cliPage() diff --git a/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthControlsSmokeTests.swift b/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthControlsSmokeTests.swift deleted file mode 100644 index 84c618339328..000000000000 --- a/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthControlsSmokeTests.swift +++ /dev/null @@ -1,29 +0,0 @@ -import Testing -@testable import OpenClaw - -@Suite(.serialized) -@MainActor -struct AnthropicAuthControlsSmokeTests { - @Test func anthropicAuthControlsBuildsBodyLocal() { - let pkce = AnthropicOAuth.PKCE(verifier: "verifier", challenge: "challenge") - let view = AnthropicAuthControls( - connectionMode: .local, - oauthStatus: .connected(expiresAtMs: 1_700_000_000_000), - pkce: pkce, - code: "code#state", - statusText: "Detected code", - autoDetectClipboard: false, - autoConnectClipboard: false) - _ = view.body - } - - @Test func anthropicAuthControlsBuildsBodyRemote() { - let view = AnthropicAuthControls( - connectionMode: .remote, - oauthStatus: .missingFile, - pkce: nil, - code: "", - statusText: nil) - _ = view.body - } -} diff --git a/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthResolverTests.swift b/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthResolverTests.swift deleted file mode 100644 index c41b7f64be4c..000000000000 --- a/apps/macos/Tests/OpenClawIPCTests/AnthropicAuthResolverTests.swift +++ /dev/null @@ -1,52 +0,0 @@ -import Foundation -import Testing -@testable import OpenClaw - -@Suite -struct AnthropicAuthResolverTests { - @Test - func prefersOAuthFileOverEnv() throws { - let dir = FileManager().temporaryDirectory - .appendingPathComponent("openclaw-oauth-\(UUID().uuidString)", isDirectory: true) - try FileManager().createDirectory(at: dir, withIntermediateDirectories: true) - let oauthFile = dir.appendingPathComponent("oauth.json") - let payload = [ - "anthropic": [ - "type": "oauth", - "refresh": "r1", - "access": "a1", - "expires": 1_234_567_890, - ], - ] - let data = try JSONSerialization.data(withJSONObject: payload, options: [.prettyPrinted, .sortedKeys]) - try data.write(to: oauthFile, options: [.atomic]) - - let status = OpenClawOAuthStore.anthropicOAuthStatus(at: oauthFile) - let mode = AnthropicAuthResolver.resolve(environment: [ - "ANTHROPIC_API_KEY": "sk-ant-ignored", - ], oauthStatus: status) - #expect(mode == .oauthFile) - } - - @Test - func reportsOAuthEnvWhenPresent() { - let mode = AnthropicAuthResolver.resolve(environment: [ - "ANTHROPIC_OAUTH_TOKEN": "token", - ], oauthStatus: .missingFile) - #expect(mode == .oauthEnv) - } - - @Test - func reportsAPIKeyEnvWhenPresent() { - let mode = AnthropicAuthResolver.resolve(environment: [ - "ANTHROPIC_API_KEY": "sk-ant-key", - ], oauthStatus: .missingFile) - #expect(mode == .apiKeyEnv) - } - - @Test - func reportsMissingWhenNothingConfigured() { - let mode = AnthropicAuthResolver.resolve(environment: [:], oauthStatus: .missingFile) - #expect(mode == .missing) - } -} diff --git a/apps/macos/Tests/OpenClawIPCTests/AnthropicOAuthCodeStateTests.swift b/apps/macos/Tests/OpenClawIPCTests/AnthropicOAuthCodeStateTests.swift deleted file mode 100644 index 3d337c2b279c..000000000000 --- a/apps/macos/Tests/OpenClawIPCTests/AnthropicOAuthCodeStateTests.swift +++ /dev/null @@ -1,31 +0,0 @@ -import Testing -@testable import OpenClaw - -@Suite -struct AnthropicOAuthCodeStateTests { - @Test - func parsesRawToken() { - let parsed = AnthropicOAuthCodeState.parse(from: "abcDEF1234#stateXYZ9876") - #expect(parsed == .init(code: "abcDEF1234", state: "stateXYZ9876")) - } - - @Test - func parsesBacktickedToken() { - let parsed = AnthropicOAuthCodeState.parse(from: "`abcDEF1234#stateXYZ9876`") - #expect(parsed == .init(code: "abcDEF1234", state: "stateXYZ9876")) - } - - @Test - func parsesCallbackURL() { - let raw = "https://console.anthropic.com/oauth/code/callback?code=abcDEF1234&state=stateXYZ9876" - let parsed = AnthropicOAuthCodeState.parse(from: raw) - #expect(parsed == .init(code: "abcDEF1234", state: "stateXYZ9876")) - } - - @Test - func extractsFromSurroundingText() { - let raw = "Paste the code#state value: abcDEF1234#stateXYZ9876 then return." - let parsed = AnthropicOAuthCodeState.parse(from: raw) - #expect(parsed == .init(code: "abcDEF1234", state: "stateXYZ9876")) - } -} diff --git a/apps/macos/Tests/OpenClawIPCTests/OpenClawOAuthStoreTests.swift b/apps/macos/Tests/OpenClawIPCTests/OpenClawOAuthStoreTests.swift deleted file mode 100644 index b34e9c3008ab..000000000000 --- a/apps/macos/Tests/OpenClawIPCTests/OpenClawOAuthStoreTests.swift +++ /dev/null @@ -1,97 +0,0 @@ -import Foundation -import Testing -@testable import OpenClaw - -@Suite -struct OpenClawOAuthStoreTests { - @Test - func returnsMissingWhenFileAbsent() { - let url = FileManager().temporaryDirectory - .appendingPathComponent("openclaw-oauth-\(UUID().uuidString)") - .appendingPathComponent("oauth.json") - #expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingFile) - } - - @Test - func usesEnvOverrideForOpenClawOAuthDir() throws { - let key = "OPENCLAW_OAUTH_DIR" - let previous = ProcessInfo.processInfo.environment[key] - defer { - if let previous { - setenv(key, previous, 1) - } else { - unsetenv(key) - } - } - - let dir = FileManager().temporaryDirectory - .appendingPathComponent("openclaw-oauth-\(UUID().uuidString)", isDirectory: true) - setenv(key, dir.path, 1) - - #expect(OpenClawOAuthStore.oauthDir().standardizedFileURL == dir.standardizedFileURL) - } - - @Test - func acceptsPiFormatTokens() throws { - let url = try self.writeOAuthFile([ - "anthropic": [ - "type": "oauth", - "refresh": "r1", - "access": "a1", - "expires": 1_234_567_890, - ], - ]) - - #expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url).isConnected) - } - - @Test - func acceptsTokenKeyVariants() throws { - let url = try self.writeOAuthFile([ - "anthropic": [ - "type": "oauth", - "refresh_token": "r1", - "access_token": "a1", - ], - ]) - - #expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url).isConnected) - } - - @Test - func reportsMissingProviderEntry() throws { - let url = try self.writeOAuthFile([ - "other": [ - "type": "oauth", - "refresh": "r1", - "access": "a1", - ], - ]) - - #expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingProviderEntry) - } - - @Test - func reportsMissingTokens() throws { - let url = try self.writeOAuthFile([ - "anthropic": [ - "type": "oauth", - "refresh": "", - "access": "a1", - ], - ]) - - #expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingTokens) - } - - private func writeOAuthFile(_ json: [String: Any]) throws -> URL { - let dir = FileManager().temporaryDirectory - .appendingPathComponent("openclaw-oauth-\(UUID().uuidString)", isDirectory: true) - try FileManager().createDirectory(at: dir, withIntermediateDirectories: true) - - let url = dir.appendingPathComponent("oauth.json") - let data = try JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .sortedKeys]) - try data.write(to: url, options: [.atomic]) - return url - } -} diff --git a/docs/start/onboarding.md b/docs/start/onboarding.md index ab9289b8a110..e9f2edeb363d 100644 --- a/docs/start/onboarding.md +++ b/docs/start/onboarding.md @@ -37,9 +37,9 @@ For a general overview of onboarding paths, see [Onboarding Overview](/start/onb Where does the **Gateway** run? -- **This Mac (Local only):** onboarding can run OAuth flows and write credentials +- **This Mac (Local only):** onboarding can configure auth and write credentials locally. -- **Remote (over SSH/Tailnet):** onboarding does **not** run OAuth locally; +- **Remote (over SSH/Tailnet):** onboarding does **not** configure local auth; credentials must exist on the gateway host. - **Configure later:** skip setup and leave the app unconfigured. From d512163d686ad6741783e7119ddb3437f493dbbc Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:18:24 +0100 Subject: [PATCH 032/160] fix(security): harden nextcloud-talk webhook replay handling --- CHANGELOG.md | 1 + .../src/monitor.backend.test.ts | 91 ++++++++++++++ .../nextcloud-talk/src/monitor.replay.test.ts | 115 ++++++++++++++++++ extensions/nextcloud-talk/src/monitor.ts | 54 ++++++++ .../nextcloud-talk/src/replay-guard.test.ts | 70 +++++++++++ extensions/nextcloud-talk/src/replay-guard.ts | 65 ++++++++++ extensions/nextcloud-talk/src/types.ts | 2 + 7 files changed, 398 insertions(+) create mode 100644 extensions/nextcloud-talk/src/monitor.backend.test.ts create mode 100644 extensions/nextcloud-talk/src/monitor.replay.test.ts create mode 100644 extensions/nextcloud-talk/src/replay-guard.test.ts create mode 100644 extensions/nextcloud-talk/src/replay-guard.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 21d78689220b..60b08c0d0d8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. diff --git a/extensions/nextcloud-talk/src/monitor.backend.test.ts b/extensions/nextcloud-talk/src/monitor.backend.test.ts new file mode 100644 index 000000000000..9fb76093605e --- /dev/null +++ b/extensions/nextcloud-talk/src/monitor.backend.test.ts @@ -0,0 +1,91 @@ +import { type AddressInfo } from "node:net"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { createNextcloudTalkWebhookServer } from "./monitor.js"; +import { generateNextcloudTalkSignature } from "./signature.js"; + +type WebhookHarness = { + webhookUrl: string; + stop: () => Promise; +}; + +const cleanupFns: Array<() => Promise> = []; + +afterEach(async () => { + while (cleanupFns.length > 0) { + const cleanup = cleanupFns.pop(); + if (cleanup) { + await cleanup(); + } + } +}); + +async function startWebhookServer(params: { + path: string; + isBackendAllowed: (backend: string) => boolean; + onMessage: () => void | Promise; +}): Promise { + const { server, start } = createNextcloudTalkWebhookServer({ + port: 0, + host: "127.0.0.1", + path: params.path, + secret: "nextcloud-secret", + isBackendAllowed: params.isBackendAllowed, + onMessage: params.onMessage, + }); + await start(); + const address = server.address() as AddressInfo | null; + if (!address) { + throw new Error("missing server address"); + } + return { + webhookUrl: `http://127.0.0.1:${address.port}${params.path}`, + stop: () => + new Promise((resolve) => { + server.close(() => resolve()); + }), + }; +} + +describe("createNextcloudTalkWebhookServer backend allowlist", () => { + it("rejects requests from unexpected backend origins", async () => { + const onMessage = vi.fn(async () => {}); + const harness = await startWebhookServer({ + path: "/nextcloud-backend-check", + isBackendAllowed: (backend) => backend === "https://nextcloud.expected", + onMessage, + }); + cleanupFns.push(harness.stop); + + const payload = { + type: "Create", + actor: { type: "Person", id: "alice", name: "Alice" }, + object: { + type: "Note", + id: "msg-1", + name: "hello", + content: "hello", + mediaType: "text/plain", + }, + target: { type: "Collection", id: "room-1", name: "Room 1" }, + }; + const body = JSON.stringify(payload); + const { random, signature } = generateNextcloudTalkSignature({ + body, + secret: "nextcloud-secret", + }); + const response = await fetch(harness.webhookUrl, { + method: "POST", + headers: { + "content-type": "application/json", + "x-nextcloud-talk-random": random, + "x-nextcloud-talk-signature": signature, + "x-nextcloud-talk-backend": "https://nextcloud.unexpected", + }, + body, + }); + + expect(response.status).toBe(401); + expect(await response.json()).toEqual({ error: "Invalid backend" }); + expect(onMessage).not.toHaveBeenCalled(); + }); +}); diff --git a/extensions/nextcloud-talk/src/monitor.replay.test.ts b/extensions/nextcloud-talk/src/monitor.replay.test.ts new file mode 100644 index 000000000000..9943b4b367d4 --- /dev/null +++ b/extensions/nextcloud-talk/src/monitor.replay.test.ts @@ -0,0 +1,115 @@ +import { type AddressInfo } from "node:net"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { createNextcloudTalkWebhookServer } from "./monitor.js"; +import { generateNextcloudTalkSignature } from "./signature.js"; + +type WebhookHarness = { + webhookUrl: string; + stop: () => Promise; +}; + +const cleanupFns: Array<() => Promise> = []; + +afterEach(async () => { + while (cleanupFns.length > 0) { + const cleanup = cleanupFns.pop(); + if (cleanup) { + await cleanup(); + } + } +}); + +async function startWebhookServer(params: { + path: string; + shouldProcessMessage?: ( + message: Parameters< + NonNullable[0]["onMessage"]> + >[0], + ) => boolean | Promise; + onMessage: (message: { messageId: string }) => void | Promise; +}): Promise { + const { server, start } = createNextcloudTalkWebhookServer({ + port: 0, + host: "127.0.0.1", + path: params.path, + secret: "nextcloud-secret", + shouldProcessMessage: params.shouldProcessMessage, + onMessage: params.onMessage, + }); + await start(); + const address = server.address() as AddressInfo | null; + if (!address) { + throw new Error("missing server address"); + } + return { + webhookUrl: `http://127.0.0.1:${address.port}${params.path}`, + stop: () => + new Promise((resolve) => { + server.close(() => resolve()); + }), + }; +} + +function createSignedRequest(body: string): { random: string; signature: string } { + return generateNextcloudTalkSignature({ + body, + secret: "nextcloud-secret", + }); +} + +describe("createNextcloudTalkWebhookServer replay handling", () => { + it("acknowledges replayed requests and skips onMessage side effects", async () => { + const seen = new Set(); + const onMessage = vi.fn(async () => {}); + const shouldProcessMessage = vi.fn(async (message: { messageId: string }) => { + if (seen.has(message.messageId)) { + return false; + } + seen.add(message.messageId); + return true; + }); + const harness = await startWebhookServer({ + path: "/nextcloud-replay", + shouldProcessMessage, + onMessage, + }); + cleanupFns.push(harness.stop); + + const payload = { + type: "Create", + actor: { type: "Person", id: "alice", name: "Alice" }, + object: { + type: "Note", + id: "msg-1", + name: "hello", + content: "hello", + mediaType: "text/plain", + }, + target: { type: "Collection", id: "room-1", name: "Room 1" }, + }; + const body = JSON.stringify(payload); + const { random, signature } = createSignedRequest(body); + const headers = { + "content-type": "application/json", + "x-nextcloud-talk-random": random, + "x-nextcloud-talk-signature": signature, + "x-nextcloud-talk-backend": "https://nextcloud.example", + }; + + const first = await fetch(harness.webhookUrl, { + method: "POST", + headers, + body, + }); + const second = await fetch(harness.webhookUrl, { + method: "POST", + headers, + body, + }); + + expect(first.status).toBe(200); + expect(second.status).toBe(200); + expect(shouldProcessMessage).toHaveBeenCalledTimes(2); + expect(onMessage).toHaveBeenCalledTimes(1); + }); +}); diff --git a/extensions/nextcloud-talk/src/monitor.ts b/extensions/nextcloud-talk/src/monitor.ts index 4b68a3c4d0b8..0408070c4a46 100644 --- a/extensions/nextcloud-talk/src/monitor.ts +++ b/extensions/nextcloud-talk/src/monitor.ts @@ -1,4 +1,5 @@ import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; +import os from "node:os"; import { createLoggerBackedRuntime, type RuntimeEnv, @@ -8,6 +9,7 @@ import { } from "openclaw/plugin-sdk"; import { resolveNextcloudTalkAccount } from "./accounts.js"; import { handleNextcloudTalkInbound } from "./inbound.js"; +import { createNextcloudTalkReplayGuard } from "./replay-guard.js"; import { getNextcloudTalkRuntime } from "./runtime.js"; import { extractNextcloudTalkHeaders, verifyNextcloudTalkSignature } from "./signature.js"; import type { @@ -31,6 +33,14 @@ function formatError(err: unknown): string { return typeof err === "string" ? err : JSON.stringify(err); } +function normalizeOrigin(value: string): string | null { + try { + return new URL(value).origin.toLowerCase(); + } catch { + return null; + } +} + function parseWebhookPayload(body: string): NextcloudTalkWebhookPayload | null { try { const data = JSON.parse(body); @@ -93,6 +103,8 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe ? Math.floor(opts.maxBodyBytes) : DEFAULT_WEBHOOK_MAX_BODY_BYTES; const readBody = opts.readBody ?? readNextcloudTalkWebhookBody; + const isBackendAllowed = opts.isBackendAllowed; + const shouldProcessMessage = opts.shouldProcessMessage; const server = createServer(async (req: IncomingMessage, res: ServerResponse) => { if (req.url === HEALTH_PATH) { @@ -116,6 +128,11 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe res.end(JSON.stringify({ error: "Missing signature headers" })); return; } + if (isBackendAllowed && !isBackendAllowed(headers.backend)) { + res.writeHead(401, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ error: "Invalid backend" })); + return; + } const body = await readBody(req, maxBodyBytes); @@ -146,6 +163,14 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe } const message = payloadToInboundMessage(payload); + if (shouldProcessMessage) { + const shouldProcess = await shouldProcessMessage(message); + if (!shouldProcess) { + res.writeHead(200); + res.end(); + return; + } + } res.writeHead(200); res.end(); @@ -233,12 +258,41 @@ export async function monitorNextcloudTalkProvider( channel: "nextcloud-talk", accountId: account.accountId, }); + const expectedBackendOrigin = normalizeOrigin(account.baseUrl); + const replayGuard = createNextcloudTalkReplayGuard({ + stateDir: core.state.resolveStateDir(process.env, os.homedir), + onDiskError: (error) => { + logger.warn( + `[nextcloud-talk:${account.accountId}] replay guard disk error: ${String(error)}`, + ); + }, + }); const { start, stop } = createNextcloudTalkWebhookServer({ port, host, path, secret: account.secret, + isBackendAllowed: (backend) => { + if (!expectedBackendOrigin) { + return true; + } + const backendOrigin = normalizeOrigin(backend); + return backendOrigin === expectedBackendOrigin; + }, + shouldProcessMessage: async (message) => { + const shouldProcess = await replayGuard.shouldProcessMessage({ + accountId: account.accountId, + roomToken: message.roomToken, + messageId: message.messageId, + }); + if (!shouldProcess) { + logger.warn( + `[nextcloud-talk:${account.accountId}] replayed webhook ignored room=${message.roomToken} messageId=${message.messageId}`, + ); + } + return shouldProcess; + }, onMessage: async (message) => { core.channel.activity.record({ channel: "nextcloud-talk", diff --git a/extensions/nextcloud-talk/src/replay-guard.test.ts b/extensions/nextcloud-talk/src/replay-guard.test.ts new file mode 100644 index 000000000000..0bf18acb600e --- /dev/null +++ b/extensions/nextcloud-talk/src/replay-guard.test.ts @@ -0,0 +1,70 @@ +import { mkdtemp, rm } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { createNextcloudTalkReplayGuard } from "./replay-guard.js"; + +const tempDirs: string[] = []; + +afterEach(async () => { + while (tempDirs.length > 0) { + const dir = tempDirs.pop(); + if (dir) { + await rm(dir, { recursive: true, force: true }); + } + } +}); + +async function makeTempDir(): Promise { + const dir = await mkdtemp(path.join(os.tmpdir(), "nextcloud-talk-replay-")); + tempDirs.push(dir); + return dir; +} + +describe("createNextcloudTalkReplayGuard", () => { + it("persists replay decisions across guard instances", async () => { + const stateDir = await makeTempDir(); + + const firstGuard = createNextcloudTalkReplayGuard({ stateDir }); + const firstAttempt = await firstGuard.shouldProcessMessage({ + accountId: "account-a", + roomToken: "room-1", + messageId: "msg-1", + }); + const replayAttempt = await firstGuard.shouldProcessMessage({ + accountId: "account-a", + roomToken: "room-1", + messageId: "msg-1", + }); + + const secondGuard = createNextcloudTalkReplayGuard({ stateDir }); + const restartReplayAttempt = await secondGuard.shouldProcessMessage({ + accountId: "account-a", + roomToken: "room-1", + messageId: "msg-1", + }); + + expect(firstAttempt).toBe(true); + expect(replayAttempt).toBe(false); + expect(restartReplayAttempt).toBe(false); + }); + + it("scopes replay state by account namespace", async () => { + const stateDir = await makeTempDir(); + const guard = createNextcloudTalkReplayGuard({ stateDir }); + + const accountAFirst = await guard.shouldProcessMessage({ + accountId: "account-a", + roomToken: "room-1", + messageId: "msg-9", + }); + const accountBFirst = await guard.shouldProcessMessage({ + accountId: "account-b", + roomToken: "room-1", + messageId: "msg-9", + }); + + expect(accountAFirst).toBe(true); + expect(accountBFirst).toBe(true); + }); +}); diff --git a/extensions/nextcloud-talk/src/replay-guard.ts b/extensions/nextcloud-talk/src/replay-guard.ts new file mode 100644 index 000000000000..14b074ed2ab6 --- /dev/null +++ b/extensions/nextcloud-talk/src/replay-guard.ts @@ -0,0 +1,65 @@ +import path from "node:path"; +import { createPersistentDedupe } from "openclaw/plugin-sdk"; + +const DEFAULT_REPLAY_TTL_MS = 24 * 60 * 60 * 1000; +const DEFAULT_MEMORY_MAX_SIZE = 1_000; +const DEFAULT_FILE_MAX_ENTRIES = 10_000; + +function sanitizeSegment(value: string): string { + const trimmed = value.trim(); + if (!trimmed) { + return "default"; + } + return trimmed.replace(/[^a-zA-Z0-9_-]/g, "_"); +} + +function buildReplayKey(params: { roomToken: string; messageId: string }): string | null { + const roomToken = params.roomToken.trim(); + const messageId = params.messageId.trim(); + if (!roomToken || !messageId) { + return null; + } + return `${roomToken}:${messageId}`; +} + +export type NextcloudTalkReplayGuardOptions = { + stateDir: string; + ttlMs?: number; + memoryMaxSize?: number; + fileMaxEntries?: number; + onDiskError?: (error: unknown) => void; +}; + +export type NextcloudTalkReplayGuard = { + shouldProcessMessage: (params: { + accountId: string; + roomToken: string; + messageId: string; + }) => Promise; +}; + +export function createNextcloudTalkReplayGuard( + options: NextcloudTalkReplayGuardOptions, +): NextcloudTalkReplayGuard { + const stateDir = options.stateDir.trim(); + const persistentDedupe = createPersistentDedupe({ + ttlMs: options.ttlMs ?? DEFAULT_REPLAY_TTL_MS, + memoryMaxSize: options.memoryMaxSize ?? DEFAULT_MEMORY_MAX_SIZE, + fileMaxEntries: options.fileMaxEntries ?? DEFAULT_FILE_MAX_ENTRIES, + resolveFilePath: (namespace) => + path.join(stateDir, "nextcloud-talk", "replay-dedupe", `${sanitizeSegment(namespace)}.json`), + }); + + return { + shouldProcessMessage: async ({ accountId, roomToken, messageId }) => { + const replayKey = buildReplayKey({ roomToken, messageId }); + if (!replayKey) { + return true; + } + return await persistentDedupe.checkAndRecord(replayKey, { + namespace: accountId, + onDiskError: options.onDiskError, + }); + }, + }; +} diff --git a/extensions/nextcloud-talk/src/types.ts b/extensions/nextcloud-talk/src/types.ts index a9fe49be36da..e7af64a965cc 100644 --- a/extensions/nextcloud-talk/src/types.ts +++ b/extensions/nextcloud-talk/src/types.ts @@ -170,6 +170,8 @@ export type NextcloudTalkWebhookServerOptions = { secret: string; maxBodyBytes?: number; readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise; + isBackendAllowed?: (backend: string) => boolean; + shouldProcessMessage?: (message: NextcloudTalkInboundMessage) => boolean | Promise; onMessage: (message: NextcloudTalkInboundMessage) => void | Promise; onError?: (error: Error) => void; abortSignal?: AbortSignal; From f60d9591efccc8d201e271061de2b331b9b6f5e6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:23:06 +0100 Subject: [PATCH 033/160] docs(changelog): add macOS auth fix note for setup-token path --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60b08c0d0d8b..92aa7af7917e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,13 +6,13 @@ Docs: https://docs.openclaw.ai ### Changes -- macOS/Onboarding: remove Anthropic OAuth sign-in from the Mac onboarding UI and keep Anthropic subscription auth setup-token-only (legacy `oauth.json` OAuth onboarding path removed). - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. ### Fixes +- Security/macOS onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. From 5325ed90b294ac01e35e744d900a4db9cefc4c2b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:23:26 +0100 Subject: [PATCH 034/160] refactor(nextcloud-talk): extract webhook pipeline and shared test harness --- .../src/monitor.auth-order.test.ts | 51 +----- .../src/monitor.backend.test.ts | 49 +----- .../nextcloud-talk/src/monitor.replay.test.ts | 56 +------ .../src/monitor.test-harness.ts | 59 +++++++ extensions/nextcloud-talk/src/monitor.ts | 150 +++++++++++++----- 5 files changed, 176 insertions(+), 189 deletions(-) create mode 100644 extensions/nextcloud-talk/src/monitor.test-harness.ts diff --git a/extensions/nextcloud-talk/src/monitor.auth-order.test.ts b/extensions/nextcloud-talk/src/monitor.auth-order.test.ts index f2b4b65054d9..6cc149dde47e 100644 --- a/extensions/nextcloud-talk/src/monitor.auth-order.test.ts +++ b/extensions/nextcloud-talk/src/monitor.auth-order.test.ts @@ -1,50 +1,5 @@ -import { type AddressInfo } from "node:net"; -import { afterEach, describe, expect, it, vi } from "vitest"; -import { createNextcloudTalkWebhookServer } from "./monitor.js"; - -type WebhookHarness = { - webhookUrl: string; - stop: () => Promise; -}; - -const cleanupFns: Array<() => Promise> = []; - -afterEach(async () => { - while (cleanupFns.length > 0) { - const cleanup = cleanupFns.pop(); - if (cleanup) { - await cleanup(); - } - } -}); - -async function startWebhookServer(params: { - path: string; - maxBodyBytes: number; - readBody?: (req: import("node:http").IncomingMessage, maxBodyBytes: number) => Promise; -}): Promise { - const { server, start } = createNextcloudTalkWebhookServer({ - port: 0, - host: "127.0.0.1", - path: params.path, - secret: "nextcloud-secret", - maxBodyBytes: params.maxBodyBytes, - readBody: params.readBody, - onMessage: vi.fn(), - }); - await start(); - const address = server.address() as AddressInfo | null; - if (!address) { - throw new Error("missing server address"); - } - return { - webhookUrl: `http://127.0.0.1:${address.port}${params.path}`, - stop: () => - new Promise((resolve) => { - server.close(() => resolve()); - }), - }; -} +import { describe, expect, it, vi } from "vitest"; +import { startWebhookServer } from "./monitor.test-harness.js"; describe("createNextcloudTalkWebhookServer auth order", () => { it("rejects missing signature headers before reading request body", async () => { @@ -55,8 +10,8 @@ describe("createNextcloudTalkWebhookServer auth order", () => { path: "/nextcloud-auth-order", maxBodyBytes: 128, readBody, + onMessage: vi.fn(), }); - cleanupFns.push(harness.stop); const response = await fetch(harness.webhookUrl, { method: "POST", diff --git a/extensions/nextcloud-talk/src/monitor.backend.test.ts b/extensions/nextcloud-talk/src/monitor.backend.test.ts index 9fb76093605e..aaf9a30a9c84 100644 --- a/extensions/nextcloud-talk/src/monitor.backend.test.ts +++ b/extensions/nextcloud-talk/src/monitor.backend.test.ts @@ -1,51 +1,7 @@ -import { type AddressInfo } from "node:net"; -import { afterEach, describe, expect, it, vi } from "vitest"; -import { createNextcloudTalkWebhookServer } from "./monitor.js"; +import { describe, expect, it, vi } from "vitest"; +import { startWebhookServer } from "./monitor.test-harness.js"; import { generateNextcloudTalkSignature } from "./signature.js"; -type WebhookHarness = { - webhookUrl: string; - stop: () => Promise; -}; - -const cleanupFns: Array<() => Promise> = []; - -afterEach(async () => { - while (cleanupFns.length > 0) { - const cleanup = cleanupFns.pop(); - if (cleanup) { - await cleanup(); - } - } -}); - -async function startWebhookServer(params: { - path: string; - isBackendAllowed: (backend: string) => boolean; - onMessage: () => void | Promise; -}): Promise { - const { server, start } = createNextcloudTalkWebhookServer({ - port: 0, - host: "127.0.0.1", - path: params.path, - secret: "nextcloud-secret", - isBackendAllowed: params.isBackendAllowed, - onMessage: params.onMessage, - }); - await start(); - const address = server.address() as AddressInfo | null; - if (!address) { - throw new Error("missing server address"); - } - return { - webhookUrl: `http://127.0.0.1:${address.port}${params.path}`, - stop: () => - new Promise((resolve) => { - server.close(() => resolve()); - }), - }; -} - describe("createNextcloudTalkWebhookServer backend allowlist", () => { it("rejects requests from unexpected backend origins", async () => { const onMessage = vi.fn(async () => {}); @@ -54,7 +10,6 @@ describe("createNextcloudTalkWebhookServer backend allowlist", () => { isBackendAllowed: (backend) => backend === "https://nextcloud.expected", onMessage, }); - cleanupFns.push(harness.stop); const payload = { type: "Create", diff --git a/extensions/nextcloud-talk/src/monitor.replay.test.ts b/extensions/nextcloud-talk/src/monitor.replay.test.ts index 9943b4b367d4..387e7a8304fc 100644 --- a/extensions/nextcloud-talk/src/monitor.replay.test.ts +++ b/extensions/nextcloud-talk/src/monitor.replay.test.ts @@ -1,54 +1,7 @@ -import { type AddressInfo } from "node:net"; -import { afterEach, describe, expect, it, vi } from "vitest"; -import { createNextcloudTalkWebhookServer } from "./monitor.js"; +import { describe, expect, it, vi } from "vitest"; +import { startWebhookServer } from "./monitor.test-harness.js"; import { generateNextcloudTalkSignature } from "./signature.js"; - -type WebhookHarness = { - webhookUrl: string; - stop: () => Promise; -}; - -const cleanupFns: Array<() => Promise> = []; - -afterEach(async () => { - while (cleanupFns.length > 0) { - const cleanup = cleanupFns.pop(); - if (cleanup) { - await cleanup(); - } - } -}); - -async function startWebhookServer(params: { - path: string; - shouldProcessMessage?: ( - message: Parameters< - NonNullable[0]["onMessage"]> - >[0], - ) => boolean | Promise; - onMessage: (message: { messageId: string }) => void | Promise; -}): Promise { - const { server, start } = createNextcloudTalkWebhookServer({ - port: 0, - host: "127.0.0.1", - path: params.path, - secret: "nextcloud-secret", - shouldProcessMessage: params.shouldProcessMessage, - onMessage: params.onMessage, - }); - await start(); - const address = server.address() as AddressInfo | null; - if (!address) { - throw new Error("missing server address"); - } - return { - webhookUrl: `http://127.0.0.1:${address.port}${params.path}`, - stop: () => - new Promise((resolve) => { - server.close(() => resolve()); - }), - }; -} +import type { NextcloudTalkInboundMessage } from "./types.js"; function createSignedRequest(body: string): { random: string; signature: string } { return generateNextcloudTalkSignature({ @@ -61,7 +14,7 @@ describe("createNextcloudTalkWebhookServer replay handling", () => { it("acknowledges replayed requests and skips onMessage side effects", async () => { const seen = new Set(); const onMessage = vi.fn(async () => {}); - const shouldProcessMessage = vi.fn(async (message: { messageId: string }) => { + const shouldProcessMessage = vi.fn(async (message: NextcloudTalkInboundMessage) => { if (seen.has(message.messageId)) { return false; } @@ -73,7 +26,6 @@ describe("createNextcloudTalkWebhookServer replay handling", () => { shouldProcessMessage, onMessage, }); - cleanupFns.push(harness.stop); const payload = { type: "Create", diff --git a/extensions/nextcloud-talk/src/monitor.test-harness.ts b/extensions/nextcloud-talk/src/monitor.test-harness.ts new file mode 100644 index 000000000000..f0daf42e8d5a --- /dev/null +++ b/extensions/nextcloud-talk/src/monitor.test-harness.ts @@ -0,0 +1,59 @@ +import { type AddressInfo } from "node:net"; +import { afterEach } from "vitest"; +import { createNextcloudTalkWebhookServer } from "./monitor.js"; +import type { NextcloudTalkWebhookServerOptions } from "./types.js"; + +export type WebhookHarness = { + webhookUrl: string; + stop: () => Promise; +}; + +const cleanupFns: Array<() => Promise> = []; + +afterEach(async () => { + while (cleanupFns.length > 0) { + const cleanup = cleanupFns.pop(); + if (cleanup) { + await cleanup(); + } + } +}); + +export type StartWebhookServerParams = Omit< + NextcloudTalkWebhookServerOptions, + "port" | "host" | "path" | "secret" +> & { + path: string; + secret?: string; + host?: string; + port?: number; +}; + +export async function startWebhookServer( + params: StartWebhookServerParams, +): Promise { + const host = params.host ?? "127.0.0.1"; + const port = params.port ?? 0; + const secret = params.secret ?? "nextcloud-secret"; + const { server, start } = createNextcloudTalkWebhookServer({ + ...params, + port, + host, + secret, + }); + await start(); + const address = server.address() as AddressInfo | null; + if (!address) { + throw new Error("missing server address"); + } + + const harness: WebhookHarness = { + webhookUrl: `http://${host}:${address.port}${params.path}`, + stop: () => + new Promise((resolve) => { + server.close(() => resolve()); + }), + }; + cleanupFns.push(harness.stop); + return harness; +} diff --git a/extensions/nextcloud-talk/src/monitor.ts b/extensions/nextcloud-talk/src/monitor.ts index 0408070c4a46..3fb3da3e75bb 100644 --- a/extensions/nextcloud-talk/src/monitor.ts +++ b/extensions/nextcloud-talk/src/monitor.ts @@ -15,6 +15,7 @@ import { extractNextcloudTalkHeaders, verifyNextcloudTalkSignature } from "./sig import type { CoreConfig, NextcloudTalkInboundMessage, + NextcloudTalkWebhookHeaders, NextcloudTalkWebhookPayload, NextcloudTalkWebhookServerOptions, } from "./types.js"; @@ -25,6 +26,14 @@ const DEFAULT_WEBHOOK_PATH = "/nextcloud-talk-webhook"; const DEFAULT_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024; const DEFAULT_WEBHOOK_BODY_TIMEOUT_MS = 30_000; const HEALTH_PATH = "/healthz"; +const WEBHOOK_ERRORS = { + missingSignatureHeaders: "Missing signature headers", + invalidBackend: "Invalid backend", + invalidSignature: "Invalid signature", + invalidPayloadFormat: "Invalid payload format", + payloadTooLarge: "Payload too large", + internalServerError: "Internal server error", +} as const; function formatError(err: unknown): string { if (err instanceof Error) { @@ -61,6 +70,83 @@ function parseWebhookPayload(body: string): NextcloudTalkWebhookPayload | null { } } +function writeJsonResponse( + res: ServerResponse, + status: number, + body?: Record, +): void { + if (body) { + res.writeHead(status, { "Content-Type": "application/json" }); + res.end(JSON.stringify(body)); + return; + } + res.writeHead(status); + res.end(); +} + +function writeWebhookError(res: ServerResponse, status: number, error: string): void { + if (res.headersSent) { + return; + } + writeJsonResponse(res, status, { error }); +} + +function validateWebhookHeaders(params: { + req: IncomingMessage; + res: ServerResponse; + isBackendAllowed?: (backend: string) => boolean; +}): NextcloudTalkWebhookHeaders | null { + const headers = extractNextcloudTalkHeaders( + params.req.headers as Record, + ); + if (!headers) { + writeWebhookError(params.res, 400, WEBHOOK_ERRORS.missingSignatureHeaders); + return null; + } + if (params.isBackendAllowed && !params.isBackendAllowed(headers.backend)) { + writeWebhookError(params.res, 401, WEBHOOK_ERRORS.invalidBackend); + return null; + } + return headers; +} + +function verifyWebhookSignature(params: { + headers: NextcloudTalkWebhookHeaders; + body: string; + secret: string; + res: ServerResponse; +}): boolean { + const isValid = verifyNextcloudTalkSignature({ + signature: params.headers.signature, + random: params.headers.random, + body: params.body, + secret: params.secret, + }); + if (!isValid) { + writeWebhookError(params.res, 401, WEBHOOK_ERRORS.invalidSignature); + return false; + } + return true; +} + +function decodeWebhookCreateMessage(params: { + body: string; + res: ServerResponse; +}): + | { kind: "message"; message: NextcloudTalkInboundMessage } + | { kind: "ignore" } + | { kind: "invalid" } { + const payload = parseWebhookPayload(params.body); + if (!payload) { + writeWebhookError(params.res, 400, WEBHOOK_ERRORS.invalidPayloadFormat); + return { kind: "invalid" }; + } + if (payload.type !== "Create") { + return { kind: "ignore" }; + } + return { kind: "message", message: payloadToInboundMessage(payload) }; +} + function payloadToInboundMessage( payload: NextcloudTalkWebhookPayload, ): NextcloudTalkInboundMessage { @@ -120,60 +206,49 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe } try { - const headers = extractNextcloudTalkHeaders( - req.headers as Record, - ); + const headers = validateWebhookHeaders({ + req, + res, + isBackendAllowed, + }); if (!headers) { - res.writeHead(400, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Missing signature headers" })); - return; - } - if (isBackendAllowed && !isBackendAllowed(headers.backend)) { - res.writeHead(401, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Invalid backend" })); return; } const body = await readBody(req, maxBodyBytes); - const isValid = verifyNextcloudTalkSignature({ - signature: headers.signature, - random: headers.random, + const hasValidSignature = verifyWebhookSignature({ + headers, body, secret, + res, }); - - if (!isValid) { - res.writeHead(401, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Invalid signature" })); + if (!hasValidSignature) { return; } - const payload = parseWebhookPayload(body); - if (!payload) { - res.writeHead(400, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Invalid payload format" })); + const decoded = decodeWebhookCreateMessage({ + body, + res, + }); + if (decoded.kind === "invalid") { return; } - - if (payload.type !== "Create") { - res.writeHead(200); - res.end(); + if (decoded.kind === "ignore") { + writeJsonResponse(res, 200); return; } - const message = payloadToInboundMessage(payload); + const message = decoded.message; if (shouldProcessMessage) { const shouldProcess = await shouldProcessMessage(message); if (!shouldProcess) { - res.writeHead(200); - res.end(); + writeJsonResponse(res, 200); return; } } - res.writeHead(200); - res.end(); + writeJsonResponse(res, 200); try { await onMessage(message); @@ -182,25 +257,16 @@ export function createNextcloudTalkWebhookServer(opts: NextcloudTalkWebhookServe } } catch (err) { if (isRequestBodyLimitError(err, "PAYLOAD_TOO_LARGE")) { - if (!res.headersSent) { - res.writeHead(413, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Payload too large" })); - } + writeWebhookError(res, 413, WEBHOOK_ERRORS.payloadTooLarge); return; } if (isRequestBodyLimitError(err, "REQUEST_BODY_TIMEOUT")) { - if (!res.headersSent) { - res.writeHead(408, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: requestBodyErrorToText("REQUEST_BODY_TIMEOUT") })); - } + writeWebhookError(res, 408, requestBodyErrorToText("REQUEST_BODY_TIMEOUT")); return; } const error = err instanceof Error ? err : new Error(formatError(err)); onError?.(error); - if (!res.headersSent) { - res.writeHead(500, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ error: "Internal server error" })); - } + writeWebhookError(res, 500, WEBHOOK_ERRORS.internalServerError); } }); From 45d59971e66f38651f2f72a6818deb854b1eb85f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:26:49 +0100 Subject: [PATCH 035/160] docs(changelog): clarify macOS beta scope for oauth fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92aa7af7917e..09d200dbd961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ Docs: https://docs.openclaw.ai ### Fixes -- Security/macOS onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. +- Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. From 125f4071bcbc0de32e769940d07967db47f09d3d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:30:43 +0100 Subject: [PATCH 036/160] fix(gateway): block agents.files symlink escapes --- CHANGELOG.md | 1 + .../server-methods/agents-mutate.test.ts | 192 +++++++++++++- src/gateway/server-methods/agents.ts | 249 ++++++++++++++++-- 3 files changed, 421 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09d200dbd961..aee7af5ad7e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Docs: https://docs.openclaw.ai - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. +- Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. diff --git a/src/gateway/server-methods/agents-mutate.test.ts b/src/gateway/server-methods/agents-mutate.test.ts index 54c285203f36..a4fddea633af 100644 --- a/src/gateway/server-methods/agents-mutate.test.ts +++ b/src/gateway/server-methods/agents-mutate.test.ts @@ -26,7 +26,10 @@ const mocks = vi.hoisted(() => ({ fsMkdir: vi.fn(async () => undefined), fsAppendFile: vi.fn(async () => {}), fsReadFile: vi.fn(async () => ""), - fsStat: vi.fn(async () => null), + fsStat: vi.fn(async (..._args: unknown[]) => null as import("node:fs").Stats | null), + fsLstat: vi.fn(async (..._args: unknown[]) => null as import("node:fs").Stats | null), + fsRealpath: vi.fn(async (p: string) => p), + fsOpen: vi.fn(async () => ({}) as unknown), })); vi.mock("../../config/config.js", () => ({ @@ -85,6 +88,9 @@ vi.mock("node:fs/promises", async () => { appendFile: mocks.fsAppendFile, readFile: mocks.fsReadFile, stat: mocks.fsStat, + lstat: mocks.fsLstat, + realpath: mocks.fsRealpath, + open: mocks.fsOpen, }; return { ...patched, default: patched }; }); @@ -125,6 +131,33 @@ function createErrnoError(code: string) { return err; } +function makeFileStat(params?: { + size?: number; + mtimeMs?: number; + dev?: number; + ino?: number; +}): import("node:fs").Stats { + return { + isFile: () => true, + isSymbolicLink: () => false, + size: params?.size ?? 10, + mtimeMs: params?.mtimeMs ?? 1234, + dev: params?.dev ?? 1, + ino: params?.ino ?? 1, + } as unknown as import("node:fs").Stats; +} + +function makeSymlinkStat(params?: { dev?: number; ino?: number }): import("node:fs").Stats { + return { + isFile: () => false, + isSymbolicLink: () => true, + size: 0, + mtimeMs: 0, + dev: params?.dev ?? 1, + ino: params?.ino ?? 2, + } as unknown as import("node:fs").Stats; +} + function mockWorkspaceStateRead(params: { onboardingCompletedAt?: string; errorCode?: string; @@ -172,6 +205,19 @@ beforeEach(() => { mocks.fsStat.mockImplementation(async () => { throw createEnoentError(); }); + mocks.fsLstat.mockImplementation(async () => { + throw createEnoentError(); + }); + mocks.fsRealpath.mockImplementation(async (p: string) => p); + mocks.fsOpen.mockImplementation( + async () => + ({ + stat: async () => makeFileStat(), + readFile: async () => Buffer.from(""), + writeFile: async () => {}, + close: async () => {}, + }) as unknown, + ); }); /* ------------------------------------------------------------------ */ @@ -459,3 +505,147 @@ describe("agents.files.list", () => { expect(names).toContain("BOOTSTRAP.md"); }); }); + +describe("agents.files.get/set symlink safety", () => { + beforeEach(() => { + vi.clearAllMocks(); + mocks.loadConfigReturn = {}; + mocks.fsMkdir.mockResolvedValue(undefined); + }); + + it("rejects agents.files.get when allowlisted file symlink escapes workspace", async () => { + const workspace = "/workspace/test-agent"; + const candidate = `${workspace}/AGENTS.md`; + mocks.fsRealpath.mockImplementation(async (p: string) => { + if (p === workspace) { + return workspace; + } + if (p === candidate) { + return "/outside/secret.txt"; + } + return p; + }); + mocks.fsLstat.mockImplementation(async (...args: unknown[]) => { + const p = typeof args[0] === "string" ? args[0] : ""; + if (p === candidate) { + return makeSymlinkStat(); + } + throw createEnoentError(); + }); + + const { respond, promise } = makeCall("agents.files.get", { + agentId: "main", + name: "AGENTS.md", + }); + await promise; + + expect(respond).toHaveBeenCalledWith( + false, + undefined, + expect.objectContaining({ message: expect.stringContaining("unsafe workspace file") }), + ); + }); + + it("rejects agents.files.set when allowlisted file symlink escapes workspace", async () => { + const workspace = "/workspace/test-agent"; + const candidate = `${workspace}/AGENTS.md`; + mocks.fsRealpath.mockImplementation(async (p: string) => { + if (p === workspace) { + return workspace; + } + if (p === candidate) { + return "/outside/secret.txt"; + } + return p; + }); + mocks.fsLstat.mockImplementation(async (...args: unknown[]) => { + const p = typeof args[0] === "string" ? args[0] : ""; + if (p === candidate) { + return makeSymlinkStat(); + } + throw createEnoentError(); + }); + + const { respond, promise } = makeCall("agents.files.set", { + agentId: "main", + name: "AGENTS.md", + content: "x", + }); + await promise; + + expect(respond).toHaveBeenCalledWith( + false, + undefined, + expect.objectContaining({ message: expect.stringContaining("unsafe workspace file") }), + ); + expect(mocks.fsOpen).not.toHaveBeenCalled(); + }); + + it("allows in-workspace symlink targets for get/set", async () => { + const workspace = "/workspace/test-agent"; + const candidate = `${workspace}/AGENTS.md`; + const target = `${workspace}/policies/AGENTS.md`; + const targetStat = makeFileStat({ size: 7, mtimeMs: 1700, dev: 9, ino: 42 }); + + mocks.fsRealpath.mockImplementation(async (p: string) => { + if (p === workspace) { + return workspace; + } + if (p === candidate) { + return target; + } + return p; + }); + mocks.fsLstat.mockImplementation(async (...args: unknown[]) => { + const p = typeof args[0] === "string" ? args[0] : ""; + if (p === candidate) { + return makeSymlinkStat({ dev: 9, ino: 41 }); + } + if (p === target) { + return targetStat; + } + throw createEnoentError(); + }); + mocks.fsStat.mockImplementation(async (...args: unknown[]) => { + const p = typeof args[0] === "string" ? args[0] : ""; + if (p === target) { + return targetStat; + } + throw createEnoentError(); + }); + mocks.fsOpen.mockImplementation( + async () => + ({ + stat: async () => targetStat, + readFile: async () => Buffer.from("inside\n"), + writeFile: async () => {}, + close: async () => {}, + }) as unknown, + ); + + const getCall = makeCall("agents.files.get", { agentId: "main", name: "AGENTS.md" }); + await getCall.promise; + expect(getCall.respond).toHaveBeenCalledWith( + true, + expect.objectContaining({ + file: expect.objectContaining({ missing: false, content: "inside\n" }), + }), + undefined, + ); + + const setCall = makeCall("agents.files.set", { + agentId: "main", + name: "AGENTS.md", + content: "updated\n", + }); + await setCall.promise; + expect(setCall.respond).toHaveBeenCalledWith( + true, + expect.objectContaining({ + ok: true, + file: expect.objectContaining({ missing: false, content: "updated\n" }), + }), + undefined, + ); + }); +}); diff --git a/src/gateway/server-methods/agents.ts b/src/gateway/server-methods/agents.ts index 04a716e077e7..413ffddc8776 100644 --- a/src/gateway/server-methods/agents.ts +++ b/src/gateway/server-methods/agents.ts @@ -1,3 +1,4 @@ +import { constants as fsConstants } from "node:fs"; import fs from "node:fs/promises"; import path from "node:path"; import { @@ -27,6 +28,9 @@ import { } from "../../commands/agents.config.js"; import { loadConfig, writeConfigFile } from "../../config/config.js"; import { resolveSessionTranscriptsDirForAgent } from "../../config/sessions/paths.js"; +import { sameFileIdentity } from "../../infra/file-identity.js"; +import { SafeOpenError, readLocalFileSafely } from "../../infra/fs-safe.js"; +import { isNotFoundPathError, isPathInside } from "../../infra/path-guards.js"; import { DEFAULT_AGENT_ID, normalizeAgentId } from "../../routing/session-key.js"; import { resolveUserPath } from "../../utils.js"; import { @@ -97,10 +101,113 @@ type FileMeta = { updatedAtMs: number; }; -async function statFile(filePath: string): Promise { +type ResolvedAgentWorkspaceFilePath = + | { + kind: "ready"; + requestPath: string; + ioPath: string; + workspaceReal: string; + } + | { + kind: "missing"; + requestPath: string; + ioPath: string; + workspaceReal: string; + } + | { + kind: "invalid"; + requestPath: string; + reason: string; + }; + +const SUPPORTS_NOFOLLOW = process.platform !== "win32" && "O_NOFOLLOW" in fsConstants; +const OPEN_WRITE_FLAGS = + fsConstants.O_WRONLY | + fsConstants.O_CREAT | + fsConstants.O_TRUNC | + (SUPPORTS_NOFOLLOW ? fsConstants.O_NOFOLLOW : 0); + +async function resolveWorkspaceRealPath(workspaceDir: string): Promise { + try { + return await fs.realpath(workspaceDir); + } catch { + return path.resolve(workspaceDir); + } +} + +async function resolveAgentWorkspaceFilePath(params: { + workspaceDir: string; + name: string; + allowMissing: boolean; +}): Promise { + const requestPath = path.join(params.workspaceDir, params.name); + const workspaceReal = await resolveWorkspaceRealPath(params.workspaceDir); + const candidatePath = path.resolve(workspaceReal, params.name); + if (!isPathInside(workspaceReal, candidatePath)) { + return { kind: "invalid", requestPath, reason: "path escapes workspace root" }; + } + + let candidateLstat: Awaited>; + try { + candidateLstat = await fs.lstat(candidatePath); + } catch (err) { + if (isNotFoundPathError(err)) { + if (params.allowMissing) { + return { kind: "missing", requestPath, ioPath: candidatePath, workspaceReal }; + } + return { kind: "invalid", requestPath, reason: "file not found" }; + } + throw err; + } + + if (candidateLstat.isSymbolicLink()) { + let targetReal: string; + try { + targetReal = await fs.realpath(candidatePath); + } catch (err) { + if (isNotFoundPathError(err)) { + if (params.allowMissing) { + return { kind: "missing", requestPath, ioPath: candidatePath, workspaceReal }; + } + return { kind: "invalid", requestPath, reason: "symlink target not found" }; + } + throw err; + } + if (!isPathInside(workspaceReal, targetReal)) { + return { kind: "invalid", requestPath, reason: "symlink target escapes workspace root" }; + } + try { + const targetStat = await fs.stat(targetReal); + if (!targetStat.isFile()) { + return { kind: "invalid", requestPath, reason: "symlink target is not a file" }; + } + } catch (err) { + if (isNotFoundPathError(err) && params.allowMissing) { + return { kind: "missing", requestPath, ioPath: targetReal, workspaceReal }; + } + throw err; + } + return { kind: "ready", requestPath, ioPath: targetReal, workspaceReal }; + } + + if (!candidateLstat.isFile()) { + return { kind: "invalid", requestPath, reason: "path is not a regular file" }; + } + + const candidateReal = await fs.realpath(candidatePath).catch(() => candidatePath); + if (!isPathInside(workspaceReal, candidateReal)) { + return { kind: "invalid", requestPath, reason: "resolved file escapes workspace root" }; + } + return { kind: "ready", requestPath, ioPath: candidateReal, workspaceReal }; +} + +async function statFileSafely(filePath: string): Promise { try { - const stat = await fs.stat(filePath); - if (!stat.isFile()) { + const [stat, lstat] = await Promise.all([fs.stat(filePath), fs.lstat(filePath)]); + if (lstat.isSymbolicLink() || !stat.isFile()) { + return null; + } + if (!sameFileIdentity(stat, lstat)) { return null; } return { @@ -112,6 +219,22 @@ async function statFile(filePath: string): Promise { } } +async function writeFileSafely(filePath: string, content: string): Promise { + const handle = await fs.open(filePath, OPEN_WRITE_FLAGS, 0o600); + try { + const [stat, lstat] = await Promise.all([handle.stat(), fs.lstat(filePath)]); + if (lstat.isSymbolicLink() || !stat.isFile()) { + throw new Error("unsafe file path"); + } + if (!sameFileIdentity(stat, lstat)) { + throw new Error("path changed during write"); + } + await handle.writeFile(content, "utf-8"); + } finally { + await handle.close().catch(() => {}); + } +} + async function listAgentFiles(workspaceDir: string, options?: { hideBootstrap?: boolean }) { const files: Array<{ name: string; @@ -125,8 +248,18 @@ async function listAgentFiles(workspaceDir: string, options?: { hideBootstrap?: ? BOOTSTRAP_FILE_NAMES_POST_ONBOARDING : BOOTSTRAP_FILE_NAMES; for (const name of bootstrapFileNames) { - const filePath = path.join(workspaceDir, name); - const meta = await statFile(filePath); + const resolved = await resolveAgentWorkspaceFilePath({ + workspaceDir, + name, + allowMissing: true, + }); + const filePath = resolved.requestPath; + const meta = + resolved.kind === "ready" + ? await statFileSafely(resolved.ioPath) + : resolved.kind === "missing" + ? null + : null; if (meta) { files.push({ name, @@ -140,29 +273,43 @@ async function listAgentFiles(workspaceDir: string, options?: { hideBootstrap?: } } - const primaryMemoryPath = path.join(workspaceDir, DEFAULT_MEMORY_FILENAME); - const primaryMeta = await statFile(primaryMemoryPath); + const primaryResolved = await resolveAgentWorkspaceFilePath({ + workspaceDir, + name: DEFAULT_MEMORY_FILENAME, + allowMissing: true, + }); + const primaryMeta = + primaryResolved.kind === "ready" ? await statFileSafely(primaryResolved.ioPath) : null; if (primaryMeta) { files.push({ name: DEFAULT_MEMORY_FILENAME, - path: primaryMemoryPath, + path: primaryResolved.requestPath, missing: false, size: primaryMeta.size, updatedAtMs: primaryMeta.updatedAtMs, }); } else { - const altMemoryPath = path.join(workspaceDir, DEFAULT_MEMORY_ALT_FILENAME); - const altMeta = await statFile(altMemoryPath); + const altMemoryResolved = await resolveAgentWorkspaceFilePath({ + workspaceDir, + name: DEFAULT_MEMORY_ALT_FILENAME, + allowMissing: true, + }); + const altMeta = + altMemoryResolved.kind === "ready" ? await statFileSafely(altMemoryResolved.ioPath) : null; if (altMeta) { files.push({ name: DEFAULT_MEMORY_ALT_FILENAME, - path: altMemoryPath, + path: altMemoryResolved.requestPath, missing: false, size: altMeta.size, updatedAtMs: altMeta.updatedAtMs, }); } else { - files.push({ name: DEFAULT_MEMORY_FILENAME, path: primaryMemoryPath, missing: true }); + files.push({ + name: DEFAULT_MEMORY_FILENAME, + path: primaryResolved.requestPath, + missing: true, + }); } } @@ -453,8 +600,23 @@ export const agentsHandlers: GatewayRequestHandlers = { } const { agentId, workspaceDir, name } = resolved; const filePath = path.join(workspaceDir, name); - const meta = await statFile(filePath); - if (!meta) { + const resolvedPath = await resolveAgentWorkspaceFilePath({ + workspaceDir, + name, + allowMissing: true, + }); + if (resolvedPath.kind === "invalid") { + respond( + false, + undefined, + errorShape( + ErrorCodes.INVALID_REQUEST, + `unsafe workspace file "${name}" (${resolvedPath.reason})`, + ), + ); + return; + } + if (resolvedPath.kind === "missing") { respond( true, { @@ -466,7 +628,29 @@ export const agentsHandlers: GatewayRequestHandlers = { ); return; } - const content = await fs.readFile(filePath, "utf-8"); + let safeRead: Awaited>; + try { + safeRead = await readLocalFileSafely({ filePath: resolvedPath.ioPath }); + } catch (err) { + if (err instanceof SafeOpenError && err.code === "not-found") { + respond( + true, + { + agentId, + workspace: workspaceDir, + file: { name, path: filePath, missing: true }, + }, + undefined, + ); + return; + } + respond( + false, + undefined, + errorShape(ErrorCodes.INVALID_REQUEST, `unsafe workspace file "${name}"`), + ); + return; + } respond( true, { @@ -476,9 +660,9 @@ export const agentsHandlers: GatewayRequestHandlers = { name, path: filePath, missing: false, - size: meta.size, - updatedAtMs: meta.updatedAtMs, - content, + size: safeRead.stat.size, + updatedAtMs: Math.floor(safeRead.stat.mtimeMs), + content: safeRead.buffer.toString("utf-8"), }, }, undefined, @@ -505,9 +689,34 @@ export const agentsHandlers: GatewayRequestHandlers = { const { agentId, workspaceDir, name } = resolved; await fs.mkdir(workspaceDir, { recursive: true }); const filePath = path.join(workspaceDir, name); + const resolvedPath = await resolveAgentWorkspaceFilePath({ + workspaceDir, + name, + allowMissing: true, + }); + if (resolvedPath.kind === "invalid") { + respond( + false, + undefined, + errorShape( + ErrorCodes.INVALID_REQUEST, + `unsafe workspace file "${name}" (${resolvedPath.reason})`, + ), + ); + return; + } const content = String(params.content ?? ""); - await fs.writeFile(filePath, content, "utf-8"); - const meta = await statFile(filePath); + try { + await writeFileSafely(resolvedPath.ioPath, content); + } catch { + respond( + false, + undefined, + errorShape(ErrorCodes.INVALID_REQUEST, `unsafe workspace file "${name}"`), + ); + return; + } + const meta = await statFileSafely(resolvedPath.ioPath); respond( true, { From 2011edc9e505ffa59949fb63f2c54a50f6400671 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 25 Feb 2026 23:30:40 +0000 Subject: [PATCH 037/160] fix(gateway): preserve agentId through gateway send path Landed from #23249 by @Sid-Qin. Includes extra regression tests for agentId precedence + blank fallback. Co-authored-by: Sid <201593046+Sid-Qin@users.noreply.github.com> --- CHANGELOG.md | 1 + .../OpenClawProtocol/GatewayModels.swift | 4 + .../OpenClawProtocol/GatewayModels.swift | 4 + src/gateway/protocol/schema/agent.ts | 2 + src/gateway/server-methods/send.test.ts | 90 +++++++++++++++++++ src/gateway/server-methods/send.ts | 23 +++-- src/infra/outbound/message.channels.test.ts | 29 ++++++ src/infra/outbound/message.ts | 1 + 8 files changed, 146 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aee7af5ad7e7..58c250c5c471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Docs: https://docs.openclaw.ai - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. +- Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. diff --git a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift index 4e766514defc..95565a68c4fb 100644 --- a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift @@ -408,6 +408,7 @@ public struct SendParams: Codable, Sendable { public let gifplayback: Bool? public let channel: String? public let accountid: String? + public let agentid: String? public let threadid: String? public let sessionkey: String? public let idempotencykey: String @@ -420,6 +421,7 @@ public struct SendParams: Codable, Sendable { gifplayback: Bool?, channel: String?, accountid: String?, + agentid: String?, threadid: String?, sessionkey: String?, idempotencykey: String) @@ -431,6 +433,7 @@ public struct SendParams: Codable, Sendable { self.gifplayback = gifplayback self.channel = channel self.accountid = accountid + self.agentid = agentid self.threadid = threadid self.sessionkey = sessionkey self.idempotencykey = idempotencykey @@ -444,6 +447,7 @@ public struct SendParams: Codable, Sendable { case gifplayback = "gifPlayback" case channel case accountid = "accountId" + case agentid = "agentId" case threadid = "threadId" case sessionkey = "sessionKey" case idempotencykey = "idempotencyKey" diff --git a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift index 4e766514defc..95565a68c4fb 100644 --- a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift @@ -408,6 +408,7 @@ public struct SendParams: Codable, Sendable { public let gifplayback: Bool? public let channel: String? public let accountid: String? + public let agentid: String? public let threadid: String? public let sessionkey: String? public let idempotencykey: String @@ -420,6 +421,7 @@ public struct SendParams: Codable, Sendable { gifplayback: Bool?, channel: String?, accountid: String?, + agentid: String?, threadid: String?, sessionkey: String?, idempotencykey: String) @@ -431,6 +433,7 @@ public struct SendParams: Codable, Sendable { self.gifplayback = gifplayback self.channel = channel self.accountid = accountid + self.agentid = agentid self.threadid = threadid self.sessionkey = sessionkey self.idempotencykey = idempotencykey @@ -444,6 +447,7 @@ public struct SendParams: Codable, Sendable { case gifplayback = "gifPlayback" case channel case accountid = "accountId" + case agentid = "agentId" case threadid = "threadId" case sessionkey = "sessionKey" case idempotencykey = "idempotencyKey" diff --git a/src/gateway/protocol/schema/agent.ts b/src/gateway/protocol/schema/agent.ts index b8c883f7f531..1508c38f70e2 100644 --- a/src/gateway/protocol/schema/agent.ts +++ b/src/gateway/protocol/schema/agent.ts @@ -22,6 +22,8 @@ export const SendParamsSchema = Type.Object( gifPlayback: Type.Optional(Type.Boolean()), channel: Type.Optional(Type.String()), accountId: Type.Optional(Type.String()), + /** Optional agent id for per-agent media root resolution on gateway sends. */ + agentId: Type.Optional(Type.String()), /** Thread id (channel-specific meaning, e.g. Telegram forum topic id). */ threadId: Type.Optional(Type.String()), /** Optional session key for mirroring delivered output back into the transcript. */ diff --git a/src/gateway/server-methods/send.test.ts b/src/gateway/server-methods/send.test.ts index 7209d3e61763..7734de8e911b 100644 --- a/src/gateway/server-methods/send.test.ts +++ b/src/gateway/server-methods/send.test.ts @@ -342,6 +342,96 @@ describe("gateway send mirroring", () => { ); }); + it("uses explicit agentId for delivery when sessionKey is not provided", async () => { + mockDeliverySuccess("m-agent"); + + await runSend({ + to: "channel:C1", + message: "hello", + channel: "slack", + agentId: "work", + idempotencyKey: "idem-agent-explicit", + }); + + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( + expect.objectContaining({ + agentId: "work", + mirror: expect.objectContaining({ + sessionKey: "agent:work:slack:channel:resolved", + agentId: "work", + }), + }), + ); + }); + + it("uses sessionKey agentId when explicit agentId is omitted", async () => { + mockDeliverySuccess("m-session-agent"); + + await runSend({ + to: "channel:C1", + message: "hello", + channel: "slack", + sessionKey: "agent:work:slack:channel:c1", + idempotencyKey: "idem-session-agent", + }); + + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( + expect.objectContaining({ + agentId: "work", + mirror: expect.objectContaining({ + sessionKey: "agent:work:slack:channel:c1", + agentId: "work", + }), + }), + ); + }); + + it("prefers explicit agentId over sessionKey agent for delivery and mirror", async () => { + mockDeliverySuccess("m-agent-precedence"); + + await runSend({ + to: "channel:C1", + message: "hello", + channel: "slack", + agentId: "work", + sessionKey: "agent:main:slack:channel:c1", + idempotencyKey: "idem-agent-precedence", + }); + + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( + expect.objectContaining({ + agentId: "work", + mirror: expect.objectContaining({ + sessionKey: "agent:main:slack:channel:c1", + agentId: "work", + }), + }), + ); + }); + + it("ignores blank explicit agentId and falls back to sessionKey agent", async () => { + mockDeliverySuccess("m-agent-blank"); + + await runSend({ + to: "channel:C1", + message: "hello", + channel: "slack", + agentId: " ", + sessionKey: "agent:work:slack:channel:c1", + idempotencyKey: "idem-agent-blank", + }); + + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( + expect.objectContaining({ + agentId: "work", + mirror: expect.objectContaining({ + sessionKey: "agent:work:slack:channel:c1", + agentId: "work", + }), + }), + ); + }); + it("forwards threadId to outbound delivery when provided", async () => { mockDeliverySuccess("m-thread"); diff --git a/src/gateway/server-methods/send.ts b/src/gateway/server-methods/send.ts index c404a47032ac..9e976a79ae17 100644 --- a/src/gateway/server-methods/send.ts +++ b/src/gateway/server-methods/send.ts @@ -106,6 +106,7 @@ export const sendHandlers: GatewayRequestHandlers = { gifPlayback?: boolean; channel?: string; accountId?: string; + agentId?: string; threadId?: string; sessionKey?: string; idempotencyKey: string; @@ -206,13 +207,21 @@ export const sendHandlers: GatewayRequestHandlers = { typeof request.sessionKey === "string" && request.sessionKey.trim() ? request.sessionKey.trim().toLowerCase() : undefined; - const derivedAgentId = resolveSessionAgentId({ config: cfg }); + const explicitAgentId = + typeof request.agentId === "string" && request.agentId.trim() + ? request.agentId.trim() + : undefined; + const sessionAgentId = providedSessionKey + ? resolveSessionAgentId({ sessionKey: providedSessionKey, config: cfg }) + : undefined; + const defaultAgentId = resolveSessionAgentId({ config: cfg }); + const effectiveAgentId = explicitAgentId ?? sessionAgentId ?? defaultAgentId; // If callers omit sessionKey, derive a target session key from the outbound route. const derivedRoute = !providedSessionKey ? await resolveOutboundSessionRoute({ cfg, channel, - agentId: derivedAgentId, + agentId: effectiveAgentId, accountId, target: resolved.to, threadId, @@ -221,7 +230,7 @@ export const sendHandlers: GatewayRequestHandlers = { if (derivedRoute) { await ensureOutboundSessionEntry({ cfg, - agentId: derivedAgentId, + agentId: effectiveAgentId, channel, accountId, route: derivedRoute, @@ -233,23 +242,21 @@ export const sendHandlers: GatewayRequestHandlers = { to: resolved.to, accountId, payloads: [{ text: message, mediaUrl, mediaUrls }], - agentId: providedSessionKey - ? resolveSessionAgentId({ sessionKey: providedSessionKey, config: cfg }) - : derivedAgentId, + agentId: effectiveAgentId, gifPlayback: request.gifPlayback, threadId: threadId ?? null, deps: outboundDeps, mirror: providedSessionKey ? { sessionKey: providedSessionKey, - agentId: resolveSessionAgentId({ sessionKey: providedSessionKey, config: cfg }), + agentId: effectiveAgentId, text: mirrorText || message, mediaUrls: mirrorMediaUrls.length > 0 ? mirrorMediaUrls : undefined, } : derivedRoute ? { sessionKey: derivedRoute.sessionKey, - agentId: derivedAgentId, + agentId: effectiveAgentId, text: mirrorText || message, mediaUrls: mirrorMediaUrls.length > 0 ? mirrorMediaUrls : undefined, } diff --git a/src/infra/outbound/message.channels.test.ts b/src/infra/outbound/message.channels.test.ts index 39e83c8ad70f..12b9b120f66f 100644 --- a/src/infra/outbound/message.channels.test.ts +++ b/src/infra/outbound/message.channels.test.ts @@ -194,6 +194,35 @@ describe("gateway url override hardening", () => { }), ); }); + + it("forwards explicit agentId in gateway send params", async () => { + setRegistry( + createTestRegistry([ + { + pluginId: "mattermost", + source: "test", + plugin: { + ...createMattermostLikePlugin({ onSendText: () => {} }), + outbound: { deliveryMode: "gateway" }, + }, + }, + ]), + ); + + callGatewayMock.mockResolvedValueOnce({ messageId: "m-agent" }); + await sendMessage({ + cfg: {}, + to: "channel:town-square", + content: "hi", + channel: "mattermost", + agentId: "work", + }); + + const call = callGatewayMock.mock.calls[0]?.[0] as { + params?: Record; + }; + expect(call.params?.agentId).toBe("work"); + }); }); const emptyRegistry = createTestRegistry([]); diff --git a/src/infra/outbound/message.ts b/src/infra/outbound/message.ts index 71b36eca6b19..649aabd0ecef 100644 --- a/src/infra/outbound/message.ts +++ b/src/infra/outbound/message.ts @@ -251,6 +251,7 @@ export async function sendMessage(params: MessageSendParams): Promise Date: Wed, 25 Feb 2026 18:34:31 -0500 Subject: [PATCH 038/160] fix: cron model fallback to agent defaults when payload.model fails (#26717) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 06454bd55b44ea864c10ad828649b293946cea8d Co-authored-by: Youyou972 <50808411+Youyou972@users.noreply.github.com> Co-authored-by: shakkernerd <165377636+shakkernerd@users.noreply.github.com> Reviewed-by: @shakkernerd --- CHANGELOG.md | 1 + .../isolated-agent/run.skill-filter.test.ts | 103 ++++++++++++++++-- src/cron/isolated-agent/run.ts | 13 ++- 3 files changed, 105 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58c250c5c471..963892e9ff52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Docs: https://docs.openclaw.ai - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. +- Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. diff --git a/src/cron/isolated-agent/run.skill-filter.test.ts b/src/cron/isolated-agent/run.skill-filter.test.ts index 02d986819d98..2b6e4bbf7be7 100644 --- a/src/cron/isolated-agent/run.skill-filter.test.ts +++ b/src/cron/isolated-agent/run.skill-filter.test.ts @@ -6,6 +6,13 @@ import { runWithModelFallback } from "../../agents/model-fallback.js"; const buildWorkspaceSkillSnapshotMock = vi.fn(); const resolveAgentConfigMock = vi.fn(); const resolveAgentSkillsFilterMock = vi.fn(); +const getModelRefStatusMock = vi.fn().mockReturnValue({ allowed: false }); +const isCliProviderMock = vi.fn().mockReturnValue(false); +const resolveAllowedModelRefMock = vi.fn(); +const resolveConfiguredModelRefMock = vi.fn(); +const resolveHooksGmailModelMock = vi.fn(); +const resolveThinkingDefaultMock = vi.fn(); +const logWarnMock = vi.fn(); vi.mock("../../agents/agent-scope.js", () => ({ resolveAgentConfig: resolveAgentConfigMock, @@ -36,14 +43,12 @@ vi.mock("../../agents/model-selection.js", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, - getModelRefStatus: vi.fn().mockReturnValue({ allowed: false }), - isCliProvider: vi.fn().mockReturnValue(false), - resolveAllowedModelRef: vi - .fn() - .mockReturnValue({ ref: { provider: "openai", model: "gpt-4" } }), - resolveConfiguredModelRef: vi.fn().mockReturnValue({ provider: "openai", model: "gpt-4" }), - resolveHooksGmailModel: vi.fn().mockReturnValue(null), - resolveThinkingDefault: vi.fn().mockReturnValue(undefined), + getModelRefStatus: getModelRefStatusMock, + isCliProvider: isCliProviderMock, + resolveAllowedModelRef: resolveAllowedModelRefMock, + resolveConfiguredModelRef: resolveConfiguredModelRefMock, + resolveHooksGmailModel: resolveHooksGmailModelMock, + resolveThinkingDefault: resolveThinkingDefaultMock, }; }); @@ -138,7 +143,7 @@ vi.mock("../../infra/skills-remote.js", () => ({ })); vi.mock("../../logger.js", () => ({ - logWarn: vi.fn(), + logWarn: (...args: unknown[]) => logWarnMock(...args), })); vi.mock("../../security/external-content.js", () => ({ @@ -222,6 +227,13 @@ describe("runCronIsolatedAgentTurn — skill filter", () => { }); resolveAgentConfigMock.mockReturnValue(undefined); resolveAgentSkillsFilterMock.mockReturnValue(undefined); + resolveConfiguredModelRefMock.mockReturnValue({ provider: "openai", model: "gpt-4" }); + resolveAllowedModelRefMock.mockReturnValue({ ref: { provider: "openai", model: "gpt-4" } }); + resolveHooksGmailModelMock.mockReturnValue(null); + resolveThinkingDefaultMock.mockReturnValue(undefined); + getModelRefStatusMock.mockReturnValue({ allowed: false }); + isCliProviderMock.mockReturnValue(false); + logWarnMock.mockReset(); // Fresh session object per test — prevents mutation leaking between tests resolveCronSessionMock.mockReturnValue({ storePath: "/tmp/store.json", @@ -408,5 +420,78 @@ describe("runCronIsolatedAgentTurn — skill filter", () => { it("preserves defaults when agent overrides primary in object form", async () => { await expectPrimaryOverridePreservesDefaults({ primary: "anthropic/claude-sonnet-4-5" }); }); + + it("applies payload.model override when model is allowed", async () => { + resolveAllowedModelRefMock.mockReturnValueOnce({ + ref: { provider: "anthropic", model: "claude-sonnet-4-6" }, + }); + + const result = await runCronIsolatedAgentTurn( + makeParams({ + job: makeJob({ + payload: { kind: "agentTurn", message: "test", model: "anthropic/claude-sonnet-4-6" }, + }), + }), + ); + + expect(result.status).toBe("ok"); + expect(logWarnMock).not.toHaveBeenCalled(); + expect(runWithModelFallbackMock).toHaveBeenCalledOnce(); + const runParams = runWithModelFallbackMock.mock.calls[0][0]; + expect(runParams.provider).toBe("anthropic"); + expect(runParams.model).toBe("claude-sonnet-4-6"); + }); + + it("falls back to agent defaults when payload.model is not allowed", async () => { + resolveAllowedModelRefMock.mockReturnValueOnce({ + error: "model not allowed: anthropic/claude-sonnet-4-6", + }); + + const result = await runCronIsolatedAgentTurn( + makeParams({ + cfg: { + agents: { + defaults: { + model: { primary: "openai-codex/gpt-5.3-codex", fallbacks: defaultFallbacks }, + }, + }, + }, + job: makeJob({ + payload: { kind: "agentTurn", message: "test", model: "anthropic/claude-sonnet-4-6" }, + }), + }), + ); + + expect(result.status).toBe("ok"); + expect(logWarnMock).toHaveBeenCalledWith( + "cron: payload.model 'anthropic/claude-sonnet-4-6' not allowed, falling back to agent defaults", + ); + expect(runWithModelFallbackMock).toHaveBeenCalledOnce(); + const callCfg = runWithModelFallbackMock.mock.calls[0][0].cfg; + const model = callCfg?.agents?.defaults?.model as + | { primary?: string; fallbacks?: string[] } + | undefined; + expect(model?.primary).toBe("openai-codex/gpt-5.3-codex"); + expect(model?.fallbacks).toEqual(defaultFallbacks); + }); + + it("returns an error when payload.model is invalid", async () => { + resolveAllowedModelRefMock.mockReturnValueOnce({ + error: "invalid model: openai/", + }); + + const result = await runCronIsolatedAgentTurn( + makeParams({ + job: makeJob({ + payload: { kind: "agentTurn", message: "test", model: "openai/" }, + }), + }), + ); + + expect(result.status).toBe("error"); + expect(result.error).toBe("invalid model: openai/"); + expect(logWarnMock).not.toHaveBeenCalled(); + expect(runWithModelFallbackMock).not.toHaveBeenCalled(); + }); }); }); diff --git a/src/cron/isolated-agent/run.ts b/src/cron/isolated-agent/run.ts index dd5c28ae616f..a4a14bc26b87 100644 --- a/src/cron/isolated-agent/run.ts +++ b/src/cron/isolated-agent/run.ts @@ -198,10 +198,17 @@ export async function runCronIsolatedAgentTurn(params: { defaultModel: resolvedDefault.model, }); if ("error" in resolvedOverride) { - return { status: "error", error: resolvedOverride.error }; + if (resolvedOverride.error.startsWith("model not allowed:")) { + logWarn( + `cron: payload.model '${modelOverride}' not allowed, falling back to agent defaults`, + ); + } else { + return { status: "error", error: resolvedOverride.error }; + } + } else { + provider = resolvedOverride.ref.provider; + model = resolvedOverride.ref.model; } - provider = resolvedOverride.ref.provider; - model = resolvedOverride.ref.model; } const now = Date.now(); const cronSession = resolveCronSession({ From ef326f5cd0f761e02d5a02339be64ccfe1b96102 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:40:43 +0100 Subject: [PATCH 039/160] fix(browser): revalidate upload paths at use time --- CHANGELOG.md | 1 + src/browser/paths.test.ts | 24 ++++ src/browser/paths.ts | 25 +++- src/browser/pw-tools-core.downloads.ts | 16 ++- ...-core.interactions.set-input-files.test.ts | 111 ++++++++++++++++++ src/browser/pw-tools-core.interactions.ts | 12 +- ...ls-core.last-file-chooser-arm-wins.test.ts | 53 ++++++--- ...-core.screenshots-element-selector.test.ts | 48 +++++++- 8 files changed, 264 insertions(+), 26 deletions(-) create mode 100644 src/browser/pw-tools-core.interactions.set-input-files.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 963892e9ff52..c8d520827bff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Docs: https://docs.openclaw.ai - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. - Security/Microsoft Teams: isolate group allowlist and command authorization from DM pairing-store entries to prevent cross-context authorization bleed. (#26111) Thanks @bmendonca3. +- Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before `setFiles`, with regression coverage for strict missing-path handling. - Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3. - Agents/Model fallback: keep explicit text + image fallback chains reachable even when `agents.defaults.models` allowlists are present, prefer explicit run `agentId` over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify `model_cooldown` / `cooling down` errors as `rate_limit` so failover continues. (#11972, #24137, #17231) - Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. diff --git a/src/browser/paths.test.ts b/src/browser/paths.test.ts index 441ee05b8694..1599c3895b2a 100644 --- a/src/browser/paths.test.ts +++ b/src/browser/paths.test.ts @@ -6,6 +6,7 @@ import { resolveExistingPathsWithinRoot, resolvePathsWithinRoot, resolvePathWithinRoot, + resolveStrictExistingPathsWithinRoot, } from "./paths.js"; async function createFixtureRoot(): Promise<{ baseDir: string; uploadsDir: string }> { @@ -194,6 +195,29 @@ describe("resolveExistingPathsWithinRoot", () => { ); }); +describe("resolveStrictExistingPathsWithinRoot", () => { + function expectInvalidResult( + result: Awaited>, + expectedSnippet: string, + ) { + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toContain(expectedSnippet); + } + } + + it("rejects missing files instead of returning lexical fallbacks", async () => { + await withFixtureRoot(async ({ uploadsDir }) => { + const result = await resolveStrictExistingPathsWithinRoot({ + rootDir: uploadsDir, + requestedPaths: ["missing.txt"], + scopeLabel: "uploads directory", + }); + expectInvalidResult(result, "regular non-symlink file"); + }); + }); +}); + describe("resolvePathWithinRoot", () => { it("uses default file name when requested path is blank", () => { const result = resolvePathWithinRoot({ diff --git a/src/browser/paths.ts b/src/browser/paths.ts index 0b458e44dece..88a541b75dce 100644 --- a/src/browser/paths.ts +++ b/src/browser/paths.ts @@ -54,6 +54,29 @@ export async function resolveExistingPathsWithinRoot(params: { rootDir: string; requestedPaths: string[]; scopeLabel: string; +}): Promise<{ ok: true; paths: string[] } | { ok: false; error: string }> { + return await resolveCheckedPathsWithinRoot({ + ...params, + allowMissingFallback: true, + }); +} + +export async function resolveStrictExistingPathsWithinRoot(params: { + rootDir: string; + requestedPaths: string[]; + scopeLabel: string; +}): Promise<{ ok: true; paths: string[] } | { ok: false; error: string }> { + return await resolveCheckedPathsWithinRoot({ + ...params, + allowMissingFallback: false, + }); +} + +async function resolveCheckedPathsWithinRoot(params: { + rootDir: string; + requestedPaths: string[]; + scopeLabel: string; + allowMissingFallback: boolean; }): Promise<{ ok: true; paths: string[] } | { ok: false; error: string }> { const rootDir = path.resolve(params.rootDir); let rootRealPath: string | undefined; @@ -119,7 +142,7 @@ export async function resolveExistingPathsWithinRoot(params: { }); resolvedPaths.push(opened.realPath); } catch (err) { - if (err instanceof SafeOpenError && err.code === "not-found") { + if (params.allowMissingFallback && err instanceof SafeOpenError && err.code === "not-found") { // Preserve historical behavior for paths that do not exist yet. resolvedPaths.push(pathResult.fallbackPath); continue; diff --git a/src/browser/pw-tools-core.downloads.ts b/src/browser/pw-tools-core.downloads.ts index 12be321653bf..4933c78b5e49 100644 --- a/src/browser/pw-tools-core.downloads.ts +++ b/src/browser/pw-tools-core.downloads.ts @@ -3,6 +3,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import type { Page } from "playwright-core"; import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js"; +import { DEFAULT_UPLOAD_DIR, resolveStrictExistingPathsWithinRoot } from "./paths.js"; import { ensurePageState, getPageForTargetId, @@ -166,7 +167,20 @@ export async function armFileUploadViaPlaywright(opts: { } return; } - await fileChooser.setFiles(opts.paths); + const uploadPathsResult = await resolveStrictExistingPathsWithinRoot({ + rootDir: DEFAULT_UPLOAD_DIR, + requestedPaths: opts.paths, + scopeLabel: `uploads directory (${DEFAULT_UPLOAD_DIR})`, + }); + if (!uploadPathsResult.ok) { + try { + await page.keyboard.press("Escape"); + } catch { + // Best-effort. + } + return; + } + await fileChooser.setFiles(uploadPathsResult.paths); try { const input = typeof fileChooser.element === "function" diff --git a/src/browser/pw-tools-core.interactions.set-input-files.test.ts b/src/browser/pw-tools-core.interactions.set-input-files.test.ts new file mode 100644 index 000000000000..dfbd6f585631 --- /dev/null +++ b/src/browser/pw-tools-core.interactions.set-input-files.test.ts @@ -0,0 +1,111 @@ +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; + +let page: Record | null = null; +let locator: Record | null = null; + +const getPageForTargetId = vi.fn(async () => { + if (!page) { + throw new Error("test: page not set"); + } + return page; +}); +const ensurePageState = vi.fn(() => ({})); +const restoreRoleRefsForTarget = vi.fn(() => {}); +const refLocator = vi.fn(() => { + if (!locator) { + throw new Error("test: locator not set"); + } + return locator; +}); +const forceDisconnectPlaywrightForTarget = vi.fn(async () => {}); + +const resolveStrictExistingPathsWithinRoot = + vi.fn(); + +vi.mock("./pw-session.js", () => { + return { + ensurePageState, + forceDisconnectPlaywrightForTarget, + getPageForTargetId, + refLocator, + restoreRoleRefsForTarget, + }; +}); + +vi.mock("./paths.js", () => { + return { + DEFAULT_UPLOAD_DIR: "/tmp/openclaw/uploads", + resolveStrictExistingPathsWithinRoot, + }; +}); + +let setInputFilesViaPlaywright: typeof import("./pw-tools-core.interactions.js").setInputFilesViaPlaywright; + +describe("setInputFilesViaPlaywright", () => { + beforeAll(async () => { + ({ setInputFilesViaPlaywright } = await import("./pw-tools-core.interactions.js")); + }); + + beforeEach(() => { + vi.clearAllMocks(); + page = null; + locator = null; + resolveStrictExistingPathsWithinRoot.mockResolvedValue({ + ok: true, + paths: ["/private/tmp/openclaw/uploads/ok.txt"], + }); + }); + + it("revalidates upload paths and uses resolved canonical paths for inputRef", async () => { + const setInputFiles = vi.fn(async () => {}); + locator = { + setInputFiles, + elementHandle: vi.fn(async () => null), + }; + page = { + locator: vi.fn(() => ({ first: () => locator })), + }; + + await setInputFilesViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + inputRef: "e7", + paths: ["/tmp/openclaw/uploads/ok.txt"], + }); + + expect(resolveStrictExistingPathsWithinRoot).toHaveBeenCalledWith({ + rootDir: "/tmp/openclaw/uploads", + requestedPaths: ["/tmp/openclaw/uploads/ok.txt"], + scopeLabel: "uploads directory (/tmp/openclaw/uploads)", + }); + expect(refLocator).toHaveBeenCalledWith(page, "e7"); + expect(setInputFiles).toHaveBeenCalledWith(["/private/tmp/openclaw/uploads/ok.txt"]); + }); + + it("throws and skips setInputFiles when use-time validation fails", async () => { + resolveStrictExistingPathsWithinRoot.mockResolvedValueOnce({ + ok: false, + error: "Invalid path: must stay within uploads directory", + }); + + const setInputFiles = vi.fn(async () => {}); + locator = { + setInputFiles, + elementHandle: vi.fn(async () => null), + }; + page = { + locator: vi.fn(() => ({ first: () => locator })), + }; + + await expect( + setInputFilesViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + element: "input[type=file]", + paths: ["/tmp/openclaw/uploads/missing.txt"], + }), + ).rejects.toThrow("Invalid path: must stay within uploads directory"); + + expect(setInputFiles).not.toHaveBeenCalled(); + }); +}); diff --git a/src/browser/pw-tools-core.interactions.ts b/src/browser/pw-tools-core.interactions.ts index 55e130c580ee..cd6ad0e165ca 100644 --- a/src/browser/pw-tools-core.interactions.ts +++ b/src/browser/pw-tools-core.interactions.ts @@ -1,4 +1,5 @@ import type { BrowserFormField } from "./client-actions-core.js"; +import { DEFAULT_UPLOAD_DIR, resolveStrictExistingPathsWithinRoot } from "./paths.js"; import { ensurePageState, forceDisconnectPlaywrightForTarget, @@ -626,9 +627,18 @@ export async function setInputFilesViaPlaywright(opts: { } const locator = inputRef ? refLocator(page, inputRef) : page.locator(element).first(); + const uploadPathsResult = await resolveStrictExistingPathsWithinRoot({ + rootDir: DEFAULT_UPLOAD_DIR, + requestedPaths: opts.paths, + scopeLabel: `uploads directory (${DEFAULT_UPLOAD_DIR})`, + }); + if (!uploadPathsResult.ok) { + throw new Error(uploadPathsResult.error); + } + const resolvedPaths = uploadPathsResult.paths; try { - await locator.setInputFiles(opts.paths); + await locator.setInputFiles(resolvedPaths); } catch (err) { throw toAIFriendlyError(err, inputRef || element); } diff --git a/src/browser/pw-tools-core.last-file-chooser-arm-wins.test.ts b/src/browser/pw-tools-core.last-file-chooser-arm-wins.test.ts index 3afbb2b9d40a..16264ba9eb32 100644 --- a/src/browser/pw-tools-core.last-file-chooser-arm-wins.test.ts +++ b/src/browser/pw-tools-core.last-file-chooser-arm-wins.test.ts @@ -1,4 +1,8 @@ +import crypto from "node:crypto"; +import fs from "node:fs/promises"; +import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { DEFAULT_UPLOAD_DIR } from "./paths.js"; import { installPwToolsCoreTestHooks, setPwToolsCoreCurrentPage, @@ -9,6 +13,15 @@ const mod = await import("./pw-tools-core.js"); describe("pw-tools-core", () => { it("last file-chooser arm wins", async () => { + const firstPath = path.join(DEFAULT_UPLOAD_DIR, `vitest-arm-1-${crypto.randomUUID()}.txt`); + const secondPath = path.join(DEFAULT_UPLOAD_DIR, `vitest-arm-2-${crypto.randomUUID()}.txt`); + await fs.mkdir(DEFAULT_UPLOAD_DIR, { recursive: true }); + await Promise.all([ + fs.writeFile(firstPath, "1", "utf8"), + fs.writeFile(secondPath, "2", "utf8"), + ]); + const secondCanonicalPath = await fs.realpath(secondPath); + let resolve1: ((value: unknown) => void) | null = null; let resolve2: ((value: unknown) => void) | null = null; @@ -35,24 +48,30 @@ describe("pw-tools-core", () => { keyboard: { press: vi.fn(async () => {}) }, }); - await mod.armFileUploadViaPlaywright({ - cdpUrl: "http://127.0.0.1:18792", - paths: ["/tmp/1"], - }); - await mod.armFileUploadViaPlaywright({ - cdpUrl: "http://127.0.0.1:18792", - paths: ["/tmp/2"], - }); - - if (!resolve1 || !resolve2) { - throw new Error("file chooser handlers were not registered"); + try { + await mod.armFileUploadViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + paths: [firstPath], + }); + await mod.armFileUploadViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + paths: [secondPath], + }); + + if (!resolve1 || !resolve2) { + throw new Error("file chooser handlers were not registered"); + } + (resolve1 as (value: unknown) => void)(fc1); + (resolve2 as (value: unknown) => void)(fc2); + await Promise.resolve(); + + expect(fc1.setFiles).not.toHaveBeenCalled(); + await vi.waitFor(() => { + expect(fc2.setFiles).toHaveBeenCalledWith([secondCanonicalPath]); + }); + } finally { + await Promise.all([fs.rm(firstPath, { force: true }), fs.rm(secondPath, { force: true })]); } - (resolve1 as (value: unknown) => void)(fc1); - (resolve2 as (value: unknown) => void)(fc2); - await Promise.resolve(); - - expect(fc1.setFiles).not.toHaveBeenCalled(); - expect(fc2.setFiles).toHaveBeenCalledWith(["/tmp/2"]); }); it("arms the next dialog and accepts/dismisses (default timeout)", async () => { const accept = vi.fn(async () => {}); diff --git a/src/browser/pw-tools-core.screenshots-element-selector.test.ts b/src/browser/pw-tools-core.screenshots-element-selector.test.ts index 843d07050fb8..1894d65912f2 100644 --- a/src/browser/pw-tools-core.screenshots-element-selector.test.ts +++ b/src/browser/pw-tools-core.screenshots-element-selector.test.ts @@ -1,4 +1,8 @@ +import crypto from "node:crypto"; +import fs from "node:fs/promises"; +import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { DEFAULT_UPLOAD_DIR } from "./paths.js"; import { getPwToolsCoreSessionMocks, installPwToolsCoreTestHooks, @@ -81,6 +85,10 @@ describe("pw-tools-core", () => { ).rejects.toThrow(/fullPage is not supported/i); }); it("arms the next file chooser and sets files (default timeout)", async () => { + const uploadPath = path.join(DEFAULT_UPLOAD_DIR, `vitest-upload-${crypto.randomUUID()}.txt`); + await fs.mkdir(path.dirname(uploadPath), { recursive: true }); + await fs.writeFile(uploadPath, "fixture", "utf8"); + const canonicalUploadPath = await fs.realpath(uploadPath); const fileChooser = { setFiles: vi.fn(async () => {}) }; const waitForEvent = vi.fn(async (_event: string, _opts: unknown) => fileChooser); setPwToolsCoreCurrentPage({ @@ -88,19 +96,47 @@ describe("pw-tools-core", () => { keyboard: { press: vi.fn(async () => {}) }, }); + try { + await mod.armFileUploadViaPlaywright({ + cdpUrl: "http://127.0.0.1:18792", + targetId: "T1", + paths: [uploadPath], + }); + + // waitForEvent is awaited immediately; handler continues async. + await Promise.resolve(); + + expect(waitForEvent).toHaveBeenCalledWith("filechooser", { + timeout: 120_000, + }); + await vi.waitFor(() => { + expect(fileChooser.setFiles).toHaveBeenCalledWith([canonicalUploadPath]); + }); + } finally { + await fs.rm(uploadPath, { force: true }); + } + }); + it("revalidates file-chooser paths at use-time and cancels missing files", async () => { + const missingPath = path.join(DEFAULT_UPLOAD_DIR, `vitest-missing-${crypto.randomUUID()}.txt`); + const fileChooser = { setFiles: vi.fn(async () => {}) }; + const press = vi.fn(async () => {}); + const waitForEvent = vi.fn(async () => fileChooser); + setPwToolsCoreCurrentPage({ + waitForEvent, + keyboard: { press }, + }); + await mod.armFileUploadViaPlaywright({ cdpUrl: "http://127.0.0.1:18792", targetId: "T1", - paths: ["/tmp/a.txt"], + paths: [missingPath], }); - - // waitForEvent is awaited immediately; handler continues async. await Promise.resolve(); - expect(waitForEvent).toHaveBeenCalledWith("filechooser", { - timeout: 120_000, + await vi.waitFor(() => { + expect(press).toHaveBeenCalledWith("Escape"); }); - expect(fileChooser.setFiles).toHaveBeenCalledWith(["/tmp/a.txt"]); + expect(fileChooser.setFiles).not.toHaveBeenCalled(); }); it("arms the next file chooser and escapes if no paths provided", async () => { const fileChooser = { setFiles: vi.fn(async () => {}) }; From 2aa7842adeedef423be7ce283a9144b9f1a0a669 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:44:36 +0100 Subject: [PATCH 040/160] fix(signal): enforce auth before reaction notification enqueue --- CHANGELOG.md | 1 + ...ends-tool-summaries-responseprefix.test.ts | 59 ++++++++++++++ src/signal/monitor/event-handler.ts | 78 ++++++++++++------- 3 files changed, 110 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d520827bff..d5cbd2384e8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. diff --git a/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts b/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts index 429f9e3896cd..cc927fe2b36a 100644 --- a/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts +++ b/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts @@ -378,6 +378,65 @@ describe("monitorSignalProvider tool results", () => { expect(events.some((text) => text.includes("Signal reaction added"))).toBe(true); }); + it("blocks reaction notifications from unauthorized senders when dmPolicy is allowlist", async () => { + setReactionNotificationConfig("all", { + dmPolicy: "allowlist", + allowFrom: ["+15550007777"], + }); + await receiveSingleEnvelope({ + ...makeBaseEnvelope(), + reactionMessage: { + emoji: "✅", + targetAuthor: "+15550002222", + targetSentTimestamp: 2, + }, + }); + + const events = getDirectSignalEventsFor("+15550001111"); + expect(events.some((text) => text.includes("Signal reaction added"))).toBe(false); + expect(sendMock).not.toHaveBeenCalled(); + expect(upsertPairingRequestMock).not.toHaveBeenCalled(); + }); + + it("blocks reaction notifications from unauthorized senders when dmPolicy is pairing", async () => { + setReactionNotificationConfig("own", { + dmPolicy: "pairing", + allowFrom: [], + account: "+15550009999", + }); + await receiveSingleEnvelope({ + ...makeBaseEnvelope(), + reactionMessage: { + emoji: "✅", + targetAuthor: "+15550009999", + targetSentTimestamp: 2, + }, + }); + + const events = getDirectSignalEventsFor("+15550001111"); + expect(events.some((text) => text.includes("Signal reaction added"))).toBe(false); + expect(sendMock).not.toHaveBeenCalled(); + expect(upsertPairingRequestMock).not.toHaveBeenCalled(); + }); + + it("allows reaction notifications for allowlisted senders when dmPolicy is allowlist", async () => { + setReactionNotificationConfig("all", { + dmPolicy: "allowlist", + allowFrom: ["+15550001111"], + }); + await receiveSingleEnvelope({ + ...makeBaseEnvelope(), + reactionMessage: { + emoji: "✅", + targetAuthor: "+15550002222", + targetSentTimestamp: 2, + }, + }); + + const events = getDirectSignalEventsFor("+15550001111"); + expect(events.some((text) => text.includes("Signal reaction added"))).toBe(true); + }); + it("notifies on own reactions when target includes uuid + phone", async () => { setReactionNotificationConfig("own", { account: "+15550002222" }); await receiveSingleEnvelope({ diff --git a/src/signal/monitor/event-handler.ts b/src/signal/monitor/event-handler.ts index b095626ab460..3cdd8cf5e9dc 100644 --- a/src/signal/monitor/event-handler.ts +++ b/src/signal/monitor/event-handler.ts @@ -36,6 +36,10 @@ import { upsertChannelPairingRequest, } from "../../pairing/pairing-store.js"; import { resolveAgentRoute } from "../../routing/resolve-route.js"; +import { + resolveDmGroupAccessDecision, + resolveEffectiveAllowFromLists, +} from "../../security/dm-policy-shared.js"; import { normalizeE164 } from "../../utils.js"; import { formatSignalPairingIdLine, @@ -366,15 +370,45 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { const quoteText = dataMessage?.quote?.text?.trim() ?? ""; const hasBodyContent = Boolean(messageText || quoteText) || Boolean(!reaction && dataMessage?.attachments?.length); + const senderDisplay = formatSignalSenderDisplay(sender); + const storeAllowFrom = + deps.dmPolicy === "allowlist" + ? [] + : await readChannelAllowFromStore("signal").catch(() => []); + const { effectiveAllowFrom: effectiveDmAllow } = resolveEffectiveAllowFromLists({ + allowFrom: deps.allowFrom, + groupAllowFrom: deps.groupAllowFrom, + storeAllowFrom, + dmPolicy: deps.dmPolicy, + }); + const effectiveGroupAllow = [...deps.groupAllowFrom, ...storeAllowFrom]; + const resolveAccessDecision = (isGroup: boolean) => + resolveDmGroupAccessDecision({ + isGroup, + dmPolicy: deps.dmPolicy, + groupPolicy: deps.groupPolicy, + effectiveAllowFrom: effectiveDmAllow, + effectiveGroupAllowFrom: effectiveGroupAllow, + isSenderAllowed: (allowFrom) => isSignalSenderAllowed(sender, allowFrom), + }); + const dmAccess = resolveAccessDecision(false); + const dmAllowed = dmAccess.decision === "allow"; if (reaction && !hasBodyContent) { if (reaction.isRemove) { return; } // Ignore reaction removals const emojiLabel = reaction.emoji?.trim() || "emoji"; - const senderDisplay = formatSignalSenderDisplay(sender); const senderName = envelope.sourceName ?? senderDisplay; logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`); + const groupId = reaction.groupInfo?.groupId ?? undefined; + const groupName = reaction.groupInfo?.groupName ?? undefined; + const isGroup = Boolean(groupId); + const reactionAccess = resolveAccessDecision(isGroup); + if (reactionAccess.decision !== "allow") { + logVerbose(`Blocked signal reaction sender ${senderDisplay} (${reactionAccess.reason})`); + return; + } const targets = deps.resolveSignalReactionTargets(reaction); const shouldNotify = deps.shouldEmitSignalReactionNotification({ mode: deps.reactionMode, @@ -387,9 +421,6 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { return; } - const groupId = reaction.groupInfo?.groupId ?? undefined; - const groupName = reaction.groupInfo?.groupName ?? undefined; - const isGroup = Boolean(groupId); const senderPeerId = resolveSignalPeerId(sender); const route = resolveAgentRoute({ cfg: deps.cfg, @@ -430,7 +461,6 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { return; } - const senderDisplay = formatSignalSenderDisplay(sender); const senderRecipient = resolveSignalRecipient(sender); const senderPeerId = resolveSignalPeerId(sender); const senderAllowId = formatSignalSenderId(sender); @@ -441,20 +471,15 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { const groupId = dataMessage.groupInfo?.groupId ?? undefined; const groupName = dataMessage.groupInfo?.groupName ?? undefined; const isGroup = Boolean(groupId); - const storeAllowFrom = - deps.dmPolicy === "allowlist" - ? [] - : await readChannelAllowFromStore("signal").catch(() => []); - const effectiveDmAllow = [...deps.allowFrom, ...storeAllowFrom]; - const effectiveGroupAllow = [...deps.groupAllowFrom, ...storeAllowFrom]; - const dmAllowed = - deps.dmPolicy === "open" ? true : isSignalSenderAllowed(sender, effectiveDmAllow); if (!isGroup) { - if (deps.dmPolicy === "disabled") { + if (dmAccess.decision === "block") { + if (deps.dmPolicy !== "disabled") { + logVerbose(`Blocked signal sender ${senderDisplay} (dmPolicy=${deps.dmPolicy})`); + } return; } - if (!dmAllowed) { + if (dmAccess.decision === "pairing") { if (deps.dmPolicy === "pairing") { const senderId = senderAllowId; const { code, created } = await upsertChannelPairingRequest({ @@ -483,23 +508,20 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { logVerbose(`signal pairing reply failed for ${senderId}: ${String(err)}`); } } - } else { - logVerbose(`Blocked signal sender ${senderDisplay} (dmPolicy=${deps.dmPolicy})`); } return; } } - if (isGroup && deps.groupPolicy === "disabled") { - logVerbose("Blocked signal group message (groupPolicy: disabled)"); - return; - } - if (isGroup && deps.groupPolicy === "allowlist") { - if (effectiveGroupAllow.length === 0) { - logVerbose("Blocked signal group message (groupPolicy: allowlist, no groupAllowFrom)"); - return; - } - if (!isSignalSenderAllowed(sender, effectiveGroupAllow)) { - logVerbose(`Blocked signal group sender ${senderDisplay} (not in groupAllowFrom)`); + if (isGroup) { + const groupAccess = resolveAccessDecision(true); + if (groupAccess.decision !== "allow") { + if (groupAccess.reason === "groupPolicy=disabled") { + logVerbose("Blocked signal group message (groupPolicy: disabled)"); + } else if (groupAccess.reason === "groupPolicy=allowlist (empty allowlist)") { + logVerbose("Blocked signal group message (groupPolicy: allowlist, no groupAllowFrom)"); + } else { + logVerbose(`Blocked signal group sender ${senderDisplay} (not in groupAllowFrom)`); + } return; } } From 8d1481cb4a9d31bd617e52dc8c392c35689d9dea Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:51:34 +0100 Subject: [PATCH 041/160] fix(gateway): require pairing for unpaired operator device auth --- CHANGELOG.md | 1 + src/gateway/server.auth.test.ts | 65 ++++++++++++------- .../server/ws-connection/message-handler.ts | 12 ++-- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5cbd2384e8d..c2df44e3950e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Docs: https://docs.openclaw.ai - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. +- Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. diff --git a/src/gateway/server.auth.test.ts b/src/gateway/server.auth.test.ts index 8da0e18ef31c..83a97644d193 100644 --- a/src/gateway/server.auth.test.ts +++ b/src/gateway/server.auth.test.ts @@ -1065,7 +1065,7 @@ describe("gateway server auth/connect", () => { } }); - test("skips pairing for operator scope upgrades when shared token auth is valid", async () => { + test("requires pairing for remote operator device identity with shared token auth", async () => { const { mkdtemp } = await import("node:fs/promises"); const { tmpdir } = await import("node:os"); const { join } = await import("node:path"); @@ -1102,21 +1102,29 @@ describe("gateway server auth/connect", () => { nonce, }; }; - const initialNonce = await readConnectChallengeNonce(ws); - const initial = await connectReq(ws, { + ws.close(); + + const wsRemoteRead = await openWs(port, { host: "gateway.example" }); + const initialNonce = await readConnectChallengeNonce(wsRemoteRead); + const initial = await connectReq(wsRemoteRead, { token: "secret", scopes: ["operator.read"], client, device: buildDevice(["operator.read"], initialNonce), }); - expect(initial.ok).toBe(true); + expect(initial.ok).toBe(false); + expect(initial.error?.message ?? "").toContain("pairing required"); let pairing = await listDevicePairing(); - expect(pairing.pending.filter((entry) => entry.deviceId === identity.deviceId)).toEqual([]); + const pendingAfterRead = pairing.pending.filter( + (entry) => entry.deviceId === identity.deviceId, + ); + expect(pendingAfterRead).toHaveLength(1); + expect(pendingAfterRead[0]?.role).toBe("operator"); + expect(pendingAfterRead[0]?.scopes ?? []).toContain("operator.read"); expect(await getPairedDevice(identity.deviceId)).toBeNull(); + wsRemoteRead.close(); - ws.close(); - - const ws2 = await openWs(port); + const ws2 = await openWs(port, { host: "gateway.example" }); const nonce2 = await readConnectChallengeNonce(ws2); const res = await connectReq(ws2, { token: "secret", @@ -1124,9 +1132,16 @@ describe("gateway server auth/connect", () => { client, device: buildDevice(["operator.admin"], nonce2), }); - expect(res.ok).toBe(true); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("pairing required"); pairing = await listDevicePairing(); - expect(pairing.pending.filter((entry) => entry.deviceId === identity.deviceId)).toEqual([]); + const pendingAfterAdmin = pairing.pending.filter( + (entry) => entry.deviceId === identity.deviceId, + ); + expect(pendingAfterAdmin).toHaveLength(1); + expect(pendingAfterAdmin[0]?.scopes ?? []).toEqual( + expect.arrayContaining(["operator.read", "operator.admin"]), + ); expect(await getPairedDevice(identity.deviceId)).toBeNull(); ws2.close(); await server.close(); @@ -1199,7 +1214,7 @@ describe("gateway server auth/connect", () => { restoreGatewayToken(prevToken); }); - test("still requires node pairing while operator shared auth succeeds for the same device", async () => { + test("merges remote node/operator pairing requests for the same unpaired device", async () => { const { mkdtemp } = await import("node:fs/promises"); const { tmpdir } = await import("node:os"); const { join } = await import("node:path"); @@ -1266,23 +1281,25 @@ describe("gateway server auth/connect", () => { expect(nodeConnect.error?.message ?? "").toContain("pairing required"); const operatorConnect = await connectWithNonce("operator", ["operator.read", "operator.write"]); - expect(operatorConnect.ok).toBe(true); + expect(operatorConnect.ok).toBe(false); + expect(operatorConnect.error?.message ?? "").toContain("pairing required"); const pending = await listDevicePairing(); const pendingForTestDevice = pending.pending.filter( (entry) => entry.deviceId === identity.deviceId, ); expect(pendingForTestDevice).toHaveLength(1); - expect(pendingForTestDevice[0]?.roles).toEqual(expect.arrayContaining(["node"])); - expect(pendingForTestDevice[0]?.roles ?? []).not.toContain("operator"); + expect(pendingForTestDevice[0]?.roles).toEqual(expect.arrayContaining(["node", "operator"])); + expect(pendingForTestDevice[0]?.scopes ?? []).toEqual( + expect.arrayContaining(["operator.read", "operator.write"]), + ); if (!pendingForTestDevice[0]) { throw new Error("expected pending pairing request"); } await approveDevicePairing(pendingForTestDevice[0].requestId); const paired = await getPairedDevice(identity.deviceId); - expect(paired?.roles).toEqual(expect.arrayContaining(["node"])); - expect(paired?.roles ?? []).not.toContain("operator"); + expect(paired?.roles).toEqual(expect.arrayContaining(["node", "operator"])); const approvedOperatorConnect = await connectWithNonce("operator", ["operator.read"]); expect(approvedOperatorConnect.ok).toBe(true); @@ -1438,8 +1455,8 @@ describe("gateway server auth/connect", () => { expect(reconnect.ok).toBe(true); const repaired = await getPairedDevice(deviceId); - expect(repaired?.roles).toBeUndefined(); - expect(repaired?.scopes).toBeUndefined(); + expect(repaired?.roles ?? []).toContain("operator"); + expect(repaired?.scopes ?? []).toContain("operator.read"); const list = await listDevicePairing(); expect(list.pending.filter((entry) => entry.deviceId === deviceId)).toEqual([]); } finally { @@ -1450,7 +1467,7 @@ describe("gateway server auth/connect", () => { } }); - test("allows shared-auth scope escalation even when paired metadata is legacy-shaped", async () => { + test("auto-approves local scope upgrades even when paired metadata is legacy-shaped", async () => { const { mkdtemp } = await import("node:fs/promises"); const { tmpdir } = await import("node:os"); const { join } = await import("node:path"); @@ -1539,9 +1556,13 @@ describe("gateway server auth/connect", () => { expect(pendingUpgrade).toBeUndefined(); const repaired = await getPairedDevice(identity.deviceId); expect(repaired?.role).toBe("operator"); - expect(repaired?.roles).toBeUndefined(); - expect(repaired?.scopes).toBeUndefined(); - expect(repaired?.approvedScopes).not.toContain("operator.admin"); + expect(repaired?.roles ?? []).toContain("operator"); + expect(repaired?.scopes ?? []).toEqual( + expect.arrayContaining(["operator.read", "operator.admin"]), + ); + expect(repaired?.approvedScopes ?? []).toEqual( + expect.arrayContaining(["operator.read", "operator.admin"]), + ); } finally { ws.close(); ws2?.close(); diff --git a/src/gateway/server/ws-connection/message-handler.ts b/src/gateway/server/ws-connection/message-handler.ts index 191278275ee8..9708325009f6 100644 --- a/src/gateway/server/ws-connection/message-handler.ts +++ b/src/gateway/server/ws-connection/message-handler.ts @@ -565,18 +565,16 @@ export function attachGatewayWsMessageHandler(params: { return; } - // Shared token/password auth is already gateway-level trust for operator clients. - // In that case, don't force device pairing on first connect. - const skipPairingForOperatorSharedAuth = - role === "operator" && sharedAuthOk && !isControlUi && !isWebchat; const trustedProxyAuthOk = isControlUi && resolvedAuth.mode === "trusted-proxy" && authOk && authMethod === "trusted-proxy"; - const skipPairing = - shouldSkipControlUiPairing(controlUiAuthPolicy, sharedAuthOk, trustedProxyAuthOk) || - skipPairingForOperatorSharedAuth; + const skipPairing = shouldSkipControlUiPairing( + controlUiAuthPolicy, + sharedAuthOk, + trustedProxyAuthOk, + ); if (device && devicePublicKey && !skipPairing) { const formatAuditList = (items: string[] | undefined): string => { if (!items || items.length === 0) { From eb73e87f18d1e94ff240c419ffcff6776f551a3d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 25 Feb 2026 23:53:43 +0000 Subject: [PATCH 042/160] fix(session): prevent silent overflow on parent thread forks (#26912) Lands #26912 from @markshields-tl with configurable session.parentForkMaxTokens and docs/tests/changelog updates. Co-authored-by: Mark Shields <239231357+markshields-tl@users.noreply.github.com> --- CHANGELOG.md | 1 + docs/gateway/configuration-reference.md | 4 + .../session-management-compaction.md | 1 + .../reply/agent-runner-execution.ts | 16 +++ src/auto-reply/reply/session.test.ts | 124 ++++++++++++++++++ src/auto-reply/reply/session.ts | 56 ++++++-- src/config/schema.help.ts | 2 + src/config/schema.labels.ts | 1 + src/config/types.base.ts | 6 + ...ema.session-maintenance-extensions.test.ts | 13 ++ src/config/zod-schema.session.ts | 1 + 11 files changed, 211 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2df44e3950e..1bbd75dac044 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. diff --git a/docs/gateway/configuration-reference.md b/docs/gateway/configuration-reference.md index 01ad82b6098c..9d164fc4ea0a 100644 --- a/docs/gateway/configuration-reference.md +++ b/docs/gateway/configuration-reference.md @@ -1250,6 +1250,7 @@ See [Multi-Agent Sandbox & Tools](/tools/multi-agent-sandbox-tools) for preceden }, resetTriggers: ["/new", "/reset"], store: "~/.openclaw/agents/{agentId}/sessions/sessions.json", + parentForkMaxTokens: 100000, // skip parent-thread fork above this token count (0 disables) maintenance: { mode: "warn", // warn | enforce pruneAfter: "30d", @@ -1283,6 +1284,9 @@ See [Multi-Agent Sandbox & Tools](/tools/multi-agent-sandbox-tools) for preceden - **`identityLinks`**: map canonical ids to provider-prefixed peers for cross-channel session sharing. - **`reset`**: primary reset policy. `daily` resets at `atHour` local time; `idle` resets after `idleMinutes`. When both configured, whichever expires first wins. - **`resetByType`**: per-type overrides (`direct`, `group`, `thread`). Legacy `dm` accepted as alias for `direct`. +- **`parentForkMaxTokens`**: max parent-session `totalTokens` allowed when creating a forked thread session (default `100000`). + - If parent `totalTokens` is above this value, OpenClaw starts a fresh thread session instead of inheriting parent transcript history. + - Set `0` to disable this guard and always allow parent forking. - **`mainKey`**: legacy field. Runtime now always uses `"main"` for the main direct-chat bucket. - **`sendPolicy`**: match by `channel`, `chatType` (`direct|group|channel`, with legacy `dm` alias), `keyPrefix`, or `rawKeyPrefix`. First deny wins. - **`maintenance`**: session-store cleanup + retention controls. diff --git a/docs/reference/session-management-compaction.md b/docs/reference/session-management-compaction.md index aff09a303e8a..d258eeb6722c 100644 --- a/docs/reference/session-management-compaction.md +++ b/docs/reference/session-management-compaction.md @@ -128,6 +128,7 @@ Rules of thumb: - **Reset** (`/new`, `/reset`) creates a new `sessionId` for that `sessionKey`. - **Daily reset** (default 4:00 AM local time on the gateway host) creates a new `sessionId` on the next message after the reset boundary. - **Idle expiry** (`session.reset.idleMinutes` or legacy `session.idleMinutes`) creates a new `sessionId` when a message arrives after the idle window. When daily + idle are both configured, whichever expires first wins. +- **Thread parent fork guard** (`session.parentForkMaxTokens`, default `100000`) skips parent transcript forking when the parent session is already too large; the new thread starts fresh. Set `0` to disable. Implementation detail: the decision happens in `initSessionState()` in `src/auto-reply/reply/session.ts`. diff --git a/src/auto-reply/reply/agent-runner-execution.ts b/src/auto-reply/reply/agent-runner-execution.ts index eb8605ccfe1c..32022f954531 100644 --- a/src/auto-reply/reply/agent-runner-execution.ts +++ b/src/auto-reply/reply/agent-runner-execution.ts @@ -572,6 +572,22 @@ export async function runAgentTurnWithFallback(params: { } } + // If the run completed but with an embedded context overflow error that + // wasn't recovered from (e.g. compaction reset already attempted), surface + // the error to the user instead of silently returning an empty response. + // See #26905: Slack DM sessions silently swallowed messages when context + // overflow errors were returned as embedded error payloads. + const finalEmbeddedError = runResult?.meta?.error; + const hasPayloadText = runResult?.payloads?.some((p) => p.text?.trim()); + if (finalEmbeddedError && isContextOverflowError(finalEmbeddedError.message) && !hasPayloadText) { + return { + kind: "final", + payload: { + text: "⚠️ Context overflow — this conversation is too large for the model. Use /new to start a fresh session.", + }, + }; + } + return { kind: "success", runId, diff --git a/src/auto-reply/reply/session.test.ts b/src/auto-reply/reply/session.test.ts index 8e9c99667b1e..cdd8b5310c02 100644 --- a/src/auto-reply/reply/session.test.ts +++ b/src/auto-reply/reply/session.test.ts @@ -205,6 +205,130 @@ describe("initSessionState thread forking", () => { warn.mockRestore(); }); + it("skips fork and creates fresh session when parent tokens exceed threshold", async () => { + const root = await makeCaseDir("openclaw-thread-session-overflow-"); + const sessionsDir = path.join(root, "sessions"); + await fs.mkdir(sessionsDir); + + const parentSessionId = "parent-overflow"; + const parentSessionFile = path.join(sessionsDir, "parent.jsonl"); + const header = { + type: "session", + version: 3, + id: parentSessionId, + timestamp: new Date().toISOString(), + cwd: process.cwd(), + }; + const message = { + type: "message", + id: "m1", + parentId: null, + timestamp: new Date().toISOString(), + message: { role: "user", content: "Parent prompt" }, + }; + await fs.writeFile( + parentSessionFile, + `${JSON.stringify(header)}\n${JSON.stringify(message)}\n`, + "utf-8", + ); + + const storePath = path.join(root, "sessions.json"); + const parentSessionKey = "agent:main:slack:channel:c1"; + // Set totalTokens well above PARENT_FORK_MAX_TOKENS (100_000) + await saveSessionStore(storePath, { + [parentSessionKey]: { + sessionId: parentSessionId, + sessionFile: parentSessionFile, + updatedAt: Date.now(), + totalTokens: 170_000, + }, + }); + + const cfg = { + session: { store: storePath }, + } as OpenClawConfig; + + const threadSessionKey = "agent:main:slack:channel:c1:thread:456"; + const result = await initSessionState({ + ctx: { + Body: "Thread reply", + SessionKey: threadSessionKey, + ParentSessionKey: parentSessionKey, + }, + cfg, + commandAuthorized: true, + }); + + // Should be marked as forked (to prevent re-attempts) but NOT actually forked from parent + expect(result.sessionEntry.forkedFromParent).toBe(true); + // Session ID should NOT match the parent — it should be a fresh UUID + expect(result.sessionEntry.sessionId).not.toBe(parentSessionId); + // Session file should NOT be the parent's file (it was not forked) + expect(result.sessionEntry.sessionFile).not.toBe(parentSessionFile); + }); + + it("respects session.parentForkMaxTokens override", async () => { + const root = await makeCaseDir("openclaw-thread-session-overflow-override-"); + const sessionsDir = path.join(root, "sessions"); + await fs.mkdir(sessionsDir); + + const parentSessionId = "parent-override"; + const parentSessionFile = path.join(sessionsDir, "parent.jsonl"); + const header = { + type: "session", + version: 3, + id: parentSessionId, + timestamp: new Date().toISOString(), + cwd: process.cwd(), + }; + const message = { + type: "message", + id: "m1", + parentId: null, + timestamp: new Date().toISOString(), + message: { role: "user", content: "Parent prompt" }, + }; + await fs.writeFile( + parentSessionFile, + `${JSON.stringify(header)}\n${JSON.stringify(message)}\n`, + "utf-8", + ); + + const storePath = path.join(root, "sessions.json"); + const parentSessionKey = "agent:main:slack:channel:c1"; + await saveSessionStore(storePath, { + [parentSessionKey]: { + sessionId: parentSessionId, + sessionFile: parentSessionFile, + updatedAt: Date.now(), + totalTokens: 170_000, + }, + }); + + const cfg = { + session: { + store: storePath, + parentForkMaxTokens: 200_000, + }, + } as OpenClawConfig; + + const threadSessionKey = "agent:main:slack:channel:c1:thread:789"; + const result = await initSessionState({ + ctx: { + Body: "Thread reply", + SessionKey: threadSessionKey, + ParentSessionKey: parentSessionKey, + }, + cfg, + commandAuthorized: true, + }); + + expect(result.sessionEntry.forkedFromParent).toBe(true); + expect(result.sessionEntry.sessionFile).toBeTruthy(); + const forkedContent = await fs.readFile(result.sessionEntry.sessionFile ?? "", "utf-8"); + expect(forkedContent).toContain(parentSessionFile); + }); + it("records topic-specific session files when MessageThreadId is present", async () => { const root = await makeCaseDir("openclaw-topic-session-"); const storePath = path.join(root, "sessions.json"); diff --git a/src/auto-reply/reply/session.ts b/src/auto-reply/reply/session.ts index 6494192c58b2..59b0c7ba3791 100644 --- a/src/auto-reply/reply/session.ts +++ b/src/auto-reply/reply/session.ts @@ -105,6 +105,21 @@ export type SessionInitResult = { triggerBodyNormalized: string; }; +/** + * Default max parent token count beyond which thread/session parent forking is skipped. + * This prevents new thread sessions from inheriting near-full parent context. + * See #26905. + */ +const DEFAULT_PARENT_FORK_MAX_TOKENS = 100_000; + +function resolveParentForkMaxTokens(cfg: OpenClawConfig): number { + const configured = cfg.session?.parentForkMaxTokens; + if (typeof configured === "number" && Number.isFinite(configured) && configured >= 0) { + return Math.floor(configured); + } + return DEFAULT_PARENT_FORK_MAX_TOKENS; +} + function forkSessionFromParent(params: { parentEntry: SessionEntry; agentId: string; @@ -171,6 +186,7 @@ export async function initSessionState(params: { const resetTriggers = sessionCfg?.resetTriggers?.length ? sessionCfg.resetTriggers : DEFAULT_RESET_TRIGGERS; + const parentForkMaxTokens = resolveParentForkMaxTokens(cfg); const sessionScope = sessionCfg?.scope ?? "per-sender"; const storePath = resolveStorePath(sessionCfg?.store, { agentId }); @@ -399,21 +415,33 @@ export async function initSessionState(params: { sessionStore[parentSessionKey] && !alreadyForked ) { - log.warn( - `forking from parent session: parentKey=${parentSessionKey} → sessionKey=${sessionKey} ` + - `parentTokens=${sessionStore[parentSessionKey].totalTokens ?? "?"}`, - ); - const forked = forkSessionFromParent({ - parentEntry: sessionStore[parentSessionKey], - agentId, - sessionsDir: path.dirname(storePath), - }); - if (forked) { - sessionId = forked.sessionId; - sessionEntry.sessionId = forked.sessionId; - sessionEntry.sessionFile = forked.sessionFile; + const parentTokens = sessionStore[parentSessionKey].totalTokens ?? 0; + if (parentForkMaxTokens > 0 && parentTokens > parentForkMaxTokens) { + // Parent context is too large — forking would create a thread session + // that immediately overflows the model's context window. Start fresh + // instead and mark as forked to prevent re-attempts. See #26905. + log.warn( + `skipping parent fork (parent too large): parentKey=${parentSessionKey} → sessionKey=${sessionKey} ` + + `parentTokens=${parentTokens} maxTokens=${parentForkMaxTokens}`, + ); sessionEntry.forkedFromParent = true; - log.warn(`forked session created: file=${forked.sessionFile}`); + } else { + log.warn( + `forking from parent session: parentKey=${parentSessionKey} → sessionKey=${sessionKey} ` + + `parentTokens=${parentTokens}`, + ); + const forked = forkSessionFromParent({ + parentEntry: sessionStore[parentSessionKey], + agentId, + sessionsDir: path.dirname(storePath), + }); + if (forked) { + sessionId = forked.sessionId; + sessionEntry.sessionId = forked.sessionId; + sessionEntry.sessionFile = forked.sessionFile; + sessionEntry.forkedFromParent = true; + log.warn(`forked session created: file=${forked.sessionFile}`); + } } } const fallbackSessionFile = !sessionEntry.sessionFile diff --git a/src/config/schema.help.ts b/src/config/schema.help.ts index e5fcb3aa6b7f..bf917461f569 100644 --- a/src/config/schema.help.ts +++ b/src/config/schema.help.ts @@ -973,6 +973,8 @@ export const FIELD_HELP: Record = { "Controls interval for repeated typing indicators while replies are being prepared in typing-capable channels. Increase to reduce chatty updates or decrease for more active typing feedback.", "session.typingMode": 'Controls typing behavior timing: "never", "instant", "thinking", or "message" based emission points. Keep conservative modes in high-volume channels to avoid unnecessary typing noise.', + "session.parentForkMaxTokens": + "Maximum parent-session token count allowed for thread/session inheritance forking. If the parent exceeds this, OpenClaw starts a fresh thread session instead of forking; set 0 to disable this protection.", "session.mainKey": 'Overrides the canonical main session key used for continuity when dmScope or routing logic points to "main". Use a stable value only if you intentionally need custom session anchoring.', "session.sendPolicy": diff --git a/src/config/schema.labels.ts b/src/config/schema.labels.ts index 7a12e9293ba2..cd28b1fafb83 100644 --- a/src/config/schema.labels.ts +++ b/src/config/schema.labels.ts @@ -455,6 +455,7 @@ export const FIELD_LABELS: Record = { "session.store": "Session Store Path", "session.typingIntervalSeconds": "Session Typing Interval (seconds)", "session.typingMode": "Session Typing Mode", + "session.parentForkMaxTokens": "Session Parent Fork Max Tokens", "session.mainKey": "Session Main Key", "session.sendPolicy": "Session Send Policy", "session.sendPolicy.default": "Session Send Policy Default Action", diff --git a/src/config/types.base.ts b/src/config/types.base.ts index cb1b926b53f7..676767fc9012 100644 --- a/src/config/types.base.ts +++ b/src/config/types.base.ts @@ -112,6 +112,12 @@ export type SessionConfig = { store?: string; typingIntervalSeconds?: number; typingMode?: TypingMode; + /** + * Max parent transcript token count allowed for thread/session forking. + * If parent totalTokens is above this value, OpenClaw skips parent fork and + * starts a fresh thread session instead. Set to 0 to disable this guard. + */ + parentForkMaxTokens?: number; mainKey?: string; sendPolicy?: SessionSendPolicyConfig; agentToAgent?: { diff --git a/src/config/zod-schema.session-maintenance-extensions.test.ts b/src/config/zod-schema.session-maintenance-extensions.test.ts index 6efe8b39907a..deb86999934d 100644 --- a/src/config/zod-schema.session-maintenance-extensions.test.ts +++ b/src/config/zod-schema.session-maintenance-extensions.test.ts @@ -14,6 +14,19 @@ describe("SessionSchema maintenance extensions", () => { ).not.toThrow(); }); + it("accepts parentForkMaxTokens including 0 to disable the guard", () => { + expect(() => SessionSchema.parse({ parentForkMaxTokens: 100_000 })).not.toThrow(); + expect(() => SessionSchema.parse({ parentForkMaxTokens: 0 })).not.toThrow(); + }); + + it("rejects negative parentForkMaxTokens", () => { + expect(() => + SessionSchema.parse({ + parentForkMaxTokens: -1, + }), + ).toThrow(/parentForkMaxTokens/i); + }); + it("accepts disabling reset archive cleanup", () => { expect(() => SessionSchema.parse({ diff --git a/src/config/zod-schema.session.ts b/src/config/zod-schema.session.ts index 5af707b28041..de23c50846e1 100644 --- a/src/config/zod-schema.session.ts +++ b/src/config/zod-schema.session.ts @@ -52,6 +52,7 @@ export const SessionSchema = z store: z.string().optional(), typingIntervalSeconds: z.number().int().positive().optional(), typingMode: TypingModeSchema.optional(), + parentForkMaxTokens: z.number().int().nonnegative().optional(), mainKey: z.string().optional(), sendPolicy: SessionSendPolicySchema.optional(), agentToAgent: z From 42f455739f03c553bb0d7014e8152078b85d8e54 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:55:25 +0100 Subject: [PATCH 043/160] fix(security): clarify denyCommands exact-match guidance --- docs/cli/security.md | 2 +- docs/gateway/security/index.md | 2 +- src/config/schema.help.ts | 2 +- src/node-host/invoke-system-run.test.ts | 25 +++++++++++++++++++++++++ src/security/audit-extra.sync.ts | 4 ++-- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/cli/security.md b/docs/cli/security.md index fe8af41ec259..cc705b31a30d 100644 --- a/docs/cli/security.md +++ b/docs/cli/security.md @@ -29,7 +29,7 @@ It also emits `security.trust_model.multi_user_heuristic` when config suggests l For intentional shared-user setups, the audit guidance is to sandbox all sessions, keep filesystem access workspace-scoped, and keep personal/private identities or credentials off that runtime. It also warns when small models (`<=300B`) are used without sandboxing and with web/browser tools enabled. For webhook ingress, it warns when `hooks.defaultSessionKey` is unset, when request `sessionKey` overrides are enabled, and when overrides are enabled without `hooks.allowedSessionKeyPrefixes`. -It also warns when sandbox Docker settings are configured while sandbox mode is off, when `gateway.nodes.denyCommands` uses ineffective pattern-like/unknown entries, when `gateway.nodes.allowCommands` explicitly enables dangerous node commands, when global `tools.profile="minimal"` is overridden by agent tool profiles, when open groups expose runtime/filesystem tools without sandbox/workspace guards, and when installed extension plugin tools may be reachable under permissive tool policy. +It also warns when sandbox Docker settings are configured while sandbox mode is off, when `gateway.nodes.denyCommands` uses ineffective pattern-like/unknown entries (exact node command-name matching only, not shell-text filtering), when `gateway.nodes.allowCommands` explicitly enables dangerous node commands, when global `tools.profile="minimal"` is overridden by agent tool profiles, when open groups expose runtime/filesystem tools without sandbox/workspace guards, and when installed extension plugin tools may be reachable under permissive tool policy. It also flags `gateway.allowRealIpFallback=true` (header-spoofing risk if proxies are misconfigured) and `discovery.mdns.mode="full"` (metadata leakage via mDNS TXT records). It also warns when sandbox browser uses Docker `bridge` network without `sandbox.browser.cdpSourceRange`. It also flags dangerous sandbox Docker network modes (including `host` and `container:*` namespace joins). diff --git a/docs/gateway/security/index.md b/docs/gateway/security/index.md index 3824d1d283e1..a61a81eab1e4 100644 --- a/docs/gateway/security/index.md +++ b/docs/gateway/security/index.md @@ -188,7 +188,7 @@ If more than one person can DM your bot: - **Browser control exposure** (remote nodes, relay ports, remote CDP endpoints). - **Local disk hygiene** (permissions, symlinks, config includes, “synced folder” paths). - **Plugins** (extensions exist without an explicit allowlist). -- **Policy drift/misconfig** (sandbox docker settings configured but sandbox mode off; ineffective `gateway.nodes.denyCommands` patterns; dangerous `gateway.nodes.allowCommands` entries; global `tools.profile="minimal"` overridden by per-agent profiles; extension plugin tools reachable under permissive tool policy). +- **Policy drift/misconfig** (sandbox docker settings configured but sandbox mode off; ineffective `gateway.nodes.denyCommands` patterns because matching is exact command-name only (for example `system.run`) and does not inspect shell text; dangerous `gateway.nodes.allowCommands` entries; global `tools.profile="minimal"` overridden by per-agent profiles; extension plugin tools reachable under permissive tool policy). - **Runtime expectation drift** (for example `tools.exec.host="sandbox"` while sandbox mode is off, which runs directly on the gateway host). - **Model hygiene** (warn when configured models look legacy; not a hard block). diff --git a/src/config/schema.help.ts b/src/config/schema.help.ts index bf917461f569..a479ec0a8537 100644 --- a/src/config/schema.help.ts +++ b/src/config/schema.help.ts @@ -330,7 +330,7 @@ export const FIELD_HELP: Record = { "gateway.nodes.allowCommands": "Extra node.invoke commands to allow beyond the gateway defaults (array of command strings). Enabling dangerous commands here is a security-sensitive override and is flagged by `openclaw security audit`.", "gateway.nodes.denyCommands": - "Commands to block even if present in node claims or default allowlist.", + "Node command names to block even if present in node claims or default allowlist (exact command-name matching only, e.g. `system.run`; does not inspect shell text inside that command).", nodeHost: "Node host controls for features exposed from this gateway node to other nodes or clients. Keep defaults unless you intentionally proxy local capabilities across your node network.", "nodeHost.browserProxy": diff --git a/src/node-host/invoke-system-run.test.ts b/src/node-host/invoke-system-run.test.ts index 2d939c7726ea..d19171990675 100644 --- a/src/node-host/invoke-system-run.test.ts +++ b/src/node-host/invoke-system-run.test.ts @@ -365,6 +365,31 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { ); }); + it("denies semicolon-chained shell payloads in allowlist mode without explicit approval", async () => { + const payloads = ["openclaw status; id", "openclaw status; cat /etc/passwd"]; + for (const payload of payloads) { + const command = + process.platform === "win32" + ? ["cmd.exe", "/d", "/s", "/c", payload] + : ["/bin/sh", "-lc", payload]; + const { runCommand, sendInvokeResult } = await runSystemInvoke({ + preferMacAppExecHost: false, + security: "allowlist", + ask: "on-miss", + command, + }); + expect(runCommand, payload).not.toHaveBeenCalled(); + expect(sendInvokeResult, payload).toHaveBeenCalledWith( + expect.objectContaining({ + ok: false, + error: expect.objectContaining({ + message: "SYSTEM_RUN_DENIED: approval required", + }), + }), + ); + } + }); + it("denies nested env shell payloads when wrapper depth is exceeded", async () => { if (process.platform === "win32") { return; diff --git a/src/security/audit-extra.sync.ts b/src/security/audit-extra.sync.ts index daa60aed73f0..a3f81d408703 100644 --- a/src/security/audit-extra.sync.ts +++ b/src/security/audit-extra.sync.ts @@ -955,11 +955,11 @@ export function collectNodeDenyCommandPatternFindings(cfg: OpenClawConfig): Secu severity: "warn", title: "Some gateway.nodes.denyCommands entries are ineffective", detail: - "gateway.nodes.denyCommands uses exact command-name matching only.\n" + + "gateway.nodes.denyCommands uses exact node command-name matching only (for example `system.run`), not shell-text filtering inside a command payload.\n" + detailParts.map((entry) => `- ${entry}`).join("\n"), remediation: `Use exact command names (for example: ${examples.join(", ")}). ` + - "If you need broader restrictions, remove risky commands from allowCommands/default workflows.", + "If you need broader restrictions, remove risky command IDs from allowCommands/default workflows and tighten tools.exec policy.", }); return findings; From b090d6019b9e540c280ea30900fb4bf71a4fafca Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 25 Feb 2026 23:57:50 +0000 Subject: [PATCH 044/160] test(agent-runner): add overflow empty-payload regression coverage (#26905) --- .../reply/agent-runner.runreplyagent.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/auto-reply/reply/agent-runner.runreplyagent.test.ts b/src/auto-reply/reply/agent-runner.runreplyagent.test.ts index 52d1e4550c20..ee8ddc251792 100644 --- a/src/auto-reply/reply/agent-runner.runreplyagent.test.ts +++ b/src/auto-reply/reply/agent-runner.runreplyagent.test.ts @@ -1188,6 +1188,54 @@ describe("runReplyAgent typing (heartbeat)", () => { }); }); + it("surfaces overflow fallback when embedded run returns empty payloads", async () => { + state.runEmbeddedPiAgentMock.mockImplementationOnce(async () => ({ + payloads: [], + meta: { + durationMs: 1, + error: { + kind: "context_overflow", + message: 'Context overflow: Summarization failed: 400 {"message":"prompt is too long"}', + }, + }, + })); + + const { run } = createMinimalRun(); + const res = await run(); + const payload = Array.isArray(res) ? res[0] : res; + expect(payload).toMatchObject({ + text: expect.stringContaining("conversation is too large"), + }); + if (!payload) { + throw new Error("expected payload"); + } + expect(payload.text).toContain("/new"); + }); + + it("surfaces overflow fallback when embedded payload text is whitespace-only", async () => { + state.runEmbeddedPiAgentMock.mockImplementationOnce(async () => ({ + payloads: [{ text: " \n\t ", isError: true }], + meta: { + durationMs: 1, + error: { + kind: "context_overflow", + message: 'Context overflow: Summarization failed: 400 {"message":"prompt is too long"}', + }, + }, + })); + + const { run } = createMinimalRun(); + const res = await run(); + const payload = Array.isArray(res) ? res[0] : res; + expect(payload).toMatchObject({ + text: expect.stringContaining("conversation is too large"), + }); + if (!payload) { + throw new Error("expected payload"); + } + expect(payload.text).toContain("/new"); + }); + it("resets the session after role ordering payloads", async () => { await withTempStateDir(async (stateDir) => { const sessionId = "session"; From 39cc547f74f137b563cbfb46cf027768ad2fd2a4 Mon Sep 17 00:00:00 2001 From: User Date: Thu, 26 Feb 2026 07:19:30 +0800 Subject: [PATCH 045/160] fix(discord): include embed title in fallback text (#26907) --- src/discord/monitor/message-utils.test.ts | 44 +++++++++++++++++++++++ src/discord/monitor/message-utils.ts | 20 +++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/discord/monitor/message-utils.test.ts b/src/discord/monitor/message-utils.test.ts index de8976ce5d23..fd3f2c4d077d 100644 --- a/src/discord/monitor/message-utils.test.ts +++ b/src/discord/monitor/message-utils.test.ts @@ -323,6 +323,50 @@ describe("resolveDiscordMessageText", () => { expect(text).toBe(" (1 sticker)"); }); + + it("uses embed title when content is empty", () => { + const text = resolveDiscordMessageText( + asMessage({ + content: "", + embeds: [{ title: "Breaking" }], + }), + ); + + expect(text).toBe("Breaking"); + }); + + it("uses embed description when content is empty", () => { + const text = resolveDiscordMessageText( + asMessage({ + content: "", + embeds: [{ description: "Details" }], + }), + ); + + expect(text).toBe("Details"); + }); + + it("joins embed title and description when content is empty", () => { + const text = resolveDiscordMessageText( + asMessage({ + content: "", + embeds: [{ title: "Breaking", description: "Details" }], + }), + ); + + expect(text).toBe("Breaking\nDetails"); + }); + + it("prefers message content over embed fallback text", () => { + const text = resolveDiscordMessageText( + asMessage({ + content: "hello from content", + embeds: [{ title: "Breaking", description: "Details" }], + }), + ); + + expect(text).toBe("hello from content"); + }); }); describe("resolveDiscordChannelInfo", () => { diff --git a/src/discord/monitor/message-utils.ts b/src/discord/monitor/message-utils.ts index 3c523d277eff..ac07f1e70e5d 100644 --- a/src/discord/monitor/message-utils.ts +++ b/src/discord/monitor/message-utils.ts @@ -403,17 +403,32 @@ function buildDiscordMediaPlaceholder(params: { return attachmentText || stickerText || ""; } +function resolveDiscordEmbedText( + embed?: { title?: string | null; description?: string | null } | null, +): string { + const title = embed?.title?.trim() || ""; + const description = embed?.description?.trim() || ""; + if (title && description) { + return `${title}\n${description}`; + } + return title || description || ""; +} + export function resolveDiscordMessageText( message: Message, options?: { fallbackText?: string; includeForwarded?: boolean }, ): string { + const embedText = resolveDiscordEmbedText( + (message.embeds?.[0] as { title?: string | null; description?: string | null } | undefined) ?? + null, + ); const baseText = message.content?.trim() || buildDiscordMediaPlaceholder({ attachments: message.attachments ?? undefined, stickers: resolveDiscordMessageStickers(message), }) || - message.embeds?.[0]?.description || + embedText || options?.fallbackText?.trim() || ""; if (!options?.includeForwarded) { @@ -477,8 +492,7 @@ function resolveDiscordSnapshotMessageText(snapshot: DiscordSnapshotMessage): st attachments: snapshot.attachments ?? undefined, stickers: resolveDiscordSnapshotStickers(snapshot), }); - const embed = snapshot.embeds?.[0]; - const embedText = embed?.description?.trim() || embed?.title?.trim() || ""; + const embedText = resolveDiscordEmbedText(snapshot.embeds?.[0]); return content || attachmentText || embedText || ""; } From a0a229a3bb92e57fae8be82706187fdd11448693 Mon Sep 17 00:00:00 2001 From: Shakker Date: Wed, 25 Feb 2026 23:50:41 +0000 Subject: [PATCH 046/160] Discord: align embed fallback in thread starter parsing --- src/discord/monitor/message-utils.test.ts | 28 ++++++++++ src/discord/monitor/message-utils.ts | 2 +- src/discord/monitor/threading.starter.test.ts | 55 +++++++++++++++++++ src/discord/monitor/threading.ts | 12 +++- 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/discord/monitor/threading.starter.test.ts diff --git a/src/discord/monitor/message-utils.test.ts b/src/discord/monitor/message-utils.test.ts index fd3f2c4d077d..28dd142a1e40 100644 --- a/src/discord/monitor/message-utils.test.ts +++ b/src/discord/monitor/message-utils.test.ts @@ -367,6 +367,34 @@ describe("resolveDiscordMessageText", () => { expect(text).toBe("hello from content"); }); + + it("joins forwarded snapshot embed title and description when content is empty", () => { + const text = resolveDiscordMessageText( + asMessage({ + content: "", + rawData: { + message_snapshots: [ + { + message: { + content: "", + embeds: [{ title: "Forwarded title", description: "Forwarded details" }], + attachments: [], + author: { + id: "u2", + username: "Bob", + discriminator: "0", + }, + }, + }, + ], + }, + }), + { includeForwarded: true }, + ); + + expect(text).toContain("[Forwarded message from @Bob]"); + expect(text).toContain("Forwarded title\nForwarded details"); + }); }); describe("resolveDiscordChannelInfo", () => { diff --git a/src/discord/monitor/message-utils.ts b/src/discord/monitor/message-utils.ts index ac07f1e70e5d..b18e877b1ce1 100644 --- a/src/discord/monitor/message-utils.ts +++ b/src/discord/monitor/message-utils.ts @@ -403,7 +403,7 @@ function buildDiscordMediaPlaceholder(params: { return attachmentText || stickerText || ""; } -function resolveDiscordEmbedText( +export function resolveDiscordEmbedText( embed?: { title?: string | null; description?: string | null } | null, ): string { const title = embed?.title?.trim() || ""; diff --git a/src/discord/monitor/threading.starter.test.ts b/src/discord/monitor/threading.starter.test.ts new file mode 100644 index 000000000000..07268d7fae98 --- /dev/null +++ b/src/discord/monitor/threading.starter.test.ts @@ -0,0 +1,55 @@ +import { ChannelType, type Client } from "@buape/carbon"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + __resetDiscordThreadStarterCacheForTest, + resolveDiscordThreadStarter, +} from "./threading.js"; + +describe("resolveDiscordThreadStarter", () => { + beforeEach(() => { + __resetDiscordThreadStarterCacheForTest(); + }); + + it("falls back to joined embed title and description when content is empty", async () => { + const get = vi.fn().mockResolvedValue({ + content: " ", + embeds: [{ title: "Alert", description: "Details" }], + author: { username: "Alice", discriminator: "0" }, + timestamp: "2026-02-24T12:00:00.000Z", + }); + const client = { rest: { get } } as unknown as Client; + + const result = await resolveDiscordThreadStarter({ + channel: { id: "thread-1" }, + client, + parentId: "parent-1", + parentType: ChannelType.GuildText, + resolveTimestampMs: () => 123, + }); + + expect(result).toEqual({ + text: "Alert\nDetails", + author: "Alice", + timestamp: 123, + }); + }); + + it("prefers starter content over embed fallback text", async () => { + const get = vi.fn().mockResolvedValue({ + content: "starter content", + embeds: [{ title: "Alert", description: "Details" }], + author: { username: "Alice", discriminator: "0" }, + }); + const client = { rest: { get } } as unknown as Client; + + const result = await resolveDiscordThreadStarter({ + channel: { id: "thread-1" }, + client, + parentId: "parent-1", + parentType: ChannelType.GuildText, + resolveTimestampMs: () => undefined, + }); + + expect(result?.text).toBe("starter content"); + }); +}); diff --git a/src/discord/monitor/threading.ts b/src/discord/monitor/threading.ts index 877329c29952..14377d8e6449 100644 --- a/src/discord/monitor/threading.ts +++ b/src/discord/monitor/threading.ts @@ -7,7 +7,11 @@ import { buildAgentSessionKey } from "../../routing/resolve-route.js"; import { truncateUtf16Safe } from "../../utils.js"; import type { DiscordChannelConfigResolved } from "./allow-list.js"; import type { DiscordMessageEvent } from "./listeners.js"; -import { resolveDiscordChannelInfo, resolveDiscordMessageChannelId } from "./message-utils.js"; +import { + resolveDiscordChannelInfo, + resolveDiscordEmbedText, + resolveDiscordMessageChannelId, +} from "./message-utils.js"; export type DiscordThreadChannel = { id: string; @@ -172,7 +176,7 @@ export async function resolveDiscordThreadStarter(params: { Routes.channelMessage(messageChannelId, params.channel.id), )) as { content?: string | null; - embeds?: Array<{ description?: string | null }>; + embeds?: Array<{ title?: string | null; description?: string | null }>; member?: { nick?: string | null; displayName?: string | null }; author?: { id?: string | null; @@ -184,7 +188,9 @@ export async function resolveDiscordThreadStarter(params: { if (!starter) { return null; } - const text = starter.content?.trim() ?? starter.embeds?.[0]?.description?.trim() ?? ""; + const content = starter.content?.trim() ?? ""; + const embedText = resolveDiscordEmbedText(starter.embeds?.[0]); + const text = content || embedText; if (!text) { return null; } From f83719937afbade398284a06752270d73d8f5efc Mon Sep 17 00:00:00 2001 From: Shakker Date: Wed, 25 Feb 2026 23:51:11 +0000 Subject: [PATCH 047/160] Changelog: note Discord embed fallback coverage --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bbd75dac044..fb4c5ec1afcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Docs: https://docs.openclaw.ai - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. +- Discord/Inbound text: preserve embed `title` + `description` fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. From c6dfa26f037977b82e91648bee47d99360d604d4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:01:58 +0100 Subject: [PATCH 048/160] refactor(signal): unify reaction auth flow and table-drive tests --- ...ends-tool-summaries-responseprefix.test.ts | 76 +++----- src/signal/monitor/event-handler.ts | 171 +++++++++++------- 2 files changed, 133 insertions(+), 114 deletions(-) diff --git a/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts b/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts index cc927fe2b36a..a06d17d61d9a 100644 --- a/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts +++ b/src/signal/monitor.tool-result.sends-tool-summaries-responseprefix.test.ts @@ -378,65 +378,49 @@ describe("monitorSignalProvider tool results", () => { expect(events.some((text) => text.includes("Signal reaction added"))).toBe(true); }); - it("blocks reaction notifications from unauthorized senders when dmPolicy is allowlist", async () => { - setReactionNotificationConfig("all", { - dmPolicy: "allowlist", - allowFrom: ["+15550007777"], - }); - await receiveSingleEnvelope({ - ...makeBaseEnvelope(), - reactionMessage: { - emoji: "✅", - targetAuthor: "+15550002222", - targetSentTimestamp: 2, - }, - }); - - const events = getDirectSignalEventsFor("+15550001111"); - expect(events.some((text) => text.includes("Signal reaction added"))).toBe(false); - expect(sendMock).not.toHaveBeenCalled(); - expect(upsertPairingRequestMock).not.toHaveBeenCalled(); - }); - - it("blocks reaction notifications from unauthorized senders when dmPolicy is pairing", async () => { - setReactionNotificationConfig("own", { - dmPolicy: "pairing", - allowFrom: [], - account: "+15550009999", - }); + it.each([ + { + name: "blocks reaction notifications from unauthorized senders when dmPolicy is allowlist", + mode: "all" as const, + extra: { dmPolicy: "allowlist", allowFrom: ["+15550007777"] } as Record, + targetAuthor: "+15550002222", + shouldEnqueue: false, + }, + { + name: "blocks reaction notifications from unauthorized senders when dmPolicy is pairing", + mode: "own" as const, + extra: { + dmPolicy: "pairing", + allowFrom: [], + account: "+15550009999", + } as Record, + targetAuthor: "+15550009999", + shouldEnqueue: false, + }, + { + name: "allows reaction notifications for allowlisted senders when dmPolicy is allowlist", + mode: "all" as const, + extra: { dmPolicy: "allowlist", allowFrom: ["+15550001111"] } as Record, + targetAuthor: "+15550002222", + shouldEnqueue: true, + }, + ])("$name", async ({ mode, extra, targetAuthor, shouldEnqueue }) => { + setReactionNotificationConfig(mode, extra); await receiveSingleEnvelope({ ...makeBaseEnvelope(), reactionMessage: { emoji: "✅", - targetAuthor: "+15550009999", + targetAuthor, targetSentTimestamp: 2, }, }); const events = getDirectSignalEventsFor("+15550001111"); - expect(events.some((text) => text.includes("Signal reaction added"))).toBe(false); + expect(events.some((text) => text.includes("Signal reaction added"))).toBe(shouldEnqueue); expect(sendMock).not.toHaveBeenCalled(); expect(upsertPairingRequestMock).not.toHaveBeenCalled(); }); - it("allows reaction notifications for allowlisted senders when dmPolicy is allowlist", async () => { - setReactionNotificationConfig("all", { - dmPolicy: "allowlist", - allowFrom: ["+15550001111"], - }); - await receiveSingleEnvelope({ - ...makeBaseEnvelope(), - reactionMessage: { - emoji: "✅", - targetAuthor: "+15550002222", - targetSentTimestamp: 2, - }, - }); - - const events = getDirectSignalEventsFor("+15550001111"); - expect(events.some((text) => text.includes("Signal reaction added"))).toBe(true); - }); - it("notifies on own reactions when target includes uuid + phone", async () => { setReactionNotificationConfig("own", { account: "+15550002222" }); await receiveSingleEnvelope({ diff --git a/src/signal/monitor/event-handler.ts b/src/signal/monitor/event-handler.ts index 3cdd8cf5e9dc..e87158d8d8d7 100644 --- a/src/signal/monitor/event-handler.ts +++ b/src/signal/monitor/event-handler.ts @@ -49,9 +49,15 @@ import { resolveSignalPeerId, resolveSignalRecipient, resolveSignalSender, + type SignalSender, } from "../identity.js"; import { sendMessageSignal, sendReadReceiptSignal, sendTypingSignal } from "../send.js"; -import type { SignalEventHandlerDeps, SignalReceivePayload } from "./event-handler.types.js"; +import type { + SignalEnvelope, + SignalEventHandlerDeps, + SignalReactionMessage, + SignalReceivePayload, +} from "./event-handler.types.js"; import { renderSignalMentions } from "./mentions.js"; export function createSignalEventHandler(deps: SignalEventHandlerDeps) { const inboundDebounceMs = resolveInboundDebounceMs({ cfg: deps.cfg, channel: "signal" }); @@ -321,6 +327,85 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { }, }); + function handleReactionOnlyInbound(params: { + envelope: SignalEnvelope; + sender: SignalSender; + senderDisplay: string; + reaction: SignalReactionMessage; + hasBodyContent: boolean; + resolveAccessDecision: (isGroup: boolean) => { + decision: "allow" | "block" | "pairing"; + reason: string; + }; + }): boolean { + if (params.hasBodyContent) { + return false; + } + if (params.reaction.isRemove) { + return true; // Ignore reaction removals + } + const emojiLabel = params.reaction.emoji?.trim() || "emoji"; + const senderName = params.envelope.sourceName ?? params.senderDisplay; + logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`); + const groupId = params.reaction.groupInfo?.groupId ?? undefined; + const groupName = params.reaction.groupInfo?.groupName ?? undefined; + const isGroup = Boolean(groupId); + const reactionAccess = params.resolveAccessDecision(isGroup); + if (reactionAccess.decision !== "allow") { + logVerbose( + `Blocked signal reaction sender ${params.senderDisplay} (${reactionAccess.reason})`, + ); + return true; + } + const targets = deps.resolveSignalReactionTargets(params.reaction); + const shouldNotify = deps.shouldEmitSignalReactionNotification({ + mode: deps.reactionMode, + account: deps.account, + targets, + sender: params.sender, + allowlist: deps.reactionAllowlist, + }); + if (!shouldNotify) { + return true; + } + + const senderPeerId = resolveSignalPeerId(params.sender); + const route = resolveAgentRoute({ + cfg: deps.cfg, + channel: "signal", + accountId: deps.accountId, + peer: { + kind: isGroup ? "group" : "direct", + id: isGroup ? (groupId ?? "unknown") : senderPeerId, + }, + }); + const groupLabel = isGroup ? `${groupName ?? "Signal Group"} id:${groupId}` : undefined; + const messageId = params.reaction.targetSentTimestamp + ? String(params.reaction.targetSentTimestamp) + : "unknown"; + const text = deps.buildSignalReactionSystemEventText({ + emojiLabel, + actorLabel: senderName, + messageId, + targetLabel: targets[0]?.display, + groupLabel, + }); + const senderId = formatSignalSenderId(params.sender); + const contextKey = [ + "signal", + "reaction", + "added", + messageId, + senderId, + emojiLabel, + groupId ?? "", + ] + .filter(Boolean) + .join(":"); + enqueueSystemEvent(text, { sessionKey: route.sessionKey, contextKey }); + return true; + } + return async (event: { event?: string; data?: string }) => { if (event.event !== "receive" || !event.data) { return; @@ -375,13 +460,13 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { deps.dmPolicy === "allowlist" ? [] : await readChannelAllowFromStore("signal").catch(() => []); - const { effectiveAllowFrom: effectiveDmAllow } = resolveEffectiveAllowFromLists({ - allowFrom: deps.allowFrom, - groupAllowFrom: deps.groupAllowFrom, - storeAllowFrom, - dmPolicy: deps.dmPolicy, - }); - const effectiveGroupAllow = [...deps.groupAllowFrom, ...storeAllowFrom]; + const { effectiveAllowFrom: effectiveDmAllow, effectiveGroupAllowFrom: effectiveGroupAllow } = + resolveEffectiveAllowFromLists({ + allowFrom: deps.allowFrom, + groupAllowFrom: deps.groupAllowFrom, + storeAllowFrom, + dmPolicy: deps.dmPolicy, + }); const resolveAccessDecision = (isGroup: boolean) => resolveDmGroupAccessDecision({ isGroup, @@ -394,67 +479,17 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { const dmAccess = resolveAccessDecision(false); const dmAllowed = dmAccess.decision === "allow"; - if (reaction && !hasBodyContent) { - if (reaction.isRemove) { - return; - } // Ignore reaction removals - const emojiLabel = reaction.emoji?.trim() || "emoji"; - const senderName = envelope.sourceName ?? senderDisplay; - logVerbose(`signal reaction: ${emojiLabel} from ${senderName}`); - const groupId = reaction.groupInfo?.groupId ?? undefined; - const groupName = reaction.groupInfo?.groupName ?? undefined; - const isGroup = Boolean(groupId); - const reactionAccess = resolveAccessDecision(isGroup); - if (reactionAccess.decision !== "allow") { - logVerbose(`Blocked signal reaction sender ${senderDisplay} (${reactionAccess.reason})`); - return; - } - const targets = deps.resolveSignalReactionTargets(reaction); - const shouldNotify = deps.shouldEmitSignalReactionNotification({ - mode: deps.reactionMode, - account: deps.account, - targets, + if ( + reaction && + handleReactionOnlyInbound({ + envelope, sender, - allowlist: deps.reactionAllowlist, - }); - if (!shouldNotify) { - return; - } - - const senderPeerId = resolveSignalPeerId(sender); - const route = resolveAgentRoute({ - cfg: deps.cfg, - channel: "signal", - accountId: deps.accountId, - peer: { - kind: isGroup ? "group" : "direct", - id: isGroup ? (groupId ?? "unknown") : senderPeerId, - }, - }); - const groupLabel = isGroup ? `${groupName ?? "Signal Group"} id:${groupId}` : undefined; - const messageId = reaction.targetSentTimestamp - ? String(reaction.targetSentTimestamp) - : "unknown"; - const text = deps.buildSignalReactionSystemEventText({ - emojiLabel, - actorLabel: senderName, - messageId, - targetLabel: targets[0]?.display, - groupLabel, - }); - const senderId = formatSignalSenderId(sender); - const contextKey = [ - "signal", - "reaction", - "added", - messageId, - senderId, - emojiLabel, - groupId ?? "", - ] - .filter(Boolean) - .join(":"); - enqueueSystemEvent(text, { sessionKey: route.sessionKey, contextKey }); + senderDisplay, + reaction, + hasBodyContent, + resolveAccessDecision, + }) + ) { return; } if (!dataMessage) { From e56b0cf1a04f992ac6ebc775899f48ea31687640 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:02:36 +0100 Subject: [PATCH 049/160] fix: enforce telegram reaction authorization --- CHANGELOG.md | 1 + docs/channels/telegram.md | 1 + src/telegram/bot-handlers.ts | 187 +++++++++++++++++++++++++---------- src/telegram/bot.test.ts | 125 +++++++++++++++++++++++ 4 files changed, 260 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb4c5ec1afcf..6d5bd81477d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Docs: https://docs.openclaw.ai - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. diff --git a/docs/channels/telegram.md b/docs/channels/telegram.md index 6a454bd8dcfa..46db95202b4e 100644 --- a/docs/channels/telegram.md +++ b/docs/channels/telegram.md @@ -553,6 +553,7 @@ curl "https://api.telegram.org/bot/getUpdates" Notes: - `own` means user reactions to bot-sent messages only (best-effort via sent-message cache). + - Reaction events still respect Telegram access controls (`dmPolicy`, `allowFrom`, `groupPolicy`, `groupAllowFrom`); unauthorized senders are dropped. - Telegram does not provide thread IDs in reaction updates. - non-forum groups route to group chat session - forum groups route to the group general-topic session (`:topic:1`), not the exact originating topic diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index e4d42cd889e5..e76607172937 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -507,6 +507,99 @@ export const registerTelegramHandlers = ({ return false; }; + const isTelegramEventSenderAuthorized = async (params: { + chatId: number; + chatTitle?: string; + isGroup: boolean; + isForum: boolean; + messageThreadId?: number; + senderId: string; + senderUsername: string; + enforceDirectAuthorization: boolean; + enforceGroupAllowlistAuthorization: boolean; + deniedDmReason: string; + deniedGroupReason: string; + groupAllowContext?: Awaited>; + }) => { + const { + chatId, + chatTitle, + isGroup, + isForum, + messageThreadId, + senderId, + senderUsername, + enforceDirectAuthorization, + enforceGroupAllowlistAuthorization, + deniedDmReason, + deniedGroupReason, + groupAllowContext: preResolvedGroupAllowContext, + } = params; + const dmPolicy = telegramCfg.dmPolicy ?? "pairing"; + const groupAllowContext = + preResolvedGroupAllowContext ?? + (await resolveTelegramGroupAllowFromContext({ + chatId, + accountId, + dmPolicy, + isForum, + messageThreadId, + groupAllowFrom, + resolveTelegramGroupConfig, + })); + const { + resolvedThreadId, + storeAllowFrom, + groupConfig, + topicConfig, + effectiveGroupAllow, + hasGroupAllowOverride, + } = groupAllowContext; + if ( + shouldSkipGroupMessage({ + isGroup, + chatId, + chatTitle, + resolvedThreadId, + senderId, + senderUsername, + effectiveGroupAllow, + hasGroupAllowOverride, + groupConfig, + topicConfig, + }) + ) { + return false; + } + + if (!isGroup && enforceDirectAuthorization) { + if (dmPolicy === "disabled") { + logVerbose( + `Blocked telegram direct event from ${senderId || "unknown"} (${deniedDmReason})`, + ); + return false; + } + if (dmPolicy !== "open") { + const effectiveDmAllow = normalizeAllowFromWithStore({ + allowFrom, + storeAllowFrom, + dmPolicy, + }); + if (!isAllowlistAuthorized(effectiveDmAllow, senderId, senderUsername)) { + logVerbose(`Blocked telegram direct sender ${senderId || "unknown"} (${deniedDmReason})`); + return false; + } + } + } + if (isGroup && enforceGroupAllowlistAuthorization) { + if (!isAllowlistAuthorized(effectiveGroupAllow, senderId, senderUsername)) { + logVerbose(`Blocked telegram group sender ${senderId || "unknown"} (${deniedGroupReason})`); + return false; + } + } + return true; + }; + // Handle emoji reactions to messages. bot.on("message_reaction", async (ctx) => { try { @@ -521,6 +614,10 @@ export const registerTelegramHandlers = ({ const chatId = reaction.chat.id; const messageId = reaction.message_id; const user = reaction.user; + const senderId = user?.id != null ? String(user.id) : ""; + const senderUsername = user?.username ?? ""; + const isGroup = reaction.chat.type === "group" || reaction.chat.type === "supergroup"; + const isForum = reaction.chat.is_forum === true; // Resolve reaction notification mode (default: "own"). const reactionMode = telegramCfg.reactionNotifications ?? "own"; @@ -533,6 +630,21 @@ export const registerTelegramHandlers = ({ if (reactionMode === "own" && !wasSentByBot(chatId, messageId)) { return; } + const senderAuthorized = await isTelegramEventSenderAuthorized({ + chatId, + chatTitle: reaction.chat.title, + isGroup, + isForum, + senderId, + senderUsername, + enforceDirectAuthorization: true, + enforceGroupAllowlistAuthorization: false, + deniedDmReason: "reaction unauthorized by dm policy/allowlist", + deniedGroupReason: "reaction unauthorized by group allowlist", + }); + if (!senderAuthorized) { + return; + } // Detect added reactions. const oldEmojis = new Set( @@ -552,12 +664,12 @@ export const registerTelegramHandlers = ({ const senderName = user ? [user.first_name, user.last_name].filter(Boolean).join(" ").trim() || user.username : undefined; - const senderUsername = user?.username ? `@${user.username}` : undefined; + const senderUsernameLabel = user?.username ? `@${user.username}` : undefined; let senderLabel = senderName; - if (senderName && senderUsername) { - senderLabel = `${senderName} (${senderUsername})`; - } else if (!senderName && senderUsername) { - senderLabel = senderUsername; + if (senderName && senderUsernameLabel) { + senderLabel = `${senderName} (${senderUsernameLabel})`; + } else if (!senderName && senderUsernameLabel) { + senderLabel = senderUsernameLabel; } if (!senderLabel && user?.id) { senderLabel = `id:${user.id}`; @@ -567,8 +679,6 @@ export const registerTelegramHandlers = ({ // Reactions target a specific message_id; the Telegram Bot API does not include // message_thread_id on MessageReactionUpdated, so we route to the chat-level // session (forum topic routing is not available for reactions). - const isGroup = reaction.chat.type === "group" || reaction.chat.type === "supergroup"; - const isForum = reaction.chat.is_forum === true; const resolvedThreadId = isForum ? resolveTelegramForumThreadId({ isForum, messageThreadId: undefined }) : undefined; @@ -864,58 +974,27 @@ export const registerTelegramHandlers = ({ groupAllowFrom, resolveTelegramGroupConfig, }); - const { - resolvedThreadId, - storeAllowFrom, - groupConfig, - topicConfig, - effectiveGroupAllow, - hasGroupAllowOverride, - } = groupAllowContext; - const dmPolicy = telegramCfg.dmPolicy ?? "pairing"; - const effectiveDmAllow = normalizeAllowFromWithStore({ - allowFrom: telegramCfg.allowFrom, - storeAllowFrom, - dmPolicy, - }); + const { resolvedThreadId, storeAllowFrom } = groupAllowContext; const senderId = callback.from?.id ? String(callback.from.id) : ""; const senderUsername = callback.from?.username ?? ""; - if ( - shouldSkipGroupMessage({ - isGroup, - chatId, - chatTitle: callbackMessage.chat.title, - resolvedThreadId, - senderId, - senderUsername, - effectiveGroupAllow, - hasGroupAllowOverride, - groupConfig, - topicConfig, - }) - ) { + const senderAuthorized = await isTelegramEventSenderAuthorized({ + chatId, + chatTitle: callbackMessage.chat.title, + isGroup, + isForum, + messageThreadId, + senderId, + senderUsername, + enforceDirectAuthorization: inlineButtonsScope === "allowlist", + enforceGroupAllowlistAuthorization: inlineButtonsScope === "allowlist", + deniedDmReason: "callback unauthorized by inlineButtonsScope allowlist", + deniedGroupReason: "callback unauthorized by inlineButtonsScope allowlist", + groupAllowContext, + }); + if (!senderAuthorized) { return; } - if (inlineButtonsScope === "allowlist") { - if (!isGroup) { - if (dmPolicy === "disabled") { - return; - } - if (dmPolicy !== "open") { - const allowed = isAllowlistAuthorized(effectiveDmAllow, senderId, senderUsername); - if (!allowed) { - return; - } - } - } else { - const allowed = isAllowlistAuthorized(effectiveGroupAllow, senderId, senderUsername); - if (!allowed) { - return; - } - } - } - const paginationMatch = data.match(/^commands_page_(\d+|noop)(?::(.+))?$/); if (paginationMatch) { const pageValue = paginationMatch[1]; diff --git a/src/telegram/bot.test.ts b/src/telegram/bot.test.ts index 03380dbbf623..4a605abb1706 100644 --- a/src/telegram/bot.test.ts +++ b/src/telegram/bot.test.ts @@ -832,6 +832,131 @@ describe("createTelegramBot", () => { ); }); + it("blocks reaction when dmPolicy is disabled", async () => { + onSpy.mockClear(); + enqueueSystemEventSpy.mockClear(); + + loadConfig.mockReturnValue({ + channels: { + telegram: { dmPolicy: "disabled", reactionNotifications: "all" }, + }, + }); + + createTelegramBot({ token: "tok" }); + const handler = getOnHandler("message_reaction") as ( + ctx: Record, + ) => Promise; + + await handler({ + update: { update_id: 510 }, + messageReaction: { + chat: { id: 1234, type: "private" }, + message_id: 42, + user: { id: 9, first_name: "Ada" }, + date: 1736380800, + old_reaction: [], + new_reaction: [{ type: "emoji", emoji: "👍" }], + }, + }); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + + it("blocks reaction in allowlist mode for unauthorized direct sender", async () => { + onSpy.mockClear(); + enqueueSystemEventSpy.mockClear(); + + loadConfig.mockReturnValue({ + channels: { + telegram: { dmPolicy: "allowlist", allowFrom: ["12345"], reactionNotifications: "all" }, + }, + }); + + createTelegramBot({ token: "tok" }); + const handler = getOnHandler("message_reaction") as ( + ctx: Record, + ) => Promise; + + await handler({ + update: { update_id: 511 }, + messageReaction: { + chat: { id: 1234, type: "private" }, + message_id: 42, + user: { id: 9, first_name: "Ada" }, + date: 1736380800, + old_reaction: [], + new_reaction: [{ type: "emoji", emoji: "👍" }], + }, + }); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + + it("allows reaction in allowlist mode for authorized direct sender", async () => { + onSpy.mockClear(); + enqueueSystemEventSpy.mockClear(); + + loadConfig.mockReturnValue({ + channels: { + telegram: { dmPolicy: "allowlist", allowFrom: ["9"], reactionNotifications: "all" }, + }, + }); + + createTelegramBot({ token: "tok" }); + const handler = getOnHandler("message_reaction") as ( + ctx: Record, + ) => Promise; + + await handler({ + update: { update_id: 512 }, + messageReaction: { + chat: { id: 1234, type: "private" }, + message_id: 42, + user: { id: 9, first_name: "Ada" }, + date: 1736380800, + old_reaction: [], + new_reaction: [{ type: "emoji", emoji: "👍" }], + }, + }); + + expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1); + }); + + it("blocks reaction in group allowlist mode for unauthorized sender", async () => { + onSpy.mockClear(); + enqueueSystemEventSpy.mockClear(); + + loadConfig.mockReturnValue({ + channels: { + telegram: { + dmPolicy: "open", + groupPolicy: "allowlist", + groupAllowFrom: ["12345"], + reactionNotifications: "all", + }, + }, + }); + + createTelegramBot({ token: "tok" }); + const handler = getOnHandler("message_reaction") as ( + ctx: Record, + ) => Promise; + + await handler({ + update: { update_id: 513 }, + messageReaction: { + chat: { id: 9999, type: "supergroup" }, + message_id: 77, + user: { id: 9, first_name: "Ada" }, + date: 1736380800, + old_reaction: [], + new_reaction: [{ type: "emoji", emoji: "🔥" }], + }, + }); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + it("skips reaction when reactionNotifications is off", async () => { onSpy.mockClear(); enqueueSystemEventSpy.mockClear(); From 496a76c03ba85e15ea715e5a583e498ae04d36e3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:01:17 +0100 Subject: [PATCH 050/160] fix(security): harden browser trace/download temp path handling --- CHANGELOG.md | 1 + src/browser/paths.test.ts | 40 ++++++ src/browser/paths.ts | 62 +++++++++ src/browser/routes/agent.act.ts | 16 ++- src/browser/routes/agent.debug.ts | 4 +- ...-contract-form-layout-act-commands.test.ts | 73 ++++++++++- src/infra/tmp-openclaw-dir.test.ts | 119 +++++++++++++----- src/infra/tmp-openclaw-dir.ts | 47 ++++++- 8 files changed, 322 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d5bd81477d8..521368ffd163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Docs: https://docs.openclaw.ai - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. +- Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. diff --git a/src/browser/paths.test.ts b/src/browser/paths.test.ts index 1599c3895b2a..0fe27fe1e4e6 100644 --- a/src/browser/paths.test.ts +++ b/src/browser/paths.test.ts @@ -7,6 +7,7 @@ import { resolvePathsWithinRoot, resolvePathWithinRoot, resolveStrictExistingPathsWithinRoot, + resolveWritablePathWithinRoot, } from "./paths.js"; async function createFixtureRoot(): Promise<{ baseDir: string; uploadsDir: string }> { @@ -245,6 +246,45 @@ describe("resolvePathWithinRoot", () => { }); }); +describe("resolveWritablePathWithinRoot", () => { + it("accepts a writable path under root when parent is a real directory", async () => { + await withFixtureRoot(async ({ uploadsDir }) => { + const result = await resolveWritablePathWithinRoot({ + rootDir: uploadsDir, + requestedPath: "safe.txt", + scopeLabel: "uploads directory", + }); + expect(result).toEqual({ + ok: true, + path: path.resolve(uploadsDir, "safe.txt"), + }); + }); + }); + + it.runIf(process.platform !== "win32")( + "rejects write paths routed through a symlinked parent directory", + async () => { + await withFixtureRoot(async ({ baseDir, uploadsDir }) => { + const outsideDir = path.join(baseDir, "outside"); + await fs.mkdir(outsideDir, { recursive: true }); + const symlinkDir = path.join(uploadsDir, "escape-link"); + await fs.symlink(outsideDir, symlinkDir); + + const result = await resolveWritablePathWithinRoot({ + rootDir: uploadsDir, + requestedPath: "escape-link/pwned.txt", + scopeLabel: "uploads directory", + }); + + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toContain("must stay within uploads directory"); + } + }); + }, + ); +}); + describe("resolvePathsWithinRoot", () => { it("resolves all valid in-root paths", () => { const result = resolvePathsWithinRoot({ diff --git a/src/browser/paths.ts b/src/browser/paths.ts index 88a541b75dce..34e927f8c5b7 100644 --- a/src/browser/paths.ts +++ b/src/browser/paths.ts @@ -1,6 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import { SafeOpenError, openFileWithinRoot } from "../infra/fs-safe.js"; +import { isNotFoundPathError, isPathInside } from "../infra/path-guards.js"; import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js"; export const DEFAULT_BROWSER_TMP_DIR = resolvePreferredOpenClawTmpDir(); @@ -30,6 +31,67 @@ export function resolvePathWithinRoot(params: { return { ok: true, path: resolved }; } +export async function resolveWritablePathWithinRoot(params: { + rootDir: string; + requestedPath: string; + scopeLabel: string; + defaultFileName?: string; +}): Promise<{ ok: true; path: string } | { ok: false; error: string }> { + const lexical = resolvePathWithinRoot(params); + if (!lexical.ok) { + return lexical; + } + + const invalid = (): { ok: false; error: string } => ({ + ok: false, + error: `Invalid path: must stay within ${params.scopeLabel}`, + }); + + const rootDir = path.resolve(params.rootDir); + let rootRealPath: string; + try { + const rootLstat = await fs.lstat(rootDir); + if (!rootLstat.isDirectory() || rootLstat.isSymbolicLink()) { + return invalid(); + } + rootRealPath = await fs.realpath(rootDir); + } catch { + return invalid(); + } + + const requestedPath = lexical.path; + const parentDir = path.dirname(requestedPath); + try { + const parentLstat = await fs.lstat(parentDir); + if (!parentLstat.isDirectory() || parentLstat.isSymbolicLink()) { + return invalid(); + } + const parentRealPath = await fs.realpath(parentDir); + if (!isPathInside(rootRealPath, parentRealPath)) { + return invalid(); + } + } catch { + return invalid(); + } + + try { + const targetLstat = await fs.lstat(requestedPath); + if (targetLstat.isSymbolicLink() || !targetLstat.isFile()) { + return invalid(); + } + const targetRealPath = await fs.realpath(requestedPath); + if (!isPathInside(rootRealPath, targetRealPath)) { + return invalid(); + } + } catch (err) { + if (!isNotFoundPathError(err)) { + return invalid(); + } + } + + return lexical; +} + export function resolvePathsWithinRoot(params: { rootDir: string; requestedPaths: string[]; diff --git a/src/browser/routes/agent.act.ts b/src/browser/routes/agent.act.ts index 78fa2f6856c0..42ea8444f53f 100644 --- a/src/browser/routes/agent.act.ts +++ b/src/browser/routes/agent.act.ts @@ -1,3 +1,4 @@ +import fs from "node:fs/promises"; import type { BrowserFormField } from "../client-actions-core.js"; import type { BrowserRouteContext } from "../server-context.js"; import { @@ -15,14 +16,17 @@ import { import { DEFAULT_DOWNLOAD_DIR, DEFAULT_UPLOAD_DIR, - resolvePathWithinRoot, + resolveWritablePathWithinRoot, resolveExistingPathsWithinRoot, } from "./path-output.js"; import type { BrowserResponse, BrowserRouteRegistrar } from "./types.js"; import { jsonError, toBoolean, toNumber, toStringArray, toStringOrEmpty } from "./utils.js"; -function resolveDownloadPathOrRespond(res: BrowserResponse, requestedPath: string): string | null { - const downloadPathResult = resolvePathWithinRoot({ +async function resolveDownloadPathOrRespond( + res: BrowserResponse, + requestedPath: string, +): Promise { + const downloadPathResult = await resolveWritablePathWithinRoot({ rootDir: DEFAULT_DOWNLOAD_DIR, requestedPath, scopeLabel: "downloads directory", @@ -466,9 +470,10 @@ export function registerBrowserAgentActRoutes( targetId, feature: "wait for download", run: async ({ cdpUrl, tab, pw }) => { + await fs.mkdir(DEFAULT_DOWNLOAD_DIR, { recursive: true }); let downloadPath: string | undefined; if (out.trim()) { - const resolvedDownloadPath = resolveDownloadPathOrRespond(res, out); + const resolvedDownloadPath = await resolveDownloadPathOrRespond(res, out); if (!resolvedDownloadPath) { return; } @@ -504,7 +509,8 @@ export function registerBrowserAgentActRoutes( targetId, feature: "download", run: async ({ cdpUrl, tab, pw }) => { - const downloadPath = resolveDownloadPathOrRespond(res, out); + await fs.mkdir(DEFAULT_DOWNLOAD_DIR, { recursive: true }); + const downloadPath = await resolveDownloadPathOrRespond(res, out); if (!downloadPath) { return; } diff --git a/src/browser/routes/agent.debug.ts b/src/browser/routes/agent.debug.ts index fab517d9589c..b3b6ee0946cb 100644 --- a/src/browser/routes/agent.debug.ts +++ b/src/browser/routes/agent.debug.ts @@ -8,7 +8,7 @@ import { resolveTargetIdFromQuery, withPlaywrightRouteContext, } from "./agent.shared.js"; -import { DEFAULT_TRACE_DIR, resolvePathWithinRoot } from "./path-output.js"; +import { DEFAULT_TRACE_DIR, resolveWritablePathWithinRoot } from "./path-output.js"; import type { BrowserRouteRegistrar } from "./types.js"; import { toBoolean, toStringOrEmpty } from "./utils.js"; @@ -122,7 +122,7 @@ export function registerBrowserAgentDebugRoutes( const id = crypto.randomUUID(); const dir = DEFAULT_TRACE_DIR; await fs.mkdir(dir, { recursive: true }); - const tracePathResult = resolvePathWithinRoot({ + const tracePathResult = await resolveWritablePathWithinRoot({ rootDir: dir, requestedPath: out, scopeLabel: "trace directory", diff --git a/src/browser/server.agent-contract-form-layout-act-commands.test.ts b/src/browser/server.agent-contract-form-layout-act-commands.test.ts index 0328736eadea..e96193e59954 100644 --- a/src/browser/server.agent-contract-form-layout-act-commands.test.ts +++ b/src/browser/server.agent-contract-form-layout-act-commands.test.ts @@ -1,7 +1,9 @@ +import fs from "node:fs/promises"; +import os from "node:os"; import path from "node:path"; import { fetch as realFetch } from "undici"; import { describe, expect, it } from "vitest"; -import { DEFAULT_UPLOAD_DIR } from "./paths.js"; +import { DEFAULT_DOWNLOAD_DIR, DEFAULT_TRACE_DIR, DEFAULT_UPLOAD_DIR } from "./paths.js"; import { installAgentContractHooks, postJson, @@ -16,6 +18,23 @@ import { const state = getBrowserControlServerTestState(); const pwMocks = getPwMocks(); +async function withSymlinkPathEscape(params: { + rootDir: string; + run: (relativePath: string) => Promise; +}): Promise { + const outsideDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-route-escape-")); + const linkName = `escape-link-${Date.now()}-${Math.random().toString(16).slice(2)}`; + const linkPath = path.join(params.rootDir, linkName); + await fs.mkdir(params.rootDir, { recursive: true }); + await fs.symlink(outsideDir, linkPath); + try { + return await params.run(`${linkName}/pwned.zip`); + } finally { + await fs.unlink(linkPath).catch(() => {}); + await fs.rm(outsideDir, { recursive: true, force: true }).catch(() => {}); + } +} + describe("browser control server", () => { installAgentContractHooks(); @@ -268,6 +287,58 @@ describe("browser control server", () => { expect(pwMocks.downloadViaPlaywright).not.toHaveBeenCalled(); }); + it.runIf(process.platform !== "win32")( + "trace stop rejects symlinked write path escape under trace dir", + async () => { + const base = await startServerAndBase(); + await withSymlinkPathEscape({ + rootDir: DEFAULT_TRACE_DIR, + run: async (pathEscape) => { + const res = await postJson<{ error?: string }>(`${base}/trace/stop`, { + path: pathEscape, + }); + expect(res.error).toContain("Invalid path"); + expect(pwMocks.traceStopViaPlaywright).not.toHaveBeenCalled(); + }, + }); + }, + ); + + it.runIf(process.platform !== "win32")( + "wait/download rejects symlinked write path escape under downloads dir", + async () => { + const base = await startServerAndBase(); + await withSymlinkPathEscape({ + rootDir: DEFAULT_DOWNLOAD_DIR, + run: async (pathEscape) => { + const res = await postJson<{ error?: string }>(`${base}/wait/download`, { + path: pathEscape, + }); + expect(res.error).toContain("Invalid path"); + expect(pwMocks.waitForDownloadViaPlaywright).not.toHaveBeenCalled(); + }, + }); + }, + ); + + it.runIf(process.platform !== "win32")( + "download rejects symlinked write path escape under downloads dir", + async () => { + const base = await startServerAndBase(); + await withSymlinkPathEscape({ + rootDir: DEFAULT_DOWNLOAD_DIR, + run: async (pathEscape) => { + const res = await postJson<{ error?: string }>(`${base}/download`, { + ref: "e12", + path: pathEscape, + }); + expect(res.error).toContain("Invalid path"); + expect(pwMocks.downloadViaPlaywright).not.toHaveBeenCalled(); + }, + }); + }, + ); + it("wait/download accepts in-root relative output path", async () => { const base = await startServerAndBase(); const res = await postJson<{ ok?: boolean; download?: { path?: string } }>( diff --git a/src/infra/tmp-openclaw-dir.test.ts b/src/infra/tmp-openclaw-dir.test.ts index 0424e5e02231..f3e3fe362991 100644 --- a/src/infra/tmp-openclaw-dir.test.ts +++ b/src/infra/tmp-openclaw-dir.test.ts @@ -8,24 +8,54 @@ function fallbackTmp(uid = 501) { return path.join("/var/fallback", `openclaw-${uid}`); } +function nodeErrorWithCode(code: string) { + const err = new Error(code) as Error & { code?: string }; + err.code = code; + return err; +} + +function secureDirStat(uid = 501) { + return { + isDirectory: () => true, + isSymbolicLink: () => false, + uid, + mode: 0o40700, + }; +} + function resolveWithMocks(params: { lstatSync: NonNullable; + fallbackLstatSync?: NonNullable; accessSync?: NonNullable; uid?: number; tmpdirPath?: string; }) { + const uid = params.uid ?? 501; + const fallbackPath = fallbackTmp(uid); const accessSync = params.accessSync ?? vi.fn(); + const wrappedLstatSync = vi.fn((target: string) => { + if (target === POSIX_OPENCLAW_TMP_DIR) { + return params.lstatSync(target); + } + if (target === fallbackPath) { + if (params.fallbackLstatSync) { + return params.fallbackLstatSync(target); + } + return secureDirStat(uid); + } + return secureDirStat(uid); + }) as NonNullable; const mkdirSync = vi.fn(); - const getuid = vi.fn(() => params.uid ?? 501); + const getuid = vi.fn(() => uid); const tmpdir = vi.fn(() => params.tmpdirPath ?? "/var/fallback"); const resolved = resolvePreferredOpenClawTmpDir({ accessSync, - lstatSync: params.lstatSync, + lstatSync: wrappedLstatSync, mkdirSync, getuid, tmpdir, }); - return { resolved, accessSync, lstatSync: params.lstatSync, mkdirSync, tmpdir }; + return { resolved, accessSync, lstatSync: wrappedLstatSync, mkdirSync, tmpdir }; } describe("resolvePreferredOpenClawTmpDir", () => { @@ -45,24 +75,12 @@ describe("resolvePreferredOpenClawTmpDir", () => { }); it("prefers /tmp/openclaw when it does not exist but /tmp is writable", () => { - const lstatSyncMock = vi.fn>(() => { - const err = new Error("missing") as Error & { code?: string }; - err.code = "ENOENT"; - throw err; - }); - - // second lstat call (after mkdir) should succeed - lstatSyncMock.mockImplementationOnce(() => { - const err = new Error("missing") as Error & { code?: string }; - err.code = "ENOENT"; - throw err; - }); - lstatSyncMock.mockImplementationOnce(() => ({ - isDirectory: () => true, - isSymbolicLink: () => false, - uid: 501, - mode: 0o40700, - })); + const lstatSyncMock = vi + .fn>() + .mockImplementationOnce(() => { + throw nodeErrorWithCode("ENOENT"); + }) + .mockImplementationOnce(() => secureDirStat(501)); const { resolved, accessSync, mkdirSync, tmpdir } = resolveWithMocks({ lstatSync: lstatSyncMock, @@ -84,7 +102,7 @@ describe("resolvePreferredOpenClawTmpDir", () => { const { resolved, tmpdir } = resolveWithMocks({ lstatSync }); expect(resolved).toBe(fallbackTmp()); - expect(tmpdir).toHaveBeenCalledTimes(1); + expect(tmpdir).toHaveBeenCalled(); }); it("falls back to os.tmpdir()/openclaw when /tmp is not writable", () => { @@ -94,9 +112,7 @@ describe("resolvePreferredOpenClawTmpDir", () => { } }); const lstatSync = vi.fn(() => { - const err = new Error("missing") as Error & { code?: string }; - err.code = "ENOENT"; - throw err; + throw nodeErrorWithCode("ENOENT"); }); const { resolved, tmpdir } = resolveWithMocks({ accessSync, @@ -104,7 +120,7 @@ describe("resolvePreferredOpenClawTmpDir", () => { }); expect(resolved).toBe(fallbackTmp()); - expect(tmpdir).toHaveBeenCalledTimes(1); + expect(tmpdir).toHaveBeenCalled(); }); it("falls back when /tmp/openclaw is a symlink", () => { @@ -118,7 +134,7 @@ describe("resolvePreferredOpenClawTmpDir", () => { const { resolved, tmpdir } = resolveWithMocks({ lstatSync }); expect(resolved).toBe(fallbackTmp()); - expect(tmpdir).toHaveBeenCalledTimes(1); + expect(tmpdir).toHaveBeenCalled(); }); it("falls back when /tmp/openclaw is not owned by the current user", () => { @@ -132,7 +148,7 @@ describe("resolvePreferredOpenClawTmpDir", () => { const { resolved, tmpdir } = resolveWithMocks({ lstatSync }); expect(resolved).toBe(fallbackTmp()); - expect(tmpdir).toHaveBeenCalledTimes(1); + expect(tmpdir).toHaveBeenCalled(); }); it("falls back when /tmp/openclaw is group/other writable", () => { @@ -145,6 +161,51 @@ describe("resolvePreferredOpenClawTmpDir", () => { const { resolved, tmpdir } = resolveWithMocks({ lstatSync }); expect(resolved).toBe(fallbackTmp()); - expect(tmpdir).toHaveBeenCalledTimes(1); + expect(tmpdir).toHaveBeenCalled(); + }); + + it("throws when fallback path is a symlink", () => { + const lstatSync = vi.fn(() => ({ + isDirectory: () => true, + isSymbolicLink: () => true, + uid: 501, + mode: 0o120777, + })); + const fallbackLstatSync = vi.fn(() => ({ + isDirectory: () => true, + isSymbolicLink: () => true, + uid: 501, + mode: 0o120777, + })); + + expect(() => + resolveWithMocks({ + lstatSync, + fallbackLstatSync, + }), + ).toThrow(/Unsafe fallback OpenClaw temp dir/); + }); + + it("creates fallback directory when missing, then validates ownership and mode", () => { + const lstatSync = vi.fn(() => ({ + isDirectory: () => true, + isSymbolicLink: () => true, + uid: 501, + mode: 0o120777, + })); + const fallbackLstatSync = vi + .fn>() + .mockImplementationOnce(() => { + throw nodeErrorWithCode("ENOENT"); + }) + .mockImplementationOnce(() => secureDirStat(501)); + + const { resolved, mkdirSync } = resolveWithMocks({ + lstatSync, + fallbackLstatSync, + }); + + expect(resolved).toBe(fallbackTmp()); + expect(mkdirSync).toHaveBeenCalledWith(fallbackTmp(), { recursive: true, mode: 0o700 }); }); }); diff --git a/src/infra/tmp-openclaw-dir.ts b/src/infra/tmp-openclaw-dir.ts index 1e8250b3210f..2897d69e48a0 100644 --- a/src/infra/tmp-openclaw-dir.ts +++ b/src/infra/tmp-openclaw-dir.ts @@ -95,12 +95,53 @@ export function resolvePreferredOpenClawTmpDir( } }; + const resolveFallbackState = ( + fallbackPath: string, + requireWritableAccess: boolean, + ): "available" | "missing" | "invalid" => { + try { + const candidate = lstatSync(fallbackPath); + if (!isTrustedPreferredDir(candidate)) { + return "invalid"; + } + if (requireWritableAccess) { + accessSync(fallbackPath, fs.constants.W_OK | fs.constants.X_OK); + } + return "available"; + } catch (err) { + if (isNodeErrorWithCode(err, "ENOENT")) { + return "missing"; + } + return "invalid"; + } + }; + + const ensureTrustedFallbackDir = (): string => { + const fallbackPath = fallback(); + const state = resolveFallbackState(fallbackPath, true); + if (state === "available") { + return fallbackPath; + } + if (state === "invalid") { + throw new Error(`Unsafe fallback OpenClaw temp dir: ${fallbackPath}`); + } + try { + mkdirSync(fallbackPath, { recursive: true, mode: 0o700 }); + } catch { + throw new Error(`Unable to create fallback OpenClaw temp dir: ${fallbackPath}`); + } + if (resolveFallbackState(fallbackPath, true) !== "available") { + throw new Error(`Unsafe fallback OpenClaw temp dir: ${fallbackPath}`); + } + return fallbackPath; + }; + const existingPreferredState = resolvePreferredState(true); if (existingPreferredState === "available") { return POSIX_OPENCLAW_TMP_DIR; } if (existingPreferredState === "invalid") { - return fallback(); + return ensureTrustedFallbackDir(); } try { @@ -108,10 +149,10 @@ export function resolvePreferredOpenClawTmpDir( // Create with a safe default; subsequent callers expect it exists. mkdirSync(POSIX_OPENCLAW_TMP_DIR, { recursive: true, mode: 0o700 }); if (resolvePreferredState(true) !== "available") { - return fallback(); + return ensureTrustedFallbackDir(); } return POSIX_OPENCLAW_TMP_DIR; } catch { - return fallback(); + return ensureTrustedFallbackDir(); } } From 046feb6b0eee7ec4984a9840ae3c7da982f4c081 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:13:57 +0100 Subject: [PATCH 051/160] refactor: simplify telegram event authorization flow --- src/telegram/bot-handlers.ts | 153 ++++++++++++++++++++--------------- src/telegram/bot.test.ts | 136 ++++++++++++------------------- 2 files changed, 138 insertions(+), 151 deletions(-) diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index e76607172937..a3b4d46a6777 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -17,6 +17,7 @@ import { resolveChannelConfigWrites } from "../channels/plugins/config-writes.js import { loadConfig } from "../config/config.js"; import { writeConfigFile } from "../config/io.js"; import { loadSessionStore, resolveStorePath } from "../config/sessions.js"; +import type { DmPolicy } from "../config/types.base.js"; import type { TelegramGroupConfig, TelegramTopicConfig } from "../config/types.js"; import { danger, logVerbose, warn } from "../globals.js"; import { enqueueSystemEvent } from "../infra/system-events.js"; @@ -507,54 +508,87 @@ export const registerTelegramHandlers = ({ return false; }; - const isTelegramEventSenderAuthorized = async (params: { + type TelegramGroupAllowContext = Awaited>; + type TelegramEventAuthorizationMode = "reaction" | "callback-scope" | "callback-allowlist"; + type TelegramEventAuthorizationResult = { allowed: true } | { allowed: false; reason: string }; + type TelegramEventAuthorizationContext = TelegramGroupAllowContext & { dmPolicy: DmPolicy }; + + const TELEGRAM_EVENT_AUTH_RULES: Record< + TelegramEventAuthorizationMode, + { + enforceDirectAuthorization: boolean; + enforceGroupAllowlistAuthorization: boolean; + deniedDmReason: string; + deniedGroupReason: string; + } + > = { + reaction: { + enforceDirectAuthorization: true, + enforceGroupAllowlistAuthorization: false, + deniedDmReason: "reaction unauthorized by dm policy/allowlist", + deniedGroupReason: "reaction unauthorized by group allowlist", + }, + "callback-scope": { + enforceDirectAuthorization: false, + enforceGroupAllowlistAuthorization: false, + deniedDmReason: "callback unauthorized by inlineButtonsScope", + deniedGroupReason: "callback unauthorized by inlineButtonsScope", + }, + "callback-allowlist": { + enforceDirectAuthorization: true, + enforceGroupAllowlistAuthorization: true, + deniedDmReason: "callback unauthorized by inlineButtonsScope allowlist", + deniedGroupReason: "callback unauthorized by inlineButtonsScope allowlist", + }, + }; + + const resolveTelegramEventAuthorizationContext = async (params: { chatId: number; - chatTitle?: string; - isGroup: boolean; isForum: boolean; messageThreadId?: number; - senderId: string; - senderUsername: string; - enforceDirectAuthorization: boolean; - enforceGroupAllowlistAuthorization: boolean; - deniedDmReason: string; - deniedGroupReason: string; - groupAllowContext?: Awaited>; - }) => { - const { - chatId, - chatTitle, - isGroup, - isForum, - messageThreadId, - senderId, - senderUsername, - enforceDirectAuthorization, - enforceGroupAllowlistAuthorization, - deniedDmReason, - deniedGroupReason, - groupAllowContext: preResolvedGroupAllowContext, - } = params; + groupAllowContext?: TelegramGroupAllowContext; + }): Promise => { const dmPolicy = telegramCfg.dmPolicy ?? "pairing"; const groupAllowContext = - preResolvedGroupAllowContext ?? + params.groupAllowContext ?? (await resolveTelegramGroupAllowFromContext({ - chatId, + chatId: params.chatId, accountId, dmPolicy, - isForum, - messageThreadId, + isForum: params.isForum, + messageThreadId: params.messageThreadId, groupAllowFrom, resolveTelegramGroupConfig, })); + return { dmPolicy, ...groupAllowContext }; + }; + + const authorizeTelegramEventSender = (params: { + chatId: number; + chatTitle?: string; + isGroup: boolean; + senderId: string; + senderUsername: string; + mode: TelegramEventAuthorizationMode; + context: TelegramEventAuthorizationContext; + }): TelegramEventAuthorizationResult => { + const { chatId, chatTitle, isGroup, senderId, senderUsername, mode, context } = params; const { + dmPolicy, resolvedThreadId, storeAllowFrom, groupConfig, topicConfig, effectiveGroupAllow, hasGroupAllowOverride, - } = groupAllowContext; + } = context; + const authRules = TELEGRAM_EVENT_AUTH_RULES[mode]; + const { + enforceDirectAuthorization, + enforceGroupAllowlistAuthorization, + deniedDmReason, + deniedGroupReason, + } = authRules; if ( shouldSkipGroupMessage({ isGroup, @@ -569,7 +603,7 @@ export const registerTelegramHandlers = ({ topicConfig, }) ) { - return false; + return { allowed: false, reason: "group-policy" }; } if (!isGroup && enforceDirectAuthorization) { @@ -577,7 +611,7 @@ export const registerTelegramHandlers = ({ logVerbose( `Blocked telegram direct event from ${senderId || "unknown"} (${deniedDmReason})`, ); - return false; + return { allowed: false, reason: "direct-disabled" }; } if (dmPolicy !== "open") { const effectiveDmAllow = normalizeAllowFromWithStore({ @@ -587,17 +621,17 @@ export const registerTelegramHandlers = ({ }); if (!isAllowlistAuthorized(effectiveDmAllow, senderId, senderUsername)) { logVerbose(`Blocked telegram direct sender ${senderId || "unknown"} (${deniedDmReason})`); - return false; + return { allowed: false, reason: "direct-unauthorized" }; } } } if (isGroup && enforceGroupAllowlistAuthorization) { if (!isAllowlistAuthorized(effectiveGroupAllow, senderId, senderUsername)) { logVerbose(`Blocked telegram group sender ${senderId || "unknown"} (${deniedGroupReason})`); - return false; + return { allowed: false, reason: "group-unauthorized" }; } } - return true; + return { allowed: true }; }; // Handle emoji reactions to messages. @@ -630,19 +664,20 @@ export const registerTelegramHandlers = ({ if (reactionMode === "own" && !wasSentByBot(chatId, messageId)) { return; } - const senderAuthorized = await isTelegramEventSenderAuthorized({ + const eventAuthContext = await resolveTelegramEventAuthorizationContext({ + chatId, + isForum, + }); + const senderAuthorization = authorizeTelegramEventSender({ chatId, chatTitle: reaction.chat.title, isGroup, - isForum, senderId, senderUsername, - enforceDirectAuthorization: true, - enforceGroupAllowlistAuthorization: false, - deniedDmReason: "reaction unauthorized by dm policy/allowlist", - deniedGroupReason: "reaction unauthorized by group allowlist", + mode: "reaction", + context: eventAuthContext, }); - if (!senderAuthorized) { + if (!senderAuthorization.allowed) { return; } @@ -965,33 +1000,26 @@ export const registerTelegramHandlers = ({ const messageThreadId = callbackMessage.message_thread_id; const isForum = callbackMessage.chat.is_forum === true; - const groupAllowContext = await resolveTelegramGroupAllowFromContext({ + const eventAuthContext = await resolveTelegramEventAuthorizationContext({ chatId, - accountId, - dmPolicy: telegramCfg.dmPolicy ?? "pairing", isForum, messageThreadId, - groupAllowFrom, - resolveTelegramGroupConfig, }); - const { resolvedThreadId, storeAllowFrom } = groupAllowContext; + const { resolvedThreadId, storeAllowFrom } = eventAuthContext; const senderId = callback.from?.id ? String(callback.from.id) : ""; const senderUsername = callback.from?.username ?? ""; - const senderAuthorized = await isTelegramEventSenderAuthorized({ + const authorizationMode: TelegramEventAuthorizationMode = + inlineButtonsScope === "allowlist" ? "callback-allowlist" : "callback-scope"; + const senderAuthorization = authorizeTelegramEventSender({ chatId, chatTitle: callbackMessage.chat.title, isGroup, - isForum, - messageThreadId, senderId, senderUsername, - enforceDirectAuthorization: inlineButtonsScope === "allowlist", - enforceGroupAllowlistAuthorization: inlineButtonsScope === "allowlist", - deniedDmReason: "callback unauthorized by inlineButtonsScope allowlist", - deniedGroupReason: "callback unauthorized by inlineButtonsScope allowlist", - groupAllowContext, + mode: authorizationMode, + context: eventAuthContext, }); - if (!senderAuthorized) { + if (!senderAuthorization.allowed) { return; } @@ -1230,25 +1258,20 @@ export const registerTelegramHandlers = ({ if (shouldSkipUpdate(event.ctxForDedupe)) { return; } - const dmPolicy = telegramCfg.dmPolicy ?? "pairing"; - - const groupAllowContext = await resolveTelegramGroupAllowFromContext({ + const eventAuthContext = await resolveTelegramEventAuthorizationContext({ chatId: event.chatId, - accountId, - dmPolicy, isForum: event.isForum, messageThreadId: event.messageThreadId, - groupAllowFrom, - resolveTelegramGroupConfig, }); const { + dmPolicy, resolvedThreadId, storeAllowFrom, groupConfig, topicConfig, effectiveGroupAllow, hasGroupAllowOverride, - } = groupAllowContext; + } = eventAuthContext; const effectiveDmAllow = normalizeAllowFromWithStore({ allowFrom, storeAllowFrom, diff --git a/src/telegram/bot.test.ts b/src/telegram/bot.test.ts index 4a605abb1706..e7e326d0e36d 100644 --- a/src/telegram/bot.test.ts +++ b/src/telegram/bot.test.ts @@ -832,24 +832,12 @@ describe("createTelegramBot", () => { ); }); - it("blocks reaction when dmPolicy is disabled", async () => { - onSpy.mockClear(); - enqueueSystemEventSpy.mockClear(); - - loadConfig.mockReturnValue({ - channels: { - telegram: { dmPolicy: "disabled", reactionNotifications: "all" }, - }, - }); - - createTelegramBot({ token: "tok" }); - const handler = getOnHandler("message_reaction") as ( - ctx: Record, - ) => Promise; - - await handler({ - update: { update_id: 510 }, - messageReaction: { + it.each([ + { + name: "blocks reaction when dmPolicy is disabled", + updateId: 510, + channelConfig: { dmPolicy: "disabled", reactionNotifications: "all" }, + reaction: { chat: { id: 1234, type: "private" }, message_id: 42, user: { id: 9, first_name: "Ada" }, @@ -857,29 +845,17 @@ describe("createTelegramBot", () => { old_reaction: [], new_reaction: [{ type: "emoji", emoji: "👍" }], }, - }); - - expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); - }); - - it("blocks reaction in allowlist mode for unauthorized direct sender", async () => { - onSpy.mockClear(); - enqueueSystemEventSpy.mockClear(); - - loadConfig.mockReturnValue({ - channels: { - telegram: { dmPolicy: "allowlist", allowFrom: ["12345"], reactionNotifications: "all" }, - }, - }); - - createTelegramBot({ token: "tok" }); - const handler = getOnHandler("message_reaction") as ( - ctx: Record, - ) => Promise; - - await handler({ - update: { update_id: 511 }, - messageReaction: { + expectedEnqueueCalls: 0, + }, + { + name: "blocks reaction in allowlist mode for unauthorized direct sender", + updateId: 511, + channelConfig: { + dmPolicy: "allowlist", + allowFrom: ["12345"], + reactionNotifications: "all", + }, + reaction: { chat: { id: 1234, type: "private" }, message_id: 42, user: { id: 9, first_name: "Ada" }, @@ -887,29 +863,13 @@ describe("createTelegramBot", () => { old_reaction: [], new_reaction: [{ type: "emoji", emoji: "👍" }], }, - }); - - expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); - }); - - it("allows reaction in allowlist mode for authorized direct sender", async () => { - onSpy.mockClear(); - enqueueSystemEventSpy.mockClear(); - - loadConfig.mockReturnValue({ - channels: { - telegram: { dmPolicy: "allowlist", allowFrom: ["9"], reactionNotifications: "all" }, - }, - }); - - createTelegramBot({ token: "tok" }); - const handler = getOnHandler("message_reaction") as ( - ctx: Record, - ) => Promise; - - await handler({ - update: { update_id: 512 }, - messageReaction: { + expectedEnqueueCalls: 0, + }, + { + name: "allows reaction in allowlist mode for authorized direct sender", + updateId: 512, + channelConfig: { dmPolicy: "allowlist", allowFrom: ["9"], reactionNotifications: "all" }, + reaction: { chat: { id: 1234, type: "private" }, message_id: 42, user: { id: 9, first_name: "Ada" }, @@ -917,23 +877,34 @@ describe("createTelegramBot", () => { old_reaction: [], new_reaction: [{ type: "emoji", emoji: "👍" }], }, - }); - - expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1); - }); - - it("blocks reaction in group allowlist mode for unauthorized sender", async () => { + expectedEnqueueCalls: 1, + }, + { + name: "blocks reaction in group allowlist mode for unauthorized sender", + updateId: 513, + channelConfig: { + dmPolicy: "open", + groupPolicy: "allowlist", + groupAllowFrom: ["12345"], + reactionNotifications: "all", + }, + reaction: { + chat: { id: 9999, type: "supergroup" }, + message_id: 77, + user: { id: 9, first_name: "Ada" }, + date: 1736380800, + old_reaction: [], + new_reaction: [{ type: "emoji", emoji: "🔥" }], + }, + expectedEnqueueCalls: 0, + }, + ])("$name", async ({ updateId, channelConfig, reaction, expectedEnqueueCalls }) => { onSpy.mockClear(); enqueueSystemEventSpy.mockClear(); loadConfig.mockReturnValue({ channels: { - telegram: { - dmPolicy: "open", - groupPolicy: "allowlist", - groupAllowFrom: ["12345"], - reactionNotifications: "all", - }, + telegram: channelConfig, }, }); @@ -943,18 +914,11 @@ describe("createTelegramBot", () => { ) => Promise; await handler({ - update: { update_id: 513 }, - messageReaction: { - chat: { id: 9999, type: "supergroup" }, - message_id: 77, - user: { id: 9, first_name: "Ada" }, - date: 1736380800, - old_reaction: [], - new_reaction: [{ type: "emoji", emoji: "🔥" }], - }, + update: { update_id: updateId }, + messageReaction: reaction, }); - expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(expectedEnqueueCalls); }); it("skips reaction when reactionNotifications is off", async () => { From f41715a18f74d44154c78df2447dc34a347eeee7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:21:20 +0100 Subject: [PATCH 052/160] refactor(browser): split act route modules and dedupe path guards --- src/browser/paths.ts | 116 +++++++------ src/browser/routes/agent.act.download.ts | 97 +++++++++++ src/browser/routes/agent.act.hooks.ts | 100 ++++++++++++ src/browser/routes/agent.act.ts | 198 +---------------------- src/browser/routes/agent.debug.ts | 16 +- src/browser/routes/output-paths.ts | 31 ++++ src/infra/tmp-openclaw-dir.ts | 36 +---- 7 files changed, 319 insertions(+), 275 deletions(-) create mode 100644 src/browser/routes/agent.act.download.ts create mode 100644 src/browser/routes/agent.act.hooks.ts create mode 100644 src/browser/routes/output-paths.ts diff --git a/src/browser/paths.ts b/src/browser/paths.ts index 34e927f8c5b7..e171f40c732c 100644 --- a/src/browser/paths.ts +++ b/src/browser/paths.ts @@ -9,6 +9,58 @@ export const DEFAULT_TRACE_DIR = DEFAULT_BROWSER_TMP_DIR; export const DEFAULT_DOWNLOAD_DIR = path.join(DEFAULT_BROWSER_TMP_DIR, "downloads"); export const DEFAULT_UPLOAD_DIR = path.join(DEFAULT_BROWSER_TMP_DIR, "uploads"); +type InvalidPathResult = { ok: false; error: string }; + +function invalidPath(scopeLabel: string): InvalidPathResult { + return { + ok: false, + error: `Invalid path: must stay within ${scopeLabel}`, + }; +} + +async function resolveRealPathIfExists(targetPath: string): Promise { + try { + return await fs.realpath(targetPath); + } catch { + return undefined; + } +} + +async function resolveTrustedRootRealPath(rootDir: string): Promise { + try { + const rootLstat = await fs.lstat(rootDir); + if (!rootLstat.isDirectory() || rootLstat.isSymbolicLink()) { + return undefined; + } + return await fs.realpath(rootDir); + } catch { + return undefined; + } +} + +async function validateCanonicalPathWithinRoot(params: { + rootRealPath: string; + candidatePath: string; + expect: "directory" | "file"; +}): Promise<"ok" | "not-found" | "invalid"> { + try { + const candidateLstat = await fs.lstat(params.candidatePath); + if (candidateLstat.isSymbolicLink()) { + return "invalid"; + } + if (params.expect === "directory" && !candidateLstat.isDirectory()) { + return "invalid"; + } + if (params.expect === "file" && !candidateLstat.isFile()) { + return "invalid"; + } + const candidateRealPath = await fs.realpath(params.candidatePath); + return isPathInside(params.rootRealPath, candidateRealPath) ? "ok" : "invalid"; + } catch (err) { + return isNotFoundPathError(err) ? "not-found" : "invalid"; + } +} + export function resolvePathWithinRoot(params: { rootDir: string; requestedPath: string; @@ -42,51 +94,30 @@ export async function resolveWritablePathWithinRoot(params: { return lexical; } - const invalid = (): { ok: false; error: string } => ({ - ok: false, - error: `Invalid path: must stay within ${params.scopeLabel}`, - }); - const rootDir = path.resolve(params.rootDir); - let rootRealPath: string; - try { - const rootLstat = await fs.lstat(rootDir); - if (!rootLstat.isDirectory() || rootLstat.isSymbolicLink()) { - return invalid(); - } - rootRealPath = await fs.realpath(rootDir); - } catch { - return invalid(); + const rootRealPath = await resolveTrustedRootRealPath(rootDir); + if (!rootRealPath) { + return invalidPath(params.scopeLabel); } const requestedPath = lexical.path; const parentDir = path.dirname(requestedPath); - try { - const parentLstat = await fs.lstat(parentDir); - if (!parentLstat.isDirectory() || parentLstat.isSymbolicLink()) { - return invalid(); - } - const parentRealPath = await fs.realpath(parentDir); - if (!isPathInside(rootRealPath, parentRealPath)) { - return invalid(); - } - } catch { - return invalid(); + const parentStatus = await validateCanonicalPathWithinRoot({ + rootRealPath, + candidatePath: parentDir, + expect: "directory", + }); + if (parentStatus !== "ok") { + return invalidPath(params.scopeLabel); } - try { - const targetLstat = await fs.lstat(requestedPath); - if (targetLstat.isSymbolicLink() || !targetLstat.isFile()) { - return invalid(); - } - const targetRealPath = await fs.realpath(requestedPath); - if (!isPathInside(rootRealPath, targetRealPath)) { - return invalid(); - } - } catch (err) { - if (!isNotFoundPathError(err)) { - return invalid(); - } + const targetStatus = await validateCanonicalPathWithinRoot({ + rootRealPath, + candidatePath: requestedPath, + expect: "file", + }); + if (targetStatus === "invalid") { + return invalidPath(params.scopeLabel); } return lexical; @@ -141,13 +172,8 @@ async function resolveCheckedPathsWithinRoot(params: { allowMissingFallback: boolean; }): Promise<{ ok: true; paths: string[] } | { ok: false; error: string }> { const rootDir = path.resolve(params.rootDir); - let rootRealPath: string | undefined; - try { - rootRealPath = await fs.realpath(rootDir); - } catch { - // Keep historical behavior for missing roots and rely on openFileWithinRoot for final checks. - rootRealPath = undefined; - } + // Keep historical behavior for missing roots and rely on openFileWithinRoot for final checks. + const rootRealPath = await resolveRealPathIfExists(rootDir); const isInRoot = (relativePath: string) => Boolean(relativePath) && !relativePath.startsWith("..") && !path.isAbsolute(relativePath); diff --git a/src/browser/routes/agent.act.download.ts b/src/browser/routes/agent.act.download.ts new file mode 100644 index 000000000000..d08287fea59f --- /dev/null +++ b/src/browser/routes/agent.act.download.ts @@ -0,0 +1,97 @@ +import type { BrowserRouteContext } from "../server-context.js"; +import { readBody, resolveTargetIdFromBody, withPlaywrightRouteContext } from "./agent.shared.js"; +import { ensureOutputRootDir, resolveWritableOutputPathOrRespond } from "./output-paths.js"; +import { DEFAULT_DOWNLOAD_DIR } from "./path-output.js"; +import type { BrowserRouteRegistrar } from "./types.js"; +import { jsonError, toNumber, toStringOrEmpty } from "./utils.js"; + +function buildDownloadRequestBase(cdpUrl: string, targetId: string, timeoutMs: number | undefined) { + return { + cdpUrl, + targetId, + timeoutMs: timeoutMs ?? undefined, + }; +} + +export function registerBrowserAgentActDownloadRoutes( + app: BrowserRouteRegistrar, + ctx: BrowserRouteContext, +) { + app.post("/wait/download", async (req, res) => { + const body = readBody(req); + const targetId = resolveTargetIdFromBody(body); + const out = toStringOrEmpty(body.path) || ""; + const timeoutMs = toNumber(body.timeoutMs); + + await withPlaywrightRouteContext({ + req, + res, + ctx, + targetId, + feature: "wait for download", + run: async ({ cdpUrl, tab, pw }) => { + await ensureOutputRootDir(DEFAULT_DOWNLOAD_DIR); + let downloadPath: string | undefined; + if (out.trim()) { + const resolvedDownloadPath = await resolveWritableOutputPathOrRespond({ + res, + rootDir: DEFAULT_DOWNLOAD_DIR, + requestedPath: out, + scopeLabel: "downloads directory", + }); + if (!resolvedDownloadPath) { + return; + } + downloadPath = resolvedDownloadPath; + } + const requestBase = buildDownloadRequestBase(cdpUrl, tab.targetId, timeoutMs); + const result = await pw.waitForDownloadViaPlaywright({ + ...requestBase, + path: downloadPath, + }); + res.json({ ok: true, targetId: tab.targetId, download: result }); + }, + }); + }); + + app.post("/download", async (req, res) => { + const body = readBody(req); + const targetId = resolveTargetIdFromBody(body); + const ref = toStringOrEmpty(body.ref); + const out = toStringOrEmpty(body.path); + const timeoutMs = toNumber(body.timeoutMs); + if (!ref) { + return jsonError(res, 400, "ref is required"); + } + if (!out) { + return jsonError(res, 400, "path is required"); + } + + await withPlaywrightRouteContext({ + req, + res, + ctx, + targetId, + feature: "download", + run: async ({ cdpUrl, tab, pw }) => { + await ensureOutputRootDir(DEFAULT_DOWNLOAD_DIR); + const downloadPath = await resolveWritableOutputPathOrRespond({ + res, + rootDir: DEFAULT_DOWNLOAD_DIR, + requestedPath: out, + scopeLabel: "downloads directory", + }); + if (!downloadPath) { + return; + } + const requestBase = buildDownloadRequestBase(cdpUrl, tab.targetId, timeoutMs); + const result = await pw.downloadViaPlaywright({ + ...requestBase, + ref, + path: downloadPath, + }); + res.json({ ok: true, targetId: tab.targetId, download: result }); + }, + }); + }); +} diff --git a/src/browser/routes/agent.act.hooks.ts b/src/browser/routes/agent.act.hooks.ts new file mode 100644 index 000000000000..56d97bb03d3d --- /dev/null +++ b/src/browser/routes/agent.act.hooks.ts @@ -0,0 +1,100 @@ +import type { BrowserRouteContext } from "../server-context.js"; +import { readBody, resolveTargetIdFromBody, withPlaywrightRouteContext } from "./agent.shared.js"; +import { DEFAULT_UPLOAD_DIR, resolveExistingPathsWithinRoot } from "./path-output.js"; +import type { BrowserRouteRegistrar } from "./types.js"; +import { jsonError, toBoolean, toNumber, toStringArray, toStringOrEmpty } from "./utils.js"; + +export function registerBrowserAgentActHookRoutes( + app: BrowserRouteRegistrar, + ctx: BrowserRouteContext, +) { + app.post("/hooks/file-chooser", async (req, res) => { + const body = readBody(req); + const targetId = resolveTargetIdFromBody(body); + const ref = toStringOrEmpty(body.ref) || undefined; + const inputRef = toStringOrEmpty(body.inputRef) || undefined; + const element = toStringOrEmpty(body.element) || undefined; + const paths = toStringArray(body.paths) ?? []; + const timeoutMs = toNumber(body.timeoutMs); + if (!paths.length) { + return jsonError(res, 400, "paths are required"); + } + + await withPlaywrightRouteContext({ + req, + res, + ctx, + targetId, + feature: "file chooser hook", + run: async ({ cdpUrl, tab, pw }) => { + const uploadPathsResult = await resolveExistingPathsWithinRoot({ + rootDir: DEFAULT_UPLOAD_DIR, + requestedPaths: paths, + scopeLabel: `uploads directory (${DEFAULT_UPLOAD_DIR})`, + }); + if (!uploadPathsResult.ok) { + res.status(400).json({ error: uploadPathsResult.error }); + return; + } + const resolvedPaths = uploadPathsResult.paths; + + if (inputRef || element) { + if (ref) { + return jsonError(res, 400, "ref cannot be combined with inputRef/element"); + } + await pw.setInputFilesViaPlaywright({ + cdpUrl, + targetId: tab.targetId, + inputRef, + element, + paths: resolvedPaths, + }); + } else { + await pw.armFileUploadViaPlaywright({ + cdpUrl, + targetId: tab.targetId, + paths: resolvedPaths, + timeoutMs: timeoutMs ?? undefined, + }); + if (ref) { + await pw.clickViaPlaywright({ + cdpUrl, + targetId: tab.targetId, + ref, + }); + } + } + res.json({ ok: true }); + }, + }); + }); + + app.post("/hooks/dialog", async (req, res) => { + const body = readBody(req); + const targetId = resolveTargetIdFromBody(body); + const accept = toBoolean(body.accept); + const promptText = toStringOrEmpty(body.promptText) || undefined; + const timeoutMs = toNumber(body.timeoutMs); + if (accept === undefined) { + return jsonError(res, 400, "accept is required"); + } + + await withPlaywrightRouteContext({ + req, + res, + ctx, + targetId, + feature: "dialog hook", + run: async ({ cdpUrl, tab, pw }) => { + await pw.armDialogViaPlaywright({ + cdpUrl, + targetId: tab.targetId, + accept, + promptText, + timeoutMs: timeoutMs ?? undefined, + }); + res.json({ ok: true }); + }, + }); + }); +} diff --git a/src/browser/routes/agent.act.ts b/src/browser/routes/agent.act.ts index 42ea8444f53f..7bbd29de42ec 100644 --- a/src/browser/routes/agent.act.ts +++ b/src/browser/routes/agent.act.ts @@ -1,6 +1,7 @@ -import fs from "node:fs/promises"; import type { BrowserFormField } from "../client-actions-core.js"; import type { BrowserRouteContext } from "../server-context.js"; +import { registerBrowserAgentActDownloadRoutes } from "./agent.act.download.js"; +import { registerBrowserAgentActHookRoutes } from "./agent.act.hooks.js"; import { type ActKind, isActKind, @@ -13,43 +14,9 @@ import { withPlaywrightRouteContext, SELECTOR_UNSUPPORTED_MESSAGE, } from "./agent.shared.js"; -import { - DEFAULT_DOWNLOAD_DIR, - DEFAULT_UPLOAD_DIR, - resolveWritablePathWithinRoot, - resolveExistingPathsWithinRoot, -} from "./path-output.js"; -import type { BrowserResponse, BrowserRouteRegistrar } from "./types.js"; +import type { BrowserRouteRegistrar } from "./types.js"; import { jsonError, toBoolean, toNumber, toStringArray, toStringOrEmpty } from "./utils.js"; -async function resolveDownloadPathOrRespond( - res: BrowserResponse, - requestedPath: string, -): Promise { - const downloadPathResult = await resolveWritablePathWithinRoot({ - rootDir: DEFAULT_DOWNLOAD_DIR, - requestedPath, - scopeLabel: "downloads directory", - }); - if (!downloadPathResult.ok) { - res.status(400).json({ error: downloadPathResult.error }); - return null; - } - return downloadPathResult.path; -} - -function buildDownloadRequestBase(cdpUrl: string, targetId: string, timeoutMs: number | undefined) { - return { - cdpUrl, - targetId, - timeoutMs: timeoutMs ?? undefined, - }; -} - -function respondWithDownloadResult(res: BrowserResponse, targetId: string, result: unknown) { - res.json({ ok: true, targetId, download: result }); -} - export function registerBrowserAgentActRoutes( app: BrowserRouteRegistrar, ctx: BrowserRouteContext, @@ -367,163 +334,8 @@ export function registerBrowserAgentActRoutes( }); }); - app.post("/hooks/file-chooser", async (req, res) => { - const body = readBody(req); - const targetId = resolveTargetIdFromBody(body); - const ref = toStringOrEmpty(body.ref) || undefined; - const inputRef = toStringOrEmpty(body.inputRef) || undefined; - const element = toStringOrEmpty(body.element) || undefined; - const paths = toStringArray(body.paths) ?? []; - const timeoutMs = toNumber(body.timeoutMs); - if (!paths.length) { - return jsonError(res, 400, "paths are required"); - } - - await withPlaywrightRouteContext({ - req, - res, - ctx, - targetId, - feature: "file chooser hook", - run: async ({ cdpUrl, tab, pw }) => { - const uploadPathsResult = await resolveExistingPathsWithinRoot({ - rootDir: DEFAULT_UPLOAD_DIR, - requestedPaths: paths, - scopeLabel: `uploads directory (${DEFAULT_UPLOAD_DIR})`, - }); - if (!uploadPathsResult.ok) { - res.status(400).json({ error: uploadPathsResult.error }); - return; - } - const resolvedPaths = uploadPathsResult.paths; - - if (inputRef || element) { - if (ref) { - return jsonError(res, 400, "ref cannot be combined with inputRef/element"); - } - await pw.setInputFilesViaPlaywright({ - cdpUrl, - targetId: tab.targetId, - inputRef, - element, - paths: resolvedPaths, - }); - } else { - await pw.armFileUploadViaPlaywright({ - cdpUrl, - targetId: tab.targetId, - paths: resolvedPaths, - timeoutMs: timeoutMs ?? undefined, - }); - if (ref) { - await pw.clickViaPlaywright({ - cdpUrl, - targetId: tab.targetId, - ref, - }); - } - } - res.json({ ok: true }); - }, - }); - }); - - app.post("/hooks/dialog", async (req, res) => { - const body = readBody(req); - const targetId = resolveTargetIdFromBody(body); - const accept = toBoolean(body.accept); - const promptText = toStringOrEmpty(body.promptText) || undefined; - const timeoutMs = toNumber(body.timeoutMs); - if (accept === undefined) { - return jsonError(res, 400, "accept is required"); - } - - await withPlaywrightRouteContext({ - req, - res, - ctx, - targetId, - feature: "dialog hook", - run: async ({ cdpUrl, tab, pw }) => { - await pw.armDialogViaPlaywright({ - cdpUrl, - targetId: tab.targetId, - accept, - promptText, - timeoutMs: timeoutMs ?? undefined, - }); - res.json({ ok: true }); - }, - }); - }); - - app.post("/wait/download", async (req, res) => { - const body = readBody(req); - const targetId = resolveTargetIdFromBody(body); - const out = toStringOrEmpty(body.path) || ""; - const timeoutMs = toNumber(body.timeoutMs); - - await withPlaywrightRouteContext({ - req, - res, - ctx, - targetId, - feature: "wait for download", - run: async ({ cdpUrl, tab, pw }) => { - await fs.mkdir(DEFAULT_DOWNLOAD_DIR, { recursive: true }); - let downloadPath: string | undefined; - if (out.trim()) { - const resolvedDownloadPath = await resolveDownloadPathOrRespond(res, out); - if (!resolvedDownloadPath) { - return; - } - downloadPath = resolvedDownloadPath; - } - const requestBase = buildDownloadRequestBase(cdpUrl, tab.targetId, timeoutMs); - const result = await pw.waitForDownloadViaPlaywright({ - ...requestBase, - path: downloadPath, - }); - respondWithDownloadResult(res, tab.targetId, result); - }, - }); - }); - - app.post("/download", async (req, res) => { - const body = readBody(req); - const targetId = resolveTargetIdFromBody(body); - const ref = toStringOrEmpty(body.ref); - const out = toStringOrEmpty(body.path); - const timeoutMs = toNumber(body.timeoutMs); - if (!ref) { - return jsonError(res, 400, "ref is required"); - } - if (!out) { - return jsonError(res, 400, "path is required"); - } - - await withPlaywrightRouteContext({ - req, - res, - ctx, - targetId, - feature: "download", - run: async ({ cdpUrl, tab, pw }) => { - await fs.mkdir(DEFAULT_DOWNLOAD_DIR, { recursive: true }); - const downloadPath = await resolveDownloadPathOrRespond(res, out); - if (!downloadPath) { - return; - } - const requestBase = buildDownloadRequestBase(cdpUrl, tab.targetId, timeoutMs); - const result = await pw.downloadViaPlaywright({ - ...requestBase, - ref, - path: downloadPath, - }); - respondWithDownloadResult(res, tab.targetId, result); - }, - }); - }); + registerBrowserAgentActHookRoutes(app, ctx); + registerBrowserAgentActDownloadRoutes(app, ctx); app.post("/response/body", async (req, res) => { const body = readBody(req); diff --git a/src/browser/routes/agent.debug.ts b/src/browser/routes/agent.debug.ts index b3b6ee0946cb..f5c0d7b2030d 100644 --- a/src/browser/routes/agent.debug.ts +++ b/src/browser/routes/agent.debug.ts @@ -1,5 +1,4 @@ import crypto from "node:crypto"; -import fs from "node:fs/promises"; import path from "node:path"; import type { BrowserRouteContext } from "../server-context.js"; import { @@ -8,7 +7,8 @@ import { resolveTargetIdFromQuery, withPlaywrightRouteContext, } from "./agent.shared.js"; -import { DEFAULT_TRACE_DIR, resolveWritablePathWithinRoot } from "./path-output.js"; +import { resolveWritableOutputPathOrRespond } from "./output-paths.js"; +import { DEFAULT_TRACE_DIR } from "./path-output.js"; import type { BrowserRouteRegistrar } from "./types.js"; import { toBoolean, toStringOrEmpty } from "./utils.js"; @@ -120,19 +120,17 @@ export function registerBrowserAgentDebugRoutes( feature: "trace stop", run: async ({ cdpUrl, tab, pw }) => { const id = crypto.randomUUID(); - const dir = DEFAULT_TRACE_DIR; - await fs.mkdir(dir, { recursive: true }); - const tracePathResult = await resolveWritablePathWithinRoot({ - rootDir: dir, + const tracePath = await resolveWritableOutputPathOrRespond({ + res, + rootDir: DEFAULT_TRACE_DIR, requestedPath: out, scopeLabel: "trace directory", defaultFileName: `browser-trace-${id}.zip`, + ensureRootDir: true, }); - if (!tracePathResult.ok) { - res.status(400).json({ error: tracePathResult.error }); + if (!tracePath) { return; } - const tracePath = tracePathResult.path; await pw.traceStopViaPlaywright({ cdpUrl, targetId: tab.targetId, diff --git a/src/browser/routes/output-paths.ts b/src/browser/routes/output-paths.ts new file mode 100644 index 000000000000..4a11d3dc8163 --- /dev/null +++ b/src/browser/routes/output-paths.ts @@ -0,0 +1,31 @@ +import fs from "node:fs/promises"; +import { resolveWritablePathWithinRoot } from "./path-output.js"; +import type { BrowserResponse } from "./types.js"; + +export async function ensureOutputRootDir(rootDir: string): Promise { + await fs.mkdir(rootDir, { recursive: true }); +} + +export async function resolveWritableOutputPathOrRespond(params: { + res: BrowserResponse; + rootDir: string; + requestedPath: string; + scopeLabel: string; + defaultFileName?: string; + ensureRootDir?: boolean; +}): Promise { + if (params.ensureRootDir) { + await ensureOutputRootDir(params.rootDir); + } + const pathResult = await resolveWritablePathWithinRoot({ + rootDir: params.rootDir, + requestedPath: params.requestedPath, + scopeLabel: params.scopeLabel, + defaultFileName: params.defaultFileName, + }); + if (!pathResult.ok) { + params.res.status(400).json({ error: pathResult.error }); + return null; + } + return pathResult.path; +} diff --git a/src/infra/tmp-openclaw-dir.ts b/src/infra/tmp-openclaw-dir.ts index 2897d69e48a0..975e25b8a1a9 100644 --- a/src/infra/tmp-openclaw-dir.ts +++ b/src/infra/tmp-openclaw-dir.ts @@ -75,37 +75,17 @@ export function resolvePreferredOpenClawTmpDir( return st.isDirectory() && !st.isSymbolicLink() && isSecureDirForUser(st); }; - const resolvePreferredState = ( + const resolveDirState = ( + candidatePath: string, requireWritableAccess: boolean, ): "available" | "missing" | "invalid" => { try { - const preferred = lstatSync(POSIX_OPENCLAW_TMP_DIR); - if (!isTrustedPreferredDir(preferred)) { - return "invalid"; - } - if (requireWritableAccess) { - accessSync(POSIX_OPENCLAW_TMP_DIR, fs.constants.W_OK | fs.constants.X_OK); - } - return "available"; - } catch (err) { - if (isNodeErrorWithCode(err, "ENOENT")) { - return "missing"; - } - return "invalid"; - } - }; - - const resolveFallbackState = ( - fallbackPath: string, - requireWritableAccess: boolean, - ): "available" | "missing" | "invalid" => { - try { - const candidate = lstatSync(fallbackPath); + const candidate = lstatSync(candidatePath); if (!isTrustedPreferredDir(candidate)) { return "invalid"; } if (requireWritableAccess) { - accessSync(fallbackPath, fs.constants.W_OK | fs.constants.X_OK); + accessSync(candidatePath, fs.constants.W_OK | fs.constants.X_OK); } return "available"; } catch (err) { @@ -118,7 +98,7 @@ export function resolvePreferredOpenClawTmpDir( const ensureTrustedFallbackDir = (): string => { const fallbackPath = fallback(); - const state = resolveFallbackState(fallbackPath, true); + const state = resolveDirState(fallbackPath, true); if (state === "available") { return fallbackPath; } @@ -130,13 +110,13 @@ export function resolvePreferredOpenClawTmpDir( } catch { throw new Error(`Unable to create fallback OpenClaw temp dir: ${fallbackPath}`); } - if (resolveFallbackState(fallbackPath, true) !== "available") { + if (resolveDirState(fallbackPath, true) !== "available") { throw new Error(`Unsafe fallback OpenClaw temp dir: ${fallbackPath}`); } return fallbackPath; }; - const existingPreferredState = resolvePreferredState(true); + const existingPreferredState = resolveDirState(POSIX_OPENCLAW_TMP_DIR, true); if (existingPreferredState === "available") { return POSIX_OPENCLAW_TMP_DIR; } @@ -148,7 +128,7 @@ export function resolvePreferredOpenClawTmpDir( accessSync("/tmp", fs.constants.W_OK | fs.constants.X_OK); // Create with a safe default; subsequent callers expect it exists. mkdirSync(POSIX_OPENCLAW_TMP_DIR, { recursive: true, mode: 0o700 }); - if (resolvePreferredState(true) !== "available") { + if (resolveDirState(POSIX_OPENCLAW_TMP_DIR, true) !== "available") { return ensureTrustedFallbackDir(); } return POSIX_OPENCLAW_TMP_DIR; From c736f11a16d6bc27ea62a0fe40fffae4cb071fdb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:22:28 +0100 Subject: [PATCH 053/160] fix(gateway): harden browser websocket auth chain --- CHANGELOG.md | 1 + docs/gateway/configuration-reference.md | 3 +- src/gateway/server-ws-runtime.ts | 3 + src/gateway/server.auth.test.ts | 69 +++++++++++++++++++ src/gateway/server.impl.ts | 7 ++ src/gateway/server/ws-connection.ts | 4 ++ .../server/ws-connection/message-handler.ts | 25 +++++-- 7 files changed, 105 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 521368ffd163..0e7532dd75b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Docs: https://docs.openclaw.ai - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. +- Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. - Discord/Inbound text: preserve embed `title` + `description` fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. diff --git a/docs/gateway/configuration-reference.md b/docs/gateway/configuration-reference.md index 9d164fc4ea0a..b03a0daa4fca 100644 --- a/docs/gateway/configuration-reference.md +++ b/docs/gateway/configuration-reference.md @@ -2145,8 +2145,9 @@ See [Plugins](/tools/plugin). - `auth.allowTailscale`: when `true`, Tailscale Serve identity headers can satisfy Control UI/WebSocket auth (verified via `tailscale whois`); HTTP API endpoints still require token/password auth. This tokenless flow assumes the gateway host is trusted. Defaults to `true` when `tailscale.mode = "serve"`. - `auth.rateLimit`: optional failed-auth limiter. Applies per client IP and per auth scope (shared-secret and device-token are tracked independently). Blocked attempts return `429` + `Retry-After`. - `auth.rateLimit.exemptLoopback` defaults to `true`; set `false` when you intentionally want localhost traffic rate-limited too (for test setups or strict proxy deployments). +- Browser-origin WS auth attempts are always throttled with loopback exemption disabled (defense-in-depth against browser-based localhost brute force). - `tailscale.mode`: `serve` (tailnet only, loopback bind) or `funnel` (public, requires auth). -- `controlUi.allowedOrigins`: explicit browser-origin allowlist for Control UI/WebChat WebSocket connects. Required when Control UI is reachable on non-loopback binds. +- `controlUi.allowedOrigins`: explicit browser-origin allowlist for Gateway WebSocket connects. Required when browser clients are expected from non-loopback origins. - `controlUi.dangerouslyAllowHostHeaderOriginFallback`: dangerous mode that enables Host-header origin fallback for deployments that intentionally rely on Host-header origin policy. - `remote.transport`: `ssh` (default) or `direct` (ws/wss). For `direct`, `remote.url` must be `ws://` or `wss://`. - `gateway.remote.token` is for remote CLI calls only; does not enable local gateway auth. diff --git a/src/gateway/server-ws-runtime.ts b/src/gateway/server-ws-runtime.ts index 9c14794a58e0..f03235daddf6 100644 --- a/src/gateway/server-ws-runtime.ts +++ b/src/gateway/server-ws-runtime.ts @@ -16,6 +16,8 @@ export function attachGatewayWsHandlers(params: { resolvedAuth: ResolvedGatewayAuth; /** Optional rate limiter for auth brute-force protection. */ rateLimiter?: AuthRateLimiter; + /** Browser-origin fallback limiter (loopback is never exempt). */ + browserRateLimiter?: AuthRateLimiter; gatewayMethods: string[]; events: string[]; logGateway: ReturnType; @@ -41,6 +43,7 @@ export function attachGatewayWsHandlers(params: { canvasHostServerPort: params.canvasHostServerPort, resolvedAuth: params.resolvedAuth, rateLimiter: params.rateLimiter, + browserRateLimiter: params.browserRateLimiter, gatewayMethods: params.gatewayMethods, events: params.events, logGateway: params.logGateway, diff --git a/src/gateway/server.auth.test.ts b/src/gateway/server.auth.test.ts index 83a97644d193..38668de7f40a 100644 --- a/src/gateway/server.auth.test.ts +++ b/src/gateway/server.auth.test.ts @@ -672,6 +672,17 @@ describe("gateway server auth/connect", () => { ws.close(); }); + test("rejects non-local browser origins for non-control-ui clients", async () => { + const ws = await openWs(port, { origin: "https://attacker.example" }); + const res = await connectReq(ws, { + token: "secret", + client: TEST_OPERATOR_CLIENT, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("origin not allowed"); + ws.close(); + }); + test("returns control ui hint when token is missing", async () => { const ws = await openWs(port, { origin: originForPort(port) }); const res = await connectReq(ws, { @@ -701,6 +712,27 @@ describe("gateway server auth/connect", () => { ); ws.close(); }); + + test("rate-limits browser-origin auth failures on loopback even when loopback exemption is enabled", async () => { + testState.gatewayAuth = { + mode: "token", + token: "secret", + rateLimit: { maxAttempts: 1, windowMs: 60_000, lockoutMs: 60_000, exemptLoopback: true }, + }; + await withGatewayServer(async ({ port }) => { + const firstWs = await openWs(port, { origin: originForPort(port) }); + const first = await connectReq(firstWs, { token: "wrong" }); + expect(first.ok).toBe(false); + expect(first.error?.message ?? "").not.toContain("retry later"); + firstWs.close(); + + const secondWs = await openWs(port, { origin: originForPort(port) }); + const second = await connectReq(secondWs, { token: "wrong" }); + expect(second.ok).toBe(false); + expect(second.error?.message ?? "").toContain("retry later"); + secondWs.close(); + }); + }); }); describe("explicit none auth", () => { @@ -1214,6 +1246,43 @@ describe("gateway server auth/connect", () => { restoreGatewayToken(prevToken); }); + test("does not silently auto-pair non-control-ui browser clients on loopback", async () => { + const { listDevicePairing } = await import("../infra/device-pairing.js"); + const { randomUUID } = await import("node:crypto"); + const os = await import("node:os"); + const path = await import("node:path"); + const { server, ws, port, prevToken } = await startServerWithClient("secret"); + ws.close(); + + const browserWs = await openWs(port, { origin: originForPort(port) }); + const nonce = await readConnectChallengeNonce(browserWs); + const { identity, device } = await createSignedDevice({ + token: "secret", + scopes: ["operator.admin"], + clientId: TEST_OPERATOR_CLIENT.id, + clientMode: TEST_OPERATOR_CLIENT.mode, + identityPath: path.join(os.tmpdir(), `openclaw-browser-device-${randomUUID()}.json`), + nonce, + }); + const res = await connectReq(browserWs, { + token: "secret", + scopes: ["operator.admin"], + client: TEST_OPERATOR_CLIENT, + device, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("pairing required"); + + const pairing = await listDevicePairing(); + const pending = pairing.pending.find((entry) => entry.deviceId === identity.deviceId); + expect(pending).toBeTruthy(); + expect(pending?.silent).toBe(false); + + browserWs.close(); + await server.close(); + restoreGatewayToken(prevToken); + }); + test("merges remote node/operator pairing requests for the same unpaired device", async () => { const { mkdtemp } = await import("node:fs/promises"); const { tmpdir } = await import("node:os"); diff --git a/src/gateway/server.impl.ts b/src/gateway/server.impl.ts index fdca08c2677e..8b368539469a 100644 --- a/src/gateway/server.impl.ts +++ b/src/gateway/server.impl.ts @@ -316,6 +316,11 @@ export async function startGatewayServer( const authRateLimiter: AuthRateLimiter | undefined = rateLimitConfig ? createAuthRateLimiter(rateLimitConfig) : undefined; + // Always keep a browser-origin fallback limiter for WS auth attempts. + const browserAuthRateLimiter: AuthRateLimiter = createAuthRateLimiter({ + ...rateLimitConfig, + exemptLoopback: false, + }); let controlUiRootState: ControlUiRootState | undefined; if (controlUiRootOverride) { @@ -574,6 +579,7 @@ export async function startGatewayServer( canvasHostServerPort, resolvedAuth, rateLimiter: authRateLimiter, + browserRateLimiter: browserAuthRateLimiter, gatewayMethods, events: GATEWAY_EVENTS, logGateway: log, @@ -777,6 +783,7 @@ export async function startGatewayServer( } skillsChangeUnsub(); authRateLimiter?.dispose(); + browserAuthRateLimiter.dispose(); channelHealthMonitor?.stop(); await close(opts); }, diff --git a/src/gateway/server/ws-connection.ts b/src/gateway/server/ws-connection.ts index e7c9d458f8f3..3abc8d6e1b94 100644 --- a/src/gateway/server/ws-connection.ts +++ b/src/gateway/server/ws-connection.ts @@ -65,6 +65,8 @@ export function attachGatewayWsConnectionHandler(params: { resolvedAuth: ResolvedGatewayAuth; /** Optional rate limiter for auth brute-force protection. */ rateLimiter?: AuthRateLimiter; + /** Browser-origin fallback limiter (loopback is never exempt). */ + browserRateLimiter?: AuthRateLimiter; gatewayMethods: string[]; events: string[]; logGateway: SubsystemLogger; @@ -90,6 +92,7 @@ export function attachGatewayWsConnectionHandler(params: { canvasHostServerPort, resolvedAuth, rateLimiter, + browserRateLimiter, gatewayMethods, events, logGateway, @@ -278,6 +281,7 @@ export function attachGatewayWsConnectionHandler(params: { connectNonce, resolvedAuth, rateLimiter, + browserRateLimiter, gatewayMethods, events, extraHandlers, diff --git a/src/gateway/server/ws-connection/message-handler.ts b/src/gateway/server/ws-connection/message-handler.ts index 9708325009f6..0d694d125294 100644 --- a/src/gateway/server/ws-connection/message-handler.ts +++ b/src/gateway/server/ws-connection/message-handler.ts @@ -99,6 +99,8 @@ export function attachGatewayWsMessageHandler(params: { resolvedAuth: ResolvedGatewayAuth; /** Optional rate limiter for auth brute-force protection. */ rateLimiter?: AuthRateLimiter; + /** Browser-origin fallback limiter (loopback is never exempt). */ + browserRateLimiter?: AuthRateLimiter; gatewayMethods: string[]; events: string[]; extraHandlers: GatewayRequestHandlers; @@ -130,6 +132,7 @@ export function attachGatewayWsMessageHandler(params: { connectNonce, resolvedAuth, rateLimiter, + browserRateLimiter, gatewayMethods, events, extraHandlers, @@ -192,6 +195,12 @@ export function attachGatewayWsMessageHandler(params: { const isWebchatConnect = (p: ConnectParams | null | undefined) => isWebchatClient(p?.client); const unauthorizedFloodGuard = new UnauthorizedFloodGuard(); + const hasBrowserOriginHeader = Boolean(requestOrigin && requestOrigin.trim() !== ""); + const enforceBrowserOriginForAnyClient = hasBrowserOriginHeader && !hasProxyHeaders; + const browserRateLimitClientIp = + hasBrowserOriginHeader && isLoopbackAddress(clientIp) ? "198.18.0.1" : clientIp; + const authRateLimiter = + hasBrowserOriginHeader && browserRateLimiter ? browserRateLimiter : rateLimiter; socket.on("message", async (data) => { if (isClosed()) { @@ -329,7 +338,7 @@ export function attachGatewayWsMessageHandler(params: { const isControlUi = connectParams.client.id === GATEWAY_CLIENT_IDS.CONTROL_UI; const isWebchat = isWebchatConnect(connectParams); - if (isControlUi || isWebchat) { + if (enforceBrowserOriginForAnyClient || isControlUi || isWebchat) { const originCheck = checkBrowserOrigin({ requestHost, origin: requestOrigin, @@ -377,8 +386,8 @@ export function attachGatewayWsMessageHandler(params: { req: upgradeReq, trustedProxies, allowRealIpFallback, - rateLimiter, - clientIp, + rateLimiter: authRateLimiter, + clientIp: browserRateLimitClientIp, }); const rejectUnauthorized = (failedAuth: GatewayAuthResult) => { markHandshakeFailure("unauthorized", { @@ -556,8 +565,8 @@ export function attachGatewayWsMessageHandler(params: { deviceId: device?.id, role, scopes, - rateLimiter, - clientIp, + rateLimiter: authRateLimiter, + clientIp: browserRateLimitClientIp, verifyDeviceToken, })); if (!authOk) { @@ -613,11 +622,15 @@ export function attachGatewayWsMessageHandler(params: { const requirePairing = async ( reason: "not-paired" | "role-upgrade" | "scope-upgrade", ) => { + const allowSilentLocalPairing = + isLocalClient && + (!hasBrowserOriginHeader || isControlUi || isWebchat) && + (reason === "not-paired" || reason === "scope-upgrade"); const pairing = await requestDevicePairing({ deviceId: device.id, publicKey: devicePublicKey, ...clientAccessMetadata, - silent: isLocalClient && (reason === "not-paired" || reason === "scope-upgrade"), + silent: allowSilentLocalPairing, }); const context = buildRequestContext(); if (pairing.request.silent === true) { From aedf62ac7e669a89c7b299201bf6537dc6b12e0e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:26:37 +0100 Subject: [PATCH 054/160] fix: harden discord and slack reaction ingress authorization --- CHANGELOG.md | 1 + src/discord/monitor.test.ts | 98 ++++++++++++- src/discord/monitor/listeners.ts | 115 ++++++++++++++- src/discord/monitor/provider.ts | 12 ++ src/security/dm-policy-shared.test.ts | 32 ++++ src/security/dm-policy-shared.ts | 35 +++++ src/slack/monitor/events/reactions.test.ts | 163 +++++++++++++++++++++ src/slack/monitor/events/reactions.ts | 32 +++- 8 files changed, 483 insertions(+), 5 deletions(-) create mode 100644 src/slack/monitor/events/reactions.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e7532dd75b0..6db4c8796dec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Docs: https://docs.openclaw.ai - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. diff --git a/src/discord/monitor.test.ts b/src/discord/monitor.test.ts index 222911894a90..4e185d965740 100644 --- a/src/discord/monitor.test.ts +++ b/src/discord/monitor.test.ts @@ -1,5 +1,5 @@ import { ChannelType, type Guild } from "@buape/carbon"; -import { describe, expect, it, vi } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import { typedCases } from "../test-utils/typed-cases.js"; import { allowListMatches, @@ -20,6 +20,12 @@ import { } from "./monitor.js"; import { DiscordMessageListener, DiscordReactionListener } from "./monitor/listeners.js"; +const readAllowFromStoreMock = vi.hoisted(() => vi.fn()); + +vi.mock("../pairing/pairing-store.js", () => ({ + readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args), +})); + const fakeGuild = (id: string, name: string) => ({ id, name }) as Guild; const makeEntries = ( @@ -899,6 +905,12 @@ function makeReactionClient(options?: { function makeReactionListenerParams(overrides?: { botUserId?: string; + dmEnabled?: boolean; + groupDmEnabled?: boolean; + groupDmChannels?: string[]; + dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom?: string[]; + groupPolicy?: "open" | "allowlist" | "disabled"; allowNameMatching?: boolean; guildEntries?: Record; }) { @@ -907,6 +919,12 @@ function makeReactionListenerParams(overrides?: { accountId: "acc-1", runtime: {} as import("../runtime.js").RuntimeEnv, botUserId: overrides?.botUserId ?? "bot-1", + dmEnabled: overrides?.dmEnabled ?? true, + groupDmEnabled: overrides?.groupDmEnabled ?? true, + groupDmChannels: overrides?.groupDmChannels ?? [], + dmPolicy: overrides?.dmPolicy ?? "open", + allowFrom: overrides?.allowFrom ?? [], + groupPolicy: overrides?.groupPolicy ?? "open", allowNameMatching: overrides?.allowNameMatching ?? false, guildEntries: overrides?.guildEntries, logger: { @@ -919,6 +937,12 @@ function makeReactionListenerParams(overrides?: { } describe("discord DM reaction handling", () => { + beforeEach(() => { + enqueueSystemEventSpy.mockClear(); + resolveAgentRouteMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + }); + it("processes DM reactions with or without guild allowlists", async () => { const cases = [ { name: "no guild allowlist", guildEntries: undefined }, @@ -952,9 +976,77 @@ describe("discord DM reaction handling", () => { } }); + it("blocks DM reactions when dmPolicy is disabled", async () => { + const data = makeReactionEvent({ botAsAuthor: true }); + const client = makeReactionClient({ channelType: ChannelType.DM }); + const listener = new DiscordReactionListener( + makeReactionListenerParams({ dmPolicy: "disabled" }), + ); + + await listener.handle(data, client); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + + it("blocks DM reactions for unauthorized sender in allowlist mode", async () => { + const data = makeReactionEvent({ botAsAuthor: true, userId: "user-1" }); + const client = makeReactionClient({ channelType: ChannelType.DM }); + const listener = new DiscordReactionListener( + makeReactionListenerParams({ + dmPolicy: "allowlist", + allowFrom: ["user:user-2"], + }), + ); + + await listener.handle(data, client); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + + it("allows DM reactions for authorized sender in allowlist mode", async () => { + const data = makeReactionEvent({ botAsAuthor: true, userId: "user-1" }); + const client = makeReactionClient({ channelType: ChannelType.DM }); + const listener = new DiscordReactionListener( + makeReactionListenerParams({ + dmPolicy: "allowlist", + allowFrom: ["user:user-1"], + }), + ); + + await listener.handle(data, client); + + expect(enqueueSystemEventSpy).toHaveBeenCalledOnce(); + }); + + it("blocks group DM reactions when group DMs are disabled", async () => { + const data = makeReactionEvent({ botAsAuthor: true }); + const client = makeReactionClient({ channelType: ChannelType.GroupDM }); + const listener = new DiscordReactionListener( + makeReactionListenerParams({ groupDmEnabled: false }), + ); + + await listener.handle(data, client); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + + it("blocks guild reactions when groupPolicy is disabled", async () => { + const data = makeReactionEvent({ + guildId: "guild-123", + botAsAuthor: true, + guild: { id: "guild-123", name: "Guild" }, + }); + const client = makeReactionClient({ channelType: ChannelType.GuildText }); + const listener = new DiscordReactionListener( + makeReactionListenerParams({ groupPolicy: "disabled" }), + ); + + await listener.handle(data, client); + + expect(enqueueSystemEventSpy).not.toHaveBeenCalled(); + }); + it("still processes guild reactions (no regression)", async () => { - enqueueSystemEventSpy.mockClear(); - resolveAgentRouteMock.mockClear(); resolveAgentRouteMock.mockReturnValueOnce({ agentId: "default", channel: "discord", diff --git a/src/discord/monitor/listeners.ts b/src/discord/monitor/listeners.ts index 9bdc73312245..002bf62816d4 100644 --- a/src/discord/monitor/listeners.ts +++ b/src/discord/monitor/listeners.ts @@ -7,14 +7,20 @@ import { PresenceUpdateListener, type User, } from "@buape/carbon"; -import { danger } from "../../globals.js"; +import { danger, logVerbose } from "../../globals.js"; import { formatDurationSeconds } from "../../infra/format-time/format-duration.ts"; import { enqueueSystemEvent } from "../../infra/system-events.js"; import { createSubsystemLogger } from "../../logging/subsystem.js"; +import { readChannelAllowFromStore } from "../../pairing/pairing-store.js"; import { resolveAgentRoute } from "../../routing/resolve-route.js"; +import { resolveDmGroupAccessWithLists } from "../../security/dm-policy-shared.js"; import { + isDiscordGroupAllowedByPolicy, + normalizeDiscordAllowList, normalizeDiscordSlug, + resolveDiscordAllowListMatch, resolveDiscordChannelConfigWithFallback, + resolveGroupDmAllow, resolveDiscordGuildEntry, shouldEmitDiscordReactionNotification, } from "./allow-list.js"; @@ -37,6 +43,12 @@ type DiscordReactionListenerParams = { accountId: string; runtime: RuntimeEnv; botUserId?: string; + dmEnabled: boolean; + groupDmEnabled: boolean; + groupDmChannels: string[]; + dmPolicy: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom: string[]; + groupPolicy: "open" | "allowlist" | "disabled"; allowNameMatching: boolean; guildEntries?: Record; logger: Logger; @@ -179,6 +191,12 @@ async function runDiscordReactionHandler(params: { cfg: params.handlerParams.cfg, accountId: params.handlerParams.accountId, botUserId: params.handlerParams.botUserId, + dmEnabled: params.handlerParams.dmEnabled, + groupDmEnabled: params.handlerParams.groupDmEnabled, + groupDmChannels: params.handlerParams.groupDmChannels, + dmPolicy: params.handlerParams.dmPolicy, + allowFrom: params.handlerParams.allowFrom, + groupPolicy: params.handlerParams.groupPolicy, allowNameMatching: params.handlerParams.allowNameMatching, guildEntries: params.handlerParams.guildEntries, logger: params.handlerParams.logger, @@ -193,6 +211,12 @@ async function handleDiscordReactionEvent(params: { cfg: LoadedConfig; accountId: string; botUserId?: string; + dmEnabled: boolean; + groupDmEnabled: boolean; + groupDmChannels: string[]; + dmPolicy: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom: string[]; + groupPolicy: "open" | "allowlist" | "disabled"; allowNameMatching: boolean; guildEntries?: Record; logger: Logger; @@ -236,6 +260,12 @@ async function handleDiscordReactionEvent(params: { channelType === ChannelType.PublicThread || channelType === ChannelType.PrivateThread || channelType === ChannelType.AnnouncementThread; + if (isDirectMessage && !params.dmEnabled) { + return; + } + if (isGroupDm && !params.groupDmEnabled) { + return; + } let parentId = "parentId" in channel ? (channel.parentId ?? undefined) : undefined; let parentName: string | undefined; let parentSlug = ""; @@ -264,6 +294,45 @@ async function handleDiscordReactionEvent(params: { reactionBase = { baseText, contextKey }; return reactionBase; }; + const isDirectReactionAuthorized = async () => { + if (!isDirectMessage) { + return true; + } + const storeAllowFrom = + params.dmPolicy === "allowlist" + ? [] + : await readChannelAllowFromStore("discord").catch(() => []); + const access = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: params.dmPolicy, + groupPolicy: params.groupPolicy, + allowFrom: params.allowFrom, + groupAllowFrom: [], + storeAllowFrom, + isSenderAllowed: (allowEntries) => { + const allowList = normalizeDiscordAllowList(allowEntries, ["discord:", "user:", "pk:"]); + const allowMatch = allowList + ? resolveDiscordAllowListMatch({ + allowList, + candidate: { + id: user.id, + name: user.username, + tag: formatDiscordUserTag(user), + }, + allowNameMatching: params.allowNameMatching, + }) + : { allowed: false }; + return allowMatch.allowed; + }, + }); + if (access.decision !== "allow") { + logVerbose( + `discord reaction blocked sender=${user.id} (dmPolicy=${params.dmPolicy}, decision=${access.decision}, reason=${access.reason})`, + ); + return false; + } + return true; + }; const emitReaction = (text: string, parentPeerId?: string) => { const { contextKey } = resolveReactionBase(); const route = resolveAgentRoute({ @@ -322,6 +391,44 @@ async function handleDiscordReactionEvent(params: { parentSlug, scope: "thread", }); + const isGuildReactionAllowed = (channelConfig: { allowed?: boolean } | null) => { + if (!isGuildMessage) { + return true; + } + const channelAllowlistConfigured = + Boolean(guildInfo?.channels) && Object.keys(guildInfo?.channels ?? {}).length > 0; + const channelAllowed = channelConfig?.allowed !== false; + if ( + !isDiscordGroupAllowedByPolicy({ + groupPolicy: params.groupPolicy, + guildAllowlisted: Boolean(guildInfo), + channelAllowlistConfigured, + channelAllowed, + }) + ) { + return false; + } + if (channelConfig?.allowed === false) { + return false; + } + return true; + }; + + if (!(await isDirectReactionAuthorized())) { + return; + } + + if ( + isGroupDm && + !resolveGroupDmAllow({ + channels: params.groupDmChannels, + channelId: data.channel_id, + channelName, + channelSlug, + }) + ) { + return; + } // Parallelize async operations for thread channels if (isThreadChannel) { @@ -370,6 +477,9 @@ async function handleDiscordReactionEvent(params: { if (channelConfig?.allowed === false) { return; } + if (!isGuildReactionAllowed(channelConfig)) { + return; + } const messageAuthorId = message?.author?.id ?? undefined; if (!shouldNotifyReaction({ mode: reactionMode, messageAuthorId })) { @@ -394,6 +504,9 @@ async function handleDiscordReactionEvent(params: { if (channelConfig?.allowed === false) { return; } + if (!isGuildReactionAllowed(channelConfig)) { + return; + } const reactionMode = guildInfo?.reactionNotifications ?? "own"; diff --git a/src/discord/monitor/provider.ts b/src/discord/monitor/provider.ts index 15c8e2aa7b4a..629f8a3e7aab 100644 --- a/src/discord/monitor/provider.ts +++ b/src/discord/monitor/provider.ts @@ -561,6 +561,12 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { accountId: account.accountId, runtime, botUserId, + dmEnabled, + groupDmEnabled, + groupDmChannels: groupDmChannels ?? [], + dmPolicy, + allowFrom: allowFrom ?? [], + groupPolicy, allowNameMatching: isDangerousNameMatchingEnabled(discordCfg), guildEntries, logger, @@ -573,6 +579,12 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { accountId: account.accountId, runtime, botUserId, + dmEnabled, + groupDmEnabled, + groupDmChannels: groupDmChannels ?? [], + dmPolicy, + allowFrom: allowFrom ?? [], + groupPolicy, allowNameMatching: isDangerousNameMatchingEnabled(discordCfg), guildEntries, logger, diff --git a/src/security/dm-policy-shared.test.ts b/src/security/dm-policy-shared.test.ts index d65d6a79188d..1fe36976a551 100644 --- a/src/security/dm-policy-shared.test.ts +++ b/src/security/dm-policy-shared.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest"; import { resolveDmAllowState, resolveDmGroupAccessDecision, + resolveDmGroupAccessWithLists, resolveEffectiveAllowFromLists, } from "./dm-policy-shared.js"; @@ -75,6 +76,37 @@ describe("security/dm-policy-shared", () => { expect(lists.effectiveGroupAllowFrom).toEqual(["+1111", "+2222"]); }); + it("resolves access + effective allowlists in one shared call", () => { + const resolved = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: "pairing", + groupPolicy: "allowlist", + allowFrom: ["owner"], + groupAllowFrom: ["group:room"], + storeAllowFrom: ["paired-user"], + isSenderAllowed: (allowFrom) => allowFrom.includes("paired-user"), + }); + expect(resolved.decision).toBe("allow"); + expect(resolved.reason).toBe("dmPolicy=pairing (allowlisted)"); + expect(resolved.effectiveAllowFrom).toEqual(["owner", "paired-user"]); + expect(resolved.effectiveGroupAllowFrom).toEqual(["group:room", "paired-user"]); + }); + + it("keeps allowlist mode strict in shared resolver (no pairing-store fallback)", () => { + const resolved = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: "allowlist", + groupPolicy: "allowlist", + allowFrom: ["owner"], + groupAllowFrom: [], + storeAllowFrom: ["paired-user"], + isSenderAllowed: () => false, + }); + expect(resolved.decision).toBe("block"); + expect(resolved.reason).toBe("dmPolicy=allowlist (not allowlisted)"); + expect(resolved.effectiveAllowFrom).toEqual(["owner"]); + }); + const channels = [ "bluebubbles", "imessage", diff --git a/src/security/dm-policy-shared.ts b/src/security/dm-policy-shared.ts index ee07dfff3c7c..a1084ace9ff2 100644 --- a/src/security/dm-policy-shared.ts +++ b/src/security/dm-policy-shared.ts @@ -77,6 +77,41 @@ export function resolveDmGroupAccessDecision(params: { return { decision: "block", reason: `dmPolicy=${dmPolicy} (not allowlisted)` }; } +export function resolveDmGroupAccessWithLists(params: { + isGroup: boolean; + dmPolicy?: string | null; + groupPolicy?: string | null; + allowFrom?: Array | null; + groupAllowFrom?: Array | null; + storeAllowFrom?: Array | null; + isSenderAllowed: (allowFrom: string[]) => boolean; +}): { + decision: DmGroupAccessDecision; + reason: string; + effectiveAllowFrom: string[]; + effectiveGroupAllowFrom: string[]; +} { + const { effectiveAllowFrom, effectiveGroupAllowFrom } = resolveEffectiveAllowFromLists({ + allowFrom: params.allowFrom, + groupAllowFrom: params.groupAllowFrom, + storeAllowFrom: params.storeAllowFrom, + dmPolicy: params.dmPolicy, + }); + const access = resolveDmGroupAccessDecision({ + isGroup: params.isGroup, + dmPolicy: params.dmPolicy, + groupPolicy: params.groupPolicy, + effectiveAllowFrom, + effectiveGroupAllowFrom, + isSenderAllowed: params.isSenderAllowed, + }); + return { + ...access, + effectiveAllowFrom, + effectiveGroupAllowFrom, + }; +} + export async function resolveDmAllowState(params: { provider: ChannelId; allowFrom?: Array | null; diff --git a/src/slack/monitor/events/reactions.test.ts b/src/slack/monitor/events/reactions.test.ts new file mode 100644 index 000000000000..815ca1c65b2c --- /dev/null +++ b/src/slack/monitor/events/reactions.test.ts @@ -0,0 +1,163 @@ +import { describe, expect, it, vi } from "vitest"; +import type { SlackMonitorContext } from "../context.js"; +import { registerSlackReactionEvents } from "./reactions.js"; + +const enqueueSystemEventMock = vi.fn(); +const readAllowFromStoreMock = vi.fn(); + +vi.mock("../../../infra/system-events.js", () => ({ + enqueueSystemEvent: (...args: unknown[]) => enqueueSystemEventMock(...args), +})); + +vi.mock("../../../pairing/pairing-store.js", () => ({ + readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args), +})); + +type SlackReactionHandler = (args: { + event: Record; + body: unknown; +}) => Promise; + +function createReactionContext(overrides?: { + dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom?: string[]; + channelType?: "im" | "channel"; +}) { + let addedHandler: SlackReactionHandler | null = null; + let removedHandler: SlackReactionHandler | null = null; + const channelType = overrides?.channelType ?? "im"; + const app = { + event: vi.fn((name: string, handler: SlackReactionHandler) => { + if (name === "reaction_added") { + addedHandler = handler; + } else if (name === "reaction_removed") { + removedHandler = handler; + } + }), + }; + const ctx = { + app, + runtime: { error: vi.fn() }, + dmPolicy: overrides?.dmPolicy ?? "open", + groupPolicy: "open", + allowFrom: overrides?.allowFrom ?? [], + allowNameMatching: false, + shouldDropMismatchedSlackEvent: vi.fn().mockReturnValue(false), + isChannelAllowed: vi.fn().mockReturnValue(true), + resolveChannelName: vi.fn().mockResolvedValue({ + name: channelType === "im" ? "direct" : "general", + type: channelType, + }), + resolveUserName: vi.fn().mockResolvedValue({ name: "alice" }), + resolveSlackSystemEventSessionKey: vi.fn().mockReturnValue("agent:main:main"), + } as unknown as SlackMonitorContext; + registerSlackReactionEvents({ ctx }); + return { + ctx, + getAddedHandler: () => addedHandler, + getRemovedHandler: () => removedHandler, + }; +} + +function makeReactionEvent(overrides?: { user?: string; channel?: string }) { + return { + type: "reaction_added", + user: overrides?.user ?? "U1", + reaction: "thumbsup", + item: { + type: "message", + channel: overrides?.channel ?? "D1", + ts: "123.456", + }, + item_user: "UBOT", + }; +} + +describe("registerSlackReactionEvents", () => { + it("enqueues DM reaction system events when dmPolicy is open", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createReactionContext({ dmPolicy: "open" }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makeReactionEvent(), + body: {}, + }); + + expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); + }); + + it("blocks DM reaction system events when dmPolicy is disabled", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createReactionContext({ dmPolicy: "disabled" }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makeReactionEvent(), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + + it("blocks DM reaction system events for unauthorized senders in allowlist mode", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createReactionContext({ + dmPolicy: "allowlist", + allowFrom: ["U2"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makeReactionEvent({ user: "U1" }), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + + it("allows DM reaction system events for authorized senders in allowlist mode", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createReactionContext({ + dmPolicy: "allowlist", + allowFrom: ["U1"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makeReactionEvent({ user: "U1" }), + body: {}, + }); + + expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); + }); + + it("enqueues channel reaction events regardless of dmPolicy", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getRemovedHandler } = createReactionContext({ + dmPolicy: "disabled", + channelType: "channel", + }); + const removedHandler = getRemovedHandler(); + expect(removedHandler).toBeTruthy(); + + await removedHandler!({ + event: { + ...makeReactionEvent({ channel: "C1" }), + type: "reaction_removed", + }, + body: {}, + }); + + expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/slack/monitor/events/reactions.ts b/src/slack/monitor/events/reactions.ts index b437352d6cad..5007c6aad939 100644 --- a/src/slack/monitor/events/reactions.ts +++ b/src/slack/monitor/events/reactions.ts @@ -1,6 +1,9 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt"; -import { danger } from "../../../globals.js"; +import { danger, logVerbose } from "../../../globals.js"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; +import { resolveDmGroupAccessWithLists } from "../../../security/dm-policy-shared.js"; +import { resolveSlackAllowListMatch } from "../allow-list.js"; +import { resolveSlackEffectiveAllowFrom } from "../auth.js"; import { resolveSlackChannelLabel } from "../channel-config.js"; import type { SlackMonitorContext } from "../context.js"; import type { SlackReactionEvent } from "../types.js"; @@ -32,6 +35,33 @@ export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext } channelName: channelInfo?.name, }); const actorInfo = event.user ? await ctx.resolveUserName(event.user) : undefined; + if (channelType === "im") { + if (!event.user) { + return; + } + const { allowFromLower } = await resolveSlackEffectiveAllowFrom(ctx); + const access = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: ctx.dmPolicy, + groupPolicy: ctx.groupPolicy, + allowFrom: allowFromLower, + groupAllowFrom: [], + storeAllowFrom: [], + isSenderAllowed: (allowList) => + resolveSlackAllowListMatch({ + allowList, + id: event.user, + name: actorInfo?.name, + allowNameMatching: ctx.allowNameMatching, + }).allowed, + }); + if (access.decision !== "allow") { + logVerbose( + `slack: drop reaction sender ${event.user} (dmPolicy=${ctx.dmPolicy}, decision=${access.decision}, reason=${access.reason})`, + ); + return; + } + } const actorLabel = actorInfo?.name ?? event.user; const emojiLabel = event.reaction ?? "emoji"; const authorInfo = event.item_user ? await ctx.resolveUserName(event.item_user) : undefined; From 4258a3307f5a616c26c5bd63e108d918b71a9217 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:30:19 +0000 Subject: [PATCH 055/160] refactor(agents): unify subagent announce delivery pipeline Co-authored-by: Smith Labs Co-authored-by: Do Cao Hieu --- CHANGELOG.md | 1 + src/agents/subagent-announce-dispatch.test.ts | 156 ++++++++++++++++++ src/agents/subagent-announce-dispatch.ts | 104 ++++++++++++ src/agents/subagent-announce.ts | 113 ++++--------- ...agent-registry.announce-loop-guard.test.ts | 39 +++++ src/agents/subagent-registry.ts | 13 +- src/gateway/server-methods/send.ts | 7 +- src/infra/outbound/channel-resolution.ts | 73 ++++++++ src/infra/outbound/message.test.ts | 51 +++++- src/infra/outbound/message.ts | 26 +-- .../targets.channel-resolution.test.ts | 61 +++++++ src/infra/outbound/targets.ts | 27 ++- src/telegram/send.test.ts | 38 +++++ src/telegram/send.ts | 46 +++--- 14 files changed, 623 insertions(+), 132 deletions(-) create mode 100644 src/agents/subagent-announce-dispatch.test.ts create mode 100644 src/agents/subagent-announce-dispatch.ts create mode 100644 src/infra/outbound/channel-resolution.ts create mode 100644 src/infra/outbound/targets.channel-resolution.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6db4c8796dec..6ee1e3d0375f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. diff --git a/src/agents/subagent-announce-dispatch.test.ts b/src/agents/subagent-announce-dispatch.test.ts new file mode 100644 index 000000000000..fcc2f992e2b7 --- /dev/null +++ b/src/agents/subagent-announce-dispatch.test.ts @@ -0,0 +1,156 @@ +import { describe, expect, it, vi } from "vitest"; +import { + mapQueueOutcomeToDeliveryResult, + runSubagentAnnounceDispatch, +} from "./subagent-announce-dispatch.js"; + +describe("mapQueueOutcomeToDeliveryResult", () => { + it("maps steered to delivered", () => { + expect(mapQueueOutcomeToDeliveryResult("steered")).toEqual({ + delivered: true, + path: "steered", + }); + }); + + it("maps queued to delivered", () => { + expect(mapQueueOutcomeToDeliveryResult("queued")).toEqual({ + delivered: true, + path: "queued", + }); + }); + + it("maps none to not-delivered", () => { + expect(mapQueueOutcomeToDeliveryResult("none")).toEqual({ + delivered: false, + path: "none", + }); + }); +}); + +describe("runSubagentAnnounceDispatch", () => { + it("uses queue-first ordering for non-completion mode", async () => { + const queue = vi.fn(async () => "none" as const); + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: false, + queue, + direct, + }); + + expect(queue).toHaveBeenCalledTimes(1); + expect(direct).toHaveBeenCalledTimes(1); + expect(result.delivered).toBe(true); + expect(result.path).toBe("direct"); + expect(result.phases).toEqual([ + { phase: "queue-primary", delivered: false, path: "none", error: undefined }, + { phase: "direct-primary", delivered: true, path: "direct", error: undefined }, + ]); + }); + + it("short-circuits direct send when non-completion queue delivers", async () => { + const queue = vi.fn(async () => "queued" as const); + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: false, + queue, + direct, + }); + + expect(queue).toHaveBeenCalledTimes(1); + expect(direct).not.toHaveBeenCalled(); + expect(result.path).toBe("queued"); + expect(result.phases).toEqual([ + { phase: "queue-primary", delivered: true, path: "queued", error: undefined }, + ]); + }); + + it("uses direct-first ordering for completion mode", async () => { + const queue = vi.fn(async () => "queued" as const); + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: true, + queue, + direct, + }); + + expect(direct).toHaveBeenCalledTimes(1); + expect(queue).not.toHaveBeenCalled(); + expect(result.path).toBe("direct"); + expect(result.phases).toEqual([ + { phase: "direct-primary", delivered: true, path: "direct", error: undefined }, + ]); + }); + + it("falls back to queue when completion direct send fails", async () => { + const queue = vi.fn(async () => "steered" as const); + const direct = vi.fn(async () => ({ + delivered: false, + path: "direct" as const, + error: "network", + })); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: true, + queue, + direct, + }); + + expect(direct).toHaveBeenCalledTimes(1); + expect(queue).toHaveBeenCalledTimes(1); + expect(result.path).toBe("steered"); + expect(result.phases).toEqual([ + { phase: "direct-primary", delivered: false, path: "direct", error: "network" }, + { phase: "queue-fallback", delivered: true, path: "steered", error: undefined }, + ]); + }); + + it("returns direct failure when completion fallback queue cannot deliver", async () => { + const queue = vi.fn(async () => "none" as const); + const direct = vi.fn(async () => ({ + delivered: false, + path: "direct" as const, + error: "failed", + })); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: true, + queue, + direct, + }); + + expect(result).toMatchObject({ + delivered: false, + path: "direct", + error: "failed", + }); + expect(result.phases).toEqual([ + { phase: "direct-primary", delivered: false, path: "direct", error: "failed" }, + { phase: "queue-fallback", delivered: false, path: "none", error: undefined }, + ]); + }); + + it("returns none immediately when signal is already aborted", async () => { + const queue = vi.fn(async () => "none" as const); + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); + const controller = new AbortController(); + controller.abort(); + + const result = await runSubagentAnnounceDispatch({ + expectsCompletionMessage: true, + signal: controller.signal, + queue, + direct, + }); + + expect(queue).not.toHaveBeenCalled(); + expect(direct).not.toHaveBeenCalled(); + expect(result).toEqual({ + delivered: false, + path: "none", + phases: [], + }); + }); +}); diff --git a/src/agents/subagent-announce-dispatch.ts b/src/agents/subagent-announce-dispatch.ts new file mode 100644 index 000000000000..93aa0dd90925 --- /dev/null +++ b/src/agents/subagent-announce-dispatch.ts @@ -0,0 +1,104 @@ +export type SubagentDeliveryPath = "queued" | "steered" | "direct" | "none"; + +export type SubagentAnnounceQueueOutcome = "steered" | "queued" | "none"; + +export type SubagentAnnounceDeliveryResult = { + delivered: boolean; + path: SubagentDeliveryPath; + error?: string; + phases?: SubagentAnnounceDispatchPhaseResult[]; +}; + +export type SubagentAnnounceDispatchPhase = "queue-primary" | "direct-primary" | "queue-fallback"; + +export type SubagentAnnounceDispatchPhaseResult = { + phase: SubagentAnnounceDispatchPhase; + delivered: boolean; + path: SubagentDeliveryPath; + error?: string; +}; + +export function mapQueueOutcomeToDeliveryResult( + outcome: SubagentAnnounceQueueOutcome, +): SubagentAnnounceDeliveryResult { + if (outcome === "steered") { + return { + delivered: true, + path: "steered", + }; + } + if (outcome === "queued") { + return { + delivered: true, + path: "queued", + }; + } + return { + delivered: false, + path: "none", + }; +} + +export async function runSubagentAnnounceDispatch(params: { + expectsCompletionMessage: boolean; + signal?: AbortSignal; + queue: () => Promise; + direct: () => Promise; +}): Promise { + const phases: SubagentAnnounceDispatchPhaseResult[] = []; + const appendPhase = ( + phase: SubagentAnnounceDispatchPhase, + result: SubagentAnnounceDeliveryResult, + ) => { + phases.push({ + phase, + delivered: result.delivered, + path: result.path, + error: result.error, + }); + }; + const withPhases = (result: SubagentAnnounceDeliveryResult): SubagentAnnounceDeliveryResult => ({ + ...result, + phases, + }); + + if (params.signal?.aborted) { + return withPhases({ + delivered: false, + path: "none", + }); + } + + if (!params.expectsCompletionMessage) { + const primaryQueue = mapQueueOutcomeToDeliveryResult(await params.queue()); + appendPhase("queue-primary", primaryQueue); + if (primaryQueue.delivered) { + return withPhases(primaryQueue); + } + + const primaryDirect = await params.direct(); + appendPhase("direct-primary", primaryDirect); + return withPhases(primaryDirect); + } + + const primaryDirect = await params.direct(); + appendPhase("direct-primary", primaryDirect); + if (primaryDirect.delivered) { + return withPhases(primaryDirect); + } + + if (params.signal?.aborted) { + return withPhases({ + delivered: false, + path: "none", + }); + } + + const fallbackQueue = mapQueueOutcomeToDeliveryResult(await params.queue()); + appendPhase("queue-fallback", fallbackQueue); + if (fallbackQueue.delivered) { + return withPhases(fallbackQueue); + } + + return withPhases(primaryDirect); +} diff --git a/src/agents/subagent-announce.ts b/src/agents/subagent-announce.ts index 7d7fd7ceb48b..c99a6cb65939 100644 --- a/src/agents/subagent-announce.ts +++ b/src/agents/subagent-announce.ts @@ -32,6 +32,10 @@ import { queueEmbeddedPiMessage, waitForEmbeddedPiRunEnd, } from "./pi-embedded.js"; +import { + runSubagentAnnounceDispatch, + type SubagentAnnounceDeliveryResult, +} from "./subagent-announce-dispatch.js"; import { type AnnounceQueueItem, enqueueAnnounce } from "./subagent-announce-queue.js"; import { getSubagentDepthFromSessionStore } from "./subagent-depth.js"; import type { SpawnSubagentMode } from "./subagent-spawn.js"; @@ -53,14 +57,6 @@ type ToolResultMessage = { content?: unknown; }; -type SubagentDeliveryPath = "queued" | "steered" | "direct" | "none"; - -type SubagentAnnounceDeliveryResult = { - delivered: boolean; - path: SubagentDeliveryPath; - error?: string; -}; - function resolveSubagentAnnounceTimeoutMs(cfg: ReturnType): number { const configured = cfg.agents?.defaults?.subagents?.announceTimeoutMs; if (typeof configured !== "number" || !Number.isFinite(configured)) { @@ -705,27 +701,6 @@ async function maybeQueueSubagentAnnounce(params: { return "none"; } -function queueOutcomeToDeliveryResult( - outcome: "steered" | "queued" | "none", -): SubagentAnnounceDeliveryResult { - if (outcome === "steered") { - return { - delivered: true, - path: "steered", - }; - } - if (outcome === "queued") { - return { - delivered: true, - path: "queued", - }; - } - return { - delivered: false, - path: "none", - }; -} - async function sendSubagentAnnounceDirectly(params: { targetRequesterSessionKey: string; triggerMessage: string; @@ -905,64 +880,34 @@ async function deliverSubagentAnnouncement(params: { directIdempotencyKey: string; signal?: AbortSignal; }): Promise { - if (params.signal?.aborted) { - return { - delivered: false, - path: "none", - }; - } - // Non-completion mode mirrors historical behavior: try queued/steered delivery first, - // then (only if not queued) attempt direct delivery. - if (!params.expectsCompletionMessage) { - const queueOutcome = await maybeQueueSubagentAnnounce({ - requesterSessionKey: params.requesterSessionKey, - announceId: params.announceId, - triggerMessage: params.triggerMessage, - summaryLine: params.summaryLine, - requesterOrigin: params.requesterOrigin, - signal: params.signal, - }); - const queued = queueOutcomeToDeliveryResult(queueOutcome); - if (queued.delivered) { - return queued; - } - } - - // Completion-mode uses direct send first so manual spawns can return immediately - // in the common ready-to-deliver case. - const direct = await sendSubagentAnnounceDirectly({ - targetRequesterSessionKey: params.targetRequesterSessionKey, - triggerMessage: params.triggerMessage, - completionMessage: params.completionMessage, - directIdempotencyKey: params.directIdempotencyKey, - completionDirectOrigin: params.completionDirectOrigin, - completionRouteMode: params.completionRouteMode, - spawnMode: params.spawnMode, - directOrigin: params.directOrigin, - requesterIsSubagent: params.requesterIsSubagent, + return await runSubagentAnnounceDispatch({ expectsCompletionMessage: params.expectsCompletionMessage, signal: params.signal, - bestEffortDeliver: params.bestEffortDeliver, - }); - if (direct.delivered || !params.expectsCompletionMessage) { - return direct; - } - - // If completion path failed direct delivery, try queueing as a fallback so the - // report can still be delivered once the requester session is idle. - const queueOutcome = await maybeQueueSubagentAnnounce({ - requesterSessionKey: params.requesterSessionKey, - announceId: params.announceId, - triggerMessage: params.triggerMessage, - summaryLine: params.summaryLine, - requesterOrigin: params.requesterOrigin, - signal: params.signal, + queue: async () => + await maybeQueueSubagentAnnounce({ + requesterSessionKey: params.requesterSessionKey, + announceId: params.announceId, + triggerMessage: params.triggerMessage, + summaryLine: params.summaryLine, + requesterOrigin: params.requesterOrigin, + signal: params.signal, + }), + direct: async () => + await sendSubagentAnnounceDirectly({ + targetRequesterSessionKey: params.targetRequesterSessionKey, + triggerMessage: params.triggerMessage, + completionMessage: params.completionMessage, + directIdempotencyKey: params.directIdempotencyKey, + completionDirectOrigin: params.completionDirectOrigin, + completionRouteMode: params.completionRouteMode, + spawnMode: params.spawnMode, + directOrigin: params.directOrigin, + requesterIsSubagent: params.requesterIsSubagent, + expectsCompletionMessage: params.expectsCompletionMessage, + signal: params.signal, + bestEffortDeliver: params.bestEffortDeliver, + }), }); - if (queueOutcome === "steered" || queueOutcome === "queued") { - return queueOutcomeToDeliveryResult(queueOutcome); - } - - return direct; } function loadSessionEntryByKey(sessionKey: string) { diff --git a/src/agents/subagent-registry.announce-loop-guard.test.ts b/src/agents/subagent-registry.announce-loop-guard.test.ts index 8389c53503c6..498b38aaedcf 100644 --- a/src/agents/subagent-registry.announce-loop-guard.test.ts +++ b/src/agents/subagent-registry.announce-loop-guard.test.ts @@ -155,4 +155,43 @@ describe("announce loop guard (#18264)", () => { const stored = runs.find((run) => run.runId === entry.runId); expect(stored?.cleanupCompletedAt).toBeDefined(); }); + + test("announce rejection resets cleanupHandled so retries can resume", async () => { + announceFn.mockReset(); + announceFn.mockRejectedValueOnce(new Error("announce failed")); + registry.resetSubagentRegistryForTests(); + + const now = Date.now(); + const runId = "test-announce-rejection"; + loadSubagentRegistryFromDisk.mockReturnValue( + new Map([ + [ + runId, + { + runId, + childSessionKey: "agent:main:subagent:child-1", + requesterSessionKey: "agent:main:main", + requesterDisplayKey: "agent:main:main", + task: "rejection test", + cleanup: "keep" as const, + createdAt: now - 30_000, + startedAt: now - 20_000, + endedAt: now - 10_000, + cleanupHandled: false, + }, + ], + ]), + ); + + registry.initSubagentRegistry(); + await Promise.resolve(); + await Promise.resolve(); + + const runs = registry.listSubagentRunsForRequester("agent:main:main"); + const stored = runs.find((run) => run.runId === runId); + expect(stored?.cleanupHandled).toBe(false); + expect(stored?.cleanupCompletedAt).toBeUndefined(); + expect(stored?.announceRetryCount).toBe(1); + expect(stored?.lastAnnounceRetryAt).toBeTypeOf("number"); + }); }); diff --git a/src/agents/subagent-registry.ts b/src/agents/subagent-registry.ts index edb8f228b07b..072fd91693fc 100644 --- a/src/agents/subagent-registry.ts +++ b/src/agents/subagent-registry.ts @@ -331,9 +331,16 @@ function startSubagentAnnounceCleanupFlow(runId: string, entry: SubagentRunRecor outcome: entry.outcome, spawnMode: entry.spawnMode, expectsCompletionMessage: entry.expectsCompletionMessage, - }).then((didAnnounce) => { - void finalizeSubagentCleanup(runId, entry.cleanup, didAnnounce); - }); + }) + .then((didAnnounce) => { + void finalizeSubagentCleanup(runId, entry.cleanup, didAnnounce); + }) + .catch((error) => { + defaultRuntime.log( + `[warn] Subagent announce flow failed during cleanup for run ${runId}: ${String(error)}`, + ); + void finalizeSubagentCleanup(runId, entry.cleanup, false); + }); return true; } diff --git a/src/gateway/server-methods/send.ts b/src/gateway/server-methods/send.ts index 9e976a79ae17..f398d94aae4e 100644 --- a/src/gateway/server-methods/send.ts +++ b/src/gateway/server-methods/send.ts @@ -1,7 +1,8 @@ import { resolveSessionAgentId } from "../../agents/agent-scope.js"; -import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js"; +import { normalizeChannelId } from "../../channels/plugins/index.js"; import { createOutboundSendDeps } from "../../cli/deps.js"; import { loadConfig } from "../../config/config.js"; +import { resolveOutboundChannelPlugin } from "../../infra/outbound/channel-resolution.js"; import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.js"; import { deliverOutboundPayloads } from "../../infra/outbound/deliver.js"; import { @@ -166,7 +167,7 @@ export const sendHandlers: GatewayRequestHandlers = { ? request.threadId.trim() : undefined; const outboundChannel = channel; - const plugin = getChannelPlugin(channel); + const plugin = resolveOutboundChannelPlugin({ channel, cfg }); if (!plugin) { respond( false, @@ -393,7 +394,7 @@ export const sendHandlers: GatewayRequestHandlers = { ? request.accountId.trim() : undefined; try { - const plugin = getChannelPlugin(channel); + const plugin = resolveOutboundChannelPlugin({ channel, cfg }); const outbound = plugin?.outbound; if (!outbound?.sendPoll) { respond( diff --git a/src/infra/outbound/channel-resolution.ts b/src/infra/outbound/channel-resolution.ts new file mode 100644 index 000000000000..58596da93f3c --- /dev/null +++ b/src/infra/outbound/channel-resolution.ts @@ -0,0 +1,73 @@ +import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js"; +import { getChannelPlugin } from "../../channels/plugins/index.js"; +import type { ChannelPlugin } from "../../channels/plugins/types.js"; +import type { OpenClawConfig } from "../../config/config.js"; +import { applyPluginAutoEnable } from "../../config/plugin-auto-enable.js"; +import { loadOpenClawPlugins } from "../../plugins/loader.js"; +import { getActivePluginRegistry, getActivePluginRegistryKey } from "../../plugins/runtime.js"; +import { + isDeliverableMessageChannel, + normalizeMessageChannel, + type DeliverableMessageChannel, +} from "../../utils/message-channel.js"; + +const bootstrapAttempts = new Set(); + +export function normalizeDeliverableOutboundChannel( + raw?: string | null, +): DeliverableMessageChannel | undefined { + const normalized = normalizeMessageChannel(raw); + if (!normalized || !isDeliverableMessageChannel(normalized)) { + return undefined; + } + return normalized; +} + +function maybeBootstrapChannelPlugin(params: { + channel: DeliverableMessageChannel; + cfg?: OpenClawConfig; +}): void { + const cfg = params.cfg; + if (!cfg) { + return; + } + + const activeRegistry = getActivePluginRegistry(); + if ((activeRegistry?.channels?.length ?? 0) > 0) { + return; + } + + const registryKey = getActivePluginRegistryKey() ?? ""; + const attemptKey = `${registryKey}:${params.channel}`; + if (bootstrapAttempts.has(attemptKey)) { + return; + } + bootstrapAttempts.add(attemptKey); + + const autoEnabled = applyPluginAutoEnable({ config: cfg }).config; + const defaultAgentId = resolveDefaultAgentId(autoEnabled); + const workspaceDir = resolveAgentWorkspaceDir(autoEnabled, defaultAgentId); + loadOpenClawPlugins({ + config: autoEnabled, + workspaceDir, + }); +} + +export function resolveOutboundChannelPlugin(params: { + channel: string; + cfg?: OpenClawConfig; +}): ChannelPlugin | undefined { + const normalized = normalizeDeliverableOutboundChannel(params.channel); + if (!normalized) { + return undefined; + } + + const resolve = () => getChannelPlugin(normalized); + const current = resolve(); + if (current) { + return current; + } + + maybeBootstrapChannelPlugin({ channel: normalized, cfg: params.cfg }); + return resolve(); +} diff --git a/src/infra/outbound/message.test.ts b/src/infra/outbound/message.test.ts index 3714e7ab5acd..d6fab2e39dc5 100644 --- a/src/infra/outbound/message.test.ts +++ b/src/infra/outbound/message.test.ts @@ -4,6 +4,7 @@ const mocks = vi.hoisted(() => ({ getChannelPlugin: vi.fn(), resolveOutboundTarget: vi.fn(), deliverOutboundPayloads: vi.fn(), + loadOpenClawPlugins: vi.fn(), })); vi.mock("../../channels/plugins/index.js", () => ({ @@ -11,6 +12,19 @@ vi.mock("../../channels/plugins/index.js", () => ({ getChannelPlugin: mocks.getChannelPlugin, })); +vi.mock("../../agents/agent-scope.js", () => ({ + resolveDefaultAgentId: () => "main", + resolveAgentWorkspaceDir: () => "/tmp/openclaw-test-workspace", +})); + +vi.mock("../../config/plugin-auto-enable.js", () => ({ + applyPluginAutoEnable: ({ config }: { config: unknown }) => ({ config, changes: [] }), +})); + +vi.mock("../../plugins/loader.js", () => ({ + loadOpenClawPlugins: mocks.loadOpenClawPlugins, +})); + vi.mock("./targets.js", () => ({ resolveOutboundTarget: mocks.resolveOutboundTarget, })); @@ -19,13 +33,17 @@ vi.mock("./deliver.js", () => ({ deliverOutboundPayloads: mocks.deliverOutboundPayloads, })); +import { setActivePluginRegistry } from "../../plugins/runtime.js"; +import { createTestRegistry } from "../../test-utils/channel-plugins.js"; import { sendMessage } from "./message.js"; describe("sendMessage", () => { beforeEach(() => { + setActivePluginRegistry(createTestRegistry([])); mocks.getChannelPlugin.mockClear(); mocks.resolveOutboundTarget.mockClear(); mocks.deliverOutboundPayloads.mockClear(); + mocks.loadOpenClawPlugins.mockClear(); mocks.getChannelPlugin.mockReturnValue({ outbound: { deliveryMode: "direct" }, @@ -37,8 +55,8 @@ describe("sendMessage", () => { it("passes explicit agentId to outbound delivery for scoped media roots", async () => { await sendMessage({ cfg: {}, - channel: "mattermost", - to: "channel:town-square", + channel: "telegram", + to: "123456", content: "hi", agentId: "work", }); @@ -46,9 +64,34 @@ describe("sendMessage", () => { expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( expect.objectContaining({ agentId: "work", - channel: "mattermost", - to: "channel:town-square", + channel: "telegram", + to: "123456", }), ); }); + + it("recovers telegram plugin resolution so message/send does not fail with Unknown channel: telegram", async () => { + const telegramPlugin = { + outbound: { deliveryMode: "direct" }, + }; + mocks.getChannelPlugin + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(telegramPlugin) + .mockReturnValue(telegramPlugin); + + await expect( + sendMessage({ + cfg: { channels: { telegram: { botToken: "test-token" } } }, + channel: "telegram", + to: "123456", + content: "hi", + }), + ).resolves.toMatchObject({ + channel: "telegram", + to: "123456", + via: "direct", + }); + + expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1); + }); }); diff --git a/src/infra/outbound/message.ts b/src/infra/outbound/message.ts index 649aabd0ecef..30451b66959c 100644 --- a/src/infra/outbound/message.ts +++ b/src/infra/outbound/message.ts @@ -1,4 +1,3 @@ -import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js"; import type { OpenClawConfig } from "../../config/config.js"; import { loadConfig } from "../../config/config.js"; import { callGatewayLeastPrivilege, randomIdempotencyKey } from "../../gateway/call.js"; @@ -10,6 +9,10 @@ import { type GatewayClientMode, type GatewayClientName, } from "../../utils/message-channel.js"; +import { + normalizeDeliverableOutboundChannel, + resolveOutboundChannelPlugin, +} from "./channel-resolution.js"; import { resolveMessageChannelSelection } from "./channel-selection.js"; import { deliverOutboundPayloads, @@ -107,17 +110,18 @@ async function resolveRequiredChannel(params: { cfg: OpenClawConfig; channel?: string; }): Promise { - const channel = params.channel?.trim() - ? normalizeChannelId(params.channel) - : (await resolveMessageChannelSelection({ cfg: params.cfg })).channel; - if (!channel) { - throw new Error(`Unknown channel: ${params.channel}`); + if (params.channel?.trim()) { + const normalized = normalizeDeliverableOutboundChannel(params.channel); + if (!normalized) { + throw new Error(`Unknown channel: ${params.channel}`); + } + return normalized; } - return channel; + return (await resolveMessageChannelSelection({ cfg: params.cfg })).channel; } -function resolveRequiredPlugin(channel: string) { - const plugin = getChannelPlugin(channel); +function resolveRequiredPlugin(channel: string, cfg: OpenClawConfig) { + const plugin = resolveOutboundChannelPlugin({ channel, cfg }); if (!plugin) { throw new Error(`Unknown channel: ${channel}`); } @@ -166,7 +170,7 @@ async function callMessageGateway(params: { export async function sendMessage(params: MessageSendParams): Promise { const cfg = params.cfg ?? loadConfig(); const channel = await resolveRequiredChannel({ cfg, channel: params.channel }); - const plugin = resolveRequiredPlugin(channel); + const plugin = resolveRequiredPlugin(channel, cfg); const deliveryMode = plugin.outbound?.deliveryMode ?? "direct"; const normalizedPayloads = normalizeReplyPayloadsForDelivery([ { @@ -279,7 +283,7 @@ export async function sendPoll(params: MessagePollParams): Promise ({ + getChannelPlugin: vi.fn(), + loadOpenClawPlugins: vi.fn(), +})); + +vi.mock("../../channels/plugins/index.js", () => ({ + getChannelPlugin: mocks.getChannelPlugin, + normalizeChannelId: (channel?: string) => channel?.trim().toLowerCase() ?? undefined, +})); + +vi.mock("../../agents/agent-scope.js", () => ({ + resolveDefaultAgentId: () => "main", + resolveAgentWorkspaceDir: () => "/tmp/openclaw-test-workspace", +})); + +vi.mock("../../config/plugin-auto-enable.js", () => ({ + applyPluginAutoEnable: ({ config }: { config: unknown }) => ({ config, changes: [] }), +})); + +vi.mock("../../plugins/loader.js", () => ({ + loadOpenClawPlugins: mocks.loadOpenClawPlugins, +})); + +import { setActivePluginRegistry } from "../../plugins/runtime.js"; +import { createTestRegistry } from "../../test-utils/channel-plugins.js"; +import { resolveOutboundTarget } from "./targets.js"; + +describe("resolveOutboundTarget channel resolution", () => { + beforeEach(() => { + setActivePluginRegistry(createTestRegistry([])); + mocks.getChannelPlugin.mockReset(); + mocks.loadOpenClawPlugins.mockReset(); + }); + + it("recovers telegram plugin resolution so announce delivery does not fail with Unsupported channel: telegram", () => { + const telegramPlugin = { + id: "telegram", + meta: { label: "Telegram" }, + config: { + listAccountIds: () => [], + resolveAccount: () => ({}), + }, + }; + mocks.getChannelPlugin + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(telegramPlugin) + .mockReturnValue(telegramPlugin); + + const result = resolveOutboundTarget({ + channel: "telegram", + to: "123456", + cfg: { channels: { telegram: { botToken: "test-token" } } }, + mode: "explicit", + }); + + expect(result).toEqual({ ok: true, to: "123456" }); + expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/infra/outbound/targets.ts b/src/infra/outbound/targets.ts index 41baa5586539..d9411e2223c8 100644 --- a/src/infra/outbound/targets.ts +++ b/src/infra/outbound/targets.ts @@ -1,5 +1,4 @@ import { normalizeChatType, type ChatType } from "../../channels/chat-type.js"; -import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js"; import type { ChannelOutboundTargetMode } from "../../channels/plugins/types.js"; import { formatCliCommand } from "../../cli/command-format.js"; import type { OpenClawConfig } from "../../config/config.js"; @@ -20,6 +19,10 @@ import { normalizeMessageChannel, } from "../../utils/message-channel.js"; import { isWhatsAppGroupJid, normalizeWhatsAppTarget } from "../../whatsapp/normalize.js"; +import { + normalizeDeliverableOutboundChannel, + resolveOutboundChannelPlugin, +} from "./channel-resolution.js"; import { missingTargetError } from "./target-errors.js"; export type OutboundChannel = DeliverableMessageChannel | "none"; @@ -181,7 +184,10 @@ export function resolveOutboundTarget(params: { }; } - const plugin = getChannelPlugin(params.channel); + const plugin = resolveOutboundChannelPlugin({ + channel: params.channel, + cfg: params.cfg, + }); if (!plugin) { return { ok: false, @@ -242,7 +248,7 @@ export function resolveHeartbeatDeliveryTarget(params: { if (rawTarget === "none" || rawTarget === "last") { target = rawTarget; } else if (typeof rawTarget === "string") { - const normalized = normalizeChannelId(rawTarget); + const normalized = normalizeDeliverableOutboundChannel(rawTarget); if (normalized) { target = normalized; } @@ -269,7 +275,10 @@ export function resolveHeartbeatDeliveryTarget(params: { let effectiveAccountId = heartbeatAccountId || resolvedTarget.accountId; if (heartbeatAccountId && resolvedTarget.channel) { - const plugin = getChannelPlugin(resolvedTarget.channel); + const plugin = resolveOutboundChannelPlugin({ + channel: resolvedTarget.channel, + cfg, + }); const listAccountIds = plugin?.config.listAccountIds; const accountIds = listAccountIds ? listAccountIds(cfg) : []; if (accountIds.length > 0) { @@ -331,7 +340,10 @@ export function resolveHeartbeatDeliveryTarget(params: { } let reason: string | undefined; - const plugin = getChannelPlugin(resolvedTarget.channel); + const plugin = resolveOutboundChannelPlugin({ + channel: resolvedTarget.channel, + cfg, + }); if (plugin?.config.resolveAllowFrom) { const explicit = resolveOutboundTarget({ channel: resolvedTarget.channel, @@ -516,7 +528,10 @@ export function resolveHeartbeatSenderContext(params: { params.delivery.accountId ?? (provider === params.delivery.lastChannel ? params.delivery.lastAccountId : undefined); const allowFromRaw = provider - ? (getChannelPlugin(provider)?.config.resolveAllowFrom?.({ + ? (resolveOutboundChannelPlugin({ + channel: provider, + cfg: params.cfg, + })?.config.resolveAllowFrom?.({ cfg: params.cfg, accountId, }) ?? []) diff --git a/src/telegram/send.test.ts b/src/telegram/send.test.ts index 37d881d843c2..afd616b5f153 100644 --- a/src/telegram/send.test.ts +++ b/src/telegram/send.test.ts @@ -196,6 +196,10 @@ describe("sendMessageTelegram", () => { for (const testCase of cases) { botCtorSpy.mockClear(); loadConfig.mockReturnValue(testCase.cfg); + botApi.sendMessage.mockResolvedValue({ + message_id: 1, + chat: { id: "123" }, + }); await sendMessageTelegram("123", "hi", testCase.opts); expect(botCtorSpy, testCase.name).toHaveBeenCalledWith( "tok", @@ -325,6 +329,40 @@ describe("sendMessageTelegram", () => { } }); + it("fails when Telegram text send returns no message_id", async () => { + const sendMessage = vi.fn().mockResolvedValue({ + chat: { id: "123" }, + }); + const api = { sendMessage } as unknown as { + sendMessage: typeof sendMessage; + }; + + await expect( + sendMessageTelegram("123", "hi", { + token: "tok", + api, + }), + ).rejects.toThrow(/returned no message_id/i); + }); + + it("fails when Telegram media send returns no message_id", async () => { + mockLoadedMedia({ contentType: "image/png", fileName: "photo.png" }); + const sendPhoto = vi.fn().mockResolvedValue({ + chat: { id: "123" }, + }); + const api = { sendPhoto } as unknown as { + sendPhoto: typeof sendPhoto; + }; + + await expect( + sendMessageTelegram("123", "caption", { + token: "tok", + api, + mediaUrl: "https://example.com/photo.png", + }), + ).rejects.toThrow(/returned no message_id/i); + }); + it("uses native fetch for BAN compatibility when api is omitted", async () => { const originalFetch = globalThis.fetch; const originalBun = (globalThis as { Bun?: unknown }).Bun; diff --git a/src/telegram/send.ts b/src/telegram/send.ts index 85327df22b58..ceaa9113e32c 100644 --- a/src/telegram/send.ts +++ b/src/telegram/send.ts @@ -86,6 +86,16 @@ type TelegramReactionOpts = { retry?: RetryConfig; }; +function resolveTelegramMessageIdOrThrow( + result: TelegramMessageLike | null | undefined, + context: string, +): number { + if (typeof result?.message_id === "number" && Number.isFinite(result.message_id)) { + return Math.trunc(result.message_id); + } + throw new Error(`Telegram ${context} returned no message_id`); +} + const PARSE_ERR_RE = /can't parse entities|parse entities|find end of the entity/i; const THREAD_NOT_FOUND_RE = /400:\s*Bad Request:\s*message thread not found/i; const MESSAGE_NOT_MODIFIED_RE = @@ -685,11 +695,9 @@ export async function sendMessageTelegram( })(); const result = await sendMedia(mediaSender.label, mediaSender.sender); - const mediaMessageId = String(result?.message_id ?? "unknown"); + const mediaMessageId = resolveTelegramMessageIdOrThrow(result, "media send"); const resolvedChatId = String(result?.chat?.id ?? chatId); - if (result?.message_id) { - recordSentMessage(chatId, result.message_id); - } + recordSentMessage(chatId, mediaMessageId); recordChannelActivity({ channel: "telegram", accountId: account.accountId, @@ -708,13 +716,15 @@ export async function sendMessageTelegram( : undefined; const textRes = await sendTelegramText(followUpText, textParams); // Return the text message ID as the "main" message (it's the actual content). + const textMessageId = resolveTelegramMessageIdOrThrow(textRes, "text follow-up send"); + recordSentMessage(chatId, textMessageId); return { - messageId: String(textRes?.message_id ?? mediaMessageId), + messageId: String(textMessageId), chatId: resolvedChatId, }; } - return { messageId: mediaMessageId, chatId: resolvedChatId }; + return { messageId: String(mediaMessageId), chatId: resolvedChatId }; } if (!text || !text.trim()) { @@ -728,16 +738,14 @@ export async function sendMessageTelegram( } : undefined; const res = await sendTelegramText(text, textParams, opts.plainText); - const messageId = String(res?.message_id ?? "unknown"); - if (res?.message_id) { - recordSentMessage(chatId, res.message_id); - } + const messageId = resolveTelegramMessageIdOrThrow(res, "text send"); + recordSentMessage(chatId, messageId); recordChannelActivity({ channel: "telegram", accountId: account.accountId, direction: "outbound", }); - return { messageId, chatId: String(res?.chat?.id ?? chatId) }; + return { messageId: String(messageId), chatId: String(res?.chat?.id ?? chatId) }; } export async function reactMessageTelegram( @@ -1013,18 +1021,16 @@ export async function sendStickerTelegram( requestWithChatNotFound(() => api.sendSticker(chatId, fileId.trim(), effectiveParams), label), ); - const messageId = String(result?.message_id ?? "unknown"); + const messageId = resolveTelegramMessageIdOrThrow(result, "sticker send"); const resolvedChatId = String(result?.chat?.id ?? chatId); - if (result?.message_id) { - recordSentMessage(chatId, result.message_id); - } + recordSentMessage(chatId, messageId); recordChannelActivity({ channel: "telegram", accountId: account.accountId, direction: "outbound", }); - return { messageId, chatId: resolvedChatId }; + return { messageId: String(messageId), chatId: resolvedChatId }; } type TelegramPollOpts = { @@ -1121,12 +1127,10 @@ export async function sendPollTelegram( ), ); - const messageId = String(result?.message_id ?? "unknown"); + const messageId = resolveTelegramMessageIdOrThrow(result, "poll send"); const resolvedChatId = String(result?.chat?.id ?? chatId); const pollId = result?.poll?.id; - if (result?.message_id) { - recordSentMessage(chatId, result.message_id); - } + recordSentMessage(chatId, messageId); recordChannelActivity({ channel: "telegram", @@ -1134,7 +1138,7 @@ export async function sendPollTelegram( direction: "outbound", }); - return { messageId, chatId: resolvedChatId, pollId }; + return { messageId: String(messageId), chatId: resolvedChatId, pollId }; } // --------------------------------------------------------------------------- From 876018f322b7f4388985fe9d14f73b8296d40072 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:31:32 +0100 Subject: [PATCH 056/160] chore(deps): update dependencies and lockfile --- CHANGELOG.md | 1 + package.json | 12 +- pnpm-lock.yaml | 2068 +++++++++++------------------------------------- 3 files changed, 449 insertions(+), 1632 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ee1e3d0375f..d71991f6a018 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai ### Changes +- Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. diff --git a/package.json b/package.json index 81a8a66cb4b7..5f6443b64c87 100644 --- a/package.json +++ b/package.json @@ -141,7 +141,7 @@ }, "dependencies": { "@agentclientprotocol/sdk": "0.14.1", - "@aws-sdk/client-bedrock": "^3.997.0", + "@aws-sdk/client-bedrock": "^3.998.0", "@buape/carbon": "0.0.0-beta-20260216184201", "@clack/prompts": "^1.0.1", "@discordjs/voice": "^0.19.0", @@ -151,10 +151,10 @@ "@larksuiteoapi/node-sdk": "^1.59.0", "@line/bot-sdk": "^10.6.0", "@lydell/node-pty": "1.2.0-beta.3", - "@mariozechner/pi-agent-core": "0.55.0", - "@mariozechner/pi-ai": "0.55.0", - "@mariozechner/pi-coding-agent": "0.55.0", - "@mariozechner/pi-tui": "0.55.0", + "@mariozechner/pi-agent-core": "0.55.1", + "@mariozechner/pi-ai": "0.55.1", + "@mariozechner/pi-coding-agent": "0.55.1", + "@mariozechner/pi-tui": "0.55.1", "@mozilla/readability": "^0.6.0", "@sinclair/typebox": "0.34.48", "@slack/bolt": "^4.6.0", @@ -204,7 +204,7 @@ "@types/node": "^25.3.0", "@types/qrcode-terminal": "^0.12.2", "@types/ws": "^8.18.1", - "@typescript/native-preview": "7.0.0-dev.20260224.1", + "@typescript/native-preview": "7.0.0-dev.20260225.1", "@vitest/coverage-v8": "^4.0.18", "lit": "^3.3.2", "oxfmt": "0.35.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 365b0ee17072..0ddc70d9f977 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,8 +24,8 @@ importers: specifier: 0.14.1 version: 0.14.1(zod@4.3.6) '@aws-sdk/client-bedrock': - specifier: ^3.997.0 - version: 3.997.0 + specifier: ^3.998.0 + version: 3.998.0 '@buape/carbon': specifier: 0.0.0-beta-20260216184201 version: 0.0.0-beta-20260216184201(@discordjs/opus@0.10.0)(hono@4.11.10)(opusscript@0.1.1) @@ -54,23 +54,23 @@ importers: specifier: 1.2.0-beta.3 version: 1.2.0-beta.3 '@mariozechner/pi-agent-core': - specifier: 0.55.0 - version: 0.55.0(ws@8.19.0)(zod@4.3.6) + specifier: 0.55.1 + version: 0.55.1(ws@8.19.0)(zod@4.3.6) '@mariozechner/pi-ai': - specifier: 0.55.0 - version: 0.55.0(ws@8.19.0)(zod@4.3.6) + specifier: 0.55.1 + version: 0.55.1(ws@8.19.0)(zod@4.3.6) '@mariozechner/pi-coding-agent': - specifier: 0.55.0 - version: 0.55.0(ws@8.19.0)(zod@4.3.6) + specifier: 0.55.1 + version: 0.55.1(ws@8.19.0)(zod@4.3.6) '@mariozechner/pi-tui': - specifier: 0.55.0 - version: 0.55.0 + specifier: 0.55.1 + version: 0.55.1 '@mozilla/readability': specifier: ^0.6.0 version: 0.6.0 '@napi-rs/canvas': specifier: ^0.1.89 - version: 0.1.92 + version: 0.1.95 '@sinclair/typebox': specifier: 0.34.48 version: 0.34.48 @@ -214,8 +214,8 @@ importers: specifier: ^8.18.1 version: 8.18.1 '@typescript/native-preview': - specifier: 7.0.0-dev.20260224.1 - version: 7.0.0-dev.20260224.1 + specifier: 7.0.0-dev.20260225.1 + version: 7.0.0-dev.20260225.1 '@vitest/coverage-v8': specifier: ^4.0.18 version: 4.0.18(@vitest/browser@4.0.18(vite@7.3.1(@types/node@25.3.0)(jiti@2.6.1)(lightningcss@1.30.2)(tsx@4.21.0)(yaml@2.8.2))(vitest@4.0.18))(vitest@4.0.18) @@ -236,7 +236,7 @@ importers: version: 0.21.1(signal-polyfill@0.2.2) tsdown: specifier: ^0.20.3 - version: 0.20.3(@typescript/native-preview@7.0.0-dev.20260224.1)(typescript@5.9.3) + version: 0.20.3(@typescript/native-preview@7.0.0-dev.20260225.1)(typescript@5.9.3) tsx: specifier: ^4.21.0 version: 4.21.0 @@ -314,7 +314,7 @@ importers: version: 10.6.1 openclaw: specifier: '>=2026.1.26' - version: 2026.2.23(@napi-rs/canvas@0.1.94)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)) + version: 2026.2.24(@napi-rs/canvas@0.1.95)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)) extensions/imessage: {} @@ -350,7 +350,7 @@ importers: dependencies: openclaw: specifier: '>=2026.1.26' - version: 2026.2.23(@napi-rs/canvas@0.1.94)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)) + version: 2026.2.24(@napi-rs/canvas@0.1.95)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)) extensions/memory-lancedb: dependencies: @@ -532,226 +532,111 @@ packages: '@aws-crypto/util@5.2.0': resolution: {integrity: sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ==} - '@aws-sdk/client-bedrock-runtime@3.995.0': - resolution: {integrity: sha512-nI7tT11L9s34AKr95GHmxs6k2+3ie+rEOew2cXOwsMC9k/5aifrZwh0JjAkBop4FqbmS8n0ZjCKDjBZFY/0YxQ==} + '@aws-sdk/client-bedrock-runtime@3.998.0': + resolution: {integrity: sha512-orRgpdNmdRLik+en3xDxlGuT5AxQU+GFUTMn97ZdRuPLnAiY7Y6/8VTsod6y97/3NB8xuTZbH9wNXzW97IWNMA==} engines: {node: '>=20.0.0'} - '@aws-sdk/client-bedrock-runtime@3.997.0': - resolution: {integrity: sha512-yEgCc/HvI7dLeXQLCuc4cnbzwE/NbNpKX8NmSSWTy3jnjiMZwrNKdHMBgPoNvaEb0klHhnTyO+JCHVVCPI/eYw==} + '@aws-sdk/client-bedrock@3.998.0': + resolution: {integrity: sha512-NeSBIdsJwVtACGHXVoguJOsKhq6oR5Q2B6BUU7LWGqIl1skwPors77aLpOa2240ZFtX3Br/0lJYfxAhB8692KA==} engines: {node: '>=20.0.0'} - '@aws-sdk/client-bedrock@3.995.0': - resolution: {integrity: sha512-ONw5c7pOeHe78kC+jK2j73hP727Kqp7cc9lZqkfshlBD8MWxXmZM9GihIQLrNBCSUKRhc19NH7DUM6B7uN0mMQ==} + '@aws-sdk/core@3.973.14': + resolution: {integrity: sha512-iAQ1jIGESTVjoqNNY9VlsE9FnCz+Hc8s+dgurF6WrgFyVIw+uggH+V102RFhwjRv4dLSSLfzjDwvQnLszov7TQ==} engines: {node: '>=20.0.0'} - '@aws-sdk/client-bedrock@3.997.0': - resolution: {integrity: sha512-PMRqxSzfkQHbU7ADVlT4jYLB7beFQWLXN9CGI9D9P8eqCIaDVv3YxTfwcT3FcBVucqktdTBTEowhvKn0whr/rA==} + '@aws-sdk/credential-provider-env@3.972.12': + resolution: {integrity: sha512-WPtj/iAYHHd+NDM6AZoilZwUz0nMaPxbTPGLA7nhyIYRZN2L8trqfbNvm7g/Jr3gzfKp1LpO6AtBTnrhz9WW2g==} engines: {node: '>=20.0.0'} - '@aws-sdk/client-sso@3.993.0': - resolution: {integrity: sha512-VLUN+wIeNX24fg12SCbzTUBnBENlL014yMKZvRhPkcn4wHR6LKgNrjsG3fZ03Xs0XoKaGtNFi1VVrq666sGBoQ==} + '@aws-sdk/credential-provider-http@3.972.14': + resolution: {integrity: sha512-umtjCicH2o/Fcc8Fu1562UkDyt6gql4czTYVlUfHfAM8S4QEKggzmtHYYYpPfQcjFj1ajyy68ahYSuF67x4ptQ==} engines: {node: '>=20.0.0'} - '@aws-sdk/core@3.973.11': - resolution: {integrity: sha512-wdQ8vrvHkKIV7yNUKXyjPWKCdYEUrZTHJ8Ojd5uJxXp9vqPCkUR1dpi1NtOLcrDgueJH7MUH5lQZxshjFPSbDA==} + '@aws-sdk/credential-provider-ini@3.972.12': + resolution: {integrity: sha512-qjzgnMl6GIBbVeK74jBqSF07+s6kyeZl5R88qjMs302JlqkxE57jkvflDmZ9I017ffEWqIUa9/M4Hfp28qyu1g==} engines: {node: '>=20.0.0'} - '@aws-sdk/core@3.973.13': - resolution: {integrity: sha512-eCFiLyBhJR7c/i8hZOETdzj2wsLFzi2L/w9/jajOgwmGqO8xrUExqkTZqdjROkwU62owqeqSuw4sIzlCv1E/ww==} + '@aws-sdk/credential-provider-login@3.972.12': + resolution: {integrity: sha512-AO57y46PzG24bJzxWLk+FYJG6MzxvXoFXnOKnmKUGV43ub4/FS/4Rz7zCC6ThqUotgqEFd30l5LTAd65RP65pg==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-env@3.972.11': - resolution: {integrity: sha512-hbyoFuVm3qOAGfIPS9t7jCs8GFLFoaOs8ZmYp/chqciuHDyEGv+J365ip7YSvXSrxxUbeW9NyB1hTLt40NBMRg==} + '@aws-sdk/credential-provider-node@3.972.13': + resolution: {integrity: sha512-ME2sgus+gFRtiudy5Xqj9iT/tj8lHOIGrFgktuO5skJU4EngOvTZ1Hpj8mknrW4FgWXmpWhc88NtEscUuuDpKw==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-env@3.972.9': - resolution: {integrity: sha512-ZptrOwQynfupubvcngLkbdIq/aXvl/czdpEG8XJ8mN8Nb19BR0jaK0bR+tfuMU36Ez9q4xv7GGkHFqEEP2hUUQ==} + '@aws-sdk/credential-provider-process@3.972.12': + resolution: {integrity: sha512-msxrHBpVP5AOIDohNPCINUtL47f7XI1TEru3N13uM3nWUMvIRA1vFa8Tlxbxm1EntPPvLAxRmvE5EbjDjOZkbw==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-http@3.972.11': - resolution: {integrity: sha512-hECWoOoH386bGr89NQc9vA/abkGf5TJrMREt+lhNcnSNmoBS04fK7vc3LrJBSQAUGGVj0Tz3f4dHB3w5veovig==} + '@aws-sdk/credential-provider-sso@3.972.12': + resolution: {integrity: sha512-D5iC5546hJyhobJN0szOT4KVeJQ8z/meZq2B3lEDZFcvHONKw+tzq36DAJUy3qLTueeB2geSxiHXngQlA11eoA==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-http@3.972.13': - resolution: {integrity: sha512-a864QxQWFkdCZ5wQF0QZNKTbqAc/DFQNeARp4gOyZZdql5RHjj4CppUSfwAzS9cpw2IPY3eeJjWqLZ1QiDB/6w==} + '@aws-sdk/credential-provider-web-identity@3.972.12': + resolution: {integrity: sha512-yluBahBVsduoA/zgV0NAXtwwXvQ6tNn95dNA3Hg+vISdiPWA46QY0d9PLO2KpNbjtm+1oGcWxemS4fYTwJ0W1w==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-ini@3.972.11': - resolution: {integrity: sha512-kvPFn626ABLzxmjFMoqMRtmFKMeiUdWPhwxhmuPu233tqHnNuXzHv0MtrZlkzHd+rwlh9j0zCbQo89B54wIazQ==} + '@aws-sdk/eventstream-handler-node@3.972.8': + resolution: {integrity: sha512-tVrf8X7hKnqv3HyVraUbsQW5mfHlD++S5NSIbfQEx0sCRvIwUbTPDl/lJCxhNmZ2zjgUyBIXIKrWilFWBxzv+w==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-ini@3.972.9': - resolution: {integrity: sha512-zr1csEu9n4eDiHMTYJabX1mDGuGLgjgUnNckIivvk43DocJC9/f6DefFrnUPZXE+GHtbW50YuXb+JIxKykU74A==} + '@aws-sdk/middleware-eventstream@3.972.5': + resolution: {integrity: sha512-j8sFerTrzS9tEJhiW2k+T9hsELE+13D5H+mqMjTRyPSgAOebkiK9d4t8vjbLOXuk7yi5lop40x15MubgcjpLmQ==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-login@3.972.11': - resolution: {integrity: sha512-stdy09EpBTmsxGiXe1vB5qtXNww9wact36/uWLlSV0/vWbCOUAY2JjhPXoDVLk8n+E6r0M5HeZseLk+iTtifxg==} + '@aws-sdk/middleware-host-header@3.972.5': + resolution: {integrity: sha512-dVA0m1cEQ2iA6yB19aHvWNeUVTuvTt3AXzT0aiIu2uxk0S7AcmwDCDaRgYa/v+eFHcJVxEnpYTozqA7X62xinw==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-login@3.972.9': - resolution: {integrity: sha512-m4RIpVgZChv0vWS/HKChg1xLgZPpx8Z+ly9Fv7FwA8SOfuC6I3htcSaBz2Ch4bneRIiBUhwP4ziUo0UZgtJStQ==} + '@aws-sdk/middleware-logger@3.972.5': + resolution: {integrity: sha512-03RqplLZjUTkYi0dDPR/bbOLnDLFNdaVvNENgA3XK7Ph1MhEBhUYlgoGfOyRAKApDZ+WG4ykOoA8jI8J04jmFA==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-node@3.972.10': - resolution: {integrity: sha512-70nCESlvnzjo4LjJ8By8MYIiBogkYPSXl3WmMZfH9RZcB/Nt9qVWbFpYj6Fk1vLa4Vk8qagFVeXgxdieMxG1QA==} + '@aws-sdk/middleware-recursion-detection@3.972.5': + resolution: {integrity: sha512-2QSuuVkpHTe84+mDdnFjHX8rAP3g0yYwLVAhS3lQN1rW5Z/zNsf8/pYQrLjLO4n4sPCsUAkTa0Vrod0lk+o1Tg==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-node@3.972.12': - resolution: {integrity: sha512-gMWGnHbNSKWRj+PAiuSg0EDpEwpyIgk0v9U6EuZ1C/5/BUv25Way+E+UFB7r+YYkscuBJMJ+ai8E2K0Q8dx50g==} + '@aws-sdk/middleware-user-agent@3.972.14': + resolution: {integrity: sha512-PzDz+yRAQuIzd+4ZY3s6/TYRzlNKAn4Gae3E5uLV7NnYHqrZHFoAfKE4beXcu3C51pA2/FQ3X2qOGSYqUoN1WQ==} engines: {node: '>=20.0.0'} - '@aws-sdk/credential-provider-process@3.972.11': - resolution: {integrity: sha512-B049fvbv41vf0Fs5bCtbzHpruBDp61sPiFDxUmkAJ/zvgSAturpj2rqzV1rj2clg4mb44Uxp9rgpcODexNFlFA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/credential-provider-process@3.972.9': - resolution: {integrity: sha512-gOWl0Fe2gETj5Bk151+LYKpeGi2lBDLNu+NMNpHRlIrKHdBmVun8/AalwMK8ci4uRfG5a3/+zvZBMpuen1SZ0A==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/credential-provider-sso@3.972.11': - resolution: {integrity: sha512-vX9z8skN8vPtamVWmSCm4KQohub+1uMuRzIo4urZ2ZUMBAl1bqHatVD/roCb3qRfAyIGvZXCA/AWS03BQRMyCQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/credential-provider-sso@3.972.9': - resolution: {integrity: sha512-ey7S686foGTArvFhi3ifQXmgptKYvLSGE2250BAQceMSXZddz7sUSNERGJT2S7u5KIe/kgugxrt01hntXVln6w==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/credential-provider-web-identity@3.972.11': - resolution: {integrity: sha512-VR2Ju/QBdOjnWNIYuxRml63eFDLGc6Zl8aDwLi1rzgWo3rLBgtaWhWVBAijhVXzyPdQIOqdL8hvll5ybqumjeQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/credential-provider-web-identity@3.972.9': - resolution: {integrity: sha512-8LnfS76nHXoEc9aRRiMMpxZxJeDG0yusdyo3NvPhCgESmBUgpMa4luhGbClW5NoX/qRcGxxM6Z/esqANSNMTow==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/eventstream-handler-node@3.972.5': - resolution: {integrity: sha512-xEmd3dnyn83K6t4AJxBJA63wpEoCD45ERFG0XMTViD2E/Ohls9TLxjOWPb1PAxR9/46cKy/TImez1GoqP6xVNQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/eventstream-handler-node@3.972.7': - resolution: {integrity: sha512-p8k2ZWKJVrR3KIcBbI+/+FcWXdwe3LLgGnixsA7w8lDwWjzSVDHFp6uPeSqBt5PQpRxzak9EheJ1xTmOnHGf4g==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-eventstream@3.972.3': - resolution: {integrity: sha512-pbvZ6Ye/Ks6BAZPa3RhsNjHrvxU9li25PMhSdDpbX0jzdpKpAkIR65gXSNKmA/REnSdEMWSD4vKUW+5eMFzB6w==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-eventstream@3.972.4': - resolution: {integrity: sha512-0t+2Dn46cRE9iu5ynUXINBtR0wNHi/Jz3FbrqS5k3dGot2O7Ln1xCqXbJUAtGM5ZAqN77SbnpETAgVWC84DeoA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-host-header@3.972.3': - resolution: {integrity: sha512-aknPTb2M+G3s+0qLCx4Li/qGZH8IIYjugHMv15JTYMe6mgZO8VBpYgeGYsNMGCqCZOcWzuf900jFBG5bopfzmA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-host-header@3.972.4': - resolution: {integrity: sha512-4q2Vg7/zOB10huDBLjzzTwVjBpG22X3J3ief2XrJEgTaANZrNfA3/cGbCVNAibSbu/nIYA7tDk8WCdsIzDDc4Q==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-logger@3.972.3': - resolution: {integrity: sha512-Ftg09xNNRqaz9QNzlfdQWfpqMCJbsQdnZVJP55jfhbKi1+FTWxGuvfPoBhDHIovqWKjqbuiew3HuhxbJ0+OjgA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-logger@3.972.4': - resolution: {integrity: sha512-xFqPvTysuZAHSkdygT+ken/5rzkR7fhOoDPejAJQslZpp0XBepmCJnDOqA57ERtCTBpu8wpjTFI1ETd4S0AXEw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-recursion-detection@3.972.3': - resolution: {integrity: sha512-PY57QhzNuXHnwbJgbWYTrqIDHYSeOlhfYERTAuc16LKZpTZRJUjzBFokp9hF7u1fuGeE3D70ERXzdbMBOqQz7Q==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-recursion-detection@3.972.4': - resolution: {integrity: sha512-tVbRaayUZ7y2bOb02hC3oEPTqQf2A0HpPDwdMl1qTmye/q8Mq1F1WiIoFkQwG/YQFvbyErYIDMbYzIlxzzLtjQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-user-agent@3.972.11': - resolution: {integrity: sha512-R8CvPsPHXwzIHCAza+bllY6PrctEk4lYq/SkHJz9NLoBHCcKQrbOcsfXxO6xmipSbUNIbNIUhH0lBsJGgsRdiw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-user-agent@3.972.13': - resolution: {integrity: sha512-p1kVYbzBxRmhuOHoL/ANJPCedqUxnVgkEjxPoxt5pQv/yzppHM7aBWciYEE9TZY59M421D3GjLfZIZBoEFboVQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/middleware-websocket@3.972.6': - resolution: {integrity: sha512-1DedO6N3m8zQ/vG6twNiHtsdwBgk773VdavLEbB3NXeKZDlzSK1BTviqWwvJdKx5UnIy4kGGP6WWpCEFEt/bhQ==} + '@aws-sdk/middleware-websocket@3.972.9': + resolution: {integrity: sha512-O+FSwU9UvKd+QNuGLHqvmP33kkH4jh8pAgdMo3wbFLf+u30fS9/2gbSSWWtNCcWkSNFyG6RUlKU7jPSLApFfGw==} engines: {node: '>= 14.0.0'} - '@aws-sdk/middleware-websocket@3.972.8': - resolution: {integrity: sha512-KPUXz8lRw73Rh12/QkELxiryC9Wi9Ah1xNzFe2Vtbz2/81c2ZA0yM8er+u0iCF/SRMMhDQshLcmRNgn/ueA+gA==} - engines: {node: '>= 14.0.0'} - - '@aws-sdk/nested-clients@3.993.0': - resolution: {integrity: sha512-iOq86f2H67924kQUIPOAvlmMaOAvOLoDOIb66I2YqSUpMYB6ufiuJW3RlREgskxv86S5qKzMnfy/X6CqMjK6XQ==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/nested-clients@3.995.0': - resolution: {integrity: sha512-7gq9gismVhESiRsSt0eYe1y1b6jS20LqLk+e/YSyPmGi9yHdndHQLIq73RbEJnK/QPpkQGFqq70M1mI46M1HGw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/nested-clients@3.996.1': - resolution: {integrity: sha512-XHVLFRGkuV2gh2uwBahCt65ALMb5wMpqplXEZIvFnWOCPlk60B7h7M5J9Em243K8iICDiWY6KhBEqVGfjTqlLA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/region-config-resolver@3.972.3': - resolution: {integrity: sha512-v4J8qYAWfOMcZ4MJUyatntOicTzEMaU7j3OpkRCGGFSL2NgXQ5VbxauIyORA+pxdKZ0qQG2tCQjQjZDlXEC3Ow==} + '@aws-sdk/nested-clients@3.996.2': + resolution: {integrity: sha512-W+u6EM8WRxOIhAhR2mXMHSaUygqItpTehkgxLwJngXqr9RlAR4t6CtECH7o7QK0ct3oyi5Z8ViDHtPbel+D2Rg==} engines: {node: '>=20.0.0'} - '@aws-sdk/region-config-resolver@3.972.4': - resolution: {integrity: sha512-3GrJYv5eI65oCKveBZP7Q246dVP+tqeys9aKMB0dfX1glUWfppWlxIu52derqdNb9BX9lxYmeiaBcBIqOAYSgQ==} + '@aws-sdk/region-config-resolver@3.972.5': + resolution: {integrity: sha512-AOitrygDwfTNCLCW7L+GScDy1p49FZ6WutTUFWROouoPetfVNmpL4q8TWD3MhfY/ynhoGhleUQENrBH374EU8w==} engines: {node: '>=20.0.0'} - '@aws-sdk/token-providers@3.993.0': - resolution: {integrity: sha512-+35g4c+8r7sB9Sjp1KPdM8qxGn6B/shBjJtEUN4e+Edw9UEQlZKIzioOGu3UAbyE0a/s450LdLZr4wbJChtmww==} + '@aws-sdk/token-providers@3.998.0': + resolution: {integrity: sha512-JFzi44tQnENZQ+1DYcHfoa/wTRKkccz0VsNMow0rvsxZtqUEkeV2pYFbir35mHTyUKju9995ay1MAGxLt1dpRA==} engines: {node: '>=20.0.0'} - '@aws-sdk/token-providers@3.995.0': - resolution: {integrity: sha512-lYSadNdZZ513qCKoj/KlJ+PgCycL3n8ZNS37qLVFC0t7TbHzoxvGquu9aD2n9OCERAn43OMhQ7dXjYDYdjAXzA==} + '@aws-sdk/types@3.973.3': + resolution: {integrity: sha512-tma6D8/xHZHJEUqmr6ksZjZ0onyIUqKDQLyp50ttZJmS0IwFYzxBgp5CxFvpYAnah52V3UtgrqGA6E83gtT7NQ==} engines: {node: '>=20.0.0'} - '@aws-sdk/token-providers@3.997.0': - resolution: {integrity: sha512-UdG36F7lU9aTqGFRieEyuRUJlgEJBqKeKKekC0esH21DbUSKhPR1kZBah214kYasIaWe1hLJLaqUigoTa5hZAQ==} + '@aws-sdk/util-endpoints@3.996.2': + resolution: {integrity: sha512-83E6T1CKi0/IozPzqRBKqduW0mS4UQdI3soBH6CG7UgupTADWunqEMOTuPWCs9XGjpJJ4ujj+yu7pn8svhp5yg==} engines: {node: '>=20.0.0'} - '@aws-sdk/types@3.973.1': - resolution: {integrity: sha512-DwHBiMNOB468JiX6+i34c+THsKHErYUdNQ3HexeXZvVn4zouLjgaS4FejiGSi2HyBuzuyHg7SuOPmjSvoU9NRg==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/types@3.973.2': - resolution: {integrity: sha512-maTZwGsALtnAw4TJr/S6yERAosTwPduu0XhUV+SdbvRZtCOgSgk1ttL2R0XYzvkYSpvbtJocn77tBXq2AKglBw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/util-endpoints@3.993.0': - resolution: {integrity: sha512-j6vioBeRZ4eHX4SWGvGPpwGg/xSOcK7f1GL0VM+rdf3ZFTIsUEhCFmD78B+5r2PgztcECSzEfvHQX01k8dPQPw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/util-endpoints@3.995.0': - resolution: {integrity: sha512-aym/pjB8SLbo9w2nmkrDdAAVKVlf7CM71B9mKhjDbJTzwpSFBPHqJIMdDyj0mLumKC0aIVDr1H6U+59m9GvMFw==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/util-endpoints@3.996.1': - resolution: {integrity: sha512-7cJyd+M5i0IoqWkJa1KFx8KNCGIx+Ywu+lT53KpqX7ReVwz03DCKUqvZ/y65vdKwo9w9/HptSAeLDluO5MpGIg==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/util-format-url@3.972.3': - resolution: {integrity: sha512-n7F2ycckcKFXa01vAsT/SJdjFHfKH9s96QHcs5gn8AaaigASICeME8WdUL9uBp8XV/OVwEt8+6gzn6KFUgQa8g==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/util-format-url@3.972.4': - resolution: {integrity: sha512-rPm9g4WvgTz4ko5kqseIG5Vp5LUAbWBBDalm4ogHLMc0i20ChwQWqwuTUPJSu8zXn43jIM0xO2KZaYQsFJb+ew==} + '@aws-sdk/util-format-url@3.972.5': + resolution: {integrity: sha512-PccfrPQVOEQSL8xaSvu988ESMlqdH1Qfk3AWPZksCOYPHyzYeUV988E+DBachXNV7tBVTUvK85cZYEZu7JtPxQ==} engines: {node: '>=20.0.0'} '@aws-sdk/util-locate-window@3.965.4': resolution: {integrity: sha512-H1onv5SkgPBK2P6JR2MjGgbOnttoNzSPIRoeZTNPZYyaplwGg50zS3amXvXqF0/qfXpWEC9rLWU564QTB9bSog==} engines: {node: '>=20.0.0'} - '@aws-sdk/util-user-agent-browser@3.972.3': - resolution: {integrity: sha512-JurOwkRUcXD/5MTDBcqdyQ9eVedtAsZgw5rBwktsPTN7QtPiS2Ld1jkJepNgYoCufz1Wcut9iup7GJDoIHp8Fw==} - - '@aws-sdk/util-user-agent-browser@3.972.4': - resolution: {integrity: sha512-GHb+8XHv6hfLWKQKAKaSOm+vRvogg07s+FWtbR3+eCXXPSFn9XVmiYF4oypAxH7dGIvoxkVG/buHEnzYukyJiA==} + '@aws-sdk/util-user-agent-browser@3.972.5': + resolution: {integrity: sha512-2ja1WqtuBaEAMgVoHYuWx393DF6ULqdt3OozeO7BosqouYaoU47Adtp9vEF+GImSG/Q8A+dqfwDULTTdMkHGUQ==} - '@aws-sdk/util-user-agent-node@3.972.10': - resolution: {integrity: sha512-LVXzICPlsheET+sE6tkcS47Q5HkSTrANIlqL1iFxGAY/wRQ236DX/PCAK56qMh9QJoXAfXfoRW0B0Og4R+X7Nw==} + '@aws-sdk/util-user-agent-node@3.972.13': + resolution: {integrity: sha512-PHErmuu+v6iAST48zcsB2cYwDKW45gk6qCp49t1p0NGZ4EaFPr/tA5jl0X/ekDwvWbuT0LTj++fjjdVQAbuh0Q==} engines: {node: '>=20.0.0'} peerDependencies: aws-crt: '>=1.0.0' @@ -759,21 +644,8 @@ packages: aws-crt: optional: true - '@aws-sdk/util-user-agent-node@3.972.12': - resolution: {integrity: sha512-c1n3wBK6te+Vd9qU86nF8AsYuiBsxLn0AADGWyFX7vEADr3btaAg5iPQT6GYj6rvzSOEVVisvaAatOWInlJUbQ==} - engines: {node: '>=20.0.0'} - peerDependencies: - aws-crt: '>=1.0.0' - peerDependenciesMeta: - aws-crt: - optional: true - - '@aws-sdk/xml-builder@3.972.5': - resolution: {integrity: sha512-mCae5Ys6Qm1LDu0qdGwx2UQ63ONUe+FHw908fJzLDqFKTDBK4LDZUqKWm4OkTCNFq19bftjsBSESIGLD/s3/rA==} - engines: {node: '>=20.0.0'} - - '@aws-sdk/xml-builder@3.972.6': - resolution: {integrity: sha512-YrXu+UnfC8IdARa4ZkrpcyuRmA/TVgYW6Lcdtvi34NQgRjM1hTirNirN+rGb+s/kNomby8oJiIAu0KNbiZC7PA==} + '@aws-sdk/xml-builder@3.972.7': + resolution: {integrity: sha512-9GF86s6mHuc1TYCbuKatMDWl2PyK3KIkpRaI7ul2/gYZPfaLzKZ+ISHhxzVb9KVeakf75tUQe6CXW2gugSCXNw==} engines: {node: '>=20.0.0'} '@aws/lambda-invoke-store@0.2.3': @@ -792,12 +664,12 @@ packages: resolution: {integrity: sha512-XPArKLzsvl0Hf0CaGyKHUyVgF7oDnhKoP85Xv6M4StF/1AhfORhZudHtOyf2s+FcbuQ9dPRAjB8J2KvRRMUK2A==} engines: {node: '>=20.0.0'} - '@azure/msal-common@16.0.4': - resolution: {integrity: sha512-0KZ9/wbUyZN65JLAx5bGNfWjkD0kRMUgM99oSpZFg7wEOb3XcKIiHrFnIpgyc8zZ70fHodyh8JKEOel1oN24Gw==} + '@azure/msal-common@16.1.0': + resolution: {integrity: sha512-uiX0ChrRFbreXlPlDR8LwHKmZpJudDAr124iNWJKJ+b7MJUWXmvVU3idSi/c5lk1FwLVZeMxhQir3BGdV09I+g==} engines: {node: '>=0.8.0'} - '@azure/msal-node@5.0.4': - resolution: {integrity: sha512-WbA77m68noCw4qV+1tMm5nodll34JCDF0KmrSrp9LskS0bGbgHt98ZRxq69BQK5mjMqDD5ThHJOrrGSfzPybxw==} + '@azure/msal-node@5.0.5': + resolution: {integrity: sha512-CxUYSZgFiviUC3d8Hc+tT7uxre6QkPEWYEHWXmyEBzaO6tfFY4hs5KbXWU6s4q9Zv1NP/04qiR3mcujYLRuYuw==} engines: {node: '>=20'} '@babel/generator@8.0.0-rc.1': @@ -1492,26 +1364,21 @@ packages: resolution: {integrity: sha512-faGUlTcXka5l7rv0lP3K3vGW/ejRuOS24RR2aSFWREUQqzjgdsuWNo/IiPqL3kWRGt6Ahl2+qcDAwtdeWeuGUw==} hasBin: true - '@mariozechner/pi-agent-core@0.54.1': - resolution: {integrity: sha512-AC0SqEbR62PckWOyP0CmhYtfcC+Q6e1DGghwEcKpomTtmNfHTy7iTVy64mmtB2CFiN8j4rJFCqh2xJHgucUvkA==} - engines: {node: '>=20.0.0'} - '@mariozechner/pi-agent-core@0.55.0': resolution: {integrity: sha512-8RLaOpmESBSqTSpA/6E9ihxYybhrkNa5LOYNdJst57LuDSDytfvkiTXlKA4DjsHua4PKopG9p0Wgqaem+kKvCA==} engines: {node: '>=20.0.0'} - '@mariozechner/pi-ai@0.54.1': - resolution: {integrity: sha512-tiVvoNQV+3dpWgRQ1U/3bwJoDVSYwL17BE/kc00nXmaSLAPwNZoxLagtQ+HBr/rGzkq5viOgQf2dk+ud+/4UCg==} + '@mariozechner/pi-agent-core@0.55.1': + resolution: {integrity: sha512-t9FAb4ouy8HJSIa8gSRC7j8oeUOb2XDdhvBiHj7FhfpYafj1vRPrvGIEXUV8fPJDCI07vhK9iztP27EPk+yEWw==} engines: {node: '>=20.0.0'} - hasBin: true '@mariozechner/pi-ai@0.55.0': resolution: {integrity: sha512-G5rutF5h1hFZgU1W2yYktZJegKUZVDhdGCxvl7zPOonrGBczuNBKmM87VXvl1m+t9718rYMsgTSBseGN0RhYug==} engines: {node: '>=20.0.0'} hasBin: true - '@mariozechner/pi-coding-agent@0.54.1': - resolution: {integrity: sha512-pPFrdaKZ16oIcdhZVcfWPhCDFx8PWHaACjQS9aFFcMOhLBduyKAGyf8bQtfysekl+gIbBSGDT2rgCxsOwK2bQw==} + '@mariozechner/pi-ai@0.55.1': + resolution: {integrity: sha512-JJX1LrVWPUPMExu0f89XR4nMNP37+FNLjEE4cIHq9Hi6xQtOiiEi7OjDFMx58hWsq81xH1CwmQXqGTWBjbXKTw==} engines: {node: '>=20.0.0'} hasBin: true @@ -1520,14 +1387,19 @@ packages: engines: {node: '>=20.0.0'} hasBin: true - '@mariozechner/pi-tui@0.54.1': - resolution: {integrity: sha512-FY8QcLlr9T276oZAwMSSPo1drg+J9Y7B+A0S9g8Jh6IFJxymKZZq29/Vit6XDziJfZIgJDraC6lpobtxgTEoFQ==} + '@mariozechner/pi-coding-agent@0.55.1': + resolution: {integrity: sha512-H2M8mbBNyDqhON6+3m4H8CjqJ9taGq/CM3B8dG73+VJJIXFm5SExhU9bdgcw2xh0wWj8yEumsj0of6Tu+F7Ffg==} engines: {node: '>=20.0.0'} + hasBin: true '@mariozechner/pi-tui@0.55.0': resolution: {integrity: sha512-qFdBsA0CTIQbUlN5hp1yJOSgJJiuTegx+oNPzpHxaMMBPjwMuh3Y8szBqE/2HxroA6mGSQfp/fzuPinTK1+Iyg==} engines: {node: '>=20.0.0'} + '@mariozechner/pi-tui@0.55.1': + resolution: {integrity: sha512-rnqDUp2fm/ySevC0Ltj/ZFRbEc1kZ1A4qHESejj9hA8NVrb/pX9g82XwTE762JOieEGrRWAtmHLNOm7/e4dJMw==} + engines: {node: '>=20.0.0'} + '@matrix-org/matrix-sdk-crypto-nodejs@0.4.0': resolution: {integrity: sha512-+qqgpn39XFSbsD0dFjssGO9vHEP7sTyfs8yTpt8vuqWpUpF20QMwpCZi0jpYw7GxjErNTsMshopuo8677DfGEA==} engines: {node: '>= 22'} @@ -1547,144 +1419,74 @@ packages: resolution: {integrity: sha512-juG5VWh4qAivzTAeMzvY9xs9HY5rAcr2E4I7tiSSCokRFi7XIZCAu92ZkSTsIj1OPceCifL3cpfteP3pDT9/QQ==} engines: {node: '>=14.0.0'} - '@napi-rs/canvas-android-arm64@0.1.92': - resolution: {integrity: sha512-rDOtq53ujfOuevD5taxAuIFALuf1QsQWZe1yS/N4MtT+tNiDBEdjufvQRPWZ11FubL2uwgP8ApYU3YOaNu1ZsQ==} + '@napi-rs/canvas-android-arm64@0.1.95': + resolution: {integrity: sha512-SqTh0wsYbetckMXEvHqmR7HKRJujVf1sYv1xdlhkifg6TlCSysz1opa49LlS3+xWuazcQcfRfmhA07HxxxGsAA==} engines: {node: '>= 10'} cpu: [arm64] os: [android] - '@napi-rs/canvas-android-arm64@0.1.94': - resolution: {integrity: sha512-YQ6K83RWNMQOtgpk1aIML97QTE3zxPmVCHTi5eA8Nss4+B9JZi5J7LHQr7B5oD7VwSfWd++xsPdUiJ1+frqsMg==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [android] - - '@napi-rs/canvas-darwin-arm64@0.1.92': - resolution: {integrity: sha512-4PT6GRGCr7yMRehp42x0LJb1V0IEy1cDZDDayv7eKbFUIGbPFkV7CRC9Bee5MPkjg1EB4ZPXXUyy3gjQm7mR8Q==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [darwin] - - '@napi-rs/canvas-darwin-arm64@0.1.94': - resolution: {integrity: sha512-h1yl9XjqSrYZAbBUHCVLAhwd2knM8D8xt081Pv40KqNJXfeMmBrhG1SfroRymG2ak+pl42iQlWjFZ2Z8AWFdSw==} + '@napi-rs/canvas-darwin-arm64@0.1.95': + resolution: {integrity: sha512-F7jT0Syu+B9DGBUBcMk3qCRIxAWiDXmvEjamwbYfbZl7asI1pmXZUnCOoIu49Wt0RNooToYfRDxU9omD6t5Xuw==} engines: {node: '>= 10'} cpu: [arm64] os: [darwin] - '@napi-rs/canvas-darwin-x64@0.1.92': - resolution: {integrity: sha512-5e/3ZapP7CqPtDcZPtmowCsjoyQwuNMMD7c0GKPtZQ8pgQhLkeq/3fmk0HqNSD1i227FyJN/9pDrhw/UMTkaWA==} + '@napi-rs/canvas-darwin-x64@0.1.95': + resolution: {integrity: sha512-54eb2Ho15RDjYGXO/harjRznBrAvu+j5nQ85Z4Qd6Qg3slR8/Ja+Yvvy9G4yo7rdX6NR9GPkZeSTf2UcKXwaXw==} engines: {node: '>= 10'} cpu: [x64] os: [darwin] - '@napi-rs/canvas-darwin-x64@0.1.94': - resolution: {integrity: sha512-rkr/lrafbU0IIHebst+sQJf1HjdHvTMN0GGqWvw5OfaVS0K/sVxhNHtxi8oCfaRSvRE62aJZjWTcdc2ue/o6yw==} - engines: {node: '>= 10'} - cpu: [x64] - os: [darwin] - - '@napi-rs/canvas-linux-arm-gnueabihf@0.1.92': - resolution: {integrity: sha512-j6KaLL9iir68lwpzzY+aBGag1PZp3+gJE2mQ3ar4VJVmyLRVOh+1qsdNK1gfWoAVy5w6U7OEYFrLzN2vOFUSng==} - engines: {node: '>= 10'} - cpu: [arm] - os: [linux] - - '@napi-rs/canvas-linux-arm-gnueabihf@0.1.94': - resolution: {integrity: sha512-q95TDo32YkTKdi+Sp2yQ2Npm7pmfKEruNoJ3RUIw1KvQQ9EHKL3fii/iuU60tnzP0W+c8BKN7BFstNFcm2KXCQ==} + '@napi-rs/canvas-linux-arm-gnueabihf@0.1.95': + resolution: {integrity: sha512-hYaLCSLx5bmbnclzQc3ado3PgZ66blJWzjXp0wJmdwpr/kH+Mwhj6vuytJIomgksyJoCdIqIa4N6aiqBGJtJ5Q==} engines: {node: '>= 10'} cpu: [arm] os: [linux] - '@napi-rs/canvas-linux-arm64-gnu@0.1.92': - resolution: {integrity: sha512-s3NlnJMHOSotUYVoTCoC1OcomaChFdKmZg0VsHFeIkeHbwX0uPHP4eCX1irjSfMykyvsGHTQDfBAtGYuqxCxhQ==} + '@napi-rs/canvas-linux-arm64-gnu@0.1.95': + resolution: {integrity: sha512-J7VipONahKsmScPZsipHVQBqpbZx4favaD8/enWzzlGcjiwycOoymL7f4tNeqdjK0su19bDOUt6mjp9gsPWYlw==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - '@napi-rs/canvas-linux-arm64-gnu@0.1.94': - resolution: {integrity: sha512-Je5/gKVybWAoIGyDOcJF1zYgBTKWkPIkfOgvCzrQcl8h7DiDvRvEY70EapA+NicGe4X3DW9VsCT34KZJnerShA==} + '@napi-rs/canvas-linux-arm64-musl@0.1.95': + resolution: {integrity: sha512-PXy0UT1J/8MPG8UAkWp6Fd51ZtIZINFzIjGH909JjQrtCuJf3X6nanHYdz1A+Wq9o4aoPAw1YEUpFS1lelsVlg==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - '@napi-rs/canvas-linux-arm64-musl@0.1.92': - resolution: {integrity: sha512-xV0GQnukYq5qY+ebkAwHjnP2OrSGBxS3vSi1zQNQj0bkXU6Ou+Tw7JjCM7pZcQ28MUyEBS1yKfo7rc7ip2IPFQ==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [linux] - - '@napi-rs/canvas-linux-arm64-musl@0.1.94': - resolution: {integrity: sha512-9YleDDauDEZNsFnfz3HyZvp1LK1ECu8N2gDUg1wtL7uWLQv8dUbfVeFtp5HOdxht1o7LsWRmQeqeIbnD4EqE2A==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [linux] - - '@napi-rs/canvas-linux-riscv64-gnu@0.1.92': - resolution: {integrity: sha512-+GKvIFbQ74eB/TopEdH6XIXcvOGcuKvCITLGXy7WLJAyNp3Kdn1ncjxg91ihatBaPR+t63QOE99yHuIWn3UQ9w==} - engines: {node: '>= 10'} - cpu: [riscv64] - os: [linux] - - '@napi-rs/canvas-linux-riscv64-gnu@0.1.94': - resolution: {integrity: sha512-lQUy9Xvz7ch8+0AXq8RkioLD41iQ6EqdKFu5uV40BxkBDijB2SCm1jna/BRhqitQRSjwAk2KlLUxTjHChyfNGg==} + '@napi-rs/canvas-linux-riscv64-gnu@0.1.95': + resolution: {integrity: sha512-2IzCkW2RHRdcgF9W5/plHvYFpc6uikyjMb5SxjqmNxfyDFz9/HB89yhi8YQo0SNqrGRI7yBVDec7Pt+uMyRWsg==} engines: {node: '>= 10'} cpu: [riscv64] os: [linux] - '@napi-rs/canvas-linux-x64-gnu@0.1.92': - resolution: {integrity: sha512-tFd6MwbEhZ1g64iVY2asV+dOJC+GT3Yd6UH4G3Hp0/VHQ6qikB+nvXEULskFYZ0+wFqlGPtXjG1Jmv7sJy+3Ww==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - '@napi-rs/canvas-linux-x64-gnu@0.1.94': - resolution: {integrity: sha512-0IYgyuUaugHdWxXRhDQUCMxTou8kAHHmpIBFtbmdRlciPlfK7AYQW5agvUU1PghPc5Ja3Zzp5qZfiiLu36vIWQ==} + '@napi-rs/canvas-linux-x64-gnu@0.1.95': + resolution: {integrity: sha512-OV/ol/OtcUr4qDhQg8G7SdViZX8XyQeKpPsVv/j3+7U178FGoU4M+yIocdVo1ih/A8GQ63+LjF4jDoEjaVU8Pw==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - '@napi-rs/canvas-linux-x64-musl@0.1.92': - resolution: {integrity: sha512-uSuqeSveB/ZGd72VfNbHCSXO9sArpZTvznMVsb42nqPP7gBGEH6NJQ0+hmF+w24unEmxBhPYakP/Wiosm16KkA==} + '@napi-rs/canvas-linux-x64-musl@0.1.95': + resolution: {integrity: sha512-Z5KzqBK/XzPz5+SFHKz7yKqClEQ8pOiEDdgk5SlphBLVNb8JFIJkxhtJKSvnJyHh2rjVgiFmvtJzMF0gNwwKyQ==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - '@napi-rs/canvas-linux-x64-musl@0.1.94': - resolution: {integrity: sha512-xuetfzzcflCIiBw2HJlOU4/+zTqhdxoe1BEcwdBsHAd/5wAQ4Pp+FGPi5g74gDvtcXQmTdEU3fLQvHc/j3wbxQ==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - '@napi-rs/canvas-win32-arm64-msvc@0.1.92': - resolution: {integrity: sha512-20SK5AU/OUNz9ZuoAPj5ekWai45EIBDh/XsdrVZ8le/pJVlhjFU3olbumSQUXRFn7lBRS+qwM8kA//uLaDx6iQ==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [win32] - - '@napi-rs/canvas-win32-arm64-msvc@0.1.94': - resolution: {integrity: sha512-2F3p8wci4Q4vjbENlQtSibqFWxBdpzYk1c8Jh1mqqLE92rBKElG018dBJ6C8Dp49vE350Hmy5LrfdLgFKMG8sg==} + '@napi-rs/canvas-win32-arm64-msvc@0.1.95': + resolution: {integrity: sha512-aj0YbRpe8qVJ4OzMsK7NfNQePgcf9zkGFzNZ9mSuaxXzhpLHmlF2GivNdCdNOg8WzA/NxV6IU4c5XkXadUMLeA==} engines: {node: '>= 10'} cpu: [arm64] os: [win32] - '@napi-rs/canvas-win32-x64-msvc@0.1.92': - resolution: {integrity: sha512-KEhyZLzq1MXCNlXybz4k25MJmHFp+uK1SIb8yJB0xfrQjz5aogAMhyseSzewo+XxAq3OAOdyKvfHGNzT3w1RPg==} - engines: {node: '>= 10'} - cpu: [x64] - os: [win32] - - '@napi-rs/canvas-win32-x64-msvc@0.1.94': - resolution: {integrity: sha512-hjwaIKMrQLoNiu3724octSGhDVKkBwJtMeQ3qUXOi+y60h2q6Sxq3+MM2za3V88+XQzzwn0DgG0Xo6v6gzV8kQ==} + '@napi-rs/canvas-win32-x64-msvc@0.1.95': + resolution: {integrity: sha512-GA8leTTCfdjuHi8reICTIxU0081PhXvl3lzIniLUjeLACx9GubUiyzkwFb+oyeKLS5IAGZFLKnzAf4wm2epRlA==} engines: {node: '>= 10'} cpu: [x64] os: [win32] - '@napi-rs/canvas@0.1.92': - resolution: {integrity: sha512-q7ZaUCJkEU5BeOdE7fBx1XWRd2T5Ady65nxq4brMf5L4cE1VV/ACq5w9Z5b/IVJs8CwSSIwc30nlthH0gFo4Ig==} - engines: {node: '>= 10'} - - '@napi-rs/canvas@0.1.94': - resolution: {integrity: sha512-8jBkvqynXNdQPNZjLJxB/Rp9PdnnMSHFBLzPmMc615nlt/O6w0ergBbkEDEOr8EbjL8nRQDpEklPx4pzD7zrbg==} + '@napi-rs/canvas@0.1.95': + resolution: {integrity: sha512-lkg23ge+rgyhgUwXmlbkPEhuhHq/hUi/gXKH+4I7vO+lJrbNfEYcQdJLIGjKyXLQzgFiiyDAwh5vAe/tITAE+w==} engines: {node: '>= 10'} '@napi-rs/wasm-runtime@1.1.1': @@ -1815,8 +1617,8 @@ packages: resolution: {integrity: sha512-DhGl4xMVFGVIyMwswXeyzdL4uXD5OGILGX5N8Y+f6W7LhC1Ze2poSNrkF/fedpVDHEEZ+PHFW0vL14I+mm8K3Q==} engines: {node: '>= 20'} - '@octokit/endpoint@11.0.2': - resolution: {integrity: sha512-4zCpzP1fWc7QlqunZ5bSEjxc6yLAlRTnDwKtgXfcI/FxxGoqedDG8V2+xJ60bV2kODqcGB+nATdtap/XYq2NZQ==} + '@octokit/endpoint@11.0.3': + resolution: {integrity: sha512-FWFlNxghg4HrXkD3ifYbS/IdL/mDHjh9QcsNyhQjN8dplUoZbejsdpmuqdA76nxj2xoWPs7p8uX2SNr9rYu0Ag==} engines: {node: '>= 20'} '@octokit/graphql@9.0.3': @@ -1859,8 +1661,8 @@ packages: peerDependencies: '@octokit/core': '>=6' - '@octokit/plugin-retry@8.0.3': - resolution: {integrity: sha512-vKGx1i3MC0za53IzYBSBXcrhmd+daQDzuZfYDd52X5S0M2otf3kVZTVP8bLA3EkU0lTvd1WEC2OlNNa4G+dohA==} + '@octokit/plugin-retry@8.1.0': + resolution: {integrity: sha512-O1FZgXeiGb2sowEr/hYTr6YunGdSAFWnr2fyW39Ah85H8O33ELASQxcvOFF5LE6Tjekcyu2ms4qAzJVhSaJxTw==} engines: {node: '>= 20'} peerDependencies: '@octokit/core': '>=7' @@ -1875,8 +1677,8 @@ packages: resolution: {integrity: sha512-KMQIfq5sOPpkQYajXHwnhjCC0slzCNScLHs9JafXc4RAJI+9f+jNDlBNaIMTvazOPLgb4BnlhGJOTbnN0wIjPw==} engines: {node: '>= 20'} - '@octokit/request@10.0.7': - resolution: {integrity: sha512-v93h0i1yu4idj8qFPZwjehoJx4j3Ntn+JhXsdJrG9pYaX6j/XRz2RmasMUHtNgQD39nrv/VwTWSqK0RNXR8upA==} + '@octokit/request@10.0.8': + resolution: {integrity: sha512-SJZNwY9pur9Agf7l87ywFi14W+Hd9Jg6Ifivsd33+/bGUQIjNujdFiXII2/qSlN2ybqUHfp5xpekMEjIBTjlSw==} engines: {node: '>= 20'} '@octokit/types@16.0.0': @@ -2667,22 +2469,10 @@ packages: resolution: {integrity: sha512-qocxM/X4XGATqQtUkbE9SPUB6wekBi+FyJOMbPj0AhvyvFGYEmOlz6VB22iMePCQsFmMIvFSeViDvA7mZJG47g==} engines: {node: '>=18.0.0'} - '@smithy/abort-controller@4.2.8': - resolution: {integrity: sha512-peuVfkYHAmS5ybKxWcfraK7WBBP0J+rkfUcbHJJKQ4ir3UAUNQI+Y4Vt/PqSzGqgloJ5O1dk7+WzNL8wcCSXbw==} - engines: {node: '>=18.0.0'} - - '@smithy/config-resolver@4.4.6': - resolution: {integrity: sha512-qJpzYC64kaj3S0fueiu3kXm8xPrR3PcXDPEgnaNMRn0EjNSZFoFjvbUp0YUDsRhN1CB90EnHJtbxWKevnH99UQ==} - engines: {node: '>=18.0.0'} - '@smithy/config-resolver@4.4.9': resolution: {integrity: sha512-ejQvXqlcU30h7liR9fXtj7PIAau1t/sFbJpgWPfiYDs7zd16jpH0IsSXKcba2jF6ChTXvIjACs27kNMc5xxE2Q==} engines: {node: '>=18.0.0'} - '@smithy/core@3.23.2': - resolution: {integrity: sha512-HaaH4VbGie4t0+9nY3tNBRSxVTr96wzIqexUa6C2qx3MPePAuz7lIxPxYtt1Wc//SPfJLNoZJzfdt0B6ksj2jA==} - engines: {node: '>=18.0.0'} - '@smithy/core@3.23.6': resolution: {integrity: sha512-4xE+0L2NrsFKpEVFlFELkIHQddBvMbQ41LRIP74dGCXnY1zQ9DgksrBcRBDJT+iOzGy4VEJIeU3hkUK5mn06kg==} engines: {node: '>=18.0.0'} @@ -2691,82 +2481,42 @@ packages: resolution: {integrity: sha512-3bsMLJJLTZGZqVGGeBVFfLzuRulVsGTj12BzRKODTHqUABpIr0jMN1vN3+u6r2OfyhAQ2pXaMZWX/swBK5I6PQ==} engines: {node: '>=18.0.0'} - '@smithy/credential-provider-imds@4.2.8': - resolution: {integrity: sha512-FNT0xHS1c/CPN8upqbMFP83+ul5YgdisfCfkZ86Jh2NSmnqw/AJ6x5pEogVCTVvSm7j9MopRU89bmDelxuDMYw==} - engines: {node: '>=18.0.0'} - '@smithy/eventstream-codec@4.2.10': resolution: {integrity: sha512-A4ynrsFFfSXUHicfTcRehytppFBcY3HQxEGYiyGktPIOye3Ot7fxpiy4VR42WmtGI4Wfo6OXt/c1Ky1nUFxYYQ==} engines: {node: '>=18.0.0'} - '@smithy/eventstream-codec@4.2.8': - resolution: {integrity: sha512-jS/O5Q14UsufqoGhov7dHLOPCzkYJl9QDzusI2Psh4wyYx/izhzvX9P4D69aTxcdfVhEPhjK+wYyn/PzLjKbbw==} - engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-browser@4.2.10': resolution: {integrity: sha512-0xupsu9yj9oDVuQ50YCTS9nuSYhGlrwqdaKQel9y2Fz7LU9fNErVlw9N0o4pm4qqvWEGbSTI4HKc6XJfB30MVw==} engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-browser@4.2.8': - resolution: {integrity: sha512-MTfQT/CRQz5g24ayXdjg53V0mhucZth4PESoA5IhvaWVDTOQLfo8qI9vzqHcPsdd2v6sqfTYqF5L/l+pea5Uyw==} - engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-config-resolver@4.3.10': resolution: {integrity: sha512-8kn6sinrduk0yaYHMJDsNuiFpXwQwibR7n/4CDUqn4UgaG+SeBHu5jHGFdU9BLFAM7Q4/gvr9RYxBHz9/jKrhA==} engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-config-resolver@4.3.8': - resolution: {integrity: sha512-ah12+luBiDGzBruhu3efNy1IlbwSEdNiw8fOZksoKoWW1ZHvO/04MQsdnws/9Aj+5b0YXSSN2JXKy/ClIsW8MQ==} - engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-node@4.2.10': resolution: {integrity: sha512-uUrxPGgIffnYfvIOUmBM5i+USdEBRTdh7mLPttjphgtooxQ8CtdO1p6K5+Q4BBAZvKlvtJ9jWyrWpBJYzBKsyQ==} engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-node@4.2.8': - resolution: {integrity: sha512-cYpCpp29z6EJHa5T9WL0KAlq3SOKUQkcgSoeRfRVwjGgSFl7Uh32eYGt7IDYCX20skiEdRffyDpvF2efEZPC0A==} - engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-universal@4.2.10': resolution: {integrity: sha512-aArqzOEvcs2dK+xQVCgLbpJQGfZihw8SD4ymhkwNTtwKbnrzdhJsFDKuMQnam2kF69WzgJYOU5eJlCx+CA32bw==} engines: {node: '>=18.0.0'} - '@smithy/eventstream-serde-universal@4.2.8': - resolution: {integrity: sha512-iJ6YNJd0bntJYnX6s52NC4WFYcZeKrPUr1Kmmr5AwZcwCSzVpS7oavAmxMR7pMq7V+D1G4s9F5NJK0xwOsKAlQ==} - engines: {node: '>=18.0.0'} - '@smithy/fetch-http-handler@5.3.11': resolution: {integrity: sha512-wbTRjOxdFuyEg0CpumjZO0hkUl+fetJFqxNROepuLIoijQh51aMBmzFLfoQdwRjxsuuS2jizzIUTjPWgd8pd7g==} engines: {node: '>=18.0.0'} - '@smithy/fetch-http-handler@5.3.9': - resolution: {integrity: sha512-I4UhmcTYXBrct03rwzQX1Y/iqQlzVQaPxWjCjula++5EmWq9YGBrx6bbGqluGc1f0XEfhSkiY4jhLgbsJUMKRA==} - engines: {node: '>=18.0.0'} - '@smithy/hash-node@4.2.10': resolution: {integrity: sha512-1VzIOI5CcsvMDvP3iv1vG/RfLJVVVc67dCRyLSB2Hn9SWCZrDO3zvcIzj3BfEtqRW5kcMg5KAeVf1K3dR6nD3w==} engines: {node: '>=18.0.0'} - '@smithy/hash-node@4.2.8': - resolution: {integrity: sha512-7ZIlPbmaDGxVoxErDZnuFG18WekhbA/g2/i97wGj+wUBeS6pcUeAym8u4BXh/75RXWhgIJhyC11hBzig6MljwA==} - engines: {node: '>=18.0.0'} - '@smithy/invalid-dependency@4.2.10': resolution: {integrity: sha512-vy9KPNSFUU0ajFYk0sDZIYiUlAWGEAhRfehIr5ZkdFrRFTAuXEPUd41USuqHU6vvLX4r6Q9X7MKBco5+Il0Org==} engines: {node: '>=18.0.0'} - '@smithy/invalid-dependency@4.2.8': - resolution: {integrity: sha512-N9iozRybwAQ2dn9Fot9kI6/w9vos2oTXLhtK7ovGqwZjlOcxu6XhPlpLpC+INsxktqHinn5gS2DXDjDF2kG5sQ==} - engines: {node: '>=18.0.0'} - '@smithy/is-array-buffer@2.2.0': resolution: {integrity: sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA==} engines: {node: '>=14.0.0'} - '@smithy/is-array-buffer@4.2.0': - resolution: {integrity: sha512-DZZZBvC7sjcYh4MazJSGiWMI2L7E0oCiRHREDzIxi/M2LY79/21iXt6aPLHge82wi5LsuRF5A06Ds3+0mlh6CQ==} - engines: {node: '>=18.0.0'} - '@smithy/is-array-buffer@4.2.1': resolution: {integrity: sha512-Yfu664Qbf1B4IYIsYgKoABt010daZjkaCRvdU/sPnZG6TtHOB0md0RjNdLGzxe5UIdn9js4ftPICzmkRa9RJ4Q==} engines: {node: '>=18.0.0'} @@ -2775,22 +2525,10 @@ packages: resolution: {integrity: sha512-TQZ9kX5c6XbjhaEBpvhSvMEZ0klBs1CFtOdPFwATZSbC9UeQfKHPLPN9Y+I6wZGMOavlYTOlHEPDrt42PMSH9w==} engines: {node: '>=18.0.0'} - '@smithy/middleware-content-length@4.2.8': - resolution: {integrity: sha512-RO0jeoaYAB1qBRhfVyq0pMgBoUK34YEJxVxyjOWYZiOKOq2yMZ4MnVXMZCUDenpozHue207+9P5ilTV1zeda0A==} - engines: {node: '>=18.0.0'} - - '@smithy/middleware-endpoint@4.4.16': - resolution: {integrity: sha512-L5GICFCSsNhbJ5JSKeWFGFy16Q2OhoBizb3X2DrxaJwXSEujVvjG9Jt386dpQn2t7jINglQl0b4K/Su69BdbMA==} - engines: {node: '>=18.0.0'} - '@smithy/middleware-endpoint@4.4.20': resolution: {integrity: sha512-9W6Np4ceBP3XCYAGLoMCmn8t2RRVzuD1ndWPLBbv7H9CrwM9Bprf6Up6BM9ZA/3alodg0b7Kf6ftBK9R1N04vw==} engines: {node: '>=18.0.0'} - '@smithy/middleware-retry@4.4.33': - resolution: {integrity: sha512-jLqZOdJhtIL4lnA9hXnAG6GgnJlo1sD3FqsTxm9wSfjviqgWesY/TMBVnT84yr4O0Vfe0jWoXlfFbzsBVph3WA==} - engines: {node: '>=18.0.0'} - '@smithy/middleware-retry@4.4.37': resolution: {integrity: sha512-/1psZZllBBSQ7+qo5+hhLz7AEPGLx3Z0+e3ramMBEuPK2PfvLK4SrncDB9VegX5mBn+oP/UTDrM6IHrFjvX1ZA==} engines: {node: '>=18.0.0'} @@ -2799,30 +2537,14 @@ packages: resolution: {integrity: sha512-STQdONGPwbbC7cusL60s7vOa6He6A9w2jWhoapL0mgVjmR19pr26slV+yoSP76SIssMTX/95e5nOZ6UQv6jolg==} engines: {node: '>=18.0.0'} - '@smithy/middleware-serde@4.2.9': - resolution: {integrity: sha512-eMNiej0u/snzDvlqRGSN3Vl0ESn3838+nKyVfF2FKNXFbi4SERYT6PR392D39iczngbqqGG0Jl1DlCnp7tBbXQ==} - engines: {node: '>=18.0.0'} - '@smithy/middleware-stack@4.2.10': resolution: {integrity: sha512-pmts/WovNcE/tlyHa8z/groPeOtqtEpp61q3W0nW1nDJuMq/x+hWa/OVQBtgU0tBqupeXq0VBOLA4UZwE8I0YA==} engines: {node: '>=18.0.0'} - '@smithy/middleware-stack@4.2.8': - resolution: {integrity: sha512-w6LCfOviTYQjBctOKSwy6A8FIkQy7ICvglrZFl6Bw4FmcQ1Z420fUtIhxaUZZshRe0VCq4kvDiPiXrPZAe8oRA==} - engines: {node: '>=18.0.0'} - '@smithy/node-config-provider@4.3.10': resolution: {integrity: sha512-UALRbJtVX34AdP2VECKVlnNgidLHA2A7YgcJzwSBg1hzmnO/bZBHl/LDQQyYifzUwp1UOODnl9JJ3KNawpUJ9w==} engines: {node: '>=18.0.0'} - '@smithy/node-config-provider@4.3.8': - resolution: {integrity: sha512-aFP1ai4lrbVlWjfpAfRSL8KFcnJQYfTl5QxLJXY32vghJrDuFyPZ6LtUL+JEGYiFRG1PfPLHLoxj107ulncLIg==} - engines: {node: '>=18.0.0'} - - '@smithy/node-http-handler@4.4.10': - resolution: {integrity: sha512-u4YeUwOWRZaHbWaebvrs3UhwQwj+2VNmcVCwXcYTvPIuVyM7Ex1ftAj+fdbG/P4AkBwLq/+SKn+ydOI4ZJE9PA==} - engines: {node: '>=18.0.0'} - '@smithy/node-http-handler@4.4.12': resolution: {integrity: sha512-zo1+WKJkR9x7ZtMeMDAAsq2PufwiLDmkhcjpWPRRkmeIuOm6nq1qjFICSZbnjBvD09ei8KMo26BWxsu2BUU+5w==} engines: {node: '>=18.0.0'} @@ -2831,46 +2553,22 @@ packages: resolution: {integrity: sha512-5jm60P0CU7tom0eNrZ7YrkgBaoLFXzmqB0wVS+4uK8PPGmosSrLNf6rRd50UBvukztawZ7zyA8TxlrKpF5z9jw==} engines: {node: '>=18.0.0'} - '@smithy/property-provider@4.2.8': - resolution: {integrity: sha512-EtCTbyIveCKeOXDSWSdze3k612yCPq1YbXsbqX3UHhkOSW8zKsM9NOJG5gTIya0vbY2DIaieG8pKo1rITHYL0w==} - engines: {node: '>=18.0.0'} - '@smithy/protocol-http@5.3.10': resolution: {integrity: sha512-2NzVWpYY0tRdfeCJLsgrR89KE3NTWT2wGulhNUxYlRmtRmPwLQwKzhrfVaiNlA9ZpJvbW7cjTVChYKgnkqXj1A==} engines: {node: '>=18.0.0'} - '@smithy/protocol-http@5.3.8': - resolution: {integrity: sha512-QNINVDhxpZ5QnP3aviNHQFlRogQZDfYlCkQT+7tJnErPQbDhysondEjhikuANxgMsZrkGeiAxXy4jguEGsDrWQ==} - engines: {node: '>=18.0.0'} - '@smithy/querystring-builder@4.2.10': resolution: {integrity: sha512-HeN7kEvuzO2DmAzLukE9UryiUvejD3tMp9a1D1NJETerIfKobBUCLfviP6QEk500166eD2IATaXM59qgUI+YDA==} engines: {node: '>=18.0.0'} - '@smithy/querystring-builder@4.2.8': - resolution: {integrity: sha512-Xr83r31+DrE8CP3MqPgMJl+pQlLLmOfiEUnoyAlGzzJIrEsbKsPy1hqH0qySaQm4oWrCBlUqRt+idEgunKB+iw==} - engines: {node: '>=18.0.0'} - '@smithy/querystring-parser@4.2.10': resolution: {integrity: sha512-4Mh18J26+ao1oX5wXJfWlTT+Q1OpDR8ssiC9PDOuEgVBGloqg18Fw7h5Ct8DyT9NBYwJgtJ2nLjKKFU6RP1G1Q==} engines: {node: '>=18.0.0'} - '@smithy/querystring-parser@4.2.8': - resolution: {integrity: sha512-vUurovluVy50CUlazOiXkPq40KGvGWSdmusa3130MwrR1UNnNgKAlj58wlOe61XSHRpUfIIh6cE0zZ8mzKaDPA==} - engines: {node: '>=18.0.0'} - '@smithy/service-error-classification@4.2.10': resolution: {integrity: sha512-0R/+/Il5y8nB/By90o8hy/bWVYptbIfvoTYad0igYQO5RefhNCDmNzqxaMx7K1t/QWo0d6UynqpqN5cCQt1MCg==} engines: {node: '>=18.0.0'} - '@smithy/service-error-classification@4.2.8': - resolution: {integrity: sha512-mZ5xddodpJhEt3RkCjbmUQuXUOaPNTkbMGR0bcS8FE0bJDLMZlhmpgrvPNCYglVw5rsYTpSnv19womw9WWXKQQ==} - engines: {node: '>=18.0.0'} - - '@smithy/shared-ini-file-loader@4.4.3': - resolution: {integrity: sha512-DfQjxXQnzC5UbCUPeC3Ie8u+rIWZTvuDPAGU/BxzrOGhRvgUanaP68kDZA+jaT3ZI+djOf+4dERGlm9mWfFDrg==} - engines: {node: '>=18.0.0'} - '@smithy/shared-ini-file-loader@4.4.5': resolution: {integrity: sha512-pHgASxl50rrtOztgQCPmOXFjRW+mCd7ALr/3uXNzRrRoGV5G2+78GOsQ3HlQuBVHCh9o6xqMNvlIKZjWn4Euug==} engines: {node: '>=18.0.0'} @@ -2879,22 +2577,10 @@ packages: resolution: {integrity: sha512-Wab3wW8468WqTKIxI+aZe3JYO52/RYT/8sDOdzkUhjnLakLe9qoQqIcfih/qxcF4qWEFoWBszY0mj5uxffaVXA==} engines: {node: '>=18.0.0'} - '@smithy/signature-v4@5.3.8': - resolution: {integrity: sha512-6A4vdGj7qKNRF16UIcO8HhHjKW27thsxYci+5r/uVRkdcBEkOEiY8OMPuydLX4QHSrJqGHPJzPRwwVTqbLZJhg==} - engines: {node: '>=18.0.0'} - - '@smithy/smithy-client@4.11.5': - resolution: {integrity: sha512-xixwBRqoeP2IUgcAl3U9dvJXc+qJum4lzo3maaJxifsZxKUYLfVfCXvhT4/jD01sRrHg5zjd1cw2Zmjr4/SuKQ==} - engines: {node: '>=18.0.0'} - '@smithy/smithy-client@4.12.0': resolution: {integrity: sha512-R8bQ9K3lCcXyZmBnQqUZJF4ChZmtWT5NLi6x5kgWx5D+/j0KorXcA0YcFg/X5TOgnTCy1tbKc6z2g2y4amFupQ==} engines: {node: '>=18.0.0'} - '@smithy/types@4.12.0': - resolution: {integrity: sha512-9YcuJVTOBDjg9LWo23Qp0lTQ3D7fQsQtwle0jVfpbUHy9qBwCEgKuVH4FqFB3VYu0nwdHKiEMA+oXz7oV8X1kw==} - engines: {node: '>=18.0.0'} - '@smithy/types@4.13.0': resolution: {integrity: sha512-COuLsZILbbQsdrwKQpkkpyep7lCsByxwj7m0Mg5v66/ZTyenlfBc40/QFQ5chO0YN/PNEH1Bi3fGtfXPnYNeDw==} engines: {node: '>=18.0.0'} @@ -2903,30 +2589,14 @@ packages: resolution: {integrity: sha512-uypjF7fCDsRk26u3qHmFI/ePL7bxxB9vKkE+2WKEciHhz+4QtbzWiHRVNRJwU3cKhrYDYQE3b0MRFtqfLYdA4A==} engines: {node: '>=18.0.0'} - '@smithy/url-parser@4.2.8': - resolution: {integrity: sha512-NQho9U68TGMEU639YkXnVMV3GEFFULmmaWdlu1E9qzyIePOHsoSnagTGSDv1Zi8DCNN6btxOSdgmy5E/hsZwhA==} - engines: {node: '>=18.0.0'} - - '@smithy/util-base64@4.3.0': - resolution: {integrity: sha512-GkXZ59JfyxsIwNTWFnjmFEI8kZpRNIBfxKjv09+nkAWPt/4aGaEWMM04m4sxgNVWkbt2MdSvE3KF/PfX4nFedQ==} - engines: {node: '>=18.0.0'} - '@smithy/util-base64@4.3.1': resolution: {integrity: sha512-BKGuawX4Doq/bI/uEmg+Zyc36rJKWuin3py89PquXBIBqmbnJwBBsmKhdHfNEp0+A4TDgLmT/3MSKZ1SxHcR6w==} engines: {node: '>=18.0.0'} - '@smithy/util-body-length-browser@4.2.0': - resolution: {integrity: sha512-Fkoh/I76szMKJnBXWPdFkQJl2r9SjPt3cMzLdOB6eJ4Pnpas8hVoWPYemX/peO0yrrvldgCUVJqOAjUrOLjbxg==} - engines: {node: '>=18.0.0'} - '@smithy/util-body-length-browser@4.2.1': resolution: {integrity: sha512-SiJeLiozrAoCrgDBUgsVbmqHmMgg/2bA15AzcbcW+zan7SuyAVHN4xTSbq0GlebAIwlcaX32xacnrG488/J/6g==} engines: {node: '>=18.0.0'} - '@smithy/util-body-length-node@4.2.1': - resolution: {integrity: sha512-h53dz/pISVrVrfxV1iqXlx5pRg3V2YWFcSQyPyXZRrZoZj4R4DeWRDo1a7dd3CPTcFi3kE+98tuNyD2axyZReA==} - engines: {node: '>=18.0.0'} - '@smithy/util-body-length-node@4.2.2': resolution: {integrity: sha512-4rHqBvxtJEBvsZcFQSPQqXP2b/yy/YlB66KlcEgcH2WNoOKCKB03DSLzXmOsXjbl8dJ4OEYTn31knhdznwk7zw==} engines: {node: '>=18.0.0'} @@ -2935,50 +2605,26 @@ packages: resolution: {integrity: sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA==} engines: {node: '>=14.0.0'} - '@smithy/util-buffer-from@4.2.0': - resolution: {integrity: sha512-kAY9hTKulTNevM2nlRtxAG2FQ3B2OR6QIrPY3zE5LqJy1oxzmgBGsHLWTcNhWXKchgA0WHW+mZkQrng/pgcCew==} - engines: {node: '>=18.0.0'} - '@smithy/util-buffer-from@4.2.1': resolution: {integrity: sha512-/swhmt1qTiVkaejlmMPPDgZhEaWb/HWMGRBheaxwuVkusp/z+ErJyQxO6kaXumOciZSWlmq6Z5mNylCd33X7Ig==} engines: {node: '>=18.0.0'} - '@smithy/util-config-provider@4.2.0': - resolution: {integrity: sha512-YEjpl6XJ36FTKmD+kRJJWYvrHeUvm5ykaUS5xK+6oXffQPHeEM4/nXlZPe+Wu0lsgRUcNZiliYNh/y7q9c2y6Q==} - engines: {node: '>=18.0.0'} - '@smithy/util-config-provider@4.2.1': resolution: {integrity: sha512-462id/00U8JWFw6qBuTSWfN5TxOHvDu4WliI97qOIOnuC/g+NDAknTU8eoGXEPlLkRVgWEr03jJBLV4o2FL8+A==} engines: {node: '>=18.0.0'} - '@smithy/util-defaults-mode-browser@4.3.32': - resolution: {integrity: sha512-092sjYfFMQ/iaPH798LY/OJFBcYu0sSK34Oy9vdixhsU36zlZu8OcYjF3TD4e2ARupyK7xaxPXl+T0VIJTEkkg==} - engines: {node: '>=18.0.0'} - '@smithy/util-defaults-mode-browser@4.3.36': resolution: {integrity: sha512-R0smq7EHQXRVMxkAxtH5akJ/FvgAmNF6bUy/GwY/N20T4GrwjT633NFm0VuRpC+8Bbv8R9A0DoJ9OiZL/M3xew==} engines: {node: '>=18.0.0'} - '@smithy/util-defaults-mode-node@4.2.35': - resolution: {integrity: sha512-miz/ggz87M8VuM29y7jJZMYkn7+IErM5p5UgKIf8OtqVs/h2bXr1Bt3uTsREsI/4nK8a0PQERbAPsVPVNIsG7Q==} - engines: {node: '>=18.0.0'} - '@smithy/util-defaults-mode-node@4.2.39': resolution: {integrity: sha512-otWuoDm35btJV1L8MyHrPl462B07QCdMTktKc7/yM+Psv6KbED/ziXiHnmr7yPHUjfIwE9S8Max0LO24Mo3ZVg==} engines: {node: '>=18.0.0'} - '@smithy/util-endpoints@3.2.8': - resolution: {integrity: sha512-8JaVTn3pBDkhZgHQ8R0epwWt+BqPSLCjdjXXusK1onwJlRuN69fbvSK66aIKKO7SwVFM6x2J2ox5X8pOaWcUEw==} - engines: {node: '>=18.0.0'} - '@smithy/util-endpoints@3.3.1': resolution: {integrity: sha512-xyctc4klmjmieQiF9I1wssBWleRV0RhJ2DpO8+8yzi2LO1Z+4IWOZNGZGNj4+hq9kdo+nyfrRLmQTzc16Op2Vg==} engines: {node: '>=18.0.0'} - '@smithy/util-hex-encoding@4.2.0': - resolution: {integrity: sha512-CCQBwJIvXMLKxVbO88IukazJD9a4kQ9ZN7/UMGBjBcJYvatpWk+9g870El4cB8/EJxfe+k+y0GmR9CAzkF+Nbw==} - engines: {node: '>=18.0.0'} - '@smithy/util-hex-encoding@4.2.1': resolution: {integrity: sha512-c1hHtkgAWmE35/50gmdKajgGAKV3ePJ7t6UtEmpfCWJmQE9BQAQPz0URUVI89eSkcDqCtzqllxzG28IQoZPvwA==} engines: {node: '>=18.0.0'} @@ -2987,30 +2633,14 @@ packages: resolution: {integrity: sha512-LxaQIWLp4y0r72eA8mwPNQ9va4h5KeLM0I3M/HV9klmFaY2kN766wf5vsTzmaOpNNb7GgXAd9a25P3h8T49PSA==} engines: {node: '>=18.0.0'} - '@smithy/util-middleware@4.2.8': - resolution: {integrity: sha512-PMqfeJxLcNPMDgvPbbLl/2Vpin+luxqTGPpW3NAQVLbRrFRzTa4rNAASYeIGjRV9Ytuhzny39SpyU04EQreF+A==} - engines: {node: '>=18.0.0'} - '@smithy/util-retry@4.2.10': resolution: {integrity: sha512-HrBzistfpyE5uqTwiyLsFHscgnwB0kgv8vySp7q5kZ0Eltn/tjosaSGGDj/jJ9ys7pWzIP/icE2d+7vMKXLv7A==} engines: {node: '>=18.0.0'} - '@smithy/util-retry@4.2.8': - resolution: {integrity: sha512-CfJqwvoRY0kTGe5AkQokpURNCT1u/MkRzMTASWMPPo2hNSnKtF1D45dQl3DE2LKLr4m+PW9mCeBMJr5mCAVThg==} - engines: {node: '>=18.0.0'} - - '@smithy/util-stream@4.5.12': - resolution: {integrity: sha512-D8tgkrmhAX/UNeCZbqbEO3uqyghUnEmmoO9YEvRuwxjlkKKUE7FOgCJnqpTlQPe9MApdWPky58mNQQHbnCzoNg==} - engines: {node: '>=18.0.0'} - '@smithy/util-stream@4.5.15': resolution: {integrity: sha512-OlOKnaqnkU9X+6wEkd7mN+WB7orPbCVDauXOj22Q7VtiTkvy7ZdSsOg4QiNAZMgI4OkvNf+/VLUC3VXkxuWJZw==} engines: {node: '>=18.0.0'} - '@smithy/util-uri-escape@4.2.0': - resolution: {integrity: sha512-igZpCKV9+E/Mzrpq6YacdTQ0qTiLm85gD6N/IrmyDvQFA4UnU3d5g3m8tMT/6zG/vVkWSU+VxeUyGonL62DuxA==} - engines: {node: '>=18.0.0'} - '@smithy/util-uri-escape@4.2.1': resolution: {integrity: sha512-YmiUDn2eo2IOiWYYvGQkgX5ZkBSiTQu4FlDo5jNPpAxng2t6Sjb6WutnZV9l6VR4eJul1ABmCrnWBC9hKHQa6Q==} engines: {node: '>=18.0.0'} @@ -3019,18 +2649,10 @@ packages: resolution: {integrity: sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A==} engines: {node: '>=14.0.0'} - '@smithy/util-utf8@4.2.0': - resolution: {integrity: sha512-zBPfuzoI8xyBtR2P6WQj63Rz8i3AmfAaJLuNG8dWsfvPe8lO4aCPYLn879mEgHndZH1zQ2oXmG8O1GGzzaoZiw==} - engines: {node: '>=18.0.0'} - '@smithy/util-utf8@4.2.1': resolution: {integrity: sha512-DSIwNaWtmzrNQHv8g7DBGR9mulSit65KSj5ymGEIAknmIN8IpbZefEep10LaMG/P/xquwbmJ1h9ectz8z6mV6g==} engines: {node: '>=18.0.0'} - '@smithy/uuid@1.1.0': - resolution: {integrity: sha512-4aUIteuyxtBUhVdiQqcDhKFitwfd9hqoSDYY2KRXiWtgoWJ9Bmise+KfEPDiVHWeJepvF8xJO9/9+WDIciMFFw==} - engines: {node: '>=18.0.0'} - '@smithy/uuid@1.1.1': resolution: {integrity: sha512-dSfDCeihDmZlV2oyr0yWPTUfh07suS+R5OB+FZGiv/hHyK3hrFBW5rR1UYjfa57vBsrP9lciFkRPzebaV1Qujw==} engines: {node: '>=18.0.0'} @@ -3292,43 +2914,46 @@ packages: '@types/ws@8.18.1': resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==} - '@typescript/native-preview-darwin-arm64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-9VHXRhB7sM5DFqdlKaeDww8vuklgfzhYCjBazLCEnuFvb4J+rJ1DodLykc2bL+6kE8k6sdhYi3x8ipfbjtO44g==} + '@types/yauzl@2.10.3': + resolution: {integrity: sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q==} + + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-3qSsqv7FmM4z09wEpEXdhmgMfiJF/OMOZa41AdgMsXTTRpX2/38hDg2KGhi3fc24M2T3MnLPLTqw6HyTOBaV1Q==} cpu: [arm64] os: [darwin] - '@typescript/native-preview-darwin-x64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-uCHipPRcIhHnvb7lAM29MQ1QT9pZ+uirqtH630aOMFm8VG3j8mkxVM9iGRLx829n38DMSDLjc3joCrQO3+sDcQ==} + '@typescript/native-preview-darwin-x64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-F8ZCCX2UESHcbxvnkd1Dn5PTnOOgpGddFHYgn4usyWRMzNZLPP+YjyGALZe9zdR/D8L0uraND0Haok+TPq8xYg==} cpu: [x64] os: [darwin] - '@typescript/native-preview-linux-arm64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-yFEEq6hD2R70+lTogb211sPdCwz3H5hpYh0+YuKVMPsKo0oM8/jMvgjj2pyutmj/uCKLdbcJ9HP2vJ/13Szbcg==} + '@typescript/native-preview-linux-arm64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-Up8Z/QNcwce5C4rWnbLNW5w7lRARdyKZcNbB1NMnaswaGOBdeDmdP0wbVsOgJMoDp6vnun+EkvrSft8hWLLhIg==} cpu: [arm64] os: [linux] - '@typescript/native-preview-linux-arm@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-cEWSRQ8b+CXdMJvoG18IjNTvBo+qT22B5imqm6nAssMpyHHQb62PvZGnrA8mPRQNPzLpa5F956j8GwAjyP8hBQ==} + '@typescript/native-preview-linux-arm@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-Iu5rnCmqwGIMUu//BXkl9VQaxAAsqVvFhU4mJoNexNkMxPqVcu9quqYAouY7tN/95WcKzUsPpyRfkThdbNFO/g==} cpu: [arm] os: [linux] - '@typescript/native-preview-linux-x64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-zGz5kVcCeBRheQwA4jVTAxtbLsBsTkp9AEvWK5AlyCs1rQCUQobBhtx37X4VEmxn4ekIDMxYgaZdlZb7/PGp8w==} + '@typescript/native-preview-linux-x64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-WWjIfHCWlcriempYYc/sPJ3HFt6znNZKp60nvDNih0+wmxNqEfT5Yzu5zAY0awIe7XLelFSY+bolkpzMYVWEIQ==} cpu: [x64] os: [linux] - '@typescript/native-preview-win32-arm64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-A0f9ZDQqKvGk/an59HuAJuzoI/wMyrgTd69oX9gFCx7+5E/ajSdgv0Eg1Fco+nyLfT/UVM0CV3ERyWrKzx277w==} + '@typescript/native-preview-win32-arm64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-lmfQO+HdmPMk0dtPoNo8dZereTUYNQuapsAI7nFHCP8F25I8eGKKXY2nD1R8W1hp/LmVtske1pqKFNN6IOCt5g==} cpu: [arm64] os: [win32] - '@typescript/native-preview-win32-x64@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-Se9JrcMdVLeDYMLn+CKEV3qy1yiildb5N23USGvnC9siNFalz8tVgd589dhRP+ywDhXnbIsZiFKDrZF/7B4wSQ==} + '@typescript/native-preview-win32-x64@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-e4eJyzR9ne0XreqYgQNqfX7SNuaePxggnUtVrLERgBv25QKwdQl72GnSXDhdxZHzrb97YwumiXWMQQJj9h8NCg==} cpu: [x64] os: [win32] - '@typescript/native-preview@7.0.0-dev.20260224.1': - resolution: {integrity: sha512-PU0zBXLvz6RKxbIubT66RCnJXgScdDIhfmNMkvRhOnX/C4SZom5TFSn7BEHC3w8JPj7OSz5OYoubtV1Haty2GA==} + '@typescript/native-preview@7.0.0-dev.20260225.1': + resolution: {integrity: sha512-mUf1aON+eZLupLorX4214n4W6uWIz/lvNv81ErzjJylD/GyJPEJkvDLmgIK3bbvLpMwTRWdVJLhpLCah5Qe8iA==} hasBin: true '@typespec/ts-http-runtime@0.3.3': @@ -3637,6 +3262,9 @@ packages: resolution: {integrity: sha512-fy6KJm2RawA5RcHkLa1z/ScpBeA762UF9KmZQxwIbDtRJrgLzM10depAiEQ+CXYcoiqW1/m96OAAoke2nE9EeA==} engines: {node: 18 || 20 || >=22} + buffer-crc32@0.2.13: + resolution: {integrity: sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==} + buffer-equal-constant-time@1.0.1: resolution: {integrity: sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==} @@ -3949,6 +3577,9 @@ packages: resolution: {integrity: sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==} engines: {node: '>= 0.8'} + end-of-stream@1.4.5: + resolution: {integrity: sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==} + entities@4.5.0: resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==} engines: {node: '>=0.12'} @@ -4046,6 +3677,11 @@ packages: extend@3.0.2: resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==} + extract-zip@2.0.1: + resolution: {integrity: sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==} + engines: {node: '>= 10.17.0'} + hasBin: true + extsprintf@1.3.0: resolution: {integrity: sha512-11Ndz7Nv+mvAC1j0ktTa7fAb0vLyGGX+rMHNBYQviQDGU0Hw7lhctJANqbPhu9nV9/izT/IntTgZ7Im/9LJs9g==} engines: {'0': node >=0.6.0} @@ -4063,6 +3699,9 @@ packages: resolution: {integrity: sha512-QNI3sAvSvaOiaMl8FYU4trnEzCwiRr8XMWgAHzlrWpTSj+QaCSvOf1h82OEP1s4hiAXhnbXSyFWCf4ldZzZRVA==} hasBin: true + fd-slicer@1.1.0: + resolution: {integrity: sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==} + fdir@6.5.0: resolution: {integrity: sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==} engines: {node: '>=12.0.0'} @@ -4182,10 +3821,6 @@ packages: resolution: {integrity: sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==} engines: {node: 6.* || 8.* || >= 10.*} - get-east-asian-width@1.4.0: - resolution: {integrity: sha512-QZjmEOC+IT1uk6Rx0sX22V6uHWVwbdbxf1faPqJ1QhLdGgsRGCZoyaQBm/piRdJy/D2um6hM1UP7ZEeQ4EkP+Q==} - engines: {node: '>=18'} - get-east-asian-width@1.5.0: resolution: {integrity: sha512-CQ+bEO+Tva/qlmw24dCejulK5pMzVnUOFOijVogd3KQs07HnRIgp8TGipvCCRT06xeYEbpbgwaCxglFyiuIcmA==} engines: {node: '>=18'} @@ -4198,6 +3833,10 @@ packages: resolution: {integrity: sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==} engines: {node: '>= 0.4'} + get-stream@5.2.0: + resolution: {integrity: sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==} + engines: {node: '>=8'} + get-tsconfig@4.13.6: resolution: {integrity: sha512-shZT/QMiSHc/YBLxxOkMtgSid5HFoauqCE3/exfsEcwg1WkeqjG+V40yBbBrsD+jW2HDXcs28xOfcbm2jI8Ddw==} @@ -4376,8 +4015,8 @@ packages: resolution: {integrity: sha512-Zv/pA+ciVFbCSBBjGfaKUya/CcGmUHzTydLMaTwrUUEM2DIEO3iZvueGxmacvmN50fGpGVKeTXpb2LcYQxeVdg==} engines: {node: '>= 10'} - ipull@3.9.3: - resolution: {integrity: sha512-ZMkxaopfwKHwmEuGDYx7giNBdLxbHbRCWcQVA1D2eqE4crUguupfxej6s7UqbidYEwT69dkyumYkY8DPHIxF9g==} + ipull@3.9.5: + resolution: {integrity: sha512-5w/yZB5lXmTfsvNawmvkCjYo4SJNuKQz/av8TC1UiOyfOHyaM+DReqbpU2XpWYfmY+NIUbRRH8PUAWsxaS+IfA==} engines: {node: '>=18.0.0'} hasBin: true @@ -4490,6 +4129,9 @@ packages: json-stringify-safe@5.0.1: resolution: {integrity: sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==} + json-with-bigint@3.5.3: + resolution: {integrity: sha512-QObKu6nxy7NsxqR0VK4rkXnsNr5L9ElJaGEg+ucJ6J7/suoKZ0n+p76cu9aCqowytxEbwYNzvrMerfMkXneF5A==} + json5@2.2.3: resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==} engines: {node: '>=6'} @@ -4538,8 +4180,8 @@ packages: lifecycle-utils@2.1.0: resolution: {integrity: sha512-AnrXnE2/OF9PHCyFg0RSqsnQTzV991XaZA/buhFDoc58xU7rhSCDgCz/09Lqpsn4MpoPHt7TRAXV1kWZypFVsA==} - lifecycle-utils@3.1.0: - resolution: {integrity: sha512-kVvegv+r/icjIo1dkHv1hznVQi4FzEVglJD2IU4w07HzevIyH3BAYsFZzEIbBk/nNZjXHGgclJ5g9rz9QdBCLw==} + lifecycle-utils@3.1.1: + resolution: {integrity: sha512-gNd3OvhFNjHykJE3uGntz7UuPzWlK9phrIdXxU9Adis0+ExkwnZibfxCJWiWWZ+a6VbKiZrb+9D9hCQWd4vjTg==} lightningcss-android-arm64@1.30.2: resolution: {integrity: sha512-BH9sEdOCahSgmkVhBLeU7Hc9DWeZ1Eb6wNS6Da8igvUwAe0sqROHddIlvU06q3WyXVEOYDZ6ykBZQnjTbmo4+A==} @@ -5019,8 +4661,8 @@ packages: zod: optional: true - openclaw@2026.2.23: - resolution: {integrity: sha512-7I7G898212v3OzUidgM8kZdZYAziT78Dc5zgeqsV2tfCbINtHK0Pdc2rg2eDLoDYAcheLh0fvH5qn/15Yu9q7A==} + openclaw@2026.2.24: + resolution: {integrity: sha512-a6zrcS6v5tUWqzsFh5cNtyu5+Tra1UW5yvPtYhRYCKSS/q6lXrLu+dj0ylJPOHRPAho2alZZL1gw1Qd2hAd2sQ==} engines: {node: '>=22.12.0'} hasBin: true peerDependencies: @@ -5030,9 +4672,6 @@ packages: opus-decoder@0.7.11: resolution: {integrity: sha512-+e+Jz3vGQLxRTBHs8YJQPRPc1Tr+/aC6coV/DlZylriA29BdHQAYXhvNRKtjftof17OFng0+P4wsFIqQu3a48A==} - opusscript@0.0.8: - resolution: {integrity: sha512-VSTi1aWFuCkRCVq+tx/BQ5q9fMnQ9pVZ3JU4UHKqTkf0ED3fKEPdr+gKAAl3IA2hj9rrP6iyq3hlcJq3HELtNQ==} - opusscript@0.1.1: resolution: {integrity: sha512-mL0fZZOUnXdZ78woRXp18lApwpp0lF5tozJOD1Wut0dgrA9WuQTgSels/CSmFleaAZrJi/nci5KOVtbuxeWoQA==} @@ -5163,6 +4802,9 @@ packages: peberminta@0.9.0: resolution: {integrity: sha512-XIxfHpEuSJbITd1H3EeQwpcZbTLHc+VVr8ANI9t5sit565tsI4/xK3KWTUFE2e6QiangUkh3B0jihzmGnNrRsQ==} + pend@1.2.0: + resolution: {integrity: sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==} + performance-now@2.1.0: resolution: {integrity: sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==} @@ -5277,6 +4919,9 @@ packages: psl@1.15.0: resolution: {integrity: sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==} + pump@3.0.3: + resolution: {integrity: sha512-todwxLMY7/heScKmntwQG8CXVkWUOdYxIvY2s0VWAAMh/nd8SoYiRaKjlr7+iCs984f2P8zvrfWcDDYVb73NfA==} + punycode.js@2.3.1: resolution: {integrity: sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA==} engines: {node: '>=6'} @@ -5520,8 +5165,8 @@ packages: peerDependencies: signal-polyfill: ^0.2.0 - simple-git@3.31.1: - resolution: {integrity: sha512-oiWP4Q9+kO8q9hHqkX35uuHmxiEbZNTrZ5IPxgMGrJwN76pzjm/jabkZO0ItEcqxAincqGAzL3QHSaHt4+knBg==} + simple-git@3.32.2: + resolution: {integrity: sha512-n/jhNmvYh8dwyfR6idSfpXrFazuyd57jwNMzgjGnKZV/1lTh0HKvPq20v4AQ62rP+l19bWjjXPTCdGHMt0AdrQ==} simple-yenc@1.0.4: resolution: {integrity: sha512-5gvxpSd79e9a3V4QDYUqnqxeD4HGlhCakVpb6gMnDD7lexJggSBJRBO5h52y/iJrdXRilX9UCuDaIJhSWm5OWw==} @@ -6060,6 +5705,9 @@ packages: resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==} engines: {node: '>=12'} + yauzl@2.10.0: + resolution: {integrity: sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==} + yoctocolors@2.1.2: resolution: {integrity: sha512-CzhO+pFNo8ajLM2d2IW/R93ipy99LWjtwblvC1RsoSUMZgyLbYFr221TnSNT7GjGdYui6P459mw9JH/g/zW2ug==} engines: {node: '>=18'} @@ -6093,7 +5741,7 @@ snapshots: '@aws-crypto/crc32@5.2.0': dependencies: '@aws-crypto/util': 5.2.0 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 tslib: 2.8.1 '@aws-crypto/sha256-browser@5.2.0': @@ -6101,7 +5749,7 @@ snapshots: '@aws-crypto/sha256-js': 5.2.0 '@aws-crypto/supports-web-crypto': 5.2.0 '@aws-crypto/util': 5.2.0 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@aws-sdk/util-locate-window': 3.965.4 '@smithy/util-utf8': 2.3.0 tslib: 2.8.1 @@ -6109,7 +5757,7 @@ snapshots: '@aws-crypto/sha256-js@5.2.0': dependencies: '@aws-crypto/util': 5.2.0 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 tslib: 2.8.1 '@aws-crypto/supports-web-crypto@5.2.0': @@ -6118,81 +5766,29 @@ snapshots: '@aws-crypto/util@5.2.0': dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/util-utf8': 2.3.0 tslib: 2.8.1 - '@aws-sdk/client-bedrock-runtime@3.995.0': + '@aws-sdk/client-bedrock-runtime@3.998.0': dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/credential-provider-node': 3.972.10 - '@aws-sdk/eventstream-handler-node': 3.972.5 - '@aws-sdk/middleware-eventstream': 3.972.3 - '@aws-sdk/middleware-host-header': 3.972.3 - '@aws-sdk/middleware-logger': 3.972.3 - '@aws-sdk/middleware-recursion-detection': 3.972.3 - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/middleware-websocket': 3.972.6 - '@aws-sdk/region-config-resolver': 3.972.3 - '@aws-sdk/token-providers': 3.995.0 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.995.0 - '@aws-sdk/util-user-agent-browser': 3.972.3 - '@aws-sdk/util-user-agent-node': 3.972.10 - '@smithy/config-resolver': 4.4.6 - '@smithy/core': 3.23.2 - '@smithy/eventstream-serde-browser': 4.2.8 - '@smithy/eventstream-serde-config-resolver': 4.3.8 - '@smithy/eventstream-serde-node': 4.2.8 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/hash-node': 4.2.8 - '@smithy/invalid-dependency': 4.2.8 - '@smithy/middleware-content-length': 4.2.8 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-retry': 4.4.33 - '@smithy/middleware-serde': 4.2.9 - '@smithy/middleware-stack': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/node-http-handler': 4.4.10 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-body-length-node': 4.2.1 - '@smithy/util-defaults-mode-browser': 4.3.32 - '@smithy/util-defaults-mode-node': 4.2.35 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/util-stream': 4.5.12 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/client-bedrock-runtime@3.997.0': - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.13 - '@aws-sdk/credential-provider-node': 3.972.12 - '@aws-sdk/eventstream-handler-node': 3.972.7 - '@aws-sdk/middleware-eventstream': 3.972.4 - '@aws-sdk/middleware-host-header': 3.972.4 - '@aws-sdk/middleware-logger': 3.972.4 - '@aws-sdk/middleware-recursion-detection': 3.972.4 - '@aws-sdk/middleware-user-agent': 3.972.13 - '@aws-sdk/middleware-websocket': 3.972.8 - '@aws-sdk/region-config-resolver': 3.972.4 - '@aws-sdk/token-providers': 3.997.0 - '@aws-sdk/types': 3.973.2 - '@aws-sdk/util-endpoints': 3.996.1 - '@aws-sdk/util-user-agent-browser': 3.972.4 - '@aws-sdk/util-user-agent-node': 3.972.12 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/credential-provider-node': 3.972.13 + '@aws-sdk/eventstream-handler-node': 3.972.8 + '@aws-sdk/middleware-eventstream': 3.972.5 + '@aws-sdk/middleware-host-header': 3.972.5 + '@aws-sdk/middleware-logger': 3.972.5 + '@aws-sdk/middleware-recursion-detection': 3.972.5 + '@aws-sdk/middleware-user-agent': 3.972.14 + '@aws-sdk/middleware-websocket': 3.972.9 + '@aws-sdk/region-config-resolver': 3.972.5 + '@aws-sdk/token-providers': 3.998.0 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/util-endpoints': 3.996.2 + '@aws-sdk/util-user-agent-browser': 3.972.5 + '@aws-sdk/util-user-agent-node': 3.972.13 '@smithy/config-resolver': 4.4.9 '@smithy/core': 3.23.6 '@smithy/eventstream-serde-browser': 4.2.10 @@ -6226,67 +5822,22 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/client-bedrock@3.995.0': - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/credential-provider-node': 3.972.10 - '@aws-sdk/middleware-host-header': 3.972.3 - '@aws-sdk/middleware-logger': 3.972.3 - '@aws-sdk/middleware-recursion-detection': 3.972.3 - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/region-config-resolver': 3.972.3 - '@aws-sdk/token-providers': 3.995.0 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.995.0 - '@aws-sdk/util-user-agent-browser': 3.972.3 - '@aws-sdk/util-user-agent-node': 3.972.10 - '@smithy/config-resolver': 4.4.6 - '@smithy/core': 3.23.2 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/hash-node': 4.2.8 - '@smithy/invalid-dependency': 4.2.8 - '@smithy/middleware-content-length': 4.2.8 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-retry': 4.4.33 - '@smithy/middleware-serde': 4.2.9 - '@smithy/middleware-stack': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/node-http-handler': 4.4.10 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-body-length-node': 4.2.1 - '@smithy/util-defaults-mode-browser': 4.3.32 - '@smithy/util-defaults-mode-node': 4.2.35 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/client-bedrock@3.997.0': + '@aws-sdk/client-bedrock@3.998.0': dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.13 - '@aws-sdk/credential-provider-node': 3.972.12 - '@aws-sdk/middleware-host-header': 3.972.4 - '@aws-sdk/middleware-logger': 3.972.4 - '@aws-sdk/middleware-recursion-detection': 3.972.4 - '@aws-sdk/middleware-user-agent': 3.972.13 - '@aws-sdk/region-config-resolver': 3.972.4 - '@aws-sdk/token-providers': 3.997.0 - '@aws-sdk/types': 3.973.2 - '@aws-sdk/util-endpoints': 3.996.1 - '@aws-sdk/util-user-agent-browser': 3.972.4 - '@aws-sdk/util-user-agent-node': 3.972.12 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/credential-provider-node': 3.972.13 + '@aws-sdk/middleware-host-header': 3.972.5 + '@aws-sdk/middleware-logger': 3.972.5 + '@aws-sdk/middleware-recursion-detection': 3.972.5 + '@aws-sdk/middleware-user-agent': 3.972.14 + '@aws-sdk/region-config-resolver': 3.972.5 + '@aws-sdk/token-providers': 3.998.0 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/util-endpoints': 3.996.2 + '@aws-sdk/util-user-agent-browser': 3.972.5 + '@aws-sdk/util-user-agent-node': 3.972.13 '@smithy/config-resolver': 4.4.9 '@smithy/core': 3.23.6 '@smithy/fetch-http-handler': 5.3.11 @@ -6316,69 +5867,10 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/client-sso@3.993.0': - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/middleware-host-header': 3.972.3 - '@aws-sdk/middleware-logger': 3.972.3 - '@aws-sdk/middleware-recursion-detection': 3.972.3 - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/region-config-resolver': 3.972.3 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.993.0 - '@aws-sdk/util-user-agent-browser': 3.972.3 - '@aws-sdk/util-user-agent-node': 3.972.10 - '@smithy/config-resolver': 4.4.6 - '@smithy/core': 3.23.2 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/hash-node': 4.2.8 - '@smithy/invalid-dependency': 4.2.8 - '@smithy/middleware-content-length': 4.2.8 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-retry': 4.4.33 - '@smithy/middleware-serde': 4.2.9 - '@smithy/middleware-stack': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/node-http-handler': 4.4.10 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-body-length-node': 4.2.1 - '@smithy/util-defaults-mode-browser': 4.3.32 - '@smithy/util-defaults-mode-node': 4.2.35 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/core@3.973.11': - dependencies: - '@aws-sdk/types': 3.973.1 - '@aws-sdk/xml-builder': 3.972.5 - '@smithy/core': 3.23.2 - '@smithy/node-config-provider': 4.3.8 - '@smithy/property-provider': 4.2.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/signature-v4': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/util-base64': 4.3.0 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - - '@aws-sdk/core@3.973.13': + '@aws-sdk/core@3.973.14': dependencies: - '@aws-sdk/types': 3.973.2 - '@aws-sdk/xml-builder': 3.972.6 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/xml-builder': 3.972.7 '@smithy/core': 3.23.6 '@smithy/node-config-provider': 4.3.10 '@smithy/property-provider': 4.2.10 @@ -6391,39 +5883,18 @@ snapshots: '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@aws-sdk/credential-provider-env@3.972.11': + '@aws-sdk/credential-provider-env@3.972.12': dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/credential-provider-env@3.972.9': + '@aws-sdk/credential-provider-http@3.972.14': dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/credential-provider-http@3.972.11': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/types': 3.973.1 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/node-http-handler': 4.4.10 - '@smithy/property-provider': 4.2.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/util-stream': 4.5.12 - tslib: 2.8.1 - - '@aws-sdk/credential-provider-http@3.972.13': - dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/types': 3.973.3 '@smithy/fetch-http-handler': 5.3.11 '@smithy/node-http-handler': 4.4.12 '@smithy/property-provider': 4.2.10 @@ -6433,17 +5904,17 @@ snapshots: '@smithy/util-stream': 4.5.15 tslib: 2.8.1 - '@aws-sdk/credential-provider-ini@3.972.11': - dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/credential-provider-env': 3.972.11 - '@aws-sdk/credential-provider-http': 3.972.13 - '@aws-sdk/credential-provider-login': 3.972.11 - '@aws-sdk/credential-provider-process': 3.972.11 - '@aws-sdk/credential-provider-sso': 3.972.11 - '@aws-sdk/credential-provider-web-identity': 3.972.11 - '@aws-sdk/nested-clients': 3.996.1 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/credential-provider-ini@3.972.12': + dependencies: + '@aws-sdk/core': 3.973.14 + '@aws-sdk/credential-provider-env': 3.972.12 + '@aws-sdk/credential-provider-http': 3.972.14 + '@aws-sdk/credential-provider-login': 3.972.12 + '@aws-sdk/credential-provider-process': 3.972.12 + '@aws-sdk/credential-provider-sso': 3.972.12 + '@aws-sdk/credential-provider-web-identity': 3.972.12 + '@aws-sdk/nested-clients': 3.996.2 + '@aws-sdk/types': 3.973.3 '@smithy/credential-provider-imds': 4.2.10 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 @@ -6452,30 +5923,11 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/credential-provider-ini@3.972.9': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/credential-provider-env': 3.972.9 - '@aws-sdk/credential-provider-http': 3.972.11 - '@aws-sdk/credential-provider-login': 3.972.9 - '@aws-sdk/credential-provider-process': 3.972.9 - '@aws-sdk/credential-provider-sso': 3.972.9 - '@aws-sdk/credential-provider-web-identity': 3.972.9 - '@aws-sdk/nested-clients': 3.993.0 - '@aws-sdk/types': 3.973.1 - '@smithy/credential-provider-imds': 4.2.8 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/credential-provider-login@3.972.11': + '@aws-sdk/credential-provider-login@3.972.12': dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/nested-clients': 3.996.1 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/nested-clients': 3.996.2 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/protocol-http': 5.3.10 '@smithy/shared-ini-file-loader': 4.4.5 @@ -6484,45 +5936,15 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/credential-provider-login@3.972.9': + '@aws-sdk/credential-provider-node@3.972.13': dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/nested-clients': 3.993.0 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/credential-provider-node@3.972.10': - dependencies: - '@aws-sdk/credential-provider-env': 3.972.9 - '@aws-sdk/credential-provider-http': 3.972.11 - '@aws-sdk/credential-provider-ini': 3.972.9 - '@aws-sdk/credential-provider-process': 3.972.9 - '@aws-sdk/credential-provider-sso': 3.972.9 - '@aws-sdk/credential-provider-web-identity': 3.972.9 - '@aws-sdk/types': 3.973.1 - '@smithy/credential-provider-imds': 4.2.8 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/credential-provider-node@3.972.12': - dependencies: - '@aws-sdk/credential-provider-env': 3.972.11 - '@aws-sdk/credential-provider-http': 3.972.13 - '@aws-sdk/credential-provider-ini': 3.972.11 - '@aws-sdk/credential-provider-process': 3.972.11 - '@aws-sdk/credential-provider-sso': 3.972.11 - '@aws-sdk/credential-provider-web-identity': 3.972.11 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/credential-provider-env': 3.972.12 + '@aws-sdk/credential-provider-http': 3.972.14 + '@aws-sdk/credential-provider-ini': 3.972.12 + '@aws-sdk/credential-provider-process': 3.972.12 + '@aws-sdk/credential-provider-sso': 3.972.12 + '@aws-sdk/credential-provider-web-identity': 3.972.12 + '@aws-sdk/types': 3.973.3 '@smithy/credential-provider-imds': 4.2.10 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 @@ -6531,30 +5953,21 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/credential-provider-process@3.972.11': + '@aws-sdk/credential-provider-process@3.972.12': dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/credential-provider-process@3.972.9': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/credential-provider-sso@3.972.11': + '@aws-sdk/credential-provider-sso@3.972.12': dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/nested-clients': 3.996.1 - '@aws-sdk/token-providers': 3.997.0 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/nested-clients': 3.996.2 + '@aws-sdk/token-providers': 3.998.0 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 '@smithy/types': 4.13.0 @@ -6562,24 +5975,11 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/credential-provider-sso@3.972.9': + '@aws-sdk/credential-provider-web-identity@3.972.12': dependencies: - '@aws-sdk/client-sso': 3.993.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/token-providers': 3.993.0 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/credential-provider-web-identity@3.972.11': - dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/nested-clients': 3.996.1 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/nested-clients': 3.996.2 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 '@smithy/types': 4.13.0 @@ -6587,127 +5987,55 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/credential-provider-web-identity@3.972.9': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/nested-clients': 3.993.0 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/eventstream-handler-node@3.972.5': - dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/eventstream-codec': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/eventstream-handler-node@3.972.7': + '@aws-sdk/eventstream-handler-node@3.972.8': dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/eventstream-codec': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-eventstream@3.972.3': + '@aws-sdk/middleware-eventstream@3.972.5': dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-eventstream@3.972.4': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/protocol-http': 5.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-host-header@3.972.3': - dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-host-header@3.972.4': + '@aws-sdk/middleware-host-header@3.972.5': dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/protocol-http': 5.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-logger@3.972.3': - dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-logger@3.972.4': + '@aws-sdk/middleware-logger@3.972.5': dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-recursion-detection@3.972.3': + '@aws-sdk/middleware-recursion-detection@3.972.5': dependencies: - '@aws-sdk/types': 3.973.1 - '@aws/lambda-invoke-store': 0.2.3 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-recursion-detection@3.972.4': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@aws/lambda-invoke-store': 0.2.3 '@smithy/protocol-http': 5.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-user-agent@3.972.11': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.993.0 - '@smithy/core': 3.23.2 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-user-agent@3.972.13': + '@aws-sdk/middleware-user-agent@3.972.14': dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/types': 3.973.2 - '@aws-sdk/util-endpoints': 3.996.1 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/util-endpoints': 3.996.2 '@smithy/core': 3.23.6 '@smithy/protocol-http': 5.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/middleware-websocket@3.972.6': - dependencies: - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-format-url': 3.972.3 - '@smithy/eventstream-codec': 4.2.8 - '@smithy/eventstream-serde-browser': 4.2.8 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/protocol-http': 5.3.8 - '@smithy/signature-v4': 5.3.8 - '@smithy/types': 4.12.0 - '@smithy/util-base64': 4.3.0 - '@smithy/util-hex-encoding': 4.2.0 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - - '@aws-sdk/middleware-websocket@3.972.8': + '@aws-sdk/middleware-websocket@3.972.9': dependencies: - '@aws-sdk/types': 3.973.2 - '@aws-sdk/util-format-url': 3.972.4 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/util-format-url': 3.972.5 '@smithy/eventstream-codec': 4.2.10 '@smithy/eventstream-serde-browser': 4.2.10 '@smithy/fetch-http-handler': 5.3.11 @@ -6719,106 +6047,20 @@ snapshots: '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@aws-sdk/nested-clients@3.993.0': + '@aws-sdk/nested-clients@3.996.2': dependencies: '@aws-crypto/sha256-browser': 5.2.0 '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/middleware-host-header': 3.972.3 - '@aws-sdk/middleware-logger': 3.972.3 - '@aws-sdk/middleware-recursion-detection': 3.972.3 - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/region-config-resolver': 3.972.3 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.993.0 - '@aws-sdk/util-user-agent-browser': 3.972.3 - '@aws-sdk/util-user-agent-node': 3.972.10 - '@smithy/config-resolver': 4.4.6 - '@smithy/core': 3.23.2 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/hash-node': 4.2.8 - '@smithy/invalid-dependency': 4.2.8 - '@smithy/middleware-content-length': 4.2.8 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-retry': 4.4.33 - '@smithy/middleware-serde': 4.2.9 - '@smithy/middleware-stack': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/node-http-handler': 4.4.10 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-body-length-node': 4.2.1 - '@smithy/util-defaults-mode-browser': 4.3.32 - '@smithy/util-defaults-mode-node': 4.2.35 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/nested-clients@3.995.0': - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.11 - '@aws-sdk/middleware-host-header': 3.972.3 - '@aws-sdk/middleware-logger': 3.972.3 - '@aws-sdk/middleware-recursion-detection': 3.972.3 - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/region-config-resolver': 3.972.3 - '@aws-sdk/types': 3.973.1 - '@aws-sdk/util-endpoints': 3.995.0 - '@aws-sdk/util-user-agent-browser': 3.972.3 - '@aws-sdk/util-user-agent-node': 3.972.10 - '@smithy/config-resolver': 4.4.6 - '@smithy/core': 3.23.2 - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/hash-node': 4.2.8 - '@smithy/invalid-dependency': 4.2.8 - '@smithy/middleware-content-length': 4.2.8 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-retry': 4.4.33 - '@smithy/middleware-serde': 4.2.9 - '@smithy/middleware-stack': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/node-http-handler': 4.4.10 - '@smithy/protocol-http': 5.3.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-body-length-node': 4.2.1 - '@smithy/util-defaults-mode-browser': 4.3.32 - '@smithy/util-defaults-mode-node': 4.2.35 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/nested-clients@3.996.1': - dependencies: - '@aws-crypto/sha256-browser': 5.2.0 - '@aws-crypto/sha256-js': 5.2.0 - '@aws-sdk/core': 3.973.13 - '@aws-sdk/middleware-host-header': 3.972.4 - '@aws-sdk/middleware-logger': 3.972.4 - '@aws-sdk/middleware-recursion-detection': 3.972.4 - '@aws-sdk/middleware-user-agent': 3.972.13 - '@aws-sdk/region-config-resolver': 3.972.4 - '@aws-sdk/types': 3.973.2 - '@aws-sdk/util-endpoints': 3.996.1 - '@aws-sdk/util-user-agent-browser': 3.972.4 - '@aws-sdk/util-user-agent-node': 3.972.12 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/middleware-host-header': 3.972.5 + '@aws-sdk/middleware-logger': 3.972.5 + '@aws-sdk/middleware-recursion-detection': 3.972.5 + '@aws-sdk/middleware-user-agent': 3.972.14 + '@aws-sdk/region-config-resolver': 3.972.5 + '@aws-sdk/types': 3.973.3 + '@aws-sdk/util-endpoints': 3.996.2 + '@aws-sdk/util-user-agent-browser': 3.972.5 + '@aws-sdk/util-user-agent-node': 3.972.13 '@smithy/config-resolver': 4.4.9 '@smithy/core': 3.23.6 '@smithy/fetch-http-handler': 5.3.11 @@ -6848,51 +6090,19 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/region-config-resolver@3.972.3': + '@aws-sdk/region-config-resolver@3.972.5': dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/config-resolver': 4.4.6 - '@smithy/node-config-provider': 4.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/region-config-resolver@3.972.4': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/config-resolver': 4.4.9 '@smithy/node-config-provider': 4.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/token-providers@3.993.0': - dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/nested-clients': 3.993.0 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/token-providers@3.995.0': + '@aws-sdk/token-providers@3.998.0': dependencies: - '@aws-sdk/core': 3.973.11 - '@aws-sdk/nested-clients': 3.995.0 - '@aws-sdk/types': 3.973.1 - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - transitivePeerDependencies: - - aws-crt - - '@aws-sdk/token-providers@3.997.0': - dependencies: - '@aws-sdk/core': 3.973.13 - '@aws-sdk/nested-clients': 3.996.1 - '@aws-sdk/types': 3.973.2 + '@aws-sdk/core': 3.973.14 + '@aws-sdk/nested-clients': 3.996.2 + '@aws-sdk/types': 3.973.3 '@smithy/property-provider': 4.2.10 '@smithy/shared-ini-file-loader': 4.4.5 '@smithy/types': 4.13.0 @@ -6900,50 +6110,22 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/types@3.973.1': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/types@3.973.2': + '@aws-sdk/types@3.973.3': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/util-endpoints@3.993.0': + '@aws-sdk/util-endpoints@3.996.2': dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-endpoints': 3.2.8 - tslib: 2.8.1 - - '@aws-sdk/util-endpoints@3.995.0': - dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-endpoints': 3.2.8 - tslib: 2.8.1 - - '@aws-sdk/util-endpoints@3.996.1': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/types': 4.13.0 '@smithy/url-parser': 4.2.10 '@smithy/util-endpoints': 3.3.1 tslib: 2.8.1 - '@aws-sdk/util-format-url@3.972.3': + '@aws-sdk/util-format-url@3.972.5': dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/querystring-builder': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/util-format-url@3.972.4': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/querystring-builder': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 @@ -6952,43 +6134,22 @@ snapshots: dependencies: tslib: 2.8.1 - '@aws-sdk/util-user-agent-browser@3.972.3': + '@aws-sdk/util-user-agent-browser@3.972.5': dependencies: - '@aws-sdk/types': 3.973.1 - '@smithy/types': 4.12.0 - bowser: 2.14.1 - tslib: 2.8.1 - - '@aws-sdk/util-user-agent-browser@3.972.4': - dependencies: - '@aws-sdk/types': 3.973.2 + '@aws-sdk/types': 3.973.3 '@smithy/types': 4.13.0 bowser: 2.14.1 tslib: 2.8.1 - '@aws-sdk/util-user-agent-node@3.972.10': - dependencies: - '@aws-sdk/middleware-user-agent': 3.972.11 - '@aws-sdk/types': 3.973.1 - '@smithy/node-config-provider': 4.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@aws-sdk/util-user-agent-node@3.972.12': - dependencies: - '@aws-sdk/middleware-user-agent': 3.972.13 - '@aws-sdk/types': 3.973.2 - '@smithy/node-config-provider': 4.3.10 - '@smithy/types': 4.13.0 - tslib: 2.8.1 - - '@aws-sdk/xml-builder@3.972.5': + '@aws-sdk/util-user-agent-node@3.972.13': dependencies: - '@smithy/types': 4.12.0 - fast-xml-parser: 5.3.6 + '@aws-sdk/middleware-user-agent': 3.972.14 + '@aws-sdk/types': 3.973.3 + '@smithy/node-config-provider': 4.3.10 + '@smithy/types': 4.13.0 tslib: 2.8.1 - '@aws-sdk/xml-builder@3.972.6': + '@aws-sdk/xml-builder@3.972.7': dependencies: '@smithy/types': 4.13.0 fast-xml-parser: 5.3.6 @@ -7016,11 +6177,11 @@ snapshots: transitivePeerDependencies: - supports-color - '@azure/msal-common@16.0.4': {} + '@azure/msal-common@16.1.0': {} - '@azure/msal-node@5.0.4': + '@azure/msal-node@5.0.5': dependencies: - '@azure/msal-common': 16.0.4 + '@azure/msal-common': 16.1.0 jsonwebtoken: 9.0.3 uuid: 8.3.2 @@ -7065,26 +6226,6 @@ snapshots: '@borewit/text-codec@0.2.1': {} - '@buape/carbon@0.0.0-beta-20260216184201(@discordjs/opus@0.10.0)(hono@4.11.10)(opusscript@0.0.8)': - dependencies: - '@types/node': 25.3.0 - discord-api-types: 0.38.37 - optionalDependencies: - '@cloudflare/workers-types': 4.20260120.0 - '@discordjs/voice': 0.19.0(@discordjs/opus@0.10.0)(opusscript@0.0.8) - '@hono/node-server': 1.19.9(hono@4.11.10) - '@types/bun': 1.3.9 - '@types/ws': 8.18.1 - ws: 8.19.0 - transitivePeerDependencies: - - '@discordjs/opus' - - bufferutil - - ffmpeg-static - - hono - - node-opus - - opusscript - - utf-8-validate - '@buape/carbon@0.0.0-beta-20260216184201(@discordjs/opus@0.10.0)(hono@4.11.10)(opusscript@0.1.1)': dependencies: '@types/node': 25.3.0 @@ -7241,21 +6382,6 @@ snapshots: - supports-color optional: true - '@discordjs/voice@0.19.0(@discordjs/opus@0.10.0)(opusscript@0.0.8)': - dependencies: - '@types/ws': 8.18.1 - discord-api-types: 0.38.40 - prism-media: 1.3.5(@discordjs/opus@0.10.0)(opusscript@0.0.8) - tslib: 2.8.1 - ws: 8.19.0 - transitivePeerDependencies: - - '@discordjs/opus' - - bufferutil - - ffmpeg-static - - node-opus - - opusscript - - utf-8-validate - '@discordjs/voice@0.19.0(@discordjs/opus@0.10.0)(opusscript@0.1.1)': dependencies: '@types/ws': 8.18.1 @@ -7602,7 +6728,7 @@ snapshots: '@larksuiteoapi/node-sdk@1.59.0': dependencies: - axios: 1.13.5 + axios: 1.13.5(debug@4.4.3) lodash.identity: 3.0.0 lodash.merge: 4.6.2 lodash.pickby: 4.6.0 @@ -7618,7 +6744,7 @@ snapshots: dependencies: '@types/node': 24.10.13 optionalDependencies: - axios: 1.13.5 + axios: 1.13.5(debug@4.4.3) transitivePeerDependencies: - debug @@ -7713,9 +6839,9 @@ snapshots: std-env: 3.10.0 yoctocolors: 2.1.2 - '@mariozechner/pi-agent-core@0.54.1(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-agent-core@0.55.0(ws@8.19.0)(zod@4.3.6)': dependencies: - '@mariozechner/pi-ai': 0.54.1(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-ai': 0.55.0(ws@8.19.0)(zod@4.3.6) transitivePeerDependencies: - '@modelcontextprotocol/sdk' - aws-crt @@ -7725,9 +6851,9 @@ snapshots: - ws - zod - '@mariozechner/pi-agent-core@0.55.0(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-agent-core@0.55.1(ws@8.19.0)(zod@4.3.6)': dependencies: - '@mariozechner/pi-ai': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-ai': 0.55.1(ws@8.19.0)(zod@4.3.6) transitivePeerDependencies: - '@modelcontextprotocol/sdk' - aws-crt @@ -7737,10 +6863,10 @@ snapshots: - ws - zod - '@mariozechner/pi-ai@0.54.1(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-ai@0.55.0(ws@8.19.0)(zod@4.3.6)': dependencies: '@anthropic-ai/sdk': 0.73.0(zod@4.3.6) - '@aws-sdk/client-bedrock-runtime': 3.995.0 + '@aws-sdk/client-bedrock-runtime': 3.998.0 '@google/genai': 1.42.0 '@mistralai/mistralai': 1.10.0 '@sinclair/typebox': 0.34.48 @@ -7761,10 +6887,10 @@ snapshots: - ws - zod - '@mariozechner/pi-ai@0.55.0(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-ai@0.55.1(ws@8.19.0)(zod@4.3.6)': dependencies: '@anthropic-ai/sdk': 0.73.0(zod@4.3.6) - '@aws-sdk/client-bedrock-runtime': 3.997.0 + '@aws-sdk/client-bedrock-runtime': 3.998.0 '@google/genai': 1.42.0 '@mistralai/mistralai': 1.10.0 '@sinclair/typebox': 0.34.48 @@ -7785,12 +6911,12 @@ snapshots: - ws - zod - '@mariozechner/pi-coding-agent@0.54.1(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-coding-agent@0.55.0(ws@8.19.0)(zod@4.3.6)': dependencies: '@mariozechner/jiti': 2.6.5 - '@mariozechner/pi-agent-core': 0.54.1(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-ai': 0.54.1(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-tui': 0.54.1 + '@mariozechner/pi-agent-core': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-ai': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-tui': 0.55.0 '@silvia-odwyer/photon-node': 0.3.4 chalk: 5.6.2 cli-highlight: 2.1.11 @@ -7814,16 +6940,17 @@ snapshots: - ws - zod - '@mariozechner/pi-coding-agent@0.55.0(ws@8.19.0)(zod@4.3.6)': + '@mariozechner/pi-coding-agent@0.55.1(ws@8.19.0)(zod@4.3.6)': dependencies: '@mariozechner/jiti': 2.6.5 - '@mariozechner/pi-agent-core': 0.55.0(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-ai': 0.55.0(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-tui': 0.55.0 + '@mariozechner/pi-agent-core': 0.55.1(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-ai': 0.55.1(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-tui': 0.55.1 '@silvia-odwyer/photon-node': 0.3.4 chalk: 5.6.2 cli-highlight: 2.1.11 diff: 8.0.3 + extract-zip: 2.0.1 file-type: 21.3.0 glob: 13.0.6 hosted-git-info: 9.0.2 @@ -7843,7 +6970,7 @@ snapshots: - ws - zod - '@mariozechner/pi-tui@0.54.1': + '@mariozechner/pi-tui@0.55.0': dependencies: '@types/mime-types': 2.1.4 chalk: 5.6.2 @@ -7852,7 +6979,7 @@ snapshots: marked: 15.0.12 mime-types: 3.0.2 - '@mariozechner/pi-tui@0.55.0': + '@mariozechner/pi-tui@0.55.1': dependencies: '@types/mime-types': 2.1.4 chalk: 5.6.2 @@ -7879,9 +7006,9 @@ snapshots: '@microsoft/agents-hosting@1.3.1': dependencies: '@azure/core-auth': 1.10.1 - '@azure/msal-node': 5.0.4 + '@azure/msal-node': 5.0.5 '@microsoft/agents-activity': 1.3.1 - axios: 1.13.5 + axios: 1.13.5(debug@4.4.3) jsonwebtoken: 9.0.3 jwks-rsa: 3.2.2 object-path: 0.11.8 @@ -7897,99 +7024,52 @@ snapshots: '@mozilla/readability@0.6.0': {} - '@napi-rs/canvas-android-arm64@0.1.92': - optional: true - - '@napi-rs/canvas-android-arm64@0.1.94': - optional: true - - '@napi-rs/canvas-darwin-arm64@0.1.92': - optional: true - - '@napi-rs/canvas-darwin-arm64@0.1.94': - optional: true - - '@napi-rs/canvas-darwin-x64@0.1.92': - optional: true - - '@napi-rs/canvas-darwin-x64@0.1.94': - optional: true - - '@napi-rs/canvas-linux-arm-gnueabihf@0.1.92': + '@napi-rs/canvas-android-arm64@0.1.95': optional: true - '@napi-rs/canvas-linux-arm-gnueabihf@0.1.94': + '@napi-rs/canvas-darwin-arm64@0.1.95': optional: true - '@napi-rs/canvas-linux-arm64-gnu@0.1.92': + '@napi-rs/canvas-darwin-x64@0.1.95': optional: true - '@napi-rs/canvas-linux-arm64-gnu@0.1.94': + '@napi-rs/canvas-linux-arm-gnueabihf@0.1.95': optional: true - '@napi-rs/canvas-linux-arm64-musl@0.1.92': + '@napi-rs/canvas-linux-arm64-gnu@0.1.95': optional: true - '@napi-rs/canvas-linux-arm64-musl@0.1.94': + '@napi-rs/canvas-linux-arm64-musl@0.1.95': optional: true - '@napi-rs/canvas-linux-riscv64-gnu@0.1.92': + '@napi-rs/canvas-linux-riscv64-gnu@0.1.95': optional: true - '@napi-rs/canvas-linux-riscv64-gnu@0.1.94': + '@napi-rs/canvas-linux-x64-gnu@0.1.95': optional: true - '@napi-rs/canvas-linux-x64-gnu@0.1.92': + '@napi-rs/canvas-linux-x64-musl@0.1.95': optional: true - '@napi-rs/canvas-linux-x64-gnu@0.1.94': + '@napi-rs/canvas-win32-arm64-msvc@0.1.95': optional: true - '@napi-rs/canvas-linux-x64-musl@0.1.92': + '@napi-rs/canvas-win32-x64-msvc@0.1.95': optional: true - '@napi-rs/canvas-linux-x64-musl@0.1.94': - optional: true - - '@napi-rs/canvas-win32-arm64-msvc@0.1.92': - optional: true - - '@napi-rs/canvas-win32-arm64-msvc@0.1.94': - optional: true - - '@napi-rs/canvas-win32-x64-msvc@0.1.92': - optional: true - - '@napi-rs/canvas-win32-x64-msvc@0.1.94': - optional: true - - '@napi-rs/canvas@0.1.92': + '@napi-rs/canvas@0.1.95': optionalDependencies: - '@napi-rs/canvas-android-arm64': 0.1.92 - '@napi-rs/canvas-darwin-arm64': 0.1.92 - '@napi-rs/canvas-darwin-x64': 0.1.92 - '@napi-rs/canvas-linux-arm-gnueabihf': 0.1.92 - '@napi-rs/canvas-linux-arm64-gnu': 0.1.92 - '@napi-rs/canvas-linux-arm64-musl': 0.1.92 - '@napi-rs/canvas-linux-riscv64-gnu': 0.1.92 - '@napi-rs/canvas-linux-x64-gnu': 0.1.92 - '@napi-rs/canvas-linux-x64-musl': 0.1.92 - '@napi-rs/canvas-win32-arm64-msvc': 0.1.92 - '@napi-rs/canvas-win32-x64-msvc': 0.1.92 - - '@napi-rs/canvas@0.1.94': - optionalDependencies: - '@napi-rs/canvas-android-arm64': 0.1.94 - '@napi-rs/canvas-darwin-arm64': 0.1.94 - '@napi-rs/canvas-darwin-x64': 0.1.94 - '@napi-rs/canvas-linux-arm-gnueabihf': 0.1.94 - '@napi-rs/canvas-linux-arm64-gnu': 0.1.94 - '@napi-rs/canvas-linux-arm64-musl': 0.1.94 - '@napi-rs/canvas-linux-riscv64-gnu': 0.1.94 - '@napi-rs/canvas-linux-x64-gnu': 0.1.94 - '@napi-rs/canvas-linux-x64-musl': 0.1.94 - '@napi-rs/canvas-win32-arm64-msvc': 0.1.94 - '@napi-rs/canvas-win32-x64-msvc': 0.1.94 + '@napi-rs/canvas-android-arm64': 0.1.95 + '@napi-rs/canvas-darwin-arm64': 0.1.95 + '@napi-rs/canvas-darwin-x64': 0.1.95 + '@napi-rs/canvas-linux-arm-gnueabihf': 0.1.95 + '@napi-rs/canvas-linux-arm64-gnu': 0.1.95 + '@napi-rs/canvas-linux-arm64-musl': 0.1.95 + '@napi-rs/canvas-linux-riscv64-gnu': 0.1.95 + '@napi-rs/canvas-linux-x64-gnu': 0.1.95 + '@napi-rs/canvas-linux-x64-musl': 0.1.95 + '@napi-rs/canvas-win32-arm64-msvc': 0.1.95 + '@napi-rs/canvas-win32-x64-msvc': 0.1.95 '@napi-rs/wasm-runtime@1.1.1': dependencies: @@ -8061,7 +7141,7 @@ snapshots: dependencies: '@octokit/auth-oauth-app': 9.0.3 '@octokit/auth-oauth-user': 6.0.2 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/request-error': 7.1.0 '@octokit/types': 16.0.0 toad-cache: 3.7.0 @@ -8072,14 +7152,14 @@ snapshots: dependencies: '@octokit/auth-oauth-device': 8.0.3 '@octokit/auth-oauth-user': 6.0.2 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/types': 16.0.0 universal-user-agent: 7.0.3 '@octokit/auth-oauth-device@8.0.3': dependencies: '@octokit/oauth-methods': 6.0.2 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/types': 16.0.0 universal-user-agent: 7.0.3 @@ -8087,7 +7167,7 @@ snapshots: dependencies: '@octokit/auth-oauth-device': 8.0.3 '@octokit/oauth-methods': 6.0.2 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/types': 16.0.0 universal-user-agent: 7.0.3 @@ -8102,20 +7182,20 @@ snapshots: dependencies: '@octokit/auth-token': 6.0.0 '@octokit/graphql': 9.0.3 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/request-error': 7.1.0 '@octokit/types': 16.0.0 before-after-hook: 4.0.0 universal-user-agent: 7.0.3 - '@octokit/endpoint@11.0.2': + '@octokit/endpoint@11.0.3': dependencies: '@octokit/types': 16.0.0 universal-user-agent: 7.0.3 '@octokit/graphql@9.0.3': dependencies: - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/types': 16.0.0 universal-user-agent: 7.0.3 @@ -8135,7 +7215,7 @@ snapshots: '@octokit/oauth-methods@6.0.2': dependencies: '@octokit/oauth-authorization-url': 8.0.0 - '@octokit/request': 10.0.7 + '@octokit/request': 10.0.8 '@octokit/request-error': 7.1.0 '@octokit/types': 16.0.0 @@ -8157,7 +7237,7 @@ snapshots: '@octokit/core': 7.0.6 '@octokit/types': 16.0.0 - '@octokit/plugin-retry@8.0.3(@octokit/core@7.0.6)': + '@octokit/plugin-retry@8.1.0(@octokit/core@7.0.6)': dependencies: '@octokit/core': 7.0.6 '@octokit/request-error': 7.1.0 @@ -8174,12 +7254,13 @@ snapshots: dependencies: '@octokit/types': 16.0.0 - '@octokit/request@10.0.7': + '@octokit/request@10.0.8': dependencies: - '@octokit/endpoint': 11.0.2 + '@octokit/endpoint': 11.0.3 '@octokit/request-error': 7.1.0 '@octokit/types': 16.0.0 fast-content-type-parse: 3.0.0 + json-with-bigint: 3.5.3 universal-user-agent: 7.0.3 '@octokit/types@16.0.0': @@ -8782,7 +7863,7 @@ snapshots: '@slack/types': 2.20.0 '@slack/web-api': 7.14.1 '@types/express': 5.0.6 - axios: 1.13.5 + axios: 1.13.5(debug@4.4.3) express: 5.2.1 path-to-regexp: 8.3.0 raw-body: 3.0.2 @@ -8828,7 +7909,7 @@ snapshots: '@slack/types': 2.20.0 '@types/node': 25.3.0 '@types/retry': 0.12.0 - axios: 1.13.5 + axios: 1.13.5(debug@4.4.3) eventemitter3: 5.0.4 form-data: 2.5.4 is-electron: 2.2.2 @@ -8844,20 +7925,6 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/abort-controller@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@smithy/config-resolver@4.4.6': - dependencies: - '@smithy/node-config-provider': 4.3.8 - '@smithy/types': 4.12.0 - '@smithy/util-config-provider': 4.2.0 - '@smithy/util-endpoints': 3.2.8 - '@smithy/util-middleware': 4.2.8 - tslib: 2.8.1 - '@smithy/config-resolver@4.4.9': dependencies: '@smithy/node-config-provider': 4.3.10 @@ -8867,19 +7934,6 @@ snapshots: '@smithy/util-middleware': 4.2.10 tslib: 2.8.1 - '@smithy/core@3.23.2': - dependencies: - '@smithy/middleware-serde': 4.2.9 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - '@smithy/util-base64': 4.3.0 - '@smithy/util-body-length-browser': 4.2.0 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-stream': 4.5.12 - '@smithy/util-utf8': 4.2.0 - '@smithy/uuid': 1.1.0 - tslib: 2.8.1 - '@smithy/core@3.23.6': dependencies: '@smithy/middleware-serde': 4.2.11 @@ -8901,14 +7955,6 @@ snapshots: '@smithy/url-parser': 4.2.10 tslib: 2.8.1 - '@smithy/credential-provider-imds@4.2.8': - dependencies: - '@smithy/node-config-provider': 4.3.8 - '@smithy/property-provider': 4.2.8 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - tslib: 2.8.1 - '@smithy/eventstream-codec@4.2.10': dependencies: '@aws-crypto/crc32': 5.2.0 @@ -8916,59 +7962,29 @@ snapshots: '@smithy/util-hex-encoding': 4.2.1 tslib: 2.8.1 - '@smithy/eventstream-codec@4.2.8': - dependencies: - '@aws-crypto/crc32': 5.2.0 - '@smithy/types': 4.12.0 - '@smithy/util-hex-encoding': 4.2.0 - tslib: 2.8.1 - '@smithy/eventstream-serde-browser@4.2.10': dependencies: '@smithy/eventstream-serde-universal': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/eventstream-serde-browser@4.2.8': - dependencies: - '@smithy/eventstream-serde-universal': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/eventstream-serde-config-resolver@4.3.10': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/eventstream-serde-config-resolver@4.3.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/eventstream-serde-node@4.2.10': dependencies: '@smithy/eventstream-serde-universal': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/eventstream-serde-node@4.2.8': - dependencies: - '@smithy/eventstream-serde-universal': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/eventstream-serde-universal@4.2.10': dependencies: '@smithy/eventstream-codec': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/eventstream-serde-universal@4.2.8': - dependencies: - '@smithy/eventstream-codec': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/fetch-http-handler@5.3.11': dependencies: '@smithy/protocol-http': 5.3.10 @@ -8977,14 +7993,6 @@ snapshots: '@smithy/util-base64': 4.3.1 tslib: 2.8.1 - '@smithy/fetch-http-handler@5.3.9': - dependencies: - '@smithy/protocol-http': 5.3.8 - '@smithy/querystring-builder': 4.2.8 - '@smithy/types': 4.12.0 - '@smithy/util-base64': 4.3.0 - tslib: 2.8.1 - '@smithy/hash-node@4.2.10': dependencies: '@smithy/types': 4.13.0 @@ -8992,31 +8000,15 @@ snapshots: '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@smithy/hash-node@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - '@smithy/util-buffer-from': 4.2.0 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - '@smithy/invalid-dependency@4.2.10': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/invalid-dependency@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/is-array-buffer@2.2.0': dependencies: tslib: 2.8.1 - '@smithy/is-array-buffer@4.2.0': - dependencies: - tslib: 2.8.1 - '@smithy/is-array-buffer@4.2.1': dependencies: tslib: 2.8.1 @@ -9027,23 +8019,6 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/middleware-content-length@4.2.8': - dependencies: - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@smithy/middleware-endpoint@4.4.16': - dependencies: - '@smithy/core': 3.23.2 - '@smithy/middleware-serde': 4.2.9 - '@smithy/node-config-provider': 4.3.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - '@smithy/url-parser': 4.2.8 - '@smithy/util-middleware': 4.2.8 - tslib: 2.8.1 - '@smithy/middleware-endpoint@4.4.20': dependencies: '@smithy/core': 3.23.6 @@ -9055,18 +8030,6 @@ snapshots: '@smithy/util-middleware': 4.2.10 tslib: 2.8.1 - '@smithy/middleware-retry@4.4.33': - dependencies: - '@smithy/node-config-provider': 4.3.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/service-error-classification': 4.2.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-retry': 4.2.8 - '@smithy/uuid': 1.1.0 - tslib: 2.8.1 - '@smithy/middleware-retry@4.4.37': dependencies: '@smithy/node-config-provider': 4.3.10 @@ -9085,22 +8048,11 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/middleware-serde@4.2.9': - dependencies: - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/middleware-stack@4.2.10': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/middleware-stack@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/node-config-provider@4.3.10': dependencies: '@smithy/property-provider': 4.2.10 @@ -9108,21 +8060,6 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/node-config-provider@4.3.8': - dependencies: - '@smithy/property-provider': 4.2.8 - '@smithy/shared-ini-file-loader': 4.4.3 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@smithy/node-http-handler@4.4.10': - dependencies: - '@smithy/abort-controller': 4.2.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/querystring-builder': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/node-http-handler@4.4.12': dependencies: '@smithy/abort-controller': 4.2.10 @@ -9136,56 +8073,26 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/property-provider@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/protocol-http@5.3.10': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/protocol-http@5.3.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/querystring-builder@4.2.10': dependencies: '@smithy/types': 4.13.0 '@smithy/util-uri-escape': 4.2.1 tslib: 2.8.1 - '@smithy/querystring-builder@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - '@smithy/util-uri-escape': 4.2.0 - tslib: 2.8.1 - '@smithy/querystring-parser@4.2.10': dependencies: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/querystring-parser@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/service-error-classification@4.2.10': dependencies: '@smithy/types': 4.13.0 - '@smithy/service-error-classification@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - - '@smithy/shared-ini-file-loader@4.4.3': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/shared-ini-file-loader@4.4.5': dependencies: '@smithy/types': 4.13.0 @@ -9202,27 +8109,6 @@ snapshots: '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@smithy/signature-v4@5.3.8': - dependencies: - '@smithy/is-array-buffer': 4.2.0 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - '@smithy/util-hex-encoding': 4.2.0 - '@smithy/util-middleware': 4.2.8 - '@smithy/util-uri-escape': 4.2.0 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - - '@smithy/smithy-client@4.11.5': - dependencies: - '@smithy/core': 3.23.2 - '@smithy/middleware-endpoint': 4.4.16 - '@smithy/middleware-stack': 4.2.8 - '@smithy/protocol-http': 5.3.8 - '@smithy/types': 4.12.0 - '@smithy/util-stream': 4.5.12 - tslib: 2.8.1 - '@smithy/smithy-client@4.12.0': dependencies: '@smithy/core': 3.23.6 @@ -9233,10 +8119,6 @@ snapshots: '@smithy/util-stream': 4.5.15 tslib: 2.8.1 - '@smithy/types@4.12.0': - dependencies: - tslib: 2.8.1 - '@smithy/types@4.13.0': dependencies: tslib: 2.8.1 @@ -9247,36 +8129,16 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/url-parser@4.2.8': - dependencies: - '@smithy/querystring-parser': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@smithy/util-base64@4.3.0': - dependencies: - '@smithy/util-buffer-from': 4.2.0 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - '@smithy/util-base64@4.3.1': dependencies: '@smithy/util-buffer-from': 4.2.1 '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@smithy/util-body-length-browser@4.2.0': - dependencies: - tslib: 2.8.1 - '@smithy/util-body-length-browser@4.2.1': dependencies: tslib: 2.8.1 - '@smithy/util-body-length-node@4.2.1': - dependencies: - tslib: 2.8.1 - '@smithy/util-body-length-node@4.2.2': dependencies: tslib: 2.8.1 @@ -9286,31 +8148,15 @@ snapshots: '@smithy/is-array-buffer': 2.2.0 tslib: 2.8.1 - '@smithy/util-buffer-from@4.2.0': - dependencies: - '@smithy/is-array-buffer': 4.2.0 - tslib: 2.8.1 - '@smithy/util-buffer-from@4.2.1': dependencies: '@smithy/is-array-buffer': 4.2.1 tslib: 2.8.1 - '@smithy/util-config-provider@4.2.0': - dependencies: - tslib: 2.8.1 - '@smithy/util-config-provider@4.2.1': dependencies: tslib: 2.8.1 - '@smithy/util-defaults-mode-browser@4.3.32': - dependencies: - '@smithy/property-provider': 4.2.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/util-defaults-mode-browser@4.3.36': dependencies: '@smithy/property-provider': 4.2.10 @@ -9318,16 +8164,6 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/util-defaults-mode-node@4.2.35': - dependencies: - '@smithy/config-resolver': 4.4.6 - '@smithy/credential-provider-imds': 4.2.8 - '@smithy/node-config-provider': 4.3.8 - '@smithy/property-provider': 4.2.8 - '@smithy/smithy-client': 4.11.5 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/util-defaults-mode-node@4.2.39': dependencies: '@smithy/config-resolver': 4.4.9 @@ -9338,22 +8174,12 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/util-endpoints@3.2.8': - dependencies: - '@smithy/node-config-provider': 4.3.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/util-endpoints@3.3.1': dependencies: '@smithy/node-config-provider': 4.3.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/util-hex-encoding@4.2.0': - dependencies: - tslib: 2.8.1 - '@smithy/util-hex-encoding@4.2.1': dependencies: tslib: 2.8.1 @@ -9363,34 +8189,12 @@ snapshots: '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/util-middleware@4.2.8': - dependencies: - '@smithy/types': 4.12.0 - tslib: 2.8.1 - '@smithy/util-retry@4.2.10': dependencies: '@smithy/service-error-classification': 4.2.10 '@smithy/types': 4.13.0 tslib: 2.8.1 - '@smithy/util-retry@4.2.8': - dependencies: - '@smithy/service-error-classification': 4.2.8 - '@smithy/types': 4.12.0 - tslib: 2.8.1 - - '@smithy/util-stream@4.5.12': - dependencies: - '@smithy/fetch-http-handler': 5.3.9 - '@smithy/node-http-handler': 4.4.10 - '@smithy/types': 4.12.0 - '@smithy/util-base64': 4.3.0 - '@smithy/util-buffer-from': 4.2.0 - '@smithy/util-hex-encoding': 4.2.0 - '@smithy/util-utf8': 4.2.0 - tslib: 2.8.1 - '@smithy/util-stream@4.5.15': dependencies: '@smithy/fetch-http-handler': 5.3.11 @@ -9402,10 +8206,6 @@ snapshots: '@smithy/util-utf8': 4.2.1 tslib: 2.8.1 - '@smithy/util-uri-escape@4.2.0': - dependencies: - tslib: 2.8.1 - '@smithy/util-uri-escape@4.2.1': dependencies: tslib: 2.8.1 @@ -9415,20 +8215,11 @@ snapshots: '@smithy/util-buffer-from': 2.2.0 tslib: 2.8.1 - '@smithy/util-utf8@4.2.0': - dependencies: - '@smithy/util-buffer-from': 4.2.0 - tslib: 2.8.1 - '@smithy/util-utf8@4.2.1': dependencies: '@smithy/util-buffer-from': 4.2.1 tslib: 2.8.1 - '@smithy/uuid@1.1.0': - dependencies: - tslib: 2.8.1 - '@smithy/uuid@1.1.1': dependencies: tslib: 2.8.1 @@ -9718,36 +8509,41 @@ snapshots: dependencies: '@types/node': 25.3.0 - '@typescript/native-preview-darwin-arm64@7.0.0-dev.20260224.1': + '@types/yauzl@2.10.3': + dependencies: + '@types/node': 25.3.0 + optional: true + + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-darwin-x64@7.0.0-dev.20260224.1': + '@typescript/native-preview-darwin-x64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-linux-arm64@7.0.0-dev.20260224.1': + '@typescript/native-preview-linux-arm64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-linux-arm@7.0.0-dev.20260224.1': + '@typescript/native-preview-linux-arm@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-linux-x64@7.0.0-dev.20260224.1': + '@typescript/native-preview-linux-x64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-win32-arm64@7.0.0-dev.20260224.1': + '@typescript/native-preview-win32-arm64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview-win32-x64@7.0.0-dev.20260224.1': + '@typescript/native-preview-win32-x64@7.0.0-dev.20260225.1': optional: true - '@typescript/native-preview@7.0.0-dev.20260224.1': + '@typescript/native-preview@7.0.0-dev.20260225.1': optionalDependencies: - '@typescript/native-preview-darwin-arm64': 7.0.0-dev.20260224.1 - '@typescript/native-preview-darwin-x64': 7.0.0-dev.20260224.1 - '@typescript/native-preview-linux-arm': 7.0.0-dev.20260224.1 - '@typescript/native-preview-linux-arm64': 7.0.0-dev.20260224.1 - '@typescript/native-preview-linux-x64': 7.0.0-dev.20260224.1 - '@typescript/native-preview-win32-arm64': 7.0.0-dev.20260224.1 - '@typescript/native-preview-win32-x64': 7.0.0-dev.20260224.1 + '@typescript/native-preview-darwin-arm64': 7.0.0-dev.20260225.1 + '@typescript/native-preview-darwin-x64': 7.0.0-dev.20260225.1 + '@typescript/native-preview-linux-arm': 7.0.0-dev.20260225.1 + '@typescript/native-preview-linux-arm64': 7.0.0-dev.20260225.1 + '@typescript/native-preview-linux-x64': 7.0.0-dev.20260225.1 + '@typescript/native-preview-win32-arm64': 7.0.0-dev.20260225.1 + '@typescript/native-preview-win32-x64': 7.0.0-dev.20260225.1 '@typespec/ts-http-runtime@0.3.3': dependencies: @@ -10071,14 +8867,6 @@ snapshots: aws4@1.13.2: {} - axios@1.13.5: - dependencies: - follow-redirects: 1.15.11 - form-data: 2.5.4 - proxy-from-env: 1.1.0 - transitivePeerDependencies: - - debug - axios@1.13.5(debug@4.4.3): dependencies: follow-redirects: 1.15.11(debug@4.4.3) @@ -10150,6 +8938,8 @@ snapshots: dependencies: balanced-match: 4.0.4 + buffer-crc32@0.2.13: {} + buffer-equal-constant-time@1.0.1: {} buffer-from@1.1.2: {} @@ -10426,6 +9216,10 @@ snapshots: encodeurl@2.0.0: {} + end-of-stream@1.4.5: + dependencies: + once: 1.4.0 + entities@4.5.0: {} entities@7.0.1: {} @@ -10583,6 +9377,16 @@ snapshots: extend@3.0.2: {} + extract-zip@2.0.1: + dependencies: + debug: 4.4.3 + get-stream: 5.2.0 + yauzl: 2.10.0 + optionalDependencies: + '@types/yauzl': 2.10.3 + transitivePeerDependencies: + - supports-color + extsprintf@1.3.0: {} fast-content-type-parse@3.0.0: {} @@ -10595,6 +9399,10 @@ snapshots: dependencies: strnum: 2.1.2 + fd-slicer@1.1.0: + dependencies: + pend: 1.2.0 + fdir@6.5.0(picomatch@4.0.3): optionalDependencies: picomatch: 4.0.3 @@ -10648,8 +9456,6 @@ snapshots: flatbuffers@24.12.23: {} - follow-redirects@1.15.11: {} - follow-redirects@1.15.11(debug@4.4.3): optionalDependencies: debug: 4.4.3 @@ -10740,8 +9546,6 @@ snapshots: get-caller-file@2.0.5: {} - get-east-asian-width@1.4.0: {} - get-east-asian-width@1.5.0: {} get-intrinsic@1.3.0: @@ -10762,6 +9566,10 @@ snapshots: dunder-proto: 1.0.1 es-object-atoms: 1.1.1 + get-stream@5.2.0: + dependencies: + pump: 3.0.3 + get-tsconfig@4.13.6: dependencies: resolve-pkg-maps: 1.0.0 @@ -10973,7 +9781,7 @@ snapshots: ipaddr.js@2.3.0: {} - ipull@3.9.3: + ipull@3.9.5: dependencies: '@tinyhttp/content-disposition': 2.2.4 async-retry: 1.3.3 @@ -11016,7 +9824,7 @@ snapshots: is-fullwidth-code-point@5.1.0: dependencies: - get-east-asian-width: 1.4.0 + get-east-asian-width: 1.5.0 is-interactive@2.0.0: {} @@ -11088,6 +9896,8 @@ snapshots: json-stringify-safe@5.0.1: {} + json-with-bigint@3.5.3: {} + json5@2.2.3: {} jsonfile@6.2.0: @@ -11160,7 +9970,7 @@ snapshots: lifecycle-utils@2.1.0: {} - lifecycle-utils@3.1.0: {} + lifecycle-utils@3.1.1: {} lightningcss-android-arm64@1.30.2: optional: true @@ -11488,9 +10298,9 @@ snapshots: filenamify: 6.0.0 fs-extra: 11.3.3 ignore: 7.0.5 - ipull: 3.9.3 + ipull: 3.9.5 is-unicode-supported: 2.1.0 - lifecycle-utils: 3.1.0 + lifecycle-utils: 3.1.1 log-symbols: 7.0.1 nanoid: 5.1.6 node-addon-api: 8.5.0 @@ -11499,7 +10309,7 @@ snapshots: pretty-ms: 9.3.0 proper-lockfile: 4.1.2 semver: 7.7.4 - simple-git: 3.31.1 + simple-git: 3.32.2 slice-ansi: 7.1.2 stdout-update: 4.0.1 strip-ansi: 7.1.2 @@ -11584,7 +10394,7 @@ snapshots: '@octokit/plugin-paginate-graphql': 6.0.0(@octokit/core@7.0.6) '@octokit/plugin-paginate-rest': 14.0.0(@octokit/core@7.0.6) '@octokit/plugin-rest-endpoint-methods': 17.0.0(@octokit/core@7.0.6) - '@octokit/plugin-retry': 8.0.3(@octokit/core@7.0.6) + '@octokit/plugin-retry': 8.1.0(@octokit/core@7.0.6) '@octokit/plugin-throttling': 11.0.3(@octokit/core@7.0.6) '@octokit/request-error': 7.1.0 '@octokit/types': 16.0.0 @@ -11628,28 +10438,29 @@ snapshots: ws: 8.19.0 zod: 4.3.6 - openclaw@2026.2.23(@napi-rs/canvas@0.1.94)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)): + openclaw@2026.2.24(@napi-rs/canvas@0.1.95)(@types/express@5.0.6)(audio-decode@2.2.3)(hono@4.11.10)(node-llama-cpp@3.15.1(typescript@5.9.3)): dependencies: '@agentclientprotocol/sdk': 0.14.1(zod@4.3.6) - '@aws-sdk/client-bedrock': 3.995.0 - '@buape/carbon': 0.0.0-beta-20260216184201(@discordjs/opus@0.10.0)(hono@4.11.10)(opusscript@0.0.8) + '@aws-sdk/client-bedrock': 3.998.0 + '@buape/carbon': 0.0.0-beta-20260216184201(@discordjs/opus@0.10.0)(hono@4.11.10)(opusscript@0.1.1) '@clack/prompts': 1.0.1 - '@discordjs/voice': 0.19.0(@discordjs/opus@0.10.0)(opusscript@0.0.8) + '@discordjs/voice': 0.19.0(@discordjs/opus@0.10.0)(opusscript@0.1.1) '@grammyjs/runner': 2.0.3(grammy@1.40.0) '@grammyjs/transformer-throttler': 1.2.1(grammy@1.40.0) '@homebridge/ciao': 1.3.5 '@larksuiteoapi/node-sdk': 1.59.0 '@line/bot-sdk': 10.6.0 '@lydell/node-pty': 1.2.0-beta.3 - '@mariozechner/pi-agent-core': 0.54.1(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-ai': 0.54.1(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-coding-agent': 0.54.1(ws@8.19.0)(zod@4.3.6) - '@mariozechner/pi-tui': 0.54.1 + '@mariozechner/pi-agent-core': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-ai': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-coding-agent': 0.55.0(ws@8.19.0)(zod@4.3.6) + '@mariozechner/pi-tui': 0.55.0 '@mozilla/readability': 0.6.0 - '@napi-rs/canvas': 0.1.94 + '@napi-rs/canvas': 0.1.95 '@sinclair/typebox': 0.34.48 '@slack/bolt': 4.6.0(@types/express@5.0.6) '@slack/web-api': 7.14.1 + '@snazzah/davey': 0.1.9 '@whiskeysockets/baileys': 7.0.0-rc.9(audio-decode@2.2.3)(sharp@0.34.5) ajv: 8.18.0 chalk: 5.6.2 @@ -11672,7 +10483,7 @@ snapshots: markdown-it: 14.1.1 node-edge-tts: 1.2.10 node-llama-cpp: 3.15.1(typescript@5.9.3) - opusscript: 0.0.8 + opusscript: 0.1.1 osc-progress: 0.3.0 pdfjs-dist: 5.4.624 playwright-core: 1.58.2 @@ -11709,8 +10520,6 @@ snapshots: '@wasm-audio-decoders/common': 9.0.7 optional: true - opusscript@0.0.8: {} - opusscript@0.1.1: {} ora@8.2.0: @@ -11874,11 +10683,13 @@ snapshots: pdfjs-dist@5.4.624: optionalDependencies: - '@napi-rs/canvas': 0.1.94 + '@napi-rs/canvas': 0.1.95 node-readable-to-web-readable-stream: 0.4.2 peberminta@0.9.0: {} + pend@1.2.0: {} + performance-now@2.1.0: {} picocolors@1.1.1: {} @@ -11939,11 +10750,6 @@ snapshots: dependencies: parse-ms: 4.0.0 - prism-media@1.3.5(@discordjs/opus@0.10.0)(opusscript@0.0.8): - optionalDependencies: - '@discordjs/opus': 0.10.0 - opusscript: 0.0.8 - prism-media@1.3.5(@discordjs/opus@0.10.0)(opusscript@0.1.1): optionalDependencies: '@discordjs/opus': 0.10.0 @@ -12029,6 +10835,11 @@ snapshots: dependencies: punycode: 2.3.1 + pump@3.0.3: + dependencies: + end-of-stream: 1.4.5 + once: 1.4.0 + punycode.js@2.3.1: {} punycode@2.3.1: {} @@ -12137,7 +10948,7 @@ snapshots: dependencies: glob: 10.5.0 - rolldown-plugin-dts@0.22.1(@typescript/native-preview@7.0.0-dev.20260224.1)(rolldown@1.0.0-rc.3)(typescript@5.9.3): + rolldown-plugin-dts@0.22.1(@typescript/native-preview@7.0.0-dev.20260225.1)(rolldown@1.0.0-rc.3)(typescript@5.9.3): dependencies: '@babel/generator': 8.0.0-rc.1 '@babel/helper-validator-identifier': 8.0.0-rc.1 @@ -12150,7 +10961,7 @@ snapshots: obug: 2.1.1 rolldown: 1.0.0-rc.3 optionalDependencies: - '@typescript/native-preview': 7.0.0-dev.20260224.1 + '@typescript/native-preview': 7.0.0-dev.20260225.1 typescript: 5.9.3 transitivePeerDependencies: - oxc-resolver @@ -12376,7 +11187,7 @@ snapshots: dependencies: signal-polyfill: 0.2.2 - simple-git@3.31.1: + simple-git@3.32.2: dependencies: '@kwsites/file-exists': 1.1.1 '@kwsites/promise-deferred': 1.1.1 @@ -12505,7 +11316,7 @@ snapshots: string-width@7.2.0: dependencies: emoji-regex: 10.6.0 - get-east-asian-width: 1.4.0 + get-east-asian-width: 1.5.0 strip-ansi: 7.1.2 string_decoder@1.1.1: @@ -12599,7 +11410,7 @@ snapshots: ts-algebra@2.0.0: {} - tsdown@0.20.3(@typescript/native-preview@7.0.0-dev.20260224.1)(typescript@5.9.3): + tsdown@0.20.3(@typescript/native-preview@7.0.0-dev.20260225.1)(typescript@5.9.3): dependencies: ansis: 4.2.0 cac: 6.7.14 @@ -12610,7 +11421,7 @@ snapshots: obug: 2.1.1 picomatch: 4.0.3 rolldown: 1.0.0-rc.3 - rolldown-plugin-dts: 0.22.1(@typescript/native-preview@7.0.0-dev.20260224.1)(rolldown@1.0.0-rc.3)(typescript@5.9.3) + rolldown-plugin-dts: 0.22.1(@typescript/native-preview@7.0.0-dev.20260225.1)(rolldown@1.0.0-rc.3)(typescript@5.9.3) semver: 7.7.4 tinyexec: 1.0.2 tinyglobby: 0.2.15 @@ -12853,6 +11664,11 @@ snapshots: y18n: 5.0.8 yargs-parser: 21.1.1 + yauzl@2.10.0: + dependencies: + buffer-crc32: 0.2.13 + fd-slicer: 1.1.0 + yoctocolors@2.1.2: {} zod-to-json-schema@3.25.1(zod@3.25.76): From 8f8e46d898c55a5b37d4b6d6f1b446e04f104a4e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:34:37 +0100 Subject: [PATCH 057/160] refactor: unify reaction ingress policy guards across channels --- .../bluebubbles/src/monitor-processing.ts | 43 ++- .../mattermost/src/mattermost/monitor.ts | 83 ++---- src/discord/monitor/listeners.ts | 263 ++++++++++++------ src/plugin-sdk/index.ts | 1 + src/security/dm-policy-shared.test.ts | 64 +++++ src/signal/monitor/event-handler.ts | 23 +- 6 files changed, 289 insertions(+), 188 deletions(-) diff --git a/extensions/bluebubbles/src/monitor-processing.ts b/extensions/bluebubbles/src/monitor-processing.ts index 67fb50a78c63..f25e47d50e62 100644 --- a/extensions/bluebubbles/src/monitor-processing.ts +++ b/extensions/bluebubbles/src/monitor-processing.ts @@ -7,8 +7,7 @@ import { logTypingFailure, recordPendingHistoryEntryIfEnabled, resolveAckReaction, - resolveDmGroupAccessDecision, - resolveEffectiveAllowFromLists, + resolveDmGroupAccessWithLists, resolveControlCommandGate, stripMarkdown, type HistoryEntry, @@ -504,24 +503,13 @@ export async function processMessage( const storeAllowFrom = await core.channel.pairing .readAllowFromStore("bluebubbles") .catch(() => []); - const { effectiveAllowFrom, effectiveGroupAllowFrom } = resolveEffectiveAllowFromLists({ - allowFrom: account.config.allowFrom, - groupAllowFrom: account.config.groupAllowFrom, - storeAllowFrom, - dmPolicy, - }); - const groupAllowEntry = formatGroupAllowlistEntry({ - chatGuid: message.chatGuid, - chatId: message.chatId ?? undefined, - chatIdentifier: message.chatIdentifier ?? undefined, - }); - const groupName = message.chatName?.trim() || undefined; - const accessDecision = resolveDmGroupAccessDecision({ + const accessDecision = resolveDmGroupAccessWithLists({ isGroup, dmPolicy, groupPolicy, - effectiveAllowFrom, - effectiveGroupAllowFrom, + allowFrom: account.config.allowFrom, + groupAllowFrom: account.config.groupAllowFrom, + storeAllowFrom, isSenderAllowed: (allowFrom) => isAllowedBlueBubblesSender({ allowFrom, @@ -531,6 +519,14 @@ export async function processMessage( chatIdentifier: message.chatIdentifier ?? undefined, }), }); + const effectiveAllowFrom = accessDecision.effectiveAllowFrom; + const effectiveGroupAllowFrom = accessDecision.effectiveGroupAllowFrom; + const groupAllowEntry = formatGroupAllowlistEntry({ + chatGuid: message.chatGuid, + chatId: message.chatId ?? undefined, + chatIdentifier: message.chatIdentifier ?? undefined, + }); + const groupName = message.chatName?.trim() || undefined; if (accessDecision.decision !== "allow") { if (isGroup) { @@ -1389,18 +1385,13 @@ export async function processReaction( const storeAllowFrom = await core.channel.pairing .readAllowFromStore("bluebubbles") .catch(() => []); - const { effectiveAllowFrom, effectiveGroupAllowFrom } = resolveEffectiveAllowFromLists({ - allowFrom: account.config.allowFrom, - groupAllowFrom: account.config.groupAllowFrom, - storeAllowFrom, - dmPolicy, - }); - const accessDecision = resolveDmGroupAccessDecision({ + const accessDecision = resolveDmGroupAccessWithLists({ isGroup: reaction.isGroup, dmPolicy, groupPolicy, - effectiveAllowFrom, - effectiveGroupAllowFrom, + allowFrom: account.config.allowFrom, + groupAllowFrom: account.config.groupAllowFrom, + storeAllowFrom, isSenderAllowed: (allowFrom) => isAllowedBlueBubblesSender({ allowFrom, diff --git a/extensions/mattermost/src/mattermost/monitor.ts b/extensions/mattermost/src/mattermost/monitor.ts index 6056c3fef156..af8d8e07e60a 100644 --- a/extensions/mattermost/src/mattermost/monitor.ts +++ b/extensions/mattermost/src/mattermost/monitor.ts @@ -17,6 +17,7 @@ import { recordPendingHistoryEntryIfEnabled, isDangerousNameMatchingEnabled, resolveControlCommandGate, + resolveDmGroupAccessWithLists, resolveAllowlistProviderRuntimeGroupPolicy, resolveDefaultGroupPolicy, resolveChannelMediaMaxBytes, @@ -883,68 +884,38 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {} const kind = channelKind(channelInfo.type); // Enforce DM/group policy and allowlist checks (same as normal messages) - if (kind === "direct") { - const dmPolicy = account.config.dmPolicy ?? "pairing"; - if (dmPolicy === "disabled") { - logVerboseMessage(`mattermost: drop reaction (dmPolicy=disabled sender=${userId})`); - return; - } - // For pairing/allowlist modes, only allow reactions from approved senders - if (dmPolicy !== "open") { - const configAllowFrom = normalizeAllowList(account.config.allowFrom ?? []); - const storeAllowFrom = normalizeAllowList( - dmPolicy === "allowlist" - ? [] - : await core.channel.pairing.readAllowFromStore("mattermost").catch(() => []), - ); - const effectiveAllowFrom = Array.from(new Set([...configAllowFrom, ...storeAllowFrom])); - const allowed = isSenderAllowed({ + const dmPolicy = account.config.dmPolicy ?? "pairing"; + const storeAllowFrom = normalizeAllowList( + dmPolicy === "allowlist" + ? [] + : await core.channel.pairing.readAllowFromStore("mattermost").catch(() => []), + ); + const reactionAccess = resolveDmGroupAccessWithLists({ + isGroup: kind !== "direct", + dmPolicy, + groupPolicy, + allowFrom: account.config.allowFrom, + groupAllowFrom: account.config.groupAllowFrom, + storeAllowFrom, + isSenderAllowed: (allowFrom) => + isSenderAllowed({ senderId: userId, senderName, - allowFrom: effectiveAllowFrom, + allowFrom: normalizeAllowList(allowFrom), allowNameMatching, - }); - if (!allowed) { - logVerboseMessage( - `mattermost: drop reaction (dmPolicy=${dmPolicy} sender=${userId} not allowed)`, - ); - return; - } - } - } else if (kind) { - if (groupPolicy === "disabled") { - logVerboseMessage(`mattermost: drop reaction (groupPolicy=disabled channel=${channelId})`); - return; - } - if (groupPolicy === "allowlist") { - const dmPolicyForStore = account.config.dmPolicy ?? "pairing"; - const configAllowFrom = normalizeAllowList(account.config.allowFrom ?? []); - const configGroupAllowFrom = normalizeAllowList(account.config.groupAllowFrom ?? []); - const storeAllowFrom = normalizeAllowList( - dmPolicyForStore === "allowlist" - ? [] - : await core.channel.pairing.readAllowFromStore("mattermost").catch(() => []), + }), + }); + if (reactionAccess.decision !== "allow") { + if (kind === "direct") { + logVerboseMessage( + `mattermost: drop reaction (dmPolicy=${dmPolicy} sender=${userId} reason=${reactionAccess.reason})`, ); - const effectiveGroupAllowFrom = Array.from( - new Set([ - ...(configGroupAllowFrom.length > 0 ? configGroupAllowFrom : configAllowFrom), - ...storeAllowFrom, - ]), + } else { + logVerboseMessage( + `mattermost: drop reaction (groupPolicy=${groupPolicy} sender=${userId} reason=${reactionAccess.reason} channel=${channelId})`, ); - // Drop when allowlist is empty (same as normal message handler) - const allowed = - effectiveGroupAllowFrom.length > 0 && - isSenderAllowed({ - senderId: userId, - senderName, - allowFrom: effectiveGroupAllowFrom, - allowNameMatching, - }); - if (!allowed) { - logVerboseMessage(`mattermost: drop reaction (groupPolicy=allowlist sender=${userId})`); - return; - } } + return; } const teamId = channelInfo?.team_id ?? undefined; diff --git a/src/discord/monitor/listeners.ts b/src/discord/monitor/listeners.ts index 002bf62816d4..c8af895ad258 100644 --- a/src/discord/monitor/listeners.ts +++ b/src/discord/monitor/listeners.ts @@ -204,6 +204,99 @@ async function runDiscordReactionHandler(params: { }); } +type DiscordReactionIngressAuthorizationParams = { + user: User; + isDirectMessage: boolean; + isGroupDm: boolean; + isGuildMessage: boolean; + channelId: string; + channelName?: string; + channelSlug: string; + dmEnabled: boolean; + groupDmEnabled: boolean; + groupDmChannels: string[]; + dmPolicy: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom: string[]; + groupPolicy: "open" | "allowlist" | "disabled"; + allowNameMatching: boolean; + guildInfo: import("./allow-list.js").DiscordGuildEntryResolved | null; + channelConfig?: { allowed?: boolean } | null; +}; + +async function authorizeDiscordReactionIngress( + params: DiscordReactionIngressAuthorizationParams, +): Promise<{ allowed: true } | { allowed: false; reason: string }> { + if (params.isDirectMessage && !params.dmEnabled) { + return { allowed: false, reason: "dm-disabled" }; + } + if (params.isGroupDm && !params.groupDmEnabled) { + return { allowed: false, reason: "group-dm-disabled" }; + } + if (params.isDirectMessage) { + const storeAllowFrom = + params.dmPolicy === "allowlist" + ? [] + : await readChannelAllowFromStore("discord").catch(() => []); + const access = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: params.dmPolicy, + groupPolicy: params.groupPolicy, + allowFrom: params.allowFrom, + groupAllowFrom: [], + storeAllowFrom, + isSenderAllowed: (allowEntries) => { + const allowList = normalizeDiscordAllowList(allowEntries, ["discord:", "user:", "pk:"]); + const allowMatch = allowList + ? resolveDiscordAllowListMatch({ + allowList, + candidate: { + id: params.user.id, + name: params.user.username, + tag: formatDiscordUserTag(params.user), + }, + allowNameMatching: params.allowNameMatching, + }) + : { allowed: false }; + return allowMatch.allowed; + }, + }); + if (access.decision !== "allow") { + return { allowed: false, reason: access.reason }; + } + } + if ( + params.isGroupDm && + !resolveGroupDmAllow({ + channels: params.groupDmChannels, + channelId: params.channelId, + channelName: params.channelName, + channelSlug: params.channelSlug, + }) + ) { + return { allowed: false, reason: "group-dm-not-allowlisted" }; + } + if (!params.isGuildMessage) { + return { allowed: true }; + } + const channelAllowlistConfigured = + Boolean(params.guildInfo?.channels) && Object.keys(params.guildInfo?.channels ?? {}).length > 0; + const channelAllowed = params.channelConfig?.allowed !== false; + if ( + !isDiscordGroupAllowedByPolicy({ + groupPolicy: params.groupPolicy, + guildAllowlisted: Boolean(params.guildInfo), + channelAllowlistConfigured, + channelAllowed, + }) + ) { + return { allowed: false, reason: "guild-policy" }; + } + if (params.channelConfig?.allowed === false) { + return { allowed: false, reason: "guild-channel-denied" }; + } + return { allowed: true }; +} + async function handleDiscordReactionEvent(params: { data: DiscordReactionEvent; client: Client; @@ -260,10 +353,25 @@ async function handleDiscordReactionEvent(params: { channelType === ChannelType.PublicThread || channelType === ChannelType.PrivateThread || channelType === ChannelType.AnnouncementThread; - if (isDirectMessage && !params.dmEnabled) { - return; - } - if (isGroupDm && !params.groupDmEnabled) { + const ingressAccess = await authorizeDiscordReactionIngress({ + user, + isDirectMessage, + isGroupDm, + isGuildMessage, + channelId: data.channel_id, + channelName, + channelSlug, + dmEnabled: params.dmEnabled, + groupDmEnabled: params.groupDmEnabled, + groupDmChannels: params.groupDmChannels, + dmPolicy: params.dmPolicy, + allowFrom: params.allowFrom, + groupPolicy: params.groupPolicy, + allowNameMatching: params.allowNameMatching, + guildInfo, + }); + if (!ingressAccess.allowed) { + logVerbose(`discord reaction blocked sender=${user.id} (reason=${ingressAccess.reason})`); return; } let parentId = "parentId" in channel ? (channel.parentId ?? undefined) : undefined; @@ -294,45 +402,6 @@ async function handleDiscordReactionEvent(params: { reactionBase = { baseText, contextKey }; return reactionBase; }; - const isDirectReactionAuthorized = async () => { - if (!isDirectMessage) { - return true; - } - const storeAllowFrom = - params.dmPolicy === "allowlist" - ? [] - : await readChannelAllowFromStore("discord").catch(() => []); - const access = resolveDmGroupAccessWithLists({ - isGroup: false, - dmPolicy: params.dmPolicy, - groupPolicy: params.groupPolicy, - allowFrom: params.allowFrom, - groupAllowFrom: [], - storeAllowFrom, - isSenderAllowed: (allowEntries) => { - const allowList = normalizeDiscordAllowList(allowEntries, ["discord:", "user:", "pk:"]); - const allowMatch = allowList - ? resolveDiscordAllowListMatch({ - allowList, - candidate: { - id: user.id, - name: user.username, - tag: formatDiscordUserTag(user), - }, - allowNameMatching: params.allowNameMatching, - }) - : { allowed: false }; - return allowMatch.allowed; - }, - }); - if (access.decision !== "allow") { - logVerbose( - `discord reaction blocked sender=${user.id} (dmPolicy=${params.dmPolicy}, decision=${access.decision}, reason=${access.reason})`, - ); - return false; - } - return true; - }; const emitReaction = (text: string, parentPeerId?: string) => { const { contextKey } = resolveReactionBase(); const route = resolveAgentRoute({ @@ -391,44 +460,6 @@ async function handleDiscordReactionEvent(params: { parentSlug, scope: "thread", }); - const isGuildReactionAllowed = (channelConfig: { allowed?: boolean } | null) => { - if (!isGuildMessage) { - return true; - } - const channelAllowlistConfigured = - Boolean(guildInfo?.channels) && Object.keys(guildInfo?.channels ?? {}).length > 0; - const channelAllowed = channelConfig?.allowed !== false; - if ( - !isDiscordGroupAllowedByPolicy({ - groupPolicy: params.groupPolicy, - guildAllowlisted: Boolean(guildInfo), - channelAllowlistConfigured, - channelAllowed, - }) - ) { - return false; - } - if (channelConfig?.allowed === false) { - return false; - } - return true; - }; - - if (!(await isDirectReactionAuthorized())) { - return; - } - - if ( - isGroupDm && - !resolveGroupDmAllow({ - channels: params.groupDmChannels, - channelId: data.channel_id, - channelName, - channelSlug, - }) - ) { - return; - } // Parallelize async operations for thread channels if (isThreadChannel) { @@ -450,7 +481,25 @@ async function handleDiscordReactionEvent(params: { await loadThreadParentInfo(); const channelConfig = resolveThreadChannelConfig(); - if (channelConfig?.allowed === false) { + const threadAccess = await authorizeDiscordReactionIngress({ + user, + isDirectMessage, + isGroupDm, + isGuildMessage, + channelId: data.channel_id, + channelName, + channelSlug, + dmEnabled: params.dmEnabled, + groupDmEnabled: params.groupDmEnabled, + groupDmChannels: params.groupDmChannels, + dmPolicy: params.dmPolicy, + allowFrom: params.allowFrom, + groupPolicy: params.groupPolicy, + allowNameMatching: params.allowNameMatching, + guildInfo, + channelConfig, + }); + if (!threadAccess.allowed) { return; } @@ -474,10 +523,25 @@ async function handleDiscordReactionEvent(params: { await loadThreadParentInfo(); const channelConfig = resolveThreadChannelConfig(); - if (channelConfig?.allowed === false) { - return; - } - if (!isGuildReactionAllowed(channelConfig)) { + const threadAccess = await authorizeDiscordReactionIngress({ + user, + isDirectMessage, + isGroupDm, + isGuildMessage, + channelId: data.channel_id, + channelName, + channelSlug, + dmEnabled: params.dmEnabled, + groupDmEnabled: params.groupDmEnabled, + groupDmChannels: params.groupDmChannels, + dmPolicy: params.dmPolicy, + allowFrom: params.allowFrom, + groupPolicy: params.groupPolicy, + allowNameMatching: params.allowNameMatching, + guildInfo, + channelConfig, + }); + if (!threadAccess.allowed) { return; } @@ -501,11 +565,28 @@ async function handleDiscordReactionEvent(params: { parentSlug, scope: "channel", }); - if (channelConfig?.allowed === false) { - return; - } - if (!isGuildReactionAllowed(channelConfig)) { - return; + if (isGuildMessage) { + const channelAccess = await authorizeDiscordReactionIngress({ + user, + isDirectMessage, + isGroupDm, + isGuildMessage, + channelId: data.channel_id, + channelName, + channelSlug, + dmEnabled: params.dmEnabled, + groupDmEnabled: params.groupDmEnabled, + groupDmChannels: params.groupDmChannels, + dmPolicy: params.dmPolicy, + allowFrom: params.allowFrom, + groupPolicy: params.groupPolicy, + allowNameMatching: params.allowNameMatching, + guildInfo, + channelConfig, + }); + if (!channelAccess.allowed) { + return; + } } const reactionMode = guildInfo?.reactionNotifications ?? "own"; diff --git a/src/plugin-sdk/index.ts b/src/plugin-sdk/index.ts index 7faa2341dc03..6e25d50740b2 100644 --- a/src/plugin-sdk/index.ts +++ b/src/plugin-sdk/index.ts @@ -375,6 +375,7 @@ export { formatDocsLink } from "../terminal/links.js"; export { resolveDmAllowState, resolveDmGroupAccessDecision, + resolveDmGroupAccessWithLists, resolveEffectiveAllowFromLists, } from "../security/dm-policy-shared.js"; export type { HookEntry } from "../hooks/types.js"; diff --git a/src/security/dm-policy-shared.test.ts b/src/security/dm-policy-shared.test.ts index 1fe36976a551..735b7d8728d7 100644 --- a/src/security/dm-policy-shared.test.ts +++ b/src/security/dm-policy-shared.test.ts @@ -118,6 +118,70 @@ describe("security/dm-policy-shared", () => { "zalo", ] as const; + it("keeps message/reaction policy parity table across channels", () => { + const cases = [ + { + name: "dmPolicy=open", + dmPolicy: "open" as const, + allowFrom: [] as string[], + senderAllowed: false, + expectedDecision: "allow" as const, + expectedReactionAllowed: true, + }, + { + name: "dmPolicy=disabled", + dmPolicy: "disabled" as const, + allowFrom: [] as string[], + senderAllowed: false, + expectedDecision: "block" as const, + expectedReactionAllowed: false, + }, + { + name: "dmPolicy=allowlist unauthorized", + dmPolicy: "allowlist" as const, + allowFrom: ["owner"], + senderAllowed: false, + expectedDecision: "block" as const, + expectedReactionAllowed: false, + }, + { + name: "dmPolicy=allowlist authorized", + dmPolicy: "allowlist" as const, + allowFrom: ["owner"], + senderAllowed: true, + expectedDecision: "allow" as const, + expectedReactionAllowed: true, + }, + { + name: "dmPolicy=pairing unauthorized", + dmPolicy: "pairing" as const, + allowFrom: [] as string[], + senderAllowed: false, + expectedDecision: "pairing" as const, + expectedReactionAllowed: false, + }, + ]; + + for (const channel of channels) { + for (const testCase of cases) { + const access = resolveDmGroupAccessWithLists({ + isGroup: false, + dmPolicy: testCase.dmPolicy, + groupPolicy: "allowlist", + allowFrom: testCase.allowFrom, + groupAllowFrom: [], + storeAllowFrom: [], + isSenderAllowed: () => testCase.senderAllowed, + }); + const reactionAllowed = access.decision === "allow"; + expect(access.decision, `[${channel}] ${testCase.name}`).toBe(testCase.expectedDecision); + expect(reactionAllowed, `[${channel}] ${testCase.name} reaction`).toBe( + testCase.expectedReactionAllowed, + ); + } + } + }); + for (const channel of channels) { it(`[${channel}] blocks DM allowlist mode when allowlist is empty`, () => { const decision = resolveDmGroupAccessDecision({ diff --git a/src/signal/monitor/event-handler.ts b/src/signal/monitor/event-handler.ts index e87158d8d8d7..ea9fa9f49d64 100644 --- a/src/signal/monitor/event-handler.ts +++ b/src/signal/monitor/event-handler.ts @@ -36,10 +36,7 @@ import { upsertChannelPairingRequest, } from "../../pairing/pairing-store.js"; import { resolveAgentRoute } from "../../routing/resolve-route.js"; -import { - resolveDmGroupAccessDecision, - resolveEffectiveAllowFromLists, -} from "../../security/dm-policy-shared.js"; +import { resolveDmGroupAccessWithLists } from "../../security/dm-policy-shared.js"; import { normalizeE164 } from "../../utils.js"; import { formatSignalPairingIdLine, @@ -460,23 +457,19 @@ export function createSignalEventHandler(deps: SignalEventHandlerDeps) { deps.dmPolicy === "allowlist" ? [] : await readChannelAllowFromStore("signal").catch(() => []); - const { effectiveAllowFrom: effectiveDmAllow, effectiveGroupAllowFrom: effectiveGroupAllow } = - resolveEffectiveAllowFromLists({ - allowFrom: deps.allowFrom, - groupAllowFrom: deps.groupAllowFrom, - storeAllowFrom, - dmPolicy: deps.dmPolicy, - }); const resolveAccessDecision = (isGroup: boolean) => - resolveDmGroupAccessDecision({ + resolveDmGroupAccessWithLists({ isGroup, dmPolicy: deps.dmPolicy, groupPolicy: deps.groupPolicy, - effectiveAllowFrom: effectiveDmAllow, - effectiveGroupAllowFrom: effectiveGroupAllow, - isSenderAllowed: (allowFrom) => isSignalSenderAllowed(sender, allowFrom), + allowFrom: deps.allowFrom, + groupAllowFrom: deps.groupAllowFrom, + storeAllowFrom, + isSenderAllowed: (allowEntries) => isSignalSenderAllowed(sender, allowEntries), }); const dmAccess = resolveAccessDecision(false); + const effectiveDmAllow = dmAccess.effectiveAllowFrom; + const effectiveGroupAllow = dmAccess.effectiveGroupAllowFrom; const dmAllowed = dmAccess.decision === "allow"; if ( From 20c2db21035e8706ccbd63d553e8042c19bbf8f1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:36:52 +0100 Subject: [PATCH 058/160] refactor(gateway): split browser auth hardening paths --- .../server.auth.browser-hardening.test.ts | 155 ++++++++++++++++++ src/gateway/server.auth.test.ts | 69 -------- src/gateway/server.impl.ts | 27 ++- .../server/ws-connection/message-handler.ts | 78 +++++++-- 4 files changed, 240 insertions(+), 89 deletions(-) create mode 100644 src/gateway/server.auth.browser-hardening.test.ts diff --git a/src/gateway/server.auth.browser-hardening.test.ts b/src/gateway/server.auth.browser-hardening.test.ts new file mode 100644 index 000000000000..070addbdc530 --- /dev/null +++ b/src/gateway/server.auth.browser-hardening.test.ts @@ -0,0 +1,155 @@ +import { randomUUID } from "node:crypto"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, test } from "vitest"; +import { WebSocket } from "ws"; +import { + loadOrCreateDeviceIdentity, + publicKeyRawBase64UrlFromPem, + signDevicePayload, +} from "../infra/device-identity.js"; +import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js"; +import { buildDeviceAuthPayload } from "./device-auth.js"; +import { + connectReq, + installGatewayTestHooks, + readConnectChallengeNonce, + testState, + trackConnectChallengeNonce, + withGatewayServer, +} from "./test-helpers.js"; + +installGatewayTestHooks({ scope: "suite" }); + +const TEST_OPERATOR_CLIENT = { + id: GATEWAY_CLIENT_NAMES.TEST, + version: "1.0.0", + platform: "test", + mode: GATEWAY_CLIENT_MODES.TEST, +}; + +const originForPort = (port: number) => `http://127.0.0.1:${port}`; + +const openWs = async (port: number, headers?: Record) => { + const ws = new WebSocket(`ws://127.0.0.1:${port}`, headers ? { headers } : undefined); + trackConnectChallengeNonce(ws); + await new Promise((resolve) => ws.once("open", resolve)); + return ws; +}; + +async function createSignedDevice(params: { + token: string; + scopes: string[]; + clientId: string; + clientMode: string; + identityPath?: string; + nonce: string; + signedAtMs?: number; +}) { + const identity = params.identityPath + ? loadOrCreateDeviceIdentity(params.identityPath) + : loadOrCreateDeviceIdentity(); + const signedAtMs = params.signedAtMs ?? Date.now(); + const payload = buildDeviceAuthPayload({ + deviceId: identity.deviceId, + clientId: params.clientId, + clientMode: params.clientMode, + role: "operator", + scopes: params.scopes, + signedAtMs, + token: params.token, + nonce: params.nonce, + }); + return { + identity, + device: { + id: identity.deviceId, + publicKey: publicKeyRawBase64UrlFromPem(identity.publicKeyPem), + signature: signDevicePayload(identity.privateKeyPem, payload), + signedAt: signedAtMs, + nonce: params.nonce, + }, + }; +} + +describe("gateway auth browser hardening", () => { + test("rejects non-local browser origins for non-control-ui clients", async () => { + testState.gatewayAuth = { mode: "token", token: "secret" }; + await withGatewayServer(async ({ port }) => { + const ws = await openWs(port, { origin: "https://attacker.example" }); + try { + const res = await connectReq(ws, { + token: "secret", + client: TEST_OPERATOR_CLIENT, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("origin not allowed"); + } finally { + ws.close(); + } + }); + }); + + test("rate-limits browser-origin auth failures on loopback even when loopback exemption is enabled", async () => { + testState.gatewayAuth = { + mode: "token", + token: "secret", + rateLimit: { maxAttempts: 1, windowMs: 60_000, lockoutMs: 60_000, exemptLoopback: true }, + }; + await withGatewayServer(async ({ port }) => { + const firstWs = await openWs(port, { origin: originForPort(port) }); + try { + const first = await connectReq(firstWs, { token: "wrong" }); + expect(first.ok).toBe(false); + expect(first.error?.message ?? "").not.toContain("retry later"); + } finally { + firstWs.close(); + } + + const secondWs = await openWs(port, { origin: originForPort(port) }); + try { + const second = await connectReq(secondWs, { token: "wrong" }); + expect(second.ok).toBe(false); + expect(second.error?.message ?? "").toContain("retry later"); + } finally { + secondWs.close(); + } + }); + }); + + test("does not silently auto-pair non-control-ui browser clients on loopback", async () => { + const { listDevicePairing } = await import("../infra/device-pairing.js"); + testState.gatewayAuth = { mode: "token", token: "secret" }; + + await withGatewayServer(async ({ port }) => { + const browserWs = await openWs(port, { origin: originForPort(port) }); + try { + const nonce = await readConnectChallengeNonce(browserWs); + expect(typeof nonce).toBe("string"); + const { identity, device } = await createSignedDevice({ + token: "secret", + scopes: ["operator.admin"], + clientId: TEST_OPERATOR_CLIENT.id, + clientMode: TEST_OPERATOR_CLIENT.mode, + identityPath: path.join(os.tmpdir(), `openclaw-browser-device-${randomUUID()}.json`), + nonce: String(nonce ?? ""), + }); + const res = await connectReq(browserWs, { + token: "secret", + scopes: ["operator.admin"], + client: TEST_OPERATOR_CLIENT, + device, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("pairing required"); + + const pairing = await listDevicePairing(); + const pending = pairing.pending.find((entry) => entry.deviceId === identity.deviceId); + expect(pending).toBeTruthy(); + expect(pending?.silent).toBe(false); + } finally { + browserWs.close(); + } + }); + }); +}); diff --git a/src/gateway/server.auth.test.ts b/src/gateway/server.auth.test.ts index 38668de7f40a..83a97644d193 100644 --- a/src/gateway/server.auth.test.ts +++ b/src/gateway/server.auth.test.ts @@ -672,17 +672,6 @@ describe("gateway server auth/connect", () => { ws.close(); }); - test("rejects non-local browser origins for non-control-ui clients", async () => { - const ws = await openWs(port, { origin: "https://attacker.example" }); - const res = await connectReq(ws, { - token: "secret", - client: TEST_OPERATOR_CLIENT, - }); - expect(res.ok).toBe(false); - expect(res.error?.message ?? "").toContain("origin not allowed"); - ws.close(); - }); - test("returns control ui hint when token is missing", async () => { const ws = await openWs(port, { origin: originForPort(port) }); const res = await connectReq(ws, { @@ -712,27 +701,6 @@ describe("gateway server auth/connect", () => { ); ws.close(); }); - - test("rate-limits browser-origin auth failures on loopback even when loopback exemption is enabled", async () => { - testState.gatewayAuth = { - mode: "token", - token: "secret", - rateLimit: { maxAttempts: 1, windowMs: 60_000, lockoutMs: 60_000, exemptLoopback: true }, - }; - await withGatewayServer(async ({ port }) => { - const firstWs = await openWs(port, { origin: originForPort(port) }); - const first = await connectReq(firstWs, { token: "wrong" }); - expect(first.ok).toBe(false); - expect(first.error?.message ?? "").not.toContain("retry later"); - firstWs.close(); - - const secondWs = await openWs(port, { origin: originForPort(port) }); - const second = await connectReq(secondWs, { token: "wrong" }); - expect(second.ok).toBe(false); - expect(second.error?.message ?? "").toContain("retry later"); - secondWs.close(); - }); - }); }); describe("explicit none auth", () => { @@ -1246,43 +1214,6 @@ describe("gateway server auth/connect", () => { restoreGatewayToken(prevToken); }); - test("does not silently auto-pair non-control-ui browser clients on loopback", async () => { - const { listDevicePairing } = await import("../infra/device-pairing.js"); - const { randomUUID } = await import("node:crypto"); - const os = await import("node:os"); - const path = await import("node:path"); - const { server, ws, port, prevToken } = await startServerWithClient("secret"); - ws.close(); - - const browserWs = await openWs(port, { origin: originForPort(port) }); - const nonce = await readConnectChallengeNonce(browserWs); - const { identity, device } = await createSignedDevice({ - token: "secret", - scopes: ["operator.admin"], - clientId: TEST_OPERATOR_CLIENT.id, - clientMode: TEST_OPERATOR_CLIENT.mode, - identityPath: path.join(os.tmpdir(), `openclaw-browser-device-${randomUUID()}.json`), - nonce, - }); - const res = await connectReq(browserWs, { - token: "secret", - scopes: ["operator.admin"], - client: TEST_OPERATOR_CLIENT, - device, - }); - expect(res.ok).toBe(false); - expect(res.error?.message ?? "").toContain("pairing required"); - - const pairing = await listDevicePairing(); - const pending = pairing.pending.find((entry) => entry.deviceId === identity.deviceId); - expect(pending).toBeTruthy(); - expect(pending?.silent).toBe(false); - - browserWs.close(); - await server.close(); - restoreGatewayToken(prevToken); - }); - test("merges remote node/operator pairing requests for the same unpaired device", async () => { const { mkdtemp } = await import("node:fs/promises"); const { tmpdir } = await import("node:os"); diff --git a/src/gateway/server.impl.ts b/src/gateway/server.impl.ts index 8b368539469a..3dbd86e1e5ee 100644 --- a/src/gateway/server.impl.ts +++ b/src/gateway/server.impl.ts @@ -110,6 +110,21 @@ const logWsControl = log.child("ws"); const gatewayRuntime = runtimeForLogger(log); const canvasRuntime = runtimeForLogger(logCanvas); +type AuthRateLimitConfig = Parameters[0]; + +function createGatewayAuthRateLimiters(rateLimitConfig: AuthRateLimitConfig | undefined): { + rateLimiter?: AuthRateLimiter; + browserRateLimiter: AuthRateLimiter; +} { + const rateLimiter = rateLimitConfig ? createAuthRateLimiter(rateLimitConfig) : undefined; + // Browser-origin WS auth attempts always use loopback-non-exempt throttling. + const browserRateLimiter = createAuthRateLimiter({ + ...rateLimitConfig, + exemptLoopback: false, + }); + return { rateLimiter, browserRateLimiter }; +} + export type GatewayServer = { close: (opts?: { reason?: string; restartExpectedMs?: number | null }) => Promise; }; @@ -311,16 +326,10 @@ export async function startGatewayServer( let hooksConfig = runtimeConfig.hooksConfig; const canvasHostEnabled = runtimeConfig.canvasHostEnabled; - // Create auth rate limiter only when explicitly configured. + // Create auth rate limiters used by connect/auth flows. const rateLimitConfig = cfgAtStart.gateway?.auth?.rateLimit; - const authRateLimiter: AuthRateLimiter | undefined = rateLimitConfig - ? createAuthRateLimiter(rateLimitConfig) - : undefined; - // Always keep a browser-origin fallback limiter for WS auth attempts. - const browserAuthRateLimiter: AuthRateLimiter = createAuthRateLimiter({ - ...rateLimitConfig, - exemptLoopback: false, - }); + const { rateLimiter: authRateLimiter, browserRateLimiter: browserAuthRateLimiter } = + createGatewayAuthRateLimiters(rateLimitConfig); let controlUiRootState: ControlUiRootState | undefined; if (controlUiRootOverride) { diff --git a/src/gateway/server/ws-connection/message-handler.ts b/src/gateway/server/ws-connection/message-handler.ts index 0d694d125294..8feb385e8f92 100644 --- a/src/gateway/server/ws-connection/message-handler.ts +++ b/src/gateway/server/ws-connection/message-handler.ts @@ -83,6 +83,52 @@ import { isUnauthorizedRoleError, UnauthorizedFloodGuard } from "./unauthorized- type SubsystemLogger = ReturnType; const DEVICE_SIGNATURE_SKEW_MS = 2 * 60 * 1000; +const BROWSER_ORIGIN_LOOPBACK_RATE_LIMIT_IP = "198.18.0.1"; + +type HandshakeBrowserSecurityContext = { + hasBrowserOriginHeader: boolean; + enforceOriginCheckForAnyClient: boolean; + rateLimitClientIp: string | undefined; + authRateLimiter?: AuthRateLimiter; +}; + +function resolveHandshakeBrowserSecurityContext(params: { + requestOrigin?: string; + hasProxyHeaders: boolean; + clientIp: string | undefined; + rateLimiter?: AuthRateLimiter; + browserRateLimiter?: AuthRateLimiter; +}): HandshakeBrowserSecurityContext { + const hasBrowserOriginHeader = Boolean( + params.requestOrigin && params.requestOrigin.trim() !== "", + ); + return { + hasBrowserOriginHeader, + enforceOriginCheckForAnyClient: hasBrowserOriginHeader && !params.hasProxyHeaders, + rateLimitClientIp: + hasBrowserOriginHeader && isLoopbackAddress(params.clientIp) + ? BROWSER_ORIGIN_LOOPBACK_RATE_LIMIT_IP + : params.clientIp, + authRateLimiter: + hasBrowserOriginHeader && params.browserRateLimiter + ? params.browserRateLimiter + : params.rateLimiter, + }; +} + +function shouldAllowSilentLocalPairing(params: { + isLocalClient: boolean; + hasBrowserOriginHeader: boolean; + isControlUi: boolean; + isWebchat: boolean; + reason: "not-paired" | "role-upgrade" | "scope-upgrade"; +}): boolean { + return ( + params.isLocalClient && + (!params.hasBrowserOriginHeader || params.isControlUi || params.isWebchat) && + (params.reason === "not-paired" || params.reason === "scope-upgrade") + ); +} export function attachGatewayWsMessageHandler(params: { socket: WebSocket; @@ -195,12 +241,19 @@ export function attachGatewayWsMessageHandler(params: { const isWebchatConnect = (p: ConnectParams | null | undefined) => isWebchatClient(p?.client); const unauthorizedFloodGuard = new UnauthorizedFloodGuard(); - const hasBrowserOriginHeader = Boolean(requestOrigin && requestOrigin.trim() !== ""); - const enforceBrowserOriginForAnyClient = hasBrowserOriginHeader && !hasProxyHeaders; - const browserRateLimitClientIp = - hasBrowserOriginHeader && isLoopbackAddress(clientIp) ? "198.18.0.1" : clientIp; - const authRateLimiter = - hasBrowserOriginHeader && browserRateLimiter ? browserRateLimiter : rateLimiter; + const browserSecurity = resolveHandshakeBrowserSecurityContext({ + requestOrigin, + hasProxyHeaders, + clientIp, + rateLimiter, + browserRateLimiter, + }); + const { + hasBrowserOriginHeader, + enforceOriginCheckForAnyClient, + rateLimitClientIp: browserRateLimitClientIp, + authRateLimiter, + } = browserSecurity; socket.on("message", async (data) => { if (isClosed()) { @@ -338,7 +391,7 @@ export function attachGatewayWsMessageHandler(params: { const isControlUi = connectParams.client.id === GATEWAY_CLIENT_IDS.CONTROL_UI; const isWebchat = isWebchatConnect(connectParams); - if (enforceBrowserOriginForAnyClient || isControlUi || isWebchat) { + if (enforceOriginCheckForAnyClient || isControlUi || isWebchat) { const originCheck = checkBrowserOrigin({ requestHost, origin: requestOrigin, @@ -622,10 +675,13 @@ export function attachGatewayWsMessageHandler(params: { const requirePairing = async ( reason: "not-paired" | "role-upgrade" | "scope-upgrade", ) => { - const allowSilentLocalPairing = - isLocalClient && - (!hasBrowserOriginHeader || isControlUi || isWebchat) && - (reason === "not-paired" || reason === "scope-upgrade"); + const allowSilentLocalPairing = shouldAllowSilentLocalPairing({ + isLocalClient, + hasBrowserOriginHeader, + isControlUi, + isWebchat, + reason, + }); const pairing = await requestDevicePairing({ deviceId: device.id, publicKey: devicePublicKey, From aaeed3c4ea2a166dc31bf7a67c40425ac27a9bfe Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:38:24 +0000 Subject: [PATCH 059/160] test(agents): add missing announce delivery regressions --- src/agents/subagent-announce.format.test.ts | 41 ++++++++++ src/gateway/server-methods/send.test.ts | 74 ++++++++++++++++++- src/infra/outbound/channel-resolution.ts | 13 +++- .../targets.channel-resolution.test.ts | 44 ++++++++++- src/telegram/send.test.ts | 31 ++++++++ 5 files changed, 197 insertions(+), 6 deletions(-) diff --git a/src/agents/subagent-announce.format.test.ts b/src/agents/subagent-announce.format.test.ts index 91f4b0d6752d..8952e82cc687 100644 --- a/src/agents/subagent-announce.format.test.ts +++ b/src/agents/subagent-announce.format.test.ts @@ -825,6 +825,47 @@ describe("subagent announce formatting", () => { } }); + it("routes manual completion direct-send for telegram forum topics", async () => { + sendSpy.mockClear(); + agentSpy.mockClear(); + sessionStore = { + "agent:main:subagent:test": { + sessionId: "child-session-telegram-topic", + }, + "agent:main:main": { + sessionId: "requester-session-telegram-topic", + lastChannel: "telegram", + lastTo: "123:topic:999", + lastThreadId: 999, + }, + }; + chatHistoryMock.mockResolvedValueOnce({ + messages: [{ role: "assistant", content: [{ type: "text", text: "done" }] }], + }); + + const didAnnounce = await runSubagentAnnounceFlow({ + childSessionKey: "agent:main:subagent:test", + childRunId: "run-direct-telegram-topic", + requesterSessionKey: "agent:main:main", + requesterDisplayKey: "main", + requesterOrigin: { + channel: "telegram", + to: "123", + threadId: 42, + }, + ...defaultOutcomeAnnounce, + expectsCompletionMessage: true, + }); + + expect(didAnnounce).toBe(true); + expect(sendSpy).toHaveBeenCalledTimes(1); + expect(agentSpy).not.toHaveBeenCalled(); + const call = sendSpy.mock.calls[0]?.[0] as { params?: Record }; + expect(call?.params?.channel).toBe("telegram"); + expect(call?.params?.to).toBe("123"); + expect(call?.params?.threadId).toBe("42"); + }); + it("uses hook-provided thread target across requester thread variants", async () => { const cases = [ { diff --git a/src/gateway/server-methods/send.test.ts b/src/gateway/server-methods/send.test.ts index 7734de8e911b..0e3bfba668cf 100644 --- a/src/gateway/server-methods/send.test.ts +++ b/src/gateway/server-methods/send.test.ts @@ -1,5 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import { resolveOutboundTarget } from "../../infra/outbound/targets.js"; +import { setActivePluginRegistry } from "../../plugins/runtime.js"; +import { createTestRegistry } from "../../test-utils/channel-plugins.js"; import { sendHandlers } from "./send.js"; import type { GatewayRequestContext } from "./types.js"; @@ -10,6 +12,8 @@ const mocks = vi.hoisted(() => ({ resolveOutboundTarget: vi.fn(() => ({ ok: true, to: "resolved" })), resolveMessageChannelSelection: vi.fn(), sendPoll: vi.fn(async () => ({ messageId: "poll-1" })), + getChannelPlugin: vi.fn(), + loadOpenClawPlugins: vi.fn(), })); vi.mock("../../config/config.js", async () => { @@ -22,10 +26,38 @@ vi.mock("../../config/config.js", async () => { }); vi.mock("../../channels/plugins/index.js", () => ({ - getChannelPlugin: () => ({ outbound: { sendPoll: mocks.sendPoll } }), + getChannelPlugin: mocks.getChannelPlugin, normalizeChannelId: (value: string) => (value === "webchat" ? null : value), })); +vi.mock("../../agents/agent-scope.js", () => ({ + resolveSessionAgentId: ({ + sessionKey, + }: { + sessionKey?: string; + config?: unknown; + agentId?: string; + }) => { + if (typeof sessionKey === "string") { + const match = sessionKey.match(/^agent:([^:]+)/i); + if (match?.[1]) { + return match[1]; + } + } + return "main"; + }, + resolveDefaultAgentId: () => "main", + resolveAgentWorkspaceDir: () => "/tmp/openclaw-test-workspace", +})); + +vi.mock("../../config/plugin-auto-enable.js", () => ({ + applyPluginAutoEnable: ({ config }: { config: unknown }) => ({ config, changes: [] }), +})); + +vi.mock("../../plugins/loader.js", () => ({ + loadOpenClawPlugins: mocks.loadOpenClawPlugins, +})); + vi.mock("../../infra/outbound/targets.js", () => ({ resolveOutboundTarget: mocks.resolveOutboundTarget, })); @@ -85,14 +117,19 @@ function mockDeliverySuccess(messageId: string) { } describe("gateway send mirroring", () => { + let registrySeq = 0; + beforeEach(() => { vi.clearAllMocks(); + registrySeq += 1; + setActivePluginRegistry(createTestRegistry([]), `send-test-${registrySeq}`); mocks.resolveOutboundTarget.mockReturnValue({ ok: true, to: "resolved" }); mocks.resolveMessageChannelSelection.mockResolvedValue({ channel: "slack", configured: ["slack"], }); mocks.sendPoll.mockResolvedValue({ messageId: "poll-1" }); + mocks.getChannelPlugin.mockReturnValue({ outbound: { sendPoll: mocks.sendPoll } }); }); it("accepts media-only sends without message", async () => { @@ -475,4 +512,39 @@ describe("gateway send mirroring", () => { }), ); }); + + it("recovers cold plugin resolution for telegram threaded sends", async () => { + mocks.resolveOutboundTarget.mockReturnValue({ ok: true, to: "123" }); + mocks.deliverOutboundPayloads.mockResolvedValue([ + { messageId: "m-telegram", channel: "telegram" }, + ]); + const telegramPlugin = { outbound: { sendPoll: mocks.sendPoll } }; + mocks.getChannelPlugin + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(telegramPlugin) + .mockReturnValue(telegramPlugin); + + const { respond } = await runSend({ + to: "123", + message: "forum completion", + channel: "telegram", + threadId: "42", + idempotencyKey: "idem-cold-telegram-thread", + }); + + expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1); + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( + expect.objectContaining({ + channel: "telegram", + to: "123", + threadId: "42", + }), + ); + expect(respond).toHaveBeenCalledWith( + true, + expect.objectContaining({ messageId: "m-telegram" }), + undefined, + expect.objectContaining({ channel: "telegram" }), + ); + }); }); diff --git a/src/infra/outbound/channel-resolution.ts b/src/infra/outbound/channel-resolution.ts index 58596da93f3c..8d17294d024d 100644 --- a/src/infra/outbound/channel-resolution.ts +++ b/src/infra/outbound/channel-resolution.ts @@ -47,10 +47,15 @@ function maybeBootstrapChannelPlugin(params: { const autoEnabled = applyPluginAutoEnable({ config: cfg }).config; const defaultAgentId = resolveDefaultAgentId(autoEnabled); const workspaceDir = resolveAgentWorkspaceDir(autoEnabled, defaultAgentId); - loadOpenClawPlugins({ - config: autoEnabled, - workspaceDir, - }); + try { + loadOpenClawPlugins({ + config: autoEnabled, + workspaceDir, + }); + } catch { + // Allow a follow-up resolution attempt if bootstrap failed transiently. + bootstrapAttempts.delete(attemptKey); + } } export function resolveOutboundChannelPlugin(params: { diff --git a/src/infra/outbound/targets.channel-resolution.test.ts b/src/infra/outbound/targets.channel-resolution.test.ts index 615134f654b2..01779d0655c7 100644 --- a/src/infra/outbound/targets.channel-resolution.test.ts +++ b/src/infra/outbound/targets.channel-resolution.test.ts @@ -28,8 +28,11 @@ import { createTestRegistry } from "../../test-utils/channel-plugins.js"; import { resolveOutboundTarget } from "./targets.js"; describe("resolveOutboundTarget channel resolution", () => { + let registrySeq = 0; + beforeEach(() => { - setActivePluginRegistry(createTestRegistry([])); + registrySeq += 1; + setActivePluginRegistry(createTestRegistry([]), `targets-test-${registrySeq}`); mocks.getChannelPlugin.mockReset(); mocks.loadOpenClawPlugins.mockReset(); }); @@ -58,4 +61,43 @@ describe("resolveOutboundTarget channel resolution", () => { expect(result).toEqual({ ok: true, to: "123456" }); expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1); }); + + it("retries bootstrap on subsequent resolve when the first bootstrap attempt fails", () => { + const telegramPlugin = { + id: "telegram", + meta: { label: "Telegram" }, + config: { + listAccountIds: () => [], + resolveAccount: () => ({}), + }, + }; + mocks.getChannelPlugin + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(undefined) + .mockReturnValueOnce(telegramPlugin) + .mockReturnValue(telegramPlugin); + mocks.loadOpenClawPlugins + .mockImplementationOnce(() => { + throw new Error("bootstrap failed"); + }) + .mockImplementation(() => undefined); + + const first = resolveOutboundTarget({ + channel: "telegram", + to: "123456", + cfg: { channels: { telegram: { botToken: "test-token" } } }, + mode: "explicit", + }); + const second = resolveOutboundTarget({ + channel: "telegram", + to: "123456", + cfg: { channels: { telegram: { botToken: "test-token" } } }, + mode: "explicit", + }); + + expect(first.ok).toBe(false); + expect(second).toEqual({ ok: true, to: "123456" }); + expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(2); + }); }); diff --git a/src/telegram/send.test.ts b/src/telegram/send.test.ts index afd616b5f153..b589fdcf52ba 100644 --- a/src/telegram/send.test.ts +++ b/src/telegram/send.test.ts @@ -1280,6 +1280,23 @@ describe("sendStickerTelegram", () => { expect(sendSticker).toHaveBeenNthCalledWith(2, chatId, "fileId123", undefined); expect(res.messageId).toBe("109"); }); + + it("fails when sticker send returns no message_id", async () => { + const chatId = "123"; + const sendSticker = vi.fn().mockResolvedValue({ + chat: { id: chatId }, + }); + const api = { sendSticker } as unknown as { + sendSticker: typeof sendSticker; + }; + + await expect( + sendStickerTelegram(chatId, "fileId123", { + token: "tok", + api, + }), + ).rejects.toThrow(/returned no message_id/i); + }); }); describe("shared send behaviors", () => { @@ -1542,6 +1559,20 @@ describe("sendPollTelegram", () => { expect(api.sendPoll).not.toHaveBeenCalled(); }); + + it("fails when poll send returns no message_id", async () => { + const api = { + sendPoll: vi.fn(async () => ({ chat: { id: 555 }, poll: { id: "p1" } })), + }; + + await expect( + sendPollTelegram( + "123", + { question: "Q", options: ["A", "B"] }, + { token: "t", api: api as unknown as Bot["api"] }, + ), + ).rejects.toThrow(/returned no message_id/i); + }); }); describe("createForumTopicTelegram", () => { From f312222159a6ef4921c4d3bd5c8e4b2bc21d2a23 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 00:42:51 +0000 Subject: [PATCH 060/160] test: preserve config exports in agent handler mock --- src/gateway/server-methods/agent.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/gateway/server-methods/agent.test.ts b/src/gateway/server-methods/agent.test.ts index 5d65d2627359..dbf21b0efbf3 100644 --- a/src/gateway/server-methods/agent.test.ts +++ b/src/gateway/server-methods/agent.test.ts @@ -43,9 +43,14 @@ vi.mock("../../commands/agent.js", () => ({ agentCommand: mocks.agentCommand, })); -vi.mock("../../config/config.js", () => ({ - loadConfig: () => mocks.loadConfigReturn, -})); +vi.mock("../../config/config.js", async () => { + const actual = + await vi.importActual("../../config/config.js"); + return { + ...actual, + loadConfig: () => mocks.loadConfigReturn, + }; +}); vi.mock("../../agents/agent-scope.js", () => ({ listAgentIds: () => ["main"], From c0026274d9cafcfaf5c84bf4b6a5386755099b30 Mon Sep 17 00:00:00 2001 From: Aleksandrs Tihenko <87486610+rrenamed@users.noreply.github.com> Date: Thu, 26 Feb 2026 02:47:16 +0200 Subject: [PATCH 061/160] fix(auth): distinguish revoked API keys from transient auth errors (#25754) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 8f9c07a200644284e11adae76368adab40c5fa4e Co-authored-by: rrenamed <87486610+rrenamed@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + ...th-profiles.markauthprofilefailure.test.ts | 16 +++++++ src/agents/auth-profiles/types.ts | 1 + src/agents/auth-profiles/usage.test.ts | 45 ++++++++++++++++++- src/agents/auth-profiles/usage.ts | 12 ++--- src/agents/failover-error.test.ts | 31 +++++++++++++ src/agents/failover-error.ts | 12 ++++- ...dded-helpers.isbillingerrormessage.test.ts | 40 +++++++++++++++++ src/agents/pi-embedded-helpers.ts | 1 + src/agents/pi-embedded-helpers/errors.ts | 15 +++++++ src/agents/pi-embedded-helpers/types.ts | 1 + src/commands/doctor-auth.hints.test.ts | 28 ++++++++++++ src/commands/doctor-auth.ts | 30 ++++++++++--- src/commands/models/list.probe.test.ts | 22 +++++++++ src/commands/models/list.probe.ts | 10 +++-- 15 files changed, 247 insertions(+), 18 deletions(-) create mode 100644 src/commands/doctor-auth.hints.test.ts create mode 100644 src/commands/models/list.probe.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d71991f6a018..f4ff72155d18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Docs: https://docs.openclaw.ai - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. +- Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. diff --git a/src/agents/auth-profiles.markauthprofilefailure.test.ts b/src/agents/auth-profiles.markauthprofilefailure.test.ts index 1a30d8a91199..865fbf878164 100644 --- a/src/agents/auth-profiles.markauthprofilefailure.test.ts +++ b/src/agents/auth-profiles.markauthprofilefailure.test.ts @@ -114,6 +114,22 @@ describe("markAuthProfileFailure", () => { expect(reloaded.usageStats?.["anthropic:default"]?.cooldownUntil).toBe(firstCooldownUntil); }); }); + it("disables auth_permanent failures via disabledUntil (like billing)", async () => { + await withAuthProfileStore(async ({ agentDir, store }) => { + await markAuthProfileFailure({ + store, + profileId: "anthropic:default", + reason: "auth_permanent", + agentDir, + }); + + const stats = store.usageStats?.["anthropic:default"]; + expect(typeof stats?.disabledUntil).toBe("number"); + expect(stats?.disabledReason).toBe("auth_permanent"); + // Should NOT set cooldownUntil (that's for transient errors) + expect(stats?.cooldownUntil).toBeUndefined(); + }); + }); it("resets backoff counters outside the failure window", async () => { const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-")); try { diff --git a/src/agents/auth-profiles/types.ts b/src/agents/auth-profiles/types.ts index 7332d3048125..c23e6aa404d0 100644 --- a/src/agents/auth-profiles/types.ts +++ b/src/agents/auth-profiles/types.ts @@ -34,6 +34,7 @@ export type AuthProfileCredential = ApiKeyCredential | TokenCredential | OAuthCr export type AuthProfileFailureReason = | "auth" + | "auth_permanent" | "format" | "rate_limit" | "billing" diff --git a/src/agents/auth-profiles/usage.test.ts b/src/agents/auth-profiles/usage.test.ts index 0025007f7290..8c499654b494 100644 --- a/src/agents/auth-profiles/usage.test.ts +++ b/src/agents/auth-profiles/usage.test.ts @@ -141,6 +141,24 @@ describe("resolveProfilesUnavailableReason", () => { ).toBe("billing"); }); + it("returns auth_permanent for active permanent auth disables", () => { + const now = Date.now(); + const store = makeStore({ + "anthropic:default": { + disabledUntil: now + 60_000, + disabledReason: "auth_permanent", + }, + }); + + expect( + resolveProfilesUnavailableReason({ + store, + profileIds: ["anthropic:default"], + now, + }), + ).toBe("auth_permanent"); + }); + it("uses recorded non-rate-limit failure counts for active cooldown windows", () => { const now = Date.now(); const store = makeStore({ @@ -490,7 +508,7 @@ describe("markAuthProfileFailure — active windows do not extend on retry", () async function markFailureAt(params: { store: ReturnType; now: number; - reason: "rate_limit" | "billing"; + reason: "rate_limit" | "billing" | "auth_permanent"; }): Promise { vi.useFakeTimers(); vi.setSystemTime(params.now); @@ -528,6 +546,18 @@ describe("markAuthProfileFailure — active windows do not extend on retry", () }), readUntil: (stats: WindowStats | undefined) => stats?.disabledUntil, }, + { + label: "disabledUntil(auth_permanent)", + reason: "auth_permanent" as const, + buildUsageStats: (now: number): WindowStats => ({ + disabledUntil: now + 20 * 60 * 60 * 1000, + disabledReason: "auth_permanent", + errorCount: 5, + failureCounts: { auth_permanent: 5 }, + lastFailureAt: now - 60_000, + }), + readUntil: (stats: WindowStats | undefined) => stats?.disabledUntil, + }, ]; for (const testCase of activeWindowCases) { @@ -573,6 +603,19 @@ describe("markAuthProfileFailure — active windows do not extend on retry", () expectedUntil: (now: number) => now + 20 * 60 * 60 * 1000, readUntil: (stats: WindowStats | undefined) => stats?.disabledUntil, }, + { + label: "disabledUntil(auth_permanent)", + reason: "auth_permanent" as const, + buildUsageStats: (now: number): WindowStats => ({ + disabledUntil: now - 60_000, + disabledReason: "auth_permanent", + errorCount: 5, + failureCounts: { auth_permanent: 2 }, + lastFailureAt: now - 60_000, + }), + expectedUntil: (now: number) => now + 20 * 60 * 60 * 1000, + readUntil: (stats: WindowStats | undefined) => stats?.disabledUntil, + }, ]; for (const testCase of expiredWindowCases) { diff --git a/src/agents/auth-profiles/usage.ts b/src/agents/auth-profiles/usage.ts index 958e3ae127e4..60c43c9c3c8d 100644 --- a/src/agents/auth-profiles/usage.ts +++ b/src/agents/auth-profiles/usage.ts @@ -4,6 +4,7 @@ import { saveAuthProfileStore, updateAuthProfileStoreWithLock } from "./store.js import type { AuthProfileFailureReason, AuthProfileStore, ProfileUsageStats } from "./types.js"; const FAILURE_REASON_PRIORITY: AuthProfileFailureReason[] = [ + "auth_permanent", "auth", "billing", "format", @@ -394,8 +395,8 @@ function computeNextProfileUsageStats(params: { lastFailureAt: params.now, }; - if (params.reason === "billing") { - const billingCount = failureCounts.billing ?? 1; + if (params.reason === "billing" || params.reason === "auth_permanent") { + const billingCount = failureCounts[params.reason] ?? 1; const backoffMs = calculateAuthProfileBillingDisableMsWithConfig({ errorCount: billingCount, baseMs: params.cfgResolved.billingBackoffMs, @@ -408,7 +409,7 @@ function computeNextProfileUsageStats(params: { now: params.now, recomputedUntil: params.now + backoffMs, }); - updatedStats.disabledReason = "billing"; + updatedStats.disabledReason = params.reason; } else { const backoffMs = calculateAuthProfileCooldownMs(nextErrorCount); // Keep active cooldown windows immutable so retries within the window @@ -424,8 +425,9 @@ function computeNextProfileUsageStats(params: { } /** - * Mark a profile as failed for a specific reason. Billing failures are treated - * as "disabled" (longer backoff) vs the regular cooldown window. + * Mark a profile as failed for a specific reason. Billing and permanent-auth + * failures are treated as "disabled" (longer backoff) vs the regular cooldown + * window. */ export async function markAuthProfileFailure(params: { store: AuthProfileStore; diff --git a/src/agents/failover-error.test.ts b/src/agents/failover-error.test.ts index d7c1edccbe18..8b2cb8462980 100644 --- a/src/agents/failover-error.test.ts +++ b/src/agents/failover-error.test.ts @@ -4,6 +4,7 @@ import { describeFailoverError, isTimeoutError, resolveFailoverReasonFromError, + resolveFailoverStatus, } from "./failover-error.js"; describe("failover-error", () => { @@ -69,6 +70,36 @@ describe("failover-error", () => { expect(err?.status).toBe(400); }); + it("401/403 with generic message still returns auth (backward compat)", () => { + expect(resolveFailoverReasonFromError({ status: 401, message: "Unauthorized" })).toBe("auth"); + expect(resolveFailoverReasonFromError({ status: 403, message: "Forbidden" })).toBe("auth"); + }); + + it("401 with permanent auth message returns auth_permanent", () => { + expect(resolveFailoverReasonFromError({ status: 401, message: "invalid_api_key" })).toBe( + "auth_permanent", + ); + }); + + it("403 with revoked key message returns auth_permanent", () => { + expect(resolveFailoverReasonFromError({ status: 403, message: "api key revoked" })).toBe( + "auth_permanent", + ); + }); + + it("resolveFailoverStatus maps auth_permanent to 403", () => { + expect(resolveFailoverStatus("auth_permanent")).toBe(403); + }); + + it("coerces permanent auth error with correct reason", () => { + const err = coerceToFailoverError( + { status: 401, message: "invalid_api_key" }, + { provider: "anthropic", model: "claude-opus-4-6" }, + ); + expect(err?.reason).toBe("auth_permanent"); + expect(err?.provider).toBe("anthropic"); + }); + it("describes non-Error values consistently", () => { const described = describeFailoverError(123); expect(described.message).toBe("123"); diff --git a/src/agents/failover-error.ts b/src/agents/failover-error.ts index 4de2babde4dd..708af55e3226 100644 --- a/src/agents/failover-error.ts +++ b/src/agents/failover-error.ts @@ -1,4 +1,8 @@ -import { classifyFailoverReason, type FailoverReason } from "./pi-embedded-helpers.js"; +import { + classifyFailoverReason, + isAuthPermanentErrorMessage, + type FailoverReason, +} from "./pi-embedded-helpers.js"; const TIMEOUT_HINT_RE = /timeout|timed out|deadline exceeded|context deadline exceeded|stop reason:\s*abort|reason:\s*abort|unhandled stop reason:\s*abort/i; @@ -47,6 +51,8 @@ export function resolveFailoverStatus(reason: FailoverReason): number | undefine return 429; case "auth": return 401; + case "auth_permanent": + return 403; case "timeout": return 408; case "format": @@ -158,6 +164,10 @@ export function resolveFailoverReasonFromError(err: unknown): FailoverReason | n return "rate_limit"; } if (status === 401 || status === 403) { + const msg = getErrorMessage(err); + if (msg && isAuthPermanentErrorMessage(msg)) { + return "auth_permanent"; + } return "auth"; } if (status === 408) { diff --git a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts index 638b6c24bb82..a109af6d89f7 100644 --- a/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts +++ b/src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest"; import { classifyFailoverReason, isAuthErrorMessage, + isAuthPermanentErrorMessage, isBillingErrorMessage, isCloudCodeAssistFormatError, isCloudflareOrHtmlErrorPage, @@ -16,6 +17,39 @@ import { parseImageSizeError, } from "./pi-embedded-helpers.js"; +describe("isAuthPermanentErrorMessage", () => { + it("matches permanent auth failure patterns", () => { + const samples = [ + "invalid_api_key", + "api key revoked", + "api key deactivated", + "key has been disabled", + "key has been revoked", + "account has been deactivated", + "could not authenticate api key", + "could not validate credentials", + "API_KEY_REVOKED", + "api_key_deleted", + ]; + for (const sample of samples) { + expect(isAuthPermanentErrorMessage(sample)).toBe(true); + } + }); + it("does not match transient auth errors", () => { + const samples = [ + "unauthorized", + "invalid token", + "authentication failed", + "forbidden", + "access denied", + "token has expired", + ]; + for (const sample of samples) { + expect(isAuthPermanentErrorMessage(sample)).toBe(false); + } + }); +}); + describe("isAuthErrorMessage", () => { it("matches credential validation errors", () => { const samples = [ @@ -480,6 +514,12 @@ describe("classifyFailoverReason", () => { ), ).toBe("rate_limit"); }); + it("classifies permanent auth errors as auth_permanent", () => { + expect(classifyFailoverReason("invalid_api_key")).toBe("auth_permanent"); + expect(classifyFailoverReason("Your api key has been revoked")).toBe("auth_permanent"); + expect(classifyFailoverReason("key has been disabled")).toBe("auth_permanent"); + expect(classifyFailoverReason("account has been deactivated")).toBe("auth_permanent"); + }); it("classifies JSON api_error internal server failures as timeout", () => { expect( classifyFailoverReason( diff --git a/src/agents/pi-embedded-helpers.ts b/src/agents/pi-embedded-helpers.ts index 06bf2b1938bc..dd10fdca3d19 100644 --- a/src/agents/pi-embedded-helpers.ts +++ b/src/agents/pi-embedded-helpers.ts @@ -16,6 +16,7 @@ export { getApiErrorPayloadFingerprint, isAuthAssistantError, isAuthErrorMessage, + isAuthPermanentErrorMessage, isModelNotFoundErrorMessage, isBillingAssistantError, parseApiErrorInfo, diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index 6eea521ede17..246f6c0ad246 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -649,6 +649,14 @@ const ERROR_PATTERNS = { "plans & billing", "insufficient balance", ], + authPermanent: [ + /api[_ ]?key[_ ]?(?:revoked|invalid|deactivated|deleted)/i, + "invalid_api_key", + "key has been disabled", + "key has been revoked", + "account has been deactivated", + /could not (?:authenticate|validate).*(?:api[_ ]?key|credentials)/i, + ], auth: [ /invalid[_ ]?api[_ ]?key/, "incorrect api key", @@ -755,6 +763,10 @@ export function isBillingAssistantError(msg: AssistantMessage | undefined): bool return isBillingErrorMessage(msg.errorMessage ?? ""); } +export function isAuthPermanentErrorMessage(raw: string): boolean { + return matchesErrorPatterns(raw, ERROR_PATTERNS.authPermanent); +} + export function isAuthErrorMessage(raw: string): boolean { return matchesErrorPatterns(raw, ERROR_PATTERNS.auth); } @@ -899,6 +911,9 @@ export function classifyFailoverReason(raw: string): FailoverReason | null { if (isTimeoutErrorMessage(raw)) { return "timeout"; } + if (isAuthPermanentErrorMessage(raw)) { + return "auth_permanent"; + } if (isAuthErrorMessage(raw)) { return "auth"; } diff --git a/src/agents/pi-embedded-helpers/types.ts b/src/agents/pi-embedded-helpers/types.ts index 2753e979eb28..2440473d9f6f 100644 --- a/src/agents/pi-embedded-helpers/types.ts +++ b/src/agents/pi-embedded-helpers/types.ts @@ -2,6 +2,7 @@ export type EmbeddedContextFile = { path: string; content: string }; export type FailoverReason = | "auth" + | "auth_permanent" | "format" | "rate_limit" | "billing" diff --git a/src/commands/doctor-auth.hints.test.ts b/src/commands/doctor-auth.hints.test.ts new file mode 100644 index 000000000000..f660a4e82a2d --- /dev/null +++ b/src/commands/doctor-auth.hints.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import { resolveUnusableProfileHint } from "./doctor-auth.js"; + +describe("resolveUnusableProfileHint", () => { + it("returns billing guidance for disabled billing profiles", () => { + expect(resolveUnusableProfileHint({ kind: "disabled", reason: "billing" })).toBe( + "Top up credits (provider billing) or switch provider.", + ); + }); + + it("returns credential guidance for permanent auth disables", () => { + expect(resolveUnusableProfileHint({ kind: "disabled", reason: "auth_permanent" })).toBe( + "Refresh or replace credentials, then retry.", + ); + }); + + it("falls back to cooldown guidance for non-billing disable reasons", () => { + expect(resolveUnusableProfileHint({ kind: "disabled", reason: "unknown" })).toBe( + "Wait for cooldown or switch provider.", + ); + }); + + it("returns cooldown guidance for cooldown windows", () => { + expect(resolveUnusableProfileHint({ kind: "cooldown" })).toBe( + "Wait for cooldown or switch provider.", + ); + }); +}); diff --git a/src/commands/doctor-auth.ts b/src/commands/doctor-auth.ts index a12ab384a200..f408dc43f933 100644 --- a/src/commands/doctor-auth.ts +++ b/src/commands/doctor-auth.ts @@ -206,6 +206,21 @@ type AuthIssue = { remainingMs?: number; }; +export function resolveUnusableProfileHint(params: { + kind: "cooldown" | "disabled"; + reason?: string; +}): string { + if (params.kind === "disabled") { + if (params.reason === "billing") { + return "Top up credits (provider billing) or switch provider."; + } + if (params.reason === "auth_permanent" || params.reason === "auth") { + return "Refresh or replace credentials, then retry."; + } + } + return "Wait for cooldown or switch provider."; +} + function formatAuthIssueHint(issue: AuthIssue): string | null { if (issue.provider === "anthropic" && issue.profileId === CLAUDE_CLI_PROFILE_ID) { return `Deprecated profile. Use ${formatCliCommand("openclaw models auth setup-token")} or ${formatCliCommand( @@ -245,13 +260,14 @@ export async function noteAuthProfileHealth(params: { } const stats = store.usageStats?.[profileId]; const remaining = formatRemainingShort(until - now); - const kind = - typeof stats?.disabledUntil === "number" && now < stats.disabledUntil - ? `disabled${stats.disabledReason ? `:${stats.disabledReason}` : ""}` - : "cooldown"; - const hint = kind.startsWith("disabled:billing") - ? "Top up credits (provider billing) or switch provider." - : "Wait for cooldown or switch provider."; + const disabledActive = typeof stats?.disabledUntil === "number" && now < stats.disabledUntil; + const kind = disabledActive + ? `disabled${stats.disabledReason ? `:${stats.disabledReason}` : ""}` + : "cooldown"; + const hint = resolveUnusableProfileHint({ + kind: disabledActive ? "disabled" : "cooldown", + reason: stats?.disabledReason, + }); out.push(`- ${profileId}: ${kind} (${remaining})${hint ? ` — ${hint}` : ""}`); } return out; diff --git a/src/commands/models/list.probe.test.ts b/src/commands/models/list.probe.test.ts new file mode 100644 index 000000000000..55c5ef064f30 --- /dev/null +++ b/src/commands/models/list.probe.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { mapFailoverReasonToProbeStatus } from "./list.probe.js"; + +describe("mapFailoverReasonToProbeStatus", () => { + it("maps auth_permanent to auth", () => { + expect(mapFailoverReasonToProbeStatus("auth_permanent")).toBe("auth"); + }); + + it("keeps existing failover reason mappings", () => { + expect(mapFailoverReasonToProbeStatus("auth")).toBe("auth"); + expect(mapFailoverReasonToProbeStatus("rate_limit")).toBe("rate_limit"); + expect(mapFailoverReasonToProbeStatus("billing")).toBe("billing"); + expect(mapFailoverReasonToProbeStatus("timeout")).toBe("timeout"); + expect(mapFailoverReasonToProbeStatus("format")).toBe("format"); + }); + + it("falls back to unknown for unrecognized values", () => { + expect(mapFailoverReasonToProbeStatus(undefined)).toBe("unknown"); + expect(mapFailoverReasonToProbeStatus(null)).toBe("unknown"); + expect(mapFailoverReasonToProbeStatus("model_not_found")).toBe("unknown"); + }); +}); diff --git a/src/commands/models/list.probe.ts b/src/commands/models/list.probe.ts index 60b38316117f..ef48564df88e 100644 --- a/src/commands/models/list.probe.ts +++ b/src/commands/models/list.probe.ts @@ -82,11 +82,13 @@ export type AuthProbeOptions = { maxTokens: number; }; -const toStatus = (reason?: string | null): AuthProbeStatus => { +export function mapFailoverReasonToProbeStatus(reason?: string | null): AuthProbeStatus { if (!reason) { return "unknown"; } - if (reason === "auth") { + if (reason === "auth" || reason === "auth_permanent") { + // Keep probe output backward-compatible: permanent auth failures still + // surface in the auth bucket instead of showing as unknown. return "auth"; } if (reason === "rate_limit") { @@ -102,7 +104,7 @@ const toStatus = (reason?: string | null): AuthProbeStatus => { return "format"; } return "unknown"; -}; +} function buildCandidateMap(modelCandidates: string[]): Map { const map = new Map(); @@ -346,7 +348,7 @@ async function probeTarget(params: { label: target.label, source: target.source, mode: target.mode, - status: toStatus(described.reason), + status: mapFailoverReasonToProbeStatus(described.reason), error: redactSecrets(described.message), latencyMs: Date.now() - start, }; From 70e31c6f68b88bcf91f6df827a0ea05bb90ab112 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:47:07 +0100 Subject: [PATCH 062/160] fix(gateway): harden hooks URL parsing (#26864) --- .../server-http.hooks-request-timeout.test.ts | 20 +++++++++++++++++-- src/gateway/server-http.ts | 6 ++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/gateway/server-http.hooks-request-timeout.test.ts b/src/gateway/server-http.hooks-request-timeout.test.ts index 448707eb1c7c..577ffe1ab43c 100644 --- a/src/gateway/server-http.hooks-request-timeout.test.ts +++ b/src/gateway/server-http.hooks-request-timeout.test.ts @@ -41,10 +41,11 @@ function createHooksConfig(): HooksConfigResolved { function createRequest(params?: { authorization?: string; remoteAddress?: string; + url?: string; }): IncomingMessage { return { method: "POST", - url: "/hooks/wake", + url: params?.url ?? "/hooks/wake", headers: { host: "127.0.0.1:18789", authorization: params?.authorization ?? "Bearer hook-secret", @@ -71,10 +72,11 @@ function createResponse(): { function createHandler(params?: { dispatchWakeHook?: HooksHandlerDeps["dispatchWakeHook"]; dispatchAgentHook?: HooksHandlerDeps["dispatchAgentHook"]; + bindHost?: string; }) { return createHooksRequestHandler({ getHooksConfig: () => createHooksConfig(), - bindHost: "127.0.0.1", + bindHost: params?.bindHost ?? "127.0.0.1", port: 18789, logHooks: { warn: vi.fn(), @@ -139,4 +141,18 @@ describe("createHooksRequestHandler timeout status mapping", () => { expect(mappedRes.statusCode).toBe(429); expect(setHeader).toHaveBeenCalledWith("Retry-After", expect.any(String)); }); + + test.each(["0.0.0.0", "::"])( + "does not throw when bindHost=%s while parsing non-hook request URL", + async (bindHost) => { + const handler = createHandler({ bindHost }); + const req = createRequest({ url: "/" }); + const { res, end } = createResponse(); + + const handled = await handler(req, res); + + expect(handled).toBe(false); + expect(end).not.toHaveBeenCalled(); + }, + ); }); diff --git a/src/gateway/server-http.ts b/src/gateway/server-http.ts index 72a81a769ad6..41d04d5d3ac8 100644 --- a/src/gateway/server-http.ts +++ b/src/gateway/server-http.ts @@ -208,7 +208,7 @@ export function createHooksRequestHandler( logHooks: SubsystemLogger; } & HookDispatchers, ): HooksRequestHandler { - const { getHooksConfig, bindHost, port, logHooks, dispatchAgentHook, dispatchWakeHook } = opts; + const { getHooksConfig, logHooks, dispatchAgentHook, dispatchWakeHook } = opts; const hookAuthLimiter = createAuthRateLimiter({ maxAttempts: HOOK_AUTH_FAILURE_LIMIT, windowMs: HOOK_AUTH_FAILURE_WINDOW_MS, @@ -227,7 +227,9 @@ export function createHooksRequestHandler( if (!hooksConfig) { return false; } - const url = new URL(req.url ?? "/", `http://${bindHost}:${port}`); + // Only pathname/search are used here; keep the base host fixed so bind-host + // representation (e.g. IPv6 wildcards) cannot break request parsing. + const url = new URL(req.url ?? "/", "http://localhost"); const basePath = hooksConfig.basePath; if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) { return false; From 6fb082e13160cfa7282c587a225d80b0e8c770bd Mon Sep 17 00:00:00 2001 From: codexGW <9350182+codexGW@users.noreply.github.com> Date: Wed, 25 Feb 2026 16:53:38 -0800 Subject: [PATCH 063/160] fix(typing): call markDispatchIdle in followup runner to prevent stuck indicator (#26881) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The followup runner (used for queued messages, inter-agent sends, heartbeat followups, etc.) only called typing.markRunComplete() in its finally block. The typing controller requires BOTH markRunComplete AND markDispatchIdle to trigger cleanup — but markDispatchIdle was only wired through the buffered dispatcher path, which followup turns bypass entirely. This caused the typing indicator to persist indefinitely on channels like Telegram when the agent replied with NO_REPLY or produced empty payloads, because the keepalive loop was never stopped. Adds markDispatchIdle() alongside markRunComplete() in the followup runner's finally block, and four test cases covering NO_REPLY, empty payloads, agent errors, and successful delivery. Complements #26295 which addressed the channel-level callback layer. Fixes #26595 Co-authored-by: Samantha --- src/auto-reply/reply/followup-runner.test.ts | 81 ++++++++++++++++++++ src/auto-reply/reply/followup-runner.ts | 8 ++ 2 files changed, 89 insertions(+) diff --git a/src/auto-reply/reply/followup-runner.test.ts b/src/auto-reply/reply/followup-runner.test.ts index da5d55fa9dd1..a6e0c9f849ac 100644 --- a/src/auto-reply/reply/followup-runner.test.ts +++ b/src/auto-reply/reply/followup-runner.test.ts @@ -428,6 +428,87 @@ describe("createFollowupRunner messaging tool dedupe", () => { }); }); +describe("createFollowupRunner typing cleanup", () => { + it("calls both markRunComplete and markDispatchIdle on NO_REPLY", async () => { + const typing = createMockTypingController(); + runEmbeddedPiAgentMock.mockResolvedValueOnce({ + payloads: [{ text: "NO_REPLY" }], + meta: {}, + }); + + const runner = createFollowupRunner({ + opts: { onBlockReply: vi.fn(async () => {}) }, + typing, + typingMode: "instant", + defaultModel: "anthropic/claude-opus-4-5", + }); + + await runner(baseQueuedRun()); + + expect(typing.markRunComplete).toHaveBeenCalled(); + expect(typing.markDispatchIdle).toHaveBeenCalled(); + }); + + it("calls both markRunComplete and markDispatchIdle on empty payloads", async () => { + const typing = createMockTypingController(); + runEmbeddedPiAgentMock.mockResolvedValueOnce({ + payloads: [], + meta: {}, + }); + + const runner = createFollowupRunner({ + opts: { onBlockReply: vi.fn(async () => {}) }, + typing, + typingMode: "instant", + defaultModel: "anthropic/claude-opus-4-5", + }); + + await runner(baseQueuedRun()); + + expect(typing.markRunComplete).toHaveBeenCalled(); + expect(typing.markDispatchIdle).toHaveBeenCalled(); + }); + + it("calls both markRunComplete and markDispatchIdle on agent error", async () => { + const typing = createMockTypingController(); + runEmbeddedPiAgentMock.mockRejectedValueOnce(new Error("agent exploded")); + + const runner = createFollowupRunner({ + opts: { onBlockReply: vi.fn(async () => {}) }, + typing, + typingMode: "instant", + defaultModel: "anthropic/claude-opus-4-5", + }); + + await runner(baseQueuedRun()); + + expect(typing.markRunComplete).toHaveBeenCalled(); + expect(typing.markDispatchIdle).toHaveBeenCalled(); + }); + + it("calls both markRunComplete and markDispatchIdle on successful delivery", async () => { + const typing = createMockTypingController(); + const onBlockReply = vi.fn(async () => {}); + runEmbeddedPiAgentMock.mockResolvedValueOnce({ + payloads: [{ text: "hello world!" }], + meta: {}, + }); + + const runner = createFollowupRunner({ + opts: { onBlockReply }, + typing, + typingMode: "instant", + defaultModel: "anthropic/claude-opus-4-5", + }); + + await runner(baseQueuedRun()); + + expect(onBlockReply).toHaveBeenCalled(); + expect(typing.markRunComplete).toHaveBeenCalled(); + expect(typing.markDispatchIdle).toHaveBeenCalled(); + }); +}); + describe("createFollowupRunner agentDir forwarding", () => { it("passes queued run agentDir to runEmbeddedPiAgent", async () => { runEmbeddedPiAgentMock.mockClear(); diff --git a/src/auto-reply/reply/followup-runner.ts b/src/auto-reply/reply/followup-runner.ts index 0c91d543d916..3f280d18e52b 100644 --- a/src/auto-reply/reply/followup-runner.ts +++ b/src/auto-reply/reply/followup-runner.ts @@ -314,7 +314,15 @@ export function createFollowupRunner(params: { await sendFollowupPayloads(finalPayloads, queued); } finally { + // Both signals are required for the typing controller to clean up. + // The main inbound dispatch path calls markDispatchIdle() from the + // buffered dispatcher's finally block, but followup turns bypass the + // dispatcher entirely — so we must fire both signals here. Without + // this, NO_REPLY / empty-payload followups leave the typing indicator + // stuck (the keepalive loop keeps sending "typing" to Telegram + // indefinitely until the TTL expires). typing.markRunComplete(); + typing.markDispatchIdle(); } }; } From ec45c317f5d0631a3d333b236da58c4749ede2a3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:38:45 +0100 Subject: [PATCH 064/160] fix(gateway): block trusted-proxy control-ui node bypass --- src/gateway/server.auth.test.ts | 108 +++++++++++++++++- .../server/ws-connection/message-handler.ts | 2 + 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/gateway/server.auth.test.ts b/src/gateway/server.auth.test.ts index 83a97644d193..900ef34b6b4c 100644 --- a/src/gateway/server.auth.test.ts +++ b/src/gateway/server.auth.test.ts @@ -131,10 +131,11 @@ async function expectHelloOkServerVersion(port: number, expectedVersion: string) } async function createSignedDevice(params: { - token: string; + token?: string | null; scopes: string[]; clientId: string; clientMode: string; + role?: "operator" | "node"; identityPath?: string; nonce: string; signedAtMs?: number; @@ -149,10 +150,10 @@ async function createSignedDevice(params: { deviceId: identity.deviceId, clientId: params.clientId, clientMode: params.clientMode, - role: "operator", + role: params.role ?? "operator", scopes: params.scopes, signedAtMs, - token: params.token, + token: params.token ?? null, nonce: params.nonce, }); return { @@ -187,6 +188,23 @@ async function approvePendingPairingIfNeeded() { } } +async function configureTrustedProxyControlUiAuth() { + testState.gatewayAuth = { + mode: "trusted-proxy", + trustedProxy: { + userHeader: "x-forwarded-user", + requiredHeaders: ["x-forwarded-proto"], + }, + }; + const { writeConfigFile } = await import("../config/config.js"); + await writeConfigFile({ + gateway: { + trustedProxies: ["127.0.0.1"], + }, + // oxlint-disable-next-line typescript/no-explicit-any + } as any); +} + function isConnectResMessage(id: string) { return (o: unknown) => { if (!o || typeof o !== "object" || Array.isArray(o)) { @@ -776,6 +794,90 @@ describe("gateway server auth/connect", () => { }); }); + test("allows trusted-proxy control ui operator without device identity", async () => { + await configureTrustedProxyControlUiAuth(); + await withGatewayServer(async ({ port }) => { + const ws = await openWs(port, { + origin: "https://localhost", + "x-forwarded-for": "203.0.113.10", + "x-forwarded-proto": "https", + "x-forwarded-user": "peter@example.com", + }); + const res = await connectReq(ws, { + skipDefaultAuth: true, + role: "operator", + device: null, + client: { ...CONTROL_UI_CLIENT }, + }); + expect(res.ok).toBe(true); + const status = await rpcReq(ws, "status"); + expect(status.ok).toBe(false); + expect(status.error?.message ?? "").toContain("missing scope"); + const health = await rpcReq(ws, "health"); + expect(health.ok).toBe(true); + ws.close(); + }); + }); + + test("rejects trusted-proxy control ui node role without device identity", async () => { + await configureTrustedProxyControlUiAuth(); + await withGatewayServer(async ({ port }) => { + const ws = await openWs(port, { + origin: "https://localhost", + "x-forwarded-for": "203.0.113.10", + "x-forwarded-proto": "https", + "x-forwarded-user": "peter@example.com", + }); + const res = await connectReq(ws, { + skipDefaultAuth: true, + role: "node", + device: null, + client: { ...CONTROL_UI_CLIENT }, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("control ui requires device identity"); + expect((res.error?.details as { code?: string } | undefined)?.code).toBe( + ConnectErrorDetailCodes.CONTROL_UI_DEVICE_IDENTITY_REQUIRED, + ); + ws.close(); + }); + }); + + test("requires pairing for trusted-proxy control ui node role with unpaired device", async () => { + await configureTrustedProxyControlUiAuth(); + await withGatewayServer(async ({ port }) => { + const ws = await openWs(port, { + origin: "https://localhost", + "x-forwarded-for": "203.0.113.10", + "x-forwarded-proto": "https", + "x-forwarded-user": "peter@example.com", + }); + const challengeNonce = await readConnectChallengeNonce(ws); + expect(challengeNonce).toBeTruthy(); + const { device } = await createSignedDevice({ + token: null, + role: "node", + scopes: [], + clientId: GATEWAY_CLIENT_NAMES.CONTROL_UI, + clientMode: GATEWAY_CLIENT_MODES.WEBCHAT, + nonce: String(challengeNonce), + }); + const res = await connectReq(ws, { + skipDefaultAuth: true, + role: "node", + scopes: [], + device, + client: { ...CONTROL_UI_CLIENT }, + }); + expect(res.ok).toBe(false); + expect(res.error?.message ?? "").toContain("pairing required"); + expect((res.error?.details as { code?: string } | undefined)?.code).toBe( + ConnectErrorDetailCodes.PAIRING_REQUIRED, + ); + ws.close(); + }); + }); + test("allows localhost control ui without device identity when insecure auth is enabled", async () => { testState.gatewayControlUi = { allowInsecureAuth: true }; const { server, ws, prevToken } = await startServerWithClient("secret", { diff --git a/src/gateway/server/ws-connection/message-handler.ts b/src/gateway/server/ws-connection/message-handler.ts index 8feb385e8f92..30d288b651d1 100644 --- a/src/gateway/server/ws-connection/message-handler.ts +++ b/src/gateway/server/ws-connection/message-handler.ts @@ -491,6 +491,7 @@ export function attachGatewayWsMessageHandler(params: { } const trustedProxyAuthOk = isControlUi && + role === "operator" && resolvedAuth.mode === "trusted-proxy" && authOk && authMethod === "trusted-proxy"; @@ -629,6 +630,7 @@ export function attachGatewayWsMessageHandler(params: { const trustedProxyAuthOk = isControlUi && + role === "operator" && resolvedAuth.mode === "trusted-proxy" && authOk && authMethod === "trusted-proxy"; From 3cd3d489f4ea9ceb03be41d17d8259f17bbb38b8 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:54:04 +0100 Subject: [PATCH 065/160] docs(changelog): note trusted-proxy control-ui hardening --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4ff72155d18..5df5ae62dda3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Docs: https://docs.openclaw.ai - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. - Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. +- Security/Gateway trusted proxy: require `operator` role for the Control UI trusted-proxy pairing bypass so unpaired `node` sessions can no longer connect via `client.id=control-ui` and invoke node event methods. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Discord/Inbound text: preserve embed `title` + `description` fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. From 8c701ba1ffb8907002e0370e99dd0e3ec8da79ba Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:54:10 +0100 Subject: [PATCH 066/160] test(gateway): add hooks bind-host hardening coverage --- src/gateway/server.plugin-http-auth.test.ts | 120 +++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/src/gateway/server.plugin-http-auth.test.ts b/src/gateway/server.plugin-http-auth.test.ts index 25568d4803e4..79093169c6ab 100644 --- a/src/gateway/server.plugin-http-auth.test.ts +++ b/src/gateway/server.plugin-http-auth.test.ts @@ -1,7 +1,9 @@ import type { IncomingMessage, ServerResponse } from "node:http"; import { describe, expect, test, vi } from "vitest"; +import type { createSubsystemLogger } from "../logging/subsystem.js"; import type { ResolvedGatewayAuth } from "./auth.js"; -import { createGatewayHttpServer } from "./server-http.js"; +import type { HooksConfigResolved } from "./hooks.js"; +import { createGatewayHttpServer, createHooksRequestHandler } from "./server-http.js"; import { withTempConfig } from "./test-temp-config.js"; function createRequest(params: { @@ -65,6 +67,25 @@ async function dispatchRequest( await new Promise((resolve) => setImmediate(resolve)); } +function createHooksConfig(): HooksConfigResolved { + return { + basePath: "/hooks", + token: "hook-secret", + maxBodyBytes: 1024, + mappings: [], + agentPolicy: { + defaultAgentId: "main", + knownAgentIds: new Set(["main"]), + allowedAgentIds: undefined, + }, + sessionPolicy: { + allowRequestSessionKey: false, + defaultSessionKey: undefined, + allowedSessionKeyPrefixes: undefined, + }, + }; +} + describe("gateway plugin HTTP auth boundary", () => { test("applies default security headers and optional strict transport security", async () => { const resolvedAuth: ResolvedGatewayAuth = { @@ -220,4 +241,101 @@ describe("gateway plugin HTTP auth boundary", () => { }, }); }); + + test.each(["0.0.0.0", "::"])( + "returns 404 (not 500) for non-hook routes with hooks enabled and bindHost=%s", + async (bindHost) => { + const resolvedAuth: ResolvedGatewayAuth = { + mode: "none", + token: undefined, + password: undefined, + allowTailscale: false, + }; + + await withTempConfig({ + cfg: { gateway: { trustedProxies: [] } }, + prefix: "openclaw-plugin-http-hooks-bindhost-", + run: async () => { + const handleHooksRequest = createHooksRequestHandler({ + getHooksConfig: () => createHooksConfig(), + bindHost, + port: 18789, + logHooks: { + warn: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + error: vi.fn(), + } as unknown as ReturnType, + dispatchWakeHook: () => {}, + dispatchAgentHook: () => "run-1", + }); + const server = createGatewayHttpServer({ + canvasHost: null, + clients: new Set(), + controlUiEnabled: false, + controlUiBasePath: "/__control__", + openAiChatCompletionsEnabled: false, + openResponsesEnabled: false, + handleHooksRequest, + resolvedAuth, + }); + + const response = createResponse(); + await dispatchRequest(server, createRequest({ path: "/" }), response.res); + + expect(response.res.statusCode).toBe(404); + expect(response.getBody()).toBe("Not Found"); + }, + }); + }, + ); + + test("rejects query-token hooks requests with bindHost=::", async () => { + const resolvedAuth: ResolvedGatewayAuth = { + mode: "none", + token: undefined, + password: undefined, + allowTailscale: false, + }; + + await withTempConfig({ + cfg: { gateway: { trustedProxies: [] } }, + prefix: "openclaw-plugin-http-hooks-query-token-", + run: async () => { + const handleHooksRequest = createHooksRequestHandler({ + getHooksConfig: () => createHooksConfig(), + bindHost: "::", + port: 18789, + logHooks: { + warn: vi.fn(), + debug: vi.fn(), + info: vi.fn(), + error: vi.fn(), + } as unknown as ReturnType, + dispatchWakeHook: () => {}, + dispatchAgentHook: () => "run-1", + }); + const server = createGatewayHttpServer({ + canvasHost: null, + clients: new Set(), + controlUiEnabled: false, + controlUiBasePath: "/__control__", + openAiChatCompletionsEnabled: false, + openResponsesEnabled: false, + handleHooksRequest, + resolvedAuth, + }); + + const response = createResponse(); + await dispatchRequest( + server, + createRequest({ path: "/hooks/wake?token=bad" }), + response.res, + ); + + expect(response.res.statusCode).toBe(400); + expect(response.getBody()).toContain("Hook token must be provided"); + }, + }); + }); }); From 5e1bfb2ce2b73a27d04e03c5049910753f09aa3f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 01:07:27 +0000 Subject: [PATCH 067/160] docs(changelog): add followup typing fix note (#26881) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5df5ae62dda3..339093dbc8b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ Docs: https://docs.openclaw.ai - Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. - Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. - Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. +- Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. From ce8c67c314b93f570f53c2a9abc124e1e3a54715 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:11:36 +0100 Subject: [PATCH 068/160] fix(slack): gate interactive system events by sender auth --- CHANGELOG.md | 1 + src/slack/modal-metadata.test.ts | 4 + src/slack/modal-metadata.ts | 3 + src/slack/monitor/auth.ts | 144 +++++++++++- src/slack/monitor/events/interactions.test.ts | 208 +++++++++++++++++- src/slack/monitor/events/interactions.ts | 75 ++++++- 6 files changed, 415 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 339093dbc8b4..0df4711abafc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Docs: https://docs.openclaw.ai - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Slack interactions: enforce channel/DM authorization and modal actor binding (`private_metadata.userId`) before enqueueing `block_action`/`view_submission`/`view_closed` system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. diff --git a/src/slack/modal-metadata.test.ts b/src/slack/modal-metadata.test.ts index d209c70587cb..a7a7ce8224b6 100644 --- a/src/slack/modal-metadata.test.ts +++ b/src/slack/modal-metadata.test.ts @@ -18,6 +18,7 @@ describe("parseSlackModalPrivateMetadata", () => { sessionKey: "agent:main:slack:channel:C1", channelId: "D123", channelType: "im", + userId: "U123", ignored: "x", }), ), @@ -25,6 +26,7 @@ describe("parseSlackModalPrivateMetadata", () => { sessionKey: "agent:main:slack:channel:C1", channelId: "D123", channelType: "im", + userId: "U123", }); }); }); @@ -37,11 +39,13 @@ describe("encodeSlackModalPrivateMetadata", () => { sessionKey: "agent:main:slack:channel:C1", channelId: "", channelType: "im", + userId: "U123", }), ), ).toEqual({ sessionKey: "agent:main:slack:channel:C1", channelType: "im", + userId: "U123", }); }); diff --git a/src/slack/modal-metadata.ts b/src/slack/modal-metadata.ts index 491fb5d38f31..963024487a93 100644 --- a/src/slack/modal-metadata.ts +++ b/src/slack/modal-metadata.ts @@ -2,6 +2,7 @@ export type SlackModalPrivateMetadata = { sessionKey?: string; channelId?: string; channelType?: string; + userId?: string; }; const SLACK_PRIVATE_METADATA_MAX = 3000; @@ -20,6 +21,7 @@ export function parseSlackModalPrivateMetadata(raw: unknown): SlackModalPrivateM sessionKey: normalizeString(parsed.sessionKey), channelId: normalizeString(parsed.channelId), channelType: normalizeString(parsed.channelType), + userId: normalizeString(parsed.userId), }; } catch { return {}; @@ -31,6 +33,7 @@ export function encodeSlackModalPrivateMetadata(input: SlackModalPrivateMetadata ...(input.sessionKey ? { sessionKey: input.sessionKey } : {}), ...(input.channelId ? { channelId: input.channelId } : {}), ...(input.channelType ? { channelType: input.channelType } : {}), + ...(input.userId ? { userId: input.userId } : {}), }; const encoded = JSON.stringify(payload); if (encoded.length > SLACK_PRIVATE_METADATA_MAX) { diff --git a/src/slack/monitor/auth.ts b/src/slack/monitor/auth.ts index d8fa5e5b4e5a..cb43241f899a 100644 --- a/src/slack/monitor/auth.ts +++ b/src/slack/monitor/auth.ts @@ -1,6 +1,12 @@ import { readChannelAllowFromStore } from "../../pairing/pairing-store.js"; -import { allowListMatches, normalizeAllowList, normalizeAllowListLower } from "./allow-list.js"; -import type { SlackMonitorContext } from "./context.js"; +import { + allowListMatches, + normalizeAllowList, + normalizeAllowListLower, + resolveSlackUserAllowed, +} from "./allow-list.js"; +import { resolveSlackChannelConfig } from "./channel-config.js"; +import { normalizeSlackChannelType, type SlackMonitorContext } from "./context.js"; export async function resolveSlackEffectiveAllowFrom(ctx: SlackMonitorContext) { const storeAllowFrom = @@ -27,3 +33,137 @@ export function isSlackSenderAllowListed(params: { }) ); } + +export type SlackSystemEventAuthResult = { + allowed: boolean; + reason?: + | "missing-sender" + | "sender-mismatch" + | "channel-not-allowed" + | "dm-disabled" + | "sender-not-allowlisted" + | "sender-not-channel-allowed"; + channelType?: "im" | "mpim" | "channel" | "group"; + channelName?: string; +}; + +export async function authorizeSlackSystemEventSender(params: { + ctx: SlackMonitorContext; + senderId?: string; + channelId?: string; + channelType?: string | null; + expectedSenderId?: string; +}): Promise { + const senderId = params.senderId?.trim(); + if (!senderId) { + return { allowed: false, reason: "missing-sender" }; + } + + const expectedSenderId = params.expectedSenderId?.trim(); + if (expectedSenderId && expectedSenderId !== senderId) { + return { allowed: false, reason: "sender-mismatch" }; + } + + const channelId = params.channelId?.trim(); + let channelType = normalizeSlackChannelType(params.channelType, channelId); + let channelName: string | undefined; + if (channelId) { + const info: { + name?: string; + type?: "im" | "mpim" | "channel" | "group"; + } = await params.ctx.resolveChannelName(channelId).catch(() => ({})); + channelName = info.name; + channelType = normalizeSlackChannelType(params.channelType ?? info.type, channelId); + if ( + !params.ctx.isChannelAllowed({ + channelId, + channelName, + channelType, + }) + ) { + return { + allowed: false, + reason: "channel-not-allowed", + channelType, + channelName, + }; + } + } + + const senderInfo: { name?: string } = await params.ctx + .resolveUserName(senderId) + .catch(() => ({})); + const senderName = senderInfo.name; + + const resolveAllowFromLower = async () => + (await resolveSlackEffectiveAllowFrom(params.ctx)).allowFromLower; + + if (channelType === "im") { + if (!params.ctx.dmEnabled || params.ctx.dmPolicy === "disabled") { + return { allowed: false, reason: "dm-disabled", channelType, channelName }; + } + if (params.ctx.dmPolicy !== "open") { + const allowFromLower = await resolveAllowFromLower(); + const senderAllowListed = isSlackSenderAllowListed({ + allowListLower: allowFromLower, + senderId, + senderName, + allowNameMatching: params.ctx.allowNameMatching, + }); + if (!senderAllowListed) { + return { + allowed: false, + reason: "sender-not-allowlisted", + channelType, + channelName, + }; + } + } + } else if (!channelId) { + // No channel context. Apply allowFrom if configured so we fail closed + // for privileged interactive events when owner allowlist is present. + const allowFromLower = await resolveAllowFromLower(); + if (allowFromLower.length > 0) { + const senderAllowListed = isSlackSenderAllowListed({ + allowListLower: allowFromLower, + senderId, + senderName, + allowNameMatching: params.ctx.allowNameMatching, + }); + if (!senderAllowListed) { + return { allowed: false, reason: "sender-not-allowlisted" }; + } + } + } else { + const channelConfig = resolveSlackChannelConfig({ + channelId, + channelName, + channels: params.ctx.channelsConfig, + defaultRequireMention: params.ctx.defaultRequireMention, + }); + const channelUsersAllowlistConfigured = + Array.isArray(channelConfig?.users) && channelConfig.users.length > 0; + if (channelUsersAllowlistConfigured) { + const channelUserAllowed = resolveSlackUserAllowed({ + allowList: channelConfig?.users, + userId: senderId, + userName: senderName, + allowNameMatching: params.ctx.allowNameMatching, + }); + if (!channelUserAllowed) { + return { + allowed: false, + reason: "sender-not-channel-allowed", + channelType, + channelName, + }; + } + } + } + + return { + allowed: true, + channelType, + channelName, + }; +} diff --git a/src/slack/monitor/events/interactions.test.ts b/src/slack/monitor/events/interactions.test.ts index 7710239cc710..cfd535063588 100644 --- a/src/slack/monitor/events/interactions.test.ts +++ b/src/slack/monitor/events/interactions.test.ts @@ -30,6 +30,7 @@ type RegisteredViewHandler = (args: { view?: { id?: string; callback_id?: string; + private_metadata?: string; root_view_id?: string; previous_view_id?: string; external_id?: string; @@ -58,7 +59,23 @@ type RegisteredViewClosedHandler = (args: { }; }) => Promise; -function createContext() { +function createContext(overrides?: { + dmEnabled?: boolean; + dmPolicy?: "open" | "allowlist" | "pairing" | "disabled"; + allowFrom?: string[]; + allowNameMatching?: boolean; + channelsConfig?: Record; + isChannelAllowed?: (params: { + channelId?: string; + channelName?: string; + channelType?: "im" | "mpim" | "channel" | "group"; + }) => boolean; + resolveUserName?: (userId: string) => Promise<{ name?: string }>; + resolveChannelName?: (channelId: string) => Promise<{ + name?: string; + type?: "im" | "mpim" | "channel" | "group"; + }>; +}) { let handler: RegisteredHandler | null = null; let viewHandler: RegisteredViewHandler | null = null; let viewClosedHandler: RegisteredViewClosedHandler | null = null; @@ -80,9 +97,40 @@ function createContext() { }; const runtimeLog = vi.fn(); const resolveSessionKey = vi.fn().mockReturnValue("agent:ops:slack:channel:C1"); + const isChannelAllowed = vi + .fn< + (params: { + channelId?: string; + channelName?: string; + channelType?: "im" | "mpim" | "channel" | "group"; + }) => boolean + >() + .mockImplementation((params) => overrides?.isChannelAllowed?.(params) ?? true); + const resolveUserName = vi + .fn<(userId: string) => Promise<{ name?: string }>>() + .mockImplementation((userId) => overrides?.resolveUserName?.(userId) ?? Promise.resolve({})); + const resolveChannelName = vi + .fn< + (channelId: string) => Promise<{ + name?: string; + type?: "im" | "mpim" | "channel" | "group"; + }> + >() + .mockImplementation( + (channelId) => overrides?.resolveChannelName?.(channelId) ?? Promise.resolve({}), + ); const ctx = { app, runtime: { log: runtimeLog }, + dmEnabled: overrides?.dmEnabled ?? true, + dmPolicy: overrides?.dmPolicy ?? ("open" as const), + allowFrom: overrides?.allowFrom ?? [], + allowNameMatching: overrides?.allowNameMatching ?? false, + channelsConfig: overrides?.channelsConfig ?? {}, + defaultRequireMention: true, + isChannelAllowed, + resolveUserName, + resolveChannelName, resolveSlackSystemEventSessionKey: resolveSessionKey, }; return { @@ -90,6 +138,9 @@ function createContext() { app, runtimeLog, resolveSessionKey, + isChannelAllowed, + resolveUserName, + resolveChannelName, getHandler: () => handler, getViewHandler: () => viewHandler, getViewClosedHandler: () => viewClosedHandler, @@ -168,7 +219,7 @@ describe("registerSlackInteractionEvents", () => { }); expect(resolveSessionKey).toHaveBeenCalledWith({ channelId: "C1", - channelType: undefined, + channelType: "channel", }); expect(app.client.chat.update).toHaveBeenCalledTimes(1); }); @@ -228,6 +279,85 @@ describe("registerSlackInteractionEvents", () => { ); }); + it("blocks block actions from users outside configured channel users allowlist", async () => { + enqueueSystemEventMock.mockClear(); + const { ctx, app, getHandler } = createContext({ + channelsConfig: { + C1: { users: ["U_ALLOWED"] }, + }, + }); + registerSlackInteractionEvents({ ctx: ctx as never }); + const handler = getHandler(); + expect(handler).toBeTruthy(); + + const ack = vi.fn().mockResolvedValue(undefined); + const respond = vi.fn().mockResolvedValue(undefined); + await handler!({ + ack, + respond, + body: { + user: { id: "U_DENIED" }, + channel: { id: "C1" }, + message: { + ts: "201.202", + blocks: [{ type: "actions", block_id: "verify_block", elements: [] }], + }, + }, + action: { + type: "button", + action_id: "openclaw:verify", + block_id: "verify_block", + }, + }); + + expect(ack).toHaveBeenCalled(); + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + expect(app.client.chat.update).not.toHaveBeenCalled(); + expect(respond).toHaveBeenCalledWith({ + text: "You are not authorized to use this control.", + response_type: "ephemeral", + }); + }); + + it("blocks DM block actions when sender is not in allowFrom", async () => { + enqueueSystemEventMock.mockClear(); + const { ctx, app, getHandler } = createContext({ + dmPolicy: "allowlist", + allowFrom: ["U_OWNER"], + }); + registerSlackInteractionEvents({ ctx: ctx as never }); + const handler = getHandler(); + expect(handler).toBeTruthy(); + + const ack = vi.fn().mockResolvedValue(undefined); + const respond = vi.fn().mockResolvedValue(undefined); + await handler!({ + ack, + respond, + body: { + user: { id: "U_ATTACKER" }, + channel: { id: "D222" }, + message: { + ts: "301.302", + blocks: [{ type: "actions", block_id: "verify_block", elements: [] }], + }, + }, + action: { + type: "button", + action_id: "openclaw:verify", + block_id: "verify_block", + }, + }); + + expect(ack).toHaveBeenCalled(); + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + expect(app.client.chat.update).not.toHaveBeenCalled(); + expect(respond).toHaveBeenCalledWith({ + text: "You are not authorized to use this control.", + response_type: "ephemeral", + }); + }); + it("ignores malformed action payloads after ack and logs warning", async () => { enqueueSystemEventMock.mockClear(); const { ctx, app, getHandler, runtimeLog } = createContext(); @@ -338,7 +468,7 @@ describe("registerSlackInteractionEvents", () => { expect(ack).toHaveBeenCalled(); expect(resolveSessionKey).toHaveBeenCalledWith({ channelId: "C222", - channelType: undefined, + channelType: "channel", }); expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); const [eventText] = enqueueSystemEventMock.mock.calls[0] as [string]; @@ -697,7 +827,11 @@ describe("registerSlackInteractionEvents", () => { previous_view_id: "VPREV", external_id: "deploy-ext-1", hash: "view-hash-1", - private_metadata: JSON.stringify({ channelId: "D123", channelType: "im" }), + private_metadata: JSON.stringify({ + channelId: "D123", + channelType: "im", + userId: "U777", + }), state: { values: { env_block: { @@ -771,6 +905,59 @@ describe("registerSlackInteractionEvents", () => { ); }); + it("blocks modal events when private metadata userId does not match submitter", async () => { + enqueueSystemEventMock.mockClear(); + const { ctx, getViewHandler } = createContext(); + registerSlackInteractionEvents({ ctx: ctx as never }); + const viewHandler = getViewHandler(); + expect(viewHandler).toBeTruthy(); + + const ack = vi.fn().mockResolvedValue(undefined); + await viewHandler!({ + ack, + body: { + user: { id: "U222" }, + view: { + callback_id: "openclaw:deploy_form", + private_metadata: JSON.stringify({ + channelId: "D123", + channelType: "im", + userId: "U111", + }), + }, + }, + } as never); + + expect(ack).toHaveBeenCalled(); + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + + it("blocks modal events when private metadata is missing userId", async () => { + enqueueSystemEventMock.mockClear(); + const { ctx, getViewHandler } = createContext(); + registerSlackInteractionEvents({ ctx: ctx as never }); + const viewHandler = getViewHandler(); + expect(viewHandler).toBeTruthy(); + + const ack = vi.fn().mockResolvedValue(undefined); + await viewHandler!({ + ack, + body: { + user: { id: "U222" }, + view: { + callback_id: "openclaw:deploy_form", + private_metadata: JSON.stringify({ + channelId: "D123", + channelType: "im", + }), + }, + }, + } as never); + + expect(ack).toHaveBeenCalled(); + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + it("captures modal input labels and picker values across block types", async () => { enqueueSystemEventMock.mockClear(); const { ctx, getViewHandler } = createContext(); @@ -786,6 +973,7 @@ describe("registerSlackInteractionEvents", () => { view: { id: "V400", callback_id: "openclaw:routing_form", + private_metadata: JSON.stringify({ userId: "U444" }), state: { values: { env_block: { @@ -1001,6 +1189,7 @@ describe("registerSlackInteractionEvents", () => { view: { id: "V555", callback_id: "openclaw:long_richtext", + private_metadata: JSON.stringify({ userId: "U555" }), state: { values: { richtext_block: { @@ -1054,7 +1243,10 @@ describe("registerSlackInteractionEvents", () => { previous_view_id: "VPREV900", external_id: "deploy-ext-900", hash: "view-hash-900", - private_metadata: JSON.stringify({ sessionKey: "agent:main:slack:channel:C99" }), + private_metadata: JSON.stringify({ + sessionKey: "agent:main:slack:channel:C99", + userId: "U900", + }), state: { values: { env_block: { @@ -1101,7 +1293,10 @@ describe("registerSlackInteractionEvents", () => { viewId: "V900", userId: "U900", isCleared: true, - privateMetadata: JSON.stringify({ sessionKey: "agent:main:slack:channel:C99" }), + privateMetadata: JSON.stringify({ + sessionKey: "agent:main:slack:channel:C99", + userId: "U900", + }), rootViewId: "VROOT900", previousViewId: "VPREV900", externalId: "deploy-ext-900", @@ -1131,6 +1326,7 @@ describe("registerSlackInteractionEvents", () => { view: { id: "V901", callback_id: "openclaw:deploy_form", + private_metadata: JSON.stringify({ userId: "U901" }), }, }, }); diff --git a/src/slack/monitor/events/interactions.ts b/src/slack/monitor/events/interactions.ts index cbc4fc9f36e0..40a06ad9f2e9 100644 --- a/src/slack/monitor/events/interactions.ts +++ b/src/slack/monitor/events/interactions.ts @@ -2,6 +2,7 @@ import type { SlackActionMiddlewareArgs } from "@slack/bolt"; import type { Block, KnownBlock } from "@slack/web-api"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; import { parseSlackModalPrivateMetadata } from "../../modal-metadata.js"; +import { authorizeSlackSystemEventSender } from "../auth.js"; import type { SlackMonitorContext } from "../context.js"; import { escapeSlackMrkdwn } from "../mrkdwn.js"; @@ -78,6 +79,7 @@ type SlackModalBody = { type SlackModalEventBase = { callbackId: string; userId: string; + expectedUserId?: string; viewId?: string; sessionRouting: ReturnType; payload: { @@ -366,11 +368,15 @@ function summarizeViewState(values: unknown): ModalInputSummary[] { function resolveModalSessionRouting(params: { ctx: SlackMonitorContext; - privateMetadata: unknown; + metadata: ReturnType; }): { sessionKey: string; channelId?: string; channelType?: string } { - const metadata = parseSlackModalPrivateMetadata(params.privateMetadata); + const metadata = params.metadata; if (metadata.sessionKey) { - return { sessionKey: metadata.sessionKey }; + return { + sessionKey: metadata.sessionKey, + channelId: metadata.channelId, + channelType: metadata.channelType, + }; } if (metadata.channelId) { return { @@ -416,17 +422,19 @@ function resolveSlackModalEventBase(params: { ctx: SlackMonitorContext; body: SlackModalBody; }): SlackModalEventBase { + const metadata = parseSlackModalPrivateMetadata(params.body.view?.private_metadata); const callbackId = params.body.view?.callback_id ?? "unknown"; const userId = params.body.user?.id ?? "unknown"; const viewId = params.body.view?.id; const inputs = summarizeViewState(params.body.view?.state?.values); const sessionRouting = resolveModalSessionRouting({ ctx: params.ctx, - privateMetadata: params.body.view?.private_metadata, + metadata, }); return { callbackId, userId, + expectedUserId: metadata.userId, viewId, sessionRouting, payload: { @@ -449,16 +457,17 @@ function resolveSlackModalEventBase(params: { }; } -function emitSlackModalLifecycleEvent(params: { +async function emitSlackModalLifecycleEvent(params: { ctx: SlackMonitorContext; body: SlackModalBody; interactionType: SlackModalInteractionKind; contextPrefix: "slack:interaction:view" | "slack:interaction:view-closed"; -}): void { - const { callbackId, userId, viewId, sessionRouting, payload } = resolveSlackModalEventBase({ - ctx: params.ctx, - body: params.body, - }); +}): Promise { + const { callbackId, userId, expectedUserId, viewId, sessionRouting, payload } = + resolveSlackModalEventBase({ + ctx: params.ctx, + body: params.body, + }); const isViewClosed = params.interactionType === "view_closed"; const isCleared = params.body.is_cleared === true; const eventPayload = isViewClosed @@ -482,6 +491,27 @@ function emitSlackModalLifecycleEvent(params: { ); } + if (!expectedUserId) { + params.ctx.runtime.log?.( + `slack:interaction drop modal callback=${callbackId} user=${userId} reason=missing-expected-user`, + ); + return; + } + + const auth = await authorizeSlackSystemEventSender({ + ctx: params.ctx, + senderId: userId, + channelId: sessionRouting.channelId, + channelType: sessionRouting.channelType, + expectedSenderId: expectedUserId, + }); + if (!auth.allowed) { + params.ctx.runtime.log?.( + `slack:interaction drop modal callback=${callbackId} user=${userId} reason=${auth.reason ?? "unauthorized"}`, + ); + return; + } + enqueueSystemEvent(`Slack interaction: ${JSON.stringify(eventPayload)}`, { sessionKey: sessionRouting.sessionKey, contextKey: [params.contextPrefix, callbackId, viewId, userId].filter(Boolean).join(":"), @@ -497,7 +527,7 @@ function registerModalLifecycleHandler(params: { }) { params.register(params.matcher, async ({ ack, body }: SlackModalEventHandlerArgs) => { await ack(); - emitSlackModalLifecycleEvent({ + await emitSlackModalLifecycleEvent({ ctx: params.ctx, body: body as SlackModalBody, interactionType: params.interactionType, @@ -557,6 +587,27 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex const channelId = typedBody.channel?.id ?? typedBody.container?.channel_id; const messageTs = typedBody.message?.ts ?? typedBody.container?.message_ts; const threadTs = typedBody.container?.thread_ts; + const auth = await authorizeSlackSystemEventSender({ + ctx, + senderId: userId, + channelId, + }); + if (!auth.allowed) { + ctx.runtime.log?.( + `slack:interaction drop action=${actionId} user=${userId} channel=${channelId ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, + ); + if (respond) { + try { + await respond({ + text: "You are not authorized to use this control.", + response_type: "ephemeral", + }); + } catch { + // Best-effort feedback only. + } + } + return; + } const actionSummary = summarizeAction(typedAction); const eventPayload: InteractionSummary = { interactionType: "block_action", @@ -581,7 +632,7 @@ export function registerSlackInteractionEvents(params: { ctx: SlackMonitorContex // Pass undefined (not "unknown") to allow proper main session fallback const sessionKey = ctx.resolveSlackSystemEventSessionKey({ channelId: channelId, - channelType: undefined, + channelType: auth.channelType, }); // Build context key - only include defined values to avoid "unknown" noise From 95c6b3a912a1a02b70913df5e6d7bdabed32cb24 Mon Sep 17 00:00:00 2001 From: sten moocow Date: Wed, 25 Feb 2026 06:58:58 -0500 Subject: [PATCH 069/160] fix(telegram): recover polling after prolonged network outages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When grammY's runner exceeds maxRetryTime during a network outage, runner.task() resolves cleanly. Previously, the polling loop treated this as an intentional stop and exited permanently — killing Telegram polling for the lifetime of the gateway process. Now the outer loop detects this case and restarts with exponential backoff, so polling recovers once connectivity is restored. Also bumps maxRetryTime from 5 minutes to 60 minutes so the runner itself survives longer outages (e.g. scheduled internet downtime) without needing the outer loop restart path. --- src/telegram/monitor.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/telegram/monitor.ts b/src/telegram/monitor.ts index 8637f488dd64..8c93eee60c95 100644 --- a/src/telegram/monitor.ts +++ b/src/telegram/monitor.ts @@ -45,8 +45,10 @@ export function createTelegramRunnerOptions(cfg: OpenClawConfig): RunOptions Date: Thu, 26 Feb 2026 02:26:46 +0100 Subject: [PATCH 070/160] refactor(gateway): centralize trusted-proxy control-ui bypass policy --- src/gateway/server.auth.test.ts | 170 +++++++++--------- .../ws-connection/connect-policy.test.ts | 52 ++++++ .../server/ws-connection/connect-policy.ts | 16 ++ .../server/ws-connection/message-handler.ts | 27 +-- 4 files changed, 173 insertions(+), 92 deletions(-) diff --git a/src/gateway/server.auth.test.ts b/src/gateway/server.auth.test.ts index 900ef34b6b4c..a0cbf5d9c1ea 100644 --- a/src/gateway/server.auth.test.ts +++ b/src/gateway/server.auth.test.ts @@ -105,6 +105,13 @@ const CONTROL_UI_CLIENT = { mode: GATEWAY_CLIENT_MODES.WEBCHAT, }; +const TRUSTED_PROXY_CONTROL_UI_HEADERS = { + origin: "https://localhost", + "x-forwarded-for": "203.0.113.10", + "x-forwarded-proto": "https", + "x-forwarded-user": "peter@example.com", +} as const; + const NODE_CLIENT = { id: GATEWAY_CLIENT_NAMES.NODE_HOST, version: "1.0.0", @@ -794,89 +801,92 @@ describe("gateway server auth/connect", () => { }); }); - test("allows trusted-proxy control ui operator without device identity", async () => { - await configureTrustedProxyControlUiAuth(); - await withGatewayServer(async ({ port }) => { - const ws = await openWs(port, { - origin: "https://localhost", - "x-forwarded-for": "203.0.113.10", - "x-forwarded-proto": "https", - "x-forwarded-user": "peter@example.com", - }); - const res = await connectReq(ws, { - skipDefaultAuth: true, - role: "operator", - device: null, - client: { ...CONTROL_UI_CLIENT }, - }); - expect(res.ok).toBe(true); - const status = await rpcReq(ws, "status"); - expect(status.ok).toBe(false); - expect(status.error?.message ?? "").toContain("missing scope"); - const health = await rpcReq(ws, "health"); - expect(health.ok).toBe(true); - ws.close(); - }); - }); - - test("rejects trusted-proxy control ui node role without device identity", async () => { - await configureTrustedProxyControlUiAuth(); - await withGatewayServer(async ({ port }) => { - const ws = await openWs(port, { - origin: "https://localhost", - "x-forwarded-for": "203.0.113.10", - "x-forwarded-proto": "https", - "x-forwarded-user": "peter@example.com", - }); - const res = await connectReq(ws, { - skipDefaultAuth: true, - role: "node", - device: null, - client: { ...CONTROL_UI_CLIENT }, - }); - expect(res.ok).toBe(false); - expect(res.error?.message ?? "").toContain("control ui requires device identity"); - expect((res.error?.details as { code?: string } | undefined)?.code).toBe( - ConnectErrorDetailCodes.CONTROL_UI_DEVICE_IDENTITY_REQUIRED, - ); - ws.close(); - }); - }); + const trustedProxyControlUiCases: Array<{ + name: string; + role: "operator" | "node"; + withUnpairedNodeDevice: boolean; + expectedOk: boolean; + expectedErrorSubstring?: string; + expectedErrorCode?: string; + expectStatusChecks: boolean; + }> = [ + { + name: "allows trusted-proxy control ui operator without device identity", + role: "operator", + withUnpairedNodeDevice: false, + expectedOk: true, + expectStatusChecks: true, + }, + { + name: "rejects trusted-proxy control ui node role without device identity", + role: "node", + withUnpairedNodeDevice: false, + expectedOk: false, + expectedErrorSubstring: "control ui requires device identity", + expectedErrorCode: ConnectErrorDetailCodes.CONTROL_UI_DEVICE_IDENTITY_REQUIRED, + expectStatusChecks: false, + }, + { + name: "requires pairing for trusted-proxy control ui node role with unpaired device", + role: "node", + withUnpairedNodeDevice: true, + expectedOk: false, + expectedErrorSubstring: "pairing required", + expectedErrorCode: ConnectErrorDetailCodes.PAIRING_REQUIRED, + expectStatusChecks: false, + }, + ]; - test("requires pairing for trusted-proxy control ui node role with unpaired device", async () => { - await configureTrustedProxyControlUiAuth(); - await withGatewayServer(async ({ port }) => { - const ws = await openWs(port, { - origin: "https://localhost", - "x-forwarded-for": "203.0.113.10", - "x-forwarded-proto": "https", - "x-forwarded-user": "peter@example.com", - }); - const challengeNonce = await readConnectChallengeNonce(ws); - expect(challengeNonce).toBeTruthy(); - const { device } = await createSignedDevice({ - token: null, - role: "node", - scopes: [], - clientId: GATEWAY_CLIENT_NAMES.CONTROL_UI, - clientMode: GATEWAY_CLIENT_MODES.WEBCHAT, - nonce: String(challengeNonce), - }); - const res = await connectReq(ws, { - skipDefaultAuth: true, - role: "node", - scopes: [], - device, - client: { ...CONTROL_UI_CLIENT }, + for (const tc of trustedProxyControlUiCases) { + test(tc.name, async () => { + await configureTrustedProxyControlUiAuth(); + await withGatewayServer(async ({ port }) => { + const ws = await openWs(port, TRUSTED_PROXY_CONTROL_UI_HEADERS); + const scopes = tc.withUnpairedNodeDevice ? [] : undefined; + let device: Awaited>["device"] | null = null; + if (tc.withUnpairedNodeDevice) { + const challengeNonce = await readConnectChallengeNonce(ws); + expect(challengeNonce).toBeTruthy(); + ({ device } = await createSignedDevice({ + token: null, + role: "node", + scopes: [], + clientId: GATEWAY_CLIENT_NAMES.CONTROL_UI, + clientMode: GATEWAY_CLIENT_MODES.WEBCHAT, + nonce: String(challengeNonce), + })); + } + const res = await connectReq(ws, { + skipDefaultAuth: true, + role: tc.role, + scopes, + device, + client: { ...CONTROL_UI_CLIENT }, + }); + expect(res.ok).toBe(tc.expectedOk); + if (!tc.expectedOk) { + if (tc.expectedErrorSubstring) { + expect(res.error?.message ?? "").toContain(tc.expectedErrorSubstring); + } + if (tc.expectedErrorCode) { + expect((res.error?.details as { code?: string } | undefined)?.code).toBe( + tc.expectedErrorCode, + ); + } + ws.close(); + return; + } + if (tc.expectStatusChecks) { + const status = await rpcReq(ws, "status"); + expect(status.ok).toBe(false); + expect(status.error?.message ?? "").toContain("missing scope"); + const health = await rpcReq(ws, "health"); + expect(health.ok).toBe(true); + } + ws.close(); }); - expect(res.ok).toBe(false); - expect(res.error?.message ?? "").toContain("pairing required"); - expect((res.error?.details as { code?: string } | undefined)?.code).toBe( - ConnectErrorDetailCodes.PAIRING_REQUIRED, - ); - ws.close(); }); - }); + } test("allows localhost control ui without device identity when insecure auth is enabled", async () => { testState.gatewayControlUi = { allowInsecureAuth: true }; diff --git a/src/gateway/server/ws-connection/connect-policy.test.ts b/src/gateway/server/ws-connection/connect-policy.test.ts index 320f90537cee..88813663a853 100644 --- a/src/gateway/server/ws-connection/connect-policy.test.ts +++ b/src/gateway/server/ws-connection/connect-policy.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "vitest"; import { evaluateMissingDeviceIdentity, + isTrustedProxyControlUiOperatorAuth, resolveControlUiAuthPolicy, shouldSkipControlUiPairing, } from "./connect-policy.js"; @@ -186,4 +187,55 @@ describe("ws connect policy", () => { expect(shouldSkipControlUiPairing(strict, true, false)).toBe(false); expect(shouldSkipControlUiPairing(strict, false, true)).toBe(true); }); + + test("trusted-proxy control-ui bypass only applies to operator + trusted-proxy auth", () => { + const cases: Array<{ + role: "operator" | "node"; + authMode: string; + authOk: boolean; + authMethod: string | undefined; + expected: boolean; + }> = [ + { + role: "operator", + authMode: "trusted-proxy", + authOk: true, + authMethod: "trusted-proxy", + expected: true, + }, + { + role: "node", + authMode: "trusted-proxy", + authOk: true, + authMethod: "trusted-proxy", + expected: false, + }, + { + role: "operator", + authMode: "token", + authOk: true, + authMethod: "token", + expected: false, + }, + { + role: "operator", + authMode: "trusted-proxy", + authOk: false, + authMethod: "trusted-proxy", + expected: false, + }, + ]; + + for (const tc of cases) { + expect( + isTrustedProxyControlUiOperatorAuth({ + isControlUi: true, + role: tc.role, + authMode: tc.authMode, + authOk: tc.authOk, + authMethod: tc.authMethod, + }), + ).toBe(tc.expected); + } + }); }); diff --git a/src/gateway/server/ws-connection/connect-policy.ts b/src/gateway/server/ws-connection/connect-policy.ts index 70dbea075058..f2467aedc983 100644 --- a/src/gateway/server/ws-connection/connect-policy.ts +++ b/src/gateway/server/ws-connection/connect-policy.ts @@ -43,6 +43,22 @@ export function shouldSkipControlUiPairing( return policy.allowBypass && sharedAuthOk; } +export function isTrustedProxyControlUiOperatorAuth(params: { + isControlUi: boolean; + role: GatewayRole; + authMode: string; + authOk: boolean; + authMethod: string | undefined; +}): boolean { + return ( + params.isControlUi && + params.role === "operator" && + params.authMode === "trusted-proxy" && + params.authOk && + params.authMethod === "trusted-proxy" + ); +} + export type MissingDeviceIdentityDecision = | { kind: "allow" } | { kind: "reject-control-ui-insecure-auth" } diff --git a/src/gateway/server/ws-connection/message-handler.ts b/src/gateway/server/ws-connection/message-handler.ts index 30d288b651d1..261e9f69da2b 100644 --- a/src/gateway/server/ws-connection/message-handler.ts +++ b/src/gateway/server/ws-connection/message-handler.ts @@ -75,6 +75,7 @@ import { resolveConnectAuthDecision, resolveConnectAuthState } from "./auth-cont import { formatGatewayAuthFailureMessage, type AuthProvidedKind } from "./auth-messages.js"; import { evaluateMissingDeviceIdentity, + isTrustedProxyControlUiOperatorAuth, resolveControlUiAuthPolicy, shouldSkipControlUiPairing, } from "./connect-policy.js"; @@ -489,12 +490,13 @@ export function attachGatewayWsMessageHandler(params: { if (!device) { clearUnboundScopes(); } - const trustedProxyAuthOk = - isControlUi && - role === "operator" && - resolvedAuth.mode === "trusted-proxy" && - authOk && - authMethod === "trusted-proxy"; + const trustedProxyAuthOk = isTrustedProxyControlUiOperatorAuth({ + isControlUi, + role, + authMode: resolvedAuth.mode, + authOk, + authMethod, + }); const decision = evaluateMissingDeviceIdentity({ hasDeviceIdentity: Boolean(device), role, @@ -628,12 +630,13 @@ export function attachGatewayWsMessageHandler(params: { return; } - const trustedProxyAuthOk = - isControlUi && - role === "operator" && - resolvedAuth.mode === "trusted-proxy" && - authOk && - authMethod === "trusted-proxy"; + const trustedProxyAuthOk = isTrustedProxyControlUiOperatorAuth({ + isControlUi, + role, + authMode: resolvedAuth.mode, + authOk, + authMethod, + }); const skipPairing = shouldSkipControlUiPairing( controlUiAuthPolicy, sharedAuthOk, From acbb93be489d4a98518c151be823fc582e0c370a Mon Sep 17 00:00:00 2001 From: Ramez Date: Wed, 25 Feb 2026 19:35:40 -0600 Subject: [PATCH 071/160] fix(agents): comprehensive quota fallback fixes - session overrides + surgical cooldown logic (#23816) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: e6f2b4742b82b9fe44a7e103170c2f96565b09c5 Co-authored-by: ramezgaberiel <844893+ramezgaberiel@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + src/agents/model-fallback.probe.test.ts | 30 ++- src/agents/model-fallback.test.ts | 310 +++++++++++++++++++++++- src/agents/model-fallback.ts | 135 ++++++++--- 4 files changed, 422 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0df4711abafc..b4b4a5b40646 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Docs: https://docs.openclaw.ai - Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before `setFiles`, with regression coverage for strict missing-path handling. - Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3. - Agents/Model fallback: keep explicit text + image fallback chains reachable even when `agents.defaults.models` allowlists are present, prefer explicit run `agentId` over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify `model_cooldown` / `cooling down` errors as `rate_limit` so failover continues. (#11972, #24137, #17231) +- Agents/Model fallback: keep same-provider fallback chains active when session model differs from configured primary, infer cooldown reason from provider profile state (instead of `disabledReason` only), keep no-profile fallback providers eligible (env/models.json paths), and only relax same-provider cooldown fallback attempts for `rate_limit`. (#23816) thanks @ramezgaberiel. - Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. - Agents/Model fallback: continue fallback traversal on unrecognized errors when candidates remain, while still throwing the original unknown error on the last candidate. (#26106) Thanks @Sid-Qin. - Telegram/Markdown spoilers: keep valid `||spoiler||` pairs while leaving unmatched trailing `||` delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin. diff --git a/src/agents/model-fallback.probe.test.ts b/src/agents/model-fallback.probe.test.ts index 0c222ec21155..3e36366c4adc 100644 --- a/src/agents/model-fallback.probe.test.ts +++ b/src/agents/model-fallback.probe.test.ts @@ -163,7 +163,7 @@ describe("runWithModelFallback – probe logic", () => { expectPrimaryProbeSuccess(result, run, "recovered"); }); - it("does NOT probe non-primary candidates during cooldown", async () => { + it("attempts non-primary fallbacks during rate-limit cooldown after primary probe failure", async () => { const cfg = makeCfg({ agents: { defaults: { @@ -182,25 +182,23 @@ describe("runWithModelFallback – probe logic", () => { const almostExpired = NOW + 30 * 1000; // 30s remaining mockedGetSoonestCooldownExpiry.mockReturnValue(almostExpired); - // Primary probe fails with 429 + // Primary probe fails with 429; fallback should still be attempted for rate_limit cooldowns. const run = vi .fn() .mockRejectedValueOnce(Object.assign(new Error("rate limited"), { status: 429 })) - .mockResolvedValue("should-not-reach"); + .mockResolvedValue("fallback-ok"); - try { - await runWithModelFallback({ - cfg, - provider: "openai", - model: "gpt-4.1-mini", - run, - }); - expect.unreachable("should have thrown since all candidates exhausted"); - } catch { - // Primary was probed (i === 0 + within margin), non-primary were skipped - expect(run).toHaveBeenCalledTimes(1); // only primary was actually called - expect(run).toHaveBeenCalledWith("openai", "gpt-4.1-mini"); - } + const result = await runWithModelFallback({ + cfg, + provider: "openai", + model: "gpt-4.1-mini", + run, + }); + + expect(result.result).toBe("fallback-ok"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(1, "openai", "gpt-4.1-mini"); + expect(run).toHaveBeenNthCalledWith(2, "anthropic", "claude-haiku-3-5"); }); it("throttles probe when called within 30s interval", async () => { diff --git a/src/agents/model-fallback.test.ts b/src/agents/model-fallback.test.ts index 16592cdb4560..cd0217faafcb 100644 --- a/src/agents/model-fallback.test.ts +++ b/src/agents/model-fallback.test.ts @@ -143,10 +143,22 @@ async function expectSkippedUnavailableProvider(params: { }) { const provider = `${params.providerPrefix}-${crypto.randomUUID()}`; const cfg = makeProviderFallbackCfg(provider); - const store = makeSingleProviderStore({ + const primaryStore = makeSingleProviderStore({ provider, usageStat: params.usageStat, }); + // Include fallback provider profile so the fallback is attempted (not skipped as no-profile). + const store: AuthProfileStore = { + ...primaryStore, + profiles: { + ...primaryStore.profiles, + "fallback:default": { + type: "api_key", + provider: "fallback", + key: "test-key", + }, + }, + }; const run = createFallbackOnlyRun(); const result = await runWithStoredAuth({ @@ -436,11 +448,11 @@ describe("runWithModelFallback", () => { run, }); - // Override model failed with model_not_found → falls back to configured primary. + // Override model failed with model_not_found → tries fallbacks first (same provider). expect(result.result).toBe("ok"); expect(run).toHaveBeenCalledTimes(2); - expect(run.mock.calls[1]?.[0]).toBe("openai"); - expect(run.mock.calls[1]?.[1]).toBe("gpt-4.1-mini"); + expect(run.mock.calls[1]?.[0]).toBe("anthropic"); + expect(run.mock.calls[1]?.[1]).toBe("claude-haiku-3-5"); }); it("skips providers when all profiles are in cooldown", async () => { @@ -794,6 +806,296 @@ describe("runWithModelFallback", () => { expect(result.provider).toBe("openai"); expect(result.model).toBe("gpt-4.1-mini"); }); + + // Tests for Bug A fix: Model fallback with session overrides + describe("fallback behavior with session model overrides", () => { + it("allows fallbacks when session model differs from config within same provider", async () => { + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["anthropic/claude-sonnet-4-5", "google/gemini-2.5-flash"], + }, + }, + }, + }); + + const run = vi + .fn() + .mockRejectedValueOnce(new Error("Rate limit exceeded")) // Session model fails + .mockResolvedValueOnce("fallback success"); // First fallback succeeds + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-sonnet-4-20250514", // Different from config primary + run, + }); + + expect(result.result).toBe("fallback success"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(1, "anthropic", "claude-sonnet-4-20250514"); + expect(run).toHaveBeenNthCalledWith(2, "anthropic", "claude-sonnet-4-5"); // Fallback tried + }); + + it("allows fallbacks with model version differences within same provider", async () => { + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi + .fn() + .mockRejectedValueOnce(new Error("Weekly quota exceeded")) + .mockResolvedValueOnce("groq success"); + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-5", // Version difference from config + run, + }); + + expect(result.result).toBe("groq success"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(2, "groq", "llama-3.3-70b-versatile"); + }); + + it("still skips fallbacks when using different provider than config", async () => { + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: [], // Empty fallbacks to match working pattern + }, + }, + }, + }); + + const run = vi + .fn() + .mockRejectedValueOnce(new Error('No credentials found for profile "openai:default".')) + .mockResolvedValueOnce("config primary worked"); + + const result = await runWithModelFallback({ + cfg, + provider: "openai", // Different provider + model: "gpt-4.1-mini", + run, + }); + + // Cross-provider requests should skip configured fallbacks but still try configured primary + expect(result.result).toBe("config primary worked"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(1, "openai", "gpt-4.1-mini"); // Original request + expect(run).toHaveBeenNthCalledWith(2, "anthropic", "claude-opus-4-6"); // Config primary as final fallback + }); + + it("uses fallbacks when session model exactly matches config primary", async () => { + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi + .fn() + .mockRejectedValueOnce(new Error("Quota exceeded")) + .mockResolvedValueOnce("fallback worked"); + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-6", // Exact match + run, + }); + + expect(result.result).toBe("fallback worked"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(2, "groq", "llama-3.3-70b-versatile"); + }); + }); + + // Tests for Bug B fix: Rate limit vs auth/billing cooldown distinction + describe("fallback behavior with provider cooldowns", () => { + async function makeAuthStoreWithCooldown( + provider: string, + reason: "rate_limit" | "auth" | "billing", + ): Promise<{ store: AuthProfileStore; dir: string }> { + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-")); + const now = Date.now(); + const store: AuthProfileStore = { + version: AUTH_STORE_VERSION, + profiles: { + [`${provider}:default`]: { type: "api_key", provider, key: "test-key" }, + }, + usageStats: { + [`${provider}:default`]: + reason === "rate_limit" + ? { + // Real rate-limit cooldowns are tracked through cooldownUntil + // and failureCounts, not disabledReason. + cooldownUntil: now + 300000, + failureCounts: { rate_limit: 1 }, + } + : { + // Auth/billing issues use disabledUntil + disabledUntil: now + 300000, + disabledReason: reason, + }, + }, + }; + saveAuthProfileStore(store, tmpDir); + return { store, dir: tmpDir }; + } + + it("attempts same-provider fallbacks during rate limit cooldown", async () => { + const { dir } = await makeAuthStoreWithCooldown("anthropic", "rate_limit"); + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["anthropic/claude-sonnet-4-5", "groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi.fn().mockResolvedValueOnce("sonnet success"); // Fallback succeeds + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-6", + run, + agentDir: dir, + }); + + expect(result.result).toBe("sonnet success"); + expect(run).toHaveBeenCalledTimes(1); // Primary skipped, fallback attempted + expect(run).toHaveBeenNthCalledWith(1, "anthropic", "claude-sonnet-4-5"); + }); + + it("skips same-provider models on auth cooldown but still tries no-profile fallback providers", async () => { + const { dir } = await makeAuthStoreWithCooldown("anthropic", "auth"); + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["anthropic/claude-sonnet-4-5", "groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi.fn().mockResolvedValueOnce("groq success"); + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-6", + run, + agentDir: dir, + }); + + expect(result.result).toBe("groq success"); + expect(run).toHaveBeenCalledTimes(1); + expect(run).toHaveBeenNthCalledWith(1, "groq", "llama-3.3-70b-versatile"); + }); + + it("skips same-provider models on billing cooldown but still tries no-profile fallback providers", async () => { + const { dir } = await makeAuthStoreWithCooldown("anthropic", "billing"); + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["anthropic/claude-sonnet-4-5", "groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi.fn().mockResolvedValueOnce("groq success"); + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-6", + run, + agentDir: dir, + }); + + expect(result.result).toBe("groq success"); + expect(run).toHaveBeenCalledTimes(1); + expect(run).toHaveBeenNthCalledWith(1, "groq", "llama-3.3-70b-versatile"); + }); + + it("tries cross-provider fallbacks when same provider has rate limit", async () => { + // Anthropic in rate limit cooldown, Groq available + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-")); + const store: AuthProfileStore = { + version: AUTH_STORE_VERSION, + profiles: { + "anthropic:default": { type: "api_key", provider: "anthropic", key: "test-key" }, + "groq:default": { type: "api_key", provider: "groq", key: "test-key" }, + }, + usageStats: { + "anthropic:default": { + // Rate-limit reason is inferred from failureCounts for cooldown windows. + cooldownUntil: Date.now() + 300000, + failureCounts: { rate_limit: 2 }, + }, + // Groq not in cooldown + }, + }; + saveAuthProfileStore(store, tmpDir); + + const cfg = makeCfg({ + agents: { + defaults: { + model: { + primary: "anthropic/claude-opus-4-6", + fallbacks: ["anthropic/claude-sonnet-4-5", "groq/llama-3.3-70b-versatile"], + }, + }, + }, + }); + + const run = vi + .fn() + .mockRejectedValueOnce(new Error("Still rate limited")) // Sonnet still fails + .mockResolvedValueOnce("groq success"); // Groq works + + const result = await runWithModelFallback({ + cfg, + provider: "anthropic", + model: "claude-opus-4-6", + run, + agentDir: tmpDir, + }); + + expect(result.result).toBe("groq success"); + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenNthCalledWith(1, "anthropic", "claude-sonnet-4-5"); // Rate limit allows attempt + expect(run).toHaveBeenNthCalledWith(2, "groq", "llama-3.3-70b-versatile"); // Cross-provider works + }); + }); }); describe("runWithImageModelFallback", () => { diff --git a/src/agents/model-fallback.ts b/src/agents/model-fallback.ts index e59d9e9357c7..da03d88d8472 100644 --- a/src/agents/model-fallback.ts +++ b/src/agents/model-fallback.ts @@ -224,21 +224,21 @@ function resolveFallbackCandidates(params: { const configuredFallbacks = resolveAgentModelFallbackValues( params.cfg?.agents?.defaults?.model, ); - if (sameModelCandidate(normalizedPrimary, configuredPrimary)) { - return configuredFallbacks; - } - // Preserve resilience after failover: when current model is one of the - // configured fallback refs, keep traversing the configured fallback chain. - const isConfiguredFallback = configuredFallbacks.some((raw) => { - const resolved = resolveModelRefFromString({ - raw: String(raw ?? ""), - defaultProvider, - aliasIndex, + // When user runs a different provider than config, only use configured fallbacks + // if the current model is already in that chain (e.g. session on first fallback). + if (normalizedPrimary.provider !== configuredPrimary.provider) { + const isConfiguredFallback = configuredFallbacks.some((raw) => { + const resolved = resolveModelRefFromString({ + raw: String(raw ?? ""), + defaultProvider, + aliasIndex, + }); + return resolved ? sameModelCandidate(resolved.ref, normalizedPrimary) : false; }); - return resolved ? sameModelCandidate(resolved.ref, normalizedPrimary) : false; - }); - // Keep legacy override behavior for ad-hoc models outside configured chain. - return isConfiguredFallback ? configuredFallbacks : []; + return isConfiguredFallback ? configuredFallbacks : []; + } + // Same provider: always use full fallback chain (model version differences within provider). + return configuredFallbacks; })(); for (const raw of modelFallbacks) { @@ -306,6 +306,76 @@ export const _probeThrottleInternals = { resolveProbeThrottleKey, } as const; +type CooldownDecision = + | { + type: "skip"; + reason: FailoverReason; + error: string; + } + | { + type: "attempt"; + reason: FailoverReason; + markProbe: boolean; + }; + +function resolveCooldownDecision(params: { + candidate: ModelCandidate; + isPrimary: boolean; + requestedModel: boolean; + hasFallbackCandidates: boolean; + now: number; + probeThrottleKey: string; + authStore: ReturnType; + profileIds: string[]; +}): CooldownDecision { + const shouldProbe = shouldProbePrimaryDuringCooldown({ + isPrimary: params.isPrimary, + hasFallbackCandidates: params.hasFallbackCandidates, + now: params.now, + throttleKey: params.probeThrottleKey, + authStore: params.authStore, + profileIds: params.profileIds, + }); + + const inferredReason = + resolveProfilesUnavailableReason({ + store: params.authStore, + profileIds: params.profileIds, + now: params.now, + }) ?? "rate_limit"; + const isPersistentIssue = + inferredReason === "auth" || + inferredReason === "auth_permanent" || + inferredReason === "billing"; + if (isPersistentIssue) { + return { + type: "skip", + reason: inferredReason, + error: `Provider ${params.candidate.provider} has ${inferredReason} issue (skipping all models)`, + }; + } + + // For primary: try when requested model or when probe allows. + // For same-provider fallbacks: only relax cooldown on rate_limit, which + // is commonly model-scoped and can recover on a sibling model. + const shouldAttemptDespiteCooldown = + (params.isPrimary && (!params.requestedModel || shouldProbe)) || + (!params.isPrimary && inferredReason === "rate_limit"); + if (!shouldAttemptDespiteCooldown) { + return { + type: "skip", + reason: inferredReason, + error: `Provider ${params.candidate.provider} is in cooldown (all profiles unavailable)`, + }; + } + + return { + type: "attempt", + reason: inferredReason, + markProbe: params.isPrimary && shouldProbe, + }; +} + export async function runWithModelFallback(params: { cfg: OpenClawConfig | undefined; provider: string; @@ -342,41 +412,38 @@ export async function runWithModelFallback(params: { if (profileIds.length > 0 && !isAnyProfileAvailable) { // All profiles for this provider are in cooldown. - // For the primary model (i === 0), probe it if the soonest cooldown - // expiry is close or already past. This avoids staying on a fallback - // model long after the real rate-limit window clears. + const isPrimary = i === 0; + const requestedModel = + params.provider === candidate.provider && params.model === candidate.model; const now = Date.now(); const probeThrottleKey = resolveProbeThrottleKey(candidate.provider, params.agentDir); - const shouldProbe = shouldProbePrimaryDuringCooldown({ - isPrimary: i === 0, + const decision = resolveCooldownDecision({ + candidate, + isPrimary, + requestedModel, hasFallbackCandidates, now, - throttleKey: probeThrottleKey, + probeThrottleKey, authStore, profileIds, }); - if (!shouldProbe) { - const inferredReason = - resolveProfilesUnavailableReason({ - store: authStore, - profileIds, - now, - }) ?? "rate_limit"; - // Skip without attempting + + if (decision.type === "skip") { attempts.push({ provider: candidate.provider, model: candidate.model, - error: `Provider ${candidate.provider} is in cooldown (all profiles unavailable)`, - reason: inferredReason, + error: decision.error, + reason: decision.reason, }); continue; } - // Primary model probe: attempt it despite cooldown to detect recovery. - // If it fails, the error is caught below and we fall through to the - // next candidate as usual. - lastProbeAttempt.set(probeThrottleKey, now); + + if (decision.markProbe) { + lastProbeAttempt.set(probeThrottleKey, now); + } } } + try { const result = await params.run(candidate.provider, candidate.model); return { From 1f004e6640006fb7ce53f03f116e1a8b89598416 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:46:43 +0100 Subject: [PATCH 072/160] refactor(tmp): simplify trusted tmp dir state checks --- src/infra/tmp-openclaw-dir.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/infra/tmp-openclaw-dir.ts b/src/infra/tmp-openclaw-dir.ts index 975e25b8a1a9..870720b55f8d 100644 --- a/src/infra/tmp-openclaw-dir.ts +++ b/src/infra/tmp-openclaw-dir.ts @@ -3,6 +3,7 @@ import os from "node:os"; import path from "node:path"; export const POSIX_OPENCLAW_TMP_DIR = "/tmp/openclaw"; +const TMP_DIR_ACCESS_MODE = fs.constants.W_OK | fs.constants.X_OK; type ResolvePreferredOpenClawTmpDirOptions = { accessSync?: (path: string, mode?: number) => void; @@ -66,7 +67,7 @@ export function resolvePreferredOpenClawTmpDir( return path.join(base, suffix); }; - const isTrustedPreferredDir = (st: { + const isTrustedTmpDir = (st: { isDirectory(): boolean; isSymbolicLink(): boolean; mode?: number; @@ -75,18 +76,13 @@ export function resolvePreferredOpenClawTmpDir( return st.isDirectory() && !st.isSymbolicLink() && isSecureDirForUser(st); }; - const resolveDirState = ( - candidatePath: string, - requireWritableAccess: boolean, - ): "available" | "missing" | "invalid" => { + const resolveDirState = (candidatePath: string): "available" | "missing" | "invalid" => { try { const candidate = lstatSync(candidatePath); - if (!isTrustedPreferredDir(candidate)) { + if (!isTrustedTmpDir(candidate)) { return "invalid"; } - if (requireWritableAccess) { - accessSync(candidatePath, fs.constants.W_OK | fs.constants.X_OK); - } + accessSync(candidatePath, TMP_DIR_ACCESS_MODE); return "available"; } catch (err) { if (isNodeErrorWithCode(err, "ENOENT")) { @@ -98,7 +94,7 @@ export function resolvePreferredOpenClawTmpDir( const ensureTrustedFallbackDir = (): string => { const fallbackPath = fallback(); - const state = resolveDirState(fallbackPath, true); + const state = resolveDirState(fallbackPath); if (state === "available") { return fallbackPath; } @@ -110,13 +106,13 @@ export function resolvePreferredOpenClawTmpDir( } catch { throw new Error(`Unable to create fallback OpenClaw temp dir: ${fallbackPath}`); } - if (resolveDirState(fallbackPath, true) !== "available") { + if (resolveDirState(fallbackPath) !== "available") { throw new Error(`Unsafe fallback OpenClaw temp dir: ${fallbackPath}`); } return fallbackPath; }; - const existingPreferredState = resolveDirState(POSIX_OPENCLAW_TMP_DIR, true); + const existingPreferredState = resolveDirState(POSIX_OPENCLAW_TMP_DIR); if (existingPreferredState === "available") { return POSIX_OPENCLAW_TMP_DIR; } @@ -125,10 +121,10 @@ export function resolvePreferredOpenClawTmpDir( } try { - accessSync("/tmp", fs.constants.W_OK | fs.constants.X_OK); + accessSync("/tmp", TMP_DIR_ACCESS_MODE); // Create with a safe default; subsequent callers expect it exists. mkdirSync(POSIX_OPENCLAW_TMP_DIR, { recursive: true, mode: 0o700 }); - if (resolveDirState(POSIX_OPENCLAW_TMP_DIR, true) !== "available") { + if (resolveDirState(POSIX_OPENCLAW_TMP_DIR) !== "available") { return ensureTrustedFallbackDir(); } return POSIX_OPENCLAW_TMP_DIR; From 347f7b9550064f5f5b33c6e07f64e85b9657b6f1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:49:41 +0100 Subject: [PATCH 073/160] fix(msteams): bind file consent invokes to conversation --- CHANGELOG.md | 1 + .../src/monitor-handler.file-consent.test.ts | 220 ++++++++++++++++++ extensions/msteams/src/monitor-handler.ts | 24 +- 3 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 extensions/msteams/src/monitor-handler.file-consent.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b4b4a5b40646..b8e6df03533c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ Docs: https://docs.openclaw.ai - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. - Security/Microsoft Teams: isolate group allowlist and command authorization from DM pairing-store entries to prevent cross-context authorization bleed. (#26111) Thanks @bmendonca3. +- Security/Microsoft Teams file consent: bind `fileConsent/invoke` upload acceptance/decline to the originating conversation before consuming pending uploads, preventing cross-conversation pending-file upload or cancellation via leaked `uploadId` values; includes regression coverage for match/mismatch invoke handling. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before `setFiles`, with regression coverage for strict missing-path handling. - Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3. - Agents/Model fallback: keep explicit text + image fallback chains reachable even when `agents.defaults.models` allowlists are present, prefer explicit run `agentId` over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify `model_cooldown` / `cooling down` errors as `rate_limit` so failover continues. (#11972, #24137, #17231) diff --git a/extensions/msteams/src/monitor-handler.file-consent.test.ts b/extensions/msteams/src/monitor-handler.file-consent.test.ts new file mode 100644 index 000000000000..804ce58107c8 --- /dev/null +++ b/extensions/msteams/src/monitor-handler.file-consent.test.ts @@ -0,0 +1,220 @@ +import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { MSTeamsConversationStore } from "./conversation-store.js"; +import type { MSTeamsAdapter } from "./messenger.js"; +import { + type MSTeamsActivityHandler, + type MSTeamsMessageHandlerDeps, + registerMSTeamsHandlers, +} from "./monitor-handler.js"; +import { clearPendingUploads, getPendingUpload, storePendingUpload } from "./pending-uploads.js"; +import type { MSTeamsPollStore } from "./polls.js"; +import { setMSTeamsRuntime } from "./runtime.js"; +import type { MSTeamsTurnContext } from "./sdk-types.js"; + +const fileConsentMockState = vi.hoisted(() => ({ + uploadToConsentUrl: vi.fn(), +})); + +vi.mock("./file-consent.js", async () => { + const actual = await vi.importActual("./file-consent.js"); + return { + ...actual, + uploadToConsentUrl: fileConsentMockState.uploadToConsentUrl, + }; +}); + +const runtimeStub: PluginRuntime = { + logging: { + shouldLogVerbose: () => false, + }, + channel: { + debounce: { + resolveInboundDebounceMs: () => 0, + createInboundDebouncer: () => ({ + enqueue: async () => {}, + }), + }, + }, +} as unknown as PluginRuntime; + +function createDeps(): MSTeamsMessageHandlerDeps { + const adapter: MSTeamsAdapter = { + continueConversation: async () => {}, + process: async () => {}, + }; + const conversationStore: MSTeamsConversationStore = { + upsert: async () => {}, + get: async () => null, + list: async () => [], + remove: async () => false, + findByUserId: async () => null, + }; + const pollStore: MSTeamsPollStore = { + createPoll: async () => {}, + getPoll: async () => null, + recordVote: async () => null, + }; + return { + cfg: {} as OpenClawConfig, + runtime: { + error: vi.fn(), + } as unknown as RuntimeEnv, + appId: "test-app-id", + adapter, + tokenProvider: { + getAccessToken: async () => "token", + }, + textLimit: 4000, + mediaMaxBytes: 8 * 1024 * 1024, + conversationStore, + pollStore, + log: { + info: vi.fn(), + error: vi.fn(), + debug: vi.fn(), + }, + }; +} + +function createActivityHandler(): MSTeamsActivityHandler { + let handler: MSTeamsActivityHandler; + handler = { + onMessage: () => handler, + onMembersAdded: () => handler, + run: async () => {}, + }; + return handler; +} + +function createInvokeContext(params: { + conversationId: string; + uploadId: string; + action: "accept" | "decline"; +}): { context: MSTeamsTurnContext; sendActivity: ReturnType } { + const sendActivity = vi.fn(async () => ({ id: "activity-id" })); + const uploadInfo = + params.action === "accept" + ? { + name: "secret.txt", + uploadUrl: "https://upload.example.com/put", + contentUrl: "https://content.example.com/file", + uniqueId: "unique-id", + fileType: "txt", + } + : undefined; + return { + context: { + activity: { + type: "invoke", + name: "fileConsent/invoke", + conversation: { id: params.conversationId }, + value: { + type: "fileUpload", + action: params.action, + uploadInfo, + context: { uploadId: params.uploadId }, + }, + }, + sendActivity, + sendActivities: async () => [], + } as unknown as MSTeamsTurnContext, + sendActivity, + }; +} + +describe("msteams file consent invoke authz", () => { + beforeEach(() => { + setMSTeamsRuntime(runtimeStub); + clearPendingUploads(); + fileConsentMockState.uploadToConsentUrl.mockReset(); + fileConsentMockState.uploadToConsentUrl.mockResolvedValue(undefined); + }); + + it("uploads when invoke conversation matches pending upload conversation", async () => { + const uploadId = storePendingUpload({ + buffer: Buffer.from("TOP_SECRET_VICTIM_FILE\n"), + filename: "secret.txt", + contentType: "text/plain", + conversationId: "19:victim@thread.v2", + }); + const deps = createDeps(); + const handler = registerMSTeamsHandlers(createActivityHandler(), deps); + const { context, sendActivity } = createInvokeContext({ + conversationId: "19:victim@thread.v2;messageid=abc123", + uploadId, + action: "accept", + }); + + await handler.run?.(context); + + expect(fileConsentMockState.uploadToConsentUrl).toHaveBeenCalledTimes(1); + expect(fileConsentMockState.uploadToConsentUrl).toHaveBeenCalledWith( + expect.objectContaining({ + url: "https://upload.example.com/put", + }), + ); + expect(getPendingUpload(uploadId)).toBeUndefined(); + expect(sendActivity).toHaveBeenCalledWith( + expect.objectContaining({ + type: "invokeResponse", + }), + ); + }); + + it("rejects cross-conversation accept invoke and keeps pending upload", async () => { + const uploadId = storePendingUpload({ + buffer: Buffer.from("TOP_SECRET_VICTIM_FILE\n"), + filename: "secret.txt", + contentType: "text/plain", + conversationId: "19:victim@thread.v2", + }); + const deps = createDeps(); + const handler = registerMSTeamsHandlers(createActivityHandler(), deps); + const { context, sendActivity } = createInvokeContext({ + conversationId: "19:attacker@thread.v2", + uploadId, + action: "accept", + }); + + await handler.run?.(context); + + expect(fileConsentMockState.uploadToConsentUrl).not.toHaveBeenCalled(); + expect(getPendingUpload(uploadId)).toBeDefined(); + expect(sendActivity).toHaveBeenCalledWith( + "The file upload request has expired. Please try sending the file again.", + ); + expect(sendActivity).toHaveBeenCalledWith( + expect.objectContaining({ + type: "invokeResponse", + }), + ); + }); + + it("ignores cross-conversation decline invoke and keeps pending upload", async () => { + const uploadId = storePendingUpload({ + buffer: Buffer.from("TOP_SECRET_VICTIM_FILE\n"), + filename: "secret.txt", + contentType: "text/plain", + conversationId: "19:victim@thread.v2", + }); + const deps = createDeps(); + const handler = registerMSTeamsHandlers(createActivityHandler(), deps); + const { context, sendActivity } = createInvokeContext({ + conversationId: "19:attacker@thread.v2", + uploadId, + action: "decline", + }); + + await handler.run?.(context); + + expect(fileConsentMockState.uploadToConsentUrl).not.toHaveBeenCalled(); + expect(getPendingUpload(uploadId)).toBeDefined(); + expect(sendActivity).toHaveBeenCalledTimes(1); + expect(sendActivity).toHaveBeenCalledWith( + expect.objectContaining({ + type: "invokeResponse", + }), + ); + }); +}); diff --git a/extensions/msteams/src/monitor-handler.ts b/extensions/msteams/src/monitor-handler.ts index d4b848fde5ae..086b82d496a7 100644 --- a/extensions/msteams/src/monitor-handler.ts +++ b/extensions/msteams/src/monitor-handler.ts @@ -1,6 +1,7 @@ import type { OpenClawConfig, RuntimeEnv } from "openclaw/plugin-sdk"; import type { MSTeamsConversationStore } from "./conversation-store.js"; import { buildFileInfoCard, parseFileConsentInvoke, uploadToConsentUrl } from "./file-consent.js"; +import { normalizeMSTeamsConversationId } from "./inbound.js"; import type { MSTeamsAdapter } from "./messenger.js"; import { createMSTeamsMessageHandler } from "./monitor-handler/message-handler.js"; import type { MSTeamsMonitorLogger } from "./monitor-types.js"; @@ -42,6 +43,8 @@ async function handleFileConsentInvoke( context: MSTeamsTurnContext, log: MSTeamsMonitorLogger, ): Promise { + const expiredUploadMessage = + "The file upload request has expired. Please try sending the file again."; const activity = context.activity; if (activity.type !== "invoke" || activity.name !== "fileConsent/invoke") { return false; @@ -57,9 +60,24 @@ async function handleFileConsentInvoke( typeof consentResponse.context?.uploadId === "string" ? consentResponse.context.uploadId : undefined; + const pendingFile = getPendingUpload(uploadId); + if (pendingFile) { + const pendingConversationId = normalizeMSTeamsConversationId(pendingFile.conversationId); + const invokeConversationId = normalizeMSTeamsConversationId(activity.conversation?.id ?? ""); + if (!invokeConversationId || pendingConversationId !== invokeConversationId) { + log.info("file consent conversation mismatch", { + uploadId, + expectedConversationId: pendingConversationId, + receivedConversationId: invokeConversationId || undefined, + }); + if (consentResponse.action === "accept") { + await context.sendActivity(expiredUploadMessage); + } + return true; + } + } if (consentResponse.action === "accept" && consentResponse.uploadInfo) { - const pendingFile = getPendingUpload(uploadId); if (pendingFile) { log.debug?.("user accepted file consent, uploading", { uploadId, @@ -101,9 +119,7 @@ async function handleFileConsentInvoke( } } else { log.debug?.("pending file not found for consent", { uploadId }); - await context.sendActivity( - "The file upload request has expired. Please try sending the file again.", - ); + await context.sendActivity(expiredUploadMessage); } } else { // User declined From b8bb8ab3ca421ed1405833c8c3e134d3ab2236e8 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:59:10 +0100 Subject: [PATCH 074/160] docs: clarify personal-by-default onboarding security notice --- CHANGELOG.md | 1 + docs/start/onboarding.md | 5 +++++ src/wizard/onboarding.ts | 11 +++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8e6df03533c..f67fa020bcfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Docs: https://docs.openclaw.ai - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. +- Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. ### Fixes diff --git a/docs/start/onboarding.md b/docs/start/onboarding.md index e9f2edeb363d..679ab059f459 100644 --- a/docs/start/onboarding.md +++ b/docs/start/onboarding.md @@ -29,6 +29,11 @@ For a general overview of onboarding paths, see [Onboarding Overview](/start/onb + +Security trust model: + +- By default, OpenClaw is a personal agent: one trusted operator boundary. +- Shared/multi-user setups require lock-down (split trust boundaries, keep tool access minimal, and follow [Security](/gateway/security)). diff --git a/src/wizard/onboarding.ts b/src/wizard/onboarding.ts index df826b62ccf9..301375fbb596 100644 --- a/src/wizard/onboarding.ts +++ b/src/wizard/onboarding.ts @@ -31,15 +31,21 @@ async function requireRiskAcknowledgement(params: { "Security warning — please read.", "", "OpenClaw is a hobby project and still in beta. Expect sharp edges.", + "By default, OpenClaw is a personal agent: one trusted operator boundary.", "This bot can read files and run actions if tools are enabled.", "A bad prompt can trick it into doing unsafe things.", "", - "If you’re not comfortable with basic security and access control, don’t run OpenClaw.", + "OpenClaw is not a hostile multi-tenant boundary by default.", + "If multiple users can message one tool-enabled agent, they share that delegated tool authority.", + "", + "If you’re not comfortable with security hardening and access control, don’t run OpenClaw.", "Ask someone experienced to help before enabling tools or exposing it to the internet.", "", "Recommended baseline:", "- Pairing/allowlists + mention gating.", + "- Multi-user/shared inbox: split trust boundaries (separate gateway/credentials, ideally separate OS users/hosts).", "- Sandbox + least-privilege tools.", + "- Shared inboxes: isolate DM sessions (`session.dmScope: per-channel-peer`) and keep tool access minimal.", "- Keep secrets out of the agent’s reachable filesystem.", "- Use the strongest available model for any bot with tools or untrusted inboxes.", "", @@ -53,7 +59,8 @@ async function requireRiskAcknowledgement(params: { ); const ok = await params.prompter.confirm({ - message: "I understand this is powerful and inherently risky. Continue?", + message: + "I understand this is personal-by-default and shared/multi-user use requires lock-down. Continue?", initialValue: false, }); if (!ok) { From 00fc1f56f1a0dc675efd02414ea1315fab686668 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 20:44:43 +0530 Subject: [PATCH 075/160] perf(android): remove startup bc provider registration --- .../app/src/main/java/ai/openclaw/android/NodeApp.kt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeApp.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeApp.kt index 2be9ee71a2c7..ab5e159cf476 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeApp.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeApp.kt @@ -2,23 +2,12 @@ package ai.openclaw.android import android.app.Application import android.os.StrictMode -import android.util.Log -import java.security.Security class NodeApp : Application() { val runtime: NodeRuntime by lazy { NodeRuntime(this) } override fun onCreate() { super.onCreate() - // Register Bouncy Castle as highest-priority provider for Ed25519 support - try { - val bcProvider = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider") - .getDeclaredConstructor().newInstance() as java.security.Provider - Security.removeProvider("BC") - Security.insertProviderAt(bcProvider, 1) - } catch (it: Throwable) { - Log.e("NodeApp", "Failed to register Bouncy Castle provider", it) - } if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( StrictMode.ThreadPolicy.Builder() From 8d681997930c547aef407447f152e83d3b0a6772 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 20:44:46 +0530 Subject: [PATCH 076/160] perf(android): cache device identity and speed hex encoding --- .../android/gateway/DeviceIdentityStore.kt | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceIdentityStore.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceIdentityStore.kt index ff651c6c17b0..68830772f9a5 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceIdentityStore.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceIdentityStore.kt @@ -3,11 +3,7 @@ package ai.openclaw.android.gateway import android.content.Context import android.util.Base64 import java.io.File -import java.security.KeyFactory -import java.security.KeyPairGenerator import java.security.MessageDigest -import java.security.Signature -import java.security.spec.PKCS8EncodedKeySpec import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json @@ -22,21 +18,26 @@ data class DeviceIdentity( class DeviceIdentityStore(context: Context) { private val json = Json { ignoreUnknownKeys = true } private val identityFile = File(context.filesDir, "openclaw/identity/device.json") + @Volatile private var cachedIdentity: DeviceIdentity? = null @Synchronized fun loadOrCreate(): DeviceIdentity { + cachedIdentity?.let { return it } val existing = load() if (existing != null) { val derived = deriveDeviceId(existing.publicKeyRawBase64) if (derived != null && derived != existing.deviceId) { val updated = existing.copy(deviceId = derived) save(updated) + cachedIdentity = updated return updated } + cachedIdentity = existing return existing } val fresh = generate() save(fresh) + cachedIdentity = fresh return fresh } @@ -151,22 +152,16 @@ class DeviceIdentityStore(context: Context) { } } - private fun stripSpkiPrefix(spki: ByteArray): ByteArray { - if (spki.size == ED25519_SPKI_PREFIX.size + 32 && - spki.copyOfRange(0, ED25519_SPKI_PREFIX.size).contentEquals(ED25519_SPKI_PREFIX) - ) { - return spki.copyOfRange(ED25519_SPKI_PREFIX.size, spki.size) - } - return spki - } - private fun sha256Hex(data: ByteArray): String { val digest = MessageDigest.getInstance("SHA-256").digest(data) - val out = StringBuilder(digest.size * 2) + val out = CharArray(digest.size * 2) + var i = 0 for (byte in digest) { - out.append(String.format("%02x", byte)) + val v = byte.toInt() and 0xff + out[i++] = HEX[v ushr 4] + out[i++] = HEX[v and 0x0f] } - return out.toString() + return String(out) } private fun base64UrlEncode(data: ByteArray): String { @@ -174,9 +169,6 @@ class DeviceIdentityStore(context: Context) { } companion object { - private val ED25519_SPKI_PREFIX = - byteArrayOf( - 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, - ) + private val HEX = "0123456789abcdef".toCharArray() } } From 4a07c89816db0ab67b2e6a5d2a669b5c73afe82a Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 20:44:50 +0530 Subject: [PATCH 077/160] perf(android): make gateway token writes async --- .../app/src/main/java/ai/openclaw/android/SecurePrefs.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt b/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt index f03e2b56e0b0..1637c928f4aa 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt @@ -153,7 +153,7 @@ class SecurePrefs(context: Context) { fun setGatewayToken(value: String) { val trimmed = value.trim() - prefs.edit(commit = true) { putString("gateway.manual.token", trimmed) } + prefs.edit { putString("gateway.manual.token", trimmed) } _gatewayToken.value = trimmed } From b49c2cbdd9c3108ae665e48d23be9dc31ff39f56 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 21:34:15 +0530 Subject: [PATCH 078/160] perf(android): tighten startup path and add perf tooling --- .../java/ai/openclaw/android/MainActivity.kt | 8 +- .../java/ai/openclaw/android/SecurePrefs.kt | 113 +++++++------ apps/android/benchmark/build.gradle.kts | 36 ++++ .../benchmark/StartupMacrobenchmark.kt | 76 +++++++++ apps/android/build.gradle.kts | 1 + .../android/scripts/perf-startup-benchmark.sh | 124 ++++++++++++++ apps/android/scripts/perf-startup-hotspots.sh | 154 ++++++++++++++++++ apps/android/settings.gradle.kts | 1 + 8 files changed, 454 insertions(+), 59 deletions(-) create mode 100644 apps/android/benchmark/build.gradle.kts create mode 100644 apps/android/benchmark/src/main/java/ai/openclaw/android/benchmark/StartupMacrobenchmark.kt create mode 100755 apps/android/scripts/perf-startup-benchmark.sh create mode 100755 apps/android/scripts/perf-startup-hotspots.sh diff --git a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt index 21d0f15ff7ab..b90427672c60 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/MainActivity.kt @@ -1,9 +1,7 @@ package ai.openclaw.android -import android.content.pm.ApplicationInfo import android.os.Bundle import android.view.WindowManager -import android.webkit.WebView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels @@ -25,9 +23,6 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) WindowCompat.setDecorFitsSystemWindows(window, false) - val isDebuggable = (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0 - WebView.setWebContentsDebuggingEnabled(isDebuggable) - NodeForegroundService.start(this) permissionRequester = PermissionRequester(this) screenCaptureRequester = ScreenCaptureRequester(this) viewModel.camera.attachLifecycleOwner(this) @@ -55,6 +50,9 @@ class MainActivity : ComponentActivity() { } } } + + // Keep startup path lean: start foreground service after first frame. + window.decorView.post { NodeForegroundService.start(this) } } override fun onStart() { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt b/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt index 1637c928f4aa..96e4572955ec 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/SecurePrefs.kt @@ -20,19 +20,21 @@ class SecurePrefs(context: Context) { val defaultWakeWords: List = listOf("openclaw", "claude") private const val displayNameKey = "node.displayName" private const val voiceWakeModeKey = "voiceWake.mode" + private const val plainPrefsName = "openclaw.node" + private const val securePrefsName = "openclaw.node.secure" } private val appContext = context.applicationContext private val json = Json { ignoreUnknownKeys = true } + private val plainPrefs: SharedPreferences = + appContext.getSharedPreferences(plainPrefsName, Context.MODE_PRIVATE) - private val masterKey = - MasterKey.Builder(context) + private val masterKey by lazy { + MasterKey.Builder(appContext) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build() - - private val prefs: SharedPreferences by lazy { - createPrefs(appContext, "openclaw.node.secure") } + private val securePrefs: SharedPreferences by lazy { createSecurePrefs(appContext, securePrefsName) } private val _instanceId = MutableStateFlow(loadOrCreateInstanceId()) val instanceId: StateFlow = _instanceId @@ -41,52 +43,51 @@ class SecurePrefs(context: Context) { MutableStateFlow(loadOrMigrateDisplayName(context = context)) val displayName: StateFlow = _displayName - private val _cameraEnabled = MutableStateFlow(prefs.getBoolean("camera.enabled", true)) + private val _cameraEnabled = MutableStateFlow(plainPrefs.getBoolean("camera.enabled", true)) val cameraEnabled: StateFlow = _cameraEnabled private val _locationMode = - MutableStateFlow(LocationMode.fromRawValue(prefs.getString("location.enabledMode", "off"))) + MutableStateFlow(LocationMode.fromRawValue(plainPrefs.getString("location.enabledMode", "off"))) val locationMode: StateFlow = _locationMode private val _locationPreciseEnabled = - MutableStateFlow(prefs.getBoolean("location.preciseEnabled", true)) + MutableStateFlow(plainPrefs.getBoolean("location.preciseEnabled", true)) val locationPreciseEnabled: StateFlow = _locationPreciseEnabled - private val _preventSleep = MutableStateFlow(prefs.getBoolean("screen.preventSleep", true)) + private val _preventSleep = MutableStateFlow(plainPrefs.getBoolean("screen.preventSleep", true)) val preventSleep: StateFlow = _preventSleep private val _manualEnabled = - MutableStateFlow(prefs.getBoolean("gateway.manual.enabled", false)) + MutableStateFlow(plainPrefs.getBoolean("gateway.manual.enabled", false)) val manualEnabled: StateFlow = _manualEnabled private val _manualHost = - MutableStateFlow(prefs.getString("gateway.manual.host", "") ?: "") + MutableStateFlow(plainPrefs.getString("gateway.manual.host", "") ?: "") val manualHost: StateFlow = _manualHost private val _manualPort = - MutableStateFlow(prefs.getInt("gateway.manual.port", 18789)) + MutableStateFlow(plainPrefs.getInt("gateway.manual.port", 18789)) val manualPort: StateFlow = _manualPort private val _manualTls = - MutableStateFlow(prefs.getBoolean("gateway.manual.tls", true)) + MutableStateFlow(plainPrefs.getBoolean("gateway.manual.tls", true)) val manualTls: StateFlow = _manualTls - private val _gatewayToken = - MutableStateFlow(prefs.getString("gateway.manual.token", "") ?: "") + private val _gatewayToken = MutableStateFlow("") val gatewayToken: StateFlow = _gatewayToken private val _onboardingCompleted = - MutableStateFlow(prefs.getBoolean("onboarding.completed", false)) + MutableStateFlow(plainPrefs.getBoolean("onboarding.completed", false)) val onboardingCompleted: StateFlow = _onboardingCompleted private val _lastDiscoveredStableId = MutableStateFlow( - prefs.getString("gateway.lastDiscoveredStableID", "") ?: "", + plainPrefs.getString("gateway.lastDiscoveredStableID", "") ?: "", ) val lastDiscoveredStableId: StateFlow = _lastDiscoveredStableId private val _canvasDebugStatusEnabled = - MutableStateFlow(prefs.getBoolean("canvas.debugStatusEnabled", false)) + MutableStateFlow(plainPrefs.getBoolean("canvas.debugStatusEnabled", false)) val canvasDebugStatusEnabled: StateFlow = _canvasDebugStatusEnabled private val _wakeWords = MutableStateFlow(loadWakeWords()) @@ -95,65 +96,65 @@ class SecurePrefs(context: Context) { private val _voiceWakeMode = MutableStateFlow(loadVoiceWakeMode()) val voiceWakeMode: StateFlow = _voiceWakeMode - private val _talkEnabled = MutableStateFlow(prefs.getBoolean("talk.enabled", false)) + private val _talkEnabled = MutableStateFlow(plainPrefs.getBoolean("talk.enabled", false)) val talkEnabled: StateFlow = _talkEnabled fun setLastDiscoveredStableId(value: String) { val trimmed = value.trim() - prefs.edit { putString("gateway.lastDiscoveredStableID", trimmed) } + plainPrefs.edit { putString("gateway.lastDiscoveredStableID", trimmed) } _lastDiscoveredStableId.value = trimmed } fun setDisplayName(value: String) { val trimmed = value.trim() - prefs.edit { putString(displayNameKey, trimmed) } + plainPrefs.edit { putString(displayNameKey, trimmed) } _displayName.value = trimmed } fun setCameraEnabled(value: Boolean) { - prefs.edit { putBoolean("camera.enabled", value) } + plainPrefs.edit { putBoolean("camera.enabled", value) } _cameraEnabled.value = value } fun setLocationMode(mode: LocationMode) { - prefs.edit { putString("location.enabledMode", mode.rawValue) } + plainPrefs.edit { putString("location.enabledMode", mode.rawValue) } _locationMode.value = mode } fun setLocationPreciseEnabled(value: Boolean) { - prefs.edit { putBoolean("location.preciseEnabled", value) } + plainPrefs.edit { putBoolean("location.preciseEnabled", value) } _locationPreciseEnabled.value = value } fun setPreventSleep(value: Boolean) { - prefs.edit { putBoolean("screen.preventSleep", value) } + plainPrefs.edit { putBoolean("screen.preventSleep", value) } _preventSleep.value = value } fun setManualEnabled(value: Boolean) { - prefs.edit { putBoolean("gateway.manual.enabled", value) } + plainPrefs.edit { putBoolean("gateway.manual.enabled", value) } _manualEnabled.value = value } fun setManualHost(value: String) { val trimmed = value.trim() - prefs.edit { putString("gateway.manual.host", trimmed) } + plainPrefs.edit { putString("gateway.manual.host", trimmed) } _manualHost.value = trimmed } fun setManualPort(value: Int) { - prefs.edit { putInt("gateway.manual.port", value) } + plainPrefs.edit { putInt("gateway.manual.port", value) } _manualPort.value = value } fun setManualTls(value: Boolean) { - prefs.edit { putBoolean("gateway.manual.tls", value) } + plainPrefs.edit { putBoolean("gateway.manual.tls", value) } _manualTls.value = value } fun setGatewayToken(value: String) { val trimmed = value.trim() - prefs.edit { putString("gateway.manual.token", trimmed) } + securePrefs.edit { putString("gateway.manual.token", trimmed) } _gatewayToken.value = trimmed } @@ -162,62 +163,67 @@ class SecurePrefs(context: Context) { } fun setOnboardingCompleted(value: Boolean) { - prefs.edit { putBoolean("onboarding.completed", value) } + plainPrefs.edit { putBoolean("onboarding.completed", value) } _onboardingCompleted.value = value } fun setCanvasDebugStatusEnabled(value: Boolean) { - prefs.edit { putBoolean("canvas.debugStatusEnabled", value) } + plainPrefs.edit { putBoolean("canvas.debugStatusEnabled", value) } _canvasDebugStatusEnabled.value = value } fun loadGatewayToken(): String? { - val manual = _gatewayToken.value.trim() + val manual = + _gatewayToken.value.trim().ifEmpty { + val stored = securePrefs.getString("gateway.manual.token", null)?.trim().orEmpty() + if (stored.isNotEmpty()) _gatewayToken.value = stored + stored + } if (manual.isNotEmpty()) return manual val key = "gateway.token.${_instanceId.value}" - val stored = prefs.getString(key, null)?.trim() + val stored = securePrefs.getString(key, null)?.trim() return stored?.takeIf { it.isNotEmpty() } } fun saveGatewayToken(token: String) { val key = "gateway.token.${_instanceId.value}" - prefs.edit { putString(key, token.trim()) } + securePrefs.edit { putString(key, token.trim()) } } fun loadGatewayPassword(): String? { val key = "gateway.password.${_instanceId.value}" - val stored = prefs.getString(key, null)?.trim() + val stored = securePrefs.getString(key, null)?.trim() return stored?.takeIf { it.isNotEmpty() } } fun saveGatewayPassword(password: String) { val key = "gateway.password.${_instanceId.value}" - prefs.edit { putString(key, password.trim()) } + securePrefs.edit { putString(key, password.trim()) } } fun loadGatewayTlsFingerprint(stableId: String): String? { val key = "gateway.tls.$stableId" - return prefs.getString(key, null)?.trim()?.takeIf { it.isNotEmpty() } + return plainPrefs.getString(key, null)?.trim()?.takeIf { it.isNotEmpty() } } fun saveGatewayTlsFingerprint(stableId: String, fingerprint: String) { val key = "gateway.tls.$stableId" - prefs.edit { putString(key, fingerprint.trim()) } + plainPrefs.edit { putString(key, fingerprint.trim()) } } fun getString(key: String): String? { - return prefs.getString(key, null) + return securePrefs.getString(key, null) } fun putString(key: String, value: String) { - prefs.edit { putString(key, value) } + securePrefs.edit { putString(key, value) } } fun remove(key: String) { - prefs.edit { remove(key) } + securePrefs.edit { remove(key) } } - private fun createPrefs(context: Context, name: String): SharedPreferences { + private fun createSecurePrefs(context: Context, name: String): SharedPreferences { return EncryptedSharedPreferences.create( context, name, @@ -228,21 +234,21 @@ class SecurePrefs(context: Context) { } private fun loadOrCreateInstanceId(): String { - val existing = prefs.getString("node.instanceId", null)?.trim() + val existing = plainPrefs.getString("node.instanceId", null)?.trim() if (!existing.isNullOrBlank()) return existing val fresh = UUID.randomUUID().toString() - prefs.edit { putString("node.instanceId", fresh) } + plainPrefs.edit { putString("node.instanceId", fresh) } return fresh } private fun loadOrMigrateDisplayName(context: Context): String { - val existing = prefs.getString(displayNameKey, null)?.trim().orEmpty() + val existing = plainPrefs.getString(displayNameKey, null)?.trim().orEmpty() if (existing.isNotEmpty() && existing != "Android Node") return existing val candidate = DeviceNames.bestDefaultNodeName(context).trim() val resolved = candidate.ifEmpty { "Android Node" } - prefs.edit { putString(displayNameKey, resolved) } + plainPrefs.edit { putString(displayNameKey, resolved) } return resolved } @@ -250,34 +256,34 @@ class SecurePrefs(context: Context) { val sanitized = WakeWords.sanitize(words, defaultWakeWords) val encoded = JsonArray(sanitized.map { JsonPrimitive(it) }).toString() - prefs.edit { putString("voiceWake.triggerWords", encoded) } + plainPrefs.edit { putString("voiceWake.triggerWords", encoded) } _wakeWords.value = sanitized } fun setVoiceWakeMode(mode: VoiceWakeMode) { - prefs.edit { putString(voiceWakeModeKey, mode.rawValue) } + plainPrefs.edit { putString(voiceWakeModeKey, mode.rawValue) } _voiceWakeMode.value = mode } fun setTalkEnabled(value: Boolean) { - prefs.edit { putBoolean("talk.enabled", value) } + plainPrefs.edit { putBoolean("talk.enabled", value) } _talkEnabled.value = value } private fun loadVoiceWakeMode(): VoiceWakeMode { - val raw = prefs.getString(voiceWakeModeKey, null) + val raw = plainPrefs.getString(voiceWakeModeKey, null) val resolved = VoiceWakeMode.fromRawValue(raw) // Default ON (foreground) when unset. if (raw.isNullOrBlank()) { - prefs.edit { putString(voiceWakeModeKey, resolved.rawValue) } + plainPrefs.edit { putString(voiceWakeModeKey, resolved.rawValue) } } return resolved } private fun loadWakeWords(): List { - val raw = prefs.getString("voiceWake.triggerWords", null)?.trim() + val raw = plainPrefs.getString("voiceWake.triggerWords", null)?.trim() if (raw.isNullOrEmpty()) return defaultWakeWords return try { val element = json.parseToJsonElement(raw) @@ -295,5 +301,4 @@ class SecurePrefs(context: Context) { defaultWakeWords } } - } diff --git a/apps/android/benchmark/build.gradle.kts b/apps/android/benchmark/build.gradle.kts new file mode 100644 index 000000000000..99d1d8e4c60c --- /dev/null +++ b/apps/android/benchmark/build.gradle.kts @@ -0,0 +1,36 @@ +plugins { + id("com.android.test") +} + +android { + namespace = "ai.openclaw.android.benchmark" + compileSdk = 36 + + defaultConfig { + minSdk = 31 + targetSdk = 36 + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunnerArguments["androidx.benchmark.suppressErrors"] = "DEBUGGABLE,EMULATOR" + } + + targetProjectPath = ":app" + experimentalProperties["android.experimental.self-instrumenting"] = true + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } +} + +kotlin { + compilerOptions { + jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17) + allWarningsAsErrors.set(true) + } +} + +dependencies { + implementation("androidx.benchmark:benchmark-macro-junit4:1.4.1") + implementation("androidx.test.ext:junit:1.2.1") + implementation("androidx.test.uiautomator:uiautomator:2.4.0-alpha06") +} diff --git a/apps/android/benchmark/src/main/java/ai/openclaw/android/benchmark/StartupMacrobenchmark.kt b/apps/android/benchmark/src/main/java/ai/openclaw/android/benchmark/StartupMacrobenchmark.kt new file mode 100644 index 000000000000..46181f6a9a18 --- /dev/null +++ b/apps/android/benchmark/src/main/java/ai/openclaw/android/benchmark/StartupMacrobenchmark.kt @@ -0,0 +1,76 @@ +package ai.openclaw.android.benchmark + +import androidx.benchmark.macro.CompilationMode +import androidx.benchmark.macro.FrameTimingMetric +import androidx.benchmark.macro.StartupMode +import androidx.benchmark.macro.StartupTimingMetric +import androidx.benchmark.macro.junit4.MacrobenchmarkRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import org.junit.Assume.assumeTrue +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class StartupMacrobenchmark { + @get:Rule + val benchmarkRule = MacrobenchmarkRule() + + private val packageName = "ai.openclaw.android" + + @Test + fun coldStartup() { + runBenchmarkOrSkip { + benchmarkRule.measureRepeated( + packageName = packageName, + metrics = listOf(StartupTimingMetric()), + startupMode = StartupMode.COLD, + compilationMode = CompilationMode.None(), + iterations = 10, + ) { + pressHome() + startActivityAndWait() + } + } + } + + @Test + fun startupAndScrollFrameTiming() { + runBenchmarkOrSkip { + benchmarkRule.measureRepeated( + packageName = packageName, + metrics = listOf(FrameTimingMetric()), + startupMode = StartupMode.WARM, + compilationMode = CompilationMode.None(), + iterations = 10, + ) { + startActivityAndWait() + val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + val x = device.displayWidth / 2 + val yStart = (device.displayHeight * 0.8f).toInt() + val yEnd = (device.displayHeight * 0.25f).toInt() + repeat(4) { + device.swipe(x, yStart, x, yEnd, 24) + device.waitForIdle() + } + } + } + } + + private fun runBenchmarkOrSkip(run: () -> Unit) { + try { + run() + } catch (err: IllegalStateException) { + val message = err.message.orEmpty() + val knownDeviceIssue = + message.contains("Unable to confirm activity launch completion") || + message.contains("no renderthread slices", ignoreCase = true) + if (knownDeviceIssue) { + assumeTrue("Skipping benchmark on this device: $message", false) + } + throw err + } + } +} diff --git a/apps/android/build.gradle.kts b/apps/android/build.gradle.kts index bea7b46b2c21..1d191c9e375c 100644 --- a/apps/android/build.gradle.kts +++ b/apps/android/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("com.android.application") version "9.0.1" apply false + id("com.android.test") version "9.0.1" apply false id("org.jetbrains.kotlin.plugin.compose") version "2.2.21" apply false id("org.jetbrains.kotlin.plugin.serialization") version "2.2.21" apply false } diff --git a/apps/android/scripts/perf-startup-benchmark.sh b/apps/android/scripts/perf-startup-benchmark.sh new file mode 100755 index 000000000000..70342d3cba48 --- /dev/null +++ b/apps/android/scripts/perf-startup-benchmark.sh @@ -0,0 +1,124 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +ANDROID_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)" +RESULTS_DIR="$ANDROID_DIR/benchmark/results" +CLASS_FILTER="ai.openclaw.android.benchmark.StartupMacrobenchmark#coldStartup" +BASELINE_JSON="" + +usage() { + cat <<'EOF' +Usage: + ./scripts/perf-startup-benchmark.sh [--baseline ] + +Runs cold-start macrobenchmark only, then prints a compact summary. +Also saves a timestamped snapshot JSON under benchmark/results/. +If --baseline is omitted, compares against latest previous snapshot when available. +EOF +} + +while [[ $# -gt 0 ]]; do + case "$1" in + --baseline) + BASELINE_JSON="${2:-}" + shift 2 + ;; + -h|--help) + usage + exit 0 + ;; + *) + echo "Unknown arg: $1" >&2 + usage >&2 + exit 2 + ;; + esac +done + +if ! command -v jq >/dev/null 2>&1; then + echo "jq required but missing." >&2 + exit 1 +fi + +if ! command -v adb >/dev/null 2>&1; then + echo "adb required but missing." >&2 + exit 1 +fi + +device_count="$(adb devices | awk 'NR>1 && $2=="device" {c+=1} END {print c+0}')" +if [[ "$device_count" -lt 1 ]]; then + echo "No connected Android device (adb state=device)." >&2 + exit 1 +fi + +mkdir -p "$RESULTS_DIR" + +run_log="$(mktemp -t openclaw-android-bench.XXXXXX.log)" +trap 'rm -f "$run_log"' EXIT + +cd "$ANDROID_DIR" + +./gradlew :benchmark:connectedDebugAndroidTest \ + -Pandroid.testInstrumentationRunnerArguments.class="$CLASS_FILTER" \ + --console=plain \ + >"$run_log" 2>&1 + +latest_json="$( + find "$ANDROID_DIR/benchmark/build/outputs/connected_android_test_additional_output/debug/connected" \ + -name '*benchmarkData.json' -type f \ + | while IFS= read -r file; do + printf '%s\t%s\n' "$(stat -f '%m' "$file")" "$file" + done \ + | sort -nr \ + | head -n1 \ + | cut -f2- +)" + +if [[ -z "$latest_json" || ! -f "$latest_json" ]]; then + echo "benchmarkData.json not found after run." >&2 + tail -n 120 "$run_log" >&2 + exit 1 +fi + +timestamp="$(date +%Y%m%d-%H%M%S)" +snapshot_json="$RESULTS_DIR/startup-$timestamp.json" +cp "$latest_json" "$snapshot_json" + +median_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.median' "$snapshot_json")" +min_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.minimum' "$snapshot_json")" +max_ms="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.maximum' "$snapshot_json")" +cov="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.coefficientOfVariation' "$snapshot_json")" +device="$(jq -r '.context.build.model' "$snapshot_json")" +sdk="$(jq -r '.context.build.version.sdk' "$snapshot_json")" +runs_count="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.runs | length' "$snapshot_json")" + +printf 'startup.cold.median_ms=%.3f min_ms=%.3f max_ms=%.3f cov=%.4f runs=%s device=%s sdk=%s\n' \ + "$median_ms" "$min_ms" "$max_ms" "$cov" "$runs_count" "$device" "$sdk" +echo "snapshot_json=$snapshot_json" + +if [[ -z "$BASELINE_JSON" ]]; then + BASELINE_JSON="$( + find "$RESULTS_DIR" -name 'startup-*.json' -type f \ + | while IFS= read -r file; do + if [[ "$file" == "$snapshot_json" ]]; then + continue + fi + printf '%s\t%s\n' "$(stat -f '%m' "$file")" "$file" + done \ + | sort -nr \ + | head -n1 \ + | cut -f2- + )" +fi + +if [[ -n "$BASELINE_JSON" ]]; then + if [[ ! -f "$BASELINE_JSON" ]]; then + echo "Baseline file missing: $BASELINE_JSON" >&2 + exit 1 + fi + base_median="$(jq -r '.benchmarks[] | select(.name=="coldStartup") | .metrics.timeToInitialDisplayMs.median' "$BASELINE_JSON")" + delta_ms="$(awk -v a="$median_ms" -v b="$base_median" 'BEGIN { printf "%.3f", (a-b) }')" + delta_pct="$(awk -v a="$median_ms" -v b="$base_median" 'BEGIN { if (b==0) { print "nan" } else { printf "%.2f", ((a-b)/b)*100 } }')" + echo "baseline_median_ms=$base_median delta_ms=$delta_ms delta_pct=$delta_pct%" +fi diff --git a/apps/android/scripts/perf-startup-hotspots.sh b/apps/android/scripts/perf-startup-hotspots.sh new file mode 100755 index 000000000000..787d5fac3005 --- /dev/null +++ b/apps/android/scripts/perf-startup-hotspots.sh @@ -0,0 +1,154 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +ANDROID_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)" + +PACKAGE="ai.openclaw.android" +ACTIVITY=".MainActivity" +DURATION_SECONDS="10" +OUTPUT_PERF_DATA="" + +usage() { + cat <<'EOF' +Usage: + ./scripts/perf-startup-hotspots.sh [--package ] [--activity ] [--duration ] [--out ] + +Captures startup CPU profile via simpleperf (app_profiler.py), then prints concise hotspot summaries. +Default package/activity target OpenClaw Android startup. +EOF +} + +while [[ $# -gt 0 ]]; do + case "$1" in + --package) + PACKAGE="${2:-}" + shift 2 + ;; + --activity) + ACTIVITY="${2:-}" + shift 2 + ;; + --duration) + DURATION_SECONDS="${2:-}" + shift 2 + ;; + --out) + OUTPUT_PERF_DATA="${2:-}" + shift 2 + ;; + -h|--help) + usage + exit 0 + ;; + *) + echo "Unknown arg: $1" >&2 + usage >&2 + exit 2 + ;; + esac +done + +if ! command -v uv >/dev/null 2>&1; then + echo "uv required but missing." >&2 + exit 1 +fi + +if ! command -v adb >/dev/null 2>&1; then + echo "adb required but missing." >&2 + exit 1 +fi + +if [[ -z "$OUTPUT_PERF_DATA" ]]; then + OUTPUT_PERF_DATA="/tmp/openclaw-startup-$(date +%Y%m%d-%H%M%S).perf.data" +fi + +device_count="$(adb devices | awk 'NR>1 && $2=="device" {c+=1} END {print c+0}')" +if [[ "$device_count" -lt 1 ]]; then + echo "No connected Android device (adb state=device)." >&2 + exit 1 +fi + +simpleperf_dir="" +if [[ -n "${ANDROID_NDK_HOME:-}" && -f "${ANDROID_NDK_HOME}/simpleperf/app_profiler.py" ]]; then + simpleperf_dir="${ANDROID_NDK_HOME}/simpleperf" +elif [[ -n "${ANDROID_NDK_ROOT:-}" && -f "${ANDROID_NDK_ROOT}/simpleperf/app_profiler.py" ]]; then + simpleperf_dir="${ANDROID_NDK_ROOT}/simpleperf" +else + latest_simpleperf="$(ls -d "${HOME}/Library/Android/sdk/ndk/"*/simpleperf 2>/dev/null | sort -V | tail -n1 || true)" + if [[ -n "$latest_simpleperf" && -f "$latest_simpleperf/app_profiler.py" ]]; then + simpleperf_dir="$latest_simpleperf" + fi +fi + +if [[ -z "$simpleperf_dir" ]]; then + echo "simpleperf not found. Set ANDROID_NDK_HOME or install NDK under ~/Library/Android/sdk/ndk/." >&2 + exit 1 +fi + +app_profiler="$simpleperf_dir/app_profiler.py" +report_py="$simpleperf_dir/report.py" +ndk_path="$(cd -- "$simpleperf_dir/.." && pwd)" + +tmp_dir="$(mktemp -d -t openclaw-android-hotspots.XXXXXX)" +trap 'rm -rf "$tmp_dir"' EXIT + +capture_log="$tmp_dir/capture.log" +dso_csv="$tmp_dir/dso.csv" +symbols_csv="$tmp_dir/symbols.csv" +children_txt="$tmp_dir/children.txt" + +cd "$ANDROID_DIR" +./gradlew :app:installDebug --console=plain >"$tmp_dir/install.log" 2>&1 + +if ! uv run --no-project python3 "$app_profiler" \ + -p "$PACKAGE" \ + -a "$ACTIVITY" \ + -o "$OUTPUT_PERF_DATA" \ + --ndk_path "$ndk_path" \ + -r "-e task-clock:u -f 1000 -g --duration $DURATION_SECONDS" \ + >"$capture_log" 2>&1; then + echo "simpleperf capture failed. tail(capture_log):" >&2 + tail -n 120 "$capture_log" >&2 + exit 1 +fi + +uv run --no-project python3 "$report_py" \ + -i "$OUTPUT_PERF_DATA" \ + --sort dso \ + --csv \ + --csv-separator "|" \ + --include-process-name "$PACKAGE" \ + >"$dso_csv" 2>"$tmp_dir/report-dso.err" + +uv run --no-project python3 "$report_py" \ + -i "$OUTPUT_PERF_DATA" \ + --sort dso,symbol \ + --csv \ + --csv-separator "|" \ + --include-process-name "$PACKAGE" \ + >"$symbols_csv" 2>"$tmp_dir/report-symbols.err" + +uv run --no-project python3 "$report_py" \ + -i "$OUTPUT_PERF_DATA" \ + --children \ + --sort dso,symbol \ + -n \ + --percent-limit 0.2 \ + --include-process-name "$PACKAGE" \ + >"$children_txt" 2>"$tmp_dir/report-children.err" + +clean_csv() { + awk 'BEGIN{print_on=0} /^Overhead\|/{print_on=1} print_on==1{print}' "$1" +} + +echo "perf_data=$OUTPUT_PERF_DATA" +echo +echo "top_dso_self:" +clean_csv "$dso_csv" | tail -n +2 | awk -F'|' 'NR<=10 {printf " %s %s\n", $1, $2}' +echo +echo "top_symbols_self:" +clean_csv "$symbols_csv" | tail -n +2 | awk -F'|' 'NR<=20 {printf " %s %s :: %s\n", $1, $2, $3}' +echo +echo "app_path_clues_children:" +rg 'androidx\.compose|MainActivity|NodeRuntime|NodeForegroundService|SecurePrefs|WebView|libwebviewchromium' "$children_txt" | awk 'NR<=20 {print}' || true diff --git a/apps/android/settings.gradle.kts b/apps/android/settings.gradle.kts index b3b43a445501..25e5d09bbe1d 100644 --- a/apps/android/settings.gradle.kts +++ b/apps/android/settings.gradle.kts @@ -16,3 +16,4 @@ dependencyResolutionManagement { rootProject.name = "OpenClawNodeAndroid" include(":app") +include(":benchmark") From 3175640ea2dee69f9f2a5bc25c8183cebf23846f Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 25 Feb 2026 21:34:31 +0530 Subject: [PATCH 079/160] docs(android): add perf CLI workflow docs --- apps/android/README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/apps/android/README.md b/apps/android/README.md index 799109c0a0f8..4a9951e64412 100644 --- a/apps/android/README.md +++ b/apps/android/README.md @@ -34,6 +34,40 @@ cd apps/android `gradlew` auto-detects the Android SDK at `~/Library/Android/sdk` (macOS default) if `ANDROID_SDK_ROOT` / `ANDROID_HOME` are unset. +## Macrobenchmark (Startup + Frame Timing) + +```bash +cd apps/android +./gradlew :benchmark:connectedDebugAndroidTest +``` + +Reports are written under: + +- `apps/android/benchmark/build/reports/androidTests/connected/` + +## Perf CLI (low-noise) + +Deterministic startup measurement + hotspot extraction with compact CLI output: + +```bash +cd apps/android +./scripts/perf-startup-benchmark.sh +./scripts/perf-startup-hotspots.sh +``` + +Benchmark script behavior: + +- Runs only `StartupMacrobenchmark#coldStartup` (10 iterations). +- Prints median/min/max/COV in one line. +- Writes timestamped snapshot JSON to `apps/android/benchmark/results/`. +- Auto-compares with previous local snapshot (or pass explicit baseline: `--baseline `). + +Hotspot script behavior: + +- Ensures debug app installed, captures startup `simpleperf` data for `.MainActivity`. +- Prints top DSOs, top symbols, and key app-path clues (Compose/MainActivity/WebView). +- Writes raw `perf.data` path for deeper follow-up if needed. + ## Run on a Real Android Phone (USB) 1) On phone, enable **Developer options** + **USB debugging**. From 410ba918fb7b60b9a777b20589d9494683ae9456 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 07:44:15 +0530 Subject: [PATCH 080/160] fix(android): hydrate gateway token state on init --- .../app/src/main/java/ai/openclaw/android/NodeRuntime.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index 15d99ffb9312..02e9b136091f 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -450,6 +450,10 @@ class NodeRuntime(context: Context) { prefs.setVoiceWakeMode(VoiceWakeMode.Off) } + scope.launch { + prefs.loadGatewayToken() + } + scope.launch { prefs.talkEnabled.collect { enabled -> micCapture.setMicEnabled(enabled) From 958cafc54f8d0638cf3342a7b2c453aebec1fc7d Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 07:49:44 +0530 Subject: [PATCH 081/160] fix: add changelog note for android startup perf (#26659) (thanks @obviyus) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f67fa020bcfd..01023da69a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Docs: https://docs.openclaw.ai ### Changes - Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. +- Android/Startup perf: defer foreground-service startup, move WebView debugging init out of critical startup, and add startup macrobenchmark + low-noise perf CLI scripts for deterministic cold-start tracking. (#26659) Thanks @obviyus. - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. From 069bbf9741bcbcad2db54349bf02668f66cf5eb0 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:20:40 +0000 Subject: [PATCH 082/160] fix(slack): land #26878 allowlist channel ID case-insensitive match (thanks @lbo728) Land contributor PR #26878 from @lbo728; include changelog credit and regression tests. Co-authored-by: lbo728 --- CHANGELOG.md | 1 + src/slack/monitor/channel-config.ts | 8 ++++++++ src/slack/monitor/monitor.test.ts | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01023da69a30..df98ab8f3766 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Docs: https://docs.openclaw.ai - Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. - Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. - Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. +- Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example `c0abc12345`) correctly match Slack runtime IDs (`C0ABC12345`) under `groupPolicy: "allowlist"`, preventing silent channel-event drops. (#26878) Thanks @lbo728. - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. diff --git a/src/slack/monitor/channel-config.ts b/src/slack/monitor/channel-config.ts index 15ba7c3b1469..b594a34d43b8 100644 --- a/src/slack/monitor/channel-config.ts +++ b/src/slack/monitor/channel-config.ts @@ -96,8 +96,16 @@ export function resolveSlackChannelConfig(params: { const keys = Object.keys(entries); const normalizedName = channelName ? normalizeSlackSlug(channelName) : ""; const directName = channelName ? channelName.trim() : ""; + // Slack always delivers channel IDs in uppercase (e.g. C0ABC12345) but + // operators commonly write them in lowercase in their config. Add both + // case variants so the lookup is case-insensitive without requiring a full + // entry-scan. buildChannelKeyCandidates deduplicates identical keys. + const channelIdLower = channelId.toLowerCase(); + const channelIdUpper = channelId.toUpperCase(); const candidates = buildChannelKeyCandidates( channelId, + channelIdLower !== channelId ? channelIdLower : undefined, + channelIdUpper !== channelId ? channelIdUpper : undefined, channelName ? `#${directName}` : undefined, directName, normalizedName, diff --git a/src/slack/monitor/monitor.test.ts b/src/slack/monitor/monitor.test.ts index 3262873718da..3da7f08164e0 100644 --- a/src/slack/monitor/monitor.test.ts +++ b/src/slack/monitor/monitor.test.ts @@ -60,6 +60,27 @@ describe("resolveSlackChannelConfig", () => { matchSource: "direct", }); }); + + it("matches channel config key stored in lowercase when Slack delivers uppercase channel ID", () => { + // Slack always delivers channel IDs in uppercase (e.g. C0ABC12345). + // Users commonly copy them in lowercase from docs or older CLI output. + const res = resolveSlackChannelConfig({ + channelId: "C0ABC12345", + channels: { c0abc12345: { allow: true, requireMention: false } }, + defaultRequireMention: true, + }); + expect(res).toMatchObject({ allowed: true, requireMention: false }); + }); + + it("matches channel config key stored in uppercase when user types lowercase channel ID", () => { + // Defensive: also handle the inverse direction. + const res = resolveSlackChannelConfig({ + channelId: "c0abc12345", + channels: { C0ABC12345: { allow: true, requireMention: false } }, + defaultRequireMention: true, + }); + expect(res).toMatchObject({ allowed: true, requireMention: false }); + }); }); const baseParams = () => ({ From b786d11fea377d97d5c07cadee7a0f8bc8e0a168 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:32:36 +0100 Subject: [PATCH 083/160] refactor(telegram): simplify polling restart flow --- src/telegram/monitor.test.ts | 177 ++++++++++++++++++++++------------- src/telegram/monitor.ts | 136 +++++++++++++++------------ 2 files changed, 186 insertions(+), 127 deletions(-) diff --git a/src/telegram/monitor.test.ts b/src/telegram/monitor.test.ts index 49fbcc131554..4e59f6c0c6a4 100644 --- a/src/telegram/monitor.test.ts +++ b/src/telegram/monitor.test.ts @@ -67,6 +67,36 @@ const { startTelegramWebhookSpy } = vi.hoisted(() => ({ startTelegramWebhookSpy: vi.fn(async () => ({ server: { close: vi.fn() }, stop: vi.fn() })), })); +type RunnerStub = { + task: () => Promise; + stop: ReturnType void | Promise>>; + isRunning: () => boolean; +}; + +const makeRunnerStub = (overrides: Partial = {}): RunnerStub => ({ + task: overrides.task ?? (() => Promise.resolve()), + stop: overrides.stop ?? vi.fn<() => void | Promise>(), + isRunning: overrides.isRunning ?? (() => false), +}); + +async function monitorWithAutoAbort( + opts: Omit[0], "abortSignal"> = {}, +) { + const abort = new AbortController(); + runSpy.mockImplementationOnce(() => + makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }), + ); + await monitorTelegramProvider({ + token: "tok", + ...opts, + abortSignal: abort.signal, + }); +} + vi.mock("../config/config.js", async (importOriginal) => { const actual = await importOriginal(); return { @@ -149,7 +179,7 @@ describe("monitorTelegramProvider (grammY)", () => { Object.values(api).forEach((fn) => { fn?.mockReset?.(); }); - await monitorTelegramProvider({ token: "tok" }); + await monitorWithAutoAbort(); expect(handlers.message).toBeDefined(); await handlers.message?.({ message: { @@ -172,7 +202,7 @@ describe("monitorTelegramProvider (grammY)", () => { channels: { telegram: {} }, }); - await monitorTelegramProvider({ token: "tok" }); + await monitorWithAutoAbort(); expect(runSpy).toHaveBeenCalledWith( expect.anything(), @@ -180,7 +210,7 @@ describe("monitorTelegramProvider (grammY)", () => { sink: { concurrency: 3 }, runner: expect.objectContaining({ silent: true, - maxRetryTime: 5 * 60 * 1000, + maxRetryTime: 60 * 60 * 1000, retryInterval: "exponential", }), }), @@ -191,7 +221,7 @@ describe("monitorTelegramProvider (grammY)", () => { Object.values(api).forEach((fn) => { fn?.mockReset?.(); }); - await monitorTelegramProvider({ token: "tok" }); + await monitorWithAutoAbort(); await handlers.message?.({ message: { message_id: 2, @@ -205,24 +235,27 @@ describe("monitorTelegramProvider (grammY)", () => { }); it("retries on recoverable undici fetch errors", async () => { + const abort = new AbortController(); const networkError = Object.assign(new TypeError("fetch failed"), { cause: Object.assign(new Error("connect timeout"), { code: "UND_ERR_CONNECT_TIMEOUT", }), }); runSpy - .mockImplementationOnce(() => ({ - task: () => Promise.reject(networkError), - stop: vi.fn(), - isRunning: (): boolean => false, - })) - .mockImplementationOnce(() => ({ - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: (): boolean => false, - })); - - await monitorTelegramProvider({ token: "tok" }); + .mockImplementationOnce(() => + makeRunnerStub({ + task: () => Promise.reject(networkError), + }), + ) + .mockImplementationOnce(() => + makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }), + ); + + await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); expect(computeBackoff).toHaveBeenCalled(); expect(sleepWithAbort).toHaveBeenCalled(); @@ -230,6 +263,7 @@ describe("monitorTelegramProvider (grammY)", () => { }); it("deletes webhook before starting polling", async () => { + const abort = new AbortController(); const order: string[] = []; api.deleteWebhook.mockReset(); api.deleteWebhook.mockImplementationOnce(async () => { @@ -238,20 +272,21 @@ describe("monitorTelegramProvider (grammY)", () => { }); runSpy.mockImplementationOnce(() => { order.push("run"); - return { - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: () => false, - }; + return makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }); }); - await monitorTelegramProvider({ token: "tok" }); + await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); expect(api.deleteWebhook).toHaveBeenCalledWith({ drop_pending_updates: false }); expect(order).toEqual(["deleteWebhook", "run"]); }); it("retries recoverable deleteWebhook failures before polling", async () => { + const abort = new AbortController(); const cleanupError = Object.assign(new TypeError("fetch failed"), { cause: Object.assign(new Error("connect timeout"), { code: "UND_ERR_CONNECT_TIMEOUT", @@ -259,13 +294,15 @@ describe("monitorTelegramProvider (grammY)", () => { }); api.deleteWebhook.mockReset(); api.deleteWebhook.mockRejectedValueOnce(cleanupError).mockResolvedValueOnce(true); - runSpy.mockImplementationOnce(() => ({ - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: () => false, - })); + runSpy.mockImplementationOnce(() => + makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }), + ); - await monitorTelegramProvider({ token: "tok" }); + await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); expect(api.deleteWebhook).toHaveBeenCalledTimes(2); expect(computeBackoff).toHaveBeenCalled(); @@ -274,6 +311,7 @@ describe("monitorTelegramProvider (grammY)", () => { }); it("retries setup-time recoverable errors before starting polling", async () => { + const abort = new AbortController(); const setupError = Object.assign(new TypeError("fetch failed"), { cause: Object.assign(new Error("connect timeout"), { code: "UND_ERR_CONNECT_TIMEOUT", @@ -281,13 +319,15 @@ describe("monitorTelegramProvider (grammY)", () => { }); createTelegramBotErrors.push(setupError); - runSpy.mockImplementationOnce(() => ({ - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: () => false, - })); + runSpy.mockImplementationOnce(() => + makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }), + ); - await monitorTelegramProvider({ token: "tok" }); + await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); expect(computeBackoff).toHaveBeenCalled(); expect(sleepWithAbort).toHaveBeenCalled(); @@ -295,6 +335,7 @@ describe("monitorTelegramProvider (grammY)", () => { }); it("awaits runner.stop before retrying after recoverable polling error", async () => { + const abort = new AbortController(); const recoverableError = Object.assign(new TypeError("fetch failed"), { cause: Object.assign(new Error("connect timeout"), { code: "UND_ERR_CONNECT_TIMEOUT", @@ -307,21 +348,22 @@ describe("monitorTelegramProvider (grammY)", () => { }); runSpy - .mockImplementationOnce(() => ({ - task: () => Promise.reject(recoverableError), - stop: firstStop, - isRunning: () => false, - })) + .mockImplementationOnce(() => + makeRunnerStub({ + task: () => Promise.reject(recoverableError), + stop: firstStop, + }), + ) .mockImplementationOnce(() => { expect(firstStopped).toBe(true); - return { - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: () => false, - }; + return makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }); }); - await monitorTelegramProvider({ token: "tok" }); + await monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); expect(firstStop).toHaveBeenCalled(); expect(computeBackoff).toHaveBeenCalled(); @@ -330,16 +372,17 @@ describe("monitorTelegramProvider (grammY)", () => { }); it("surfaces non-recoverable errors", async () => { - runSpy.mockImplementationOnce(() => ({ - task: () => Promise.reject(new Error("bad token")), - stop: vi.fn(), - isRunning: (): boolean => false, - })); + runSpy.mockImplementationOnce(() => + makeRunnerStub({ + task: () => Promise.reject(new Error("bad token")), + }), + ); await expect(monitorTelegramProvider({ token: "tok" })).rejects.toThrow("bad token"); }); it("force-restarts polling when unhandled network rejection stalls runner", async () => { + const abort = new AbortController(); let running = true; let releaseTask: (() => void) | undefined; const stop = vi.fn(async () => { @@ -348,21 +391,25 @@ describe("monitorTelegramProvider (grammY)", () => { }); runSpy - .mockImplementationOnce(() => ({ - task: () => - new Promise((resolve) => { - releaseTask = resolve; - }), - stop, - isRunning: () => running, - })) - .mockImplementationOnce(() => ({ - task: () => Promise.resolve(), - stop: vi.fn(), - isRunning: () => false, - })); - - const monitor = monitorTelegramProvider({ token: "tok" }); + .mockImplementationOnce(() => + makeRunnerStub({ + task: () => + new Promise((resolve) => { + releaseTask = resolve; + }), + stop, + isRunning: () => running, + }), + ) + .mockImplementationOnce(() => + makeRunnerStub({ + task: async () => { + abort.abort(); + }, + }), + ); + + const monitor = monitorTelegramProvider({ token: "tok", abortSignal: abort.signal }); await vi.waitFor(() => expect(runSpy).toHaveBeenCalledTimes(1)); expect(emitUnhandledRejection(new TypeError("fetch failed"))).toBe(true); diff --git a/src/telegram/monitor.ts b/src/telegram/monitor.ts index 8c93eee60c95..579db8ad3a1f 100644 --- a/src/telegram/monitor.ts +++ b/src/telegram/monitor.ts @@ -45,9 +45,8 @@ export function createTelegramRunnerOptions(cfg: OpenClawConfig): RunOptions; + const isGetUpdatesConflict = (err: unknown) => { if (!err || typeof err !== "object") { return false; @@ -188,21 +189,11 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { let restartAttempts = 0; let webhookCleared = false; const runnerOptions = createTelegramRunnerOptions(cfg); - const waitBeforeRetryOnRecoverableSetupError = async ( - err: unknown, - logPrefix: string, - ): Promise => { - if (opts.abortSignal?.aborted) { - return false; - } - if (!isRecoverableTelegramNetworkError(err, { context: "unknown" })) { - throw err; - } + const waitBeforeRestart = async (buildLine: (delay: string) => string): Promise => { restartAttempts += 1; const delayMs = computeBackoff(TELEGRAM_POLL_RESTART_POLICY, restartAttempts); - (opts.runtime?.error ?? console.error)( - `${logPrefix}: ${formatErrorMessage(err)}; retrying in ${formatDurationPrecise(delayMs)}.`, - ); + const delay = formatDurationPrecise(delayMs); + log(buildLine(delay)); try { await sleepWithAbort(delayMs, opts.abortSignal); } catch (sleepErr) { @@ -214,10 +205,24 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { return true; }; - while (!opts.abortSignal?.aborted) { - let bot; + const waitBeforeRetryOnRecoverableSetupError = async ( + err: unknown, + logPrefix: string, + ): Promise => { + if (opts.abortSignal?.aborted) { + return false; + } + if (!isRecoverableTelegramNetworkError(err, { context: "unknown" })) { + throw err; + } + return waitBeforeRestart( + (delay) => `${logPrefix}: ${formatErrorMessage(err)}; retrying in ${delay}.`, + ); + }; + + const createPollingBot = async (): Promise => { try { - bot = createTelegramBot({ + return createTelegramBot({ token, runtime: opts.runtime, proxyFetch, @@ -234,31 +239,34 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { "Telegram setup network error", ); if (!shouldRetry) { - return; + return undefined; } - continue; + return undefined; } + }; - if (!webhookCleared) { - try { - await withTelegramApiErrorLogging({ - operation: "deleteWebhook", - runtime: opts.runtime, - fn: () => bot.api.deleteWebhook({ drop_pending_updates: false }), - }); - webhookCleared = true; - } catch (err) { - const shouldRetry = await waitBeforeRetryOnRecoverableSetupError( - err, - "Telegram webhook cleanup failed", - ); - if (!shouldRetry) { - return; - } - continue; - } + const ensureWebhookCleanup = async (bot: TelegramBot): Promise<"ready" | "retry" | "exit"> => { + if (webhookCleared) { + return "ready"; + } + try { + await withTelegramApiErrorLogging({ + operation: "deleteWebhook", + runtime: opts.runtime, + fn: () => bot.api.deleteWebhook({ drop_pending_updates: false }), + }); + webhookCleared = true; + return "ready"; + } catch (err) { + const shouldRetry = await waitBeforeRetryOnRecoverableSetupError( + err, + "Telegram webhook cleanup failed", + ); + return shouldRetry ? "retry" : "exit"; } + }; + const runPollingCycle = async (bot: TelegramBot): Promise<"continue" | "exit"> => { const runner = run(bot, runnerOptions); activeRunner = runner; let stopPromise: Promise | undefined; @@ -280,23 +288,16 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { // runner.task() returns a promise that resolves when the runner stops await runner.task(); if (opts.abortSignal?.aborted) { - return; + return "exit"; } - // The runner stopped on its own. This can happen when grammY's - // maxRetryTime is exceeded (e.g. prolonged network outage). - // Instead of exiting permanently, restart with backoff so polling - // recovers once connectivity is restored. - restartAttempts += 1; - const delayMs = computeBackoff(TELEGRAM_POLL_RESTART_POLICY, restartAttempts); const reason = forceRestarted ? "unhandled network error" : "runner stopped (maxRetryTime exceeded or graceful stop)"; forceRestarted = false; - log( - `Telegram polling runner stopped (${reason}); restarting in ${formatDurationPrecise(delayMs)}.`, + const shouldRestart = await waitBeforeRestart( + (delay) => `Telegram polling runner stopped (${reason}); restarting in ${delay}.`, ); - await sleepWithAbort(delayMs, opts.abortSignal); - continue; + return shouldRestart ? "continue" : "exit"; } catch (err) { forceRestarted = false; if (opts.abortSignal?.aborted) { @@ -307,25 +308,36 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { if (!isConflict && !isRecoverable) { throw err; } - restartAttempts += 1; - const delayMs = computeBackoff(TELEGRAM_POLL_RESTART_POLICY, restartAttempts); const reason = isConflict ? "getUpdates conflict" : "network error"; const errMsg = formatErrorMessage(err); - (opts.runtime?.error ?? console.error)( - `Telegram ${reason}: ${errMsg}; retrying in ${formatDurationPrecise(delayMs)}.`, + const shouldRestart = await waitBeforeRestart( + (delay) => `Telegram ${reason}: ${errMsg}; retrying in ${delay}.`, ); - try { - await sleepWithAbort(delayMs, opts.abortSignal); - } catch (sleepErr) { - if (opts.abortSignal?.aborted) { - return; - } - throw sleepErr; - } + return shouldRestart ? "continue" : "exit"; } finally { opts.abortSignal?.removeEventListener("abort", stopOnAbort); await stopRunner(); } + }; + + while (!opts.abortSignal?.aborted) { + const bot = await createPollingBot(); + if (!bot) { + continue; + } + + const cleanupState = await ensureWebhookCleanup(bot); + if (cleanupState === "retry") { + continue; + } + if (cleanupState === "exit") { + return; + } + + const state = await runPollingCycle(bot); + if (state === "exit") { + return; + } } } finally { unregisterHandler(); From 7b4fe6d9bc85483c1820b33548e80715cc4cb64b Mon Sep 17 00:00:00 2001 From: Junyi Date: Sat, 7 Feb 2026 21:48:14 +0800 Subject: [PATCH 084/160] style(chat): UI: add mobile layout for chat compose actions - Stack chat compose row vertically on mobile (max-width: 640px) - Change action buttons to vertical layout with full width - Improve mobile UX for send and session control buttons --- ui/src/styles/chat/layout.css | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ui/src/styles/chat/layout.css b/ui/src/styles/chat/layout.css index 4a5c4cdfa46d..25fa6742b4a5 100644 --- a/ui/src/styles/chat/layout.css +++ b/ui/src/styles/chat/layout.css @@ -452,6 +452,24 @@ grid-template-columns: 1fr; } + /* Mobile: stack compose row vertically */ + .chat-compose__row { + flex-direction: column; + gap: 8px; + } + + /* Mobile: stack action buttons vertically */ + .chat-compose__actions { + flex-direction: column; + width: 100%; + gap: 8px; + } + + /* Mobile: full-width buttons */ + .chat-compose .chat-compose__actions .btn { + width: 100%; + } + .chat-controls { flex-wrap: wrap; gap: 8px; From 260bec5985008e33529a8c10ea1126a80a635535 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 08:03:46 +0530 Subject: [PATCH 085/160] fix: add changelog for chat compose mobile layout (#11167) (thanks @junyiz) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df98ab8f3766..8f82aa875ee7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Docs: https://docs.openclaw.ai - Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. - Android/Startup perf: defer foreground-service startup, move WebView debugging init out of critical startup, and add startup macrobenchmark + low-noise perf CLI scripts for deterministic cold-start tracking. (#26659) Thanks @obviyus. - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. +- UI/Chat compose: add mobile stacked layout for compose action buttons on small screens to improve send/session controls usability. (#11167) Thanks @junyiz. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. - Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. From baf656bc6fd7f83b6033e6dbc2548ec75028641f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:34:47 +0100 Subject: [PATCH 086/160] fix: block IPv6 multicast SSRF bypass --- CHANGELOG.md | 1 + src/infra/net/ssrf.test.ts | 3 +++ src/shared/net/ip.test.ts | 4 +++- src/shared/net/ip.ts | 1 + 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f82aa875ee7..b55a00e44f21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Docs: https://docs.openclaw.ai ### Fixes - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. +- Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. diff --git a/src/infra/net/ssrf.test.ts b/src/infra/net/ssrf.test.ts index 5826669196db..e823b35be31f 100644 --- a/src/infra/net/ssrf.test.ts +++ b/src/infra/net/ssrf.test.ts @@ -38,6 +38,9 @@ const privateIpCases = [ "fe80::1%lo0", "fd00::1", "fec0::1", + "ff02::1", + "ff05::1:3", + "[ff02::1]", "2001:db8:1234::5efe:127.0.0.1", "2001:db8:1234:1:200:5efe:7f00:1", ]; diff --git a/src/shared/net/ip.test.ts b/src/shared/net/ip.test.ts index 73d385832f0c..a8e4c9bd8e8e 100644 --- a/src/shared/net/ip.test.ts +++ b/src/shared/net/ip.test.ts @@ -45,8 +45,10 @@ describe("shared ip helpers", () => { } }); - it("treats deprecated site-local IPv6 as private/internal", () => { + it("treats blocked IPv6 classes as private/internal", () => { expect(isPrivateOrLoopbackIpAddress("fec0::1")).toBe(true); + expect(isPrivateOrLoopbackIpAddress("ff02::1")).toBe(true); + expect(isPrivateOrLoopbackIpAddress("[ff05::1:3]")).toBe(true); expect(isPrivateOrLoopbackIpAddress("2001:4860:4860::8888")).toBe(false); }); }); diff --git a/src/shared/net/ip.ts b/src/shared/net/ip.ts index 2342bdedafe0..d1f1c0a90698 100644 --- a/src/shared/net/ip.ts +++ b/src/shared/net/ip.ts @@ -27,6 +27,7 @@ const PRIVATE_OR_LOOPBACK_IPV6_RANGES = new Set([ "loopback", "linkLocal", "uniqueLocal", + "multicast", ]); const RFC2544_BENCHMARK_PREFIX: [ipaddr.IPv4, number] = [ipaddr.IPv4.parse("198.18.0.0"), 15]; export type Ipv4SpecialUseBlockOptions = { From 03e689fc89bbecbcd02876a95957ef1ad9caa176 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:40:42 +0100 Subject: [PATCH 087/160] fix(security): bind system.run approvals to argv identity --- CHANGELOG.md | 1 + .../bash-tools.exec-approval-request.ts | 6 ++ src/agents/bash-tools.exec-host-node.ts | 1 + src/agents/tools/nodes-tool.ts | 4 +- src/gateway/exec-approval-manager.ts | 1 + .../node-invoke-system-run-approval.test.ts | 61 ++++++++++++++++++- .../node-invoke-system-run-approval.ts | 17 +++++- src/gateway/protocol/schema/exec-approvals.ts | 1 + src/gateway/server-methods/exec-approval.ts | 5 ++ src/infra/exec-approvals.ts | 1 + src/infra/system-run-command.test.ts | 4 ++ src/infra/system-run-command.ts | 9 ++- 12 files changed, 102 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b55a00e44f21..9874384fd667 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Docs: https://docs.openclaw.ai - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Exec approvals: bind `system.run` approval matching to exact argv identity and preserve argv whitespace in rendered command text, preventing trailing-space executable path swaps from reusing a mismatched approval. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack interactions: enforce channel/DM authorization and modal actor binding (`private_metadata.userId`) before enqueueing `block_action`/`view_submission`/`view_closed` system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. diff --git a/src/agents/bash-tools.exec-approval-request.ts b/src/agents/bash-tools.exec-approval-request.ts index 83323845c0cf..cda30757e269 100644 --- a/src/agents/bash-tools.exec-approval-request.ts +++ b/src/agents/bash-tools.exec-approval-request.ts @@ -8,6 +8,7 @@ import { callGatewayTool } from "./tools/gateway.js"; export type RequestExecApprovalDecisionParams = { id: string; command: string; + commandArgv?: string[]; cwd: string; nodeId?: string; host: "gateway" | "node"; @@ -62,6 +63,7 @@ export async function registerExecApprovalRequest( { id: params.id, command: params.command, + commandArgv: params.commandArgv, cwd: params.cwd, nodeId: params.nodeId, host: params.host, @@ -116,6 +118,7 @@ export async function requestExecApprovalDecision( export async function requestExecApprovalDecisionForHost(params: { approvalId: string; command: string; + commandArgv?: string[]; workdir: string; host: "gateway" | "node"; nodeId?: string; @@ -128,6 +131,7 @@ export async function requestExecApprovalDecisionForHost(params: { return await requestExecApprovalDecision({ id: params.approvalId, command: params.command, + commandArgv: params.commandArgv, cwd: params.workdir, nodeId: params.nodeId, host: params.host, @@ -142,6 +146,7 @@ export async function requestExecApprovalDecisionForHost(params: { export async function registerExecApprovalRequestForHost(params: { approvalId: string; command: string; + commandArgv?: string[]; workdir: string; host: "gateway" | "node"; nodeId?: string; @@ -154,6 +159,7 @@ export async function registerExecApprovalRequestForHost(params: { return await registerExecApprovalRequest({ id: params.approvalId, command: params.command, + commandArgv: params.commandArgv, cwd: params.workdir, nodeId: params.nodeId, host: params.host, diff --git a/src/agents/bash-tools.exec-host-node.ts b/src/agents/bash-tools.exec-host-node.ts index 5a45c8692924..47f2931b980d 100644 --- a/src/agents/bash-tools.exec-host-node.ts +++ b/src/agents/bash-tools.exec-host-node.ts @@ -194,6 +194,7 @@ export async function executeNodeHostCommand( const registration = await registerExecApprovalRequestForHost({ approvalId, command: params.command, + commandArgv: argv, workdir: params.workdir, host: "node", nodeId, diff --git a/src/agents/tools/nodes-tool.ts b/src/agents/tools/nodes-tool.ts index c17ff9f9c488..4cfd84dc4741 100644 --- a/src/agents/tools/nodes-tool.ts +++ b/src/agents/tools/nodes-tool.ts @@ -18,6 +18,7 @@ import { } from "../../cli/nodes-screen.js"; import { parseDurationMs } from "../../cli/parse-duration.js"; import type { OpenClawConfig } from "../../config/config.js"; +import { formatExecCommand } from "../../infra/system-run-command.js"; import { imageMimeFromFormat } from "../../media/mime.js"; import { resolveSessionAgentId } from "../agent-scope.js"; import { resolveImageSanitizationLimits } from "../image-sanitization.js"; @@ -473,7 +474,7 @@ export function createNodesTool(options?: { // Node requires approval – create a pending approval request on // the gateway and wait for the user to approve/deny via the UI. const APPROVAL_TIMEOUT_MS = 120_000; - const cmdText = command.join(" "); + const cmdText = formatExecCommand(command); const approvalId = crypto.randomUUID(); const approvalResult = await callGatewayTool( "exec.approval.request", @@ -481,6 +482,7 @@ export function createNodesTool(options?: { { id: approvalId, command: cmdText, + commandArgv: command, cwd, nodeId, host: "node", diff --git a/src/gateway/exec-approval-manager.ts b/src/gateway/exec-approval-manager.ts index 5e582d42a03a..127d5feae094 100644 --- a/src/gateway/exec-approval-manager.ts +++ b/src/gateway/exec-approval-manager.ts @@ -6,6 +6,7 @@ const RESOLVED_ENTRY_GRACE_MS = 15_000; export type ExecApprovalRequestPayload = { command: string; + commandArgv?: string[] | null; cwd?: string | null; nodeId?: string | null; host?: string | null; diff --git a/src/gateway/node-invoke-system-run-approval.test.ts b/src/gateway/node-invoke-system-run-approval.test.ts index 196b5947f451..c2d3cbe1dfa6 100644 --- a/src/gateway/node-invoke-system-run-approval.test.ts +++ b/src/gateway/node-invoke-system-run-approval.test.ts @@ -13,13 +13,14 @@ describe("sanitizeSystemRunParamsForForwarding", () => { }, }; - function makeRecord(command: string): ExecApprovalRecord { + function makeRecord(command: string, commandArgv?: string[] | null): ExecApprovalRecord { return { id: "approval-1", request: { host: "node", nodeId: "node-1", command, + commandArgv: commandArgv ?? null, cwd: null, agentId: null, sessionKey: null, @@ -139,6 +140,64 @@ describe("sanitizeSystemRunParamsForForwarding", () => { }); expectAllowOnceForwardingResult(result); }); + + test("rejects trailing-space argv mismatch against legacy command-only approval", () => { + const result = sanitizeSystemRunParamsForForwarding({ + rawParams: { + command: ["runner "], + runId: "approval-1", + approved: true, + approvalDecision: "allow-once", + }, + nodeId: "node-1", + client, + execApprovalManager: manager(makeRecord("runner")), + nowMs: now, + }); + expect(result.ok).toBe(false); + if (result.ok) { + throw new Error("unreachable"); + } + expect(result.message).toContain("approval id does not match request"); + expect(result.details?.code).toBe("APPROVAL_REQUEST_MISMATCH"); + }); + + test("enforces commandArgv identity when approval includes argv binding", () => { + const result = sanitizeSystemRunParamsForForwarding({ + rawParams: { + command: ["echo", "SAFE"], + runId: "approval-1", + approved: true, + approvalDecision: "allow-once", + }, + nodeId: "node-1", + client, + execApprovalManager: manager(makeRecord("echo SAFE", ["echo SAFE"])), + nowMs: now, + }); + expect(result.ok).toBe(false); + if (result.ok) { + throw new Error("unreachable"); + } + expect(result.message).toContain("approval id does not match request"); + expect(result.details?.code).toBe("APPROVAL_REQUEST_MISMATCH"); + }); + + test("accepts matching commandArgv binding for trailing-space argv", () => { + const result = sanitizeSystemRunParamsForForwarding({ + rawParams: { + command: ["runner "], + runId: "approval-1", + approved: true, + approvalDecision: "allow-once", + }, + nodeId: "node-1", + client, + execApprovalManager: manager(makeRecord('"runner "', ["runner "])), + nowMs: now, + }); + expectAllowOnceForwardingResult(result); + }); test("consumes allow-once approvals and blocks same runId replay", async () => { const approvalManager = new ExecApprovalManager(); const runId = "approval-replay-1"; diff --git a/src/gateway/node-invoke-system-run-approval.ts b/src/gateway/node-invoke-system-run-approval.ts index d5600adf032a..9623eb1b518a 100644 --- a/src/gateway/node-invoke-system-run-approval.ts +++ b/src/gateway/node-invoke-system-run-approval.ts @@ -55,6 +55,7 @@ function clientHasApprovals(client: ApprovalClient | null): boolean { function approvalMatchesRequest( cmdText: string, + argv: string[], params: SystemRunParamsLike, record: ExecApprovalRecord, ): boolean { @@ -62,7 +63,19 @@ function approvalMatchesRequest( return false; } - if (!cmdText || record.request.command !== cmdText) { + const requestedArgv = Array.isArray(record.request.commandArgv) + ? record.request.commandArgv + : null; + if (requestedArgv) { + if (requestedArgv.length === 0 || requestedArgv.length !== argv.length) { + return false; + } + for (let i = 0; i < requestedArgv.length; i += 1) { + if (requestedArgv[i] !== argv[i]) { + return false; + } + } + } else if (!cmdText || record.request.command !== cmdText) { return false; } @@ -237,7 +250,7 @@ export function sanitizeSystemRunParamsForForwarding(opts: { }; } - if (!approvalMatchesRequest(cmdText, p, snapshot)) { + if (!approvalMatchesRequest(cmdText, cmdTextResolution.argv, p, snapshot)) { return { ok: false, message: "approval id does not match request", diff --git a/src/gateway/protocol/schema/exec-approvals.ts b/src/gateway/protocol/schema/exec-approvals.ts index a7c5fcf09bba..1482ae4cfef8 100644 --- a/src/gateway/protocol/schema/exec-approvals.ts +++ b/src/gateway/protocol/schema/exec-approvals.ts @@ -89,6 +89,7 @@ export const ExecApprovalRequestParamsSchema = Type.Object( { id: Type.Optional(NonEmptyString), command: NonEmptyString, + commandArgv: Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()])), cwd: Type.Optional(Type.Union([Type.String(), Type.Null()])), nodeId: Type.Optional(Type.Union([NonEmptyString, Type.Null()])), host: Type.Optional(Type.Union([Type.String(), Type.Null()])), diff --git a/src/gateway/server-methods/exec-approval.ts b/src/gateway/server-methods/exec-approval.ts index d1cfc9ec0d99..555348bc777a 100644 --- a/src/gateway/server-methods/exec-approval.ts +++ b/src/gateway/server-methods/exec-approval.ts @@ -43,6 +43,7 @@ export function createExecApprovalHandlers( const p = params as { id?: string; command: string; + commandArgv?: string[] | null; cwd?: string; nodeId?: string; host?: string; @@ -60,6 +61,9 @@ export function createExecApprovalHandlers( const explicitId = typeof p.id === "string" && p.id.trim().length > 0 ? p.id.trim() : null; const host = typeof p.host === "string" ? p.host.trim() : ""; const nodeId = typeof p.nodeId === "string" ? p.nodeId.trim() : ""; + const commandArgv = Array.isArray(p.commandArgv) + ? p.commandArgv.map((entry) => String(entry)) + : null; if (host === "node" && !nodeId) { respond( false, @@ -78,6 +82,7 @@ export function createExecApprovalHandlers( } const request = { command: p.command, + commandArgv, cwd: p.cwd ?? null, nodeId: host === "node" ? nodeId : null, host: host || null, diff --git a/src/infra/exec-approvals.ts b/src/infra/exec-approvals.ts index be4264e22ecd..688972d83611 100644 --- a/src/infra/exec-approvals.ts +++ b/src/infra/exec-approvals.ts @@ -15,6 +15,7 @@ export type ExecApprovalRequest = { id: string; request: { command: string; + commandArgv?: string[] | null; cwd?: string | null; nodeId?: string | null; host?: string | null; diff --git a/src/infra/system-run-command.test.ts b/src/infra/system-run-command.test.ts index 7186823d84b3..7f7d4fee96c2 100644 --- a/src/infra/system-run-command.test.ts +++ b/src/infra/system-run-command.test.ts @@ -21,6 +21,10 @@ describe("system run command helpers", () => { expect(formatExecCommand(["echo", "hi there"])).toBe('echo "hi there"'); }); + test("formatExecCommand preserves trailing whitespace in argv tokens", () => { + expect(formatExecCommand(["runner "])).toBe('"runner "'); + }); + test("extractShellCommandFromArgv extracts sh -lc command", () => { expect(extractShellCommandFromArgv(["/bin/sh", "-lc", "echo hi"])).toBe("echo hi"); }); diff --git a/src/infra/system-run-command.ts b/src/infra/system-run-command.ts index b03d715fc72d..dc54bf7b5619 100644 --- a/src/infra/system-run-command.ts +++ b/src/infra/system-run-command.ts @@ -35,15 +35,14 @@ export type ResolvedSystemRunCommand = export function formatExecCommand(argv: string[]): string { return argv .map((arg) => { - const trimmed = arg.trim(); - if (!trimmed) { + if (arg.length === 0) { return '""'; } - const needsQuotes = /\s|"/.test(trimmed); + const needsQuotes = /\s|"/.test(arg); if (!needsQuotes) { - return trimmed; + return arg; } - return `"${trimmed.replace(/"/g, '\\"')}"`; + return `"${arg.replace(/"/g, '\\"')}"`; }) .join(" "); } From 53fcfdf794bb3e001bea542fe597847529e2f05e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:41:58 +0100 Subject: [PATCH 088/160] fix(telegram): preserve finalized previews on mixed text+voice turns --- CHANGELOG.md | 1 + src/telegram/bot-message-dispatch.test.ts | 46 +++++++++++++++++++++++ src/telegram/bot-message-dispatch.ts | 7 +++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9874384fd667..afed51965f9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ Docs: https://docs.openclaw.ai - Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. - Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. - Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. +- Telegram/Preview cleanup: keep finalized text previews when a later assistant message is media-only (for example mixed text plus voice turns) by skipping finalized preview archival at assistant-message boundaries, preventing cleanup from deleting already-visible final text messages. (#27042) - Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example `c0abc12345`) correctly match Slack runtime IDs (`C0ABC12345`) under `groupPolicy: "allowlist"`, preventing silent channel-event drops. (#26878) Thanks @lbo728. - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. diff --git a/src/telegram/bot-message-dispatch.test.ts b/src/telegram/bot-message-dispatch.test.ts index 75a8fb6b9af5..7e82adafec22 100644 --- a/src/telegram/bot-message-dispatch.test.ts +++ b/src/telegram/bot-message-dispatch.test.ts @@ -691,6 +691,52 @@ describe("dispatchTelegramMessage draft streaming", () => { expect(deliverReplies).not.toHaveBeenCalled(); }); + it.each(["partial", "block"] as const)( + "keeps finalized text preview when the next assistant message is media-only (%s mode)", + async (streamMode) => { + let answerMessageId: number | undefined = 1001; + const answerDraftStream = { + update: vi.fn(), + flush: vi.fn().mockResolvedValue(undefined), + messageId: vi.fn().mockImplementation(() => answerMessageId), + clear: vi.fn().mockResolvedValue(undefined), + stop: vi.fn().mockResolvedValue(undefined), + forceNewMessage: vi.fn().mockImplementation(() => { + answerMessageId = undefined; + }), + }; + const reasoningDraftStream = createDraftStream(); + createTelegramDraftStream + .mockImplementationOnce(() => answerDraftStream) + .mockImplementationOnce(() => reasoningDraftStream); + dispatchReplyWithBufferedBlockDispatcher.mockImplementation( + async ({ dispatcherOptions, replyOptions }) => { + await replyOptions?.onPartialReply?.({ text: "First message preview" }); + await dispatcherOptions.deliver({ text: "First message final" }, { kind: "final" }); + await replyOptions?.onAssistantMessageStart?.(); + await dispatcherOptions.deliver({ mediaUrl: "file:///tmp/voice.ogg" }, { kind: "final" }); + return { queuedFinal: true }; + }, + ); + deliverReplies.mockResolvedValue({ delivered: true }); + editMessageTelegram.mockResolvedValue({ ok: true, chatId: "123", messageId: "1001" }); + const bot = createBot(); + + await dispatchWithContext({ context: createContext(), streamMode, bot }); + + expect(editMessageTelegram).toHaveBeenCalledWith( + 123, + 1001, + "First message final", + expect.any(Object), + ); + const deleteMessageCalls = ( + bot.api as unknown as { deleteMessage: { mock: { calls: unknown[][] } } } + ).deleteMessage.mock.calls; + expect(deleteMessageCalls).not.toContainEqual([123, 1001]); + }, + ); + it("maps finals correctly when archived preview id arrives during final flush", async () => { let answerMessageId: number | undefined; let answerDraftParams: diff --git a/src/telegram/bot-message-dispatch.ts b/src/telegram/bot-message-dispatch.ts index f45b79fb9abd..5b000a8dcd08 100644 --- a/src/telegram/bot-message-dispatch.ts +++ b/src/telegram/bot-message-dispatch.ts @@ -567,7 +567,10 @@ export const dispatchTelegramMessage = async ({ reasoningStepState.resetForNextStep(); if (answerLane.hasStreamedMessage) { const previewMessageId = answerLane.stream?.messageId(); - if (typeof previewMessageId === "number") { + // Only archive previews that still need a matching final text update. + // Once a preview has already been finalized, archiving it here causes + // cleanup to delete a user-visible final message on later media-only turns. + if (typeof previewMessageId === "number" && !finalizedPreviewByLane.answer) { archivedAnswerPreviews.push({ messageId: previewMessageId, textSnapshot: answerLane.lastPartialText, @@ -576,6 +579,8 @@ export const dispatchTelegramMessage = async ({ answerLane.stream?.forceNewMessage(); } resetDraftLaneState(answerLane); + // New assistant message boundary: this lane now tracks a fresh preview lifecycle. + finalizedPreviewByLane.answer = false; } : undefined, onReasoningEnd: reasoningLane.stream From 04d91d0319b82fd4de91ed05e9fc5219ff2ab64e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:42:22 +0100 Subject: [PATCH 089/160] fix(security): block workspace hardlink alias escapes --- CHANGELOG.md | 1 + src/agents/apply-patch.test.ts | 36 +++++++++++++++++++ src/agents/apply-patch.ts | 2 ++ src/agents/pi-tools.workspace-paths.test.ts | 40 +++++++++++++++++++++ src/agents/sandbox-paths.ts | 34 +++++++----------- src/agents/sandbox/fs-bridge.test.ts | 36 +++++++++++++++++++ src/agents/sandbox/fs-bridge.ts | 10 ++++++ src/infra/hardlink-guards.ts | 38 ++++++++++++++++++++ 8 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 src/infra/hardlink-guards.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index afed51965f9a..cba652419f45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ Docs: https://docs.openclaw.ai - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. +- Security/Workspace FS: reject hardlinked workspace file aliases in `tools.fs.workspaceOnly` and `tools.exec.applyPatch.workspaceOnly` boundary checks (including sandbox mount-root guards) to prevent out-of-workspace read/write via in-workspace hardlink paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. diff --git a/src/agents/apply-patch.test.ts b/src/agents/apply-patch.test.ts index 5a2dae87e757..79d0aa0c07b5 100644 --- a/src/agents/apply-patch.test.ts +++ b/src/agents/apply-patch.test.ts @@ -159,6 +159,42 @@ describe("applyPatch", () => { }); }); + it("rejects hardlink alias escapes by default", async () => { + if (process.platform === "win32") { + return; + } + await withTempDir(async (dir) => { + const outside = path.join( + path.dirname(dir), + `outside-hardlink-${process.pid}-${Date.now()}.txt`, + ); + const linkPath = path.join(dir, "hardlink.txt"); + await fs.writeFile(outside, "initial\n", "utf8"); + try { + try { + await fs.link(outside, linkPath); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "EXDEV") { + return; + } + throw err; + } + const patch = `*** Begin Patch +*** Update File: hardlink.txt +@@ +-initial ++pwned +*** End Patch`; + await expect(applyPatch(patch, { cwd: dir })).rejects.toThrow(/hardlink|sandbox/i); + const outsideContents = await fs.readFile(outside, "utf8"); + expect(outsideContents).toBe("initial\n"); + } finally { + await fs.rm(linkPath, { force: true }); + await fs.rm(outside, { force: true }); + } + }); + }); + it("allows symlinks that resolve within cwd by default", async () => { await withTempDir(async (dir) => { const target = path.join(dir, "target.txt"); diff --git a/src/agents/apply-patch.ts b/src/agents/apply-patch.ts index fecf4cf03bc1..4b147fd79fb1 100644 --- a/src/agents/apply-patch.ts +++ b/src/agents/apply-patch.ts @@ -266,6 +266,7 @@ async function resolvePatchPath( cwd: options.cwd, root: options.cwd, allowFinalSymlink: purpose === "unlink", + allowFinalHardlink: purpose === "unlink", }); } return { @@ -282,6 +283,7 @@ async function resolvePatchPath( cwd: options.cwd, root: options.cwd, allowFinalSymlink: purpose === "unlink", + allowFinalHardlink: purpose === "unlink", }) ).resolved : resolvePathFromCwd(filePath, options.cwd); diff --git a/src/agents/pi-tools.workspace-paths.test.ts b/src/agents/pi-tools.workspace-paths.test.ts index 6fe98ff03f8f..4efa494555eb 100644 --- a/src/agents/pi-tools.workspace-paths.test.ts +++ b/src/agents/pi-tools.workspace-paths.test.ts @@ -151,6 +151,46 @@ describe("workspace path resolution", () => { ).rejects.toThrow(/Path escapes sandbox root/i); }); }); + + it("rejects hardlinked file aliases when workspaceOnly is enabled", async () => { + if (process.platform === "win32") { + return; + } + await withTempDir("openclaw-ws-", async (workspaceDir) => { + const cfg: OpenClawConfig = { tools: { fs: { workspaceOnly: true } } }; + const tools = createOpenClawCodingTools({ workspaceDir, config: cfg }); + const { readTool, writeTool } = expectReadWriteEditTools(tools); + const outsidePath = path.join( + path.dirname(workspaceDir), + `outside-hardlink-${process.pid}-${Date.now()}.txt`, + ); + const hardlinkPath = path.join(workspaceDir, "linked.txt"); + await fs.writeFile(outsidePath, "top-secret", "utf8"); + try { + try { + await fs.link(outsidePath, hardlinkPath); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "EXDEV") { + return; + } + throw err; + } + await expect(readTool.execute("ws-read-hardlink", { path: "linked.txt" })).rejects.toThrow( + /hardlink|sandbox/i, + ); + await expect( + writeTool.execute("ws-write-hardlink", { + path: "linked.txt", + content: "pwned", + }), + ).rejects.toThrow(/hardlink|sandbox/i); + expect(await fs.readFile(outsidePath, "utf8")).toBe("top-secret"); + } finally { + await fs.rm(hardlinkPath, { force: true }); + await fs.rm(outsidePath, { force: true }); + } + }); + }); }); describe("sandboxed workspace paths", () => { diff --git a/src/agents/sandbox-paths.ts b/src/agents/sandbox-paths.ts index 761106e85740..b50e90c32415 100644 --- a/src/agents/sandbox-paths.ts +++ b/src/agents/sandbox-paths.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath, URL } from "node:url"; +import { assertNoHardlinkedFinalPath } from "../infra/hardlink-guards.js"; import { isNotFoundPathError, isPathInside } from "../infra/path-guards.js"; import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js"; @@ -62,11 +63,18 @@ export async function assertSandboxPath(params: { cwd: string; root: string; allowFinalSymlink?: boolean; + allowFinalHardlink?: boolean; }) { const resolved = resolveSandboxPath(params); await assertNoSymlinkEscape(resolved.relative, path.resolve(params.root), { allowFinalSymlink: params.allowFinalSymlink, }); + await assertNoHardlinkedFinalPath({ + filePath: resolved.resolved, + root: path.resolve(params.root), + boundaryLabel: "sandbox root", + allowFinalHardlink: params.allowFinalHardlink, + }); return resolved; } @@ -195,27 +203,11 @@ async function assertNoTmpAliasEscape(params: { tmpRoot: string; }): Promise { await assertNoSymlinkEscape(path.relative(params.tmpRoot, params.filePath), params.tmpRoot); - await assertNoHardlinkedFinalPath(params.filePath, params.tmpRoot); -} - -async function assertNoHardlinkedFinalPath(filePath: string, tmpRoot: string): Promise { - let stat: Awaited>; - try { - stat = await fs.stat(filePath); - } catch (err) { - if (isNotFoundPathError(err)) { - return; - } - throw err; - } - if (!stat.isFile()) { - return; - } - if (stat.nlink > 1) { - throw new Error( - `Hardlinked tmp media path is not allowed under tmp root (${shortPath(tmpRoot)}): ${shortPath(filePath)}`, - ); - } + await assertNoHardlinkedFinalPath({ + filePath: params.filePath, + root: params.tmpRoot, + boundaryLabel: "tmp root", + }); } async function assertNoSymlinkEscape( diff --git a/src/agents/sandbox/fs-bridge.test.ts b/src/agents/sandbox/fs-bridge.test.ts index d3bcd735e9ea..f5c9aaedd6da 100644 --- a/src/agents/sandbox/fs-bridge.test.ts +++ b/src/agents/sandbox/fs-bridge.test.ts @@ -195,6 +195,42 @@ describe("sandbox fs bridge shell compatibility", () => { await fs.rm(stateDir, { recursive: true, force: true }); }); + it("rejects pre-existing host hardlink escapes before docker exec", async () => { + if (process.platform === "win32") { + return; + } + const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-fs-bridge-hardlink-")); + const workspaceDir = path.join(stateDir, "workspace"); + const outsideDir = path.join(stateDir, "outside"); + const outsideFile = path.join(outsideDir, "secret.txt"); + await fs.mkdir(workspaceDir, { recursive: true }); + await fs.mkdir(outsideDir, { recursive: true }); + await fs.writeFile(outsideFile, "classified"); + const hardlinkPath = path.join(workspaceDir, "link.txt"); + try { + try { + await fs.link(outsideFile, hardlinkPath); + } catch (err) { + if ((err as NodeJS.ErrnoException).code === "EXDEV") { + return; + } + throw err; + } + + const bridge = createSandboxFsBridge({ + sandbox: createSandbox({ + workspaceDir, + agentWorkspaceDir: workspaceDir, + }), + }); + + await expect(bridge.readFile({ filePath: "link.txt" })).rejects.toThrow(/hardlink|sandbox/i); + expect(mockedExecDockerRaw).not.toHaveBeenCalled(); + } finally { + await fs.rm(stateDir, { recursive: true, force: true }); + } + }); + it("rejects container-canonicalized paths outside allowed mounts", async () => { mockedExecDockerRaw.mockImplementation(async (args) => { const script = getDockerScript(args); diff --git a/src/agents/sandbox/fs-bridge.ts b/src/agents/sandbox/fs-bridge.ts index 226fc39ca1d4..18991f60da6d 100644 --- a/src/agents/sandbox/fs-bridge.ts +++ b/src/agents/sandbox/fs-bridge.ts @@ -1,5 +1,6 @@ import fs from "node:fs/promises"; import path from "node:path"; +import { assertNoHardlinkedFinalPath } from "../../infra/hardlink-guards.js"; import { isNotFoundPathError, isPathInside } from "../../infra/path-guards.js"; import { execDockerRaw, type ExecDockerRawResult } from "./docker.js"; import { @@ -21,6 +22,7 @@ type RunCommandOptions = { type PathSafetyOptions = { action: string; allowFinalSymlink?: boolean; + allowFinalHardlink?: boolean; requireWritable?: boolean; }; @@ -151,6 +153,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { action: "remove files", requireWritable: true, allowFinalSymlink: true, + allowFinalHardlink: true, }); const flags = [params.force === false ? "" : "-f", params.recursive ? "-r" : ""].filter( Boolean, @@ -176,6 +179,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { action: "rename files", requireWritable: true, allowFinalSymlink: true, + allowFinalHardlink: true, }); await this.assertPathSafety(to, { action: "rename files", @@ -257,6 +261,12 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { rootPath: lexicalMount.hostRoot, allowFinalSymlink: options.allowFinalSymlink === true, }); + await assertNoHardlinkedFinalPath({ + filePath: target.hostPath, + root: lexicalMount.hostRoot, + boundaryLabel: "sandbox mount root", + allowFinalHardlink: options.allowFinalHardlink === true, + }); const canonicalContainerPath = await this.resolveCanonicalContainerPath({ containerPath: target.containerPath, diff --git a/src/infra/hardlink-guards.ts b/src/infra/hardlink-guards.ts new file mode 100644 index 000000000000..9681bc09b785 --- /dev/null +++ b/src/infra/hardlink-guards.ts @@ -0,0 +1,38 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import { isNotFoundPathError } from "./path-guards.js"; + +export async function assertNoHardlinkedFinalPath(params: { + filePath: string; + root: string; + boundaryLabel: string; + allowFinalHardlink?: boolean; +}): Promise { + if (params.allowFinalHardlink) { + return; + } + let stat: Awaited>; + try { + stat = await fs.stat(params.filePath); + } catch (err) { + if (isNotFoundPathError(err)) { + return; + } + throw err; + } + if (!stat.isFile()) { + return; + } + if (stat.nlink > 1) { + throw new Error( + `Hardlinked path is not allowed under ${params.boundaryLabel} (${shortPath(params.root)}): ${shortPath(params.filePath)}`, + ); + } +} + +function shortPath(value: string) { + if (value.startsWith(os.homedir())) { + return `~${value.slice(os.homedir().length)}`; + } + return value; +} From 61b3246a7f44d2da498844e4cb448e5574253d0e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:43:30 +0100 Subject: [PATCH 090/160] fix(ssrf): unify ipv6 special-use blocking --- src/infra/net/fetch-guard.ssrf.test.ts | 1 + src/infra/net/ssrf.test.ts | 5 ++--- src/infra/net/ssrf.ts | 4 ++-- src/shared/net/ip-test-fixtures.ts | 1 + src/shared/net/ip.test.ts | 6 ++++-- src/shared/net/ip.ts | 10 +++++++--- 6 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 src/shared/net/ip-test-fixtures.ts diff --git a/src/infra/net/fetch-guard.ssrf.test.ts b/src/infra/net/fetch-guard.ssrf.test.ts index a03afba325f6..223695c1a53f 100644 --- a/src/infra/net/fetch-guard.ssrf.test.ts +++ b/src/infra/net/fetch-guard.ssrf.test.ts @@ -18,6 +18,7 @@ describe("fetchWithSsrFGuard hardening", () => { it("blocks private and legacy loopback literals before fetch", async () => { const blockedUrls = [ "http://127.0.0.1:8080/internal", + "http://[ff02::1]/internal", "http://0177.0.0.1:8080/internal", "http://0x7f000001/internal", ]; diff --git a/src/infra/net/ssrf.test.ts b/src/infra/net/ssrf.test.ts index e823b35be31f..2698bf3db9e9 100644 --- a/src/infra/net/ssrf.test.ts +++ b/src/infra/net/ssrf.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { blockedIpv6MulticastLiterals } from "../../shared/net/ip-test-fixtures.js"; import { normalizeFingerprint } from "../tls/fingerprint.js"; import { isBlockedHostnameOrIp, isPrivateIpAddress } from "./ssrf.js"; @@ -38,9 +39,7 @@ const privateIpCases = [ "fe80::1%lo0", "fd00::1", "fec0::1", - "ff02::1", - "ff05::1:3", - "[ff02::1]", + ...blockedIpv6MulticastLiterals, "2001:db8:1234::5efe:127.0.0.1", "2001:db8:1234:1:200:5efe:7f00:1", ]; diff --git a/src/infra/net/ssrf.ts b/src/infra/net/ssrf.ts index b84469390c08..8ba29b38e2ae 100644 --- a/src/infra/net/ssrf.ts +++ b/src/infra/net/ssrf.ts @@ -4,11 +4,11 @@ import { Agent, type Dispatcher } from "undici"; import { extractEmbeddedIpv4FromIpv6, isBlockedSpecialUseIpv4Address, + isBlockedSpecialUseIpv6Address, isCanonicalDottedDecimalIPv4, type Ipv4SpecialUseBlockOptions, isIpv4Address, isLegacyIpv4Literal, - isPrivateOrLoopbackIpAddress, parseCanonicalIpAddress, parseLooseIpAddress, } from "../../shared/net/ip.js"; @@ -120,7 +120,7 @@ export function isPrivateIpAddress(address: string, policy?: SsrFPolicy): boolea if (isIpv4Address(strictIp)) { return isBlockedSpecialUseIpv4Address(strictIp, blockOptions); } - if (isPrivateOrLoopbackIpAddress(strictIp.toString())) { + if (isBlockedSpecialUseIpv6Address(strictIp)) { return true; } const embeddedIpv4 = extractEmbeddedIpv4FromIpv6(strictIp); diff --git a/src/shared/net/ip-test-fixtures.ts b/src/shared/net/ip-test-fixtures.ts new file mode 100644 index 000000000000..d2fa9cd5436c --- /dev/null +++ b/src/shared/net/ip-test-fixtures.ts @@ -0,0 +1 @@ +export const blockedIpv6MulticastLiterals = ["ff02::1", "ff05::1:3", "[ff02::1]"] as const; diff --git a/src/shared/net/ip.test.ts b/src/shared/net/ip.test.ts index a8e4c9bd8e8e..f89fb03f7ef2 100644 --- a/src/shared/net/ip.test.ts +++ b/src/shared/net/ip.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { blockedIpv6MulticastLiterals } from "./ip-test-fixtures.js"; import { extractEmbeddedIpv4FromIpv6, isCanonicalDottedDecimalIPv4, @@ -47,8 +48,9 @@ describe("shared ip helpers", () => { it("treats blocked IPv6 classes as private/internal", () => { expect(isPrivateOrLoopbackIpAddress("fec0::1")).toBe(true); - expect(isPrivateOrLoopbackIpAddress("ff02::1")).toBe(true); - expect(isPrivateOrLoopbackIpAddress("[ff05::1:3]")).toBe(true); + for (const literal of blockedIpv6MulticastLiterals) { + expect(isPrivateOrLoopbackIpAddress(literal)).toBe(true); + } expect(isPrivateOrLoopbackIpAddress("2001:4860:4860::8888")).toBe(false); }); }); diff --git a/src/shared/net/ip.ts b/src/shared/net/ip.ts index d1f1c0a90698..c386c6878984 100644 --- a/src/shared/net/ip.ts +++ b/src/shared/net/ip.ts @@ -22,7 +22,7 @@ const PRIVATE_OR_LOOPBACK_IPV4_RANGES = new Set([ "carrierGradeNat", ]); -const PRIVATE_OR_LOOPBACK_IPV6_RANGES = new Set([ +const BLOCKED_IPV6_SPECIAL_USE_RANGES = new Set([ "unspecified", "loopback", "linkLocal", @@ -228,11 +228,15 @@ export function isPrivateOrLoopbackIpAddress(raw: string | undefined): boolean { if (isIpv4Address(normalized)) { return PRIVATE_OR_LOOPBACK_IPV4_RANGES.has(normalized.range()); } - if (PRIVATE_OR_LOOPBACK_IPV6_RANGES.has(normalized.range())) { + return isBlockedSpecialUseIpv6Address(normalized); +} + +export function isBlockedSpecialUseIpv6Address(address: ipaddr.IPv6): boolean { + if (BLOCKED_IPV6_SPECIAL_USE_RANGES.has(address.range())) { return true; } // ipaddr.js does not classify deprecated site-local fec0::/10 as private. - return (normalized.parts[0] & 0xffc0) === 0xfec0; + return (address.parts[0] & 0xffc0) === 0xfec0; } export function isRfc1918Ipv4Address(raw: string | undefined): boolean { From 75dfb71e4e8b7c2feba5a8ca662f92ea840e0147 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:47:52 +0100 Subject: [PATCH 091/160] fix(slack): gate pin/reaction system events by sender auth --- CHANGELOG.md | 3 +- src/slack/monitor/events/pins.test.ts | 170 +++++++++++++++++++++ src/slack/monitor/events/pins.ts | 24 +-- src/slack/monitor/events/reactions.test.ts | 30 ++++ src/slack/monitor/events/reactions.ts | 53 ++----- 5 files changed, 227 insertions(+), 53 deletions(-) create mode 100644 src/slack/monitor/events/pins.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index cba652419f45..6a46b2779420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,8 @@ Docs: https://docs.openclaw.ai - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Exec approvals: bind `system.run` approval matching to exact argv identity and preserve argv whitespace in rendered command text, preventing trailing-space executable path swaps from reusing a mismatched approval. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Security/Discord + Slack reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Discord reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Slack reactions + pins: gate `reaction_*` and `pin_*` system-event enqueue through shared sender authorization so DM `dmPolicy`/`allowFrom` and channel `users` allowlists are enforced consistently for non-message ingress, with regression coverage for denied/allowed sender paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack interactions: enforce channel/DM authorization and modal actor binding (`private_metadata.userId`) before enqueueing `block_action`/`view_submission`/`view_closed` system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. diff --git a/src/slack/monitor/events/pins.test.ts b/src/slack/monitor/events/pins.test.ts new file mode 100644 index 000000000000..3bdae247613f --- /dev/null +++ b/src/slack/monitor/events/pins.test.ts @@ -0,0 +1,170 @@ +import { describe, expect, it, vi } from "vitest"; +import type { SlackMonitorContext } from "../context.js"; +import { registerSlackPinEvents } from "./pins.js"; + +const enqueueSystemEventMock = vi.fn(); +const readAllowFromStoreMock = vi.fn(); + +vi.mock("../../../infra/system-events.js", () => ({ + enqueueSystemEvent: (...args: unknown[]) => enqueueSystemEventMock(...args), +})); + +vi.mock("../../../pairing/pairing-store.js", () => ({ + readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args), +})); + +type SlackPinHandler = (args: { event: Record; body: unknown }) => Promise; + +function createPinContext(overrides?: { + dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom?: string[]; + channelType?: "im" | "channel"; + channelUsers?: string[]; +}) { + let addedHandler: SlackPinHandler | null = null; + let removedHandler: SlackPinHandler | null = null; + const channelType = overrides?.channelType ?? "im"; + const app = { + event: vi.fn((name: string, handler: SlackPinHandler) => { + if (name === "pin_added") { + addedHandler = handler; + } else if (name === "pin_removed") { + removedHandler = handler; + } + }), + }; + const ctx = { + app, + runtime: { error: vi.fn() }, + dmEnabled: true, + dmPolicy: overrides?.dmPolicy ?? "open", + defaultRequireMention: true, + channelsConfig: overrides?.channelUsers + ? { + C1: { + users: overrides.channelUsers, + allow: true, + }, + } + : undefined, + groupPolicy: "open", + allowFrom: overrides?.allowFrom ?? [], + allowNameMatching: false, + shouldDropMismatchedSlackEvent: vi.fn().mockReturnValue(false), + isChannelAllowed: vi.fn().mockReturnValue(true), + resolveChannelName: vi.fn().mockResolvedValue({ + name: channelType === "im" ? "direct" : "general", + type: channelType, + }), + resolveUserName: vi.fn().mockResolvedValue({ name: "alice" }), + resolveSlackSystemEventSessionKey: vi.fn().mockReturnValue("agent:main:main"), + } as unknown as SlackMonitorContext; + registerSlackPinEvents({ ctx }); + return { + ctx, + getAddedHandler: () => addedHandler, + getRemovedHandler: () => removedHandler, + }; +} + +function makePinEvent(overrides?: { user?: string; channel?: string }) { + return { + type: "pin_added", + user: overrides?.user ?? "U1", + channel_id: overrides?.channel ?? "D1", + event_ts: "123.456", + item: { + type: "message", + message: { + ts: "123.456", + }, + }, + }; +} + +describe("registerSlackPinEvents", () => { + it("enqueues DM pin system events when dmPolicy is open", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createPinContext({ dmPolicy: "open" }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makePinEvent(), + body: {}, + }); + + expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); + }); + + it("blocks DM pin system events when dmPolicy is disabled", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createPinContext({ dmPolicy: "disabled" }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makePinEvent(), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + + it("blocks DM pin system events for unauthorized senders in allowlist mode", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createPinContext({ + dmPolicy: "allowlist", + allowFrom: ["U2"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makePinEvent({ user: "U1" }), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); + + it("allows DM pin system events for authorized senders in allowlist mode", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createPinContext({ + dmPolicy: "allowlist", + allowFrom: ["U1"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makePinEvent({ user: "U1" }), + body: {}, + }); + + expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); + }); + + it("blocks channel pin events for users outside channel users allowlist", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createPinContext({ + dmPolicy: "open", + channelType: "channel", + channelUsers: ["U_OWNER"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makePinEvent({ channel: "C1", user: "U_ATTACKER" }), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); +}); diff --git a/src/slack/monitor/events/pins.ts b/src/slack/monitor/events/pins.ts index 2613bc35e24a..89d0e2264e8c 100644 --- a/src/slack/monitor/events/pins.ts +++ b/src/slack/monitor/events/pins.ts @@ -1,6 +1,7 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt"; -import { danger } from "../../../globals.js"; +import { danger, logVerbose } from "../../../globals.js"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; +import { authorizeSlackSystemEventSender } from "../auth.js"; import { resolveSlackChannelLabel } from "../channel-config.js"; import type { SlackMonitorContext } from "../context.js"; import type { SlackPinEvent } from "../types.js"; @@ -22,19 +23,20 @@ async function handleSlackPinEvent(params: { const payload = event as SlackPinEvent; const channelId = payload.channel_id; - const channelInfo = channelId ? await ctx.resolveChannelName(channelId) : {}; - if ( - !ctx.isChannelAllowed({ - channelId, - channelName: channelInfo?.name, - channelType: channelInfo?.type, - }) - ) { + const auth = await authorizeSlackSystemEventSender({ + ctx, + senderId: payload.user, + channelId, + }); + if (!auth.allowed) { + logVerbose( + `slack: drop pin sender ${payload.user ?? "unknown"} channel=${channelId ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, + ); return; } const label = resolveSlackChannelLabel({ channelId, - channelName: channelInfo?.name, + channelName: auth.channelName, }); const userInfo = payload.user ? await ctx.resolveUserName(payload.user) : {}; const userLabel = userInfo?.name ?? payload.user ?? "someone"; @@ -42,7 +44,7 @@ async function handleSlackPinEvent(params: { const messageId = payload.item?.message?.ts ?? payload.event_ts; const sessionKey = ctx.resolveSlackSystemEventSessionKey({ channelId, - channelType: channelInfo?.type ?? undefined, + channelType: auth.channelType, }); enqueueSystemEvent(`Slack: ${userLabel} ${action} a ${itemType} in ${label}.`, { sessionKey, diff --git a/src/slack/monitor/events/reactions.test.ts b/src/slack/monitor/events/reactions.test.ts index 815ca1c65b2c..bb64fbb5b4a6 100644 --- a/src/slack/monitor/events/reactions.test.ts +++ b/src/slack/monitor/events/reactions.test.ts @@ -22,6 +22,7 @@ function createReactionContext(overrides?: { dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; allowFrom?: string[]; channelType?: "im" | "channel"; + channelUsers?: string[]; }) { let addedHandler: SlackReactionHandler | null = null; let removedHandler: SlackReactionHandler | null = null; @@ -38,7 +39,17 @@ function createReactionContext(overrides?: { const ctx = { app, runtime: { error: vi.fn() }, + dmEnabled: true, dmPolicy: overrides?.dmPolicy ?? "open", + defaultRequireMention: true, + channelsConfig: overrides?.channelUsers + ? { + C1: { + users: overrides.channelUsers, + allow: true, + }, + } + : undefined, groupPolicy: "open", allowFrom: overrides?.allowFrom ?? [], allowNameMatching: false, @@ -160,4 +171,23 @@ describe("registerSlackReactionEvents", () => { expect(enqueueSystemEventMock).toHaveBeenCalledTimes(1); }); + + it("blocks channel reaction events for users outside channel users allowlist", async () => { + enqueueSystemEventMock.mockClear(); + readAllowFromStoreMock.mockReset().mockResolvedValue([]); + const { getAddedHandler } = createReactionContext({ + dmPolicy: "open", + channelType: "channel", + channelUsers: ["U_OWNER"], + }); + const addedHandler = getAddedHandler(); + expect(addedHandler).toBeTruthy(); + + await addedHandler!({ + event: makeReactionEvent({ channel: "C1", user: "U_ATTACKER" }), + body: {}, + }); + + expect(enqueueSystemEventMock).not.toHaveBeenCalled(); + }); }); diff --git a/src/slack/monitor/events/reactions.ts b/src/slack/monitor/events/reactions.ts index 5007c6aad939..844b6c94080f 100644 --- a/src/slack/monitor/events/reactions.ts +++ b/src/slack/monitor/events/reactions.ts @@ -1,9 +1,7 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt"; import { danger, logVerbose } from "../../../globals.js"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; -import { resolveDmGroupAccessWithLists } from "../../../security/dm-policy-shared.js"; -import { resolveSlackAllowListMatch } from "../allow-list.js"; -import { resolveSlackEffectiveAllowFrom } from "../auth.js"; +import { authorizeSlackSystemEventSender } from "../auth.js"; import { resolveSlackChannelLabel } from "../channel-config.js"; import type { SlackMonitorContext } from "../context.js"; import type { SlackReactionEvent } from "../types.js"; @@ -18,50 +16,23 @@ export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext } return; } - const channelInfo = item.channel ? await ctx.resolveChannelName(item.channel) : {}; - const channelType = channelInfo?.type; - if ( - !ctx.isChannelAllowed({ - channelId: item.channel, - channelName: channelInfo?.name, - channelType, - }) - ) { + const auth = await authorizeSlackSystemEventSender({ + ctx, + senderId: event.user, + channelId: item.channel, + }); + if (!auth.allowed) { + logVerbose( + `slack: drop reaction sender ${event.user ?? "unknown"} channel=${item.channel ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, + ); return; } const channelLabel = resolveSlackChannelLabel({ channelId: item.channel, - channelName: channelInfo?.name, + channelName: auth.channelName, }); const actorInfo = event.user ? await ctx.resolveUserName(event.user) : undefined; - if (channelType === "im") { - if (!event.user) { - return; - } - const { allowFromLower } = await resolveSlackEffectiveAllowFrom(ctx); - const access = resolveDmGroupAccessWithLists({ - isGroup: false, - dmPolicy: ctx.dmPolicy, - groupPolicy: ctx.groupPolicy, - allowFrom: allowFromLower, - groupAllowFrom: [], - storeAllowFrom: [], - isSenderAllowed: (allowList) => - resolveSlackAllowListMatch({ - allowList, - id: event.user, - name: actorInfo?.name, - allowNameMatching: ctx.allowNameMatching, - }).allowed, - }); - if (access.decision !== "allow") { - logVerbose( - `slack: drop reaction sender ${event.user} (dmPolicy=${ctx.dmPolicy}, decision=${access.decision}, reason=${access.reason})`, - ); - return; - } - } const actorLabel = actorInfo?.name ?? event.user; const emojiLabel = event.reaction ?? "emoji"; const authorInfo = event.item_user ? await ctx.resolveUserName(event.item_user) : undefined; @@ -70,7 +41,7 @@ export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext } const text = authorLabel ? `${baseText} from ${authorLabel}` : baseText; const sessionKey = ctx.resolveSlackSystemEventSessionKey({ channelId: item.channel, - channelType, + channelType: auth.channelType, }); enqueueSystemEvent(text, { sessionKey, From 92eb3dfc9d25ca2600d6687cd025922193d3dba5 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:54:27 +0100 Subject: [PATCH 092/160] refactor(security): unify exec approval request matching --- src/gateway/exec-approval-manager.ts | 18 +--- ...e-invoke-system-run-approval-match.test.ts | 94 +++++++++++++++++++ .../node-invoke-system-run-approval-match.ts | 51 ++++++++++ .../node-invoke-system-run-approval.test.ts | 4 +- .../node-invoke-system-run-approval.ts | 61 +++--------- src/gateway/protocol/schema/exec-approvals.ts | 2 +- src/gateway/server-methods/exec-approval.ts | 4 +- src/infra/exec-approvals.ts | 26 ++--- 8 files changed, 182 insertions(+), 78 deletions(-) create mode 100644 src/gateway/node-invoke-system-run-approval-match.test.ts create mode 100644 src/gateway/node-invoke-system-run-approval-match.ts diff --git a/src/gateway/exec-approval-manager.ts b/src/gateway/exec-approval-manager.ts index 127d5feae094..320b4da0b1f0 100644 --- a/src/gateway/exec-approval-manager.ts +++ b/src/gateway/exec-approval-manager.ts @@ -1,21 +1,13 @@ import { randomUUID } from "node:crypto"; -import type { ExecApprovalDecision } from "../infra/exec-approvals.js"; +import type { + ExecApprovalDecision, + ExecApprovalRequestPayload as InfraExecApprovalRequestPayload, +} from "../infra/exec-approvals.js"; // Grace period to keep resolved entries for late awaitDecision calls const RESOLVED_ENTRY_GRACE_MS = 15_000; -export type ExecApprovalRequestPayload = { - command: string; - commandArgv?: string[] | null; - cwd?: string | null; - nodeId?: string | null; - host?: string | null; - security?: string | null; - ask?: string | null; - agentId?: string | null; - resolvedPath?: string | null; - sessionKey?: string | null; -}; +export type ExecApprovalRequestPayload = InfraExecApprovalRequestPayload; export type ExecApprovalRecord = { id: string; diff --git a/src/gateway/node-invoke-system-run-approval-match.test.ts b/src/gateway/node-invoke-system-run-approval-match.test.ts new file mode 100644 index 000000000000..f5f093426c6f --- /dev/null +++ b/src/gateway/node-invoke-system-run-approval-match.test.ts @@ -0,0 +1,94 @@ +import { describe, expect, test } from "vitest"; +import { approvalMatchesSystemRunRequest } from "./node-invoke-system-run-approval-match.js"; + +describe("approvalMatchesSystemRunRequest", () => { + test("matches legacy command text when binding fields match", () => { + const result = approvalMatchesSystemRunRequest({ + cmdText: "echo SAFE", + argv: ["echo", "SAFE"], + request: { + host: "node", + command: "echo SAFE", + cwd: "/tmp", + agentId: "agent-1", + sessionKey: "session-1", + }, + binding: { + cwd: "/tmp", + agentId: "agent-1", + sessionKey: "session-1", + }, + }); + expect(result).toBe(true); + }); + + test("rejects legacy command mismatch", () => { + const result = approvalMatchesSystemRunRequest({ + cmdText: "echo PWNED", + argv: ["echo", "PWNED"], + request: { + host: "node", + command: "echo SAFE", + }, + binding: { + cwd: null, + agentId: null, + sessionKey: null, + }, + }); + expect(result).toBe(false); + }); + + test("enforces exact argv binding when commandArgv is set", () => { + const result = approvalMatchesSystemRunRequest({ + cmdText: "echo SAFE", + argv: ["echo", "SAFE"], + request: { + host: "node", + command: "echo SAFE", + commandArgv: ["echo", "SAFE"], + }, + binding: { + cwd: null, + agentId: null, + sessionKey: null, + }, + }); + expect(result).toBe(true); + }); + + test("rejects argv mismatch even when command text matches", () => { + const result = approvalMatchesSystemRunRequest({ + cmdText: "echo SAFE", + argv: ["echo", "SAFE"], + request: { + host: "node", + command: "echo SAFE", + commandArgv: ["echo SAFE"], + }, + binding: { + cwd: null, + agentId: null, + sessionKey: null, + }, + }); + expect(result).toBe(false); + }); + + test("rejects non-node host requests", () => { + const result = approvalMatchesSystemRunRequest({ + cmdText: "echo SAFE", + argv: ["echo", "SAFE"], + request: { + host: "gateway", + command: "echo SAFE", + }, + binding: { + cwd: null, + agentId: null, + sessionKey: null, + }, + }); + expect(result).toBe(false); + }); +}); diff --git a/src/gateway/node-invoke-system-run-approval-match.ts b/src/gateway/node-invoke-system-run-approval-match.ts new file mode 100644 index 000000000000..3dccc9b793d3 --- /dev/null +++ b/src/gateway/node-invoke-system-run-approval-match.ts @@ -0,0 +1,51 @@ +import type { ExecApprovalRequestPayload } from "../infra/exec-approvals.js"; + +export type SystemRunApprovalBinding = { + cwd: string | null; + agentId: string | null; + sessionKey: string | null; +}; + +function argvMatchesRequest(requestedArgv: string[], argv: string[]): boolean { + if (requestedArgv.length === 0 || requestedArgv.length !== argv.length) { + return false; + } + for (let i = 0; i < requestedArgv.length; i += 1) { + if (requestedArgv[i] !== argv[i]) { + return false; + } + } + return true; +} + +export function approvalMatchesSystemRunRequest(params: { + cmdText: string; + argv: string[]; + request: ExecApprovalRequestPayload; + binding: SystemRunApprovalBinding; +}): boolean { + if (params.request.host !== "node") { + return false; + } + + const requestedArgv = params.request.commandArgv; + if (Array.isArray(requestedArgv)) { + if (!argvMatchesRequest(requestedArgv, params.argv)) { + return false; + } + } else if (!params.cmdText || params.request.command !== params.cmdText) { + return false; + } + + if ((params.request.cwd ?? null) !== params.binding.cwd) { + return false; + } + if ((params.request.agentId ?? null) !== params.binding.agentId) { + return false; + } + if ((params.request.sessionKey ?? null) !== params.binding.sessionKey) { + return false; + } + + return true; +} diff --git a/src/gateway/node-invoke-system-run-approval.test.ts b/src/gateway/node-invoke-system-run-approval.test.ts index c2d3cbe1dfa6..833bbf6f3cff 100644 --- a/src/gateway/node-invoke-system-run-approval.test.ts +++ b/src/gateway/node-invoke-system-run-approval.test.ts @@ -13,14 +13,14 @@ describe("sanitizeSystemRunParamsForForwarding", () => { }, }; - function makeRecord(command: string, commandArgv?: string[] | null): ExecApprovalRecord { + function makeRecord(command: string, commandArgv?: string[]): ExecApprovalRecord { return { id: "approval-1", request: { host: "node", nodeId: "node-1", command, - commandArgv: commandArgv ?? null, + commandArgv, cwd: null, agentId: null, sessionKey: null, diff --git a/src/gateway/node-invoke-system-run-approval.ts b/src/gateway/node-invoke-system-run-approval.ts index 9623eb1b518a..35cd18c66b9f 100644 --- a/src/gateway/node-invoke-system-run-approval.ts +++ b/src/gateway/node-invoke-system-run-approval.ts @@ -1,5 +1,6 @@ import { resolveSystemRunCommand } from "../infra/system-run-command.js"; import type { ExecApprovalRecord } from "./exec-approval-manager.js"; +import { approvalMatchesSystemRunRequest } from "./node-invoke-system-run-approval-match.js"; type SystemRunParamsLike = { command?: unknown; @@ -53,53 +54,6 @@ function clientHasApprovals(client: ApprovalClient | null): boolean { return scopes.includes("operator.admin") || scopes.includes("operator.approvals"); } -function approvalMatchesRequest( - cmdText: string, - argv: string[], - params: SystemRunParamsLike, - record: ExecApprovalRecord, -): boolean { - if (record.request.host !== "node") { - return false; - } - - const requestedArgv = Array.isArray(record.request.commandArgv) - ? record.request.commandArgv - : null; - if (requestedArgv) { - if (requestedArgv.length === 0 || requestedArgv.length !== argv.length) { - return false; - } - for (let i = 0; i < requestedArgv.length; i += 1) { - if (requestedArgv[i] !== argv[i]) { - return false; - } - } - } else if (!cmdText || record.request.command !== cmdText) { - return false; - } - - const reqCwd = record.request.cwd ?? null; - const runCwd = normalizeString(params.cwd) ?? null; - if (reqCwd !== runCwd) { - return false; - } - - const reqAgentId = record.request.agentId ?? null; - const runAgentId = normalizeString(params.agentId) ?? null; - if (reqAgentId !== runAgentId) { - return false; - } - - const reqSessionKey = record.request.sessionKey ?? null; - const runSessionKey = normalizeString(params.sessionKey) ?? null; - if (reqSessionKey !== runSessionKey) { - return false; - } - - return true; -} - function pickSystemRunParams(raw: Record): Record { // Defensive allowlist: only forward fields that the node-host `system.run` handler understands. // This prevents future internal control fields from being smuggled through the gateway. @@ -250,7 +204,18 @@ export function sanitizeSystemRunParamsForForwarding(opts: { }; } - if (!approvalMatchesRequest(cmdText, cmdTextResolution.argv, p, snapshot)) { + if ( + !approvalMatchesSystemRunRequest({ + cmdText, + argv: cmdTextResolution.argv, + request: snapshot.request, + binding: { + cwd: normalizeString(p.cwd) ?? null, + agentId: normalizeString(p.agentId) ?? null, + sessionKey: normalizeString(p.sessionKey) ?? null, + }, + }) + ) { return { ok: false, message: "approval id does not match request", diff --git a/src/gateway/protocol/schema/exec-approvals.ts b/src/gateway/protocol/schema/exec-approvals.ts index 1482ae4cfef8..083a445a4cfd 100644 --- a/src/gateway/protocol/schema/exec-approvals.ts +++ b/src/gateway/protocol/schema/exec-approvals.ts @@ -89,7 +89,7 @@ export const ExecApprovalRequestParamsSchema = Type.Object( { id: Type.Optional(NonEmptyString), command: NonEmptyString, - commandArgv: Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()])), + commandArgv: Type.Optional(Type.Array(Type.String())), cwd: Type.Optional(Type.Union([Type.String(), Type.Null()])), nodeId: Type.Optional(Type.Union([NonEmptyString, Type.Null()])), host: Type.Optional(Type.Union([Type.String(), Type.Null()])), diff --git a/src/gateway/server-methods/exec-approval.ts b/src/gateway/server-methods/exec-approval.ts index 555348bc777a..a9b3db150ce5 100644 --- a/src/gateway/server-methods/exec-approval.ts +++ b/src/gateway/server-methods/exec-approval.ts @@ -43,7 +43,7 @@ export function createExecApprovalHandlers( const p = params as { id?: string; command: string; - commandArgv?: string[] | null; + commandArgv?: string[]; cwd?: string; nodeId?: string; host?: string; @@ -63,7 +63,7 @@ export function createExecApprovalHandlers( const nodeId = typeof p.nodeId === "string" ? p.nodeId.trim() : ""; const commandArgv = Array.isArray(p.commandArgv) ? p.commandArgv.map((entry) => String(entry)) - : null; + : undefined; if (host === "node" && !nodeId) { respond( false, diff --git a/src/infra/exec-approvals.ts b/src/infra/exec-approvals.ts index 688972d83611..d78f3d137e95 100644 --- a/src/infra/exec-approvals.ts +++ b/src/infra/exec-approvals.ts @@ -11,20 +11,22 @@ export type ExecHost = "sandbox" | "gateway" | "node"; export type ExecSecurity = "deny" | "allowlist" | "full"; export type ExecAsk = "off" | "on-miss" | "always"; +export type ExecApprovalRequestPayload = { + command: string; + commandArgv?: string[]; + cwd?: string | null; + nodeId?: string | null; + host?: string | null; + security?: string | null; + ask?: string | null; + agentId?: string | null; + resolvedPath?: string | null; + sessionKey?: string | null; +}; + export type ExecApprovalRequest = { id: string; - request: { - command: string; - commandArgv?: string[] | null; - cwd?: string | null; - nodeId?: string | null; - host?: string | null; - security?: string | null; - ask?: string | null; - agentId?: string | null; - resolvedPath?: string | null; - sessionKey?: string | null; - }; + request: ExecApprovalRequestPayload; createdAtMs: number; expiresAtMs: number; }; From 1e7ec8bfd2e7e5e689c6e5b6f1808b3251a5fe0d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 02:43:22 +0000 Subject: [PATCH 093/160] fix(routing): preserve explicit cron account and bound message defaults Co-authored-by: lbo728 <72309817+lbo728@users.noreply.github.com> Co-authored-by: stakeswky <64798754+stakeswky@users.noreply.github.com> --- CHANGELOG.md | 1 + src/cron/delivery.test.ts | 18 ++++++++++ src/cron/delivery.ts | 13 +++++++ .../isolated-agent/delivery-target.test.ts | 35 +++++++++++++++++++ src/cron/isolated-agent/delivery-target.ts | 6 ++++ src/cron/isolated-agent/run.ts | 1 + src/cron/types.ts | 1 + src/gateway/protocol/schema/cron.ts | 1 + .../outbound/message-action-runner.test.ts | 28 +++++++++++++++ src/infra/outbound/message-action-runner.ts | 11 +++++- 10 files changed, 114 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a46b2779420..7c605803a44e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Docs: https://docs.openclaw.ai - Security/Workspace FS: reject hardlinked workspace file aliases in `tools.fs.workspaceOnly` and `tools.exec.applyPatch.workspaceOnly` boundary checks (including sandbox mount-root guards) to prevent out-of-workspace read/write via in-workspace hardlink paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. +- Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. - Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. diff --git a/src/cron/delivery.test.ts b/src/cron/delivery.test.ts index 6eaa5c667076..495e99d00397 100644 --- a/src/cron/delivery.test.ts +++ b/src/cron/delivery.test.ts @@ -54,4 +54,22 @@ describe("resolveCronDeliveryPlan", () => { expect(plan.channel).toBeUndefined(); expect(plan.to).toBe("https://example.invalid/cron"); }); + + it("threads delivery.accountId when explicitly configured", () => { + const plan = resolveCronDeliveryPlan( + makeJob({ + delivery: { + mode: "announce", + channel: "telegram", + to: "123", + accountId: " bot-a ", + }, + }), + ); + expect(plan.mode).toBe("announce"); + expect(plan.requested).toBe(true); + expect(plan.channel).toBe("telegram"); + expect(plan.to).toBe("123"); + expect(plan.accountId).toBe("bot-a"); + }); }); diff --git a/src/cron/delivery.ts b/src/cron/delivery.ts index 377cdb49b2fa..9022d09fd5fc 100644 --- a/src/cron/delivery.ts +++ b/src/cron/delivery.ts @@ -4,6 +4,7 @@ export type CronDeliveryPlan = { mode: CronDeliveryMode; channel?: CronMessageChannel; to?: string; + accountId?: string; source: "delivery" | "payload"; requested: boolean; }; @@ -27,6 +28,14 @@ function normalizeTo(value: unknown): string | undefined { return trimmed ? trimmed : undefined; } +function normalizeAccountId(value: unknown): string | undefined { + if (typeof value !== "string") { + return undefined; + } + const trimmed = value.trim(); + return trimmed ? trimmed : undefined; +} + export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan { const payload = job.payload.kind === "agentTurn" ? job.payload : null; const delivery = job.delivery; @@ -50,6 +59,9 @@ export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan { (delivery as { channel?: unknown } | undefined)?.channel, ); const deliveryTo = normalizeTo((delivery as { to?: unknown } | undefined)?.to); + const deliveryAccountId = normalizeAccountId( + (delivery as { accountId?: unknown } | undefined)?.accountId, + ); const channel = deliveryChannel ?? payloadChannel ?? "last"; const to = deliveryTo ?? payloadTo; @@ -59,6 +71,7 @@ export function resolveCronDeliveryPlan(job: CronJob): CronDeliveryPlan { mode: resolvedMode, channel: resolvedMode === "announce" ? channel : undefined, to, + accountId: deliveryAccountId, source: "delivery", requested: resolvedMode === "announce", }; diff --git a/src/cron/isolated-agent/delivery-target.test.ts b/src/cron/isolated-agent/delivery-target.test.ts index ad1df42bb478..b28239adda89 100644 --- a/src/cron/isolated-agent/delivery-target.test.ts +++ b/src/cron/isolated-agent/delivery-target.test.ts @@ -299,4 +299,39 @@ describe("resolveDeliveryTarget", () => { expect(result.to).toBe("987654"); expect(result.ok).toBe(true); }); + + it("explicit delivery.accountId overrides session-derived accountId", async () => { + setMainSessionEntry({ + sessionId: "sess-5", + updatedAt: 1000, + lastChannel: "telegram", + lastTo: "chat-999", + lastAccountId: "default", + }); + + const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, { + channel: "telegram", + to: "chat-999", + accountId: "bot-b", + }); + + expect(result.ok).toBe(true); + expect(result.accountId).toBe("bot-b"); + }); + + it("explicit delivery.accountId overrides bindings-derived accountId", async () => { + setMainSessionEntry(undefined); + const cfg = makeCfg({ + bindings: [{ agentId: AGENT_ID, match: { channel: "telegram", accountId: "bound" } }], + }); + + const result = await resolveDeliveryTarget(cfg, AGENT_ID, { + channel: "telegram", + to: "chat-777", + accountId: "explicit", + }); + + expect(result.ok).toBe(true); + expect(result.accountId).toBe("explicit"); + }); }); diff --git a/src/cron/isolated-agent/delivery-target.ts b/src/cron/isolated-agent/delivery-target.ts index 0aa261881202..1af69ee027a1 100644 --- a/src/cron/isolated-agent/delivery-target.ts +++ b/src/cron/isolated-agent/delivery-target.ts @@ -43,6 +43,7 @@ export async function resolveDeliveryTarget( channel?: "last" | ChannelId; to?: string; sessionKey?: string; + accountId?: string; }, ): Promise { const requestedChannel = typeof jobPayload.channel === "string" ? jobPayload.channel : "last"; @@ -114,6 +115,11 @@ export async function resolveDeliveryTarget( } } + // Explicit delivery account should override inferred session/binding account. + if (jobPayload.accountId) { + accountId = jobPayload.accountId; + } + // Carry threadId when it was explicitly set (from :topic: parsing or config) // or when delivering to the same recipient as the session's last conversation. // Session-derived threadIds are dropped when the target differs to prevent diff --git a/src/cron/isolated-agent/run.ts b/src/cron/isolated-agent/run.ts index a4a14bc26b87..751ea2bc13ea 100644 --- a/src/cron/isolated-agent/run.ts +++ b/src/cron/isolated-agent/run.ts @@ -314,6 +314,7 @@ export async function runCronIsolatedAgentTurn(params: { channel: deliveryPlan.channel ?? "last", to: deliveryPlan.to, sessionKey: params.job.sessionKey, + accountId: deliveryPlan.accountId, }); const { formattedTime, timeLine } = resolveCronStyleNow(params.cfg, now); diff --git a/src/cron/types.ts b/src/cron/types.ts index 837cba2168e6..4480b22ae6b4 100644 --- a/src/cron/types.ts +++ b/src/cron/types.ts @@ -22,6 +22,7 @@ export type CronDelivery = { mode: CronDeliveryMode; channel?: CronMessageChannel; to?: string; + accountId?: string; bestEffort?: boolean; }; diff --git a/src/gateway/protocol/schema/cron.ts b/src/gateway/protocol/schema/cron.ts index dae3b340d7e8..7e0ebe549176 100644 --- a/src/gateway/protocol/schema/cron.ts +++ b/src/gateway/protocol/schema/cron.ts @@ -138,6 +138,7 @@ export const CronPayloadPatchSchema = Type.Union([ const CronDeliverySharedProperties = { channel: Type.Optional(Type.Union([Type.Literal("last"), NonEmptyString])), + accountId: Type.Optional(NonEmptyString), bestEffort: Type.Optional(Type.Boolean()), }; diff --git a/src/infra/outbound/message-action-runner.test.ts b/src/infra/outbound/message-action-runner.test.ts index 6fdec33ab492..cf3ddabceadf 100644 --- a/src/infra/outbound/message-action-runner.test.ts +++ b/src/infra/outbound/message-action-runner.test.ts @@ -1021,4 +1021,32 @@ describe("runMessageAction accountId defaults", () => { expect(ctx.accountId).toBe("ops"); expect(ctx.params.accountId).toBe("ops"); }); + + it("falls back to the agent's bound account when accountId is omitted", async () => { + await runMessageAction({ + cfg: { + bindings: [{ agentId: "agent-b", match: { channel: "discord", accountId: "account-b" } }], + } as OpenClawConfig, + action: "send", + params: { + channel: "discord", + target: "channel:123", + message: "hi", + }, + agentId: "agent-b", + }); + + expect(handleAction).toHaveBeenCalled(); + const ctx = (handleAction.mock.calls as unknown as Array<[unknown]>)[0]?.[0] as + | { + accountId?: string | null; + params: Record; + } + | undefined; + if (!ctx) { + throw new Error("expected action context"); + } + expect(ctx.accountId).toBe("account-b"); + expect(ctx.params.accountId).toBe("account-b"); + }); }); diff --git a/src/infra/outbound/message-action-runner.ts b/src/infra/outbound/message-action-runner.ts index 57032e27de85..2693d110306d 100644 --- a/src/infra/outbound/message-action-runner.ts +++ b/src/infra/outbound/message-action-runner.ts @@ -14,6 +14,8 @@ import type { } from "../../channels/plugins/types.js"; import type { OpenClawConfig } from "../../config/config.js"; import { getAgentScopedMediaLocalRoots } from "../../media/local-roots.js"; +import { buildChannelAccountBindings } from "../../routing/bindings.js"; +import { normalizeAgentId } from "../../routing/session-key.js"; import { isDeliverableMessageChannel, normalizeMessageChannel, @@ -753,7 +755,14 @@ export async function runMessageAction( } const channel = await resolveChannel(cfg, params); - const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId; + let accountId = readStringParam(params, "accountId") ?? input.defaultAccountId; + if (!accountId && resolvedAgentId) { + const byAgent = buildChannelAccountBindings(cfg).get(channel); + const boundAccountIds = byAgent?.get(normalizeAgentId(resolvedAgentId)); + if (boundAccountIds && boundAccountIds.length > 0) { + accountId = boundAccountIds[0]; + } + } if (accountId) { params.accountId = accountId; } From ee594e2fdb718da52e87a41d0414b16c322a9af6 Mon Sep 17 00:00:00 2001 From: Harold Hunt Date: Wed, 25 Feb 2026 21:56:53 -0500 Subject: [PATCH 094/160] fix(telegram): webhook hang - tests and fix (openclaw#26933) thanks @huntharo Verified: - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: huntharo <5617868+huntharo@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> --- CHANGELOG.md | 1 + src/telegram/webhook.test.ts | 568 ++++++++++++++++++++++++++++++++++- src/telegram/webhook.ts | 234 +++++++++++---- 3 files changed, 736 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c605803a44e..149a517fd649 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. diff --git a/src/telegram/webhook.test.ts b/src/telegram/webhook.test.ts index 2c943a4be6f8..0117c55823ac 100644 --- a/src/telegram/webhook.test.ts +++ b/src/telegram/webhook.test.ts @@ -1,24 +1,26 @@ +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import { request } from "node:http"; +import { setTimeout as sleep } from "node:timers/promises"; import { describe, expect, it, vi } from "vitest"; import { startTelegramWebhook } from "./webhook.js"; -const handlerSpy = vi.hoisted(() => - vi.fn( - (_req: unknown, res: { writeHead: (status: number) => void; end: (body?: string) => void }) => { - res.writeHead(200); - res.end("ok"); - }, - ), -); +const handlerSpy = vi.hoisted(() => vi.fn((..._args: unknown[]): unknown => undefined)); const setWebhookSpy = vi.hoisted(() => vi.fn()); +const deleteWebhookSpy = vi.hoisted(() => vi.fn(async () => true)); +const initSpy = vi.hoisted(() => vi.fn(async () => undefined)); const stopSpy = vi.hoisted(() => vi.fn()); const webhookCallbackSpy = vi.hoisted(() => vi.fn(() => handlerSpy)); const createTelegramBotSpy = vi.hoisted(() => vi.fn(() => ({ - api: { setWebhook: setWebhookSpy }, + init: initSpy, + api: { setWebhook: setWebhookSpy, deleteWebhook: deleteWebhookSpy }, stop: stopSpy, })), ); +const WEBHOOK_POST_TIMEOUT_MS = process.platform === "win32" ? 20_000 : 8_000; + vi.mock("grammy", async (importOriginal) => { const actual = await importOriginal(); return { @@ -31,8 +33,178 @@ vi.mock("./bot.js", () => ({ createTelegramBot: createTelegramBotSpy, })); +async function fetchWithTimeout( + input: string, + init: Omit, + timeoutMs: number, +): Promise { + const abort = new AbortController(); + const timer = setTimeout(() => { + abort.abort(); + }, timeoutMs); + try { + return await fetch(input, { ...init, signal: abort.signal }); + } finally { + clearTimeout(timer); + } +} + +async function postWebhookJson(params: { + url: string; + payload: string; + secret?: string; + timeoutMs?: number; +}): Promise { + return await fetchWithTimeout( + params.url, + { + method: "POST", + headers: { + "content-type": "application/json", + ...(params.secret ? { "x-telegram-bot-api-secret-token": params.secret } : {}), + }, + body: params.payload, + }, + params.timeoutMs ?? 5_000, + ); +} + +function createDeterministicRng(seed: number): () => number { + let state = seed >>> 0; + return () => { + state = (state * 1_664_525 + 1_013_904_223) >>> 0; + return state / 4_294_967_296; + }; +} + +async function postWebhookPayloadWithChunkPlan(params: { + port: number; + path: string; + payload: string; + secret: string; + mode: "single" | "random-chunked"; + timeoutMs?: number; +}): Promise<{ statusCode: number; body: string }> { + const payloadBuffer = Buffer.from(params.payload, "utf-8"); + return await new Promise((resolve, reject) => { + let bytesQueued = 0; + let chunksQueued = 0; + let phase: "writing" | "awaiting-response" = "writing"; + let settled = false; + const finishResolve = (value: { statusCode: number; body: string }) => { + if (settled) { + return; + } + settled = true; + clearTimeout(timeout); + resolve(value); + }; + const finishReject = (error: unknown) => { + if (settled) { + return; + } + settled = true; + clearTimeout(timeout); + reject(error); + }; + + const req = request( + { + hostname: "127.0.0.1", + port: params.port, + path: params.path, + method: "POST", + headers: { + "content-type": "application/json", + "content-length": String(payloadBuffer.length), + "x-telegram-bot-api-secret-token": params.secret, + }, + }, + (res) => { + const chunks: Buffer[] = []; + res.on("data", (chunk: Buffer | string) => { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); + }); + res.on("end", () => { + finishResolve({ + statusCode: res.statusCode ?? 0, + body: Buffer.concat(chunks).toString("utf-8"), + }); + }); + }, + ); + + const timeout = setTimeout(() => { + finishReject( + new Error( + `webhook post timed out after ${params.timeoutMs ?? 15_000}ms (phase=${phase}, bytesQueued=${bytesQueued}, chunksQueued=${chunksQueued}, totalBytes=${payloadBuffer.length})`, + ), + ); + req.destroy(); + }, params.timeoutMs ?? 15_000); + + req.on("error", (error) => { + finishReject(error); + }); + + const writeAll = async () => { + if (params.mode === "single") { + req.end(payloadBuffer); + return; + } + + const rng = createDeterministicRng(26156); + let offset = 0; + while (offset < payloadBuffer.length) { + const remaining = payloadBuffer.length - offset; + const nextSize = Math.max(1, Math.min(remaining, 1 + Math.floor(rng() * 8_192))); + const chunk = payloadBuffer.subarray(offset, offset + nextSize); + const canContinue = req.write(chunk); + offset += nextSize; + bytesQueued = offset; + chunksQueued += 1; + if (chunksQueued % 10 === 0) { + await sleep(1 + Math.floor(rng() * 3)); + } + if (!canContinue) { + // Windows CI occasionally stalls on waiting for drain indefinitely. + // Bound the wait, then continue queuing this small (~1MB) payload. + await Promise.race([once(req, "drain"), sleep(25)]); + } + } + phase = "awaiting-response"; + req.end(); + }; + + void writeAll().catch((error) => { + finishReject(error); + }); + }); +} + +function createNearLimitTelegramPayload(): { payload: string; sizeBytes: number } { + const maxBytes = 1_024 * 1_024; + const targetBytes = maxBytes - 4_096; + const shell = { update_id: 77_777, message: { text: "" } }; + const shellSize = Buffer.byteLength(JSON.stringify(shell), "utf-8"); + const textLength = Math.max(1, targetBytes - shellSize); + const pattern = "the quick brown fox jumps over the lazy dog "; + const repeats = Math.ceil(textLength / pattern.length); + const text = pattern.repeat(repeats).slice(0, textLength); + const payload = JSON.stringify({ + update_id: 77_777, + message: { text }, + }); + return { payload, sizeBytes: Buffer.byteLength(payload, "utf-8") }; +} + +function sha256(text: string): string { + return createHash("sha256").update(text).digest("hex"); +} + describe("startTelegramWebhook", () => { it("starts server, registers webhook, and serves health", async () => { + initSpy.mockClear(); createTelegramBotSpy.mockClear(); webhookCallbackSpy.mockClear(); const abort = new AbortController(); @@ -59,6 +231,7 @@ describe("startTelegramWebhook", () => { const health = await fetch(`${url}/healthz`); expect(health.status).toBe(200); + expect(initSpy).toHaveBeenCalledTimes(1); expect(setWebhookSpy).toHaveBeenCalled(); expect(webhookCallbackSpy).toHaveBeenCalledWith( expect.objectContaining({ @@ -66,7 +239,7 @@ describe("startTelegramWebhook", () => { setWebhook: expect.any(Function), }), }), - "http", + "callback", { secretToken: "secret", onTimeout: "return", @@ -101,7 +274,13 @@ describe("startTelegramWebhook", () => { if (!addr || typeof addr === "string") { throw new Error("no addr"); } - await fetch(`http://127.0.0.1:${addr.port}/hook`, { method: "POST" }); + const payload = JSON.stringify({ update_id: 1, message: { text: "hello" } }); + const response = await postWebhookJson({ + url: `http://127.0.0.1:${addr.port}/hook`, + payload, + secret: "secret", + }); + expect(response.status).toBe(200); expect(handlerSpy).toHaveBeenCalled(); abort.abort(); }); @@ -113,4 +292,371 @@ describe("startTelegramWebhook", () => { }), ).rejects.toThrow(/requires a non-empty secret token/i); }); + + it("registers webhook using the bound listening port when port is 0", async () => { + setWebhookSpy.mockClear(); + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret: "secret", + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + try { + const addr = server.address(); + if (!addr || typeof addr === "string") { + throw new Error("no addr"); + } + expect(addr.port).toBeGreaterThan(0); + expect(setWebhookSpy).toHaveBeenCalledTimes(1); + expect(setWebhookSpy).toHaveBeenCalledWith( + `http://127.0.0.1:${addr.port}/hook`, + expect.objectContaining({ + secret_token: "secret", + }), + ); + } finally { + abort.abort(); + } + }); + + it("keeps webhook payload readable when callback delays body read", async () => { + handlerSpy.mockImplementationOnce(async (...args: unknown[]) => { + const [update, reply] = args as [unknown, (json: string) => Promise]; + await sleep(50); + await reply(JSON.stringify(update)); + }); + + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret: "secret", + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + try { + const addr = server.address(); + if (!addr || typeof addr === "string") { + throw new Error("no addr"); + } + + const payload = JSON.stringify({ update_id: 1, message: { text: "hello" } }); + const res = await postWebhookJson({ + url: `http://127.0.0.1:${addr.port}/hook`, + payload, + secret: "secret", + }); + expect(res.status).toBe(200); + const responseBody = await res.text(); + expect(JSON.parse(responseBody)).toEqual(JSON.parse(payload)); + } finally { + abort.abort(); + } + }); + + it("keeps webhook payload readable across multiple delayed reads", async () => { + const seenPayloads: string[] = []; + const delayedHandler = async (...args: unknown[]) => { + const [update, reply] = args as [unknown, (json: string) => Promise]; + await sleep(50); + seenPayloads.push(JSON.stringify(update)); + await reply("ok"); + }; + handlerSpy.mockImplementationOnce(delayedHandler).mockImplementationOnce(delayedHandler); + + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret: "secret", + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + try { + const addr = server.address(); + if (!addr || typeof addr === "string") { + throw new Error("no addr"); + } + + const payloads = [ + JSON.stringify({ update_id: 1, message: { text: "first" } }), + JSON.stringify({ update_id: 2, message: { text: "second" } }), + ]; + + for (const payload of payloads) { + const res = await postWebhookJson({ + url: `http://127.0.0.1:${addr.port}/hook`, + payload, + secret: "secret", + }); + expect(res.status).toBe(200); + } + + expect(seenPayloads.map((x) => JSON.parse(x))).toEqual(payloads.map((x) => JSON.parse(x))); + } finally { + abort.abort(); + } + }); + + it("processes a second request after first-request delayed-init data loss", async () => { + const seenUpdates: unknown[] = []; + webhookCallbackSpy.mockImplementationOnce( + () => + vi.fn( + ( + update: unknown, + reply: (json: string) => Promise, + _secretHeader: string | undefined, + _unauthorized: () => Promise, + ) => { + seenUpdates.push(update); + void (async () => { + await sleep(50); + await reply("ok"); + })(); + }, + ) as unknown as typeof handlerSpy, + ); + + const secret = "secret"; + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret, + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + + try { + const address = server.address(); + if (!address || typeof address === "string") { + throw new Error("no addr"); + } + + const firstPayload = JSON.stringify({ update_id: 100, message: { text: "first" } }); + const secondPayload = JSON.stringify({ update_id: 101, message: { text: "second" } }); + const firstResponse = await postWebhookPayloadWithChunkPlan({ + port: address.port, + path: "/hook", + payload: firstPayload, + secret, + mode: "single", + timeoutMs: WEBHOOK_POST_TIMEOUT_MS, + }); + const secondResponse = await postWebhookPayloadWithChunkPlan({ + port: address.port, + path: "/hook", + payload: secondPayload, + secret, + mode: "single", + timeoutMs: WEBHOOK_POST_TIMEOUT_MS, + }); + + expect(firstResponse.statusCode).toBe(200); + expect(secondResponse.statusCode).toBe(200); + expect(seenUpdates).toEqual([JSON.parse(firstPayload), JSON.parse(secondPayload)]); + } finally { + abort.abort(); + } + }); + + it("handles near-limit payload with random chunk writes and event-loop yields", async () => { + const seenUpdates: Array<{ update_id: number; message: { text: string } }> = []; + webhookCallbackSpy.mockImplementationOnce( + () => + vi.fn( + ( + update: unknown, + reply: (json: string) => Promise, + _secretHeader: string | undefined, + _unauthorized: () => Promise, + ) => { + seenUpdates.push(update as { update_id: number; message: { text: string } }); + void reply("ok"); + }, + ) as unknown as typeof handlerSpy, + ); + + const { payload, sizeBytes } = createNearLimitTelegramPayload(); + expect(sizeBytes).toBeLessThan(1_024 * 1_024); + expect(sizeBytes).toBeGreaterThan(256 * 1_024); + const expected = JSON.parse(payload) as { update_id: number; message: { text: string } }; + + const secret = "secret"; + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret, + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + + try { + const address = server.address(); + if (!address || typeof address === "string") { + throw new Error("no addr"); + } + + const response = await postWebhookPayloadWithChunkPlan({ + port: address.port, + path: "/hook", + payload, + secret, + mode: "random-chunked", + timeoutMs: WEBHOOK_POST_TIMEOUT_MS, + }); + + expect(response.statusCode).toBe(200); + expect(seenUpdates).toHaveLength(1); + expect(seenUpdates[0]?.update_id).toBe(expected.update_id); + expect(seenUpdates[0]?.message.text.length).toBe(expected.message.text.length); + expect(sha256(seenUpdates[0]?.message.text ?? "")).toBe(sha256(expected.message.text)); + } finally { + abort.abort(); + } + }); + + it("handles near-limit payload written in a single request write", async () => { + const seenUpdates: Array<{ update_id: number; message: { text: string } }> = []; + webhookCallbackSpy.mockImplementationOnce( + () => + vi.fn( + ( + update: unknown, + reply: (json: string) => Promise, + _secretHeader: string | undefined, + _unauthorized: () => Promise, + ) => { + seenUpdates.push(update as { update_id: number; message: { text: string } }); + void reply("ok"); + }, + ) as unknown as typeof handlerSpy, + ); + + const { payload, sizeBytes } = createNearLimitTelegramPayload(); + expect(sizeBytes).toBeLessThan(1_024 * 1_024); + expect(sizeBytes).toBeGreaterThan(256 * 1_024); + const expected = JSON.parse(payload) as { update_id: number; message: { text: string } }; + + const secret = "secret"; + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret, + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + + try { + const address = server.address(); + if (!address || typeof address === "string") { + throw new Error("no addr"); + } + + const response = await postWebhookPayloadWithChunkPlan({ + port: address.port, + path: "/hook", + payload, + secret, + mode: "single", + timeoutMs: WEBHOOK_POST_TIMEOUT_MS, + }); + + expect(response.statusCode).toBe(200); + expect(seenUpdates).toHaveLength(1); + expect(seenUpdates[0]?.update_id).toBe(expected.update_id); + expect(seenUpdates[0]?.message.text.length).toBe(expected.message.text.length); + expect(sha256(seenUpdates[0]?.message.text ?? "")).toBe(sha256(expected.message.text)); + } finally { + abort.abort(); + } + }); + + it("rejects payloads larger than 1MB before invoking webhook handler", async () => { + handlerSpy.mockClear(); + const abort = new AbortController(); + const { server } = await startTelegramWebhook({ + token: "tok", + secret: "secret", + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + + try { + const address = server.address(); + if (!address || typeof address === "string") { + throw new Error("no addr"); + } + + const responseOrError = await new Promise< + | { kind: "response"; statusCode: number; body: string } + | { kind: "error"; code: string | undefined } + >((resolve) => { + const req = request( + { + hostname: "127.0.0.1", + port: address.port, + path: "/hook", + method: "POST", + headers: { + "content-type": "application/json", + "content-length": String(1_024 * 1_024 + 2_048), + "x-telegram-bot-api-secret-token": "secret", + }, + }, + (res) => { + const chunks: Buffer[] = []; + res.on("data", (chunk: Buffer | string) => { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); + }); + res.on("end", () => { + resolve({ + kind: "response", + statusCode: res.statusCode ?? 0, + body: Buffer.concat(chunks).toString("utf-8"), + }); + }); + }, + ); + req.on("error", (error: NodeJS.ErrnoException) => { + resolve({ kind: "error", code: error.code }); + }); + req.end("{}"); + }); + + if (responseOrError.kind === "response") { + expect(responseOrError.statusCode).toBe(413); + expect(responseOrError.body).toBe("Payload too large"); + } else { + expect(responseOrError.code).toBeOneOf(["ECONNRESET", "EPIPE"]); + } + expect(handlerSpy).not.toHaveBeenCalled(); + } finally { + abort.abort(); + } + }); + + it("de-registers webhook when shutting down", async () => { + deleteWebhookSpy.mockClear(); + const abort = new AbortController(); + await startTelegramWebhook({ + token: "tok", + secret: "secret", + port: 0, + abortSignal: abort.signal, + path: "/hook", + }); + + abort.abort(); + await sleep(25); + + expect(deleteWebhookSpy).toHaveBeenCalledTimes(1); + expect(deleteWebhookSpy).toHaveBeenCalledWith({ drop_pending_updates: false }); + }); }); diff --git a/src/telegram/webhook.ts b/src/telegram/webhook.ts index 9eb3c73d7f4f..0fd887f956c6 100644 --- a/src/telegram/webhook.ts +++ b/src/telegram/webhook.ts @@ -3,7 +3,7 @@ import { webhookCallback } from "grammy"; import type { OpenClawConfig } from "../config/config.js"; import { isDiagnosticsEnabled } from "../infra/diagnostic-events.js"; import { formatErrorMessage } from "../infra/errors.js"; -import { installRequestBodyLimitGuard } from "../infra/http-body.js"; +import { readJsonBodyWithLimit } from "../infra/http-body.js"; import { logWebhookError, logWebhookProcessed, @@ -21,6 +21,59 @@ const TELEGRAM_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024; const TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS = 30_000; const TELEGRAM_WEBHOOK_CALLBACK_TIMEOUT_MS = 10_000; +async function listenHttpServer(params: { + server: ReturnType; + port: number; + host: string; +}) { + await new Promise((resolve, reject) => { + const onError = (err: Error) => { + params.server.off("error", onError); + reject(err); + }; + params.server.once("error", onError); + params.server.listen(params.port, params.host, () => { + params.server.off("error", onError); + resolve(); + }); + }); +} + +function resolveWebhookPublicUrl(params: { + configuredPublicUrl?: string; + server: ReturnType; + path: string; + host: string; + port: number; +}) { + if (params.configuredPublicUrl) { + return params.configuredPublicUrl; + } + const address = params.server.address(); + if (address && typeof address !== "string") { + const resolvedHost = + params.host === "0.0.0.0" || address.address === "0.0.0.0" || address.address === "::" + ? "localhost" + : address.address; + return `http://${resolvedHost}:${address.port}${params.path}`; + } + const fallbackHost = params.host === "0.0.0.0" ? "localhost" : params.host; + return `http://${fallbackHost}:${params.port}${params.path}`; +} + +async function initializeTelegramWebhookBot(params: { + bot: ReturnType; + runtime: RuntimeEnv; + abortSignal?: AbortSignal; +}) { + const initSignal = params.abortSignal as Parameters<(typeof params.bot)["init"]>[0]; + await withTelegramApiErrorLogging({ + operation: "getMe", + runtime: params.runtime, + fn: () => params.bot.init(initSignal), + }); +} + export async function startTelegramWebhook(opts: { token: string; accountId?: string; @@ -55,7 +108,12 @@ export async function startTelegramWebhook(opts: { config: opts.config, accountId: opts.accountId, }); - const handler = webhookCallback(bot, "http", { + await initializeTelegramWebhookBot({ + bot, + runtime, + abortSignal: opts.abortSignal, + }); + const handler = webhookCallback(bot, "callback", { secretToken: secret, onTimeout: "return", timeoutMilliseconds: TELEGRAM_WEBHOOK_CALLBACK_TIMEOUT_MS, @@ -66,6 +124,14 @@ export async function startTelegramWebhook(opts: { } const server = createServer((req, res) => { + const respondText = (statusCode: number, text = "") => { + if (res.headersSent || res.writableEnded) { + return; + } + res.writeHead(statusCode, { "Content-Type": "text/plain; charset=utf-8" }); + res.end(text); + }; + if (req.url === healthPath) { res.writeHead(200); res.end("ok"); @@ -80,69 +146,125 @@ export async function startTelegramWebhook(opts: { if (diagnosticsEnabled) { logWebhookReceived({ channel: "telegram", updateType: "telegram-post" }); } - const guard = installRequestBodyLimitGuard(req, res, { - maxBytes: TELEGRAM_WEBHOOK_MAX_BODY_BYTES, - timeoutMs: TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS, - responseFormat: "text", - }); - if (guard.isTripped()) { - return; - } - const handled = handler(req, res); - if (handled && typeof handled.catch === "function") { - void handled - .then(() => { - if (diagnosticsEnabled) { - logWebhookProcessed({ - channel: "telegram", - updateType: "telegram-post", - durationMs: Date.now() - startTime, - }); - } - }) - .catch((err) => { - if (guard.isTripped()) { - return; - } - const errMsg = formatErrorMessage(err); - if (diagnosticsEnabled) { - logWebhookError({ - channel: "telegram", - updateType: "telegram-post", - error: errMsg, - }); - } - runtime.log?.(`webhook handler failed: ${errMsg}`); - if (!res.headersSent) { - res.writeHead(500); - } - res.end(); - }) - .finally(() => { - guard.dispose(); + void (async () => { + const body = await readJsonBodyWithLimit(req, { + maxBytes: TELEGRAM_WEBHOOK_MAX_BODY_BYTES, + timeoutMs: TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS, + emptyObjectOnEmpty: false, + }); + if (!body.ok) { + if (body.code === "PAYLOAD_TOO_LARGE") { + respondText(413, body.error); + return; + } + if (body.code === "REQUEST_BODY_TIMEOUT") { + respondText(408, body.error); + return; + } + if (body.code === "CONNECTION_CLOSED") { + respondText(400, body.error); + return; + } + respondText(400, body.error); + return; + } + + let replied = false; + const reply = async (json: string) => { + if (replied) { + return; + } + replied = true; + if (res.headersSent || res.writableEnded) { + return; + } + res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" }); + res.end(json); + }; + const unauthorized = async () => { + if (replied) { + return; + } + replied = true; + respondText(401, "unauthorized"); + }; + const secretHeaderRaw = req.headers["x-telegram-bot-api-secret-token"]; + const secretHeader = Array.isArray(secretHeaderRaw) ? secretHeaderRaw[0] : secretHeaderRaw; + + await handler(body.value, reply, secretHeader, unauthorized); + if (!replied) { + respondText(200); + } + + if (diagnosticsEnabled) { + logWebhookProcessed({ + channel: "telegram", + updateType: "telegram-post", + durationMs: Date.now() - startTime, }); - return; - } - guard.dispose(); + } + })().catch((err) => { + const errMsg = formatErrorMessage(err); + if (diagnosticsEnabled) { + logWebhookError({ + channel: "telegram", + updateType: "telegram-post", + error: errMsg, + }); + } + runtime.log?.(`webhook handler failed: ${errMsg}`); + respondText(500); + }); }); - const publicUrl = - opts.publicUrl ?? `http://${host === "0.0.0.0" ? "localhost" : host}:${port}${path}`; + await listenHttpServer({ + server, + port, + host, + }); - await withTelegramApiErrorLogging({ - operation: "setWebhook", - runtime, - fn: () => - bot.api.setWebhook(publicUrl, { - secret_token: secret, - allowed_updates: resolveTelegramAllowedUpdates(), - }), + const publicUrl = resolveWebhookPublicUrl({ + configuredPublicUrl: opts.publicUrl, + server, + path, + host, + port, }); - await new Promise((resolve) => server.listen(port, host, resolve)); + try { + await withTelegramApiErrorLogging({ + operation: "setWebhook", + runtime, + fn: () => + bot.api.setWebhook(publicUrl, { + secret_token: secret, + allowed_updates: resolveTelegramAllowedUpdates(), + }), + }); + } catch (err) { + server.close(); + void bot.stop(); + if (diagnosticsEnabled) { + stopDiagnosticHeartbeat(); + } + throw err; + } + runtime.log?.(`webhook listening on ${publicUrl}`); + let shutDown = false; const shutdown = () => { + if (shutDown) { + return; + } + shutDown = true; + void withTelegramApiErrorLogging({ + operation: "deleteWebhook", + runtime, + fn: () => bot.api.deleteWebhook({ drop_pending_updates: false }), + }).catch(() => { + // withTelegramApiErrorLogging has already emitted the failure. + }); server.close(); void bot.stop(); if (diagnosticsEnabled) { From 8a006a32603b0a2cd7ecd9cb7f858e1b96332035 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:56:40 +0100 Subject: [PATCH 095/160] feat(heartbeat): add directPolicy and restore default direct delivery --- CHANGELOG.md | 5 ++ docs/gateway/configuration-reference.md | 3 +- docs/gateway/configuration.md | 3 +- docs/gateway/heartbeat.md | 6 +- docs/gateway/troubleshooting.md | 2 +- docs/start/openclaw.md | 2 +- src/config/config.plugin-validation.test.ts | 28 +++++++ src/config/schema.help.ts | 4 + src/config/schema.labels.ts | 2 + src/config/types.agent-defaults.ts | 2 + src/config/zod-schema.agent-runtime.ts | 1 + ...tbeat-runner.returns-default-unset.test.ts | 24 ++++++ src/infra/outbound/targets.test.ts | 81 ++++++++++++++++--- src/infra/outbound/targets.ts | 2 +- 14 files changed, 149 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 149a517fd649..d0fa2607a195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ Docs: https://docs.openclaw.ai - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. - Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. +- Heartbeat/Config: replace heartbeat DM toggle with `agents.defaults.heartbeat.directPolicy` (`allow` | `block`; also supported per-agent via `agents.list[].heartbeat.directPolicy`) for clearer delivery semantics. + +### Breaking + +- **BREAKING:** Heartbeat direct/DM delivery default is now `allow` again. To keep DM-blocked behavior from `2026.2.24`, set `agents.defaults.heartbeat.directPolicy: "block"` (or per-agent override). ### Fixes diff --git a/docs/gateway/configuration-reference.md b/docs/gateway/configuration-reference.md index b03a0daa4fca..8d147b23fd70 100644 --- a/docs/gateway/configuration-reference.md +++ b/docs/gateway/configuration-reference.md @@ -800,6 +800,7 @@ Periodic heartbeat runs. includeReasoning: false, session: "main", to: "+15555550123", + directPolicy: "allow", // allow (default) | block target: "none", // default: none | options: last | whatsapp | telegram | discord | ... prompt: "Read HEARTBEAT.md if it exists...", ackMaxChars: 300, @@ -812,7 +813,7 @@ Periodic heartbeat runs. - `every`: duration string (ms/s/m/h). Default: `30m`. - `suppressToolErrorWarnings`: when true, suppresses tool error warning payloads during heartbeat runs. -- Heartbeats never deliver to direct/DM chat targets when the destination can be classified as direct (for example `user:`, Telegram user chat IDs, or WhatsApp direct numbers/JIDs); those runs still execute, but outbound delivery is skipped. +- `directPolicy`: direct/DM delivery policy. `allow` (default) permits direct-target delivery. `block` suppresses direct-target delivery and emits `reason=dm-blocked`. - Per-agent: set `agents.list[].heartbeat`. When any agent defines `heartbeat`, **only those agents** run heartbeats. - Heartbeats run full agent turns — shorter intervals burn more tokens. diff --git a/docs/gateway/configuration.md b/docs/gateway/configuration.md index 3f7403d4647e..ff3179d28e2a 100644 --- a/docs/gateway/configuration.md +++ b/docs/gateway/configuration.md @@ -239,7 +239,8 @@ When validation fails: ``` - `every`: duration string (`30m`, `2h`). Set `0m` to disable. - - `target`: `last` | `whatsapp` | `telegram` | `discord` | `none` (DM-style `user:` heartbeat delivery is blocked) + - `target`: `last` | `whatsapp` | `telegram` | `discord` | `none` + - `directPolicy`: `allow` (default) or `block` for DM-style heartbeat targets - See [Heartbeat](/gateway/heartbeat) for the full guide. diff --git a/docs/gateway/heartbeat.md b/docs/gateway/heartbeat.md index cf7ea489c40b..70f4b968233b 100644 --- a/docs/gateway/heartbeat.md +++ b/docs/gateway/heartbeat.md @@ -215,7 +215,9 @@ Use `accountId` to target a specific account on multi-account channels like Tele - `last`: deliver to the last used external channel. - explicit channel: `whatsapp` / `telegram` / `discord` / `googlechat` / `slack` / `msteams` / `signal` / `imessage`. - `none` (default): run the heartbeat but **do not deliver** externally. -- Direct/DM heartbeat destinations are blocked when target parsing identifies a direct chat (for example `user:`, Telegram user chat IDs, or WhatsApp direct numbers/JIDs). +- `directPolicy`: controls direct/DM delivery behavior: + - `allow` (default): allow direct/DM heartbeat delivery. + - `block`: suppress direct/DM delivery (`reason=dm-blocked`). - `to`: optional recipient override (channel-specific id, e.g. E.164 for WhatsApp or a Telegram chat id). For Telegram topics/threads, use `:topic:`. - `accountId`: optional account id for multi-account channels. When `target: "last"`, the account id applies to the resolved last channel if it supports accounts; otherwise it is ignored. If the account id does not match a configured account for the resolved channel, delivery is skipped. - `prompt`: overrides the default prompt body (not merged). @@ -236,7 +238,7 @@ Use `accountId` to target a specific account on multi-account channels like Tele - `session` only affects the run context; delivery is controlled by `target` and `to`. - To deliver to a specific channel/recipient, set `target` + `to`. With `target: "last"`, delivery uses the last external channel for that session. -- Heartbeat deliveries never send to direct/DM targets when the destination is identified as direct; those runs still execute, but outbound delivery is skipped. +- Heartbeat deliveries allow direct/DM targets by default. Set `directPolicy: "block"` to suppress direct-target sends while still running the heartbeat turn. - If the main queue is busy, the heartbeat is skipped and retried later. - If `target` resolves to no external destination, the run still happens but no outbound message is sent. diff --git a/docs/gateway/troubleshooting.md b/docs/gateway/troubleshooting.md index 23483076102b..45963f155798 100644 --- a/docs/gateway/troubleshooting.md +++ b/docs/gateway/troubleshooting.md @@ -174,7 +174,7 @@ Common signatures: - `cron: timer tick failed` → scheduler tick failed; check file/log/runtime errors. - `heartbeat skipped` with `reason=quiet-hours` → outside active hours window. - `heartbeat: unknown accountId` → invalid account id for heartbeat delivery target. -- `heartbeat skipped` with `reason=dm-blocked` → heartbeat target resolved to a DM-style `user:` destination (blocked by design). +- `heartbeat skipped` with `reason=dm-blocked` → heartbeat target resolved to a DM-style destination while `agents.defaults.heartbeat.directPolicy` (or per-agent override) is set to `block`. Related: diff --git a/docs/start/openclaw.md b/docs/start/openclaw.md index 058f2fa67fef..671efe420c72 100644 --- a/docs/start/openclaw.md +++ b/docs/start/openclaw.md @@ -164,7 +164,7 @@ Set `agents.defaults.heartbeat.every: "0m"` to disable. - If `HEARTBEAT.md` exists but is effectively empty (only blank lines and markdown headers like `# Heading`), OpenClaw skips the heartbeat run to save API calls. - If the file is missing, the heartbeat still runs and the model decides what to do. - If the agent replies with `HEARTBEAT_OK` (optionally with short padding; see `agents.defaults.heartbeat.ackMaxChars`), OpenClaw suppresses outbound delivery for that heartbeat. -- Heartbeat delivery to DM-style `user:` targets is blocked; those runs still execute but skip outbound delivery. +- By default, heartbeat delivery to DM-style `user:` targets is allowed. Set `agents.defaults.heartbeat.directPolicy: "block"` to suppress direct-target delivery while keeping heartbeat runs active. - Heartbeats run full agent turns — shorter intervals burn more tokens. ```json5 diff --git a/src/config/config.plugin-validation.test.ts b/src/config/config.plugin-validation.test.ts index d9e6b3190e17..62584f138de5 100644 --- a/src/config/config.plugin-validation.test.ts +++ b/src/config/config.plugin-validation.test.ts @@ -234,4 +234,32 @@ describe("config plugin validation", () => { }); } }); + + it("accepts heartbeat directPolicy enum values", async () => { + const home = await createCaseHome(); + const res = validateInHome(home, { + agents: { + defaults: { heartbeat: { target: "last", directPolicy: "block" } }, + list: [{ id: "pi", heartbeat: { directPolicy: "allow" } }], + }, + }); + expect(res.ok).toBe(true); + }); + + it("rejects invalid heartbeat directPolicy values", async () => { + const home = await createCaseHome(); + const res = validateInHome(home, { + agents: { + defaults: { heartbeat: { directPolicy: "maybe" } }, + list: [{ id: "pi" }], + }, + }); + expect(res.ok).toBe(false); + if (!res.ok) { + const hasIssue = res.issues.some( + (issue) => issue.path === "agents.defaults.heartbeat.directPolicy", + ); + expect(hasIssue).toBe(true); + } + }); }); diff --git a/src/config/schema.help.ts b/src/config/schema.help.ts index a479ec0a8537..f32433e13334 100644 --- a/src/config/schema.help.ts +++ b/src/config/schema.help.ts @@ -1238,6 +1238,10 @@ export const FIELD_HELP: Record = { "Shows degraded/error heartbeat alerts when true so operator channels surface problems promptly. Keep enabled in production so broken channel states are visible.", "channels.defaults.heartbeat.useIndicator": "Enables concise indicator-style heartbeat rendering instead of verbose status text where supported. Use indicator mode for dense dashboards with many active channels.", + "agents.defaults.heartbeat.directPolicy": + 'Controls whether heartbeat delivery may target direct/DM chats: "allow" (default) permits DM delivery and "block" suppresses direct-target sends.', + "agents.list.*.heartbeat.directPolicy": + 'Per-agent override for heartbeat direct/DM delivery policy; use "block" for agents that should only send heartbeat alerts to non-DM destinations.', "channels.telegram.configWrites": "Allow Telegram to write config in response to channel events/commands (default: true).", "channels.telegram.botToken": diff --git a/src/config/schema.labels.ts b/src/config/schema.labels.ts index cd28b1fafb83..8c0c6350d7b7 100644 --- a/src/config/schema.labels.ts +++ b/src/config/schema.labels.ts @@ -402,6 +402,8 @@ export const FIELD_LABELS: Record = { "Compaction Memory Flush Soft Threshold", "agents.defaults.compaction.memoryFlush.prompt": "Compaction Memory Flush Prompt", "agents.defaults.compaction.memoryFlush.systemPrompt": "Compaction Memory Flush System Prompt", + "agents.defaults.heartbeat.directPolicy": "Heartbeat Direct Policy", + "agents.list.*.heartbeat.directPolicy": "Heartbeat Direct Policy", "agents.defaults.heartbeat.suppressToolErrorWarnings": "Heartbeat Suppress Tool Error Warnings", "agents.defaults.sandbox.browser.network": "Sandbox Browser Network", "agents.defaults.sandbox.browser.cdpSourceRange": "Sandbox Browser CDP Source Port Range", diff --git a/src/config/types.agent-defaults.ts b/src/config/types.agent-defaults.ts index e8eac6850865..afc65e3daec8 100644 --- a/src/config/types.agent-defaults.ts +++ b/src/config/types.agent-defaults.ts @@ -213,6 +213,8 @@ export type AgentDefaultsConfig = { session?: string; /** Delivery target ("last", "none", or a channel id). */ target?: "last" | "none" | ChannelId; + /** Direct/DM delivery policy. Default: "allow". */ + directPolicy?: "allow" | "block"; /** Optional delivery override (E.164 for WhatsApp, chat id for Telegram). Supports :topic:NNN suffix for Telegram topics. */ to?: string; /** Optional account id for multi-account channels. */ diff --git a/src/config/zod-schema.agent-runtime.ts b/src/config/zod-schema.agent-runtime.ts index c477cc1743b6..9df0776b9560 100644 --- a/src/config/zod-schema.agent-runtime.ts +++ b/src/config/zod-schema.agent-runtime.ts @@ -26,6 +26,7 @@ export const HeartbeatSchema = z session: z.string().optional(), includeReasoning: z.boolean().optional(), target: z.string().optional(), + directPolicy: z.union([z.literal("allow"), z.literal("block")]).optional(), to: z.string().optional(), accountId: z.string().optional(), prompt: z.string().optional(), diff --git a/src/infra/heartbeat-runner.returns-default-unset.test.ts b/src/infra/heartbeat-runner.returns-default-unset.test.ts index 0ec2afcafdd5..c4f45b5e039c 100644 --- a/src/infra/heartbeat-runner.returns-default-unset.test.ts +++ b/src/infra/heartbeat-runner.returns-default-unset.test.ts @@ -325,6 +325,30 @@ describe("resolveHeartbeatDeliveryTarget", () => { lastAccountId: undefined, }, }, + { + name: "allow direct target by default", + cfg: { agents: { defaults: { heartbeat: { target: "last" } } } }, + entry: { ...baseEntry, lastChannel: "telegram", lastTo: "5232990709" }, + expected: { + channel: "telegram", + to: "5232990709", + accountId: undefined, + lastChannel: "telegram", + lastAccountId: undefined, + }, + }, + { + name: "block direct target when directPolicy is block", + cfg: { agents: { defaults: { heartbeat: { target: "last", directPolicy: "block" } } } }, + entry: { ...baseEntry, lastChannel: "telegram", lastTo: "5232990709" }, + expected: { + channel: "none", + reason: "dm-blocked", + accountId: undefined, + lastChannel: "telegram", + lastAccountId: undefined, + }, + }, ]; for (const testCase of cases) { expect( diff --git a/src/infra/outbound/targets.test.ts b/src/infra/outbound/targets.test.ts index 8f120702de08..cbad502cddec 100644 --- a/src/infra/outbound/targets.test.ts +++ b/src/infra/outbound/targets.test.ts @@ -301,7 +301,7 @@ describe("resolveSessionDeliveryTarget", () => { expect(resolved.to).toBe("63448508"); }); - it("blocks heartbeat delivery to Slack DMs and avoids inherited threadId", () => { + it("allows heartbeat delivery to Slack DMs and avoids inherited threadId by default", () => { const cfg: OpenClawConfig = {}; const resolved = resolveHeartbeatDeliveryTarget({ cfg, @@ -317,12 +317,34 @@ describe("resolveSessionDeliveryTarget", () => { }, }); + expect(resolved.channel).toBe("slack"); + expect(resolved.to).toBe("user:U123"); + expect(resolved.threadId).toBeUndefined(); + }); + + it("blocks heartbeat delivery to Slack DMs when directPolicy is block", () => { + const cfg: OpenClawConfig = {}; + const resolved = resolveHeartbeatDeliveryTarget({ + cfg, + entry: { + sessionId: "sess-heartbeat-outbound", + updatedAt: 1, + lastChannel: "slack", + lastTo: "user:U123", + lastThreadId: "1739142736.000100", + }, + heartbeat: { + target: "last", + directPolicy: "block", + }, + }); + expect(resolved.channel).toBe("none"); expect(resolved.reason).toBe("dm-blocked"); expect(resolved.threadId).toBeUndefined(); }); - it("blocks heartbeat delivery to Discord DMs", () => { + it("allows heartbeat delivery to Discord DMs by default", () => { const cfg: OpenClawConfig = {}; const resolved = resolveHeartbeatDeliveryTarget({ cfg, @@ -337,11 +359,11 @@ describe("resolveSessionDeliveryTarget", () => { }, }); - expect(resolved.channel).toBe("none"); - expect(resolved.reason).toBe("dm-blocked"); + expect(resolved.channel).toBe("discord"); + expect(resolved.to).toBe("user:12345"); }); - it("blocks heartbeat delivery to Telegram direct chats", () => { + it("allows heartbeat delivery to Telegram direct chats by default", () => { const cfg: OpenClawConfig = {}; const resolved = resolveHeartbeatDeliveryTarget({ cfg, @@ -356,6 +378,26 @@ describe("resolveSessionDeliveryTarget", () => { }, }); + expect(resolved.channel).toBe("telegram"); + expect(resolved.to).toBe("5232990709"); + }); + + it("blocks heartbeat delivery to Telegram direct chats when directPolicy is block", () => { + const cfg: OpenClawConfig = {}; + const resolved = resolveHeartbeatDeliveryTarget({ + cfg, + entry: { + sessionId: "sess-heartbeat-telegram-direct", + updatedAt: 1, + lastChannel: "telegram", + lastTo: "5232990709", + }, + heartbeat: { + target: "last", + directPolicy: "block", + }, + }); + expect(resolved.channel).toBe("none"); expect(resolved.reason).toBe("dm-blocked"); }); @@ -379,7 +421,7 @@ describe("resolveSessionDeliveryTarget", () => { expect(resolved.to).toBe("-1001234567890"); }); - it("blocks heartbeat delivery to WhatsApp direct chats", () => { + it("allows heartbeat delivery to WhatsApp direct chats by default", () => { const cfg: OpenClawConfig = {}; const resolved = resolveHeartbeatDeliveryTarget({ cfg, @@ -394,8 +436,8 @@ describe("resolveSessionDeliveryTarget", () => { }, }); - expect(resolved.channel).toBe("none"); - expect(resolved.reason).toBe("dm-blocked"); + expect(resolved.channel).toBe("whatsapp"); + expect(resolved.to).toBe("+15551234567"); }); it("keeps heartbeat delivery to WhatsApp groups", () => { @@ -417,7 +459,27 @@ describe("resolveSessionDeliveryTarget", () => { expect(resolved.to).toBe("120363140186826074@g.us"); }); - it("uses session chatType hint when target parser cannot classify", () => { + it("uses session chatType hint when target parser cannot classify and allows direct by default", () => { + const cfg: OpenClawConfig = {}; + const resolved = resolveHeartbeatDeliveryTarget({ + cfg, + entry: { + sessionId: "sess-heartbeat-imessage-direct", + updatedAt: 1, + lastChannel: "imessage", + lastTo: "chat-guid-unknown-shape", + chatType: "direct", + }, + heartbeat: { + target: "last", + }, + }); + + expect(resolved.channel).toBe("imessage"); + expect(resolved.to).toBe("chat-guid-unknown-shape"); + }); + + it("blocks session chatType direct hints when directPolicy is block", () => { const cfg: OpenClawConfig = {}; const resolved = resolveHeartbeatDeliveryTarget({ cfg, @@ -430,6 +492,7 @@ describe("resolveSessionDeliveryTarget", () => { }, heartbeat: { target: "last", + directPolicy: "block", }, }); diff --git a/src/infra/outbound/targets.ts b/src/infra/outbound/targets.ts index d9411e2223c8..89e68e57566e 100644 --- a/src/infra/outbound/targets.ts +++ b/src/infra/outbound/targets.ts @@ -330,7 +330,7 @@ export function resolveHeartbeatDeliveryTarget(params: { to: resolved.to, sessionChatType: sessionChatTypeHint, }); - if (deliveryChatType === "direct") { + if (deliveryChatType === "direct" && heartbeat?.directPolicy === "block") { return buildNoHeartbeatDeliveryTarget({ reason: "dm-blocked", accountId: effectiveAccountId, From de61e9c9771899d76710251c2f445a75d6488644 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:59:08 +0100 Subject: [PATCH 096/160] refactor(security): unify path alias guard policies --- src/agents/apply-patch.ts | 13 ++--- src/agents/sandbox-paths.ts | 79 ++++++----------------------- src/agents/sandbox/fs-bridge.ts | 83 +++++------------------------- src/infra/hardlink-guards.ts | 4 +- src/infra/path-alias-guards.ts | 89 +++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 142 deletions(-) create mode 100644 src/infra/path-alias-guards.ts diff --git a/src/agents/apply-patch.ts b/src/agents/apply-patch.ts index 4b147fd79fb1..4f1487d34ea1 100644 --- a/src/agents/apply-patch.ts +++ b/src/agents/apply-patch.ts @@ -2,6 +2,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import type { AgentTool } from "@mariozechner/pi-agent-core"; import { Type } from "@sinclair/typebox"; +import { PATH_ALIAS_POLICIES, type PathAliasPolicy } from "../infra/path-alias-guards.js"; import { applyUpdateHunk } from "./apply-patch-update.js"; import { assertSandboxPath, resolveSandboxInputPath } from "./sandbox-paths.js"; import type { SandboxFsBridge } from "./sandbox/fs-bridge.js"; @@ -154,7 +155,7 @@ export async function applyPatch( } if (hunk.kind === "delete") { - const target = await resolvePatchPath(hunk.path, options, "unlink"); + const target = await resolvePatchPath(hunk.path, options, PATH_ALIAS_POLICIES.unlinkTarget); await fileOps.remove(target.resolved); recordSummary(summary, seen, "deleted", target.display); continue; @@ -253,7 +254,7 @@ async function ensureDir(filePath: string, ops: PatchFileOps) { async function resolvePatchPath( filePath: string, options: ApplyPatchOptions, - purpose: "readWrite" | "unlink" = "readWrite", + aliasPolicy: PathAliasPolicy = PATH_ALIAS_POLICIES.strict, ): Promise<{ resolved: string; display: string }> { if (options.sandbox) { const resolved = options.sandbox.bridge.resolvePath({ @@ -265,8 +266,8 @@ async function resolvePatchPath( filePath: resolved.hostPath, cwd: options.cwd, root: options.cwd, - allowFinalSymlink: purpose === "unlink", - allowFinalHardlink: purpose === "unlink", + allowFinalSymlinkForUnlink: aliasPolicy.allowFinalSymlinkForUnlink, + allowFinalHardlinkForUnlink: aliasPolicy.allowFinalHardlinkForUnlink, }); } return { @@ -282,8 +283,8 @@ async function resolvePatchPath( filePath, cwd: options.cwd, root: options.cwd, - allowFinalSymlink: purpose === "unlink", - allowFinalHardlink: purpose === "unlink", + allowFinalSymlinkForUnlink: aliasPolicy.allowFinalSymlinkForUnlink, + allowFinalHardlinkForUnlink: aliasPolicy.allowFinalHardlinkForUnlink, }) ).resolved : resolvePathFromCwd(filePath, options.cwd); diff --git a/src/agents/sandbox-paths.ts b/src/agents/sandbox-paths.ts index b50e90c32415..7cb026c28a4e 100644 --- a/src/agents/sandbox-paths.ts +++ b/src/agents/sandbox-paths.ts @@ -1,9 +1,8 @@ -import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath, URL } from "node:url"; -import { assertNoHardlinkedFinalPath } from "../infra/hardlink-guards.js"; -import { isNotFoundPathError, isPathInside } from "../infra/path-guards.js"; +import { assertNoPathAliasEscape, type PathAliasPolicy } from "../infra/path-alias-guards.js"; +import { isPathInside } from "../infra/path-guards.js"; import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js"; const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g; @@ -62,18 +61,19 @@ export async function assertSandboxPath(params: { filePath: string; cwd: string; root: string; - allowFinalSymlink?: boolean; - allowFinalHardlink?: boolean; + allowFinalSymlinkForUnlink?: boolean; + allowFinalHardlinkForUnlink?: boolean; }) { const resolved = resolveSandboxPath(params); - await assertNoSymlinkEscape(resolved.relative, path.resolve(params.root), { - allowFinalSymlink: params.allowFinalSymlink, - }); - await assertNoHardlinkedFinalPath({ - filePath: resolved.resolved, - root: path.resolve(params.root), + const policy: PathAliasPolicy = { + allowFinalSymlinkForUnlink: params.allowFinalSymlinkForUnlink, + allowFinalHardlinkForUnlink: params.allowFinalHardlinkForUnlink, + }; + await assertNoPathAliasEscape({ + absolutePath: resolved.resolved, + rootPath: path.resolve(params.root), boundaryLabel: "sandbox root", - allowFinalHardlink: params.allowFinalHardlink, + policy, }); return resolved; } @@ -202,62 +202,13 @@ async function assertNoTmpAliasEscape(params: { filePath: string; tmpRoot: string; }): Promise { - await assertNoSymlinkEscape(path.relative(params.tmpRoot, params.filePath), params.tmpRoot); - await assertNoHardlinkedFinalPath({ - filePath: params.filePath, - root: params.tmpRoot, + await assertNoPathAliasEscape({ + absolutePath: params.filePath, + rootPath: params.tmpRoot, boundaryLabel: "tmp root", }); } -async function assertNoSymlinkEscape( - relative: string, - root: string, - options?: { allowFinalSymlink?: boolean }, -) { - if (!relative) { - return; - } - const rootReal = await tryRealpath(root); - const parts = relative.split(path.sep).filter(Boolean); - let current = root; - for (let idx = 0; idx < parts.length; idx += 1) { - const part = parts[idx]; - const isLast = idx === parts.length - 1; - current = path.join(current, part); - try { - const stat = await fs.lstat(current); - if (stat.isSymbolicLink()) { - // Unlinking a symlink itself is safe even if it points outside the root. What we - // must prevent is traversing through a symlink to reach targets outside root. - if (options?.allowFinalSymlink && isLast) { - return; - } - const target = await tryRealpath(current); - if (!isPathInside(rootReal, target)) { - throw new Error( - `Symlink escapes sandbox root (${shortPath(rootReal)}): ${shortPath(current)}`, - ); - } - current = target; - } - } catch (err) { - if (isNotFoundPathError(err)) { - return; - } - throw err; - } - } -} - -async function tryRealpath(value: string): Promise { - try { - return await fs.realpath(value); - } catch { - return path.resolve(value); - } -} - function shortPath(value: string) { if (value.startsWith(os.homedir())) { return `~${value.slice(os.homedir().length)}`; diff --git a/src/agents/sandbox/fs-bridge.ts b/src/agents/sandbox/fs-bridge.ts index 18991f60da6d..23ebcce51b1a 100644 --- a/src/agents/sandbox/fs-bridge.ts +++ b/src/agents/sandbox/fs-bridge.ts @@ -1,7 +1,8 @@ -import fs from "node:fs/promises"; -import path from "node:path"; -import { assertNoHardlinkedFinalPath } from "../../infra/hardlink-guards.js"; -import { isNotFoundPathError, isPathInside } from "../../infra/path-guards.js"; +import { + assertNoPathAliasEscape, + PATH_ALIAS_POLICIES, + type PathAliasPolicy, +} from "../../infra/path-alias-guards.js"; import { execDockerRaw, type ExecDockerRawResult } from "./docker.js"; import { buildSandboxFsMounts, @@ -21,8 +22,7 @@ type RunCommandOptions = { type PathSafetyOptions = { action: string; - allowFinalSymlink?: boolean; - allowFinalHardlink?: boolean; + aliasPolicy?: PathAliasPolicy; requireWritable?: boolean; }; @@ -152,8 +152,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { await this.assertPathSafety(target, { action: "remove files", requireWritable: true, - allowFinalSymlink: true, - allowFinalHardlink: true, + aliasPolicy: PATH_ALIAS_POLICIES.unlinkTarget, }); const flags = [params.force === false ? "" : "-f", params.recursive ? "-r" : ""].filter( Boolean, @@ -178,8 +177,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { await this.assertPathSafety(from, { action: "rename files", requireWritable: true, - allowFinalSymlink: true, - allowFinalHardlink: true, + aliasPolicy: PATH_ALIAS_POLICIES.unlinkTarget, }); await this.assertPathSafety(to, { action: "rename files", @@ -256,21 +254,16 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { ); } - await assertNoHostSymlinkEscape({ + await assertNoPathAliasEscape({ absolutePath: target.hostPath, rootPath: lexicalMount.hostRoot, - allowFinalSymlink: options.allowFinalSymlink === true, - }); - await assertNoHardlinkedFinalPath({ - filePath: target.hostPath, - root: lexicalMount.hostRoot, boundaryLabel: "sandbox mount root", - allowFinalHardlink: options.allowFinalHardlink === true, + policy: options.aliasPolicy, }); const canonicalContainerPath = await this.resolveCanonicalContainerPath({ containerPath: target.containerPath, - allowFinalSymlink: options.allowFinalSymlink === true, + allowFinalSymlinkForUnlink: options.aliasPolicy?.allowFinalSymlinkForUnlink === true, }); const canonicalMount = this.resolveMountByContainerPath(canonicalContainerPath); if (!canonicalMount) { @@ -297,7 +290,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { private async resolveCanonicalContainerPath(params: { containerPath: string; - allowFinalSymlink: boolean; + allowFinalSymlinkForUnlink: boolean; }): Promise { const script = [ "set -eu", @@ -318,7 +311,7 @@ class SandboxFsBridgeImpl implements SandboxFsBridge { 'printf "%s%s\\n" "$canonical" "$suffix"', ].join("\n"); const result = await this.runCommand(script, { - args: [params.containerPath, params.allowFinalSymlink ? "1" : "0"], + args: [params.containerPath, params.allowFinalSymlinkForUnlink ? "1" : "0"], }); const canonical = result.stdout.toString("utf8").trim(); if (!canonical.startsWith("/")) { @@ -361,53 +354,3 @@ function coerceStatType(typeRaw?: string): "file" | "directory" | "other" { } return "other"; } - -async function assertNoHostSymlinkEscape(params: { - absolutePath: string; - rootPath: string; - allowFinalSymlink: boolean; -}): Promise { - const root = path.resolve(params.rootPath); - const target = path.resolve(params.absolutePath); - if (!isPathInside(root, target)) { - throw new Error(`Sandbox path escapes mount root (${root}): ${params.absolutePath}`); - } - const relative = path.relative(root, target); - if (!relative) { - return; - } - const rootReal = await tryRealpath(root); - const parts = relative.split(path.sep).filter(Boolean); - let current = root; - for (let idx = 0; idx < parts.length; idx += 1) { - current = path.join(current, parts[idx] ?? ""); - const isLast = idx === parts.length - 1; - try { - const stat = await fs.lstat(current); - if (!stat.isSymbolicLink()) { - continue; - } - if (params.allowFinalSymlink && isLast) { - return; - } - const symlinkTarget = await tryRealpath(current); - if (!isPathInside(rootReal, symlinkTarget)) { - throw new Error(`Symlink escapes sandbox mount root (${rootReal}): ${current}`); - } - current = symlinkTarget; - } catch (error) { - if (isNotFoundPathError(error)) { - return; - } - throw error; - } - } -} - -async function tryRealpath(value: string): Promise { - try { - return await fs.realpath(value); - } catch { - return path.resolve(value); - } -} diff --git a/src/infra/hardlink-guards.ts b/src/infra/hardlink-guards.ts index 9681bc09b785..ad99729b4637 100644 --- a/src/infra/hardlink-guards.ts +++ b/src/infra/hardlink-guards.ts @@ -6,9 +6,9 @@ export async function assertNoHardlinkedFinalPath(params: { filePath: string; root: string; boundaryLabel: string; - allowFinalHardlink?: boolean; + allowFinalHardlinkForUnlink?: boolean; }): Promise { - if (params.allowFinalHardlink) { + if (params.allowFinalHardlinkForUnlink) { return; } let stat: Awaited>; diff --git a/src/infra/path-alias-guards.ts b/src/infra/path-alias-guards.ts new file mode 100644 index 000000000000..86d08a3e44a0 --- /dev/null +++ b/src/infra/path-alias-guards.ts @@ -0,0 +1,89 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { assertNoHardlinkedFinalPath } from "./hardlink-guards.js"; +import { isNotFoundPathError, isPathInside } from "./path-guards.js"; + +export type PathAliasPolicy = { + allowFinalSymlinkForUnlink?: boolean; + allowFinalHardlinkForUnlink?: boolean; +}; + +export const PATH_ALIAS_POLICIES = { + strict: Object.freeze({ + allowFinalSymlinkForUnlink: false, + allowFinalHardlinkForUnlink: false, + }), + unlinkTarget: Object.freeze({ + allowFinalSymlinkForUnlink: true, + allowFinalHardlinkForUnlink: true, + }), +} as const; + +export async function assertNoPathAliasEscape(params: { + absolutePath: string; + rootPath: string; + boundaryLabel: string; + policy?: PathAliasPolicy; +}): Promise { + const root = path.resolve(params.rootPath); + const target = path.resolve(params.absolutePath); + if (!isPathInside(root, target)) { + throw new Error( + `Path escapes ${params.boundaryLabel} (${shortPath(root)}): ${shortPath(params.absolutePath)}`, + ); + } + const relative = path.relative(root, target); + if (relative) { + const rootReal = await tryRealpath(root); + const parts = relative.split(path.sep).filter(Boolean); + let current = root; + for (let idx = 0; idx < parts.length; idx += 1) { + current = path.join(current, parts[idx] ?? ""); + const isLast = idx === parts.length - 1; + try { + const stat = await fs.lstat(current); + if (!stat.isSymbolicLink()) { + continue; + } + if (params.policy?.allowFinalSymlinkForUnlink && isLast) { + return; + } + const symlinkTarget = await tryRealpath(current); + if (!isPathInside(rootReal, symlinkTarget)) { + throw new Error( + `Symlink escapes ${params.boundaryLabel} (${shortPath(rootReal)}): ${shortPath(current)}`, + ); + } + current = symlinkTarget; + } catch (error) { + if (isNotFoundPathError(error)) { + break; + } + throw error; + } + } + } + + await assertNoHardlinkedFinalPath({ + filePath: target, + root, + boundaryLabel: params.boundaryLabel, + allowFinalHardlinkForUnlink: params.policy?.allowFinalHardlinkForUnlink, + }); +} + +async function tryRealpath(value: string): Promise { + try { + return await fs.realpath(value); + } catch { + return path.resolve(value); + } +} + +function shortPath(value: string) { + if (value.startsWith(os.homedir())) { + return `~${value.slice(os.homedir().length)}`; + } + return value; +} From 4ada143794789dd8ee7eff523ae520034f321ae1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:59:30 +0100 Subject: [PATCH 097/160] docs(heartbeat): add directPolicy to config examples --- docs/gateway/configuration-examples.md | 1 + docs/gateway/heartbeat.md | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/gateway/configuration-examples.md b/docs/gateway/configuration-examples.md index d3838bbdae60..abc010ce8fe6 100644 --- a/docs/gateway/configuration-examples.md +++ b/docs/gateway/configuration-examples.md @@ -273,6 +273,7 @@ Save to `~/.openclaw/openclaw.json` and you can DM the bot from that number. every: "30m", model: "anthropic/claude-sonnet-4-5", target: "last", + directPolicy: "allow", // allow (default) | block to: "+15555550123", prompt: "HEARTBEAT", ackMaxChars: 300, diff --git a/docs/gateway/heartbeat.md b/docs/gateway/heartbeat.md index 70f4b968233b..a4f4aa64ea94 100644 --- a/docs/gateway/heartbeat.md +++ b/docs/gateway/heartbeat.md @@ -32,6 +32,7 @@ Example config: heartbeat: { every: "30m", target: "last", // explicit delivery to last contact (default is "none") + directPolicy: "allow", // default: allow direct/DM targets; set "block" to suppress // activeHours: { start: "08:00", end: "24:00" }, // includeReasoning: true, // optional: send separate `Reasoning:` message too }, From e16e8f5af2ce51565d39e9a56c6e5247b07a32b6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:01:19 +0100 Subject: [PATCH 098/160] refactor(slack): share system-event ingress and test harness --- src/slack/monitor/events/pins.test.ts | 58 +++---------------- src/slack/monitor/events/pins.ts | 32 ++++------ src/slack/monitor/events/reactions.test.ts | 58 +++---------------- src/slack/monitor/events/reactions.ts | 34 +++++------ .../monitor/events/system-event-context.ts | 44 ++++++++++++++ .../events/system-event-test-harness.ts | 56 ++++++++++++++++++ 6 files changed, 144 insertions(+), 138 deletions(-) create mode 100644 src/slack/monitor/events/system-event-context.ts create mode 100644 src/slack/monitor/events/system-event-test-harness.ts diff --git a/src/slack/monitor/events/pins.test.ts b/src/slack/monitor/events/pins.test.ts index 3bdae247613f..00c2528bbdb4 100644 --- a/src/slack/monitor/events/pins.test.ts +++ b/src/slack/monitor/events/pins.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it, vi } from "vitest"; -import type { SlackMonitorContext } from "../context.js"; import { registerSlackPinEvents } from "./pins.js"; +import { + createSlackSystemEventTestHarness, + type SlackSystemEventTestOverrides, +} from "./system-event-test-harness.js"; const enqueueSystemEventMock = vi.fn(); const readAllowFromStoreMock = vi.fn(); @@ -15,55 +18,12 @@ vi.mock("../../../pairing/pairing-store.js", () => ({ type SlackPinHandler = (args: { event: Record; body: unknown }) => Promise; -function createPinContext(overrides?: { - dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; - allowFrom?: string[]; - channelType?: "im" | "channel"; - channelUsers?: string[]; -}) { - let addedHandler: SlackPinHandler | null = null; - let removedHandler: SlackPinHandler | null = null; - const channelType = overrides?.channelType ?? "im"; - const app = { - event: vi.fn((name: string, handler: SlackPinHandler) => { - if (name === "pin_added") { - addedHandler = handler; - } else if (name === "pin_removed") { - removedHandler = handler; - } - }), - }; - const ctx = { - app, - runtime: { error: vi.fn() }, - dmEnabled: true, - dmPolicy: overrides?.dmPolicy ?? "open", - defaultRequireMention: true, - channelsConfig: overrides?.channelUsers - ? { - C1: { - users: overrides.channelUsers, - allow: true, - }, - } - : undefined, - groupPolicy: "open", - allowFrom: overrides?.allowFrom ?? [], - allowNameMatching: false, - shouldDropMismatchedSlackEvent: vi.fn().mockReturnValue(false), - isChannelAllowed: vi.fn().mockReturnValue(true), - resolveChannelName: vi.fn().mockResolvedValue({ - name: channelType === "im" ? "direct" : "general", - type: channelType, - }), - resolveUserName: vi.fn().mockResolvedValue({ name: "alice" }), - resolveSlackSystemEventSessionKey: vi.fn().mockReturnValue("agent:main:main"), - } as unknown as SlackMonitorContext; - registerSlackPinEvents({ ctx }); +function createPinContext(overrides?: SlackSystemEventTestOverrides) { + const harness = createSlackSystemEventTestHarness(overrides); + registerSlackPinEvents({ ctx: harness.ctx }); return { - ctx, - getAddedHandler: () => addedHandler, - getRemovedHandler: () => removedHandler, + getAddedHandler: () => harness.getHandler("pin_added") as SlackPinHandler | null, + getRemovedHandler: () => harness.getHandler("pin_removed") as SlackPinHandler | null, }; } diff --git a/src/slack/monitor/events/pins.ts b/src/slack/monitor/events/pins.ts index 89d0e2264e8c..9a63aa4a972f 100644 --- a/src/slack/monitor/events/pins.ts +++ b/src/slack/monitor/events/pins.ts @@ -1,10 +1,9 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt"; -import { danger, logVerbose } from "../../../globals.js"; +import { danger } from "../../../globals.js"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; -import { authorizeSlackSystemEventSender } from "../auth.js"; -import { resolveSlackChannelLabel } from "../channel-config.js"; import type { SlackMonitorContext } from "../context.js"; import type { SlackPinEvent } from "../types.js"; +import { authorizeAndResolveSlackSystemEventContext } from "./system-event-context.js"; async function handleSlackPinEvent(params: { ctx: SlackMonitorContext; @@ -23,33 +22,26 @@ async function handleSlackPinEvent(params: { const payload = event as SlackPinEvent; const channelId = payload.channel_id; - const auth = await authorizeSlackSystemEventSender({ + const ingressContext = await authorizeAndResolveSlackSystemEventContext({ ctx, senderId: payload.user, channelId, + eventKind: "pin", }); - if (!auth.allowed) { - logVerbose( - `slack: drop pin sender ${payload.user ?? "unknown"} channel=${channelId ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, - ); + if (!ingressContext) { return; } - const label = resolveSlackChannelLabel({ - channelId, - channelName: auth.channelName, - }); const userInfo = payload.user ? await ctx.resolveUserName(payload.user) : {}; const userLabel = userInfo?.name ?? payload.user ?? "someone"; const itemType = payload.item?.type ?? "item"; const messageId = payload.item?.message?.ts ?? payload.event_ts; - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId, - channelType: auth.channelType, - }); - enqueueSystemEvent(`Slack: ${userLabel} ${action} a ${itemType} in ${label}.`, { - sessionKey, - contextKey: `slack:pin:${contextKeySuffix}:${channelId ?? "unknown"}:${messageId ?? "unknown"}`, - }); + enqueueSystemEvent( + `Slack: ${userLabel} ${action} a ${itemType} in ${ingressContext.channelLabel}.`, + { + sessionKey: ingressContext.sessionKey, + contextKey: `slack:pin:${contextKeySuffix}:${channelId ?? "unknown"}:${messageId ?? "unknown"}`, + }, + ); } catch (err) { ctx.runtime.error?.(danger(`slack ${errorLabel} handler failed: ${String(err)}`)); } diff --git a/src/slack/monitor/events/reactions.test.ts b/src/slack/monitor/events/reactions.test.ts index bb64fbb5b4a6..e95a1ec5a8c3 100644 --- a/src/slack/monitor/events/reactions.test.ts +++ b/src/slack/monitor/events/reactions.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it, vi } from "vitest"; -import type { SlackMonitorContext } from "../context.js"; import { registerSlackReactionEvents } from "./reactions.js"; +import { + createSlackSystemEventTestHarness, + type SlackSystemEventTestOverrides, +} from "./system-event-test-harness.js"; const enqueueSystemEventMock = vi.fn(); const readAllowFromStoreMock = vi.fn(); @@ -18,55 +21,12 @@ type SlackReactionHandler = (args: { body: unknown; }) => Promise; -function createReactionContext(overrides?: { - dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; - allowFrom?: string[]; - channelType?: "im" | "channel"; - channelUsers?: string[]; -}) { - let addedHandler: SlackReactionHandler | null = null; - let removedHandler: SlackReactionHandler | null = null; - const channelType = overrides?.channelType ?? "im"; - const app = { - event: vi.fn((name: string, handler: SlackReactionHandler) => { - if (name === "reaction_added") { - addedHandler = handler; - } else if (name === "reaction_removed") { - removedHandler = handler; - } - }), - }; - const ctx = { - app, - runtime: { error: vi.fn() }, - dmEnabled: true, - dmPolicy: overrides?.dmPolicy ?? "open", - defaultRequireMention: true, - channelsConfig: overrides?.channelUsers - ? { - C1: { - users: overrides.channelUsers, - allow: true, - }, - } - : undefined, - groupPolicy: "open", - allowFrom: overrides?.allowFrom ?? [], - allowNameMatching: false, - shouldDropMismatchedSlackEvent: vi.fn().mockReturnValue(false), - isChannelAllowed: vi.fn().mockReturnValue(true), - resolveChannelName: vi.fn().mockResolvedValue({ - name: channelType === "im" ? "direct" : "general", - type: channelType, - }), - resolveUserName: vi.fn().mockResolvedValue({ name: "alice" }), - resolveSlackSystemEventSessionKey: vi.fn().mockReturnValue("agent:main:main"), - } as unknown as SlackMonitorContext; - registerSlackReactionEvents({ ctx }); +function createReactionContext(overrides?: SlackSystemEventTestOverrides) { + const harness = createSlackSystemEventTestHarness(overrides); + registerSlackReactionEvents({ ctx: harness.ctx }); return { - ctx, - getAddedHandler: () => addedHandler, - getRemovedHandler: () => removedHandler, + getAddedHandler: () => harness.getHandler("reaction_added") as SlackReactionHandler | null, + getRemovedHandler: () => harness.getHandler("reaction_removed") as SlackReactionHandler | null, }; } diff --git a/src/slack/monitor/events/reactions.ts b/src/slack/monitor/events/reactions.ts index 844b6c94080f..07dcf0f8be35 100644 --- a/src/slack/monitor/events/reactions.ts +++ b/src/slack/monitor/events/reactions.ts @@ -1,10 +1,9 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt"; -import { danger, logVerbose } from "../../../globals.js"; +import { danger } from "../../../globals.js"; import { enqueueSystemEvent } from "../../../infra/system-events.js"; -import { authorizeSlackSystemEventSender } from "../auth.js"; -import { resolveSlackChannelLabel } from "../channel-config.js"; import type { SlackMonitorContext } from "../context.js"; import type { SlackReactionEvent } from "../types.js"; +import { authorizeAndResolveSlackSystemEventContext } from "./system-event-context.js"; export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext }) { const { ctx } = params; @@ -16,35 +15,30 @@ export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext } return; } - const auth = await authorizeSlackSystemEventSender({ + const ingressContext = await authorizeAndResolveSlackSystemEventContext({ ctx, senderId: event.user, channelId: item.channel, + eventKind: "reaction", }); - if (!auth.allowed) { - logVerbose( - `slack: drop reaction sender ${event.user ?? "unknown"} channel=${item.channel ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, - ); + if (!ingressContext) { return; } - const channelLabel = resolveSlackChannelLabel({ - channelId: item.channel, - channelName: auth.channelName, - }); - const actorInfo = event.user ? await ctx.resolveUserName(event.user) : undefined; + const actorInfoPromise: Promise<{ name?: string } | undefined> = event.user + ? ctx.resolveUserName(event.user) + : Promise.resolve(undefined); + const authorInfoPromise: Promise<{ name?: string } | undefined> = event.item_user + ? ctx.resolveUserName(event.item_user) + : Promise.resolve(undefined); + const [actorInfo, authorInfo] = await Promise.all([actorInfoPromise, authorInfoPromise]); const actorLabel = actorInfo?.name ?? event.user; const emojiLabel = event.reaction ?? "emoji"; - const authorInfo = event.item_user ? await ctx.resolveUserName(event.item_user) : undefined; const authorLabel = authorInfo?.name ?? event.item_user; - const baseText = `Slack reaction ${action}: :${emojiLabel}: by ${actorLabel} in ${channelLabel} msg ${item.ts}`; + const baseText = `Slack reaction ${action}: :${emojiLabel}: by ${actorLabel} in ${ingressContext.channelLabel} msg ${item.ts}`; const text = authorLabel ? `${baseText} from ${authorLabel}` : baseText; - const sessionKey = ctx.resolveSlackSystemEventSessionKey({ - channelId: item.channel, - channelType: auth.channelType, - }); enqueueSystemEvent(text, { - sessionKey, + sessionKey: ingressContext.sessionKey, contextKey: `slack:reaction:${action}:${item.channel}:${item.ts}:${event.user}:${emojiLabel}`, }); } catch (err) { diff --git a/src/slack/monitor/events/system-event-context.ts b/src/slack/monitor/events/system-event-context.ts new file mode 100644 index 000000000000..5df48dfd1671 --- /dev/null +++ b/src/slack/monitor/events/system-event-context.ts @@ -0,0 +1,44 @@ +import { logVerbose } from "../../../globals.js"; +import { authorizeSlackSystemEventSender } from "../auth.js"; +import { resolveSlackChannelLabel } from "../channel-config.js"; +import type { SlackMonitorContext } from "../context.js"; + +export type SlackAuthorizedSystemEventContext = { + channelLabel: string; + sessionKey: string; +}; + +export async function authorizeAndResolveSlackSystemEventContext(params: { + ctx: SlackMonitorContext; + senderId?: string; + channelId?: string; + channelType?: string | null; + eventKind: string; +}): Promise { + const { ctx, senderId, channelId, channelType, eventKind } = params; + const auth = await authorizeSlackSystemEventSender({ + ctx, + senderId, + channelId, + channelType, + }); + if (!auth.allowed) { + logVerbose( + `slack: drop ${eventKind} sender ${senderId ?? "unknown"} channel=${channelId ?? "unknown"} reason=${auth.reason ?? "unauthorized"}`, + ); + return undefined; + } + + const channelLabel = resolveSlackChannelLabel({ + channelId, + channelName: auth.channelName, + }); + const sessionKey = ctx.resolveSlackSystemEventSessionKey({ + channelId, + channelType: auth.channelType, + }); + return { + channelLabel, + sessionKey, + }; +} diff --git a/src/slack/monitor/events/system-event-test-harness.ts b/src/slack/monitor/events/system-event-test-harness.ts new file mode 100644 index 000000000000..73a50d0444c2 --- /dev/null +++ b/src/slack/monitor/events/system-event-test-harness.ts @@ -0,0 +1,56 @@ +import type { SlackMonitorContext } from "../context.js"; + +export type SlackSystemEventHandler = (args: { + event: Record; + body: unknown; +}) => Promise; + +export type SlackSystemEventTestOverrides = { + dmPolicy?: "open" | "pairing" | "allowlist" | "disabled"; + allowFrom?: string[]; + channelType?: "im" | "channel"; + channelUsers?: string[]; +}; + +export function createSlackSystemEventTestHarness(overrides?: SlackSystemEventTestOverrides) { + const handlers: Record = {}; + const channelType = overrides?.channelType ?? "im"; + const app = { + event: (name: string, handler: SlackSystemEventHandler) => { + handlers[name] = handler; + }, + }; + const ctx = { + app, + runtime: { error: () => {} }, + dmEnabled: true, + dmPolicy: overrides?.dmPolicy ?? "open", + defaultRequireMention: true, + channelsConfig: overrides?.channelUsers + ? { + C1: { + users: overrides.channelUsers, + allow: true, + }, + } + : undefined, + groupPolicy: "open", + allowFrom: overrides?.allowFrom ?? [], + allowNameMatching: false, + shouldDropMismatchedSlackEvent: () => false, + isChannelAllowed: () => true, + resolveChannelName: async () => ({ + name: channelType === "im" ? "direct" : "general", + type: channelType, + }), + resolveUserName: async () => ({ name: "alice" }), + resolveSlackSystemEventSessionKey: () => "agent:main:main", + } as unknown as SlackMonitorContext; + + return { + ctx, + getHandler(name: string): SlackSystemEventHandler | null { + return handlers[name] ?? null; + }, + }; +} From b37dc42240726c4778ea193d172cfbb74bfa9912 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 03:07:27 +0000 Subject: [PATCH 099/160] fix(cron): suppress fallback summary after attempted announce delivery --- CHANGELOG.md | 1 + ...p-recipient-besteffortdeliver-true.test.ts | 28 +++++++++++++++++++ src/cron/isolated-agent/delivery-dispatch.ts | 16 +++++++++++ src/cron/isolated-agent/run.ts | 24 +++++++++++++--- ...runs-one-shot-main-job-disables-it.test.ts | 22 +++++++++++++++ src/cron/service/state.ts | 5 ++++ src/cron/service/timer.ts | 23 ++++++++++----- 7 files changed, 108 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0fa2607a195..717451a2e722 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ Docs: https://docs.openclaw.ai - Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. +- Cron/Announce duplicate guard: track attempted announce/direct delivery separately from confirmed `delivered`, and suppress fallback main-session cron summaries when delivery was already attempted to avoid duplicate end-user sends in uncertain-ack paths. (#27018) - Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. - Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. - Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. diff --git a/src/cron/isolated-agent.skips-delivery-without-whatsapp-recipient-besteffortdeliver-true.test.ts b/src/cron/isolated-agent.skips-delivery-without-whatsapp-recipient-besteffortdeliver-true.test.ts index 7d2dc3cf07a2..01a407692e09 100644 --- a/src/cron/isolated-agent.skips-delivery-without-whatsapp-recipient-besteffortdeliver-true.test.ts +++ b/src/cron/isolated-agent.skips-delivery-without-whatsapp-recipient-besteffortdeliver-true.test.ts @@ -56,6 +56,7 @@ async function expectBestEffortTelegramNotDelivered( expect(res.status).toBe("ok"); expect(res.delivered).toBe(false); + expect(res.deliveryAttempted).toBe(true); expect(runSubagentAnnounceFlow).not.toHaveBeenCalled(); expect(deps.sendMessageTelegram).toHaveBeenCalledTimes(1); }); @@ -287,6 +288,33 @@ describe("runCronIsolatedAgentTurn", () => { }); }); + it("marks attempted when announce delivery reports false and best-effort is enabled", async () => { + await withTempCronHome(async (home) => { + const storePath = await writeSessionStore(home, { lastProvider: "webchat", lastTo: "" }); + const deps = createCliDeps(); + mockAgentPayloads([{ text: "hello from cron" }]); + vi.mocked(runSubagentAnnounceFlow).mockResolvedValueOnce(false); + + const res = await runTelegramAnnounceTurn({ + home, + storePath, + deps, + delivery: { + mode: "announce", + channel: "telegram", + to: "123", + bestEffort: true, + }, + }); + + expect(res.status).toBe("ok"); + expect(res.delivered).toBe(false); + expect(res.deliveryAttempted).toBe(true); + expect(runSubagentAnnounceFlow).toHaveBeenCalledTimes(1); + expect(deps.sendMessageTelegram).not.toHaveBeenCalled(); + }); + }); + it("ignores structured direct delivery failures when best-effort is enabled", async () => { await expectBestEffortTelegramNotDelivered({ text: "hello from cron", diff --git a/src/cron/isolated-agent/delivery-dispatch.ts b/src/cron/isolated-agent/delivery-dispatch.ts index 697c0e2b8a80..1feae211df85 100644 --- a/src/cron/isolated-agent/delivery-dispatch.ts +++ b/src/cron/isolated-agent/delivery-dispatch.ts @@ -117,6 +117,7 @@ type DispatchCronDeliveryParams = { export type DispatchCronDeliveryState = { result?: RunCronAgentTurnResult; delivered: boolean; + deliveryAttempted: boolean; summary?: string; outputText?: string; synthesizedText?: string; @@ -134,6 +135,7 @@ export async function dispatchCronDelivery( // `true` means we confirmed at least one outbound send reached the target. // Keep this strict so timer fallback can safely decide whether to wake main. let delivered = params.skipMessagingToolDelivery; + let deliveryAttempted = params.skipMessagingToolDelivery; const failDeliveryTarget = (error: string) => params.withRunSession({ status: "error", @@ -141,6 +143,7 @@ export async function dispatchCronDelivery( errorKind: "delivery-target", summary, outputText, + deliveryAttempted, ...params.telemetry, }); @@ -162,9 +165,11 @@ export async function dispatchCronDelivery( return params.withRunSession({ status: "error", error: params.abortReason(), + deliveryAttempted, ...params.telemetry, }); } + deliveryAttempted = true; const deliveryResults = await deliverOutboundPayloads({ cfg: params.cfgWithAgentDefaults, channel: delivery.channel, @@ -187,6 +192,7 @@ export async function dispatchCronDelivery( summary, outputText, error: String(err), + deliveryAttempted, ...params.telemetry, }); } @@ -277,9 +283,11 @@ export async function dispatchCronDelivery( return params.withRunSession({ status: "error", error: params.abortReason(), + deliveryAttempted, ...params.telemetry, }); } + deliveryAttempted = true; const didAnnounce = await runSubagentAnnounceFlow({ childSessionKey: params.agentSessionKey, childRunId: `${params.job.id}:${params.runSessionId}:${params.runStartedAt}`, @@ -315,6 +323,7 @@ export async function dispatchCronDelivery( summary, outputText, error: message, + deliveryAttempted, ...params.telemetry, }); } @@ -327,6 +336,7 @@ export async function dispatchCronDelivery( summary, outputText, error: String(err), + deliveryAttempted, ...params.telemetry, }); } @@ -345,6 +355,7 @@ export async function dispatchCronDelivery( return { result: failDeliveryTarget(params.resolvedDelivery.error.message), delivered, + deliveryAttempted, summary, outputText, synthesizedText, @@ -357,9 +368,11 @@ export async function dispatchCronDelivery( status: "ok", summary, outputText, + deliveryAttempted, ...params.telemetry, }), delivered, + deliveryAttempted, summary, outputText, synthesizedText, @@ -383,6 +396,7 @@ export async function dispatchCronDelivery( return { result: directResult, delivered, + deliveryAttempted, summary, outputText, synthesizedText, @@ -395,6 +409,7 @@ export async function dispatchCronDelivery( return { result: announceResult, delivered, + deliveryAttempted, summary, outputText, synthesizedText, @@ -406,6 +421,7 @@ export async function dispatchCronDelivery( return { delivered, + deliveryAttempted, summary, outputText, synthesizedText, diff --git a/src/cron/isolated-agent/run.ts b/src/cron/isolated-agent/run.ts index 751ea2bc13ea..10b8b5c74145 100644 --- a/src/cron/isolated-agent/run.ts +++ b/src/cron/isolated-agent/run.ts @@ -77,6 +77,12 @@ export type RunCronAgentTurnResult = { * messages. See: https://github.com/openclaw/openclaw/issues/15692 */ delivered?: boolean; + /** + * `true` when cron attempted announce/direct delivery for this run. + * This is tracked separately from `delivered` because some announce paths + * cannot guarantee a final delivery ack synchronously. + */ + deliveryAttempted?: boolean; } & CronRunOutcome & CronRunTelemetry; @@ -565,7 +571,7 @@ export async function runCronIsolatedAgentTurn(params: { const embeddedRunError = hasErrorPayload ? (lastErrorPayloadText ?? "cron isolated run returned an error payload") : undefined; - const resolveRunOutcome = (params?: { delivered?: boolean }) => + const resolveRunOutcome = (params?: { delivered?: boolean; deliveryAttempted?: boolean }) => withRunSession({ status: hasErrorPayload ? "error" : "ok", ...(hasErrorPayload @@ -574,6 +580,7 @@ export async function runCronIsolatedAgentTurn(params: { summary, outputText, delivered: params?.delivered, + deliveryAttempted: params?.deliveryAttempted, ...telemetry, }); @@ -619,14 +626,23 @@ export async function runCronIsolatedAgentTurn(params: { withRunSession, }); if (deliveryResult.result) { + const resultWithDeliveryMeta: RunCronAgentTurnResult = { + ...deliveryResult.result, + deliveryAttempted: + deliveryResult.result.deliveryAttempted ?? deliveryResult.deliveryAttempted, + }; if (!hasErrorPayload || deliveryResult.result.status !== "ok") { - return deliveryResult.result; + return resultWithDeliveryMeta; } - return resolveRunOutcome({ delivered: deliveryResult.result.delivered }); + return resolveRunOutcome({ + delivered: deliveryResult.result.delivered, + deliveryAttempted: resultWithDeliveryMeta.deliveryAttempted, + }); } const delivered = deliveryResult.delivered; + const deliveryAttempted = deliveryResult.deliveryAttempted; summary = deliveryResult.summary; outputText = deliveryResult.outputText; - return resolveRunOutcome({ delivered }); + return resolveRunOutcome({ delivered, deliveryAttempted }); } diff --git a/src/cron/service.runs-one-shot-main-job-disables-it.test.ts b/src/cron/service.runs-one-shot-main-job-disables-it.test.ts index 027a464357df..37079addef0b 100644 --- a/src/cron/service.runs-one-shot-main-job-disables-it.test.ts +++ b/src/cron/service.runs-one-shot-main-job-disables-it.test.ts @@ -625,6 +625,28 @@ describe("CronService", () => { await store.cleanup(); }); + it("does not post isolated summary to main when announce delivery was attempted", async () => { + const runIsolatedAgentJob = vi.fn(async () => ({ + status: "ok" as const, + summary: "done", + delivered: false, + deliveryAttempted: true, + })); + const { store, cron, enqueueSystemEvent, requestHeartbeatNow, events } = + await createIsolatedAnnounceHarness(runIsolatedAgentJob); + await runIsolatedAnnounceJobAndWait({ + cron, + events, + name: "weekly attempted", + status: "ok", + }); + expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1); + expect(enqueueSystemEvent).not.toHaveBeenCalled(); + expect(requestHeartbeatNow).not.toHaveBeenCalled(); + cron.stop(); + await store.cleanup(); + }); + it("migrates legacy payload.provider to payload.channel on load", async () => { const rawJob = createLegacyDeliveryMigrationJob({ id: "legacy-1", diff --git a/src/cron/service/state.ts b/src/cron/service/state.ts index 19b139b3703e..3ad9cc1f591b 100644 --- a/src/cron/service/state.ts +++ b/src/cron/service/state.ts @@ -80,6 +80,11 @@ export type CronServiceDeps = { * https://github.com/openclaw/openclaw/issues/15692 */ delivered?: boolean; + /** + * `true` when announce/direct delivery was attempted for this run, even + * if the final per-message ack status is uncertain. + */ + deliveryAttempted?: boolean; } & CronRunOutcome & CronRunTelemetry >; diff --git a/src/cron/service/timer.ts b/src/cron/service/timer.ts index 34cdab97f5ab..acb3f3037d32 100644 --- a/src/cron/service/timer.ts +++ b/src/cron/service/timer.ts @@ -41,6 +41,7 @@ type TimedCronRunOutcome = CronRunOutcome & CronRunTelemetry & { jobId: string; delivered?: boolean; + deliveryAttempted?: boolean; startedAt: number; endedAt: number; }; @@ -606,7 +607,9 @@ export async function executeJobCore( state: CronServiceState, job: CronJob, abortSignal?: AbortSignal, -): Promise { +): Promise< + CronRunOutcome & CronRunTelemetry & { delivered?: boolean; deliveryAttempted?: boolean } +> { const resolveAbortError = () => ({ status: "error" as const, error: timeoutErrorMessage(), @@ -729,17 +732,22 @@ export async function executeJobCore( return { status: "error", error: timeoutErrorMessage() }; } - // Post a short summary back to the main session — but only when the - // isolated run did NOT already deliver its output to the target channel. - // When `res.delivered` is true the announce flow (or direct outbound - // delivery) already sent the result, so posting the summary to main - // would wake the main agent and cause a duplicate message. + // Post a short summary back to the main session only when announce + // delivery was requested and we are confident no outbound delivery path + // ran. If delivery was attempted but final ack is uncertain, suppress the + // main summary to avoid duplicate user-facing sends. // See: https://github.com/openclaw/openclaw/issues/15692 const summaryText = res.summary?.trim(); const deliveryPlan = resolveCronDeliveryPlan(job); const suppressMainSummary = res.status === "error" && res.errorKind === "delivery-target" && deliveryPlan.requested; - if (summaryText && deliveryPlan.requested && !res.delivered && !suppressMainSummary) { + if ( + summaryText && + deliveryPlan.requested && + !res.delivered && + res.deliveryAttempted !== true && + !suppressMainSummary + ) { const prefix = "Cron"; const label = res.status === "error" ? `${prefix} (error): ${summaryText}` : `${prefix}: ${summaryText}`; @@ -762,6 +770,7 @@ export async function executeJobCore( error: res.error, summary: res.summary, delivered: res.delivered, + deliveryAttempted: res.deliveryAttempted, sessionId: res.sessionId, sessionKey: res.sessionKey, model: res.model, From 8a97803474fb4589e377e51dd724c48e4ea36403 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:11:27 +0100 Subject: [PATCH 100/160] fix(agents): normalize malformed tool results in adapter (#27007) --- CHANGELOG.md | 1 + src/agents/pi-tool-definition-adapter.test.ts | 51 +++++++++++++++++ src/agents/pi-tool-definition-adapter.ts | 56 ++++++++++++++++++- 3 files changed, 107 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 717451a2e722..8f6b560f460c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ Docs: https://docs.openclaw.ai - Agents/Model fallback: keep same-provider fallback chains active when session model differs from configured primary, infer cooldown reason from provider profile state (instead of `disabledReason` only), keep no-profile fallback providers eligible (env/models.json paths), and only relax same-provider cooldown fallback attempts for `rate_limit`. (#23816) thanks @ramezgaberiel. - Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. - Agents/Model fallback: continue fallback traversal on unrecognized errors when candidates remain, while still throwing the original unknown error on the last candidate. (#26106) Thanks @Sid-Qin. +- Agents/Tools: normalize non-standard plugin tool results that omit `content` so embedded runs no longer crash with `Cannot read properties of undefined (reading 'filter')` after tool completion (including `tesseramemo_query`). (#27007) - Telegram/Markdown spoilers: keep valid `||spoiler||` pairs while leaving unmatched trailing `||` delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin. - Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. - Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. diff --git a/src/agents/pi-tool-definition-adapter.test.ts b/src/agents/pi-tool-definition-adapter.test.ts index 1b11bbf49be5..6def07167cbb 100644 --- a/src/agents/pi-tool-definition-adapter.test.ts +++ b/src/agents/pi-tool-definition-adapter.test.ts @@ -25,6 +25,15 @@ async function executeThrowingTool(name: string, callId: string) { return await def.execute(callId, {}, undefined, undefined, extensionContext); } +async function executeTool(tool: AgentTool, callId: string) { + const defs = toToolDefinitions([tool]); + const def = defs[0]; + if (!def) { + throw new Error("missing tool definition"); + } + return await def.execute(callId, {}, undefined, undefined, extensionContext); +} + describe("pi tool definition adapter", () => { it("wraps tool errors into a tool result", async () => { const result = await executeThrowingTool("boom", "call1"); @@ -46,4 +55,46 @@ describe("pi tool definition adapter", () => { error: "nope", }); }); + + it("coerces details-only tool results to include content", async () => { + const tool = { + name: "memory_query", + label: "Memory Query", + description: "returns details only", + parameters: Type.Object({}), + execute: (async () => ({ + details: { + hits: [{ id: "a1", score: 0.9 }], + }, + })) as unknown as AgentTool["execute"], + } satisfies AgentTool; + + const result = await executeTool(tool, "call3"); + expect(result.details).toEqual({ + hits: [{ id: "a1", score: 0.9 }], + }); + expect(result.content[0]).toMatchObject({ type: "text" }); + expect((result.content[0] as { text?: string }).text).toContain('"hits"'); + }); + + it("coerces non-standard object results to include content", async () => { + const tool = { + name: "memory_query_raw", + label: "Memory Query Raw", + description: "returns plain object", + parameters: Type.Object({}), + execute: (async () => ({ + count: 2, + ids: ["m1", "m2"], + })) as unknown as AgentTool["execute"], + } satisfies AgentTool; + + const result = await executeTool(tool, "call4"); + expect(result.details).toEqual({ + count: 2, + ids: ["m1", "m2"], + }); + expect(result.content[0]).toMatchObject({ type: "text" }); + expect((result.content[0] as { text?: string }).text).toContain('"count"'); + }); }); diff --git a/src/agents/pi-tool-definition-adapter.ts b/src/agents/pi-tool-definition-adapter.ts index f3963600c809..a62215862427 100644 --- a/src/agents/pi-tool-definition-adapter.ts +++ b/src/agents/pi-tool-definition-adapter.ts @@ -62,6 +62,56 @@ function describeToolExecutionError(err: unknown): { return { message: String(err) }; } +function stringifyToolPayload(payload: unknown): string { + if (typeof payload === "string") { + return payload; + } + try { + const encoded = JSON.stringify(payload, null, 2); + if (typeof encoded === "string") { + return encoded; + } + } catch { + // Fall through to String(payload) for non-serializable values. + } + return String(payload); +} + +function normalizeToolExecutionResult(params: { + toolName: string; + result: unknown; +}): AgentToolResult { + const { toolName, result } = params; + if (result && typeof result === "object") { + const record = result as Record; + if (Array.isArray(record.content)) { + return result as AgentToolResult; + } + logDebug(`tools: ${toolName} returned non-standard result (missing content[]); coercing`); + const details = "details" in record ? record.details : record; + const safeDetails = details ?? { status: "ok", tool: toolName }; + return { + content: [ + { + type: "text", + text: stringifyToolPayload(safeDetails), + }, + ], + details: safeDetails, + }; + } + const safeDetails = result ?? { status: "ok", tool: toolName }; + return { + content: [ + { + type: "text", + text: stringifyToolPayload(safeDetails), + }, + ], + details: safeDetails, + }; +} + function splitToolExecuteArgs(args: ToolExecuteArgsAny): { toolCallId: string; params: unknown; @@ -111,7 +161,11 @@ export function toToolDefinitions(tools: AnyAgentTool[]): ToolDefinition[] { } executeParams = hookOutcome.params; } - const result = await tool.execute(toolCallId, executeParams, signal, onUpdate); + const rawResult = await tool.execute(toolCallId, executeParams, signal, onUpdate); + const result = normalizeToolExecutionResult({ + toolName: normalizedName, + result: rawResult, + }); const afterParams = beforeHookWrapped ? (consumeAdjustedParamsForToolCall(toolCallId) ?? executeParams) : executeParams; From 8f8e2b13b47b48762e0d288e588f9805a37b3265 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:12:26 +0100 Subject: [PATCH 101/160] fix: disable tts tool for voice provider --- CHANGELOG.md | 1 + ...s-claude-style-aliases-schemas-without-dropping.test.ts | 5 +++++ src/agents/pi-tools.ts | 7 ++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f6b560f460c..83c55a178fab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ Docs: https://docs.openclaw.ai - Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. - Telegram/Preview cleanup: keep finalized text previews when a later assistant message is media-only (for example mixed text plus voice turns) by skipping finalized preview archival at assistant-message boundaries, preventing cleanup from deleting already-visible final text messages. (#27042) - Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example `c0abc12345`) correctly match Slack runtime IDs (`C0ABC12345`) under `groupPolicy: "allowlist"`, preventing silent channel-event drops. (#26878) Thanks @lbo728. +- Voice-call/TTS tools: hide the `tts` tool when the message provider is `voice`, preventing voice-call runs from selecting self-playback TTS and falling into silent no-output loops. (#27025) - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. - Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. diff --git a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts index 22d68f15ff82..4f2b0be9f479 100644 --- a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts +++ b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts @@ -319,6 +319,11 @@ describe("createOpenClawCodingTools", () => { expect(names.has("telegram")).toBe(false); expect(names.has("whatsapp")).toBe(false); }); + it("does not expose tts tool for voice message provider", () => { + const tools = createOpenClawCodingTools({ messageProvider: "voice" }); + const names = new Set(tools.map((tool) => tool.name)); + expect(names.has("tts")).toBe(false); + }); it("filters session tools for sub-agent sessions by default", () => { const tools = createOpenClawCodingTools({ sessionKey: "agent:main:subagent:test", diff --git a/src/agents/pi-tools.ts b/src/agents/pi-tools.ts index e2d29d375da3..f4252f562bb9 100644 --- a/src/agents/pi-tools.ts +++ b/src/agents/pi-tools.ts @@ -217,6 +217,8 @@ export function createOpenClawCodingTools(options?: { /** Whether the sender is an owner (required for owner-only tools). */ senderIsOwner?: boolean; }): AnyAgentTool[] { + const rawMessageProvider = options?.messageProvider?.trim().toLowerCase(); + const isVoiceMessageProvider = rawMessageProvider === "voice"; const execToolName = "exec"; const sandbox = options?.sandbox?.enabled ? options.sandbox : undefined; const { @@ -480,9 +482,12 @@ export function createOpenClawCodingTools(options?: { senderIsOwner: options?.senderIsOwner, }), ]; + const toolsForMessageProvider = isVoiceMessageProvider + ? tools.filter((tool) => tool.name !== "tts") + : tools; // Security: treat unknown/undefined as unauthorized (opt-in, not opt-out) const senderIsOwner = options?.senderIsOwner === true; - const toolsByAuthorization = applyOwnerOnlyToolPolicy(tools, senderIsOwner); + const toolsByAuthorization = applyOwnerOnlyToolPolicy(toolsForMessageProvider, senderIsOwner); const subagentFiltered = applyToolPolicyPipeline({ tools: toolsByAuthorization, toolMeta: (tool) => getPluginToolMeta(tool), From f789f880c934caa8be25b38832f27f90f37903db Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:13:59 +0100 Subject: [PATCH 102/160] fix(security): harden approval-bound node exec cwd handling --- CHANGELOG.md | 1 + src/cli/nodes-cli.coverage.test.ts | 9 ++ src/cli/nodes-cli/register.invoke.ts | 1 + src/node-host/invoke-system-run.test.ts | 67 ++++++++++++++ src/node-host/invoke-system-run.ts | 113 ++++++++++++++++++++++++ 5 files changed, 191 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83c55a178fab..c472e545356c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ Docs: https://docs.openclaw.ai - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. - Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Exec approvals: bind `system.run` approval matching to exact argv identity and preserve argv whitespace in rendered command text, preventing trailing-space executable path swaps from reusing a mismatched approval. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Exec approvals: harden approval-bound `system.run` execution on node hosts by rejecting symlink `cwd` paths and canonicalizing path-like executable argv before spawn, blocking mutable-cwd symlink retarget chains between approval and execution. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack reactions + pins: gate `reaction_*` and `pin_*` system-event enqueue through shared sender authorization so DM `dmPolicy`/`allowFrom` and channel `users` allowlists are enforced consistently for non-message ingress, with regression coverage for denied/allowed sender paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. diff --git a/src/cli/nodes-cli.coverage.test.ts b/src/cli/nodes-cli.coverage.test.ts index b8ddc75308c5..cd3fe62857d4 100644 --- a/src/cli/nodes-cli.coverage.test.ts +++ b/src/cli/nodes-cli.coverage.test.ts @@ -83,6 +83,11 @@ describe("nodes-cli coverage", () => { const getNodeInvokeCall = () => callGateway.mock.calls.find((call) => call[0]?.method === "node.invoke")?.[0] as NodeInvokeCall; + const getApprovalRequestCall = () => + callGateway.mock.calls.find((call) => call[0]?.method === "exec.approval.request")?.[0] as { + params?: Record; + }; + const createNodesProgram = () => { const program = new Command(); program.exitOverride(); @@ -140,6 +145,8 @@ describe("nodes-cli coverage", () => { runId: expect.any(String), }); expect(invoke?.params?.timeoutMs).toBe(5000); + const approval = getApprovalRequestCall(); + expect(approval?.params?.["commandArgv"]).toEqual(["echo", "hi"]); }); it("invokes system.run with raw command", async () => { @@ -165,6 +172,8 @@ describe("nodes-cli coverage", () => { approvalDecision: "allow-once", runId: expect.any(String), }); + const approval = getApprovalRequestCall(); + expect(approval?.params?.["commandArgv"]).toEqual(["/bin/sh", "-lc", "echo hi"]); }); it("invokes system.notify with provided fields", async () => { diff --git a/src/cli/nodes-cli/register.invoke.ts b/src/cli/nodes-cli/register.invoke.ts index a53cc783041e..e644d754d12f 100644 --- a/src/cli/nodes-cli/register.invoke.ts +++ b/src/cli/nodes-cli/register.invoke.ts @@ -252,6 +252,7 @@ export function registerNodesInvokeCommands(nodes: Command) { { id: approvalId, command: rawCommand ?? argv.join(" "), + commandArgv: argv, cwd: opts.cwd, nodeId, host: "node", diff --git a/src/node-host/invoke-system-run.test.ts b/src/node-host/invoke-system-run.test.ts index d19171990675..2682edd2423d 100644 --- a/src/node-host/invoke-system-run.test.ts +++ b/src/node-host/invoke-system-run.test.ts @@ -49,6 +49,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { preferMacAppExecHost: boolean; runViaResponse?: ExecHostResponse | null; command?: string[]; + cwd?: string; security?: "full" | "allowlist"; ask?: "off" | "on-miss" | "always"; approved?: boolean; @@ -70,6 +71,7 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { client: {} as never, params: { command: params.command ?? ["echo", "ok"], + cwd: params.cwd, approved: params.approved ?? false, sessionKey: "agent:main:main", }, @@ -214,6 +216,71 @@ describe("handleSystemRunInvoke mac app exec host routing", () => { }), ); }); + + it.runIf(process.platform !== "win32")( + "denies approval-based execution when cwd is a symlink", + async () => { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-approval-cwd-link-")); + const safeDir = path.join(tmp, "safe"); + const linkDir = path.join(tmp, "cwd-link"); + const script = path.join(safeDir, "run.sh"); + fs.mkdirSync(safeDir, { recursive: true }); + fs.writeFileSync(script, "#!/bin/sh\necho SAFE\n"); + fs.chmodSync(script, 0o755); + fs.symlinkSync(safeDir, linkDir, "dir"); + try { + const { runCommand, sendInvokeResult } = await runSystemInvoke({ + preferMacAppExecHost: false, + command: ["./run.sh"], + cwd: linkDir, + approved: true, + security: "full", + ask: "off", + }); + expect(runCommand).not.toHaveBeenCalled(); + expect(sendInvokeResult).toHaveBeenCalledWith( + expect.objectContaining({ + ok: false, + error: expect.objectContaining({ + message: expect.stringContaining("canonical cwd"), + }), + }), + ); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + }, + ); + + it("uses canonical executable path for approval-based relative command execution", async () => { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-approval-cwd-real-")); + const script = path.join(tmp, "run.sh"); + fs.writeFileSync(script, "#!/bin/sh\necho SAFE\n"); + fs.chmodSync(script, 0o755); + try { + const { runCommand, sendInvokeResult } = await runSystemInvoke({ + preferMacAppExecHost: false, + command: ["./run.sh", "--flag"], + cwd: tmp, + approved: true, + security: "full", + ask: "off", + }); + expect(runCommand).toHaveBeenCalledWith( + [fs.realpathSync(script), "--flag"], + fs.realpathSync(tmp), + undefined, + undefined, + ); + expect(sendInvokeResult).toHaveBeenCalledWith( + expect.objectContaining({ + ok: true, + }), + ); + } finally { + fs.rmSync(tmp, { recursive: true, force: true }); + } + }); it("denies ./sh wrapper spoof in allowlist on-miss mode before execution", async () => { const marker = path.join(os.tmpdir(), `openclaw-wrapper-spoof-${process.pid}-${Date.now()}`); const runCommand = vi.fn(async () => { diff --git a/src/node-host/invoke-system-run.ts b/src/node-host/invoke-system-run.ts index 39e6766f7d55..93edb85e0b7f 100644 --- a/src/node-host/invoke-system-run.ts +++ b/src/node-host/invoke-system-run.ts @@ -1,4 +1,6 @@ import crypto from "node:crypto"; +import fs from "node:fs"; +import path from "node:path"; import { resolveAgentConfig } from "../agents/agent-scope.js"; import { loadConfig } from "../config/config.js"; import type { GatewayClient } from "../gateway/client.js"; @@ -18,6 +20,7 @@ import { } from "../infra/exec-approvals.js"; import type { ExecHostRequest, ExecHostResponse, ExecHostRunResult } from "../infra/exec-host.js"; import { resolveExecSafeBinRuntimePolicy } from "../infra/exec-safe-bin-runtime-policy.js"; +import { sameFileIdentity } from "../infra/file-identity.js"; import { sanitizeSystemRunEnvOverrides } from "../infra/host-env-security.js"; import { resolveSystemRunCommand } from "../infra/system-run-command.js"; import { evaluateSystemRunPolicy, resolveExecApprovalDecision } from "./exec-policy.js"; @@ -110,6 +113,100 @@ function normalizeDeniedReason(reason: string | null | undefined): SystemRunDeni } } +function isPathLikeExecutableToken(value: string): boolean { + if (!value) { + return false; + } + if (value.startsWith(".") || value.startsWith("/") || value.startsWith("\\")) { + return true; + } + if (value.includes("/") || value.includes("\\")) { + return true; + } + if (process.platform === "win32" && /^[a-zA-Z]:[\\/]/.test(value)) { + return true; + } + return false; +} + +function hardenApprovedExecutionPaths(params: { + approvedByAsk: boolean; + argv: string[]; + shellCommand: string | null; + cwd: string | undefined; +}): { ok: true; argv: string[]; cwd: string | undefined } | { ok: false; message: string } { + if (!params.approvedByAsk) { + return { ok: true, argv: params.argv, cwd: params.cwd }; + } + + let hardenedCwd = params.cwd; + if (hardenedCwd) { + const requestedCwd = path.resolve(hardenedCwd); + let cwdLstat: fs.Stats; + let cwdStat: fs.Stats; + let cwdReal: string; + let cwdRealStat: fs.Stats; + try { + cwdLstat = fs.lstatSync(requestedCwd); + cwdStat = fs.statSync(requestedCwd); + cwdReal = fs.realpathSync(requestedCwd); + cwdRealStat = fs.statSync(cwdReal); + } catch { + return { + ok: false, + message: "SYSTEM_RUN_DENIED: approval requires an existing canonical cwd", + }; + } + if (!cwdStat.isDirectory()) { + return { + ok: false, + message: "SYSTEM_RUN_DENIED: approval requires cwd to be a directory", + }; + } + if (cwdLstat.isSymbolicLink()) { + return { + ok: false, + message: "SYSTEM_RUN_DENIED: approval requires canonical cwd (no symlink cwd)", + }; + } + if ( + !sameFileIdentity(cwdStat, cwdLstat) || + !sameFileIdentity(cwdStat, cwdRealStat) || + !sameFileIdentity(cwdLstat, cwdRealStat) + ) { + return { + ok: false, + message: "SYSTEM_RUN_DENIED: approval cwd identity mismatch", + }; + } + hardenedCwd = cwdReal; + } + + if (params.shellCommand !== null || params.argv.length === 0) { + return { ok: true, argv: params.argv, cwd: hardenedCwd }; + } + + const argv = [...params.argv]; + const rawExecutable = argv[0] ?? ""; + if (!isPathLikeExecutableToken(rawExecutable)) { + return { ok: true, argv, cwd: hardenedCwd }; + } + + const base = hardenedCwd ?? process.cwd(); + const candidate = path.isAbsolute(rawExecutable) + ? rawExecutable + : path.resolve(base, rawExecutable); + try { + argv[0] = fs.realpathSync(candidate); + } catch { + return { + ok: false, + message: "SYSTEM_RUN_DENIED: approval requires a stable executable path", + }; + } + return { ok: true, argv, cwd: hardenedCwd }; +} + export type HandleSystemRunInvokeOptions = { client: GatewayClient; params: SystemRunParams; @@ -422,6 +519,20 @@ async function evaluateSystemRunPolicyPhase( return null; } + const hardenedPaths = hardenApprovedExecutionPaths({ + approvedByAsk: policy.approvedByAsk, + argv: parsed.argv, + shellCommand: parsed.shellCommand, + cwd: parsed.cwd, + }); + if (!hardenedPaths.ok) { + await sendSystemRunDenied(opts, parsed.execution, { + reason: "approval-required", + message: hardenedPaths.message, + }); + return null; + } + const plannedAllowlistArgv = resolvePlannedAllowlistArgv({ security, shellCommand: parsed.shellCommand, @@ -437,6 +548,8 @@ async function evaluateSystemRunPolicyPhase( } return { ...parsed, + argv: hardenedPaths.argv, + cwd: hardenedPaths.cwd, approvals, security, policy, From e4d62c21be2f5454749d1a22b11f8f2dd6594d7f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:15:04 +0100 Subject: [PATCH 103/160] test: expand voice provider tts regression coverage --- ...-style-aliases-schemas-without-dropping.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts index 4f2b0be9f479..e074b6f91893 100644 --- a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts +++ b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts @@ -319,10 +319,18 @@ describe("createOpenClawCodingTools", () => { expect(names.has("telegram")).toBe(false); expect(names.has("whatsapp")).toBe(false); }); - it("does not expose tts tool for voice message provider", () => { - const tools = createOpenClawCodingTools({ messageProvider: "voice" }); + it.each(["voice", "VOICE", " Voice "])( + "does not expose tts tool for normalized voice message provider: %s", + (messageProvider) => { + const tools = createOpenClawCodingTools({ messageProvider }); + const names = new Set(tools.map((tool) => tool.name)); + expect(names.has("tts")).toBe(false); + }, + ); + it("keeps tts tool for non-voice providers", () => { + const tools = createOpenClawCodingTools({ messageProvider: "discord" }); const names = new Set(tools.map((tool) => tool.name)); - expect(names.has("tts")).toBe(false); + expect(names.has("tts")).toBe(true); }); it("filters session tools for sub-agent sessions by default", () => { const tools = createOpenClawCodingTools({ From f55238e72a61a839d585364c689c6fa006136289 Mon Sep 17 00:00:00 2001 From: Hongwei Ma Date: Thu, 26 Feb 2026 00:35:11 +0800 Subject: [PATCH 104/160] chore: remove accidental PR_STATUS.md from repo This file appears to be a personal agent tracking document that was accidentally committed to the main repository. It contains internal PR submission plans and CI status tracking that doesn't belong in the upstream codebase. --- PR_STATUS.md | 78 ---------------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 PR_STATUS.md diff --git a/PR_STATUS.md b/PR_STATUS.md deleted file mode 100644 index 1887eca27d95..000000000000 --- a/PR_STATUS.md +++ /dev/null @@ -1,78 +0,0 @@ -# OpenClaw PR Submission Status - -> Auto-maintained by agent team. Last updated: 2026-02-22 - -## PR Plan Overview - -All PRs target upstream `openclaw/openclaw` via fork `kevinWangSheng/openclaw`. -Each PR follows [CONTRIBUTING.md](./CONTRIBUTING.md) and uses the [PR template](./.github/PULL_REQUEST_TEMPLATE.md). - -## Duplicate Check - -Before submission, each PR was cross-referenced against: - -- 100+ open upstream PRs (as of 2026-02-22) -- 50 recently merged PRs -- 50+ open issues - -No overlap found with existing PRs. - -## PR Status Table - -| # | Branch | Title | Type | Status | PR URL | -| --- | -------------------------------------- | --------------------------------------------------------------------------- | -------- | --------------- | --------------------------------------------------------- | -| 1 | `security/redos-safe-regex` | fix(security): add ReDoS protection for user-controlled regex patterns | Security | CI Pass | [#23670](https://github.com/openclaw/openclaw/pull/23670) | -| 2 | `security/session-slug-crypto-random` | fix(security): use crypto.randomInt for session slug generation | Security | CI Pass | [#23671](https://github.com/openclaw/openclaw/pull/23671) | -| 3 | `fix/json-parse-crash-guard` | fix(resilience): guard JSON.parse of external process output with try-catch | Bug fix | CI Pass | [#23672](https://github.com/openclaw/openclaw/pull/23672) | -| 4 | `refactor/console-to-subsystem-logger` | refactor(logging): migrate remaining console calls to subsystem logger | Refactor | CI Pass | [#23669](https://github.com/openclaw/openclaw/pull/23669) | -| 5 | `fix/sanitize-rpc-error-messages` | fix(security): sanitize RPC error messages in signal and imessage clients | Security | CI Pass | [#23724](https://github.com/openclaw/openclaw/pull/23724) | -| 6 | `fix/download-stream-cleanup` | fix(resilience): destroy write streams on download errors | Bug fix | CI Pass | [#23726](https://github.com/openclaw/openclaw/pull/23726) | -| 7 | `fix/telegram-status-reaction-cleanup` | fix(telegram): clear done reaction when removeAckAfterReply is true | Bug fix | CI Pass | [#23728](https://github.com/openclaw/openclaw/pull/23728) | -| 8 | `fix/session-cache-eviction` | fix(memory): add max size eviction to session manager cache | Bug fix | CI Pass (17/17) | [#23744](https://github.com/openclaw/openclaw/pull/23744) | -| 9 | `fix/fetch-missing-timeout` | fix(resilience): add timeout to unguarded fetch calls in browser subsystem | Bug fix | CI Pass (18/18) | [#23745](https://github.com/openclaw/openclaw/pull/23745) | -| 10 | `fix/skills-download-partial-cleanup` | fix(resilience): clean up partial file on skill download failure | Bug fix | CI Pass (19/19) | [#24141](https://github.com/openclaw/openclaw/pull/24141) | -| 11 | `fix/extension-relay-stop-cleanup` | fix(browser): flush pending extension timers on relay stop | Bug fix | CI Pass (20/20) | [#24142](https://github.com/openclaw/openclaw/pull/24142) | - -## Isolation Rules - -- Each agent works on a separate git worktree branch -- No two agents modify the same file -- File ownership: - - PR 1: `src/infra/exec-approval-forwarder.ts`, `src/discord/monitor/exec-approvals.ts` - - PR 2: `src/agents/session-slug.ts` - - PR 3: `src/infra/bonjour-discovery.ts`, `src/infra/outbound/delivery-queue.ts` - - PR 4: `src/infra/tailscale.ts`, `src/node-host/runner.ts` - - PR 5: `src/signal/client.ts`, `src/imessage/client.ts` - - PR 6: `src/media/store.ts`, `src/commands/signal-install.ts` - - PR 7: `src/telegram/bot-message-dispatch.ts` - - PR 8: `src/agents/pi-embedded-runner/session-manager-cache.ts` - - PR 9: `src/cli/nodes-camera.ts`, `src/browser/pw-session.ts` - - PR 10: `src/agents/skills-install-download.ts` - - PR 11: `src/browser/extension-relay.ts` - -## Verification Results - -### Batch 1 (PRs 1-4) — All CI Green - -- PR 1: 17 tests pass, check/build/tests all green -- PR 2: 3 tests pass, check/build/tests all green -- PR 3: 45 tests pass (3 new), check/build/tests all green -- PR 4: 12 tests pass, check/build/tests all green - -### Batch 2 (PRs 5-7) — CI Running - -- PR 5: 3 signal tests pass, check pass, awaiting full test suite -- PR 6: 38 tests pass (20 media + 18 signal-install), check pass, awaiting full suite -- PR 7: 47 tests pass (3 new), check pass, awaiting full suite - -### Batch 3 (PRs 8-9) — All CI Green - -- PR 8 & 9: Initially failed due to pre-existing upstream TS errors + Windows flaky test. Fixed by rebasing onto latest upstream/main and removing `yieldMs: 10` from flaky sandbox test. -- PR 8: 17/17 pass, check/build/tests/windows all green -- PR 9: 18/18 pass, check/build/tests/windows all green - -### Batch 4 (PRs 10-11) — All CI Green - -- PR 10 & 11: Initially failed Windows flaky test (`yieldMs: 10` race). Fixed by removing `yieldMs: 10` from flaky sandbox test (same fix as PRs 8-9). -- PR 10: 19/19 pass, check/build/tests/windows all green -- PR 11: 20/20 pass, check/build/tests/windows all green From 243e28df4fb087869bc80946a2ad23b5db4b5075 Mon Sep 17 00:00:00 2001 From: SidQin-cyber Date: Wed, 25 Feb 2026 21:06:44 +0800 Subject: [PATCH 105/160] fix(line): keep startAccount pending until abort signal to prevent restart loop monitorLineProvider() registers the webhook HTTP route and returns immediately. Because startAccount() directly returned that resolved promise, the channel supervisor interpreted it as "provider exited" and triggered auto-restart up to 10 times. Await a promise gated on ctx.abortSignal so startAccount stays alive for the full provider lifecycle, matching the contract expected by the channel supervisor. Closes #26478 Co-authored-by: Cursor --- extensions/line/src/channel.startup.test.ts | 59 ++++++++++++++++++++- extensions/line/src/channel.ts | 16 +++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/extensions/line/src/channel.startup.test.ts b/extensions/line/src/channel.startup.test.ts index e5b0ce333f5c..11ba80bda126 100644 --- a/extensions/line/src/channel.startup.test.ts +++ b/extensions/line/src/channel.startup.test.ts @@ -37,6 +37,7 @@ function createStartAccountCtx(params: { token: string; secret: string; runtime: ReturnType; + abortSignal?: AbortSignal; }): ChannelGatewayContext { const snapshot: ChannelAccountSnapshot = { accountId: "default", @@ -56,7 +57,7 @@ function createStartAccountCtx(params: { }, cfg: {} as OpenClawConfig, runtime: params.runtime, - abortSignal: new AbortController().signal, + abortSignal: params.abortSignal ?? new AbortController().signal, log: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, getStatus: () => snapshot, setStatus: vi.fn(), @@ -104,14 +105,19 @@ describe("linePlugin gateway.startAccount", () => { const { runtime, monitorLineProvider } = createRuntime(); setLineRuntime(runtime); - await linePlugin.gateway!.startAccount!( + const abort = new AbortController(); + const task = linePlugin.gateway!.startAccount!( createStartAccountCtx({ token: "token", secret: "secret", runtime: createRuntimeEnv(), + abortSignal: abort.signal, }), ); + // Allow async internals (probeLineBot await) to flush + await new Promise((r) => setTimeout(r, 20)); + expect(monitorLineProvider).toHaveBeenCalledWith( expect.objectContaining({ channelAccessToken: "token", @@ -119,5 +125,54 @@ describe("linePlugin gateway.startAccount", () => { accountId: "default", }), ); + + abort.abort(); + await task; + }); + + it("stays pending until abort signal fires (no premature exit)", async () => { + const { runtime, monitorLineProvider } = createRuntime(); + setLineRuntime(runtime); + + const abort = new AbortController(); + let resolved = false; + + const task = linePlugin.gateway!.startAccount!( + createStartAccountCtx({ + token: "token", + secret: "secret", + runtime: createRuntimeEnv(), + abortSignal: abort.signal, + }), + ).then(() => { + resolved = true; + }); + + // Allow async internals to flush + await new Promise((r) => setTimeout(r, 50)); + + expect(monitorLineProvider).toHaveBeenCalled(); + expect(resolved).toBe(false); + + abort.abort(); + await task; + expect(resolved).toBe(true); + }); + + it("resolves immediately when abortSignal is already aborted", async () => { + const { runtime } = createRuntime(); + setLineRuntime(runtime); + + const abort = new AbortController(); + abort.abort(); + + await linePlugin.gateway!.startAccount!( + createStartAccountCtx({ + token: "token", + secret: "secret", + runtime: createRuntimeEnv(), + abortSignal: abort.signal, + }), + ); }); }); diff --git a/extensions/line/src/channel.ts b/extensions/line/src/channel.ts index a260d96c9615..f37a86aa0c4e 100644 --- a/extensions/line/src/channel.ts +++ b/extensions/line/src/channel.ts @@ -651,7 +651,7 @@ export const linePlugin: ChannelPlugin = { ctx.log?.info(`[${account.accountId}] starting LINE provider${lineBotLabel}`); - return getLineRuntime().channel.line.monitorLineProvider({ + const monitor = await getLineRuntime().channel.line.monitorLineProvider({ channelAccessToken: token, channelSecret: secret, accountId: account.accountId, @@ -660,6 +660,20 @@ export const linePlugin: ChannelPlugin = { abortSignal: ctx.abortSignal, webhookPath: account.config.webhookPath, }); + + // Keep the provider alive until the abort signal fires. Without this, + // the startAccount promise resolves immediately after webhook registration + // and the channel supervisor treats the provider as "exited", triggering an + // auto-restart loop (up to 10 attempts). + await new Promise((resolve) => { + if (ctx.abortSignal.aborted) { + resolve(); + return; + } + ctx.abortSignal.addEventListener("abort", () => resolve(), { once: true }); + }); + + return monitor; }, logoutAccount: async ({ accountId, cfg }) => { const envToken = process.env.LINE_CHANNEL_ACCESS_TOKEN?.trim() ?? ""; From 9b81a530161cfb54d75fbf15e21a85808b87a48e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:16:10 +0100 Subject: [PATCH 106/160] fix: add changelog note for LINE lifecycle fix (#26528) (thanks @Sid-Qin) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c472e545356c..3fe1e695a7e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai - Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. +- LINE/Lifecycle: keep LINE `startAccount` pending until abort so webhook startup is no longer misread as immediate channel exit, preventing restart-loop storms on LINE provider boot. (#26528) Thanks @Sid-Qin. - Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. From 7af6849c2f85e212dfcdeeabc7b1b4b82939db0e Mon Sep 17 00:00:00 2001 From: Theo Tarr Date: Sun, 22 Feb 2026 13:43:06 -0500 Subject: [PATCH 107/160] Discord: handle early gateway startup errors --- CHANGELOG.md | 1 + .../monitor/provider.lifecycle.test.ts | 105 ++++++++++++++++-- src/discord/monitor/provider.lifecycle.ts | 62 +++++++---- src/discord/monitor/provider.test.ts | 39 ++++++- src/discord/monitor/provider.ts | 34 ++++++ 5 files changed, 205 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fe1e695a7e7..5f4e3fb749bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Docs: https://docs.openclaw.ai - Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - LINE/Lifecycle: keep LINE `startAccount` pending until abort so webhook startup is no longer misread as immediate channel exit, preventing restart-loop storms on LINE provider boot. (#26528) Thanks @Sid-Qin. +- Discord/Gateway: capture and drain startup-time gateway `error` events before lifecycle listeners attach so early `Fatal Gateway error: 4014` closes surface as actionable intent guidance instead of uncaught gateway crashes. (#23832) Thanks @theotarr. - Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. diff --git a/src/discord/monitor/provider.lifecycle.test.ts b/src/discord/monitor/provider.lifecycle.test.ts index 9b74a0badfb7..e503d88ccde3 100644 --- a/src/discord/monitor/provider.lifecycle.test.ts +++ b/src/discord/monitor/provider.lifecycle.test.ts @@ -49,23 +49,33 @@ describe("runDiscordGatewayLifecycle", () => { accountId?: string; start?: () => Promise; stop?: () => Promise; + isDisallowedIntentsError?: (err: unknown) => boolean; + pendingGatewayErrors?: unknown[]; }) => { const start = vi.fn(params?.start ?? (async () => undefined)); const stop = vi.fn(params?.stop ?? (async () => undefined)); const threadStop = vi.fn(); + const runtimeError = vi.fn(); + const releaseEarlyGatewayErrorGuard = vi.fn(); return { start, stop, threadStop, + runtimeError, + releaseEarlyGatewayErrorGuard, lifecycleParams: { accountId: params?.accountId ?? "default", client: { getPlugin: vi.fn(() => undefined) } as unknown as Client, - runtime: {} as RuntimeEnv, - isDisallowedIntentsError: () => false, + runtime: { + error: runtimeError, + } as RuntimeEnv, + isDisallowedIntentsError: params?.isDisallowedIntentsError ?? (() => false), voiceManager: null, voiceManagerRef: { current: null }, execApprovalsHandler: { start, stop }, threadBindings: { stop: threadStop }, + pendingGatewayErrors: params?.pendingGatewayErrors, + releaseEarlyGatewayErrorGuard, }, }; }; @@ -75,6 +85,7 @@ describe("runDiscordGatewayLifecycle", () => { stop: ReturnType; threadStop: ReturnType; waitCalls: number; + releaseEarlyGatewayErrorGuard: ReturnType; }) { expect(params.start).toHaveBeenCalledTimes(1); expect(params.stop).toHaveBeenCalledTimes(1); @@ -82,39 +93,109 @@ describe("runDiscordGatewayLifecycle", () => { expect(unregisterGatewayMock).toHaveBeenCalledWith("default"); expect(stopGatewayLoggingMock).toHaveBeenCalledTimes(1); expect(params.threadStop).toHaveBeenCalledTimes(1); + expect(params.releaseEarlyGatewayErrorGuard).toHaveBeenCalledTimes(1); } it("cleans up thread bindings when exec approvals startup fails", async () => { const { runDiscordGatewayLifecycle } = await import("./provider.lifecycle.js"); - const { lifecycleParams, start, stop, threadStop } = createLifecycleHarness({ - start: async () => { - throw new Error("startup failed"); - }, - }); + const { lifecycleParams, start, stop, threadStop, releaseEarlyGatewayErrorGuard } = + createLifecycleHarness({ + start: async () => { + throw new Error("startup failed"); + }, + }); await expect(runDiscordGatewayLifecycle(lifecycleParams)).rejects.toThrow("startup failed"); - expectLifecycleCleanup({ start, stop, threadStop, waitCalls: 0 }); + expectLifecycleCleanup({ + start, + stop, + threadStop, + waitCalls: 0, + releaseEarlyGatewayErrorGuard, + }); }); it("cleans up when gateway wait fails after startup", async () => { const { runDiscordGatewayLifecycle } = await import("./provider.lifecycle.js"); waitForDiscordGatewayStopMock.mockRejectedValueOnce(new Error("gateway wait failed")); - const { lifecycleParams, start, stop, threadStop } = createLifecycleHarness(); + const { lifecycleParams, start, stop, threadStop, releaseEarlyGatewayErrorGuard } = + createLifecycleHarness(); await expect(runDiscordGatewayLifecycle(lifecycleParams)).rejects.toThrow( "gateway wait failed", ); - expectLifecycleCleanup({ start, stop, threadStop, waitCalls: 1 }); + expectLifecycleCleanup({ + start, + stop, + threadStop, + waitCalls: 1, + releaseEarlyGatewayErrorGuard, + }); }); it("cleans up after successful gateway wait", async () => { const { runDiscordGatewayLifecycle } = await import("./provider.lifecycle.js"); - const { lifecycleParams, start, stop, threadStop } = createLifecycleHarness(); + const { lifecycleParams, start, stop, threadStop, releaseEarlyGatewayErrorGuard } = + createLifecycleHarness(); + + await expect(runDiscordGatewayLifecycle(lifecycleParams)).resolves.toBeUndefined(); + + expectLifecycleCleanup({ + start, + stop, + threadStop, + waitCalls: 1, + releaseEarlyGatewayErrorGuard, + }); + }); + + it("handles queued disallowed intents errors without waiting for gateway events", async () => { + const { runDiscordGatewayLifecycle } = await import("./provider.lifecycle.js"); + const { + lifecycleParams, + start, + stop, + threadStop, + runtimeError, + releaseEarlyGatewayErrorGuard, + } = createLifecycleHarness({ + pendingGatewayErrors: [new Error("Fatal Gateway error: 4014")], + isDisallowedIntentsError: (err) => String(err).includes("4014"), + }); await expect(runDiscordGatewayLifecycle(lifecycleParams)).resolves.toBeUndefined(); - expectLifecycleCleanup({ start, stop, threadStop, waitCalls: 1 }); + expect(runtimeError).toHaveBeenCalledWith( + expect.stringContaining("discord: gateway closed with code 4014"), + ); + expectLifecycleCleanup({ + start, + stop, + threadStop, + waitCalls: 0, + releaseEarlyGatewayErrorGuard, + }); + }); + + it("throws queued non-disallowed fatal gateway errors", async () => { + const { runDiscordGatewayLifecycle } = await import("./provider.lifecycle.js"); + const { lifecycleParams, start, stop, threadStop, releaseEarlyGatewayErrorGuard } = + createLifecycleHarness({ + pendingGatewayErrors: [new Error("Fatal Gateway error: 4000")], + }); + + await expect(runDiscordGatewayLifecycle(lifecycleParams)).rejects.toThrow( + "Fatal Gateway error: 4000", + ); + + expectLifecycleCleanup({ + start, + stop, + threadStop, + waitCalls: 0, + releaseEarlyGatewayErrorGuard, + }); }); }); diff --git a/src/discord/monitor/provider.lifecycle.ts b/src/discord/monitor/provider.lifecycle.ts index 8e5177bb9459..489657d08bde 100644 --- a/src/discord/monitor/provider.lifecycle.ts +++ b/src/discord/monitor/provider.lifecycle.ts @@ -22,6 +22,8 @@ export async function runDiscordGatewayLifecycle(params: { voiceManagerRef: { current: DiscordVoiceManager | null }; execApprovalsHandler: ExecApprovalsHandler | null; threadBindings: { stop: () => void }; + pendingGatewayErrors?: unknown[]; + releaseEarlyGatewayErrorGuard?: () => void; }) { const gateway = params.client.getPlugin("gateway"); if (gateway) { @@ -74,11 +76,48 @@ export async function runDiscordGatewayLifecycle(params: { gatewayEmitter?.on("debug", onGatewayDebug); let sawDisallowedIntents = false; + const logGatewayError = (err: unknown) => { + if (params.isDisallowedIntentsError(err)) { + sawDisallowedIntents = true; + params.runtime.error?.( + danger( + "discord: gateway closed with code 4014 (missing privileged gateway intents). Enable the required intents in the Discord Developer Portal or disable them in config.", + ), + ); + return; + } + params.runtime.error?.(danger(`discord gateway error: ${String(err)}`)); + }; + const shouldStopOnGatewayError = (err: unknown) => { + const message = String(err); + return ( + message.includes("Max reconnect attempts") || + message.includes("Fatal Gateway error") || + params.isDisallowedIntentsError(err) + ); + }; try { if (params.execApprovalsHandler) { await params.execApprovalsHandler.start(); } + // Drain gateway errors emitted before lifecycle listeners were attached. + const pendingGatewayErrors = params.pendingGatewayErrors ?? []; + if (pendingGatewayErrors.length > 0) { + const queuedErrors = [...pendingGatewayErrors]; + pendingGatewayErrors.length = 0; + for (const err of queuedErrors) { + logGatewayError(err); + if (!shouldStopOnGatewayError(err)) { + continue; + } + if (params.isDisallowedIntentsError(err)) { + return; + } + throw err; + } + } + await waitForDiscordGatewayStop({ gateway: gateway ? { @@ -87,32 +126,15 @@ export async function runDiscordGatewayLifecycle(params: { } : undefined, abortSignal: params.abortSignal, - onGatewayError: (err) => { - if (params.isDisallowedIntentsError(err)) { - sawDisallowedIntents = true; - params.runtime.error?.( - danger( - "discord: gateway closed with code 4014 (missing privileged gateway intents). Enable the required intents in the Discord Developer Portal or disable them in config.", - ), - ); - return; - } - params.runtime.error?.(danger(`discord gateway error: ${String(err)}`)); - }, - shouldStopOnError: (err) => { - const message = String(err); - return ( - message.includes("Max reconnect attempts") || - message.includes("Fatal Gateway error") || - params.isDisallowedIntentsError(err) - ); - }, + onGatewayError: logGatewayError, + shouldStopOnError: shouldStopOnGatewayError, }); } catch (err) { if (!sawDisallowedIntents && !params.isDisallowedIntentsError(err)) { throw err; } } finally { + params.releaseEarlyGatewayErrorGuard?.(); unregisterGateway(params.accountId); stopGatewayLogging(); if (helloTimeoutId) { diff --git a/src/discord/monitor/provider.test.ts b/src/discord/monitor/provider.test.ts index 14b137fd1bd8..db998ac67200 100644 --- a/src/discord/monitor/provider.test.ts +++ b/src/discord/monitor/provider.test.ts @@ -1,8 +1,11 @@ +import { EventEmitter } from "node:events"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../../config/config.js"; import type { RuntimeEnv } from "../../runtime.js"; const { + clientFetchUserMock, + clientGetPluginMock, createDiscordNativeCommandMock, createNoopThreadBindingManagerMock, createThreadBindingManagerMock, @@ -17,6 +20,8 @@ const { } = vi.hoisted(() => { const createdBindingManagers: Array<{ stop: ReturnType }> = []; return { + clientFetchUserMock: vi.fn(async () => ({ id: "bot-1" })), + clientGetPluginMock: vi.fn(() => undefined), createDiscordNativeCommandMock: vi.fn(() => ({ name: "mock-command" })), createNoopThreadBindingManagerMock: vi.fn(() => { const manager = { stop: vi.fn() }; @@ -65,11 +70,11 @@ vi.mock("@buape/carbon", () => { async handleDeployRequest() { return undefined; } - async fetchUser(_target: string) { - return { id: "bot-1" }; + async fetchUser(target: string) { + return await clientFetchUserMock(target); } - getPlugin(_name: string) { - return undefined; + getPlugin(name: string) { + return clientGetPluginMock(name); } } return { Client, ReadyListener }; @@ -242,6 +247,8 @@ describe("monitorDiscordProvider", () => { }) as OpenClawConfig; beforeEach(() => { + clientFetchUserMock.mockClear().mockResolvedValue({ id: "bot-1" }); + clientGetPluginMock.mockClear().mockReturnValue(undefined); createDiscordNativeCommandMock.mockClear().mockReturnValue({ name: "mock-command" }); createNoopThreadBindingManagerMock.mockClear(); createThreadBindingManagerMock.mockClear(); @@ -290,4 +297,28 @@ describe("monitorDiscordProvider", () => { expect(createdBindingManagers).toHaveLength(1); expect(createdBindingManagers[0]?.stop).toHaveBeenCalledTimes(1); }); + + it("captures gateway errors emitted before lifecycle wait starts", async () => { + const { monitorDiscordProvider } = await import("./provider.js"); + const emitter = new EventEmitter(); + clientGetPluginMock.mockImplementation((name: string) => + name === "gateway" ? { emitter, disconnect: vi.fn() } : undefined, + ); + clientFetchUserMock.mockImplementationOnce(async () => { + emitter.emit("error", new Error("Fatal Gateway error: 4014")); + return { id: "bot-1" }; + }); + + await monitorDiscordProvider({ + config: baseConfig(), + runtime: baseRuntime(), + }); + + expect(monitorLifecycleMock).toHaveBeenCalledTimes(1); + const lifecycleArgs = monitorLifecycleMock.mock.calls[0]?.[0] as { + pendingGatewayErrors?: unknown[]; + }; + expect(lifecycleArgs.pendingGatewayErrors).toHaveLength(1); + expect(String(lifecycleArgs.pendingGatewayErrors?.[0])).toContain("4014"); + }); }); diff --git a/src/discord/monitor/provider.ts b/src/discord/monitor/provider.ts index 629f8a3e7aab..2239503a5dbf 100644 --- a/src/discord/monitor/provider.ts +++ b/src/discord/monitor/provider.ts @@ -34,6 +34,7 @@ import { createDiscordRetryRunner } from "../../infra/retry-policy.js"; import { createSubsystemLogger } from "../../logging/subsystem.js"; import { createNonExitingRuntime, type RuntimeEnv } from "../../runtime.js"; import { resolveDiscordAccount } from "../accounts.js"; +import { getDiscordGatewayEmitter } from "../monitor.gateway.js"; import { fetchDiscordApplicationId } from "../probe.js"; import { normalizeDiscordToken } from "../token.js"; import { createDiscordVoiceCommand } from "../voice/command.js"; @@ -229,6 +230,33 @@ function isDiscordDisallowedIntentsError(err: unknown): boolean { return message.includes(String(DISCORD_DISALLOWED_INTENTS_CODE)); } +type EarlyGatewayErrorGuard = { + pendingErrors: unknown[]; + release: () => void; +}; + +function attachEarlyGatewayErrorGuard(client: Client): EarlyGatewayErrorGuard { + const pendingErrors: unknown[] = []; + const gateway = client.getPlugin("gateway"); + const emitter = getDiscordGatewayEmitter(gateway); + if (!emitter) { + return { + pendingErrors, + release: () => {}, + }; + } + const onGatewayError = (err: unknown) => { + pendingErrors.push(err); + }; + emitter.on("error", onGatewayError); + return { + pendingErrors, + release: () => { + emitter.removeListener("error", onGatewayError); + }, + }; +} + export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { const cfg = opts.config ?? loadConfig(); const account = resolveDiscordAccount({ @@ -365,6 +393,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { }) : createNoopThreadBindingManager(account.accountId); let lifecycleStarted = false; + let releaseEarlyGatewayErrorGuard = () => {}; try { const commands: BaseCommand[] = commandSpecs.map((spec) => createDiscordNativeCommand({ @@ -496,6 +525,8 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { }, clientPlugins, ); + const earlyGatewayErrorGuard = attachEarlyGatewayErrorGuard(client); + releaseEarlyGatewayErrorGuard = earlyGatewayErrorGuard.release; await deployDiscordCommands({ client, runtime, enabled: nativeEnabled }); @@ -612,8 +643,11 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { voiceManagerRef, execApprovalsHandler, threadBindings, + pendingGatewayErrors: earlyGatewayErrorGuard.pendingErrors, + releaseEarlyGatewayErrorGuard, }); } finally { + releaseEarlyGatewayErrorGuard(); if (!lifecycleStarted) { threadBindings.stop(); } From e35fe7888b98e31715b95c3b425510189618a07f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:22:44 +0100 Subject: [PATCH 108/160] refactor: centralize message-provider tool filtering --- ...e-aliases-schemas-without-dropping.test.ts | 13 -------- .../pi-tools.message-provider-policy.test.ts | 19 ++++++++++++ src/agents/pi-tools.ts | 31 ++++++++++++++++--- 3 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 src/agents/pi-tools.message-provider-policy.test.ts diff --git a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts index e074b6f91893..22d68f15ff82 100644 --- a/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts +++ b/src/agents/pi-tools.create-openclaw-coding-tools.adds-claude-style-aliases-schemas-without-dropping.test.ts @@ -319,19 +319,6 @@ describe("createOpenClawCodingTools", () => { expect(names.has("telegram")).toBe(false); expect(names.has("whatsapp")).toBe(false); }); - it.each(["voice", "VOICE", " Voice "])( - "does not expose tts tool for normalized voice message provider: %s", - (messageProvider) => { - const tools = createOpenClawCodingTools({ messageProvider }); - const names = new Set(tools.map((tool) => tool.name)); - expect(names.has("tts")).toBe(false); - }, - ); - it("keeps tts tool for non-voice providers", () => { - const tools = createOpenClawCodingTools({ messageProvider: "discord" }); - const names = new Set(tools.map((tool) => tool.name)); - expect(names.has("tts")).toBe(true); - }); it("filters session tools for sub-agent sessions by default", () => { const tools = createOpenClawCodingTools({ sessionKey: "agent:main:subagent:test", diff --git a/src/agents/pi-tools.message-provider-policy.test.ts b/src/agents/pi-tools.message-provider-policy.test.ts new file mode 100644 index 000000000000..0bcdd5144f0f --- /dev/null +++ b/src/agents/pi-tools.message-provider-policy.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest"; +import { createOpenClawCodingTools } from "./pi-tools.js"; + +describe("createOpenClawCodingTools message provider policy", () => { + it.each(["voice", "VOICE", " Voice "])( + "does not expose tts tool for normalized voice provider: %s", + (messageProvider) => { + const tools = createOpenClawCodingTools({ messageProvider }); + const names = new Set(tools.map((tool) => tool.name)); + expect(names.has("tts")).toBe(false); + }, + ); + + it("keeps tts tool for non-voice providers", () => { + const tools = createOpenClawCodingTools({ messageProvider: "discord" }); + const names = new Set(tools.map((tool) => tool.name)); + expect(names.has("tts")).toBe(true); + }); +}); diff --git a/src/agents/pi-tools.ts b/src/agents/pi-tools.ts index f4252f562bb9..15be5766c896 100644 --- a/src/agents/pi-tools.ts +++ b/src/agents/pi-tools.ts @@ -67,6 +67,31 @@ function isOpenAIProvider(provider?: string) { return normalized === "openai" || normalized === "openai-codex"; } +const TOOL_DENY_BY_MESSAGE_PROVIDER: Readonly> = { + voice: ["tts"], +}; + +function normalizeMessageProvider(messageProvider?: string): string | undefined { + const normalized = messageProvider?.trim().toLowerCase(); + return normalized && normalized.length > 0 ? normalized : undefined; +} + +function applyMessageProviderToolPolicy( + tools: AnyAgentTool[], + messageProvider?: string, +): AnyAgentTool[] { + const normalizedProvider = normalizeMessageProvider(messageProvider); + if (!normalizedProvider) { + return tools; + } + const deniedTools = TOOL_DENY_BY_MESSAGE_PROVIDER[normalizedProvider]; + if (!deniedTools || deniedTools.length === 0) { + return tools; + } + const deniedSet = new Set(deniedTools); + return tools.filter((tool) => !deniedSet.has(tool.name)); +} + function isApplyPatchAllowedForModel(params: { modelProvider?: string; modelId?: string; @@ -217,8 +242,6 @@ export function createOpenClawCodingTools(options?: { /** Whether the sender is an owner (required for owner-only tools). */ senderIsOwner?: boolean; }): AnyAgentTool[] { - const rawMessageProvider = options?.messageProvider?.trim().toLowerCase(); - const isVoiceMessageProvider = rawMessageProvider === "voice"; const execToolName = "exec"; const sandbox = options?.sandbox?.enabled ? options.sandbox : undefined; const { @@ -482,9 +505,7 @@ export function createOpenClawCodingTools(options?: { senderIsOwner: options?.senderIsOwner, }), ]; - const toolsForMessageProvider = isVoiceMessageProvider - ? tools.filter((tool) => tool.name !== "tts") - : tools; + const toolsForMessageProvider = applyMessageProviderToolPolicy(tools, options?.messageProvider); // Security: treat unknown/undefined as unauthorized (opt-in, not opt-out) const senderIsOwner = options?.senderIsOwner === true; const toolsByAuthorization = applyOwnerOnlyToolPolicy(toolsForMessageProvider, senderIsOwner); From 02c731826a036b7be49fe516436173554a1488f0 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:35:49 +0100 Subject: [PATCH 109/160] test(discord): fix monitor test typings --- src/discord/monitor/provider.lifecycle.test.ts | 11 ++++++++--- src/discord/monitor/provider.test.ts | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/discord/monitor/provider.lifecycle.test.ts b/src/discord/monitor/provider.lifecycle.test.ts index e503d88ccde3..f29bd8e8cc17 100644 --- a/src/discord/monitor/provider.lifecycle.test.ts +++ b/src/discord/monitor/provider.lifecycle.test.ts @@ -55,8 +55,15 @@ describe("runDiscordGatewayLifecycle", () => { const start = vi.fn(params?.start ?? (async () => undefined)); const stop = vi.fn(params?.stop ?? (async () => undefined)); const threadStop = vi.fn(); + const runtimeLog = vi.fn(); const runtimeError = vi.fn(); + const runtimeExit = vi.fn(); const releaseEarlyGatewayErrorGuard = vi.fn(); + const runtime: RuntimeEnv = { + log: runtimeLog, + error: runtimeError, + exit: runtimeExit, + }; return { start, stop, @@ -66,9 +73,7 @@ describe("runDiscordGatewayLifecycle", () => { lifecycleParams: { accountId: params?.accountId ?? "default", client: { getPlugin: vi.fn(() => undefined) } as unknown as Client, - runtime: { - error: runtimeError, - } as RuntimeEnv, + runtime, isDisallowedIntentsError: params?.isDisallowedIntentsError ?? (() => false), voiceManager: null, voiceManagerRef: { current: null }, diff --git a/src/discord/monitor/provider.test.ts b/src/discord/monitor/provider.test.ts index db998ac67200..75552749fda2 100644 --- a/src/discord/monitor/provider.test.ts +++ b/src/discord/monitor/provider.test.ts @@ -20,8 +20,8 @@ const { } = vi.hoisted(() => { const createdBindingManagers: Array<{ stop: ReturnType }> = []; return { - clientFetchUserMock: vi.fn(async () => ({ id: "bot-1" })), - clientGetPluginMock: vi.fn(() => undefined), + clientFetchUserMock: vi.fn(async (_target: string) => ({ id: "bot-1" })), + clientGetPluginMock: vi.fn<(_name: string) => unknown>(() => undefined), createDiscordNativeCommandMock: vi.fn(() => ({ name: "mock-command" })), createNoopThreadBindingManagerMock: vi.fn(() => { const manager = { stop: vi.fn() }; From e915b4c64a774365fafbb8558b94373c8c0cad2d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:36:00 +0100 Subject: [PATCH 110/160] refactor: unify monitor abort lifecycle handling --- changelog/fragments/README.md | 13 +++ extensions/line/src/channel.startup.test.ts | 46 ---------- extensions/line/src/channel.ts | 12 --- scripts/pr | 80 +++++++++++++++- .../monitor/gateway-error-guard.test.ts | 33 +++++++ src/discord/monitor/gateway-error-guard.ts | 36 ++++++++ src/discord/monitor/provider.ts | 29 +----- src/infra/abort-signal.test.ts | 29 ++++++ src/infra/abort-signal.ts | 12 +++ src/line/monitor.lifecycle.test.ts | 92 +++++++++++++++++++ src/line/monitor.ts | 13 ++- src/telegram/monitor.test.ts | 15 ++- src/telegram/monitor.ts | 12 +-- 13 files changed, 319 insertions(+), 103 deletions(-) create mode 100644 changelog/fragments/README.md create mode 100644 src/discord/monitor/gateway-error-guard.test.ts create mode 100644 src/discord/monitor/gateway-error-guard.ts create mode 100644 src/infra/abort-signal.test.ts create mode 100644 src/infra/abort-signal.ts create mode 100644 src/line/monitor.lifecycle.test.ts diff --git a/changelog/fragments/README.md b/changelog/fragments/README.md new file mode 100644 index 000000000000..93bb5b65d706 --- /dev/null +++ b/changelog/fragments/README.md @@ -0,0 +1,13 @@ +# Changelog Fragments + +Use this directory when a PR should not edit `CHANGELOG.md` directly. + +- One fragment file per PR. +- File name recommendation: `pr-.md`. +- Include at least one line with both `#` and `thanks @`. + +Example: + +```md +- Fix LINE monitor lifecycle wait ownership (#27001) (thanks @alice) +``` diff --git a/extensions/line/src/channel.startup.test.ts b/extensions/line/src/channel.startup.test.ts index 11ba80bda126..812636113cb9 100644 --- a/extensions/line/src/channel.startup.test.ts +++ b/extensions/line/src/channel.startup.test.ts @@ -129,50 +129,4 @@ describe("linePlugin gateway.startAccount", () => { abort.abort(); await task; }); - - it("stays pending until abort signal fires (no premature exit)", async () => { - const { runtime, monitorLineProvider } = createRuntime(); - setLineRuntime(runtime); - - const abort = new AbortController(); - let resolved = false; - - const task = linePlugin.gateway!.startAccount!( - createStartAccountCtx({ - token: "token", - secret: "secret", - runtime: createRuntimeEnv(), - abortSignal: abort.signal, - }), - ).then(() => { - resolved = true; - }); - - // Allow async internals to flush - await new Promise((r) => setTimeout(r, 50)); - - expect(monitorLineProvider).toHaveBeenCalled(); - expect(resolved).toBe(false); - - abort.abort(); - await task; - expect(resolved).toBe(true); - }); - - it("resolves immediately when abortSignal is already aborted", async () => { - const { runtime } = createRuntime(); - setLineRuntime(runtime); - - const abort = new AbortController(); - abort.abort(); - - await linePlugin.gateway!.startAccount!( - createStartAccountCtx({ - token: "token", - secret: "secret", - runtime: createRuntimeEnv(), - abortSignal: abort.signal, - }), - ); - }); }); diff --git a/extensions/line/src/channel.ts b/extensions/line/src/channel.ts index f37a86aa0c4e..1c87ad8e2f3f 100644 --- a/extensions/line/src/channel.ts +++ b/extensions/line/src/channel.ts @@ -661,18 +661,6 @@ export const linePlugin: ChannelPlugin = { webhookPath: account.config.webhookPath, }); - // Keep the provider alive until the abort signal fires. Without this, - // the startAccount promise resolves immediately after webhook registration - // and the channel supervisor treats the provider as "exited", triggering an - // auto-restart loop (up to 10 attempts). - await new Promise((resolve) => { - if (ctx.abortSignal.aborted) { - resolve(); - return; - } - ctx.abortSignal.addEventListener("abort", () => resolve(), { once: true }); - }); - return monitor; }, logoutAccount: async ({ accountId, cfg }) => { diff --git a/scripts/pr b/scripts/pr index 90cfe029db08..36ab74972c4f 100755 --- a/scripts/pr +++ b/scripts/pr @@ -664,6 +664,61 @@ validate_changelog_entry_for_pr() { echo "changelog validated: found PR #$pr (contributor handle unavailable, skipping thanks check)" } +changed_changelog_fragment_files() { + git diff --name-only origin/main...HEAD -- changelog/fragments | rg '^changelog/fragments/.*\.md$' || true +} + +validate_changelog_fragments_for_pr() { + local pr="$1" + local contrib="$2" + shift 2 + + if [ "$#" -lt 1 ]; then + echo "No changelog fragments provided for validation." + exit 1 + fi + + local pr_pattern + pr_pattern="(#$pr|openclaw#$pr)" + + local added_lines + local file + local all_added_lines="" + for file in "$@"; do + added_lines=$(git diff --unified=0 origin/main...HEAD -- "$file" | awk ' + /^\+\+\+/ { next } + /^\+/ { print substr($0, 2) } + ') + + if [ -z "$added_lines" ]; then + echo "$file is in diff but no added lines were detected." + exit 1 + fi + + all_added_lines=$(printf '%s\n%s\n' "$all_added_lines" "$added_lines") + done + + local with_pr + with_pr=$(printf '%s\n' "$all_added_lines" | rg -in "$pr_pattern" || true) + if [ -z "$with_pr" ]; then + echo "Changelog fragment update must reference PR #$pr (for example, (#$pr))." + exit 1 + fi + + if [ -n "$contrib" ] && [ "$contrib" != "null" ]; then + local with_pr_and_thanks + with_pr_and_thanks=$(printf '%s\n' "$all_added_lines" | rg -in "$pr_pattern" | rg -i "thanks @$contrib" || true) + if [ -z "$with_pr_and_thanks" ]; then + echo "Changelog fragment update must include both PR #$pr and thanks @$contrib on the entry line." + exit 1 + fi + echo "changelog fragments validated: found PR #$pr + thanks @$contrib" + return 0 + fi + + echo "changelog fragments validated: found PR #$pr (contributor handle unavailable, skipping thanks check)" +} + prepare_gates() { local pr="$1" enter_worktree "$pr" false @@ -684,13 +739,30 @@ prepare_gates() { docs_only=true fi - # Enforce workflow policy: every prepared PR must include a changelog update. - if ! printf '%s\n' "$changed_files" | rg -q '^CHANGELOG\.md$'; then - echo "Missing CHANGELOG.md update in PR diff. This workflow requires a changelog entry." + local has_changelog_update=false + if printf '%s\n' "$changed_files" | rg -q '^CHANGELOG\.md$'; then + has_changelog_update=true + fi + local fragment_files + fragment_files=$(changed_changelog_fragment_files) + local has_fragment_update=false + if [ -n "$fragment_files" ]; then + has_fragment_update=true + fi + # Enforce workflow policy: every prepared PR must include either CHANGELOG.md + # or one or more changelog fragments. + if [ "$has_changelog_update" = "false" ] && [ "$has_fragment_update" = "false" ]; then + echo "Missing changelog update. Add CHANGELOG.md changes or changelog/fragments/*.md entry." exit 1 fi local contrib="${PR_AUTHOR:-}" - validate_changelog_entry_for_pr "$pr" "$contrib" + if [ "$has_changelog_update" = "true" ]; then + validate_changelog_entry_for_pr "$pr" "$contrib" + fi + if [ "$has_fragment_update" = "true" ]; then + mapfile -t fragment_file_list <<<"$fragment_files" + validate_changelog_fragments_for_pr "$pr" "$contrib" "${fragment_file_list[@]}" + fi run_quiet_logged "pnpm build" ".local/gates-build.log" pnpm build run_quiet_logged "pnpm check" ".local/gates-check.log" pnpm check diff --git a/src/discord/monitor/gateway-error-guard.test.ts b/src/discord/monitor/gateway-error-guard.test.ts new file mode 100644 index 000000000000..783fcc6a7124 --- /dev/null +++ b/src/discord/monitor/gateway-error-guard.test.ts @@ -0,0 +1,33 @@ +import { EventEmitter } from "node:events"; +import { describe, expect, it, vi } from "vitest"; +import { attachEarlyGatewayErrorGuard } from "./gateway-error-guard.js"; + +describe("attachEarlyGatewayErrorGuard", () => { + it("captures gateway errors until released", () => { + const emitter = new EventEmitter(); + const fallbackErrorListener = vi.fn(); + emitter.on("error", fallbackErrorListener); + const client = { + getPlugin: vi.fn(() => ({ emitter })), + }; + + const guard = attachEarlyGatewayErrorGuard(client as never); + emitter.emit("error", new Error("Fatal Gateway error: 4014")); + expect(guard.pendingErrors).toHaveLength(1); + + guard.release(); + emitter.emit("error", new Error("Fatal Gateway error: 4000")); + expect(guard.pendingErrors).toHaveLength(1); + expect(fallbackErrorListener).toHaveBeenCalledTimes(2); + }); + + it("returns noop guard when gateway emitter is unavailable", () => { + const client = { + getPlugin: vi.fn(() => undefined), + }; + + const guard = attachEarlyGatewayErrorGuard(client as never); + expect(guard.pendingErrors).toEqual([]); + expect(() => guard.release()).not.toThrow(); + }); +}); diff --git a/src/discord/monitor/gateway-error-guard.ts b/src/discord/monitor/gateway-error-guard.ts new file mode 100644 index 000000000000..5cb79753325f --- /dev/null +++ b/src/discord/monitor/gateway-error-guard.ts @@ -0,0 +1,36 @@ +import type { Client } from "@buape/carbon"; +import { getDiscordGatewayEmitter } from "../monitor.gateway.js"; + +export type EarlyGatewayErrorGuard = { + pendingErrors: unknown[]; + release: () => void; +}; + +export function attachEarlyGatewayErrorGuard(client: Client): EarlyGatewayErrorGuard { + const pendingErrors: unknown[] = []; + const gateway = client.getPlugin("gateway"); + const emitter = getDiscordGatewayEmitter(gateway); + if (!emitter) { + return { + pendingErrors, + release: () => {}, + }; + } + + let released = false; + const onGatewayError = (err: unknown) => { + pendingErrors.push(err); + }; + emitter.on("error", onGatewayError); + + return { + pendingErrors, + release: () => { + if (released) { + return; + } + released = true; + emitter.removeListener("error", onGatewayError); + }, + }; +} diff --git a/src/discord/monitor/provider.ts b/src/discord/monitor/provider.ts index 2239503a5dbf..8243da5a2465 100644 --- a/src/discord/monitor/provider.ts +++ b/src/discord/monitor/provider.ts @@ -34,7 +34,6 @@ import { createDiscordRetryRunner } from "../../infra/retry-policy.js"; import { createSubsystemLogger } from "../../logging/subsystem.js"; import { createNonExitingRuntime, type RuntimeEnv } from "../../runtime.js"; import { resolveDiscordAccount } from "../accounts.js"; -import { getDiscordGatewayEmitter } from "../monitor.gateway.js"; import { fetchDiscordApplicationId } from "../probe.js"; import { normalizeDiscordToken } from "../token.js"; import { createDiscordVoiceCommand } from "../voice/command.js"; @@ -52,6 +51,7 @@ import { } from "./agent-components.js"; import { resolveDiscordSlashCommandConfig } from "./commands.js"; import { createExecApprovalButton, DiscordExecApprovalHandler } from "./exec-approvals.js"; +import { attachEarlyGatewayErrorGuard } from "./gateway-error-guard.js"; import { createDiscordGatewayPlugin } from "./gateway-plugin.js"; import { DiscordMessageListener, @@ -230,33 +230,6 @@ function isDiscordDisallowedIntentsError(err: unknown): boolean { return message.includes(String(DISCORD_DISALLOWED_INTENTS_CODE)); } -type EarlyGatewayErrorGuard = { - pendingErrors: unknown[]; - release: () => void; -}; - -function attachEarlyGatewayErrorGuard(client: Client): EarlyGatewayErrorGuard { - const pendingErrors: unknown[] = []; - const gateway = client.getPlugin("gateway"); - const emitter = getDiscordGatewayEmitter(gateway); - if (!emitter) { - return { - pendingErrors, - release: () => {}, - }; - } - const onGatewayError = (err: unknown) => { - pendingErrors.push(err); - }; - emitter.on("error", onGatewayError); - return { - pendingErrors, - release: () => { - emitter.removeListener("error", onGatewayError); - }, - }; -} - export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) { const cfg = opts.config ?? loadConfig(); const account = resolveDiscordAccount({ diff --git a/src/infra/abort-signal.test.ts b/src/infra/abort-signal.test.ts new file mode 100644 index 000000000000..be32e0d881a9 --- /dev/null +++ b/src/infra/abort-signal.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { waitForAbortSignal } from "./abort-signal.js"; + +describe("waitForAbortSignal", () => { + it("resolves immediately when signal is missing", async () => { + await expect(waitForAbortSignal(undefined)).resolves.toBeUndefined(); + }); + + it("resolves immediately when signal is already aborted", async () => { + const abort = new AbortController(); + abort.abort(); + await expect(waitForAbortSignal(abort.signal)).resolves.toBeUndefined(); + }); + + it("waits until abort fires", async () => { + const abort = new AbortController(); + let resolved = false; + + const task = waitForAbortSignal(abort.signal).then(() => { + resolved = true; + }); + await Promise.resolve(); + expect(resolved).toBe(false); + + abort.abort(); + await task; + expect(resolved).toBe(true); + }); +}); diff --git a/src/infra/abort-signal.ts b/src/infra/abort-signal.ts new file mode 100644 index 000000000000..77922784eda4 --- /dev/null +++ b/src/infra/abort-signal.ts @@ -0,0 +1,12 @@ +export async function waitForAbortSignal(signal?: AbortSignal): Promise { + if (!signal || signal.aborted) { + return; + } + await new Promise((resolve) => { + const onAbort = () => { + signal.removeEventListener("abort", onAbort); + resolve(); + }; + signal.addEventListener("abort", onAbort, { once: true }); + }); +} diff --git a/src/line/monitor.lifecycle.test.ts b/src/line/monitor.lifecycle.test.ts new file mode 100644 index 000000000000..635d921e7ad6 --- /dev/null +++ b/src/line/monitor.lifecycle.test.ts @@ -0,0 +1,92 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { OpenClawConfig } from "../config/config.js"; +import type { RuntimeEnv } from "../runtime.js"; + +const { createLineBotMock, registerPluginHttpRouteMock, unregisterHttpMock } = vi.hoisted(() => ({ + createLineBotMock: vi.fn(() => ({ + account: { accountId: "default" }, + handleWebhook: vi.fn(), + })), + registerPluginHttpRouteMock: vi.fn(), + unregisterHttpMock: vi.fn(), +})); + +vi.mock("./bot.js", () => ({ + createLineBot: createLineBotMock, +})); + +vi.mock("../plugins/http-path.js", () => ({ + normalizePluginHttpPath: (_path: string | undefined, fallback: string) => fallback, +})); + +vi.mock("../plugins/http-registry.js", () => ({ + registerPluginHttpRoute: registerPluginHttpRouteMock, +})); + +vi.mock("./webhook-node.js", () => ({ + createLineNodeWebhookHandler: vi.fn(() => vi.fn()), +})); + +describe("monitorLineProvider lifecycle", () => { + beforeEach(() => { + createLineBotMock.mockClear(); + unregisterHttpMock.mockClear(); + registerPluginHttpRouteMock.mockClear().mockReturnValue(unregisterHttpMock); + }); + + it("waits for abort before resolving", async () => { + const { monitorLineProvider } = await import("./monitor.js"); + const abort = new AbortController(); + let resolved = false; + + const task = monitorLineProvider({ + channelAccessToken: "token", + channelSecret: "secret", + config: {} as OpenClawConfig, + runtime: {} as RuntimeEnv, + abortSignal: abort.signal, + }).then((monitor) => { + resolved = true; + return monitor; + }); + + await vi.waitFor(() => expect(registerPluginHttpRouteMock).toHaveBeenCalledTimes(1)); + expect(resolved).toBe(false); + + abort.abort(); + await task; + expect(unregisterHttpMock).toHaveBeenCalledTimes(1); + }); + + it("stops immediately when signal is already aborted", async () => { + const { monitorLineProvider } = await import("./monitor.js"); + const abort = new AbortController(); + abort.abort(); + + await monitorLineProvider({ + channelAccessToken: "token", + channelSecret: "secret", + config: {} as OpenClawConfig, + runtime: {} as RuntimeEnv, + abortSignal: abort.signal, + }); + + expect(unregisterHttpMock).toHaveBeenCalledTimes(1); + }); + + it("returns immediately without abort signal and stop is idempotent", async () => { + const { monitorLineProvider } = await import("./monitor.js"); + + const monitor = await monitorLineProvider({ + channelAccessToken: "token", + channelSecret: "secret", + config: {} as OpenClawConfig, + runtime: {} as RuntimeEnv, + }); + + expect(unregisterHttpMock).not.toHaveBeenCalled(); + monitor.stop(); + monitor.stop(); + expect(unregisterHttpMock).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/line/monitor.ts b/src/line/monitor.ts index 07a995c4eed5..49fcc518a3f9 100644 --- a/src/line/monitor.ts +++ b/src/line/monitor.ts @@ -4,6 +4,7 @@ import { dispatchReplyWithBufferedBlockDispatcher } from "../auto-reply/reply/pr import { createReplyPrefixOptions } from "../channels/reply-prefix.js"; import type { OpenClawConfig } from "../config/config.js"; import { danger, logVerbose } from "../globals.js"; +import { waitForAbortSignal } from "../infra/abort-signal.js"; import { normalizePluginHttpPath } from "../plugins/http-path.js"; import { registerPluginHttpRoute } from "../plugins/http-registry.js"; import type { RuntimeEnv } from "../runtime.js"; @@ -296,7 +297,12 @@ export async function monitorLineProvider( logVerbose(`line: registered webhook handler at ${normalizedPath}`); // Handle abort signal + let stopped = false; const stopHandler = () => { + if (stopped) { + return; + } + stopped = true; logVerbose(`line: stopping provider for account ${resolvedAccountId}`); unregisterHttp(); recordChannelRuntimeState({ @@ -309,7 +315,12 @@ export async function monitorLineProvider( }); }; - abortSignal?.addEventListener("abort", stopHandler); + if (abortSignal?.aborted) { + stopHandler(); + } else if (abortSignal) { + abortSignal.addEventListener("abort", stopHandler, { once: true }); + await waitForAbortSignal(abortSignal); + } return { account: bot.account, diff --git a/src/telegram/monitor.test.ts b/src/telegram/monitor.test.ts index 4e59f6c0c6a4..5c0df3de6efa 100644 --- a/src/telegram/monitor.test.ts +++ b/src/telegram/monitor.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { monitorTelegramProvider } from "./monitor.js"; type MockCtx = { @@ -160,19 +160,30 @@ vi.mock("../auto-reply/reply.js", () => ({ })); describe("monitorTelegramProvider (grammY)", () => { + let consoleErrorSpy: { mockRestore: () => void } | undefined; + beforeEach(() => { loadConfig.mockReturnValue({ agents: { defaults: { maxConcurrent: 2 } }, channels: { telegram: {} }, }); initSpy.mockClear(); - runSpy.mockClear(); + runSpy.mockReset().mockImplementation(() => + makeRunnerStub({ + task: () => Promise.reject(new Error("runSpy called without explicit test stub")), + }), + ); computeBackoff.mockClear(); sleepWithAbort.mockClear(); startTelegramWebhookSpy.mockClear(); registerUnhandledRejectionHandlerMock.mockClear(); resetUnhandledRejection(); createTelegramBotErrors.length = 0; + consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); + }); + + afterEach(() => { + consoleErrorSpy?.mockRestore(); }); it("processes a DM and sends reply", async () => { diff --git a/src/telegram/monitor.ts b/src/telegram/monitor.ts index 579db8ad3a1f..06410b74ed18 100644 --- a/src/telegram/monitor.ts +++ b/src/telegram/monitor.ts @@ -2,6 +2,7 @@ import { type RunOptions, run } from "@grammyjs/runner"; import { resolveAgentMaxConcurrent } from "../config/agent-limits.js"; import type { OpenClawConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js"; +import { waitForAbortSignal } from "../infra/abort-signal.js"; import { computeBackoff, sleepWithAbort } from "../infra/backoff.js"; import { formatErrorMessage } from "../infra/errors.js"; import { formatDurationPrecise } from "../infra/format-time/format-duration.ts"; @@ -172,16 +173,7 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) { abortSignal: opts.abortSignal, publicUrl: opts.webhookUrl, }); - const abortSignal = opts.abortSignal; - if (abortSignal && !abortSignal.aborted) { - await new Promise((resolve) => { - const onAbort = () => { - abortSignal.removeEventListener("abort", onAbort); - resolve(); - }; - abortSignal.addEventListener("abort", onAbort, { once: true }); - }); - } + await waitForAbortSignal(opts.abortSignal); return; } From fdea7415ccd4a6de5deeea70f7cc97721b469fdb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:38:56 +0100 Subject: [PATCH 111/160] docs: reorder unreleased changelog by user impact --- CHANGELOG.md | 76 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f4e3fb749bb..4717658d4edd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,14 +6,14 @@ Docs: https://docs.openclaw.ai ### Changes -- Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. -- Android/Startup perf: defer foreground-service startup, move WebView debugging init out of critical startup, and add startup macrobenchmark + low-noise perf CLI scripts for deterministic cold-start tracking. (#26659) Thanks @obviyus. - Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus. +- Android/Startup perf: defer foreground-service startup, move WebView debugging init out of critical startup, and add startup macrobenchmark + low-noise perf CLI scripts for deterministic cold-start tracking. (#26659) Thanks @obviyus. - UI/Chat compose: add mobile stacked layout for compose action buttons on small screens to improve send/session controls usability. (#11167) Thanks @junyiz. +- Heartbeat/Config: replace heartbeat DM toggle with `agents.defaults.heartbeat.directPolicy` (`allow` | `block`; also supported per-agent via `agents.list[].heartbeat.directPolicy`) for clearer delivery semantics. +- Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. -- Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. -- Heartbeat/Config: replace heartbeat DM toggle with `agents.defaults.heartbeat.directPolicy` (`allow` | `block`; also supported per-agent via `agents.list[].heartbeat.directPolicy`) for clearer delivery semantics. +- Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. ### Breaking @@ -21,56 +21,56 @@ Docs: https://docs.openclaw.ai ### Fixes -- Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. +- Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) +- Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. +- Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. +- Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. +- Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. +- Cron/Announce duplicate guard: track attempted announce/direct delivery separately from confirmed `delivered`, and suppress fallback main-session cron summaries when delivery was already attempted to avoid duplicate end-user sends in uncertain-ack paths. (#27018) - LINE/Lifecycle: keep LINE `startAccount` pending until abort so webhook startup is no longer misread as immediate channel exit, preventing restart-loop storms on LINE provider boot. (#26528) Thanks @Sid-Qin. - Discord/Gateway: capture and drain startup-time gateway `error` events before lifecycle listeners attach so early `Fatal Gateway error: 4014` closes surface as actionable intent guidance instead of uncaught gateway crashes. (#23832) Thanks @theotarr. -- Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. -- Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. +- Discord/Inbound text: preserve embed `title` + `description` fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky. +- Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. +- Telegram/Preview cleanup: keep finalized text previews when a later assistant message is media-only (for example mixed text plus voice turns) by skipping finalized preview archival at assistant-message boundaries, preventing cleanup from deleting already-visible final text messages. (#27042) +- Telegram/Markdown spoilers: keep valid `||spoiler||` pairs while leaving unmatched trailing `||` delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin. +- Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example `c0abc12345`) correctly match Slack runtime IDs (`C0ABC12345`) under `groupPolicy: "allowlist"`, preventing silent channel-event drops. (#26878) Thanks @lbo728. +- Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. +- Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. +- Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. +- Voice-call/TTS tools: hide the `tts` tool when the message provider is `voice`, preventing voice-call runs from selecting self-playback TTS and falling into silent no-output loops. (#27025) +- Agents/Tools: normalize non-standard plugin tool results that omit `content` so embedded runs no longer crash with `Cannot read properties of undefined (reading 'filter')` after tool completion (including `tesseramemo_query`). (#27007) +- Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. +- Agents/Model fallback: keep explicit text + image fallback chains reachable even when `agents.defaults.models` allowlists are present, prefer explicit run `agentId` over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify `model_cooldown` / `cooling down` errors as `rate_limit` so failover continues. (#11972, #24137, #17231) +- Agents/Model fallback: keep same-provider fallback chains active when session model differs from configured primary, infer cooldown reason from provider profile state (instead of `disabledReason` only), keep no-profile fallback providers eligible (env/models.json paths), and only relax same-provider cooldown fallback attempts for `rate_limit`. (#23816) thanks @ramezgaberiel. +- Agents/Model fallback: continue fallback traversal on unrecognized errors when candidates remain, while still throwing the original unknown error on the last candidate. (#26106) Thanks @Sid-Qin. - Models/Auth probes: map permanent auth failover reasons (`auth_permanent`, for example revoked keys) into probe auth status instead of `unknown`, so `openclaw models status --probe` reports actionable auth failures. (#25754) thanks @rrenamed. -- Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. +- Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. +- Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. +- Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. +- Security/Gateway trusted proxy: require `operator` role for the Control UI trusted-proxy pairing bypass so unpaired `node` sessions can no longer connect via `client.id=control-ui` and invoke node event methods. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. +- Security/Microsoft Teams file consent: bind `fileConsent/invoke` upload acceptance/decline to the originating conversation before consuming pending uploads, preventing cross-conversation pending-file upload or cancellation via leaked `uploadId` values; includes regression coverage for match/mismatch invoke handling. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. +- Security/Workspace FS: reject hardlinked workspace file aliases in `tools.fs.workspaceOnly` and `tools.exec.applyPatch.workspaceOnly` boundary checks (including sandbox mount-root guards) to prevent out-of-workspace read/write via in-workspace hardlink paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before `setFiles`, with regression coverage for strict missing-path handling. - Security/Exec approvals: bind `system.run` approval matching to exact argv identity and preserve argv whitespace in rendered command text, preventing trailing-space executable path swaps from reusing a mismatched approval. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Exec approvals: harden approval-bound `system.run` execution on node hosts by rejecting symlink `cwd` paths and canonicalizing path-like executable argv before spawn, blocking mutable-cwd symlink retarget chains between approval and execution. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under `dmPolicy`/`groupPolicy`; reaction notifications now require channel access checks first. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Discord reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack reactions + pins: gate `reaction_*` and `pin_*` system-event enqueue through shared sender authorization so DM `dmPolicy`/`allowFrom` and channel `users` allowlists are enforced consistently for non-message ingress, with regression coverage for denied/allowed sender paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack interactions: enforce channel/DM authorization and modal actor binding (`private_metadata.userId`) before enqueueing `block_action`/`view_submission`/`view_closed` system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy `oauth.json` onboarding path that exposed the PKCE verifier via OAuth `state`; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (`2026.2.25`). Thanks @zdi-disclosures for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. -- Security/Gateway: harden `agents.files` path handling to block out-of-workspace symlink targets for `agents.files.get`/`agents.files.set`, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting. -- Security/Workspace FS: reject hardlinked workspace file aliases in `tools.fs.workspaceOnly` and `tools.exec.applyPatch.workspaceOnly` boundary checks (including sandbox mount-root guards) to prevent out-of-workspace read/write via in-workspace hardlink paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. -- Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. -- Cron/Announce duplicate guard: track attempted announce/direct delivery separately from confirmed `delivered`, and suppress fallback main-session cron summaries when delivery was already attempted to avoid duplicate end-user sends in uncertain-ack paths. (#27018) -- Cron/Model overrides: when isolated `payload.model` is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972. -- Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting. -- Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (`2026.2.25`). Thanks @luz-oasis for reporting. -- Security/Gateway trusted proxy: require `operator` role for the Control UI trusted-proxy pairing bypass so unpaired `node` sessions can no longer connect via `client.id=control-ui` and invoke node event methods. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Discord/Inbound text: preserve embed `title` + `description` fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. - Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3. +- Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3. - Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3. - Security/Microsoft Teams: isolate group allowlist and command authorization from DM pairing-store entries to prevent cross-context authorization bleed. (#26111) Thanks @bmendonca3. -- Security/Microsoft Teams file consent: bind `fileConsent/invoke` upload acceptance/decline to the originating conversation before consuming pending uploads, preventing cross-conversation pending-file upload or cancellation via leaked `uploadId` values; includes regression coverage for match/mismatch invoke handling. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. -- Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before `setFiles`, with regression coverage for strict missing-path handling. -- Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3. -- Agents/Model fallback: keep explicit text + image fallback chains reachable even when `agents.defaults.models` allowlists are present, prefer explicit run `agentId` over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify `model_cooldown` / `cooling down` errors as `rate_limit` so failover continues. (#11972, #24137, #17231) -- Agents/Model fallback: keep same-provider fallback chains active when session model differs from configured primary, infer cooldown reason from provider profile state (instead of `disabledReason` only), keep no-profile fallback providers eligible (env/models.json paths), and only relax same-provider cooldown fallback attempts for `rate_limit`. (#23816) thanks @ramezgaberiel. -- Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. -- Agents/Model fallback: continue fallback traversal on unrecognized errors when candidates remain, while still throwing the original unknown error on the last candidate. (#26106) Thanks @Sid-Qin. -- Agents/Tools: normalize non-standard plugin tool results that omit `content` so embedded runs no longer crash with `Cannot read properties of undefined (reading 'filter')` after tool completion (including `tesseramemo_query`). (#27007) -- Telegram/Markdown spoilers: keep valid `||spoiler||` pairs while leaving unmatched trailing `||` delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin. -- Hooks/Inbound metadata: include `guildId` and `channelName` in `message_received` metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck. -- Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get `CommandAuthorized: true` on modal/button events. (#26119) Thanks @bmendonca3. -- Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman. -- Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r. -- Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including `NO_REPLY`, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW. -- Telegram/Preview cleanup: keep finalized text previews when a later assistant message is media-only (for example mixed text plus voice turns) by skipping finalized preview archival at assistant-message boundaries, preventing cleanup from deleting already-visible final text messages. (#27042) -- Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example `c0abc12345`) correctly match Slack runtime IDs (`C0ABC12345`) under `groupPolicy: "allowlist"`, preventing silent channel-event drops. (#26878) Thanks @lbo728. -- Voice-call/TTS tools: hide the `tts` tool when the message provider is `voice`, preventing voice-call runs from selecting self-playback TTS and falling into silent no-output loops. (#27025) +- Security/SSRF guard: classify IPv6 multicast literals (`ff00::/8`) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (`2026.2.25`). Thanks @zpbrent for reporting. - Tests/Low-memory stability: disable Vitest `vmForks` by default on low-memory local hosts (`<64 GiB`), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with `setSessionRuntimeModel` usage to avoid deterministic suite failures. (#26324) Thanks @ngutman. -- Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to `file` so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode. ## 2026.2.24 From 550000049298c1f00c41cbdbc8c6de491aecb235 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 04:43:27 +0100 Subject: [PATCH 112/160] chore(protocol): regenerate Swift gateway models --- apps/macos/Sources/OpenClawProtocol/GatewayModels.swift | 4 ++++ .../OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift index 95565a68c4fb..60b44d4545c8 100644 --- a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift @@ -2809,6 +2809,7 @@ public struct ExecApprovalsSnapshot: Codable, Sendable { public struct ExecApprovalRequestParams: Codable, Sendable { public let id: String? public let command: String + public let commandargv: [String]? public let cwd: AnyCodable? public let nodeid: AnyCodable? public let host: AnyCodable? @@ -2823,6 +2824,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { public init( id: String?, command: String, + commandargv: [String]?, cwd: AnyCodable?, nodeid: AnyCodable?, host: AnyCodable?, @@ -2836,6 +2838,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { { self.id = id self.command = command + self.commandargv = commandargv self.cwd = cwd self.nodeid = nodeid self.host = host @@ -2851,6 +2854,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { private enum CodingKeys: String, CodingKey { case id case command + case commandargv = "commandArgv" case cwd case nodeid = "nodeId" case host diff --git a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift index 95565a68c4fb..60b44d4545c8 100644 --- a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift @@ -2809,6 +2809,7 @@ public struct ExecApprovalsSnapshot: Codable, Sendable { public struct ExecApprovalRequestParams: Codable, Sendable { public let id: String? public let command: String + public let commandargv: [String]? public let cwd: AnyCodable? public let nodeid: AnyCodable? public let host: AnyCodable? @@ -2823,6 +2824,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { public init( id: String?, command: String, + commandargv: [String]?, cwd: AnyCodable?, nodeid: AnyCodable?, host: AnyCodable?, @@ -2836,6 +2838,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { { self.id = id self.command = command + self.commandargv = commandargv self.cwd = cwd self.nodeid = nodeid self.host = host @@ -2851,6 +2854,7 @@ public struct ExecApprovalRequestParams: Codable, Sendable { private enum CodingKeys: String, CodingKey { case id case command + case commandargv = "commandArgv" case cwd case nodeid = "nodeId" case host From c7352f6b3f0ddd7b9c56cbeb2ef5355dc9b5ff39 Mon Sep 17 00:00:00 2001 From: bmendonca3 <208517100+bmendonca3@users.noreply.github.com> Date: Tue, 24 Feb 2026 19:07:20 -0700 Subject: [PATCH 113/160] security(telegram): fail closed group allowlist against DM pairing store --- src/telegram/bot-message-context.ts | 14 +++++++----- src/telegram/bot.create-telegram-bot.test.ts | 24 ++++++++++++++++++++ src/telegram/bot/helpers.ts | 14 ++++-------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/telegram/bot-message-context.ts b/src/telegram/bot-message-context.ts index 3ea805c944d5..c3a2cfcdcb1d 100644 --- a/src/telegram/bot-message-context.ts +++ b/src/telegram/bot-message-context.ts @@ -36,7 +36,12 @@ import { recordChannelActivity } from "../infra/channel-activity.js"; import { resolveAgentRoute } from "../routing/resolve-route.js"; import { resolveThreadSessionKeys } from "../routing/session-key.js"; import { withTelegramApiErrorLogging } from "./api-logging.js"; -import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js"; +import { + firstDefined, + isSenderAllowed, + normalizeAllowFrom, + normalizeAllowFromWithStore, +} from "./bot-access.js"; import { buildGroupLabel, buildSenderLabel, @@ -189,11 +194,8 @@ export const buildTelegramMessageContext = async ({ const mentionRegexes = buildMentionRegexes(cfg, route.agentId); const effectiveDmAllow = normalizeAllowFromWithStore({ allowFrom, storeAllowFrom, dmPolicy }); const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom); - const effectiveGroupAllow = normalizeAllowFromWithStore({ - allowFrom: groupAllowOverride ?? groupAllowFrom, - storeAllowFrom, - dmPolicy, - }); + // Group sender checks are explicit and must not inherit DM pairing-store entries. + const effectiveGroupAllow = normalizeAllowFrom(groupAllowOverride ?? groupAllowFrom); const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined"; const senderId = msg.from?.id ? String(msg.from.id) : ""; const senderUsername = msg.from?.username ?? ""; diff --git a/src/telegram/bot.create-telegram-bot.test.ts b/src/telegram/bot.create-telegram-bot.test.ts index 942a1c6c2b3f..4be6b0dcbf3c 100644 --- a/src/telegram/bot.create-telegram-bot.test.ts +++ b/src/telegram/bot.create-telegram-bot.test.ts @@ -1416,6 +1416,30 @@ describe("createTelegramBot", () => { expect(replySpy.mock.calls.length, testCase.name).toBe(0); } }); + it("blocks group sender not in groupAllowFrom even when sender is paired in DM store", async () => { + resetHarnessSpies(); + loadConfig.mockReturnValue({ + channels: { + telegram: { + groupPolicy: "allowlist", + groupAllowFrom: ["222222222"], + groups: { "*": { requireMention: false } }, + }, + }, + }); + readChannelAllowFromStore.mockResolvedValueOnce(["123456789"]); + + await dispatchMessage({ + message: { + chat: { id: -100123456789, type: "group", title: "Test Group" }, + from: { id: 123456789, username: "testuser" }, + text: "hello", + date: 1736380800, + }, + }); + + expect(replySpy).not.toHaveBeenCalled(); + }); it("allows control commands with TG-prefixed groupAllowFrom entries", async () => { loadConfig.mockReturnValue({ channels: { diff --git a/src/telegram/bot/helpers.ts b/src/telegram/bot/helpers.ts index 493ad010082c..0e41a7d0b286 100644 --- a/src/telegram/bot/helpers.ts +++ b/src/telegram/bot/helpers.ts @@ -3,11 +3,7 @@ import { formatLocationText, type NormalizedLocation } from "../../channels/loca import { resolveTelegramPreviewStreamMode } from "../../config/discord-preview-streaming.js"; import type { TelegramGroupConfig, TelegramTopicConfig } from "../../config/types.js"; import { readChannelAllowFromStore } from "../../pairing/pairing-store.js"; -import { - firstDefined, - normalizeAllowFromWithStore, - type NormalizedAllowFrom, -} from "../bot-access.js"; +import { firstDefined, normalizeAllowFrom, type NormalizedAllowFrom } from "../bot-access.js"; import type { TelegramStreamMode } from "./types.js"; const TELEGRAM_GENERAL_TOPIC_ID = 1; @@ -51,11 +47,9 @@ export async function resolveTelegramGroupAllowFromContext(params: { resolvedThreadId, ); const groupAllowOverride = firstDefined(topicConfig?.allowFrom, groupConfig?.allowFrom); - const effectiveGroupAllow = normalizeAllowFromWithStore({ - allowFrom: groupAllowOverride ?? params.groupAllowFrom, - storeAllowFrom, - dmPolicy: params.dmPolicy, - }); + // Group sender access must remain explicit (groupAllowFrom/per-group allowFrom only). + // DM pairing store entries are not a group authorization source. + const effectiveGroupAllow = normalizeAllowFrom(groupAllowOverride ?? params.groupAllowFrom); const hasGroupAllowOverride = typeof groupAllowOverride !== "undefined"; return { resolvedThreadId, From 470c606dac1c9a2fd7333e1a5800c25680012af5 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 09:02:46 +0530 Subject: [PATCH 114/160] refactor(telegram): remove dmPolicy from group allow context helper --- src/telegram/bot-handlers.ts | 1 - src/telegram/bot-native-commands.ts | 1 - src/telegram/bot/helpers.ts | 1 - 3 files changed, 3 deletions(-) diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index a3b4d46a6777..ad28c32883d7 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -554,7 +554,6 @@ export const registerTelegramHandlers = ({ (await resolveTelegramGroupAllowFromContext({ chatId: params.chatId, accountId, - dmPolicy, isForum: params.isForum, messageThreadId: params.messageThreadId, groupAllowFrom, diff --git a/src/telegram/bot-native-commands.ts b/src/telegram/bot-native-commands.ts index 88316cbeb82b..f963aa269cc6 100644 --- a/src/telegram/bot-native-commands.ts +++ b/src/telegram/bot-native-commands.ts @@ -170,7 +170,6 @@ async function resolveTelegramCommandAuth(params: { const groupAllowContext = await resolveTelegramGroupAllowFromContext({ chatId, accountId, - dmPolicy: telegramCfg.dmPolicy ?? "pairing", isForum, messageThreadId, groupAllowFrom, diff --git a/src/telegram/bot/helpers.ts b/src/telegram/bot/helpers.ts index 0e41a7d0b286..ebfe36fbac0c 100644 --- a/src/telegram/bot/helpers.ts +++ b/src/telegram/bot/helpers.ts @@ -16,7 +16,6 @@ export type TelegramThreadSpec = { export async function resolveTelegramGroupAllowFromContext(params: { chatId: string | number; accountId?: string; - dmPolicy?: string; isForum?: boolean; messageThreadId?: number | null; groupAllowFrom?: Array; From 3b0298562b7105c276380f26d966a10216c3f4de Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 09:21:31 +0530 Subject: [PATCH 115/160] fix: document telegram group allowlist hardening (#25988) (thanks @bmendonca3) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4717658d4edd..b112aacf83ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Docs: https://docs.openclaw.ai - Security/Discord reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild `groupPolicy` channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Slack reactions + pins: gate `reaction_*` and `pin_*` system-event enqueue through shared sender authorization so DM `dmPolicy`/`allowFrom` and channel `users` allowlists are enforced consistently for non-message ingress, with regression coverage for denied/allowed sender paths. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Telegram reactions: enforce `dmPolicy`/`allowFrom` and group allowlist authorization on `message_reaction` events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. +- Security/Telegram group allowlist: fail closed for group sender authorization by removing DM pairing-store fallback from group allowlist evaluation; group sender access now requires explicit `groupAllowFrom` or per-group/per-topic `allowFrom`. (#25988) Thanks @bmendonca3. - Security/Slack interactions: enforce channel/DM authorization and modal actor binding (`private_metadata.userId`) before enqueueing `block_action`/`view_submission`/`view_closed` system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (`2026.2.25`). Thanks @tdjackey for reporting. - Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting. - Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3. From bf70614943fe932b49e6071790ef7d464711db60 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 09:27:41 +0530 Subject: [PATCH 116/160] fix(ci): publish latest tag for stable docker release --- .github/workflows/docker-release.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index fc0d97d40913..6ad41bf4b771 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -172,6 +172,9 @@ jobs: if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then version="${GITHUB_REF#refs/tags/v}" tags+=("${IMAGE}:${version}") + if [[ "$version" != *-* ]]; then + tags+=("${IMAGE}:latest") + fi fi if [[ ${#tags[@]} -eq 0 ]]; then echo "::error::No manifest tags resolved for ref ${GITHUB_REF}" From 41314c691dcc905d39d1bf8ce215ad3c63180471 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 09:30:10 +0530 Subject: [PATCH 117/160] fix(ci): gate docker latest tag to stable release format --- .github/workflows/docker-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 6ad41bf4b771..2eb415027afa 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -172,7 +172,7 @@ jobs: if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then version="${GITHUB_REF#refs/tags/v}" tags+=("${IMAGE}:${version}") - if [[ "$version" != *-* ]]; then + if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then tags+=("${IMAGE}:latest") fi fi From 7493f11b406588cbf0a861154efc123d278d30ee Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 09:31:17 +0530 Subject: [PATCH 118/160] fix(ci): allow legacy patch tags to publish docker latest --- .github/workflows/docker-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-release.yml b/.github/workflows/docker-release.yml index 2eb415027afa..eff0993b4664 100644 --- a/.github/workflows/docker-release.yml +++ b/.github/workflows/docker-release.yml @@ -172,7 +172,7 @@ jobs: if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then version="${GITHUB_REF#refs/tags/v}" tags+=("${IMAGE}:${version}") - if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?$ ]]; then tags+=("${IMAGE}:latest") fi fi From 04870a552882c3946aeb3dc790aea55aa9290f7d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 05:12:44 +0100 Subject: [PATCH 119/160] test(session): make fork parent path assertion cross-platform --- src/auto-reply/reply/session.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/auto-reply/reply/session.test.ts b/src/auto-reply/reply/session.test.ts index cdd8b5310c02..12433057b146 100644 --- a/src/auto-reply/reply/session.test.ts +++ b/src/auto-reply/reply/session.test.ts @@ -326,7 +326,12 @@ describe("initSessionState thread forking", () => { expect(result.sessionEntry.forkedFromParent).toBe(true); expect(result.sessionEntry.sessionFile).toBeTruthy(); const forkedContent = await fs.readFile(result.sessionEntry.sessionFile ?? "", "utf-8"); - expect(forkedContent).toContain(parentSessionFile); + const [sessionHeaderLine] = forkedContent.split("\n"); + const sessionHeader = JSON.parse(sessionHeaderLine ?? "{}") as { parentSession?: string }; + expect(sessionHeader.parentSession).toBeTruthy(); + const resolvedParentSession = await fs.realpath(parentSessionFile); + const resolvedForkParentSession = await fs.realpath(sessionHeader.parentSession ?? ""); + expect(resolvedForkParentSession).toBe(resolvedParentSession); }); it("records topic-specific session files when MessageThreadId is present", async () => { From 4b5d4a4c660d05e4bd73f0e11123e68fd9664432 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 05:15:21 +0100 Subject: [PATCH 120/160] docs: finalize 2026.2.25 release notes and appcast --- CHANGELOG.md | 2 +- appcast.xml | 152 ++++++++++++++++++++++----------------------------- 2 files changed, 66 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b112aacf83ea..cfb743032435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Docs: https://docs.openclaw.ai -## 2026.2.25 (Unreleased) +## 2026.2.25 ### Changes diff --git a/appcast.xml b/appcast.xml index 902d60972fd7..f5eb16999343 100644 --- a/appcast.xml +++ b/appcast.xml @@ -209,106 +209,84 @@ - 2026.2.24 - Wed, 25 Feb 2026 02:59:30 +0000 + 2026.2.25 + Thu, 26 Feb 2026 05:14:17 +0100 https://raw.githubusercontent.com/openclaw/openclaw/main/appcast.xml - 14728 - 2026.2.24 + 14883 + 2026.2.25 15.0 - OpenClaw 2026.2.24 + OpenClaw 2026.2.25

Changes

    -
  • Auto-reply/Abort shortcuts: expand standalone stop phrases (stop openclaw, stop action, stop run, stop agent, please stop, and related variants), accept trailing punctuation (for example STOP OPENCLAW!!!), add multilingual stop keywords (including ES/FR/ZH/HI/AR/JP/DE/PT/RU forms), and treat exact do not do that as a stop trigger while preserving strict standalone matching. (#25103) Thanks @steipete and @vincentkoc.
  • -
  • Android/App UX: ship a native four-step onboarding flow, move post-onboarding into a five-tab shell (Connect, Chat, Voice, Screen, Settings), add a full Connect setup/manual mode screen, and refresh Android chat/settings surfaces for the new navigation model.
  • -
  • Talk/Gateway config: add provider-agnostic Talk configuration with legacy compatibility, and expose gateway Talk ElevenLabs config metadata for setup/status surfaces.
  • -
  • Security/Audit: add security.trust_model.multi_user_heuristic to flag likely shared-user ingress and clarify the personal-assistant trust model, with hardening guidance for intentional multi-user setups (sandbox.mode="all", workspace-scoped FS, reduced tool surface, no personal/private identities on shared runtimes).
  • -
  • Dependencies: refresh key runtime and tooling packages across the workspace (Bedrock SDK, pi runtime stack, OpenAI, Google auth, and oxlint/oxfmt), while intentionally keeping @buape/carbon pinned.
  • +
  • Android/Chat: improve streaming delivery handling and markdown rendering quality in the native Android chat UI, including better GitHub-flavored markdown behavior. (#26079) Thanks @obviyus.
  • +
  • Android/Startup perf: defer foreground-service startup, move WebView debugging init out of critical startup, and add startup macrobenchmark + low-noise perf CLI scripts for deterministic cold-start tracking. (#26659) Thanks @obviyus.
  • +
  • UI/Chat compose: add mobile stacked layout for compose action buttons on small screens to improve send/session controls usability. (#11167) Thanks @junyiz.
  • +
  • Heartbeat/Config: replace heartbeat DM toggle with agents.defaults.heartbeat.directPolicy (allow | block; also supported per-agent via agents.list[].heartbeat.directPolicy) for clearer delivery semantics.
  • +
  • Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening.
  • +
  • Branding/Docs + Apple surfaces: replace remaining bot.molt launchd label, bundle-id, logging subsystem, and command examples with ai.openclaw across docs, iOS app surfaces, helper scripts, and CLI test fixtures.
  • +
  • Agents/Config: remind agents to call config.schema before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow.
  • +
  • Dependencies: update workspace dependency pins and lockfile (Bedrock SDK 3.998.0, @mariozechner/pi-* 0.55.1, TypeScript native preview 7.0.0-dev.20260225.1) while keeping @buape/carbon pinned.

Breaking

    -
  • BREAKING: Heartbeat delivery now blocks direct/DM targets when destination parsing identifies a direct chat (for example user:, Telegram user chat IDs, or WhatsApp direct numbers/JIDs). Heartbeat runs still execute, but direct-message delivery is skipped and only non-DM destinations (for example channel/group targets) can receive outbound heartbeat messages.
  • -
  • BREAKING: Security/Sandbox: block Docker network: "container:" namespace-join mode by default for sandbox and sandbox-browser containers. To keep that behavior intentionally, set agents.defaults.sandbox.docker.dangerouslyAllowContainerNamespaceJoin: true (break-glass). Thanks @tdjackey for reporting.
  • +
  • BREAKING: Heartbeat direct/DM delivery default is now allow again. To keep DM-blocked behavior from 2026.2.24, set agents.defaults.heartbeat.directPolicy: "block" (or per-agent override).

Fixes

    -
  • Routing/Session isolation: harden followup routing so explicit cross-channel origin replies never fall back to the active dispatcher on route failure, preserve queued overflow summary routing metadata (channel/to/thread) across followup drain, and prefer originating channel context over internal provider tags for embedded followup runs. This prevents webchat/control-ui context from hijacking Discord-targeted replies in shared sessions. (#25864) Thanks @Gamedesigner.
  • -
  • Security/Routing: fail closed for shared-session cross-channel replies by binding outbound target resolution to the current turn’s source channel metadata (instead of stale session route fallbacks), and wire those turn-source fields through gateway + command delivery planners with regression coverage. (#24571) Thanks @brandonwise.
  • -
  • Heartbeat routing: prevent heartbeat leakage/spam into Discord and other direct-message destinations by blocking direct-chat heartbeat delivery targets and keeping blocked-delivery cron/exec prompts internal-only. (#25871)
  • -
  • Heartbeat defaults/prompts: switch the implicit heartbeat delivery target from last to none (opt-in for external delivery), and use internal-only cron/exec heartbeat prompt wording when delivery is disabled so background checks do not nudge user-facing relay behavior. (#25871, #24638, #25851)
  • -
  • Auto-reply/Heartbeat queueing: drop heartbeat runs when a session already has an active run instead of enqueueing a stale followup, preventing duplicate heartbeat response branches after queue drain. (#25610, #25606) Thanks @mcaxtr.
  • -
  • Cron/Heartbeat delivery: stop inheriting cached session lastThreadId for heartbeat-mode target resolution unless a thread/topic is explicitly requested, so announce-mode cron and heartbeat deliveries stay on top-level destinations instead of leaking into active conversation threads. (#25730) Thanks @markshields-tl.
  • -
  • Messaging tool dedupe: treat originating channel metadata as authoritative for same-target message.send suppression in proactive runs (heartbeat/cron/exec-event), including synthetic-provider contexts, so delivery-mirror transcript entries no longer cause duplicate Telegram sends. (#25835) Thanks @jadeathena84-arch.
  • -
  • Channels/Typing keepalive: refresh channel typing callbacks on a keepalive interval during long replies and clear keepalive timers on idle/cleanup across core + extension dispatcher callsites so typing indicators do not expire mid-inference. (#25886, #25882) Thanks @stakeswky.
  • -
  • Agents/Model fallback: when a run is currently on a configured fallback model, keep traversing the configured fallback chain instead of collapsing straight to primary-only, preventing dead-end failures when primary stays in cooldown. (#25922, #25912) Thanks @Taskle.
  • -
  • Gateway/Models: honor explicit agents.defaults.models allowlist refs even when bundled model catalog data is stale, synthesize missing allowlist entries in models.list, and allow sessions.patch//model selection for those refs without false model not allowed errors. (#20291) Thanks @kensipe, @nikolasdehor, and @vincentkoc.
  • -
  • Control UI/Agents: inherit agents.defaults.model.fallbacks in the Overview fallback input when no per-agent model entry exists, while preserving explicit per-agent fallback overrides (including empty lists). (#25729, #25710) Thanks @Suko.
  • -
  • Automation/Subagent/Cron reliability: honor ANNOUNCE_SKIP in sessions_spawn completion/direct announce flows (no user-visible token leaks), add transient direct-announce retries for channel unavailability (for example WhatsApp listener reconnect windows), and include cron in the coding tool profile so /tools/invoke can execute cron actions when explicitly allowed by gateway policy. (#25800, #25656, #25842, #25813, #25822, #25821) Thanks @astra-fer, @aaajiao, @dwight11232-coder, @kevinWangSheng, @widingmarcus-cyber, and @stakeswky.
  • -
  • Discord/Voice reliability: restore runtime DAVE dependency (@snazzah/davey), add configurable DAVE join options (channels.discord.voice.daveEncryption and channels.discord.voice.decryptionFailureTolerance), clean up voice listeners/session teardown, guard against stale connection events, and trigger controlled rejoin recovery after repeated decrypt failures to improve inbound STT stability under DAVE receive errors. (#25861, #25372, #24883, #24825, #23890, #23105, #22961, #23421, #23278, #23032)
  • -
  • Discord/Block streaming: restore block-streamed reply delivery by suppressing only reasoning payloads (instead of all block payloads), fixing missing Discord replies in channels.discord.streaming=block mode. (#25839, #25836, #25792) Thanks @pewallin.
  • -
  • Discord/Proxy + reactions + model picker: thread channel proxy fetch into inbound media/sticker downloads, use proxy-aware gateway metadata fetch for WSL/corporate proxy setups, wire messages.statusReactions.{emojis,timing} into Discord reaction lifecycle control, and compact model-picker custom_id keys to stay under Discord's 100-char limit while keeping backward-compatible parsing. (#25232, #25507, #25564, #25695) Thanks @openperf, @chilu18, @Yipsh, @lbo728, and @s1korrrr.
  • -
  • WhatsApp/Web reconnect: treat close status 440 as non-retryable (including string-form status values), stop reconnect loops immediately, and emit operator guidance to relink after resolving session conflicts. (#25858) Thanks @markmusson.
  • -
  • WhatsApp/Reasoning safety: suppress outbound payloads marked as reasoning and hard-drop text payloads that begin with Reasoning: before WhatsApp delivery, preventing hidden thinking blocks from leaking to end users through final-message paths. (#25804, #25214, #24328)
  • -
  • Matrix/Read receipts: send read receipts as soon as Matrix messages arrive (before handler pipeline work), so clients no longer show long-lived unread/sent states while replies are processing. (#25841, #25840) Thanks @joshjhall.
  • -
  • Telegram/Replies: when markdown formatting renders to empty HTML (for example syntax-only chunks in threaded replies), retry delivery with plain text, and fail loud when both formatted and plain payloads are empty to avoid false delivered states. (#25096, #25091) Thanks @Glucksberg.
  • -
  • Telegram/Media fetch: prioritize IPv4 before IPv6 in SSRF pinned DNS address ordering so media downloads still work on hosts with broken IPv6 routing. (#24295, #23975) Thanks @Glucksberg.
  • -
  • Telegram/Outbound API: replace Node 22's global undici dispatcher when applying Telegram autoSelectFamily decisions so outbound fetch calls inherit IPv4 fallback instead of staying pinned to stale dispatcher settings. (#25682, #25676) Thanks @lairtonlelis.
  • -
  • Onboarding/Telegram: keep core-channel onboarding available when plugin registry population is missing by falling back to built-in adapters and continuing wizard setup with actionable recovery guidance. (#25803) Thanks @Suko.
  • -
  • Android/Gateway auth: preserve Android gateway auth state across onboarding, use the native client id for operator sessions, retry with shared-token fallback after device-token auth failures, and avoid clearing tokens on transient connect errors.
  • -
  • Slack/DM routing: treat D* channel IDs as direct messages even when Slack sends an incorrect channel_type, preventing DM traffic from being misclassified as channel/group chats. (#25479) Thanks @mcaxtr.
  • -
  • Zalo/Group policy: enforce sender authorization for group messages with groupPolicy + groupAllowFrom (fallback to allowFrom), default runtime group behavior to fail-closed allowlist, and block unauthorized non-command group messages before dispatch. Thanks @tdjackey for reporting.
  • -
  • macOS/Voice input: guard all audio-input startup paths against missing default microphones (Voice Wake, Talk Mode, Push-to-Talk, mic-level monitor, tester) to avoid launch/runtime crashes on mic-less Macs and fail gracefully until input becomes available. (#25817) Thanks @sfo2001.
  • -
  • macOS/IME input: when marked text is active, treat Return as IME candidate confirmation first in both the voice overlay composer and shared chat composer to prevent accidental sends while composing CJK text. (#25178) Thanks @bottotl.
  • -
  • macOS/Voice wake routing: default forwarded voice-wake transcripts to the webchat channel (instead of ambiguous last routing) so local voice prompts stay pinned to the control chat surface unless explicitly overridden. (#25440) Thanks @chilu18.
  • -
  • macOS/Gateway launch: prefer an available openclaw binary before pnpm/node runtime fallback when resolving local gateway commands, so local startup no longer fails on hosts with broken runtime discovery. (#25512) Thanks @chilu18.
  • -
  • macOS/Menu bar: stop reusing the injector delegate for the "Usage cost (30 days)" submenu to prevent recursive submenu injection loops when opening cost history. (#25341) Thanks @yingchunbai.
  • -
  • macOS/WebChat panel: fix rounded-corner clipping by using panel-specific visual-effect blending and matching corner masking on both effect and hosting layers. (#22458) Thanks @apethree and @agisilaos.
  • -
  • Windows/Exec shell selection: prefer PowerShell 7 (pwsh) discovery (Program Files, ProgramW6432, PATH) before falling back to Windows PowerShell 5.1, fixing && command chaining failures on Windows hosts with PS7 installed. (#25684, #25638) Thanks @zerone0x.
  • -
  • Windows/Media safety checks: align async local-file identity validation with sync-safe-open behavior by treating win32 dev=0 stats as unknown-device fallbacks (while keeping strict dev checks when both sides are non-zero), fixing false Local media path is not safe to read drops for local attachments/TTS/images. (#25708, #21989, #25699, #25878) Thanks @kevinWangSheng.
  • -
  • iMessage/Reasoning safety: harden iMessage echo suppression with outbound messageId matching (plus scoped text fallback), and enforce reasoning-payload suppression on routed outbound delivery paths to prevent hidden thinking text from being sent as user-visible channel messages. (#25897, #1649, #25757) Thanks @rmarr and @Iranb.
  • -
  • Providers/OpenRouter/Auth profiles: bypass auth-profile cooldown/disable windows for OpenRouter, so provider failures no longer put OpenRouter profiles into local cooldown and stale legacy cooldown markers are ignored in fallback and status selection paths. (#25892) Thanks @alexanderatallah for raising this and @vincentkoc for the fix.
  • -
  • Providers/Google reasoning: sanitize invalid negative thinkingBudget payloads for Gemini 3.1 requests by dropping -1 budgets and mapping configured reasoning effort to thinkingLevel, preventing malformed reasoning payloads on google-generative-ai. (#25900)
  • -
  • Providers/SiliconFlow: normalize thinking="off" to thinking: null for Pro/* model payloads to avoid provider-side 400 loops and misleading compaction retries. (#25435) Thanks @Zjianru.
  • -
  • Models/Bedrock auth: normalize additional Bedrock provider aliases (bedrock, aws-bedrock, aws_bedrock, amazon bedrock) to canonical amazon-bedrock, ensuring auth-mode resolution consistently selects AWS SDK fallback. (#25756) Thanks @fwhite13.
  • -
  • Models/Providers: preserve explicit user reasoning overrides when merging provider model config with built-in catalog metadata, so reasoning: false is no longer overwritten by catalog defaults. (#25314) Thanks @lbo728.
  • -
  • Gateway/Auth: allow trusted-proxy authenticated Control UI websocket sessions to skip device pairing when device identity is absent, preventing false pairing required failures behind trusted reverse proxies. (#25428) Thanks @SidQin-cyber.
  • -
  • CLI/Memory search: accept --query for openclaw memory search (while keeping positional query support), and emit a clear error when neither form is provided. (#25904, #25857) Thanks @niceysam and @stakeswky.
  • -
  • CLI/Doctor: correct stale recovery hints to use valid commands (openclaw gateway status --deep and openclaw configure --section model). (#24485) Thanks @chilu18.
  • -
  • Doctor/Sandbox: when sandbox mode is enabled but Docker is unavailable, surface a clear actionable warning (including failure impact and remediation) instead of a mild “skip checks” note. (#25438) Thanks @mcaxtr.
  • -
  • Doctor/Plugins: auto-enable now resolves third-party channel plugins by manifest plugin id (not channel id), preventing invalid plugins.entries. writes when ids differ. (#25275) Thanks @zerone0x.
  • -
  • Config/Plugins: treat stale removed google-antigravity-auth plugin references as compatibility warnings (not hard validation errors) across plugins.entries, plugins.allow, plugins.deny, and plugins.slots.memory, so startup no longer fails after antigravity removal. (#25538, #25862) Thanks @chilu18.
  • -
  • Config/Meta: accept numeric meta.lastTouchedAt timestamps and coerce them to ISO strings, preserving compatibility with agent edits that write Date.now() values. (#25491) Thanks @mcaxtr.
  • -
  • Usage accounting: parse Moonshot/Kimi cached_tokens fields (including prompt_tokens_details.cached_tokens) into normalized cache-read usage metrics. (#25436) Thanks @Elarwei001.
  • -
  • Agents/Tool dispatch: await block-reply flush before tool execution starts so buffered block replies preserve message ordering around tool calls. (#25427) Thanks @SidQin-cyber.
  • -
  • Agents/Billing classification: prevent long assistant/user-facing text from being rewritten as billing failures while preserving explicit status/code/http 402 detection for oversized structured error payloads. (#25680, #25661) Thanks @lairtonlelis.
  • -
  • Sessions/Tool-result guard: avoid generating synthetic toolResult entries for assistant turns that ended with stopReason: "aborted" or "error", preventing orphaned tool-use IDs from triggering downstream API validation errors. (#25429) Thanks @mikaeldiakhate-cell.
  • -
  • Auto-reply/Reset hooks: guarantee native /new and /reset flows emit command/reset hooks even on early-return command paths, with dedupe protection to avoid double hook emission. (#25459) Thanks @chilu18.
  • -
  • Hooks/Slug generator: resolve session slug model from the agent’s effective model (including defaults/fallback resolution) instead of raw agent-primary config only. (#25485) Thanks @SudeepMalipeddi.
  • -
  • Sandbox/FS bridge tests: add regression coverage for dash-leading basenames to confirm sandbox file reads resolve to absolute container paths (and avoid shell-option misdiagnosis for dashed filenames). (#25891) Thanks @albertlieyingadrian.
  • -
  • Sandbox/FS bridge: build canonical-path shell scripts with newline separators (not ; joins) to avoid POSIX sh do; syntax errors that broke sandbox file/image read-write operations. (#25737, #25824, #25868) Thanks @DennisGoldfinger and @peteragility.
  • -
  • Sandbox/Config: preserve dangerouslyAllowReservedContainerTargets and dangerouslyAllowExternalBindSources during sandbox docker config resolution so explicit bind-mount break-glass overrides reach runtime validation. (#25410) Thanks @skyer-jian.
  • -
  • Gateway/Security: enforce gateway auth for the exact /api/channels plugin root path (plus /api/channels/ descendants), with regression coverage for query/trailing-slash variants and near-miss paths that must remain plugin-owned. (#25753) Thanks @bmendonca3.
  • -
  • Exec approvals: treat bare allowlist * as a true wildcard for parsed executables, including unresolved PATH lookups, so global opt-in allowlists work as configured. (#25250) Thanks @widingmarcus-cyber.
  • -
  • iOS/Signing: improve scripts/ios-team-id.sh for Xcode 16+ by falling back to Xcode-managed provisioning profiles, add actionable guidance when an Apple account exists but no Team ID can be resolved, and ignore Xcode xcodebuild output directories (apps/ios/build, apps/shared/OpenClawKit/build, Swabble/build). (#22773) Thanks @brianleach.
  • -
  • Control UI/Chat images: route image-click opens through a shared safe-open helper (allowing only safe URL schemes) and open new tabs with opener isolation to block tabnabbing. (#18685, #25444, #25847) Thanks @Mariana-Codebase and @shakkernerd.
  • -
  • Security/Exec: sanitize inherited host execution environment before merge, canonicalize inherited PATH handling, and strip dangerous keys (LD_*, DYLD_*, SSLKEYLOGFILE, and related injection vectors) from non-sandboxed exec runs. (#25755) Thanks @bmendonca3.
  • -
  • Security/Hooks: normalize hook session-key classification with trim/lowercase plus Unicode NFKC folding (for example full-width HOOK:...) so external-content wrapping cannot be bypassed by mixed-case or lookalike prefixes. (#25750) Thanks @bmendonca3.
  • -
  • Security/Voice Call: add Telnyx webhook replay detection and canonicalize replay-key signature encoding (Base64/Base64URL equivalent forms dedupe together), so duplicate signed webhook deliveries no longer re-trigger side effects. (#25832) Thanks @bmendonca3.
  • -
  • Security/Sandbox media: restrict sandbox media tmp-path allowances to OpenClaw-managed tmp roots instead of broad host os.tmpdir() trust, and add outbound/channel guardrails (tmp-path lint + media-root smoke tests) to prevent regressions in local media attachment reads. Thanks @tdjackey for reporting.
  • -
  • Security/Sandbox media: reject hard-linked OpenClaw tmp media aliases (including symlink-to-hardlink chains) during sandbox media path resolution to prevent out-of-sandbox inode alias reads. (#25820) Thanks @bmendonca3.
  • -
  • Security/Message actions: enforce local media root checks for sendAttachment and setGroupIcon when sandboxRoot is unset, preventing attachment hydration from reading arbitrary host files via local absolute paths. Thanks @GCXWLP for reporting.
  • -
  • Security/Telegram: enforce DM authorization before media download/write (including media groups) and move telegram inbound activity tracking after DM authorization, preventing unauthorized sender-triggered inbound media disk writes. Thanks @v8hid for reporting.
  • -
  • Security/Workspace FS: normalize @-prefixed paths before workspace-boundary checks (including workspace-only read/write/edit and sandbox mount path guards), preventing absolute-path escape attempts from bypassing guard validation. Thanks @tdjackey for reporting.
  • -
  • Security/Synology Chat: enforce fail-closed allowlist behavior for DM ingress so dmPolicy: "allowlist" with empty allowedUserIds rejects all senders instead of allowing unauthorized dispatch. (#25827) Thanks @bmendonca3 for the contribution and @tdjackey for reporting.
  • -
  • Security/Native images: enforce tools.fs.workspaceOnly for native prompt image auto-load (including history refs), preventing out-of-workspace sandbox mounts from being implicitly ingested as vision input. Thanks @tdjackey for reporting.
  • -
  • Security/Exec approvals: bind system.run command display/approval text to full argv when shell-wrapper inline payloads carry positional argv values, and reject payload-only rawCommand mismatches for those wrapper-carrier forms, preventing hidden command execution under misleading approval text. Thanks @tdjackey for reporting.
  • -
  • Security/Exec companion host: forward canonical system.run display text (not payload-only shell snippets) to the macOS exec host, and enforce rawCommand/argv consistency there for shell-wrapper positional-argv carriers and env-modifier preludes, preventing companion-side approval/display drift. Thanks @tdjackey for reporting.
  • -
  • Security/Exec approvals: fail closed when transparent dispatch-wrapper unwrapping exceeds the depth cap, so nested /usr/bin/env chains cannot bypass shell-wrapper approval gating in allowlist + ask=on-miss mode. Thanks @tdjackey for reporting.
  • -
  • Security/Exec: limit default safe-bin trusted directories to immutable system paths (/bin, /usr/bin) and require explicit opt-in (tools.exec.safeBinTrustedDirs) for package-manager/user bin paths (for example Homebrew), add security-audit findings for risky trusted-dir choices, warn at runtime when explicitly trusted dirs are group/world writable, and add doctor hints when configured safeBins resolve outside trusted dirs. Thanks @tdjackey for reporting.
  • -
  • Security/Sandbox: canonicalize bind-mount source paths via existing-ancestor realpath so symlink-parent + non-existent-leaf paths cannot bypass allowed-source-roots or blocked-path checks. Thanks @tdjackey.
  • +
  • Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without message_id as delivery failures (instead of false-success "unknown" IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808.
  • +
  • Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156)
  • +
  • Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable session.parentForkMaxTokens (default 100000, 0 disables). (#26912) Thanks @markshields-tl.
  • +
  • Cron/Message multi-account routing: honor explicit delivery.accountId for isolated cron delivery resolution, and when message.send omits accountId, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky.
  • +
  • Gateway/Message media roots: thread agentId through gateway send RPC and prefer explicit agentId over session/default resolution so non-default agent workspace media sends no longer fail with LocalMediaAccessError; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin.
  • +
  • Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin.
  • +
  • Cron/Announce duplicate guard: track attempted announce/direct delivery separately from confirmed delivered, and suppress fallback main-session cron summaries when delivery was already attempted to avoid duplicate end-user sends in uncertain-ack paths. (#27018)
  • +
  • LINE/Lifecycle: keep LINE startAccount pending until abort so webhook startup is no longer misread as immediate channel exit, preventing restart-loop storms on LINE provider boot. (#26528) Thanks @Sid-Qin.
  • +
  • Discord/Gateway: capture and drain startup-time gateway error events before lifecycle listeners attach so early Fatal Gateway error: 4014 closes surface as actionable intent guidance instead of uncaught gateway crashes. (#23832) Thanks @theotarr.
  • +
  • Discord/Inbound text: preserve embed title + description fallback text in message and forwarded snapshot parsing so embed titles are not silently dropped from agent input. (#26946) Thanks @stakeswky.
  • +
  • Slack/Inbound media fallback: deliver file-only messages even when Slack media downloads fail by adding a filename placeholder fallback, capping fallback names to the shared media-file limit, and normalizing empty filenames to file so attachment-only messages are not silently dropped. (#25181) Thanks @justinhuangcode.
  • +
  • Telegram/Preview cleanup: keep finalized text previews when a later assistant message is media-only (for example mixed text plus voice turns) by skipping finalized preview archival at assistant-message boundaries, preventing cleanup from deleting already-visible final text messages. (#27042)
  • +
  • Telegram/Markdown spoilers: keep valid ||spoiler|| pairs while leaving unmatched trailing || delimiters as literal text, avoiding false all-or-nothing spoiler suppression. (#26105) Thanks @Sid-Qin.
  • +
  • Slack/Allowlist channels: match channel IDs case-insensitively during channel allowlist resolution so lowercase config keys (for example c0abc12345) correctly match Slack runtime IDs (C0ABC12345) under groupPolicy: "allowlist", preventing silent channel-event drops. (#26878) Thanks @lbo728.
  • +
  • Discord/Typing indicator: prevent stuck typing indicators by sealing channel typing keepalive callbacks after idle/cleanup and ensuring Discord dispatch always marks typing idle even if preview-stream cleanup fails. (#26295) Thanks @ngutman.
  • +
  • Channels/Typing indicator: guard typing keepalive start callbacks after idle/cleanup close so post-close ticks cannot re-trigger stale typing indicators. (#26325) Thanks @win4r.
  • +
  • Followups/Typing indicator: ensure followup turns mark dispatch idle on every exit path (including NO_REPLY, empty payloads, and agent errors) so typing keepalive cleanup always runs and channel typing indicators do not get stuck after queued/silent followups. (#26881) Thanks @codexGW.
  • +
  • Voice-call/TTS tools: hide the tts tool when the message provider is voice, preventing voice-call runs from selecting self-playback TTS and falling into silent no-output loops. (#27025)
  • +
  • Agents/Tools: normalize non-standard plugin tool results that omit content so embedded runs no longer crash with Cannot read properties of undefined (reading 'filter') after tool completion (including tesseramemo_query). (#27007)
  • +
  • Cron/Model overrides: when isolated payload.model is no longer allowlisted, fall back to default model selection instead of failing the job, while still returning explicit errors for invalid model strings. (#26717) Thanks @Youyou972.
  • +
  • Agents/Model fallback: keep explicit text + image fallback chains reachable even when agents.defaults.models allowlists are present, prefer explicit run agentId over session-key parsing for followup fallback override resolution (with session-key fallback), treat agent-level fallback overrides as configured in embedded runner preflight, and classify model_cooldown / cooling down errors as rate_limit so failover continues. (#11972, #24137, #17231)
  • +
  • Agents/Model fallback: keep same-provider fallback chains active when session model differs from configured primary, infer cooldown reason from provider profile state (instead of disabledReason only), keep no-profile fallback providers eligible (env/models.json paths), and only relax same-provider cooldown fallback attempts for rate_limit. (#23816) thanks @ramezgaberiel.
  • +
  • Agents/Model fallback: continue fallback traversal on unrecognized errors when candidates remain, while still throwing the original unknown error on the last candidate. (#26106) Thanks @Sid-Qin.
  • +
  • Models/Auth probes: map permanent auth failover reasons (auth_permanent, for example revoked keys) into probe auth status instead of unknown, so openclaw models status --probe reports actionable auth failures. (#25754) thanks @rrenamed.
  • +
  • Hooks/Inbound metadata: include guildId and channelName in message_received metadata for both plugin and internal hook paths. (#26115) Thanks @davidrudduck.
  • +
  • Discord/Component auth: evaluate guild component interactions with command-gating authorizers so unauthorized users no longer get CommandAuthorized: true on modal/button events. (#26119) Thanks @bmendonca3.
  • +
  • Security/Gateway auth: require pairing for operator device-identity sessions authenticated with shared token auth so unpaired devices cannot self-assign operator scopes. Thanks @tdjackey for reporting.
  • +
  • Security/Gateway WebSocket auth: enforce origin checks for direct browser WebSocket clients beyond Control UI/Webchat, apply password-auth failure throttling to browser-origin loopback attempts (including localhost), and block silent auto-pairing for non-Control-UI browser clients to prevent cross-origin brute-force and session takeover chains. This ships in the next npm release (2026.2.25). Thanks @luz-oasis for reporting.
  • +
  • Security/Gateway trusted proxy: require operator role for the Control UI trusted-proxy pairing bypass so unpaired node sessions can no longer connect via client.id=control-ui and invoke node event methods. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/macOS beta onboarding: remove Anthropic OAuth sign-in and the legacy oauth.json onboarding path that exposed the PKCE verifier via OAuth state; this impacted the macOS beta onboarding path only. Anthropic subscription auth is now setup-token-only and will ship in the next npm release (2026.2.25). Thanks @zdi-disclosures for reporting.
  • +
  • Security/Microsoft Teams file consent: bind fileConsent/invoke upload acceptance/decline to the originating conversation before consuming pending uploads, preventing cross-conversation pending-file upload or cancellation via leaked uploadId values; includes regression coverage for match/mismatch invoke handling. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Gateway: harden agents.files path handling to block out-of-workspace symlink targets for agents.files.get/agents.files.set, keep in-workspace symlink targets supported, and add gateway regression coverage for both blocked escapes and allowed in-workspace symlinks. Thanks @tdjackey for reporting.
  • +
  • Security/Workspace FS: reject hardlinked workspace file aliases in tools.fs.workspaceOnly and tools.exec.applyPatch.workspaceOnly boundary checks (including sandbox mount-root guards) to prevent out-of-workspace read/write via in-workspace hardlink paths. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Browser temp paths: harden trace/download output-path handling against symlink-root and symlink-parent escapes with realpath-based write-path checks plus secure fallback tmp-dir validation that fails closed on unsafe fallback links. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Browser uploads: revalidate upload paths at use-time in Playwright file-chooser and direct-input flows so missing/rebound paths are rejected before setFiles, with regression coverage for strict missing-path handling.
  • +
  • Security/Exec approvals: bind system.run approval matching to exact argv identity and preserve argv whitespace in rendered command text, preventing trailing-space executable path swaps from reusing a mismatched approval. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Exec approvals: harden approval-bound system.run execution on node hosts by rejecting symlink cwd paths and canonicalizing path-like executable argv before spawn, blocking mutable-cwd symlink retarget chains between approval and execution. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Signal: enforce DM/group authorization before reaction-only notification enqueue so unauthorized senders can no longer inject Signal reaction system events under dmPolicy/groupPolicy; reaction notifications now require channel access checks first. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Discord reactions: enforce DM policy/allowlist authorization before reaction-event system enqueue in direct messages; Discord reaction handling now also honors DM/group-DM enablement and guild groupPolicy channel gating to keep reaction ingress aligned with normal message preflight. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Slack reactions + pins: gate reaction_* and pin_* system-event enqueue through shared sender authorization so DM dmPolicy/allowFrom and channel users allowlists are enforced consistently for non-message ingress, with regression coverage for denied/allowed sender paths. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Telegram reactions: enforce dmPolicy/allowFrom and group allowlist authorization on message_reaction events before enqueueing reaction system events, preventing unauthorized reaction-triggered input in DMs and groups; ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Slack interactions: enforce channel/DM authorization and modal actor binding (private_metadata.userId) before enqueueing block_action/view_submission/view_closed system events, with regression coverage for unauthorized senders and missing/mismatched actor metadata. This ships in the next npm release (2026.2.25). Thanks @tdjackey for reporting.
  • +
  • Security/Nextcloud Talk: drop replayed signed webhook events with persistent per-account replay dedupe across restarts, and reject unexpected webhook backend origins when account base URL is configured. Thanks @aristorechina for reporting.
  • +
  • Security/Nextcloud Talk: reject unsigned webhook traffic before full body reads, reducing unauthenticated request-body exposure, with auth-order regression coverage. (#26118) Thanks @bmendonca3.
  • +
  • Security/Nextcloud Talk: stop treating DM pairing-store entries as group allowlist senders, so group authorization remains bounded to configured group allowlists. (#26116) Thanks @bmendonca3.
  • +
  • Security/LINE: cap unsigned webhook body reads before auth/signature handling to bound unauthenticated body processing. (#26095) Thanks @bmendonca3.
  • +
  • Security/IRC: keep pairing-store approvals DM-only and out of IRC group allowlist authorization, with policy regression tests for allowlist resolution. (#26112) Thanks @bmendonca3.
  • +
  • Security/Microsoft Teams: isolate group allowlist and command authorization from DM pairing-store entries to prevent cross-context authorization bleed. (#26111) Thanks @bmendonca3.
  • +
  • Security/SSRF guard: classify IPv6 multicast literals (ff00::/8) as blocked/private-internal targets in shared SSRF IP checks, preventing multicast literals from bypassing URL-host preflight and DNS answer validation. This ships in the next npm release (2026.2.25). Thanks @zpbrent for reporting.
  • +
  • Tests/Low-memory stability: disable Vitest vmForks by default on low-memory local hosts (<64 GiB), keep low-profile extension lane parallelism at 4 workers, and align cron isolated-agent tests with setSessionRuntimeModel usage to avoid deterministic suite failures. (#26324) Thanks @ngutman.

View full changelog

]]>
- +
\ No newline at end of file From cb3e5c35b05e921a67978035958a4d8bfb4b8452 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 05:23:30 +0100 Subject: [PATCH 121/160] docs: fix onboarding markdown list spacing --- docs/start/onboarding.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/start/onboarding.md b/docs/start/onboarding.md index 679ab059f459..dfa058af545b 100644 --- a/docs/start/onboarding.md +++ b/docs/start/onboarding.md @@ -34,6 +34,7 @@ Security trust model: - By default, OpenClaw is a personal agent: one trusted operator boundary. - Shared/multi-user setups require lock-down (split trust boundaries, keep tool access minimal, and follow [Security](/gateway/security)). +
@@ -50,9 +51,11 @@ Where does the **Gateway** run? **Gateway auth tip:** + - The wizard now generates a **token** even for loopback, so local WS clients must authenticate. - If you disable auth, any local process can connect; use that only on fully trusted machines. - Use a **token** for multi‑machine access or non‑loopback binds. + From e8197404d0984289cf7aaee8a61007ce59fe9d53 Mon Sep 17 00:00:00 2001 From: pandego <7780875+pandego@users.noreply.github.com> Date: Wed, 25 Feb 2026 08:35:48 +0100 Subject: [PATCH 122/160] Docker/docs: reduce docker build OOM risk on small GCP hosts --- Dockerfile | 3 +++ docs/install/docker.md | 1 + docs/install/gcp.md | 13 ++++++++----- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 255340cb02bf..c5f7b1dc2775 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,9 @@ COPY --chown=node:node patches ./patches COPY --chown=node:node scripts ./scripts USER node +# Reduce OOM risk on low-memory hosts during dependency installation. +# Docker builds on small VMs may otherwise fail with "Killed" (exit 137). +ENV NODE_OPTIONS=--max-old-space-size=2048 RUN pnpm install --frozen-lockfile # Optionally install Chromium and Xvfb for browser automation. diff --git a/docs/install/docker.md b/docs/install/docker.md index decd1d779ee7..42cefd4be01b 100644 --- a/docs/install/docker.md +++ b/docs/install/docker.md @@ -26,6 +26,7 @@ Sandboxing details: [Sandboxing](/gateway/sandboxing) ## Requirements - Docker Desktop (or Docker Engine) + Docker Compose v2 +- At least 2 GB RAM for image build (`pnpm install` may be OOM-killed on 1 GB hosts with exit 137) - Enough disk for images + logs ## Containerized Gateway (Docker Compose) diff --git a/docs/install/gcp.md b/docs/install/gcp.md index b0ec51a75dd0..a4485611402c 100644 --- a/docs/install/gcp.md +++ b/docs/install/gcp.md @@ -114,10 +114,11 @@ gcloud services enable compute.googleapis.com **Machine types:** -| Type | Specs | Cost | Notes | -| -------- | ------------------------ | ------------------ | ------------------ | -| e2-small | 2 vCPU, 2GB RAM | ~$12/mo | Recommended | -| e2-micro | 2 vCPU (shared), 1GB RAM | Free tier eligible | May OOM under load | +| Type | Specs | Cost | Notes | +| --------- | ------------------------ | ------------------ | -------------------------------------------- | +| e2-medium | 2 vCPU, 4GB RAM | ~$25/mo | Most reliable for local Docker builds | +| e2-small | 2 vCPU, 2GB RAM | ~$12/mo | Minimum recommended for Docker build | +| e2-micro | 2 vCPU (shared), 1GB RAM | Free tier eligible | Often fails with Docker build OOM (exit 137) | **CLI:** @@ -350,6 +351,8 @@ docker compose build docker compose up -d openclaw-gateway ``` +If build fails with `Killed` / `exit code 137` during `pnpm install --frozen-lockfile`, the VM is out of memory. Use `e2-small` minimum, or `e2-medium` for more reliable first builds. + Verify binaries: ```bash @@ -449,7 +452,7 @@ Ensure your account has the required IAM permissions (Compute OS Login or Comput **Out of memory (OOM)** -If using e2-micro and hitting OOM, upgrade to e2-small or e2-medium: +If Docker build fails with `Killed` and `exit code 137`, the VM was OOM-killed. Upgrade to e2-small (minimum) or e2-medium (recommended for reliable local builds): ```bash # Stop the VM first From 35976da7a0785cda4002f7379395a6515d12f0c1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 26 Feb 2026 05:45:57 +0100 Subject: [PATCH 123/160] fix: harden Docker/GCP onboarding flow (#26253) (thanks @pandego) --- CHANGELOG.md | 1 + Dockerfile | 3 +- docker-setup.sh | 82 +++++++++++++++++++++++++++++++++++++++- docs/install/gcp.md | 23 ++++++++++- src/docker-setup.test.ts | 21 ++++++++++ src/dockerfile.test.ts | 2 +- 6 files changed, 127 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb743032435..0162138f63a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Docker/GCP onboarding: reduce first-build OOM risk by capping Node heap during `pnpm install`, reuse existing gateway token during `docker-setup.sh` reruns so `.env` stays aligned with config, auto-bootstrap Control UI allowed origins for non-loopback Docker binds, and add GCP docs guidance for tokenized dashboard links + pairing recovery commands. (#26253) Thanks @pandego. - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. diff --git a/Dockerfile b/Dockerfile index c5f7b1dc2775..2229a299a560 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,8 +25,7 @@ COPY --chown=node:node scripts ./scripts USER node # Reduce OOM risk on low-memory hosts during dependency installation. # Docker builds on small VMs may otherwise fail with "Killed" (exit 137). -ENV NODE_OPTIONS=--max-old-space-size=2048 -RUN pnpm install --frozen-lockfile +RUN NODE_OPTIONS=--max-old-space-size=2048 pnpm install --frozen-lockfile # Optionally install Chromium and Xvfb for browser automation. # Build with: docker build --build-arg OPENCLAW_INSTALL_BROWSER=1 ... diff --git a/docker-setup.sh b/docker-setup.sh index c0cd925c4c3e..1f6e51cd75d8 100755 --- a/docker-setup.sh +++ b/docker-setup.sh @@ -20,6 +20,78 @@ require_cmd() { fi } +read_config_gateway_token() { + local config_path="$OPENCLAW_CONFIG_DIR/openclaw.json" + if [[ ! -f "$config_path" ]]; then + return 0 + fi + if command -v python3 >/dev/null 2>&1; then + python3 - "$config_path" <<'PY' +import json +import sys + +path = sys.argv[1] +try: + with open(path, "r", encoding="utf-8") as f: + cfg = json.load(f) +except Exception: + raise SystemExit(0) + +gateway = cfg.get("gateway") +if not isinstance(gateway, dict): + raise SystemExit(0) +auth = gateway.get("auth") +if not isinstance(auth, dict): + raise SystemExit(0) +token = auth.get("token") +if isinstance(token, str): + token = token.strip() + if token: + print(token) +PY + return 0 + fi + if command -v node >/dev/null 2>&1; then + node - "$config_path" <<'NODE' +const fs = require("node:fs"); +const configPath = process.argv[2]; +try { + const cfg = JSON.parse(fs.readFileSync(configPath, "utf8")); + const token = cfg?.gateway?.auth?.token; + if (typeof token === "string" && token.trim().length > 0) { + process.stdout.write(token.trim()); + } +} catch { + // Keep docker-setup resilient when config parsing fails. +} +NODE + fi +} + +ensure_control_ui_allowed_origins() { + if [[ "${OPENCLAW_GATEWAY_BIND}" == "loopback" ]]; then + return 0 + fi + + local allowed_origin_json + local current_allowed_origins + allowed_origin_json="$(printf '["http://127.0.0.1:%s"]' "$OPENCLAW_GATEWAY_PORT")" + current_allowed_origins="$( + docker compose "${COMPOSE_ARGS[@]}" run --rm openclaw-cli \ + config get gateway.controlUi.allowedOrigins 2>/dev/null || true + )" + current_allowed_origins="${current_allowed_origins//$'\r'/}" + + if [[ -n "$current_allowed_origins" && "$current_allowed_origins" != "null" && "$current_allowed_origins" != "[]" ]]; then + echo "Control UI allowlist already configured; leaving gateway.controlUi.allowedOrigins unchanged." + return 0 + fi + + docker compose "${COMPOSE_ARGS[@]}" run --rm openclaw-cli \ + config set gateway.controlUi.allowedOrigins "$allowed_origin_json" --strict-json >/dev/null + echo "Set gateway.controlUi.allowedOrigins to $allowed_origin_json for non-loopback bind." +} + contains_disallowed_chars() { local value="$1" [[ "$value" == *$'\n'* || "$value" == *$'\r'* || "$value" == *$'\t'* ]] @@ -97,7 +169,11 @@ export OPENCLAW_EXTRA_MOUNTS="$EXTRA_MOUNTS" export OPENCLAW_HOME_VOLUME="$HOME_VOLUME_NAME" if [[ -z "${OPENCLAW_GATEWAY_TOKEN:-}" ]]; then - if command -v openssl >/dev/null 2>&1; then + EXISTING_CONFIG_TOKEN="$(read_config_gateway_token || true)" + if [[ -n "$EXISTING_CONFIG_TOKEN" ]]; then + OPENCLAW_GATEWAY_TOKEN="$EXISTING_CONFIG_TOKEN" + echo "Reusing gateway token from $OPENCLAW_CONFIG_DIR/openclaw.json" + elif command -v openssl >/dev/null 2>&1; then OPENCLAW_GATEWAY_TOKEN="$(openssl rand -hex 32)" else OPENCLAW_GATEWAY_TOKEN="$(python3 - <<'PY' @@ -273,6 +349,10 @@ echo " - Install Gateway daemon: No" echo "" docker compose "${COMPOSE_ARGS[@]}" run --rm openclaw-cli onboard --no-install-daemon +echo "" +echo "==> Control UI origin allowlist" +ensure_control_ui_allowed_origins + echo "" echo "==> Provider setup (optional)" echo "WhatsApp (QR):" diff --git a/docs/install/gcp.md b/docs/install/gcp.md index a4485611402c..2c6bdd8ac1f6 100644 --- a/docs/install/gcp.md +++ b/docs/install/gcp.md @@ -353,6 +353,14 @@ docker compose up -d openclaw-gateway If build fails with `Killed` / `exit code 137` during `pnpm install --frozen-lockfile`, the VM is out of memory. Use `e2-small` minimum, or `e2-medium` for more reliable first builds. +When binding to LAN (`OPENCLAW_GATEWAY_BIND=lan`), configure a trusted browser origin before continuing: + +```bash +docker compose run --rm openclaw-cli config set gateway.controlUi.allowedOrigins '["http://127.0.0.1:18789"]' --strict-json +``` + +If you changed the gateway port, replace `18789` with your configured port. + Verify binaries: ```bash @@ -397,7 +405,20 @@ Open in your browser: `http://127.0.0.1:18789/` -Paste your gateway token. +Fetch a fresh tokenized dashboard link: + +```bash +docker compose run --rm openclaw-cli dashboard --no-open +``` + +Paste the token from that URL. + +If Control UI shows `unauthorized` or `disconnected (1008): pairing required`, approve the browser device: + +```bash +docker compose run --rm openclaw-cli devices list +docker compose run --rm openclaw-cli devices approve +``` --- diff --git a/src/docker-setup.test.ts b/src/docker-setup.test.ts index 20f754990e33..8737ff5a7934 100644 --- a/src/docker-setup.test.ts +++ b/src/docker-setup.test.ts @@ -168,6 +168,27 @@ describe("docker-setup.sh", () => { expect(identityDirStat.isDirectory()).toBe(true); }); + it("reuses existing config token when OPENCLAW_GATEWAY_TOKEN is unset", async () => { + const activeSandbox = requireSandbox(sandbox); + const configDir = join(activeSandbox.rootDir, "config-token-reuse"); + const workspaceDir = join(activeSandbox.rootDir, "workspace-token-reuse"); + await mkdir(configDir, { recursive: true }); + await writeFile( + join(configDir, "openclaw.json"), + JSON.stringify({ gateway: { auth: { mode: "token", token: "config-token-123" } } }), + ); + + const result = runDockerSetup(activeSandbox, { + OPENCLAW_GATEWAY_TOKEN: undefined, + OPENCLAW_CONFIG_DIR: configDir, + OPENCLAW_WORKSPACE_DIR: workspaceDir, + }); + + expect(result.status).toBe(0); + const envFile = await readFile(join(activeSandbox.rootDir, ".env"), "utf8"); + expect(envFile).toContain("OPENCLAW_GATEWAY_TOKEN=config-token-123"); + }); + it("rejects injected multiline OPENCLAW_EXTRA_MOUNTS values", async () => { const activeSandbox = requireSandbox(sandbox); diff --git a/src/dockerfile.test.ts b/src/dockerfile.test.ts index 4e75caeb4202..5cd55d9b53fc 100644 --- a/src/dockerfile.test.ts +++ b/src/dockerfile.test.ts @@ -9,7 +9,7 @@ const dockerfilePath = join(repoRoot, "Dockerfile"); describe("Dockerfile", () => { it("installs optional browser dependencies after pnpm install", async () => { const dockerfile = await readFile(dockerfilePath, "utf8"); - const installIndex = dockerfile.indexOf("RUN pnpm install --frozen-lockfile"); + const installIndex = dockerfile.indexOf("pnpm install --frozen-lockfile"); const browserArgIndex = dockerfile.indexOf("ARG OPENCLAW_INSTALL_BROWSER"); expect(installIndex).toBeGreaterThan(-1); From cf8d01bc5a3d5fcaf539c81c264ca2a591f49610 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Wed, 25 Feb 2026 23:48:43 -0500 Subject: [PATCH 124/160] pairing: isolate account-scoped allowlist and pending requests --- src/pairing/pairing-store.test.ts | 30 ++++++++++++++++++++++++++++-- src/pairing/pairing-store.ts | 21 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/pairing/pairing-store.test.ts b/src/pairing/pairing-store.test.ts index e44dd391eafd..3d42546f6c16 100644 --- a/src/pairing/pairing-store.test.ts +++ b/src/pairing/pairing-store.test.ts @@ -257,7 +257,7 @@ describe("pairing store", () => { }); }); - it("reads sync allowFrom with scoped + legacy dedupe and wildcard filtering", async () => { + it("reads sync allowFrom with account-scoped isolation and wildcard filtering", async () => { await withTempStateDir(async (stateDir) => { await writeAllowFromFixture({ stateDir, @@ -273,11 +273,37 @@ describe("pairing store", () => { const scoped = readChannelAllowFromStoreSync("telegram", process.env, "yy"); const channelScoped = readChannelAllowFromStoreSync("telegram"); - expect(scoped).toEqual(["1002", "1001"]); + expect(scoped).toEqual(["1002", "1001", "1002"]); expect(channelScoped).toEqual(["1001", "1001"]); }); }); + it("does not reuse pairing requests across accounts for the same sender id", async () => { + await withTempStateDir(async () => { + const first = await upsertChannelPairingRequest({ + channel: "telegram", + accountId: "alpha", + id: "12345", + }); + const second = await upsertChannelPairingRequest({ + channel: "telegram", + accountId: "beta", + id: "12345", + }); + + expect(first.created).toBe(true); + expect(second.created).toBe(true); + expect(second.code).not.toBe(first.code); + + const alpha = await listChannelPairingRequests("telegram", process.env, "alpha"); + const beta = await listChannelPairingRequests("telegram", process.env, "beta"); + expect(alpha).toHaveLength(1); + expect(beta).toHaveLength(1); + expect(alpha[0]?.code).toBe(first.code); + expect(beta[0]?.code).toBe(second.code); + }); + }); + it("reads legacy channel-scoped allowFrom for default account", async () => { await withTempStateDir(async (stateDir) => { await writeAllowFromFixture({ stateDir, channel: "telegram", allowFrom: ["1001"] }); diff --git a/src/pairing/pairing-store.ts b/src/pairing/pairing-store.ts index eb0b52b308bf..0f46d53b4794 100644 --- a/src/pairing/pairing-store.ts +++ b/src/pairing/pairing-store.ts @@ -218,6 +218,12 @@ function requestMatchesAccountId(entry: PairingRequest, normalizedAccountId: str ); } +function shouldIncludeLegacyAllowFromEntries(normalizedAccountId: string): boolean { + // Keep backward compatibility for legacy channel-scoped allowFrom only on default account. + // Non-default accounts should remain isolated to avoid cross-account implicit approvals. + return !normalizedAccountId || normalizedAccountId === "default"; +} + function normalizeId(value: string | number): string { return String(value).trim(); } @@ -344,8 +350,11 @@ export async function readChannelAllowFromStore( const scopedPath = resolveAllowFromPath(channel, env, accountId); const scopedEntries = await readAllowFromStateForPath(channel, scopedPath); + if (!shouldIncludeLegacyAllowFromEntries(normalizedAccountId)) { + return scopedEntries; + } // Backward compatibility: legacy channel-level allowFrom store was unscoped. - // Keep honoring it alongside account-scoped files to prevent re-pair prompts after upgrades. + // Keep honoring it for default account to prevent re-pair prompts after upgrades. const legacyPath = resolveAllowFromPath(channel, env); const legacyEntries = await readAllowFromStateForPath(channel, legacyPath); return dedupePreserveOrder([...scopedEntries, ...legacyEntries]); @@ -364,6 +373,9 @@ export function readChannelAllowFromStoreSync( const scopedPath = resolveAllowFromPath(channel, env, accountId); const scopedEntries = readAllowFromStateForPathSync(channel, scopedPath); + if (!shouldIncludeLegacyAllowFromEntries(normalizedAccountId)) { + return scopedEntries; + } const legacyPath = resolveAllowFromPath(channel, env); const legacyEntries = readAllowFromStateForPathSync(channel, legacyPath); return dedupePreserveOrder([...scopedEntries, ...legacyEntries]); @@ -503,7 +515,12 @@ export async function upsertChannelPairingRequest(params: { nowMs, ); reqs = prunedExpired; - const existingIdx = reqs.findIndex((r) => r.id === id); + const existingIdx = reqs.findIndex((r) => { + if (r.id !== id) { + return false; + } + return requestMatchesAccountId(r, normalizePairingAccountId(normalizedAccountId)); + }); const existingCodes = new Set( reqs.map((req) => String(req.code ?? "") From d9b19e5970bdd34a0182e7e0e8f82094d1984fdf Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 00:00:00 -0500 Subject: [PATCH 125/160] plugin-sdk: export shared timezone formatting helpers (#27196) --- src/plugin-sdk/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/plugin-sdk/index.ts b/src/plugin-sdk/index.ts index 6e25d50740b2..828ec0899030 100644 --- a/src/plugin-sdk/index.ts +++ b/src/plugin-sdk/index.ts @@ -244,6 +244,11 @@ export type { PersistentDedupeOptions, } from "./persistent-dedupe.js"; export { formatErrorMessage } from "../infra/errors.js"; +export { + formatUtcTimestamp, + formatZonedTimestamp, + resolveTimezone, +} from "../infra/format-time/format-datetime.js"; export { DEFAULT_WEBHOOK_BODY_TIMEOUT_MS, DEFAULT_WEBHOOK_MAX_BODY_BYTES, From 91a3f0a3fe8af55218273b029f5183184d4819f5 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 00:31:24 -0500 Subject: [PATCH 126/160] pairing: enforce strict account-scoped state --- CHANGELOG.md | 1 + docs/channels/pairing.md | 9 +++- docs/gateway/security/index.md | 6 ++- docs/start/setup.md | 4 +- src/pairing/pairing-store.test.ts | 70 +++++++++++++++++++++++++- src/pairing/pairing-store.ts | 81 ++++++++++++++++++++++++++----- 6 files changed, 152 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0162138f63a0..5b9a53cecfbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Docs: https://docs.openclaw.ai - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. +- Pairing/Multi-account isolation: keep non-default account pairing allowlists and pending requests strictly account-scoped, while default account continues to use channel-scoped pairing allowlist storage. - Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. diff --git a/docs/channels/pairing.md b/docs/channels/pairing.md index 4b575eb87c76..d402de166623 100644 --- a/docs/channels/pairing.md +++ b/docs/channels/pairing.md @@ -43,7 +43,14 @@ Supported channels: `telegram`, `whatsapp`, `signal`, `imessage`, `discord`, `sl Stored under `~/.openclaw/credentials/`: - Pending requests: `-pairing.json` -- Approved allowlist store: `-allowFrom.json` +- Approved allowlist store: + - Default account: `-allowFrom.json` + - Non-default account: `--allowFrom.json` + +Account scoping behavior: + +- Non-default accounts read/write only their scoped allowlist file. +- Default account uses the channel-scoped unscoped allowlist file. Treat these as sensitive (they gate access to your assistant). diff --git a/docs/gateway/security/index.md b/docs/gateway/security/index.md index a61a81eab1e4..32a2a55329d8 100644 --- a/docs/gateway/security/index.md +++ b/docs/gateway/security/index.md @@ -202,7 +202,9 @@ Use this when auditing access or deciding what to back up: - **Telegram bot token**: config/env or `channels.telegram.tokenFile` - **Discord bot token**: config/env (token file not yet supported) - **Slack tokens**: config/env (`channels.slack.*`) -- **Pairing allowlists**: `~/.openclaw/credentials/-allowFrom.json` +- **Pairing allowlists**: + - `~/.openclaw/credentials/-allowFrom.json` (default account) + - `~/.openclaw/credentials/--allowFrom.json` (non-default accounts) - **Model auth profiles**: `~/.openclaw/agents//agent/auth-profiles.json` - **Legacy OAuth import**: `~/.openclaw/credentials/oauth.json` @@ -488,7 +490,7 @@ If you run multiple accounts on the same channel, use `per-account-channel-peer` OpenClaw has two separate “who can trigger me?” layers: - **DM allowlist** (`allowFrom` / `channels.discord.allowFrom` / `channels.slack.allowFrom`; legacy: `channels.discord.dm.allowFrom`, `channels.slack.dm.allowFrom`): who is allowed to talk to the bot in direct messages. - - When `dmPolicy="pairing"`, approvals are written to `~/.openclaw/credentials/-allowFrom.json` (merged with config allowlists). + - When `dmPolicy="pairing"`, approvals are written to the account-scoped pairing allowlist store under `~/.openclaw/credentials/` (`-allowFrom.json` for default account, `--allowFrom.json` for non-default accounts), merged with config allowlists. - **Group allowlist** (channel-specific): which groups/channels/guilds the bot will accept messages from at all. - Common patterns: - `channels.whatsapp.groups`, `channels.telegram.groups`, `channels.imessage.groups`: per-group defaults like `requireMention`; when set, it also acts as a group allowlist (include `"*"` to keep allow-all behavior). diff --git a/docs/start/setup.md b/docs/start/setup.md index ee50e02afd47..7eef5bce7143 100644 --- a/docs/start/setup.md +++ b/docs/start/setup.md @@ -130,7 +130,9 @@ Use this when debugging auth or deciding what to back up: - **Telegram bot token**: config/env or `channels.telegram.tokenFile` - **Discord bot token**: config/env (token file not yet supported) - **Slack tokens**: config/env (`channels.slack.*`) -- **Pairing allowlists**: `~/.openclaw/credentials/-allowFrom.json` +- **Pairing allowlists**: + - `~/.openclaw/credentials/-allowFrom.json` (default account) + - `~/.openclaw/credentials/--allowFrom.json` (non-default accounts) - **Model auth profiles**: `~/.openclaw/agents//agent/auth-profiles.json` - **Legacy OAuth import**: `~/.openclaw/credentials/oauth.json` More detail: [Security](/gateway/security#credential-storage-map). diff --git a/src/pairing/pairing-store.test.ts b/src/pairing/pairing-store.test.ts index 3d42546f6c16..130a8dc3807a 100644 --- a/src/pairing/pairing-store.test.ts +++ b/src/pairing/pairing-store.test.ts @@ -35,6 +35,7 @@ async function withTempStateDir(fn: (stateDir: string) => Promise) { } async function writeJsonFixture(filePath: string, value: unknown) { + await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); } @@ -42,6 +43,11 @@ function resolvePairingFilePath(stateDir: string, channel: string) { return path.join(resolveOAuthDir(process.env, stateDir), `${channel}-pairing.json`); } +function resolveAllowFromFilePath(stateDir: string, channel: string, accountId?: string) { + const suffix = accountId ? `-${accountId}` : ""; + return path.join(resolveOAuthDir(process.env, stateDir), `${channel}${suffix}-allowFrom.json`); +} + async function writeAllowFromFixture(params: { stateDir: string; channel: string; @@ -273,8 +279,68 @@ describe("pairing store", () => { const scoped = readChannelAllowFromStoreSync("telegram", process.env, "yy"); const channelScoped = readChannelAllowFromStoreSync("telegram"); - expect(scoped).toEqual(["1002", "1001", "1002"]); - expect(channelScoped).toEqual(["1001", "1001"]); + expect(scoped).toEqual(["1002", "1001"]); + expect(channelScoped).toEqual(["1001"]); + }); + }); + + it("does not read legacy channel-scoped allowFrom for non-default account ids", async () => { + await withTempStateDir(async (stateDir) => { + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + allowFrom: ["1001", "*", "1002", "1001"], + }); + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + accountId: "yy", + allowFrom: ["1003"], + }); + + const asyncScoped = await readChannelAllowFromStore("telegram", process.env, "yy"); + const syncScoped = readChannelAllowFromStoreSync("telegram", process.env, "yy"); + expect(asyncScoped).toEqual(["1003"]); + expect(syncScoped).toEqual(["1003"]); + }); + }); + + it("does not fall back to legacy allowFrom when scoped file exists but is empty", async () => { + await withTempStateDir(async (stateDir) => { + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + allowFrom: ["1001"], + }); + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + accountId: "yy", + allowFrom: [], + }); + + const asyncScoped = await readChannelAllowFromStore("telegram", process.env, "yy"); + const syncScoped = readChannelAllowFromStoreSync("telegram", process.env, "yy"); + expect(asyncScoped).toEqual([]); + expect(syncScoped).toEqual([]); + }); + }); + + it("keeps async and sync reads aligned for malformed scoped allowFrom files", async () => { + await withTempStateDir(async (stateDir) => { + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + allowFrom: ["1001"], + }); + const malformedScopedPath = resolveAllowFromFilePath(stateDir, "telegram", "yy"); + await fs.mkdir(path.dirname(malformedScopedPath), { recursive: true }); + await fs.writeFile(malformedScopedPath, "{ this is not json\n", "utf8"); + + const asyncScoped = await readChannelAllowFromStore("telegram", process.env, "yy"); + const syncScoped = readChannelAllowFromStoreSync("telegram", process.env, "yy"); + expect(asyncScoped).toEqual([]); + expect(syncScoped).toEqual([]); }); }); diff --git a/src/pairing/pairing-store.ts b/src/pairing/pairing-store.ts index 0f46d53b4794..d6a8b9e6c8ed 100644 --- a/src/pairing/pairing-store.ts +++ b/src/pairing/pairing-store.ts @@ -243,7 +243,9 @@ function normalizeAllowEntry(channel: PairingChannel, entry: string): string { function normalizeAllowFromList(channel: PairingChannel, store: AllowFromStore): string[] { const list = Array.isArray(store.allowFrom) ? store.allowFrom : []; - return list.map((v) => normalizeAllowEntry(channel, String(v))).filter(Boolean); + return dedupePreserveOrder( + list.map((v) => normalizeAllowEntry(channel, String(v))).filter(Boolean), + ); } function normalizeAllowFromInput(channel: PairingChannel, entry: string | number): string { @@ -268,20 +270,46 @@ async function readAllowFromStateForPath( channel: PairingChannel, filePath: string, ): Promise { - const { value } = await readJsonFile(filePath, { + return (await readAllowFromStateForPathWithExists(channel, filePath)).entries; +} + +async function readAllowFromStateForPathWithExists( + channel: PairingChannel, + filePath: string, +): Promise<{ entries: string[]; exists: boolean }> { + const { value, exists } = await readJsonFile(filePath, { version: 1, allowFrom: [], }); - return normalizeAllowFromList(channel, value); + const entries = normalizeAllowFromList(channel, value); + return { entries, exists }; } function readAllowFromStateForPathSync(channel: PairingChannel, filePath: string): string[] { + return readAllowFromStateForPathSyncWithExists(channel, filePath).entries; +} + +function readAllowFromStateForPathSyncWithExists( + channel: PairingChannel, + filePath: string, +): { entries: string[]; exists: boolean } { + let raw = ""; + try { + raw = fs.readFileSync(filePath, "utf8"); + } catch (err) { + const code = (err as { code?: string }).code; + if (code === "ENOENT") { + return { entries: [], exists: false }; + } + return { entries: [], exists: false }; + } try { - const raw = fs.readFileSync(filePath, "utf8"); const parsed = JSON.parse(raw) as AllowFromStore; - return normalizeAllowFromList(channel, parsed); + const entries = normalizeAllowFromList(channel, parsed); + return { entries, exists: true }; } catch { - return []; + // Keep parity with async reads: malformed JSON still means the file exists. + return { entries: [], exists: true }; } } @@ -306,6 +334,24 @@ async function writeAllowFromState(filePath: string, allowFrom: string[]): Promi } satisfies AllowFromStore); } +async function readNonDefaultAccountAllowFrom(params: { + channel: PairingChannel; + env: NodeJS.ProcessEnv; + accountId: string; +}): Promise { + const scopedPath = resolveAllowFromPath(params.channel, params.env, params.accountId); + return await readAllowFromStateForPath(params.channel, scopedPath); +} + +function readNonDefaultAccountAllowFromSync(params: { + channel: PairingChannel; + env: NodeJS.ProcessEnv; + accountId: string; +}): string[] { + const scopedPath = resolveAllowFromPath(params.channel, params.env, params.accountId); + return readAllowFromStateForPathSync(params.channel, scopedPath); +} + async function updateAllowFromStoreEntry(params: { channel: PairingChannel; entry: string | number; @@ -348,11 +394,15 @@ export async function readChannelAllowFromStore( return await readAllowFromStateForPath(channel, filePath); } - const scopedPath = resolveAllowFromPath(channel, env, accountId); - const scopedEntries = await readAllowFromStateForPath(channel, scopedPath); if (!shouldIncludeLegacyAllowFromEntries(normalizedAccountId)) { - return scopedEntries; + return await readNonDefaultAccountAllowFrom({ + channel, + env, + accountId: normalizedAccountId, + }); } + const scopedPath = resolveAllowFromPath(channel, env, accountId); + const scopedEntries = await readAllowFromStateForPath(channel, scopedPath); // Backward compatibility: legacy channel-level allowFrom store was unscoped. // Keep honoring it for default account to prevent re-pair prompts after upgrades. const legacyPath = resolveAllowFromPath(channel, env); @@ -371,11 +421,15 @@ export function readChannelAllowFromStoreSync( return readAllowFromStateForPathSync(channel, filePath); } - const scopedPath = resolveAllowFromPath(channel, env, accountId); - const scopedEntries = readAllowFromStateForPathSync(channel, scopedPath); if (!shouldIncludeLegacyAllowFromEntries(normalizedAccountId)) { - return scopedEntries; + return readNonDefaultAccountAllowFromSync({ + channel, + env, + accountId: normalizedAccountId, + }); } + const scopedPath = resolveAllowFromPath(channel, env, accountId); + const scopedEntries = readAllowFromStateForPathSync(channel, scopedPath); const legacyPath = resolveAllowFromPath(channel, env); const legacyEntries = readAllowFromStateForPathSync(channel, legacyPath); return dedupePreserveOrder([...scopedEntries, ...legacyEntries]); @@ -515,11 +569,12 @@ export async function upsertChannelPairingRequest(params: { nowMs, ); reqs = prunedExpired; + const normalizedMatchingAccountId = normalizePairingAccountId(normalizedAccountId); const existingIdx = reqs.findIndex((r) => { if (r.id !== id) { return false; } - return requestMatchesAccountId(r, normalizePairingAccountId(normalizedAccountId)); + return requestMatchesAccountId(r, normalizedMatchingAccountId); }); const existingCodes = new Set( reqs.map((req) => From 39a1c13635b4a770711e0fa9661448a39079c15e Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 00:39:18 -0500 Subject: [PATCH 127/160] chore(ci): fix cross-platform symlink path assertions in agents file tests --- src/gateway/server-methods/agents-mutate.test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/gateway/server-methods/agents-mutate.test.ts b/src/gateway/server-methods/agents-mutate.test.ts index a4fddea633af..26503db553c6 100644 --- a/src/gateway/server-methods/agents-mutate.test.ts +++ b/src/gateway/server-methods/agents-mutate.test.ts @@ -1,3 +1,4 @@ +import path from "node:path"; import { describe, expect, it, vi, beforeEach } from "vitest"; /* ------------------------------------------------------------------ */ @@ -515,7 +516,7 @@ describe("agents.files.get/set symlink safety", () => { it("rejects agents.files.get when allowlisted file symlink escapes workspace", async () => { const workspace = "/workspace/test-agent"; - const candidate = `${workspace}/AGENTS.md`; + const candidate = path.resolve(workspace, "AGENTS.md"); mocks.fsRealpath.mockImplementation(async (p: string) => { if (p === workspace) { return workspace; @@ -548,7 +549,7 @@ describe("agents.files.get/set symlink safety", () => { it("rejects agents.files.set when allowlisted file symlink escapes workspace", async () => { const workspace = "/workspace/test-agent"; - const candidate = `${workspace}/AGENTS.md`; + const candidate = path.resolve(workspace, "AGENTS.md"); mocks.fsRealpath.mockImplementation(async (p: string) => { if (p === workspace) { return workspace; @@ -583,8 +584,8 @@ describe("agents.files.get/set symlink safety", () => { it("allows in-workspace symlink targets for get/set", async () => { const workspace = "/workspace/test-agent"; - const candidate = `${workspace}/AGENTS.md`; - const target = `${workspace}/policies/AGENTS.md`; + const candidate = path.resolve(workspace, "AGENTS.md"); + const target = path.resolve(workspace, "policies", "AGENTS.md"); const targetStat = makeFileStat({ size: 7, mtimeMs: 1700, dev: 9, ino: 42 }); mocks.fsRealpath.mockImplementation(async (p: string) => { From f08fe02a1b19791fe5f18f3fce1792ccbc9bfb1d Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 01:14:57 -0500 Subject: [PATCH 128/160] Onboarding: support plugin-owned interactive channel flows (#27191) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 53872cf8e75562db012b66f888928524daff08d2 Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + docs/tools/plugin.md | 23 ++ src/channels/plugins/onboarding-types.ts | 13 + src/commands/channel-test-helpers.ts | 24 ++ src/commands/onboard-channels.test.ts | 308 ++++++++++++++++++++++- src/commands/onboard-channels.ts | 64 ++++- 6 files changed, 426 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b9a53cecfbb..5f2ce6018e9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Docs: https://docs.openclaw.ai - UI/Chat compose: add mobile stacked layout for compose action buttons on small screens to improve send/session controls usability. (#11167) Thanks @junyiz. - Heartbeat/Config: replace heartbeat DM toggle with `agents.defaults.heartbeat.directPolicy` (`allow` | `block`; also supported per-agent via `agents.list[].heartbeat.directPolicy`) for clearer delivery semantics. - Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. +- Onboarding/Plugins: let channel plugins own interactive onboarding flows with optional `configureInteractive` and `configureWhenConfigured` hooks while preserving the generic fallback path. (#27191) thanks @gumadeiras. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. - Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. diff --git a/docs/tools/plugin.md b/docs/tools/plugin.md index 9250501f2d9e..3dc575088eb9 100644 --- a/docs/tools/plugin.md +++ b/docs/tools/plugin.md @@ -452,6 +452,29 @@ Notes: - `meta.preferOver` lists channel ids to skip auto-enable when both are configured. - `meta.detailLabel` and `meta.systemImage` let UIs show richer channel labels/icons. +### Channel onboarding hooks + +Channel plugins can define optional onboarding hooks on `plugin.onboarding`: + +- `configure(ctx)` is the baseline setup flow. +- `configureInteractive(ctx)` can fully own interactive setup for both configured and unconfigured states. +- `configureWhenConfigured(ctx)` can override behavior only for already configured channels. + +Hook precedence in the wizard: + +1. `configureInteractive` (if present) +2. `configureWhenConfigured` (only when channel status is already configured) +3. fallback to `configure` + +Context details: + +- `configureInteractive` and `configureWhenConfigured` receive: + - `configured` (`true` or `false`) + - `label` (user-facing channel name used by prompts) + - plus the shared config/runtime/prompter/options fields +- Returning `"skip"` leaves selection and account tracking unchanged. +- Returning `{ cfg, accountId? }` applies config updates and records account selection. + ### Write a new messaging channel (step‑by‑step) Use this when you want a **new chat surface** (a "messaging channel"), not a model provider. diff --git a/src/channels/plugins/onboarding-types.ts b/src/channels/plugins/onboarding-types.ts index 897487a49c6f..342f29bf5b50 100644 --- a/src/channels/plugins/onboarding-types.ts +++ b/src/channels/plugins/onboarding-types.ts @@ -62,6 +62,13 @@ export type ChannelOnboardingResult = { accountId?: string; }; +export type ChannelOnboardingConfiguredResult = ChannelOnboardingResult | "skip"; + +export type ChannelOnboardingInteractiveContext = ChannelOnboardingConfigureContext & { + configured: boolean; + label: string; +}; + export type ChannelOnboardingDmPolicy = { label: string; channel: ChannelId; @@ -80,6 +87,12 @@ export type ChannelOnboardingAdapter = { channel: ChannelId; getStatus: (ctx: ChannelOnboardingStatusContext) => Promise; configure: (ctx: ChannelOnboardingConfigureContext) => Promise; + configureInteractive?: ( + ctx: ChannelOnboardingInteractiveContext, + ) => Promise; + configureWhenConfigured?: ( + ctx: ChannelOnboardingInteractiveContext, + ) => Promise; dmPolicy?: ChannelOnboardingDmPolicy; onAccountRecorded?: (accountId: string, options?: SetupChannelsOptions) => void; disable?: (cfg: OpenClawConfig) => OpenClawConfig; diff --git a/src/commands/channel-test-helpers.ts b/src/commands/channel-test-helpers.ts index fd7e6f36278f..65745a55d5e3 100644 --- a/src/commands/channel-test-helpers.ts +++ b/src/commands/channel-test-helpers.ts @@ -6,6 +6,9 @@ import { telegramPlugin } from "../../extensions/telegram/src/channel.js"; import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js"; import { setActivePluginRegistry } from "../plugins/runtime.js"; import { createTestRegistry } from "../test-utils/channel-plugins.js"; +import type { ChannelChoice } from "./onboard-types.js"; +import { getChannelOnboardingAdapter } from "./onboarding/registry.js"; +import type { ChannelOnboardingAdapter } from "./onboarding/types.js"; export function setDefaultChannelPluginRegistryForTests(): void { const channels = [ @@ -18,3 +21,24 @@ export function setDefaultChannelPluginRegistryForTests(): void { ] as unknown as Parameters[0]; setActivePluginRegistry(createTestRegistry(channels)); } + +export function patchChannelOnboardingAdapter( + channel: ChannelChoice, + patch: Pick, +): () => void { + const adapter = getChannelOnboardingAdapter(channel); + if (!adapter) { + throw new Error(`missing onboarding adapter for ${channel}`); + } + const keys = Object.keys(patch) as K[]; + const previous = {} as Pick; + for (const key of keys) { + previous[key] = adapter[key]; + adapter[key] = patch[key]; + } + return () => { + for (const key of keys) { + adapter[key] = previous[key]; + } + }; +} diff --git a/src/commands/onboard-channels.test.ts b/src/commands/onboard-channels.test.ts index d6c0669e4fd7..cd146b82c091 100644 --- a/src/commands/onboard-channels.test.ts +++ b/src/commands/onboard-channels.test.ts @@ -3,7 +3,10 @@ import type { OpenClawConfig } from "../config/config.js"; import { createEmptyPluginRegistry } from "../plugins/registry.js"; import { setActivePluginRegistry } from "../plugins/runtime.js"; import type { WizardPrompter } from "../wizard/prompts.js"; -import { setDefaultChannelPluginRegistryForTests } from "./channel-test-helpers.js"; +import { + patchChannelOnboardingAdapter, + setDefaultChannelPluginRegistryForTests, +} from "./channel-test-helpers.js"; import { setupChannels } from "./onboard-channels.js"; import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js"; @@ -249,4 +252,307 @@ describe("setupChannels", () => { expect(select).toHaveBeenCalledWith(expect.objectContaining({ message: "Select a channel" })); expect(multiselect).not.toHaveBeenCalled(); }); + + it("uses configureInteractive skip without mutating selection/account state", async () => { + const select = vi.fn(async ({ message }: { message: string }) => { + if (message === "Select channel (QuickStart)") { + return "telegram"; + } + return "__done__"; + }); + const selection = vi.fn(); + const onAccountId = vi.fn(); + const configureInteractive = vi.fn(async () => "skip" as const); + const restore = patchChannelOnboardingAdapter("telegram", { + getStatus: vi.fn(async ({ cfg }) => ({ + channel: "telegram", + configured: Boolean(cfg.channels?.telegram?.botToken), + statusLines: [], + })), + configureInteractive, + }); + const { multiselect, text } = createUnexpectedPromptGuards(); + + const prompter = createPrompter({ + select: select as unknown as WizardPrompter["select"], + multiselect, + text, + }); + + const runtime = createExitThrowingRuntime(); + try { + const cfg = await setupChannels({} as OpenClawConfig, runtime, prompter, { + skipConfirm: true, + quickstartDefaults: true, + onSelection: selection, + onAccountId, + }); + + expect(configureInteractive).toHaveBeenCalledWith( + expect.objectContaining({ configured: false, label: expect.any(String) }), + ); + expect(selection).toHaveBeenCalledWith([]); + expect(onAccountId).not.toHaveBeenCalled(); + expect(cfg.channels?.telegram?.botToken).toBeUndefined(); + } finally { + restore(); + } + }); + + it("applies configureInteractive result cfg/account updates", async () => { + const select = vi.fn(async ({ message }: { message: string }) => { + if (message === "Select channel (QuickStart)") { + return "telegram"; + } + return "__done__"; + }); + const selection = vi.fn(); + const onAccountId = vi.fn(); + const configureInteractive = vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({ + cfg: { + ...cfg, + channels: { + ...cfg.channels, + telegram: { ...cfg.channels?.telegram, botToken: "new-token" }, + }, + } as OpenClawConfig, + accountId: "acct-1", + })); + const configure = vi.fn(async () => { + throw new Error("configure should not be called when configureInteractive is present"); + }); + const restore = patchChannelOnboardingAdapter("telegram", { + getStatus: vi.fn(async ({ cfg }) => ({ + channel: "telegram", + configured: Boolean(cfg.channels?.telegram?.botToken), + statusLines: [], + })), + configureInteractive, + configure, + }); + const { multiselect, text } = createUnexpectedPromptGuards(); + + const prompter = createPrompter({ + select: select as unknown as WizardPrompter["select"], + multiselect, + text, + }); + + const runtime = createExitThrowingRuntime(); + try { + const cfg = await setupChannels({} as OpenClawConfig, runtime, prompter, { + skipConfirm: true, + quickstartDefaults: true, + onSelection: selection, + onAccountId, + }); + + expect(configureInteractive).toHaveBeenCalledTimes(1); + expect(configure).not.toHaveBeenCalled(); + expect(selection).toHaveBeenCalledWith(["telegram"]); + expect(onAccountId).toHaveBeenCalledWith("telegram", "acct-1"); + expect(cfg.channels?.telegram?.botToken).toBe("new-token"); + } finally { + restore(); + } + }); + + it("uses configureWhenConfigured when channel is already configured", async () => { + const select = vi.fn(async ({ message }: { message: string }) => { + if (message === "Select channel (QuickStart)") { + return "telegram"; + } + return "__done__"; + }); + const selection = vi.fn(); + const onAccountId = vi.fn(); + const configureWhenConfigured = vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({ + cfg: { + ...cfg, + channels: { + ...cfg.channels, + telegram: { ...cfg.channels?.telegram, botToken: "updated-token" }, + }, + } as OpenClawConfig, + accountId: "acct-2", + })); + const configure = vi.fn(async () => { + throw new Error( + "configure should not be called when configureWhenConfigured handles updates", + ); + }); + const restore = patchChannelOnboardingAdapter("telegram", { + getStatus: vi.fn(async ({ cfg }) => ({ + channel: "telegram", + configured: Boolean(cfg.channels?.telegram?.botToken), + statusLines: [], + })), + configureInteractive: undefined, + configureWhenConfigured, + configure, + }); + const { multiselect, text } = createUnexpectedPromptGuards(); + + const prompter = createPrompter({ + select: select as unknown as WizardPrompter["select"], + multiselect, + text, + }); + + const runtime = createExitThrowingRuntime(); + try { + const cfg = await setupChannels( + { + channels: { + telegram: { + botToken: "old-token", + }, + }, + } as OpenClawConfig, + runtime, + prompter, + { + skipConfirm: true, + quickstartDefaults: true, + onSelection: selection, + onAccountId, + }, + ); + + expect(configureWhenConfigured).toHaveBeenCalledTimes(1); + expect(configureWhenConfigured).toHaveBeenCalledWith( + expect.objectContaining({ configured: true, label: expect.any(String) }), + ); + expect(configure).not.toHaveBeenCalled(); + expect(selection).toHaveBeenCalledWith(["telegram"]); + expect(onAccountId).toHaveBeenCalledWith("telegram", "acct-2"); + expect(cfg.channels?.telegram?.botToken).toBe("updated-token"); + } finally { + restore(); + } + }); + + it("respects configureWhenConfigured skip without mutating selection or account state", async () => { + const select = vi.fn(async ({ message }: { message: string }) => { + if (message === "Select channel (QuickStart)") { + return "telegram"; + } + throw new Error(`unexpected select prompt: ${message}`); + }); + const selection = vi.fn(); + const onAccountId = vi.fn(); + const configureWhenConfigured = vi.fn(async () => "skip" as const); + const configure = vi.fn(async () => { + throw new Error("configure should not run when configureWhenConfigured handles skip"); + }); + const restore = patchChannelOnboardingAdapter("telegram", { + getStatus: vi.fn(async ({ cfg }) => ({ + channel: "telegram", + configured: Boolean(cfg.channels?.telegram?.botToken), + statusLines: [], + })), + configureInteractive: undefined, + configureWhenConfigured, + configure, + }); + const { multiselect, text } = createUnexpectedPromptGuards(); + + const prompter = createPrompter({ + select: select as unknown as WizardPrompter["select"], + multiselect, + text, + }); + + const runtime = createExitThrowingRuntime(); + try { + const cfg = await setupChannels( + { + channels: { + telegram: { + botToken: "old-token", + }, + }, + } as OpenClawConfig, + runtime, + prompter, + { + skipConfirm: true, + quickstartDefaults: true, + onSelection: selection, + onAccountId, + }, + ); + + expect(configureWhenConfigured).toHaveBeenCalledWith( + expect.objectContaining({ configured: true, label: expect.any(String) }), + ); + expect(configure).not.toHaveBeenCalled(); + expect(selection).toHaveBeenCalledWith([]); + expect(onAccountId).not.toHaveBeenCalled(); + expect(cfg.channels?.telegram?.botToken).toBe("old-token"); + } finally { + restore(); + } + }); + + it("prefers configureInteractive over configureWhenConfigured when both hooks exist", async () => { + const select = vi.fn(async ({ message }: { message: string }) => { + if (message === "Select channel (QuickStart)") { + return "telegram"; + } + throw new Error(`unexpected select prompt: ${message}`); + }); + const selection = vi.fn(); + const onAccountId = vi.fn(); + const configureInteractive = vi.fn(async () => "skip" as const); + const configureWhenConfigured = vi.fn(async () => { + throw new Error("configureWhenConfigured should not run when configureInteractive exists"); + }); + const restore = patchChannelOnboardingAdapter("telegram", { + getStatus: vi.fn(async ({ cfg }) => ({ + channel: "telegram", + configured: Boolean(cfg.channels?.telegram?.botToken), + statusLines: [], + })), + configureInteractive, + configureWhenConfigured, + }); + const { multiselect, text } = createUnexpectedPromptGuards(); + + const prompter = createPrompter({ + select: select as unknown as WizardPrompter["select"], + multiselect, + text, + }); + + const runtime = createExitThrowingRuntime(); + try { + await setupChannels( + { + channels: { + telegram: { + botToken: "old-token", + }, + }, + } as OpenClawConfig, + runtime, + prompter, + { + skipConfirm: true, + quickstartDefaults: true, + onSelection: selection, + onAccountId, + }, + ); + + expect(configureInteractive).toHaveBeenCalledWith( + expect.objectContaining({ configured: true, label: expect.any(String) }), + ); + expect(configureWhenConfigured).not.toHaveBeenCalled(); + expect(selection).toHaveBeenCalledWith([]); + expect(onAccountId).not.toHaveBeenCalled(); + } finally { + restore(); + } + }); }); diff --git a/src/commands/onboard-channels.ts b/src/commands/onboard-channels.ts index 32510c29f39d..6e79379e1f13 100644 --- a/src/commands/onboard-channels.ts +++ b/src/commands/onboard-channels.ts @@ -27,7 +27,9 @@ import { listChannelOnboardingAdapters, } from "./onboarding/registry.js"; import type { + ChannelOnboardingConfiguredResult, ChannelOnboardingDmPolicy, + ChannelOnboardingResult, ChannelOnboardingStatus, SetupChannelsOptions, } from "./onboarding/types.js"; @@ -488,6 +490,26 @@ export async function setupChannels( return true; }; + const applyOnboardingResult = async (channel: ChannelChoice, result: ChannelOnboardingResult) => { + next = result.cfg; + if (result.accountId) { + recordAccount(channel, result.accountId); + } + addSelection(channel); + await refreshStatus(channel); + }; + + const applyCustomOnboardingResult = async ( + channel: ChannelChoice, + result: ChannelOnboardingConfiguredResult, + ) => { + if (result === "skip") { + return false; + } + await applyOnboardingResult(channel, result); + return true; + }; + const configureChannel = async (channel: ChannelChoice) => { const adapter = getChannelOnboardingAdapter(channel); if (!adapter) { @@ -503,17 +525,29 @@ export async function setupChannels( shouldPromptAccountIds, forceAllowFrom: forceAllowFromChannels.has(channel), }); - next = result.cfg; - if (result.accountId) { - recordAccount(channel, result.accountId); - } - addSelection(channel); - await refreshStatus(channel); + await applyOnboardingResult(channel, result); }; const handleConfiguredChannel = async (channel: ChannelChoice, label: string) => { const plugin = getChannelPlugin(channel); const adapter = getChannelOnboardingAdapter(channel); + if (adapter?.configureWhenConfigured) { + const custom = await adapter.configureWhenConfigured({ + cfg: next, + runtime, + prompter, + options, + accountOverrides, + shouldPromptAccountIds, + forceAllowFrom: forceAllowFromChannels.has(channel), + configured: true, + label, + }); + if (!(await applyCustomOnboardingResult(channel, custom))) { + return; + } + return; + } const supportsDisable = Boolean( options?.allowDisable && (plugin?.config.setAccountEnabled || adapter?.disable), ); @@ -615,9 +649,27 @@ export async function setupChannels( } const plugin = getChannelPlugin(channel); + const adapter = getChannelOnboardingAdapter(channel); const label = plugin?.meta.label ?? catalogEntry?.meta.label ?? channel; const status = statusByChannel.get(channel); const configured = status?.configured ?? false; + if (adapter?.configureInteractive) { + const custom = await adapter.configureInteractive({ + cfg: next, + runtime, + prompter, + options, + accountOverrides, + shouldPromptAccountIds, + forceAllowFrom: forceAllowFromChannels.has(channel), + configured, + label, + }); + if (!(await applyCustomOnboardingResult(channel, custom))) { + return; + } + return; + } if (configured) { await handleConfiguredChannel(channel, label); return; From 72adf2458bb538b5568bbabe68ff141419cf92a3 Mon Sep 17 00:00:00 2001 From: Josh Avant <830519+joshavant@users.noreply.github.com> Date: Thu, 26 Feb 2026 00:33:36 -0600 Subject: [PATCH 129/160] CI: shard Windows test lane for faster CI critical path (#27234) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: f7c41089e0d5c36f59addd643d2038502bafe933 Co-authored-by: joshavant <830519+joshavant@users.noreply.github.com> Co-authored-by: joshavant <830519+joshavant@users.noreply.github.com> Reviewed-by: @joshavant --- .github/workflows/ci.yml | 19 ++++++++++++++++++- CHANGELOG.md | 6 ++++++ scripts/test-parallel.mjs | 33 ++++++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8de4f3882c8a..e7bef285a7ae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -418,12 +418,23 @@ jobs: include: - runtime: node task: lint + shard_index: 0 + shard_count: 1 command: pnpm lint - runtime: node task: test + shard_index: 1 + shard_count: 2 + command: pnpm canvas:a2ui:bundle && pnpm test + - runtime: node + task: test + shard_index: 2 + shard_count: 2 command: pnpm canvas:a2ui:bundle && pnpm test - runtime: node task: protocol + shard_index: 0 + shard_count: 1 command: pnpm protocol:check steps: - name: Checkout @@ -495,6 +506,12 @@ jobs: pnpm -v pnpm install --frozen-lockfile --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true || pnpm install --frozen-lockfile --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true + - name: Configure test shard (Windows) + if: matrix.task == 'test' + run: | + echo "OPENCLAW_TEST_SHARDS=${{ matrix.shard_count }}" >> "$GITHUB_ENV" + echo "OPENCLAW_TEST_SHARD_INDEX=${{ matrix.shard_index }}" >> "$GITHUB_ENV" + - name: Configure vitest JSON reports if: matrix.task == 'test' run: echo "OPENCLAW_VITEST_REPORT_DIR=$RUNNER_TEMP/vitest-reports" >> "$GITHUB_ENV" @@ -512,7 +529,7 @@ jobs: if: matrix.task == 'test' uses: actions/upload-artifact@v4 with: - name: vitest-reports-${{ runner.os }}-${{ matrix.runtime }} + name: vitest-reports-${{ runner.os }}-${{ matrix.runtime }}-shard${{ matrix.shard_index }}of${{ matrix.shard_count }} path: | ${{ env.OPENCLAW_VITEST_REPORT_DIR }} ${{ runner.temp }}/vitest-slowest.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f2ce6018e9e..ce9d381407fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Docs: https://docs.openclaw.ai +## 2026.2.26 (Unreleased) + +### Fixes + +- CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. + ## 2026.2.25 ### Changes diff --git a/scripts/test-parallel.mjs b/scripts/test-parallel.mjs index 35afef83c3f8..e866ef712ab1 100644 --- a/scripts/test-parallel.mjs +++ b/scripts/test-parallel.mjs @@ -160,11 +160,31 @@ const runs = [ }, ]; const shardOverride = Number.parseInt(process.env.OPENCLAW_TEST_SHARDS ?? "", 10); -const shardCount = isWindowsCi - ? Number.isFinite(shardOverride) && shardOverride > 1 - ? shardOverride - : 2 - : 1; +const configuredShardCount = + Number.isFinite(shardOverride) && shardOverride > 1 ? shardOverride : null; +const shardCount = configuredShardCount ?? (isWindowsCi ? 2 : 1); +const shardIndexOverride = (() => { + const parsed = Number.parseInt(process.env.OPENCLAW_TEST_SHARD_INDEX ?? "", 10); + return Number.isFinite(parsed) && parsed > 0 ? parsed : null; +})(); + +if (shardIndexOverride !== null && shardCount <= 1) { + console.error( + `[test-parallel] OPENCLAW_TEST_SHARD_INDEX=${String( + shardIndexOverride, + )} requires OPENCLAW_TEST_SHARDS>1.`, + ); + process.exit(2); +} + +if (shardIndexOverride !== null && shardIndexOverride > shardCount) { + console.error( + `[test-parallel] OPENCLAW_TEST_SHARD_INDEX=${String( + shardIndexOverride, + )} exceeds OPENCLAW_TEST_SHARDS=${String(shardCount)}.`, + ); + process.exit(2); +} const windowsCiArgs = isWindowsCi ? ["--dangerouslyIgnoreUnhandledErrors"] : []; const silentArgs = process.env.OPENCLAW_TEST_SHOW_PASSED_LOGS === "1" ? [] : ["--silent=passed-only"]; @@ -391,6 +411,9 @@ const run = async (entry) => { if (shardCount <= 1) { return runOnce(entry); } + if (shardIndexOverride !== null) { + return runOnce(entry, ["--shard", `${shardIndexOverride}/${shardCount}`]); + } for (let shardIndex = 1; shardIndex <= shardCount; shardIndex += 1) { // eslint-disable-next-line no-await-in-loop const code = await runOnce(entry, ["--shard", `${shardIndex}/${shardCount}`]); From bee0c564cfa1033e397501bd84765ba2549d8e3d Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:35:00 +0530 Subject: [PATCH 130/160] test(android): add GatewaySession invoke roundtrip test --- apps/android/app/build.gradle.kts | 1 + .../android/gateway/GatewaySession.kt | 6 +- .../gateway/GatewaySessionInvokeTest.kt | 163 ++++++++++++++++++ 3 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt diff --git a/apps/android/app/build.gradle.kts b/apps/android/app/build.gradle.kts index dda17320625e..da82e9e1ea9f 100644 --- a/apps/android/app/build.gradle.kts +++ b/apps/android/app/build.gradle.kts @@ -146,6 +146,7 @@ dependencies { testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2") testImplementation("io.kotest:kotest-runner-junit5-jvm:6.1.3") testImplementation("io.kotest:kotest-assertions-core-jvm:6.1.3") + testImplementation("com.squareup.okhttp3:mockwebserver:5.3.2") testImplementation("org.robolectric:robolectric:4.16.1") testRuntimeOnly("org.junit.vintage:junit-vintage-engine:6.0.2") } diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt index 92acf9689547..ad34ca4f1c15 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt @@ -55,7 +55,7 @@ data class GatewayConnectOptions( class GatewaySession( private val scope: CoroutineScope, private val identityStore: DeviceIdentityStore, - private val deviceAuthStore: DeviceAuthStore, + private val deviceAuthStore: DeviceAuthStore? = null, private val onConnected: (serverName: String?, remoteAddress: String?, mainSessionKey: String?) -> Unit, private val onDisconnected: (message: String) -> Unit, private val onEvent: (event: String, payloadJson: String?) -> Unit, @@ -305,7 +305,7 @@ class GatewaySession( private suspend fun sendConnect(connectNonce: String) { val identity = identityStore.loadOrCreate() - val storedToken = deviceAuthStore.loadToken(identity.deviceId, options.role) + val storedToken = deviceAuthStore?.loadToken(identity.deviceId, options.role) val trimmedToken = token?.trim().orEmpty() // QR/setup/manual shared token must take precedence; stale role tokens can survive re-onboarding. val authToken = if (trimmedToken.isNotBlank()) trimmedToken else storedToken.orEmpty() @@ -327,7 +327,7 @@ class GatewaySession( val deviceToken = authObj?.get("deviceToken").asStringOrNull() val authRole = authObj?.get("role").asStringOrNull() ?: options.role if (!deviceToken.isNullOrBlank()) { - deviceAuthStore.saveToken(deviceId, authRole, deviceToken) + deviceAuthStore?.saveToken(deviceId, authRole, deviceToken) } val rawCanvas = obj["canvasHostUrl"].asStringOrNull() canvasHostUrl = normalizeCanvasHostUrl(rawCanvas, endpoint, isTlsConnection = tls != null) diff --git a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt new file mode 100644 index 000000000000..e0dded486d53 --- /dev/null +++ b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt @@ -0,0 +1,163 @@ +package ai.openclaw.android.gateway + +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import kotlinx.coroutines.withTimeoutOrNull +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive +import okhttp3.Response +import okhttp3.WebSocket +import okhttp3.WebSocketListener +import okhttp3.mockwebserver.Dispatcher +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import okhttp3.mockwebserver.RecordedRequest +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.util.concurrent.atomic.AtomicReference + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34]) +class GatewaySessionInvokeTest { + @Test + fun nodeInvokeRequest_roundTripsInvokeResult() = runBlocking { + val json = Json { ignoreUnknownKeys = true } + val connected = CompletableDeferred() + val invokeRequest = CompletableDeferred() + val invokeResultParams = CompletableDeferred() + val lastDisconnect = AtomicReference("") + val server = + MockWebServer().apply { + dispatcher = + object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + return MockResponse().withWebSocketUpgrade( + object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + webSocket.send( + """{"type":"event","event":"connect.challenge","payload":{"nonce":"android-test-nonce"}}""", + ) + } + + override fun onMessage(webSocket: WebSocket, text: String) { + val frame = json.parseToJsonElement(text).jsonObject + if (frame["type"]?.jsonPrimitive?.content != "req") return + val id = frame["id"]?.jsonPrimitive?.content ?: return + val method = frame["method"]?.jsonPrimitive?.content ?: return + when (method) { + "connect" -> { + webSocket.send( + """{"type":"res","id":"$id","ok":true,"payload":{"snapshot":{"sessionDefaults":{"mainSessionKey":"main"}}}}""", + ) + webSocket.send( + """{"type":"event","event":"node.invoke.request","payload":{"id":"invoke-1","nodeId":"node-1","command":"debug.ping","params":{"ping":"pong"},"timeoutMs":5000}}""", + ) + } + "node.invoke.result" -> { + if (!invokeResultParams.isCompleted) { + invokeResultParams.complete(frame["params"]?.toString().orEmpty()) + } + webSocket.send("""{"type":"res","id":"$id","ok":true,"payload":{"ok":true}}""") + webSocket.close(1000, "done") + } + } + } + }, + ) + } + } + start() + } + + val app = RuntimeEnvironment.getApplication() + val sessionJob = SupervisorJob() + val session = + GatewaySession( + scope = CoroutineScope(sessionJob + Dispatchers.Default), + identityStore = DeviceIdentityStore(app), + deviceAuthStore = null, + onConnected = { _, _, _ -> + if (!connected.isCompleted) connected.complete(Unit) + }, + onDisconnected = { message -> + lastDisconnect.set(message) + }, + onEvent = { _, _ -> }, + onInvoke = { req -> + if (!invokeRequest.isCompleted) invokeRequest.complete(req) + GatewaySession.InvokeResult.ok("""{"handled":true}""") + }, + ) + + try { + session.connect( + endpoint = + GatewayEndpoint( + stableId = "manual|127.0.0.1|${server.port}", + name = "test", + host = "127.0.0.1", + port = server.port, + tlsEnabled = false, + ), + token = "test-token", + password = null, + options = + GatewayConnectOptions( + role = "node", + scopes = listOf("node:invoke"), + caps = emptyList(), + commands = emptyList(), + permissions = emptyMap(), + client = + GatewayClientInfo( + id = "openclaw-android-test", + displayName = "Android Test", + version = "1.0.0-test", + platform = "android", + mode = "node", + instanceId = "android-test-instance", + deviceFamily = "android", + modelIdentifier = "test", + ), + ), + tls = null, + ) + + val connectedWithinTimeout = withTimeoutOrNull(8_000) { + connected.await() + true + } == true + if (!connectedWithinTimeout) { + throw AssertionError("never connected; lastDisconnect=${lastDisconnect.get()}; requests=${server.requestCount}") + } + val req = withTimeout(8_000) { invokeRequest.await() } + val resultParamsJson = withTimeout(8_000) { invokeResultParams.await() } + val resultParams = json.parseToJsonElement(resultParamsJson).jsonObject + + assertEquals("invoke-1", req.id) + assertEquals("node-1", req.nodeId) + assertEquals("debug.ping", req.command) + assertEquals("""{"ping":"pong"}""", req.paramsJson) + assertEquals("invoke-1", resultParams["id"]?.jsonPrimitive?.content) + assertEquals("node-1", resultParams["nodeId"]?.jsonPrimitive?.content) + assertEquals(true, resultParams["ok"]?.jsonPrimitive?.content?.toBooleanStrict()) + assertEquals( + true, + resultParams["payload"]?.jsonObject?.get("handled")?.jsonPrimitive?.content?.toBooleanStrict(), + ) + } finally { + session.disconnect() + sessionJob.cancelAndJoin() + } + } +} From 8117a13dd646a7c043b48ab6c1093a4e9fb76f20 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:35:05 +0530 Subject: [PATCH 131/160] fix(nodes): default camera snap to front high-quality image --- .../android/node/CameraCaptureManager.kt | 4 +-- src/agents/openclaw-tools.camera.test.ts | 35 +++++++++++++++++++ src/agents/tools/nodes-tool.ts | 6 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt index 65bac915effa..aa038ad9a945 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/CameraCaptureManager.kt @@ -81,8 +81,8 @@ class CameraCaptureManager(private val context: Context) { ensureCameraPermission() val owner = lifecycleOwner ?: throw IllegalStateException("UNAVAILABLE: camera not ready") val facing = parseFacing(paramsJson) ?: "front" - val quality = (parseQuality(paramsJson) ?: 0.5).coerceIn(0.1, 1.0) - val maxWidth = parseMaxWidth(paramsJson) ?: 800 + val quality = (parseQuality(paramsJson) ?: 0.95).coerceIn(0.1, 1.0) + val maxWidth = parseMaxWidth(paramsJson) ?: 1600 val provider = context.cameraProvider() val capture = ImageCapture.Builder().build() diff --git a/src/agents/openclaw-tools.camera.test.ts b/src/agents/openclaw-tools.camera.test.ts index 3082c849609f..6b1d2e35c335 100644 --- a/src/agents/openclaw-tools.camera.test.ts +++ b/src/agents/openclaw-tools.camera.test.ts @@ -43,6 +43,41 @@ beforeEach(() => { }); describe("nodes camera_snap", () => { + it("uses front/high-quality defaults when params are omitted", async () => { + callGateway.mockImplementation(async ({ method, params }) => { + if (method === "node.list") { + return mockNodeList(); + } + if (method === "node.invoke") { + expect(params).toMatchObject({ + command: "camera.snap", + params: { + facing: "front", + maxWidth: 1600, + quality: 0.95, + }, + }); + return { + payload: { + format: "jpg", + base64: "aGVsbG8=", + width: 1, + height: 1, + }, + }; + } + return unexpectedGatewayMethod(method); + }); + + const result = await executeNodes({ + action: "camera_snap", + node: NODE_ID, + }); + + const images = (result.content ?? []).filter((block) => block.type === "image"); + expect(images).toHaveLength(1); + }); + it("maps jpg payloads to image/jpeg", async () => { callGateway.mockImplementation(async ({ method }) => { if (method === "node.list") { diff --git a/src/agents/tools/nodes-tool.ts b/src/agents/tools/nodes-tool.ts index 4cfd84dc4741..3006b9cfddc5 100644 --- a/src/agents/tools/nodes-tool.ts +++ b/src/agents/tools/nodes-tool.ts @@ -186,7 +186,7 @@ export function createNodesTool(options?: { const node = readStringParam(params, "node", { required: true }); const nodeId = await resolveNodeId(gatewayOpts, node); const facingRaw = - typeof params.facing === "string" ? params.facing.toLowerCase() : "both"; + typeof params.facing === "string" ? params.facing.toLowerCase() : "front"; const facings: CameraFacing[] = facingRaw === "both" ? ["front", "back"] @@ -198,11 +198,11 @@ export function createNodesTool(options?: { const maxWidth = typeof params.maxWidth === "number" && Number.isFinite(params.maxWidth) ? params.maxWidth - : undefined; + : 1600; const quality = typeof params.quality === "number" && Number.isFinite(params.quality) ? params.quality - : undefined; + : 0.95; const delayMs = typeof params.delayMs === "number" && Number.isFinite(params.delayMs) ? params.delayMs From d4ae8a8d3493a10669b5daddca8915376f8651b4 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:41:07 +0530 Subject: [PATCH 132/160] test(android): cover invoke paramsJSON and error mapping --- .../gateway/GatewaySessionInvokeTest.kt | 262 ++++++++++++++++++ 1 file changed, 262 insertions(+) diff --git a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt index e0dded486d53..a04bcb1606ff 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt @@ -158,6 +158,268 @@ class GatewaySessionInvokeTest { } finally { session.disconnect() sessionJob.cancelAndJoin() + server.shutdown() + } + } + + @Test + fun nodeInvokeRequest_usesParamsJsonWhenProvided() = runBlocking { + val json = Json { ignoreUnknownKeys = true } + val connected = CompletableDeferred() + val invokeRequest = CompletableDeferred() + val invokeResultParams = CompletableDeferred() + val lastDisconnect = AtomicReference("") + val server = + MockWebServer().apply { + dispatcher = + object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + return MockResponse().withWebSocketUpgrade( + object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + webSocket.send( + """{"type":"event","event":"connect.challenge","payload":{"nonce":"android-test-nonce"}}""", + ) + } + + override fun onMessage(webSocket: WebSocket, text: String) { + val frame = json.parseToJsonElement(text).jsonObject + if (frame["type"]?.jsonPrimitive?.content != "req") return + val id = frame["id"]?.jsonPrimitive?.content ?: return + val method = frame["method"]?.jsonPrimitive?.content ?: return + when (method) { + "connect" -> { + webSocket.send( + """{"type":"res","id":"$id","ok":true,"payload":{"snapshot":{"sessionDefaults":{"mainSessionKey":"main"}}}}""", + ) + webSocket.send( + """{"type":"event","event":"node.invoke.request","payload":{"id":"invoke-2","nodeId":"node-2","command":"debug.raw","paramsJSON":"{\"raw\":true}","params":{"ignored":1},"timeoutMs":5000}}""", + ) + } + "node.invoke.result" -> { + if (!invokeResultParams.isCompleted) { + invokeResultParams.complete(frame["params"]?.toString().orEmpty()) + } + webSocket.send("""{"type":"res","id":"$id","ok":true,"payload":{"ok":true}}""") + webSocket.close(1000, "done") + } + } + } + }, + ) + } + } + start() + } + + val app = RuntimeEnvironment.getApplication() + val sessionJob = SupervisorJob() + val session = + GatewaySession( + scope = CoroutineScope(sessionJob + Dispatchers.Default), + identityStore = DeviceIdentityStore(app), + deviceAuthStore = null, + onConnected = { _, _, _ -> + if (!connected.isCompleted) connected.complete(Unit) + }, + onDisconnected = { message -> + lastDisconnect.set(message) + }, + onEvent = { _, _ -> }, + onInvoke = { req -> + if (!invokeRequest.isCompleted) invokeRequest.complete(req) + GatewaySession.InvokeResult.ok("""{"handled":true}""") + }, + ) + + try { + session.connect( + endpoint = + GatewayEndpoint( + stableId = "manual|127.0.0.1|${server.port}", + name = "test", + host = "127.0.0.1", + port = server.port, + tlsEnabled = false, + ), + token = "test-token", + password = null, + options = + GatewayConnectOptions( + role = "node", + scopes = listOf("node:invoke"), + caps = emptyList(), + commands = emptyList(), + permissions = emptyMap(), + client = + GatewayClientInfo( + id = "openclaw-android-test", + displayName = "Android Test", + version = "1.0.0-test", + platform = "android", + mode = "node", + instanceId = "android-test-instance", + deviceFamily = "android", + modelIdentifier = "test", + ), + ), + tls = null, + ) + + val connectedWithinTimeout = withTimeoutOrNull(8_000) { + connected.await() + true + } == true + if (!connectedWithinTimeout) { + throw AssertionError("never connected; lastDisconnect=${lastDisconnect.get()}; requests=${server.requestCount}") + } + + val req = withTimeout(8_000) { invokeRequest.await() } + val resultParamsJson = withTimeout(8_000) { invokeResultParams.await() } + val resultParams = json.parseToJsonElement(resultParamsJson).jsonObject + + assertEquals("invoke-2", req.id) + assertEquals("node-2", req.nodeId) + assertEquals("debug.raw", req.command) + assertEquals("""{"raw":true}""", req.paramsJson) + assertEquals("invoke-2", resultParams["id"]?.jsonPrimitive?.content) + assertEquals("node-2", resultParams["nodeId"]?.jsonPrimitive?.content) + assertEquals(true, resultParams["ok"]?.jsonPrimitive?.content?.toBooleanStrict()) + } finally { + session.disconnect() + sessionJob.cancelAndJoin() + server.shutdown() + } + } + + @Test + fun nodeInvokeRequest_mapsCodePrefixedErrorsIntoInvokeResult() = runBlocking { + val json = Json { ignoreUnknownKeys = true } + val connected = CompletableDeferred() + val invokeResultParams = CompletableDeferred() + val lastDisconnect = AtomicReference("") + val server = + MockWebServer().apply { + dispatcher = + object : Dispatcher() { + override fun dispatch(request: RecordedRequest): MockResponse { + return MockResponse().withWebSocketUpgrade( + object : WebSocketListener() { + override fun onOpen(webSocket: WebSocket, response: Response) { + webSocket.send( + """{"type":"event","event":"connect.challenge","payload":{"nonce":"android-test-nonce"}}""", + ) + } + + override fun onMessage(webSocket: WebSocket, text: String) { + val frame = json.parseToJsonElement(text).jsonObject + if (frame["type"]?.jsonPrimitive?.content != "req") return + val id = frame["id"]?.jsonPrimitive?.content ?: return + val method = frame["method"]?.jsonPrimitive?.content ?: return + when (method) { + "connect" -> { + webSocket.send( + """{"type":"res","id":"$id","ok":true,"payload":{"snapshot":{"sessionDefaults":{"mainSessionKey":"main"}}}}""", + ) + webSocket.send( + """{"type":"event","event":"node.invoke.request","payload":{"id":"invoke-3","nodeId":"node-3","command":"camera.snap","params":{"facing":"front"},"timeoutMs":5000}}""", + ) + } + "node.invoke.result" -> { + if (!invokeResultParams.isCompleted) { + invokeResultParams.complete(frame["params"]?.toString().orEmpty()) + } + webSocket.send("""{"type":"res","id":"$id","ok":true,"payload":{"ok":true}}""") + webSocket.close(1000, "done") + } + } + } + }, + ) + } + } + start() + } + + val app = RuntimeEnvironment.getApplication() + val sessionJob = SupervisorJob() + val session = + GatewaySession( + scope = CoroutineScope(sessionJob + Dispatchers.Default), + identityStore = DeviceIdentityStore(app), + deviceAuthStore = null, + onConnected = { _, _, _ -> + if (!connected.isCompleted) connected.complete(Unit) + }, + onDisconnected = { message -> + lastDisconnect.set(message) + }, + onEvent = { _, _ -> }, + onInvoke = { + throw IllegalStateException("CAMERA_PERMISSION_REQUIRED: grant Camera permission") + }, + ) + + try { + session.connect( + endpoint = + GatewayEndpoint( + stableId = "manual|127.0.0.1|${server.port}", + name = "test", + host = "127.0.0.1", + port = server.port, + tlsEnabled = false, + ), + token = "test-token", + password = null, + options = + GatewayConnectOptions( + role = "node", + scopes = listOf("node:invoke"), + caps = emptyList(), + commands = emptyList(), + permissions = emptyMap(), + client = + GatewayClientInfo( + id = "openclaw-android-test", + displayName = "Android Test", + version = "1.0.0-test", + platform = "android", + mode = "node", + instanceId = "android-test-instance", + deviceFamily = "android", + modelIdentifier = "test", + ), + ), + tls = null, + ) + + val connectedWithinTimeout = withTimeoutOrNull(8_000) { + connected.await() + true + } == true + if (!connectedWithinTimeout) { + throw AssertionError("never connected; lastDisconnect=${lastDisconnect.get()}; requests=${server.requestCount}") + } + + val resultParamsJson = withTimeout(8_000) { invokeResultParams.await() } + val resultParams = json.parseToJsonElement(resultParamsJson).jsonObject + + assertEquals("invoke-3", resultParams["id"]?.jsonPrimitive?.content) + assertEquals("node-3", resultParams["nodeId"]?.jsonPrimitive?.content) + assertEquals(false, resultParams["ok"]?.jsonPrimitive?.content?.toBooleanStrict()) + assertEquals( + "CAMERA_PERMISSION_REQUIRED", + resultParams["error"]?.jsonObject?.get("code")?.jsonPrimitive?.content, + ) + assertEquals( + "grant Camera permission", + resultParams["error"]?.jsonObject?.get("message")?.jsonPrimitive?.content, + ) + } finally { + session.disconnect() + sessionJob.cancelAndJoin() + server.shutdown() } } } From 18fc4c113bf9209268cec0c886776987b186b7e8 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:46:00 +0530 Subject: [PATCH 133/160] refactor(android): centralize invoke command registry --- .../android/node/ConnectionManager.kt | 38 +----- .../android/node/InvokeCommandRegistry.kt | 114 ++++++++++++++++++ .../android/node/InvokeCommandRegistryTest.kt | 48 ++++++++ 3 files changed, 168 insertions(+), 32 deletions(-) create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt create mode 100644 apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/ConnectionManager.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/ConnectionManager.kt index 9b449fc85f37..de30b8af8fe4 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/ConnectionManager.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/ConnectionManager.kt @@ -7,12 +7,6 @@ import ai.openclaw.android.gateway.GatewayClientInfo import ai.openclaw.android.gateway.GatewayConnectOptions import ai.openclaw.android.gateway.GatewayEndpoint import ai.openclaw.android.gateway.GatewayTlsParams -import ai.openclaw.android.protocol.OpenClawCanvasA2UICommand -import ai.openclaw.android.protocol.OpenClawCanvasCommand -import ai.openclaw.android.protocol.OpenClawCameraCommand -import ai.openclaw.android.protocol.OpenClawLocationCommand -import ai.openclaw.android.protocol.OpenClawScreenCommand -import ai.openclaw.android.protocol.OpenClawSmsCommand import ai.openclaw.android.protocol.OpenClawCapability import ai.openclaw.android.LocationMode import ai.openclaw.android.VoiceWakeMode @@ -80,32 +74,12 @@ class ConnectionManager( } fun buildInvokeCommands(): List = - buildList { - add(OpenClawCanvasCommand.Present.rawValue) - add(OpenClawCanvasCommand.Hide.rawValue) - add(OpenClawCanvasCommand.Navigate.rawValue) - add(OpenClawCanvasCommand.Eval.rawValue) - add(OpenClawCanvasCommand.Snapshot.rawValue) - add(OpenClawCanvasA2UICommand.Push.rawValue) - add(OpenClawCanvasA2UICommand.PushJSONL.rawValue) - add(OpenClawCanvasA2UICommand.Reset.rawValue) - add(OpenClawScreenCommand.Record.rawValue) - if (cameraEnabled()) { - add(OpenClawCameraCommand.Snap.rawValue) - add(OpenClawCameraCommand.Clip.rawValue) - } - if (locationMode() != LocationMode.Off) { - add(OpenClawLocationCommand.Get.rawValue) - } - if (smsAvailable()) { - add(OpenClawSmsCommand.Send.rawValue) - } - if (BuildConfig.DEBUG) { - add("debug.logs") - add("debug.ed25519") - } - add("app.update") - } + InvokeCommandRegistry.advertisedCommands( + cameraEnabled = cameraEnabled(), + locationEnabled = locationMode() != LocationMode.Off, + smsAvailable = smsAvailable(), + debugBuild = BuildConfig.DEBUG, + ) fun buildCapabilities(): List = buildList { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt new file mode 100644 index 000000000000..812ecf2ba4ef --- /dev/null +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt @@ -0,0 +1,114 @@ +package ai.openclaw.android.node + +import ai.openclaw.android.protocol.OpenClawCanvasA2UICommand +import ai.openclaw.android.protocol.OpenClawCanvasCommand +import ai.openclaw.android.protocol.OpenClawCameraCommand +import ai.openclaw.android.protocol.OpenClawLocationCommand +import ai.openclaw.android.protocol.OpenClawScreenCommand +import ai.openclaw.android.protocol.OpenClawSmsCommand + +enum class InvokeCommandAvailability { + Always, + CameraEnabled, + LocationEnabled, + SmsAvailable, + DebugBuild, +} + +data class InvokeCommandSpec( + val name: String, + val requiresForeground: Boolean = false, + val availability: InvokeCommandAvailability = InvokeCommandAvailability.Always, +) + +object InvokeCommandRegistry { + val all: List = + listOf( + InvokeCommandSpec( + name = OpenClawCanvasCommand.Present.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasCommand.Hide.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasCommand.Navigate.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasCommand.Eval.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasCommand.Snapshot.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasA2UICommand.Push.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasA2UICommand.PushJSONL.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCanvasA2UICommand.Reset.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawScreenCommand.Record.rawValue, + requiresForeground = true, + ), + InvokeCommandSpec( + name = OpenClawCameraCommand.Snap.rawValue, + requiresForeground = true, + availability = InvokeCommandAvailability.CameraEnabled, + ), + InvokeCommandSpec( + name = OpenClawCameraCommand.Clip.rawValue, + requiresForeground = true, + availability = InvokeCommandAvailability.CameraEnabled, + ), + InvokeCommandSpec( + name = OpenClawLocationCommand.Get.rawValue, + availability = InvokeCommandAvailability.LocationEnabled, + ), + InvokeCommandSpec( + name = OpenClawSmsCommand.Send.rawValue, + availability = InvokeCommandAvailability.SmsAvailable, + ), + InvokeCommandSpec( + name = "debug.logs", + availability = InvokeCommandAvailability.DebugBuild, + ), + InvokeCommandSpec( + name = "debug.ed25519", + availability = InvokeCommandAvailability.DebugBuild, + ), + InvokeCommandSpec(name = "app.update"), + ) + + private val byNameInternal: Map = all.associateBy { it.name } + + fun find(command: String): InvokeCommandSpec? = byNameInternal[command] + + fun advertisedCommands( + cameraEnabled: Boolean, + locationEnabled: Boolean, + smsAvailable: Boolean, + debugBuild: Boolean, + ): List { + return all + .filter { spec -> + when (spec.availability) { + InvokeCommandAvailability.Always -> true + InvokeCommandAvailability.CameraEnabled -> cameraEnabled + InvokeCommandAvailability.LocationEnabled -> locationEnabled + InvokeCommandAvailability.SmsAvailable -> smsAvailable + InvokeCommandAvailability.DebugBuild -> debugBuild + } + } + .map { it.name } + } +} diff --git a/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt new file mode 100644 index 000000000000..65b18656708f --- /dev/null +++ b/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt @@ -0,0 +1,48 @@ +package ai.openclaw.android.node + +import ai.openclaw.android.protocol.OpenClawCameraCommand +import ai.openclaw.android.protocol.OpenClawLocationCommand +import ai.openclaw.android.protocol.OpenClawSmsCommand +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class InvokeCommandRegistryTest { + @Test + fun advertisedCommands_respectsFeatureAvailability() { + val commands = + InvokeCommandRegistry.advertisedCommands( + cameraEnabled = false, + locationEnabled = false, + smsAvailable = false, + debugBuild = false, + ) + + assertFalse(commands.contains(OpenClawCameraCommand.Snap.rawValue)) + assertFalse(commands.contains(OpenClawCameraCommand.Clip.rawValue)) + assertFalse(commands.contains(OpenClawLocationCommand.Get.rawValue)) + assertFalse(commands.contains(OpenClawSmsCommand.Send.rawValue)) + assertFalse(commands.contains("debug.logs")) + assertFalse(commands.contains("debug.ed25519")) + assertTrue(commands.contains("app.update")) + } + + @Test + fun advertisedCommands_includesFeatureCommandsWhenEnabled() { + val commands = + InvokeCommandRegistry.advertisedCommands( + cameraEnabled = true, + locationEnabled = true, + smsAvailable = true, + debugBuild = true, + ) + + assertTrue(commands.contains(OpenClawCameraCommand.Snap.rawValue)) + assertTrue(commands.contains(OpenClawCameraCommand.Clip.rawValue)) + assertTrue(commands.contains(OpenClawLocationCommand.Get.rawValue)) + assertTrue(commands.contains(OpenClawSmsCommand.Send.rawValue)) + assertTrue(commands.contains("debug.logs")) + assertTrue(commands.contains("debug.ed25519")) + assertTrue(commands.contains("app.update")) + } +} From 39d362aeff550a23045595786bad78f298b42900 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:47:39 +0530 Subject: [PATCH 134/160] refactor(android): distill invoke dispatcher command flow --- .../openclaw/android/node/InvokeDispatcher.kt | 139 +++++++++--------- 1 file changed, 66 insertions(+), 73 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt index 91e9da8add14..0e58517f2f60 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt @@ -24,31 +24,25 @@ class InvokeDispatcher( private val onCanvasA2uiReset: () -> Unit, ) { suspend fun handleInvoke(command: String, paramsJson: String?): GatewaySession.InvokeResult { - // Check foreground requirement for canvas/camera/screen commands - if ( - command.startsWith(OpenClawCanvasCommand.NamespacePrefix) || - command.startsWith(OpenClawCanvasA2UICommand.NamespacePrefix) || - command.startsWith(OpenClawCameraCommand.NamespacePrefix) || - command.startsWith(OpenClawScreenCommand.NamespacePrefix) - ) { - if (!isForeground()) { - return GatewaySession.InvokeResult.error( - code = "NODE_BACKGROUND_UNAVAILABLE", - message = "NODE_BACKGROUND_UNAVAILABLE: canvas/camera/screen commands require foreground", + val spec = + InvokeCommandRegistry.find(command) + ?: return GatewaySession.InvokeResult.error( + code = "INVALID_REQUEST", + message = "INVALID_REQUEST: unknown command", ) - } + if (spec.requiresForeground && !isForeground()) { + return GatewaySession.InvokeResult.error( + code = "NODE_BACKGROUND_UNAVAILABLE", + message = "NODE_BACKGROUND_UNAVAILABLE: canvas/camera/screen commands require foreground", + ) } - - // Check camera enabled - if (command.startsWith(OpenClawCameraCommand.NamespacePrefix) && !cameraEnabled()) { + if (spec.availability == InvokeCommandAvailability.CameraEnabled && !cameraEnabled()) { return GatewaySession.InvokeResult.error( code = "CAMERA_DISABLED", message = "CAMERA_DISABLED: enable Camera in Settings", ) } - - // Check location enabled - if (command.startsWith(OpenClawLocationCommand.NamespacePrefix) && !locationEnabled()) { + if (spec.availability == InvokeCommandAvailability.LocationEnabled && !locationEnabled()) { return GatewaySession.InvokeResult.error( code = "LOCATION_DISABLED", message = "LOCATION_DISABLED: enable Location in Settings", @@ -75,53 +69,33 @@ class InvokeDispatcher( code = "INVALID_REQUEST", message = "INVALID_REQUEST: javaScript required", ) - val result = - try { - canvas.eval(js) - } catch (err: Throwable) { - return GatewaySession.InvokeResult.error( - code = "NODE_BACKGROUND_UNAVAILABLE", - message = "NODE_BACKGROUND_UNAVAILABLE: canvas unavailable", - ) - } - GatewaySession.InvokeResult.ok("""{"result":${result.toJsonString()}}""") + withCanvasAvailable { + val result = canvas.eval(js) + GatewaySession.InvokeResult.ok("""{"result":${result.toJsonString()}}""") + } } OpenClawCanvasCommand.Snapshot.rawValue -> { val snapshotParams = CanvasController.parseSnapshotParams(paramsJson) - val base64 = - try { + withCanvasAvailable { + val base64 = canvas.snapshotBase64( format = snapshotParams.format, quality = snapshotParams.quality, maxWidth = snapshotParams.maxWidth, ) - } catch (err: Throwable) { - return GatewaySession.InvokeResult.error( - code = "NODE_BACKGROUND_UNAVAILABLE", - message = "NODE_BACKGROUND_UNAVAILABLE: canvas unavailable", - ) - } - GatewaySession.InvokeResult.ok("""{"format":"${snapshotParams.format.rawValue}","base64":"$base64"}""") + GatewaySession.InvokeResult.ok("""{"format":"${snapshotParams.format.rawValue}","base64":"$base64"}""") + } } // A2UI commands - OpenClawCanvasA2UICommand.Reset.rawValue -> { - val a2uiUrl = a2uiHandler.resolveA2uiHostUrl() - ?: return GatewaySession.InvokeResult.error( - code = "A2UI_HOST_NOT_CONFIGURED", - message = "A2UI_HOST_NOT_CONFIGURED: gateway did not advertise canvas host", - ) - val ready = a2uiHandler.ensureA2uiReady(a2uiUrl) - if (!ready) { - return GatewaySession.InvokeResult.error( - code = "A2UI_HOST_UNAVAILABLE", - message = "A2UI host not reachable", - ) + OpenClawCanvasA2UICommand.Reset.rawValue -> + withReadyA2ui { + withCanvasAvailable { + val res = canvas.eval(A2UIHandler.a2uiResetJS) + onCanvasA2uiReset() + GatewaySession.InvokeResult.ok(res) + } } - val res = canvas.eval(A2UIHandler.a2uiResetJS) - onCanvasA2uiReset() - GatewaySession.InvokeResult.ok(res) - } OpenClawCanvasA2UICommand.Push.rawValue, OpenClawCanvasA2UICommand.PushJSONL.rawValue -> { val messages = try { @@ -132,22 +106,14 @@ class InvokeDispatcher( message = err.message ?: "invalid A2UI payload" ) } - val a2uiUrl = a2uiHandler.resolveA2uiHostUrl() - ?: return GatewaySession.InvokeResult.error( - code = "A2UI_HOST_NOT_CONFIGURED", - message = "A2UI_HOST_NOT_CONFIGURED: gateway did not advertise canvas host", - ) - val ready = a2uiHandler.ensureA2uiReady(a2uiUrl) - if (!ready) { - return GatewaySession.InvokeResult.error( - code = "A2UI_HOST_UNAVAILABLE", - message = "A2UI host not reachable", - ) + withReadyA2ui { + withCanvasAvailable { + val js = A2UIHandler.a2uiApplyMessagesJS(messages) + val res = canvas.eval(js) + onCanvasA2uiPush() + GatewaySession.InvokeResult.ok(res) + } } - val js = A2UIHandler.a2uiApplyMessagesJS(messages) - val res = canvas.eval(js) - onCanvasA2uiPush() - GatewaySession.InvokeResult.ok(res) } // Camera commands @@ -170,11 +136,38 @@ class InvokeDispatcher( // App update "app.update" -> appUpdateHandler.handleUpdate(paramsJson) - else -> - GatewaySession.InvokeResult.error( - code = "INVALID_REQUEST", - message = "INVALID_REQUEST: unknown command", - ) + else -> GatewaySession.InvokeResult.error(code = "INVALID_REQUEST", message = "INVALID_REQUEST: unknown command") + } + } + + private suspend fun withReadyA2ui( + block: suspend () -> GatewaySession.InvokeResult, + ): GatewaySession.InvokeResult { + val a2uiUrl = a2uiHandler.resolveA2uiHostUrl() + ?: return GatewaySession.InvokeResult.error( + code = "A2UI_HOST_NOT_CONFIGURED", + message = "A2UI_HOST_NOT_CONFIGURED: gateway did not advertise canvas host", + ) + val ready = a2uiHandler.ensureA2uiReady(a2uiUrl) + if (!ready) { + return GatewaySession.InvokeResult.error( + code = "A2UI_HOST_UNAVAILABLE", + message = "A2UI host not reachable", + ) + } + return block() + } + + private suspend fun withCanvasAvailable( + block: suspend () -> GatewaySession.InvokeResult, + ): GatewaySession.InvokeResult { + return try { + block() + } catch (_: Throwable) { + GatewaySession.InvokeResult.error( + code = "NODE_BACKGROUND_UNAVAILABLE", + message = "NODE_BACKGROUND_UNAVAILABLE: canvas unavailable", + ) } } } From c3f54fcddd62853e51c1969eeff6dc24b802eb04 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:48:50 +0530 Subject: [PATCH 135/160] refactor(android): unify invoke error parsing --- .../android/gateway/GatewaySession.kt | 12 +----- .../android/gateway/InvokeErrorParser.kt | 39 +++++++++++++++++++ .../ai/openclaw/android/node/NodeUtils.kt | 12 ++---- .../android/gateway/InvokeErrorParserTest.kt | 33 ++++++++++++++++ 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/gateway/InvokeErrorParser.kt create mode 100644 apps/android/app/src/test/java/ai/openclaw/android/gateway/InvokeErrorParserTest.kt diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt index ad34ca4f1c15..0c6d14721e0b 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt @@ -535,16 +535,8 @@ class GatewaySession( } private fun invokeErrorFromThrowable(err: Throwable): InvokeResult { - val msg = err.message?.trim().takeIf { !it.isNullOrEmpty() } ?: err::class.java.simpleName - val parts = msg.split(":", limit = 2) - if (parts.size == 2) { - val code = parts[0].trim() - val rest = parts[1].trim() - if (code.isNotEmpty() && code.all { it.isUpperCase() || it == '_' }) { - return InvokeResult.error(code = code, message = rest.ifEmpty { msg }) - } - } - return InvokeResult.error(code = "UNAVAILABLE", message = msg) + val parsed = parseInvokeErrorFromThrowable(err, fallbackMessage = err::class.java.simpleName) + return InvokeResult.error(code = parsed.code, message = parsed.message) } private fun failPending() { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/InvokeErrorParser.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/InvokeErrorParser.kt new file mode 100644 index 000000000000..7242f4a55333 --- /dev/null +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/InvokeErrorParser.kt @@ -0,0 +1,39 @@ +package ai.openclaw.android.gateway + +data class ParsedInvokeError( + val code: String, + val message: String, + val hadExplicitCode: Boolean, +) { + val prefixedMessage: String + get() = "$code: $message" +} + +fun parseInvokeErrorMessage(raw: String): ParsedInvokeError { + val trimmed = raw.trim() + if (trimmed.isEmpty()) { + return ParsedInvokeError(code = "UNAVAILABLE", message = "error", hadExplicitCode = false) + } + + val parts = trimmed.split(":", limit = 2) + if (parts.size == 2) { + val code = parts[0].trim() + val rest = parts[1].trim() + if (code.isNotEmpty() && code.all { it.isUpperCase() || it == '_' }) { + return ParsedInvokeError( + code = code, + message = rest.ifEmpty { trimmed }, + hadExplicitCode = true, + ) + } + } + return ParsedInvokeError(code = "UNAVAILABLE", message = trimmed, hadExplicitCode = false) +} + +fun parseInvokeErrorFromThrowable( + err: Throwable, + fallbackMessage: String = "error", +): ParsedInvokeError { + val raw = err.message?.trim().takeIf { !it.isNullOrEmpty() } ?: fallbackMessage + return parseInvokeErrorMessage(raw) +} diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/NodeUtils.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/NodeUtils.kt index 8ba5ad276d5e..c3f463174a4e 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/NodeUtils.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/NodeUtils.kt @@ -1,5 +1,6 @@ package ai.openclaw.android.node +import ai.openclaw.android.gateway.parseInvokeErrorFromThrowable import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject @@ -37,14 +38,9 @@ fun parseHexColorArgb(raw: String?): Long? { } fun invokeErrorFromThrowable(err: Throwable): Pair { - val raw = (err.message ?: "").trim() - if (raw.isEmpty()) return "UNAVAILABLE" to "UNAVAILABLE: error" - - val idx = raw.indexOf(':') - if (idx <= 0) return "UNAVAILABLE" to raw - val code = raw.substring(0, idx).trim().ifEmpty { "UNAVAILABLE" } - val message = raw.substring(idx + 1).trim().ifEmpty { raw } - return code to "$code: $message" + val parsed = parseInvokeErrorFromThrowable(err, fallbackMessage = "UNAVAILABLE: error") + val message = if (parsed.hadExplicitCode) parsed.prefixedMessage else parsed.message + return parsed.code to message } fun normalizeMainKey(raw: String?): String? { diff --git a/apps/android/app/src/test/java/ai/openclaw/android/gateway/InvokeErrorParserTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/gateway/InvokeErrorParserTest.kt new file mode 100644 index 000000000000..ca8e8f21424a --- /dev/null +++ b/apps/android/app/src/test/java/ai/openclaw/android/gateway/InvokeErrorParserTest.kt @@ -0,0 +1,33 @@ +package ai.openclaw.android.gateway + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class InvokeErrorParserTest { + @Test + fun parseInvokeErrorMessage_parsesUppercaseCodePrefix() { + val parsed = parseInvokeErrorMessage("CAMERA_PERMISSION_REQUIRED: grant Camera permission") + assertEquals("CAMERA_PERMISSION_REQUIRED", parsed.code) + assertEquals("grant Camera permission", parsed.message) + assertTrue(parsed.hadExplicitCode) + assertEquals("CAMERA_PERMISSION_REQUIRED: grant Camera permission", parsed.prefixedMessage) + } + + @Test + fun parseInvokeErrorMessage_rejectsNonCanonicalCodePrefix() { + val parsed = parseInvokeErrorMessage("IllegalStateException: boom") + assertEquals("UNAVAILABLE", parsed.code) + assertEquals("IllegalStateException: boom", parsed.message) + assertFalse(parsed.hadExplicitCode) + } + + @Test + fun parseInvokeErrorFromThrowable_usesFallbackWhenMessageMissing() { + val parsed = parseInvokeErrorFromThrowable(IllegalStateException(), fallbackMessage = "fallback") + assertEquals("UNAVAILABLE", parsed.code) + assertEquals("fallback", parsed.message) + assertFalse(parsed.hadExplicitCode) + } +} From f7865527afa172a6812f3223550477ed325e6335 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 11:56:04 +0530 Subject: [PATCH 136/160] fix(android): omit websocket Origin for native gateway connect --- .../main/java/ai/openclaw/android/gateway/GatewaySession.kt | 4 +--- .../ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt index 0c6d14721e0b..0ec3132336f4 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt @@ -200,9 +200,7 @@ class GatewaySession( suspend fun connect() { val scheme = if (tls != null) "wss" else "ws" val url = "$scheme://${endpoint.host}:${endpoint.port}" - val httpScheme = if (tls != null) "https" else "http" - val origin = "$httpScheme://${endpoint.host}:${endpoint.port}" - val request = Request.Builder().url(url).header("Origin", origin).build() + val request = Request.Builder().url(url).build() socket = client.newWebSocket(request, Listener()) try { connectDeferred.await() diff --git a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt index a04bcb1606ff..e5f98a0b6536 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt @@ -19,6 +19,7 @@ import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.RecordedRequest import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @@ -35,12 +36,14 @@ class GatewaySessionInvokeTest { val connected = CompletableDeferred() val invokeRequest = CompletableDeferred() val invokeResultParams = CompletableDeferred() + val handshakeOrigin = AtomicReference(null) val lastDisconnect = AtomicReference("") val server = MockWebServer().apply { dispatcher = object : Dispatcher() { override fun dispatch(request: RecordedRequest): MockResponse { + handshakeOrigin.compareAndSet(null, request.getHeader("Origin")) return MockResponse().withWebSocketUpgrade( object : WebSocketListener() { override fun onOpen(webSocket: WebSocket, response: Response) { @@ -148,6 +151,7 @@ class GatewaySessionInvokeTest { assertEquals("node-1", req.nodeId) assertEquals("debug.ping", req.command) assertEquals("""{"ping":"pong"}""", req.paramsJson) + assertNull(handshakeOrigin.get()) assertEquals("invoke-1", resultParams["id"]?.jsonPrimitive?.content) assertEquals("node-1", resultParams["nodeId"]?.jsonPrimitive?.content) assertEquals(true, resultParams["ok"]?.jsonPrimitive?.content?.toBooleanStrict()) From a87d961ebc252512299a158a43946fc90e8315f4 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 12:07:09 +0530 Subject: [PATCH 137/160] fix(android): require gateway device auth store --- .../android/gateway/DeviceAuthStore.kt | 11 ++++++++--- .../android/gateway/GatewaySession.kt | 6 +++--- .../gateway/GatewaySessionInvokeTest.kt | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceAuthStore.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceAuthStore.kt index 810e029fba89..8ace62e087c3 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceAuthStore.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/DeviceAuthStore.kt @@ -2,13 +2,18 @@ package ai.openclaw.android.gateway import ai.openclaw.android.SecurePrefs -class DeviceAuthStore(private val prefs: SecurePrefs) { - fun loadToken(deviceId: String, role: String): String? { +interface DeviceAuthTokenStore { + fun loadToken(deviceId: String, role: String): String? + fun saveToken(deviceId: String, role: String, token: String) +} + +class DeviceAuthStore(private val prefs: SecurePrefs) : DeviceAuthTokenStore { + override fun loadToken(deviceId: String, role: String): String? { val key = tokenKey(deviceId, role) return prefs.getString(key)?.trim()?.takeIf { it.isNotEmpty() } } - fun saveToken(deviceId: String, role: String, token: String) { + override fun saveToken(deviceId: String, role: String, token: String) { val key = tokenKey(deviceId, role) prefs.putString(key, token.trim()) } diff --git a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt index 0ec3132336f4..e0aea39768e1 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/gateway/GatewaySession.kt @@ -55,7 +55,7 @@ data class GatewayConnectOptions( class GatewaySession( private val scope: CoroutineScope, private val identityStore: DeviceIdentityStore, - private val deviceAuthStore: DeviceAuthStore? = null, + private val deviceAuthStore: DeviceAuthTokenStore, private val onConnected: (serverName: String?, remoteAddress: String?, mainSessionKey: String?) -> Unit, private val onDisconnected: (message: String) -> Unit, private val onEvent: (event: String, payloadJson: String?) -> Unit, @@ -303,7 +303,7 @@ class GatewaySession( private suspend fun sendConnect(connectNonce: String) { val identity = identityStore.loadOrCreate() - val storedToken = deviceAuthStore?.loadToken(identity.deviceId, options.role) + val storedToken = deviceAuthStore.loadToken(identity.deviceId, options.role) val trimmedToken = token?.trim().orEmpty() // QR/setup/manual shared token must take precedence; stale role tokens can survive re-onboarding. val authToken = if (trimmedToken.isNotBlank()) trimmedToken else storedToken.orEmpty() @@ -325,7 +325,7 @@ class GatewaySession( val deviceToken = authObj?.get("deviceToken").asStringOrNull() val authRole = authObj?.get("role").asStringOrNull() ?: options.role if (!deviceToken.isNullOrBlank()) { - deviceAuthStore?.saveToken(deviceId, authRole, deviceToken) + deviceAuthStore.saveToken(deviceId, authRole, deviceToken) } val rawCanvas = obj["canvasHostUrl"].asStringOrNull() canvasHostUrl = normalizeCanvasHostUrl(rawCanvas, endpoint, isTlsConnection = tls != null) diff --git a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt index e5f98a0b6536..e8a37aef21b5 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/gateway/GatewaySessionInvokeTest.kt @@ -27,6 +27,16 @@ import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config import java.util.concurrent.atomic.AtomicReference +private class InMemoryDeviceAuthStore : DeviceAuthTokenStore { + private val tokens = mutableMapOf() + + override fun loadToken(deviceId: String, role: String): String? = tokens["${deviceId.trim()}|${role.trim()}"]?.trim()?.takeIf { it.isNotEmpty() } + + override fun saveToken(deviceId: String, role: String, token: String) { + tokens["${deviceId.trim()}|${role.trim()}"] = token.trim() + } +} + @RunWith(RobolectricTestRunner::class) @Config(sdk = [34]) class GatewaySessionInvokeTest { @@ -84,11 +94,12 @@ class GatewaySessionInvokeTest { val app = RuntimeEnvironment.getApplication() val sessionJob = SupervisorJob() + val deviceAuthStore = InMemoryDeviceAuthStore() val session = GatewaySession( scope = CoroutineScope(sessionJob + Dispatchers.Default), identityStore = DeviceIdentityStore(app), - deviceAuthStore = null, + deviceAuthStore = deviceAuthStore, onConnected = { _, _, _ -> if (!connected.isCompleted) connected.complete(Unit) }, @@ -218,11 +229,12 @@ class GatewaySessionInvokeTest { val app = RuntimeEnvironment.getApplication() val sessionJob = SupervisorJob() + val deviceAuthStore = InMemoryDeviceAuthStore() val session = GatewaySession( scope = CoroutineScope(sessionJob + Dispatchers.Default), identityStore = DeviceIdentityStore(app), - deviceAuthStore = null, + deviceAuthStore = deviceAuthStore, onConnected = { _, _, _ -> if (!connected.isCompleted) connected.complete(Unit) }, @@ -347,11 +359,12 @@ class GatewaySessionInvokeTest { val app = RuntimeEnvironment.getApplication() val sessionJob = SupervisorJob() + val deviceAuthStore = InMemoryDeviceAuthStore() val session = GatewaySession( scope = CoroutineScope(sessionJob + Dispatchers.Default), identityStore = DeviceIdentityStore(app), - deviceAuthStore = null, + deviceAuthStore = deviceAuthStore, onConnected = { _, _, _ -> if (!connected.isCompleted) connected.complete(Unit) }, From ac6539ed03ace4aadfa3e1b52bcf63bb3e9e257b Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 12:07:51 +0530 Subject: [PATCH 138/160] refactor(android): unify invoke availability gating --- .../java/ai/openclaw/android/NodeRuntime.kt | 2 + .../openclaw/android/node/InvokeDispatcher.kt | 57 +++++++++++++++---- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt index 02e9b136091f..8cb2b0b47dcb 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/NodeRuntime.kt @@ -131,6 +131,8 @@ class NodeRuntime(context: Context) { isForeground = { _isForeground.value }, cameraEnabled = { cameraEnabled.value }, locationEnabled = { locationMode.value != LocationMode.Off }, + smsAvailable = { sms.canSendSms() }, + debugBuild = { BuildConfig.DEBUG }, onCanvasA2uiPush = { _canvasA2uiHydrated.value = true _canvasRehydratePending.value = false diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt index 0e58517f2f60..d293df76668c 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt @@ -20,6 +20,8 @@ class InvokeDispatcher( private val isForeground: () -> Boolean, private val cameraEnabled: () -> Boolean, private val locationEnabled: () -> Boolean, + private val smsAvailable: () -> Boolean, + private val debugBuild: () -> Boolean, private val onCanvasA2uiPush: () -> Unit, private val onCanvasA2uiReset: () -> Unit, ) { @@ -36,18 +38,7 @@ class InvokeDispatcher( message = "NODE_BACKGROUND_UNAVAILABLE: canvas/camera/screen commands require foreground", ) } - if (spec.availability == InvokeCommandAvailability.CameraEnabled && !cameraEnabled()) { - return GatewaySession.InvokeResult.error( - code = "CAMERA_DISABLED", - message = "CAMERA_DISABLED: enable Camera in Settings", - ) - } - if (spec.availability == InvokeCommandAvailability.LocationEnabled && !locationEnabled()) { - return GatewaySession.InvokeResult.error( - code = "LOCATION_DISABLED", - message = "LOCATION_DISABLED: enable Location in Settings", - ) - } + availabilityError(spec.availability)?.let { return it } return when (command) { // Canvas commands @@ -170,4 +161,46 @@ class InvokeDispatcher( ) } } + + private fun availabilityError(availability: InvokeCommandAvailability): GatewaySession.InvokeResult? { + return when (availability) { + InvokeCommandAvailability.Always -> null + InvokeCommandAvailability.CameraEnabled -> + if (cameraEnabled()) { + null + } else { + GatewaySession.InvokeResult.error( + code = "CAMERA_DISABLED", + message = "CAMERA_DISABLED: enable Camera in Settings", + ) + } + InvokeCommandAvailability.LocationEnabled -> + if (locationEnabled()) { + null + } else { + GatewaySession.InvokeResult.error( + code = "LOCATION_DISABLED", + message = "LOCATION_DISABLED: enable Location in Settings", + ) + } + InvokeCommandAvailability.SmsAvailable -> + if (smsAvailable()) { + null + } else { + GatewaySession.InvokeResult.error( + code = "SMS_UNAVAILABLE", + message = "SMS_UNAVAILABLE: SMS not available on this device", + ) + } + InvokeCommandAvailability.DebugBuild -> + if (debugBuild()) { + null + } else { + GatewaySession.InvokeResult.error( + code = "INVALID_REQUEST", + message = "INVALID_REQUEST: unknown command", + ) + } + } + } } From c5d040bbea3b199ea8c2c7f323aa9fd143958178 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 12:17:14 +0530 Subject: [PATCH 139/160] fix: update changelog for android invoke distill (#27257) (thanks @obviyus) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce9d381407fb..1a60109a0946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. ## 2026.2.25 From 96c77025263d44a9c5c5cad9b5713b98b340fcbe Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 02:36:56 -0500 Subject: [PATCH 140/160] Agents: add account-scoped bind and routing commands (#27195) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: ad35a458a55427614a35c9d0713a7386172464ad Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 4 + docs/cli/agents.md | 50 +++- docs/cli/channels.md | 10 + docs/cli/index.md | 32 ++- docs/concepts/multi-agent.md | 6 + src/channels/plugins/types.adapters.ts | 11 +- src/cli/program/preaction.test.ts | 10 + src/cli/program/preaction.ts | 1 + src/cli/program/register.agent.test.ts | 64 +++++ src/cli/program/register.agent.ts | 65 +++++ src/commands/agents.bind.commands.test.ts | 200 +++++++++++++ src/commands/agents.bindings.ts | 194 +++++++++++-- src/commands/agents.commands.add.ts | 3 +- src/commands/agents.commands.bind.ts | 324 ++++++++++++++++++++++ src/commands/agents.test.ts | 109 ++++++++ src/commands/agents.ts | 1 + src/commands/channels/add.ts | 73 ++++- 17 files changed, 1133 insertions(+), 24 deletions(-) create mode 100644 src/commands/agents.bind.commands.test.ts create mode 100644 src/commands/agents.commands.bind.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a60109a0946..b890896f0d33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Docs: https://docs.openclaw.ai ## 2026.2.26 (Unreleased) +### Changes + +- Agents/Routing CLI: add `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` for account-scoped route management, including channel-only to account-scoped binding upgrades, role-aware binding identity handling, plugin-resolved binding account IDs, and optional account-binding prompts in `openclaw channels add`. (#27195) thanks @gumadeiras. + ### Fixes - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. diff --git a/docs/cli/agents.md b/docs/cli/agents.md index 39679265f14a..5bdc8a68bf21 100644 --- a/docs/cli/agents.md +++ b/docs/cli/agents.md @@ -1,5 +1,5 @@ --- -summary: "CLI reference for `openclaw agents` (list/add/delete/set identity)" +summary: "CLI reference for `openclaw agents` (list/add/delete/bindings/bind/unbind/set identity)" read_when: - You want multiple isolated agents (workspaces + routing + auth) title: "agents" @@ -19,11 +19,59 @@ Related: ```bash openclaw agents list openclaw agents add work --workspace ~/.openclaw/workspace-work +openclaw agents bindings +openclaw agents bind --agent work --bind telegram:ops +openclaw agents unbind --agent work --bind telegram:ops openclaw agents set-identity --workspace ~/.openclaw/workspace --from-identity openclaw agents set-identity --agent main --avatar avatars/openclaw.png openclaw agents delete work ``` +## Routing bindings + +Use routing bindings to pin inbound channel traffic to a specific agent. + +List bindings: + +```bash +openclaw agents bindings +openclaw agents bindings --agent work +openclaw agents bindings --json +``` + +Add bindings: + +```bash +openclaw agents bind --agent work --bind telegram:ops --bind discord:guild-a +``` + +If you omit `accountId` (`--bind `), OpenClaw resolves it from channel defaults and plugin setup hooks when available. + +### Binding scope behavior + +- A binding without `accountId` matches the channel default account only. +- `accountId: "*"` is the channel-wide fallback (all accounts) and is less specific than an explicit account binding. +- If the same agent already has a matching channel binding without `accountId`, and you later bind with an explicit or resolved `accountId`, OpenClaw upgrades that existing binding in place instead of adding a duplicate. + +Example: + +```bash +# initial channel-only binding +openclaw agents bind --agent work --bind telegram + +# later upgrade to account-scoped binding +openclaw agents bind --agent work --bind telegram:ops +``` + +After the upgrade, routing for that binding is scoped to `telegram:ops`. If you also want default-account routing, add it explicitly (for example `--bind telegram:default`). + +Remove bindings: + +```bash +openclaw agents unbind --agent work --bind telegram:ops +openclaw agents unbind --agent work --all +``` + ## Identity files Each agent workspace can include an `IDENTITY.md` at the workspace root: diff --git a/docs/cli/channels.md b/docs/cli/channels.md index 4213efb3eb7d..0f9c3fecb774 100644 --- a/docs/cli/channels.md +++ b/docs/cli/channels.md @@ -35,6 +35,16 @@ openclaw channels remove --channel telegram --delete Tip: `openclaw channels add --help` shows per-channel flags (token, app token, signal-cli paths, etc). +When you run `openclaw channels add` without flags, the interactive wizard can prompt: + +- account ids per selected channel +- optional display names for those accounts +- `Bind configured channel accounts to agents now?` + +If you confirm bind now, the wizard asks which agent should own each configured channel account and writes account-scoped routing bindings. + +You can also manage the same routing rules later with `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` (see [agents](/cli/agents)). + ## Login / logout (interactive) ```bash diff --git a/docs/cli/index.md b/docs/cli/index.md index 32eb31b5eb3d..1394d83db0e3 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -574,7 +574,37 @@ Options: - `--non-interactive` - `--json` -Binding specs use `channel[:accountId]`. When `accountId` is omitted for WhatsApp, the default account id is used. +Binding specs use `channel[:accountId]`. When `accountId` is omitted, OpenClaw may resolve account scope via channel defaults/plugin hooks; otherwise it is a channel binding without explicit account scope. + +#### `agents bindings` + +List routing bindings. + +Options: + +- `--agent ` +- `--json` + +#### `agents bind` + +Add routing bindings for an agent. + +Options: + +- `--agent ` +- `--bind ` (repeatable) +- `--json` + +#### `agents unbind` + +Remove routing bindings for an agent. + +Options: + +- `--agent ` +- `--bind ` (repeatable) +- `--all` +- `--json` #### `agents delete ` diff --git a/docs/concepts/multi-agent.md b/docs/concepts/multi-agent.md index 069fcfb63679..842531cc2a68 100644 --- a/docs/concepts/multi-agent.md +++ b/docs/concepts/multi-agent.md @@ -185,6 +185,12 @@ Bindings are **deterministic** and **most-specific wins**: If multiple bindings match in the same tier, the first one in config order wins. If a binding sets multiple match fields (for example `peer` + `guildId`), all specified fields are required (`AND` semantics). +Important account-scope detail: + +- A binding that omits `accountId` matches the default account only. +- Use `accountId: "*"` for a channel-wide fallback across all accounts. +- If you later add the same binding for the same agent with an explicit account id, OpenClaw upgrades the existing channel-only binding to account-scoped instead of duplicating it. + ## Multiple accounts / phone numbers Channels that support **multiple accounts** (e.g. WhatsApp) use `accountId` to identify diff --git a/src/channels/plugins/types.adapters.ts b/src/channels/plugins/types.adapters.ts index 113df6ad5cd2..ead7f68b2fa8 100644 --- a/src/channels/plugins/types.adapters.ts +++ b/src/channels/plugins/types.adapters.ts @@ -21,7 +21,16 @@ import type { } from "./types.core.js"; export type ChannelSetupAdapter = { - resolveAccountId?: (params: { cfg: OpenClawConfig; accountId?: string }) => string; + resolveAccountId?: (params: { + cfg: OpenClawConfig; + accountId?: string; + input?: ChannelSetupInput; + }) => string; + resolveBindingAccountId?: (params: { + cfg: OpenClawConfig; + agentId: string; + accountId?: string; + }) => string | undefined; applyAccountName?: (params: { cfg: OpenClawConfig; accountId: string; diff --git a/src/cli/program/preaction.test.ts b/src/cli/program/preaction.test.ts index bf4184d362a9..caa9dd24869c 100644 --- a/src/cli/program/preaction.test.ts +++ b/src/cli/program/preaction.test.ts @@ -80,6 +80,7 @@ describe("registerPreActionHooks", () => { program.command("update").action(async () => {}); program.command("channels").action(async () => {}); program.command("directory").action(async () => {}); + program.command("agents").action(async () => {}); program.command("configure").action(async () => {}); program.command("onboard").action(async () => {}); program @@ -145,6 +146,15 @@ describe("registerPreActionHooks", () => { expect(ensurePluginRegistryLoadedMock).toHaveBeenCalledTimes(1); }); + it("loads plugin registry for agents command", async () => { + await runCommand({ + parseArgv: ["agents"], + processArgv: ["node", "openclaw", "agents"], + }); + + expect(ensurePluginRegistryLoadedMock).toHaveBeenCalledTimes(1); + }); + it("skips config guard for doctor and completion commands", async () => { await runCommand({ parseArgv: ["doctor"], diff --git a/src/cli/program/preaction.ts b/src/cli/program/preaction.ts index 6a9abc3e99ed..6a232386b14b 100644 --- a/src/cli/program/preaction.ts +++ b/src/cli/program/preaction.ts @@ -25,6 +25,7 @@ const PLUGIN_REQUIRED_COMMANDS = new Set([ "message", "channels", "directory", + "agents", "configure", "onboard", ]); diff --git a/src/cli/program/register.agent.test.ts b/src/cli/program/register.agent.test.ts index 9ad1fa19d52e..2d37e56a702b 100644 --- a/src/cli/program/register.agent.test.ts +++ b/src/cli/program/register.agent.test.ts @@ -3,9 +3,12 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const agentCliCommandMock = vi.fn(); const agentsAddCommandMock = vi.fn(); +const agentsBindingsCommandMock = vi.fn(); +const agentsBindCommandMock = vi.fn(); const agentsDeleteCommandMock = vi.fn(); const agentsListCommandMock = vi.fn(); const agentsSetIdentityCommandMock = vi.fn(); +const agentsUnbindCommandMock = vi.fn(); const setVerboseMock = vi.fn(); const createDefaultDepsMock = vi.fn(() => ({ deps: true })); @@ -21,9 +24,12 @@ vi.mock("../../commands/agent-via-gateway.js", () => ({ vi.mock("../../commands/agents.js", () => ({ agentsAddCommand: agentsAddCommandMock, + agentsBindingsCommand: agentsBindingsCommandMock, + agentsBindCommand: agentsBindCommandMock, agentsDeleteCommand: agentsDeleteCommandMock, agentsListCommand: agentsListCommandMock, agentsSetIdentityCommand: agentsSetIdentityCommandMock, + agentsUnbindCommand: agentsUnbindCommandMock, })); vi.mock("../../globals.js", () => ({ @@ -55,9 +61,12 @@ describe("registerAgentCommands", () => { vi.clearAllMocks(); agentCliCommandMock.mockResolvedValue(undefined); agentsAddCommandMock.mockResolvedValue(undefined); + agentsBindingsCommandMock.mockResolvedValue(undefined); + agentsBindCommandMock.mockResolvedValue(undefined); agentsDeleteCommandMock.mockResolvedValue(undefined); agentsListCommandMock.mockResolvedValue(undefined); agentsSetIdentityCommandMock.mockResolvedValue(undefined); + agentsUnbindCommandMock.mockResolvedValue(undefined); createDefaultDepsMock.mockReturnValue({ deps: true }); }); @@ -147,6 +156,61 @@ describe("registerAgentCommands", () => { ); }); + it("forwards agents bindings options", async () => { + await runCli(["agents", "bindings", "--agent", "ops", "--json"]); + expect(agentsBindingsCommandMock).toHaveBeenCalledWith( + { + agent: "ops", + json: true, + }, + runtime, + ); + }); + + it("forwards agents bind options", async () => { + await runCli([ + "agents", + "bind", + "--agent", + "ops", + "--bind", + "matrix-js:ops", + "--bind", + "telegram", + "--json", + ]); + expect(agentsBindCommandMock).toHaveBeenCalledWith( + { + agent: "ops", + bind: ["matrix-js:ops", "telegram"], + json: true, + }, + runtime, + ); + }); + + it("documents bind accountId resolution behavior in help text", () => { + const program = new Command(); + registerAgentCommands(program, { agentChannelOptions: "last|telegram|discord" }); + const agents = program.commands.find((command) => command.name() === "agents"); + const bind = agents?.commands.find((command) => command.name() === "bind"); + const help = bind?.helpInformation() ?? ""; + expect(help).toContain("accountId is resolved by channel defaults/hooks"); + }); + + it("forwards agents unbind options", async () => { + await runCli(["agents", "unbind", "--agent", "ops", "--all", "--json"]); + expect(agentsUnbindCommandMock).toHaveBeenCalledWith( + { + agent: "ops", + bind: [], + all: true, + json: true, + }, + runtime, + ); + }); + it("forwards agents delete options", async () => { await runCli(["agents", "delete", "worker-a", "--force", "--json"]); expect(agentsDeleteCommandMock).toHaveBeenCalledWith( diff --git a/src/cli/program/register.agent.ts b/src/cli/program/register.agent.ts index 4f112403c141..fdb45a0960ae 100644 --- a/src/cli/program/register.agent.ts +++ b/src/cli/program/register.agent.ts @@ -2,9 +2,12 @@ import type { Command } from "commander"; import { agentCliCommand } from "../../commands/agent-via-gateway.js"; import { agentsAddCommand, + agentsBindingsCommand, + agentsBindCommand, agentsDeleteCommand, agentsListCommand, agentsSetIdentityCommand, + agentsUnbindCommand, } from "../../commands/agents.js"; import { setVerbose } from "../../globals.js"; import { defaultRuntime } from "../../runtime.js"; @@ -102,6 +105,68 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/agent", "docs.openclaw.ai/cli/age }); }); + agents + .command("bindings") + .description("List routing bindings") + .option("--agent ", "Filter by agent id") + .option("--json", "Output JSON instead of text", false) + .action(async (opts) => { + await runCommandWithRuntime(defaultRuntime, async () => { + await agentsBindingsCommand( + { + agent: opts.agent as string | undefined, + json: Boolean(opts.json), + }, + defaultRuntime, + ); + }); + }); + + agents + .command("bind") + .description("Add routing bindings for an agent") + .option("--agent ", "Agent id (defaults to current default agent)") + .option( + "--bind ", + "Binding to add (repeatable). If omitted, accountId is resolved by channel defaults/hooks.", + collectOption, + [], + ) + .option("--json", "Output JSON summary", false) + .action(async (opts) => { + await runCommandWithRuntime(defaultRuntime, async () => { + await agentsBindCommand( + { + agent: opts.agent as string | undefined, + bind: Array.isArray(opts.bind) ? (opts.bind as string[]) : undefined, + json: Boolean(opts.json), + }, + defaultRuntime, + ); + }); + }); + + agents + .command("unbind") + .description("Remove routing bindings for an agent") + .option("--agent ", "Agent id (defaults to current default agent)") + .option("--bind ", "Binding to remove (repeatable)", collectOption, []) + .option("--all", "Remove all bindings for this agent", false) + .option("--json", "Output JSON summary", false) + .action(async (opts) => { + await runCommandWithRuntime(defaultRuntime, async () => { + await agentsUnbindCommand( + { + agent: opts.agent as string | undefined, + bind: Array.isArray(opts.bind) ? (opts.bind as string[]) : undefined, + all: Boolean(opts.all), + json: Boolean(opts.json), + }, + defaultRuntime, + ); + }); + }); + agents .command("add [name]") .description("Add a new isolated agent") diff --git a/src/commands/agents.bind.commands.test.ts b/src/commands/agents.bind.commands.test.ts new file mode 100644 index 000000000000..0fe03173be67 --- /dev/null +++ b/src/commands/agents.bind.commands.test.ts @@ -0,0 +1,200 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { baseConfigSnapshot, createTestRuntime } from "./test-runtime-config-helpers.js"; + +const readConfigFileSnapshotMock = vi.hoisted(() => vi.fn()); +const writeConfigFileMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined)); + +vi.mock("../config/config.js", async (importOriginal) => ({ + ...(await importOriginal()), + readConfigFileSnapshot: readConfigFileSnapshotMock, + writeConfigFile: writeConfigFileMock, +})); + +vi.mock("../channels/plugins/index.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + getChannelPlugin: (channel: string) => { + if (channel === "matrix-js") { + return { + id: "matrix-js", + setup: { + resolveBindingAccountId: ({ agentId }: { agentId: string }) => agentId.toLowerCase(), + }, + }; + } + return actual.getChannelPlugin(channel); + }, + normalizeChannelId: (channel: string) => { + if (channel.trim().toLowerCase() === "matrix-js") { + return "matrix-js"; + } + return actual.normalizeChannelId(channel); + }, + }; +}); + +import { agentsBindCommand, agentsBindingsCommand, agentsUnbindCommand } from "./agents.js"; + +const runtime = createTestRuntime(); + +describe("agents bind/unbind commands", () => { + beforeEach(() => { + readConfigFileSnapshotMock.mockClear(); + writeConfigFileMock.mockClear(); + runtime.log.mockClear(); + runtime.error.mockClear(); + runtime.exit.mockClear(); + }); + + it("lists all bindings by default", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + bindings: [ + { agentId: "main", match: { channel: "matrix-js" } }, + { agentId: "ops", match: { channel: "telegram", accountId: "work" } }, + ], + }, + }); + + await agentsBindingsCommand({}, runtime); + + expect(runtime.log).toHaveBeenCalledWith(expect.stringContaining("main <- matrix-js")); + expect(runtime.log).toHaveBeenCalledWith( + expect.stringContaining("ops <- telegram accountId=work"), + ); + }); + + it("binds routes to default agent when --agent is omitted", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: {}, + }); + + await agentsBindCommand({ bind: ["telegram"] }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [{ agentId: "main", match: { channel: "telegram" } }], + }), + ); + expect(runtime.exit).not.toHaveBeenCalled(); + }); + + it("defaults matrix-js accountId to the target agent id when omitted", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: {}, + }); + + await agentsBindCommand({ agent: "main", bind: ["matrix-js"] }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [{ agentId: "main", match: { channel: "matrix-js", accountId: "main" } }], + }), + ); + expect(runtime.exit).not.toHaveBeenCalled(); + }); + + it("upgrades existing channel-only binding when accountId is later provided", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + bindings: [{ agentId: "main", match: { channel: "telegram" } }], + }, + }); + + await agentsBindCommand({ bind: ["telegram:work"] }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [{ agentId: "main", match: { channel: "telegram", accountId: "work" } }], + }), + ); + expect(runtime.log).toHaveBeenCalledWith("Updated bindings:"); + expect(runtime.exit).not.toHaveBeenCalled(); + }); + + it("unbinds all routes for an agent", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + agents: { list: [{ id: "ops", workspace: "/tmp/ops" }] }, + bindings: [ + { agentId: "main", match: { channel: "matrix-js" } }, + { agentId: "ops", match: { channel: "telegram", accountId: "work" } }, + ], + }, + }); + + await agentsUnbindCommand({ agent: "ops", all: true }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [{ agentId: "main", match: { channel: "matrix-js" } }], + }), + ); + expect(runtime.exit).not.toHaveBeenCalled(); + }); + + it("reports ownership conflicts during unbind and exits 1", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + agents: { list: [{ id: "ops", workspace: "/tmp/ops" }] }, + bindings: [{ agentId: "main", match: { channel: "telegram", accountId: "ops" } }], + }, + }); + + await agentsUnbindCommand({ agent: "ops", bind: ["telegram:ops"] }, runtime); + + expect(writeConfigFileMock).not.toHaveBeenCalled(); + expect(runtime.error).toHaveBeenCalledWith("Bindings are owned by another agent:"); + expect(runtime.exit).toHaveBeenCalledWith(1); + }); + + it("keeps role-based bindings when removing channel-level discord binding", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + roles: ["111", "222"], + }, + }, + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + }, + }, + ], + }, + }); + + await agentsUnbindCommand({ bind: ["discord:guild-a"] }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + roles: ["111", "222"], + }, + }, + ], + }), + ); + expect(runtime.exit).not.toHaveBeenCalled(); + }); +}); diff --git a/src/commands/agents.bindings.ts b/src/commands/agents.bindings.ts index f0eaf959e1e9..ca0c0ee649cf 100644 --- a/src/commands/agents.bindings.ts +++ b/src/commands/agents.bindings.ts @@ -8,16 +8,51 @@ import type { ChannelChoice } from "./onboard-types.js"; function bindingMatchKey(match: AgentBinding["match"]) { const accountId = match.accountId?.trim() || DEFAULT_ACCOUNT_ID; + const identityKey = bindingMatchIdentityKey(match); + return [identityKey, accountId].join("|"); +} + +function bindingMatchIdentityKey(match: AgentBinding["match"]) { + const roles = Array.isArray(match.roles) + ? Array.from( + new Set( + match.roles + .map((role) => role.trim()) + .filter(Boolean) + .toSorted(), + ), + ) + : []; return [ match.channel, - accountId, match.peer?.kind ?? "", match.peer?.id ?? "", match.guildId ?? "", match.teamId ?? "", + roles.join(","), ].join("|"); } +function canUpgradeBindingAccountScope(params: { + existing: AgentBinding; + incoming: AgentBinding; + normalizedIncomingAgentId: string; +}): boolean { + if (!params.incoming.match.accountId?.trim()) { + return false; + } + if (params.existing.match.accountId?.trim()) { + return false; + } + if (normalizeAgentId(params.existing.agentId) !== params.normalizedIncomingAgentId) { + return false; + } + return ( + bindingMatchIdentityKey(params.existing.match) === + bindingMatchIdentityKey(params.incoming.match) + ); +} + export function describeBinding(binding: AgentBinding) { const match = binding.match; const parts = [match.channel]; @@ -42,10 +77,11 @@ export function applyAgentBindings( ): { config: OpenClawConfig; added: AgentBinding[]; + updated: AgentBinding[]; skipped: AgentBinding[]; conflicts: Array<{ binding: AgentBinding; existingAgentId: string }>; } { - const existing = cfg.bindings ?? []; + const existing = [...(cfg.bindings ?? [])]; const existingMatchMap = new Map(); for (const binding of existing) { const key = bindingMatchKey(binding.match); @@ -55,6 +91,7 @@ export function applyAgentBindings( } const added: AgentBinding[] = []; + const updated: AgentBinding[] = []; const skipped: AgentBinding[] = []; const conflicts: Array<{ binding: AgentBinding; existingAgentId: string }> = []; @@ -70,12 +107,41 @@ export function applyAgentBindings( } continue; } + + const upgradeIndex = existing.findIndex((candidate) => + canUpgradeBindingAccountScope({ + existing: candidate, + incoming: binding, + normalizedIncomingAgentId: agentId, + }), + ); + if (upgradeIndex >= 0) { + const current = existing[upgradeIndex]; + if (!current) { + continue; + } + const previousKey = bindingMatchKey(current.match); + const upgradedBinding: AgentBinding = { + ...current, + agentId, + match: { + ...current.match, + accountId: binding.match.accountId?.trim(), + }, + }; + existing[upgradeIndex] = upgradedBinding; + existingMatchMap.delete(previousKey); + existingMatchMap.set(bindingMatchKey(upgradedBinding.match), agentId); + updated.push(upgradedBinding); + continue; + } + existingMatchMap.set(key, agentId); added.push({ ...binding, agentId }); } - if (added.length === 0) { - return { config: cfg, added, skipped, conflicts }; + if (added.length === 0 && updated.length === 0) { + return { config: cfg, added, updated, skipped, conflicts }; } return { @@ -84,11 +150,78 @@ export function applyAgentBindings( bindings: [...existing, ...added], }, added, + updated, skipped, conflicts, }; } +export function removeAgentBindings( + cfg: OpenClawConfig, + bindings: AgentBinding[], +): { + config: OpenClawConfig; + removed: AgentBinding[]; + missing: AgentBinding[]; + conflicts: Array<{ binding: AgentBinding; existingAgentId: string }>; +} { + const existing = cfg.bindings ?? []; + const removeIndexes = new Set(); + const removed: AgentBinding[] = []; + const missing: AgentBinding[] = []; + const conflicts: Array<{ binding: AgentBinding; existingAgentId: string }> = []; + + for (const binding of bindings) { + const desiredAgentId = normalizeAgentId(binding.agentId); + const key = bindingMatchKey(binding.match); + let matchedIndex = -1; + let conflictingAgentId: string | null = null; + for (let i = 0; i < existing.length; i += 1) { + if (removeIndexes.has(i)) { + continue; + } + const current = existing[i]; + if (!current || bindingMatchKey(current.match) !== key) { + continue; + } + const currentAgentId = normalizeAgentId(current.agentId); + if (currentAgentId === desiredAgentId) { + matchedIndex = i; + break; + } + conflictingAgentId = currentAgentId; + } + if (matchedIndex >= 0) { + const matched = existing[matchedIndex]; + if (matched) { + removeIndexes.add(matchedIndex); + removed.push(matched); + } + continue; + } + if (conflictingAgentId) { + conflicts.push({ binding, existingAgentId: conflictingAgentId }); + continue; + } + missing.push(binding); + } + + if (removeIndexes.size === 0) { + return { config: cfg, removed, missing, conflicts }; + } + + const nextBindings = existing.filter((_, index) => !removeIndexes.has(index)); + return { + config: { + ...cfg, + bindings: nextBindings.length > 0 ? nextBindings : undefined, + }, + removed, + missing, + conflicts, + }; +} + function resolveDefaultAccountId(cfg: OpenClawConfig, provider: ChannelId): string { const plugin = getChannelPlugin(provider); if (!plugin) { @@ -97,6 +230,33 @@ function resolveDefaultAccountId(cfg: OpenClawConfig, provider: ChannelId): stri return resolveChannelDefaultAccountId({ plugin, cfg }); } +function resolveBindingAccountId(params: { + channel: ChannelId; + config: OpenClawConfig; + agentId: string; + explicitAccountId?: string; +}): string | undefined { + const explicitAccountId = params.explicitAccountId?.trim(); + if (explicitAccountId) { + return explicitAccountId; + } + + const plugin = getChannelPlugin(params.channel); + const pluginAccountId = plugin?.setup?.resolveBindingAccountId?.({ + cfg: params.config, + agentId: params.agentId, + }); + if (pluginAccountId?.trim()) { + return pluginAccountId.trim(); + } + + if (plugin?.meta.forceAccountBinding) { + return resolveDefaultAccountId(params.config, params.channel); + } + + return undefined; +} + export function buildChannelBindings(params: { agentId: string; selection: ChannelChoice[]; @@ -107,14 +267,14 @@ export function buildChannelBindings(params: { const agentId = normalizeAgentId(params.agentId); for (const channel of params.selection) { const match: AgentBinding["match"] = { channel }; - const accountId = params.accountIds?.[channel]?.trim(); + const accountId = resolveBindingAccountId({ + channel, + config: params.config, + agentId, + explicitAccountId: params.accountIds?.[channel], + }); if (accountId) { match.accountId = accountId; - } else { - const plugin = getChannelPlugin(channel); - if (plugin?.meta.forceAccountBinding) { - match.accountId = resolveDefaultAccountId(params.config, channel); - } } bindings.push({ agentId, match }); } @@ -141,17 +301,17 @@ export function parseBindingSpecs(params: { errors.push(`Unknown channel "${channelRaw}".`); continue; } - let accountId = accountRaw?.trim(); + let accountId: string | undefined = accountRaw?.trim(); if (accountRaw !== undefined && !accountId) { errors.push(`Invalid binding "${trimmed}" (empty account id).`); continue; } - if (!accountId) { - const plugin = getChannelPlugin(channel); - if (plugin?.meta.forceAccountBinding) { - accountId = resolveDefaultAccountId(params.config, channel); - } - } + accountId = resolveBindingAccountId({ + channel, + config: params.config, + agentId, + explicitAccountId: accountId, + }); const match: AgentBinding["match"] = { channel }; if (accountId) { match.accountId = accountId; diff --git a/src/commands/agents.commands.add.ts b/src/commands/agents.commands.add.ts index 807ecca0b204..61c45392f598 100644 --- a/src/commands/agents.commands.add.ts +++ b/src/commands/agents.commands.add.ts @@ -125,7 +125,7 @@ export async function agentsAddCommand( const bindingResult = bindingParse.bindings.length > 0 ? applyAgentBindings(nextConfig, bindingParse.bindings) - : { config: nextConfig, added: [], skipped: [], conflicts: [] }; + : { config: nextConfig, added: [], updated: [], skipped: [], conflicts: [] }; await writeConfigFile(bindingResult.config); if (!opts.json) { @@ -145,6 +145,7 @@ export async function agentsAddCommand( model, bindings: { added: bindingResult.added.map(describeBinding), + updated: bindingResult.updated.map(describeBinding), skipped: bindingResult.skipped.map(describeBinding), conflicts: bindingResult.conflicts.map( (conflict) => `${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`, diff --git a/src/commands/agents.commands.bind.ts b/src/commands/agents.commands.bind.ts new file mode 100644 index 000000000000..b7a021053c6d --- /dev/null +++ b/src/commands/agents.commands.bind.ts @@ -0,0 +1,324 @@ +import { resolveDefaultAgentId } from "../agents/agent-scope.js"; +import { writeConfigFile } from "../config/config.js"; +import { logConfigUpdated } from "../config/logging.js"; +import type { AgentBinding } from "../config/types.js"; +import { normalizeAgentId } from "../routing/session-key.js"; +import type { RuntimeEnv } from "../runtime.js"; +import { defaultRuntime } from "../runtime.js"; +import { + applyAgentBindings, + describeBinding, + parseBindingSpecs, + removeAgentBindings, +} from "./agents.bindings.js"; +import { requireValidConfig } from "./agents.command-shared.js"; +import { buildAgentSummaries } from "./agents.config.js"; + +type AgentsBindingsListOptions = { + agent?: string; + json?: boolean; +}; + +type AgentsBindOptions = { + agent?: string; + bind?: string[]; + json?: boolean; +}; + +type AgentsUnbindOptions = { + agent?: string; + bind?: string[]; + all?: boolean; + json?: boolean; +}; + +function resolveAgentId( + cfg: Awaited>, + agentInput: string | undefined, + params?: { fallbackToDefault?: boolean }, +): string | null { + if (!cfg) { + return null; + } + if (agentInput?.trim()) { + return normalizeAgentId(agentInput); + } + if (params?.fallbackToDefault) { + return resolveDefaultAgentId(cfg); + } + return null; +} + +function hasAgent(cfg: Awaited>, agentId: string): boolean { + if (!cfg) { + return false; + } + return buildAgentSummaries(cfg).some((summary) => summary.id === agentId); +} + +function formatBindingOwnerLine(binding: AgentBinding): string { + return `${normalizeAgentId(binding.agentId)} <- ${describeBinding(binding)}`; +} + +export async function agentsBindingsCommand( + opts: AgentsBindingsListOptions, + runtime: RuntimeEnv = defaultRuntime, +) { + const cfg = await requireValidConfig(runtime); + if (!cfg) { + return; + } + + const filterAgentId = resolveAgentId(cfg, opts.agent?.trim()); + if (opts.agent && !filterAgentId) { + runtime.error("Agent id is required."); + runtime.exit(1); + return; + } + if (filterAgentId && !hasAgent(cfg, filterAgentId)) { + runtime.error(`Agent "${filterAgentId}" not found.`); + runtime.exit(1); + return; + } + + const filtered = (cfg.bindings ?? []).filter( + (binding) => !filterAgentId || normalizeAgentId(binding.agentId) === filterAgentId, + ); + if (opts.json) { + runtime.log( + JSON.stringify( + filtered.map((binding) => ({ + agentId: normalizeAgentId(binding.agentId), + match: binding.match, + description: describeBinding(binding), + })), + null, + 2, + ), + ); + return; + } + + if (filtered.length === 0) { + runtime.log( + filterAgentId ? `No routing bindings for agent "${filterAgentId}".` : "No routing bindings.", + ); + return; + } + + runtime.log( + [ + "Routing bindings:", + ...filtered.map((binding) => `- ${formatBindingOwnerLine(binding)}`), + ].join("\n"), + ); +} + +export async function agentsBindCommand( + opts: AgentsBindOptions, + runtime: RuntimeEnv = defaultRuntime, +) { + const cfg = await requireValidConfig(runtime); + if (!cfg) { + return; + } + + const agentId = resolveAgentId(cfg, opts.agent?.trim(), { fallbackToDefault: true }); + if (!agentId) { + runtime.error("Unable to resolve agent id."); + runtime.exit(1); + return; + } + if (!hasAgent(cfg, agentId)) { + runtime.error(`Agent "${agentId}" not found.`); + runtime.exit(1); + return; + } + + const specs = (opts.bind ?? []).map((value) => value.trim()).filter(Boolean); + if (specs.length === 0) { + runtime.error("Provide at least one --bind ."); + runtime.exit(1); + return; + } + + const parsed = parseBindingSpecs({ agentId, specs, config: cfg }); + if (parsed.errors.length > 0) { + runtime.error(parsed.errors.join("\n")); + runtime.exit(1); + return; + } + + const result = applyAgentBindings(cfg, parsed.bindings); + if (result.added.length > 0 || result.updated.length > 0) { + await writeConfigFile(result.config); + if (!opts.json) { + logConfigUpdated(runtime); + } + } + + const payload = { + agentId, + added: result.added.map(describeBinding), + updated: result.updated.map(describeBinding), + skipped: result.skipped.map(describeBinding), + conflicts: result.conflicts.map( + (conflict) => `${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`, + ), + }; + if (opts.json) { + runtime.log(JSON.stringify(payload, null, 2)); + if (result.conflicts.length > 0) { + runtime.exit(1); + } + return; + } + + if (result.added.length > 0) { + runtime.log("Added bindings:"); + for (const binding of result.added) { + runtime.log(`- ${describeBinding(binding)}`); + } + } else if (result.updated.length === 0) { + runtime.log("No new bindings added."); + } + + if (result.updated.length > 0) { + runtime.log("Updated bindings:"); + for (const binding of result.updated) { + runtime.log(`- ${describeBinding(binding)}`); + } + } + + if (result.skipped.length > 0) { + runtime.log("Already present:"); + for (const binding of result.skipped) { + runtime.log(`- ${describeBinding(binding)}`); + } + } + + if (result.conflicts.length > 0) { + runtime.error("Skipped bindings already claimed by another agent:"); + for (const conflict of result.conflicts) { + runtime.error(`- ${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`); + } + runtime.exit(1); + } +} + +export async function agentsUnbindCommand( + opts: AgentsUnbindOptions, + runtime: RuntimeEnv = defaultRuntime, +) { + const cfg = await requireValidConfig(runtime); + if (!cfg) { + return; + } + + const agentId = resolveAgentId(cfg, opts.agent?.trim(), { fallbackToDefault: true }); + if (!agentId) { + runtime.error("Unable to resolve agent id."); + runtime.exit(1); + return; + } + if (!hasAgent(cfg, agentId)) { + runtime.error(`Agent "${agentId}" not found.`); + runtime.exit(1); + return; + } + if (opts.all && (opts.bind?.length ?? 0) > 0) { + runtime.error("Use either --all or --bind, not both."); + runtime.exit(1); + return; + } + + if (opts.all) { + const existing = cfg.bindings ?? []; + const removed = existing.filter((binding) => normalizeAgentId(binding.agentId) === agentId); + const kept = existing.filter((binding) => normalizeAgentId(binding.agentId) !== agentId); + if (removed.length === 0) { + runtime.log(`No bindings to remove for agent "${agentId}".`); + return; + } + const next = { + ...cfg, + bindings: kept.length > 0 ? kept : undefined, + }; + await writeConfigFile(next); + if (!opts.json) { + logConfigUpdated(runtime); + } + const payload = { + agentId, + removed: removed.map(describeBinding), + missing: [] as string[], + conflicts: [] as string[], + }; + if (opts.json) { + runtime.log(JSON.stringify(payload, null, 2)); + return; + } + runtime.log(`Removed ${removed.length} binding(s) for "${agentId}".`); + return; + } + + const specs = (opts.bind ?? []).map((value) => value.trim()).filter(Boolean); + if (specs.length === 0) { + runtime.error("Provide at least one --bind or use --all."); + runtime.exit(1); + return; + } + + const parsed = parseBindingSpecs({ agentId, specs, config: cfg }); + if (parsed.errors.length > 0) { + runtime.error(parsed.errors.join("\n")); + runtime.exit(1); + return; + } + + const result = removeAgentBindings(cfg, parsed.bindings); + if (result.removed.length > 0) { + await writeConfigFile(result.config); + if (!opts.json) { + logConfigUpdated(runtime); + } + } + + const payload = { + agentId, + removed: result.removed.map(describeBinding), + missing: result.missing.map(describeBinding), + conflicts: result.conflicts.map( + (conflict) => `${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`, + ), + }; + if (opts.json) { + runtime.log(JSON.stringify(payload, null, 2)); + if (result.conflicts.length > 0) { + runtime.exit(1); + } + return; + } + + if (result.removed.length > 0) { + runtime.log("Removed bindings:"); + for (const binding of result.removed) { + runtime.log(`- ${describeBinding(binding)}`); + } + } else { + runtime.log("No bindings removed."); + } + if (result.missing.length > 0) { + runtime.log("Not found:"); + for (const binding of result.missing) { + runtime.log(`- ${describeBinding(binding)}`); + } + } + if (result.conflicts.length > 0) { + runtime.error("Bindings are owned by another agent:"); + for (const conflict of result.conflicts) { + runtime.error(`- ${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`); + } + runtime.exit(1); + } +} diff --git a/src/commands/agents.test.ts b/src/commands/agents.test.ts index 1becb77548fa..dfb339e43840 100644 --- a/src/commands/agents.test.ts +++ b/src/commands/agents.test.ts @@ -8,6 +8,7 @@ import { applyAgentConfig, buildAgentSummaries, pruneAgentConfig, + removeAgentBindings, } from "./agents.js"; describe("agents helpers", () => { @@ -111,6 +112,114 @@ describe("agents helpers", () => { expect(result.config.bindings).toHaveLength(2); }); + it("applyAgentBindings upgrades channel-only binding to account-specific binding for same agent", () => { + const cfg: OpenClawConfig = { + bindings: [ + { + agentId: "main", + match: { channel: "telegram" }, + }, + ], + }; + + const result = applyAgentBindings(cfg, [ + { + agentId: "main", + match: { channel: "telegram", accountId: "work" }, + }, + ]); + + expect(result.added).toHaveLength(0); + expect(result.updated).toHaveLength(1); + expect(result.conflicts).toHaveLength(0); + expect(result.config.bindings).toEqual([ + { + agentId: "main", + match: { channel: "telegram", accountId: "work" }, + }, + ]); + }); + + it("applyAgentBindings treats role-based bindings as distinct routes", () => { + const cfg: OpenClawConfig = { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + ], + }; + + const result = applyAgentBindings(cfg, [ + { + agentId: "work", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ]); + + expect(result.added).toHaveLength(1); + expect(result.conflicts).toHaveLength(0); + expect(result.config.bindings).toHaveLength(2); + }); + + it("removeAgentBindings does not remove role-based bindings when removing channel-level routes", () => { + const cfg: OpenClawConfig = { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ], + }; + + const result = removeAgentBindings(cfg, [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ]); + + expect(result.removed).toHaveLength(1); + expect(result.conflicts).toHaveLength(0); + expect(result.config.bindings).toEqual([ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + ]); + }); + it("pruneAgentConfig removes agent, bindings, and allowlist entries", () => { const cfg: OpenClawConfig = { agents: { diff --git a/src/commands/agents.ts b/src/commands/agents.ts index 6679bb853da7..5f5bdcd3c7b7 100644 --- a/src/commands/agents.ts +++ b/src/commands/agents.ts @@ -1,4 +1,5 @@ export * from "./agents.bindings.js"; +export * from "./agents.commands.bind.js"; export * from "./agents.commands.add.js"; export * from "./agents.commands.delete.js"; export * from "./agents.commands.identity.js"; diff --git a/src/commands/channels/add.ts b/src/commands/channels/add.ts index a23fb2428e25..eaa6fc533976 100644 --- a/src/commands/channels/add.ts +++ b/src/commands/channels/add.ts @@ -8,6 +8,8 @@ import { defaultRuntime, type RuntimeEnv } from "../../runtime.js"; import { resolveTelegramAccount } from "../../telegram/accounts.js"; import { deleteTelegramUpdateOffset } from "../../telegram/update-offset-store.js"; import { createClackPrompter } from "../../wizard/clack-prompter.js"; +import { applyAgentBindings, describeBinding } from "../agents.bindings.js"; +import { buildAgentSummaries } from "../agents.config.js"; import { setupChannels } from "../onboard-channels.js"; import type { ChannelChoice } from "../onboard-types.js"; import { @@ -111,6 +113,68 @@ export async function channelsAddCommand( } } + const bindTargets = selection + .map((channel) => ({ + channel, + accountId: accountIds[channel]?.trim(), + })) + .filter( + ( + value, + ): value is { + channel: ChannelChoice; + accountId: string; + } => Boolean(value.accountId), + ); + if (bindTargets.length > 0) { + const bindNow = await prompter.confirm({ + message: "Bind configured channel accounts to agents now?", + initialValue: true, + }); + if (bindNow) { + const agentSummaries = buildAgentSummaries(nextConfig); + const defaultAgentId = resolveDefaultAgentId(nextConfig); + for (const target of bindTargets) { + const targetAgentId = await prompter.select({ + message: `Route ${target.channel} account "${target.accountId}" to agent`, + options: agentSummaries.map((agent) => ({ + value: agent.id, + label: agent.isDefault ? `${agent.id} (default)` : agent.id, + })), + initialValue: defaultAgentId, + }); + const bindingResult = applyAgentBindings(nextConfig, [ + { + agentId: targetAgentId, + match: { channel: target.channel, accountId: target.accountId }, + }, + ]); + nextConfig = bindingResult.config; + if (bindingResult.added.length > 0 || bindingResult.updated.length > 0) { + await prompter.note( + [ + ...bindingResult.added.map((binding) => `Added: ${describeBinding(binding)}`), + ...bindingResult.updated.map((binding) => `Updated: ${describeBinding(binding)}`), + ].join("\n"), + "Routing bindings", + ); + } + if (bindingResult.conflicts.length > 0) { + await prompter.note( + [ + "Skipped bindings already claimed by another agent:", + ...bindingResult.conflicts.map( + (conflict) => + `- ${describeBinding(conflict.binding)} (agent=${conflict.existingAgentId})`, + ), + ].join("\n"), + "Routing bindings", + ); + } + } + } + } + await writeConfigFile(nextConfig); await prompter.outro("Channels updated."); return; @@ -153,9 +217,6 @@ export async function channelsAddCommand( runtime.exit(1); return; } - const accountId = - plugin.setup.resolveAccountId?.({ cfg: nextConfig, accountId: opts.account }) ?? - normalizeAccountId(opts.account); const useEnv = opts.useEnv === true; const initialSyncLimit = typeof opts.initialSyncLimit === "number" @@ -199,6 +260,12 @@ export async function channelsAddCommand( dmAllowlist, autoDiscoverChannels: opts.autoDiscoverChannels, }; + const accountId = + plugin.setup.resolveAccountId?.({ + cfg: nextConfig, + accountId: opts.account, + input, + }) ?? normalizeAccountId(opts.account); const validationError = plugin.setup.validateInput?.({ cfg: nextConfig, From b9757114290f1939f07718b18727417cee3cfaa2 Mon Sep 17 00:00:00 2001 From: Frank Yang Date: Wed, 25 Feb 2026 23:40:48 -0800 Subject: [PATCH 141/160] fix(daemon): stabilize LaunchAgent restart and proxy env passthrough (#27276) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: b08797a99561f3d849443f77fda4fe086c508b49 Co-authored-by: frankekn <4488090+frankekn@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + src/daemon/launchd-plist.ts | 4 +- src/daemon/launchd.integration.test.ts | 112 +++++++++++++++++++++++++ src/daemon/launchd.test.ts | 86 +++++++++++++++++++ src/daemon/launchd.ts | 70 +++++++++++++++- src/daemon/service-env.test.ts | 33 ++++++++ src/daemon/service-env.ts | 33 ++++++++ 7 files changed, 334 insertions(+), 5 deletions(-) create mode 100644 src/daemon/launchd.integration.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b890896f0d33..21e75f9ab6d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Daemon/macOS launchd: forward proxy env vars into supervised service environments, switch LaunchAgent keepalive policy to crash-only with throttling, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. diff --git a/src/daemon/launchd-plist.ts b/src/daemon/launchd-plist.ts index e685cd9941cf..7918e1c8a372 100644 --- a/src/daemon/launchd-plist.ts +++ b/src/daemon/launchd-plist.ts @@ -1,5 +1,7 @@ import fs from "node:fs/promises"; +const LAUNCHD_THROTTLE_INTERVAL_SECONDS = 5; + const plistEscape = (value: string): string => value .replaceAll("&", "&") @@ -106,5 +108,5 @@ export function buildLaunchAgentPlist({ ? `\n Comment\n ${plistEscape(comment.trim())}` : ""; const envXml = renderEnvDict(environment); - return `\n\n\n \n Label\n ${plistEscape(label)}\n ${commentXml}\n RunAtLoad\n \n KeepAlive\n \n ProgramArguments\n ${argsXml}\n \n ${workingDirXml}\n StandardOutPath\n ${plistEscape(stdoutPath)}\n StandardErrorPath\n ${plistEscape(stderrPath)}${envXml}\n \n\n`; + return `\n\n\n \n Label\n ${plistEscape(label)}\n ${commentXml}\n RunAtLoad\n \n KeepAlive\n \n SuccessfulExit\n \n \n ThrottleInterval\n ${LAUNCHD_THROTTLE_INTERVAL_SECONDS}\n ProgramArguments\n ${argsXml}\n \n ${workingDirXml}\n StandardOutPath\n ${plistEscape(stdoutPath)}\n StandardErrorPath\n ${plistEscape(stderrPath)}${envXml}\n \n\n`; } diff --git a/src/daemon/launchd.integration.test.ts b/src/daemon/launchd.integration.test.ts new file mode 100644 index 000000000000..423ab3fa1a1c --- /dev/null +++ b/src/daemon/launchd.integration.test.ts @@ -0,0 +1,112 @@ +import { spawnSync } from "node:child_process"; +import { randomUUID } from "node:crypto"; +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { PassThrough } from "node:stream"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + installLaunchAgent, + readLaunchAgentRuntime, + restartLaunchAgent, + resolveLaunchAgentPlistPath, + uninstallLaunchAgent, +} from "./launchd.js"; +import type { GatewayServiceEnv } from "./service-types.js"; + +const WAIT_INTERVAL_MS = 200; +const WAIT_TIMEOUT_MS = 15_000; + +function canRunLaunchdIntegration(): boolean { + if (process.platform !== "darwin") { + return false; + } + if (typeof process.getuid !== "function") { + return false; + } + const domain = `gui/${process.getuid()}`; + const probe = spawnSync("launchctl", ["print", domain], { encoding: "utf8" }); + if (probe.error) { + return false; + } + return probe.status === 0; +} + +const describeLaunchdIntegration = canRunLaunchdIntegration() ? describe : describe.skip; + +async function waitForRunningRuntime(params: { + env: GatewayServiceEnv; + pidNot?: number; + timeoutMs?: number; +}): Promise<{ pid: number }> { + const timeoutMs = params.timeoutMs ?? WAIT_TIMEOUT_MS; + const deadline = Date.now() + timeoutMs; + let lastStatus = "unknown"; + let lastPid: number | undefined; + while (Date.now() < deadline) { + const runtime = await readLaunchAgentRuntime(params.env); + lastStatus = runtime.status; + lastPid = runtime.pid; + if ( + runtime.status === "running" && + typeof runtime.pid === "number" && + runtime.pid > 1 && + (params.pidNot === undefined || runtime.pid !== params.pidNot) + ) { + return { pid: runtime.pid }; + } + await new Promise((resolve) => { + setTimeout(resolve, WAIT_INTERVAL_MS); + }); + } + throw new Error( + `Timed out waiting for launchd runtime (status=${lastStatus}, pid=${lastPid ?? "none"})`, + ); +} + +describeLaunchdIntegration("launchd integration", () => { + let env: GatewayServiceEnv | undefined; + let homeDir = ""; + const stdout = new PassThrough(); + + beforeAll(async () => { + const testId = randomUUID().slice(0, 8); + homeDir = await fs.mkdtemp(path.join(os.tmpdir(), `openclaw-launchd-int-${testId}-`)); + env = { + HOME: homeDir, + OPENCLAW_LAUNCHD_LABEL: `ai.openclaw.launchd-int-${testId}`, + OPENCLAW_LOG_PREFIX: `gateway-launchd-int-${testId}`, + }; + await installLaunchAgent({ + env, + stdout, + programArguments: [process.execPath, "-e", "setInterval(() => {}, 1000);"], + }); + await waitForRunningRuntime({ env }); + }, 30_000); + + afterAll(async () => { + if (env) { + try { + await uninstallLaunchAgent({ env, stdout }); + } catch { + // Best-effort cleanup in case launchctl state already changed. + } + } + if (homeDir) { + await fs.rm(homeDir, { recursive: true, force: true }); + } + }, 30_000); + + it("restarts launchd service and keeps it running with a new pid", async () => { + if (!env) { + throw new Error("launchd integration env was not initialized"); + } + const before = await waitForRunningRuntime({ env }); + await restartLaunchAgent({ env, stdout }); + const after = await waitForRunningRuntime({ env, pidNot: before.pid }); + expect(after.pid).toBeGreaterThan(1); + expect(after.pid).not.toBe(before.pid); + await fs.access(resolveLaunchAgentPlistPath(env)); + }, 30_000); +}); diff --git a/src/daemon/launchd.test.ts b/src/daemon/launchd.test.ts index b68774cb19f3..7465666a158c 100644 --- a/src/daemon/launchd.test.ts +++ b/src/daemon/launchd.test.ts @@ -5,12 +5,14 @@ import { isLaunchAgentListed, parseLaunchctlPrint, repairLaunchAgentBootstrap, + restartLaunchAgent, resolveLaunchAgentPlistPath, } from "./launchd.js"; const state = vi.hoisted(() => ({ launchctlCalls: [] as string[][], listOutput: "", + printOutput: "", bootstrapError: "", dirs: new Set(), files: new Map(), @@ -35,6 +37,9 @@ vi.mock("./exec-file.js", () => ({ if (call[0] === "list") { return { stdout: state.listOutput, stderr: "", code: 0 }; } + if (call[0] === "print") { + return { stdout: state.printOutput, stderr: "", code: 0 }; + } if (call[0] === "bootstrap" && state.bootstrapError) { return { stdout: "", stderr: state.bootstrapError, code: 1 }; } @@ -71,6 +76,7 @@ vi.mock("node:fs/promises", async (importOriginal) => { beforeEach(() => { state.launchctlCalls.length = 0; state.listOutput = ""; + state.printOutput = ""; state.bootstrapError = ""; state.dirs.clear(); state.files.clear(); @@ -179,6 +185,86 @@ describe("launchd install", () => { expect(plist).toContain(`${tmpDir}`); }); + it("writes crash-only KeepAlive policy with throttle interval", async () => { + const env = createDefaultLaunchdEnv(); + await installLaunchAgent({ + env, + stdout: new PassThrough(), + programArguments: defaultProgramArguments, + }); + + const plistPath = resolveLaunchAgentPlistPath(env); + const plist = state.files.get(plistPath) ?? ""; + expect(plist).toContain("KeepAlive"); + expect(plist).toContain("SuccessfulExit"); + expect(plist).toContain(""); + expect(plist).toContain("ThrottleInterval"); + expect(plist).toContain("5"); + }); + + it("restarts LaunchAgent with bootout-bootstrap-kickstart order", async () => { + const env = createDefaultLaunchdEnv(); + await restartLaunchAgent({ + env, + stdout: new PassThrough(), + }); + + const domain = typeof process.getuid === "function" ? `gui/${process.getuid()}` : "gui/501"; + const label = "ai.openclaw.gateway"; + const plistPath = resolveLaunchAgentPlistPath(env); + const bootoutIndex = state.launchctlCalls.findIndex( + (c) => c[0] === "bootout" && c[1] === `${domain}/${label}`, + ); + const bootstrapIndex = state.launchctlCalls.findIndex( + (c) => c[0] === "bootstrap" && c[1] === domain && c[2] === plistPath, + ); + const kickstartIndex = state.launchctlCalls.findIndex( + (c) => c[0] === "kickstart" && c[1] === "-k" && c[2] === `${domain}/${label}`, + ); + + expect(bootoutIndex).toBeGreaterThanOrEqual(0); + expect(bootstrapIndex).toBeGreaterThanOrEqual(0); + expect(kickstartIndex).toBeGreaterThanOrEqual(0); + expect(bootoutIndex).toBeLessThan(bootstrapIndex); + expect(bootstrapIndex).toBeLessThan(kickstartIndex); + }); + + it("waits for previous launchd pid to exit before bootstrapping", async () => { + const env = createDefaultLaunchdEnv(); + state.printOutput = ["state = running", "pid = 4242"].join("\n"); + const killSpy = vi.spyOn(process, "kill"); + killSpy + .mockImplementationOnce(() => true) + .mockImplementationOnce(() => { + const err = new Error("no such process") as NodeJS.ErrnoException; + err.code = "ESRCH"; + throw err; + }); + + vi.useFakeTimers(); + try { + const restartPromise = restartLaunchAgent({ + env, + stdout: new PassThrough(), + }); + await vi.advanceTimersByTimeAsync(250); + await restartPromise; + expect(killSpy).toHaveBeenCalledWith(4242, 0); + const domain = typeof process.getuid === "function" ? `gui/${process.getuid()}` : "gui/501"; + const label = "ai.openclaw.gateway"; + const bootoutIndex = state.launchctlCalls.findIndex( + (c) => c[0] === "bootout" && c[1] === `${domain}/${label}`, + ); + const bootstrapIndex = state.launchctlCalls.findIndex((c) => c[0] === "bootstrap"); + expect(bootoutIndex).toBeGreaterThanOrEqual(0); + expect(bootstrapIndex).toBeGreaterThanOrEqual(0); + expect(bootoutIndex).toBeLessThan(bootstrapIndex); + } finally { + vi.useRealTimers(); + killSpy.mockRestore(); + } + }); + it("shows actionable guidance when launchctl gui domain does not support bootstrap", async () => { state.bootstrapError = "Bootstrap failed: 125: Domain does not support specified action"; const env = createDefaultLaunchdEnv(); diff --git a/src/daemon/launchd.ts b/src/daemon/launchd.ts index dded364858be..5326413b73dd 100644 --- a/src/daemon/launchd.ts +++ b/src/daemon/launchd.ts @@ -331,6 +331,34 @@ function isUnsupportedGuiDomain(detail: string): boolean { ); } +const RESTART_PID_WAIT_TIMEOUT_MS = 10_000; +const RESTART_PID_WAIT_INTERVAL_MS = 200; + +async function sleepMs(ms: number): Promise { + await new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} + +async function waitForPidExit(pid: number): Promise { + if (!Number.isFinite(pid) || pid <= 1) { + return; + } + const deadline = Date.now() + RESTART_PID_WAIT_TIMEOUT_MS; + while (Date.now() < deadline) { + try { + process.kill(pid, 0); + } catch (err) { + const code = (err as NodeJS.ErrnoException).code; + if (code === "ESRCH" || code === "EPERM") { + return; + } + return; + } + await sleepMs(RESTART_PID_WAIT_INTERVAL_MS); + } +} + export async function stopLaunchAgent({ stdout, env }: GatewayServiceControlArgs): Promise { const domain = resolveGuiDomain(); const label = resolveLaunchAgentLabel({ env }); @@ -418,11 +446,45 @@ export async function restartLaunchAgent({ stdout, env, }: GatewayServiceControlArgs): Promise { + const serviceEnv = env ?? (process.env as GatewayServiceEnv); const domain = resolveGuiDomain(); - const label = resolveLaunchAgentLabel({ env }); - const res = await execLaunchctl(["kickstart", "-k", `${domain}/${label}`]); - if (res.code !== 0) { - throw new Error(`launchctl kickstart failed: ${res.stderr || res.stdout}`.trim()); + const label = resolveLaunchAgentLabel({ env: serviceEnv }); + const plistPath = resolveLaunchAgentPlistPath(serviceEnv); + + const runtime = await execLaunchctl(["print", `${domain}/${label}`]); + const previousPid = + runtime.code === 0 + ? parseLaunchctlPrint(runtime.stdout || runtime.stderr || "").pid + : undefined; + + const stop = await execLaunchctl(["bootout", `${domain}/${label}`]); + if (stop.code !== 0 && !isLaunchctlNotLoaded(stop)) { + throw new Error(`launchctl bootout failed: ${stop.stderr || stop.stdout}`.trim()); + } + if (typeof previousPid === "number") { + await waitForPidExit(previousPid); + } + + const boot = await execLaunchctl(["bootstrap", domain, plistPath]); + if (boot.code !== 0) { + const detail = (boot.stderr || boot.stdout).trim(); + if (isUnsupportedGuiDomain(detail)) { + throw new Error( + [ + `launchctl bootstrap failed: ${detail}`, + `LaunchAgent restart requires a logged-in macOS GUI session for this user (${domain}).`, + "This usually means you are running from SSH/headless context or as the wrong user (including sudo).", + "Fix: sign in to the macOS desktop as the target user and rerun `openclaw gateway restart`.", + "Headless deployments should use a dedicated logged-in user session or a custom LaunchDaemon (not shipped): https://docs.openclaw.ai/gateway", + ].join("\n"), + ); + } + throw new Error(`launchctl bootstrap failed: ${detail}`); + } + + const start = await execLaunchctl(["kickstart", "-k", `${domain}/${label}`]); + if (start.code !== 0) { + throw new Error(`launchctl kickstart failed: ${start.stderr || start.stdout}`.trim()); } try { stdout.write(`${formatLine("Restarted LaunchAgent", `${domain}/${label}`)}\n`); diff --git a/src/daemon/service-env.test.ts b/src/daemon/service-env.test.ts index 31a46c499094..2cfa4cce1de5 100644 --- a/src/daemon/service-env.test.ts +++ b/src/daemon/service-env.test.ts @@ -309,6 +309,26 @@ describe("buildServiceEnvironment", () => { expect(env.OPENCLAW_LAUNCHD_LABEL).toBe("ai.openclaw.work"); } }); + + it("forwards proxy environment variables for launchd/systemd runtime", () => { + const env = buildServiceEnvironment({ + env: { + HOME: "/home/user", + HTTP_PROXY: " http://proxy.local:7890 ", + HTTPS_PROXY: "https://proxy.local:7890", + NO_PROXY: "localhost,127.0.0.1", + http_proxy: "http://proxy.local:7890", + all_proxy: "socks5://proxy.local:1080", + }, + port: 18789, + }); + + expect(env.HTTP_PROXY).toBe("http://proxy.local:7890"); + expect(env.HTTPS_PROXY).toBe("https://proxy.local:7890"); + expect(env.NO_PROXY).toBe("localhost,127.0.0.1"); + expect(env.http_proxy).toBe("http://proxy.local:7890"); + expect(env.all_proxy).toBe("socks5://proxy.local:1080"); + }); }); describe("buildNodeServiceEnvironment", () => { @@ -319,6 +339,19 @@ describe("buildNodeServiceEnvironment", () => { expect(env.HOME).toBe("/home/user"); }); + it("forwards proxy environment variables for node services", () => { + const env = buildNodeServiceEnvironment({ + env: { + HOME: "/home/user", + HTTPS_PROXY: " https://proxy.local:7890 ", + no_proxy: "localhost,127.0.0.1", + }, + }); + + expect(env.HTTPS_PROXY).toBe("https://proxy.local:7890"); + expect(env.no_proxy).toBe("localhost,127.0.0.1"); + }); + it("forwards TMPDIR for node services", () => { const env = buildNodeServiceEnvironment({ env: { HOME: "/home/user", TMPDIR: "/tmp/custom" }, diff --git a/src/daemon/service-env.ts b/src/daemon/service-env.ts index 4925a3376116..458ca515c1df 100644 --- a/src/daemon/service-env.ts +++ b/src/daemon/service-env.ts @@ -25,6 +25,35 @@ type BuildServicePathOptions = MinimalServicePathOptions & { env?: Record; }; +const SERVICE_PROXY_ENV_KEYS = [ + "HTTP_PROXY", + "HTTPS_PROXY", + "NO_PROXY", + "ALL_PROXY", + "http_proxy", + "https_proxy", + "no_proxy", + "all_proxy", +] as const; + +function readServiceProxyEnvironment( + env: Record, +): Record { + const out: Record = {}; + for (const key of SERVICE_PROXY_ENV_KEYS) { + const value = env[key]; + if (typeof value !== "string") { + continue; + } + const trimmed = value.trim(); + if (!trimmed) { + continue; + } + out[key] = trimmed; + } + return out; +} + function addNonEmptyDir(dirs: string[], dir: string | undefined): void { if (dir) { dirs.push(dir); @@ -218,10 +247,12 @@ export function buildServiceEnvironment(params: { const configPath = env.OPENCLAW_CONFIG_PATH; // Keep a usable temp directory for supervised services even when the host env omits TMPDIR. const tmpDir = env.TMPDIR?.trim() || os.tmpdir(); + const proxyEnv = readServiceProxyEnvironment(env); return { HOME: env.HOME, TMPDIR: tmpDir, PATH: buildMinimalServicePath({ env }), + ...proxyEnv, OPENCLAW_PROFILE: profile, OPENCLAW_STATE_DIR: stateDir, OPENCLAW_CONFIG_PATH: configPath, @@ -242,10 +273,12 @@ export function buildNodeServiceEnvironment(params: { const stateDir = env.OPENCLAW_STATE_DIR; const configPath = env.OPENCLAW_CONFIG_PATH; const tmpDir = env.TMPDIR?.trim() || os.tmpdir(); + const proxyEnv = readServiceProxyEnvironment(env); return { HOME: env.HOME, TMPDIR: tmpDir, PATH: buildMinimalServicePath({ env }), + ...proxyEnv, OPENCLAW_STATE_DIR: stateDir, OPENCLAW_CONFIG_PATH: configPath, OPENCLAW_LAUNCHD_LABEL: resolveNodeLaunchAgentLabel(), From 4ebefe647a7c1800a27acf8359734504de724fbe Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 02:52:00 -0500 Subject: [PATCH 142/160] fix(daemon): keep launchd KeepAlive while preserving restart hardening --- CHANGELOG.md | 2 +- src/daemon/launchd-plist.ts | 4 +--- src/daemon/launchd.test.ts | 9 ++++----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21e75f9ab6d3..90f019acc788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Docs: https://docs.openclaw.ai ### Fixes -- Daemon/macOS launchd: forward proxy env vars into supervised service environments, switch LaunchAgent keepalive policy to crash-only with throttling, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. +- Daemon/macOS launchd: forward proxy env vars into supervised service environments, keep LaunchAgent `KeepAlive=true` semantics, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. diff --git a/src/daemon/launchd-plist.ts b/src/daemon/launchd-plist.ts index 7918e1c8a372..e685cd9941cf 100644 --- a/src/daemon/launchd-plist.ts +++ b/src/daemon/launchd-plist.ts @@ -1,7 +1,5 @@ import fs from "node:fs/promises"; -const LAUNCHD_THROTTLE_INTERVAL_SECONDS = 5; - const plistEscape = (value: string): string => value .replaceAll("&", "&") @@ -108,5 +106,5 @@ export function buildLaunchAgentPlist({ ? `\n Comment\n ${plistEscape(comment.trim())}` : ""; const envXml = renderEnvDict(environment); - return `\n\n\n \n Label\n ${plistEscape(label)}\n ${commentXml}\n RunAtLoad\n \n KeepAlive\n \n SuccessfulExit\n \n \n ThrottleInterval\n ${LAUNCHD_THROTTLE_INTERVAL_SECONDS}\n ProgramArguments\n ${argsXml}\n \n ${workingDirXml}\n StandardOutPath\n ${plistEscape(stdoutPath)}\n StandardErrorPath\n ${plistEscape(stderrPath)}${envXml}\n \n\n`; + return `\n\n\n \n Label\n ${plistEscape(label)}\n ${commentXml}\n RunAtLoad\n \n KeepAlive\n \n ProgramArguments\n ${argsXml}\n \n ${workingDirXml}\n StandardOutPath\n ${plistEscape(stdoutPath)}\n StandardErrorPath\n ${plistEscape(stderrPath)}${envXml}\n \n\n`; } diff --git a/src/daemon/launchd.test.ts b/src/daemon/launchd.test.ts index 7465666a158c..ac092536c5a9 100644 --- a/src/daemon/launchd.test.ts +++ b/src/daemon/launchd.test.ts @@ -185,7 +185,7 @@ describe("launchd install", () => { expect(plist).toContain(`${tmpDir}`); }); - it("writes crash-only KeepAlive policy with throttle interval", async () => { + it("writes KeepAlive=true policy", async () => { const env = createDefaultLaunchdEnv(); await installLaunchAgent({ env, @@ -196,10 +196,9 @@ describe("launchd install", () => { const plistPath = resolveLaunchAgentPlistPath(env); const plist = state.files.get(plistPath) ?? ""; expect(plist).toContain("KeepAlive"); - expect(plist).toContain("SuccessfulExit"); - expect(plist).toContain(""); - expect(plist).toContain("ThrottleInterval"); - expect(plist).toContain("5"); + expect(plist).toContain(""); + expect(plist).not.toContain("SuccessfulExit"); + expect(plist).not.toContain("ThrottleInterval"); }); it("restarts LaunchAgent with bootout-bootstrap-kickstart order", async () => { From 39d725f4d3e2dcc4894142433e5999142864bbc5 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 03:24:48 -0500 Subject: [PATCH 143/160] Daemon tests: guard undefined runtime status --- src/daemon/launchd.integration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon/launchd.integration.test.ts b/src/daemon/launchd.integration.test.ts index 423ab3fa1a1c..7afa30ac4bdf 100644 --- a/src/daemon/launchd.integration.test.ts +++ b/src/daemon/launchd.integration.test.ts @@ -45,7 +45,7 @@ async function waitForRunningRuntime(params: { let lastPid: number | undefined; while (Date.now() < deadline) { const runtime = await readLaunchAgentRuntime(params.env); - lastStatus = runtime.status; + lastStatus = runtime.status ?? "unknown"; lastPid = runtime.pid; if ( runtime.status === "running" && From 9915a0be49c7269b52cccf55d8d33970ec0aa30a Mon Sep 17 00:00:00 2001 From: siri Date: Thu, 26 Feb 2026 16:36:27 +0800 Subject: [PATCH 144/160] chore: re-apply QVerisBot fork patches after upstream sync --- extensions/synology-chat/package.json | 2 +- scripts/upstream-auto-sync.sh | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/extensions/synology-chat/package.json b/extensions/synology-chat/package.json index e4474651f07e..9046055d2f9f 100644 --- a/extensions/synology-chat/package.json +++ b/extensions/synology-chat/package.json @@ -22,7 +22,7 @@ "install": { "npmSpec": "@openclaw/synology-chat", "localPath": "extensions/synology-chat", - "defaultChoice": "npm" + "defaultChoice": "local" } } } diff --git a/scripts/upstream-auto-sync.sh b/scripts/upstream-auto-sync.sh index 7a26afd7c836..42715e02fcd6 100755 --- a/scripts/upstream-auto-sync.sh +++ b/scripts/upstream-auto-sync.sh @@ -4,7 +4,8 @@ set -euo pipefail # Idempotent upstream sync script for QVerisBot fork. # # Usage: -# bash scripts/upstream-auto-sync.sh +# bash scripts/upstream-auto-sync.sh # 完整同步 +# bash scripts/upstream-auto-sync.sh --post-merge-only # 冲突解决后只执行 fork 补丁 # # Optional env overrides: # UPSTREAM_REMOTE=upstream @@ -19,6 +20,8 @@ TODAY="$(date +"%Y-%m-%d")" SYNC_BRANCH="${SYNC_BRANCH:-sync/upstream-$TODAY}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +POST_MERGE_ONLY=false +[[ "${1:-}" == "--post-merge-only" ]] && POST_MERGE_ONLY=true die() { echo "ERROR: $*" >&2 @@ -148,6 +151,15 @@ SHIM_PKG # Main # --------------------------------------------------------------------------- +if [[ "$POST_MERGE_ONLY" == "true" ]]; then + ensure_git_repo + echo "==> Post-merge only: applying QVerisBot fork patches" + apply_fork_patches + echo + echo "==> Done." + exit 0 +fi + echo "==> Preflight checks" ensure_git_repo ensure_no_in_progress_ops From 92c309f2e171331a6945c2bc1ff1c019b5c5285b Mon Sep 17 00:00:00 2001 From: yinghaosang Date: Thu, 26 Feb 2026 12:07:29 +0800 Subject: [PATCH 145/160] docs: fix wrong Providers link in configuration examples --- docs/gateway/configuration-examples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gateway/configuration-examples.md b/docs/gateway/configuration-examples.md index abc010ce8fe6..0639dc36e926 100644 --- a/docs/gateway/configuration-examples.md +++ b/docs/gateway/configuration-examples.md @@ -628,4 +628,4 @@ Only enable direct mutable name/email/nick matching with each channel's `dangero - If you set `dmPolicy: "open"`, the matching `allowFrom` list must include `"*"`. - Provider IDs differ (phone numbers, user IDs, channel IDs). Use the provider docs to confirm the format. - Optional sections to add later: `web`, `browser`, `ui`, `discovery`, `canvasHost`, `talk`, `signal`, `imessage`. -- See [Providers](/channels/whatsapp) and [Troubleshooting](/gateway/troubleshooting) for deeper setup notes. +- See [Providers](/providers) and [Troubleshooting](/gateway/troubleshooting) for deeper setup notes. From c289b5ff9f15de7e58174aec34a5af05ac9ceae9 Mon Sep 17 00:00:00 2001 From: Sid Date: Thu, 26 Feb 2026 16:46:36 +0800 Subject: [PATCH 146/160] fix(config): preserve agent-level apiKey/baseUrl during models.json merge (#27293) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 6b4b37b03d74e40e14afc2c55fef24a1e59fb0b3 Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + docs/concepts/models.md | 6 + docs/gateway/configuration-reference.md | 4 + ...ssing-provider-apikey-from-env-var.test.ts | 110 ++++++++++++++++++ src/agents/models-config.ts | 25 +++- src/config/schema.help.ts | 2 +- 6 files changed, 146 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90f019acc788..2e4a3c5c17ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Docs: https://docs.openclaw.ai - Daemon/macOS launchd: forward proxy env vars into supervised service environments, keep LaunchAgent `KeepAlive=true` semantics, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. +- Agents/Models config: preserve agent-level provider `apiKey` and `baseUrl` during merge-mode `models.json` updates when agent values are present. (#27293) thanks @Sid-Qin. ## 2026.2.25 diff --git a/docs/concepts/models.md b/docs/concepts/models.md index ee8f06ecb3d4..b4317273d5c8 100644 --- a/docs/concepts/models.md +++ b/docs/concepts/models.md @@ -207,3 +207,9 @@ mode, pass `--yes` to accept defaults. Custom providers in `models.providers` are written into `models.json` under the agent directory (default `~/.openclaw/agents//models.json`). This file is merged by default unless `models.mode` is set to `replace`. + +Merge mode precedence for matching provider IDs: + +- Non-empty `apiKey`/`baseUrl` already present in the agent `models.json` win. +- Empty or missing agent `apiKey`/`baseUrl` fall back to config `models.providers`. +- Other provider fields are refreshed from config and normalized catalog data. diff --git a/docs/gateway/configuration-reference.md b/docs/gateway/configuration-reference.md index 8d147b23fd70..c548fc973a5f 100644 --- a/docs/gateway/configuration-reference.md +++ b/docs/gateway/configuration-reference.md @@ -1741,6 +1741,10 @@ OpenClaw uses the pi-coding-agent model catalog. Add custom providers via `model - Use `authHeader: true` + `headers` for custom auth needs. - Override agent config root with `OPENCLAW_AGENT_DIR` (or `PI_CODING_AGENT_DIR`). +- Merge precedence for matching provider IDs: + - Non-empty agent `models.json` `apiKey`/`baseUrl` win. + - Empty or missing agent `apiKey`/`baseUrl` fall back to `models.providers` in config. + - Use `models.mode: "replace"` when you want config to fully rewrite `models.json`. ### Provider examples diff --git a/src/agents/models-config.fills-missing-provider-apikey-from-env-var.test.ts b/src/agents/models-config.fills-missing-provider-apikey-from-env-var.test.ts index c26142158e83..4abfa4f1ab48 100644 --- a/src/agents/models-config.fills-missing-provider-apikey-from-env-var.test.ts +++ b/src/agents/models-config.fills-missing-provider-apikey-from-env-var.test.ts @@ -134,6 +134,116 @@ describe("models-config", () => { }); }); + it("preserves non-empty agent apiKey/baseUrl for matching providers in merge mode", async () => { + await withTempHome(async () => { + const agentDir = resolveOpenClawAgentDir(); + await fs.mkdir(agentDir, { recursive: true }); + await fs.writeFile( + path.join(agentDir, "models.json"), + JSON.stringify( + { + providers: { + custom: { + baseUrl: "https://agent.example/v1", + apiKey: "AGENT_KEY", + api: "openai-responses", + models: [{ id: "agent-model", name: "Agent model", input: ["text"] }], + }, + }, + }, + null, + 2, + ), + "utf8", + ); + + await ensureOpenClawModelsJson({ + models: { + mode: "merge", + providers: { + custom: { + baseUrl: "https://config.example/v1", + apiKey: "CONFIG_KEY", + api: "openai-responses", + models: [ + { + id: "config-model", + name: "Config model", + input: ["text"], + reasoning: false, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 8192, + maxTokens: 2048, + }, + ], + }, + }, + }, + }); + + const parsed = await readGeneratedModelsJson<{ + providers: Record; + }>(); + expect(parsed.providers.custom?.apiKey).toBe("AGENT_KEY"); + expect(parsed.providers.custom?.baseUrl).toBe("https://agent.example/v1"); + }); + }); + + it("uses config apiKey/baseUrl when existing agent values are empty", async () => { + await withTempHome(async () => { + const agentDir = resolveOpenClawAgentDir(); + await fs.mkdir(agentDir, { recursive: true }); + await fs.writeFile( + path.join(agentDir, "models.json"), + JSON.stringify( + { + providers: { + custom: { + baseUrl: "", + apiKey: "", + api: "openai-responses", + models: [{ id: "agent-model", name: "Agent model", input: ["text"] }], + }, + }, + }, + null, + 2, + ), + "utf8", + ); + + await ensureOpenClawModelsJson({ + models: { + mode: "merge", + providers: { + custom: { + baseUrl: "https://config.example/v1", + apiKey: "CONFIG_KEY", + api: "openai-responses", + models: [ + { + id: "config-model", + name: "Config model", + input: ["text"], + reasoning: false, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 8192, + maxTokens: 2048, + }, + ], + }, + }, + }, + }); + + const parsed = await readGeneratedModelsJson<{ + providers: Record; + }>(); + expect(parsed.providers.custom?.apiKey).toBe("CONFIG_KEY"); + expect(parsed.providers.custom?.baseUrl).toBe("https://config.example/v1"); + }); + }); + it("refreshes stale explicit moonshot model capabilities from implicit catalog", async () => { await withTempHome(async () => { const prevKey = process.env.MOONSHOT_API_KEY; diff --git a/src/agents/models-config.ts b/src/agents/models-config.ts index 4b38b8243984..3b02737eb4c4 100644 --- a/src/agents/models-config.ts +++ b/src/agents/models-config.ts @@ -142,7 +142,30 @@ export async function ensureOpenClawModelsJson( string, NonNullable[string] >; - mergedProviders = { ...existingProviders, ...providers }; + mergedProviders = {}; + for (const [key, entry] of Object.entries(existingProviders)) { + mergedProviders[key] = entry; + } + for (const [key, newEntry] of Object.entries(providers)) { + const existing = existingProviders[key] as + | (NonNullable[string] & { + apiKey?: string; + baseUrl?: string; + }) + | undefined; + if (existing) { + const preserved: Record = {}; + if (typeof existing.apiKey === "string" && existing.apiKey) { + preserved.apiKey = existing.apiKey; + } + if (typeof existing.baseUrl === "string" && existing.baseUrl) { + preserved.baseUrl = existing.baseUrl; + } + mergedProviders[key] = { ...newEntry, ...preserved }; + } else { + mergedProviders[key] = newEntry; + } + } } } diff --git a/src/config/schema.help.ts b/src/config/schema.help.ts index f32433e13334..c16e25df84aa 100644 --- a/src/config/schema.help.ts +++ b/src/config/schema.help.ts @@ -608,7 +608,7 @@ export const FIELD_HELP: Record = { models: "Model catalog root for provider definitions, merge/replace behavior, and optional Bedrock discovery integration. Keep provider definitions explicit and validated before relying on production failover paths.", "models.mode": - 'Controls provider catalog behavior: "merge" keeps built-ins and overlays your custom providers, while "replace" uses only your configured providers. Keep "merge" unless you intentionally want a strict custom list.', + 'Controls provider catalog behavior: "merge" keeps built-ins and overlays your custom providers, while "replace" uses only your configured providers. In "merge", matching provider IDs preserve non-empty agent models.json apiKey/baseUrl values and fall back to config when agent values are empty or missing.', "models.providers": "Provider map keyed by provider ID containing connection/auth settings and concrete model definitions. Use stable provider keys so references from agents and tooling remain portable across environments.", "models.providers.*.baseUrl": From cf4fe4195767f7bff4b4af73a8a918f9dc187ffa Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:02:39 +0530 Subject: [PATCH 147/160] feat(android): add notifications.list node command --- apps/android/app/src/main/AndroidManifest.xml | 9 + .../java/ai/openclaw/android/NodeRuntime.kt | 5 + .../node/DeviceNotificationListenerService.kt | 171 ++++++++++++++++++ .../android/node/InvokeCommandRegistry.kt | 4 + .../openclaw/android/node/InvokeDispatcher.kt | 5 + .../android/node/NotificationsHandler.kt | 57 ++++++ .../protocol/OpenClawProtocolConstants.kt | 9 + .../android/node/InvokeCommandRegistryTest.kt | 3 + .../protocol/OpenClawProtocolConstantsTest.kt | 5 + 9 files changed, 268 insertions(+) create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/node/DeviceNotificationListenerService.kt create mode 100644 apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt diff --git a/apps/android/app/src/main/AndroidManifest.xml b/apps/android/app/src/main/AndroidManifest.xml index 6b8dd7eedbaf..3d0b27f39e66 100644 --- a/apps/android/app/src/main/AndroidManifest.xml +++ b/apps/android/app/src/main/AndroidManifest.xml @@ -38,6 +38,15 @@ android:name=".NodeForegroundService" android:exported="false" android:foregroundServiceType="dataSync|microphone|mediaProjection" /> + + + + + , +) + +private object DeviceNotificationStore { + private val lock = Any() + private var connected = false + private val byKey = LinkedHashMap() + + fun replace(entries: List) { + synchronized(lock) { + byKey.clear() + for (entry in entries) { + byKey[entry.key] = entry + } + } + } + + fun upsert(entry: DeviceNotificationEntry) { + synchronized(lock) { + byKey[entry.key] = entry + } + } + + fun remove(key: String) { + synchronized(lock) { + byKey.remove(key) + } + } + + fun setConnected(value: Boolean) { + synchronized(lock) { + connected = value + if (!value) { + byKey.clear() + } + } + } + + fun snapshot(enabled: Boolean): DeviceNotificationSnapshot { + val (isConnected, entries) = + synchronized(lock) { + connected to byKey.values.sortedByDescending { it.postTimeMs } + } + return DeviceNotificationSnapshot( + enabled = enabled, + connected = isConnected, + notifications = entries, + ) + } +} + +class DeviceNotificationListenerService : NotificationListenerService() { + override fun onListenerConnected() { + super.onListenerConnected() + DeviceNotificationStore.setConnected(true) + refreshActiveNotifications() + } + + override fun onListenerDisconnected() { + DeviceNotificationStore.setConnected(false) + super.onListenerDisconnected() + } + + override fun onNotificationPosted(sbn: StatusBarNotification?) { + super.onNotificationPosted(sbn) + val entry = sbn?.toEntry() ?: return + DeviceNotificationStore.upsert(entry) + } + + override fun onNotificationRemoved(sbn: StatusBarNotification?) { + super.onNotificationRemoved(sbn) + val key = sbn?.key ?: return + DeviceNotificationStore.remove(key) + } + + private fun refreshActiveNotifications() { + val entries = + runCatching { + activeNotifications + ?.mapNotNull { it.toEntry() } + ?: emptyList() + }.getOrElse { emptyList() } + DeviceNotificationStore.replace(entries) + } + + private fun StatusBarNotification.toEntry(): DeviceNotificationEntry { + val extras = notification.extras + val keyValue = key.takeIf { it.isNotBlank() } ?: "$packageName:$id:$postTime" + val title = sanitizeText(extras?.getCharSequence(Notification.EXTRA_TITLE)) + val body = + sanitizeText(extras?.getCharSequence(Notification.EXTRA_BIG_TEXT)) + ?: sanitizeText(extras?.getCharSequence(Notification.EXTRA_TEXT)) + val subText = sanitizeText(extras?.getCharSequence(Notification.EXTRA_SUB_TEXT)) + return DeviceNotificationEntry( + key = keyValue, + packageName = packageName, + title = title, + text = body, + subText = subText, + category = notification.category?.trim()?.ifEmpty { null }, + channelId = notification.channelId?.trim()?.ifEmpty { null }, + postTimeMs = postTime, + isOngoing = isOngoing, + isClearable = isClearable, + ) + } + + private fun sanitizeText(value: CharSequence?): String? { + val normalized = value?.toString()?.trim().orEmpty() + if (normalized.isEmpty()) { + return null + } + return if (normalized.length <= MAX_NOTIFICATION_TEXT_CHARS) { + normalized + } else { + normalized.take(MAX_NOTIFICATION_TEXT_CHARS) + } + } + + companion object { + private fun serviceComponent(context: Context): ComponentName { + return ComponentName(context, DeviceNotificationListenerService::class.java) + } + + fun isAccessEnabled(context: Context): Boolean { + val manager = context.getSystemService(NotificationManager::class.java) ?: return false + return manager.isNotificationListenerAccessGranted(serviceComponent(context)) + } + + fun snapshot(context: Context): DeviceNotificationSnapshot { + return DeviceNotificationStore.snapshot(enabled = isAccessEnabled(context)) + } + + fun requestServiceRebind(context: Context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + return + } + runCatching { + NotificationListenerService.requestRebind(serviceComponent(context)) + } + } + } +} diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt index 812ecf2ba4ef..ce87525904fe 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeCommandRegistry.kt @@ -4,6 +4,7 @@ import ai.openclaw.android.protocol.OpenClawCanvasA2UICommand import ai.openclaw.android.protocol.OpenClawCanvasCommand import ai.openclaw.android.protocol.OpenClawCameraCommand import ai.openclaw.android.protocol.OpenClawLocationCommand +import ai.openclaw.android.protocol.OpenClawNotificationsCommand import ai.openclaw.android.protocol.OpenClawScreenCommand import ai.openclaw.android.protocol.OpenClawSmsCommand @@ -74,6 +75,9 @@ object InvokeCommandRegistry { name = OpenClawLocationCommand.Get.rawValue, availability = InvokeCommandAvailability.LocationEnabled, ), + InvokeCommandSpec( + name = OpenClawNotificationsCommand.List.rawValue, + ), InvokeCommandSpec( name = OpenClawSmsCommand.Send.rawValue, availability = InvokeCommandAvailability.SmsAvailable, diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt index d293df76668c..936ad7b3d111 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/InvokeDispatcher.kt @@ -5,6 +5,7 @@ import ai.openclaw.android.protocol.OpenClawCanvasA2UICommand import ai.openclaw.android.protocol.OpenClawCanvasCommand import ai.openclaw.android.protocol.OpenClawCameraCommand import ai.openclaw.android.protocol.OpenClawLocationCommand +import ai.openclaw.android.protocol.OpenClawNotificationsCommand import ai.openclaw.android.protocol.OpenClawScreenCommand import ai.openclaw.android.protocol.OpenClawSmsCommand @@ -12,6 +13,7 @@ class InvokeDispatcher( private val canvas: CanvasController, private val cameraHandler: CameraHandler, private val locationHandler: LocationHandler, + private val notificationsHandler: NotificationsHandler, private val screenHandler: ScreenHandler, private val smsHandler: SmsHandler, private val a2uiHandler: A2UIHandler, @@ -114,6 +116,9 @@ class InvokeDispatcher( // Location command OpenClawLocationCommand.Get.rawValue -> locationHandler.handleLocationGet(paramsJson) + // Notifications command + OpenClawNotificationsCommand.List.rawValue -> notificationsHandler.handleNotificationsList(paramsJson) + // Screen command OpenClawScreenCommand.Record.rawValue -> screenHandler.handleScreenRecord(paramsJson) diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt new file mode 100644 index 000000000000..17123d936742 --- /dev/null +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt @@ -0,0 +1,57 @@ +package ai.openclaw.android.node + +import android.content.Context +import ai.openclaw.android.gateway.GatewaySession +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.put + +class NotificationsHandler( + private val appContext: Context, +) { + suspend fun handleNotificationsList(_paramsJson: String?): GatewaySession.InvokeResult { + if (!DeviceNotificationListenerService.isAccessEnabled(appContext)) { + return GatewaySession.InvokeResult.error( + code = "NOTIFICATION_LISTENER_DISABLED", + message = + "NOTIFICATION_LISTENER_DISABLED: enable Notification Access for OpenClaw in system settings", + ) + } + val snapshot = DeviceNotificationListenerService.snapshot(appContext) + if (!snapshot.connected) { + DeviceNotificationListenerService.requestServiceRebind(appContext) + return GatewaySession.InvokeResult.error( + code = "NOTIFICATION_LISTENER_UNAVAILABLE", + message = "NOTIFICATION_LISTENER_UNAVAILABLE: listener is reconnecting; retry shortly", + ) + } + + val payload = + buildJsonObject { + put("enabled", JsonPrimitive(snapshot.enabled)) + put("connected", JsonPrimitive(snapshot.connected)) + put("count", JsonPrimitive(snapshot.notifications.size)) + put( + "notifications", + JsonArray( + snapshot.notifications.map { entry -> + buildJsonObject { + put("key", JsonPrimitive(entry.key)) + put("packageName", JsonPrimitive(entry.packageName)) + put("postTimeMs", JsonPrimitive(entry.postTimeMs)) + put("isOngoing", JsonPrimitive(entry.isOngoing)) + put("isClearable", JsonPrimitive(entry.isClearable)) + entry.title?.let { put("title", JsonPrimitive(it)) } + entry.text?.let { put("text", JsonPrimitive(it)) } + entry.subText?.let { put("subText", JsonPrimitive(it)) } + entry.category?.let { put("category", JsonPrimitive(it)) } + entry.channelId?.let { put("channelId", JsonPrimitive(it)) } + } + }, + ), + ) + } + return GatewaySession.InvokeResult.ok(payload.toString()) + } +} diff --git a/apps/android/app/src/main/java/ai/openclaw/android/protocol/OpenClawProtocolConstants.kt b/apps/android/app/src/main/java/ai/openclaw/android/protocol/OpenClawProtocolConstants.kt index ccca40c4c353..d73c61d233b4 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/protocol/OpenClawProtocolConstants.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/protocol/OpenClawProtocolConstants.kt @@ -69,3 +69,12 @@ enum class OpenClawLocationCommand(val rawValue: String) { const val NamespacePrefix: String = "location." } } + +enum class OpenClawNotificationsCommand(val rawValue: String) { + List("notifications.list"), + ; + + companion object { + const val NamespacePrefix: String = "notifications." + } +} diff --git a/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt index 65b18656708f..88795b0d9cec 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/node/InvokeCommandRegistryTest.kt @@ -2,6 +2,7 @@ package ai.openclaw.android.node import ai.openclaw.android.protocol.OpenClawCameraCommand import ai.openclaw.android.protocol.OpenClawLocationCommand +import ai.openclaw.android.protocol.OpenClawNotificationsCommand import ai.openclaw.android.protocol.OpenClawSmsCommand import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue @@ -21,6 +22,7 @@ class InvokeCommandRegistryTest { assertFalse(commands.contains(OpenClawCameraCommand.Snap.rawValue)) assertFalse(commands.contains(OpenClawCameraCommand.Clip.rawValue)) assertFalse(commands.contains(OpenClawLocationCommand.Get.rawValue)) + assertTrue(commands.contains(OpenClawNotificationsCommand.List.rawValue)) assertFalse(commands.contains(OpenClawSmsCommand.Send.rawValue)) assertFalse(commands.contains("debug.logs")) assertFalse(commands.contains("debug.ed25519")) @@ -40,6 +42,7 @@ class InvokeCommandRegistryTest { assertTrue(commands.contains(OpenClawCameraCommand.Snap.rawValue)) assertTrue(commands.contains(OpenClawCameraCommand.Clip.rawValue)) assertTrue(commands.contains(OpenClawLocationCommand.Get.rawValue)) + assertTrue(commands.contains(OpenClawNotificationsCommand.List.rawValue)) assertTrue(commands.contains(OpenClawSmsCommand.Send.rawValue)) assertTrue(commands.contains("debug.logs")) assertTrue(commands.contains("debug.ed25519")) diff --git a/apps/android/app/src/test/java/ai/openclaw/android/protocol/OpenClawProtocolConstantsTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/protocol/OpenClawProtocolConstantsTest.kt index 10ab733ae53f..71eec189509e 100644 --- a/apps/android/app/src/test/java/ai/openclaw/android/protocol/OpenClawProtocolConstantsTest.kt +++ b/apps/android/app/src/test/java/ai/openclaw/android/protocol/OpenClawProtocolConstantsTest.kt @@ -32,4 +32,9 @@ class OpenClawProtocolConstantsTest { fun screenCommandsUseStableStrings() { assertEquals("screen.record", OpenClawScreenCommand.Record.rawValue) } + + @Test + fun notificationsCommandsUseStableStrings() { + assertEquals("notifications.list", OpenClawNotificationsCommand.List.rawValue) + } } From e6a5d5784ca248aacca547c62b7e580b26dbae61 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:02:43 +0530 Subject: [PATCH 148/160] feat(gateway): allow notifications.list for android nodes --- src/gateway/gateway-misc.test.ts | 13 +++++++++++++ src/gateway/node-command-policy.ts | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/gateway/gateway-misc.test.ts b/src/gateway/gateway-misc.test.ts index a202e4b2915c..e6f65ed1b773 100644 --- a/src/gateway/gateway-misc.test.ts +++ b/src/gateway/gateway-misc.test.ts @@ -334,6 +334,19 @@ describe("resolveNodeCommandAllowlist", () => { } }); + it("includes Android notifications.list by default", () => { + const allow = resolveNodeCommandAllowlist( + {}, + { + platform: "android 16", + deviceFamily: "Android", + }, + ); + + expect(allow.has("notifications.list")).toBe(true); + expect(allow.has("system.notify")).toBe(false); + }); + it("can explicitly allow dangerous commands via allowCommands", () => { const allow = resolveNodeCommandAllowlist( { diff --git a/src/gateway/node-command-policy.ts b/src/gateway/node-command-policy.ts index ec829b0c5f6c..68eb8bb2835a 100644 --- a/src/gateway/node-command-policy.ts +++ b/src/gateway/node-command-policy.ts @@ -18,6 +18,7 @@ const CAMERA_DANGEROUS_COMMANDS = ["camera.snap", "camera.clip"]; const SCREEN_DANGEROUS_COMMANDS = ["screen.record"]; const LOCATION_COMMANDS = ["location.get"]; +const NOTIFICATION_COMMANDS = ["notifications.list"]; const DEVICE_COMMANDS = ["device.info", "device.status"]; @@ -69,6 +70,7 @@ const PLATFORM_DEFAULTS: Record = { ...CANVAS_COMMANDS, ...CAMERA_COMMANDS, ...LOCATION_COMMANDS, + ...NOTIFICATION_COMMANDS, ...DEVICE_COMMANDS, ...CONTACTS_COMMANDS, ...CALENDAR_COMMANDS, From c0073b3d47b2794b1e3b8f30f587160c0acf2eab Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:02:51 +0530 Subject: [PATCH 149/160] feat(agents): add nodes notifications_list action --- src/agents/openclaw-tools.camera.test.ts | 36 ++++++++++++++++++++++++ src/agents/tools/nodes-tool.ts | 14 ++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/agents/openclaw-tools.camera.test.ts b/src/agents/openclaw-tools.camera.test.ts index 6b1d2e35c335..96be774b2974 100644 --- a/src/agents/openclaw-tools.camera.test.ts +++ b/src/agents/openclaw-tools.camera.test.ts @@ -138,6 +138,42 @@ describe("nodes camera_snap", () => { }); }); +describe("nodes notifications_list", () => { + it("invokes notifications.list and returns payload", async () => { + callGateway.mockImplementation(async ({ method, params }) => { + if (method === "node.list") { + return mockNodeList(["notifications.list"]); + } + if (method === "node.invoke") { + expect(params).toMatchObject({ + nodeId: NODE_ID, + command: "notifications.list", + params: {}, + }); + return { + payload: { + enabled: true, + connected: true, + count: 1, + notifications: [{ key: "n1", packageName: "com.example.app" }], + }, + }; + } + return unexpectedGatewayMethod(method); + }); + + const result = await executeNodes({ + action: "notifications_list", + node: NODE_ID, + }); + + expect(result.content?.[0]).toMatchObject({ + type: "text", + text: expect.stringContaining('"notifications"'), + }); + }); +}); + describe("nodes run", () => { it("passes invoke and command timeouts", async () => { callGateway.mockImplementation(async ({ method, params }) => { diff --git a/src/agents/tools/nodes-tool.ts b/src/agents/tools/nodes-tool.ts index 3006b9cfddc5..b0dfef3eeeda 100644 --- a/src/agents/tools/nodes-tool.ts +++ b/src/agents/tools/nodes-tool.ts @@ -40,6 +40,7 @@ const NODES_TOOL_ACTIONS = [ "camera_clip", "screen_record", "location_get", + "notifications_list", "run", "invoke", ] as const; @@ -122,7 +123,7 @@ export function createNodesTool(options?: { label: "Nodes", name: "nodes", description: - "Discover and control paired nodes (status/describe/pairing/notify/camera/screen/location/run/invoke).", + "Discover and control paired nodes (status/describe/pairing/notify/camera/screen/location/notifications/run/invoke).", parameters: NodesToolSchema, execute: async (_toolCallId, args) => { const params = args as Record; @@ -406,6 +407,17 @@ export function createNodesTool(options?: { }); return jsonResult(raw?.payload ?? {}); } + case "notifications_list": { + const node = readStringParam(params, "node", { required: true }); + const nodeId = await resolveNodeId(gatewayOpts, node); + const raw = await callGatewayTool<{ payload: unknown }>("node.invoke", gatewayOpts, { + nodeId, + command: "notifications.list", + params: {}, + idempotencyKey: crypto.randomUUID(), + }); + return jsonResult(raw?.payload ?? {}); + } case "run": { const node = readStringParam(params, "node", { required: true }); const nodes = await listNodes(gatewayOpts); From 05817187fee1bd5a82f05a44c932c82ac1649253 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:16:31 +0530 Subject: [PATCH 150/160] refactor(android): unify notifications.list status flow --- .../node/DeviceNotificationListenerService.kt | 29 ++-- .../android/node/NotificationsHandler.kt | 104 ++++++++----- .../android/node/NotificationsHandlerTest.kt | 146 ++++++++++++++++++ 3 files changed, 221 insertions(+), 58 deletions(-) create mode 100644 apps/android/app/src/test/java/ai/openclaw/android/node/NotificationsHandlerTest.kt diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/DeviceNotificationListenerService.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/DeviceNotificationListenerService.kt index e585e5643b64..709e9af5ec54 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/DeviceNotificationListenerService.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/DeviceNotificationListenerService.kt @@ -10,6 +10,11 @@ import android.service.notification.StatusBarNotification private const val MAX_NOTIFICATION_TEXT_CHARS = 512 +internal fun sanitizeNotificationText(value: CharSequence?): String? { + val normalized = value?.toString()?.trim().orEmpty() + return normalized.take(MAX_NOTIFICATION_TEXT_CHARS).ifEmpty { null } +} + data class DeviceNotificationEntry( val key: String, val packageName: String, @@ -114,11 +119,11 @@ class DeviceNotificationListenerService : NotificationListenerService() { private fun StatusBarNotification.toEntry(): DeviceNotificationEntry { val extras = notification.extras val keyValue = key.takeIf { it.isNotBlank() } ?: "$packageName:$id:$postTime" - val title = sanitizeText(extras?.getCharSequence(Notification.EXTRA_TITLE)) + val title = sanitizeNotificationText(extras?.getCharSequence(Notification.EXTRA_TITLE)) val body = - sanitizeText(extras?.getCharSequence(Notification.EXTRA_BIG_TEXT)) - ?: sanitizeText(extras?.getCharSequence(Notification.EXTRA_TEXT)) - val subText = sanitizeText(extras?.getCharSequence(Notification.EXTRA_SUB_TEXT)) + sanitizeNotificationText(extras?.getCharSequence(Notification.EXTRA_BIG_TEXT)) + ?: sanitizeNotificationText(extras?.getCharSequence(Notification.EXTRA_TEXT)) + val subText = sanitizeNotificationText(extras?.getCharSequence(Notification.EXTRA_SUB_TEXT)) return DeviceNotificationEntry( key = keyValue, packageName = packageName, @@ -133,18 +138,6 @@ class DeviceNotificationListenerService : NotificationListenerService() { ) } - private fun sanitizeText(value: CharSequence?): String? { - val normalized = value?.toString()?.trim().orEmpty() - if (normalized.isEmpty()) { - return null - } - return if (normalized.length <= MAX_NOTIFICATION_TEXT_CHARS) { - normalized - } else { - normalized.take(MAX_NOTIFICATION_TEXT_CHARS) - } - } - companion object { private fun serviceComponent(context: Context): ComponentName { return ComponentName(context, DeviceNotificationListenerService::class.java) @@ -155,8 +148,8 @@ class DeviceNotificationListenerService : NotificationListenerService() { return manager.isNotificationListenerAccessGranted(serviceComponent(context)) } - fun snapshot(context: Context): DeviceNotificationSnapshot { - return DeviceNotificationStore.snapshot(enabled = isAccessEnabled(context)) + fun snapshot(context: Context, enabled: Boolean = isAccessEnabled(context)): DeviceNotificationSnapshot { + return DeviceNotificationStore.snapshot(enabled = enabled) } fun requestServiceRebind(context: Context) { diff --git a/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt b/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt index 17123d936742..0216e19208cf 100644 --- a/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt +++ b/apps/android/app/src/main/java/ai/openclaw/android/node/NotificationsHandler.kt @@ -7,51 +7,75 @@ import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put -class NotificationsHandler( +internal interface NotificationsStateProvider { + fun readSnapshot(context: Context): DeviceNotificationSnapshot + + fun requestServiceRebind(context: Context) +} + +private object SystemNotificationsStateProvider : NotificationsStateProvider { + override fun readSnapshot(context: Context): DeviceNotificationSnapshot { + val enabled = DeviceNotificationListenerService.isAccessEnabled(context) + if (!enabled) { + return DeviceNotificationSnapshot( + enabled = false, + connected = false, + notifications = emptyList(), + ) + } + return DeviceNotificationListenerService.snapshot(context, enabled = true) + } + + override fun requestServiceRebind(context: Context) { + DeviceNotificationListenerService.requestServiceRebind(context) + } +} + +class NotificationsHandler private constructor( private val appContext: Context, + private val stateProvider: NotificationsStateProvider, ) { + constructor(appContext: Context) : this(appContext = appContext, stateProvider = SystemNotificationsStateProvider) + suspend fun handleNotificationsList(_paramsJson: String?): GatewaySession.InvokeResult { - if (!DeviceNotificationListenerService.isAccessEnabled(appContext)) { - return GatewaySession.InvokeResult.error( - code = "NOTIFICATION_LISTENER_DISABLED", - message = - "NOTIFICATION_LISTENER_DISABLED: enable Notification Access for OpenClaw in system settings", - ) + val snapshot = stateProvider.readSnapshot(appContext) + if (snapshot.enabled && !snapshot.connected) { + stateProvider.requestServiceRebind(appContext) } - val snapshot = DeviceNotificationListenerService.snapshot(appContext) - if (!snapshot.connected) { - DeviceNotificationListenerService.requestServiceRebind(appContext) - return GatewaySession.InvokeResult.error( - code = "NOTIFICATION_LISTENER_UNAVAILABLE", - message = "NOTIFICATION_LISTENER_UNAVAILABLE: listener is reconnecting; retry shortly", + return GatewaySession.InvokeResult.ok(snapshotPayloadJson(snapshot)) + } + + private fun snapshotPayloadJson(snapshot: DeviceNotificationSnapshot): String { + return buildJsonObject { + put("enabled", JsonPrimitive(snapshot.enabled)) + put("connected", JsonPrimitive(snapshot.connected)) + put("count", JsonPrimitive(snapshot.notifications.size)) + put( + "notifications", + JsonArray( + snapshot.notifications.map { entry -> + buildJsonObject { + put("key", JsonPrimitive(entry.key)) + put("packageName", JsonPrimitive(entry.packageName)) + put("postTimeMs", JsonPrimitive(entry.postTimeMs)) + put("isOngoing", JsonPrimitive(entry.isOngoing)) + put("isClearable", JsonPrimitive(entry.isClearable)) + entry.title?.let { put("title", JsonPrimitive(it)) } + entry.text?.let { put("text", JsonPrimitive(it)) } + entry.subText?.let { put("subText", JsonPrimitive(it)) } + entry.category?.let { put("category", JsonPrimitive(it)) } + entry.channelId?.let { put("channelId", JsonPrimitive(it)) } + } + }, + ), ) - } + }.toString() + } - val payload = - buildJsonObject { - put("enabled", JsonPrimitive(snapshot.enabled)) - put("connected", JsonPrimitive(snapshot.connected)) - put("count", JsonPrimitive(snapshot.notifications.size)) - put( - "notifications", - JsonArray( - snapshot.notifications.map { entry -> - buildJsonObject { - put("key", JsonPrimitive(entry.key)) - put("packageName", JsonPrimitive(entry.packageName)) - put("postTimeMs", JsonPrimitive(entry.postTimeMs)) - put("isOngoing", JsonPrimitive(entry.isOngoing)) - put("isClearable", JsonPrimitive(entry.isClearable)) - entry.title?.let { put("title", JsonPrimitive(it)) } - entry.text?.let { put("text", JsonPrimitive(it)) } - entry.subText?.let { put("subText", JsonPrimitive(it)) } - entry.category?.let { put("category", JsonPrimitive(it)) } - entry.channelId?.let { put("channelId", JsonPrimitive(it)) } - } - }, - ), - ) - } - return GatewaySession.InvokeResult.ok(payload.toString()) + companion object { + internal fun forTesting( + appContext: Context, + stateProvider: NotificationsStateProvider, + ): NotificationsHandler = NotificationsHandler(appContext = appContext, stateProvider = stateProvider) } } diff --git a/apps/android/app/src/test/java/ai/openclaw/android/node/NotificationsHandlerTest.kt b/apps/android/app/src/test/java/ai/openclaw/android/node/NotificationsHandlerTest.kt new file mode 100644 index 000000000000..7768e6e25dab --- /dev/null +++ b/apps/android/app/src/test/java/ai/openclaw/android/node/NotificationsHandlerTest.kt @@ -0,0 +1,146 @@ +package ai.openclaw.android.node + +import android.content.Context +import ai.openclaw.android.gateway.GatewaySession +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.int +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class NotificationsHandlerTest { + @Test + fun notificationsListReturnsStatusPayloadWhenDisabled() = + runTest { + val provider = + FakeNotificationsStateProvider( + DeviceNotificationSnapshot( + enabled = false, + connected = false, + notifications = emptyList(), + ), + ) + val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider) + + val result = handler.handleNotificationsList(null) + + assertTrue(result.ok) + assertNull(result.error) + val payload = parsePayload(result) + assertFalse(payload.getValue("enabled").jsonPrimitive.boolean) + assertFalse(payload.getValue("connected").jsonPrimitive.boolean) + assertEquals(0, payload.getValue("count").jsonPrimitive.int) + assertEquals(0, payload.getValue("notifications").jsonArray.size) + assertEquals(0, provider.rebindRequests) + } + + @Test + fun notificationsListRequestsRebindWhenEnabledButDisconnected() = + runTest { + val provider = + FakeNotificationsStateProvider( + DeviceNotificationSnapshot( + enabled = true, + connected = false, + notifications = listOf(sampleEntry("n1")), + ), + ) + val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider) + + val result = handler.handleNotificationsList(null) + + assertTrue(result.ok) + assertNull(result.error) + val payload = parsePayload(result) + assertTrue(payload.getValue("enabled").jsonPrimitive.boolean) + assertFalse(payload.getValue("connected").jsonPrimitive.boolean) + assertEquals(1, payload.getValue("count").jsonPrimitive.int) + assertEquals(1, payload.getValue("notifications").jsonArray.size) + assertEquals(1, provider.rebindRequests) + } + + @Test + fun notificationsListDoesNotRequestRebindWhenConnected() = + runTest { + val provider = + FakeNotificationsStateProvider( + DeviceNotificationSnapshot( + enabled = true, + connected = true, + notifications = listOf(sampleEntry("n2")), + ), + ) + val handler = NotificationsHandler.forTesting(appContext = appContext(), stateProvider = provider) + + val result = handler.handleNotificationsList(null) + + assertTrue(result.ok) + assertNull(result.error) + val payload = parsePayload(result) + assertTrue(payload.getValue("enabled").jsonPrimitive.boolean) + assertTrue(payload.getValue("connected").jsonPrimitive.boolean) + assertEquals(1, payload.getValue("count").jsonPrimitive.int) + assertEquals(0, provider.rebindRequests) + } + + @Test + fun sanitizeNotificationTextReturnsNullForBlankInput() { + assertNull(sanitizeNotificationText(null)) + assertNull(sanitizeNotificationText(" ")) + } + + @Test + fun sanitizeNotificationTextTrimsAndTruncates() { + val value = " ${"x".repeat(600)} " + val sanitized = sanitizeNotificationText(value) + + assertEquals(512, sanitized?.length) + assertTrue((sanitized ?: "").all { it == 'x' }) + } + + private fun parsePayload(result: GatewaySession.InvokeResult): JsonObject { + val payloadJson = result.payloadJson ?: error("expected payload") + return Json.parseToJsonElement(payloadJson).jsonObject + } + + private fun appContext(): Context = RuntimeEnvironment.getApplication() + + private fun sampleEntry(key: String): DeviceNotificationEntry = + DeviceNotificationEntry( + key = key, + packageName = "com.example.app", + title = "Title", + text = "Text", + subText = null, + category = null, + channelId = null, + postTimeMs = 123L, + isOngoing = false, + isClearable = true, + ) +} + +private class FakeNotificationsStateProvider( + private val snapshot: DeviceNotificationSnapshot, +) : NotificationsStateProvider { + var rebindRequests: Int = 0 + private set + + override fun readSnapshot(context: Context): DeviceNotificationSnapshot = snapshot + + override fun requestServiceRebind(context: Context) { + rebindRequests += 1 + } +} From a0cf753b2e7e8b8b459450353b4b8c46a7580b61 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:16:34 +0530 Subject: [PATCH 151/160] refactor(agents): dedupe node read invoke commands --- src/agents/tools/nodes-tool.ts | 48 +++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/agents/tools/nodes-tool.ts b/src/agents/tools/nodes-tool.ts index b0dfef3eeeda..25b194033528 100644 --- a/src/agents/tools/nodes-tool.ts +++ b/src/agents/tools/nodes-tool.ts @@ -49,6 +49,23 @@ const NOTIFY_PRIORITIES = ["passive", "active", "timeSensitive"] as const; const NOTIFY_DELIVERIES = ["system", "overlay", "auto"] as const; const CAMERA_FACING = ["front", "back", "both"] as const; const LOCATION_ACCURACY = ["coarse", "balanced", "precise"] as const; +type GatewayCallOptions = ReturnType; + +async function invokeNodeCommandPayload(params: { + gatewayOpts: GatewayCallOptions; + node: string; + command: string; + commandParams?: Record; +}): Promise { + const nodeId = await resolveNodeId(params.gatewayOpts, params.node); + const raw = await callGatewayTool<{ payload: unknown }>("node.invoke", params.gatewayOpts, { + nodeId, + command: params.command, + params: params.commandParams ?? {}, + idempotencyKey: crypto.randomUUID(), + }); + return raw?.payload ?? {}; +} function isPairingRequiredMessage(message: string): boolean { const lower = message.toLowerCase(); @@ -273,15 +290,13 @@ export function createNodesTool(options?: { } case "camera_list": { const node = readStringParam(params, "node", { required: true }); - const nodeId = await resolveNodeId(gatewayOpts, node); - const raw = await callGatewayTool<{ payload: unknown }>("node.invoke", gatewayOpts, { - nodeId, + const payloadRaw = await invokeNodeCommandPayload({ + gatewayOpts, + node, command: "camera.list", - params: {}, - idempotencyKey: crypto.randomUUID(), }); const payload = - raw && typeof raw.payload === "object" && raw.payload !== null ? raw.payload : {}; + payloadRaw && typeof payloadRaw === "object" && payloadRaw !== null ? payloadRaw : {}; return jsonResult(payload); } case "camera_clip": { @@ -379,7 +394,6 @@ export function createNodesTool(options?: { } case "location_get": { const node = readStringParam(params, "node", { required: true }); - const nodeId = await resolveNodeId(gatewayOpts, node); const maxAgeMs = typeof params.maxAgeMs === "number" && Number.isFinite(params.maxAgeMs) ? params.maxAgeMs @@ -395,28 +409,26 @@ export function createNodesTool(options?: { Number.isFinite(params.locationTimeoutMs) ? params.locationTimeoutMs : undefined; - const raw = await callGatewayTool<{ payload: unknown }>("node.invoke", gatewayOpts, { - nodeId, + const payload = await invokeNodeCommandPayload({ + gatewayOpts, + node, command: "location.get", - params: { + commandParams: { maxAgeMs, desiredAccuracy, timeoutMs: locationTimeoutMs, }, - idempotencyKey: crypto.randomUUID(), }); - return jsonResult(raw?.payload ?? {}); + return jsonResult(payload); } case "notifications_list": { const node = readStringParam(params, "node", { required: true }); - const nodeId = await resolveNodeId(gatewayOpts, node); - const raw = await callGatewayTool<{ payload: unknown }>("node.invoke", gatewayOpts, { - nodeId, + const payload = await invokeNodeCommandPayload({ + gatewayOpts, + node, command: "notifications.list", - params: {}, - idempotencyKey: crypto.randomUUID(), }); - return jsonResult(raw?.payload ?? {}); + return jsonResult(payload); } case "run": { const node = readStringParam(params, "node", { required: true }); From da6a96ed334610968c7179e4b74ea55984cb8827 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:33:00 +0530 Subject: [PATCH 152/160] fix: update changelog for notifications list land (#27344) (thanks @obviyus) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e4a3c5c17ba..6490add55874 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Docs: https://docs.openclaw.ai ### Changes - Agents/Routing CLI: add `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` for account-scoped route management, including channel-only to account-scoped binding upgrades, role-aware binding identity handling, plugin-resolved binding account IDs, and optional account-binding prompts in `openclaw channels add`. (#27195) thanks @gumadeiras. +- Android/Nodes: add `notifications.list` support on Android nodes and expose `nodes notifications_list` in agent tooling for listing active device notifications. (#27344) thanks @obviyus. ### Fixes From dfa0b5b4fc724432f37e9830269cd2558fd36df6 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 04:06:03 -0500 Subject: [PATCH 153/160] Channels: move single-account config into accounts.default (#27334) Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 50b57718085368d302680ec93fab67f5ed6140a4 Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Reviewed-by: @gumadeiras --- CHANGELOG.md | 1 + docs/cli/channels.md | 10 ++ docs/cli/index.md | 2 + docs/gateway/configuration-reference.md | 3 + docs/gateway/doctor.md | 1 + .../plugins/onboarding/helpers.test.ts | 33 ++++++ src/channels/plugins/onboarding/helpers.ts | 19 ++- src/channels/plugins/setup-helpers.ts | 112 ++++++++++++++++++ ....adds-non-default-telegram-account.test.ts | 90 ++++++++++++++ src/commands/channels/add.ts | 8 ++ ...fault-account-bindings.integration.test.ts | 56 +++++++++ ...w.missing-default-account-bindings.test.ts | 89 ++++++++++++++ src/commands/doctor-config-flow.ts | 105 ++++++++++++++++ .../doctor-legacy-config.migrations.test.ts | 44 ++++++- src/commands/doctor-legacy-config.ts | 73 ++++++++++++ 15 files changed, 639 insertions(+), 7 deletions(-) create mode 100644 src/commands/doctor-config-flow.missing-default-account-bindings.integration.test.ts create mode 100644 src/commands/doctor-config-flow.missing-default-account-bindings.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6490add55874..844fe8eb636d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Channels/Multi-account config: when adding a non-default channel account to a single-account top-level channel setup, move existing account-scoped top-level single-account values into `channels..accounts.default` before writing the new account so the original account keeps working without duplicated account values at channel root; `openclaw doctor --fix` now repairs previously mixed channel account shapes the same way. (#27334) thanks @gumadeiras. - Daemon/macOS launchd: forward proxy env vars into supervised service environments, keep LaunchAgent `KeepAlive=true` semantics, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. diff --git a/docs/cli/channels.md b/docs/cli/channels.md index 0f9c3fecb774..23e0b2cfd4be 100644 --- a/docs/cli/channels.md +++ b/docs/cli/channels.md @@ -45,6 +45,16 @@ If you confirm bind now, the wizard asks which agent should own each configured You can also manage the same routing rules later with `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` (see [agents](/cli/agents)). +When you add a non-default account to a channel that is still using single-account top-level settings (no `channels..accounts` entries yet), OpenClaw moves account-scoped single-account top-level values into `channels..accounts.default`, then writes the new account. This preserves the original account behavior while moving to the multi-account shape. + +Routing behavior stays consistent: + +- Existing channel-only bindings (no `accountId`) continue to match the default account. +- `channels add` does not auto-create or rewrite bindings in non-interactive mode. +- Interactive setup can optionally add account-scoped bindings. + +If your config was already in a mixed state (named accounts present, missing `default`, and top-level single-account values still set), run `openclaw doctor --fix` to move account-scoped values into `accounts.default`. + ## Login / logout (interactive) ```bash diff --git a/docs/cli/index.md b/docs/cli/index.md index 1394d83db0e3..a780dfd2a5ed 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -400,6 +400,8 @@ Subcommands: - Tip: `channels status` prints warnings with suggested fixes when it can detect common misconfigurations (then points you to `openclaw doctor`). - `channels logs`: show recent channel logs from the gateway log file. - `channels add`: wizard-style setup when no flags are passed; flags switch to non-interactive mode. + - When adding a non-default account to a channel still using single-account top-level config, OpenClaw moves account-scoped values into `channels..accounts.default` before writing the new account. + - Non-interactive `channels add` does not auto-create/upgrade bindings; channel-only bindings continue to match the default account. - `channels remove`: disable by default; pass `--delete` to remove config entries without prompts. - `channels login`: interactive channel login (WhatsApp Web only). - `channels logout`: log out of a channel session (if supported). diff --git a/docs/gateway/configuration-reference.md b/docs/gateway/configuration-reference.md index c548fc973a5f..a715ec89ba62 100644 --- a/docs/gateway/configuration-reference.md +++ b/docs/gateway/configuration-reference.md @@ -505,6 +505,9 @@ Run multiple accounts per channel (each with its own `accountId`): - Env tokens only apply to the **default** account. - Base channel settings apply to all accounts unless overridden per account. - Use `bindings[].match.accountId` to route each account to a different agent. +- If you add a non-default account via `openclaw channels add` (or channel onboarding) while still on a single-account top-level channel config, OpenClaw moves account-scoped top-level single-account values into `channels..accounts.default` first so the original account keeps working. +- Existing channel-only bindings (no `accountId`) keep matching the default account; account-scoped bindings remain optional. +- `openclaw doctor --fix` also repairs mixed shapes by moving account-scoped top-level single-account values into `accounts.default` when named accounts exist but `default` is missing. ### Group chat mention gating diff --git a/docs/gateway/doctor.md b/docs/gateway/doctor.md index 4647cb8b411b..4ecc10b4c66f 100644 --- a/docs/gateway/doctor.md +++ b/docs/gateway/doctor.md @@ -121,6 +121,7 @@ Current migrations: - `routing.agentToAgent` → `tools.agentToAgent` - `routing.transcribeAudio` → `tools.media.audio.models` - `bindings[].match.accountID` → `bindings[].match.accountId` +- For channels with named `accounts` but missing `accounts.default`, move account-scoped top-level single-account channel values into `channels..accounts.default` when present - `identity` → `agents.list[].identity` - `agent.*` → `agents.defaults` + `tools.*` (tools/elevated/exec/sandbox/subagents) - `agent.model`/`allowedModels`/`modelAliases`/`modelFallbacks`/`imageModelFallbacks` diff --git a/src/channels/plugins/onboarding/helpers.test.ts b/src/channels/plugins/onboarding/helpers.test.ts index cecb55181544..b209be558f52 100644 --- a/src/channels/plugins/onboarding/helpers.test.ts +++ b/src/channels/plugins/onboarding/helpers.test.ts @@ -554,6 +554,39 @@ describe("patchChannelConfigForAccount", () => { expect(next.channels?.slack?.accounts?.work?.appToken).toBe("new-app"); }); + it("moves single-account config into default account when patching non-default", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + enabled: true, + botToken: "legacy-token", + allowFrom: ["100"], + groupPolicy: "allowlist", + streaming: "partial", + }, + }, + }; + + const next = patchChannelConfigForAccount({ + cfg, + channel: "telegram", + accountId: "work", + patch: { botToken: "work-token" }, + }); + + expect(next.channels?.telegram?.accounts?.default).toEqual({ + botToken: "legacy-token", + allowFrom: ["100"], + groupPolicy: "allowlist", + streaming: "partial", + }); + expect(next.channels?.telegram?.botToken).toBeUndefined(); + expect(next.channels?.telegram?.allowFrom).toBeUndefined(); + expect(next.channels?.telegram?.groupPolicy).toBeUndefined(); + expect(next.channels?.telegram?.streaming).toBeUndefined(); + expect(next.channels?.telegram?.accounts?.work?.botToken).toBe("work-token"); + }); + it("supports imessage/signal account-scoped channel patches", () => { const cfg: OpenClawConfig = { channels: { diff --git a/src/channels/plugins/onboarding/helpers.ts b/src/channels/plugins/onboarding/helpers.ts index 258aa7b6782f..7a1b92001ad9 100644 --- a/src/channels/plugins/onboarding/helpers.ts +++ b/src/channels/plugins/onboarding/helpers.ts @@ -4,6 +4,7 @@ import { promptAccountId as promptAccountIdSdk } from "../../../plugin-sdk/onboa import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../../routing/session-key.js"; import type { WizardPrompter } from "../../../wizard/prompts.js"; import type { PromptAccountId, PromptAccountIdParams } from "../onboarding-types.js"; +import { moveSingleAccountChannelSectionToDefaultAccount } from "../setup-helpers.js"; export const promptAccountId: PromptAccountId = async (params: PromptAccountIdParams) => { return await promptAccountIdSdk(params); @@ -282,13 +283,21 @@ function patchConfigForScopedAccount(params: { ensureEnabled: boolean; }): OpenClawConfig { const { cfg, channel, accountId, patch, ensureEnabled } = params; - const channelConfig = (cfg.channels?.[channel] as Record | undefined) ?? {}; + const seededCfg = + accountId === DEFAULT_ACCOUNT_ID + ? cfg + : moveSingleAccountChannelSectionToDefaultAccount({ + cfg, + channelKey: channel, + }); + const channelConfig = + (seededCfg.channels?.[channel] as Record | undefined) ?? {}; if (accountId === DEFAULT_ACCOUNT_ID) { return { - ...cfg, + ...seededCfg, channels: { - ...cfg.channels, + ...seededCfg.channels, [channel]: { ...channelConfig, ...(ensureEnabled ? { enabled: true } : {}), @@ -303,9 +312,9 @@ function patchConfigForScopedAccount(params: { const existingAccount = accounts[accountId] ?? {}; return { - ...cfg, + ...seededCfg, channels: { - ...cfg.channels, + ...seededCfg.channels, [channel]: { ...channelConfig, ...(ensureEnabled ? { enabled: true } : {}), diff --git a/src/channels/plugins/setup-helpers.ts b/src/channels/plugins/setup-helpers.ts index c6a695b1e8d7..72b3163a62e8 100644 --- a/src/channels/plugins/setup-helpers.ts +++ b/src/channels/plugins/setup-helpers.ts @@ -119,3 +119,115 @@ export function migrateBaseNameToDefaultAccount(params: { }, } as OpenClawConfig; } + +type ChannelSectionRecord = Record & { + accounts?: Record>; +}; + +const COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE = new Set([ + "name", + "token", + "tokenFile", + "botToken", + "appToken", + "account", + "signalNumber", + "authDir", + "cliPath", + "dbPath", + "httpUrl", + "httpHost", + "httpPort", + "webhookPath", + "webhookUrl", + "webhookSecret", + "service", + "region", + "homeserver", + "userId", + "accessToken", + "password", + "deviceName", + "url", + "code", + "dmPolicy", + "allowFrom", + "groupPolicy", + "groupAllowFrom", + "defaultTo", +]); + +const SINGLE_ACCOUNT_KEYS_TO_MOVE_BY_CHANNEL: Record> = { + telegram: new Set(["streaming"]), +}; + +export function shouldMoveSingleAccountChannelKey(params: { + channelKey: string; + key: string; +}): boolean { + if (COMMON_SINGLE_ACCOUNT_KEYS_TO_MOVE.has(params.key)) { + return true; + } + return SINGLE_ACCOUNT_KEYS_TO_MOVE_BY_CHANNEL[params.channelKey]?.has(params.key) ?? false; +} + +function cloneIfObject(value: T): T { + if (value && typeof value === "object") { + return structuredClone(value); + } + return value; +} + +// When promoting a single-account channel config to multi-account, +// move top-level account settings into accounts.default so the original +// account keeps working without duplicate account values at channel root. +export function moveSingleAccountChannelSectionToDefaultAccount(params: { + cfg: OpenClawConfig; + channelKey: string; +}): OpenClawConfig { + const channels = params.cfg.channels as Record | undefined; + const baseConfig = channels?.[params.channelKey]; + const base = + typeof baseConfig === "object" && baseConfig ? (baseConfig as ChannelSectionRecord) : undefined; + if (!base) { + return params.cfg; + } + + const accounts = base.accounts ?? {}; + if (Object.keys(accounts).length > 0) { + return params.cfg; + } + + const keysToMove = Object.entries(base) + .filter( + ([key, value]) => + key !== "accounts" && + key !== "enabled" && + value !== undefined && + shouldMoveSingleAccountChannelKey({ channelKey: params.channelKey, key }), + ) + .map(([key]) => key); + const defaultAccount: Record = {}; + for (const key of keysToMove) { + const value = base[key]; + defaultAccount[key] = cloneIfObject(value); + } + const nextChannel: ChannelSectionRecord = { ...base }; + for (const key of keysToMove) { + delete nextChannel[key]; + } + + return { + ...params.cfg, + channels: { + ...params.cfg.channels, + [params.channelKey]: { + ...nextChannel, + accounts: { + ...accounts, + [DEFAULT_ACCOUNT_ID]: defaultAccount, + }, + }, + }, + } as OpenClawConfig; +} diff --git a/src/commands/channels.adds-non-default-telegram-account.test.ts b/src/commands/channels.adds-non-default-telegram-account.test.ts index 0187675788d2..3df9fc110610 100644 --- a/src/commands/channels.adds-non-default-telegram-account.test.ts +++ b/src/commands/channels.adds-non-default-telegram-account.test.ts @@ -66,6 +66,96 @@ describe("channels command", () => { expect(next.channels?.telegram?.accounts?.alerts?.botToken).toBe("123:abc"); }); + it("moves single-account telegram config into accounts.default when adding non-default", async () => { + configMocks.readConfigFileSnapshot.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + channels: { + telegram: { + enabled: true, + botToken: "legacy-token", + dmPolicy: "allowlist", + allowFrom: ["111"], + groupPolicy: "allowlist", + streaming: "partial", + }, + }, + }, + }); + + await channelsAddCommand( + { channel: "telegram", account: "alerts", token: "alerts-token" }, + runtime, + { hasFlags: true }, + ); + + const next = configMocks.writeConfigFile.mock.calls[0]?.[0] as { + channels?: { + telegram?: { + botToken?: string; + dmPolicy?: string; + allowFrom?: string[]; + groupPolicy?: string; + streaming?: string; + accounts?: Record< + string, + { + botToken?: string; + dmPolicy?: string; + allowFrom?: string[]; + groupPolicy?: string; + streaming?: string; + } + >; + }; + }; + }; + expect(next.channels?.telegram?.accounts?.default).toEqual({ + botToken: "legacy-token", + dmPolicy: "allowlist", + allowFrom: ["111"], + groupPolicy: "allowlist", + streaming: "partial", + }); + expect(next.channels?.telegram?.botToken).toBeUndefined(); + expect(next.channels?.telegram?.dmPolicy).toBeUndefined(); + expect(next.channels?.telegram?.allowFrom).toBeUndefined(); + expect(next.channels?.telegram?.groupPolicy).toBeUndefined(); + expect(next.channels?.telegram?.streaming).toBeUndefined(); + expect(next.channels?.telegram?.accounts?.alerts?.botToken).toBe("alerts-token"); + }); + + it("seeds accounts.default for env-only single-account telegram config when adding non-default", async () => { + configMocks.readConfigFileSnapshot.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + channels: { + telegram: { + enabled: true, + }, + }, + }, + }); + + await channelsAddCommand( + { channel: "telegram", account: "alerts", token: "alerts-token" }, + runtime, + { hasFlags: true }, + ); + + const next = configMocks.writeConfigFile.mock.calls[0]?.[0] as { + channels?: { + telegram?: { + enabled?: boolean; + accounts?: Record; + }; + }; + }; + expect(next.channels?.telegram?.enabled).toBe(true); + expect(next.channels?.telegram?.accounts?.default).toEqual({}); + expect(next.channels?.telegram?.accounts?.alerts?.botToken).toBe("alerts-token"); + }); + it("adds a default slack account with tokens", async () => { configMocks.readConfigFileSnapshot.mockResolvedValue({ ...baseConfigSnapshot }); await channelsAddCommand( diff --git a/src/commands/channels/add.ts b/src/commands/channels/add.ts index eaa6fc533976..882e7f16ca55 100644 --- a/src/commands/channels/add.ts +++ b/src/commands/channels/add.ts @@ -1,6 +1,7 @@ import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js"; import { listChannelPluginCatalogEntries } from "../../channels/plugins/catalog.js"; import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js"; +import { moveSingleAccountChannelSectionToDefaultAccount } from "../../channels/plugins/setup-helpers.js"; import type { ChannelId, ChannelSetupInput } from "../../channels/plugins/types.js"; import { writeConfigFile, type OpenClawConfig } from "../../config/config.js"; import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js"; @@ -283,6 +284,13 @@ export async function channelsAddCommand( ? resolveTelegramAccount({ cfg: nextConfig, accountId }).token.trim() : ""; + if (accountId !== DEFAULT_ACCOUNT_ID) { + nextConfig = moveSingleAccountChannelSectionToDefaultAccount({ + cfg: nextConfig, + channelKey: channel, + }); + } + nextConfig = applyChannelAccountConfig({ cfg: nextConfig, channel, diff --git a/src/commands/doctor-config-flow.missing-default-account-bindings.integration.test.ts b/src/commands/doctor-config-flow.missing-default-account-bindings.integration.test.ts new file mode 100644 index 000000000000..0856b3aa9b5c --- /dev/null +++ b/src/commands/doctor-config-flow.missing-default-account-bindings.integration.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it, vi } from "vitest"; +import { withEnvAsync } from "../test-utils/env.js"; +import { runDoctorConfigWithInput } from "./doctor-config-flow.test-utils.js"; + +const { noteSpy } = vi.hoisted(() => ({ + noteSpy: vi.fn(), +})); + +vi.mock("../terminal/note.js", () => ({ + note: noteSpy, +})); + +vi.mock("./doctor-legacy-config.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + normalizeLegacyConfigValues: (cfg: unknown) => ({ + config: cfg, + changes: [], + }), + }; +}); + +import { loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow.js"; + +describe("doctor missing default account binding warning", () => { + it("emits a doctor warning when named accounts have no valid account-scoped bindings", async () => { + await withEnvAsync( + { + TELEGRAM_BOT_TOKEN: undefined, + TELEGRAM_BOT_TOKEN_FILE: undefined, + }, + async () => { + await runDoctorConfigWithInput({ + config: { + channels: { + telegram: { + accounts: { + alerts: {}, + work: {}, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram" } }], + }, + run: loadAndMaybeMigrateDoctorConfig, + }); + }, + ); + + expect(noteSpy).toHaveBeenCalledWith( + expect.stringContaining("channels.telegram: accounts.default is missing"), + "Doctor warnings", + ); + }); +}); diff --git a/src/commands/doctor-config-flow.missing-default-account-bindings.test.ts b/src/commands/doctor-config-flow.missing-default-account-bindings.test.ts new file mode 100644 index 000000000000..6a47ab1f962a --- /dev/null +++ b/src/commands/doctor-config-flow.missing-default-account-bindings.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from "vitest"; +import type { OpenClawConfig } from "../config/config.js"; +import { collectMissingDefaultAccountBindingWarnings } from "./doctor-config-flow.js"; + +describe("collectMissingDefaultAccountBindingWarnings", () => { + it("warns when named accounts exist without default and no valid binding exists", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + accounts: { + alerts: { botToken: "a" }, + work: { botToken: "w" }, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram" } }], + }; + + const warnings = collectMissingDefaultAccountBindingWarnings(cfg); + expect(warnings).toHaveLength(1); + expect(warnings[0]).toContain("channels.telegram"); + expect(warnings[0]).toContain("alerts, work"); + }); + + it("does not warn when an explicit account binding exists", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + accounts: { + alerts: { botToken: "a" }, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram", accountId: "alerts" } }], + }; + + expect(collectMissingDefaultAccountBindingWarnings(cfg)).toEqual([]); + }); + + it("warns when bindings cover only a subset of configured accounts", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + accounts: { + alerts: { botToken: "a" }, + work: { botToken: "w" }, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram", accountId: "alerts" } }], + }; + + const warnings = collectMissingDefaultAccountBindingWarnings(cfg); + expect(warnings).toHaveLength(1); + expect(warnings[0]).toContain("subset"); + expect(warnings[0]).toContain("Uncovered accounts: work"); + }); + + it("does not warn when wildcard account binding exists", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + accounts: { + alerts: { botToken: "a" }, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram", accountId: "*" } }], + }; + + expect(collectMissingDefaultAccountBindingWarnings(cfg)).toEqual([]); + }); + + it("does not warn when default account is present", () => { + const cfg: OpenClawConfig = { + channels: { + telegram: { + accounts: { + default: { botToken: "d" }, + alerts: { botToken: "a" }, + }, + }, + }, + bindings: [{ agentId: "ops", match: { channel: "telegram" } }], + }; + + expect(collectMissingDefaultAccountBindingWarnings(cfg)).toEqual([]); + }); +}); diff --git a/src/commands/doctor-config-flow.ts b/src/commands/doctor-config-flow.ts index f4a7e4132a8d..fffa67bff4d2 100644 --- a/src/commands/doctor-config-flow.ts +++ b/src/commands/doctor-config-flow.ts @@ -1,6 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import type { ZodIssue } from "zod"; +import { normalizeChatChannelId } from "../channels/registry.js"; import { isNumericTelegramUserId, normalizeTelegramAllowFromEntry, @@ -27,6 +28,7 @@ import { isTrustedSafeBinPath, normalizeTrustedSafeBinDirs, } from "../infra/exec-safe-bin-trust.js"; +import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js"; import { isDiscordMutableAllowEntry, isGoogleChatMutableAllowEntry, @@ -207,6 +209,103 @@ function asObjectRecord(value: unknown): Record | null { return value as Record; } +function normalizeBindingChannelKey(raw?: string | null): string { + const normalized = normalizeChatChannelId(raw); + if (normalized) { + return normalized; + } + return (raw ?? "").trim().toLowerCase(); +} + +export function collectMissingDefaultAccountBindingWarnings(cfg: OpenClawConfig): string[] { + const channels = asObjectRecord(cfg.channels); + if (!channels) { + return []; + } + + const bindings = Array.isArray(cfg.bindings) ? cfg.bindings : []; + const warnings: string[] = []; + + for (const [channelKey, rawChannel] of Object.entries(channels)) { + const channel = asObjectRecord(rawChannel); + if (!channel) { + continue; + } + const accounts = asObjectRecord(channel.accounts); + if (!accounts) { + continue; + } + + const normalizedAccountIds = Array.from( + new Set( + Object.keys(accounts) + .map((accountId) => normalizeAccountId(accountId)) + .filter(Boolean), + ), + ); + if (normalizedAccountIds.length === 0 || normalizedAccountIds.includes(DEFAULT_ACCOUNT_ID)) { + continue; + } + const accountIdSet = new Set(normalizedAccountIds); + const channelPattern = normalizeBindingChannelKey(channelKey); + + let hasWildcardBinding = false; + const coveredAccountIds = new Set(); + for (const binding of bindings) { + const bindingRecord = asObjectRecord(binding); + if (!bindingRecord) { + continue; + } + const match = asObjectRecord(bindingRecord.match); + if (!match) { + continue; + } + + const matchChannel = + typeof match.channel === "string" ? normalizeBindingChannelKey(match.channel) : ""; + if (!matchChannel || matchChannel !== channelPattern) { + continue; + } + + const rawAccountId = typeof match.accountId === "string" ? match.accountId.trim() : ""; + if (!rawAccountId) { + continue; + } + if (rawAccountId === "*") { + hasWildcardBinding = true; + continue; + } + const normalizedBindingAccountId = normalizeAccountId(rawAccountId); + if (accountIdSet.has(normalizedBindingAccountId)) { + coveredAccountIds.add(normalizedBindingAccountId); + } + } + + if (hasWildcardBinding) { + continue; + } + + const uncoveredAccountIds = normalizedAccountIds.filter( + (accountId) => !coveredAccountIds.has(accountId), + ); + if (uncoveredAccountIds.length === 0) { + continue; + } + if (coveredAccountIds.size > 0) { + warnings.push( + `- channels.${channelKey}: accounts.default is missing and account bindings only cover a subset of configured accounts. Uncovered accounts: ${uncoveredAccountIds.join(", ")}. Add bindings[].match.accountId for uncovered accounts (or "*"), or add channels.${channelKey}.accounts.default.`, + ); + continue; + } + + warnings.push( + `- channels.${channelKey}: accounts.default is missing and no valid account-scoped binding exists for configured accounts (${normalizedAccountIds.join(", ")}). Channel-only bindings (no accountId) match only default. Add bindings[].match.accountId for one of these accounts (or "*"), or add channels.${channelKey}.accounts.default.`, + ); + } + + return warnings; +} + function collectTelegramAccountScopes( cfg: OpenClawConfig, ): Array<{ prefix: string; account: Record }> { @@ -1421,6 +1520,12 @@ export async function loadAndMaybeMigrateDoctorConfig(params: { } } + const missingDefaultAccountBindingWarnings = + collectMissingDefaultAccountBindingWarnings(candidate); + if (missingDefaultAccountBindingWarnings.length > 0) { + note(missingDefaultAccountBindingWarnings.join("\n"), "Doctor warnings"); + } + if (shouldRepair) { const repair = await maybeRepairTelegramAllowFromUsernames(candidate); if (repair.changes.length > 0) { diff --git a/src/commands/doctor-legacy-config.migrations.test.ts b/src/commands/doctor-legacy-config.migrations.test.ts index a626371c8e3a..775966bae1d9 100644 --- a/src/commands/doctor-legacy-config.migrations.test.ts +++ b/src/commands/doctor-legacy-config.migrations.test.ts @@ -164,10 +164,12 @@ describe("normalizeLegacyConfigValues", () => { expect(res.config.channels?.discord?.streamMode).toBeUndefined(); expect(res.config.channels?.discord?.accounts?.work?.streaming).toBe("off"); expect(res.config.channels?.discord?.accounts?.work?.streamMode).toBeUndefined(); - expect(res.changes).toEqual([ + expect(res.changes).toContain( "Normalized channels.discord.streaming boolean → enum (partial).", + ); + expect(res.changes).toContain( "Normalized channels.discord.accounts.work.streaming boolean → enum (off).", - ]); + ); }); it("migrates Discord legacy streamMode into streaming enum", () => { @@ -223,6 +225,44 @@ describe("normalizeLegacyConfigValues", () => { ]); }); + it("moves missing default account from single-account top-level config when named accounts already exist", () => { + const res = normalizeLegacyConfigValues({ + channels: { + telegram: { + enabled: true, + botToken: "legacy-token", + dmPolicy: "allowlist", + allowFrom: ["123"], + groupPolicy: "allowlist", + streaming: "partial", + accounts: { + alerts: { + enabled: true, + botToken: "alerts-token", + }, + }, + }, + }, + }); + + expect(res.config.channels?.telegram?.accounts?.default).toEqual({ + botToken: "legacy-token", + dmPolicy: "allowlist", + allowFrom: ["123"], + groupPolicy: "allowlist", + streaming: "partial", + }); + expect(res.config.channels?.telegram?.botToken).toBeUndefined(); + expect(res.config.channels?.telegram?.dmPolicy).toBeUndefined(); + expect(res.config.channels?.telegram?.allowFrom).toBeUndefined(); + expect(res.config.channels?.telegram?.groupPolicy).toBeUndefined(); + expect(res.config.channels?.telegram?.streaming).toBeUndefined(); + expect(res.config.channels?.telegram?.accounts?.alerts?.botToken).toBe("alerts-token"); + expect(res.changes).toContain( + "Moved channels.telegram single-account top-level values into channels.telegram.accounts.default.", + ); + }); + it("migrates browser ssrfPolicy allowPrivateNetwork to dangerouslyAllowPrivateNetwork", () => { const res = normalizeLegacyConfigValues({ browser: { diff --git a/src/commands/doctor-legacy-config.ts b/src/commands/doctor-legacy-config.ts index 6f84067ca621..35cd5fba277b 100644 --- a/src/commands/doctor-legacy-config.ts +++ b/src/commands/doctor-legacy-config.ts @@ -1,3 +1,4 @@ +import { shouldMoveSingleAccountChannelKey } from "../channels/plugins/setup-helpers.js"; import type { OpenClawConfig } from "../config/config.js"; import { resolveDiscordPreviewStreamMode, @@ -5,6 +6,7 @@ import { resolveSlackStreamingMode, resolveTelegramPreviewStreamMode, } from "../config/discord-preview-streaming.js"; +import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js"; export function normalizeLegacyConfigValues(cfg: OpenClawConfig): { config: OpenClawConfig; @@ -289,9 +291,80 @@ export function normalizeLegacyConfigValues(cfg: OpenClawConfig): { } }; + const seedMissingDefaultAccountsFromSingleAccountBase = () => { + const channels = next.channels as Record | undefined; + if (!channels) { + return; + } + + let channelsChanged = false; + const nextChannels = { ...channels }; + for (const [channelId, rawChannel] of Object.entries(channels)) { + if (!isRecord(rawChannel)) { + continue; + } + const rawAccounts = rawChannel.accounts; + if (!isRecord(rawAccounts)) { + continue; + } + const accountKeys = Object.keys(rawAccounts); + if (accountKeys.length === 0) { + continue; + } + const hasDefault = accountKeys.some((key) => key.trim().toLowerCase() === DEFAULT_ACCOUNT_ID); + if (hasDefault) { + continue; + } + + const keysToMove = Object.entries(rawChannel) + .filter( + ([key, value]) => + key !== "accounts" && + key !== "enabled" && + value !== undefined && + shouldMoveSingleAccountChannelKey({ channelKey: channelId, key }), + ) + .map(([key]) => key); + if (keysToMove.length === 0) { + continue; + } + + const defaultAccount: Record = {}; + for (const key of keysToMove) { + const value = rawChannel[key]; + defaultAccount[key] = value && typeof value === "object" ? structuredClone(value) : value; + } + const nextChannel: Record = { + ...rawChannel, + }; + for (const key of keysToMove) { + delete nextChannel[key]; + } + nextChannel.accounts = { + ...rawAccounts, + [DEFAULT_ACCOUNT_ID]: defaultAccount, + }; + + nextChannels[channelId] = nextChannel; + channelsChanged = true; + changes.push( + `Moved channels.${channelId} single-account top-level values into channels.${channelId}.accounts.default.`, + ); + } + + if (!channelsChanged) { + return; + } + next = { + ...next, + channels: nextChannels as OpenClawConfig["channels"], + }; + }; + normalizeProvider("telegram"); normalizeProvider("slack"); normalizeProvider("discord"); + seedMissingDefaultAccountsFromSingleAccountBase(); const normalizeBrowserSsrFPolicyAlias = () => { const rawBrowser = next.browser; From 58fef1d70319362e5443041230ab71329d7c5805 Mon Sep 17 00:00:00 2001 From: GodsBoy Date: Thu, 26 Feb 2026 10:33:57 +0200 Subject: [PATCH 154/160] fix(telegram): allow inline button callbacks in groups when command was authorized (#27309) --- src/telegram/bot-handlers.ts | 4 +++- src/telegram/bot.test.ts | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/telegram/bot-handlers.ts b/src/telegram/bot-handlers.ts index ad28c32883d7..33626ff475aa 100644 --- a/src/telegram/bot-handlers.ts +++ b/src/telegram/bot-handlers.ts @@ -536,7 +536,9 @@ export const registerTelegramHandlers = ({ }, "callback-allowlist": { enforceDirectAuthorization: true, - enforceGroupAllowlistAuthorization: true, + // Group auth is already enforced by shouldSkipGroupMessage (group policy + allowlist). + // An extra allowlist gate here would block users whose original command was authorized. + enforceGroupAllowlistAuthorization: false, deniedDmReason: "callback unauthorized by inlineButtonsScope allowlist", deniedGroupReason: "callback unauthorized by inlineButtonsScope allowlist", }, diff --git a/src/telegram/bot.test.ts b/src/telegram/bot.test.ts index e7e326d0e36d..2ffcc489baf7 100644 --- a/src/telegram/bot.test.ts +++ b/src/telegram/bot.test.ts @@ -193,6 +193,50 @@ describe("createTelegramBot", () => { expect(answerCallbackQuerySpy).toHaveBeenCalledWith("cbq-2"); }); + it("allows callback_query in groups when group policy authorizes the sender", async () => { + onSpy.mockClear(); + editMessageTextSpy.mockClear(); + listSkillCommandsForAgents.mockClear(); + + createTelegramBot({ + token: "tok", + config: { + channels: { + telegram: { + dmPolicy: "open", + capabilities: { inlineButtons: "allowlist" }, + allowFrom: [], + groupPolicy: "open", + groups: { "*": { requireMention: false } }, + }, + }, + }, + }); + const callbackHandler = onSpy.mock.calls.find((call) => call[0] === "callback_query")?.[1] as ( + ctx: Record, + ) => Promise; + expect(callbackHandler).toBeDefined(); + + await callbackHandler({ + callbackQuery: { + id: "cbq-group-1", + data: "commands_page_2", + from: { id: 42, first_name: "Ada", username: "ada_bot" }, + message: { + chat: { id: -100999, type: "supergroup", title: "Test Group" }, + date: 1736380800, + message_id: 20, + }, + }, + me: { username: "openclaw_bot" }, + getFile: async () => ({ download: async () => new Uint8Array() }), + }); + + // The callback should be processed (not silently blocked) + expect(editMessageTextSpy).toHaveBeenCalledTimes(1); + expect(answerCallbackQuerySpy).toHaveBeenCalledWith("cbq-group-1"); + }); + it("edits commands list for pagination callbacks", async () => { onSpy.mockClear(); listSkillCommandsForAgents.mockClear(); From 0e3ed28950a383c3f4ce4591763881889dd7516c Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:42:47 +0530 Subject: [PATCH 155/160] fix: changelog for telegram group inline callbacks (#27343) (thanks @GodsBoy) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 844fe8eb636d..7cd03a4b1d11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Docs: https://docs.openclaw.ai ### Fixes - Channels/Multi-account config: when adding a non-default channel account to a single-account top-level channel setup, move existing account-scoped top-level single-account values into `channels..accounts.default` before writing the new account so the original account keeps working without duplicated account values at channel root; `openclaw doctor --fix` now repairs previously mixed channel account shapes the same way. (#27334) thanks @gumadeiras. +- Telegram/Inline buttons: allow callback-query button handling in groups (including `/models` follow-up buttons) when group policy authorizes the sender, by removing the redundant callback allowlist gate that blocked open-policy groups. (#27343) Thanks @GodsBoy. - Daemon/macOS launchd: forward proxy env vars into supervised service environments, keep LaunchAgent `KeepAlive=true` semantics, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. From 30fd2bbe195ab944ab649bdb0288bdd3f77f1af0 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Thu, 26 Feb 2026 14:48:35 +0530 Subject: [PATCH 156/160] fix(ssrf): honor global family policy for pinned dispatcher --- src/infra/net/ssrf.dispatcher.test.ts | 8 +++++--- src/infra/net/ssrf.ts | 2 -- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/infra/net/ssrf.dispatcher.test.ts b/src/infra/net/ssrf.dispatcher.test.ts index 0dfb816aa008..aaccebc17379 100644 --- a/src/infra/net/ssrf.dispatcher.test.ts +++ b/src/infra/net/ssrf.dispatcher.test.ts @@ -13,7 +13,7 @@ vi.mock("undici", () => ({ import { createPinnedDispatcher, type PinnedHostname } from "./ssrf.js"; describe("createPinnedDispatcher", () => { - it("enables network family auto-selection for pinned lookups", () => { + it("uses pinned lookup without overriding global family policy", () => { const lookup = vi.fn() as unknown as PinnedHostname["lookup"]; const pinned: PinnedHostname = { hostname: "api.telegram.org", @@ -27,9 +27,11 @@ describe("createPinnedDispatcher", () => { expect(agentCtor).toHaveBeenCalledWith({ connect: { lookup, - autoSelectFamily: true, - autoSelectFamilyAttemptTimeout: 300, }, }); + const firstCallArg = agentCtor.mock.calls[0]?.[0] as + | { connect?: Record } + | undefined; + expect(firstCallArg?.connect?.autoSelectFamily).toBeUndefined(); }); }); diff --git a/src/infra/net/ssrf.ts b/src/infra/net/ssrf.ts index 8ba29b38e2ae..7798e5990a44 100644 --- a/src/infra/net/ssrf.ts +++ b/src/infra/net/ssrf.ts @@ -333,8 +333,6 @@ export function createPinnedDispatcher(pinned: PinnedHostname): Dispatcher { return new Agent({ connect: { lookup: pinned.lookup, - autoSelectFamily: true, - autoSelectFamilyAttemptTimeout: 300, }, }); } From a690b62391cb6c3f5d4b507b828aa97bb96c3171 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 04:35:08 -0500 Subject: [PATCH 157/160] Doctor: ignore slash sessions in transcript integrity check Merged via deterministic merge flow. Prepared head SHA: e5cee7a2eca80e9a61021b323190786ef6a016bd Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com> --- CHANGELOG.md | 1 + src/commands/doctor-state-integrity.test.ts | 24 +++++++++++++++++++++ src/commands/doctor-state-integrity.ts | 15 +++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cd03a4b1d11..0a28452ec998 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- Doctor/State integrity: ignore metadata-only slash routing sessions when checking recent missing transcripts so `openclaw doctor` no longer reports false-positive transcript-missing warnings for `*:slash:*` keys. (#27375) thanks @gumadeiras. - Channels/Multi-account config: when adding a non-default channel account to a single-account top-level channel setup, move existing account-scoped top-level single-account values into `channels..accounts.default` before writing the new account so the original account keeps working without duplicated account values at channel root; `openclaw doctor --fix` now repairs previously mixed channel account shapes the same way. (#27334) thanks @gumadeiras. - Telegram/Inline buttons: allow callback-query button handling in groups (including `/models` follow-up buttons) when group policy authorizes the sender, by removing the redundant callback allowlist gate that blocked open-policy groups. (#27343) Thanks @GodsBoy. - Daemon/macOS launchd: forward proxy env vars into supervised service environments, keep LaunchAgent `KeepAlive=true` semantics, and harden restart sequencing to `print -> bootout -> wait old pid exit -> bootstrap -> kickstart`. (#27276) thanks @frankekn. diff --git a/src/commands/doctor-state-integrity.test.ts b/src/commands/doctor-state-integrity.test.ts index ba889d28bdf6..a9d28e3971bc 100644 --- a/src/commands/doctor-state-integrity.test.ts +++ b/src/commands/doctor-state-integrity.test.ts @@ -171,4 +171,28 @@ describe("doctor state integrity oauth dir checks", () => { expect(text).not.toContain("--active"); expect(text).not.toContain(" ls "); }); + + it("ignores slash-routing sessions for recent missing transcript warnings", async () => { + const cfg: OpenClawConfig = {}; + setupSessionState(cfg, process.env, process.env.HOME ?? ""); + const storePath = resolveStorePath(cfg.session?.store, { agentId: "main" }); + fs.writeFileSync( + storePath, + JSON.stringify( + { + "agent:main:telegram:slash:6790081233": { + sessionId: "missing-slash-transcript", + updatedAt: Date.now(), + }, + }, + null, + 2, + ), + ); + + await noteStateIntegrity(cfg, { confirmSkipInNonInteractive: vi.fn(async () => false) }); + + const text = stateIntegrityText(); + expect(text).not.toContain("recent sessions are missing transcripts"); + }); }); diff --git a/src/commands/doctor-state-integrity.ts b/src/commands/doctor-state-integrity.ts index 2e31da8e76a6..7b056a27b1e8 100644 --- a/src/commands/doctor-state-integrity.ts +++ b/src/commands/doctor-state-integrity.ts @@ -16,6 +16,7 @@ import { resolveStorePath, } from "../config/sessions.js"; import { resolveRequiredHomeDir } from "../infra/home-dir.js"; +import { parseAgentSessionKey } from "../sessions/session-key-utils.js"; import { note } from "../terminal/note.js"; import { shortenHomePath } from "../utils.js"; @@ -165,6 +166,15 @@ function hasPairingPolicy(value: unknown): boolean { return false; } +function isSlashRoutingSessionKey(sessionKey: string): boolean { + const raw = sessionKey.trim().toLowerCase(); + if (!raw) { + return false; + } + const scoped = parseAgentSessionKey(raw)?.rest ?? raw; + return /^[^:]+:slash:[^:]+(?:$|:)/.test(scoped); +} + function shouldRequireOAuthDir(cfg: OpenClawConfig, env: NodeJS.ProcessEnv): boolean { if (env.OPENCLAW_OAUTH_DIR?.trim()) { return true; @@ -413,7 +423,8 @@ export async function noteStateIntegrity( return bUpdated - aUpdated; }) .slice(0, 5); - const missing = recent.filter(([, entry]) => { + const recentTranscriptCandidates = recent.filter(([key]) => !isSlashRoutingSessionKey(key)); + const missing = recentTranscriptCandidates.filter(([, entry]) => { const sessionId = entry.sessionId; if (!sessionId) { return false; @@ -424,7 +435,7 @@ export async function noteStateIntegrity( if (missing.length > 0) { warnings.push( [ - `- ${missing.length}/${recent.length} recent sessions are missing transcripts.`, + `- ${missing.length}/${recentTranscriptCandidates.length} recent sessions are missing transcripts.`, ` Verify sessions in store: ${formatCliCommand(`openclaw sessions --store "${absoluteStorePath}"`)}`, ` Preview cleanup impact: ${formatCliCommand(`openclaw sessions cleanup --store "${absoluteStorePath}" --dry-run`)}`, ].join("\n"), From a9d9a968ed80879f63f519497f778ca09af6f327 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 26 Feb 2026 04:59:54 -0500 Subject: [PATCH 158/160] chore(changelog): move post release entries to unreleased section --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a28452ec998..e83b518b4936 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Docs: https://docs.openclaw.ai - Agents/Routing CLI: add `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` for account-scoped route management, including channel-only to account-scoped binding upgrades, role-aware binding identity handling, plugin-resolved binding account IDs, and optional account-binding prompts in `openclaw channels add`. (#27195) thanks @gumadeiras. - Android/Nodes: add `notifications.list` support on Android nodes and expose `nodes notifications_list` in agent tooling for listing active device notifications. (#27344) thanks @obviyus. +- Onboarding/Plugins: let channel plugins own interactive onboarding flows with optional `configureInteractive` and `configureWhenConfigured` hooks while preserving the generic fallback path. (#27191) thanks @gumadeiras. ### Fixes @@ -18,6 +19,8 @@ Docs: https://docs.openclaw.ai - Android/Node invoke: remove native gateway WebSocket `Origin` header to avoid false origin rejections, unify invoke command registry/policy/error parsing paths, and keep command availability checks centralized to reduce dispatcher/advertisement drift. (#27257) Thanks @obviyus. - CI/Windows: shard the Windows `checks-windows` test lane into two matrix jobs and honor explicit shard index overrides in `scripts/test-parallel.mjs` to reduce CI critical-path wall time. (#27234) Thanks @joshavant. - Agents/Models config: preserve agent-level provider `apiKey` and `baseUrl` during merge-mode `models.json` updates when agent values are present. (#27293) thanks @Sid-Qin. +- Docker/GCP onboarding: reduce first-build OOM risk by capping Node heap during `pnpm install`, reuse existing gateway token during `docker-setup.sh` reruns so `.env` stays aligned with config, auto-bootstrap Control UI allowed origins for non-loopback Docker binds, and add GCP docs guidance for tokenized dashboard links + pairing recovery commands. (#26253) Thanks @pandego. +- Pairing/Multi-account isolation: keep non-default account pairing allowlists and pending requests strictly account-scoped, while default account continues to use channel-scoped pairing allowlist storage. Thanks @gumadeiras. ## 2026.2.25 @@ -28,7 +31,6 @@ Docs: https://docs.openclaw.ai - UI/Chat compose: add mobile stacked layout for compose action buttons on small screens to improve send/session controls usability. (#11167) Thanks @junyiz. - Heartbeat/Config: replace heartbeat DM toggle with `agents.defaults.heartbeat.directPolicy` (`allow` | `block`; also supported per-agent via `agents.list[].heartbeat.directPolicy`) for clearer delivery semantics. - Onboarding/Security: clarify onboarding security notices that OpenClaw is personal-by-default (single trusted operator boundary) and shared/multi-user setups require explicit lock-down/hardening. -- Onboarding/Plugins: let channel plugins own interactive onboarding flows with optional `configureInteractive` and `configureWhenConfigured` hooks while preserving the generic fallback path. (#27191) thanks @gumadeiras. - Branding/Docs + Apple surfaces: replace remaining `bot.molt` launchd label, bundle-id, logging subsystem, and command examples with `ai.openclaw` across docs, iOS app surfaces, helper scripts, and CLI test fixtures. - Agents/Config: remind agents to call `config.schema` before config edits or config-field questions to avoid guessing. Thanks @thewilloftheshadow. - Dependencies: update workspace dependency pins and lockfile (Bedrock SDK `3.998.0`, `@mariozechner/pi-*` `0.55.1`, TypeScript native preview `7.0.0-dev.20260225.1`) while keeping `@buape/carbon` pinned. @@ -39,11 +41,9 @@ Docs: https://docs.openclaw.ai ### Fixes -- Docker/GCP onboarding: reduce first-build OOM risk by capping Node heap during `pnpm install`, reuse existing gateway token during `docker-setup.sh` reruns so `.env` stays aligned with config, auto-bootstrap Control UI allowed origins for non-loopback Docker binds, and add GCP docs guidance for tokenized dashboard links + pairing recovery commands. (#26253) Thanks @pandego. - Agents/Subagents delivery: refactor subagent completion announce dispatch into an explicit queue/direct/fallback state machine, recover outbound channel-plugin resolution in cold/stale plugin-registry states across announce/message/gateway send paths, finalize cleanup bookkeeping when announce flow rejects, and treat Telegram sends without `message_id` as delivery failures (instead of false-success `"unknown"` IDs). (#26867, #25961, #26803, #25069, #26741) Thanks @SmithLabsLLC and @docaohieu2808. - Telegram/Webhook: pre-initialize webhook bots, switch webhook processing to callback-mode JSON handling, and preserve full near-limit payload reads under delayed handlers to prevent webhook request hangs and dropped updates. (#26156) - Slack/Session threads: prevent oversized parent-session inheritance from silently bricking new thread sessions, surface embedded context-overflow empty-result failures to users, and add configurable `session.parentForkMaxTokens` (default `100000`, `0` disables). (#26912) Thanks @markshields-tl. -- Pairing/Multi-account isolation: keep non-default account pairing allowlists and pending requests strictly account-scoped, while default account continues to use channel-scoped pairing allowlist storage. - Cron/Message multi-account routing: honor explicit `delivery.accountId` for isolated cron delivery resolution, and when `message.send` omits `accountId`, fall back to the sending agent's bound channel account instead of defaulting to the global account. (#27015, #26975) Thanks @lbo728 and @stakeswky. - Gateway/Message media roots: thread `agentId` through gateway `send` RPC and prefer explicit `agentId` over session/default resolution so non-default agent workspace media sends no longer fail with `LocalMediaAccessError`; added regression coverage for agent precedence and blank-agent fallback. (#23249) Thanks @Sid-Qin. - Followups/Routing: when explicit origin routing fails, allow same-channel fallback dispatch (while still blocking cross-channel fallback) so followup replies do not get dropped on transient origin-adapter failures. (#26109) Thanks @Sid-Qin. From a7d56e3554d088d437477d97d2c967754b9b1f5d Mon Sep 17 00:00:00 2001 From: Onur Solmaz <2453968+osolmaz@users.noreply.github.com> Date: Thu, 26 Feb 2026 11:00:09 +0100 Subject: [PATCH 159/160] feat: ACP thread-bound agents (#23580) * docs: add ACP thread-bound agents plan doc * docs: expand ACP implementation specification * feat(acp): route ACP sessions through core dispatch and lifecycle cleanup * feat(acp): add /acp commands and Discord spawn gate * ACP: add acpx runtime plugin backend * fix(subagents): defer transient lifecycle errors before announce * Agents: harden ACP sessions_spawn and tighten spawn guidance * Agents: require explicit ACP target for runtime spawns * docs: expand ACP control-plane implementation plan * ACP: harden metadata seeding and spawn guidance * ACP: centralize runtime control-plane manager and fail-closed dispatch * ACP: harden runtime manager and unify spawn helpers * Commands: route ACP sessions through ACP runtime in agent command * ACP: require persisted metadata for runtime spawns * Sessions: preserve ACP metadata when updating entries * Plugins: harden ACP backend registry across loaders * ACPX: make availability probe compatible with adapters * E2E: add manual Discord ACP plain-language smoke script * ACPX: preserve streamed spacing across Discord delivery * Docs: add ACP Discord streaming strategy * ACP: harden Discord stream buffering for thread replies * ACP: reuse shared block reply pipeline for projector * ACP: unify streaming config and adopt coalesceIdleMs * Docs: add temporary ACP production hardening plan * Docs: trim temporary ACP hardening plan goals * Docs: gate ACP thread controls by backend capabilities * ACP: add capability-gated runtime controls and /acp operator commands * Docs: remove temporary ACP hardening plan * ACP: fix spawn target validation and close cache cleanup * ACP: harden runtime dispatch and recovery paths * ACP: split ACP command/runtime internals and centralize policy * ACP: harden runtime lifecycle, validation, and observability * ACP: surface runtime and backend session IDs in thread bindings * docs: add temp plan for binding-service migration * ACP: migrate thread binding flows to SessionBindingService * ACP: address review feedback and preserve prompt wording * ACPX plugin: pin runtime dependency and prefer bundled CLI * Discord: complete binding-service migration cleanup and restore ACP plan * Docs: add standalone ACP agents guide * ACP: route harness intents to thread-bound ACP sessions * ACP: fix spawn thread routing and queue-owner stall * ACP: harden startup reconciliation and command bypass handling * ACP: fix dispatch bypass type narrowing * ACP: align runtime metadata to agentSessionId * ACP: normalize session identifier handling and labels * ACP: mark thread banner session ids provisional until first reply * ACP: stabilize session identity mapping and startup reconciliation * ACP: add resolved session-id notices and cwd in thread intros * Discord: prefix thread meta notices consistently * Discord: unify ACP/thread meta notices with gear prefix * Discord: split thread persona naming from meta formatting * Extensions: bump acpx plugin dependency to 0.1.9 * Agents: gate ACP prompt guidance behind acp.enabled * Docs: remove temp experiment plan docs * Docs: scope streaming plan to holy grail refactor * Docs: refactor ACP agents guide for human-first flow * Docs/Skill: add ACP feature-flag guidance and direct acpx telephone-game flow * Docs/Skill: add OpenCode and Pi to ACP harness lists * Docs/Skill: align ACP harness list with current acpx registry * Dev/Test: move ACP plain-language smoke script and mark as keep * Docs/Skill: reorder ACP harness lists with Pi first * ACP: split control-plane manager into core/types/utils modules * Docs: refresh ACP thread-bound agents plan * ACP: extract dispatch lane and split manager domains * ACP: centralize binding context and remove reverse deps * Infra: unify system message formatting * ACP: centralize error boundaries and session id rendering * ACP: enforce init concurrency cap and strict meta clear * Tests: fix ACP dispatch binding mock typing * Tests: fix Discord thread-binding mock drift and ACP request id * ACP: gate slash bypass and persist cleared overrides * ACPX: await pre-abort cancel before runTurn return * Extension: pin acpx runtime dependency to 0.1.11 * Docs: add pinned acpx install strategy for ACP extension * Extensions/acpx: enforce strict local pinned startup * Extensions/acpx: tighten acp-router install guidance * ACPX: retry runtime test temp-dir cleanup * Extensions/acpx: require proactive ACPX repair for thread spawns * Extensions/acpx: require restart offer after acpx reinstall * extensions/acpx: remove workspace protocol devDependency * extensions/acpx: bump pinned acpx to 0.1.13 * extensions/acpx: sync lockfile after dependency bump * ACPX: make runtime spawn Windows-safe * fix: align doctor-config-flow repair tests with default-account migration (#23580) (thanks @osolmaz) --- .github/labeler.yml | 4 + CHANGELOG.md | 1 + docs/docs.json | 7 +- .../plans/acp-thread-bound-agents.md | 800 ++++++++++ .../plans/acp-unified-streaming-refactor.md | 96 ++ docs/help/testing.md | 5 + docs/tools/acp-agents.md | 265 ++++ docs/tools/index.md | 3 +- docs/tools/slash-commands.md | 2 + docs/tools/subagents.md | 1 + extensions/acpx/index.ts | 19 + extensions/acpx/openclaw.plugin.json | 55 + extensions/acpx/package.json | 14 + extensions/acpx/skills/acp-router/SKILL.md | 209 +++ extensions/acpx/src/config.test.ts | 53 + extensions/acpx/src/config.ts | 196 +++ extensions/acpx/src/ensure.test.ts | 125 ++ extensions/acpx/src/ensure.ts | 169 +++ .../acpx/src/runtime-internals/events.ts | 140 ++ .../acpx/src/runtime-internals/process.ts | 137 ++ .../acpx/src/runtime-internals/shared.ts | 56 + extensions/acpx/src/runtime.test.ts | 619 ++++++++ extensions/acpx/src/runtime.ts | 578 ++++++++ extensions/acpx/src/service.test.ts | 173 +++ extensions/acpx/src/service.ts | 102 ++ package.json | 3 +- pnpm-lock.yaml | 115 +- scripts/check-channel-agnostic-boundaries.mjs | 405 +++++ .../dev/discord-acp-plain-language-smoke.ts | 779 ++++++++++ skills/coding-agent/SKILL.md | 2 +- src/acp/control-plane/manager.core.ts | 1314 +++++++++++++++++ .../manager.identity-reconcile.ts | 159 ++ .../control-plane/manager.runtime-controls.ts | 118 ++ src/acp/control-plane/manager.test.ts | 1250 ++++++++++++++++ src/acp/control-plane/manager.ts | 29 + src/acp/control-plane/manager.types.ts | 141 ++ src/acp/control-plane/manager.utils.ts | 64 + src/acp/control-plane/runtime-cache.test.ts | 62 + src/acp/control-plane/runtime-cache.ts | 99 ++ src/acp/control-plane/runtime-options.ts | 349 +++++ src/acp/control-plane/session-actor-queue.ts | 53 + src/acp/control-plane/spawn.ts | 77 + src/acp/policy.test.ts | 59 + src/acp/policy.ts | 69 + src/acp/runtime/adapter-contract.testkit.ts | 114 ++ src/acp/runtime/error-text.test.ts | 19 + src/acp/runtime/error-text.ts | 45 + src/acp/runtime/errors.test.ts | 33 + src/acp/runtime/errors.ts | 61 + src/acp/runtime/registry.test.ts | 99 ++ src/acp/runtime/registry.ts | 118 ++ src/acp/runtime/session-identifiers.test.ts | 89 ++ src/acp/runtime/session-identifiers.ts | 131 ++ src/acp/runtime/session-identity.ts | 210 +++ src/acp/runtime/session-meta.ts | 165 +++ src/acp/runtime/types.ts | 110 ++ ...acp-binding-architecture.guardrail.test.ts | 42 + src/agents/acp-spawn.test.ts | 373 +++++ src/agents/acp-spawn.ts | 424 ++++++ src/agents/cli-runner/helpers.ts | 1 + src/agents/openclaw-tools.sessions.test.ts | 2 + src/agents/pi-embedded-runner/compact.ts | 1 + src/agents/pi-embedded-runner/run/attempt.ts | 1 + .../pi-embedded-runner/system-prompt.ts | 3 + src/agents/skills/plugin-skills.test.ts | 103 ++ src/agents/skills/plugin-skills.ts | 5 + src/agents/subagent-announce.ts | 14 + ...ent-registry.lifecycle-retry-grace.test.ts | 157 ++ src/agents/subagent-registry.ts | 94 +- src/agents/subagent-spawn.ts | 1 + src/agents/system-prompt.test.ts | 79 +- src/agents/system-prompt.ts | 20 +- src/agents/tools/agents-list-tool.ts | 3 +- src/agents/tools/sessions-spawn-tool.test.ts | 118 ++ src/agents/tools/sessions-spawn-tool.ts | 78 +- src/auto-reply/commands-registry.data.ts | 40 + src/auto-reply/commands-registry.test.ts | 24 + src/auto-reply/reply/acp-projector.test.ts | 145 ++ src/auto-reply/reply/acp-projector.ts | 140 ++ src/auto-reply/reply/agent-runner.ts | 12 +- src/auto-reply/reply/block-streaming.test.ts | 68 + src/auto-reply/reply/block-streaming.ts | 95 +- src/auto-reply/reply/commands-acp.test.ts | 796 ++++++++++ src/auto-reply/reply/commands-acp.ts | 83 ++ .../reply/commands-acp/context.test.ts | 51 + src/auto-reply/reply/commands-acp/context.ts | 58 + .../reply/commands-acp/diagnostics.ts | 203 +++ .../reply/commands-acp/lifecycle.ts | 588 ++++++++ .../reply/commands-acp/runtime-options.ts | 348 +++++ src/auto-reply/reply/commands-acp/shared.ts | 500 +++++++ src/auto-reply/reply/commands-acp/targets.ts | 90 ++ src/auto-reply/reply/commands-core.ts | 2 + .../reply/commands-subagents-focus.test.ts | 198 ++- .../reply/commands-subagents/action-focus.ts | 113 +- .../reply/commands-system-prompt.ts | 1 + .../reply/directive-handling.shared.ts | 11 +- src/auto-reply/reply/dispatch-acp.ts | 379 +++++ .../reply/dispatch-from-config.test.ts | 920 ++++++++++++ src/auto-reply/reply/dispatch-from-config.ts | 79 +- src/channels/thread-bindings-messages.ts | 84 ++ src/channels/thread-bindings-policy.ts | 168 +++ src/commands/agent.acp.test.ts | 294 ++++ src/commands/agent.test.ts | 67 + src/commands/agent.ts | 166 ++- src/commands/agent/session-store.test.ts | 66 + src/commands/agent/session-store.ts | 9 +- src/commands/doctor-config-flow.test.ts | 25 +- src/config/plugin-auto-enable.test.ts | 28 + src/config/plugin-auto-enable.ts | 10 + src/config/schema.help.ts | 24 + src/config/schema.labels.ts | 13 + src/config/schema.test.ts | 2 + src/config/sessions/types.ts | 44 + src/config/types.acp.ts | 31 + src/config/types.discord.ts | 5 + src/config/types.openclaw.ts | 2 + src/config/types.ts | 1 + src/config/zod-schema.providers-core.ts | 1 + src/config/zod-schema.ts | 30 + .../monitor/message-handler.preflight.test.ts | 56 +- .../monitor/message-handler.preflight.ts | 28 +- .../message-handler.preflight.types.ts | 9 +- .../monitor/message-handler.process.test.ts | 12 +- src/discord/monitor/provider.test.ts | 21 + src/discord/monitor/provider.ts | 61 +- src/discord/monitor/reply-delivery.test.ts | 17 + src/discord/monitor/reply-delivery.ts | 28 +- src/discord/monitor/thread-bindings.config.ts | 21 + .../monitor/thread-bindings.discord-api.ts | 4 +- .../monitor/thread-bindings.lifecycle.ts | 67 + .../monitor/thread-bindings.manager.ts | 27 +- .../monitor/thread-bindings.messages.ts | 78 +- .../monitor/thread-bindings.persona.test.ts | 33 + .../monitor/thread-bindings.persona.ts | 25 + src/discord/monitor/thread-bindings.ts | 13 + .../monitor/thread-bindings.ttl.test.ts | 132 ++ src/gateway/server-methods/agent.test.ts | 40 + src/gateway/server-methods/agent.ts | 12 +- src/gateway/server-methods/sessions.ts | 102 +- src/gateway/server-startup.ts | 18 + ...sessions.gateway-server-sessions-a.test.ts | 142 ++ src/infra/outbound/conversation-id.test.ts | 40 + src/infra/outbound/conversation-id.ts | 41 + .../outbound/session-binding-service.test.ts | 201 +++ src/infra/outbound/session-binding-service.ts | 149 +- src/infra/system-message.test.ts | 19 + src/infra/system-message.ts | 20 + src/plugin-sdk/index.ts | 24 + src/plugins/loader.test.ts | 38 +- src/plugins/loader.ts | 21 +- .../check-channel-agnostic-boundaries.test.ts | 127 ++ 151 files changed, 19005 insertions(+), 324 deletions(-) create mode 100644 docs/experiments/plans/acp-thread-bound-agents.md create mode 100644 docs/experiments/plans/acp-unified-streaming-refactor.md create mode 100644 docs/tools/acp-agents.md create mode 100644 extensions/acpx/index.ts create mode 100644 extensions/acpx/openclaw.plugin.json create mode 100644 extensions/acpx/package.json create mode 100644 extensions/acpx/skills/acp-router/SKILL.md create mode 100644 extensions/acpx/src/config.test.ts create mode 100644 extensions/acpx/src/config.ts create mode 100644 extensions/acpx/src/ensure.test.ts create mode 100644 extensions/acpx/src/ensure.ts create mode 100644 extensions/acpx/src/runtime-internals/events.ts create mode 100644 extensions/acpx/src/runtime-internals/process.ts create mode 100644 extensions/acpx/src/runtime-internals/shared.ts create mode 100644 extensions/acpx/src/runtime.test.ts create mode 100644 extensions/acpx/src/runtime.ts create mode 100644 extensions/acpx/src/service.test.ts create mode 100644 extensions/acpx/src/service.ts create mode 100644 scripts/check-channel-agnostic-boundaries.mjs create mode 100644 scripts/dev/discord-acp-plain-language-smoke.ts create mode 100644 src/acp/control-plane/manager.core.ts create mode 100644 src/acp/control-plane/manager.identity-reconcile.ts create mode 100644 src/acp/control-plane/manager.runtime-controls.ts create mode 100644 src/acp/control-plane/manager.test.ts create mode 100644 src/acp/control-plane/manager.ts create mode 100644 src/acp/control-plane/manager.types.ts create mode 100644 src/acp/control-plane/manager.utils.ts create mode 100644 src/acp/control-plane/runtime-cache.test.ts create mode 100644 src/acp/control-plane/runtime-cache.ts create mode 100644 src/acp/control-plane/runtime-options.ts create mode 100644 src/acp/control-plane/session-actor-queue.ts create mode 100644 src/acp/control-plane/spawn.ts create mode 100644 src/acp/policy.test.ts create mode 100644 src/acp/policy.ts create mode 100644 src/acp/runtime/adapter-contract.testkit.ts create mode 100644 src/acp/runtime/error-text.test.ts create mode 100644 src/acp/runtime/error-text.ts create mode 100644 src/acp/runtime/errors.test.ts create mode 100644 src/acp/runtime/errors.ts create mode 100644 src/acp/runtime/registry.test.ts create mode 100644 src/acp/runtime/registry.ts create mode 100644 src/acp/runtime/session-identifiers.test.ts create mode 100644 src/acp/runtime/session-identifiers.ts create mode 100644 src/acp/runtime/session-identity.ts create mode 100644 src/acp/runtime/session-meta.ts create mode 100644 src/acp/runtime/types.ts create mode 100644 src/agents/acp-binding-architecture.guardrail.test.ts create mode 100644 src/agents/acp-spawn.test.ts create mode 100644 src/agents/acp-spawn.ts create mode 100644 src/agents/skills/plugin-skills.test.ts create mode 100644 src/agents/subagent-registry.lifecycle-retry-grace.test.ts create mode 100644 src/agents/tools/sessions-spawn-tool.test.ts create mode 100644 src/auto-reply/reply/acp-projector.test.ts create mode 100644 src/auto-reply/reply/acp-projector.ts create mode 100644 src/auto-reply/reply/block-streaming.test.ts create mode 100644 src/auto-reply/reply/commands-acp.test.ts create mode 100644 src/auto-reply/reply/commands-acp.ts create mode 100644 src/auto-reply/reply/commands-acp/context.test.ts create mode 100644 src/auto-reply/reply/commands-acp/context.ts create mode 100644 src/auto-reply/reply/commands-acp/diagnostics.ts create mode 100644 src/auto-reply/reply/commands-acp/lifecycle.ts create mode 100644 src/auto-reply/reply/commands-acp/runtime-options.ts create mode 100644 src/auto-reply/reply/commands-acp/shared.ts create mode 100644 src/auto-reply/reply/commands-acp/targets.ts create mode 100644 src/auto-reply/reply/dispatch-acp.ts create mode 100644 src/channels/thread-bindings-messages.ts create mode 100644 src/channels/thread-bindings-policy.ts create mode 100644 src/commands/agent.acp.test.ts create mode 100644 src/commands/agent/session-store.test.ts create mode 100644 src/config/types.acp.ts create mode 100644 src/discord/monitor/thread-bindings.config.ts create mode 100644 src/discord/monitor/thread-bindings.persona.test.ts create mode 100644 src/discord/monitor/thread-bindings.persona.ts create mode 100644 src/infra/outbound/conversation-id.test.ts create mode 100644 src/infra/outbound/conversation-id.ts create mode 100644 src/infra/outbound/session-binding-service.test.ts create mode 100644 src/infra/system-message.test.ts create mode 100644 src/infra/system-message.ts create mode 100644 test/scripts/check-channel-agnostic-boundaries.test.ts diff --git a/.github/labeler.yml b/.github/labeler.yml index 78366fb2097e..ffe55984ac62 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -240,6 +240,10 @@ - changed-files: - any-glob-to-any-file: - "extensions/device-pair/**" +"extensions: acpx": + - changed-files: + - any-glob-to-any-file: + - "extensions/acpx/**" "extensions: minimax-portal-auth": - changed-files: - any-glob-to-any-file: diff --git a/CHANGELOG.md b/CHANGELOG.md index e83b518b4936..39f57d947ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai ### Changes +- ACP/Thread-bound agents: make ACP agents first-class runtimes for thread sessions with `acp` spawn/send dispatch integration, acpx backend bridging, lifecycle controls, startup reconciliation, runtime cleanup, and coalesced thread replies. (#23580) thanks @osolmaz. - Agents/Routing CLI: add `openclaw agents bindings`, `openclaw agents bind`, and `openclaw agents unbind` for account-scoped route management, including channel-only to account-scoped binding upgrades, role-aware binding identity handling, plugin-resolved binding account IDs, and optional account-binding prompts in `openclaw channels add`. (#27195) thanks @gumadeiras. - Android/Nodes: add `notifications.list` support on Android nodes and expose `nodes notifications_list` in agent tooling for listing active device notifications. (#27344) thanks @obviyus. - Onboarding/Plugins: let channel plugins own interactive onboarding flows with optional `configureInteractive` and `configureWhenConfigured` hooks while preserving the generic fallback path. (#27191) thanks @gumadeiras. diff --git a/docs/docs.json b/docs/docs.json index 4c83f3058bdb..5f6b21d7e82c 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -1002,7 +1002,12 @@ }, { "group": "Agent coordination", - "pages": ["tools/agent-send", "tools/subagents", "tools/multi-agent-sandbox-tools"] + "pages": [ + "tools/agent-send", + "tools/subagents", + "tools/acp-agents", + "tools/multi-agent-sandbox-tools" + ] }, { "group": "Skills", diff --git a/docs/experiments/plans/acp-thread-bound-agents.md b/docs/experiments/plans/acp-thread-bound-agents.md new file mode 100644 index 000000000000..3ca509c94927 --- /dev/null +++ b/docs/experiments/plans/acp-thread-bound-agents.md @@ -0,0 +1,800 @@ +--- +summary: "Integrate ACP coding agents via a first-class ACP control plane in core and plugin-backed runtimes (acpx first)" +owner: "onutc" +status: "draft" +last_updated: "2026-02-25" +title: "ACP Thread Bound Agents" +--- + +# ACP Thread Bound Agents + +## Overview + +This plan defines how OpenClaw should support ACP coding agents in thread-capable channels (Discord first) with production-level lifecycle and recovery. + +Related document: + +- [Unified Runtime Streaming Refactor Plan](/experiments/plans/acp-unified-streaming-refactor) + +Target user experience: + +- a user spawns or focuses an ACP session into a thread +- user messages in that thread route to the bound ACP session +- agent output streams back to the same thread persona +- session can be persistent or one shot with explicit cleanup controls + +## Decision summary + +Long term recommendation is a hybrid architecture: + +- OpenClaw core owns ACP control plane concerns + - session identity and metadata + - thread binding and routing decisions + - delivery invariants and duplicate suppression + - lifecycle cleanup and recovery semantics +- ACP runtime backend is pluggable + - first backend is an acpx-backed plugin service + - runtime does ACP transport, queueing, cancel, reconnect + +OpenClaw should not reimplement ACP transport internals in core. +OpenClaw should not rely on a pure plugin-only interception path for routing. + +## North-star architecture (holy grail) + +Treat ACP as a first-class control plane in OpenClaw, with pluggable runtime adapters. + +Non-negotiable invariants: + +- every ACP thread binding references a valid ACP session record +- every ACP session has explicit lifecycle state (`creating`, `idle`, `running`, `cancelling`, `closed`, `error`) +- every ACP run has explicit run state (`queued`, `running`, `completed`, `failed`, `cancelled`) +- spawn, bind, and initial enqueue are atomic +- command retries are idempotent (no duplicate runs or duplicate Discord outputs) +- bound-thread channel output is a projection of ACP run events, never ad-hoc side effects + +Long-term ownership model: + +- `AcpSessionManager` is the single ACP writer and orchestrator +- manager lives in gateway process first; can be moved to a dedicated sidecar later behind the same interface +- per ACP session key, manager owns one in-memory actor (serialized command execution) +- adapters (`acpx`, future backends) are transport/runtime implementations only + +Long-term persistence model: + +- move ACP control-plane state to a dedicated SQLite store (WAL mode) under OpenClaw state dir +- keep `SessionEntry.acp` as compatibility projection during migration, not source-of-truth +- store ACP events append-only to support replay, crash recovery, and deterministic delivery + +### Delivery strategy (bridge to holy-grail) + +- short-term bridge + - keep current thread binding mechanics and existing ACP config surface + - fix metadata-gap bugs and route ACP turns through a single core ACP branch + - add idempotency keys and fail-closed routing checks immediately +- long-term cutover + - move ACP source-of-truth to control-plane DB + actors + - make bound-thread delivery purely event-projection based + - remove legacy fallback behavior that depends on opportunistic session-entry metadata + +## Why not pure plugin only + +Current plugin hooks are not sufficient for end to end ACP session routing without core changes. + +- inbound routing from thread binding resolves to a session key in core dispatch first +- message hooks are fire-and-forget and cannot short-circuit the main reply path +- plugin commands are good for control operations but not for replacing core per-turn dispatch flow + +Result: + +- ACP runtime can be pluginized +- ACP routing branch must exist in core + +## Existing foundation to reuse + +Already implemented and should remain canonical: + +- thread binding target supports `subagent` and `acp` +- inbound thread routing override resolves by binding before normal dispatch +- outbound thread identity via webhook in reply delivery +- `/focus` and `/unfocus` flow with ACP target compatibility +- persistent binding store with restore on startup +- unbind lifecycle on archive, delete, unfocus, reset, and delete + +This plan extends that foundation rather than replacing it. + +## Architecture + +### Boundary model + +Core (must be in OpenClaw core): + +- ACP session-mode dispatch branch in the reply pipeline +- delivery arbitration to avoid parent plus thread duplication +- ACP control-plane persistence (with `SessionEntry.acp` compatibility projection during migration) +- lifecycle unbind and runtime detach semantics tied to session reset/delete + +Plugin backend (acpx implementation): + +- ACP runtime worker supervision +- acpx process invocation and event parsing +- ACP command handlers (`/acp ...`) and operator UX +- backend-specific config defaults and diagnostics + +### Runtime ownership model + +- one gateway process owns ACP orchestration state +- ACP execution runs in supervised child processes via acpx backend +- process strategy is long lived per active ACP session key, not per message + +This avoids startup cost on every prompt and keeps cancel and reconnect semantics reliable. + +### Core runtime contract + +Add a core ACP runtime contract so routing code does not depend on CLI details and can switch backends without changing dispatch logic: + +```ts +export type AcpRuntimePromptMode = "prompt" | "steer"; + +export type AcpRuntimeHandle = { + sessionKey: string; + backend: string; + runtimeSessionName: string; +}; + +export type AcpRuntimeEvent = + | { type: "text_delta"; stream: "output" | "thought"; text: string } + | { type: "tool_call"; name: string; argumentsText: string } + | { type: "done"; usage?: Record } + | { type: "error"; code: string; message: string; retryable?: boolean }; + +export interface AcpRuntime { + ensureSession(input: { + sessionKey: string; + agent: string; + mode: "persistent" | "oneshot"; + cwd?: string; + env?: Record; + idempotencyKey: string; + }): Promise; + + submit(input: { + handle: AcpRuntimeHandle; + text: string; + mode: AcpRuntimePromptMode; + idempotencyKey: string; + }): Promise<{ runtimeRunId: string }>; + + stream(input: { + handle: AcpRuntimeHandle; + runtimeRunId: string; + onEvent: (event: AcpRuntimeEvent) => Promise | void; + signal?: AbortSignal; + }): Promise; + + cancel(input: { + handle: AcpRuntimeHandle; + runtimeRunId?: string; + reason?: string; + idempotencyKey: string; + }): Promise; + + close(input: { handle: AcpRuntimeHandle; reason: string; idempotencyKey: string }): Promise; + + health?(): Promise<{ ok: boolean; details?: string }>; +} +``` + +Implementation detail: + +- first backend: `AcpxRuntime` shipped as a plugin service +- core resolves runtime via registry and fails with explicit operator error when no ACP runtime backend is available + +### Control-plane data model and persistence + +Long-term source-of-truth is a dedicated ACP SQLite database (WAL mode), for transactional updates and crash-safe recovery: + +- `acp_sessions` + - `session_key` (pk), `backend`, `agent`, `mode`, `cwd`, `state`, `created_at`, `updated_at`, `last_error` +- `acp_runs` + - `run_id` (pk), `session_key` (fk), `state`, `requester_message_id`, `idempotency_key`, `started_at`, `ended_at`, `error_code`, `error_message` +- `acp_bindings` + - `binding_key` (pk), `thread_id`, `channel_id`, `account_id`, `session_key` (fk), `expires_at`, `bound_at` +- `acp_events` + - `event_id` (pk), `run_id` (fk), `seq`, `kind`, `payload_json`, `created_at` +- `acp_delivery_checkpoint` + - `run_id` (pk/fk), `last_event_seq`, `last_discord_message_id`, `updated_at` +- `acp_idempotency` + - `scope`, `idempotency_key`, `result_json`, `created_at`, unique `(scope, idempotency_key)` + +```ts +export type AcpSessionMeta = { + backend: string; + agent: string; + runtimeSessionName: string; + mode: "persistent" | "oneshot"; + cwd?: string; + state: "idle" | "running" | "error"; + lastActivityAt: number; + lastError?: string; +}; +``` + +Storage rules: + +- keep `SessionEntry.acp` as a compatibility projection during migration +- process ids and sockets stay in memory only +- durable lifecycle and run status live in ACP DB, not generic session JSON +- if runtime owner dies, gateway rehydrates from ACP DB and resumes from checkpoints + +### Routing and delivery + +Inbound: + +- keep current thread binding lookup as first routing step +- if bound target is ACP session, route to ACP runtime branch instead of `getReplyFromConfig` +- explicit `/acp steer` command uses `mode: "steer"` + +Outbound: + +- ACP event stream is normalized to OpenClaw reply chunks +- delivery target is resolved through existing bound destination path +- when a bound thread is active for that session turn, parent channel completion is suppressed + +Streaming policy: + +- stream partial output with coalescing window +- configurable min interval and max chunk bytes to stay under Discord rate limits +- final message always emitted on completion or failure + +### State machines and transaction boundaries + +Session state machine: + +- `creating -> idle -> running -> idle` +- `running -> cancelling -> idle | error` +- `idle -> closed` +- `error -> idle | closed` + +Run state machine: + +- `queued -> running -> completed` +- `running -> failed | cancelled` +- `queued -> cancelled` + +Required transaction boundaries: + +- spawn transaction + - create ACP session row + - create/update ACP thread binding row + - enqueue initial run row +- close transaction + - mark session closed + - delete/expire binding rows + - write final close event +- cancel transaction + - mark target run cancelling/cancelled with idempotency key + +No partial success is allowed across these boundaries. + +### Per-session actor model + +`AcpSessionManager` runs one actor per ACP session key: + +- actor mailbox serializes `submit`, `cancel`, `close`, and `stream` side effects +- actor owns runtime handle hydration and runtime adapter process lifecycle for that session +- actor writes run events in-order (`seq`) before any Discord delivery +- actor updates delivery checkpoints after successful outbound send + +This removes cross-turn races and prevents duplicate or out-of-order thread output. + +### Idempotency and delivery projection + +All external ACP actions must carry idempotency keys: + +- spawn idempotency key +- prompt/steer idempotency key +- cancel idempotency key +- close idempotency key + +Delivery rules: + +- Discord messages are derived from `acp_events` plus `acp_delivery_checkpoint` +- retries resume from checkpoint without re-sending already delivered chunks +- final reply emission is exactly-once per run from projection logic + +### Recovery and self-healing + +On gateway start: + +- load non-terminal ACP sessions (`creating`, `idle`, `running`, `cancelling`, `error`) +- recreate actors lazily on first inbound event or eagerly under configured cap +- reconcile any `running` runs missing heartbeats and mark `failed` or recover via adapter + +On inbound Discord thread message: + +- if binding exists but ACP session is missing, fail closed with explicit stale-binding message +- optionally auto-unbind stale binding after operator-safe validation +- never silently route stale ACP bindings to normal LLM path + +### Lifecycle and safety + +Supported operations: + +- cancel current run: `/acp cancel` +- unbind thread: `/unfocus` +- close ACP session: `/acp close` +- auto close idle sessions by effective TTL + +TTL policy: + +- effective TTL is minimum of + - global/session TTL + - Discord thread binding TTL + - ACP runtime owner TTL + +Safety controls: + +- allowlist ACP agents by name +- restrict workspace roots for ACP sessions +- env allowlist passthrough +- max concurrent ACP sessions per account and globally +- bounded restart backoff for runtime crashes + +## Config surface + +Core keys: + +- `acp.enabled` +- `acp.dispatch.enabled` (independent ACP routing kill switch) +- `acp.backend` (default `acpx`) +- `acp.defaultAgent` +- `acp.allowedAgents[]` +- `acp.maxConcurrentSessions` +- `acp.stream.coalesceIdleMs` +- `acp.stream.maxChunkChars` +- `acp.runtime.ttlMinutes` +- `acp.controlPlane.store` (`sqlite` default) +- `acp.controlPlane.storePath` +- `acp.controlPlane.recovery.eagerActors` +- `acp.controlPlane.recovery.reconcileRunningAfterMs` +- `acp.controlPlane.checkpoint.flushEveryEvents` +- `acp.controlPlane.checkpoint.flushEveryMs` +- `acp.idempotency.ttlHours` +- `channels.discord.threadBindings.spawnAcpSessions` + +Plugin/backend keys (acpx plugin section): + +- backend command/path overrides +- backend env allowlist +- backend per-agent presets +- backend startup/stop timeouts +- backend max inflight runs per session + +## Implementation specification + +### Control-plane modules (new) + +Add dedicated ACP control-plane modules in core: + +- `src/acp/control-plane/manager.ts` + - owns ACP actors, lifecycle transitions, command serialization +- `src/acp/control-plane/store.ts` + - SQLite schema management, transactions, query helpers +- `src/acp/control-plane/events.ts` + - typed ACP event definitions and serialization +- `src/acp/control-plane/checkpoint.ts` + - durable delivery checkpoints and replay cursors +- `src/acp/control-plane/idempotency.ts` + - idempotency key reservation and response replay +- `src/acp/control-plane/recovery.ts` + - boot-time reconciliation and actor rehydrate plan + +Compatibility bridge modules: + +- `src/acp/runtime/session-meta.ts` + - remains temporarily for projection into `SessionEntry.acp` + - must stop being source-of-truth after migration cutover + +### Required invariants (must enforce in code) + +- ACP session creation and thread bind are atomic (single transaction) +- there is at most one active run per ACP session actor at a time +- event `seq` is strictly increasing per run +- delivery checkpoint never advances past last committed event +- idempotency replay returns previous success payload for duplicate command keys +- stale/missing ACP metadata cannot route into normal non-ACP reply path + +### Core touchpoints + +Core files to change: + +- `src/auto-reply/reply/dispatch-from-config.ts` + - ACP branch calls `AcpSessionManager.submit` and event-projection delivery + - remove direct ACP fallback that bypasses control-plane invariants +- `src/auto-reply/reply/inbound-context.ts` (or nearest normalized context boundary) + - expose normalized routing keys and idempotency seeds for ACP control plane +- `src/config/sessions/types.ts` + - keep `SessionEntry.acp` as projection-only compatibility field +- `src/gateway/server-methods/sessions.ts` + - reset/delete/archive must call ACP manager close/unbind transaction path +- `src/infra/outbound/bound-delivery-router.ts` + - enforce fail-closed destination behavior for ACP bound session turns +- `src/discord/monitor/thread-bindings.ts` + - add ACP stale-binding validation helpers wired to control-plane lookups +- `src/auto-reply/reply/commands-acp.ts` + - route spawn/cancel/close/steer through ACP manager APIs +- `src/agents/acp-spawn.ts` + - stop ad-hoc metadata writes; call ACP manager spawn transaction +- `src/plugin-sdk/**` and plugin runtime bridge + - expose ACP backend registration and health semantics cleanly + +Core files explicitly not replaced: + +- `src/discord/monitor/message-handler.preflight.ts` + - keep thread binding override behavior as the canonical session-key resolver + +### ACP runtime registry API + +Add a core registry module: + +- `src/acp/runtime/registry.ts` + +Required API: + +```ts +export type AcpRuntimeBackend = { + id: string; + runtime: AcpRuntime; + healthy?: () => boolean; +}; + +export function registerAcpRuntimeBackend(backend: AcpRuntimeBackend): void; +export function unregisterAcpRuntimeBackend(id: string): void; +export function getAcpRuntimeBackend(id?: string): AcpRuntimeBackend | null; +export function requireAcpRuntimeBackend(id?: string): AcpRuntimeBackend; +``` + +Behavior: + +- `requireAcpRuntimeBackend` throws a typed ACP backend missing error when unavailable +- plugin service registers backend on `start` and unregisters on `stop` +- runtime lookups are read-only and process-local + +### acpx runtime plugin contract (implementation detail) + +For the first production backend (`extensions/acpx`), OpenClaw and acpx are +connected with a strict command contract: + +- backend id: `acpx` +- plugin service id: `acpx-runtime` +- runtime handle encoding: `runtimeSessionName = acpx:v1:` +- encoded payload fields: + - `name` (acpx named session; uses OpenClaw `sessionKey`) + - `agent` (acpx agent command) + - `cwd` (session workspace root) + - `mode` (`persistent | oneshot`) + +Command mapping: + +- ensure session: + - `acpx --format json --json-strict --cwd sessions ensure --name ` +- prompt turn: + - `acpx --format json --json-strict --cwd prompt --session --file -` +- cancel: + - `acpx --format json --json-strict --cwd cancel --session ` +- close: + - `acpx --format json --json-strict --cwd sessions close ` + +Streaming: + +- OpenClaw consumes ndjson events from `acpx --format json --json-strict` +- `text` => `text_delta/output` +- `thought` => `text_delta/thought` +- `tool_call` => `tool_call` +- `done` => `done` +- `error` => `error` + +### Session schema patch + +Patch `SessionEntry` in `src/config/sessions/types.ts`: + +```ts +type SessionAcpMeta = { + backend: string; + agent: string; + runtimeSessionName: string; + mode: "persistent" | "oneshot"; + cwd?: string; + state: "idle" | "running" | "error"; + lastActivityAt: number; + lastError?: string; +}; +``` + +Persisted field: + +- `SessionEntry.acp?: SessionAcpMeta` + +Migration rules: + +- phase A: dual-write (`acp` projection + ACP SQLite source-of-truth) +- phase B: read-primary from ACP SQLite, fallback-read from legacy `SessionEntry.acp` +- phase C: migration command backfills missing ACP rows from valid legacy entries +- phase D: remove fallback-read and keep projection optional for UX only +- legacy fields (`cliSessionIds`, `claudeCliSessionId`) remain untouched + +### Error contract + +Add stable ACP error codes and user-facing messages: + +- `ACP_BACKEND_MISSING` + - message: `ACP runtime backend is not configured. Install and enable the acpx runtime plugin.` +- `ACP_BACKEND_UNAVAILABLE` + - message: `ACP runtime backend is currently unavailable. Try again in a moment.` +- `ACP_SESSION_INIT_FAILED` + - message: `Could not initialize ACP session runtime.` +- `ACP_TURN_FAILED` + - message: `ACP turn failed before completion.` + +Rules: + +- return actionable user-safe message in-thread +- log detailed backend/system error only in runtime logs +- never silently fall back to normal LLM path when ACP routing was explicitly selected + +### Duplicate delivery arbitration + +Single routing rule for ACP bound turns: + +- if an active thread binding exists for the target ACP session and requester context, deliver only to that bound thread +- do not also send to parent channel for the same turn +- if bound destination selection is ambiguous, fail closed with explicit error (no implicit parent fallback) +- if no active binding exists, use normal session destination behavior + +### Observability and operational readiness + +Required metrics: + +- ACP spawn success/failure count by backend and error code +- ACP run latency percentiles (queue wait, runtime turn time, delivery projection time) +- ACP actor restart count and restart reason +- stale-binding detection count +- idempotency replay hit rate +- Discord delivery retry and rate-limit counters + +Required logs: + +- structured logs keyed by `sessionKey`, `runId`, `backend`, `threadId`, `idempotencyKey` +- explicit state transition logs for session and run state machines +- adapter command logs with redaction-safe arguments and exit summary + +Required diagnostics: + +- `/acp sessions` includes state, active run, last error, and binding status +- `/acp doctor` (or equivalent) validates backend registration, store health, and stale bindings + +### Config precedence and effective values + +ACP enablement precedence: + +- account override: `channels.discord.accounts..threadBindings.spawnAcpSessions` +- channel override: `channels.discord.threadBindings.spawnAcpSessions` +- global ACP gate: `acp.enabled` +- dispatch gate: `acp.dispatch.enabled` +- backend availability: registered backend for `acp.backend` + +Auto-enable behavior: + +- when ACP is configured (`acp.enabled=true`, `acp.dispatch.enabled=true`, or + `acp.backend=acpx`), plugin auto-enable marks `plugins.entries.acpx.enabled=true` + unless denylisted or explicitly disabled + +TTL effective value: + +- `min(session ttl, discord thread binding ttl, acp runtime ttl)` + +### Test map + +Unit tests: + +- `src/acp/runtime/registry.test.ts` (new) +- `src/auto-reply/reply/dispatch-from-config.acp.test.ts` (new) +- `src/infra/outbound/bound-delivery-router.test.ts` (extend ACP fail-closed cases) +- `src/config/sessions/types.test.ts` or nearest session-store tests (ACP metadata persistence) + +Integration tests: + +- `src/discord/monitor/reply-delivery.test.ts` (bound ACP delivery target behavior) +- `src/discord/monitor/message-handler.preflight*.test.ts` (bound ACP session-key routing continuity) +- acpx plugin runtime tests in backend package (service register/start/stop + event normalization) + +Gateway e2e tests: + +- `src/gateway/server.sessions.gateway-server-sessions-a.e2e.test.ts` (extend ACP reset/delete lifecycle coverage) +- ACP thread turn roundtrip e2e for spawn, message, stream, cancel, unfocus, restart recovery + +### Rollout guard + +Add independent ACP dispatch kill switch: + +- `acp.dispatch.enabled` default `false` for first release +- when disabled: + - ACP spawn/focus control commands may still bind sessions + - ACP dispatch path does not activate + - user receives explicit message that ACP dispatch is disabled by policy +- after canary validation, default can be flipped to `true` in a later release + +## Command and UX plan + +### New commands + +- `/acp spawn [--mode persistent|oneshot] [--thread auto|here|off]` +- `/acp cancel [session]` +- `/acp steer ` +- `/acp close [session]` +- `/acp sessions` + +### Existing command compatibility + +- `/focus ` continues to support ACP targets +- `/unfocus` keeps current semantics +- `/session ttl` remains the top level TTL override + +## Phased rollout + +### Phase 0 ADR and schema freeze + +- ship ADR for ACP control-plane ownership and adapter boundaries +- freeze DB schema (`acp_sessions`, `acp_runs`, `acp_bindings`, `acp_events`, `acp_delivery_checkpoint`, `acp_idempotency`) +- define stable ACP error codes, event contract, and state-transition guards + +### Phase 1 Control-plane foundation in core + +- implement `AcpSessionManager` and per-session actor runtime +- implement ACP SQLite store and transaction helpers +- implement idempotency store and replay helpers +- implement event append + delivery checkpoint modules +- wire spawn/cancel/close APIs to manager with transactional guarantees + +### Phase 2 Core routing and lifecycle integration + +- route thread-bound ACP turns from dispatch pipeline into ACP manager +- enforce fail-closed routing when ACP binding/session invariants fail +- integrate reset/delete/archive/unfocus lifecycle with ACP close/unbind transactions +- add stale-binding detection and optional auto-unbind policy + +### Phase 3 acpx backend adapter/plugin + +- implement `acpx` adapter against runtime contract (`ensureSession`, `submit`, `stream`, `cancel`, `close`) +- add backend health checks and startup/teardown registration +- normalize acpx ndjson events into ACP runtime events +- enforce backend timeouts, process supervision, and restart/backoff policy + +### Phase 4 Delivery projection and channel UX (Discord first) + +- implement event-driven channel projection with checkpoint resume (Discord first) +- coalesce streaming chunks with rate-limit aware flush policy +- guarantee exactly-once final completion message per run +- ship `/acp spawn`, `/acp cancel`, `/acp steer`, `/acp close`, `/acp sessions` + +### Phase 5 Migration and cutover + +- introduce dual-write to `SessionEntry.acp` projection plus ACP SQLite source-of-truth +- add migration utility for legacy ACP metadata rows +- flip read path to ACP SQLite primary +- remove legacy fallback routing that depends on missing `SessionEntry.acp` + +### Phase 6 Hardening, SLOs, and scale limits + +- enforce concurrency limits (global/account/session), queue policies, and timeout budgets +- add full telemetry, dashboards, and alert thresholds +- chaos-test crash recovery and duplicate-delivery suppression +- publish runbook for backend outage, DB corruption, and stale-binding remediation + +### Full implementation checklist + +- core control-plane modules and tests +- DB migrations and rollback plan +- ACP manager API integration across dispatch and commands +- adapter registration interface in plugin runtime bridge +- acpx adapter implementation and tests +- thread-capable channel delivery projection logic with checkpoint replay (Discord first) +- lifecycle hooks for reset/delete/archive/unfocus +- stale-binding detector and operator-facing diagnostics +- config validation and precedence tests for all new ACP keys +- operational docs and troubleshooting runbook + +## Test plan + +Unit tests: + +- ACP DB transaction boundaries (spawn/bind/enqueue atomicity, cancel, close) +- ACP state-machine transition guards for sessions and runs +- idempotency reservation/replay semantics across all ACP commands +- per-session actor serialization and queue ordering +- acpx event parser and chunk coalescer +- runtime supervisor restart and backoff policy +- config precedence and effective TTL calculation +- core ACP routing branch selection and fail-closed behavior when backend/session is invalid + +Integration tests: + +- fake ACP adapter process for deterministic streaming and cancel behavior +- ACP manager + dispatch integration with transactional persistence +- thread-bound inbound routing to ACP session key +- thread-bound outbound delivery suppresses parent channel duplication +- checkpoint replay recovers after delivery failure and resumes from last event +- plugin service registration and teardown of ACP runtime backend + +Gateway e2e tests: + +- spawn ACP with thread, exchange multi-turn prompts, unfocus +- gateway restart with persisted ACP DB and bindings, then continue same session +- concurrent ACP sessions in multiple threads have no cross-talk +- duplicate command retries (same idempotency key) do not create duplicate runs or replies +- stale-binding scenario yields explicit error and optional auto-clean behavior + +## Risks and mitigations + +- Duplicate deliveries during transition + - Mitigation: single destination resolver and idempotent event checkpoint +- Runtime process churn under load + - Mitigation: long lived per session owners + concurrency caps + backoff +- Plugin absent or misconfigured + - Mitigation: explicit operator-facing error and fail-closed ACP routing (no implicit fallback to normal session path) +- Config confusion between subagent and ACP gates + - Mitigation: explicit ACP keys and command feedback that includes effective policy source +- Control-plane store corruption or migration bugs + - Mitigation: WAL mode, backup/restore hooks, migration smoke tests, and read-only fallback diagnostics +- Actor deadlocks or mailbox starvation + - Mitigation: watchdog timers, actor health probes, and bounded mailbox depth with rejection telemetry + +## Acceptance checklist + +- ACP session spawn can create or bind a thread in a supported channel adapter (currently Discord) +- all thread messages route to bound ACP session only +- ACP outputs appear in the same thread identity with streaming or batches +- no duplicate output in parent channel for bound turns +- spawn+bind+initial enqueue are atomic in persistent store +- ACP command retries are idempotent and do not duplicate runs or outputs +- cancel, close, unfocus, archive, reset, and delete perform deterministic cleanup +- crash restart preserves mapping and resumes multi turn continuity +- concurrent thread bound ACP sessions work independently +- ACP backend missing state produces clear actionable error +- stale bindings are detected and surfaced explicitly (with optional safe auto-clean) +- control-plane metrics and diagnostics are available for operators +- new unit, integration, and e2e coverage passes + +## Addendum: targeted refactors for current implementation (status) + +These are non-blocking follow-ups to keep the ACP path maintainable after the current feature set lands. + +### 1) Centralize ACP dispatch policy evaluation (completed) + +- implemented via shared ACP policy helpers in `src/acp/policy.ts` +- dispatch, ACP command lifecycle handlers, and ACP spawn path now consume shared policy logic + +### 2) Split ACP command handler by subcommand domain (completed) + +- `src/auto-reply/reply/commands-acp.ts` is now a thin router +- subcommand behavior is split into: + - `src/auto-reply/reply/commands-acp/lifecycle.ts` + - `src/auto-reply/reply/commands-acp/runtime-options.ts` + - `src/auto-reply/reply/commands-acp/diagnostics.ts` + - shared helpers in `src/auto-reply/reply/commands-acp/shared.ts` + +### 3) Split ACP session manager by responsibility (completed) + +- manager is split into: + - `src/acp/control-plane/manager.ts` (public facade + singleton) + - `src/acp/control-plane/manager.core.ts` (manager implementation) + - `src/acp/control-plane/manager.types.ts` (manager types/deps) + - `src/acp/control-plane/manager.utils.ts` (normalization + helper functions) + +### 4) Optional acpx runtime adapter cleanup + +- `extensions/acpx/src/runtime.ts` can be split into: +- process execution/supervision +- ndjson event parsing/normalization +- runtime API surface (`submit`, `cancel`, `close`, etc.) +- improves testability and makes backend behavior easier to audit diff --git a/docs/experiments/plans/acp-unified-streaming-refactor.md b/docs/experiments/plans/acp-unified-streaming-refactor.md new file mode 100644 index 000000000000..3834fb9f8d89 --- /dev/null +++ b/docs/experiments/plans/acp-unified-streaming-refactor.md @@ -0,0 +1,96 @@ +--- +summary: "Holy grail refactor plan for one unified runtime streaming pipeline across main, subagent, and ACP" +owner: "onutc" +status: "draft" +last_updated: "2026-02-25" +title: "Unified Runtime Streaming Refactor Plan" +--- + +# Unified Runtime Streaming Refactor Plan + +## Objective + +Deliver one shared streaming pipeline for `main`, `subagent`, and `acp` so all runtimes get identical coalescing, chunking, delivery ordering, and crash recovery behavior. + +## Why this exists + +- Current behavior is split across multiple runtime-specific shaping paths. +- Formatting/coalescing bugs can be fixed in one path but remain in others. +- Delivery consistency, duplicate suppression, and recovery semantics are harder to reason about. + +## Target architecture + +Single pipeline, runtime-specific adapters: + +1. Runtime adapters emit canonical events only. +2. Shared stream assembler coalesces and finalizes text/tool/status events. +3. Shared channel projector applies channel-specific chunking/formatting once. +4. Shared delivery ledger enforces idempotent send/replay semantics. +5. Outbound channel adapter executes sends and records delivery checkpoints. + +Canonical event contract: + +- `turn_started` +- `text_delta` +- `block_final` +- `tool_started` +- `tool_finished` +- `status` +- `turn_completed` +- `turn_failed` +- `turn_cancelled` + +## Workstreams + +### 1) Canonical streaming contract + +- Define strict event schema + validation in core. +- Add adapter contract tests to guarantee each runtime emits compatible events. +- Reject malformed runtime events early and surface structured diagnostics. + +### 2) Shared stream processor + +- Replace runtime-specific coalescer/projector logic with one processor. +- Processor owns text delta buffering, idle flush, max-chunk splitting, and completion flush. +- Move ACP/main/subagent config resolution into one helper to prevent drift. + +### 3) Shared channel projection + +- Keep channel adapters dumb: accept finalized blocks and send. +- Move Discord-specific chunking quirks to channel projector only. +- Keep pipeline channel-agnostic before projection. + +### 4) Delivery ledger + replay + +- Add per-turn/per-chunk delivery IDs. +- Record checkpoints before and after physical send. +- On restart, replay pending chunks idempotently and avoid duplicates. + +### 5) Migration and cutover + +- Phase 1: shadow mode (new pipeline computes output but old path sends; compare). +- Phase 2: runtime-by-runtime cutover (`acp`, then `subagent`, then `main` or reverse by risk). +- Phase 3: delete legacy runtime-specific streaming code. + +## Non-goals + +- No changes to ACP policy/permissions model in this refactor. +- No channel-specific feature expansion outside projection compatibility fixes. +- No transport/backend redesign (acpx plugin contract remains as-is unless needed for event parity). + +## Risks and mitigations + +- Risk: behavioral regressions in existing main/subagent paths. + Mitigation: shadow mode diffing + adapter contract tests + channel e2e tests. +- Risk: duplicate sends during crash recovery. + Mitigation: durable delivery IDs + idempotent replay in delivery adapter. +- Risk: runtime adapters diverge again. + Mitigation: required shared contract test suite for all adapters. + +## Acceptance criteria + +- All runtimes pass shared streaming contract tests. +- Discord ACP/main/subagent produce equivalent spacing/chunking behavior for tiny deltas. +- Crash/restart replay sends no duplicate chunk for the same delivery ID. +- Legacy ACP projector/coalescer path is removed. +- Streaming config resolution is shared and runtime-independent. diff --git a/docs/help/testing.md b/docs/help/testing.md index 7932a1f244fa..01bb80abb473 100644 --- a/docs/help/testing.md +++ b/docs/help/testing.md @@ -336,6 +336,11 @@ These run `pnpm test:live` inside the repo Docker image, mounting your local con - Gateway networking (two containers, WS auth + health): `pnpm test:docker:gateway-network` (script: `scripts/e2e/gateway-network-docker.sh`) - Plugins (custom extension load + registry smoke): `pnpm test:docker:plugins` (script: `scripts/e2e/plugins-docker.sh`) +Manual ACP plain-language thread smoke (not CI): + +- `bun scripts/dev/discord-acp-plain-language-smoke.ts --channel ...` +- Keep this script for regression/debug workflows. It may be needed again for ACP thread routing validation, so do not delete it. + Useful env vars: - `OPENCLAW_CONFIG_DIR=...` (default: `~/.openclaw`) mounted to `/home/node/.openclaw` diff --git a/docs/tools/acp-agents.md b/docs/tools/acp-agents.md new file mode 100644 index 000000000000..4cfc3ca92c4a --- /dev/null +++ b/docs/tools/acp-agents.md @@ -0,0 +1,265 @@ +--- +summary: "Use ACP runtime sessions for Pi, Claude Code, Codex, OpenCode, Gemini CLI, and other harness agents" +read_when: + - Running coding harnesses through ACP + - Setting up thread-bound ACP sessions on thread-capable channels + - Troubleshooting ACP backend and plugin wiring +title: "ACP Agents" +--- + +# ACP agents + +ACP sessions let OpenClaw run external coding harnesses (for example Pi, Claude Code, Codex, OpenCode, and Gemini CLI) through an ACP backend plugin. + +If you ask OpenClaw in plain language to "run this in Codex" or "start Claude Code in a thread", OpenClaw should route that request to the ACP runtime (not the native sub-agent runtime). + +## Quick start for humans + +Examples of natural requests: + +- "Start a persistent Codex session in a thread here and keep it focused." +- "Run this as a one-shot Claude Code ACP session and summarize the result." +- "Use Gemini CLI for this task in a thread, then keep follow-ups in that same thread." + +What OpenClaw should do: + +1. Pick `runtime: "acp"`. +2. Resolve the requested harness target (`agentId`, for example `codex`). +3. If thread binding is requested and the current channel supports it, bind the ACP session to the thread. +4. Route follow-up thread messages to that same ACP session until unfocused/closed/expired. + +## ACP versus sub-agents + +Use ACP when you want an external harness runtime. Use sub-agents when you want OpenClaw-native delegated runs. + +| Area | ACP session | Sub-agent run | +| ------------- | ------------------------------------- | ---------------------------------- | +| Runtime | ACP backend plugin (for example acpx) | OpenClaw native sub-agent runtime | +| Session key | `agent::acp:` | `agent::subagent:` | +| Main commands | `/acp ...` | `/subagents ...` | +| Spawn tool | `sessions_spawn` with `runtime:"acp"` | `sessions_spawn` (default runtime) | + +See also [Sub-agents](/tools/subagents). + +## Thread-bound sessions (channel-agnostic) + +When thread bindings are enabled for a channel adapter, ACP sessions can be bound to threads: + +- OpenClaw binds a thread to a target ACP session. +- Follow-up messages in that thread route to the bound ACP session. +- ACP output is delivered back to the same thread. +- Unfocus/close/archive/TTL expiry removes the binding. + +Thread binding support is adapter-specific. If the active channel adapter does not support thread bindings, OpenClaw returns a clear unsupported/unavailable message. + +Required feature flags for thread-bound ACP: + +- `acp.enabled=true` +- `acp.dispatch.enabled=true` +- Channel-adapter ACP thread-spawn flag enabled (adapter-specific) + - Discord: `channels.discord.threadBindings.spawnAcpSessions=true` + +### Thread supporting channels + +- Any channel adapter that exposes session/thread binding capability. +- Current built-in support: Discord. +- Plugin channels can add support through the same binding interface. + +## Start ACP sessions (interfaces) + +### From `sessions_spawn` + +Use `runtime: "acp"` to start an ACP session from an agent turn or tool call. + +```json +{ + "task": "Open the repo and summarize failing tests", + "runtime": "acp", + "agentId": "codex", + "thread": true, + "mode": "session" +} +``` + +Notes: + +- `runtime` defaults to `subagent`, so set `runtime: "acp"` explicitly for ACP sessions. +- If `agentId` is omitted, OpenClaw uses `acp.defaultAgent` when configured. +- `mode: "session"` requires `thread: true` to keep a persistent bound conversation. + +Interface details: + +- `task` (required): initial prompt sent to the ACP session. +- `runtime` (required for ACP): must be `"acp"`. +- `agentId` (optional): ACP target harness id. Falls back to `acp.defaultAgent` if set. +- `thread` (optional, default `false`): request thread binding flow where supported. +- `mode` (optional): `run` (one-shot) or `session` (persistent). + - default is `run` + - if `thread: true` and mode omitted, OpenClaw may default to persistent behavior per runtime path + - `mode: "session"` requires `thread: true` +- `cwd` (optional): requested runtime working directory (validated by backend/runtime policy). +- `label` (optional): operator-facing label used in session/banner text. + +### From `/acp` command + +Use `/acp spawn` for explicit operator control from chat when needed. + +```text +/acp spawn codex --mode persistent --thread auto +/acp spawn codex --mode oneshot --thread off +/acp spawn codex --thread here +``` + +Key flags: + +- `--mode persistent|oneshot` +- `--thread auto|here|off` +- `--cwd ` +- `--label ` + +See [Slash Commands](/tools/slash-commands). + +## ACP controls + +Available command family: + +- `/acp spawn` +- `/acp cancel` +- `/acp steer` +- `/acp close` +- `/acp status` +- `/acp set-mode` +- `/acp set` +- `/acp cwd` +- `/acp permissions` +- `/acp timeout` +- `/acp model` +- `/acp reset-options` +- `/acp sessions` +- `/acp doctor` +- `/acp install` + +`/acp status` shows the effective runtime options and, when available, both runtime-level and backend-level session identifiers. + +Some controls depend on backend capabilities. If a backend does not support a control, OpenClaw returns a clear unsupported-control error. + +## acpx harness support (current) + +Current acpx built-in harness aliases: + +- `pi` +- `claude` +- `codex` +- `opencode` +- `gemini` + +When OpenClaw uses the acpx backend, prefer these values for `agentId` unless your acpx config defines custom agent aliases. + +Direct acpx CLI usage can also target arbitrary adapters via `--agent `, but that raw escape hatch is an acpx CLI feature (not the normal OpenClaw `agentId` path). + +## Required config + +Core ACP baseline: + +```json5 +{ + acp: { + enabled: true, + dispatch: { enabled: true }, + backend: "acpx", + defaultAgent: "codex", + allowedAgents: ["pi", "claude", "codex", "opencode", "gemini"], + maxConcurrentSessions: 8, + stream: { + coalesceIdleMs: 300, + maxChunkChars: 1200, + }, + runtime: { + ttlMinutes: 120, + }, + }, +} +``` + +Thread binding config is channel-adapter specific. Example for Discord: + +```json5 +{ + session: { + threadBindings: { + enabled: true, + ttlHours: 24, + }, + }, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: true, + }, + }, + }, +} +``` + +If thread-bound ACP spawn does not work, verify the adapter feature flag first: + +- Discord: `channels.discord.threadBindings.spawnAcpSessions=true` + +See [Configuration Reference](/gateway/configuration-reference). + +## Plugin setup for acpx backend + +Install and enable plugin: + +```bash +openclaw plugins install @openclaw/acpx +openclaw config set plugins.entries.acpx.enabled true +``` + +Local workspace install during development: + +```bash +openclaw plugins install ./extensions/acpx +``` + +Then verify backend health: + +```text +/acp doctor +``` + +### Pinned acpx install strategy (current behavior) + +`@openclaw/acpx` now enforces a strict plugin-local pinning model: + +1. The extension pins an exact acpx dependency in `extensions/acpx/package.json`. +2. Runtime command is fixed to the plugin-local binary (`extensions/acpx/node_modules/.bin/acpx`), not global `PATH`. +3. Plugin config does not expose `command` or `commandArgs`, so runtime command drift is blocked. +4. Startup registers the ACP backend immediately as not-ready. +5. A background ensure job verifies `acpx --version` against the pinned version. +6. If missing/mismatched, it runs plugin-local install (`npm install --omit=dev --no-save acpx@`) and re-verifies before healthy. + +Notes: + +- OpenClaw startup stays non-blocking while acpx ensure runs. +- If network/install fails, backend remains unavailable and `/acp doctor` reports an actionable fix. + +See [Plugins](/tools/plugin). + +## Troubleshooting + +- Error: `ACP runtime backend is not configured` + Install and enable the configured backend plugin, then run `/acp doctor`. + +- Error: ACP dispatch disabled + Enable `acp.dispatch.enabled=true`. + +- Error: target agent not allowed + Pass an allowed `agentId` or update `acp.allowedAgents`. + +- Error: thread binding unavailable on this channel + Use a channel adapter that supports thread bindings, or run ACP in non-thread mode. + +- Error: missing ACP metadata for a bound session + Recreate the session with `/acp spawn` (or `sessions_spawn` with `runtime:"acp"`) and rebind the thread. diff --git a/docs/tools/index.md b/docs/tools/index.md index 269b6856d038..fa35a63cb7bd 100644 --- a/docs/tools/index.md +++ b/docs/tools/index.md @@ -464,7 +464,7 @@ Core parameters: - `sessions_list`: `kinds?`, `limit?`, `activeMinutes?`, `messageLimit?` (0 = none) - `sessions_history`: `sessionKey` (or `sessionId`), `limit?`, `includeTools?` - `sessions_send`: `sessionKey` (or `sessionId`), `message`, `timeoutSeconds?` (0 = fire-and-forget) -- `sessions_spawn`: `task`, `label?`, `agentId?`, `model?`, `thinking?`, `runTimeoutSeconds?`, `thread?`, `mode?`, `cleanup?` +- `sessions_spawn`: `task`, `label?`, `runtime?`, `agentId?`, `model?`, `thinking?`, `cwd?`, `runTimeoutSeconds?`, `thread?`, `mode?`, `cleanup?` - `session_status`: `sessionKey?` (default current; accepts `sessionId`), `model?` (`default` clears override) Notes: @@ -474,6 +474,7 @@ Notes: - Session targeting is controlled by `tools.sessions.visibility` (default `tree`: current session + spawned subagent sessions). If you run a shared agent for multiple users, consider setting `tools.sessions.visibility: "self"` to prevent cross-session browsing. - `sessions_send` waits for final completion when `timeoutSeconds > 0`. - Delivery/announce happens after completion and is best-effort; `status: "ok"` confirms the agent run finished, not that the announce was delivered. +- `sessions_spawn` supports `runtime: "subagent" | "acp"` (`subagent` default). For ACP runtime behavior, see [ACP Agents](/tools/acp-agents). - `sessions_spawn` starts a sub-agent run and posts an announce reply back to the requester chat. - Supports one-shot mode (`mode: "run"`) and persistent thread-bound mode (`mode: "session"` with `thread: true`). - If `thread: true` and `mode` is omitted, mode defaults to `session`. diff --git a/docs/tools/slash-commands.md b/docs/tools/slash-commands.md index 86dd32a83c8d..4d045d4ee716 100644 --- a/docs/tools/slash-commands.md +++ b/docs/tools/slash-commands.md @@ -80,6 +80,7 @@ Text + native (when enabled): - `/whoami` (show your sender id; alias: `/id`) - `/session ttl ` (manage session-level settings, such as TTL) - `/subagents list|kill|log|info|send|steer|spawn` (inspect, control, or spawn sub-agent runs for the current session) +- `/acp spawn|cancel|steer|close|status|set-mode|set|cwd|permissions|timeout|model|reset-options|doctor|install|sessions` (inspect and control ACP runtime sessions) - `/agents` (list thread-bound agents for this session) - `/focus ` (Discord: bind this thread, or a new thread, to a session/subagent target) - `/unfocus` (Discord: remove the current thread binding) @@ -125,6 +126,7 @@ Notes: - `/restart` is enabled by default; set `commands.restart: false` to disable it. - Discord-only native command: `/vc join|leave|status` controls voice channels (requires `channels.discord.voice` and native commands; not available as text). - Discord thread-binding commands (`/focus`, `/unfocus`, `/agents`, `/session ttl`) require effective thread bindings to be enabled (`session.threadBindings.enabled` and/or `channels.discord.threadBindings.enabled`). +- ACP command reference and runtime behavior: [ACP Agents](/tools/acp-agents). - `/verbose` is meant for debugging and extra visibility; keep it **off** in normal use. - Tool failure summaries are still shown when relevant, but detailed failure text is only included when `/verbose` is `on` or `full`. - `/reasoning` (and `/verbose`) are risky in group settings: they may reveal internal reasoning or tool output you did not intend to expose. Prefer leaving them off, especially in group chats. diff --git a/docs/tools/subagents.md b/docs/tools/subagents.md index 9542858c8402..8d066a94e7f3 100644 --- a/docs/tools/subagents.md +++ b/docs/tools/subagents.md @@ -51,6 +51,7 @@ These commands work on channels that support persistent thread bindings. See **T - `--model` and `--thinking` override defaults for that specific run. - Use `info`/`log` to inspect details and output after completion. - `/subagents spawn` is one-shot mode (`mode: "run"`). For persistent thread-bound sessions, use `sessions_spawn` with `thread: true` and `mode: "session"`. +- For ACP harness sessions (Codex, Claude Code, Gemini CLI), use `sessions_spawn` with `runtime: "acp"` and see [ACP Agents](/tools/acp-agents). Primary goals: diff --git a/extensions/acpx/index.ts b/extensions/acpx/index.ts new file mode 100644 index 000000000000..5f57e396f801 --- /dev/null +++ b/extensions/acpx/index.ts @@ -0,0 +1,19 @@ +import type { OpenClawPluginApi } from "openclaw/plugin-sdk"; +import { createAcpxPluginConfigSchema } from "./src/config.js"; +import { createAcpxRuntimeService } from "./src/service.js"; + +const plugin = { + id: "acpx", + name: "ACPX Runtime", + description: "ACP runtime backend powered by the acpx CLI.", + configSchema: createAcpxPluginConfigSchema(), + register(api: OpenClawPluginApi) { + api.registerService( + createAcpxRuntimeService({ + pluginConfig: api.pluginConfig, + }), + ); + }, +}; + +export default plugin; diff --git a/extensions/acpx/openclaw.plugin.json b/extensions/acpx/openclaw.plugin.json new file mode 100644 index 000000000000..61790e6ca05d --- /dev/null +++ b/extensions/acpx/openclaw.plugin.json @@ -0,0 +1,55 @@ +{ + "id": "acpx", + "name": "ACPX Runtime", + "description": "ACP runtime backend powered by a pinned plugin-local acpx CLI.", + "skills": ["./skills"], + "configSchema": { + "type": "object", + "additionalProperties": false, + "properties": { + "cwd": { + "type": "string" + }, + "permissionMode": { + "type": "string", + "enum": ["approve-all", "approve-reads", "deny-all"] + }, + "nonInteractivePermissions": { + "type": "string", + "enum": ["deny", "fail"] + }, + "timeoutSeconds": { + "type": "number", + "minimum": 0.001 + }, + "queueOwnerTtlSeconds": { + "type": "number", + "minimum": 0 + } + } + }, + "uiHints": { + "cwd": { + "label": "Default Working Directory", + "help": "Default cwd for ACP session operations when not set per session." + }, + "permissionMode": { + "label": "Permission Mode", + "help": "Default acpx permission policy for runtime prompts." + }, + "nonInteractivePermissions": { + "label": "Non-Interactive Permission Policy", + "help": "acpx policy when interactive permission prompts are unavailable." + }, + "timeoutSeconds": { + "label": "Prompt Timeout Seconds", + "help": "Optional acpx timeout for each runtime turn.", + "advanced": true + }, + "queueOwnerTtlSeconds": { + "label": "Queue Owner TTL Seconds", + "help": "Idle queue-owner TTL for acpx prompt turns. Keep this short in OpenClaw to avoid delayed completion after each turn.", + "advanced": true + } + } +} diff --git a/extensions/acpx/package.json b/extensions/acpx/package.json new file mode 100644 index 000000000000..7f77d8a04ac5 --- /dev/null +++ b/extensions/acpx/package.json @@ -0,0 +1,14 @@ +{ + "name": "@openclaw/acpx", + "version": "2026.2.22", + "description": "OpenClaw ACP runtime backend via acpx", + "type": "module", + "dependencies": { + "acpx": "^0.1.13" + }, + "openclaw": { + "extensions": [ + "./index.ts" + ] + } +} diff --git a/extensions/acpx/skills/acp-router/SKILL.md b/extensions/acpx/skills/acp-router/SKILL.md new file mode 100644 index 000000000000..c80978fa8ae6 --- /dev/null +++ b/extensions/acpx/skills/acp-router/SKILL.md @@ -0,0 +1,209 @@ +--- +name: acp-router +description: Route plain-language requests for Pi, Claude Code, Codex, OpenCode, Gemini CLI, or ACP harness work into either OpenClaw ACP runtime sessions or direct acpx-driven sessions ("telephone game" flow). +user-invocable: false +--- + +# ACP Harness Router + +When user intent is "run this in Pi/Claude Code/Codex/OpenCode/Gemini (ACP harness)", do not use subagent runtime or PTY scraping. Route through ACP-aware flows. + +## Intent detection + +Trigger this skill when the user asks OpenClaw to: + +- run something in Pi / Claude Code / Codex / OpenCode / Gemini +- continue existing harness work +- relay instructions to an external coding harness +- keep an external harness conversation in a thread-like conversation + +## Mode selection + +Choose one of these paths: + +1. OpenClaw ACP runtime path (default): use `sessions_spawn` / ACP runtime tools. +2. Direct `acpx` path (telephone game): use `acpx` CLI through `exec` to drive the harness session directly. + +Use direct `acpx` when one of these is true: + +- user explicitly asks for direct `acpx` driving +- ACP runtime/plugin path is unavailable or unhealthy +- the task is "just relay prompts to harness" and no OpenClaw ACP lifecycle features are needed + +Do not use: + +- `subagents` runtime for harness control +- `/acp` command delegation as a requirement for the user +- PTY scraping of pi/claude/codex/opencode/gemini CLIs when `acpx` is available + +## AgentId mapping + +Use these defaults when user names a harness directly: + +- "pi" -> `agentId: "pi"` +- "claude" or "claude code" -> `agentId: "claude"` +- "codex" -> `agentId: "codex"` +- "opencode" -> `agentId: "opencode"` +- "gemini" or "gemini cli" -> `agentId: "gemini"` + +These defaults match current acpx built-in aliases. + +If policy rejects the chosen id, report the policy error clearly and ask for the allowed ACP agent id. + +## OpenClaw ACP runtime path + +Required behavior: + +1. Use `sessions_spawn` with: + - `runtime: "acp"` + - `thread: true` + - `mode: "session"` (unless user explicitly wants one-shot) +2. Put requested work in `task` so the ACP session gets it immediately. +3. Set `agentId` explicitly unless ACP default agent is known. +4. Do not ask user to run slash commands or CLI when this path works directly. + +Example: + +User: "spawn a test codex session in thread and tell it to say hi" + +Call: + +```json +{ + "task": "Say hi.", + "runtime": "acp", + "agentId": "codex", + "thread": true, + "mode": "session" +} +``` + +## Thread spawn recovery policy + +When the user asks to start a coding harness in a thread (for example "start a codex/claude/pi thread"), treat that as an ACP runtime request and try to satisfy it end-to-end. + +Required behavior when ACP backend is unavailable: + +1. Do not immediately ask the user to pick an alternate path. +2. First attempt automatic local repair: + - ensure plugin-local pinned acpx is installed in `extensions/acpx` + - verify `${ACPX_CMD} --version` +3. After reinstall/repair, restart the gateway and explicitly offer to run that restart for the user. +4. Retry ACP thread spawn once after repair. +5. Only if repair+retry fails, report the concrete error and then offer fallback options. + +When offering fallback, keep ACP first: + +- Option 1: retry ACP spawn after showing exact failing step +- Option 2: direct acpx telephone-game flow + +Do not default to subagent runtime for these requests. + +## ACPX install and version policy (direct acpx path) + +For this repo, direct `acpx` calls must follow the same pinned policy as the `@openclaw/acpx` extension. + +1. Prefer plugin-local binary, not global PATH: + - `./extensions/acpx/node_modules/.bin/acpx` +2. Resolve pinned version from extension dependency: + - `node -e "console.log(require('./extensions/acpx/package.json').dependencies.acpx)"` +3. If binary is missing or version mismatched, install plugin-local pinned version: + - `cd extensions/acpx && npm install --omit=dev --no-save acpx@` +4. Verify before use: + - `./extensions/acpx/node_modules/.bin/acpx --version` +5. If install/repair changed ACPX artifacts, restart the gateway and offer to run the restart. +6. Do not run `npm install -g acpx` unless the user explicitly asks for global install. + +Set and reuse: + +```bash +ACPX_CMD="./extensions/acpx/node_modules/.bin/acpx" +``` + +## Direct acpx path ("telephone game") + +Use this path to drive harness sessions without `/acp` or subagent runtime. + +### Rules + +1. Use `exec` commands that call `${ACPX_CMD}`. +2. Reuse a stable session name per conversation so follow-up prompts stay in the same harness context. +3. Prefer `--format quiet` for clean assistant text to relay back to user. +4. Use `exec` (one-shot) only when the user wants one-shot behavior. +5. Keep working directory explicit (`--cwd`) when task scope depends on repo context. + +### Session naming + +Use a deterministic name, for example: + +- `oc--` + +Where `conversationId` is thread id when available, otherwise channel/conversation id. + +### Command templates + +Persistent session (create if missing, then prompt): + +```bash +${ACPX_CMD} codex sessions show oc-codex- \ + || ${ACPX_CMD} codex sessions new --name oc-codex- + +${ACPX_CMD} codex -s oc-codex- --cwd --format quiet "" +``` + +One-shot: + +```bash +${ACPX_CMD} codex exec --cwd --format quiet "" +``` + +Cancel in-flight turn: + +```bash +${ACPX_CMD} codex cancel -s oc-codex- +``` + +Close session: + +```bash +${ACPX_CMD} codex sessions close oc-codex- +``` + +### Harness aliases in acpx + +- `pi` +- `claude` +- `codex` +- `opencode` +- `gemini` + +### Built-in adapter commands in acpx + +Defaults are: + +- `pi -> npx pi-acp` +- `claude -> npx -y @zed-industries/claude-agent-acp` +- `codex -> npx @zed-industries/codex-acp` +- `opencode -> npx -y opencode-ai acp` +- `gemini -> gemini` + +If `~/.acpx/config.json` overrides `agents`, those overrides replace defaults. + +### Failure handling + +- `acpx: command not found`: + - for thread-spawn ACP requests, install plugin-local pinned acpx in `extensions/acpx` immediately + - restart gateway after install and offer to run the restart automatically + - then retry once + - do not ask for install permission first unless policy explicitly requires it + - do not install global `acpx` unless explicitly requested +- adapter command missing (for example `claude-agent-acp` not found): + - for thread-spawn ACP requests, first restore built-in defaults by removing broken `~/.acpx/config.json` agent overrides + - then retry once before offering fallback + - if user wants binary-based overrides, install exactly the configured adapter binary +- `NO_SESSION`: run `${ACPX_CMD} sessions new --name ` then retry prompt. +- queue busy: either wait for completion (default) or use `--no-wait` when async behavior is explicitly desired. + +### Output relay + +When relaying to user, return the final assistant text output from `acpx` command result. Avoid relaying raw local tool noise unless user asked for verbose logs. diff --git a/extensions/acpx/src/config.test.ts b/extensions/acpx/src/config.test.ts new file mode 100644 index 000000000000..efd6d5c7e730 --- /dev/null +++ b/extensions/acpx/src/config.test.ts @@ -0,0 +1,53 @@ +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { + ACPX_BUNDLED_BIN, + createAcpxPluginConfigSchema, + resolveAcpxPluginConfig, +} from "./config.js"; + +describe("acpx plugin config parsing", () => { + it("resolves a strict plugin-local acpx command", () => { + const resolved = resolveAcpxPluginConfig({ + rawConfig: { + cwd: "/tmp/workspace", + }, + workspaceDir: "/tmp/workspace", + }); + + expect(resolved.command).toBe(ACPX_BUNDLED_BIN); + expect(resolved.cwd).toBe(path.resolve("/tmp/workspace")); + }); + + it("rejects command overrides", () => { + expect(() => + resolveAcpxPluginConfig({ + rawConfig: { + command: "acpx-custom", + }, + workspaceDir: "/tmp/workspace", + }), + ).toThrow("unknown config key: command"); + }); + + it("rejects commandArgs overrides", () => { + expect(() => + resolveAcpxPluginConfig({ + rawConfig: { + commandArgs: ["--foo"], + }, + workspaceDir: "/tmp/workspace", + }), + ).toThrow("unknown config key: commandArgs"); + }); + + it("schema rejects empty cwd", () => { + const schema = createAcpxPluginConfigSchema(); + if (!schema.safeParse) { + throw new Error("acpx config schema missing safeParse"); + } + const parsed = schema.safeParse({ cwd: " " }); + + expect(parsed.success).toBe(false); + }); +}); diff --git a/extensions/acpx/src/config.ts b/extensions/acpx/src/config.ts new file mode 100644 index 000000000000..bf5d0e0993ee --- /dev/null +++ b/extensions/acpx/src/config.ts @@ -0,0 +1,196 @@ +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import type { OpenClawPluginConfigSchema } from "openclaw/plugin-sdk"; + +export const ACPX_PERMISSION_MODES = ["approve-all", "approve-reads", "deny-all"] as const; +export type AcpxPermissionMode = (typeof ACPX_PERMISSION_MODES)[number]; + +export const ACPX_NON_INTERACTIVE_POLICIES = ["deny", "fail"] as const; +export type AcpxNonInteractivePermissionPolicy = (typeof ACPX_NON_INTERACTIVE_POLICIES)[number]; + +export const ACPX_PINNED_VERSION = "0.1.13"; +const ACPX_BIN_NAME = process.platform === "win32" ? "acpx.cmd" : "acpx"; +export const ACPX_PLUGIN_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); +export const ACPX_BUNDLED_BIN = path.join(ACPX_PLUGIN_ROOT, "node_modules", ".bin", ACPX_BIN_NAME); +export const ACPX_LOCAL_INSTALL_COMMAND = `npm install --omit=dev --no-save acpx@${ACPX_PINNED_VERSION}`; + +export type AcpxPluginConfig = { + cwd?: string; + permissionMode?: AcpxPermissionMode; + nonInteractivePermissions?: AcpxNonInteractivePermissionPolicy; + timeoutSeconds?: number; + queueOwnerTtlSeconds?: number; +}; + +export type ResolvedAcpxPluginConfig = { + command: string; + cwd: string; + permissionMode: AcpxPermissionMode; + nonInteractivePermissions: AcpxNonInteractivePermissionPolicy; + timeoutSeconds?: number; + queueOwnerTtlSeconds: number; +}; + +const DEFAULT_PERMISSION_MODE: AcpxPermissionMode = "approve-reads"; +const DEFAULT_NON_INTERACTIVE_POLICY: AcpxNonInteractivePermissionPolicy = "fail"; +const DEFAULT_QUEUE_OWNER_TTL_SECONDS = 0.1; + +type ParseResult = + | { ok: true; value: AcpxPluginConfig | undefined } + | { ok: false; message: string }; + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function isPermissionMode(value: string): value is AcpxPermissionMode { + return ACPX_PERMISSION_MODES.includes(value as AcpxPermissionMode); +} + +function isNonInteractivePermissionPolicy( + value: string, +): value is AcpxNonInteractivePermissionPolicy { + return ACPX_NON_INTERACTIVE_POLICIES.includes(value as AcpxNonInteractivePermissionPolicy); +} + +function parseAcpxPluginConfig(value: unknown): ParseResult { + if (value === undefined) { + return { ok: true, value: undefined }; + } + if (!isRecord(value)) { + return { ok: false, message: "expected config object" }; + } + const allowedKeys = new Set([ + "cwd", + "permissionMode", + "nonInteractivePermissions", + "timeoutSeconds", + "queueOwnerTtlSeconds", + ]); + for (const key of Object.keys(value)) { + if (!allowedKeys.has(key)) { + return { ok: false, message: `unknown config key: ${key}` }; + } + } + + const cwd = value.cwd; + if (cwd !== undefined && (typeof cwd !== "string" || cwd.trim() === "")) { + return { ok: false, message: "cwd must be a non-empty string" }; + } + + const permissionMode = value.permissionMode; + if ( + permissionMode !== undefined && + (typeof permissionMode !== "string" || !isPermissionMode(permissionMode)) + ) { + return { + ok: false, + message: `permissionMode must be one of: ${ACPX_PERMISSION_MODES.join(", ")}`, + }; + } + + const nonInteractivePermissions = value.nonInteractivePermissions; + if ( + nonInteractivePermissions !== undefined && + (typeof nonInteractivePermissions !== "string" || + !isNonInteractivePermissionPolicy(nonInteractivePermissions)) + ) { + return { + ok: false, + message: `nonInteractivePermissions must be one of: ${ACPX_NON_INTERACTIVE_POLICIES.join(", ")}`, + }; + } + + const timeoutSeconds = value.timeoutSeconds; + if ( + timeoutSeconds !== undefined && + (typeof timeoutSeconds !== "number" || !Number.isFinite(timeoutSeconds) || timeoutSeconds <= 0) + ) { + return { ok: false, message: "timeoutSeconds must be a positive number" }; + } + + const queueOwnerTtlSeconds = value.queueOwnerTtlSeconds; + if ( + queueOwnerTtlSeconds !== undefined && + (typeof queueOwnerTtlSeconds !== "number" || + !Number.isFinite(queueOwnerTtlSeconds) || + queueOwnerTtlSeconds < 0) + ) { + return { ok: false, message: "queueOwnerTtlSeconds must be a non-negative number" }; + } + + return { + ok: true, + value: { + cwd: typeof cwd === "string" ? cwd.trim() : undefined, + permissionMode: typeof permissionMode === "string" ? permissionMode : undefined, + nonInteractivePermissions: + typeof nonInteractivePermissions === "string" ? nonInteractivePermissions : undefined, + timeoutSeconds: typeof timeoutSeconds === "number" ? timeoutSeconds : undefined, + queueOwnerTtlSeconds: + typeof queueOwnerTtlSeconds === "number" ? queueOwnerTtlSeconds : undefined, + }, + }; +} + +export function createAcpxPluginConfigSchema(): OpenClawPluginConfigSchema { + return { + safeParse(value: unknown): + | { success: true; data?: unknown } + | { + success: false; + error: { issues: Array<{ path: Array; message: string }> }; + } { + const parsed = parseAcpxPluginConfig(value); + if (parsed.ok) { + return { success: true, data: parsed.value }; + } + return { + success: false, + error: { + issues: [{ path: [], message: parsed.message }], + }, + }; + }, + jsonSchema: { + type: "object", + additionalProperties: false, + properties: { + cwd: { type: "string" }, + permissionMode: { + type: "string", + enum: [...ACPX_PERMISSION_MODES], + }, + nonInteractivePermissions: { + type: "string", + enum: [...ACPX_NON_INTERACTIVE_POLICIES], + }, + timeoutSeconds: { type: "number", minimum: 0.001 }, + queueOwnerTtlSeconds: { type: "number", minimum: 0 }, + }, + }, + }; +} + +export function resolveAcpxPluginConfig(params: { + rawConfig: unknown; + workspaceDir?: string; +}): ResolvedAcpxPluginConfig { + const parsed = parseAcpxPluginConfig(params.rawConfig); + if (!parsed.ok) { + throw new Error(parsed.message); + } + const normalized = parsed.value ?? {}; + const fallbackCwd = params.workspaceDir?.trim() || process.cwd(); + const cwd = path.resolve(normalized.cwd?.trim() || fallbackCwd); + + return { + command: ACPX_BUNDLED_BIN, + cwd, + permissionMode: normalized.permissionMode ?? DEFAULT_PERMISSION_MODE, + nonInteractivePermissions: + normalized.nonInteractivePermissions ?? DEFAULT_NON_INTERACTIVE_POLICY, + timeoutSeconds: normalized.timeoutSeconds, + queueOwnerTtlSeconds: normalized.queueOwnerTtlSeconds ?? DEFAULT_QUEUE_OWNER_TTL_SECONDS, + }; +} diff --git a/extensions/acpx/src/ensure.test.ts b/extensions/acpx/src/ensure.test.ts new file mode 100644 index 000000000000..0b36c3def365 --- /dev/null +++ b/extensions/acpx/src/ensure.test.ts @@ -0,0 +1,125 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { ACPX_LOCAL_INSTALL_COMMAND, ACPX_PINNED_VERSION } from "./config.js"; + +const { resolveSpawnFailureMock, spawnAndCollectMock } = vi.hoisted(() => ({ + resolveSpawnFailureMock: vi.fn(() => null), + spawnAndCollectMock: vi.fn(), +})); + +vi.mock("./runtime-internals/process.js", () => ({ + resolveSpawnFailure: resolveSpawnFailureMock, + spawnAndCollect: spawnAndCollectMock, +})); + +import { checkPinnedAcpxVersion, ensurePinnedAcpx } from "./ensure.js"; + +describe("acpx ensure", () => { + beforeEach(() => { + resolveSpawnFailureMock.mockReset(); + resolveSpawnFailureMock.mockReturnValue(null); + spawnAndCollectMock.mockReset(); + }); + + it("accepts the pinned acpx version", async () => { + spawnAndCollectMock.mockResolvedValueOnce({ + stdout: `acpx ${ACPX_PINNED_VERSION}\n`, + stderr: "", + code: 0, + error: null, + }); + + const result = await checkPinnedAcpxVersion({ + command: "/plugin/node_modules/.bin/acpx", + cwd: "/plugin", + expectedVersion: ACPX_PINNED_VERSION, + }); + + expect(result).toEqual({ + ok: true, + version: ACPX_PINNED_VERSION, + expectedVersion: ACPX_PINNED_VERSION, + }); + }); + + it("reports version mismatch", async () => { + spawnAndCollectMock.mockResolvedValueOnce({ + stdout: "acpx 0.0.9\n", + stderr: "", + code: 0, + error: null, + }); + + const result = await checkPinnedAcpxVersion({ + command: "/plugin/node_modules/.bin/acpx", + cwd: "/plugin", + expectedVersion: ACPX_PINNED_VERSION, + }); + + expect(result).toMatchObject({ + ok: false, + reason: "version-mismatch", + expectedVersion: ACPX_PINNED_VERSION, + installedVersion: "0.0.9", + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }); + }); + + it("installs and verifies pinned acpx when precheck fails", async () => { + spawnAndCollectMock + .mockResolvedValueOnce({ + stdout: "acpx 0.0.9\n", + stderr: "", + code: 0, + error: null, + }) + .mockResolvedValueOnce({ + stdout: "added 1 package\n", + stderr: "", + code: 0, + error: null, + }) + .mockResolvedValueOnce({ + stdout: `acpx ${ACPX_PINNED_VERSION}\n`, + stderr: "", + code: 0, + error: null, + }); + + await ensurePinnedAcpx({ + command: "/plugin/node_modules/.bin/acpx", + pluginRoot: "/plugin", + expectedVersion: ACPX_PINNED_VERSION, + }); + + expect(spawnAndCollectMock).toHaveBeenCalledTimes(3); + expect(spawnAndCollectMock.mock.calls[1]?.[0]).toMatchObject({ + command: "npm", + args: ["install", "--omit=dev", "--no-save", `acpx@${ACPX_PINNED_VERSION}`], + cwd: "/plugin", + }); + }); + + it("fails with actionable error when npm install fails", async () => { + spawnAndCollectMock + .mockResolvedValueOnce({ + stdout: "acpx 0.0.9\n", + stderr: "", + code: 0, + error: null, + }) + .mockResolvedValueOnce({ + stdout: "", + stderr: "network down", + code: 1, + error: null, + }); + + await expect( + ensurePinnedAcpx({ + command: "/plugin/node_modules/.bin/acpx", + pluginRoot: "/plugin", + expectedVersion: ACPX_PINNED_VERSION, + }), + ).rejects.toThrow("failed to install plugin-local acpx"); + }); +}); diff --git a/extensions/acpx/src/ensure.ts b/extensions/acpx/src/ensure.ts new file mode 100644 index 000000000000..6bb015587ae7 --- /dev/null +++ b/extensions/acpx/src/ensure.ts @@ -0,0 +1,169 @@ +import type { PluginLogger } from "openclaw/plugin-sdk"; +import { ACPX_LOCAL_INSTALL_COMMAND, ACPX_PINNED_VERSION, ACPX_PLUGIN_ROOT } from "./config.js"; +import { resolveSpawnFailure, spawnAndCollect } from "./runtime-internals/process.js"; + +const SEMVER_PATTERN = /\b\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?\b/; + +export type AcpxVersionCheckResult = + | { + ok: true; + version: string; + expectedVersion: string; + } + | { + ok: false; + reason: "missing-command" | "missing-version" | "version-mismatch" | "execution-failed"; + message: string; + expectedVersion: string; + installCommand: string; + installedVersion?: string; + }; + +function extractVersion(stdout: string, stderr: string): string | null { + const combined = `${stdout}\n${stderr}`; + const match = combined.match(SEMVER_PATTERN); + return match?.[0] ?? null; +} + +export async function checkPinnedAcpxVersion(params: { + command: string; + cwd?: string; + expectedVersion?: string; +}): Promise { + const expectedVersion = params.expectedVersion ?? ACPX_PINNED_VERSION; + const cwd = params.cwd ?? ACPX_PLUGIN_ROOT; + const result = await spawnAndCollect({ + command: params.command, + args: ["--version"], + cwd, + }); + + if (result.error) { + const spawnFailure = resolveSpawnFailure(result.error, cwd); + if (spawnFailure === "missing-command") { + return { + ok: false, + reason: "missing-command", + message: `acpx command not found at ${params.command}`, + expectedVersion, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }; + } + return { + ok: false, + reason: "execution-failed", + message: result.error.message, + expectedVersion, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }; + } + + if ((result.code ?? 0) !== 0) { + const stderr = result.stderr.trim(); + return { + ok: false, + reason: "execution-failed", + message: stderr || `acpx --version failed with code ${result.code ?? "unknown"}`, + expectedVersion, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }; + } + + const installedVersion = extractVersion(result.stdout, result.stderr); + if (!installedVersion) { + return { + ok: false, + reason: "missing-version", + message: "acpx --version output did not include a parseable version", + expectedVersion, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }; + } + + if (installedVersion !== expectedVersion) { + return { + ok: false, + reason: "version-mismatch", + message: `acpx version mismatch: found ${installedVersion}, expected ${expectedVersion}`, + expectedVersion, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + installedVersion, + }; + } + + return { + ok: true, + version: installedVersion, + expectedVersion, + }; +} + +let pendingEnsure: Promise | null = null; + +export async function ensurePinnedAcpx(params: { + command: string; + logger?: PluginLogger; + pluginRoot?: string; + expectedVersion?: string; +}): Promise { + if (pendingEnsure) { + return await pendingEnsure; + } + + pendingEnsure = (async () => { + const pluginRoot = params.pluginRoot ?? ACPX_PLUGIN_ROOT; + const expectedVersion = params.expectedVersion ?? ACPX_PINNED_VERSION; + + const precheck = await checkPinnedAcpxVersion({ + command: params.command, + cwd: pluginRoot, + expectedVersion, + }); + if (precheck.ok) { + return; + } + + params.logger?.warn( + `acpx local binary unavailable or mismatched (${precheck.message}); running plugin-local install`, + ); + + const install = await spawnAndCollect({ + command: "npm", + args: ["install", "--omit=dev", "--no-save", `acpx@${expectedVersion}`], + cwd: pluginRoot, + }); + + if (install.error) { + const spawnFailure = resolveSpawnFailure(install.error, pluginRoot); + if (spawnFailure === "missing-command") { + throw new Error("npm is required to install plugin-local acpx but was not found on PATH"); + } + throw new Error(`failed to install plugin-local acpx: ${install.error.message}`); + } + + if ((install.code ?? 0) !== 0) { + const stderr = install.stderr.trim(); + const stdout = install.stdout.trim(); + const detail = stderr || stdout || `npm exited with code ${install.code ?? "unknown"}`; + throw new Error(`failed to install plugin-local acpx: ${detail}`); + } + + const postcheck = await checkPinnedAcpxVersion({ + command: params.command, + cwd: pluginRoot, + expectedVersion, + }); + + if (!postcheck.ok) { + throw new Error(`plugin-local acpx verification failed after install: ${postcheck.message}`); + } + + params.logger?.info(`acpx plugin-local binary ready (version ${postcheck.version})`); + })(); + + try { + await pendingEnsure; + } finally { + pendingEnsure = null; + } +} diff --git a/extensions/acpx/src/runtime-internals/events.ts b/extensions/acpx/src/runtime-internals/events.ts new file mode 100644 index 000000000000..074787b3fdff --- /dev/null +++ b/extensions/acpx/src/runtime-internals/events.ts @@ -0,0 +1,140 @@ +import type { AcpRuntimeEvent } from "openclaw/plugin-sdk"; +import { + asOptionalBoolean, + asOptionalString, + asString, + asTrimmedString, + type AcpxErrorEvent, + type AcpxJsonObject, + isRecord, +} from "./shared.js"; + +export function toAcpxErrorEvent(value: unknown): AcpxErrorEvent | null { + if (!isRecord(value)) { + return null; + } + if (asTrimmedString(value.type) !== "error") { + return null; + } + return { + message: asTrimmedString(value.message) || "acpx reported an error", + code: asOptionalString(value.code), + retryable: asOptionalBoolean(value.retryable), + }; +} + +export function parseJsonLines(value: string): AcpxJsonObject[] { + const events: AcpxJsonObject[] = []; + for (const line of value.split(/\r?\n/)) { + const trimmed = line.trim(); + if (!trimmed) { + continue; + } + try { + const parsed = JSON.parse(trimmed) as unknown; + if (isRecord(parsed)) { + events.push(parsed); + } + } catch { + // Ignore malformed lines; callers handle missing typed events via exit code. + } + } + return events; +} + +export function parsePromptEventLine(line: string): AcpRuntimeEvent | null { + const trimmed = line.trim(); + if (!trimmed) { + return null; + } + let parsed: unknown; + try { + parsed = JSON.parse(trimmed); + } catch { + return { + type: "status", + text: trimmed, + }; + } + + if (!isRecord(parsed)) { + return null; + } + + const type = asTrimmedString(parsed.type); + switch (type) { + case "text": { + const content = asString(parsed.content); + if (content == null || content.length === 0) { + return null; + } + return { + type: "text_delta", + text: content, + stream: "output", + }; + } + case "thought": { + const content = asString(parsed.content); + if (content == null || content.length === 0) { + return null; + } + return { + type: "text_delta", + text: content, + stream: "thought", + }; + } + case "tool_call": { + const title = asTrimmedString(parsed.title) || asTrimmedString(parsed.toolCallId) || "tool"; + const status = asTrimmedString(parsed.status); + return { + type: "tool_call", + text: status ? `${title} (${status})` : title, + }; + } + case "client_operation": { + const method = asTrimmedString(parsed.method) || "operation"; + const status = asTrimmedString(parsed.status); + const summary = asTrimmedString(parsed.summary); + const text = [method, status, summary].filter(Boolean).join(" "); + if (!text) { + return null; + } + return { type: "status", text }; + } + case "plan": { + const entries = Array.isArray(parsed.entries) ? parsed.entries : []; + const first = entries.find((entry) => isRecord(entry)) as Record | undefined; + const content = asTrimmedString(first?.content); + if (!content) { + return null; + } + return { type: "status", text: `plan: ${content}` }; + } + case "update": { + const update = asTrimmedString(parsed.update); + if (!update) { + return null; + } + return { type: "status", text: update }; + } + case "done": { + return { + type: "done", + stopReason: asOptionalString(parsed.stopReason), + }; + } + case "error": { + const message = asTrimmedString(parsed.message) || "acpx runtime error"; + return { + type: "error", + message, + code: asOptionalString(parsed.code), + retryable: asOptionalBoolean(parsed.retryable), + }; + } + default: + return null; + } +} diff --git a/extensions/acpx/src/runtime-internals/process.ts b/extensions/acpx/src/runtime-internals/process.ts new file mode 100644 index 000000000000..752b48835ec7 --- /dev/null +++ b/extensions/acpx/src/runtime-internals/process.ts @@ -0,0 +1,137 @@ +import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process"; +import { existsSync } from "node:fs"; +import path from "node:path"; + +export type SpawnExit = { + code: number | null; + signal: NodeJS.Signals | null; + error: Error | null; +}; + +type ResolvedSpawnCommand = { + command: string; + args: string[]; + shell?: boolean; +}; + +function resolveSpawnCommand(params: { command: string; args: string[] }): ResolvedSpawnCommand { + if (process.platform !== "win32") { + return { command: params.command, args: params.args }; + } + + const extension = path.extname(params.command).toLowerCase(); + if (extension === ".js" || extension === ".cjs" || extension === ".mjs") { + return { + command: process.execPath, + args: [params.command, ...params.args], + }; + } + + if (extension === ".cmd" || extension === ".bat") { + return { + command: params.command, + args: params.args, + shell: true, + }; + } + + return { + command: params.command, + args: params.args, + }; +} + +export function spawnWithResolvedCommand(params: { + command: string; + args: string[]; + cwd: string; +}): ChildProcessWithoutNullStreams { + const resolved = resolveSpawnCommand({ + command: params.command, + args: params.args, + }); + + return spawn(resolved.command, resolved.args, { + cwd: params.cwd, + env: process.env, + stdio: ["pipe", "pipe", "pipe"], + shell: resolved.shell, + }); +} + +export async function waitForExit(child: ChildProcessWithoutNullStreams): Promise { + return await new Promise((resolve) => { + let settled = false; + const finish = (result: SpawnExit) => { + if (settled) { + return; + } + settled = true; + resolve(result); + }; + + child.once("error", (err) => { + finish({ code: null, signal: null, error: err }); + }); + + child.once("close", (code, signal) => { + finish({ code, signal, error: null }); + }); + }); +} + +export async function spawnAndCollect(params: { + command: string; + args: string[]; + cwd: string; +}): Promise<{ + stdout: string; + stderr: string; + code: number | null; + error: Error | null; +}> { + const child = spawnWithResolvedCommand(params); + child.stdin.end(); + + let stdout = ""; + let stderr = ""; + child.stdout.on("data", (chunk) => { + stdout += String(chunk); + }); + child.stderr.on("data", (chunk) => { + stderr += String(chunk); + }); + + const exit = await waitForExit(child); + return { + stdout, + stderr, + code: exit.code, + error: exit.error, + }; +} + +export function resolveSpawnFailure( + err: unknown, + cwd: string, +): "missing-command" | "missing-cwd" | null { + if (!err || typeof err !== "object") { + return null; + } + const code = (err as NodeJS.ErrnoException).code; + if (code !== "ENOENT") { + return null; + } + return directoryExists(cwd) ? "missing-command" : "missing-cwd"; +} + +function directoryExists(cwd: string): boolean { + if (!cwd) { + return false; + } + try { + return existsSync(cwd); + } catch { + return false; + } +} diff --git a/extensions/acpx/src/runtime-internals/shared.ts b/extensions/acpx/src/runtime-internals/shared.ts new file mode 100644 index 000000000000..2f9b48025e66 --- /dev/null +++ b/extensions/acpx/src/runtime-internals/shared.ts @@ -0,0 +1,56 @@ +import type { ResolvedAcpxPluginConfig } from "../config.js"; + +export type AcpxHandleState = { + name: string; + agent: string; + cwd: string; + mode: "persistent" | "oneshot"; + acpxRecordId?: string; + backendSessionId?: string; + agentSessionId?: string; +}; + +export type AcpxJsonObject = Record; + +export type AcpxErrorEvent = { + message: string; + code?: string; + retryable?: boolean; +}; + +export function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +export function asTrimmedString(value: unknown): string { + return typeof value === "string" ? value.trim() : ""; +} + +export function asString(value: unknown): string | undefined { + return typeof value === "string" ? value : undefined; +} + +export function asOptionalString(value: unknown): string | undefined { + const text = asTrimmedString(value); + return text || undefined; +} + +export function asOptionalBoolean(value: unknown): boolean | undefined { + return typeof value === "boolean" ? value : undefined; +} + +export function deriveAgentFromSessionKey(sessionKey: string, fallbackAgent: string): string { + const match = sessionKey.match(/^agent:([^:]+):/i); + const candidate = match?.[1] ? asTrimmedString(match[1]) : ""; + return candidate || fallbackAgent; +} + +export function buildPermissionArgs(mode: ResolvedAcpxPluginConfig["permissionMode"]): string[] { + if (mode === "approve-all") { + return ["--approve-all"]; + } + if (mode === "deny-all") { + return ["--deny-all"]; + } + return ["--approve-reads"]; +} diff --git a/extensions/acpx/src/runtime.test.ts b/extensions/acpx/src/runtime.test.ts new file mode 100644 index 000000000000..d5e4fd275c79 --- /dev/null +++ b/extensions/acpx/src/runtime.test.ts @@ -0,0 +1,619 @@ +import fs from "node:fs"; +import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { runAcpRuntimeAdapterContract } from "../../../src/acp/runtime/adapter-contract.testkit.js"; +import { ACPX_PINNED_VERSION, type ResolvedAcpxPluginConfig } from "./config.js"; +import { AcpxRuntime, decodeAcpxRuntimeHandleState } from "./runtime.js"; + +const NOOP_LOGGER = { + info: (_message: string) => {}, + warn: (_message: string) => {}, + error: (_message: string) => {}, + debug: (_message: string) => {}, +}; + +const MOCK_CLI_SCRIPT = String.raw`#!/usr/bin/env node +const fs = require("node:fs"); + +const args = process.argv.slice(2); +const logPath = process.env.MOCK_ACPX_LOG; +const writeLog = (entry) => { + if (!logPath) return; + fs.appendFileSync(logPath, JSON.stringify(entry) + "\n"); +}; + +if (args.includes("--version")) { + process.stdout.write("mock-acpx ${ACPX_PINNED_VERSION}\\n"); + process.exit(0); +} + +if (args.includes("--help")) { + process.stdout.write("mock-acpx help\\n"); + process.exit(0); +} + +const commandIndex = args.findIndex( + (arg) => + arg === "prompt" || + arg === "cancel" || + arg === "sessions" || + arg === "set-mode" || + arg === "set" || + arg === "status", +); +const command = commandIndex >= 0 ? args[commandIndex] : ""; +const agent = commandIndex > 0 ? args[commandIndex - 1] : "unknown"; + +const readFlag = (flag) => { + const idx = args.indexOf(flag); + if (idx < 0) return ""; + return String(args[idx + 1] || ""); +}; + +const sessionFromOption = readFlag("--session"); +const ensureName = readFlag("--name"); +const closeName = command === "sessions" && args[commandIndex + 1] === "close" ? String(args[commandIndex + 2] || "") : ""; +const setModeValue = command === "set-mode" ? String(args[commandIndex + 1] || "") : ""; +const setKey = command === "set" ? String(args[commandIndex + 1] || "") : ""; +const setValue = command === "set" ? String(args[commandIndex + 2] || "") : ""; + +if (command === "sessions" && args[commandIndex + 1] === "ensure") { + writeLog({ kind: "ensure", agent, args, sessionName: ensureName }); + process.stdout.write(JSON.stringify({ + type: "session_ensured", + acpxRecordId: "rec-" + ensureName, + acpxSessionId: "sid-" + ensureName, + agentSessionId: "inner-" + ensureName, + name: ensureName, + created: true, + }) + "\n"); + process.exit(0); +} + +if (command === "cancel") { + writeLog({ kind: "cancel", agent, args, sessionName: sessionFromOption }); + process.stdout.write(JSON.stringify({ + acpxSessionId: "sid-" + sessionFromOption, + cancelled: true, + }) + "\n"); + process.exit(0); +} + +if (command === "set-mode") { + writeLog({ kind: "set-mode", agent, args, sessionName: sessionFromOption, mode: setModeValue }); + process.stdout.write(JSON.stringify({ + type: "mode_set", + acpxSessionId: "sid-" + sessionFromOption, + mode: setModeValue, + }) + "\n"); + process.exit(0); +} + +if (command === "set") { + writeLog({ + kind: "set", + agent, + args, + sessionName: sessionFromOption, + key: setKey, + value: setValue, + }); + process.stdout.write(JSON.stringify({ + type: "config_set", + acpxSessionId: "sid-" + sessionFromOption, + key: setKey, + value: setValue, + }) + "\n"); + process.exit(0); +} + +if (command === "status") { + writeLog({ kind: "status", agent, args, sessionName: sessionFromOption }); + process.stdout.write(JSON.stringify({ + acpxRecordId: sessionFromOption ? "rec-" + sessionFromOption : null, + acpxSessionId: sessionFromOption ? "sid-" + sessionFromOption : null, + agentSessionId: sessionFromOption ? "inner-" + sessionFromOption : null, + status: sessionFromOption ? "alive" : "no-session", + pid: 4242, + uptime: 120, + }) + "\n"); + process.exit(0); +} + +if (command === "sessions" && args[commandIndex + 1] === "close") { + writeLog({ kind: "close", agent, args, sessionName: closeName }); + process.stdout.write(JSON.stringify({ + type: "session_closed", + acpxRecordId: "rec-" + closeName, + acpxSessionId: "sid-" + closeName, + name: closeName, + }) + "\n"); + process.exit(0); +} + +if (command === "prompt") { + const stdinText = fs.readFileSync(0, "utf8"); + writeLog({ kind: "prompt", agent, args, sessionName: sessionFromOption, stdinText }); + const acpxSessionId = "sid-" + sessionFromOption; + + if (stdinText.includes("trigger-error")) { + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 0, + stream: "prompt", + type: "error", + code: "RUNTIME", + message: "mock failure", + }) + "\n"); + process.exit(1); + } + + if (stdinText.includes("split-spacing")) { + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 0, + stream: "prompt", + type: "text", + content: "alpha", + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 1, + stream: "prompt", + type: "text", + content: " beta", + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 2, + stream: "prompt", + type: "text", + content: " gamma", + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 3, + stream: "prompt", + type: "done", + stopReason: "end_turn", + }) + "\n"); + process.exit(0); + } + + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 0, + stream: "prompt", + type: "thought", + content: "thinking", + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 1, + stream: "prompt", + type: "tool_call", + title: "run-tests", + status: "in_progress", + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 2, + stream: "prompt", + type: "text", + content: "echo:" + stdinText.trim(), + }) + "\n"); + process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId, + requestId: "req-1", + seq: 3, + stream: "prompt", + type: "done", + stopReason: "end_turn", + }) + "\n"); + process.exit(0); +} + +writeLog({ kind: "unknown", args }); +process.stdout.write(JSON.stringify({ + eventVersion: 1, + acpxSessionId: "unknown", + seq: 0, + stream: "control", + type: "error", + code: "USAGE", + message: "unknown command", +}) + "\n"); +process.exit(2); +`; + +const tempDirs: string[] = []; + +async function createMockRuntime(params?: { + permissionMode?: ResolvedAcpxPluginConfig["permissionMode"]; + queueOwnerTtlSeconds?: number; +}): Promise<{ + runtime: AcpxRuntime; + logPath: string; + config: ResolvedAcpxPluginConfig; +}> { + const dir = await mkdtemp(path.join(os.tmpdir(), "openclaw-acpx-runtime-test-")); + tempDirs.push(dir); + const scriptPath = path.join(dir, "mock-acpx.cjs"); + const logPath = path.join(dir, "calls.log"); + await writeFile(scriptPath, MOCK_CLI_SCRIPT, "utf8"); + await chmod(scriptPath, 0o755); + process.env.MOCK_ACPX_LOG = logPath; + + const config: ResolvedAcpxPluginConfig = { + command: scriptPath, + cwd: dir, + permissionMode: params?.permissionMode ?? "approve-all", + nonInteractivePermissions: "fail", + queueOwnerTtlSeconds: params?.queueOwnerTtlSeconds ?? 0.1, + }; + + return { + runtime: new AcpxRuntime(config, { + queueOwnerTtlSeconds: params?.queueOwnerTtlSeconds, + logger: NOOP_LOGGER, + }), + logPath, + config, + }; +} + +async function readLogEntries(logPath: string): Promise>> { + if (!fs.existsSync(logPath)) { + return []; + } + const raw = await readFile(logPath, "utf8"); + return raw + .split(/\r?\n/) + .map((line) => line.trim()) + .filter(Boolean) + .map((line) => JSON.parse(line) as Record); +} + +afterEach(async () => { + delete process.env.MOCK_ACPX_LOG; + while (tempDirs.length > 0) { + const dir = tempDirs.pop(); + if (!dir) { + continue; + } + await rm(dir, { + recursive: true, + force: true, + maxRetries: 10, + retryDelay: 10, + }); + } +}); + +describe("AcpxRuntime", () => { + it("passes the shared ACP adapter contract suite", async () => { + const fixture = await createMockRuntime(); + await runAcpRuntimeAdapterContract({ + createRuntime: async () => fixture.runtime, + agentId: "codex", + successPrompt: "contract-pass", + errorPrompt: "trigger-error", + assertSuccessEvents: (events) => { + expect(events.some((event) => event.type === "done")).toBe(true); + }, + assertErrorOutcome: ({ events, thrown }) => { + expect(events.some((event) => event.type === "error") || Boolean(thrown)).toBe(true); + }, + }); + + const logs = await readLogEntries(fixture.logPath); + expect(logs.some((entry) => entry.kind === "ensure")).toBe(true); + expect(logs.some((entry) => entry.kind === "status")).toBe(true); + expect(logs.some((entry) => entry.kind === "set-mode")).toBe(true); + expect(logs.some((entry) => entry.kind === "set")).toBe(true); + expect(logs.some((entry) => entry.kind === "cancel")).toBe(true); + expect(logs.some((entry) => entry.kind === "close")).toBe(true); + }); + + it("ensures sessions and streams prompt events", async () => { + const { runtime, logPath } = await createMockRuntime({ queueOwnerTtlSeconds: 180 }); + + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:123", + agent: "codex", + mode: "persistent", + }); + expect(handle.backend).toBe("acpx"); + expect(handle.acpxRecordId).toBe("rec-agent:codex:acp:123"); + expect(handle.agentSessionId).toBe("inner-agent:codex:acp:123"); + expect(handle.backendSessionId).toBe("sid-agent:codex:acp:123"); + const decoded = decodeAcpxRuntimeHandleState(handle.runtimeSessionName); + expect(decoded?.acpxRecordId).toBe("rec-agent:codex:acp:123"); + expect(decoded?.agentSessionId).toBe("inner-agent:codex:acp:123"); + expect(decoded?.backendSessionId).toBe("sid-agent:codex:acp:123"); + + const events = []; + for await (const event of runtime.runTurn({ + handle, + text: "hello world", + mode: "prompt", + requestId: "req-test", + })) { + events.push(event); + } + + expect(events).toContainEqual({ + type: "text_delta", + text: "thinking", + stream: "thought", + }); + expect(events).toContainEqual({ + type: "tool_call", + text: "run-tests (in_progress)", + }); + expect(events).toContainEqual({ + type: "text_delta", + text: "echo:hello world", + stream: "output", + }); + expect(events).toContainEqual({ + type: "done", + stopReason: "end_turn", + }); + + const logs = await readLogEntries(logPath); + const ensure = logs.find((entry) => entry.kind === "ensure"); + const prompt = logs.find((entry) => entry.kind === "prompt"); + expect(ensure).toBeDefined(); + expect(prompt).toBeDefined(); + expect(Array.isArray(prompt?.args)).toBe(true); + const promptArgs = (prompt?.args as string[]) ?? []; + expect(promptArgs).toContain("--ttl"); + expect(promptArgs).toContain("180"); + expect(promptArgs).toContain("--approve-all"); + }); + + it("passes a queue-owner TTL by default to avoid long idle stalls", async () => { + const { runtime, logPath } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:ttl-default", + agent: "codex", + mode: "persistent", + }); + + for await (const _event of runtime.runTurn({ + handle, + text: "ttl-default", + mode: "prompt", + requestId: "req-ttl-default", + })) { + // drain + } + + const logs = await readLogEntries(logPath); + const prompt = logs.find((entry) => entry.kind === "prompt"); + expect(prompt).toBeDefined(); + const promptArgs = (prompt?.args as string[]) ?? []; + const ttlFlagIndex = promptArgs.indexOf("--ttl"); + expect(ttlFlagIndex).toBeGreaterThanOrEqual(0); + expect(promptArgs[ttlFlagIndex + 1]).toBe("0.1"); + }); + + it("preserves leading spaces across streamed text deltas", async () => { + const { runtime } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:space", + agent: "codex", + mode: "persistent", + }); + + const textDeltas: string[] = []; + for await (const event of runtime.runTurn({ + handle, + text: "split-spacing", + mode: "prompt", + requestId: "req-space", + })) { + if (event.type === "text_delta" && event.stream === "output") { + textDeltas.push(event.text); + } + } + + expect(textDeltas).toEqual(["alpha", " beta", " gamma"]); + expect(textDeltas.join("")).toBe("alpha beta gamma"); + }); + + it("maps acpx error events into ACP runtime error events", async () => { + const { runtime } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:456", + agent: "codex", + mode: "persistent", + }); + + const events = []; + for await (const event of runtime.runTurn({ + handle, + text: "trigger-error", + mode: "prompt", + requestId: "req-err", + })) { + events.push(event); + } + + expect(events).toContainEqual({ + type: "error", + message: "mock failure", + code: "RUNTIME", + retryable: undefined, + }); + }); + + it("supports cancel and close using encoded runtime handle state", async () => { + const { runtime, logPath, config } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:claude:acp:789", + agent: "claude", + mode: "persistent", + }); + + const decoded = decodeAcpxRuntimeHandleState(handle.runtimeSessionName); + expect(decoded?.name).toBe("agent:claude:acp:789"); + + const secondRuntime = new AcpxRuntime(config, { logger: NOOP_LOGGER }); + + await secondRuntime.cancel({ handle, reason: "test" }); + await secondRuntime.close({ handle, reason: "test" }); + + const logs = await readLogEntries(logPath); + const cancel = logs.find((entry) => entry.kind === "cancel"); + const close = logs.find((entry) => entry.kind === "close"); + expect(cancel?.sessionName).toBe("agent:claude:acp:789"); + expect(close?.sessionName).toBe("agent:claude:acp:789"); + }); + + it("exposes control capabilities and runs set-mode/set/status commands", async () => { + const { runtime, logPath } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:controls", + agent: "codex", + mode: "persistent", + }); + + const capabilities = runtime.getCapabilities(); + expect(capabilities.controls).toContain("session/set_mode"); + expect(capabilities.controls).toContain("session/set_config_option"); + expect(capabilities.controls).toContain("session/status"); + + await runtime.setMode({ + handle, + mode: "plan", + }); + await runtime.setConfigOption({ + handle, + key: "model", + value: "openai-codex/gpt-5.3-codex", + }); + const status = await runtime.getStatus({ handle }); + const ensuredSessionName = "agent:codex:acp:controls"; + + expect(status.summary).toContain("status=alive"); + expect(status.acpxRecordId).toBe("rec-" + ensuredSessionName); + expect(status.backendSessionId).toBe("sid-" + ensuredSessionName); + expect(status.agentSessionId).toBe("inner-" + ensuredSessionName); + expect(status.details?.acpxRecordId).toBe("rec-" + ensuredSessionName); + expect(status.details?.status).toBe("alive"); + expect(status.details?.pid).toBe(4242); + + const logs = await readLogEntries(logPath); + expect(logs.find((entry) => entry.kind === "set-mode")?.mode).toBe("plan"); + expect(logs.find((entry) => entry.kind === "set")?.key).toBe("model"); + expect(logs.find((entry) => entry.kind === "status")).toBeDefined(); + }); + + it("skips prompt execution when runTurn starts with an already-aborted signal", async () => { + const { runtime, logPath } = await createMockRuntime(); + const handle = await runtime.ensureSession({ + sessionKey: "agent:codex:acp:aborted", + agent: "codex", + mode: "persistent", + }); + const controller = new AbortController(); + controller.abort(); + + const events = []; + for await (const event of runtime.runTurn({ + handle, + text: "should-not-run", + mode: "prompt", + requestId: "req-aborted", + signal: controller.signal, + })) { + events.push(event); + } + + const logs = await readLogEntries(logPath); + expect(events).toEqual([]); + expect(logs.some((entry) => entry.kind === "prompt")).toBe(false); + }); + + it("does not mark backend unhealthy when a per-session cwd is missing", async () => { + const { runtime } = await createMockRuntime(); + const missingCwd = path.join(os.tmpdir(), "openclaw-acpx-runtime-test-missing-cwd"); + + await runtime.probeAvailability(); + expect(runtime.isHealthy()).toBe(true); + + await expect( + runtime.ensureSession({ + sessionKey: "agent:codex:acp:missing-cwd", + agent: "codex", + mode: "persistent", + cwd: missingCwd, + }), + ).rejects.toMatchObject({ + code: "ACP_SESSION_INIT_FAILED", + message: expect.stringContaining("working directory does not exist"), + }); + expect(runtime.isHealthy()).toBe(true); + }); + + it("marks runtime unhealthy when command is missing", async () => { + const runtime = new AcpxRuntime( + { + command: "/definitely/missing/acpx", + cwd: process.cwd(), + permissionMode: "approve-reads", + nonInteractivePermissions: "fail", + queueOwnerTtlSeconds: 0.1, + }, + { logger: NOOP_LOGGER }, + ); + + await runtime.probeAvailability(); + expect(runtime.isHealthy()).toBe(false); + }); + + it("marks runtime healthy when command is available", async () => { + const { runtime } = await createMockRuntime(); + await runtime.probeAvailability(); + expect(runtime.isHealthy()).toBe(true); + }); + + it("returns doctor report for missing command", async () => { + const runtime = new AcpxRuntime( + { + command: "/definitely/missing/acpx", + cwd: process.cwd(), + permissionMode: "approve-reads", + nonInteractivePermissions: "fail", + queueOwnerTtlSeconds: 0.1, + }, + { logger: NOOP_LOGGER }, + ); + + const report = await runtime.doctor(); + expect(report.ok).toBe(false); + expect(report.code).toBe("ACP_BACKEND_UNAVAILABLE"); + expect(report.installCommand).toContain("acpx"); + }); +}); diff --git a/extensions/acpx/src/runtime.ts b/extensions/acpx/src/runtime.ts new file mode 100644 index 000000000000..a5273c7e0f28 --- /dev/null +++ b/extensions/acpx/src/runtime.ts @@ -0,0 +1,578 @@ +import { createInterface } from "node:readline"; +import type { + AcpRuntimeCapabilities, + AcpRuntimeDoctorReport, + AcpRuntime, + AcpRuntimeEnsureInput, + AcpRuntimeErrorCode, + AcpRuntimeEvent, + AcpRuntimeHandle, + AcpRuntimeStatus, + AcpRuntimeTurnInput, + PluginLogger, +} from "openclaw/plugin-sdk"; +import { AcpRuntimeError } from "openclaw/plugin-sdk"; +import { + ACPX_LOCAL_INSTALL_COMMAND, + ACPX_PINNED_VERSION, + type ResolvedAcpxPluginConfig, +} from "./config.js"; +import { checkPinnedAcpxVersion } from "./ensure.js"; +import { + parseJsonLines, + parsePromptEventLine, + toAcpxErrorEvent, +} from "./runtime-internals/events.js"; +import { + resolveSpawnFailure, + spawnAndCollect, + spawnWithResolvedCommand, + waitForExit, +} from "./runtime-internals/process.js"; +import { + asOptionalString, + asTrimmedString, + buildPermissionArgs, + deriveAgentFromSessionKey, + isRecord, + type AcpxHandleState, + type AcpxJsonObject, +} from "./runtime-internals/shared.js"; + +export const ACPX_BACKEND_ID = "acpx"; + +const ACPX_RUNTIME_HANDLE_PREFIX = "acpx:v1:"; +const DEFAULT_AGENT_FALLBACK = "codex"; +const ACPX_CAPABILITIES: AcpRuntimeCapabilities = { + controls: ["session/set_mode", "session/set_config_option", "session/status"], +}; + +export function encodeAcpxRuntimeHandleState(state: AcpxHandleState): string { + const payload = Buffer.from(JSON.stringify(state), "utf8").toString("base64url"); + return `${ACPX_RUNTIME_HANDLE_PREFIX}${payload}`; +} + +export function decodeAcpxRuntimeHandleState(runtimeSessionName: string): AcpxHandleState | null { + const trimmed = runtimeSessionName.trim(); + if (!trimmed.startsWith(ACPX_RUNTIME_HANDLE_PREFIX)) { + return null; + } + const encoded = trimmed.slice(ACPX_RUNTIME_HANDLE_PREFIX.length); + if (!encoded) { + return null; + } + try { + const raw = Buffer.from(encoded, "base64url").toString("utf8"); + const parsed = JSON.parse(raw) as unknown; + if (!isRecord(parsed)) { + return null; + } + const name = asTrimmedString(parsed.name); + const agent = asTrimmedString(parsed.agent); + const cwd = asTrimmedString(parsed.cwd); + const mode = asTrimmedString(parsed.mode); + const acpxRecordId = asOptionalString(parsed.acpxRecordId); + const backendSessionId = asOptionalString(parsed.backendSessionId); + const agentSessionId = asOptionalString(parsed.agentSessionId); + if (!name || !agent || !cwd) { + return null; + } + if (mode !== "persistent" && mode !== "oneshot") { + return null; + } + return { + name, + agent, + cwd, + mode, + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(backendSessionId ? { backendSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + }; + } catch { + return null; + } +} + +export class AcpxRuntime implements AcpRuntime { + private healthy = false; + private readonly logger?: PluginLogger; + private readonly queueOwnerTtlSeconds: number; + + constructor( + private readonly config: ResolvedAcpxPluginConfig, + opts?: { + logger?: PluginLogger; + queueOwnerTtlSeconds?: number; + }, + ) { + this.logger = opts?.logger; + const requestedQueueOwnerTtlSeconds = opts?.queueOwnerTtlSeconds; + this.queueOwnerTtlSeconds = + typeof requestedQueueOwnerTtlSeconds === "number" && + Number.isFinite(requestedQueueOwnerTtlSeconds) && + requestedQueueOwnerTtlSeconds >= 0 + ? requestedQueueOwnerTtlSeconds + : this.config.queueOwnerTtlSeconds; + } + + isHealthy(): boolean { + return this.healthy; + } + + async probeAvailability(): Promise { + const versionCheck = await checkPinnedAcpxVersion({ + command: this.config.command, + cwd: this.config.cwd, + expectedVersion: ACPX_PINNED_VERSION, + }); + if (!versionCheck.ok) { + this.healthy = false; + return; + } + + try { + const result = await spawnAndCollect({ + command: this.config.command, + args: ["--help"], + cwd: this.config.cwd, + }); + this.healthy = result.error == null && (result.code ?? 0) === 0; + } catch { + this.healthy = false; + } + } + + async ensureSession(input: AcpRuntimeEnsureInput): Promise { + const sessionName = asTrimmedString(input.sessionKey); + if (!sessionName) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + const agent = asTrimmedString(input.agent); + if (!agent) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP agent id is required."); + } + const cwd = asTrimmedString(input.cwd) || this.config.cwd; + const mode = input.mode; + + const events = await this.runControlCommand({ + args: this.buildControlArgs({ + cwd, + command: [agent, "sessions", "ensure", "--name", sessionName], + }), + cwd, + fallbackCode: "ACP_SESSION_INIT_FAILED", + }); + const ensuredEvent = events.find( + (event) => + asOptionalString(event.agentSessionId) || + asOptionalString(event.acpxSessionId) || + asOptionalString(event.acpxRecordId), + ); + const acpxRecordId = ensuredEvent ? asOptionalString(ensuredEvent.acpxRecordId) : undefined; + const agentSessionId = ensuredEvent ? asOptionalString(ensuredEvent.agentSessionId) : undefined; + const backendSessionId = ensuredEvent + ? asOptionalString(ensuredEvent.acpxSessionId) + : undefined; + + return { + sessionKey: input.sessionKey, + backend: ACPX_BACKEND_ID, + runtimeSessionName: encodeAcpxRuntimeHandleState({ + name: sessionName, + agent, + cwd, + mode, + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(backendSessionId ? { backendSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + }), + cwd, + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(backendSessionId ? { backendSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + }; + } + + async *runTurn(input: AcpRuntimeTurnInput): AsyncIterable { + const state = this.resolveHandleState(input.handle); + const args = this.buildPromptArgs({ + agent: state.agent, + sessionName: state.name, + cwd: state.cwd, + }); + + const cancelOnAbort = async () => { + await this.cancel({ + handle: input.handle, + reason: "abort-signal", + }).catch((err) => { + this.logger?.warn?.(`acpx runtime abort-cancel failed: ${String(err)}`); + }); + }; + const onAbort = () => { + void cancelOnAbort(); + }; + + if (input.signal?.aborted) { + await cancelOnAbort(); + return; + } + if (input.signal) { + input.signal.addEventListener("abort", onAbort, { once: true }); + } + const child = spawnWithResolvedCommand({ + command: this.config.command, + args, + cwd: state.cwd, + }); + child.stdin.on("error", () => { + // Ignore EPIPE when the child exits before stdin flush completes. + }); + + child.stdin.end(input.text); + + let stderr = ""; + child.stderr.on("data", (chunk) => { + stderr += String(chunk); + }); + + let sawDone = false; + let sawError = false; + const lines = createInterface({ input: child.stdout }); + try { + for await (const line of lines) { + const parsed = parsePromptEventLine(line); + if (!parsed) { + continue; + } + if (parsed.type === "done") { + sawDone = true; + } + if (parsed.type === "error") { + sawError = true; + } + yield parsed; + } + + const exit = await waitForExit(child); + if (exit.error) { + const spawnFailure = resolveSpawnFailure(exit.error, state.cwd); + if (spawnFailure === "missing-command") { + this.healthy = false; + throw new AcpRuntimeError( + "ACP_BACKEND_UNAVAILABLE", + `acpx command not found: ${this.config.command}`, + { cause: exit.error }, + ); + } + if (spawnFailure === "missing-cwd") { + throw new AcpRuntimeError( + "ACP_TURN_FAILED", + `ACP runtime working directory does not exist: ${state.cwd}`, + { cause: exit.error }, + ); + } + throw new AcpRuntimeError("ACP_TURN_FAILED", exit.error.message, { cause: exit.error }); + } + + if ((exit.code ?? 0) !== 0 && !sawError) { + yield { + type: "error", + message: stderr.trim() || `acpx exited with code ${exit.code ?? "unknown"}`, + }; + return; + } + + if (!sawDone && !sawError) { + yield { type: "done" }; + } + } finally { + lines.close(); + if (input.signal) { + input.signal.removeEventListener("abort", onAbort); + } + } + } + + getCapabilities(): AcpRuntimeCapabilities { + return ACPX_CAPABILITIES; + } + + async getStatus(input: { handle: AcpRuntimeHandle }): Promise { + const state = this.resolveHandleState(input.handle); + const events = await this.runControlCommand({ + args: this.buildControlArgs({ + cwd: state.cwd, + command: [state.agent, "status", "--session", state.name], + }), + cwd: state.cwd, + fallbackCode: "ACP_TURN_FAILED", + ignoreNoSession: true, + }); + const detail = events.find((event) => !toAcpxErrorEvent(event)) ?? events[0]; + if (!detail) { + return { + summary: "acpx status unavailable", + }; + } + const status = asTrimmedString(detail.status) || "unknown"; + const acpxRecordId = asOptionalString(detail.acpxRecordId); + const acpxSessionId = asOptionalString(detail.acpxSessionId); + const agentSessionId = asOptionalString(detail.agentSessionId); + const pid = typeof detail.pid === "number" && Number.isFinite(detail.pid) ? detail.pid : null; + const summary = [ + `status=${status}`, + acpxRecordId ? `acpxRecordId=${acpxRecordId}` : null, + acpxSessionId ? `acpxSessionId=${acpxSessionId}` : null, + pid != null ? `pid=${pid}` : null, + ] + .filter(Boolean) + .join(" "); + return { + summary, + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(acpxSessionId ? { backendSessionId: acpxSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + details: detail, + }; + } + + async setMode(input: { handle: AcpRuntimeHandle; mode: string }): Promise { + const state = this.resolveHandleState(input.handle); + const mode = asTrimmedString(input.mode); + if (!mode) { + throw new AcpRuntimeError("ACP_TURN_FAILED", "ACP runtime mode is required."); + } + await this.runControlCommand({ + args: this.buildControlArgs({ + cwd: state.cwd, + command: [state.agent, "set-mode", mode, "--session", state.name], + }), + cwd: state.cwd, + fallbackCode: "ACP_TURN_FAILED", + }); + } + + async setConfigOption(input: { + handle: AcpRuntimeHandle; + key: string; + value: string; + }): Promise { + const state = this.resolveHandleState(input.handle); + const key = asTrimmedString(input.key); + const value = asTrimmedString(input.value); + if (!key || !value) { + throw new AcpRuntimeError("ACP_TURN_FAILED", "ACP config option key/value are required."); + } + await this.runControlCommand({ + args: this.buildControlArgs({ + cwd: state.cwd, + command: [state.agent, "set", key, value, "--session", state.name], + }), + cwd: state.cwd, + fallbackCode: "ACP_TURN_FAILED", + }); + } + + async doctor(): Promise { + const versionCheck = await checkPinnedAcpxVersion({ + command: this.config.command, + cwd: this.config.cwd, + expectedVersion: ACPX_PINNED_VERSION, + }); + if (!versionCheck.ok) { + this.healthy = false; + const details = [ + `expected=${versionCheck.expectedVersion}`, + versionCheck.installedVersion ? `installed=${versionCheck.installedVersion}` : null, + ].filter((detail): detail is string => Boolean(detail)); + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: versionCheck.message, + installCommand: versionCheck.installCommand, + details, + }; + } + + try { + const result = await spawnAndCollect({ + command: this.config.command, + args: ["--help"], + cwd: this.config.cwd, + }); + if (result.error) { + const spawnFailure = resolveSpawnFailure(result.error, this.config.cwd); + if (spawnFailure === "missing-command") { + this.healthy = false; + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: `acpx command not found: ${this.config.command}`, + installCommand: ACPX_LOCAL_INSTALL_COMMAND, + }; + } + if (spawnFailure === "missing-cwd") { + this.healthy = false; + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: `ACP runtime working directory does not exist: ${this.config.cwd}`, + }; + } + this.healthy = false; + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: result.error.message, + details: [String(result.error)], + }; + } + if ((result.code ?? 0) !== 0) { + this.healthy = false; + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: result.stderr.trim() || `acpx exited with code ${result.code ?? "unknown"}`, + }; + } + this.healthy = true; + return { + ok: true, + message: `acpx command available (${this.config.command}, version ${versionCheck.version})`, + }; + } catch (error) { + this.healthy = false; + return { + ok: false, + code: "ACP_BACKEND_UNAVAILABLE", + message: error instanceof Error ? error.message : String(error), + }; + } + } + + async cancel(input: { handle: AcpRuntimeHandle; reason?: string }): Promise { + const state = this.resolveHandleState(input.handle); + await this.runControlCommand({ + args: this.buildControlArgs({ + cwd: state.cwd, + command: [state.agent, "cancel", "--session", state.name], + }), + cwd: state.cwd, + fallbackCode: "ACP_TURN_FAILED", + ignoreNoSession: true, + }); + } + + async close(input: { handle: AcpRuntimeHandle; reason: string }): Promise { + const state = this.resolveHandleState(input.handle); + await this.runControlCommand({ + args: this.buildControlArgs({ + cwd: state.cwd, + command: [state.agent, "sessions", "close", state.name], + }), + cwd: state.cwd, + fallbackCode: "ACP_TURN_FAILED", + ignoreNoSession: true, + }); + } + + private resolveHandleState(handle: AcpRuntimeHandle): AcpxHandleState { + const decoded = decodeAcpxRuntimeHandleState(handle.runtimeSessionName); + if (decoded) { + return decoded; + } + + const legacyName = asTrimmedString(handle.runtimeSessionName); + if (!legacyName) { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + "Invalid acpx runtime handle: runtimeSessionName is missing.", + ); + } + + return { + name: legacyName, + agent: deriveAgentFromSessionKey(handle.sessionKey, DEFAULT_AGENT_FALLBACK), + cwd: this.config.cwd, + mode: "persistent", + }; + } + + private buildControlArgs(params: { cwd: string; command: string[] }): string[] { + return ["--format", "json", "--json-strict", "--cwd", params.cwd, ...params.command]; + } + + private buildPromptArgs(params: { agent: string; sessionName: string; cwd: string }): string[] { + const args = [ + "--format", + "json", + "--json-strict", + "--cwd", + params.cwd, + ...buildPermissionArgs(this.config.permissionMode), + "--non-interactive-permissions", + this.config.nonInteractivePermissions, + ]; + if (this.config.timeoutSeconds) { + args.push("--timeout", String(this.config.timeoutSeconds)); + } + args.push("--ttl", String(this.queueOwnerTtlSeconds)); + args.push(params.agent, "prompt", "--session", params.sessionName, "--file", "-"); + return args; + } + + private async runControlCommand(params: { + args: string[]; + cwd: string; + fallbackCode: AcpRuntimeErrorCode; + ignoreNoSession?: boolean; + }): Promise { + const result = await spawnAndCollect({ + command: this.config.command, + args: params.args, + cwd: params.cwd, + }); + + if (result.error) { + const spawnFailure = resolveSpawnFailure(result.error, params.cwd); + if (spawnFailure === "missing-command") { + this.healthy = false; + throw new AcpRuntimeError( + "ACP_BACKEND_UNAVAILABLE", + `acpx command not found: ${this.config.command}`, + { cause: result.error }, + ); + } + if (spawnFailure === "missing-cwd") { + throw new AcpRuntimeError( + params.fallbackCode, + `ACP runtime working directory does not exist: ${params.cwd}`, + { cause: result.error }, + ); + } + throw new AcpRuntimeError(params.fallbackCode, result.error.message, { cause: result.error }); + } + + const events = parseJsonLines(result.stdout); + const errorEvent = events.map((event) => toAcpxErrorEvent(event)).find(Boolean) ?? null; + if (errorEvent) { + if (params.ignoreNoSession && errorEvent.code === "NO_SESSION") { + return events; + } + throw new AcpRuntimeError( + params.fallbackCode, + errorEvent.code ? `${errorEvent.code}: ${errorEvent.message}` : errorEvent.message, + ); + } + + if ((result.code ?? 0) !== 0) { + throw new AcpRuntimeError( + params.fallbackCode, + result.stderr.trim() || `acpx exited with code ${result.code ?? "unknown"}`, + ); + } + return events; + } +} diff --git a/extensions/acpx/src/service.test.ts b/extensions/acpx/src/service.test.ts new file mode 100644 index 000000000000..30fc9fa7205f --- /dev/null +++ b/extensions/acpx/src/service.test.ts @@ -0,0 +1,173 @@ +import type { AcpRuntime, OpenClawPluginServiceContext } from "openclaw/plugin-sdk"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { AcpRuntimeError } from "../../../src/acp/runtime/errors.js"; +import { + __testing, + getAcpRuntimeBackend, + requireAcpRuntimeBackend, +} from "../../../src/acp/runtime/registry.js"; +import { ACPX_BUNDLED_BIN } from "./config.js"; +import { createAcpxRuntimeService } from "./service.js"; + +const { ensurePinnedAcpxSpy } = vi.hoisted(() => ({ + ensurePinnedAcpxSpy: vi.fn(async () => {}), +})); + +vi.mock("./ensure.js", () => ({ + ensurePinnedAcpx: ensurePinnedAcpxSpy, +})); + +type RuntimeStub = AcpRuntime & { + probeAvailability(): Promise; + isHealthy(): boolean; +}; + +function createRuntimeStub(healthy: boolean): { + runtime: RuntimeStub; + probeAvailabilitySpy: ReturnType; + isHealthySpy: ReturnType; +} { + const probeAvailabilitySpy = vi.fn(async () => {}); + const isHealthySpy = vi.fn(() => healthy); + return { + runtime: { + ensureSession: vi.fn(async (input) => ({ + sessionKey: input.sessionKey, + backend: "acpx", + runtimeSessionName: input.sessionKey, + })), + runTurn: vi.fn(async function* () { + yield { type: "done" as const }; + }), + cancel: vi.fn(async () => {}), + close: vi.fn(async () => {}), + async probeAvailability() { + await probeAvailabilitySpy(); + }, + isHealthy() { + return isHealthySpy(); + }, + }, + probeAvailabilitySpy, + isHealthySpy, + }; +} + +function createServiceContext( + overrides: Partial = {}, +): OpenClawPluginServiceContext { + return { + config: {}, + workspaceDir: "/tmp/workspace", + stateDir: "/tmp/state", + logger: { + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), + debug: vi.fn(), + }, + ...overrides, + }; +} + +describe("createAcpxRuntimeService", () => { + beforeEach(() => { + __testing.resetAcpRuntimeBackendsForTests(); + ensurePinnedAcpxSpy.mockReset(); + ensurePinnedAcpxSpy.mockImplementation(async () => {}); + }); + + it("registers and unregisters the acpx backend", async () => { + const { runtime, probeAvailabilitySpy } = createRuntimeStub(true); + const service = createAcpxRuntimeService({ + runtimeFactory: () => runtime, + }); + const context = createServiceContext(); + + await service.start(context); + expect(getAcpRuntimeBackend("acpx")?.runtime).toBe(runtime); + + await vi.waitFor(() => { + expect(ensurePinnedAcpxSpy).toHaveBeenCalledOnce(); + expect(probeAvailabilitySpy).toHaveBeenCalledOnce(); + }); + + await service.stop?.(context); + expect(getAcpRuntimeBackend("acpx")).toBeNull(); + }); + + it("marks backend unavailable when runtime health check fails", async () => { + const { runtime } = createRuntimeStub(false); + const service = createAcpxRuntimeService({ + runtimeFactory: () => runtime, + }); + const context = createServiceContext(); + + await service.start(context); + + expect(() => requireAcpRuntimeBackend("acpx")).toThrowError(AcpRuntimeError); + try { + requireAcpRuntimeBackend("acpx"); + throw new Error("expected ACP backend lookup to fail"); + } catch (error) { + expect((error as AcpRuntimeError).code).toBe("ACP_BACKEND_UNAVAILABLE"); + } + }); + + it("passes queue-owner TTL from plugin config", async () => { + const { runtime } = createRuntimeStub(true); + const runtimeFactory = vi.fn(() => runtime); + const service = createAcpxRuntimeService({ + runtimeFactory, + pluginConfig: { + queueOwnerTtlSeconds: 0.25, + }, + }); + const context = createServiceContext(); + + await service.start(context); + + expect(runtimeFactory).toHaveBeenCalledWith( + expect.objectContaining({ + queueOwnerTtlSeconds: 0.25, + pluginConfig: expect.objectContaining({ + command: ACPX_BUNDLED_BIN, + }), + }), + ); + }); + + it("uses a short default queue-owner TTL", async () => { + const { runtime } = createRuntimeStub(true); + const runtimeFactory = vi.fn(() => runtime); + const service = createAcpxRuntimeService({ + runtimeFactory, + }); + const context = createServiceContext(); + + await service.start(context); + + expect(runtimeFactory).toHaveBeenCalledWith( + expect.objectContaining({ + queueOwnerTtlSeconds: 0.1, + }), + ); + }); + + it("does not block startup while acpx ensure runs", async () => { + const { runtime } = createRuntimeStub(true); + ensurePinnedAcpxSpy.mockImplementation(() => new Promise(() => {})); + const service = createAcpxRuntimeService({ + runtimeFactory: () => runtime, + }); + const context = createServiceContext(); + + const startResult = await Promise.race([ + Promise.resolve(service.start(context)).then(() => "started"), + new Promise((resolve) => setTimeout(() => resolve("timed_out"), 100)), + ]); + + expect(startResult).toBe("started"); + expect(getAcpRuntimeBackend("acpx")?.runtime).toBe(runtime); + }); +}); diff --git a/extensions/acpx/src/service.ts b/extensions/acpx/src/service.ts new file mode 100644 index 000000000000..65768d00ce8a --- /dev/null +++ b/extensions/acpx/src/service.ts @@ -0,0 +1,102 @@ +import type { + AcpRuntime, + OpenClawPluginService, + OpenClawPluginServiceContext, + PluginLogger, +} from "openclaw/plugin-sdk"; +import { registerAcpRuntimeBackend, unregisterAcpRuntimeBackend } from "openclaw/plugin-sdk"; +import { + ACPX_PINNED_VERSION, + resolveAcpxPluginConfig, + type ResolvedAcpxPluginConfig, +} from "./config.js"; +import { ensurePinnedAcpx } from "./ensure.js"; +import { ACPX_BACKEND_ID, AcpxRuntime } from "./runtime.js"; + +type AcpxRuntimeLike = AcpRuntime & { + probeAvailability(): Promise; + isHealthy(): boolean; +}; + +type AcpxRuntimeFactoryParams = { + pluginConfig: ResolvedAcpxPluginConfig; + queueOwnerTtlSeconds: number; + logger?: PluginLogger; +}; + +type CreateAcpxRuntimeServiceParams = { + pluginConfig?: unknown; + runtimeFactory?: (params: AcpxRuntimeFactoryParams) => AcpxRuntimeLike; +}; + +function createDefaultRuntime(params: AcpxRuntimeFactoryParams): AcpxRuntimeLike { + return new AcpxRuntime(params.pluginConfig, { + logger: params.logger, + queueOwnerTtlSeconds: params.queueOwnerTtlSeconds, + }); +} + +export function createAcpxRuntimeService( + params: CreateAcpxRuntimeServiceParams = {}, +): OpenClawPluginService { + let runtime: AcpxRuntimeLike | null = null; + let lifecycleRevision = 0; + + return { + id: "acpx-runtime", + async start(ctx: OpenClawPluginServiceContext): Promise { + const pluginConfig = resolveAcpxPluginConfig({ + rawConfig: params.pluginConfig, + workspaceDir: ctx.workspaceDir, + }); + const runtimeFactory = params.runtimeFactory ?? createDefaultRuntime; + runtime = runtimeFactory({ + pluginConfig, + queueOwnerTtlSeconds: pluginConfig.queueOwnerTtlSeconds, + logger: ctx.logger, + }); + + registerAcpRuntimeBackend({ + id: ACPX_BACKEND_ID, + runtime, + healthy: () => runtime?.isHealthy() ?? false, + }); + ctx.logger.info( + `acpx runtime backend registered (command: ${pluginConfig.command}, pinned: ${ACPX_PINNED_VERSION})`, + ); + + lifecycleRevision += 1; + const currentRevision = lifecycleRevision; + void (async () => { + try { + await ensurePinnedAcpx({ + command: pluginConfig.command, + logger: ctx.logger, + expectedVersion: ACPX_PINNED_VERSION, + }); + if (currentRevision !== lifecycleRevision) { + return; + } + await runtime?.probeAvailability(); + if (runtime?.isHealthy()) { + ctx.logger.info("acpx runtime backend ready"); + } else { + ctx.logger.warn("acpx runtime backend probe failed after local install"); + } + } catch (err) { + if (currentRevision !== lifecycleRevision) { + return; + } + ctx.logger.warn( + `acpx runtime setup failed: ${err instanceof Error ? err.message : String(err)}`, + ); + } + })(); + }, + async stop(_ctx: OpenClawPluginServiceContext): Promise { + lifecycleRevision += 1; + unregisterAcpRuntimeBackend(ACPX_BACKEND_ID); + runtime = null; + }, + }; +} diff --git a/package.json b/package.json index 5f6443b64c87..48641d6d875a 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "build": "pnpm canvas:a2ui:bundle && tsdown && pnpm build:plugin-sdk:dts && node --import tsx scripts/write-plugin-sdk-entry-dts.ts && node --import tsx scripts/canvas-a2ui-copy.ts && node --import tsx scripts/copy-hook-metadata.ts && node --import tsx scripts/copy-export-html-templates.ts && node --import tsx scripts/write-build-info.ts && node --import tsx scripts/write-cli-compat.ts", "build:plugin-sdk:dts": "tsc -p tsconfig.plugin-sdk.dts.json", "canvas:a2ui:bundle": "bash scripts/bundle-a2ui.sh", - "check": "pnpm format:check && pnpm tsgo && pnpm lint && pnpm lint:tmp:no-random-messaging", + "check": "pnpm format:check && pnpm tsgo && pnpm lint && pnpm lint:tmp:no-random-messaging && pnpm lint:tmp:channel-agnostic-boundaries", "check:docs": "pnpm format:docs:check && pnpm lint:docs && pnpm docs:check-links", "check:loc": "node --import tsx scripts/check-ts-max-loc.ts --max 500", "deadcode:ci": "pnpm deadcode:report:ci:knip && pnpm deadcode:report:ci:ts-prune && pnpm deadcode:report:ci:ts-unused", @@ -93,6 +93,7 @@ "lint:docs:fix": "pnpm dlx markdownlint-cli2 --fix", "lint:fix": "oxlint --type-aware --fix && pnpm format", "lint:swift": "swiftlint lint --config .swiftlint.yml && (cd apps/ios && swiftlint lint --config .swiftlint.yml)", + "lint:tmp:channel-agnostic-boundaries": "node scripts/check-channel-agnostic-boundaries.mjs", "lint:tmp:no-random-messaging": "node scripts/check-no-random-messaging-tmp.mjs", "lint:ui:no-raw-window-open": "node scripts/check-no-raw-window-open.mjs", "mac:open": "open dist/OpenClaw.app", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0ddc70d9f977..7498f85a4071 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -251,6 +251,12 @@ importers: specifier: ^0.10.0 version: 0.10.0 + extensions/acpx: + dependencies: + acpx: + specifier: ^0.1.13 + version: 0.1.13(zod@4.3.6) + extensions/bluebubbles: {} extensions/copilot-proxy: {} @@ -3045,8 +3051,8 @@ packages: link-preview-js: optional: true - '@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67': - resolution: {tarball: https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67} + '@whiskeysockets/libsignal-node@git+https://github.com/whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67': + resolution: {commit: 1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67, repo: https://github.com/whiskeysockets/libsignal-node.git, type: git} version: 2.0.1 abbrev@1.1.1: @@ -3074,6 +3080,11 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + acpx@0.1.13: + resolution: {integrity: sha512-C032VkV3cNa13ubq9YhskTWvDTsciNAQfNHZLW3PIN3atdkrzkV0v2yi6Znp7UZDw+pzgpKUsOrZWl64Lwr+3w==} + engines: {node: '>=18'} + hasBin: true + agent-base@6.0.2: resolution: {integrity: sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==} engines: {node: '>= 6.0.0'} @@ -3211,10 +3222,26 @@ packages: axios@1.13.5: resolution: {integrity: sha512-cz4ur7Vb0xS4/KUN0tPWe44eqxrIu31me+fbang3ijiNscE129POzipJJA6zniq2C/Z6sJCjMimjS8Lc/GAs8Q==} + b4a@1.8.0: + resolution: {integrity: sha512-qRuSmNSkGQaHwNbM7J78Wwy+ghLEYF1zNrSeMxj4Kgw6y33O3mXcQ6Ie9fRvfU/YnxWkOchPXbaLb73TkIsfdg==} + peerDependencies: + react-native-b4a: '*' + peerDependenciesMeta: + react-native-b4a: + optional: true + balanced-match@4.0.4: resolution: {integrity: sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==} engines: {node: 18 || 20 || >=22} + bare-events@2.8.2: + resolution: {integrity: sha512-riJjyv1/mHLIPX4RwiK+oW9/4c3TEUeORHKefKAKnZ5kyslbN+HXowtbaVEqt4IMUB7OXlfixcs6gsFeo/jhiQ==} + peerDependencies: + bare-abort-controller: '*' + peerDependenciesMeta: + bare-abort-controller: + optional: true + base64-js@1.5.1: resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} @@ -3385,6 +3412,10 @@ packages: resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==} engines: {node: '>=14'} + commander@13.1.0: + resolution: {integrity: sha512-/rFeCpNJQbhSZjGVwO9RFV3xPqbnERS8MmIQzCtD/zl6gpJuV/bMLuN92oG3F7d8oDEHHRrujSXNUr8fpjntKw==} + engines: {node: '>=18'} + commander@14.0.3: resolution: {integrity: sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==} engines: {node: '>=20'} @@ -3662,6 +3693,9 @@ packages: eventemitter3@5.0.4: resolution: {integrity: sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==} + events-universal@1.0.1: + resolution: {integrity: sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw==} + expect-type@1.3.0: resolution: {integrity: sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==} engines: {node: '>=12.0.0'} @@ -3692,6 +3726,9 @@ packages: fast-deep-equal@3.1.3: resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} + fast-fifo@1.3.2: + resolution: {integrity: sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==} + fast-uri@3.1.0: resolution: {integrity: sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA==} @@ -5178,6 +5215,11 @@ packages: sisteransi@1.0.5: resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==} + skillflag@0.1.4: + resolution: {integrity: sha512-egFg+XCF5sloOWdtzxZivTX7n4UDj5pxQoY33wbT8h+YSDjMQJ76MZUg2rXQIBXmIDtlZhLgirS1g/3R5/qaHA==} + engines: {node: '>=18'} + hasBin: true + sleep-promise@9.1.0: resolution: {integrity: sha512-UHYzVpz9Xn8b+jikYSD6bqvf754xL2uBUzDFwiU6NcdZeifPr6UfgU43xpkPu67VMS88+TI2PSI7Eohgqf2fKA==} @@ -5277,6 +5319,9 @@ packages: resolution: {integrity: sha512-yhPIQXjrlt1xv7dyPQg2P17URmXbuM5pdGkpiMB3RenprfiBlvK415Lctfe0eshk90oA7/tNq7WEiMK8RSP39A==} engines: {node: '>=18'} + streamx@2.23.0: + resolution: {integrity: sha512-kn+e44esVfn2Fa/O0CPFcex27fjIL6MkVae0Mm6q+E6f0hWv578YCERbv+4m02cjxvDsPKLnmxral/rR6lBMAg==} + string-width@4.2.3: resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==} engines: {node: '>=8'} @@ -5322,11 +5367,17 @@ packages: resolution: {integrity: sha512-iK5/YhZxq5GO5z8wb0bY1317uDF3Zjpha0QFFLA8/trAoiLbQD0HUbMesEaxyzUgDxi2QlcbM8IvqOlEjgoXBA==} engines: {node: '>=12.17'} + tar-stream@3.1.7: + resolution: {integrity: sha512-qJj60CXt7IU1Ffyc3NJMjh6EkuCFej46zUqJ4J7pqYlThyd9bO0XBTmcOIhSzZJVWfsLks0+nle/j538YAW9RQ==} + tar@7.5.9: resolution: {integrity: sha512-BTLcK0xsDh2+PUe9F6c2TlRp4zOOBMTkoQHQIWSIzI0R7KG46uEwq4OPk2W7bZcprBMsuaeFsqwYr7pjh6CuHg==} engines: {node: '>=18'} deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me + text-decoder@1.2.7: + resolution: {integrity: sha512-vlLytXkeP4xvEq2otHeJfSQIRyWxo/oZGEbXrtEEF9Hnmrdly59sUbzZ/QgyWuLYHctCHxFF4tRQZNQ9k60ExQ==} + thenify-all@1.6.0: resolution: {integrity: sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==} engines: {node: '>=0.8'} @@ -8693,7 +8744,7 @@ snapshots: '@cacheable/node-cache': 1.7.6 '@hapi/boom': 9.1.4 async-mutex: 0.5.0 - libsignal: '@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67' + libsignal: '@whiskeysockets/libsignal-node@git+https://github.com/whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67' lru-cache: 11.2.6 music-metadata: 11.12.1 p-queue: 9.1.0 @@ -8708,7 +8759,7 @@ snapshots: - supports-color - utf-8-validate - '@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67': + '@whiskeysockets/libsignal-node@git+https://github.com/whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67': dependencies: curve25519-js: 0.0.4 protobufjs: 6.8.8 @@ -8736,6 +8787,16 @@ snapshots: acorn@8.16.0: {} + acpx@0.1.13(zod@4.3.6): + dependencies: + '@agentclientprotocol/sdk': 0.14.1(zod@4.3.6) + commander: 13.1.0 + skillflag: 0.1.4 + transitivePeerDependencies: + - bare-abort-controller + - react-native-b4a + - zod + agent-base@6.0.2: dependencies: debug: 4.4.3 @@ -8875,8 +8936,12 @@ snapshots: transitivePeerDependencies: - debug + b4a@1.8.0: {} + balanced-match@4.0.4: {} + bare-events@2.8.2: {} + base64-js@1.5.1: {} basic-auth@2.0.1: @@ -9073,6 +9138,8 @@ snapshots: commander@10.0.1: {} + commander@13.1.0: {} + commander@14.0.3: {} console-control-strings@1.1.0: {} @@ -9304,6 +9371,12 @@ snapshots: eventemitter3@5.0.4: {} + events-universal@1.0.1: + dependencies: + bare-events: 2.8.2 + transitivePeerDependencies: + - bare-abort-controller + expect-type@1.3.0: {} express@4.22.1: @@ -9393,6 +9466,8 @@ snapshots: fast-deep-equal@3.1.3: {} + fast-fifo@1.3.2: {} + fast-uri@3.1.0: {} fast-xml-parser@5.3.6: @@ -11206,6 +11281,14 @@ snapshots: sisteransi@1.0.5: {} + skillflag@0.1.4: + dependencies: + '@clack/prompts': 1.0.1 + tar-stream: 3.1.7 + transitivePeerDependencies: + - bare-abort-controller + - react-native-b4a + sleep-promise@9.1.0: {} slice-ansi@7.1.2: @@ -11301,6 +11384,15 @@ snapshots: steno@4.0.2: {} + streamx@2.23.0: + dependencies: + events-universal: 1.0.1 + fast-fifo: 1.3.2 + text-decoder: 1.2.7 + transitivePeerDependencies: + - bare-abort-controller + - react-native-b4a + string-width@4.2.3: dependencies: emoji-regex: 8.0.0 @@ -11352,6 +11444,15 @@ snapshots: array-back: 6.2.2 wordwrapjs: 5.1.1 + tar-stream@3.1.7: + dependencies: + b4a: 1.8.0 + fast-fifo: 1.3.2 + streamx: 2.23.0 + transitivePeerDependencies: + - bare-abort-controller + - react-native-b4a + tar@7.5.9: dependencies: '@isaacs/fs-minipass': 4.0.1 @@ -11360,6 +11461,12 @@ snapshots: minizlib: 3.1.0 yallist: 5.0.0 + text-decoder@1.2.7: + dependencies: + b4a: 1.8.0 + transitivePeerDependencies: + - react-native-b4a + thenify-all@1.6.0: dependencies: thenify: 3.3.1 diff --git a/scripts/check-channel-agnostic-boundaries.mjs b/scripts/check-channel-agnostic-boundaries.mjs new file mode 100644 index 000000000000..3b63911e86d0 --- /dev/null +++ b/scripts/check-channel-agnostic-boundaries.mjs @@ -0,0 +1,405 @@ +#!/usr/bin/env node + +import { promises as fs } from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import ts from "typescript"; + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); + +const acpCoreProtectedSources = [ + path.join(repoRoot, "src", "acp"), + path.join(repoRoot, "src", "agents", "acp-spawn.ts"), + path.join(repoRoot, "src", "auto-reply", "reply", "commands-acp"), + path.join(repoRoot, "src", "infra", "outbound", "conversation-id.ts"), +]; + +const channelCoreProtectedSources = [ + path.join(repoRoot, "src", "channels", "thread-bindings-policy.ts"), + path.join(repoRoot, "src", "channels", "thread-bindings-messages.ts"), +]; +const acpUserFacingTextSources = [ + path.join(repoRoot, "src", "auto-reply", "reply", "commands-acp"), +]; +const systemMarkLiteralGuardSources = [ + path.join(repoRoot, "src", "auto-reply", "reply", "commands-acp"), + path.join(repoRoot, "src", "auto-reply", "reply", "dispatch-acp.ts"), + path.join(repoRoot, "src", "auto-reply", "reply", "directive-handling.shared.ts"), + path.join(repoRoot, "src", "channels", "thread-bindings-messages.ts"), +]; + +const channelIds = [ + "bluebubbles", + "discord", + "googlechat", + "imessage", + "irc", + "line", + "matrix", + "msteams", + "signal", + "slack", + "telegram", + "web", + "whatsapp", + "zalo", + "zalouser", +]; + +const channelIdSet = new Set(channelIds); +const channelSegmentRe = new RegExp(`(^|[._/-])(?:${channelIds.join("|")})([._/-]|$)`); +const comparisonOperators = new Set([ + ts.SyntaxKind.EqualsEqualsEqualsToken, + ts.SyntaxKind.ExclamationEqualsEqualsToken, + ts.SyntaxKind.EqualsEqualsToken, + ts.SyntaxKind.ExclamationEqualsToken, +]); + +const allowedViolations = new Set([]); + +function isTestLikeFile(filePath) { + return ( + filePath.endsWith(".test.ts") || + filePath.endsWith(".test-utils.ts") || + filePath.endsWith(".test-harness.ts") || + filePath.endsWith(".e2e-harness.ts") + ); +} + +async function collectTypeScriptFiles(targetPath) { + const stat = await fs.stat(targetPath); + if (stat.isFile()) { + if (!targetPath.endsWith(".ts") || isTestLikeFile(targetPath)) { + return []; + } + return [targetPath]; + } + + const entries = await fs.readdir(targetPath, { withFileTypes: true }); + const files = []; + for (const entry of entries) { + const entryPath = path.join(targetPath, entry.name); + if (entry.isDirectory()) { + files.push(...(await collectTypeScriptFiles(entryPath))); + continue; + } + if (!entry.isFile()) { + continue; + } + if (!entryPath.endsWith(".ts")) { + continue; + } + if (isTestLikeFile(entryPath)) { + continue; + } + files.push(entryPath); + } + return files; +} + +function toLine(sourceFile, node) { + return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1; +} + +function isChannelsPropertyAccess(node) { + if (ts.isPropertyAccessExpression(node)) { + return node.name.text === "channels"; + } + if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression)) { + return node.argumentExpression.text === "channels"; + } + return false; +} + +function readStringLiteral(node) { + if (ts.isStringLiteral(node)) { + return node.text; + } + if (ts.isNoSubstitutionTemplateLiteral(node)) { + return node.text; + } + return null; +} + +function isChannelLiteralNode(node) { + const text = readStringLiteral(node); + return text ? channelIdSet.has(text) : false; +} + +function matchesChannelModuleSpecifier(specifier) { + return channelSegmentRe.test(specifier.replaceAll("\\", "/")); +} + +function getPropertyNameText(name) { + if (ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNumericLiteral(name)) { + return name.text; + } + return null; +} + +const userFacingChannelNameRe = + /\b(?:discord|telegram|slack|signal|imessage|whatsapp|google\s*chat|irc|line|zalo|matrix|msteams|bluebubbles)\b/i; +const systemMarkLiteral = "⚙️"; + +function isModuleSpecifierStringNode(node) { + const parent = node.parent; + if (ts.isImportDeclaration(parent) || ts.isExportDeclaration(parent)) { + return true; + } + return ( + ts.isCallExpression(parent) && + parent.expression.kind === ts.SyntaxKind.ImportKeyword && + parent.arguments[0] === node + ); +} + +export function findChannelAgnosticBoundaryViolations( + content, + fileName = "source.ts", + options = {}, +) { + const checkModuleSpecifiers = options.checkModuleSpecifiers ?? true; + const checkConfigPaths = options.checkConfigPaths ?? true; + const checkChannelComparisons = options.checkChannelComparisons ?? true; + const checkChannelAssignments = options.checkChannelAssignments ?? true; + const moduleSpecifierMatcher = options.moduleSpecifierMatcher ?? matchesChannelModuleSpecifier; + + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true); + const violations = []; + + const visit = (node) => { + if ( + checkModuleSpecifiers && + ts.isImportDeclaration(node) && + ts.isStringLiteral(node.moduleSpecifier) + ) { + const specifier = node.moduleSpecifier.text; + if (moduleSpecifierMatcher(specifier)) { + violations.push({ + line: toLine(sourceFile, node.moduleSpecifier), + reason: `imports channel module "${specifier}"`, + }); + } + } + + if ( + checkModuleSpecifiers && + ts.isExportDeclaration(node) && + node.moduleSpecifier && + ts.isStringLiteral(node.moduleSpecifier) + ) { + const specifier = node.moduleSpecifier.text; + if (moduleSpecifierMatcher(specifier)) { + violations.push({ + line: toLine(sourceFile, node.moduleSpecifier), + reason: `re-exports channel module "${specifier}"`, + }); + } + } + + if ( + checkModuleSpecifiers && + ts.isCallExpression(node) && + node.expression.kind === ts.SyntaxKind.ImportKeyword && + node.arguments.length > 0 && + ts.isStringLiteral(node.arguments[0]) + ) { + const specifier = node.arguments[0].text; + if (moduleSpecifierMatcher(specifier)) { + violations.push({ + line: toLine(sourceFile, node.arguments[0]), + reason: `dynamically imports channel module "${specifier}"`, + }); + } + } + + if ( + checkConfigPaths && + ts.isPropertyAccessExpression(node) && + channelIdSet.has(node.name.text) + ) { + if (isChannelsPropertyAccess(node.expression)) { + violations.push({ + line: toLine(sourceFile, node.name), + reason: `references config path "channels.${node.name.text}"`, + }); + } + } + + if ( + checkConfigPaths && + ts.isElementAccessExpression(node) && + ts.isStringLiteral(node.argumentExpression) && + channelIdSet.has(node.argumentExpression.text) + ) { + if (isChannelsPropertyAccess(node.expression)) { + violations.push({ + line: toLine(sourceFile, node.argumentExpression), + reason: `references config path "channels[${JSON.stringify(node.argumentExpression.text)}]"`, + }); + } + } + + if ( + checkChannelComparisons && + ts.isBinaryExpression(node) && + comparisonOperators.has(node.operatorToken.kind) + ) { + if (isChannelLiteralNode(node.left) || isChannelLiteralNode(node.right)) { + const leftText = node.left.getText(sourceFile); + const rightText = node.right.getText(sourceFile); + violations.push({ + line: toLine(sourceFile, node.operatorToken), + reason: `compares with channel id literal (${leftText} ${node.operatorToken.getText(sourceFile)} ${rightText})`, + }); + } + } + + if (checkChannelAssignments && ts.isPropertyAssignment(node)) { + const propName = getPropertyNameText(node.name); + if (propName === "channel" && isChannelLiteralNode(node.initializer)) { + violations.push({ + line: toLine(sourceFile, node.initializer), + reason: `assigns channel id literal to "channel" (${node.initializer.getText(sourceFile)})`, + }); + } + } + + ts.forEachChild(node, visit); + }; + + visit(sourceFile); + return violations; +} + +export function findChannelCoreReverseDependencyViolations(content, fileName = "source.ts") { + return findChannelAgnosticBoundaryViolations(content, fileName, { + checkModuleSpecifiers: true, + checkConfigPaths: false, + checkChannelComparisons: false, + checkChannelAssignments: false, + moduleSpecifierMatcher: matchesChannelModuleSpecifier, + }); +} + +export function findAcpUserFacingChannelNameViolations(content, fileName = "source.ts") { + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true); + const violations = []; + + const visit = (node) => { + const text = readStringLiteral(node); + if (text && userFacingChannelNameRe.test(text) && !isModuleSpecifierStringNode(node)) { + violations.push({ + line: toLine(sourceFile, node), + reason: `user-facing text references channel name (${JSON.stringify(text)})`, + }); + } + ts.forEachChild(node, visit); + }; + + visit(sourceFile); + return violations; +} + +export function findSystemMarkLiteralViolations(content, fileName = "source.ts") { + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true); + const violations = []; + + const visit = (node) => { + const text = readStringLiteral(node); + if (text && text.includes(systemMarkLiteral) && !isModuleSpecifierStringNode(node)) { + violations.push({ + line: toLine(sourceFile, node), + reason: `hardcoded system mark literal (${JSON.stringify(text)})`, + }); + } + ts.forEachChild(node, visit); + }; + + visit(sourceFile); + return violations; +} + +const boundaryRuleSets = [ + { + id: "acp-core", + sources: acpCoreProtectedSources, + scan: (content, fileName) => findChannelAgnosticBoundaryViolations(content, fileName), + }, + { + id: "channel-core-reverse-deps", + sources: channelCoreProtectedSources, + scan: (content, fileName) => findChannelCoreReverseDependencyViolations(content, fileName), + }, + { + id: "acp-user-facing-text", + sources: acpUserFacingTextSources, + scan: (content, fileName) => findAcpUserFacingChannelNameViolations(content, fileName), + }, + { + id: "system-mark-literal-usage", + sources: systemMarkLiteralGuardSources, + scan: (content, fileName) => findSystemMarkLiteralViolations(content, fileName), + }, +]; + +export async function main() { + const violations = []; + for (const ruleSet of boundaryRuleSets) { + const files = ( + await Promise.all( + ruleSet.sources.map(async (sourcePath) => { + try { + return await collectTypeScriptFiles(sourcePath); + } catch (error) { + if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") { + return []; + } + throw error; + } + }), + ) + ).flat(); + for (const filePath of files) { + const relativeFile = path.relative(repoRoot, filePath); + if ( + allowedViolations.has(`${ruleSet.id}:${relativeFile}`) || + allowedViolations.has(relativeFile) + ) { + continue; + } + const content = await fs.readFile(filePath, "utf8"); + for (const violation of ruleSet.scan(content, relativeFile)) { + violations.push(`${ruleSet.id} ${relativeFile}:${violation.line}: ${violation.reason}`); + } + } + } + + if (violations.length === 0) { + return; + } + + console.error("Found channel-specific references in channel-agnostic sources:"); + for (const violation of violations) { + console.error(`- ${violation}`); + } + console.error( + "Move channel-specific logic to channel adapters or add a justified allowlist entry.", + ); + process.exit(1); +} + +const isDirectExecution = (() => { + const entry = process.argv[1]; + if (!entry) { + return false; + } + return path.resolve(entry) === fileURLToPath(import.meta.url); +})(); + +if (isDirectExecution) { + main().catch((error) => { + console.error(error); + process.exit(1); + }); +} diff --git a/scripts/dev/discord-acp-plain-language-smoke.ts b/scripts/dev/discord-acp-plain-language-smoke.ts new file mode 100644 index 000000000000..33b8eb0d54f9 --- /dev/null +++ b/scripts/dev/discord-acp-plain-language-smoke.ts @@ -0,0 +1,779 @@ +#!/usr/bin/env bun +// Manual ACP thread smoke for plain-language routing. +// Keep this script available for regression/debug validation. Do not delete. +import { randomUUID } from "node:crypto"; +import fs from "node:fs/promises"; +import path from "node:path"; + +type ThreadBindingRecord = { + accountId?: string; + channelId?: string; + threadId?: string; + targetKind?: string; + targetSessionKey?: string; + agentId?: string; + boundBy?: string; + boundAt?: number; +}; + +type ThreadBindingsPayload = { + version?: number; + bindings?: Record; +}; + +type DiscordMessage = { + id: string; + content?: string; + timestamp?: string; + author?: { + id?: string; + username?: string; + bot?: boolean; + }; +}; + +type DiscordUser = { + id: string; + username: string; + bot?: boolean; +}; + +type DriverMode = "token" | "webhook"; + +type Args = { + channelId: string; + driverMode: DriverMode; + driverToken: string; + driverTokenPrefix: string; + botToken: string; + botTokenPrefix: string; + targetAgent: string; + timeoutMs: number; + pollMs: number; + mentionUserId?: string; + instruction?: string; + threadBindingsPath: string; + json: boolean; +}; + +type SuccessResult = { + ok: true; + smokeId: string; + ackToken: string; + sentMessageId: string; + binding: { + threadId: string; + targetSessionKey: string; + targetKind: string; + agentId: string; + boundAt: number; + accountId?: string; + channelId?: string; + }; + ackMessage: { + id: string; + authorId?: string; + authorUsername?: string; + timestamp?: string; + content?: string; + }; +}; + +type FailureResult = { + ok: false; + smokeId: string; + stage: "validation" | "send-message" | "wait-binding" | "wait-ack" | "discord-api" | "unexpected"; + error: string; + diagnostics?: { + parentChannelRecent?: Array<{ + id: string; + author?: string; + bot?: boolean; + content?: string; + }>; + bindingCandidates?: Array<{ + threadId: string; + targetSessionKey: string; + targetKind?: string; + agentId?: string; + boundAt?: number; + }>; + }; +}; + +const DISCORD_API_BASE = "https://discord.com/api/v10"; + +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +function parseNumber(value: string | undefined, fallback: number): number { + if (!value) { + return fallback; + } + const parsed = Number.parseInt(value, 10); + return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback; +} + +function resolveStateDir(): string { + const override = process.env.OPENCLAW_STATE_DIR?.trim() || process.env.CLAWDBOT_STATE_DIR?.trim(); + if (override) { + return override.startsWith("~") + ? path.resolve(process.env.HOME || "", override.slice(1)) + : path.resolve(override); + } + const home = process.env.OPENCLAW_HOME?.trim() || process.env.HOME || ""; + return path.join(home, ".openclaw"); +} + +function resolveArg(flag: string): string | undefined { + const argv = process.argv.slice(2); + const eq = argv.find((entry) => entry.startsWith(`${flag}=`)); + if (eq) { + return eq.slice(flag.length + 1); + } + const idx = argv.indexOf(flag); + if (idx >= 0 && idx + 1 < argv.length) { + return argv[idx + 1]; + } + return undefined; +} + +function hasFlag(flag: string): boolean { + return process.argv.slice(2).includes(flag); +} + +function usage(): string { + return ( + "Usage: bun scripts/dev/discord-acp-plain-language-smoke.ts " + + "--channel [--token | --driver webhook --bot-token ] [options]\n\n" + + "Manual live smoke only (not CI). Sends a plain-language instruction in Discord and verifies:\n" + + "1) OpenClaw spawned an ACP thread binding\n" + + "2) agent replied in that bound thread with the expected ACK token\n\n" + + "Options:\n" + + " --channel Parent Discord channel id (required)\n" + + " --driver Driver transport mode (default: token)\n" + + " --token Driver Discord token (required for driver=token)\n" + + " --token-prefix Auth prefix for --token (default: Bot)\n" + + " --bot-token Bot token for webhook driver mode\n" + + " --bot-token-prefix Auth prefix for --bot-token (default: Bot)\n" + + " --agent Expected ACP agent id (default: codex)\n" + + " --mention Mention this user in the instruction (optional)\n" + + " --instruction Custom instruction template (optional)\n" + + " --timeout-ms Total timeout in ms (default: 240000)\n" + + " --poll-ms Poll interval in ms (default: 1500)\n" + + " --thread-bindings-path

Override thread-bindings json path\n" + + " --json Emit JSON output\n" + + "\n" + + "Environment fallbacks:\n" + + " OPENCLAW_DISCORD_SMOKE_CHANNEL_ID\n" + + " OPENCLAW_DISCORD_SMOKE_DRIVER\n" + + " OPENCLAW_DISCORD_SMOKE_DRIVER_TOKEN\n" + + " OPENCLAW_DISCORD_SMOKE_DRIVER_TOKEN_PREFIX\n" + + " OPENCLAW_DISCORD_SMOKE_BOT_TOKEN\n" + + " OPENCLAW_DISCORD_SMOKE_BOT_TOKEN_PREFIX\n" + + " OPENCLAW_DISCORD_SMOKE_AGENT\n" + + " OPENCLAW_DISCORD_SMOKE_MENTION_USER_ID\n" + + " OPENCLAW_DISCORD_SMOKE_TIMEOUT_MS\n" + + " OPENCLAW_DISCORD_SMOKE_POLL_MS\n" + + " OPENCLAW_DISCORD_SMOKE_THREAD_BINDINGS_PATH" + ); +} + +function parseArgs(): Args { + const channelId = + resolveArg("--channel") || + process.env.OPENCLAW_DISCORD_SMOKE_CHANNEL_ID || + process.env.CLAWDBOT_DISCORD_SMOKE_CHANNEL_ID || + ""; + const driverModeRaw = + resolveArg("--driver") || + process.env.OPENCLAW_DISCORD_SMOKE_DRIVER || + process.env.CLAWDBOT_DISCORD_SMOKE_DRIVER || + "token"; + const normalizedDriverMode = driverModeRaw.trim().toLowerCase(); + const driverMode: DriverMode = + normalizedDriverMode === "webhook" + ? "webhook" + : normalizedDriverMode === "token" + ? "token" + : "token"; + const driverToken = + resolveArg("--token") || + process.env.OPENCLAW_DISCORD_SMOKE_DRIVER_TOKEN || + process.env.CLAWDBOT_DISCORD_SMOKE_DRIVER_TOKEN || + ""; + const driverTokenPrefix = + resolveArg("--token-prefix") || process.env.OPENCLAW_DISCORD_SMOKE_DRIVER_TOKEN_PREFIX || "Bot"; + const botToken = + resolveArg("--bot-token") || + process.env.OPENCLAW_DISCORD_SMOKE_BOT_TOKEN || + process.env.CLAWDBOT_DISCORD_SMOKE_BOT_TOKEN || + process.env.DISCORD_BOT_TOKEN || + ""; + const botTokenPrefix = + resolveArg("--bot-token-prefix") || + process.env.OPENCLAW_DISCORD_SMOKE_BOT_TOKEN_PREFIX || + "Bot"; + const targetAgent = + resolveArg("--agent") || + process.env.OPENCLAW_DISCORD_SMOKE_AGENT || + process.env.CLAWDBOT_DISCORD_SMOKE_AGENT || + "codex"; + const mentionUserId = + resolveArg("--mention") || + process.env.OPENCLAW_DISCORD_SMOKE_MENTION_USER_ID || + process.env.CLAWDBOT_DISCORD_SMOKE_MENTION_USER_ID || + undefined; + const instruction = + resolveArg("--instruction") || + process.env.OPENCLAW_DISCORD_SMOKE_INSTRUCTION || + process.env.CLAWDBOT_DISCORD_SMOKE_INSTRUCTION || + undefined; + const timeoutMs = parseNumber( + resolveArg("--timeout-ms") || process.env.OPENCLAW_DISCORD_SMOKE_TIMEOUT_MS, + 240_000, + ); + const pollMs = parseNumber( + resolveArg("--poll-ms") || process.env.OPENCLAW_DISCORD_SMOKE_POLL_MS, + 1_500, + ); + const defaultBindingsPath = path.join(resolveStateDir(), "discord", "thread-bindings.json"); + const threadBindingsPath = + resolveArg("--thread-bindings-path") || + process.env.OPENCLAW_DISCORD_SMOKE_THREAD_BINDINGS_PATH || + defaultBindingsPath; + const json = hasFlag("--json"); + + if (!channelId) { + throw new Error(usage()); + } + if (driverMode === "token" && !driverToken) { + throw new Error(usage()); + } + if (driverMode === "webhook" && !botToken) { + throw new Error(usage()); + } + + return { + channelId, + driverMode, + driverToken, + driverTokenPrefix, + botToken, + botTokenPrefix, + targetAgent, + timeoutMs, + pollMs, + mentionUserId, + instruction, + threadBindingsPath, + json, + }; +} + +function resolveAuthorizationHeader(params: { token: string; tokenPrefix: string }): string { + const token = params.token.trim(); + if (!token) { + throw new Error("Missing Discord driver token."); + } + if (token.includes(" ")) { + return token; + } + return `${params.tokenPrefix.trim() || "Bot"} ${token}`; +} + +async function discordApi(params: { + method: "GET" | "POST"; + path: string; + authHeader: string; + body?: unknown; + retries?: number; +}): Promise { + const retries = params.retries ?? 6; + for (let attempt = 0; attempt <= retries; attempt += 1) { + const response = await fetch(`${DISCORD_API_BASE}${params.path}`, { + method: params.method, + headers: { + Authorization: params.authHeader, + "Content-Type": "application/json", + }, + body: params.body === undefined ? undefined : JSON.stringify(params.body), + }); + + if (response.status === 429) { + const body = (await response.json().catch(() => ({}))) as { retry_after?: number }; + const waitSeconds = typeof body.retry_after === "number" ? body.retry_after : 1; + await sleep(Math.ceil(waitSeconds * 1000)); + continue; + } + + if (!response.ok) { + const text = await response.text().catch(() => ""); + throw new Error( + `Discord API ${params.method} ${params.path} failed: ${response.status} ${response.statusText}${text ? ` :: ${text}` : ""}`, + ); + } + + if (response.status === 204) { + return undefined as T; + } + + return (await response.json()) as T; + } + + throw new Error(`Discord API ${params.method} ${params.path} exceeded retry budget.`); +} + +async function discordWebhookApi(params: { + method: "POST" | "DELETE"; + webhookId: string; + webhookToken: string; + body?: unknown; + query?: string; + retries?: number; +}): Promise { + const retries = params.retries ?? 6; + const suffix = params.query ? `?${params.query}` : ""; + const path = `/webhooks/${encodeURIComponent(params.webhookId)}/${encodeURIComponent(params.webhookToken)}${suffix}`; + for (let attempt = 0; attempt <= retries; attempt += 1) { + const response = await fetch(`${DISCORD_API_BASE}${path}`, { + method: params.method, + headers: { + "Content-Type": "application/json", + }, + body: params.body === undefined ? undefined : JSON.stringify(params.body), + }); + + if (response.status === 429) { + const body = (await response.json().catch(() => ({}))) as { retry_after?: number }; + const waitSeconds = typeof body.retry_after === "number" ? body.retry_after : 1; + await sleep(Math.ceil(waitSeconds * 1000)); + continue; + } + + if (!response.ok) { + const text = await response.text().catch(() => ""); + throw new Error( + `Discord webhook API ${params.method} ${path} failed: ${response.status} ${response.statusText}${text ? ` :: ${text}` : ""}`, + ); + } + + if (response.status === 204) { + return undefined as T; + } + + return (await response.json()) as T; + } + + throw new Error(`Discord webhook API ${params.method} ${path} exceeded retry budget.`); +} + +async function readThreadBindings(filePath: string): Promise { + const raw = await fs.readFile(filePath, "utf8"); + const payload = JSON.parse(raw) as ThreadBindingsPayload; + const entries = Object.values(payload.bindings ?? {}); + return entries.filter((entry) => Boolean(entry?.threadId && entry?.targetSessionKey)); +} + +function normalizeBoundAt(record: ThreadBindingRecord): number { + if (typeof record.boundAt === "number" && Number.isFinite(record.boundAt)) { + return record.boundAt; + } + return 0; +} + +function resolveCandidateBindings(params: { + entries: ThreadBindingRecord[]; + minBoundAt: number; + targetAgent: string; +}): ThreadBindingRecord[] { + const normalizedTargetAgent = params.targetAgent.trim().toLowerCase(); + return params.entries + .filter((entry) => { + const targetKind = String(entry.targetKind || "") + .trim() + .toLowerCase(); + if (targetKind !== "acp") { + return false; + } + if (normalizeBoundAt(entry) < params.minBoundAt) { + return false; + } + const agentId = String(entry.agentId || "") + .trim() + .toLowerCase(); + if (normalizedTargetAgent && agentId && agentId !== normalizedTargetAgent) { + return false; + } + return true; + }) + .toSorted((a, b) => normalizeBoundAt(b) - normalizeBoundAt(a)); +} + +function buildInstruction(params: { + smokeId: string; + ackToken: string; + targetAgent: string; + mentionUserId?: string; + template?: string; +}): string { + const mentionPrefix = params.mentionUserId?.trim() ? `<@${params.mentionUserId.trim()}> ` : ""; + if (params.template?.trim()) { + return mentionPrefix + params.template.trim(); + } + return ( + mentionPrefix + + `Manual smoke ${params.smokeId}: Please spawn a ${params.targetAgent} ACP coding agent in a thread for this request, keep it persistent, and in that thread reply with exactly "${params.ackToken}" and nothing else.` + ); +} + +function toRecentMessageRow(message: DiscordMessage) { + return { + id: message.id, + author: message.author?.username || message.author?.id || "unknown", + bot: Boolean(message.author?.bot), + content: (message.content || "").slice(0, 500), + }; +} + +function printOutput(params: { json: boolean; payload: SuccessResult | FailureResult }) { + if (params.json) { + // eslint-disable-next-line no-console + console.log(JSON.stringify(params.payload, null, 2)); + return; + } + if (params.payload.ok) { + const success = params.payload; + // eslint-disable-next-line no-console + console.log("PASS"); + // eslint-disable-next-line no-console + console.log(`smokeId: ${success.smokeId}`); + // eslint-disable-next-line no-console + console.log(`sentMessageId: ${success.sentMessageId}`); + // eslint-disable-next-line no-console + console.log(`threadId: ${success.binding.threadId}`); + // eslint-disable-next-line no-console + console.log(`sessionKey: ${success.binding.targetSessionKey}`); + // eslint-disable-next-line no-console + console.log(`ackMessageId: ${success.ackMessage.id}`); + // eslint-disable-next-line no-console + console.log( + `ackAuthor: ${success.ackMessage.authorUsername || success.ackMessage.authorId || "unknown"}`, + ); + return; + } + const failure = params.payload; + // eslint-disable-next-line no-console + console.error("FAIL"); + // eslint-disable-next-line no-console + console.error(`stage: ${failure.stage}`); + // eslint-disable-next-line no-console + console.error(`smokeId: ${failure.smokeId}`); + // eslint-disable-next-line no-console + console.error(`error: ${failure.error}`); + if (failure.diagnostics?.bindingCandidates?.length) { + // eslint-disable-next-line no-console + console.error("binding candidates:"); + for (const candidate of failure.diagnostics.bindingCandidates) { + // eslint-disable-next-line no-console + console.error( + ` thread=${candidate.threadId} kind=${candidate.targetKind || "?"} agent=${candidate.agentId || "?"} boundAt=${candidate.boundAt || 0} session=${candidate.targetSessionKey}`, + ); + } + } + if (failure.diagnostics?.parentChannelRecent?.length) { + // eslint-disable-next-line no-console + console.error("recent parent channel messages:"); + for (const row of failure.diagnostics.parentChannelRecent) { + // eslint-disable-next-line no-console + console.error(` ${row.id} ${row.author}${row.bot ? " [bot]" : ""}: ${row.content || ""}`); + } + } +} + +async function run(): Promise { + let args: Args; + try { + args = parseArgs(); + } catch (err) { + return { + ok: false, + stage: "validation", + smokeId: "n/a", + error: err instanceof Error ? err.message : String(err), + }; + } + + const smokeId = `acp-smoke-${Date.now()}-${randomUUID().slice(0, 8)}`; + const ackToken = `ACP_SMOKE_ACK_${smokeId}`; + const instruction = buildInstruction({ + smokeId, + ackToken, + targetAgent: args.targetAgent, + mentionUserId: args.mentionUserId, + template: args.instruction, + }); + + let readAuthHeader = ""; + let sentMessageId = ""; + let setupStage: "discord-api" | "send-message" = "discord-api"; + let senderAuthorId: string | undefined; + let webhookForCleanup: + | { + id: string; + token: string; + } + | undefined; + + try { + if (args.driverMode === "token") { + const authHeader = resolveAuthorizationHeader({ + token: args.driverToken, + tokenPrefix: args.driverTokenPrefix, + }); + readAuthHeader = authHeader; + + const driverUser = await discordApi({ + method: "GET", + path: "/users/@me", + authHeader, + }); + senderAuthorId = driverUser.id; + + setupStage = "send-message"; + const sent = await discordApi({ + method: "POST", + path: `/channels/${encodeURIComponent(args.channelId)}/messages`, + authHeader, + body: { + content: instruction, + allowed_mentions: args.mentionUserId + ? { parse: [], users: [args.mentionUserId] } + : { parse: [] }, + }, + }); + sentMessageId = sent.id; + } else { + const botAuthHeader = resolveAuthorizationHeader({ + token: args.botToken, + tokenPrefix: args.botTokenPrefix, + }); + readAuthHeader = botAuthHeader; + + await discordApi({ + method: "GET", + path: "/users/@me", + authHeader: botAuthHeader, + }); + + setupStage = "send-message"; + const webhook = await discordApi<{ id: string; token?: string | null }>({ + method: "POST", + path: `/channels/${encodeURIComponent(args.channelId)}/webhooks`, + authHeader: botAuthHeader, + body: { + name: `openclaw-acp-smoke-${smokeId.slice(-8)}`, + }, + }); + if (!webhook.id || !webhook.token) { + return { + ok: false, + stage: "send-message", + smokeId, + error: + "Discord webhook creation succeeded but no webhook token was returned; cannot post smoke message.", + }; + } + webhookForCleanup = { id: webhook.id, token: webhook.token }; + + const sent = await discordWebhookApi({ + method: "POST", + webhookId: webhook.id, + webhookToken: webhook.token, + query: "wait=true", + body: { + content: instruction, + allowed_mentions: args.mentionUserId + ? { parse: [], users: [args.mentionUserId] } + : { parse: [] }, + }, + }); + sentMessageId = sent.id; + senderAuthorId = sent.author?.id; + } + } catch (err) { + return { + ok: false, + stage: setupStage, + smokeId, + error: err instanceof Error ? err.message : String(err), + }; + } + + const startedAt = Date.now(); + + const deadline = startedAt + args.timeoutMs; + let winningBinding: ThreadBindingRecord | undefined; + let latestCandidates: ThreadBindingRecord[] = []; + + try { + while (Date.now() < deadline && !winningBinding) { + try { + const entries = await readThreadBindings(args.threadBindingsPath); + latestCandidates = resolveCandidateBindings({ + entries, + minBoundAt: startedAt - 3_000, + targetAgent: args.targetAgent, + }); + winningBinding = latestCandidates[0]; + } catch { + // Keep polling; file may not exist yet or may be mid-write. + } + if (!winningBinding) { + await sleep(args.pollMs); + } + } + + if (!winningBinding?.threadId || !winningBinding?.targetSessionKey) { + let parentRecent: DiscordMessage[] = []; + try { + parentRecent = await discordApi({ + method: "GET", + path: `/channels/${encodeURIComponent(args.channelId)}/messages?limit=20`, + authHeader: readAuthHeader, + }); + } catch { + // Best effort diagnostics only. + } + return { + ok: false, + stage: "wait-binding", + smokeId, + error: `Timed out waiting for new ACP thread binding (path: ${args.threadBindingsPath}).`, + diagnostics: { + bindingCandidates: latestCandidates.slice(0, 6).map((entry) => ({ + threadId: entry.threadId || "", + targetSessionKey: entry.targetSessionKey || "", + targetKind: entry.targetKind, + agentId: entry.agentId, + boundAt: entry.boundAt, + })), + parentChannelRecent: parentRecent.map(toRecentMessageRow), + }, + }; + } + + const threadId = winningBinding.threadId; + let ackMessage: DiscordMessage | undefined; + while (Date.now() < deadline && !ackMessage) { + try { + const threadMessages = await discordApi({ + method: "GET", + path: `/channels/${encodeURIComponent(threadId)}/messages?limit=50`, + authHeader: readAuthHeader, + }); + ackMessage = threadMessages.find((message) => { + const content = message.content || ""; + if (!content.includes(ackToken)) { + return false; + } + const authorId = message.author?.id || ""; + return !senderAuthorId || authorId !== senderAuthorId; + }); + } catch { + // Keep polling; thread can appear before read permissions settle. + } + if (!ackMessage) { + await sleep(args.pollMs); + } + } + + if (!ackMessage) { + let parentRecent: DiscordMessage[] = []; + try { + parentRecent = await discordApi({ + method: "GET", + path: `/channels/${encodeURIComponent(args.channelId)}/messages?limit=20`, + authHeader: readAuthHeader, + }); + } catch { + // Best effort diagnostics only. + } + + return { + ok: false, + stage: "wait-ack", + smokeId, + error: `Thread bound (${threadId}) but timed out waiting for ACK token "${ackToken}" from OpenClaw.`, + diagnostics: { + bindingCandidates: [ + { + threadId: winningBinding.threadId || "", + targetSessionKey: winningBinding.targetSessionKey || "", + targetKind: winningBinding.targetKind, + agentId: winningBinding.agentId, + boundAt: winningBinding.boundAt, + }, + ], + parentChannelRecent: parentRecent.map(toRecentMessageRow), + }, + }; + } + + return { + ok: true, + smokeId, + ackToken, + sentMessageId, + binding: { + threadId, + targetSessionKey: winningBinding.targetSessionKey, + targetKind: String(winningBinding.targetKind || "acp"), + agentId: String(winningBinding.agentId || args.targetAgent), + boundAt: normalizeBoundAt(winningBinding), + accountId: winningBinding.accountId, + channelId: winningBinding.channelId, + }, + ackMessage: { + id: ackMessage.id, + authorId: ackMessage.author?.id, + authorUsername: ackMessage.author?.username, + timestamp: ackMessage.timestamp, + content: ackMessage.content, + }, + }; + } finally { + if (webhookForCleanup) { + await discordWebhookApi({ + method: "DELETE", + webhookId: webhookForCleanup.id, + webhookToken: webhookForCleanup.token, + }).catch(() => { + // Best-effort cleanup only. + }); + } + } +} + +if (hasFlag("--help") || hasFlag("-h")) { + // eslint-disable-next-line no-console + console.log(usage()); + process.exit(0); +} + +const result = await run().catch( + (err): FailureResult => ({ + ok: false, + stage: "unexpected", + smokeId: "n/a", + error: err instanceof Error ? err.message : String(err), + }), +); + +printOutput({ + json: hasFlag("--json"), + payload: result, +}); + +process.exit(result.ok ? 0 : 1); diff --git a/skills/coding-agent/SKILL.md b/skills/coding-agent/SKILL.md index ef4e059499d2..cca6ef83ad54 100644 --- a/skills/coding-agent/SKILL.md +++ b/skills/coding-agent/SKILL.md @@ -1,6 +1,6 @@ --- name: coding-agent -description: "Delegate coding tasks to Codex, Claude Code, or Pi agents via background process. Use when: (1) building/creating new features or apps, (2) reviewing PRs (spawn in temp dir), (3) refactoring large codebases, (4) iterative coding that needs file exploration. NOT for: simple one-liner fixes (just edit), reading code (use read tool), or any work in ~/clawd workspace (never spawn agents here). Requires a bash tool that supports pty:true." +description: 'Delegate coding tasks to Codex, Claude Code, or Pi agents via background process. Use when: (1) building/creating new features or apps, (2) reviewing PRs (spawn in temp dir), (3) refactoring large codebases, (4) iterative coding that needs file exploration. NOT for: simple one-liner fixes (just edit), reading code (use read tool), thread-bound ACP harness requests in chat (for example spawn/run Codex or Claude Code in a Discord thread; use sessions_spawn with runtime:"acp"), or any work in ~/clawd workspace (never spawn agents here). Requires a bash tool that supports pty:true.' metadata: { "openclaw": { "emoji": "🧩", "requires": { "anyBins": ["claude", "codex", "opencode", "pi"] } }, diff --git a/src/acp/control-plane/manager.core.ts b/src/acp/control-plane/manager.core.ts new file mode 100644 index 000000000000..99ec096bb7fb --- /dev/null +++ b/src/acp/control-plane/manager.core.ts @@ -0,0 +1,1314 @@ +import type { OpenClawConfig } from "../../config/config.js"; +import { logVerbose } from "../../globals.js"; +import { normalizeAgentId } from "../../routing/session-key.js"; +import { isAcpSessionKey } from "../../sessions/session-key-utils.js"; +import { + AcpRuntimeError, + toAcpRuntimeError, + withAcpRuntimeErrorBoundary, +} from "../runtime/errors.js"; +import { + createIdentityFromEnsure, + identityEquals, + isSessionIdentityPending, + mergeSessionIdentity, + resolveRuntimeHandleIdentifiersFromIdentity, + resolveSessionIdentityFromMeta, +} from "../runtime/session-identity.js"; +import type { + AcpRuntime, + AcpRuntimeCapabilities, + AcpRuntimeHandle, + AcpRuntimeStatus, +} from "../runtime/types.js"; +import { reconcileManagerRuntimeSessionIdentifiers } from "./manager.identity-reconcile.js"; +import { + applyManagerRuntimeControls, + resolveManagerRuntimeCapabilities, +} from "./manager.runtime-controls.js"; +import { + type AcpCloseSessionInput, + type AcpCloseSessionResult, + type AcpInitializeSessionInput, + type AcpManagerObservabilitySnapshot, + type AcpRunTurnInput, + type AcpSessionManagerDeps, + type AcpSessionResolution, + type AcpSessionRuntimeOptions, + type AcpSessionStatus, + type AcpStartupIdentityReconcileResult, + type ActiveTurnState, + DEFAULT_DEPS, + type SessionAcpMeta, + type SessionEntry, + type TurnLatencyStats, +} from "./manager.types.js"; +import { + createUnsupportedControlError, + hasLegacyAcpIdentityProjection, + normalizeAcpErrorCode, + normalizeActorKey, + normalizeSessionKey, + resolveAcpAgentFromSessionKey, + resolveMissingMetaError, + resolveRuntimeIdleTtlMs, +} from "./manager.utils.js"; +import { CachedRuntimeState, RuntimeCache } from "./runtime-cache.js"; +import { + inferRuntimeOptionPatchFromConfigOption, + mergeRuntimeOptions, + normalizeRuntimeOptions, + normalizeText, + resolveRuntimeOptionsFromMeta, + runtimeOptionsEqual, + validateRuntimeConfigOptionInput, + validateRuntimeModeInput, + validateRuntimeOptionPatch, +} from "./runtime-options.js"; +import { SessionActorQueue } from "./session-actor-queue.js"; + +export class AcpSessionManager { + private readonly actorQueue = new SessionActorQueue(); + private readonly actorTailBySession = this.actorQueue.getTailMapForTesting(); + private readonly runtimeCache = new RuntimeCache(); + private readonly activeTurnBySession = new Map(); + private readonly turnLatencyStats: TurnLatencyStats = { + completed: 0, + failed: 0, + totalMs: 0, + maxMs: 0, + }; + private readonly errorCountsByCode = new Map(); + private evictedRuntimeCount = 0; + private lastEvictedAt: number | undefined; + + constructor(private readonly deps: AcpSessionManagerDeps = DEFAULT_DEPS) {} + + resolveSession(params: { cfg: OpenClawConfig; sessionKey: string }): AcpSessionResolution { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + return { + kind: "none", + sessionKey, + }; + } + const acp = this.deps.readSessionEntry({ + cfg: params.cfg, + sessionKey, + })?.acp; + if (acp) { + return { + kind: "ready", + sessionKey, + meta: acp, + }; + } + if (isAcpSessionKey(sessionKey)) { + return { + kind: "stale", + sessionKey, + error: resolveMissingMetaError(sessionKey), + }; + } + return { + kind: "none", + sessionKey, + }; + } + + getObservabilitySnapshot(cfg: OpenClawConfig): AcpManagerObservabilitySnapshot { + const completedTurns = this.turnLatencyStats.completed + this.turnLatencyStats.failed; + const averageLatencyMs = + completedTurns > 0 ? Math.round(this.turnLatencyStats.totalMs / completedTurns) : 0; + return { + runtimeCache: { + activeSessions: this.runtimeCache.size(), + idleTtlMs: resolveRuntimeIdleTtlMs(cfg), + evictedTotal: this.evictedRuntimeCount, + ...(this.lastEvictedAt ? { lastEvictedAt: this.lastEvictedAt } : {}), + }, + turns: { + active: this.activeTurnBySession.size, + queueDepth: this.actorQueue.getTotalPendingCount(), + completed: this.turnLatencyStats.completed, + failed: this.turnLatencyStats.failed, + averageLatencyMs, + maxLatencyMs: this.turnLatencyStats.maxMs, + }, + errorsByCode: Object.fromEntries( + [...this.errorCountsByCode.entries()].toSorted(([a], [b]) => a.localeCompare(b)), + ), + }; + } + + async reconcilePendingSessionIdentities(params: { + cfg: OpenClawConfig; + }): Promise { + let checked = 0; + let resolved = 0; + let failed = 0; + + let acpSessions: Awaited>; + try { + acpSessions = await this.deps.listAcpSessions({ + cfg: params.cfg, + }); + } catch (error) { + logVerbose(`acp-manager: startup identity scan failed: ${String(error)}`); + return { checked, resolved, failed: failed + 1 }; + } + + for (const session of acpSessions) { + if (!session.acp || !session.sessionKey) { + continue; + } + const currentIdentity = resolveSessionIdentityFromMeta(session.acp); + if (!isSessionIdentityPending(currentIdentity)) { + continue; + } + + checked += 1; + try { + const becameResolved = await this.withSessionActor(session.sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey: session.sessionKey, + }); + if (resolution.kind !== "ready") { + return false; + } + const { runtime, handle, meta } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey: session.sessionKey, + meta: resolution.meta, + }); + const reconciled = await this.reconcileRuntimeSessionIdentifiers({ + cfg: params.cfg, + sessionKey: session.sessionKey, + runtime, + handle, + meta, + failOnStatusError: false, + }); + return !isSessionIdentityPending(resolveSessionIdentityFromMeta(reconciled.meta)); + }); + if (becameResolved) { + resolved += 1; + } + } catch (error) { + failed += 1; + logVerbose( + `acp-manager: startup identity reconcile failed for ${session.sessionKey}: ${String(error)}`, + ); + } + } + + return { checked, resolved, failed }; + } + + async initializeSession(input: AcpInitializeSessionInput): Promise<{ + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + }> { + const sessionKey = normalizeSessionKey(input.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + const agent = normalizeAgentId(input.agent); + await this.evictIdleRuntimeHandles({ cfg: input.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const backend = this.deps.requireRuntimeBackend(input.backendId || input.cfg.acp?.backend); + const runtime = backend.runtime; + const initialRuntimeOptions = validateRuntimeOptionPatch({ cwd: input.cwd }); + const requestedCwd = initialRuntimeOptions.cwd; + this.enforceConcurrentSessionLimit({ + cfg: input.cfg, + sessionKey, + }); + const handle = await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.ensureSession({ + sessionKey, + agent, + mode: input.mode, + cwd: requestedCwd, + }), + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Could not initialize ACP session runtime.", + }); + const effectiveCwd = normalizeText(handle.cwd) ?? requestedCwd; + const effectiveRuntimeOptions = normalizeRuntimeOptions({ + ...initialRuntimeOptions, + ...(effectiveCwd ? { cwd: effectiveCwd } : {}), + }); + + const identityNow = Date.now(); + const initializedIdentity = + mergeSessionIdentity({ + current: undefined, + incoming: createIdentityFromEnsure({ + handle, + now: identityNow, + }), + now: identityNow, + }) ?? + ({ + state: "pending", + source: "ensure", + lastUpdatedAt: identityNow, + } as const); + const meta: SessionAcpMeta = { + backend: handle.backend || backend.id, + agent, + runtimeSessionName: handle.runtimeSessionName, + identity: initializedIdentity, + mode: input.mode, + ...(Object.keys(effectiveRuntimeOptions).length > 0 + ? { runtimeOptions: effectiveRuntimeOptions } + : {}), + cwd: effectiveCwd, + state: "idle", + lastActivityAt: Date.now(), + }; + try { + const persisted = await this.writeSessionMeta({ + cfg: input.cfg, + sessionKey, + mutate: () => meta, + failOnError: true, + }); + if (!persisted?.acp) { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Could not persist ACP metadata for ${sessionKey}.`, + ); + } + } catch (error) { + await runtime + .close({ + handle, + reason: "init-meta-failed", + }) + .catch((closeError) => { + logVerbose( + `acp-manager: cleanup close failed after metadata write error for ${sessionKey}: ${String(closeError)}`, + ); + }); + throw error; + } + this.setCachedRuntimeState(sessionKey, { + runtime, + handle, + backend: handle.backend || backend.id, + agent, + mode: input.mode, + cwd: effectiveCwd, + }); + return { + runtime, + handle, + meta, + }; + }); + } + + async getSessionStatus(params: { + cfg: OpenClawConfig; + sessionKey: string; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const { + runtime, + handle: ensuredHandle, + meta: ensuredMeta, + } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey, + meta: resolution.meta, + }); + let handle = ensuredHandle; + let meta = ensuredMeta; + const capabilities = await this.resolveRuntimeCapabilities({ runtime, handle }); + let runtimeStatus: AcpRuntimeStatus | undefined; + if (runtime.getStatus) { + runtimeStatus = await withAcpRuntimeErrorBoundary({ + run: async () => await runtime.getStatus!({ handle }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not read ACP runtime status.", + }); + } + ({ handle, meta, runtimeStatus } = await this.reconcileRuntimeSessionIdentifiers({ + cfg: params.cfg, + sessionKey, + runtime, + handle, + meta, + runtimeStatus, + failOnStatusError: true, + })); + const identity = resolveSessionIdentityFromMeta(meta); + return { + sessionKey, + backend: handle.backend || meta.backend, + agent: meta.agent, + ...(identity ? { identity } : {}), + state: meta.state, + mode: meta.mode, + runtimeOptions: resolveRuntimeOptionsFromMeta(meta), + capabilities, + runtimeStatus, + lastActivityAt: meta.lastActivityAt, + lastError: meta.lastError, + }; + }); + } + + async setSessionRuntimeMode(params: { + cfg: OpenClawConfig; + sessionKey: string; + runtimeMode: string; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + const runtimeMode = validateRuntimeModeInput(params.runtimeMode); + + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const { runtime, handle, meta } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey, + meta: resolution.meta, + }); + const capabilities = await this.resolveRuntimeCapabilities({ runtime, handle }); + if (!capabilities.controls.includes("session/set_mode") || !runtime.setMode) { + throw createUnsupportedControlError({ + backend: handle.backend || meta.backend, + control: "session/set_mode", + }); + } + + await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.setMode!({ + handle, + mode: runtimeMode, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP runtime mode.", + }); + + const nextOptions = mergeRuntimeOptions({ + current: resolveRuntimeOptionsFromMeta(meta), + patch: { runtimeMode }, + }); + await this.persistRuntimeOptions({ + cfg: params.cfg, + sessionKey, + options: nextOptions, + }); + return nextOptions; + }); + } + + async setSessionConfigOption(params: { + cfg: OpenClawConfig; + sessionKey: string; + key: string; + value: string; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + const normalizedOption = validateRuntimeConfigOptionInput(params.key, params.value); + const key = normalizedOption.key; + const value = normalizedOption.value; + + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const { runtime, handle, meta } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey, + meta: resolution.meta, + }); + const inferredPatch = inferRuntimeOptionPatchFromConfigOption(key, value); + const capabilities = await this.resolveRuntimeCapabilities({ runtime, handle }); + if ( + !capabilities.controls.includes("session/set_config_option") || + !runtime.setConfigOption + ) { + throw createUnsupportedControlError({ + backend: handle.backend || meta.backend, + control: "session/set_config_option", + }); + } + + const advertisedKeys = new Set( + (capabilities.configOptionKeys ?? []) + .map((entry) => normalizeText(entry)) + .filter(Boolean) as string[], + ); + if (advertisedKeys.size > 0 && !advertisedKeys.has(key)) { + throw new AcpRuntimeError( + "ACP_BACKEND_UNSUPPORTED_CONTROL", + `ACP backend "${handle.backend || meta.backend}" does not accept config key "${key}".`, + ); + } + + await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.setConfigOption!({ + handle, + key, + value, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP runtime config option.", + }); + + const nextOptions = mergeRuntimeOptions({ + current: resolveRuntimeOptionsFromMeta(meta), + patch: inferredPatch, + }); + await this.persistRuntimeOptions({ + cfg: params.cfg, + sessionKey, + options: nextOptions, + }); + return nextOptions; + }); + } + + async updateSessionRuntimeOptions(params: { + cfg: OpenClawConfig; + sessionKey: string; + patch: Partial; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + const validatedPatch = validateRuntimeOptionPatch(params.patch); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const nextOptions = mergeRuntimeOptions({ + current: resolveRuntimeOptionsFromMeta(resolution.meta), + patch: validatedPatch, + }); + await this.persistRuntimeOptions({ + cfg: params.cfg, + sessionKey, + options: nextOptions, + }); + return nextOptions; + }); + } + + async resetSessionRuntimeOptions(params: { + cfg: OpenClawConfig; + sessionKey: string; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const { runtime, handle } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey, + meta: resolution.meta, + }); + await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.close({ + handle, + reason: "reset-runtime-options", + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not reset ACP runtime options.", + }); + this.clearCachedRuntimeState(sessionKey); + await this.persistRuntimeOptions({ + cfg: params.cfg, + sessionKey, + options: {}, + }); + return {}; + }); + } + + async runTurn(input: AcpRunTurnInput): Promise { + const sessionKey = normalizeSessionKey(input.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + await this.evictIdleRuntimeHandles({ cfg: input.cfg }); + await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: input.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + + const { + runtime, + handle: ensuredHandle, + meta: ensuredMeta, + } = await this.ensureRuntimeHandle({ + cfg: input.cfg, + sessionKey, + meta: resolution.meta, + }); + let handle = ensuredHandle; + const meta = ensuredMeta; + await this.applyRuntimeControls({ + sessionKey, + runtime, + handle, + meta, + }); + const turnStartedAt = Date.now(); + const actorKey = normalizeActorKey(sessionKey); + + await this.setSessionState({ + cfg: input.cfg, + sessionKey, + state: "running", + clearLastError: true, + }); + + const internalAbortController = new AbortController(); + const onCallerAbort = () => { + internalAbortController.abort(); + }; + if (input.signal?.aborted) { + internalAbortController.abort(); + } else if (input.signal) { + input.signal.addEventListener("abort", onCallerAbort, { once: true }); + } + + const activeTurn: ActiveTurnState = { + runtime, + handle, + abortController: internalAbortController, + }; + this.activeTurnBySession.set(actorKey, activeTurn); + + let streamError: AcpRuntimeError | null = null; + try { + const combinedSignal = + input.signal && typeof AbortSignal.any === "function" + ? AbortSignal.any([input.signal, internalAbortController.signal]) + : internalAbortController.signal; + for await (const event of runtime.runTurn({ + handle, + text: input.text, + mode: input.mode, + requestId: input.requestId, + signal: combinedSignal, + })) { + if (event.type === "error") { + streamError = new AcpRuntimeError( + normalizeAcpErrorCode(event.code), + event.message?.trim() || "ACP turn failed before completion.", + ); + } + if (input.onEvent) { + await input.onEvent(event); + } + } + if (streamError) { + throw streamError; + } + this.recordTurnCompletion({ + startedAt: turnStartedAt, + }); + await this.setSessionState({ + cfg: input.cfg, + sessionKey, + state: "idle", + clearLastError: true, + }); + } catch (error) { + const acpError = toAcpRuntimeError({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP turn failed before completion.", + }); + this.recordTurnCompletion({ + startedAt: turnStartedAt, + errorCode: acpError.code, + }); + await this.setSessionState({ + cfg: input.cfg, + sessionKey, + state: "error", + lastError: acpError.message, + }); + throw acpError; + } finally { + if (input.signal) { + input.signal.removeEventListener("abort", onCallerAbort); + } + if (this.activeTurnBySession.get(actorKey) === activeTurn) { + this.activeTurnBySession.delete(actorKey); + } + if (meta.mode !== "oneshot") { + ({ handle } = await this.reconcileRuntimeSessionIdentifiers({ + cfg: input.cfg, + sessionKey, + runtime, + handle, + meta, + failOnStatusError: false, + })); + } + if (meta.mode === "oneshot") { + try { + await runtime.close({ + handle, + reason: "oneshot-complete", + }); + } catch (error) { + logVerbose(`acp-manager: ACP oneshot close failed for ${sessionKey}: ${String(error)}`); + } finally { + this.clearCachedRuntimeState(sessionKey); + } + } + } + }); + } + + async cancelSession(params: { + cfg: OpenClawConfig; + sessionKey: string; + reason?: string; + }): Promise { + const sessionKey = normalizeSessionKey(params.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + await this.evictIdleRuntimeHandles({ cfg: params.cfg }); + const actorKey = normalizeActorKey(sessionKey); + const activeTurn = this.activeTurnBySession.get(actorKey); + if (activeTurn) { + activeTurn.abortController.abort(); + if (!activeTurn.cancelPromise) { + activeTurn.cancelPromise = activeTurn.runtime.cancel({ + handle: activeTurn.handle, + reason: params.reason, + }); + } + await withAcpRuntimeErrorBoundary({ + run: async () => await activeTurn.cancelPromise!, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP cancel failed before completion.", + }); + return; + } + + await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: params.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + if (resolution.kind === "stale") { + throw resolution.error; + } + const { runtime, handle } = await this.ensureRuntimeHandle({ + cfg: params.cfg, + sessionKey, + meta: resolution.meta, + }); + try { + await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.cancel({ + handle, + reason: params.reason, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP cancel failed before completion.", + }); + await this.setSessionState({ + cfg: params.cfg, + sessionKey, + state: "idle", + clearLastError: true, + }); + } catch (error) { + const acpError = toAcpRuntimeError({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP cancel failed before completion.", + }); + await this.setSessionState({ + cfg: params.cfg, + sessionKey, + state: "error", + lastError: acpError.message, + }); + throw acpError; + } + }); + } + + async closeSession(input: AcpCloseSessionInput): Promise { + const sessionKey = normalizeSessionKey(input.sessionKey); + if (!sessionKey) { + throw new AcpRuntimeError("ACP_SESSION_INIT_FAILED", "ACP session key is required."); + } + await this.evictIdleRuntimeHandles({ cfg: input.cfg }); + return await this.withSessionActor(sessionKey, async () => { + const resolution = this.resolveSession({ + cfg: input.cfg, + sessionKey, + }); + if (resolution.kind === "none") { + if (input.requireAcpSession ?? true) { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${sessionKey}`, + ); + } + return { + runtimeClosed: false, + metaCleared: false, + }; + } + if (resolution.kind === "stale") { + if (input.requireAcpSession ?? true) { + throw resolution.error; + } + return { + runtimeClosed: false, + metaCleared: false, + }; + } + + let runtimeClosed = false; + let runtimeNotice: string | undefined; + try { + const { runtime, handle } = await this.ensureRuntimeHandle({ + cfg: input.cfg, + sessionKey, + meta: resolution.meta, + }); + await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.close({ + handle, + reason: input.reason, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP close failed before completion.", + }); + runtimeClosed = true; + this.clearCachedRuntimeState(sessionKey); + } catch (error) { + const acpError = toAcpRuntimeError({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP close failed before completion.", + }); + if ( + input.allowBackendUnavailable && + (acpError.code === "ACP_BACKEND_MISSING" || acpError.code === "ACP_BACKEND_UNAVAILABLE") + ) { + // Treat unavailable backends as terminal for this cached handle so it + // cannot continue counting against maxConcurrentSessions. + this.clearCachedRuntimeState(sessionKey); + runtimeNotice = acpError.message; + } else { + throw acpError; + } + } + + let metaCleared = false; + if (input.clearMeta) { + await this.writeSessionMeta({ + cfg: input.cfg, + sessionKey, + mutate: (_current, entry) => { + if (!entry) { + return null; + } + return null; + }, + failOnError: true, + }); + metaCleared = true; + } + + return { + runtimeClosed, + runtimeNotice, + metaCleared, + }; + }); + } + + private async ensureRuntimeHandle(params: { + cfg: OpenClawConfig; + sessionKey: string; + meta: SessionAcpMeta; + }): Promise<{ runtime: AcpRuntime; handle: AcpRuntimeHandle; meta: SessionAcpMeta }> { + const agent = + params.meta.agent?.trim() || resolveAcpAgentFromSessionKey(params.sessionKey, "main"); + const mode = params.meta.mode; + const runtimeOptions = resolveRuntimeOptionsFromMeta(params.meta); + const cwd = runtimeOptions.cwd ?? normalizeText(params.meta.cwd); + const configuredBackend = (params.meta.backend || params.cfg.acp?.backend || "").trim(); + const cached = this.getCachedRuntimeState(params.sessionKey); + if (cached) { + const backendMatches = !configuredBackend || cached.backend === configuredBackend; + const agentMatches = cached.agent === agent; + const modeMatches = cached.mode === mode; + const cwdMatches = (cached.cwd ?? "") === (cwd ?? ""); + if (backendMatches && agentMatches && modeMatches && cwdMatches) { + return { + runtime: cached.runtime, + handle: cached.handle, + meta: params.meta, + }; + } + this.clearCachedRuntimeState(params.sessionKey); + } + + this.enforceConcurrentSessionLimit({ + cfg: params.cfg, + sessionKey: params.sessionKey, + }); + + const backend = this.deps.requireRuntimeBackend(configuredBackend || undefined); + const runtime = backend.runtime; + const ensured = await withAcpRuntimeErrorBoundary({ + run: async () => + await runtime.ensureSession({ + sessionKey: params.sessionKey, + agent, + mode, + cwd, + }), + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Could not initialize ACP session runtime.", + }); + + const previousMeta = params.meta; + const previousIdentity = resolveSessionIdentityFromMeta(previousMeta); + const now = Date.now(); + const effectiveCwd = normalizeText(ensured.cwd) ?? cwd; + const nextRuntimeOptions = normalizeRuntimeOptions({ + ...runtimeOptions, + ...(effectiveCwd ? { cwd: effectiveCwd } : {}), + }); + const nextIdentity = + mergeSessionIdentity({ + current: previousIdentity, + incoming: createIdentityFromEnsure({ + handle: ensured, + now, + }), + now, + }) ?? previousIdentity; + const nextHandleIdentifiers = resolveRuntimeHandleIdentifiersFromIdentity(nextIdentity); + const nextHandle: AcpRuntimeHandle = { + ...ensured, + ...(nextHandleIdentifiers.backendSessionId + ? { backendSessionId: nextHandleIdentifiers.backendSessionId } + : {}), + ...(nextHandleIdentifiers.agentSessionId + ? { agentSessionId: nextHandleIdentifiers.agentSessionId } + : {}), + }; + const nextMeta: SessionAcpMeta = { + backend: ensured.backend || backend.id, + agent, + runtimeSessionName: ensured.runtimeSessionName, + ...(nextIdentity ? { identity: nextIdentity } : {}), + mode: params.meta.mode, + ...(Object.keys(nextRuntimeOptions).length > 0 ? { runtimeOptions: nextRuntimeOptions } : {}), + ...(effectiveCwd ? { cwd: effectiveCwd } : {}), + state: previousMeta.state, + lastActivityAt: now, + ...(previousMeta.lastError ? { lastError: previousMeta.lastError } : {}), + }; + const shouldPersistMeta = + previousMeta.backend !== nextMeta.backend || + previousMeta.runtimeSessionName !== nextMeta.runtimeSessionName || + !identityEquals(previousIdentity, nextIdentity) || + previousMeta.agent !== nextMeta.agent || + previousMeta.cwd !== nextMeta.cwd || + !runtimeOptionsEqual(previousMeta.runtimeOptions, nextMeta.runtimeOptions) || + hasLegacyAcpIdentityProjection(previousMeta); + if (shouldPersistMeta) { + await this.writeSessionMeta({ + cfg: params.cfg, + sessionKey: params.sessionKey, + mutate: (_current, entry) => { + if (!entry) { + return null; + } + return nextMeta; + }, + }); + } + this.setCachedRuntimeState(params.sessionKey, { + runtime, + handle: nextHandle, + backend: ensured.backend || backend.id, + agent, + mode, + cwd: effectiveCwd, + appliedControlSignature: undefined, + }); + return { + runtime, + handle: nextHandle, + meta: nextMeta, + }; + } + + private async persistRuntimeOptions(params: { + cfg: OpenClawConfig; + sessionKey: string; + options: AcpSessionRuntimeOptions; + }): Promise { + const normalized = normalizeRuntimeOptions(params.options); + const hasOptions = Object.keys(normalized).length > 0; + await this.writeSessionMeta({ + cfg: params.cfg, + sessionKey: params.sessionKey, + mutate: (current, entry) => { + if (!entry) { + return null; + } + const base = current ?? entry.acp; + if (!base) { + return null; + } + return { + backend: base.backend, + agent: base.agent, + runtimeSessionName: base.runtimeSessionName, + ...(base.identity ? { identity: base.identity } : {}), + mode: base.mode, + runtimeOptions: hasOptions ? normalized : undefined, + cwd: normalized.cwd, + state: base.state, + lastActivityAt: Date.now(), + ...(base.lastError ? { lastError: base.lastError } : {}), + }; + }, + failOnError: true, + }); + + const cached = this.getCachedRuntimeState(params.sessionKey); + if (!cached) { + return; + } + if ((cached.cwd ?? "") !== (normalized.cwd ?? "")) { + this.clearCachedRuntimeState(params.sessionKey); + return; + } + // Persisting options does not guarantee this process pushed all controls to the runtime. + // Force the next turn to reconcile runtime controls from persisted metadata. + cached.appliedControlSignature = undefined; + } + + private enforceConcurrentSessionLimit(params: { cfg: OpenClawConfig; sessionKey: string }): void { + const configuredLimit = params.cfg.acp?.maxConcurrentSessions; + if (typeof configuredLimit !== "number" || !Number.isFinite(configuredLimit)) { + return; + } + const limit = Math.max(1, Math.floor(configuredLimit)); + const actorKey = normalizeActorKey(params.sessionKey); + if (this.runtimeCache.has(actorKey)) { + return; + } + const activeCount = this.runtimeCache.size(); + if (activeCount >= limit) { + throw new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `ACP max concurrent sessions reached (${activeCount}/${limit}).`, + ); + } + } + + private recordTurnCompletion(params: { startedAt: number; errorCode?: AcpRuntimeError["code"] }) { + const durationMs = Math.max(0, Date.now() - params.startedAt); + this.turnLatencyStats.totalMs += durationMs; + this.turnLatencyStats.maxMs = Math.max(this.turnLatencyStats.maxMs, durationMs); + if (params.errorCode) { + this.turnLatencyStats.failed += 1; + this.recordErrorCode(params.errorCode); + return; + } + this.turnLatencyStats.completed += 1; + } + + private recordErrorCode(code: string): void { + const normalized = normalizeAcpErrorCode(code); + this.errorCountsByCode.set(normalized, (this.errorCountsByCode.get(normalized) ?? 0) + 1); + } + + private async evictIdleRuntimeHandles(params: { cfg: OpenClawConfig }): Promise { + const idleTtlMs = resolveRuntimeIdleTtlMs(params.cfg); + if (idleTtlMs <= 0 || this.runtimeCache.size() === 0) { + return; + } + const now = Date.now(); + const candidates = this.runtimeCache.collectIdleCandidates({ + maxIdleMs: idleTtlMs, + now, + }); + if (candidates.length === 0) { + return; + } + + for (const candidate of candidates) { + await this.actorQueue.run(candidate.actorKey, async () => { + if (this.activeTurnBySession.has(candidate.actorKey)) { + return; + } + const lastTouchedAt = this.runtimeCache.getLastTouchedAt(candidate.actorKey); + if (lastTouchedAt == null || now - lastTouchedAt < idleTtlMs) { + return; + } + const cached = this.runtimeCache.peek(candidate.actorKey); + if (!cached) { + return; + } + this.runtimeCache.clear(candidate.actorKey); + this.evictedRuntimeCount += 1; + this.lastEvictedAt = Date.now(); + try { + await cached.runtime.close({ + handle: cached.handle, + reason: "idle-evicted", + }); + } catch (error) { + logVerbose( + `acp-manager: idle eviction close failed for ${candidate.state.handle.sessionKey}: ${String(error)}`, + ); + } + }); + } + } + + private async resolveRuntimeCapabilities(params: { + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + }): Promise { + return await resolveManagerRuntimeCapabilities(params); + } + + private async applyRuntimeControls(params: { + sessionKey: string; + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + }): Promise { + await applyManagerRuntimeControls({ + ...params, + getCachedRuntimeState: (sessionKey) => this.getCachedRuntimeState(sessionKey), + }); + } + + private async setSessionState(params: { + cfg: OpenClawConfig; + sessionKey: string; + state: SessionAcpMeta["state"]; + lastError?: string; + clearLastError?: boolean; + }): Promise { + await this.writeSessionMeta({ + cfg: params.cfg, + sessionKey: params.sessionKey, + mutate: (current, entry) => { + if (!entry) { + return null; + } + const base = current ?? entry.acp; + if (!base) { + return null; + } + const next: SessionAcpMeta = { + backend: base.backend, + agent: base.agent, + runtimeSessionName: base.runtimeSessionName, + ...(base.identity ? { identity: base.identity } : {}), + mode: base.mode, + ...(base.runtimeOptions ? { runtimeOptions: base.runtimeOptions } : {}), + ...(base.cwd ? { cwd: base.cwd } : {}), + state: params.state, + lastActivityAt: Date.now(), + ...(base.lastError ? { lastError: base.lastError } : {}), + }; + if (params.lastError?.trim()) { + next.lastError = params.lastError.trim(); + } else if (params.clearLastError) { + delete next.lastError; + } + return next; + }, + }); + } + + private async reconcileRuntimeSessionIdentifiers(params: { + cfg: OpenClawConfig; + sessionKey: string; + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + runtimeStatus?: AcpRuntimeStatus; + failOnStatusError: boolean; + }): Promise<{ + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + runtimeStatus?: AcpRuntimeStatus; + }> { + return await reconcileManagerRuntimeSessionIdentifiers({ + ...params, + setCachedHandle: (sessionKey, handle) => { + const cached = this.getCachedRuntimeState(sessionKey); + if (cached) { + cached.handle = handle; + } + }, + writeSessionMeta: async (writeParams) => await this.writeSessionMeta(writeParams), + }); + } + + private async writeSessionMeta(params: { + cfg: OpenClawConfig; + sessionKey: string; + mutate: ( + current: SessionAcpMeta | undefined, + entry: SessionEntry | undefined, + ) => SessionAcpMeta | null | undefined; + failOnError?: boolean; + }): Promise { + try { + return await this.deps.upsertSessionMeta({ + cfg: params.cfg, + sessionKey: params.sessionKey, + mutate: params.mutate, + }); + } catch (error) { + if (params.failOnError) { + throw error; + } + logVerbose( + `acp-manager: failed persisting ACP metadata for ${params.sessionKey}: ${String(error)}`, + ); + return null; + } + } + + private async withSessionActor(sessionKey: string, op: () => Promise): Promise { + const actorKey = normalizeActorKey(sessionKey); + return await this.actorQueue.run(actorKey, op); + } + + private getCachedRuntimeState(sessionKey: string): CachedRuntimeState | null { + return this.runtimeCache.get(normalizeActorKey(sessionKey)); + } + + private setCachedRuntimeState(sessionKey: string, state: CachedRuntimeState): void { + this.runtimeCache.set(normalizeActorKey(sessionKey), state); + } + + private clearCachedRuntimeState(sessionKey: string): void { + this.runtimeCache.clear(normalizeActorKey(sessionKey)); + } +} diff --git a/src/acp/control-plane/manager.identity-reconcile.ts b/src/acp/control-plane/manager.identity-reconcile.ts new file mode 100644 index 000000000000..d78a22ea04fe --- /dev/null +++ b/src/acp/control-plane/manager.identity-reconcile.ts @@ -0,0 +1,159 @@ +import type { OpenClawConfig } from "../../config/config.js"; +import { logVerbose } from "../../globals.js"; +import { withAcpRuntimeErrorBoundary } from "../runtime/errors.js"; +import { + createIdentityFromStatus, + identityEquals, + mergeSessionIdentity, + resolveRuntimeHandleIdentifiersFromIdentity, + resolveSessionIdentityFromMeta, +} from "../runtime/session-identity.js"; +import type { AcpRuntime, AcpRuntimeHandle, AcpRuntimeStatus } from "../runtime/types.js"; +import type { SessionAcpMeta, SessionEntry } from "./manager.types.js"; +import { hasLegacyAcpIdentityProjection } from "./manager.utils.js"; + +export async function reconcileManagerRuntimeSessionIdentifiers(params: { + cfg: OpenClawConfig; + sessionKey: string; + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + runtimeStatus?: AcpRuntimeStatus; + failOnStatusError: boolean; + setCachedHandle: (sessionKey: string, handle: AcpRuntimeHandle) => void; + writeSessionMeta: (params: { + cfg: OpenClawConfig; + sessionKey: string; + mutate: ( + current: SessionAcpMeta | undefined, + entry: SessionEntry | undefined, + ) => SessionAcpMeta | null | undefined; + failOnError?: boolean; + }) => Promise; +}): Promise<{ + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + runtimeStatus?: AcpRuntimeStatus; +}> { + let runtimeStatus = params.runtimeStatus; + if (!runtimeStatus && params.runtime.getStatus) { + try { + runtimeStatus = await withAcpRuntimeErrorBoundary({ + run: async () => + await params.runtime.getStatus!({ + handle: params.handle, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not read ACP runtime status.", + }); + } catch (error) { + if (params.failOnStatusError) { + throw error; + } + logVerbose( + `acp-manager: failed to refresh ACP runtime status for ${params.sessionKey}: ${String(error)}`, + ); + return { + handle: params.handle, + meta: params.meta, + runtimeStatus, + }; + } + } + + const now = Date.now(); + const currentIdentity = resolveSessionIdentityFromMeta(params.meta); + const nextIdentity = + mergeSessionIdentity({ + current: currentIdentity, + incoming: createIdentityFromStatus({ + status: runtimeStatus, + now, + }), + now, + }) ?? currentIdentity; + const handleIdentifiers = resolveRuntimeHandleIdentifiersFromIdentity(nextIdentity); + const handleChanged = + handleIdentifiers.backendSessionId !== params.handle.backendSessionId || + handleIdentifiers.agentSessionId !== params.handle.agentSessionId; + const nextHandle: AcpRuntimeHandle = handleChanged + ? { + ...params.handle, + ...(handleIdentifiers.backendSessionId + ? { backendSessionId: handleIdentifiers.backendSessionId } + : {}), + ...(handleIdentifiers.agentSessionId + ? { agentSessionId: handleIdentifiers.agentSessionId } + : {}), + } + : params.handle; + if (handleChanged) { + params.setCachedHandle(params.sessionKey, nextHandle); + } + + const metaChanged = + !identityEquals(currentIdentity, nextIdentity) || hasLegacyAcpIdentityProjection(params.meta); + if (!metaChanged) { + return { + handle: nextHandle, + meta: params.meta, + runtimeStatus, + }; + } + const nextMeta: SessionAcpMeta = { + backend: params.meta.backend, + agent: params.meta.agent, + runtimeSessionName: params.meta.runtimeSessionName, + ...(nextIdentity ? { identity: nextIdentity } : {}), + mode: params.meta.mode, + ...(params.meta.runtimeOptions ? { runtimeOptions: params.meta.runtimeOptions } : {}), + ...(params.meta.cwd ? { cwd: params.meta.cwd } : {}), + lastActivityAt: now, + state: params.meta.state, + ...(params.meta.lastError ? { lastError: params.meta.lastError } : {}), + }; + if (!identityEquals(currentIdentity, nextIdentity)) { + const currentAgentSessionId = currentIdentity?.agentSessionId ?? ""; + const nextAgentSessionId = nextIdentity?.agentSessionId ?? ""; + const currentAcpxSessionId = currentIdentity?.acpxSessionId ?? ""; + const nextAcpxSessionId = nextIdentity?.acpxSessionId ?? ""; + const currentAcpxRecordId = currentIdentity?.acpxRecordId ?? ""; + const nextAcpxRecordId = nextIdentity?.acpxRecordId ?? ""; + logVerbose( + `acp-manager: session identity updated for ${params.sessionKey} ` + + `(agentSessionId ${currentAgentSessionId} -> ${nextAgentSessionId}, ` + + `acpxSessionId ${currentAcpxSessionId} -> ${nextAcpxSessionId}, ` + + `acpxRecordId ${currentAcpxRecordId} -> ${nextAcpxRecordId})`, + ); + } + await params.writeSessionMeta({ + cfg: params.cfg, + sessionKey: params.sessionKey, + mutate: (current, entry) => { + if (!entry) { + return null; + } + const base = current ?? entry.acp; + if (!base) { + return null; + } + return { + backend: base.backend, + agent: base.agent, + runtimeSessionName: base.runtimeSessionName, + ...(nextIdentity ? { identity: nextIdentity } : {}), + mode: base.mode, + ...(base.runtimeOptions ? { runtimeOptions: base.runtimeOptions } : {}), + ...(base.cwd ? { cwd: base.cwd } : {}), + state: base.state, + lastActivityAt: now, + ...(base.lastError ? { lastError: base.lastError } : {}), + }; + }, + }); + return { + handle: nextHandle, + meta: nextMeta, + runtimeStatus, + }; +} diff --git a/src/acp/control-plane/manager.runtime-controls.ts b/src/acp/control-plane/manager.runtime-controls.ts new file mode 100644 index 000000000000..6c2b9e0a267a --- /dev/null +++ b/src/acp/control-plane/manager.runtime-controls.ts @@ -0,0 +1,118 @@ +import { AcpRuntimeError, withAcpRuntimeErrorBoundary } from "../runtime/errors.js"; +import type { AcpRuntime, AcpRuntimeCapabilities, AcpRuntimeHandle } from "../runtime/types.js"; +import type { SessionAcpMeta } from "./manager.types.js"; +import { createUnsupportedControlError } from "./manager.utils.js"; +import type { CachedRuntimeState } from "./runtime-cache.js"; +import { + buildRuntimeConfigOptionPairs, + buildRuntimeControlSignature, + normalizeText, + resolveRuntimeOptionsFromMeta, +} from "./runtime-options.js"; + +export async function resolveManagerRuntimeCapabilities(params: { + runtime: AcpRuntime; + handle: AcpRuntimeHandle; +}): Promise { + let reported: AcpRuntimeCapabilities | undefined; + if (params.runtime.getCapabilities) { + reported = await withAcpRuntimeErrorBoundary({ + run: async () => await params.runtime.getCapabilities!({ handle: params.handle }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not read ACP runtime capabilities.", + }); + } + const controls = new Set(reported?.controls ?? []); + if (params.runtime.setMode) { + controls.add("session/set_mode"); + } + if (params.runtime.setConfigOption) { + controls.add("session/set_config_option"); + } + if (params.runtime.getStatus) { + controls.add("session/status"); + } + const normalizedKeys = (reported?.configOptionKeys ?? []) + .map((entry) => normalizeText(entry)) + .filter(Boolean) as string[]; + return { + controls: [...controls].toSorted(), + ...(normalizedKeys.length > 0 ? { configOptionKeys: normalizedKeys } : {}), + }; +} + +export async function applyManagerRuntimeControls(params: { + sessionKey: string; + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + meta: SessionAcpMeta; + getCachedRuntimeState: (sessionKey: string) => CachedRuntimeState | null; +}): Promise { + const options = resolveRuntimeOptionsFromMeta(params.meta); + const signature = buildRuntimeControlSignature(options); + const cached = params.getCachedRuntimeState(params.sessionKey); + if (cached?.appliedControlSignature === signature) { + return; + } + + const capabilities = await resolveManagerRuntimeCapabilities({ + runtime: params.runtime, + handle: params.handle, + }); + const backend = params.handle.backend || params.meta.backend; + const runtimeMode = normalizeText(options.runtimeMode); + const configOptions = buildRuntimeConfigOptionPairs(options); + const advertisedKeys = new Set( + (capabilities.configOptionKeys ?? []) + .map((entry) => normalizeText(entry)) + .filter(Boolean) as string[], + ); + + await withAcpRuntimeErrorBoundary({ + run: async () => { + if (runtimeMode) { + if (!capabilities.controls.includes("session/set_mode") || !params.runtime.setMode) { + throw createUnsupportedControlError({ + backend, + control: "session/set_mode", + }); + } + await params.runtime.setMode({ + handle: params.handle, + mode: runtimeMode, + }); + } + + if (configOptions.length > 0) { + if ( + !capabilities.controls.includes("session/set_config_option") || + !params.runtime.setConfigOption + ) { + throw createUnsupportedControlError({ + backend, + control: "session/set_config_option", + }); + } + for (const [key, value] of configOptions) { + if (advertisedKeys.size > 0 && !advertisedKeys.has(key)) { + throw new AcpRuntimeError( + "ACP_BACKEND_UNSUPPORTED_CONTROL", + `ACP backend "${backend}" does not accept config key "${key}".`, + ); + } + await params.runtime.setConfigOption({ + handle: params.handle, + key, + value, + }); + } + } + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not apply ACP runtime options before turn execution.", + }); + + if (cached) { + cached.appliedControlSignature = signature; + } +} diff --git a/src/acp/control-plane/manager.test.ts b/src/acp/control-plane/manager.test.ts new file mode 100644 index 000000000000..ebdf356ca9fb --- /dev/null +++ b/src/acp/control-plane/manager.test.ts @@ -0,0 +1,1250 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { OpenClawConfig } from "../../config/config.js"; +import type { AcpSessionRuntimeOptions, SessionAcpMeta } from "../../config/sessions/types.js"; +import { AcpRuntimeError } from "../runtime/errors.js"; +import type { AcpRuntime, AcpRuntimeCapabilities } from "../runtime/types.js"; + +const hoisted = vi.hoisted(() => { + const listAcpSessionEntriesMock = vi.fn(); + const readAcpSessionEntryMock = vi.fn(); + const upsertAcpSessionMetaMock = vi.fn(); + const requireAcpRuntimeBackendMock = vi.fn(); + return { + listAcpSessionEntriesMock, + readAcpSessionEntryMock, + upsertAcpSessionMetaMock, + requireAcpRuntimeBackendMock, + }; +}); + +vi.mock("../runtime/session-meta.js", () => ({ + listAcpSessionEntries: (params: unknown) => hoisted.listAcpSessionEntriesMock(params), + readAcpSessionEntry: (params: unknown) => hoisted.readAcpSessionEntryMock(params), + upsertAcpSessionMeta: (params: unknown) => hoisted.upsertAcpSessionMetaMock(params), +})); + +vi.mock("../runtime/registry.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + requireAcpRuntimeBackend: (backendId?: string) => + hoisted.requireAcpRuntimeBackendMock(backendId), + }; +}); + +const { AcpSessionManager } = await import("./manager.js"); + +const baseCfg = { + acp: { + enabled: true, + backend: "acpx", + dispatch: { enabled: true }, + }, +} as const; + +function createRuntime(): { + runtime: AcpRuntime; + ensureSession: ReturnType; + runTurn: ReturnType; + cancel: ReturnType; + close: ReturnType; + getCapabilities: ReturnType; + getStatus: ReturnType; + setMode: ReturnType; + setConfigOption: ReturnType; +} { + const ensureSession = vi.fn( + async (input: { sessionKey: string; agent: string; mode: "persistent" | "oneshot" }) => ({ + sessionKey: input.sessionKey, + backend: "acpx", + runtimeSessionName: `${input.sessionKey}:${input.mode}:runtime`, + }), + ); + const runTurn = vi.fn(async function* () { + yield { type: "done" as const }; + }); + const cancel = vi.fn(async () => {}); + const close = vi.fn(async () => {}); + const getCapabilities = vi.fn( + async (): Promise => ({ + controls: ["session/set_mode", "session/set_config_option", "session/status"], + }), + ); + const getStatus = vi.fn(async () => ({ + summary: "status=alive", + details: { status: "alive" }, + })); + const setMode = vi.fn(async () => {}); + const setConfigOption = vi.fn(async () => {}); + return { + runtime: { + ensureSession, + runTurn, + getCapabilities, + getStatus, + setMode, + setConfigOption, + cancel, + close, + }, + ensureSession, + runTurn, + cancel, + close, + getCapabilities, + getStatus, + setMode, + setConfigOption, + }; +} + +function readySessionMeta() { + return { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent" as const, + state: "idle" as const, + lastActivityAt: Date.now(), + }; +} + +function extractStatesFromUpserts(): SessionAcpMeta["state"][] { + const states: SessionAcpMeta["state"][] = []; + for (const [firstArg] of hoisted.upsertAcpSessionMetaMock.mock.calls) { + const payload = firstArg as { + mutate: ( + current: SessionAcpMeta | undefined, + entry: { acp?: SessionAcpMeta } | undefined, + ) => SessionAcpMeta | null | undefined; + }; + const current = readySessionMeta(); + const next = payload.mutate(current, { acp: current }); + if (next?.state) { + states.push(next.state); + } + } + return states; +} + +function extractRuntimeOptionsFromUpserts(): Array { + const options: Array = []; + for (const [firstArg] of hoisted.upsertAcpSessionMetaMock.mock.calls) { + const payload = firstArg as { + mutate: ( + current: SessionAcpMeta | undefined, + entry: { acp?: SessionAcpMeta } | undefined, + ) => SessionAcpMeta | null | undefined; + }; + const current = readySessionMeta(); + const next = payload.mutate(current, { acp: current }); + if (next) { + options.push(next.runtimeOptions); + } + } + return options; +} + +describe("AcpSessionManager", () => { + beforeEach(() => { + hoisted.listAcpSessionEntriesMock.mockReset().mockResolvedValue([]); + hoisted.readAcpSessionEntryMock.mockReset(); + hoisted.upsertAcpSessionMetaMock.mockReset().mockResolvedValue(null); + hoisted.requireAcpRuntimeBackendMock.mockReset(); + }); + + it("marks ACP-shaped sessions without metadata as stale", () => { + hoisted.readAcpSessionEntryMock.mockReturnValue(null); + const manager = new AcpSessionManager(); + + const resolved = manager.resolveSession({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + }); + + expect(resolved.kind).toBe("stale"); + if (resolved.kind !== "stale") { + return; + } + expect(resolved.error.code).toBe("ACP_SESSION_INIT_FAILED"); + expect(resolved.error.message).toContain("ACP metadata is missing"); + }); + + it("serializes concurrent turns for the same ACP session", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + let inFlight = 0; + let maxInFlight = 0; + runtimeState.runTurn.mockImplementation(async function* (_input: { requestId: string }) { + inFlight += 1; + maxInFlight = Math.max(maxInFlight, inFlight); + try { + await new Promise((resolve) => setTimeout(resolve, 10)); + yield { type: "done" }; + } finally { + inFlight -= 1; + } + }); + + const manager = new AcpSessionManager(); + const first = manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "first", + mode: "prompt", + requestId: "r1", + }); + const second = manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "second", + mode: "prompt", + requestId: "r2", + }); + await Promise.all([first, second]); + + expect(maxInFlight).toBe(1); + expect(runtimeState.runTurn).toHaveBeenCalledTimes(2); + }); + + it("runs turns for different ACP sessions in parallel", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + + let inFlight = 0; + let maxInFlight = 0; + runtimeState.runTurn.mockImplementation(async function* () { + inFlight += 1; + maxInFlight = Math.max(maxInFlight, inFlight); + try { + await new Promise((resolve) => setTimeout(resolve, 15)); + yield { type: "done" as const }; + } finally { + inFlight -= 1; + } + }); + + const manager = new AcpSessionManager(); + await Promise.all([ + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }), + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }), + ]); + + expect(maxInFlight).toBe(2); + }); + + it("reuses runtime session handles for repeat turns in the same manager process", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "first", + mode: "prompt", + requestId: "r1", + }); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "second", + mode: "prompt", + requestId: "r2", + }); + + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(1); + expect(runtimeState.runTurn).toHaveBeenCalledTimes(2); + }); + + it("rehydrates runtime handles after a manager restart", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + const managerA = new AcpSessionManager(); + await managerA.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "before restart", + mode: "prompt", + requestId: "r1", + }); + const managerB = new AcpSessionManager(); + await managerB.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "after restart", + mode: "prompt", + requestId: "r2", + }); + + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(2); + }); + + it("enforces acp.maxConcurrentSessions when opening new runtime handles", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + const limitedCfg = { + acp: { + ...baseCfg.acp, + maxConcurrentSessions: 1, + }, + } as OpenClawConfig; + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }); + + await expect( + manager.runTurn({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }), + ).rejects.toMatchObject({ + code: "ACP_SESSION_INIT_FAILED", + message: expect.stringContaining("max concurrent sessions"), + }); + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(1); + }); + + it("enforces acp.maxConcurrentSessions during initializeSession", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.upsertAcpSessionMetaMock.mockResolvedValue({ + sessionKey: "agent:codex:acp:session-a", + storeSessionKey: "agent:codex:acp:session-a", + acp: readySessionMeta(), + }); + const limitedCfg = { + acp: { + ...baseCfg.acp, + maxConcurrentSessions: 1, + }, + } as OpenClawConfig; + + const manager = new AcpSessionManager(); + await manager.initializeSession({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-a", + agent: "codex", + mode: "persistent", + }); + + await expect( + manager.initializeSession({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-b", + agent: "codex", + mode: "persistent", + }), + ).rejects.toMatchObject({ + code: "ACP_SESSION_INIT_FAILED", + message: expect.stringContaining("max concurrent sessions"), + }); + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(1); + }); + + it("drops cached runtime handles when close tolerates backend-unavailable errors", async () => { + const runtimeState = createRuntime(); + runtimeState.close.mockRejectedValueOnce( + new AcpRuntimeError("ACP_BACKEND_UNAVAILABLE", "runtime temporarily unavailable"), + ); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + const limitedCfg = { + acp: { + ...baseCfg.acp, + maxConcurrentSessions: 1, + }, + } as OpenClawConfig; + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }); + + const closeResult = await manager.closeSession({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-a", + reason: "manual-close", + allowBackendUnavailable: true, + }); + expect(closeResult.runtimeClosed).toBe(false); + expect(closeResult.runtimeNotice).toContain("temporarily unavailable"); + + await expect( + manager.runTurn({ + cfg: limitedCfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }), + ).resolves.toBeUndefined(); + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(2); + }); + + it("evicts idle cached runtimes before enforcing max concurrent limits", async () => { + vi.useFakeTimers(); + try { + vi.setSystemTime(new Date("2026-02-23T00:00:00.000Z")); + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + const cfg = { + acp: { + ...baseCfg.acp, + maxConcurrentSessions: 1, + runtime: { + ttlMinutes: 0.01, + }, + }, + } as OpenClawConfig; + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }); + + vi.advanceTimersByTime(2_000); + await manager.runTurn({ + cfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }); + + expect(runtimeState.ensureSession).toHaveBeenCalledTimes(2); + expect(runtimeState.close).toHaveBeenCalledWith( + expect.objectContaining({ + reason: "idle-evicted", + handle: expect.objectContaining({ + sessionKey: "agent:codex:acp:session-a", + }), + }), + ); + } finally { + vi.useRealTimers(); + } + }); + + it("tracks ACP turn latency and error-code observability", async () => { + const runtimeState = createRuntime(); + runtimeState.runTurn.mockImplementation(async function* (input: { requestId: string }) { + if (input.requestId === "fail") { + throw new Error("runtime exploded"); + } + yield { type: "done" as const }; + }); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "ok", + mode: "prompt", + requestId: "ok", + }); + await expect( + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "boom", + mode: "prompt", + requestId: "fail", + }), + ).rejects.toMatchObject({ + code: "ACP_TURN_FAILED", + }); + + const snapshot = manager.getObservabilitySnapshot(baseCfg); + expect(snapshot.turns.completed).toBe(1); + expect(snapshot.turns.failed).toBe(1); + expect(snapshot.turns.active).toBe(0); + expect(snapshot.turns.queueDepth).toBe(0); + expect(snapshot.errorsByCode.ACP_TURN_FAILED).toBe(1); + }); + + it("rolls back ensured runtime sessions when metadata persistence fails", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.upsertAcpSessionMetaMock.mockRejectedValueOnce(new Error("disk full")); + + const manager = new AcpSessionManager(); + await expect( + manager.initializeSession({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + agent: "codex", + mode: "persistent", + }), + ).rejects.toThrow("disk full"); + expect(runtimeState.close).toHaveBeenCalledWith( + expect.objectContaining({ + reason: "init-meta-failed", + handle: expect.objectContaining({ + sessionKey: "agent:codex:acp:session-1", + }), + }), + ); + }); + + it("preempts an active turn on cancel and returns to idle state", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + let enteredRun = false; + runtimeState.runTurn.mockImplementation(async function* (input: { signal?: AbortSignal }) { + enteredRun = true; + await new Promise((resolve) => { + if (input.signal?.aborted) { + resolve(); + return; + } + input.signal?.addEventListener("abort", () => resolve(), { once: true }); + }); + yield { type: "done" as const, stopReason: "cancel" }; + }); + + const manager = new AcpSessionManager(); + const runPromise = manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "long task", + mode: "prompt", + requestId: "run-1", + }); + await vi.waitFor(() => { + expect(enteredRun).toBe(true); + }); + + await manager.cancelSession({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + reason: "manual-cancel", + }); + await runPromise; + + expect(runtimeState.cancel).toHaveBeenCalledTimes(1); + expect(runtimeState.cancel).toHaveBeenCalledWith( + expect.objectContaining({ + reason: "manual-cancel", + }), + ); + const states = extractStatesFromUpserts(); + expect(states).toContain("running"); + expect(states).toContain("idle"); + expect(states).not.toContain("error"); + }); + + it("cleans actor-tail bookkeeping after session turns complete", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = (paramsUnknown as { sessionKey?: string }).sessionKey ?? ""; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: { + ...readySessionMeta(), + runtimeSessionName: `runtime:${sessionKey}`, + }, + }; + }); + runtimeState.runTurn.mockImplementation(async function* () { + yield { type: "done" as const }; + }); + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }); + + const internals = manager as unknown as { + actorTailBySession: Map>; + }; + expect(internals.actorTailBySession.size).toBe(0); + }); + + it("surfaces backend failures raised after a done event", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + runtimeState.runTurn.mockImplementation(async function* () { + yield { type: "done" as const }; + throw new Error("acpx exited with code 1"); + }); + + const manager = new AcpSessionManager(); + await expect( + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "do work", + mode: "prompt", + requestId: "run-1", + }), + ).rejects.toMatchObject({ + code: "ACP_TURN_FAILED", + message: "acpx exited with code 1", + }); + + const states = extractStatesFromUpserts(); + expect(states).toContain("running"); + expect(states).toContain("error"); + expect(states.at(-1)).toBe("error"); + }); + + it("persists runtime mode changes through setSessionRuntimeMode", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + const manager = new AcpSessionManager(); + const options = await manager.setSessionRuntimeMode({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + runtimeMode: "plan", + }); + + expect(runtimeState.setMode).toHaveBeenCalledWith( + expect.objectContaining({ + mode: "plan", + }), + ); + expect(options.runtimeMode).toBe("plan"); + expect(extractRuntimeOptionsFromUpserts().some((entry) => entry?.runtimeMode === "plan")).toBe( + true, + ); + }); + + it("reapplies persisted controls on next turn after runtime option updates", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + + let currentMeta: SessionAcpMeta = { + ...readySessionMeta(), + runtimeOptions: { + runtimeMode: "plan", + }, + }; + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = + (paramsUnknown as { sessionKey?: string }).sessionKey ?? "agent:codex:acp:session-1"; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: currentMeta, + }; + }); + hoisted.upsertAcpSessionMetaMock.mockImplementation(async (paramsUnknown: unknown) => { + const params = paramsUnknown as { + mutate: ( + current: SessionAcpMeta | undefined, + entry: { acp?: SessionAcpMeta } | undefined, + ) => SessionAcpMeta | null | undefined; + }; + const next = params.mutate(currentMeta, { acp: currentMeta }); + if (next) { + currentMeta = next; + } + return { + sessionId: "session-1", + updatedAt: Date.now(), + acp: currentMeta, + }; + }); + + const manager = new AcpSessionManager(); + await manager.setSessionConfigOption({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + key: "model", + value: "openai-codex/gpt-5.3-codex", + }); + expect(runtimeState.setMode).not.toHaveBeenCalled(); + + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "do work", + mode: "prompt", + requestId: "run-1", + }); + + expect(runtimeState.setMode).toHaveBeenCalledWith( + expect.objectContaining({ + mode: "plan", + }), + ); + }); + + it("reconciles persisted ACP session identifiers from runtime status after a turn", async () => { + const runtimeState = createRuntime(); + runtimeState.ensureSession.mockResolvedValue({ + sessionKey: "agent:codex:acp:session-1", + backend: "acpx", + runtimeSessionName: "runtime-1", + backendSessionId: "acpx-stale", + agentSessionId: "agent-stale", + }); + runtimeState.getStatus.mockResolvedValue({ + summary: "status=alive", + backendSessionId: "acpx-fresh", + agentSessionId: "agent-fresh", + details: { status: "alive" }, + }); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + + let currentMeta: SessionAcpMeta = { + ...readySessionMeta(), + identity: { + state: "resolved", + source: "status", + acpxSessionId: "acpx-stale", + agentSessionId: "agent-stale", + lastUpdatedAt: Date.now(), + }, + }; + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const sessionKey = + (paramsUnknown as { sessionKey?: string }).sessionKey ?? "agent:codex:acp:session-1"; + return { + sessionKey, + storeSessionKey: sessionKey, + acp: currentMeta, + }; + }); + hoisted.upsertAcpSessionMetaMock.mockImplementation(async (paramsUnknown: unknown) => { + const params = paramsUnknown as { + mutate: ( + current: SessionAcpMeta | undefined, + entry: { acp?: SessionAcpMeta } | undefined, + ) => SessionAcpMeta | null | undefined; + }; + const next = params.mutate(currentMeta, { acp: currentMeta }); + if (next) { + currentMeta = next; + } + return { + sessionId: "session-1", + updatedAt: Date.now(), + acp: currentMeta, + }; + }); + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "do work", + mode: "prompt", + requestId: "run-1", + }); + + expect(runtimeState.getStatus).toHaveBeenCalledTimes(1); + expect(currentMeta.identity?.acpxSessionId).toBe("acpx-fresh"); + expect(currentMeta.identity?.agentSessionId).toBe("agent-fresh"); + }); + + it("reconciles pending ACP identities during startup scan", async () => { + const runtimeState = createRuntime(); + runtimeState.getStatus.mockResolvedValue({ + summary: "status=alive", + acpxRecordId: "acpx-record-1", + backendSessionId: "acpx-session-1", + agentSessionId: "agent-session-1", + details: { status: "alive" }, + }); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + + let currentMeta: SessionAcpMeta = { + ...readySessionMeta(), + identity: { + state: "pending", + source: "ensure", + acpxSessionId: "acpx-stale", + lastUpdatedAt: Date.now(), + }, + }; + const sessionKey = "agent:codex:acp:session-1"; + hoisted.listAcpSessionEntriesMock.mockResolvedValue([ + { + cfg: baseCfg, + storePath: "/tmp/sessions-acp.json", + sessionKey, + storeSessionKey: sessionKey, + entry: { + sessionId: "session-1", + updatedAt: Date.now(), + acp: currentMeta, + }, + acp: currentMeta, + }, + ]); + hoisted.readAcpSessionEntryMock.mockImplementation((paramsUnknown: unknown) => { + const key = (paramsUnknown as { sessionKey?: string }).sessionKey ?? sessionKey; + return { + sessionKey: key, + storeSessionKey: key, + acp: currentMeta, + }; + }); + hoisted.upsertAcpSessionMetaMock.mockImplementation(async (paramsUnknown: unknown) => { + const params = paramsUnknown as { + mutate: ( + current: SessionAcpMeta | undefined, + entry: { acp?: SessionAcpMeta } | undefined, + ) => SessionAcpMeta | null | undefined; + }; + const next = params.mutate(currentMeta, { acp: currentMeta }); + if (next) { + currentMeta = next; + } + return { + sessionId: "session-1", + updatedAt: Date.now(), + acp: currentMeta, + }; + }); + + const manager = new AcpSessionManager(); + const result = await manager.reconcilePendingSessionIdentities({ cfg: baseCfg }); + + expect(result).toEqual({ checked: 1, resolved: 1, failed: 0 }); + expect(currentMeta.identity?.state).toBe("resolved"); + expect(currentMeta.identity?.acpxRecordId).toBe("acpx-record-1"); + expect(currentMeta.identity?.acpxSessionId).toBe("acpx-session-1"); + expect(currentMeta.identity?.agentSessionId).toBe("agent-session-1"); + }); + + it("skips startup identity reconciliation for already resolved sessions", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + const sessionKey = "agent:codex:acp:session-1"; + const resolvedMeta: SessionAcpMeta = { + ...readySessionMeta(), + identity: { + state: "resolved", + source: "status", + acpxSessionId: "acpx-sid-1", + agentSessionId: "agent-sid-1", + lastUpdatedAt: Date.now(), + }, + }; + hoisted.listAcpSessionEntriesMock.mockResolvedValue([ + { + cfg: baseCfg, + storePath: "/tmp/sessions-acp.json", + sessionKey, + storeSessionKey: sessionKey, + entry: { + sessionId: "session-1", + updatedAt: Date.now(), + acp: resolvedMeta, + }, + acp: resolvedMeta, + }, + ]); + + const manager = new AcpSessionManager(); + const result = await manager.reconcilePendingSessionIdentities({ cfg: baseCfg }); + + expect(result).toEqual({ checked: 0, resolved: 0, failed: 0 }); + expect(runtimeState.getStatus).not.toHaveBeenCalled(); + expect(runtimeState.ensureSession).not.toHaveBeenCalled(); + }); + + it("preserves existing ACP session identifiers when ensure returns none", async () => { + const runtimeState = createRuntime(); + runtimeState.ensureSession.mockResolvedValue({ + sessionKey: "agent:codex:acp:session-1", + backend: "acpx", + runtimeSessionName: "runtime-2", + }); + runtimeState.getStatus.mockResolvedValue({ + summary: "status=alive", + details: { status: "alive" }, + }); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: { + ...readySessionMeta(), + identity: { + state: "resolved", + source: "status", + acpxSessionId: "acpx-stable", + agentSessionId: "agent-stable", + lastUpdatedAt: Date.now(), + }, + }, + }); + + const manager = new AcpSessionManager(); + const status = await manager.getSessionStatus({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + }); + + expect(status.identity?.acpxSessionId).toBe("acpx-stable"); + expect(status.identity?.agentSessionId).toBe("agent-stable"); + }); + + it("applies persisted runtime options before running turns", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: { + ...readySessionMeta(), + runtimeOptions: { + runtimeMode: "plan", + model: "openai-codex/gpt-5.3-codex", + permissionProfile: "strict", + timeoutSeconds: 120, + }, + }, + }); + + const manager = new AcpSessionManager(); + await manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + text: "do work", + mode: "prompt", + requestId: "run-1", + }); + + expect(runtimeState.setMode).toHaveBeenCalledWith( + expect.objectContaining({ + mode: "plan", + }), + ); + expect(runtimeState.setConfigOption).toHaveBeenCalledWith( + expect.objectContaining({ + key: "model", + value: "openai-codex/gpt-5.3-codex", + }), + ); + expect(runtimeState.setConfigOption).toHaveBeenCalledWith( + expect.objectContaining({ + key: "approval_policy", + value: "strict", + }), + ); + expect(runtimeState.setConfigOption).toHaveBeenCalledWith( + expect.objectContaining({ + key: "timeout", + value: "120", + }), + ); + }); + + it("returns unsupported-control error when backend does not support set_config_option", async () => { + const runtimeState = createRuntime(); + const unsupportedRuntime: AcpRuntime = { + ensureSession: runtimeState.ensureSession as AcpRuntime["ensureSession"], + runTurn: runtimeState.runTurn as AcpRuntime["runTurn"], + getCapabilities: vi.fn(async () => ({ controls: [] })), + cancel: runtimeState.cancel as AcpRuntime["cancel"], + close: runtimeState.close as AcpRuntime["close"], + }; + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: unsupportedRuntime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + const manager = new AcpSessionManager(); + await expect( + manager.setSessionConfigOption({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + key: "model", + value: "gpt-5.3-codex", + }), + ).rejects.toMatchObject({ + code: "ACP_BACKEND_UNSUPPORTED_CONTROL", + }); + }); + + it("rejects invalid runtime option values before backend controls run", async () => { + const runtimeState = createRuntime(); + hoisted.requireAcpRuntimeBackendMock.mockReturnValue({ + id: "acpx", + runtime: runtimeState.runtime, + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + + const manager = new AcpSessionManager(); + await expect( + manager.setSessionConfigOption({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + key: "timeout", + value: "not-a-number", + }), + ).rejects.toMatchObject({ + code: "ACP_INVALID_RUNTIME_OPTION", + }); + expect(runtimeState.setConfigOption).not.toHaveBeenCalled(); + + await expect( + manager.updateSessionRuntimeOptions({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + patch: { cwd: "relative/path" }, + }), + ).rejects.toMatchObject({ + code: "ACP_INVALID_RUNTIME_OPTION", + }); + }); + + it("can close and clear metadata when backend is unavailable", async () => { + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + hoisted.requireAcpRuntimeBackendMock.mockImplementation(() => { + throw new AcpRuntimeError( + "ACP_BACKEND_MISSING", + "ACP runtime backend is not configured. Install and enable the acpx runtime plugin.", + ); + }); + + const manager = new AcpSessionManager(); + const result = await manager.closeSession({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + reason: "manual-close", + allowBackendUnavailable: true, + clearMeta: true, + }); + + expect(result.runtimeClosed).toBe(false); + expect(result.runtimeNotice).toContain("not configured"); + expect(result.metaCleared).toBe(true); + expect(hoisted.upsertAcpSessionMetaMock).toHaveBeenCalled(); + }); + + it("surfaces metadata clear errors during closeSession", async () => { + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:session-1", + storeSessionKey: "agent:codex:acp:session-1", + acp: readySessionMeta(), + }); + hoisted.requireAcpRuntimeBackendMock.mockImplementation(() => { + throw new AcpRuntimeError( + "ACP_BACKEND_MISSING", + "ACP runtime backend is not configured. Install and enable the acpx runtime plugin.", + ); + }); + hoisted.upsertAcpSessionMetaMock.mockRejectedValueOnce(new Error("disk locked")); + + const manager = new AcpSessionManager(); + await expect( + manager.closeSession({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-1", + reason: "manual-close", + allowBackendUnavailable: true, + clearMeta: true, + }), + ).rejects.toThrow("disk locked"); + }); +}); diff --git a/src/acp/control-plane/manager.ts b/src/acp/control-plane/manager.ts new file mode 100644 index 000000000000..e15bf1ec9b77 --- /dev/null +++ b/src/acp/control-plane/manager.ts @@ -0,0 +1,29 @@ +import { AcpSessionManager } from "./manager.core.js"; + +export { AcpSessionManager } from "./manager.core.js"; +export type { + AcpCloseSessionInput, + AcpCloseSessionResult, + AcpInitializeSessionInput, + AcpManagerObservabilitySnapshot, + AcpRunTurnInput, + AcpSessionResolution, + AcpSessionRuntimeOptions, + AcpSessionStatus, + AcpStartupIdentityReconcileResult, +} from "./manager.types.js"; + +let ACP_SESSION_MANAGER_SINGLETON: AcpSessionManager | null = null; + +export function getAcpSessionManager(): AcpSessionManager { + if (!ACP_SESSION_MANAGER_SINGLETON) { + ACP_SESSION_MANAGER_SINGLETON = new AcpSessionManager(); + } + return ACP_SESSION_MANAGER_SINGLETON; +} + +export const __testing = { + resetAcpSessionManagerForTests() { + ACP_SESSION_MANAGER_SINGLETON = null; + }, +}; diff --git a/src/acp/control-plane/manager.types.ts b/src/acp/control-plane/manager.types.ts new file mode 100644 index 000000000000..7337e8063f9c --- /dev/null +++ b/src/acp/control-plane/manager.types.ts @@ -0,0 +1,141 @@ +import type { OpenClawConfig } from "../../config/config.js"; +import type { + SessionAcpIdentity, + AcpSessionRuntimeOptions, + SessionAcpMeta, + SessionEntry, +} from "../../config/sessions/types.js"; +import type { AcpRuntimeError } from "../runtime/errors.js"; +import { requireAcpRuntimeBackend } from "../runtime/registry.js"; +import { + listAcpSessionEntries, + readAcpSessionEntry, + upsertAcpSessionMeta, +} from "../runtime/session-meta.js"; +import type { + AcpRuntime, + AcpRuntimeCapabilities, + AcpRuntimeEvent, + AcpRuntimeHandle, + AcpRuntimePromptMode, + AcpRuntimeSessionMode, + AcpRuntimeStatus, +} from "../runtime/types.js"; + +export type AcpSessionResolution = + | { + kind: "none"; + sessionKey: string; + } + | { + kind: "stale"; + sessionKey: string; + error: AcpRuntimeError; + } + | { + kind: "ready"; + sessionKey: string; + meta: SessionAcpMeta; + }; + +export type AcpInitializeSessionInput = { + cfg: OpenClawConfig; + sessionKey: string; + agent: string; + mode: AcpRuntimeSessionMode; + cwd?: string; + backendId?: string; +}; + +export type AcpRunTurnInput = { + cfg: OpenClawConfig; + sessionKey: string; + text: string; + mode: AcpRuntimePromptMode; + requestId: string; + signal?: AbortSignal; + onEvent?: (event: AcpRuntimeEvent) => Promise | void; +}; + +export type AcpCloseSessionInput = { + cfg: OpenClawConfig; + sessionKey: string; + reason: string; + clearMeta?: boolean; + allowBackendUnavailable?: boolean; + requireAcpSession?: boolean; +}; + +export type AcpCloseSessionResult = { + runtimeClosed: boolean; + runtimeNotice?: string; + metaCleared: boolean; +}; + +export type AcpSessionStatus = { + sessionKey: string; + backend: string; + agent: string; + identity?: SessionAcpIdentity; + state: SessionAcpMeta["state"]; + mode: AcpRuntimeSessionMode; + runtimeOptions: AcpSessionRuntimeOptions; + capabilities: AcpRuntimeCapabilities; + runtimeStatus?: AcpRuntimeStatus; + lastActivityAt: number; + lastError?: string; +}; + +export type AcpManagerObservabilitySnapshot = { + runtimeCache: { + activeSessions: number; + idleTtlMs: number; + evictedTotal: number; + lastEvictedAt?: number; + }; + turns: { + active: number; + queueDepth: number; + completed: number; + failed: number; + averageLatencyMs: number; + maxLatencyMs: number; + }; + errorsByCode: Record; +}; + +export type AcpStartupIdentityReconcileResult = { + checked: number; + resolved: number; + failed: number; +}; + +export type ActiveTurnState = { + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + abortController: AbortController; + cancelPromise?: Promise; +}; + +export type TurnLatencyStats = { + completed: number; + failed: number; + totalMs: number; + maxMs: number; +}; + +export type AcpSessionManagerDeps = { + listAcpSessions: typeof listAcpSessionEntries; + readSessionEntry: typeof readAcpSessionEntry; + upsertSessionMeta: typeof upsertAcpSessionMeta; + requireRuntimeBackend: typeof requireAcpRuntimeBackend; +}; + +export const DEFAULT_DEPS: AcpSessionManagerDeps = { + listAcpSessions: listAcpSessionEntries, + readSessionEntry: readAcpSessionEntry, + upsertSessionMeta: upsertAcpSessionMeta, + requireRuntimeBackend: requireAcpRuntimeBackend, +}; + +export type { AcpSessionRuntimeOptions, SessionAcpMeta, SessionEntry }; diff --git a/src/acp/control-plane/manager.utils.ts b/src/acp/control-plane/manager.utils.ts new file mode 100644 index 000000000000..3b6b2dacc45d --- /dev/null +++ b/src/acp/control-plane/manager.utils.ts @@ -0,0 +1,64 @@ +import type { OpenClawConfig } from "../../config/config.js"; +import type { SessionAcpMeta } from "../../config/sessions/types.js"; +import { normalizeAgentId, parseAgentSessionKey } from "../../routing/session-key.js"; +import { ACP_ERROR_CODES, AcpRuntimeError } from "../runtime/errors.js"; + +export function resolveAcpAgentFromSessionKey(sessionKey: string, fallback = "main"): string { + const parsed = parseAgentSessionKey(sessionKey); + return normalizeAgentId(parsed?.agentId ?? fallback); +} + +export function resolveMissingMetaError(sessionKey: string): AcpRuntimeError { + return new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `ACP metadata is missing for ${sessionKey}. Recreate this ACP session with /acp spawn and rebind the thread.`, + ); +} + +export function normalizeSessionKey(sessionKey: string): string { + return sessionKey.trim(); +} + +export function normalizeActorKey(sessionKey: string): string { + return sessionKey.trim().toLowerCase(); +} + +export function normalizeAcpErrorCode(code: string | undefined): AcpRuntimeError["code"] { + if (!code) { + return "ACP_TURN_FAILED"; + } + const normalized = code.trim().toUpperCase(); + for (const allowed of ACP_ERROR_CODES) { + if (allowed === normalized) { + return allowed; + } + } + return "ACP_TURN_FAILED"; +} + +export function createUnsupportedControlError(params: { + backend: string; + control: string; +}): AcpRuntimeError { + return new AcpRuntimeError( + "ACP_BACKEND_UNSUPPORTED_CONTROL", + `ACP backend "${params.backend}" does not support ${params.control}.`, + ); +} + +export function resolveRuntimeIdleTtlMs(cfg: OpenClawConfig): number { + const ttlMinutes = cfg.acp?.runtime?.ttlMinutes; + if (typeof ttlMinutes !== "number" || !Number.isFinite(ttlMinutes) || ttlMinutes <= 0) { + return 0; + } + return Math.round(ttlMinutes * 60 * 1000); +} + +export function hasLegacyAcpIdentityProjection(meta: SessionAcpMeta): boolean { + const raw = meta as Record; + return ( + Object.hasOwn(raw, "backendSessionId") || + Object.hasOwn(raw, "agentSessionId") || + Object.hasOwn(raw, "sessionIdsProvisional") + ); +} diff --git a/src/acp/control-plane/runtime-cache.test.ts b/src/acp/control-plane/runtime-cache.test.ts new file mode 100644 index 000000000000..ea0aa2f7124b --- /dev/null +++ b/src/acp/control-plane/runtime-cache.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it, vi } from "vitest"; +import type { AcpRuntime } from "../runtime/types.js"; +import type { AcpRuntimeHandle } from "../runtime/types.js"; +import type { CachedRuntimeState } from "./runtime-cache.js"; +import { RuntimeCache } from "./runtime-cache.js"; + +function mockState(sessionKey: string): CachedRuntimeState { + const runtime = { + ensureSession: vi.fn(async () => ({ + sessionKey, + backend: "acpx", + runtimeSessionName: `runtime:${sessionKey}`, + })), + runTurn: vi.fn(async function* () { + yield { type: "done" as const }; + }), + cancel: vi.fn(async () => {}), + close: vi.fn(async () => {}), + } as unknown as AcpRuntime; + return { + runtime, + handle: { + sessionKey, + backend: "acpx", + runtimeSessionName: `runtime:${sessionKey}`, + } as AcpRuntimeHandle, + backend: "acpx", + agent: "codex", + mode: "persistent", + }; +} + +describe("RuntimeCache", () => { + it("tracks idle candidates with touch-aware lookups", () => { + vi.useFakeTimers(); + try { + const cache = new RuntimeCache(); + const actor = "agent:codex:acp:s1"; + cache.set(actor, mockState(actor), { now: 1_000 }); + + expect(cache.collectIdleCandidates({ maxIdleMs: 1_000, now: 1_999 })).toHaveLength(0); + expect(cache.collectIdleCandidates({ maxIdleMs: 1_000, now: 2_000 })).toHaveLength(1); + + cache.get(actor, { now: 2_500 }); + expect(cache.collectIdleCandidates({ maxIdleMs: 1_000, now: 3_200 })).toHaveLength(0); + expect(cache.collectIdleCandidates({ maxIdleMs: 1_000, now: 3_500 })).toHaveLength(1); + } finally { + vi.useRealTimers(); + } + }); + + it("returns snapshot entries with idle durations", () => { + const cache = new RuntimeCache(); + cache.set("a", mockState("a"), { now: 10 }); + cache.set("b", mockState("b"), { now: 100 }); + + const snapshot = cache.snapshot({ now: 1_100 }); + const byActor = new Map(snapshot.map((entry) => [entry.actorKey, entry])); + expect(byActor.get("a")?.idleMs).toBe(1_090); + expect(byActor.get("b")?.idleMs).toBe(1_000); + }); +}); diff --git a/src/acp/control-plane/runtime-cache.ts b/src/acp/control-plane/runtime-cache.ts new file mode 100644 index 000000000000..ca00cc1331bb --- /dev/null +++ b/src/acp/control-plane/runtime-cache.ts @@ -0,0 +1,99 @@ +import type { AcpRuntime, AcpRuntimeHandle, AcpRuntimeSessionMode } from "../runtime/types.js"; + +export type CachedRuntimeState = { + runtime: AcpRuntime; + handle: AcpRuntimeHandle; + backend: string; + agent: string; + mode: AcpRuntimeSessionMode; + cwd?: string; + appliedControlSignature?: string; +}; + +type RuntimeCacheEntry = { + state: CachedRuntimeState; + lastTouchedAt: number; +}; + +export type CachedRuntimeSnapshot = { + actorKey: string; + state: CachedRuntimeState; + lastTouchedAt: number; + idleMs: number; +}; + +export class RuntimeCache { + private readonly cache = new Map(); + + size(): number { + return this.cache.size; + } + + has(actorKey: string): boolean { + return this.cache.has(actorKey); + } + + get( + actorKey: string, + params: { + touch?: boolean; + now?: number; + } = {}, + ): CachedRuntimeState | null { + const entry = this.cache.get(actorKey); + if (!entry) { + return null; + } + if (params.touch !== false) { + entry.lastTouchedAt = params.now ?? Date.now(); + } + return entry.state; + } + + peek(actorKey: string): CachedRuntimeState | null { + return this.get(actorKey, { touch: false }); + } + + getLastTouchedAt(actorKey: string): number | null { + return this.cache.get(actorKey)?.lastTouchedAt ?? null; + } + + set( + actorKey: string, + state: CachedRuntimeState, + params: { + now?: number; + } = {}, + ): void { + this.cache.set(actorKey, { + state, + lastTouchedAt: params.now ?? Date.now(), + }); + } + + clear(actorKey: string): void { + this.cache.delete(actorKey); + } + + snapshot(params: { now?: number } = {}): CachedRuntimeSnapshot[] { + const now = params.now ?? Date.now(); + const entries: CachedRuntimeSnapshot[] = []; + for (const [actorKey, entry] of this.cache.entries()) { + entries.push({ + actorKey, + state: entry.state, + lastTouchedAt: entry.lastTouchedAt, + idleMs: Math.max(0, now - entry.lastTouchedAt), + }); + } + return entries; + } + + collectIdleCandidates(params: { maxIdleMs: number; now?: number }): CachedRuntimeSnapshot[] { + if (!Number.isFinite(params.maxIdleMs) || params.maxIdleMs <= 0) { + return []; + } + const now = params.now ?? Date.now(); + return this.snapshot({ now }).filter((entry) => entry.idleMs >= params.maxIdleMs); + } +} diff --git a/src/acp/control-plane/runtime-options.ts b/src/acp/control-plane/runtime-options.ts new file mode 100644 index 000000000000..5f3b77bf1c8e --- /dev/null +++ b/src/acp/control-plane/runtime-options.ts @@ -0,0 +1,349 @@ +import { isAbsolute } from "node:path"; +import type { AcpSessionRuntimeOptions, SessionAcpMeta } from "../../config/sessions/types.js"; +import { AcpRuntimeError } from "../runtime/errors.js"; + +const MAX_RUNTIME_MODE_LENGTH = 64; +const MAX_MODEL_LENGTH = 200; +const MAX_PERMISSION_PROFILE_LENGTH = 80; +const MAX_CWD_LENGTH = 4096; +const MIN_TIMEOUT_SECONDS = 1; +const MAX_TIMEOUT_SECONDS = 24 * 60 * 60; +const MAX_BACKEND_OPTION_KEY_LENGTH = 64; +const MAX_BACKEND_OPTION_VALUE_LENGTH = 512; +const MAX_BACKEND_EXTRAS = 32; + +const SAFE_OPTION_KEY_RE = /^[a-z0-9][a-z0-9._:-]*$/i; + +function failInvalidOption(message: string): never { + throw new AcpRuntimeError("ACP_INVALID_RUNTIME_OPTION", message); +} + +function validateNoControlChars(value: string, field: string): string { + for (let i = 0; i < value.length; i += 1) { + const code = value.charCodeAt(i); + if (code < 32 || code === 127) { + failInvalidOption(`${field} must not include control characters.`); + } + } + return value; +} + +function validateBoundedText(params: { value: unknown; field: string; maxLength: number }): string { + const normalized = normalizeText(params.value); + if (!normalized) { + failInvalidOption(`${params.field} must not be empty.`); + } + if (normalized.length > params.maxLength) { + failInvalidOption(`${params.field} must be at most ${params.maxLength} characters.`); + } + return validateNoControlChars(normalized, params.field); +} + +function validateBackendOptionKey(rawKey: unknown): string { + const key = validateBoundedText({ + value: rawKey, + field: "ACP config key", + maxLength: MAX_BACKEND_OPTION_KEY_LENGTH, + }); + if (!SAFE_OPTION_KEY_RE.test(key)) { + failInvalidOption( + "ACP config key must use letters, numbers, dots, colons, underscores, or dashes.", + ); + } + return key; +} + +function validateBackendOptionValue(rawValue: unknown): string { + return validateBoundedText({ + value: rawValue, + field: "ACP config value", + maxLength: MAX_BACKEND_OPTION_VALUE_LENGTH, + }); +} + +export function validateRuntimeModeInput(rawMode: unknown): string { + return validateBoundedText({ + value: rawMode, + field: "Runtime mode", + maxLength: MAX_RUNTIME_MODE_LENGTH, + }); +} + +export function validateRuntimeModelInput(rawModel: unknown): string { + return validateBoundedText({ + value: rawModel, + field: "Model id", + maxLength: MAX_MODEL_LENGTH, + }); +} + +export function validateRuntimePermissionProfileInput(rawProfile: unknown): string { + return validateBoundedText({ + value: rawProfile, + field: "Permission profile", + maxLength: MAX_PERMISSION_PROFILE_LENGTH, + }); +} + +export function validateRuntimeCwdInput(rawCwd: unknown): string { + const cwd = validateBoundedText({ + value: rawCwd, + field: "Working directory", + maxLength: MAX_CWD_LENGTH, + }); + if (!isAbsolute(cwd)) { + failInvalidOption(`Working directory must be an absolute path. Received "${cwd}".`); + } + return cwd; +} + +export function validateRuntimeTimeoutSecondsInput(rawTimeout: unknown): number { + if (typeof rawTimeout !== "number" || !Number.isFinite(rawTimeout)) { + failInvalidOption("Timeout must be a positive integer in seconds."); + } + const timeout = Math.round(rawTimeout); + if (timeout < MIN_TIMEOUT_SECONDS || timeout > MAX_TIMEOUT_SECONDS) { + failInvalidOption( + `Timeout must be between ${MIN_TIMEOUT_SECONDS} and ${MAX_TIMEOUT_SECONDS} seconds.`, + ); + } + return timeout; +} + +export function parseRuntimeTimeoutSecondsInput(rawTimeout: unknown): number { + const normalized = normalizeText(rawTimeout); + if (!normalized || !/^\d+$/.test(normalized)) { + failInvalidOption("Timeout must be a positive integer in seconds."); + } + return validateRuntimeTimeoutSecondsInput(Number.parseInt(normalized, 10)); +} + +export function validateRuntimeConfigOptionInput( + rawKey: unknown, + rawValue: unknown, +): { + key: string; + value: string; +} { + return { + key: validateBackendOptionKey(rawKey), + value: validateBackendOptionValue(rawValue), + }; +} + +export function validateRuntimeOptionPatch( + patch: Partial | undefined, +): Partial { + if (!patch) { + return {}; + } + const rawPatch = patch as Record; + const allowedKeys = new Set([ + "runtimeMode", + "model", + "cwd", + "permissionProfile", + "timeoutSeconds", + "backendExtras", + ]); + for (const key of Object.keys(rawPatch)) { + if (!allowedKeys.has(key)) { + failInvalidOption(`Unknown runtime option "${key}".`); + } + } + + const next: Partial = {}; + if (Object.hasOwn(rawPatch, "runtimeMode")) { + if (rawPatch.runtimeMode === undefined) { + next.runtimeMode = undefined; + } else { + next.runtimeMode = validateRuntimeModeInput(rawPatch.runtimeMode); + } + } + if (Object.hasOwn(rawPatch, "model")) { + if (rawPatch.model === undefined) { + next.model = undefined; + } else { + next.model = validateRuntimeModelInput(rawPatch.model); + } + } + if (Object.hasOwn(rawPatch, "cwd")) { + if (rawPatch.cwd === undefined) { + next.cwd = undefined; + } else { + next.cwd = validateRuntimeCwdInput(rawPatch.cwd); + } + } + if (Object.hasOwn(rawPatch, "permissionProfile")) { + if (rawPatch.permissionProfile === undefined) { + next.permissionProfile = undefined; + } else { + next.permissionProfile = validateRuntimePermissionProfileInput(rawPatch.permissionProfile); + } + } + if (Object.hasOwn(rawPatch, "timeoutSeconds")) { + if (rawPatch.timeoutSeconds === undefined) { + next.timeoutSeconds = undefined; + } else { + next.timeoutSeconds = validateRuntimeTimeoutSecondsInput(rawPatch.timeoutSeconds); + } + } + if (Object.hasOwn(rawPatch, "backendExtras")) { + const rawExtras = rawPatch.backendExtras; + if (rawExtras === undefined) { + next.backendExtras = undefined; + } else if (!rawExtras || typeof rawExtras !== "object" || Array.isArray(rawExtras)) { + failInvalidOption("Backend extras must be a key/value object."); + } else { + const entries = Object.entries(rawExtras); + if (entries.length > MAX_BACKEND_EXTRAS) { + failInvalidOption(`Backend extras must include at most ${MAX_BACKEND_EXTRAS} entries.`); + } + const extras: Record = {}; + for (const [entryKey, entryValue] of entries) { + const { key, value } = validateRuntimeConfigOptionInput(entryKey, entryValue); + extras[key] = value; + } + next.backendExtras = Object.keys(extras).length > 0 ? extras : undefined; + } + } + + return next; +} + +export function normalizeText(value: unknown): string | undefined { + if (typeof value !== "string") { + return undefined; + } + const trimmed = value.trim(); + return trimmed || undefined; +} + +export function normalizeRuntimeOptions( + options: AcpSessionRuntimeOptions | undefined, +): AcpSessionRuntimeOptions { + const runtimeMode = normalizeText(options?.runtimeMode); + const model = normalizeText(options?.model); + const cwd = normalizeText(options?.cwd); + const permissionProfile = normalizeText(options?.permissionProfile); + let timeoutSeconds: number | undefined; + if (typeof options?.timeoutSeconds === "number" && Number.isFinite(options.timeoutSeconds)) { + const rounded = Math.round(options.timeoutSeconds); + if (rounded > 0) { + timeoutSeconds = rounded; + } + } + const backendExtrasEntries = Object.entries(options?.backendExtras ?? {}) + .map(([key, value]) => [normalizeText(key), normalizeText(value)] as const) + .filter(([key, value]) => Boolean(key && value)) as Array<[string, string]>; + const backendExtras = + backendExtrasEntries.length > 0 ? Object.fromEntries(backendExtrasEntries) : undefined; + return { + ...(runtimeMode ? { runtimeMode } : {}), + ...(model ? { model } : {}), + ...(cwd ? { cwd } : {}), + ...(permissionProfile ? { permissionProfile } : {}), + ...(typeof timeoutSeconds === "number" ? { timeoutSeconds } : {}), + ...(backendExtras ? { backendExtras } : {}), + }; +} + +export function mergeRuntimeOptions(params: { + current?: AcpSessionRuntimeOptions; + patch?: Partial; +}): AcpSessionRuntimeOptions { + const current = normalizeRuntimeOptions(params.current); + const patch = normalizeRuntimeOptions(validateRuntimeOptionPatch(params.patch)); + const mergedExtras = { + ...current.backendExtras, + ...patch.backendExtras, + }; + return normalizeRuntimeOptions({ + ...current, + ...patch, + ...(Object.keys(mergedExtras).length > 0 ? { backendExtras: mergedExtras } : {}), + }); +} + +export function resolveRuntimeOptionsFromMeta(meta: SessionAcpMeta): AcpSessionRuntimeOptions { + const normalized = normalizeRuntimeOptions(meta.runtimeOptions); + if (normalized.cwd || !meta.cwd) { + return normalized; + } + return normalizeRuntimeOptions({ + ...normalized, + cwd: meta.cwd, + }); +} + +export function runtimeOptionsEqual( + a: AcpSessionRuntimeOptions | undefined, + b: AcpSessionRuntimeOptions | undefined, +): boolean { + return JSON.stringify(normalizeRuntimeOptions(a)) === JSON.stringify(normalizeRuntimeOptions(b)); +} + +export function buildRuntimeControlSignature(options: AcpSessionRuntimeOptions): string { + const normalized = normalizeRuntimeOptions(options); + const extras = Object.entries(normalized.backendExtras ?? {}).toSorted(([a], [b]) => + a.localeCompare(b), + ); + return JSON.stringify({ + runtimeMode: normalized.runtimeMode ?? null, + model: normalized.model ?? null, + permissionProfile: normalized.permissionProfile ?? null, + timeoutSeconds: normalized.timeoutSeconds ?? null, + backendExtras: extras, + }); +} + +export function buildRuntimeConfigOptionPairs( + options: AcpSessionRuntimeOptions, +): Array<[string, string]> { + const normalized = normalizeRuntimeOptions(options); + const pairs = new Map(); + if (normalized.model) { + pairs.set("model", normalized.model); + } + if (normalized.permissionProfile) { + pairs.set("approval_policy", normalized.permissionProfile); + } + if (typeof normalized.timeoutSeconds === "number") { + pairs.set("timeout", String(normalized.timeoutSeconds)); + } + for (const [key, value] of Object.entries(normalized.backendExtras ?? {})) { + if (!pairs.has(key)) { + pairs.set(key, value); + } + } + return [...pairs.entries()]; +} + +export function inferRuntimeOptionPatchFromConfigOption( + key: string, + value: string, +): Partial { + const validated = validateRuntimeConfigOptionInput(key, value); + const normalizedKey = validated.key.toLowerCase(); + if (normalizedKey === "model") { + return { model: validateRuntimeModelInput(validated.value) }; + } + if ( + normalizedKey === "approval_policy" || + normalizedKey === "permission_profile" || + normalizedKey === "permissions" + ) { + return { permissionProfile: validateRuntimePermissionProfileInput(validated.value) }; + } + if (normalizedKey === "timeout" || normalizedKey === "timeout_seconds") { + return { timeoutSeconds: parseRuntimeTimeoutSecondsInput(validated.value) }; + } + if (normalizedKey === "cwd") { + return { cwd: validateRuntimeCwdInput(validated.value) }; + } + return { + backendExtras: { + [validated.key]: validated.value, + }, + }; +} diff --git a/src/acp/control-plane/session-actor-queue.ts b/src/acp/control-plane/session-actor-queue.ts new file mode 100644 index 000000000000..67dd6119a3bc --- /dev/null +++ b/src/acp/control-plane/session-actor-queue.ts @@ -0,0 +1,53 @@ +export class SessionActorQueue { + private readonly tailBySession = new Map>(); + private readonly pendingBySession = new Map(); + + getTailMapForTesting(): Map> { + return this.tailBySession; + } + + getTotalPendingCount(): number { + let total = 0; + for (const count of this.pendingBySession.values()) { + total += count; + } + return total; + } + + getPendingCountForSession(actorKey: string): number { + return this.pendingBySession.get(actorKey) ?? 0; + } + + async run(actorKey: string, op: () => Promise): Promise { + const previous = this.tailBySession.get(actorKey) ?? Promise.resolve(); + this.pendingBySession.set(actorKey, (this.pendingBySession.get(actorKey) ?? 0) + 1); + let release: () => void = () => {}; + const marker = new Promise((resolve) => { + release = resolve; + }); + const queuedTail = previous + .catch(() => { + // Keep actor queue alive after an operation failure. + }) + .then(() => marker); + this.tailBySession.set(actorKey, queuedTail); + + await previous.catch(() => { + // Previous failures should not block newer commands. + }); + try { + return await op(); + } finally { + const pending = (this.pendingBySession.get(actorKey) ?? 1) - 1; + if (pending <= 0) { + this.pendingBySession.delete(actorKey); + } else { + this.pendingBySession.set(actorKey, pending); + } + release(); + if (this.tailBySession.get(actorKey) === queuedTail) { + this.tailBySession.delete(actorKey); + } + } + } +} diff --git a/src/acp/control-plane/spawn.ts b/src/acp/control-plane/spawn.ts new file mode 100644 index 000000000000..5d9790cb5e78 --- /dev/null +++ b/src/acp/control-plane/spawn.ts @@ -0,0 +1,77 @@ +import type { OpenClawConfig } from "../../config/config.js"; +import { callGateway } from "../../gateway/call.js"; +import { logVerbose } from "../../globals.js"; +import { getSessionBindingService } from "../../infra/outbound/session-binding-service.js"; +import { getAcpSessionManager } from "./manager.js"; + +export type AcpSpawnRuntimeCloseHandle = { + runtime: { + close: (params: { + handle: { sessionKey: string; backend: string; runtimeSessionName: string }; + reason: string; + }) => Promise; + }; + handle: { sessionKey: string; backend: string; runtimeSessionName: string }; +}; + +export async function cleanupFailedAcpSpawn(params: { + cfg: OpenClawConfig; + sessionKey: string; + shouldDeleteSession: boolean; + deleteTranscript: boolean; + runtimeCloseHandle?: AcpSpawnRuntimeCloseHandle; +}): Promise { + if (params.runtimeCloseHandle) { + await params.runtimeCloseHandle.runtime + .close({ + handle: params.runtimeCloseHandle.handle, + reason: "spawn-failed", + }) + .catch((err) => { + logVerbose( + `acp-spawn: runtime cleanup close failed for ${params.sessionKey}: ${String(err)}`, + ); + }); + } + + const acpManager = getAcpSessionManager(); + await acpManager + .closeSession({ + cfg: params.cfg, + sessionKey: params.sessionKey, + reason: "spawn-failed", + allowBackendUnavailable: true, + requireAcpSession: false, + }) + .catch((err) => { + logVerbose( + `acp-spawn: manager cleanup close failed for ${params.sessionKey}: ${String(err)}`, + ); + }); + + await getSessionBindingService() + .unbind({ + targetSessionKey: params.sessionKey, + reason: "spawn-failed", + }) + .catch((err) => { + logVerbose( + `acp-spawn: binding cleanup unbind failed for ${params.sessionKey}: ${String(err)}`, + ); + }); + + if (!params.shouldDeleteSession) { + return; + } + await callGateway({ + method: "sessions.delete", + params: { + key: params.sessionKey, + deleteTranscript: params.deleteTranscript, + emitLifecycleHooks: false, + }, + timeoutMs: 10_000, + }).catch(() => { + // Best-effort cleanup only. + }); +} diff --git a/src/acp/policy.test.ts b/src/acp/policy.test.ts new file mode 100644 index 000000000000..3a623373a7ba --- /dev/null +++ b/src/acp/policy.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "vitest"; +import type { OpenClawConfig } from "../config/config.js"; +import { + isAcpAgentAllowedByPolicy, + isAcpDispatchEnabledByPolicy, + isAcpEnabledByPolicy, + resolveAcpAgentPolicyError, + resolveAcpDispatchPolicyError, + resolveAcpDispatchPolicyMessage, + resolveAcpDispatchPolicyState, +} from "./policy.js"; + +describe("acp policy", () => { + it("treats ACP as enabled by default", () => { + const cfg = {} satisfies OpenClawConfig; + expect(isAcpEnabledByPolicy(cfg)).toBe(true); + expect(isAcpDispatchEnabledByPolicy(cfg)).toBe(false); + expect(resolveAcpDispatchPolicyState(cfg)).toBe("dispatch_disabled"); + }); + + it("reports ACP disabled state when acp.enabled is false", () => { + const cfg = { + acp: { + enabled: false, + }, + } satisfies OpenClawConfig; + expect(isAcpEnabledByPolicy(cfg)).toBe(false); + expect(resolveAcpDispatchPolicyState(cfg)).toBe("acp_disabled"); + expect(resolveAcpDispatchPolicyMessage(cfg)).toContain("acp.enabled=false"); + expect(resolveAcpDispatchPolicyError(cfg)?.code).toBe("ACP_DISPATCH_DISABLED"); + }); + + it("reports dispatch-disabled state when dispatch gate is false", () => { + const cfg = { + acp: { + enabled: true, + dispatch: { + enabled: false, + }, + }, + } satisfies OpenClawConfig; + expect(isAcpDispatchEnabledByPolicy(cfg)).toBe(false); + expect(resolveAcpDispatchPolicyState(cfg)).toBe("dispatch_disabled"); + expect(resolveAcpDispatchPolicyMessage(cfg)).toContain("acp.dispatch.enabled=false"); + }); + + it("applies allowlist filtering for ACP agents", () => { + const cfg = { + acp: { + allowedAgents: ["Codex", "claude-code"], + }, + } satisfies OpenClawConfig; + expect(isAcpAgentAllowedByPolicy(cfg, "codex")).toBe(true); + expect(isAcpAgentAllowedByPolicy(cfg, "claude-code")).toBe(true); + expect(isAcpAgentAllowedByPolicy(cfg, "gemini")).toBe(false); + expect(resolveAcpAgentPolicyError(cfg, "gemini")?.code).toBe("ACP_SESSION_INIT_FAILED"); + expect(resolveAcpAgentPolicyError(cfg, "codex")).toBeNull(); + }); +}); diff --git a/src/acp/policy.ts b/src/acp/policy.ts new file mode 100644 index 000000000000..8297783b62d9 --- /dev/null +++ b/src/acp/policy.ts @@ -0,0 +1,69 @@ +import type { OpenClawConfig } from "../config/config.js"; +import { normalizeAgentId } from "../routing/session-key.js"; +import { AcpRuntimeError } from "./runtime/errors.js"; + +const ACP_DISABLED_MESSAGE = "ACP is disabled by policy (`acp.enabled=false`)."; +const ACP_DISPATCH_DISABLED_MESSAGE = + "ACP dispatch is disabled by policy (`acp.dispatch.enabled=false`)."; + +export type AcpDispatchPolicyState = "enabled" | "acp_disabled" | "dispatch_disabled"; + +export function isAcpEnabledByPolicy(cfg: OpenClawConfig): boolean { + return cfg.acp?.enabled !== false; +} + +export function resolveAcpDispatchPolicyState(cfg: OpenClawConfig): AcpDispatchPolicyState { + if (!isAcpEnabledByPolicy(cfg)) { + return "acp_disabled"; + } + if (cfg.acp?.dispatch?.enabled !== true) { + return "dispatch_disabled"; + } + return "enabled"; +} + +export function isAcpDispatchEnabledByPolicy(cfg: OpenClawConfig): boolean { + return resolveAcpDispatchPolicyState(cfg) === "enabled"; +} + +export function resolveAcpDispatchPolicyMessage(cfg: OpenClawConfig): string | null { + const state = resolveAcpDispatchPolicyState(cfg); + if (state === "acp_disabled") { + return ACP_DISABLED_MESSAGE; + } + if (state === "dispatch_disabled") { + return ACP_DISPATCH_DISABLED_MESSAGE; + } + return null; +} + +export function resolveAcpDispatchPolicyError(cfg: OpenClawConfig): AcpRuntimeError | null { + const message = resolveAcpDispatchPolicyMessage(cfg); + if (!message) { + return null; + } + return new AcpRuntimeError("ACP_DISPATCH_DISABLED", message); +} + +export function isAcpAgentAllowedByPolicy(cfg: OpenClawConfig, agentId: string): boolean { + const allowed = (cfg.acp?.allowedAgents ?? []) + .map((entry) => normalizeAgentId(entry)) + .filter(Boolean); + if (allowed.length === 0) { + return true; + } + return allowed.includes(normalizeAgentId(agentId)); +} + +export function resolveAcpAgentPolicyError( + cfg: OpenClawConfig, + agentId: string, +): AcpRuntimeError | null { + if (isAcpAgentAllowedByPolicy(cfg, agentId)) { + return null; + } + return new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `ACP agent "${normalizeAgentId(agentId)}" is not allowed by policy.`, + ); +} diff --git a/src/acp/runtime/adapter-contract.testkit.ts b/src/acp/runtime/adapter-contract.testkit.ts new file mode 100644 index 000000000000..3c715b4777fb --- /dev/null +++ b/src/acp/runtime/adapter-contract.testkit.ts @@ -0,0 +1,114 @@ +import { randomUUID } from "node:crypto"; +import { expect } from "vitest"; +import { toAcpRuntimeError } from "./errors.js"; +import type { AcpRuntime, AcpRuntimeEvent } from "./types.js"; + +export type AcpRuntimeAdapterContractParams = { + createRuntime: () => Promise | AcpRuntime; + agentId?: string; + successPrompt?: string; + errorPrompt?: string; + assertSuccessEvents?: (events: AcpRuntimeEvent[]) => void | Promise; + assertErrorOutcome?: (params: { + events: AcpRuntimeEvent[]; + thrown: unknown; + }) => void | Promise; +}; + +export async function runAcpRuntimeAdapterContract( + params: AcpRuntimeAdapterContractParams, +): Promise { + const runtime = await params.createRuntime(); + const sessionKey = `agent:${params.agentId ?? "codex"}:acp:contract-${randomUUID()}`; + const agent = params.agentId ?? "codex"; + + const handle = await runtime.ensureSession({ + sessionKey, + agent, + mode: "persistent", + }); + expect(handle.sessionKey).toBe(sessionKey); + expect(handle.backend.trim()).not.toHaveLength(0); + expect(handle.runtimeSessionName.trim()).not.toHaveLength(0); + + const successEvents: AcpRuntimeEvent[] = []; + for await (const event of runtime.runTurn({ + handle, + text: params.successPrompt ?? "contract-success", + mode: "prompt", + requestId: `contract-success-${randomUUID()}`, + })) { + successEvents.push(event); + } + expect( + successEvents.some( + (event) => + event.type === "done" || + event.type === "text_delta" || + event.type === "status" || + event.type === "tool_call", + ), + ).toBe(true); + await params.assertSuccessEvents?.(successEvents); + + if (runtime.getStatus) { + const status = await runtime.getStatus({ handle }); + expect(status).toBeDefined(); + expect(typeof status).toBe("object"); + } + if (runtime.setMode) { + await runtime.setMode({ + handle, + mode: "contract", + }); + } + if (runtime.setConfigOption) { + await runtime.setConfigOption({ + handle, + key: "contract_key", + value: "contract_value", + }); + } + + let errorThrown: unknown = null; + const errorEvents: AcpRuntimeEvent[] = []; + const errorPrompt = params.errorPrompt?.trim(); + if (errorPrompt) { + try { + for await (const event of runtime.runTurn({ + handle, + text: errorPrompt, + mode: "prompt", + requestId: `contract-error-${randomUUID()}`, + })) { + errorEvents.push(event); + } + } catch (error) { + errorThrown = error; + } + const sawErrorEvent = errorEvents.some((event) => event.type === "error"); + expect(Boolean(errorThrown) || sawErrorEvent).toBe(true); + if (errorThrown) { + const acpError = toAcpRuntimeError({ + error: errorThrown, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP runtime contract expected an error turn failure.", + }); + expect(acpError.code.length).toBeGreaterThan(0); + expect(acpError.message.length).toBeGreaterThan(0); + } + } + await params.assertErrorOutcome?.({ + events: errorEvents, + thrown: errorThrown, + }); + + await runtime.cancel({ + handle, + reason: "contract-cancel", + }); + await runtime.close({ + handle, + reason: "contract-close", + }); +} diff --git a/src/acp/runtime/error-text.test.ts b/src/acp/runtime/error-text.test.ts new file mode 100644 index 000000000000..b58cd3ef4fbb --- /dev/null +++ b/src/acp/runtime/error-text.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest"; +import { formatAcpRuntimeErrorText } from "./error-text.js"; +import { AcpRuntimeError } from "./errors.js"; + +describe("formatAcpRuntimeErrorText", () => { + it("adds actionable next steps for known ACP runtime error codes", () => { + const text = formatAcpRuntimeErrorText( + new AcpRuntimeError("ACP_BACKEND_MISSING", "backend missing"), + ); + expect(text).toContain("ACP error (ACP_BACKEND_MISSING): backend missing"); + expect(text).toContain("next:"); + }); + + it("returns consistent ACP error envelope for runtime failures", () => { + const text = formatAcpRuntimeErrorText(new AcpRuntimeError("ACP_TURN_FAILED", "turn failed")); + expect(text).toContain("ACP error (ACP_TURN_FAILED): turn failed"); + expect(text).toContain("next:"); + }); +}); diff --git a/src/acp/runtime/error-text.ts b/src/acp/runtime/error-text.ts new file mode 100644 index 000000000000..e4901e1c8694 --- /dev/null +++ b/src/acp/runtime/error-text.ts @@ -0,0 +1,45 @@ +import { type AcpRuntimeErrorCode, AcpRuntimeError, toAcpRuntimeError } from "./errors.js"; + +function resolveAcpRuntimeErrorNextStep(error: AcpRuntimeError): string | undefined { + if (error.code === "ACP_BACKEND_MISSING" || error.code === "ACP_BACKEND_UNAVAILABLE") { + return "Run `/acp doctor`, install/enable the backend plugin, then retry."; + } + if (error.code === "ACP_DISPATCH_DISABLED") { + return "Enable `acp.dispatch.enabled=true` to allow thread-message ACP turns."; + } + if (error.code === "ACP_SESSION_INIT_FAILED") { + return "If this session is stale, recreate it with `/acp spawn` and rebind the thread."; + } + if (error.code === "ACP_INVALID_RUNTIME_OPTION") { + return "Use `/acp status` to inspect options and pass valid values."; + } + if (error.code === "ACP_BACKEND_UNSUPPORTED_CONTROL") { + return "This backend does not support that control; use a supported command."; + } + if (error.code === "ACP_TURN_FAILED") { + return "Retry, or use `/acp cancel` and send the message again."; + } + return undefined; +} + +export function formatAcpRuntimeErrorText(error: AcpRuntimeError): string { + const next = resolveAcpRuntimeErrorNextStep(error); + if (!next) { + return `ACP error (${error.code}): ${error.message}`; + } + return `ACP error (${error.code}): ${error.message}\nnext: ${next}`; +} + +export function toAcpRuntimeErrorText(params: { + error: unknown; + fallbackCode: AcpRuntimeErrorCode; + fallbackMessage: string; +}): string { + return formatAcpRuntimeErrorText( + toAcpRuntimeError({ + error: params.error, + fallbackCode: params.fallbackCode, + fallbackMessage: params.fallbackMessage, + }), + ); +} diff --git a/src/acp/runtime/errors.test.ts b/src/acp/runtime/errors.test.ts new file mode 100644 index 000000000000..10ba3667d840 --- /dev/null +++ b/src/acp/runtime/errors.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "vitest"; +import { AcpRuntimeError, withAcpRuntimeErrorBoundary } from "./errors.js"; + +describe("withAcpRuntimeErrorBoundary", () => { + it("wraps generic errors with fallback code and source message", async () => { + await expect( + withAcpRuntimeErrorBoundary({ + run: async () => { + throw new Error("boom"); + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "fallback", + }), + ).rejects.toMatchObject({ + name: "AcpRuntimeError", + code: "ACP_TURN_FAILED", + message: "boom", + }); + }); + + it("passes through existing ACP runtime errors", async () => { + const existing = new AcpRuntimeError("ACP_BACKEND_MISSING", "backend missing"); + await expect( + withAcpRuntimeErrorBoundary({ + run: async () => { + throw existing; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "fallback", + }), + ).rejects.toBe(existing); + }); +}); diff --git a/src/acp/runtime/errors.ts b/src/acp/runtime/errors.ts new file mode 100644 index 000000000000..0ac56251f8eb --- /dev/null +++ b/src/acp/runtime/errors.ts @@ -0,0 +1,61 @@ +export const ACP_ERROR_CODES = [ + "ACP_BACKEND_MISSING", + "ACP_BACKEND_UNAVAILABLE", + "ACP_BACKEND_UNSUPPORTED_CONTROL", + "ACP_DISPATCH_DISABLED", + "ACP_INVALID_RUNTIME_OPTION", + "ACP_SESSION_INIT_FAILED", + "ACP_TURN_FAILED", +] as const; + +export type AcpRuntimeErrorCode = (typeof ACP_ERROR_CODES)[number]; + +export class AcpRuntimeError extends Error { + readonly code: AcpRuntimeErrorCode; + override readonly cause?: unknown; + + constructor(code: AcpRuntimeErrorCode, message: string, options?: { cause?: unknown }) { + super(message); + this.name = "AcpRuntimeError"; + this.code = code; + this.cause = options?.cause; + } +} + +export function isAcpRuntimeError(value: unknown): value is AcpRuntimeError { + return value instanceof AcpRuntimeError; +} + +export function toAcpRuntimeError(params: { + error: unknown; + fallbackCode: AcpRuntimeErrorCode; + fallbackMessage: string; +}): AcpRuntimeError { + if (params.error instanceof AcpRuntimeError) { + return params.error; + } + if (params.error instanceof Error) { + return new AcpRuntimeError(params.fallbackCode, params.error.message, { + cause: params.error, + }); + } + return new AcpRuntimeError(params.fallbackCode, params.fallbackMessage, { + cause: params.error, + }); +} + +export async function withAcpRuntimeErrorBoundary(params: { + run: () => Promise; + fallbackCode: AcpRuntimeErrorCode; + fallbackMessage: string; +}): Promise { + try { + return await params.run(); + } catch (error) { + throw toAcpRuntimeError({ + error, + fallbackCode: params.fallbackCode, + fallbackMessage: params.fallbackMessage, + }); + } +} diff --git a/src/acp/runtime/registry.test.ts b/src/acp/runtime/registry.test.ts new file mode 100644 index 000000000000..fab6a1b51e73 --- /dev/null +++ b/src/acp/runtime/registry.test.ts @@ -0,0 +1,99 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { AcpRuntimeError } from "./errors.js"; +import { + __testing, + getAcpRuntimeBackend, + registerAcpRuntimeBackend, + requireAcpRuntimeBackend, + unregisterAcpRuntimeBackend, +} from "./registry.js"; +import type { AcpRuntime } from "./types.js"; + +function createRuntimeStub(): AcpRuntime { + return { + ensureSession: vi.fn(async (input) => ({ + sessionKey: input.sessionKey, + backend: "stub", + runtimeSessionName: `${input.sessionKey}:runtime`, + })), + runTurn: vi.fn(async function* () { + // no-op stream + }), + cancel: vi.fn(async () => {}), + close: vi.fn(async () => {}), + }; +} + +describe("acp runtime registry", () => { + beforeEach(() => { + __testing.resetAcpRuntimeBackendsForTests(); + }); + + it("registers and resolves backends by id", () => { + const runtime = createRuntimeStub(); + registerAcpRuntimeBackend({ id: "acpx", runtime }); + + const backend = getAcpRuntimeBackend("acpx"); + expect(backend?.id).toBe("acpx"); + expect(backend?.runtime).toBe(runtime); + }); + + it("prefers a healthy backend when resolving without explicit id", () => { + const unhealthyRuntime = createRuntimeStub(); + const healthyRuntime = createRuntimeStub(); + + registerAcpRuntimeBackend({ + id: "unhealthy", + runtime: unhealthyRuntime, + healthy: () => false, + }); + registerAcpRuntimeBackend({ + id: "healthy", + runtime: healthyRuntime, + healthy: () => true, + }); + + const backend = getAcpRuntimeBackend(); + expect(backend?.id).toBe("healthy"); + }); + + it("throws a typed missing-backend error when no backend is registered", () => { + expect(() => requireAcpRuntimeBackend()).toThrowError(AcpRuntimeError); + expect(() => requireAcpRuntimeBackend()).toThrowError(/ACP runtime backend is not configured/i); + }); + + it("throws a typed unavailable error when the requested backend is unhealthy", () => { + registerAcpRuntimeBackend({ + id: "acpx", + runtime: createRuntimeStub(), + healthy: () => false, + }); + + try { + requireAcpRuntimeBackend("acpx"); + throw new Error("expected requireAcpRuntimeBackend to throw"); + } catch (err) { + expect(err).toBeInstanceOf(AcpRuntimeError); + expect((err as AcpRuntimeError).code).toBe("ACP_BACKEND_UNAVAILABLE"); + } + }); + + it("unregisters a backend by id", () => { + registerAcpRuntimeBackend({ id: "acpx", runtime: createRuntimeStub() }); + unregisterAcpRuntimeBackend("acpx"); + expect(getAcpRuntimeBackend("acpx")).toBeNull(); + }); + + it("keeps backend state on a global registry for cross-loader access", () => { + const runtime = createRuntimeStub(); + const sharedState = __testing.getAcpRuntimeRegistryGlobalStateForTests(); + + sharedState.backendsById.set("acpx", { + id: "acpx", + runtime, + }); + + const backend = getAcpRuntimeBackend("acpx"); + expect(backend?.runtime).toBe(runtime); + }); +}); diff --git a/src/acp/runtime/registry.ts b/src/acp/runtime/registry.ts new file mode 100644 index 000000000000..4c0a3d73cd07 --- /dev/null +++ b/src/acp/runtime/registry.ts @@ -0,0 +1,118 @@ +import { AcpRuntimeError } from "./errors.js"; +import type { AcpRuntime } from "./types.js"; + +export type AcpRuntimeBackend = { + id: string; + runtime: AcpRuntime; + healthy?: () => boolean; +}; + +type AcpRuntimeRegistryGlobalState = { + backendsById: Map; +}; + +const ACP_RUNTIME_REGISTRY_STATE_KEY = Symbol.for("openclaw.acpRuntimeRegistryState"); + +function createAcpRuntimeRegistryGlobalState(): AcpRuntimeRegistryGlobalState { + return { + backendsById: new Map(), + }; +} + +function resolveAcpRuntimeRegistryGlobalState(): AcpRuntimeRegistryGlobalState { + const runtimeGlobal = globalThis as typeof globalThis & { + [ACP_RUNTIME_REGISTRY_STATE_KEY]?: AcpRuntimeRegistryGlobalState; + }; + if (!runtimeGlobal[ACP_RUNTIME_REGISTRY_STATE_KEY]) { + runtimeGlobal[ACP_RUNTIME_REGISTRY_STATE_KEY] = createAcpRuntimeRegistryGlobalState(); + } + return runtimeGlobal[ACP_RUNTIME_REGISTRY_STATE_KEY]; +} + +const ACP_BACKENDS_BY_ID = resolveAcpRuntimeRegistryGlobalState().backendsById; + +function normalizeBackendId(id: string | undefined): string { + return id?.trim().toLowerCase() || ""; +} + +function isBackendHealthy(backend: AcpRuntimeBackend): boolean { + if (!backend.healthy) { + return true; + } + try { + return backend.healthy(); + } catch { + return false; + } +} + +export function registerAcpRuntimeBackend(backend: AcpRuntimeBackend): void { + const id = normalizeBackendId(backend.id); + if (!id) { + throw new Error("ACP runtime backend id is required"); + } + if (!backend.runtime) { + throw new Error(`ACP runtime backend "${id}" is missing runtime implementation`); + } + ACP_BACKENDS_BY_ID.set(id, { + ...backend, + id, + }); +} + +export function unregisterAcpRuntimeBackend(id: string): void { + const normalized = normalizeBackendId(id); + if (!normalized) { + return; + } + ACP_BACKENDS_BY_ID.delete(normalized); +} + +export function getAcpRuntimeBackend(id?: string): AcpRuntimeBackend | null { + const normalized = normalizeBackendId(id); + if (normalized) { + return ACP_BACKENDS_BY_ID.get(normalized) ?? null; + } + if (ACP_BACKENDS_BY_ID.size === 0) { + return null; + } + for (const backend of ACP_BACKENDS_BY_ID.values()) { + if (isBackendHealthy(backend)) { + return backend; + } + } + return ACP_BACKENDS_BY_ID.values().next().value ?? null; +} + +export function requireAcpRuntimeBackend(id?: string): AcpRuntimeBackend { + const normalized = normalizeBackendId(id); + const backend = getAcpRuntimeBackend(normalized || undefined); + if (!backend) { + throw new AcpRuntimeError( + "ACP_BACKEND_MISSING", + "ACP runtime backend is not configured. Install and enable the acpx runtime plugin.", + ); + } + if (!isBackendHealthy(backend)) { + throw new AcpRuntimeError( + "ACP_BACKEND_UNAVAILABLE", + "ACP runtime backend is currently unavailable. Try again in a moment.", + ); + } + if (normalized && backend.id !== normalized) { + throw new AcpRuntimeError( + "ACP_BACKEND_MISSING", + `ACP runtime backend "${normalized}" is not registered.`, + ); + } + return backend; +} + +export const __testing = { + resetAcpRuntimeBackendsForTests() { + ACP_BACKENDS_BY_ID.clear(); + }, + getAcpRuntimeRegistryGlobalStateForTests() { + return resolveAcpRuntimeRegistryGlobalState(); + }, +}; diff --git a/src/acp/runtime/session-identifiers.test.ts b/src/acp/runtime/session-identifiers.test.ts new file mode 100644 index 000000000000..fe7b0d6c2bcd --- /dev/null +++ b/src/acp/runtime/session-identifiers.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from "vitest"; +import { + resolveAcpSessionCwd, + resolveAcpSessionIdentifierLinesFromIdentity, + resolveAcpThreadSessionDetailLines, +} from "./session-identifiers.js"; + +describe("session identifier helpers", () => { + it("hides unresolved identifiers from thread intro details while pending", () => { + const lines = resolveAcpThreadSessionDetailLines({ + sessionKey: "agent:codex:acp:pending-1", + meta: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + identity: { + state: "pending", + source: "ensure", + lastUpdatedAt: Date.now(), + acpxSessionId: "acpx-123", + agentSessionId: "inner-123", + }, + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + + expect(lines).toEqual([]); + }); + + it("adds a Codex resume hint when agent identity is resolved", () => { + const lines = resolveAcpThreadSessionDetailLines({ + sessionKey: "agent:codex:acp:resolved-1", + meta: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + identity: { + state: "resolved", + source: "status", + lastUpdatedAt: Date.now(), + acpxSessionId: "acpx-123", + agentSessionId: "inner-123", + }, + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + + expect(lines).toContain("agent session id: inner-123"); + expect(lines).toContain("acpx session id: acpx-123"); + expect(lines).toContain( + "resume in Codex CLI: `codex resume inner-123` (continues this conversation).", + ); + }); + + it("shows pending identity text for status rendering", () => { + const lines = resolveAcpSessionIdentifierLinesFromIdentity({ + backend: "acpx", + mode: "status", + identity: { + state: "pending", + source: "status", + lastUpdatedAt: Date.now(), + agentSessionId: "inner-123", + }, + }); + + expect(lines).toEqual(["session ids: pending (available after the first reply)"]); + }); + + it("prefers runtimeOptions.cwd over legacy meta.cwd", () => { + const cwd = resolveAcpSessionCwd({ + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + runtimeOptions: { + cwd: "/repo/new", + }, + cwd: "/repo/old", + state: "idle", + lastActivityAt: Date.now(), + }); + expect(cwd).toBe("/repo/new"); + }); +}); diff --git a/src/acp/runtime/session-identifiers.ts b/src/acp/runtime/session-identifiers.ts new file mode 100644 index 000000000000..d342d8b02eb9 --- /dev/null +++ b/src/acp/runtime/session-identifiers.ts @@ -0,0 +1,131 @@ +import type { SessionAcpIdentity, SessionAcpMeta } from "../../config/sessions/types.js"; +import { isSessionIdentityPending, resolveSessionIdentityFromMeta } from "./session-identity.js"; + +export const ACP_SESSION_IDENTITY_RENDERER_VERSION = "v1"; +export type AcpSessionIdentifierRenderMode = "status" | "thread"; + +type SessionResumeHintResolver = (params: { agentSessionId: string }) => string; + +const ACP_AGENT_RESUME_HINT_BY_KEY = new Map([ + [ + "codex", + ({ agentSessionId }) => + `resume in Codex CLI: \`codex resume ${agentSessionId}\` (continues this conversation).`, + ], + [ + "openai-codex", + ({ agentSessionId }) => + `resume in Codex CLI: \`codex resume ${agentSessionId}\` (continues this conversation).`, + ], + [ + "codex-cli", + ({ agentSessionId }) => + `resume in Codex CLI: \`codex resume ${agentSessionId}\` (continues this conversation).`, + ], +]); + +function normalizeText(value: unknown): string | undefined { + if (typeof value !== "string") { + return undefined; + } + const trimmed = value.trim(); + return trimmed || undefined; +} + +function normalizeAgentHintKey(value: unknown): string | undefined { + const normalized = normalizeText(value); + if (!normalized) { + return undefined; + } + return normalized.toLowerCase().replace(/[\s_]+/g, "-"); +} + +function resolveAcpAgentResumeHintLine(params: { + agentId?: string; + agentSessionId?: string; +}): string | undefined { + const agentSessionId = normalizeText(params.agentSessionId); + const agentKey = normalizeAgentHintKey(params.agentId); + if (!agentSessionId || !agentKey) { + return undefined; + } + const resolver = ACP_AGENT_RESUME_HINT_BY_KEY.get(agentKey); + return resolver ? resolver({ agentSessionId }) : undefined; +} + +export function resolveAcpSessionIdentifierLines(params: { + sessionKey: string; + meta?: SessionAcpMeta; +}): string[] { + const backend = normalizeText(params.meta?.backend) ?? "backend"; + const identity = resolveSessionIdentityFromMeta(params.meta); + return resolveAcpSessionIdentifierLinesFromIdentity({ + backend, + identity, + mode: "status", + }); +} + +export function resolveAcpSessionIdentifierLinesFromIdentity(params: { + backend: string; + identity?: SessionAcpIdentity; + mode?: AcpSessionIdentifierRenderMode; +}): string[] { + const backend = normalizeText(params.backend) ?? "backend"; + const mode = params.mode ?? "status"; + const identity = params.identity; + const agentSessionId = normalizeText(identity?.agentSessionId); + const acpxSessionId = normalizeText(identity?.acpxSessionId); + const acpxRecordId = normalizeText(identity?.acpxRecordId); + const hasIdentifier = Boolean(agentSessionId || acpxSessionId || acpxRecordId); + if (isSessionIdentityPending(identity) && hasIdentifier) { + if (mode === "status") { + return ["session ids: pending (available after the first reply)"]; + } + return []; + } + const lines: string[] = []; + if (agentSessionId) { + lines.push(`agent session id: ${agentSessionId}`); + } + if (acpxSessionId) { + lines.push(`${backend} session id: ${acpxSessionId}`); + } + if (acpxRecordId) { + lines.push(`${backend} record id: ${acpxRecordId}`); + } + return lines; +} + +export function resolveAcpSessionCwd(meta?: SessionAcpMeta): string | undefined { + const runtimeCwd = normalizeText(meta?.runtimeOptions?.cwd); + if (runtimeCwd) { + return runtimeCwd; + } + return normalizeText(meta?.cwd); +} + +export function resolveAcpThreadSessionDetailLines(params: { + sessionKey: string; + meta?: SessionAcpMeta; +}): string[] { + const meta = params.meta; + const identity = resolveSessionIdentityFromMeta(meta); + const backend = normalizeText(meta?.backend) ?? "backend"; + const lines = resolveAcpSessionIdentifierLinesFromIdentity({ + backend, + identity, + mode: "thread", + }); + if (lines.length === 0) { + return lines; + } + const hint = resolveAcpAgentResumeHintLine({ + agentId: meta?.agent, + agentSessionId: identity?.agentSessionId, + }); + if (hint) { + lines.push(hint); + } + return lines; +} diff --git a/src/acp/runtime/session-identity.ts b/src/acp/runtime/session-identity.ts new file mode 100644 index 000000000000..066a3cb71e56 --- /dev/null +++ b/src/acp/runtime/session-identity.ts @@ -0,0 +1,210 @@ +import type { + SessionAcpIdentity, + SessionAcpIdentitySource, + SessionAcpMeta, +} from "../../config/sessions/types.js"; +import type { AcpRuntimeHandle, AcpRuntimeStatus } from "./types.js"; + +function normalizeText(value: unknown): string | undefined { + if (typeof value !== "string") { + return undefined; + } + const trimmed = value.trim(); + return trimmed || undefined; +} + +function normalizeIdentityState(value: unknown): SessionAcpIdentity["state"] | undefined { + if (value !== "pending" && value !== "resolved") { + return undefined; + } + return value; +} + +function normalizeIdentitySource(value: unknown): SessionAcpIdentitySource | undefined { + if (value !== "ensure" && value !== "status" && value !== "event") { + return undefined; + } + return value; +} + +function normalizeIdentity( + identity: SessionAcpIdentity | undefined, +): SessionAcpIdentity | undefined { + if (!identity) { + return undefined; + } + const state = normalizeIdentityState(identity.state); + const source = normalizeIdentitySource(identity.source); + const acpxRecordId = normalizeText(identity.acpxRecordId); + const acpxSessionId = normalizeText(identity.acpxSessionId); + const agentSessionId = normalizeText(identity.agentSessionId); + const lastUpdatedAt = + typeof identity.lastUpdatedAt === "number" && Number.isFinite(identity.lastUpdatedAt) + ? identity.lastUpdatedAt + : undefined; + const hasAnyId = Boolean(acpxRecordId || acpxSessionId || agentSessionId); + if (!state && !source && !hasAnyId && lastUpdatedAt === undefined) { + return undefined; + } + const resolved = Boolean(acpxSessionId || agentSessionId); + const normalizedState = state ?? (resolved ? "resolved" : "pending"); + return { + state: normalizedState, + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(acpxSessionId ? { acpxSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + source: source ?? "status", + lastUpdatedAt: lastUpdatedAt ?? Date.now(), + }; +} + +export function resolveSessionIdentityFromMeta( + meta: SessionAcpMeta | undefined, +): SessionAcpIdentity | undefined { + if (!meta) { + return undefined; + } + return normalizeIdentity(meta.identity); +} + +export function identityHasStableSessionId(identity: SessionAcpIdentity | undefined): boolean { + return Boolean(identity?.acpxSessionId || identity?.agentSessionId); +} + +export function isSessionIdentityPending(identity: SessionAcpIdentity | undefined): boolean { + if (!identity) { + return true; + } + return identity.state === "pending"; +} + +export function identityEquals( + left: SessionAcpIdentity | undefined, + right: SessionAcpIdentity | undefined, +): boolean { + const a = normalizeIdentity(left); + const b = normalizeIdentity(right); + if (!a && !b) { + return true; + } + if (!a || !b) { + return false; + } + return ( + a.state === b.state && + a.acpxRecordId === b.acpxRecordId && + a.acpxSessionId === b.acpxSessionId && + a.agentSessionId === b.agentSessionId && + a.source === b.source + ); +} + +export function mergeSessionIdentity(params: { + current: SessionAcpIdentity | undefined; + incoming: SessionAcpIdentity | undefined; + now: number; +}): SessionAcpIdentity | undefined { + const current = normalizeIdentity(params.current); + const incoming = normalizeIdentity(params.incoming); + if (!current) { + if (!incoming) { + return undefined; + } + return { ...incoming, lastUpdatedAt: params.now }; + } + if (!incoming) { + return current; + } + + const currentResolved = current.state === "resolved"; + const incomingResolved = incoming.state === "resolved"; + const allowIncomingValue = !currentResolved || incomingResolved; + const nextRecordId = + allowIncomingValue && incoming.acpxRecordId ? incoming.acpxRecordId : current.acpxRecordId; + const nextAcpxSessionId = + allowIncomingValue && incoming.acpxSessionId ? incoming.acpxSessionId : current.acpxSessionId; + const nextAgentSessionId = + allowIncomingValue && incoming.agentSessionId + ? incoming.agentSessionId + : current.agentSessionId; + + const nextResolved = Boolean(nextAcpxSessionId || nextAgentSessionId); + const nextState: SessionAcpIdentity["state"] = nextResolved + ? "resolved" + : currentResolved + ? "resolved" + : incoming.state; + const nextSource = allowIncomingValue ? incoming.source : current.source; + const next: SessionAcpIdentity = { + state: nextState, + ...(nextRecordId ? { acpxRecordId: nextRecordId } : {}), + ...(nextAcpxSessionId ? { acpxSessionId: nextAcpxSessionId } : {}), + ...(nextAgentSessionId ? { agentSessionId: nextAgentSessionId } : {}), + source: nextSource, + lastUpdatedAt: params.now, + }; + return next; +} + +export function createIdentityFromEnsure(params: { + handle: AcpRuntimeHandle; + now: number; +}): SessionAcpIdentity | undefined { + const acpxRecordId = normalizeText((params.handle as { acpxRecordId?: unknown }).acpxRecordId); + const acpxSessionId = normalizeText(params.handle.backendSessionId); + const agentSessionId = normalizeText(params.handle.agentSessionId); + if (!acpxRecordId && !acpxSessionId && !agentSessionId) { + return undefined; + } + return { + state: "pending", + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(acpxSessionId ? { acpxSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + source: "ensure", + lastUpdatedAt: params.now, + }; +} + +export function createIdentityFromStatus(params: { + status: AcpRuntimeStatus | undefined; + now: number; +}): SessionAcpIdentity | undefined { + if (!params.status) { + return undefined; + } + const details = params.status.details; + const acpxRecordId = + normalizeText((params.status as { acpxRecordId?: unknown }).acpxRecordId) ?? + normalizeText(details?.acpxRecordId); + const acpxSessionId = + normalizeText(params.status.backendSessionId) ?? + normalizeText(details?.backendSessionId) ?? + normalizeText(details?.acpxSessionId); + const agentSessionId = + normalizeText(params.status.agentSessionId) ?? normalizeText(details?.agentSessionId); + if (!acpxRecordId && !acpxSessionId && !agentSessionId) { + return undefined; + } + const resolved = Boolean(acpxSessionId || agentSessionId); + return { + state: resolved ? "resolved" : "pending", + ...(acpxRecordId ? { acpxRecordId } : {}), + ...(acpxSessionId ? { acpxSessionId } : {}), + ...(agentSessionId ? { agentSessionId } : {}), + source: "status", + lastUpdatedAt: params.now, + }; +} + +export function resolveRuntimeHandleIdentifiersFromIdentity( + identity: SessionAcpIdentity | undefined, +): { backendSessionId?: string; agentSessionId?: string } { + if (!identity) { + return {}; + } + return { + ...(identity.acpxSessionId ? { backendSessionId: identity.acpxSessionId } : {}), + ...(identity.agentSessionId ? { agentSessionId: identity.agentSessionId } : {}), + }; +} diff --git a/src/acp/runtime/session-meta.ts b/src/acp/runtime/session-meta.ts new file mode 100644 index 000000000000..fd4a5813f9b5 --- /dev/null +++ b/src/acp/runtime/session-meta.ts @@ -0,0 +1,165 @@ +import path from "node:path"; +import { resolveAgentSessionDirs } from "../../agents/session-dirs.js"; +import type { OpenClawConfig } from "../../config/config.js"; +import { loadConfig } from "../../config/config.js"; +import { resolveStateDir } from "../../config/paths.js"; +import { loadSessionStore, resolveStorePath, updateSessionStore } from "../../config/sessions.js"; +import { + mergeSessionEntry, + type SessionAcpMeta, + type SessionEntry, +} from "../../config/sessions/types.js"; +import { parseAgentSessionKey } from "../../routing/session-key.js"; + +export type AcpSessionStoreEntry = { + cfg: OpenClawConfig; + storePath: string; + sessionKey: string; + storeSessionKey: string; + entry?: SessionEntry; + acp?: SessionAcpMeta; + storeReadFailed?: boolean; +}; + +function resolveStoreSessionKey(store: Record, sessionKey: string): string { + const normalized = sessionKey.trim(); + if (!normalized) { + return ""; + } + if (store[normalized]) { + return normalized; + } + const lower = normalized.toLowerCase(); + if (store[lower]) { + return lower; + } + for (const key of Object.keys(store)) { + if (key.toLowerCase() === lower) { + return key; + } + } + return lower; +} + +export function resolveSessionStorePathForAcp(params: { + sessionKey: string; + cfg?: OpenClawConfig; +}): { cfg: OpenClawConfig; storePath: string } { + const cfg = params.cfg ?? loadConfig(); + const parsed = parseAgentSessionKey(params.sessionKey); + const storePath = resolveStorePath(cfg.session?.store, { + agentId: parsed?.agentId, + }); + return { cfg, storePath }; +} + +export function readAcpSessionEntry(params: { + sessionKey: string; + cfg?: OpenClawConfig; +}): AcpSessionStoreEntry | null { + const sessionKey = params.sessionKey.trim(); + if (!sessionKey) { + return null; + } + const { cfg, storePath } = resolveSessionStorePathForAcp({ + sessionKey, + cfg: params.cfg, + }); + let store: Record; + let storeReadFailed = false; + try { + store = loadSessionStore(storePath); + } catch { + storeReadFailed = true; + store = {}; + } + const storeSessionKey = resolveStoreSessionKey(store, sessionKey); + const entry = store[storeSessionKey]; + return { + cfg, + storePath, + sessionKey, + storeSessionKey, + entry, + acp: entry?.acp, + storeReadFailed, + }; +} + +export async function listAcpSessionEntries(params: { + cfg?: OpenClawConfig; +}): Promise { + const cfg = params.cfg ?? loadConfig(); + const stateDir = resolveStateDir(process.env); + const sessionDirs = await resolveAgentSessionDirs(stateDir); + const entries: AcpSessionStoreEntry[] = []; + + for (const sessionsDir of sessionDirs) { + const storePath = path.join(sessionsDir, "sessions.json"); + let store: Record; + try { + store = loadSessionStore(storePath); + } catch { + continue; + } + for (const [sessionKey, entry] of Object.entries(store)) { + if (!entry?.acp) { + continue; + } + entries.push({ + cfg, + storePath, + sessionKey, + storeSessionKey: sessionKey, + entry, + acp: entry.acp, + }); + } + } + + return entries; +} + +export async function upsertAcpSessionMeta(params: { + sessionKey: string; + cfg?: OpenClawConfig; + mutate: ( + current: SessionAcpMeta | undefined, + entry: SessionEntry | undefined, + ) => SessionAcpMeta | null | undefined; +}): Promise { + const sessionKey = params.sessionKey.trim(); + if (!sessionKey) { + return null; + } + const { storePath } = resolveSessionStorePathForAcp({ + sessionKey, + cfg: params.cfg, + }); + return await updateSessionStore( + storePath, + (store) => { + const storeSessionKey = resolveStoreSessionKey(store, sessionKey); + const currentEntry = store[storeSessionKey]; + const nextMeta = params.mutate(currentEntry?.acp, currentEntry); + if (nextMeta === undefined) { + return currentEntry ?? null; + } + if (nextMeta === null && !currentEntry) { + return null; + } + + const nextEntry = mergeSessionEntry(currentEntry, { + acp: nextMeta ?? undefined, + }); + if (nextMeta === null) { + delete nextEntry.acp; + } + store[storeSessionKey] = nextEntry; + return nextEntry; + }, + { + activeSessionKey: sessionKey.toLowerCase(), + }, + ); +} diff --git a/src/acp/runtime/types.ts b/src/acp/runtime/types.ts new file mode 100644 index 000000000000..4e479eb8c8cb --- /dev/null +++ b/src/acp/runtime/types.ts @@ -0,0 +1,110 @@ +export type AcpRuntimePromptMode = "prompt" | "steer"; + +export type AcpRuntimeSessionMode = "persistent" | "oneshot"; + +export type AcpRuntimeControl = "session/set_mode" | "session/set_config_option" | "session/status"; + +export type AcpRuntimeHandle = { + sessionKey: string; + backend: string; + runtimeSessionName: string; + /** Effective runtime working directory for this ACP session, if exposed by adapter/runtime. */ + cwd?: string; + /** Backend-local record identifier, if exposed by adapter/runtime (for example acpx record id). */ + acpxRecordId?: string; + /** Backend-level ACP session identifier, if exposed by adapter/runtime. */ + backendSessionId?: string; + /** Upstream harness session identifier, if exposed by adapter/runtime. */ + agentSessionId?: string; +}; + +export type AcpRuntimeEnsureInput = { + sessionKey: string; + agent: string; + mode: AcpRuntimeSessionMode; + cwd?: string; + env?: Record; +}; + +export type AcpRuntimeTurnInput = { + handle: AcpRuntimeHandle; + text: string; + mode: AcpRuntimePromptMode; + requestId: string; + signal?: AbortSignal; +}; + +export type AcpRuntimeCapabilities = { + controls: AcpRuntimeControl[]; + /** + * Optional backend-advertised option keys for session/set_config_option. + * Empty/undefined means "backend accepts keys, but did not advertise a strict list". + */ + configOptionKeys?: string[]; +}; + +export type AcpRuntimeStatus = { + summary?: string; + /** Backend-local record identifier, if exposed by adapter/runtime. */ + acpxRecordId?: string; + /** Backend-level ACP session identifier, if known at status time. */ + backendSessionId?: string; + /** Upstream harness session identifier, if known at status time. */ + agentSessionId?: string; + details?: Record; +}; + +export type AcpRuntimeDoctorReport = { + ok: boolean; + code?: string; + message: string; + installCommand?: string; + details?: string[]; +}; + +export type AcpRuntimeEvent = + | { + type: "text_delta"; + text: string; + stream?: "output" | "thought"; + } + | { + type: "status"; + text: string; + } + | { + type: "tool_call"; + text: string; + } + | { + type: "done"; + stopReason?: string; + } + | { + type: "error"; + message: string; + code?: string; + retryable?: boolean; + }; + +export interface AcpRuntime { + ensureSession(input: AcpRuntimeEnsureInput): Promise; + + runTurn(input: AcpRuntimeTurnInput): AsyncIterable; + + getCapabilities?(input: { + handle?: AcpRuntimeHandle; + }): Promise | AcpRuntimeCapabilities; + + getStatus?(input: { handle: AcpRuntimeHandle }): Promise; + + setMode?(input: { handle: AcpRuntimeHandle; mode: string }): Promise; + + setConfigOption?(input: { handle: AcpRuntimeHandle; key: string; value: string }): Promise; + + doctor?(): Promise; + + cancel(input: { handle: AcpRuntimeHandle; reason?: string }): Promise; + + close(input: { handle: AcpRuntimeHandle; reason: string }): Promise; +} diff --git a/src/agents/acp-binding-architecture.guardrail.test.ts b/src/agents/acp-binding-architecture.guardrail.test.ts new file mode 100644 index 000000000000..ab8f04a21667 --- /dev/null +++ b/src/agents/acp-binding-architecture.guardrail.test.ts @@ -0,0 +1,42 @@ +import { readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), ".."); + +type GuardedSource = { + path: string; + forbiddenPatterns: RegExp[]; +}; + +const GUARDED_SOURCES: GuardedSource[] = [ + { + path: "agents/acp-spawn.ts", + forbiddenPatterns: [/\bgetThreadBindingManager\b/, /\bparseDiscordTarget\b/], + }, + { + path: "auto-reply/reply/commands-acp/lifecycle.ts", + forbiddenPatterns: [/\bgetThreadBindingManager\b/, /\bunbindThreadBindingsBySessionKey\b/], + }, + { + path: "auto-reply/reply/commands-acp/targets.ts", + forbiddenPatterns: [/\bgetThreadBindingManager\b/], + }, + { + path: "auto-reply/reply/commands-subagents/action-focus.ts", + forbiddenPatterns: [/\bgetThreadBindingManager\b/], + }, +]; + +describe("ACP/session binding architecture guardrails", () => { + it("keeps ACP/focus flows off Discord thread-binding manager APIs", () => { + for (const source of GUARDED_SOURCES) { + const absolutePath = resolve(ROOT_DIR, source.path); + const text = readFileSync(absolutePath, "utf8"); + for (const pattern of source.forbiddenPatterns) { + expect(text).not.toMatch(pattern); + } + } + }); +}); diff --git a/src/agents/acp-spawn.test.ts b/src/agents/acp-spawn.test.ts new file mode 100644 index 000000000000..f722451d0c65 --- /dev/null +++ b/src/agents/acp-spawn.test.ts @@ -0,0 +1,373 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { OpenClawConfig } from "../config/config.js"; +import type { SessionBindingRecord } from "../infra/outbound/session-binding-service.js"; + +const hoisted = vi.hoisted(() => { + const callGatewayMock = vi.fn(); + const sessionBindingCapabilitiesMock = vi.fn(); + const sessionBindingBindMock = vi.fn(); + const sessionBindingUnbindMock = vi.fn(); + const sessionBindingResolveByConversationMock = vi.fn(); + const sessionBindingListBySessionMock = vi.fn(); + const closeSessionMock = vi.fn(); + const initializeSessionMock = vi.fn(); + const state = { + cfg: { + acp: { + enabled: true, + backend: "acpx", + allowedAgents: ["codex"], + }, + session: { + mainKey: "main", + scope: "per-sender", + }, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: true, + }, + }, + }, + } as OpenClawConfig, + }; + return { + callGatewayMock, + sessionBindingCapabilitiesMock, + sessionBindingBindMock, + sessionBindingUnbindMock, + sessionBindingResolveByConversationMock, + sessionBindingListBySessionMock, + closeSessionMock, + initializeSessionMock, + state, + }; +}); + +vi.mock("../config/config.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + loadConfig: () => hoisted.state.cfg, + }; +}); + +vi.mock("../gateway/call.js", () => ({ + callGateway: (opts: unknown) => hoisted.callGatewayMock(opts), +})); + +vi.mock("../acp/control-plane/manager.js", () => { + return { + getAcpSessionManager: () => ({ + initializeSession: (params: unknown) => hoisted.initializeSessionMock(params), + closeSession: (params: unknown) => hoisted.closeSessionMock(params), + }), + }; +}); + +vi.mock("../infra/outbound/session-binding-service.js", async (importOriginal) => { + const actual = + await importOriginal(); + return { + ...actual, + getSessionBindingService: () => ({ + bind: (input: unknown) => hoisted.sessionBindingBindMock(input), + getCapabilities: (params: unknown) => hoisted.sessionBindingCapabilitiesMock(params), + listBySession: (targetSessionKey: string) => + hoisted.sessionBindingListBySessionMock(targetSessionKey), + resolveByConversation: (ref: unknown) => hoisted.sessionBindingResolveByConversationMock(ref), + touch: vi.fn(), + unbind: (input: unknown) => hoisted.sessionBindingUnbindMock(input), + }), + }; +}); + +const { spawnAcpDirect } = await import("./acp-spawn.js"); + +function createSessionBinding(overrides?: Partial): SessionBindingRecord { + return { + bindingId: "default:child-thread", + targetSessionKey: "agent:codex:acp:s1", + targetKind: "session", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "child-thread", + parentConversationId: "parent-channel", + }, + status: "active", + boundAt: Date.now(), + metadata: { + agentId: "codex", + boundBy: "system", + }, + ...overrides, + }; +} + +describe("spawnAcpDirect", () => { + beforeEach(() => { + hoisted.state.cfg = { + acp: { + enabled: true, + backend: "acpx", + allowedAgents: ["codex"], + }, + session: { + mainKey: "main", + scope: "per-sender", + }, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: true, + }, + }, + }, + } satisfies OpenClawConfig; + + hoisted.callGatewayMock.mockReset().mockImplementation(async (argsUnknown: unknown) => { + const args = argsUnknown as { method?: string }; + if (args.method === "sessions.patch") { + return { ok: true }; + } + if (args.method === "agent") { + return { runId: "run-1" }; + } + if (args.method === "sessions.delete") { + return { ok: true }; + } + return {}; + }); + + hoisted.closeSessionMock.mockReset().mockResolvedValue({ + runtimeClosed: true, + metaCleared: false, + }); + hoisted.initializeSessionMock.mockReset().mockImplementation(async (argsUnknown: unknown) => { + const args = argsUnknown as { + sessionKey: string; + agent: string; + mode: "persistent" | "oneshot"; + cwd?: string; + }; + const runtimeSessionName = `${args.sessionKey}:runtime`; + const cwd = typeof args.cwd === "string" ? args.cwd : undefined; + return { + runtime: { + close: vi.fn().mockResolvedValue(undefined), + }, + handle: { + sessionKey: args.sessionKey, + backend: "acpx", + runtimeSessionName, + ...(cwd ? { cwd } : {}), + agentSessionId: "codex-inner-1", + backendSessionId: "acpx-1", + }, + meta: { + backend: "acpx", + agent: args.agent, + runtimeSessionName, + ...(cwd ? { runtimeOptions: { cwd }, cwd } : {}), + identity: { + state: "pending", + source: "ensure", + acpxSessionId: "acpx-1", + agentSessionId: "codex-inner-1", + lastUpdatedAt: Date.now(), + }, + mode: args.mode, + state: "idle", + lastActivityAt: Date.now(), + }, + }; + }); + + hoisted.sessionBindingCapabilitiesMock.mockReset().mockReturnValue({ + adapterAvailable: true, + bindSupported: true, + unbindSupported: true, + placements: ["current", "child"], + }); + hoisted.sessionBindingBindMock + .mockReset() + .mockImplementation( + async (input: { + targetSessionKey: string; + conversation: { accountId: string }; + metadata?: Record; + }) => + createSessionBinding({ + targetSessionKey: input.targetSessionKey, + conversation: { + channel: "discord", + accountId: input.conversation.accountId, + conversationId: "child-thread", + parentConversationId: "parent-channel", + }, + metadata: { + boundBy: + typeof input.metadata?.boundBy === "string" ? input.metadata.boundBy : "system", + agentId: "codex", + webhookId: "wh-1", + }, + }), + ); + hoisted.sessionBindingResolveByConversationMock.mockReset().mockReturnValue(null); + hoisted.sessionBindingListBySessionMock.mockReset().mockReturnValue([]); + hoisted.sessionBindingUnbindMock.mockReset().mockResolvedValue([]); + }); + + it("spawns ACP session, binds a new thread, and dispatches initial task", async () => { + const result = await spawnAcpDirect( + { + task: "Investigate flaky tests", + agentId: "codex", + mode: "session", + thread: true, + }, + { + agentSessionKey: "agent:main:main", + agentChannel: "discord", + agentAccountId: "default", + agentTo: "channel:parent-channel", + agentThreadId: "requester-thread", + }, + ); + + expect(result.status).toBe("accepted"); + expect(result.childSessionKey).toMatch(/^agent:codex:acp:/); + expect(result.runId).toBe("run-1"); + expect(result.mode).toBe("session"); + expect(hoisted.sessionBindingBindMock).toHaveBeenCalledWith( + expect.objectContaining({ + targetKind: "session", + placement: "child", + }), + ); + expect(hoisted.sessionBindingBindMock).toHaveBeenCalledWith( + expect.objectContaining({ + metadata: expect.objectContaining({ + introText: expect.not.stringContaining( + "session ids: pending (available after the first reply)", + ), + }), + }), + ); + + const agentCall = hoisted.callGatewayMock.mock.calls + .map((call: unknown[]) => call[0] as { method?: string; params?: Record }) + .find((request) => request.method === "agent"); + expect(agentCall?.params?.sessionKey).toMatch(/^agent:codex:acp:/); + expect(agentCall?.params?.to).toBe("channel:child-thread"); + expect(agentCall?.params?.threadId).toBe("child-thread"); + expect(agentCall?.params?.deliver).toBe(true); + expect(hoisted.initializeSessionMock).toHaveBeenCalledWith( + expect.objectContaining({ + sessionKey: expect.stringMatching(/^agent:codex:acp:/), + agent: "codex", + mode: "persistent", + }), + ); + }); + + it("includes cwd in ACP thread intro banner when provided at spawn time", async () => { + const result = await spawnAcpDirect( + { + task: "Check workspace", + agentId: "codex", + cwd: "/home/bob/clawd", + mode: "session", + thread: true, + }, + { + agentSessionKey: "agent:main:main", + agentChannel: "discord", + agentAccountId: "default", + agentTo: "channel:parent-channel", + }, + ); + + expect(result.status).toBe("accepted"); + expect(hoisted.sessionBindingBindMock).toHaveBeenCalledWith( + expect.objectContaining({ + metadata: expect.objectContaining({ + introText: expect.stringContaining("cwd: /home/bob/clawd"), + }), + }), + ); + }); + + it("rejects disallowed ACP agents", async () => { + hoisted.state.cfg = { + ...hoisted.state.cfg, + acp: { + enabled: true, + backend: "acpx", + allowedAgents: ["claudecode"], + }, + }; + + const result = await spawnAcpDirect( + { + task: "hello", + agentId: "codex", + }, + { + agentSessionKey: "agent:main:main", + }, + ); + + expect(result).toMatchObject({ + status: "forbidden", + }); + }); + + it("requires an explicit ACP agent when no config default exists", async () => { + const result = await spawnAcpDirect( + { + task: "hello", + }, + { + agentSessionKey: "agent:main:main", + }, + ); + + expect(result.status).toBe("error"); + expect(result.error).toContain("set `acp.defaultAgent`"); + }); + + it("fails fast when Discord ACP thread spawn is disabled", async () => { + hoisted.state.cfg = { + ...hoisted.state.cfg, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: false, + }, + }, + }, + }; + + const result = await spawnAcpDirect( + { + task: "hello", + agentId: "codex", + thread: true, + mode: "session", + }, + { + agentChannel: "discord", + agentAccountId: "default", + agentTo: "channel:parent-channel", + }, + ); + + expect(result.status).toBe("error"); + expect(result.error).toContain("spawnAcpSessions=true"); + }); +}); diff --git a/src/agents/acp-spawn.ts b/src/agents/acp-spawn.ts new file mode 100644 index 000000000000..1ebd7b9d8563 --- /dev/null +++ b/src/agents/acp-spawn.ts @@ -0,0 +1,424 @@ +import crypto from "node:crypto"; +import { getAcpSessionManager } from "../acp/control-plane/manager.js"; +import { + cleanupFailedAcpSpawn, + type AcpSpawnRuntimeCloseHandle, +} from "../acp/control-plane/spawn.js"; +import { isAcpEnabledByPolicy, resolveAcpAgentPolicyError } from "../acp/policy.js"; +import { + resolveAcpSessionCwd, + resolveAcpThreadSessionDetailLines, +} from "../acp/runtime/session-identifiers.js"; +import type { AcpRuntimeSessionMode } from "../acp/runtime/types.js"; +import { + resolveThreadBindingIntroText, + resolveThreadBindingThreadName, +} from "../channels/thread-bindings-messages.js"; +import { + formatThreadBindingDisabledError, + formatThreadBindingSpawnDisabledError, + resolveThreadBindingSessionTtlMsForChannel, + resolveThreadBindingSpawnPolicy, +} from "../channels/thread-bindings-policy.js"; +import { loadConfig } from "../config/config.js"; +import type { OpenClawConfig } from "../config/config.js"; +import { callGateway } from "../gateway/call.js"; +import { resolveConversationIdFromTargets } from "../infra/outbound/conversation-id.js"; +import { + getSessionBindingService, + isSessionBindingError, + type SessionBindingRecord, +} from "../infra/outbound/session-binding-service.js"; +import { normalizeAgentId } from "../routing/session-key.js"; +import { normalizeDeliveryContext } from "../utils/delivery-context.js"; + +export const ACP_SPAWN_MODES = ["run", "session"] as const; +export type SpawnAcpMode = (typeof ACP_SPAWN_MODES)[number]; + +export type SpawnAcpParams = { + task: string; + label?: string; + agentId?: string; + cwd?: string; + mode?: SpawnAcpMode; + thread?: boolean; +}; + +export type SpawnAcpContext = { + agentSessionKey?: string; + agentChannel?: string; + agentAccountId?: string; + agentTo?: string; + agentThreadId?: string | number; +}; + +export type SpawnAcpResult = { + status: "accepted" | "forbidden" | "error"; + childSessionKey?: string; + runId?: string; + mode?: SpawnAcpMode; + note?: string; + error?: string; +}; + +export const ACP_SPAWN_ACCEPTED_NOTE = + "initial ACP task queued in isolated session; follow-ups continue in the bound thread."; +export const ACP_SPAWN_SESSION_ACCEPTED_NOTE = + "thread-bound ACP session stays active after this task; continue in-thread for follow-ups."; + +type PreparedAcpThreadBinding = { + channel: string; + accountId: string; + conversationId: string; +}; + +function resolveSpawnMode(params: { + requestedMode?: SpawnAcpMode; + threadRequested: boolean; +}): SpawnAcpMode { + if (params.requestedMode === "run" || params.requestedMode === "session") { + return params.requestedMode; + } + // Thread-bound spawns should default to persistent sessions. + return params.threadRequested ? "session" : "run"; +} + +function resolveAcpSessionMode(mode: SpawnAcpMode): AcpRuntimeSessionMode { + return mode === "session" ? "persistent" : "oneshot"; +} + +function resolveTargetAcpAgentId(params: { + requestedAgentId?: string; + cfg: OpenClawConfig; +}): { ok: true; agentId: string } | { ok: false; error: string } { + const requested = normalizeOptionalAgentId(params.requestedAgentId); + if (requested) { + return { ok: true, agentId: requested }; + } + + const configuredDefault = normalizeOptionalAgentId(params.cfg.acp?.defaultAgent); + if (configuredDefault) { + return { ok: true, agentId: configuredDefault }; + } + + return { + ok: false, + error: + "ACP target agent is not configured. Pass `agentId` in `sessions_spawn` or set `acp.defaultAgent` in config.", + }; +} + +function normalizeOptionalAgentId(value: string | undefined | null): string | undefined { + const trimmed = (value ?? "").trim(); + if (!trimmed) { + return undefined; + } + return normalizeAgentId(trimmed); +} + +function summarizeError(err: unknown): string { + if (err instanceof Error) { + return err.message; + } + if (typeof err === "string") { + return err; + } + return "error"; +} + +function resolveConversationIdForThreadBinding(params: { + to?: string; + threadId?: string | number; +}): string | undefined { + return resolveConversationIdFromTargets({ + threadId: params.threadId, + targets: [params.to], + }); +} + +function prepareAcpThreadBinding(params: { + cfg: OpenClawConfig; + channel?: string; + accountId?: string; + to?: string; + threadId?: string | number; +}): { ok: true; binding: PreparedAcpThreadBinding } | { ok: false; error: string } { + const channel = params.channel?.trim().toLowerCase(); + if (!channel) { + return { + ok: false, + error: "thread=true for ACP sessions requires a channel context.", + }; + } + + const accountId = params.accountId?.trim() || "default"; + const policy = resolveThreadBindingSpawnPolicy({ + cfg: params.cfg, + channel, + accountId, + kind: "acp", + }); + if (!policy.enabled) { + return { + ok: false, + error: formatThreadBindingDisabledError({ + channel: policy.channel, + accountId: policy.accountId, + kind: "acp", + }), + }; + } + if (!policy.spawnEnabled) { + return { + ok: false, + error: formatThreadBindingSpawnDisabledError({ + channel: policy.channel, + accountId: policy.accountId, + kind: "acp", + }), + }; + } + const bindingService = getSessionBindingService(); + const capabilities = bindingService.getCapabilities({ + channel: policy.channel, + accountId: policy.accountId, + }); + if (!capabilities.adapterAvailable) { + return { + ok: false, + error: `Thread bindings are unavailable for ${policy.channel}.`, + }; + } + if (!capabilities.bindSupported || !capabilities.placements.includes("child")) { + return { + ok: false, + error: `Thread bindings do not support ACP thread spawn for ${policy.channel}.`, + }; + } + const conversationId = resolveConversationIdForThreadBinding({ + to: params.to, + threadId: params.threadId, + }); + if (!conversationId) { + return { + ok: false, + error: `Could not resolve a ${policy.channel} conversation for ACP thread spawn.`, + }; + } + + return { + ok: true, + binding: { + channel: policy.channel, + accountId: policy.accountId, + conversationId, + }, + }; +} + +export async function spawnAcpDirect( + params: SpawnAcpParams, + ctx: SpawnAcpContext, +): Promise { + const cfg = loadConfig(); + if (!isAcpEnabledByPolicy(cfg)) { + return { + status: "forbidden", + error: "ACP is disabled by policy (`acp.enabled=false`).", + }; + } + + const requestThreadBinding = params.thread === true; + const spawnMode = resolveSpawnMode({ + requestedMode: params.mode, + threadRequested: requestThreadBinding, + }); + if (spawnMode === "session" && !requestThreadBinding) { + return { + status: "error", + error: 'mode="session" requires thread=true so the ACP session can stay bound to a thread.', + }; + } + + const targetAgentResult = resolveTargetAcpAgentId({ + requestedAgentId: params.agentId, + cfg, + }); + if (!targetAgentResult.ok) { + return { + status: "error", + error: targetAgentResult.error, + }; + } + const targetAgentId = targetAgentResult.agentId; + const agentPolicyError = resolveAcpAgentPolicyError(cfg, targetAgentId); + if (agentPolicyError) { + return { + status: "forbidden", + error: agentPolicyError.message, + }; + } + + const sessionKey = `agent:${targetAgentId}:acp:${crypto.randomUUID()}`; + const runtimeMode = resolveAcpSessionMode(spawnMode); + + let preparedBinding: PreparedAcpThreadBinding | null = null; + if (requestThreadBinding) { + const prepared = prepareAcpThreadBinding({ + cfg, + channel: ctx.agentChannel, + accountId: ctx.agentAccountId, + to: ctx.agentTo, + threadId: ctx.agentThreadId, + }); + if (!prepared.ok) { + return { + status: "error", + error: prepared.error, + }; + } + preparedBinding = prepared.binding; + } + + const acpManager = getAcpSessionManager(); + const bindingService = getSessionBindingService(); + let binding: SessionBindingRecord | null = null; + let sessionCreated = false; + let initializedRuntime: AcpSpawnRuntimeCloseHandle | undefined; + try { + await callGateway({ + method: "sessions.patch", + params: { + key: sessionKey, + ...(params.label ? { label: params.label } : {}), + }, + timeoutMs: 10_000, + }); + sessionCreated = true; + const initialized = await acpManager.initializeSession({ + cfg, + sessionKey, + agent: targetAgentId, + mode: runtimeMode, + cwd: params.cwd, + backendId: cfg.acp?.backend, + }); + initializedRuntime = { + runtime: initialized.runtime, + handle: initialized.handle, + }; + + if (preparedBinding) { + binding = await bindingService.bind({ + targetSessionKey: sessionKey, + targetKind: "session", + conversation: { + channel: preparedBinding.channel, + accountId: preparedBinding.accountId, + conversationId: preparedBinding.conversationId, + }, + placement: "child", + metadata: { + threadName: resolveThreadBindingThreadName({ + agentId: targetAgentId, + label: params.label || targetAgentId, + }), + agentId: targetAgentId, + label: params.label || undefined, + boundBy: "system", + introText: resolveThreadBindingIntroText({ + agentId: targetAgentId, + label: params.label || undefined, + sessionTtlMs: resolveThreadBindingSessionTtlMsForChannel({ + cfg, + channel: preparedBinding.channel, + accountId: preparedBinding.accountId, + }), + sessionCwd: resolveAcpSessionCwd(initialized.meta), + sessionDetails: resolveAcpThreadSessionDetailLines({ + sessionKey, + meta: initialized.meta, + }), + }), + }, + }); + if (!binding?.conversation.conversationId) { + throw new Error( + `Failed to create and bind a ${preparedBinding.channel} thread for this ACP session.`, + ); + } + } + } catch (err) { + await cleanupFailedAcpSpawn({ + cfg, + sessionKey, + shouldDeleteSession: sessionCreated, + deleteTranscript: true, + runtimeCloseHandle: initializedRuntime, + }); + return { + status: "error", + error: isSessionBindingError(err) ? err.message : summarizeError(err), + }; + } + + const requesterOrigin = normalizeDeliveryContext({ + channel: ctx.agentChannel, + accountId: ctx.agentAccountId, + to: ctx.agentTo, + threadId: ctx.agentThreadId, + }); + // For thread-bound ACP spawns, force bootstrap delivery to the new child thread. + const boundThreadIdRaw = binding?.conversation.conversationId; + const boundThreadId = boundThreadIdRaw ? String(boundThreadIdRaw).trim() || undefined : undefined; + const fallbackThreadIdRaw = requesterOrigin?.threadId; + const fallbackThreadId = + fallbackThreadIdRaw != null ? String(fallbackThreadIdRaw).trim() || undefined : undefined; + const deliveryThreadId = boundThreadId ?? fallbackThreadId; + const inferredDeliveryTo = boundThreadId + ? `channel:${boundThreadId}` + : requesterOrigin?.to?.trim() || (deliveryThreadId ? `channel:${deliveryThreadId}` : undefined); + const hasDeliveryTarget = Boolean(requesterOrigin?.channel && inferredDeliveryTo); + const childIdem = crypto.randomUUID(); + let childRunId: string = childIdem; + try { + const response = await callGateway<{ runId?: string }>({ + method: "agent", + params: { + message: params.task, + sessionKey, + channel: hasDeliveryTarget ? requesterOrigin?.channel : undefined, + to: hasDeliveryTarget ? inferredDeliveryTo : undefined, + accountId: hasDeliveryTarget ? (requesterOrigin?.accountId ?? undefined) : undefined, + threadId: hasDeliveryTarget ? deliveryThreadId : undefined, + idempotencyKey: childIdem, + deliver: hasDeliveryTarget, + label: params.label || undefined, + }, + timeoutMs: 10_000, + }); + if (typeof response?.runId === "string" && response.runId.trim()) { + childRunId = response.runId.trim(); + } + } catch (err) { + await cleanupFailedAcpSpawn({ + cfg, + sessionKey, + shouldDeleteSession: true, + deleteTranscript: true, + }); + return { + status: "error", + error: summarizeError(err), + childSessionKey: sessionKey, + }; + } + + return { + status: "accepted", + childSessionKey: sessionKey, + runId: childRunId, + mode: spawnMode, + note: spawnMode === "session" ? ACP_SPAWN_SESSION_ACCEPTED_NOTE : ACP_SPAWN_ACCEPTED_NOTE, + }; +} diff --git a/src/agents/cli-runner/helpers.ts b/src/agents/cli-runner/helpers.ts index e211e3df49c5..dbabca75faaa 100644 --- a/src/agents/cli-runner/helpers.ts +++ b/src/agents/cli-runner/helpers.ts @@ -93,6 +93,7 @@ export function buildSystemPrompt(params: { reasoningTagHint: false, heartbeatPrompt: params.heartbeatPrompt, docsPath: params.docsPath, + acpEnabled: params.config?.acp?.enabled !== false, runtimeInfo, toolNames: params.tools.map((tool) => tool.name), modelAliasLines: buildModelAliasLines(params.config), diff --git a/src/agents/openclaw-tools.sessions.test.ts b/src/agents/openclaw-tools.sessions.test.ts index 42a3210fa801..753426a4c516 100644 --- a/src/agents/openclaw-tools.sessions.test.ts +++ b/src/agents/openclaw-tools.sessions.test.ts @@ -91,6 +91,8 @@ describe("sessions tools", () => { expect(schemaProp("sessions_spawn", "runTimeoutSeconds").type).toBe("number"); expect(schemaProp("sessions_spawn", "thread").type).toBe("boolean"); expect(schemaProp("sessions_spawn", "mode").type).toBe("string"); + expect(schemaProp("sessions_spawn", "runtime").type).toBe("string"); + expect(schemaProp("sessions_spawn", "cwd").type).toBe("string"); expect(schemaProp("subagents", "recentMinutes").type).toBe("number"); }); diff --git a/src/agents/pi-embedded-runner/compact.ts b/src/agents/pi-embedded-runner/compact.ts index 9734c73be459..388cb125a241 100644 --- a/src/agents/pi-embedded-runner/compact.ts +++ b/src/agents/pi-embedded-runner/compact.ts @@ -499,6 +499,7 @@ export async function compactEmbeddedPiSessionDirect( docsPath: docsPath ?? undefined, ttsHint, promptMode, + acpEnabled: params.config?.acp?.enabled !== false, runtimeInfo, reactionGuidance, messageToolHints, diff --git a/src/agents/pi-embedded-runner/run/attempt.ts b/src/agents/pi-embedded-runner/run/attempt.ts index 25d8528fc48a..64c9e23170c7 100644 --- a/src/agents/pi-embedded-runner/run/attempt.ts +++ b/src/agents/pi-embedded-runner/run/attempt.ts @@ -545,6 +545,7 @@ export async function runEmbeddedAttempt( workspaceNotes, reactionGuidance, promptMode, + acpEnabled: params.config?.acp?.enabled !== false, runtimeInfo, messageToolHints, sandboxInfo, diff --git a/src/agents/pi-embedded-runner/system-prompt.ts b/src/agents/pi-embedded-runner/system-prompt.ts index 67df44936952..ef246d1af23e 100644 --- a/src/agents/pi-embedded-runner/system-prompt.ts +++ b/src/agents/pi-embedded-runner/system-prompt.ts @@ -28,6 +28,8 @@ export function buildEmbeddedSystemPrompt(params: { workspaceNotes?: string[]; /** Controls which hardcoded sections to include. Defaults to "full". */ promptMode?: PromptMode; + /** Whether ACP-specific routing guidance should be included. Defaults to true. */ + acpEnabled?: boolean; runtimeInfo: { agentId?: string; host: string; @@ -67,6 +69,7 @@ export function buildEmbeddedSystemPrompt(params: { workspaceNotes: params.workspaceNotes, reactionGuidance: params.reactionGuidance, promptMode: params.promptMode, + acpEnabled: params.acpEnabled, runtimeInfo: params.runtimeInfo, messageToolHints: params.messageToolHints, sandboxInfo: params.sandboxInfo, diff --git a/src/agents/skills/plugin-skills.test.ts b/src/agents/skills/plugin-skills.test.ts new file mode 100644 index 000000000000..4747d59bf5c9 --- /dev/null +++ b/src/agents/skills/plugin-skills.test.ts @@ -0,0 +1,103 @@ +import fs from "node:fs/promises"; +import path from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { OpenClawConfig } from "../../config/config.js"; +import type { PluginManifestRegistry } from "../../plugins/manifest-registry.js"; +import { createTrackedTempDirs } from "../../test-utils/tracked-temp-dirs.js"; + +const hoisted = vi.hoisted(() => ({ + loadPluginManifestRegistry: vi.fn(), +})); + +vi.mock("../../plugins/manifest-registry.js", () => ({ + loadPluginManifestRegistry: (...args: unknown[]) => hoisted.loadPluginManifestRegistry(...args), +})); + +const { resolvePluginSkillDirs } = await import("./plugin-skills.js"); + +const tempDirs = createTrackedTempDirs(); + +function buildRegistry(params: { acpxRoot: string; helperRoot: string }): PluginManifestRegistry { + return { + diagnostics: [], + plugins: [ + { + id: "acpx", + name: "ACPX Runtime", + channels: [], + providers: [], + skills: ["./skills"], + origin: "workspace", + rootDir: params.acpxRoot, + source: params.acpxRoot, + manifestPath: path.join(params.acpxRoot, "openclaw.plugin.json"), + }, + { + id: "helper", + name: "Helper", + channels: [], + providers: [], + skills: ["./skills"], + origin: "workspace", + rootDir: params.helperRoot, + source: params.helperRoot, + manifestPath: path.join(params.helperRoot, "openclaw.plugin.json"), + }, + ], + }; +} + +afterEach(async () => { + hoisted.loadPluginManifestRegistry.mockReset(); + await tempDirs.cleanup(); +}); + +describe("resolvePluginSkillDirs", () => { + it("keeps acpx plugin skills when ACP is enabled", async () => { + const workspaceDir = await tempDirs.make("openclaw-"); + const acpxRoot = await tempDirs.make("openclaw-acpx-plugin-"); + const helperRoot = await tempDirs.make("openclaw-helper-plugin-"); + await fs.mkdir(path.join(acpxRoot, "skills"), { recursive: true }); + await fs.mkdir(path.join(helperRoot, "skills"), { recursive: true }); + + hoisted.loadPluginManifestRegistry.mockReturnValue( + buildRegistry({ + acpxRoot, + helperRoot, + }), + ); + + const dirs = resolvePluginSkillDirs({ + workspaceDir, + config: { + acp: { enabled: true }, + } as OpenClawConfig, + }); + + expect(dirs).toEqual([path.resolve(acpxRoot, "skills"), path.resolve(helperRoot, "skills")]); + }); + + it("skips acpx plugin skills when ACP is disabled", async () => { + const workspaceDir = await tempDirs.make("openclaw-"); + const acpxRoot = await tempDirs.make("openclaw-acpx-plugin-"); + const helperRoot = await tempDirs.make("openclaw-helper-plugin-"); + await fs.mkdir(path.join(acpxRoot, "skills"), { recursive: true }); + await fs.mkdir(path.join(helperRoot, "skills"), { recursive: true }); + + hoisted.loadPluginManifestRegistry.mockReturnValue( + buildRegistry({ + acpxRoot, + helperRoot, + }), + ); + + const dirs = resolvePluginSkillDirs({ + workspaceDir, + config: { + acp: { enabled: false }, + } as OpenClawConfig, + }); + + expect(dirs).toEqual([path.resolve(helperRoot, "skills")]); + }); +}); diff --git a/src/agents/skills/plugin-skills.ts b/src/agents/skills/plugin-skills.ts index 90c8711cd744..594bfcdabb3f 100644 --- a/src/agents/skills/plugin-skills.ts +++ b/src/agents/skills/plugin-skills.ts @@ -27,6 +27,7 @@ export function resolvePluginSkillDirs(params: { return []; } const normalizedPlugins = normalizePluginsConfig(params.config?.plugins); + const acpEnabled = params.config?.acp?.enabled !== false; const memorySlot = normalizedPlugins.slots.memory; let selectedMemoryPluginId: string | null = null; const seen = new Set(); @@ -45,6 +46,10 @@ export function resolvePluginSkillDirs(params: { if (!enableState.enabled) { continue; } + // ACP router skills should not be attached when ACP is explicitly disabled. + if (!acpEnabled && record.id === "acpx") { + continue; + } const memoryDecision = resolveMemorySlotDecision({ id: record.id, kind: record.kind, diff --git a/src/agents/subagent-announce.ts b/src/agents/subagent-announce.ts index c99a6cb65939..0d2f961c01e4 100644 --- a/src/agents/subagent-announce.ts +++ b/src/agents/subagent-announce.ts @@ -924,6 +924,8 @@ export function buildSubagentSystemPrompt(params: { childSessionKey: string; label?: string; task?: string; + /** Whether ACP-specific routing guidance should be included. Defaults to true. */ + acpEnabled?: boolean; /** Depth of the child being spawned (1 = sub-agent, 2 = sub-sub-agent). */ childDepth?: number; /** Config value: max allowed spawn depth. */ @@ -938,6 +940,7 @@ export function buildSubagentSystemPrompt(params: { typeof params.maxSpawnDepth === "number" ? params.maxSpawnDepth : DEFAULT_SUBAGENT_MAX_SPAWN_DEPTH; + const acpEnabled = params.acpEnabled !== false; const canSpawn = childDepth < maxSpawnDepth; const parentLabel = childDepth >= 2 ? "parent orchestrator" : "main agent"; @@ -983,6 +986,17 @@ export function buildSubagentSystemPrompt(params: { "Default workflow: spawn work, continue orchestrating, and wait for auto-announced completions.", "Do NOT repeatedly poll `subagents list` in a loop unless you are actively debugging or intervening.", "Coordinate their work and synthesize results before reporting back.", + ...(acpEnabled + ? [ + 'For ACP harness sessions (codex/claudecode/gemini), use `sessions_spawn` with `runtime: "acp"` (set `agentId` unless `acp.defaultAgent` is configured).', + '`agents_list` and `subagents` apply to OpenClaw sub-agents (`runtime: "subagent"`); ACP harness ids are controlled by `acp.allowedAgents`.', + "Do not ask users to run slash commands or CLI when `sessions_spawn` can do it directly.", + "Do not use `exec` (`openclaw ...`, `acpx ...`) to spawn ACP sessions.", + 'Use `subagents` only for OpenClaw subagents (`runtime: "subagent"`).', + "Subagent results auto-announce back to you; ACP sessions continue in their bound thread.", + "Avoid polling loops; spawn, orchestrate, and synthesize results.", + ] + : []), "", ); } else if (childDepth >= 2) { diff --git a/src/agents/subagent-registry.lifecycle-retry-grace.test.ts b/src/agents/subagent-registry.lifecycle-retry-grace.test.ts new file mode 100644 index 000000000000..7f919c4fd49f --- /dev/null +++ b/src/agents/subagent-registry.lifecycle-retry-grace.test.ts @@ -0,0 +1,157 @@ +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; + +const noop = () => {}; + +let lifecycleHandler: + | ((evt: { + stream?: string; + runId: string; + data?: { + phase?: string; + startedAt?: number; + endedAt?: number; + aborted?: boolean; + error?: string; + }; + }) => void) + | undefined; + +vi.mock("../gateway/call.js", () => ({ + callGateway: vi.fn(async (request: unknown) => { + const method = (request as { method?: string }).method; + if (method === "agent.wait") { + // Keep wait unresolved from the RPC path so lifecycle fallback logic is exercised. + return { status: "pending" }; + } + return {}; + }), +})); + +vi.mock("../infra/agent-events.js", () => ({ + onAgentEvent: vi.fn((handler: typeof lifecycleHandler) => { + lifecycleHandler = handler; + return noop; + }), +})); + +vi.mock("../config/config.js", () => ({ + loadConfig: vi.fn(() => ({ + agents: { defaults: { subagents: { archiveAfterMinutes: 0 } } }, + })), +})); + +const announceSpy = vi.fn(async () => true); +vi.mock("./subagent-announce.js", () => ({ + runSubagentAnnounceFlow: announceSpy, +})); + +vi.mock("../plugins/hook-runner-global.js", () => ({ + getGlobalHookRunner: vi.fn(() => null), +})); + +vi.mock("./subagent-registry.store.js", () => ({ + loadSubagentRegistryFromDisk: vi.fn(() => new Map()), + saveSubagentRegistryToDisk: vi.fn(() => {}), +})); + +describe("subagent registry lifecycle error grace", () => { + let mod: typeof import("./subagent-registry.js"); + + beforeAll(async () => { + mod = await import("./subagent-registry.js"); + }); + + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + announceSpy.mockClear(); + lifecycleHandler = undefined; + mod.resetSubagentRegistryForTests({ persist: false }); + vi.useRealTimers(); + }); + + const flushAsync = async () => { + await Promise.resolve(); + await Promise.resolve(); + }; + + it("ignores transient lifecycle errors when run retries and then ends successfully", async () => { + mod.registerSubagentRun({ + runId: "run-transient-error", + childSessionKey: "agent:main:subagent:transient-error", + requesterSessionKey: "agent:main:main", + requesterDisplayKey: "main", + task: "transient error test", + cleanup: "keep", + expectsCompletionMessage: true, + }); + + lifecycleHandler?.({ + stream: "lifecycle", + runId: "run-transient-error", + data: { phase: "error", error: "rate limit", endedAt: 1_000 }, + }); + await flushAsync(); + expect(announceSpy).not.toHaveBeenCalled(); + + await vi.advanceTimersByTimeAsync(14_999); + expect(announceSpy).not.toHaveBeenCalled(); + + lifecycleHandler?.({ + stream: "lifecycle", + runId: "run-transient-error", + data: { phase: "start", startedAt: 1_050 }, + }); + await flushAsync(); + + await vi.advanceTimersByTimeAsync(20_000); + expect(announceSpy).not.toHaveBeenCalled(); + + lifecycleHandler?.({ + stream: "lifecycle", + runId: "run-transient-error", + data: { phase: "end", endedAt: 1_250 }, + }); + await flushAsync(); + + expect(announceSpy).toHaveBeenCalledTimes(1); + const announceCalls = announceSpy.mock.calls as unknown as Array>; + const first = (announceCalls[0]?.[0] ?? {}) as { + outcome?: { status?: string; error?: string }; + }; + expect(first.outcome?.status).toBe("ok"); + }); + + it("announces error when lifecycle error remains terminal after grace window", async () => { + mod.registerSubagentRun({ + runId: "run-terminal-error", + childSessionKey: "agent:main:subagent:terminal-error", + requesterSessionKey: "agent:main:main", + requesterDisplayKey: "main", + task: "terminal error test", + cleanup: "keep", + expectsCompletionMessage: true, + }); + + lifecycleHandler?.({ + stream: "lifecycle", + runId: "run-terminal-error", + data: { phase: "error", error: "fatal failure", endedAt: 2_000 }, + }); + await flushAsync(); + expect(announceSpy).not.toHaveBeenCalled(); + + await vi.advanceTimersByTimeAsync(15_000); + await flushAsync(); + + expect(announceSpy).toHaveBeenCalledTimes(1); + const announceCalls = announceSpy.mock.calls as unknown as Array>; + const first = (announceCalls[0]?.[0] ?? {}) as { + outcome?: { status?: string; error?: string }; + }; + expect(first.outcome?.status).toBe("error"); + expect(first.outcome?.error).toBe("fatal failure"); + }); +}); diff --git a/src/agents/subagent-registry.ts b/src/agents/subagent-registry.ts index 072fd91693fc..10a6416f4cef 100644 --- a/src/agents/subagent-registry.ts +++ b/src/agents/subagent-registry.ts @@ -66,6 +66,12 @@ const MAX_ANNOUNCE_RETRY_COUNT = 3; */ const ANNOUNCE_EXPIRY_MS = 5 * 60_000; // 5 minutes type SubagentRunOrphanReason = "missing-session-entry" | "missing-session-id"; +/** + * Embedded runs can emit transient lifecycle `error` events while provider/model + * retry is still in progress. Defer terminal error cleanup briefly so a + * subsequent lifecycle `start` / `end` can cancel premature failure announces. + */ +const LIFECYCLE_ERROR_RETRY_GRACE_MS = 15_000; function resolveAnnounceRetryDelayMs(retryCount: number) { const boundedRetryCount = Math.max(0, Math.min(retryCount, 10)); @@ -204,6 +210,66 @@ function reconcileOrphanedRestoredRuns() { const resumedRuns = new Set(); const endedHookInFlightRunIds = new Set(); +const pendingLifecycleErrorByRunId = new Map< + string, + { + timer: NodeJS.Timeout; + endedAt: number; + error?: string; + } +>(); + +function clearPendingLifecycleError(runId: string) { + const pending = pendingLifecycleErrorByRunId.get(runId); + if (!pending) { + return; + } + clearTimeout(pending.timer); + pendingLifecycleErrorByRunId.delete(runId); +} + +function clearAllPendingLifecycleErrors() { + for (const pending of pendingLifecycleErrorByRunId.values()) { + clearTimeout(pending.timer); + } + pendingLifecycleErrorByRunId.clear(); +} + +function schedulePendingLifecycleError(params: { runId: string; endedAt: number; error?: string }) { + clearPendingLifecycleError(params.runId); + const timer = setTimeout(() => { + const pending = pendingLifecycleErrorByRunId.get(params.runId); + if (!pending || pending.timer !== timer) { + return; + } + pendingLifecycleErrorByRunId.delete(params.runId); + const entry = subagentRuns.get(params.runId); + if (!entry) { + return; + } + if (entry.endedReason === SUBAGENT_ENDED_REASON_COMPLETE || entry.outcome?.status === "ok") { + return; + } + void completeSubagentRun({ + runId: params.runId, + endedAt: pending.endedAt, + outcome: { + status: "error", + error: pending.error, + }, + reason: SUBAGENT_ENDED_REASON_ERROR, + sendFarewell: true, + accountId: entry.requesterOrigin?.accountId, + triggerCleanup: true, + }); + }, LIFECYCLE_ERROR_RETRY_GRACE_MS); + timer.unref?.(); + pendingLifecycleErrorByRunId.set(params.runId, { + timer, + endedAt: params.endedAt, + error: params.error, + }); +} function suppressAnnounceForSteerRestart(entry?: SubagentRunRecord) { return entry?.suppressAnnounceReason === "steer-restart"; @@ -256,6 +322,7 @@ async function completeSubagentRun(params: { accountId?: string; triggerCleanup: boolean; }) { + clearPendingLifecycleError(params.runId); const entry = subagentRuns.get(params.runId); if (!entry) { return; @@ -491,6 +558,7 @@ async function sweepSubagentRuns() { if (!entry.archiveAtMs || entry.archiveAtMs > now) { continue; } + clearPendingLifecycleError(runId); subagentRuns.delete(runId); mutated = true; try { @@ -531,6 +599,7 @@ function ensureListener() { } const phase = evt.data?.phase; if (phase === "start") { + clearPendingLifecycleError(evt.runId); const startedAt = typeof evt.data?.startedAt === "number" ? evt.data.startedAt : undefined; if (startedAt) { entry.startedAt = startedAt; @@ -543,17 +612,23 @@ function ensureListener() { } const endedAt = typeof evt.data?.endedAt === "number" ? evt.data.endedAt : Date.now(); const error = typeof evt.data?.error === "string" ? evt.data.error : undefined; - const outcome: SubagentRunOutcome = - phase === "error" - ? { status: "error", error } - : evt.data?.aborted - ? { status: "timeout" } - : { status: "ok" }; + if (phase === "error") { + schedulePendingLifecycleError({ + runId: evt.runId, + endedAt, + error, + }); + return; + } + clearPendingLifecycleError(evt.runId); + const outcome: SubagentRunOutcome = evt.data?.aborted + ? { status: "timeout" } + : { status: "ok" }; await completeSubagentRun({ runId: evt.runId, endedAt, outcome, - reason: phase === "error" ? SUBAGENT_ENDED_REASON_ERROR : SUBAGENT_ENDED_REASON_COMPLETE, + reason: SUBAGENT_ENDED_REASON_COMPLETE, sendFarewell: true, accountId: entry.requesterOrigin?.accountId, triggerCleanup: true, @@ -661,6 +736,7 @@ function completeCleanupBookkeeping(params: { completedAt: number; }) { if (params.cleanup === "delete") { + clearPendingLifecycleError(params.runId); subagentRuns.delete(params.runId); persistSubagentRuns(); retryDeferredCompletedAnnounces(params.runId); @@ -774,6 +850,7 @@ export function replaceSubagentRunAfterSteer(params: { } if (previousRunId !== nextRunId) { + clearPendingLifecycleError(previousRunId); subagentRuns.delete(previousRunId); resumedRuns.delete(previousRunId); } @@ -935,6 +1012,7 @@ export function resetSubagentRegistryForTests(opts?: { persist?: boolean }) { subagentRuns.clear(); resumedRuns.clear(); endedHookInFlightRunIds.clear(); + clearAllPendingLifecycleErrors(); resetAnnounceQueuesForTests(); stopSweeper(); restoreAttempted = false; @@ -953,6 +1031,7 @@ export function addSubagentRunForTests(entry: SubagentRunRecord) { } export function releaseSubagentRun(runId: string) { + clearPendingLifecycleError(runId); const didDelete = subagentRuns.delete(runId); if (didDelete) { persistSubagentRuns(); @@ -1020,6 +1099,7 @@ export function markSubagentRunTerminated(params: { let updated = 0; const entriesByChildSessionKey = new Map(); for (const runId of runIds) { + clearPendingLifecycleError(runId); const entry = subagentRuns.get(runId); if (!entry) { continue; diff --git a/src/agents/subagent-spawn.ts b/src/agents/subagent-spawn.ts index 7d4f672f2f1e..37b612145ed1 100644 --- a/src/agents/subagent-spawn.ts +++ b/src/agents/subagent-spawn.ts @@ -385,6 +385,7 @@ export async function spawnSubagentDirect( childSessionKey, label: label || undefined, task, + acpEnabled: cfg.acp?.enabled !== false, childDepth, maxSpawnDepth, }); diff --git a/src/agents/system-prompt.test.ts b/src/agents/system-prompt.test.ts index b45c64e72eca..01cdfb2cc3a9 100644 --- a/src/agents/system-prompt.test.ts +++ b/src/agents/system-prompt.test.ts @@ -221,6 +221,9 @@ describe("buildAgentSystemPrompt", () => { ); expect(prompt).toContain("Completion is push-based: it will auto-announce when done."); expect(prompt).toContain("Do not poll `subagents list` / `sessions_list` in a loop"); + expect(prompt).toContain( + "When a first-class tool exists for an action, use the tool directly instead of asking the user to run equivalent CLI or slash commands.", + ); }); it("lists available tools when provided", () => { @@ -235,6 +238,52 @@ describe("buildAgentSystemPrompt", () => { expect(prompt).toContain("sessions_send"); }); + it("documents ACP sessions_spawn agent targeting requirements", () => { + const prompt = buildAgentSystemPrompt({ + workspaceDir: "/tmp/openclaw", + toolNames: ["sessions_spawn"], + }); + + expect(prompt).toContain("sessions_spawn"); + expect(prompt).toContain( + 'runtime="acp" requires `agentId` unless `acp.defaultAgent` is configured', + ); + expect(prompt).toContain("not agents_list"); + }); + + it("guides harness requests to ACP thread-bound spawns", () => { + const prompt = buildAgentSystemPrompt({ + workspaceDir: "/tmp/openclaw", + toolNames: ["sessions_spawn", "subagents", "agents_list", "exec"], + }); + + expect(prompt).toContain( + 'For requests like "do this in codex/claude code/gemini", treat it as ACP harness intent', + ); + expect(prompt).toContain( + 'On Discord, default ACP harness requests to thread-bound persistent sessions (`thread: true`, `mode: "session"`)', + ); + expect(prompt).toContain( + "do not route ACP harness requests through `subagents`/`agents_list` or local PTY exec flows", + ); + }); + + it("omits ACP harness guidance when ACP is disabled", () => { + const prompt = buildAgentSystemPrompt({ + workspaceDir: "/tmp/openclaw", + toolNames: ["sessions_spawn", "subagents", "agents_list", "exec"], + acpEnabled: false, + }); + + expect(prompt).not.toContain( + 'For requests like "do this in codex/claude code/gemini", treat it as ACP harness intent', + ); + expect(prompt).not.toContain('runtime="acp" requires `agentId`'); + expect(prompt).not.toContain("not ACP harness ids"); + expect(prompt).toContain("- sessions_spawn: Spawn an isolated sub-agent session"); + expect(prompt).toContain("- agents_list: List OpenClaw agent ids allowed for sessions_spawn"); + }); + it("preserves tool casing in the prompt", () => { const prompt = buildAgentSystemPrompt({ workspaceDir: "/tmp/openclaw", @@ -599,11 +648,18 @@ describe("buildSubagentSystemPrompt", () => { }); expect(prompt).toContain("## Sub-Agent Spawning"); - expect(prompt).toContain("You CAN spawn your own sub-agents"); + expect(prompt).toContain( + "You CAN spawn your own sub-agents for parallel or complex work using `sessions_spawn`.", + ); expect(prompt).toContain("sessions_spawn"); - expect(prompt).toContain("`subagents` tool"); - expect(prompt).toContain("announce their results back to you automatically"); - expect(prompt).toContain("Do NOT repeatedly poll `subagents list`"); + expect(prompt).toContain('runtime: "acp"'); + expect(prompt).toContain("For ACP harness sessions (codex/claudecode/gemini)"); + expect(prompt).toContain("set `agentId` unless `acp.defaultAgent` is configured"); + expect(prompt).toContain("Do not ask users to run slash commands or CLI"); + expect(prompt).toContain("Do not use `exec` (`openclaw ...`, `acpx ...`)"); + expect(prompt).toContain("Use `subagents` only for OpenClaw subagents"); + expect(prompt).toContain("Subagent results auto-announce back to you"); + expect(prompt).toContain("Avoid polling loops"); expect(prompt).toContain("spawned by the main agent"); expect(prompt).toContain("reported to the main agent"); expect(prompt).toContain("[compacted: tool output removed to free context]"); @@ -612,6 +668,21 @@ describe("buildSubagentSystemPrompt", () => { expect(prompt).toContain("instead of full-file `cat`"); }); + it("omits ACP spawning guidance when ACP is disabled", () => { + const prompt = buildSubagentSystemPrompt({ + childSessionKey: "agent:main:subagent:abc", + task: "research task", + childDepth: 1, + maxSpawnDepth: 2, + acpEnabled: false, + }); + + expect(prompt).not.toContain('runtime: "acp"'); + expect(prompt).not.toContain("For ACP harness sessions (codex/claudecode/gemini)"); + expect(prompt).not.toContain("set `agentId` unless `acp.defaultAgent` is configured"); + expect(prompt).toContain("You CAN spawn your own sub-agents"); + }); + it("renders depth-2 leaf guidance with parent orchestrator labels", () => { const prompt = buildSubagentSystemPrompt({ childSessionKey: "agent:main:subagent:abc:subagent:def", diff --git a/src/agents/system-prompt.ts b/src/agents/system-prompt.ts index c8b229a198a4..3b3453be6f77 100644 --- a/src/agents/system-prompt.ts +++ b/src/agents/system-prompt.ts @@ -209,6 +209,8 @@ export function buildAgentSystemPrompt(params: { ttsHint?: string; /** Controls which hardcoded sections to include. Defaults to "full". */ promptMode?: PromptMode; + /** Whether ACP-specific routing guidance should be included. Defaults to true. */ + acpEnabled?: boolean; runtimeInfo?: { agentId?: string; host?: string; @@ -231,6 +233,7 @@ export function buildAgentSystemPrompt(params: { }; memoryCitationsMode?: MemoryCitationsMode; }) { + const acpEnabled = params.acpEnabled !== false; const coreToolSummaries: Record = { read: "Read file contents", write: "Create or overwrite files", @@ -250,11 +253,15 @@ export function buildAgentSystemPrompt(params: { cron: "Manage cron jobs and wake events (use for reminders; when scheduling a reminder, write the systemEvent text as something that will read like a reminder when it fires, and mention that it is a reminder depending on the time gap between setting and firing; include recent context in reminder text if appropriate)", message: "Send messages and channel actions", gateway: "Restart, apply config, or run updates on the running OpenClaw process", - agents_list: "List agent ids allowed for sessions_spawn", + agents_list: acpEnabled + ? 'List OpenClaw agent ids allowed for sessions_spawn when runtime="subagent" (not ACP harness ids)' + : "List OpenClaw agent ids allowed for sessions_spawn", sessions_list: "List other sessions (incl. sub-agents) with filters/last", sessions_history: "Fetch history for another session/sub-agent", sessions_send: "Send a message to another session/sub-agent", - sessions_spawn: "Spawn a sub-agent session", + sessions_spawn: acpEnabled + ? 'Spawn an isolated sub-agent or ACP coding session (runtime="acp" requires `agentId` unless `acp.defaultAgent` is configured; ACP harness ids follow acp.allowedAgents, not agents_list)' + : "Spawn an isolated sub-agent session", subagents: "List, steer, or kill sub-agent runs for this requester session", session_status: "Show a /status-equivalent status card (usage + time + Reasoning/Verbose/Elevated); use for model-use questions (📊 session_status); optional per-session model override", @@ -303,6 +310,7 @@ export function buildAgentSystemPrompt(params: { const normalizedTools = canonicalToolNames.map((tool) => tool.toLowerCase()); const availableTools = new Set(normalizedTools); + const hasSessionsSpawn = availableTools.has("sessions_spawn"); const externalToolSummaries = new Map(); for (const [key, value] of Object.entries(params.toolSummaries ?? {})) { const normalized = key.trim().toLowerCase(); @@ -436,6 +444,13 @@ export function buildAgentSystemPrompt(params: { "TOOLS.md does not control tool availability; it is user guidance for how to use external tools.", `For long waits, avoid rapid poll loops: use ${execToolName} with enough yieldMs or ${processToolName}(action=poll, timeout=).`, "If a task is more complex or takes longer, spawn a sub-agent. Completion is push-based: it will auto-announce when done.", + ...(hasSessionsSpawn && acpEnabled + ? [ + 'For requests like "do this in codex/claude code/gemini", treat it as ACP harness intent and call `sessions_spawn` with `runtime: "acp"`.', + 'On Discord, default ACP harness requests to thread-bound persistent sessions (`thread: true`, `mode: "session"`) unless the user asks otherwise.', + "Set `agentId` explicitly unless `acp.defaultAgent` is configured, and do not route ACP harness requests through `subagents`/`agents_list` or local PTY exec flows.", + ] + : []), "Do not poll `subagents list` / `sessions_list` in a loop; only check status on-demand (for intervention, debugging, or when explicitly asked).", "", "## Tool Call Style", @@ -443,6 +458,7 @@ export function buildAgentSystemPrompt(params: { "Narrate only when it helps: multi-step work, complex/challenging problems, sensitive actions (e.g., deletions), or when the user explicitly asks.", "Keep narration brief and value-dense; avoid repeating obvious steps.", "Use plain human language for narration unless in a technical context.", + "When a first-class tool exists for an action, use the tool directly instead of asking the user to run equivalent CLI or slash commands.", "", ...safetySection, "## OpenClaw CLI Quick Reference", diff --git a/src/agents/tools/agents-list-tool.ts b/src/agents/tools/agents-list-tool.ts index 277ac990647d..879ad96de068 100644 --- a/src/agents/tools/agents-list-tool.ts +++ b/src/agents/tools/agents-list-tool.ts @@ -26,7 +26,8 @@ export function createAgentsListTool(opts?: { return { label: "Agents", name: "agents_list", - description: "List agent ids you can target with sessions_spawn (based on allowlists).", + description: + 'List OpenClaw agent ids you can target with `sessions_spawn` when `runtime="subagent"` (based on subagent allowlists).', parameters: AgentsListToolSchema, execute: async () => { const cfg = loadConfig(); diff --git a/src/agents/tools/sessions-spawn-tool.test.ts b/src/agents/tools/sessions-spawn-tool.test.ts new file mode 100644 index 000000000000..c18f5bb86827 --- /dev/null +++ b/src/agents/tools/sessions-spawn-tool.test.ts @@ -0,0 +1,118 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const hoisted = vi.hoisted(() => { + const spawnSubagentDirectMock = vi.fn(); + const spawnAcpDirectMock = vi.fn(); + return { + spawnSubagentDirectMock, + spawnAcpDirectMock, + }; +}); + +vi.mock("../subagent-spawn.js", () => ({ + SUBAGENT_SPAWN_MODES: ["run", "session"], + spawnSubagentDirect: (...args: unknown[]) => hoisted.spawnSubagentDirectMock(...args), +})); + +vi.mock("../acp-spawn.js", () => ({ + ACP_SPAWN_MODES: ["run", "session"], + spawnAcpDirect: (...args: unknown[]) => hoisted.spawnAcpDirectMock(...args), +})); + +const { createSessionsSpawnTool } = await import("./sessions-spawn-tool.js"); + +describe("sessions_spawn tool", () => { + beforeEach(() => { + hoisted.spawnSubagentDirectMock.mockReset().mockResolvedValue({ + status: "accepted", + childSessionKey: "agent:main:subagent:1", + runId: "run-subagent", + }); + hoisted.spawnAcpDirectMock.mockReset().mockResolvedValue({ + status: "accepted", + childSessionKey: "agent:codex:acp:1", + runId: "run-acp", + }); + }); + + it("uses subagent runtime by default", async () => { + const tool = createSessionsSpawnTool({ + agentSessionKey: "agent:main:main", + agentChannel: "discord", + agentAccountId: "default", + agentTo: "channel:123", + agentThreadId: "456", + }); + + const result = await tool.execute("call-1", { + task: "build feature", + agentId: "main", + model: "anthropic/claude-sonnet-4-6", + thinking: "medium", + runTimeoutSeconds: 5, + thread: true, + mode: "session", + cleanup: "keep", + }); + + expect(result.details).toMatchObject({ + status: "accepted", + childSessionKey: "agent:main:subagent:1", + runId: "run-subagent", + }); + expect(hoisted.spawnSubagentDirectMock).toHaveBeenCalledWith( + expect.objectContaining({ + task: "build feature", + agentId: "main", + model: "anthropic/claude-sonnet-4-6", + thinking: "medium", + runTimeoutSeconds: 5, + thread: true, + mode: "session", + cleanup: "keep", + }), + expect.objectContaining({ + agentSessionKey: "agent:main:main", + }), + ); + expect(hoisted.spawnAcpDirectMock).not.toHaveBeenCalled(); + }); + + it("routes to ACP runtime when runtime=acp", async () => { + const tool = createSessionsSpawnTool({ + agentSessionKey: "agent:main:main", + agentChannel: "discord", + agentAccountId: "default", + agentTo: "channel:123", + agentThreadId: "456", + }); + + const result = await tool.execute("call-2", { + runtime: "acp", + task: "investigate the failing CI run", + agentId: "codex", + cwd: "/workspace", + thread: true, + mode: "session", + }); + + expect(result.details).toMatchObject({ + status: "accepted", + childSessionKey: "agent:codex:acp:1", + runId: "run-acp", + }); + expect(hoisted.spawnAcpDirectMock).toHaveBeenCalledWith( + expect.objectContaining({ + task: "investigate the failing CI run", + agentId: "codex", + cwd: "/workspace", + thread: true, + mode: "session", + }), + expect.objectContaining({ + agentSessionKey: "agent:main:main", + }), + ); + expect(hoisted.spawnSubagentDirectMock).not.toHaveBeenCalled(); + }); +}); diff --git a/src/agents/tools/sessions-spawn-tool.ts b/src/agents/tools/sessions-spawn-tool.ts index 9102d24847d7..e8f23f75660b 100644 --- a/src/agents/tools/sessions-spawn-tool.ts +++ b/src/agents/tools/sessions-spawn-tool.ts @@ -1,16 +1,21 @@ import { Type } from "@sinclair/typebox"; import type { GatewayMessageChannel } from "../../utils/message-channel.js"; +import { ACP_SPAWN_MODES, spawnAcpDirect } from "../acp-spawn.js"; import { optionalStringEnum } from "../schema/typebox.js"; import { SUBAGENT_SPAWN_MODES, spawnSubagentDirect } from "../subagent-spawn.js"; import type { AnyAgentTool } from "./common.js"; import { jsonResult, readStringParam } from "./common.js"; +const SESSIONS_SPAWN_RUNTIMES = ["subagent", "acp"] as const; + const SessionsSpawnToolSchema = Type.Object({ task: Type.String(), label: Type.Optional(Type.String()), + runtime: optionalStringEnum(SESSIONS_SPAWN_RUNTIMES), agentId: Type.Optional(Type.String()), model: Type.Optional(Type.String()), thinking: Type.Optional(Type.String()), + cwd: Type.Optional(Type.String()), runTimeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })), // Back-compat: older callers used timeoutSeconds for this tool. timeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })), @@ -36,15 +41,17 @@ export function createSessionsSpawnTool(opts?: { label: "Sessions", name: "sessions_spawn", description: - 'Spawn a sub-agent in an isolated session (mode="run" one-shot or mode="session" persistent) and route results back to the requester chat/thread.', + 'Spawn an isolated session (runtime="subagent" or runtime="acp"). mode="run" is one-shot and mode="session" is persistent/thread-bound.', parameters: SessionsSpawnToolSchema, execute: async (_toolCallId, args) => { const params = args as Record; const task = readStringParam(params, "task", { required: true }); const label = typeof params.label === "string" ? params.label.trim() : ""; + const runtime = params.runtime === "acp" ? "acp" : "subagent"; const requestedAgentId = readStringParam(params, "agentId"); const modelOverride = readStringParam(params, "model"); const thinkingOverrideRaw = readStringParam(params, "thinking"); + const cwd = readStringParam(params, "cwd"); const mode = params.mode === "run" || params.mode === "session" ? params.mode : undefined; const cleanup = params.cleanup === "keep" || params.cleanup === "delete" ? params.cleanup : "keep"; @@ -61,31 +68,50 @@ export function createSessionsSpawnTool(opts?: { : undefined; const thread = params.thread === true; - const result = await spawnSubagentDirect( - { - task, - label: label || undefined, - agentId: requestedAgentId, - model: modelOverride, - thinking: thinkingOverrideRaw, - runTimeoutSeconds, - thread, - mode, - cleanup, - expectsCompletionMessage: true, - }, - { - agentSessionKey: opts?.agentSessionKey, - agentChannel: opts?.agentChannel, - agentAccountId: opts?.agentAccountId, - agentTo: opts?.agentTo, - agentThreadId: opts?.agentThreadId, - agentGroupId: opts?.agentGroupId, - agentGroupChannel: opts?.agentGroupChannel, - agentGroupSpace: opts?.agentGroupSpace, - requesterAgentIdOverride: opts?.requesterAgentIdOverride, - }, - ); + const result = + runtime === "acp" + ? await spawnAcpDirect( + { + task, + label: label || undefined, + agentId: requestedAgentId, + cwd, + mode: mode && ACP_SPAWN_MODES.includes(mode) ? mode : undefined, + thread, + }, + { + agentSessionKey: opts?.agentSessionKey, + agentChannel: opts?.agentChannel, + agentAccountId: opts?.agentAccountId, + agentTo: opts?.agentTo, + agentThreadId: opts?.agentThreadId, + }, + ) + : await spawnSubagentDirect( + { + task, + label: label || undefined, + agentId: requestedAgentId, + model: modelOverride, + thinking: thinkingOverrideRaw, + runTimeoutSeconds, + thread, + mode, + cleanup, + expectsCompletionMessage: true, + }, + { + agentSessionKey: opts?.agentSessionKey, + agentChannel: opts?.agentChannel, + agentAccountId: opts?.agentAccountId, + agentTo: opts?.agentTo, + agentThreadId: opts?.agentThreadId, + agentGroupId: opts?.agentGroupId, + agentGroupChannel: opts?.agentGroupChannel, + agentGroupSpace: opts?.agentGroupSpace, + requesterAgentIdOverride: opts?.requesterAgentIdOverride, + }, + ); return jsonResult(result); }, diff --git a/src/auto-reply/commands-registry.data.ts b/src/auto-reply/commands-registry.data.ts index eb3e6f6d5a2b..d6b031d1b817 100644 --- a/src/auto-reply/commands-registry.data.ts +++ b/src/auto-reply/commands-registry.data.ts @@ -311,6 +311,46 @@ function buildChatCommands(): ChatCommandDefinition[] { ], argsMenu: "auto", }), + defineChatCommand({ + key: "acp", + nativeName: "acp", + description: "Manage ACP sessions and runtime options.", + textAlias: "/acp", + category: "management", + args: [ + { + name: "action", + description: + "spawn | cancel | steer | close | sessions | status | set-mode | set | cwd | permissions | timeout | model | reset-options | doctor | install | help", + type: "string", + choices: [ + "spawn", + "cancel", + "steer", + "close", + "sessions", + "status", + "set-mode", + "set", + "cwd", + "permissions", + "timeout", + "model", + "reset-options", + "doctor", + "install", + "help", + ], + }, + { + name: "value", + description: "Action arguments", + type: "string", + captureRemaining: true, + }, + ], + argsMenu: "auto", + }), defineChatCommand({ key: "focus", nativeName: "focus", diff --git a/src/auto-reply/commands-registry.test.ts b/src/auto-reply/commands-registry.test.ts index b05e5ea839c8..acf81b48dce4 100644 --- a/src/auto-reply/commands-registry.test.ts +++ b/src/auto-reply/commands-registry.test.ts @@ -109,6 +109,30 @@ describe("commands registry", () => { expect(findCommandByNativeName("tts", "discord")).toBeUndefined(); }); + it("keeps ACP native action choices aligned with implemented handlers", () => { + const acp = listChatCommands().find((command) => command.key === "acp"); + expect(acp).toBeTruthy(); + const actionArg = acp?.args?.find((arg) => arg.name === "action"); + expect(actionArg?.choices).toEqual([ + "spawn", + "cancel", + "steer", + "close", + "sessions", + "status", + "set-mode", + "set", + "cwd", + "permissions", + "timeout", + "model", + "reset-options", + "doctor", + "install", + "help", + ]); + }); + it("detects known text commands", () => { const detection = getCommandDetection(); expect(detection.exact.has("/commands")).toBe(true); diff --git a/src/auto-reply/reply/acp-projector.test.ts b/src/auto-reply/reply/acp-projector.test.ts new file mode 100644 index 000000000000..829ef7cc4525 --- /dev/null +++ b/src/auto-reply/reply/acp-projector.test.ts @@ -0,0 +1,145 @@ +import { describe, expect, it, vi } from "vitest"; +import type { OpenClawConfig } from "../../config/config.js"; +import { createAcpReplyProjector } from "./acp-projector.js"; + +function createCfg(overrides?: Partial): OpenClawConfig { + return { + acp: { + enabled: true, + stream: { + coalesceIdleMs: 0, + maxChunkChars: 50, + }, + }, + ...overrides, + } as OpenClawConfig; +} + +describe("createAcpReplyProjector", () => { + it("coalesces text deltas into bounded block chunks", async () => { + const deliveries: Array<{ kind: string; text?: string }> = []; + const projector = createAcpReplyProjector({ + cfg: createCfg(), + shouldSendToolSummaries: true, + deliver: async (kind, payload) => { + deliveries.push({ kind, text: payload.text }); + return true; + }, + }); + + await projector.onEvent({ + type: "text_delta", + text: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + }); + await projector.onEvent({ + type: "text_delta", + text: "bbbbbbbbbb", + }); + await projector.flush(true); + + expect(deliveries).toEqual([ + { + kind: "block", + text: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + }, + { kind: "block", text: "aabbbbbbbbbb" }, + ]); + }); + + it("buffers tiny token deltas and flushes once at turn end", async () => { + const deliveries: Array<{ kind: string; text?: string }> = []; + const projector = createAcpReplyProjector({ + cfg: createCfg({ + acp: { + enabled: true, + stream: { + coalesceIdleMs: 0, + maxChunkChars: 256, + }, + }, + }), + shouldSendToolSummaries: true, + provider: "discord", + deliver: async (kind, payload) => { + deliveries.push({ kind, text: payload.text }); + return true; + }, + }); + + await projector.onEvent({ type: "text_delta", text: "What" }); + await projector.onEvent({ type: "text_delta", text: " do" }); + await projector.onEvent({ type: "text_delta", text: " you want to work on?" }); + + expect(deliveries).toEqual([]); + + await projector.flush(true); + + expect(deliveries).toEqual([{ kind: "block", text: "What do you want to work on?" }]); + }); + + it("filters thought stream text and suppresses tool summaries when disabled", async () => { + const deliver = vi.fn(async () => true); + const projector = createAcpReplyProjector({ + cfg: createCfg(), + shouldSendToolSummaries: false, + deliver, + }); + + await projector.onEvent({ type: "text_delta", text: "internal", stream: "thought" }); + await projector.onEvent({ type: "status", text: "running tool" }); + await projector.onEvent({ type: "tool_call", text: "ls" }); + await projector.flush(true); + + expect(deliver).not.toHaveBeenCalled(); + }); + + it("emits status and tool_call summaries when enabled", async () => { + const deliveries: Array<{ kind: string; text?: string }> = []; + const projector = createAcpReplyProjector({ + cfg: createCfg(), + shouldSendToolSummaries: true, + deliver: async (kind, payload) => { + deliveries.push({ kind, text: payload.text }); + return true; + }, + }); + + await projector.onEvent({ type: "status", text: "planning" }); + await projector.onEvent({ type: "tool_call", text: "exec ls" }); + + expect(deliveries).toEqual([ + { kind: "tool", text: "⚙️ planning" }, + { kind: "tool", text: "🧰 exec ls" }, + ]); + }); + + it("flushes pending streamed text before tool/status updates", async () => { + const deliveries: Array<{ kind: string; text?: string }> = []; + const projector = createAcpReplyProjector({ + cfg: createCfg({ + acp: { + enabled: true, + stream: { + coalesceIdleMs: 0, + maxChunkChars: 256, + }, + }, + }), + shouldSendToolSummaries: true, + provider: "discord", + deliver: async (kind, payload) => { + deliveries.push({ kind, text: payload.text }); + return true; + }, + }); + + await projector.onEvent({ type: "text_delta", text: "Hello" }); + await projector.onEvent({ type: "text_delta", text: " world" }); + await projector.onEvent({ type: "status", text: "running tool" }); + + expect(deliveries).toEqual([ + { kind: "block", text: "Hello world" }, + { kind: "tool", text: "⚙️ running tool" }, + ]); + }); +}); diff --git a/src/auto-reply/reply/acp-projector.ts b/src/auto-reply/reply/acp-projector.ts new file mode 100644 index 000000000000..8bbe643dc308 --- /dev/null +++ b/src/auto-reply/reply/acp-projector.ts @@ -0,0 +1,140 @@ +import type { AcpRuntimeEvent } from "../../acp/runtime/types.js"; +import { EmbeddedBlockChunker } from "../../agents/pi-embedded-block-chunker.js"; +import type { OpenClawConfig } from "../../config/config.js"; +import type { ReplyPayload } from "../types.js"; +import { createBlockReplyPipeline } from "./block-reply-pipeline.js"; +import { resolveEffectiveBlockStreamingConfig } from "./block-streaming.js"; +import type { ReplyDispatchKind } from "./reply-dispatcher.js"; + +const DEFAULT_ACP_STREAM_COALESCE_IDLE_MS = 350; +const DEFAULT_ACP_STREAM_MAX_CHUNK_CHARS = 1800; +const ACP_BLOCK_REPLY_TIMEOUT_MS = 15_000; + +function clampPositiveInteger( + value: unknown, + fallback: number, + bounds: { min: number; max: number }, +): number { + if (typeof value !== "number" || !Number.isFinite(value)) { + return fallback; + } + const rounded = Math.round(value); + if (rounded < bounds.min) { + return bounds.min; + } + if (rounded > bounds.max) { + return bounds.max; + } + return rounded; +} + +function resolveAcpStreamCoalesceIdleMs(cfg: OpenClawConfig): number { + return clampPositiveInteger( + cfg.acp?.stream?.coalesceIdleMs, + DEFAULT_ACP_STREAM_COALESCE_IDLE_MS, + { + min: 0, + max: 5_000, + }, + ); +} + +function resolveAcpStreamMaxChunkChars(cfg: OpenClawConfig): number { + return clampPositiveInteger(cfg.acp?.stream?.maxChunkChars, DEFAULT_ACP_STREAM_MAX_CHUNK_CHARS, { + min: 50, + max: 4_000, + }); +} + +function resolveAcpStreamingConfig(params: { + cfg: OpenClawConfig; + provider?: string; + accountId?: string; +}) { + return resolveEffectiveBlockStreamingConfig({ + cfg: params.cfg, + provider: params.provider, + accountId: params.accountId, + maxChunkChars: resolveAcpStreamMaxChunkChars(params.cfg), + coalesceIdleMs: resolveAcpStreamCoalesceIdleMs(params.cfg), + }); +} + +export type AcpReplyProjector = { + onEvent: (event: AcpRuntimeEvent) => Promise; + flush: (force?: boolean) => Promise; +}; + +export function createAcpReplyProjector(params: { + cfg: OpenClawConfig; + shouldSendToolSummaries: boolean; + deliver: (kind: ReplyDispatchKind, payload: ReplyPayload) => Promise; + provider?: string; + accountId?: string; +}): AcpReplyProjector { + const streaming = resolveAcpStreamingConfig({ + cfg: params.cfg, + provider: params.provider, + accountId: params.accountId, + }); + const blockReplyPipeline = createBlockReplyPipeline({ + onBlockReply: async (payload) => { + await params.deliver("block", payload); + }, + timeoutMs: ACP_BLOCK_REPLY_TIMEOUT_MS, + coalescing: streaming.coalescing, + }); + const chunker = new EmbeddedBlockChunker(streaming.chunking); + + const drainChunker = (force: boolean) => { + chunker.drain({ + force, + emit: (chunk) => { + blockReplyPipeline.enqueue({ text: chunk }); + }, + }); + }; + + const flush = async (force = false): Promise => { + drainChunker(force); + await blockReplyPipeline.flush({ force }); + }; + + const emitToolSummary = async (prefix: string, text: string): Promise => { + if (!params.shouldSendToolSummaries || !text) { + return; + } + // Keep tool summaries ordered after any pending streamed text. + await flush(true); + await params.deliver("tool", { text: `${prefix} ${text}` }); + }; + + const onEvent = async (event: AcpRuntimeEvent): Promise => { + if (event.type === "text_delta") { + if (event.stream && event.stream !== "output") { + return; + } + if (event.text) { + chunker.append(event.text); + drainChunker(false); + } + return; + } + if (event.type === "status") { + await emitToolSummary("⚙️", event.text); + return; + } + if (event.type === "tool_call") { + await emitToolSummary("🧰", event.text); + return; + } + if (event.type === "done" || event.type === "error") { + await flush(true); + } + }; + + return { + onEvent, + flush, + }; +} diff --git a/src/auto-reply/reply/agent-runner.ts b/src/auto-reply/reply/agent-runner.ts index 8628fe33a514..9fb2af09ade9 100644 --- a/src/auto-reply/reply/agent-runner.ts +++ b/src/auto-reply/reply/agent-runner.ts @@ -41,7 +41,7 @@ import { runMemoryFlushIfNeeded } from "./agent-runner-memory.js"; import { buildReplyPayloads } from "./agent-runner-payloads.js"; import { appendUsageLine, formatResponseUsageLine } from "./agent-runner-utils.js"; import { createAudioAsVoiceBuffer, createBlockReplyPipeline } from "./block-reply-pipeline.js"; -import { resolveBlockStreamingCoalescing } from "./block-streaming.js"; +import { resolveEffectiveBlockStreamingConfig } from "./block-streaming.js"; import { createFollowupRunner } from "./followup-runner.js"; import { resolveOriginMessageProvider, resolveOriginMessageTo } from "./origin-routing.js"; import { @@ -195,12 +195,12 @@ export async function runReplyAgent(params: { const cfg = followupRun.run.config; const blockReplyCoalescing = blockStreamingEnabled && opts?.onBlockReply - ? resolveBlockStreamingCoalescing( + ? resolveEffectiveBlockStreamingConfig({ cfg, - sessionCtx.Provider, - sessionCtx.AccountId, - blockReplyChunking, - ) + provider: sessionCtx.Provider, + accountId: sessionCtx.AccountId, + chunking: blockReplyChunking, + }).coalescing : undefined; const blockReplyPipeline = blockStreamingEnabled && opts?.onBlockReply diff --git a/src/auto-reply/reply/block-streaming.test.ts b/src/auto-reply/reply/block-streaming.test.ts new file mode 100644 index 000000000000..29264ca99b3c --- /dev/null +++ b/src/auto-reply/reply/block-streaming.test.ts @@ -0,0 +1,68 @@ +import { describe, expect, it } from "vitest"; +import type { OpenClawConfig } from "../../config/config.js"; +import { + resolveBlockStreamingChunking, + resolveEffectiveBlockStreamingConfig, +} from "./block-streaming.js"; + +describe("resolveEffectiveBlockStreamingConfig", () => { + it("applies ACP-style overrides while preserving chunk/coalescer bounds", () => { + const cfg = {} as OpenClawConfig; + const baseChunking = resolveBlockStreamingChunking(cfg, "discord"); + const resolved = resolveEffectiveBlockStreamingConfig({ + cfg, + provider: "discord", + maxChunkChars: 64, + coalesceIdleMs: 25, + }); + + expect(baseChunking.maxChars).toBeGreaterThanOrEqual(64); + expect(resolved.chunking.maxChars).toBe(64); + expect(resolved.chunking.minChars).toBeLessThanOrEqual(resolved.chunking.maxChars); + expect(resolved.coalescing.maxChars).toBeLessThanOrEqual(resolved.chunking.maxChars); + expect(resolved.coalescing.minChars).toBeLessThanOrEqual(resolved.coalescing.maxChars); + expect(resolved.coalescing.idleMs).toBe(25); + }); + + it("reuses caller-provided chunking for shared main/subagent/ACP config resolution", () => { + const resolved = resolveEffectiveBlockStreamingConfig({ + cfg: undefined, + chunking: { + minChars: 10, + maxChars: 20, + breakPreference: "paragraph", + }, + coalesceIdleMs: 0, + }); + + expect(resolved.chunking).toEqual({ + minChars: 10, + maxChars: 20, + breakPreference: "paragraph", + }); + expect(resolved.coalescing.maxChars).toBe(20); + expect(resolved.coalescing.idleMs).toBe(0); + }); + + it("allows ACP maxChunkChars overrides above base defaults up to provider text limits", () => { + const cfg = { + channels: { + discord: { + textChunkLimit: 4096, + }, + }, + } as OpenClawConfig; + + const baseChunking = resolveBlockStreamingChunking(cfg, "discord"); + expect(baseChunking.maxChars).toBeLessThan(1800); + + const resolved = resolveEffectiveBlockStreamingConfig({ + cfg, + provider: "discord", + maxChunkChars: 1800, + }); + + expect(resolved.chunking.maxChars).toBe(1800); + expect(resolved.chunking.minChars).toBeLessThanOrEqual(resolved.chunking.maxChars); + }); +}); diff --git a/src/auto-reply/reply/block-streaming.ts b/src/auto-reply/reply/block-streaming.ts index 318da9822385..67b7a4528a7a 100644 --- a/src/auto-reply/reply/block-streaming.ts +++ b/src/auto-reply/reply/block-streaming.ts @@ -59,16 +59,101 @@ export type BlockStreamingCoalescing = { flushOnEnqueue?: boolean; }; -export function resolveBlockStreamingChunking( - cfg: OpenClawConfig | undefined, - provider?: string, - accountId?: string | null, -): { +export type BlockStreamingChunking = { minChars: number; maxChars: number; breakPreference: "paragraph" | "newline" | "sentence"; flushOnParagraph?: boolean; +}; + +function clampPositiveInteger( + value: number | undefined, + fallback: number, + bounds: { min: number; max: number }, +): number { + if (typeof value !== "number" || !Number.isFinite(value)) { + return fallback; + } + const rounded = Math.round(value); + if (rounded < bounds.min) { + return bounds.min; + } + if (rounded > bounds.max) { + return bounds.max; + } + return rounded; +} + +export function resolveEffectiveBlockStreamingConfig(params: { + cfg: OpenClawConfig | undefined; + provider?: string; + accountId?: string | null; + chunking?: BlockStreamingChunking; + /** Optional upper bound for chunking/coalescing max chars. */ + maxChunkChars?: number; + /** Optional coalescer idle flush override in milliseconds. */ + coalesceIdleMs?: number; +}): { + chunking: BlockStreamingChunking; + coalescing: BlockStreamingCoalescing; } { + const providerKey = normalizeChunkProvider(params.provider); + const providerId = providerKey ? normalizeChannelId(providerKey) : null; + const providerChunkLimit = providerId + ? getChannelDock(providerId)?.outbound?.textChunkLimit + : undefined; + const textLimit = resolveTextChunkLimit(params.cfg, providerKey, params.accountId, { + fallbackLimit: providerChunkLimit, + }); + const chunkingDefaults = + params.chunking ?? resolveBlockStreamingChunking(params.cfg, params.provider, params.accountId); + const chunkingMax = clampPositiveInteger(params.maxChunkChars, chunkingDefaults.maxChars, { + min: 1, + max: Math.max(1, textLimit), + }); + const chunking: BlockStreamingChunking = { + ...chunkingDefaults, + minChars: Math.min(chunkingDefaults.minChars, chunkingMax), + maxChars: chunkingMax, + }; + const coalescingDefaults = resolveBlockStreamingCoalescing( + params.cfg, + params.provider, + params.accountId, + chunking, + ); + const coalescingMax = Math.max( + 1, + Math.min(coalescingDefaults?.maxChars ?? chunking.maxChars, chunking.maxChars), + ); + const coalescingMin = Math.min(coalescingDefaults?.minChars ?? chunking.minChars, coalescingMax); + const coalescingIdleMs = clampPositiveInteger( + params.coalesceIdleMs, + coalescingDefaults?.idleMs ?? DEFAULT_BLOCK_STREAM_COALESCE_IDLE_MS, + { min: 0, max: 5_000 }, + ); + const coalescing: BlockStreamingCoalescing = { + minChars: coalescingMin, + maxChars: coalescingMax, + idleMs: coalescingIdleMs, + joiner: + coalescingDefaults?.joiner ?? + (chunking.breakPreference === "sentence" + ? " " + : chunking.breakPreference === "newline" + ? "\n" + : "\n\n"), + flushOnEnqueue: coalescingDefaults?.flushOnEnqueue ?? chunking.flushOnParagraph === true, + }; + + return { chunking, coalescing }; +} + +export function resolveBlockStreamingChunking( + cfg: OpenClawConfig | undefined, + provider?: string, + accountId?: string | null, +): BlockStreamingChunking { const providerKey = normalizeChunkProvider(provider); const providerConfigKey = providerKey; const providerId = providerKey ? normalizeChannelId(providerKey) : null; diff --git a/src/auto-reply/reply/commands-acp.test.ts b/src/auto-reply/reply/commands-acp.test.ts new file mode 100644 index 000000000000..df3135f1b5b8 --- /dev/null +++ b/src/auto-reply/reply/commands-acp.test.ts @@ -0,0 +1,796 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { AcpRuntimeError } from "../../acp/runtime/errors.js"; +import type { OpenClawConfig } from "../../config/config.js"; +import type { SessionBindingRecord } from "../../infra/outbound/session-binding-service.js"; + +const hoisted = vi.hoisted(() => { + const callGatewayMock = vi.fn(); + const requireAcpRuntimeBackendMock = vi.fn(); + const getAcpRuntimeBackendMock = vi.fn(); + const listAcpSessionEntriesMock = vi.fn(); + const readAcpSessionEntryMock = vi.fn(); + const upsertAcpSessionMetaMock = vi.fn(); + const resolveSessionStorePathForAcpMock = vi.fn(); + const loadSessionStoreMock = vi.fn(); + const sessionBindingCapabilitiesMock = vi.fn(); + const sessionBindingBindMock = vi.fn(); + const sessionBindingListBySessionMock = vi.fn(); + const sessionBindingResolveByConversationMock = vi.fn(); + const sessionBindingUnbindMock = vi.fn(); + const ensureSessionMock = vi.fn(); + const runTurnMock = vi.fn(); + const cancelMock = vi.fn(); + const closeMock = vi.fn(); + const getCapabilitiesMock = vi.fn(); + const getStatusMock = vi.fn(); + const setModeMock = vi.fn(); + const setConfigOptionMock = vi.fn(); + const doctorMock = vi.fn(); + return { + callGatewayMock, + requireAcpRuntimeBackendMock, + getAcpRuntimeBackendMock, + listAcpSessionEntriesMock, + readAcpSessionEntryMock, + upsertAcpSessionMetaMock, + resolveSessionStorePathForAcpMock, + loadSessionStoreMock, + sessionBindingCapabilitiesMock, + sessionBindingBindMock, + sessionBindingListBySessionMock, + sessionBindingResolveByConversationMock, + sessionBindingUnbindMock, + ensureSessionMock, + runTurnMock, + cancelMock, + closeMock, + getCapabilitiesMock, + getStatusMock, + setModeMock, + setConfigOptionMock, + doctorMock, + }; +}); + +vi.mock("../../gateway/call.js", () => ({ + callGateway: (args: unknown) => hoisted.callGatewayMock(args), +})); + +vi.mock("../../acp/runtime/registry.js", () => ({ + requireAcpRuntimeBackend: (id?: string) => hoisted.requireAcpRuntimeBackendMock(id), + getAcpRuntimeBackend: (id?: string) => hoisted.getAcpRuntimeBackendMock(id), +})); + +vi.mock("../../acp/runtime/session-meta.js", () => ({ + listAcpSessionEntries: (args: unknown) => hoisted.listAcpSessionEntriesMock(args), + readAcpSessionEntry: (args: unknown) => hoisted.readAcpSessionEntryMock(args), + upsertAcpSessionMeta: (args: unknown) => hoisted.upsertAcpSessionMetaMock(args), + resolveSessionStorePathForAcp: (args: unknown) => hoisted.resolveSessionStorePathForAcpMock(args), +})); + +vi.mock("../../config/sessions.js", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + loadSessionStore: (...args: unknown[]) => hoisted.loadSessionStoreMock(...args), + }; +}); + +vi.mock("../../infra/outbound/session-binding-service.js", async (importOriginal) => { + const actual = + await importOriginal(); + return { + ...actual, + getSessionBindingService: () => ({ + bind: (input: unknown) => hoisted.sessionBindingBindMock(input), + getCapabilities: (params: unknown) => hoisted.sessionBindingCapabilitiesMock(params), + listBySession: (targetSessionKey: string) => + hoisted.sessionBindingListBySessionMock(targetSessionKey), + resolveByConversation: (ref: unknown) => hoisted.sessionBindingResolveByConversationMock(ref), + touch: vi.fn(), + unbind: (input: unknown) => hoisted.sessionBindingUnbindMock(input), + }), + }; +}); + +// Prevent transitive import chain from reaching discord/monitor which needs https-proxy-agent. +vi.mock("../../discord/monitor/gateway-plugin.js", () => ({ + createDiscordGatewayPlugin: () => ({}), +})); + +const { handleAcpCommand } = await import("./commands-acp.js"); +const { buildCommandTestParams } = await import("./commands-spawn.test-harness.js"); +const { __testing: acpManagerTesting } = await import("../../acp/control-plane/manager.js"); + +type FakeBinding = { + bindingId: string; + targetSessionKey: string; + targetKind: "subagent" | "session"; + conversation: { + channel: "discord"; + accountId: string; + conversationId: string; + parentConversationId?: string; + }; + status: "active"; + boundAt: number; + metadata?: { + agentId?: string; + label?: string; + boundBy?: string; + webhookId?: string; + }; +}; + +function createSessionBinding(overrides?: Partial): FakeBinding { + return { + bindingId: "default:thread-created", + targetSessionKey: "agent:codex:acp:s1", + targetKind: "session", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-created", + parentConversationId: "parent-1", + }, + status: "active", + boundAt: Date.now(), + metadata: { + agentId: "codex", + boundBy: "user-1", + }, + ...overrides, + }; +} + +const baseCfg = { + session: { mainKey: "main", scope: "per-sender" }, + acp: { + enabled: true, + dispatch: { enabled: true }, + backend: "acpx", + }, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: true, + }, + }, + }, +} satisfies OpenClawConfig; + +function createDiscordParams(commandBody: string, cfg: OpenClawConfig = baseCfg) { + const params = buildCommandTestParams(commandBody, cfg, { + Provider: "discord", + Surface: "discord", + OriginatingChannel: "discord", + OriginatingTo: "channel:parent-1", + AccountId: "default", + }); + params.command.senderId = "user-1"; + return params; +} + +describe("/acp command", () => { + beforeEach(() => { + acpManagerTesting.resetAcpSessionManagerForTests(); + hoisted.listAcpSessionEntriesMock.mockReset().mockResolvedValue([]); + hoisted.callGatewayMock.mockReset().mockResolvedValue({ ok: true }); + hoisted.readAcpSessionEntryMock.mockReset().mockReturnValue(null); + hoisted.upsertAcpSessionMetaMock.mockReset().mockResolvedValue({ + sessionId: "session-1", + updatedAt: Date.now(), + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "run-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + hoisted.resolveSessionStorePathForAcpMock.mockReset().mockReturnValue({ + cfg: baseCfg, + storePath: "/tmp/sessions-acp.json", + }); + hoisted.loadSessionStoreMock.mockReset().mockReturnValue({}); + hoisted.sessionBindingCapabilitiesMock.mockReset().mockReturnValue({ + adapterAvailable: true, + bindSupported: true, + unbindSupported: true, + placements: ["current", "child"], + }); + hoisted.sessionBindingBindMock + .mockReset() + .mockImplementation( + async (input: { + targetSessionKey: string; + conversation: { accountId: string; conversationId: string }; + placement: "current" | "child"; + metadata?: Record; + }) => + createSessionBinding({ + targetSessionKey: input.targetSessionKey, + conversation: { + channel: "discord", + accountId: input.conversation.accountId, + conversationId: + input.placement === "child" ? "thread-created" : input.conversation.conversationId, + parentConversationId: "parent-1", + }, + metadata: { + boundBy: + typeof input.metadata?.boundBy === "string" ? input.metadata.boundBy : "user-1", + webhookId: "wh-1", + }, + }), + ); + hoisted.sessionBindingListBySessionMock.mockReset().mockReturnValue([]); + hoisted.sessionBindingResolveByConversationMock.mockReset().mockReturnValue(null); + hoisted.sessionBindingUnbindMock.mockReset().mockResolvedValue([]); + + hoisted.ensureSessionMock + .mockReset() + .mockImplementation(async (input: { sessionKey: string }) => ({ + sessionKey: input.sessionKey, + backend: "acpx", + runtimeSessionName: `${input.sessionKey}:runtime`, + })); + hoisted.runTurnMock.mockReset().mockImplementation(async function* () { + yield { type: "done" }; + }); + hoisted.cancelMock.mockReset().mockResolvedValue(undefined); + hoisted.closeMock.mockReset().mockResolvedValue(undefined); + hoisted.getCapabilitiesMock.mockReset().mockResolvedValue({ + controls: ["session/set_mode", "session/set_config_option", "session/status"], + }); + hoisted.getStatusMock.mockReset().mockResolvedValue({ + summary: "status=alive sessionId=sid-1 pid=1234", + details: { status: "alive", sessionId: "sid-1", pid: 1234 }, + }); + hoisted.setModeMock.mockReset().mockResolvedValue(undefined); + hoisted.setConfigOptionMock.mockReset().mockResolvedValue(undefined); + hoisted.doctorMock.mockReset().mockResolvedValue({ + ok: true, + message: "acpx command available", + }); + + const runtimeBackend = { + id: "acpx", + runtime: { + ensureSession: hoisted.ensureSessionMock, + runTurn: hoisted.runTurnMock, + getCapabilities: hoisted.getCapabilitiesMock, + getStatus: hoisted.getStatusMock, + setMode: hoisted.setModeMock, + setConfigOption: hoisted.setConfigOptionMock, + doctor: hoisted.doctorMock, + cancel: hoisted.cancelMock, + close: hoisted.closeMock, + }, + }; + hoisted.requireAcpRuntimeBackendMock.mockReset().mockReturnValue(runtimeBackend); + hoisted.getAcpRuntimeBackendMock.mockReset().mockReturnValue(runtimeBackend); + }); + + it("returns null when the message is not /acp", async () => { + const params = createDiscordParams("/status"); + const result = await handleAcpCommand(params, true); + expect(result).toBeNull(); + }); + + it("shows help by default", async () => { + const params = createDiscordParams("/acp"); + const result = await handleAcpCommand(params, true); + expect(result?.reply?.text).toContain("ACP commands:"); + expect(result?.reply?.text).toContain("/acp spawn"); + }); + + it("spawns an ACP session and binds a Discord thread", async () => { + hoisted.ensureSessionMock.mockResolvedValueOnce({ + sessionKey: "agent:codex:acp:s1", + backend: "acpx", + runtimeSessionName: "agent:codex:acp:s1:runtime", + agentSessionId: "codex-inner-1", + backendSessionId: "acpx-1", + }); + + const params = createDiscordParams("/acp spawn codex --cwd /home/bob/clawd"); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("Spawned ACP session agent:codex:acp:"); + expect(result?.reply?.text).toContain("Created thread thread-created and bound it"); + expect(hoisted.requireAcpRuntimeBackendMock).toHaveBeenCalledWith("acpx"); + expect(hoisted.ensureSessionMock).toHaveBeenCalledWith( + expect.objectContaining({ + agent: "codex", + mode: "persistent", + cwd: "/home/bob/clawd", + }), + ); + expect(hoisted.sessionBindingBindMock).toHaveBeenCalledWith( + expect.objectContaining({ + targetKind: "session", + placement: "child", + metadata: expect.objectContaining({ + introText: expect.stringContaining("cwd: /home/bob/clawd"), + }), + }), + ); + expect(hoisted.sessionBindingBindMock).toHaveBeenCalledWith( + expect.objectContaining({ + metadata: expect.objectContaining({ + introText: expect.not.stringContaining( + "session ids: pending (available after the first reply)", + ), + }), + }), + ); + expect(hoisted.callGatewayMock).toHaveBeenCalledWith( + expect.objectContaining({ + method: "sessions.patch", + }), + ); + expect(hoisted.upsertAcpSessionMetaMock).toHaveBeenCalled(); + const upsertArgs = hoisted.upsertAcpSessionMetaMock.mock.calls[0]?.[0] as + | { + sessionKey: string; + mutate: ( + current: unknown, + entry: { sessionId: string; updatedAt: number } | undefined, + ) => { + backend?: string; + runtimeSessionName?: string; + }; + } + | undefined; + expect(upsertArgs?.sessionKey).toMatch(/^agent:codex:acp:/); + const seededWithoutEntry = upsertArgs?.mutate(undefined, undefined); + expect(seededWithoutEntry?.backend).toBe("acpx"); + expect(seededWithoutEntry?.runtimeSessionName).toContain(":runtime"); + }); + + it("requires explicit ACP target when acp.defaultAgent is not configured", async () => { + const params = createDiscordParams("/acp spawn"); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP target agent is required"); + expect(hoisted.ensureSessionMock).not.toHaveBeenCalled(); + }); + + it("rejects thread-bound ACP spawn when spawnAcpSessions is disabled", async () => { + const cfg = { + ...baseCfg, + channels: { + discord: { + threadBindings: { + enabled: true, + spawnAcpSessions: false, + }, + }, + }, + } satisfies OpenClawConfig; + + const params = createDiscordParams("/acp spawn codex", cfg); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("spawnAcpSessions=true"); + expect(hoisted.closeMock).toHaveBeenCalledTimes(1); + expect(hoisted.callGatewayMock).toHaveBeenCalledWith( + expect.objectContaining({ + method: "sessions.delete", + params: expect.objectContaining({ + key: expect.stringMatching(/^agent:codex:acp:/), + deleteTranscript: false, + emitLifecycleHooks: false, + }), + }), + ); + expect(hoisted.callGatewayMock).not.toHaveBeenCalledWith( + expect.objectContaining({ method: "sessions.patch" }), + ); + }); + + it("cancels the ACP session bound to the current thread", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "running", + lastActivityAt: Date.now(), + }, + }); + + const params = createDiscordParams("/acp cancel", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + + const result = await handleAcpCommand(params, true); + expect(result?.reply?.text).toContain("Cancel requested for ACP session agent:codex:acp:s1"); + expect(hoisted.cancelMock).toHaveBeenCalledWith({ + handle: expect.objectContaining({ + sessionKey: "agent:codex:acp:s1", + backend: "acpx", + }), + reason: "manual-cancel", + }); + }); + + it("sends steer instructions via ACP runtime", async () => { + hoisted.callGatewayMock.mockImplementation(async (request: { method?: string }) => { + if (request.method === "sessions.resolve") { + return { key: "agent:codex:acp:s1" }; + } + return { ok: true }; + }); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + hoisted.runTurnMock.mockImplementation(async function* () { + yield { type: "text_delta", text: "Applied steering." }; + yield { type: "done" }; + }); + + const params = createDiscordParams("/acp steer --session agent:codex:acp:s1 tighten logging"); + const result = await handleAcpCommand(params, true); + + expect(hoisted.runTurnMock).toHaveBeenCalledWith( + expect.objectContaining({ + mode: "steer", + text: "tighten logging", + }), + ); + expect(result?.reply?.text).toContain("Applied steering."); + }); + + it("blocks /acp steer when ACP dispatch is disabled by policy", async () => { + const cfg = { + ...baseCfg, + acp: { + ...baseCfg.acp, + dispatch: { enabled: false }, + }, + } satisfies OpenClawConfig; + const params = createDiscordParams("/acp steer tighten logging", cfg); + const result = await handleAcpCommand(params, true); + expect(result?.reply?.text).toContain("ACP dispatch is disabled by policy"); + expect(hoisted.runTurnMock).not.toHaveBeenCalled(); + }); + + it("closes an ACP session, unbinds thread targets, and clears metadata", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + hoisted.sessionBindingUnbindMock.mockResolvedValue([ + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }) as SessionBindingRecord, + ]); + + const params = createDiscordParams("/acp close", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + + const result = await handleAcpCommand(params, true); + + expect(hoisted.closeMock).toHaveBeenCalledTimes(1); + expect(hoisted.sessionBindingUnbindMock).toHaveBeenCalledWith( + expect.objectContaining({ + targetSessionKey: "agent:codex:acp:s1", + reason: "manual", + }), + ); + expect(hoisted.upsertAcpSessionMetaMock).toHaveBeenCalled(); + expect(result?.reply?.text).toContain("Removed 1 binding"); + }); + + it("lists ACP sessions from the session store", async () => { + hoisted.sessionBindingListBySessionMock.mockImplementation((key: string) => + key === "agent:codex:acp:s1" + ? [ + createSessionBinding({ + targetSessionKey: key, + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }) as SessionBindingRecord, + ] + : [], + ); + hoisted.loadSessionStoreMock.mockReturnValue({ + "agent:codex:acp:s1": { + sessionId: "sess-1", + updatedAt: Date.now(), + label: "codex-main", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }, + "agent:main:main": { + sessionId: "sess-main", + updatedAt: Date.now(), + }, + }); + + const params = createDiscordParams("/acp sessions", baseCfg); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP sessions:"); + expect(result?.reply?.text).toContain("codex-main"); + expect(result?.reply?.text).toContain("thread:thread-1"); + }); + + it("shows ACP status for the thread-bound ACP session", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + identity: { + state: "resolved", + source: "status", + acpxSessionId: "acpx-sid-1", + agentSessionId: "codex-sid-1", + lastUpdatedAt: Date.now(), + }, + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + const params = createDiscordParams("/acp status", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP status:"); + expect(result?.reply?.text).toContain("session: agent:codex:acp:s1"); + expect(result?.reply?.text).toContain("agent session id: codex-sid-1"); + expect(result?.reply?.text).toContain("acpx session id: acpx-sid-1"); + expect(result?.reply?.text).toContain("capabilities:"); + expect(hoisted.getStatusMock).toHaveBeenCalledTimes(1); + }); + + it("updates ACP runtime mode via /acp set-mode", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + const params = createDiscordParams("/acp set-mode plan", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + + const result = await handleAcpCommand(params, true); + + expect(hoisted.setModeMock).toHaveBeenCalledWith( + expect.objectContaining({ + mode: "plan", + }), + ); + expect(result?.reply?.text).toContain("Updated ACP runtime mode"); + }); + + it("updates ACP config options and keeps cwd local when using /acp set", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + + const setModelParams = createDiscordParams("/acp set model gpt-5.3-codex", baseCfg); + setModelParams.ctx.MessageThreadId = "thread-1"; + const setModel = await handleAcpCommand(setModelParams, true); + expect(hoisted.setConfigOptionMock).toHaveBeenCalledWith( + expect.objectContaining({ + key: "model", + value: "gpt-5.3-codex", + }), + ); + expect(setModel?.reply?.text).toContain("Updated ACP config option"); + + hoisted.setConfigOptionMock.mockClear(); + const setCwdParams = createDiscordParams("/acp set cwd /tmp/worktree", baseCfg); + setCwdParams.ctx.MessageThreadId = "thread-1"; + const setCwd = await handleAcpCommand(setCwdParams, true); + expect(hoisted.setConfigOptionMock).not.toHaveBeenCalled(); + expect(setCwd?.reply?.text).toContain("Updated ACP cwd"); + }); + + it("rejects non-absolute cwd values via ACP runtime option validation", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + + const params = createDiscordParams("/acp cwd relative/path", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP error (ACP_INVALID_RUNTIME_OPTION)"); + expect(result?.reply?.text).toContain("absolute path"); + }); + + it("rejects invalid timeout values before backend config writes", async () => { + hoisted.sessionBindingResolveByConversationMock.mockReturnValue( + createSessionBinding({ + targetSessionKey: "agent:codex:acp:s1", + conversation: { + channel: "discord", + accountId: "default", + conversationId: "thread-1", + parentConversationId: "parent-1", + }, + }), + ); + hoisted.readAcpSessionEntryMock.mockReturnValue({ + sessionKey: "agent:codex:acp:s1", + storeSessionKey: "agent:codex:acp:s1", + acp: { + backend: "acpx", + agent: "codex", + runtimeSessionName: "runtime-1", + mode: "persistent", + state: "idle", + lastActivityAt: Date.now(), + }, + }); + + const params = createDiscordParams("/acp timeout 10s", baseCfg); + params.ctx.MessageThreadId = "thread-1"; + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP error (ACP_INVALID_RUNTIME_OPTION)"); + expect(hoisted.setConfigOptionMock).not.toHaveBeenCalled(); + }); + + it("returns actionable doctor output when backend is missing", async () => { + hoisted.getAcpRuntimeBackendMock.mockReturnValue(null); + hoisted.requireAcpRuntimeBackendMock.mockImplementation(() => { + throw new AcpRuntimeError( + "ACP_BACKEND_MISSING", + "ACP runtime backend is not configured. Install and enable the acpx runtime plugin.", + ); + }); + + const params = createDiscordParams("/acp doctor", baseCfg); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP doctor:"); + expect(result?.reply?.text).toContain("healthy: no"); + expect(result?.reply?.text).toContain("next:"); + }); + + it("shows deterministic install instructions via /acp install", async () => { + const params = createDiscordParams("/acp install", baseCfg); + const result = await handleAcpCommand(params, true); + + expect(result?.reply?.text).toContain("ACP install:"); + expect(result?.reply?.text).toContain("run:"); + expect(result?.reply?.text).toContain("then: /acp doctor"); + }); +}); diff --git a/src/auto-reply/reply/commands-acp.ts b/src/auto-reply/reply/commands-acp.ts new file mode 100644 index 000000000000..2eef395c9a2c --- /dev/null +++ b/src/auto-reply/reply/commands-acp.ts @@ -0,0 +1,83 @@ +import { logVerbose } from "../../globals.js"; +import { + handleAcpDoctorAction, + handleAcpInstallAction, + handleAcpSessionsAction, +} from "./commands-acp/diagnostics.js"; +import { + handleAcpCancelAction, + handleAcpCloseAction, + handleAcpSpawnAction, + handleAcpSteerAction, +} from "./commands-acp/lifecycle.js"; +import { + handleAcpCwdAction, + handleAcpModelAction, + handleAcpPermissionsAction, + handleAcpResetOptionsAction, + handleAcpSetAction, + handleAcpSetModeAction, + handleAcpStatusAction, + handleAcpTimeoutAction, +} from "./commands-acp/runtime-options.js"; +import { + COMMAND, + type AcpAction, + resolveAcpAction, + resolveAcpHelpText, + stopWithText, +} from "./commands-acp/shared.js"; +import type { + CommandHandler, + CommandHandlerResult, + HandleCommandsParams, +} from "./commands-types.js"; + +type AcpActionHandler = ( + params: HandleCommandsParams, + tokens: string[], +) => Promise; + +const ACP_ACTION_HANDLERS: Record, AcpActionHandler> = { + spawn: handleAcpSpawnAction, + cancel: handleAcpCancelAction, + steer: handleAcpSteerAction, + close: handleAcpCloseAction, + status: handleAcpStatusAction, + "set-mode": handleAcpSetModeAction, + set: handleAcpSetAction, + cwd: handleAcpCwdAction, + permissions: handleAcpPermissionsAction, + timeout: handleAcpTimeoutAction, + model: handleAcpModelAction, + "reset-options": handleAcpResetOptionsAction, + doctor: handleAcpDoctorAction, + install: async (params, tokens) => handleAcpInstallAction(params, tokens), + sessions: async (params, tokens) => handleAcpSessionsAction(params, tokens), +}; + +export const handleAcpCommand: CommandHandler = async (params, allowTextCommands) => { + if (!allowTextCommands) { + return null; + } + + const normalized = params.command.commandBodyNormalized; + if (!normalized.startsWith(COMMAND)) { + return null; + } + + if (!params.command.isAuthorizedSender) { + logVerbose(`Ignoring /acp from unauthorized sender: ${params.command.senderId || ""}`); + return { shouldContinue: false }; + } + + const rest = normalized.slice(COMMAND.length).trim(); + const tokens = rest.split(/\s+/).filter(Boolean); + const action = resolveAcpAction(tokens); + if (action === "help") { + return stopWithText(resolveAcpHelpText()); + } + + const handler = ACP_ACTION_HANDLERS[action]; + return handler ? await handler(params, tokens) : stopWithText(resolveAcpHelpText()); +}; diff --git a/src/auto-reply/reply/commands-acp/context.test.ts b/src/auto-reply/reply/commands-acp/context.test.ts new file mode 100644 index 000000000000..92952ad749f1 --- /dev/null +++ b/src/auto-reply/reply/commands-acp/context.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from "vitest"; +import type { OpenClawConfig } from "../../../config/config.js"; +import { buildCommandTestParams } from "../commands-spawn.test-harness.js"; +import { + isAcpCommandDiscordChannel, + resolveAcpCommandBindingContext, + resolveAcpCommandConversationId, +} from "./context.js"; + +const baseCfg = { + session: { mainKey: "main", scope: "per-sender" }, +} satisfies OpenClawConfig; + +describe("commands-acp context", () => { + it("resolves channel/account/thread context from originating fields", () => { + const params = buildCommandTestParams("/acp sessions", baseCfg, { + Provider: "discord", + Surface: "discord", + OriginatingChannel: "discord", + OriginatingTo: "channel:parent-1", + AccountId: "work", + MessageThreadId: "thread-42", + }); + + expect(resolveAcpCommandBindingContext(params)).toEqual({ + channel: "discord", + accountId: "work", + threadId: "thread-42", + conversationId: "thread-42", + }); + expect(isAcpCommandDiscordChannel(params)).toBe(true); + }); + + it("falls back to default account and target-derived conversation id", () => { + const params = buildCommandTestParams("/acp status", baseCfg, { + Provider: "slack", + Surface: "slack", + OriginatingChannel: "slack", + To: "<#123456789>", + }); + + expect(resolveAcpCommandBindingContext(params)).toEqual({ + channel: "slack", + accountId: "default", + threadId: undefined, + conversationId: "123456789", + }); + expect(resolveAcpCommandConversationId(params)).toBe("123456789"); + expect(isAcpCommandDiscordChannel(params)).toBe(false); + }); +}); diff --git a/src/auto-reply/reply/commands-acp/context.ts b/src/auto-reply/reply/commands-acp/context.ts new file mode 100644 index 000000000000..f9ac901ec92e --- /dev/null +++ b/src/auto-reply/reply/commands-acp/context.ts @@ -0,0 +1,58 @@ +import { DISCORD_THREAD_BINDING_CHANNEL } from "../../../channels/thread-bindings-policy.js"; +import { resolveConversationIdFromTargets } from "../../../infra/outbound/conversation-id.js"; +import type { HandleCommandsParams } from "../commands-types.js"; + +function normalizeString(value: unknown): string { + if (typeof value === "string") { + return value.trim(); + } + if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean") { + return `${value}`.trim(); + } + return ""; +} + +export function resolveAcpCommandChannel(params: HandleCommandsParams): string { + const raw = + params.ctx.OriginatingChannel ?? + params.command.channel ?? + params.ctx.Surface ?? + params.ctx.Provider; + return normalizeString(raw).toLowerCase(); +} + +export function resolveAcpCommandAccountId(params: HandleCommandsParams): string { + const accountId = normalizeString(params.ctx.AccountId); + return accountId || "default"; +} + +export function resolveAcpCommandThreadId(params: HandleCommandsParams): string | undefined { + const threadId = + params.ctx.MessageThreadId != null ? normalizeString(String(params.ctx.MessageThreadId)) : ""; + return threadId || undefined; +} + +export function resolveAcpCommandConversationId(params: HandleCommandsParams): string | undefined { + return resolveConversationIdFromTargets({ + threadId: params.ctx.MessageThreadId, + targets: [params.ctx.OriginatingTo, params.command.to, params.ctx.To], + }); +} + +export function isAcpCommandDiscordChannel(params: HandleCommandsParams): boolean { + return resolveAcpCommandChannel(params) === DISCORD_THREAD_BINDING_CHANNEL; +} + +export function resolveAcpCommandBindingContext(params: HandleCommandsParams): { + channel: string; + accountId: string; + threadId?: string; + conversationId?: string; +} { + return { + channel: resolveAcpCommandChannel(params), + accountId: resolveAcpCommandAccountId(params), + threadId: resolveAcpCommandThreadId(params), + conversationId: resolveAcpCommandConversationId(params), + }; +} diff --git a/src/auto-reply/reply/commands-acp/diagnostics.ts b/src/auto-reply/reply/commands-acp/diagnostics.ts new file mode 100644 index 000000000000..d521ac7ae5fd --- /dev/null +++ b/src/auto-reply/reply/commands-acp/diagnostics.ts @@ -0,0 +1,203 @@ +import { getAcpSessionManager } from "../../../acp/control-plane/manager.js"; +import { formatAcpRuntimeErrorText } from "../../../acp/runtime/error-text.js"; +import { toAcpRuntimeError } from "../../../acp/runtime/errors.js"; +import { getAcpRuntimeBackend, requireAcpRuntimeBackend } from "../../../acp/runtime/registry.js"; +import { resolveSessionStorePathForAcp } from "../../../acp/runtime/session-meta.js"; +import { loadSessionStore } from "../../../config/sessions.js"; +import type { SessionEntry } from "../../../config/sessions/types.js"; +import { getSessionBindingService } from "../../../infra/outbound/session-binding-service.js"; +import type { CommandHandlerResult, HandleCommandsParams } from "../commands-types.js"; +import { resolveAcpCommandBindingContext } from "./context.js"; +import { + ACP_DOCTOR_USAGE, + ACP_INSTALL_USAGE, + ACP_SESSIONS_USAGE, + formatAcpCapabilitiesText, + resolveAcpInstallCommandHint, + resolveConfiguredAcpBackendId, + stopWithText, +} from "./shared.js"; +import { resolveBoundAcpThreadSessionKey } from "./targets.js"; + +export async function handleAcpDoctorAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + if (restTokens.length > 0) { + return stopWithText(`⚠️ ${ACP_DOCTOR_USAGE}`); + } + + const backendId = resolveConfiguredAcpBackendId(params.cfg); + const installHint = resolveAcpInstallCommandHint(params.cfg); + const registeredBackend = getAcpRuntimeBackend(backendId); + const managerSnapshot = getAcpSessionManager().getObservabilitySnapshot(params.cfg); + const lines = ["ACP doctor:", "-----", `configuredBackend: ${backendId}`]; + lines.push(`activeRuntimeSessions: ${managerSnapshot.runtimeCache.activeSessions}`); + lines.push(`runtimeIdleTtlMs: ${managerSnapshot.runtimeCache.idleTtlMs}`); + lines.push(`evictedIdleRuntimes: ${managerSnapshot.runtimeCache.evictedTotal}`); + lines.push(`activeTurns: ${managerSnapshot.turns.active}`); + lines.push(`queueDepth: ${managerSnapshot.turns.queueDepth}`); + lines.push( + `turnLatencyMs: avg=${managerSnapshot.turns.averageLatencyMs}, max=${managerSnapshot.turns.maxLatencyMs}`, + ); + lines.push( + `turnCounts: completed=${managerSnapshot.turns.completed}, failed=${managerSnapshot.turns.failed}`, + ); + const errorStatsText = + Object.entries(managerSnapshot.errorsByCode) + .map(([code, count]) => `${code}=${count}`) + .join(", ") || "(none)"; + lines.push(`errorCodes: ${errorStatsText}`); + if (registeredBackend) { + lines.push(`registeredBackend: ${registeredBackend.id}`); + } else { + lines.push("registeredBackend: (none)"); + } + + if (registeredBackend?.runtime.doctor) { + try { + const report = await registeredBackend.runtime.doctor(); + lines.push(`runtimeDoctor: ${report.ok ? "ok" : "error"} (${report.message})`); + if (report.code) { + lines.push(`runtimeDoctorCode: ${report.code}`); + } + if (report.installCommand) { + lines.push(`runtimeDoctorInstall: ${report.installCommand}`); + } + for (const detail of report.details ?? []) { + lines.push(`runtimeDoctorDetail: ${detail}`); + } + } catch (error) { + lines.push( + `runtimeDoctor: error (${ + toAcpRuntimeError({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Runtime doctor failed.", + }).message + })`, + ); + } + } + + try { + const backend = requireAcpRuntimeBackend(backendId); + const capabilities = backend.runtime.getCapabilities + ? await backend.runtime.getCapabilities({}) + : { controls: [] as string[], configOptionKeys: [] as string[] }; + lines.push("healthy: yes"); + lines.push(`capabilities: ${formatAcpCapabilitiesText(capabilities.controls ?? [])}`); + if ((capabilities.configOptionKeys?.length ?? 0) > 0) { + lines.push(`configKeys: ${capabilities.configOptionKeys?.join(", ")}`); + } + return stopWithText(lines.join("\n")); + } catch (error) { + const acpError = toAcpRuntimeError({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP backend doctor failed.", + }); + lines.push("healthy: no"); + lines.push(formatAcpRuntimeErrorText(acpError)); + lines.push(`next: ${installHint}`); + lines.push(`next: openclaw config set plugins.entries.${backendId}.enabled true`); + if (backendId.toLowerCase() === "acpx") { + lines.push("next: verify acpx is installed (`acpx --help`)."); + } + return stopWithText(lines.join("\n")); + } +} + +export function handleAcpInstallAction( + params: HandleCommandsParams, + restTokens: string[], +): CommandHandlerResult { + if (restTokens.length > 0) { + return stopWithText(`⚠️ ${ACP_INSTALL_USAGE}`); + } + const backendId = resolveConfiguredAcpBackendId(params.cfg); + const installHint = resolveAcpInstallCommandHint(params.cfg); + const lines = [ + "ACP install:", + "-----", + `configuredBackend: ${backendId}`, + `run: ${installHint}`, + `then: openclaw config set plugins.entries.${backendId}.enabled true`, + "then: /acp doctor", + ]; + return stopWithText(lines.join("\n")); +} + +function formatAcpSessionLine(params: { + key: string; + entry: SessionEntry; + currentSessionKey?: string; + threadId?: string; +}): string { + const acp = params.entry.acp; + if (!acp) { + return ""; + } + const marker = params.currentSessionKey === params.key ? "*" : " "; + const label = params.entry.label?.trim() || acp.agent; + const threadText = params.threadId ? `, thread:${params.threadId}` : ""; + return `${marker} ${label} (${acp.mode}, ${acp.state}, backend:${acp.backend}${threadText}) -> ${params.key}`; +} + +export function handleAcpSessionsAction( + params: HandleCommandsParams, + restTokens: string[], +): CommandHandlerResult { + if (restTokens.length > 0) { + return stopWithText(ACP_SESSIONS_USAGE); + } + + const currentSessionKey = resolveBoundAcpThreadSessionKey(params) || params.sessionKey; + if (!currentSessionKey) { + return stopWithText("⚠️ Missing session key."); + } + + const { storePath } = resolveSessionStorePathForAcp({ + cfg: params.cfg, + sessionKey: currentSessionKey, + }); + + let store: Record; + try { + store = loadSessionStore(storePath); + } catch { + store = {}; + } + + const bindingContext = resolveAcpCommandBindingContext(params); + const normalizedChannel = bindingContext.channel; + const normalizedAccountId = bindingContext.accountId || undefined; + const bindingService = getSessionBindingService(); + + const rows = Object.entries(store) + .filter(([, entry]) => Boolean(entry?.acp)) + .toSorted(([, a], [, b]) => (b?.updatedAt ?? 0) - (a?.updatedAt ?? 0)) + .slice(0, 20) + .map(([key, entry]) => { + const bindingThreadId = bindingService + .listBySession(key) + .find( + (binding) => + (!normalizedChannel || binding.conversation.channel === normalizedChannel) && + (!normalizedAccountId || binding.conversation.accountId === normalizedAccountId), + )?.conversation.conversationId; + return formatAcpSessionLine({ + key, + entry, + currentSessionKey, + threadId: bindingThreadId, + }); + }) + .filter(Boolean); + + if (rows.length === 0) { + return stopWithText("ACP sessions:\n-----\n(none)"); + } + + return stopWithText(["ACP sessions:", "-----", ...rows].join("\n")); +} diff --git a/src/auto-reply/reply/commands-acp/lifecycle.ts b/src/auto-reply/reply/commands-acp/lifecycle.ts new file mode 100644 index 000000000000..9039cfe64e00 --- /dev/null +++ b/src/auto-reply/reply/commands-acp/lifecycle.ts @@ -0,0 +1,588 @@ +import { randomUUID } from "node:crypto"; +import { getAcpSessionManager } from "../../../acp/control-plane/manager.js"; +import { + cleanupFailedAcpSpawn, + type AcpSpawnRuntimeCloseHandle, +} from "../../../acp/control-plane/spawn.js"; +import { + isAcpEnabledByPolicy, + resolveAcpAgentPolicyError, + resolveAcpDispatchPolicyError, + resolveAcpDispatchPolicyMessage, +} from "../../../acp/policy.js"; +import { AcpRuntimeError } from "../../../acp/runtime/errors.js"; +import { + resolveAcpSessionCwd, + resolveAcpThreadSessionDetailLines, +} from "../../../acp/runtime/session-identifiers.js"; +import { + resolveThreadBindingIntroText, + resolveThreadBindingThreadName, +} from "../../../channels/thread-bindings-messages.js"; +import { + formatThreadBindingDisabledError, + formatThreadBindingSpawnDisabledError, + resolveThreadBindingSessionTtlMsForChannel, + resolveThreadBindingSpawnPolicy, +} from "../../../channels/thread-bindings-policy.js"; +import type { OpenClawConfig } from "../../../config/config.js"; +import type { SessionAcpMeta } from "../../../config/sessions/types.js"; +import { callGateway } from "../../../gateway/call.js"; +import { + getSessionBindingService, + type SessionBindingRecord, +} from "../../../infra/outbound/session-binding-service.js"; +import type { CommandHandlerResult, HandleCommandsParams } from "../commands-types.js"; +import { + resolveAcpCommandAccountId, + resolveAcpCommandBindingContext, + resolveAcpCommandThreadId, +} from "./context.js"; +import { + ACP_STEER_OUTPUT_LIMIT, + collectAcpErrorText, + parseSpawnInput, + parseSteerInput, + resolveCommandRequestId, + stopWithText, + type AcpSpawnThreadMode, + withAcpCommandErrorBoundary, +} from "./shared.js"; +import { resolveAcpTargetSessionKey } from "./targets.js"; + +async function bindSpawnedAcpSessionToThread(params: { + commandParams: HandleCommandsParams; + sessionKey: string; + agentId: string; + label?: string; + threadMode: AcpSpawnThreadMode; + sessionMeta?: SessionAcpMeta; +}): Promise<{ ok: true; binding: SessionBindingRecord } | { ok: false; error: string }> { + const { commandParams, threadMode } = params; + if (threadMode === "off") { + return { + ok: false, + error: "internal: thread binding is disabled for this spawn", + }; + } + + const bindingContext = resolveAcpCommandBindingContext(commandParams); + const channel = bindingContext.channel; + if (!channel) { + return { + ok: false, + error: "ACP thread binding requires a channel context.", + }; + } + + const accountId = resolveAcpCommandAccountId(commandParams); + const spawnPolicy = resolveThreadBindingSpawnPolicy({ + cfg: commandParams.cfg, + channel, + accountId, + kind: "acp", + }); + if (!spawnPolicy.enabled) { + return { + ok: false, + error: formatThreadBindingDisabledError({ + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + kind: "acp", + }), + }; + } + if (!spawnPolicy.spawnEnabled) { + return { + ok: false, + error: formatThreadBindingSpawnDisabledError({ + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + kind: "acp", + }), + }; + } + + const bindingService = getSessionBindingService(); + const capabilities = bindingService.getCapabilities({ + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + }); + if (!capabilities.adapterAvailable) { + return { + ok: false, + error: `Thread bindings are unavailable for ${channel}.`, + }; + } + if (!capabilities.bindSupported) { + return { + ok: false, + error: `Thread bindings are unavailable for ${channel}.`, + }; + } + + const currentThreadId = bindingContext.threadId ?? ""; + + if (threadMode === "here" && !currentThreadId) { + return { + ok: false, + error: `--thread here requires running /acp spawn inside an active ${channel} thread/conversation.`, + }; + } + + const threadId = currentThreadId || undefined; + const placement = threadId ? "current" : "child"; + if (!capabilities.placements.includes(placement)) { + return { + ok: false, + error: `Thread bindings do not support ${placement} placement for ${channel}.`, + }; + } + const channelId = placement === "child" ? bindingContext.conversationId : undefined; + + if (placement === "child" && !channelId) { + return { + ok: false, + error: `Could not resolve a ${channel} conversation for ACP thread spawn.`, + }; + } + + const senderId = commandParams.command.senderId?.trim() || ""; + if (threadId) { + const existingBinding = bindingService.resolveByConversation({ + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + conversationId: threadId, + }); + const boundBy = + typeof existingBinding?.metadata?.boundBy === "string" + ? existingBinding.metadata.boundBy.trim() + : ""; + if (existingBinding && boundBy && boundBy !== "system" && senderId && senderId !== boundBy) { + return { + ok: false, + error: `Only ${boundBy} can rebind this thread.`, + }; + } + } + + const label = params.label || params.agentId; + const conversationId = threadId || channelId; + if (!conversationId) { + return { + ok: false, + error: `Could not resolve a ${channel} conversation for ACP thread spawn.`, + }; + } + + try { + const binding = await bindingService.bind({ + targetSessionKey: params.sessionKey, + targetKind: "session", + conversation: { + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + conversationId, + }, + placement, + metadata: { + threadName: resolveThreadBindingThreadName({ + agentId: params.agentId, + label, + }), + agentId: params.agentId, + label, + boundBy: senderId || "unknown", + introText: resolveThreadBindingIntroText({ + agentId: params.agentId, + label, + sessionTtlMs: resolveThreadBindingSessionTtlMsForChannel({ + cfg: commandParams.cfg, + channel: spawnPolicy.channel, + accountId: spawnPolicy.accountId, + }), + sessionCwd: resolveAcpSessionCwd(params.sessionMeta), + sessionDetails: resolveAcpThreadSessionDetailLines({ + sessionKey: params.sessionKey, + meta: params.sessionMeta, + }), + }), + }, + }); + return { + ok: true, + binding, + }; + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + return { + ok: false, + error: message || `Failed to bind a ${channel} thread/conversation to the new ACP session.`, + }; + } +} + +async function cleanupFailedSpawn(params: { + cfg: OpenClawConfig; + sessionKey: string; + shouldDeleteSession: boolean; + initializedRuntime?: AcpSpawnRuntimeCloseHandle; +}) { + await cleanupFailedAcpSpawn({ + cfg: params.cfg, + sessionKey: params.sessionKey, + shouldDeleteSession: params.shouldDeleteSession, + deleteTranscript: false, + runtimeCloseHandle: params.initializedRuntime, + }); +} + +export async function handleAcpSpawnAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + if (!isAcpEnabledByPolicy(params.cfg)) { + return stopWithText("ACP is disabled by policy (`acp.enabled=false`)."); + } + + const parsed = parseSpawnInput(params, restTokens); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + + const spawn = parsed.value; + const agentPolicyError = resolveAcpAgentPolicyError(params.cfg, spawn.agentId); + if (agentPolicyError) { + return stopWithText( + collectAcpErrorText({ + error: agentPolicyError, + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "ACP target agent is not allowed by policy.", + }), + ); + } + + const acpManager = getAcpSessionManager(); + const sessionKey = `agent:${spawn.agentId}:acp:${randomUUID()}`; + + let initializedBackend = ""; + let initializedMeta: SessionAcpMeta | undefined; + let initializedRuntime: AcpSpawnRuntimeCloseHandle | undefined; + try { + const initialized = await acpManager.initializeSession({ + cfg: params.cfg, + sessionKey, + agent: spawn.agentId, + mode: spawn.mode, + cwd: spawn.cwd, + }); + initializedRuntime = { + runtime: initialized.runtime, + handle: initialized.handle, + }; + initializedBackend = initialized.handle.backend || initialized.meta.backend; + initializedMeta = initialized.meta; + } catch (err) { + return stopWithText( + collectAcpErrorText({ + error: err, + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Could not initialize ACP session runtime.", + }), + ); + } + + let binding: SessionBindingRecord | null = null; + if (spawn.thread !== "off") { + const bound = await bindSpawnedAcpSessionToThread({ + commandParams: params, + sessionKey, + agentId: spawn.agentId, + label: spawn.label, + threadMode: spawn.thread, + sessionMeta: initializedMeta, + }); + if (!bound.ok) { + await cleanupFailedSpawn({ + cfg: params.cfg, + sessionKey, + shouldDeleteSession: true, + initializedRuntime, + }); + return stopWithText(`⚠️ ${bound.error}`); + } + binding = bound.binding; + } + + try { + await callGateway({ + method: "sessions.patch", + params: { + key: sessionKey, + ...(spawn.label ? { label: spawn.label } : {}), + }, + timeoutMs: 10_000, + }); + } catch (err) { + await cleanupFailedSpawn({ + cfg: params.cfg, + sessionKey, + shouldDeleteSession: true, + initializedRuntime, + }); + const message = err instanceof Error ? err.message : String(err); + return stopWithText(`⚠️ ACP spawn failed: ${message}`); + } + + const parts = [ + `✅ Spawned ACP session ${sessionKey} (${spawn.mode}, backend ${initializedBackend}).`, + ]; + if (binding) { + const currentThreadId = resolveAcpCommandThreadId(params) ?? ""; + const boundConversationId = binding.conversation.conversationId.trim(); + if (currentThreadId && boundConversationId === currentThreadId) { + parts.push(`Bound this thread to ${sessionKey}.`); + } else { + parts.push(`Created thread ${boundConversationId} and bound it to ${sessionKey}.`); + } + } else { + parts.push("Session is unbound (use /focus to bind this thread/conversation)."); + } + + const dispatchNote = resolveAcpDispatchPolicyMessage(params.cfg); + if (dispatchNote) { + parts.push(`ℹ️ ${dispatchNote}`); + } + + return stopWithText(parts.join(" ")); +} + +export async function handleAcpCancelAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const acpManager = getAcpSessionManager(); + const token = restTokens.join(" ").trim() || undefined; + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + const resolved = acpManager.resolveSession({ + cfg: params.cfg, + sessionKey: target.sessionKey, + }); + if (resolved.kind === "none") { + return stopWithText( + collectAcpErrorText({ + error: new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${target.sessionKey}`, + ), + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Session is not ACP-enabled.", + }), + ); + } + if (resolved.kind === "stale") { + return stopWithText( + collectAcpErrorText({ + error: resolved.error, + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: resolved.error.message, + }), + ); + } + + return await withAcpCommandErrorBoundary({ + run: async () => + await acpManager.cancelSession({ + cfg: params.cfg, + sessionKey: target.sessionKey, + reason: "manual-cancel", + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP cancel failed before completion.", + onSuccess: () => stopWithText(`✅ Cancel requested for ACP session ${target.sessionKey}.`), + }); +} + +async function runAcpSteer(params: { + cfg: OpenClawConfig; + sessionKey: string; + instruction: string; + requestId: string; +}): Promise { + const acpManager = getAcpSessionManager(); + let output = ""; + + await acpManager.runTurn({ + cfg: params.cfg, + sessionKey: params.sessionKey, + text: params.instruction, + mode: "steer", + requestId: params.requestId, + onEvent: (event) => { + if (event.type !== "text_delta") { + return; + } + if (event.stream && event.stream !== "output") { + return; + } + if (event.text) { + output += event.text; + if (output.length > ACP_STEER_OUTPUT_LIMIT) { + output = `${output.slice(0, ACP_STEER_OUTPUT_LIMIT)}…`; + } + } + }, + }); + return output.trim(); +} + +export async function handleAcpSteerAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const dispatchPolicyError = resolveAcpDispatchPolicyError(params.cfg); + if (dispatchPolicyError) { + return stopWithText( + collectAcpErrorText({ + error: dispatchPolicyError, + fallbackCode: "ACP_DISPATCH_DISABLED", + fallbackMessage: dispatchPolicyError.message, + }), + ); + } + + const parsed = parseSteerInput(restTokens); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const acpManager = getAcpSessionManager(); + + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + const resolved = acpManager.resolveSession({ + cfg: params.cfg, + sessionKey: target.sessionKey, + }); + if (resolved.kind === "none") { + return stopWithText( + collectAcpErrorText({ + error: new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${target.sessionKey}`, + ), + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Session is not ACP-enabled.", + }), + ); + } + if (resolved.kind === "stale") { + return stopWithText( + collectAcpErrorText({ + error: resolved.error, + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: resolved.error.message, + }), + ); + } + + return await withAcpCommandErrorBoundary({ + run: async () => + await runAcpSteer({ + cfg: params.cfg, + sessionKey: target.sessionKey, + instruction: parsed.value.instruction, + requestId: `${resolveCommandRequestId(params)}:steer`, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP steer failed before completion.", + onSuccess: (steerOutput) => { + if (!steerOutput) { + return stopWithText(`✅ ACP steer sent to ${target.sessionKey}.`); + } + return stopWithText(`✅ ACP steer sent to ${target.sessionKey}.\n${steerOutput}`); + }, + }); +} + +export async function handleAcpCloseAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const acpManager = getAcpSessionManager(); + const token = restTokens.join(" ").trim() || undefined; + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + const resolved = acpManager.resolveSession({ + cfg: params.cfg, + sessionKey: target.sessionKey, + }); + if (resolved.kind === "none") { + return stopWithText( + collectAcpErrorText({ + error: new AcpRuntimeError( + "ACP_SESSION_INIT_FAILED", + `Session is not ACP-enabled: ${target.sessionKey}`, + ), + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: "Session is not ACP-enabled.", + }), + ); + } + if (resolved.kind === "stale") { + return stopWithText( + collectAcpErrorText({ + error: resolved.error, + fallbackCode: "ACP_SESSION_INIT_FAILED", + fallbackMessage: resolved.error.message, + }), + ); + } + + let runtimeNotice = ""; + try { + const closed = await acpManager.closeSession({ + cfg: params.cfg, + sessionKey: target.sessionKey, + reason: "manual-close", + allowBackendUnavailable: true, + clearMeta: true, + }); + runtimeNotice = closed.runtimeNotice ? ` (${closed.runtimeNotice})` : ""; + } catch (error) { + return stopWithText( + collectAcpErrorText({ + error, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "ACP close failed before completion.", + }), + ); + } + + const removedBindings = await getSessionBindingService().unbind({ + targetSessionKey: target.sessionKey, + reason: "manual", + }); + + return stopWithText( + `✅ Closed ACP session ${target.sessionKey}${runtimeNotice}. Removed ${removedBindings.length} binding${removedBindings.length === 1 ? "" : "s"}.`, + ); +} diff --git a/src/auto-reply/reply/commands-acp/runtime-options.ts b/src/auto-reply/reply/commands-acp/runtime-options.ts new file mode 100644 index 000000000000..359b712e0e33 --- /dev/null +++ b/src/auto-reply/reply/commands-acp/runtime-options.ts @@ -0,0 +1,348 @@ +import { getAcpSessionManager } from "../../../acp/control-plane/manager.js"; +import { + parseRuntimeTimeoutSecondsInput, + validateRuntimeConfigOptionInput, + validateRuntimeCwdInput, + validateRuntimeModeInput, + validateRuntimeModelInput, + validateRuntimePermissionProfileInput, +} from "../../../acp/control-plane/runtime-options.js"; +import { resolveAcpSessionIdentifierLinesFromIdentity } from "../../../acp/runtime/session-identifiers.js"; +import type { CommandHandlerResult, HandleCommandsParams } from "../commands-types.js"; +import { + ACP_CWD_USAGE, + ACP_MODEL_USAGE, + ACP_PERMISSIONS_USAGE, + ACP_RESET_OPTIONS_USAGE, + ACP_SET_MODE_USAGE, + ACP_STATUS_USAGE, + ACP_TIMEOUT_USAGE, + formatAcpCapabilitiesText, + formatRuntimeOptionsText, + parseOptionalSingleTarget, + parseSetCommandInput, + parseSingleValueCommandInput, + stopWithText, + withAcpCommandErrorBoundary, +} from "./shared.js"; +import { resolveAcpTargetSessionKey } from "./targets.js"; + +export async function handleAcpStatusAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseOptionalSingleTarget(restTokens, ACP_STATUS_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + return await withAcpCommandErrorBoundary({ + run: async () => + await getAcpSessionManager().getSessionStatus({ + cfg: params.cfg, + sessionKey: target.sessionKey, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not read ACP session status.", + onSuccess: (status) => { + const sessionIdentifierLines = resolveAcpSessionIdentifierLinesFromIdentity({ + backend: status.backend, + identity: status.identity, + }); + const lines = [ + "ACP status:", + "-----", + `session: ${status.sessionKey}`, + `backend: ${status.backend}`, + `agent: ${status.agent}`, + ...sessionIdentifierLines, + `sessionMode: ${status.mode}`, + `state: ${status.state}`, + `runtimeOptions: ${formatRuntimeOptionsText(status.runtimeOptions)}`, + `capabilities: ${formatAcpCapabilitiesText(status.capabilities.controls)}`, + `lastActivityAt: ${new Date(status.lastActivityAt).toISOString()}`, + ...(status.lastError ? [`lastError: ${status.lastError}`] : []), + ...(status.runtimeStatus?.summary ? [`runtime: ${status.runtimeStatus.summary}`] : []), + ...(status.runtimeStatus?.details + ? [`runtimeDetails: ${JSON.stringify(status.runtimeStatus.details)}`] + : []), + ]; + return stopWithText(lines.join("\n")); + }, + }); +} + +export async function handleAcpSetModeAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSingleValueCommandInput(restTokens, ACP_SET_MODE_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + return await withAcpCommandErrorBoundary({ + run: async () => { + const runtimeMode = validateRuntimeModeInput(parsed.value.value); + const options = await getAcpSessionManager().setSessionRuntimeMode({ + cfg: params.cfg, + sessionKey: target.sessionKey, + runtimeMode, + }); + return { + runtimeMode, + options, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP runtime mode.", + onSuccess: ({ runtimeMode, options }) => + stopWithText( + `✅ Updated ACP runtime mode for ${target.sessionKey}: ${runtimeMode}. Effective options: ${formatRuntimeOptionsText(options)}`, + ), + }); +} + +export async function handleAcpSetAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSetCommandInput(restTokens); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + const key = parsed.value.key.trim(); + const value = parsed.value.value.trim(); + + return await withAcpCommandErrorBoundary({ + run: async () => { + const lowerKey = key.toLowerCase(); + if (lowerKey === "cwd") { + const cwd = validateRuntimeCwdInput(value); + const options = await getAcpSessionManager().updateSessionRuntimeOptions({ + cfg: params.cfg, + sessionKey: target.sessionKey, + patch: { cwd }, + }); + return { + text: `✅ Updated ACP cwd for ${target.sessionKey}: ${cwd}. Effective options: ${formatRuntimeOptionsText(options)}`, + }; + } + const validated = validateRuntimeConfigOptionInput(key, value); + const options = await getAcpSessionManager().setSessionConfigOption({ + cfg: params.cfg, + sessionKey: target.sessionKey, + key: validated.key, + value: validated.value, + }); + return { + text: `✅ Updated ACP config option for ${target.sessionKey}: ${validated.key}=${validated.value}. Effective options: ${formatRuntimeOptionsText(options)}`, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP config option.", + onSuccess: ({ text }) => stopWithText(text), + }); +} + +export async function handleAcpCwdAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSingleValueCommandInput(restTokens, ACP_CWD_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + return await withAcpCommandErrorBoundary({ + run: async () => { + const cwd = validateRuntimeCwdInput(parsed.value.value); + const options = await getAcpSessionManager().updateSessionRuntimeOptions({ + cfg: params.cfg, + sessionKey: target.sessionKey, + patch: { cwd }, + }); + return { + cwd, + options, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP cwd.", + onSuccess: ({ cwd, options }) => + stopWithText( + `✅ Updated ACP cwd for ${target.sessionKey}: ${cwd}. Effective options: ${formatRuntimeOptionsText(options)}`, + ), + }); +} + +export async function handleAcpPermissionsAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSingleValueCommandInput(restTokens, ACP_PERMISSIONS_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + return await withAcpCommandErrorBoundary({ + run: async () => { + const permissionProfile = validateRuntimePermissionProfileInput(parsed.value.value); + const options = await getAcpSessionManager().setSessionConfigOption({ + cfg: params.cfg, + sessionKey: target.sessionKey, + key: "approval_policy", + value: permissionProfile, + }); + return { + permissionProfile, + options, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP permissions profile.", + onSuccess: ({ permissionProfile, options }) => + stopWithText( + `✅ Updated ACP permissions profile for ${target.sessionKey}: ${permissionProfile}. Effective options: ${formatRuntimeOptionsText(options)}`, + ), + }); +} + +export async function handleAcpTimeoutAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSingleValueCommandInput(restTokens, ACP_TIMEOUT_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + return await withAcpCommandErrorBoundary({ + run: async () => { + const timeoutSeconds = parseRuntimeTimeoutSecondsInput(parsed.value.value); + const options = await getAcpSessionManager().setSessionConfigOption({ + cfg: params.cfg, + sessionKey: target.sessionKey, + key: "timeout", + value: String(timeoutSeconds), + }); + return { + timeoutSeconds, + options, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP timeout.", + onSuccess: ({ timeoutSeconds, options }) => + stopWithText( + `✅ Updated ACP timeout for ${target.sessionKey}: ${timeoutSeconds}s. Effective options: ${formatRuntimeOptionsText(options)}`, + ), + }); +} + +export async function handleAcpModelAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseSingleValueCommandInput(restTokens, ACP_MODEL_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.value.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + return await withAcpCommandErrorBoundary({ + run: async () => { + const model = validateRuntimeModelInput(parsed.value.value); + const options = await getAcpSessionManager().setSessionConfigOption({ + cfg: params.cfg, + sessionKey: target.sessionKey, + key: "model", + value: model, + }); + return { + model, + options, + }; + }, + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not update ACP model.", + onSuccess: ({ model, options }) => + stopWithText( + `✅ Updated ACP model for ${target.sessionKey}: ${model}. Effective options: ${formatRuntimeOptionsText(options)}`, + ), + }); +} + +export async function handleAcpResetOptionsAction( + params: HandleCommandsParams, + restTokens: string[], +): Promise { + const parsed = parseOptionalSingleTarget(restTokens, ACP_RESET_OPTIONS_USAGE); + if (!parsed.ok) { + return stopWithText(`⚠️ ${parsed.error}`); + } + const target = await resolveAcpTargetSessionKey({ + commandParams: params, + token: parsed.sessionToken, + }); + if (!target.ok) { + return stopWithText(`⚠️ ${target.error}`); + } + + return await withAcpCommandErrorBoundary({ + run: async () => + await getAcpSessionManager().resetSessionRuntimeOptions({ + cfg: params.cfg, + sessionKey: target.sessionKey, + }), + fallbackCode: "ACP_TURN_FAILED", + fallbackMessage: "Could not reset ACP runtime options.", + onSuccess: () => stopWithText(`✅ Reset ACP runtime options for ${target.sessionKey}.`), + }); +} diff --git a/src/auto-reply/reply/commands-acp/shared.ts b/src/auto-reply/reply/commands-acp/shared.ts new file mode 100644 index 000000000000..adf31247b6da --- /dev/null +++ b/src/auto-reply/reply/commands-acp/shared.ts @@ -0,0 +1,500 @@ +import { randomUUID } from "node:crypto"; +import { existsSync } from "node:fs"; +import path from "node:path"; +import { toAcpRuntimeErrorText } from "../../../acp/runtime/error-text.js"; +import type { AcpRuntimeError } from "../../../acp/runtime/errors.js"; +import type { AcpRuntimeSessionMode } from "../../../acp/runtime/types.js"; +import { DISCORD_THREAD_BINDING_CHANNEL } from "../../../channels/thread-bindings-policy.js"; +import type { OpenClawConfig } from "../../../config/config.js"; +import type { AcpSessionRuntimeOptions } from "../../../config/sessions/types.js"; +import { normalizeAgentId } from "../../../routing/session-key.js"; +import type { CommandHandlerResult, HandleCommandsParams } from "../commands-types.js"; +import { resolveAcpCommandChannel, resolveAcpCommandThreadId } from "./context.js"; + +export const COMMAND = "/acp"; +export const ACP_SPAWN_USAGE = + "Usage: /acp spawn [agentId] [--mode persistent|oneshot] [--thread auto|here|off] [--cwd ] [--label