@slack/radar-mcp 1.22.0 → 1.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -12
- package/dist/mcp/dashboard-identity.d.ts +10 -0
- package/dist/mcp/dashboard-identity.js +20 -0
- package/dist/mcp/debug-package-reset.d.ts +5 -0
- package/dist/mcp/debug-package-reset.js +28 -0
- package/dist/mcp/flags-tools.d.ts +2 -0
- package/dist/mcp/flags-tools.js +15 -1
- package/dist/mcp/index.js +172 -142
- package/dist/mcp/radar-http.d.ts +22 -0
- package/dist/mcp/radar-http.js +63 -0
- package/dist/mcp/radar-response.d.ts +1 -0
- package/dist/mcp/session-enable.d.ts +15 -0
- package/dist/mcp/session-enable.js +195 -33
- package/dist/mcp/sources-gate.js +5 -0
- package/dist/mcp/switchboard-tools.d.ts +5 -2
- package/dist/mcp/switchboard-tools.js +18 -12
- package/dist/mcp/test-cases.d.ts +14 -0
- package/dist/mcp/test-cases.js +77 -0
- package/dist/mcp/test-runs-tools.d.ts +8 -0
- package/dist/mcp/test-runs-tools.js +95 -0
- package/dist/mcp/tools.js +106 -171
- package/dist/mcp/web-child-env.js +2 -1
- package/dist/shared/android.d.ts +75 -10
- package/dist/shared/android.js +352 -109
- package/dist/shared/constants.d.ts +17 -5
- package/dist/shared/constants.js +59 -5
- package/dist/shared/db.d.ts +1 -1
- package/dist/shared/db.js +21 -14
- package/dist/shared/debug-log.js +2 -4
- package/dist/shared/enable-diagnosis.d.ts +27 -1
- package/dist/shared/enable-diagnosis.js +73 -0
- package/dist/shared/log-session.d.ts +2 -1
- package/dist/shared/logcat-capture.js +17 -1
- package/dist/shared/prefs.js +4 -5
- package/dist/shared/radar-request-raw.js +3 -1
- package/dist/shared/switchboard.d.ts +1 -0
- package/dist/shared/switchboard.js +3 -1
- package/dist/shared/transport.d.ts +30 -0
- package/dist/web/install-app.d.ts +18 -0
- package/dist/web/install-app.js +60 -7
- package/dist/web/macapp/RadarApp.swift +147 -1
- package/dist/web/server.d.ts +17 -0
- package/dist/web/server.js +180 -35
- package/package.json +1 -1
|
@@ -1436,11 +1436,47 @@ func triggerSelfHealCheck() {
|
|
|
1436
1436
|
}
|
|
1437
1437
|
}
|
|
1438
1438
|
|
|
1439
|
+
/// Runs `install-app <flag>` from the installed Radar and returns the JSON object it printed as its
|
|
1440
|
+
/// last line, or nil on failure. Blocks (drains the pipe before waiting), so call it off the main thread.
|
|
1441
|
+
func runInstalledRadar(_ flag: String) -> [String: Any]? {
|
|
1442
|
+
guard let node = resolveNodePath(RADAR_NODE) else {
|
|
1443
|
+
appendLaunchLog("update: node was not at the baked path \(RADAR_NODE), cannot run \(flag).")
|
|
1444
|
+
return nil
|
|
1445
|
+
}
|
|
1446
|
+
let task = Process()
|
|
1447
|
+
task.executableURL = URL(fileURLWithPath: node)
|
|
1448
|
+
task.arguments = [RADAR_MCP_BIN, "install-app", flag]
|
|
1449
|
+
var env = ProcessInfo.processInfo.environment
|
|
1450
|
+
env["SLACK_RADAR_WEB_PORT"] = String(radarPort)
|
|
1451
|
+
env["RADAR_APP_VERSION"] = RADAR_APP_VERSION
|
|
1452
|
+
task.environment = env
|
|
1453
|
+
let output = Pipe()
|
|
1454
|
+
task.standardOutput = output
|
|
1455
|
+
task.standardError = radarLogHandle() ?? FileHandle.nullDevice
|
|
1456
|
+
do {
|
|
1457
|
+
try task.run()
|
|
1458
|
+
} catch {
|
|
1459
|
+
appendLaunchLog("update: could not start \(flag): \(error)")
|
|
1460
|
+
return nil
|
|
1461
|
+
}
|
|
1462
|
+
let data = output.fileHandleForReading.readDataToEndOfFile()
|
|
1463
|
+
task.waitUntilExit()
|
|
1464
|
+
radarLogHandle()?.write(data)
|
|
1465
|
+
guard let text = String(data: data, encoding: .utf8),
|
|
1466
|
+
let line = text.split(separator: "\n").last(where: { $0.hasPrefix("{") }),
|
|
1467
|
+
let object = try? JSONSerialization.jsonObject(with: Data(line.utf8)) as? [String: Any]
|
|
1468
|
+
else {
|
|
1469
|
+
appendLaunchLog("update: \(flag) printed no JSON result.")
|
|
1470
|
+
return nil
|
|
1471
|
+
}
|
|
1472
|
+
return object
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1439
1475
|
/// The launch attempt that runs before the window exists (see the bottom of this file), whose load is
|
|
1440
1476
|
/// the window's first.
|
|
1441
1477
|
let firstLaunch = LaunchTicket()
|
|
1442
1478
|
|
|
1443
|
-
class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKDownloadDelegate, WKScriptMessageHandler {
|
|
1479
|
+
class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKDownloadDelegate, WKScriptMessageHandler, NSMenuItemValidation {
|
|
1444
1480
|
var window: NSWindow!
|
|
1445
1481
|
var webView: WKWebView!
|
|
1446
1482
|
// The load that ended a launch attempt, and which attempt that was: the only navigation that
|
|
@@ -1535,6 +1571,114 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKDown
|
|
|
1535
1571
|
NSApp.activate(ignoringOtherApps: true)
|
|
1536
1572
|
}
|
|
1537
1573
|
|
|
1574
|
+
// Main thread only. Keeps a second click from starting a second check or install while one is running.
|
|
1575
|
+
private var updateInProgress = false
|
|
1576
|
+
|
|
1577
|
+
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
1578
|
+
if menuItem.action == #selector(checkForUpdates(_:)) { return !updateInProgress }
|
|
1579
|
+
return true
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
@objc func checkForUpdates(_ sender: Any?) {
|
|
1583
|
+
updateInProgress = true
|
|
1584
|
+
DispatchQueue.global().async {
|
|
1585
|
+
let check = runInstalledRadar("--check-update")
|
|
1586
|
+
DispatchQueue.main.async { self.presentUpdateCheck(check) }
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
/// Ends an update attempt (including a successful "up to date" check) with a one-button alert.
|
|
1591
|
+
/// Capitalizes detail's first letter once here, so a lowercase error reads right either way.
|
|
1592
|
+
private func endUpdate(_ message: String, _ detail: String) {
|
|
1593
|
+
updateInProgress = false
|
|
1594
|
+
let alert = NSAlert()
|
|
1595
|
+
alert.messageText = message
|
|
1596
|
+
alert.informativeText = detail.prefix(1).uppercased() + detail.dropFirst()
|
|
1597
|
+
alert.addButton(withTitle: "OK")
|
|
1598
|
+
alert.runModal()
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
/// A two-button yes/no alert, shared by the restart and the install confirmations below.
|
|
1602
|
+
private func confirm(_ title: String, _ detail: String, _ button: String) -> Bool {
|
|
1603
|
+
let alert = NSAlert()
|
|
1604
|
+
alert.messageText = title
|
|
1605
|
+
alert.informativeText = detail
|
|
1606
|
+
alert.addButton(withTitle: button)
|
|
1607
|
+
alert.addButton(withTitle: "Not Now")
|
|
1608
|
+
return alert.runModal() == .alertFirstButtonReturn
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
private func presentUpdateCheck(_ check: [String: Any]?) {
|
|
1612
|
+
guard let check = check else {
|
|
1613
|
+
endUpdate("Could not check for updates", "Radar could not run its update check. See ~/Library/Logs/slack-radar-dashboard.log for the log.")
|
|
1614
|
+
return
|
|
1615
|
+
}
|
|
1616
|
+
if let error = check["error"] as? String {
|
|
1617
|
+
endUpdate("Could not check for updates", error)
|
|
1618
|
+
return
|
|
1619
|
+
}
|
|
1620
|
+
let installed = check["installed"] as? String ?? "unknown"
|
|
1621
|
+
// The launch-time self-heal already rebuilt the installed bundle while this window kept running
|
|
1622
|
+
// the older build it launched with: no install needed, just a restart to pick it up.
|
|
1623
|
+
if check["restartAvailable"] as? Bool == true {
|
|
1624
|
+
guard confirm("Slack Radar \(installed) is already installed", "A background update already finished. Restart to use it now?", "Restart Now") else {
|
|
1625
|
+
updateInProgress = false
|
|
1626
|
+
return
|
|
1627
|
+
}
|
|
1628
|
+
relaunch()
|
|
1629
|
+
return
|
|
1630
|
+
}
|
|
1631
|
+
guard check["updateAvailable"] as? Bool == true, let latest = check["latest"] as? String else {
|
|
1632
|
+
endUpdate("Slack Radar is up to date", "You have the latest version (\(installed)).")
|
|
1633
|
+
return
|
|
1634
|
+
}
|
|
1635
|
+
let detail = "You have \(installed). Install the update now? Radar will restart when it finishes."
|
|
1636
|
+
guard confirm("Slack Radar \(latest) is available", detail, "Install and Restart") else {
|
|
1637
|
+
updateInProgress = false
|
|
1638
|
+
return
|
|
1639
|
+
}
|
|
1640
|
+
installUpdate()
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
/// A minimal sheet so Install shows something for the up-to-a-minute rebuild instead of going
|
|
1644
|
+
/// quiet until it relaunches or fails. No real buttons: the hidden one just lets NSAlert show.
|
|
1645
|
+
private func installUpdate() {
|
|
1646
|
+
let progress = NSAlert()
|
|
1647
|
+
progress.messageText = "Installing Slack Radar"
|
|
1648
|
+
progress.informativeText = "This can take a minute."
|
|
1649
|
+
progress.addButton(withTitle: "OK")
|
|
1650
|
+
progress.buttons[0].isHidden = true
|
|
1651
|
+
progress.beginSheetModal(for: window, completionHandler: nil)
|
|
1652
|
+
DispatchQueue.global().async {
|
|
1653
|
+
let result = runInstalledRadar("--update-now")
|
|
1654
|
+
DispatchQueue.main.async {
|
|
1655
|
+
self.window.endSheet(progress.window)
|
|
1656
|
+
if result?["updated"] as? Bool == true {
|
|
1657
|
+
self.relaunch()
|
|
1658
|
+
return
|
|
1659
|
+
}
|
|
1660
|
+
self.endUpdate("Update failed", result?["error"] as? String ?? "Radar could not run the installer.")
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
/// Starts a fresh copy once this one has exited, waiting on our pid so the quit finishes first.
|
|
1666
|
+
private func relaunch() {
|
|
1667
|
+
let helper = Process()
|
|
1668
|
+
helper.executableURL = URL(fileURLWithPath: "/bin/sh")
|
|
1669
|
+
helper.arguments = [
|
|
1670
|
+
"-c", "while kill -0 \"$1\" 2>/dev/null; do sleep 0.2; done; /usr/bin/open \"$2\"",
|
|
1671
|
+
"sh", String(ProcessInfo.processInfo.processIdentifier), Bundle.main.bundlePath,
|
|
1672
|
+
]
|
|
1673
|
+
do {
|
|
1674
|
+
try helper.run()
|
|
1675
|
+
} catch {
|
|
1676
|
+
endUpdate("Updated but could not restart", "Quit Slack Radar and open it again to use the new version.")
|
|
1677
|
+
return
|
|
1678
|
+
}
|
|
1679
|
+
NSApp.terminate(nil)
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1538
1682
|
/// Quit when the window is closed. A dock app with no window and no menu bar presence would
|
|
1539
1683
|
/// otherwise sit running invisibly, and the next dock click would do nothing at all.
|
|
1540
1684
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
@@ -1997,6 +2141,8 @@ let mainMenu = NSMenu()
|
|
|
1997
2141
|
let appMenuItem = NSMenuItem()
|
|
1998
2142
|
mainMenu.addItem(appMenuItem)
|
|
1999
2143
|
let appMenu = NSMenu()
|
|
2144
|
+
appMenu.addItem(NSMenuItem(title: "Check for Updates…", action: #selector(AppDelegate.checkForUpdates(_:)), keyEquivalent: ""))
|
|
2145
|
+
appMenu.addItem(NSMenuItem.separator())
|
|
2000
2146
|
appMenu.addItem(NSMenuItem(title: "Quit Slack Radar", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
|
|
2001
2147
|
appMenuItem.submenu = appMenu
|
|
2002
2148
|
let editMenuItem = NSMenuItem()
|
package/dist/web/server.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { StatusReason } from "../shared/wire.js";
|
|
|
3
3
|
import { SWITCHBOARD_KIND, SWITCHBOARD_BACKFILL_DEADLINE_MS, SWITCHBOARD_BACKFILL_SESSIONS, type SwitchboardExportGet } from "../shared/switchboard.js";
|
|
4
4
|
import { type DashboardScreenController } from "../shared/dashboard-screen.js";
|
|
5
5
|
import type { ProcessState } from "../shared/transport.js";
|
|
6
|
+
import type { LogSession } from "../shared/log-session.js";
|
|
6
7
|
declare const WEB_PORT: number;
|
|
7
8
|
/**
|
|
8
9
|
* Decide whether to drop a log frame under backpressure. LIVE frames are dropped
|
|
@@ -15,6 +16,14 @@ declare const WEB_PORT: number;
|
|
|
15
16
|
* without a socket. `cap` defaults to the live ceiling.
|
|
16
17
|
*/
|
|
17
18
|
export declare function shouldDropLogFrame(replay: boolean, writableLength: number, cap?: number): boolean;
|
|
19
|
+
export declare const DEVICE_ABSENT_BACKOFF_MS = 10000;
|
|
20
|
+
/** Milliseconds left in the device-absent window, 0 when it is not open. */
|
|
21
|
+
export declare function deviceAbsentMsLeft(now?: number): number;
|
|
22
|
+
export declare function markDeviceAbsent(now?: number): void;
|
|
23
|
+
export declare function resetDeviceAbsentForTest(): void;
|
|
24
|
+
export declare function _setDeviceAbsentBackoffForTest(ms: number | null): void;
|
|
25
|
+
type CaptureSession = Pick<LogSession, "isRunning" | "start" | "stop" | "subscribe" | "subscribeStatus" | "takeLastFailure" | "registerCleanup" | "restart">;
|
|
26
|
+
export declare function _setLogSessionForTest(session: CaptureSession | undefined): void;
|
|
18
27
|
/**
|
|
19
28
|
* Write the starting spec for a server that is taking ownership of this port. MUST be
|
|
20
29
|
* called only AFTER a successful listen() bind, never at module load: importing this
|
|
@@ -189,6 +198,14 @@ export declare function getPresenceState(): {
|
|
|
189
198
|
everSeen: boolean;
|
|
190
199
|
emptySince: number | null;
|
|
191
200
|
};
|
|
201
|
+
/**
|
|
202
|
+
* Keep the host log capture only while a tab is polling or a log stream is open.
|
|
203
|
+
* Stopping keeps the ring, so a tab that comes back still replays the history. Starting is
|
|
204
|
+
* limited to a capture some log stream armed earlier. Exported for tests with a fake clock.
|
|
205
|
+
*/
|
|
206
|
+
export declare function reconcileCapture(now?: number): "start" | "stop" | "keep";
|
|
207
|
+
/** One sweep step: auto-close wins, otherwise the capture is reconciled. */
|
|
208
|
+
export declare function sweepTick(now: number, onClose: () => void): void;
|
|
192
209
|
/** Reset presence between tests (module state is a process singleton). */
|
|
193
210
|
export declare function resetPresenceForTest(): void;
|
|
194
211
|
interface ShutdownDeps {
|
package/dist/web/server.js
CHANGED
|
@@ -12,11 +12,11 @@ import { resolveAdbPath } from "../shared/android.js";
|
|
|
12
12
|
import { createTransport } from "../shared/platform.js";
|
|
13
13
|
import { planReconnect } from "../shared/stream.js";
|
|
14
14
|
import { createDashboardScreenController, } from "../shared/dashboard-screen.js";
|
|
15
|
-
import { ENABLE_PING_WAITS_MS, RADAR_HOST, RADAR_PORT, RADAR_VERSION, WEB_PORT_DEFAULT } from "../shared/constants.js";
|
|
15
|
+
import { ENABLE_PING_WAITS_MS, RADAR_HOST, RADAR_PORT, RADAR_VERSION, WEB_PORT_DEFAULT, resetSlackDebugPackage } from "../shared/constants.js";
|
|
16
16
|
import { radarRequestRaw } from "../shared/radar-request-raw.js";
|
|
17
17
|
import { TokenFileError, clearRadarTokenCache, radarTokenHeaderFor, tokenFileMessage, unauthorizedMessageFor } from "../shared/radar-token.js";
|
|
18
18
|
import { friendlyFeatureFlagError } from "../shared/feature-flags.js";
|
|
19
|
-
import { deviceServerSilentError, readProcessState, } from "../shared/enable-diagnosis.js";
|
|
19
|
+
import { deviceServerSilentError, foreignRadarOwnerMessage, BareEnableError, readProcessState, } from "../shared/enable-diagnosis.js";
|
|
20
20
|
import { cleanupPulledDatabases, initializeDatabasePath, listDatabases, pullDatabase, pulledSize, queryDatabase, resetDbState, availableUsers, } from "../shared/db.js";
|
|
21
21
|
import { listPrefsFiles, readPrefsFile, parsePrefsXml, searchPrefs, } from "../shared/prefs.js";
|
|
22
22
|
import { BLANK_SPEC, coerceSpec, validateSpec } from "./spec.js";
|
|
@@ -157,27 +157,60 @@ const PING_PROBE_MS = 4000;
|
|
|
157
157
|
// forward port -- the split-brain flows both ways. Platforms without a shared
|
|
158
158
|
// host-level forward may omit reassertForward, so this falls back to ensureForward.
|
|
159
159
|
function forwardForRead() {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
const
|
|
160
|
+
const built = transport.reassertForward?.() ?? transport.ensureForward();
|
|
161
|
+
if (built)
|
|
162
|
+
clearDeviceAbsent();
|
|
163
|
+
return built;
|
|
164
|
+
}
|
|
165
|
+
// With no device attached every poll costs adb spawns and a doomed ping, from every open tab.
|
|
166
|
+
// After a probe that could neither build a forward nor reach the device port, /health and the
|
|
167
|
+
// /stream retry stay quiet for this long. Only the absent answer is cached, never a live one.
|
|
168
|
+
// Any sign the desk has a device again (a forward that builds, or a device listing) ends the
|
|
169
|
+
// window at once.
|
|
170
|
+
export const DEVICE_ABSENT_BACKOFF_MS = 10_000;
|
|
171
|
+
let deviceAbsentBackoffMs = DEVICE_ABSENT_BACKOFF_MS;
|
|
172
|
+
// Set by a mark, zeroed by a clear. The window runs out by itself and only throttles probes.
|
|
173
|
+
let deviceAbsentUntil = 0;
|
|
174
|
+
/** Milliseconds left in the device-absent window, 0 when it is not open. */
|
|
175
|
+
export function deviceAbsentMsLeft(now = Date.now()) {
|
|
176
|
+
return Math.max(0, deviceAbsentUntil - now);
|
|
177
|
+
}
|
|
178
|
+
export function markDeviceAbsent(now = Date.now()) {
|
|
179
|
+
deviceAbsentUntil = now + deviceAbsentBackoffMs;
|
|
180
|
+
}
|
|
181
|
+
function clearDeviceAbsent() {
|
|
182
|
+
deviceAbsentUntil = 0;
|
|
183
|
+
}
|
|
184
|
+
export function resetDeviceAbsentForTest() {
|
|
185
|
+
clearDeviceAbsent();
|
|
186
|
+
}
|
|
187
|
+
// Test seam: the real window would make every stream test wait ten seconds of wall clock.
|
|
188
|
+
export function _setDeviceAbsentBackoffForTest(ms) {
|
|
189
|
+
deviceAbsentBackoffMs = ms ?? DEVICE_ABSENT_BACKOFF_MS;
|
|
190
|
+
}
|
|
191
|
+
let logSession = transport.logSession ?? null;
|
|
171
192
|
// Kill the persistent capture child on process exit so it does not orphan when the
|
|
172
193
|
// dashboard is spawned and later terminated by the open_radar_dashboard MCP tool.
|
|
173
194
|
logSession?.registerCleanup();
|
|
174
|
-
//
|
|
175
|
-
//
|
|
176
|
-
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
//
|
|
195
|
+
// Set once a log stream has asked for the capture. The sweep only restarts a capture some
|
|
196
|
+
// stream wanted, so a dashboard nobody opened a log tab on never spawns a reader.
|
|
197
|
+
let captureArmed = false;
|
|
198
|
+
// Open log streams. A page keeps its stream open while hidden and still gets live frames, so
|
|
199
|
+
// stopping the capture under it would lose lines to the gap the next start skips.
|
|
200
|
+
let openLogStreams = 0;
|
|
201
|
+
// The capture is NOT stopped when the last log stream disconnects (e.g. switching to the
|
|
202
|
+
// Network tab): it keeps filling the session ring so returning to a log tab still shows the
|
|
203
|
+
// full history, the way Android Studio retains logcat for the whole session. The sweep stops
|
|
204
|
+
// it when no tab has polled for TAB_LIVENESS_MS and no log stream is open, keeping the ring.
|
|
205
|
+
// With no device, `adb logcat` waits for it, as it always did. Memory stays bounded by the
|
|
206
|
+
// ring cap and by the per-connection backpressure guard in handleStream (a stalled client
|
|
207
|
+
// drops frames rather than buffering without limit).
|
|
208
|
+
// Test seam: the real session spawns adb logcat, which a test host may not have.
|
|
209
|
+
export function _setLogSessionForTest(session) {
|
|
210
|
+
logSession = session ?? transport.logSession ?? null;
|
|
211
|
+
captureArmed = false;
|
|
212
|
+
openLogStreams = 0;
|
|
213
|
+
}
|
|
181
214
|
function writeSpec(spec) {
|
|
182
215
|
try {
|
|
183
216
|
fs.mkdirSync(SPEC_DIR, { recursive: true });
|
|
@@ -296,7 +329,8 @@ export function registerScreenCleanupOnBind() {
|
|
|
296
329
|
// throttling batches chained timers to roughly once per minute after a tab is hidden past 5
|
|
297
330
|
// minutes, occasionally stretching to ~90s. 75s covers the common throttled gap; a rare longer
|
|
298
331
|
// gap still just flaps the tab out and back in (the 5-minute grace below absorbs that flap
|
|
299
|
-
// harmlessly), it does not misfire an auto-close.
|
|
332
|
+
// harmlessly), it does not misfire an auto-close. An open log stream holds the capture through
|
|
333
|
+
// the flap, so the flap only touches the auto-close bookkeeping. Exported so tests pin the real constant
|
|
300
334
|
// instead of a duplicated copy.
|
|
301
335
|
export const TAB_LIVENESS_MS = 75_000;
|
|
302
336
|
/**
|
|
@@ -582,6 +616,37 @@ export function getPresenceState() {
|
|
|
582
616
|
emptySince: presence.emptySince,
|
|
583
617
|
};
|
|
584
618
|
}
|
|
619
|
+
/**
|
|
620
|
+
* Keep the host log capture only while a tab is polling or a log stream is open.
|
|
621
|
+
* Stopping keeps the ring, so a tab that comes back still replays the history. Starting is
|
|
622
|
+
* limited to a capture some log stream armed earlier. Exported for tests with a fake clock.
|
|
623
|
+
*/
|
|
624
|
+
export function reconcileCapture(now = Date.now()) {
|
|
625
|
+
if (!logSession)
|
|
626
|
+
return "keep";
|
|
627
|
+
const liveTab = [...presence.tabs.values()].some((ts) => now - ts <= TAB_LIVENESS_MS);
|
|
628
|
+
const keep = liveTab || openLogStreams > 0;
|
|
629
|
+
const running = logSession.isRunning();
|
|
630
|
+
if (!keep) {
|
|
631
|
+
// Also when not running: stop() clears the restart timer that subscribe or a child exit
|
|
632
|
+
// arms, so a reader is not spawned for a capture nobody wants.
|
|
633
|
+
logSession.stop();
|
|
634
|
+
return running ? "stop" : "keep";
|
|
635
|
+
}
|
|
636
|
+
if (!running && captureArmed) {
|
|
637
|
+
logSession.start();
|
|
638
|
+
return "start";
|
|
639
|
+
}
|
|
640
|
+
return "keep";
|
|
641
|
+
}
|
|
642
|
+
/** One sweep step: auto-close wins, otherwise the capture is reconciled. */
|
|
643
|
+
export function sweepTick(now, onClose) {
|
|
644
|
+
if (sweepPresence(now)) {
|
|
645
|
+
onClose();
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
reconcileCapture(now);
|
|
649
|
+
}
|
|
585
650
|
/** Reset presence between tests (module state is a process singleton). */
|
|
586
651
|
export function resetPresenceForTest() {
|
|
587
652
|
presence.tabs.clear();
|
|
@@ -854,8 +919,14 @@ async function armDevice() {
|
|
|
854
919
|
// default, which can be SHORTER than one another process explicitly asked for. An MCP session that
|
|
855
920
|
// armed for 30 minutes would have its idle window silently cut to the default by someone merely
|
|
856
921
|
// opening a dashboard to look, with no error surfaced to either side.
|
|
857
|
-
|
|
922
|
+
const bootPing = await pingDevice(true);
|
|
923
|
+
if (bootPing.state === "armed") {
|
|
924
|
+
// A 401 or 500 also counts as armed above, but only a 200 body is Radar's own status.
|
|
925
|
+
const body = bootPing.status === 200 ? parsedPingBody(bootPing.body) : null;
|
|
926
|
+
if (body)
|
|
927
|
+
transport.adoptRadarOwner?.(body);
|
|
858
928
|
return;
|
|
929
|
+
}
|
|
859
930
|
// Anything else the ping says is inconclusive HERE, including a reset: the probe travels the
|
|
860
931
|
// adb forward, and the host adb server accepts every connection before discovering whether the
|
|
861
932
|
// device side exists, so an absent socket and a misbehaving live session read identically. The
|
|
@@ -893,8 +964,7 @@ export function startAutoCloseSweep(server) {
|
|
|
893
964
|
if (process.env.SLACK_RADAR_NO_AUTO_CLOSE === "1")
|
|
894
965
|
return null;
|
|
895
966
|
const timer = setInterval(() => {
|
|
896
|
-
|
|
897
|
-
void shutdownDashboard(server);
|
|
967
|
+
sweepTick(Date.now(), () => void shutdownDashboard(server));
|
|
898
968
|
}, AUTO_CLOSE_SWEEP_MS);
|
|
899
969
|
timer.unref();
|
|
900
970
|
return timer;
|
|
@@ -1410,12 +1480,18 @@ async function handleHealth(res) {
|
|
|
1410
1480
|
let unauthorized = false;
|
|
1411
1481
|
let forwarded = false;
|
|
1412
1482
|
if (transport.supportsRadarHttpTools) {
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1483
|
+
// Window open: skip the ping too. A tokenless ping at a port another process forwarded
|
|
1484
|
+
// would read 401 and flap the headline.
|
|
1485
|
+
if (deviceAbsentMsLeft() === 0) {
|
|
1486
|
+
forwarded = forwardForRead();
|
|
1487
|
+
const startedAt = Date.now();
|
|
1488
|
+
ping = await pingDevice(forwarded);
|
|
1489
|
+
ok = ping.state === "armed" && ping.status === 200 && pingMatchesTransport(ping);
|
|
1490
|
+
unauthorized = ping.state === "armed" && ping.status === 401 && pingMatchesTransport(ping);
|
|
1491
|
+
if (!forwarded && ping.state === "unarmed")
|
|
1492
|
+
markDeviceAbsent();
|
|
1493
|
+
noteHealthPing(ok, unauthorized, startedAt);
|
|
1494
|
+
}
|
|
1419
1495
|
}
|
|
1420
1496
|
else if (transport.supportsHostLogs) {
|
|
1421
1497
|
// A log-only transport has no app-side HTTP listener to probe. Resolve its selected host
|
|
@@ -1437,6 +1513,8 @@ async function handleHealth(res) {
|
|
|
1437
1513
|
if (ok) {
|
|
1438
1514
|
try {
|
|
1439
1515
|
const p = JSON.parse(ping.body);
|
|
1516
|
+
// A dashboard up before the MCP armed learns the owner here instead of only at boot.
|
|
1517
|
+
transport.adoptRadarOwner?.(p);
|
|
1440
1518
|
if (p.android_user_id != null)
|
|
1441
1519
|
uid = String(p.android_user_id);
|
|
1442
1520
|
if (typeof p.profile === "string")
|
|
@@ -1897,6 +1975,9 @@ tab) {
|
|
|
1897
1975
|
// the pulled-DB scratch is A's. Same two drops the switched path below performs; only the
|
|
1898
1976
|
// screen teardown is withheld, and lastArmedSerial stays put so the press earns it.
|
|
1899
1977
|
cleanupPulledDatabases();
|
|
1978
|
+
// Device B may run a different Slack debug build; the next arm re-detects it.
|
|
1979
|
+
resetSlackDebugPackage();
|
|
1980
|
+
transport.releaseDebugBuild?.();
|
|
1900
1981
|
logSession?.restart();
|
|
1901
1982
|
return { status: 200, body: {
|
|
1902
1983
|
enabled: false,
|
|
@@ -1942,6 +2023,9 @@ tab) {
|
|
|
1942
2023
|
// on its next /stream reconnect; nothing here needs to force-restart it eagerly.
|
|
1943
2024
|
if (switched) {
|
|
1944
2025
|
cleanupPulledDatabases();
|
|
2026
|
+
// Device B may run a different Slack debug build; the next arm re-detects it.
|
|
2027
|
+
resetSlackDebugPackage();
|
|
2028
|
+
transport.releaseDebugBuild?.();
|
|
1945
2029
|
logSession?.restart();
|
|
1946
2030
|
// The old device's streamed switchboard records belong to the wrong hardware.
|
|
1947
2031
|
clearSwitchboardHostStore();
|
|
@@ -1999,6 +2083,8 @@ tab) {
|
|
|
1999
2083
|
// enabled:false after the ping probe times out. Async for the same reason as the boot chain:
|
|
2000
2084
|
// the broadcast waits for ordered delivery under a 15 second ceiling, and a Reconnect press
|
|
2001
2085
|
// must not hold the event loop for it.
|
|
2086
|
+
// A fresh enable decides the build again, so drop the one a past arm held.
|
|
2087
|
+
transport.releaseDebugBuild?.();
|
|
2002
2088
|
if (transport.activateAsync) {
|
|
2003
2089
|
await transport.activateAsync(transport.getTimeoutMinutes());
|
|
2004
2090
|
}
|
|
@@ -2014,12 +2100,13 @@ tab) {
|
|
|
2014
2100
|
}
|
|
2015
2101
|
const activateError = transport.getLastActivateError?.() ?? null;
|
|
2016
2102
|
if (activateError) {
|
|
2103
|
+
const bare = transport.getLastActivateBareError?.() != null;
|
|
2017
2104
|
return { status: 200, body: {
|
|
2018
2105
|
enabled: false,
|
|
2019
|
-
error: transport.supportsRadarHttpTools
|
|
2106
|
+
error: bare ? activateError : transport.supportsRadarHttpTools
|
|
2020
2107
|
? `Broadcast failed: ${activateError}. Is the device connected?`
|
|
2021
2108
|
: `Log capture activation failed: ${activateError}`,
|
|
2022
|
-
instructs:
|
|
2109
|
+
instructs: bare,
|
|
2023
2110
|
device_serial: pinnedSerial(),
|
|
2024
2111
|
} };
|
|
2025
2112
|
}
|
|
@@ -2045,6 +2132,21 @@ tab) {
|
|
|
2045
2132
|
return refusedReply();
|
|
2046
2133
|
}
|
|
2047
2134
|
const enabled = ping.state === "armed" && ping.status === 200;
|
|
2135
|
+
// Another build holding the socket answers a healthy ping, so the arm did not land on ours.
|
|
2136
|
+
const pingBody = enabled && transport.foreignRadarOwner ? parsedPingBody(ping.body) : null;
|
|
2137
|
+
// The dashboard is a viewer: it follows the build that owns the socket. An override or an owner
|
|
2138
|
+
// that is not a shipped id is not adopted, so the check below still refuses it.
|
|
2139
|
+
if (pingBody)
|
|
2140
|
+
transport.adoptRadarOwner?.(pingBody);
|
|
2141
|
+
const foreignOwner = pingBody ? transport.foreignRadarOwner?.(pingBody) ?? null : null;
|
|
2142
|
+
if (foreignOwner !== null) {
|
|
2143
|
+
return { status: 200, body: {
|
|
2144
|
+
enabled: false,
|
|
2145
|
+
error: foreignRadarOwnerMessage(transport.logPackage ?? "the armed build", foreignOwner),
|
|
2146
|
+
instructs: true,
|
|
2147
|
+
device_serial: pinnedSerial(),
|
|
2148
|
+
} };
|
|
2149
|
+
}
|
|
2048
2150
|
// A verified arm refutes any cached "no_session" the same way a stream connect does (the
|
|
2049
2151
|
// memo is evidence about a session this route just restored): without this, another stream
|
|
2050
2152
|
// client's mid-retry memo read could announce the pre-arm diagnosis for up to its TTL and
|
|
@@ -3259,6 +3361,7 @@ function handleStream(req, res) {
|
|
|
3259
3361
|
const armLogs = () => {
|
|
3260
3362
|
if (!wantsLogs || closed)
|
|
3261
3363
|
return;
|
|
3364
|
+
captureArmed = true;
|
|
3262
3365
|
const ok = logSession?.start() ?? false;
|
|
3263
3366
|
if (!ok) {
|
|
3264
3367
|
const failure = logSession?.takeLastFailure();
|
|
@@ -3279,6 +3382,29 @@ function handleStream(req, res) {
|
|
|
3279
3382
|
return;
|
|
3280
3383
|
settled = true;
|
|
3281
3384
|
retries++;
|
|
3385
|
+
// While the window is open there is nothing to diagnose or rebuild: wait it out.
|
|
3386
|
+
const absentMs = deviceAbsentMsLeft();
|
|
3387
|
+
if (absentMs > 0) {
|
|
3388
|
+
// The skipped probe is the only thing that retracts a latched no_session, and it would
|
|
3389
|
+
// keep saying "no radar session" while the device is simply gone.
|
|
3390
|
+
if (lastReason === "no_session")
|
|
3391
|
+
announce("waiting", "device not answering, retrying…", "undiagnosed");
|
|
3392
|
+
// An early clear (/enable, activate, the device chip's listing) must wake the stream
|
|
3393
|
+
// before the old deadline, so re-read the window every couple of seconds instead of
|
|
3394
|
+
// sleeping it.
|
|
3395
|
+
const tick = () => {
|
|
3396
|
+
if (closed)
|
|
3397
|
+
return;
|
|
3398
|
+
const left = deviceAbsentMsLeft();
|
|
3399
|
+
if (left === 0) {
|
|
3400
|
+
connect();
|
|
3401
|
+
return;
|
|
3402
|
+
}
|
|
3403
|
+
setTimeout(tick, Math.min(left, 2000));
|
|
3404
|
+
};
|
|
3405
|
+
setTimeout(tick, Math.min(absentMs, 2000));
|
|
3406
|
+
return;
|
|
3407
|
+
}
|
|
3282
3408
|
// A failure that keeps repeating deserves a WHY. The second attempt (the first repeat —
|
|
3283
3409
|
// one failure is routine) and a coarse cadence after that, so a fresh classification can
|
|
3284
3410
|
// still ARRIVE mid-outage without running an adb probe on every 2s retry. announce never
|
|
@@ -3436,12 +3562,14 @@ function handleStream(req, res) {
|
|
|
3436
3562
|
retry();
|
|
3437
3563
|
});
|
|
3438
3564
|
});
|
|
3439
|
-
upstream.on("error", () => {
|
|
3565
|
+
upstream.on("error", (err) => {
|
|
3440
3566
|
if (attempt !== attemptSeq)
|
|
3441
3567
|
return;
|
|
3442
3568
|
clearTimeout(myDeadline);
|
|
3443
3569
|
if (headersDeadline === myDeadline)
|
|
3444
3570
|
headersDeadline = null;
|
|
3571
|
+
if (!forwarded && err.code === "ECONNREFUSED")
|
|
3572
|
+
markDeviceAbsent();
|
|
3445
3573
|
announce("waiting", "device unreachable, retrying…");
|
|
3446
3574
|
retry();
|
|
3447
3575
|
});
|
|
@@ -3501,7 +3629,10 @@ function handleStream(req, res) {
|
|
|
3501
3629
|
// never tears down the capture or loses lines in a reconnect gap.
|
|
3502
3630
|
let unsubscribeLogs = null;
|
|
3503
3631
|
let unsubscribeLogStatus = null;
|
|
3632
|
+
let countedLogStream = false;
|
|
3504
3633
|
if (wantsLogs) {
|
|
3634
|
+
openLogStreams++;
|
|
3635
|
+
countedLogStream = true;
|
|
3505
3636
|
// Subscribe before starting so a synchronous spawn failure is announced with its
|
|
3506
3637
|
// real cause. armLogs' fallback wording would otherwise win the status deduper and
|
|
3507
3638
|
// hide the more useful failure emitted by LogSession.
|
|
@@ -3555,6 +3686,8 @@ function handleStream(req, res) {
|
|
|
3555
3686
|
clearInterval(heartbeat);
|
|
3556
3687
|
if (headersDeadline)
|
|
3557
3688
|
clearTimeout(headersDeadline);
|
|
3689
|
+
if (countedLogStream)
|
|
3690
|
+
openLogStreams--;
|
|
3558
3691
|
if (unsubscribeLogs)
|
|
3559
3692
|
unsubscribeLogs(); // detach listener; capture stays up
|
|
3560
3693
|
if (unsubscribeLogStatus)
|
|
@@ -3585,6 +3718,15 @@ function listDevices(res) {
|
|
|
3585
3718
|
// listDevices() at all (undefined) is treated as "no devices", not an adb error.
|
|
3586
3719
|
const listed = transport.listDevices?.();
|
|
3587
3720
|
const adbError = listed === null;
|
|
3721
|
+
// A failed forward plus a refused port is also what an adb server restart looks like, and the
|
|
3722
|
+
// page lists devices every 5s, so a listed device is the earliest free sign the desk is not
|
|
3723
|
+
// empty. A null listing or only unauthorized/offline entries leave the window alone. With the
|
|
3724
|
+
// pin on serial X and only Y attached, each /health poll and each waiting stream re-marks after
|
|
3725
|
+
// a clear, so the forward pair repeats about once per chip poll; bounded, no state kept for it.
|
|
3726
|
+
// This reads the listing only, builds no forward and runs nothing else, so a GET on this open
|
|
3727
|
+
// route can at most end the probe window early.
|
|
3728
|
+
if (listed?.some((d) => d.state === "device"))
|
|
3729
|
+
clearDeviceAbsent();
|
|
3588
3730
|
sendJson(res, 200, {
|
|
3589
3731
|
devices: listed ?? [],
|
|
3590
3732
|
selected: transport.getSelectedDevice?.() ?? null,
|
|
@@ -3604,11 +3746,13 @@ function activateRadar(res, userId) {
|
|
|
3604
3746
|
});
|
|
3605
3747
|
return;
|
|
3606
3748
|
}
|
|
3749
|
+
clearDeviceAbsent();
|
|
3607
3750
|
try {
|
|
3608
3751
|
if (userId && transport.activateForUser) {
|
|
3609
3752
|
transport.activateForUser(userId, 5);
|
|
3610
3753
|
}
|
|
3611
3754
|
else {
|
|
3755
|
+
transport.releaseDebugBuild?.();
|
|
3612
3756
|
transport.activate(5);
|
|
3613
3757
|
}
|
|
3614
3758
|
}
|
|
@@ -3617,7 +3761,8 @@ function activateRadar(res, userId) {
|
|
|
3617
3761
|
ok: false,
|
|
3618
3762
|
error: e instanceof TokenFileError
|
|
3619
3763
|
? tokenFileMessage(e, "dashboard")
|
|
3620
|
-
:
|
|
3764
|
+
: e instanceof BareEnableError ? e.message
|
|
3765
|
+
: `Broadcast failed: ${e.message}. Is the device connected?`,
|
|
3621
3766
|
});
|
|
3622
3767
|
return;
|
|
3623
3768
|
}
|
|
@@ -3628,7 +3773,7 @@ function activateRadar(res, userId) {
|
|
|
3628
3773
|
}
|
|
3629
3774
|
const activateError = transport.getLastActivateError?.();
|
|
3630
3775
|
if (activateError) {
|
|
3631
|
-
sendJson(res, 502, { ok: false, error: `Broadcast failed: ${activateError}. Is the device connected?` });
|
|
3776
|
+
sendJson(res, 502, { ok: false, error: transport.getLastActivateBareError?.() ? activateError : `Broadcast failed: ${activateError}. Is the device connected?` });
|
|
3632
3777
|
return;
|
|
3633
3778
|
}
|
|
3634
3779
|
sendJson(res, 200, {
|