@slack/radar-mcp 1.14.0 → 1.17.1
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 +15 -3
- package/dist/mcp/db-tools.js +23 -0
- package/dist/mcp/dispatch.d.ts +4 -0
- package/dist/mcp/dispatch.js +26 -0
- package/dist/mcp/index.js +105 -37
- package/dist/mcp/platform-detect.d.ts +110 -0
- package/dist/mcp/platform-detect.js +246 -0
- package/dist/mcp/prefs-tools.d.ts +4 -3
- package/dist/mcp/prefs-tools.js +26 -3
- package/dist/mcp/session-enable.js +19 -1
- package/dist/mcp/snapshot.js +4 -1
- package/dist/mcp/sources-gate.js +6 -3
- package/dist/mcp/tools.d.ts +11 -0
- package/dist/mcp/tools.js +38 -0
- package/dist/mcp/web-child-env.d.ts +1 -0
- package/dist/mcp/web-child-env.js +32 -0
- package/dist/shared/android.d.ts +89 -1
- package/dist/shared/android.js +352 -78
- package/dist/shared/constants.d.ts +25 -1
- package/dist/shared/constants.js +27 -1
- package/dist/shared/db.js +11 -2
- package/dist/shared/debug-log.js +6 -1
- package/dist/shared/enable-diagnosis.d.ts +28 -0
- package/dist/shared/enable-diagnosis.js +41 -0
- package/dist/shared/prefs.d.ts +0 -5
- package/dist/shared/prefs.js +6 -2
- package/dist/shared/screen.d.ts +46 -9
- package/dist/shared/screen.js +74 -11
- package/dist/shared/transport.d.ts +62 -5
- package/dist/shared/webapp.d.ts +50 -0
- package/dist/shared/webapp.js +65 -0
- package/dist/web/bin.js +5 -1
- package/dist/web/install-app.d.ts +87 -0
- package/dist/web/install-app.js +703 -0
- package/dist/web/macapp/RadarApp.swift +466 -0
- package/dist/web/public/icons/radar-1024.png +0 -0
- package/dist/web/public/icons/radar.svg +40 -0
- package/dist/web/public/index.html +41 -1
- package/dist/web/public/next/app.js +19 -4
- package/dist/web/public/next/db.js +145 -5
- package/dist/web/public/next/feed.js +18 -0
- package/dist/web/public/next/hooks.js +10 -1
- package/dist/web/public/next/registry.js +18 -10
- package/dist/web/public/next/screen.js +147 -43
- package/dist/web/public/next/store.js +393 -51
- package/dist/web/public/next/toast.js +134 -0
- package/dist/web/server.d.ts +26 -1
- package/dist/web/server.js +341 -113
- package/package.json +2 -2
package/dist/shared/android.js
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
-
import { execSync, execFileSync } from "child_process";
|
|
1
|
+
import { execFile, execSync, execFileSync } from "child_process";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import os from "os";
|
|
4
4
|
import path from "path";
|
|
5
|
+
import { promisify } from "util";
|
|
5
6
|
import { readDebugLogBuffer } from "./debug-log.js";
|
|
6
7
|
import { filterLogLines, getCaptureFilePath, isCapturing, readRecentLogs, registerCleanup, rotateCaptureIfNeeded, startCapture, stopCapture, } from "./logcat-capture.js";
|
|
7
8
|
import { RADAR_PORT, RADAR_VERSION } from "./constants.js";
|
|
8
9
|
const SOCKET_NAME = "slack-radar";
|
|
9
10
|
const ENABLE_ACTION = "slack.debug.ENABLE_RADAR";
|
|
11
|
+
/**
|
|
12
|
+
* Default ceiling for the `ps` process probe. Patient by design: on the MCP enable path
|
|
13
|
+
* several seconds of ping retries have already elapsed, so a slow adb is worth waiting out
|
|
14
|
+
* to get a real answer rather than an "unknown".
|
|
15
|
+
*/
|
|
16
|
+
const PS_PROBE_TIMEOUT_MS = 5000;
|
|
10
17
|
/**
|
|
11
18
|
* Resolve the adb binary. MCP servers spawned from GUI launchers (Spotlight,
|
|
12
19
|
* Raycast, Dock) do not inherit the user's shell PATH, so a bare `adb` call
|
|
@@ -24,6 +31,7 @@ export function resolveAdbPath() {
|
|
|
24
31
|
timeout: 2000,
|
|
25
32
|
encoding: "utf-8",
|
|
26
33
|
shell: "/bin/sh",
|
|
34
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
27
35
|
}).trim();
|
|
28
36
|
if (which && fs.existsSync(which))
|
|
29
37
|
return "adb";
|
|
@@ -59,6 +67,35 @@ export function resolveAdbPath() {
|
|
|
59
67
|
// Module-level resolution, done once at import time. The MCP process lifetime
|
|
60
68
|
// is short enough (session-scoped) that we do not need to re-resolve.
|
|
61
69
|
const ADB = resolveAdbPath();
|
|
70
|
+
// Promisified execFile for the *Async transport methods below. argv form (no shell), so an
|
|
71
|
+
// interpolated value can never break out of a command string, and stdout/stderr are captured
|
|
72
|
+
// rather than inherited, matching the stdio discipline execsync-stdio.test.mjs pins.
|
|
73
|
+
const execFileAsync = promisify(execFile);
|
|
74
|
+
// One wording for a missing adb on the forward path, shared by the sync and async rebuilds so
|
|
75
|
+
// the two cannot drift.
|
|
76
|
+
const ADB_NOT_FOUND_FORWARD_ERROR = "adb binary not found. Set ANDROID_HOME or install Android SDK platform-tools. Searched: PATH, $ANDROID_HOME/platform-tools/adb, ~/Library/Android/sdk/platform-tools/adb, ~/Android/Sdk/platform-tools/adb, /usr/local/bin/adb, /opt/homebrew/bin/adb.";
|
|
77
|
+
// The device-side `ps` pipeline both debug-build probes run. See psDebugBuildProbe's doc
|
|
78
|
+
// comment for why the `|| exit 9` and the trailing `|| true` are each load-bearing.
|
|
79
|
+
const PS_DEBUG_BUILD_SCRIPT = 'out=$(ps -A -o USER,NAME) || exit 9; echo "$out" | grep Slack | grep debug || true';
|
|
80
|
+
// The enable broadcast, in argv form so the sync and async activate paths send the exact
|
|
81
|
+
// same command.
|
|
82
|
+
function enableBroadcastArgs(userId, timeoutMinutes) {
|
|
83
|
+
return [
|
|
84
|
+
"shell",
|
|
85
|
+
"am",
|
|
86
|
+
"broadcast",
|
|
87
|
+
"--user",
|
|
88
|
+
userId,
|
|
89
|
+
"-a",
|
|
90
|
+
ENABLE_ACTION,
|
|
91
|
+
"--ei",
|
|
92
|
+
"timeout_minutes",
|
|
93
|
+
String(timeoutMinutes),
|
|
94
|
+
"--es",
|
|
95
|
+
"mcp_version",
|
|
96
|
+
RADAR_VERSION,
|
|
97
|
+
];
|
|
98
|
+
}
|
|
62
99
|
/**
|
|
63
100
|
* Parse the output of `ps -A -o USER,NAME` to extract the Android user ID
|
|
64
101
|
* for the debug build process. When multiple profiles are running, prefers
|
|
@@ -123,6 +160,11 @@ export function parseAdbDevicesOutput(raw) {
|
|
|
123
160
|
}
|
|
124
161
|
export class AndroidTransport {
|
|
125
162
|
platform = "android";
|
|
163
|
+
// adb reaches the app's databases and shared prefs through run-as, so the DB and prefs tools
|
|
164
|
+
// (which shell to adb themselves) are cleared to run.
|
|
165
|
+
supportsLocalFileAccess = true;
|
|
166
|
+
// The dashboard's device arming and Reconnect flow is built around an adb-attached device.
|
|
167
|
+
supportsDeviceDashboard = true;
|
|
126
168
|
portForwarded = false;
|
|
127
169
|
detectedUserId = null;
|
|
128
170
|
currentTimeoutMinutes = 5;
|
|
@@ -142,8 +184,7 @@ export class AndroidTransport {
|
|
|
142
184
|
// never trust the cache (see reassertForward's doc comment on shared-transport.ts).
|
|
143
185
|
rebuildForward() {
|
|
144
186
|
if (!ADB) {
|
|
145
|
-
this.lastForwardError =
|
|
146
|
-
"adb binary not found. Set ANDROID_HOME or install Android SDK platform-tools. Searched: PATH, $ANDROID_HOME/platform-tools/adb, ~/Library/Android/sdk/platform-tools/adb, ~/Android/Sdk/platform-tools/adb, /usr/local/bin/adb, /opt/homebrew/bin/adb.";
|
|
187
|
+
this.lastForwardError = ADB_NOT_FOUND_FORWARD_ERROR;
|
|
147
188
|
return false;
|
|
148
189
|
}
|
|
149
190
|
// Drop any existing mapping first. Re-issuing `adb forward` over a stale mapping
|
|
@@ -153,7 +194,10 @@ export class AndroidTransport {
|
|
|
153
194
|
// remove fails harmlessly when no mapping exists. process.env.ANDROID_SERIAL (set by
|
|
154
195
|
// pin()) targets both calls at OUR device, not whichever adb would pick by default.
|
|
155
196
|
try {
|
|
156
|
-
execSync(`${ADB} forward --remove tcp:${RADAR_PORT}`, {
|
|
197
|
+
execSync(`${ADB} forward --remove tcp:${RADAR_PORT}`, {
|
|
198
|
+
timeout: 5000,
|
|
199
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
200
|
+
});
|
|
157
201
|
}
|
|
158
202
|
catch {
|
|
159
203
|
// No existing mapping, or adb hiccup; the add below surfaces real failures.
|
|
@@ -161,6 +205,7 @@ export class AndroidTransport {
|
|
|
161
205
|
try {
|
|
162
206
|
execSync(`${ADB} forward tcp:${RADAR_PORT} localabstract:${SOCKET_NAME}`, {
|
|
163
207
|
timeout: 5000,
|
|
208
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
164
209
|
});
|
|
165
210
|
this.portForwarded = true;
|
|
166
211
|
this.lastForwardError = null;
|
|
@@ -185,13 +230,10 @@ export class AndroidTransport {
|
|
|
185
230
|
const list = execSync(`${ADB} forward --list`, {
|
|
186
231
|
timeout: 5000,
|
|
187
232
|
encoding: "utf-8",
|
|
233
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
188
234
|
});
|
|
189
|
-
|
|
190
|
-
if (list.split("\n").some((line) => line.trim() === wanted)) {
|
|
191
|
-
this.portForwarded = true;
|
|
192
|
-
this.lastForwardError = null;
|
|
235
|
+
if (this.recordForwardIfListed(list, serial))
|
|
193
236
|
return true;
|
|
194
|
-
}
|
|
195
237
|
}
|
|
196
238
|
catch {
|
|
197
239
|
// Listing failed; fall through to the unconditional rebuild below, which
|
|
@@ -200,6 +242,66 @@ export class AndroidTransport {
|
|
|
200
242
|
}
|
|
201
243
|
return this.rebuildForward();
|
|
202
244
|
}
|
|
245
|
+
// The listing check reassertForward and reassertForwardAsync share: when the live mapping
|
|
246
|
+
// already points our RADAR_PORT at our pinned serial, record it good and skip the rebuild.
|
|
247
|
+
recordForwardIfListed(list, serial) {
|
|
248
|
+
const wanted = `${serial} tcp:${RADAR_PORT} localabstract:${SOCKET_NAME}`;
|
|
249
|
+
if (!list.split("\n").some((line) => line.trim() === wanted))
|
|
250
|
+
return false;
|
|
251
|
+
this.portForwarded = true;
|
|
252
|
+
this.lastForwardError = null;
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* reassertForward, off the event loop. Same listing-first-then-rebuild logic and the same
|
|
257
|
+
* state fields, but every adb call runs through execFileAsync so a caller that must keep
|
|
258
|
+
* serving HTTP while adb is slow (the dashboard's boot-arm chain) is never blocked, even
|
|
259
|
+
* inside a single call. The sync twin stays for the MCP request paths, which have nothing
|
|
260
|
+
* else to do while adb runs.
|
|
261
|
+
*/
|
|
262
|
+
async reassertForwardAsync() {
|
|
263
|
+
const serial = process.env.ANDROID_SERIAL;
|
|
264
|
+
if (serial && ADB) {
|
|
265
|
+
try {
|
|
266
|
+
const { stdout } = await execFileAsync(ADB, ["forward", "--list"], {
|
|
267
|
+
timeout: 5000,
|
|
268
|
+
encoding: "utf-8",
|
|
269
|
+
});
|
|
270
|
+
if (this.recordForwardIfListed(stdout, serial))
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// Listing failed; fall through to the rebuild, exactly like the sync twin.
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return this.rebuildForwardAsync();
|
|
278
|
+
}
|
|
279
|
+
// rebuildForward, off the event loop. Mirrors the sync twin step for step: drop any
|
|
280
|
+
// existing mapping (failure is harmless), then add ours and record the outcome.
|
|
281
|
+
async rebuildForwardAsync() {
|
|
282
|
+
if (!ADB) {
|
|
283
|
+
this.lastForwardError = ADB_NOT_FOUND_FORWARD_ERROR;
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
await execFileAsync(ADB, ["forward", "--remove", `tcp:${RADAR_PORT}`], { timeout: 5000 });
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
// No existing mapping, or adb hiccup; the add below surfaces real failures.
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
await execFileAsync(ADB, ["forward", `tcp:${RADAR_PORT}`, `localabstract:${SOCKET_NAME}`], {
|
|
294
|
+
timeout: 5000,
|
|
295
|
+
});
|
|
296
|
+
this.portForwarded = true;
|
|
297
|
+
this.lastForwardError = null;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
catch (e) {
|
|
301
|
+
this.lastForwardError = e.message;
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
203
305
|
getLastForwardError() {
|
|
204
306
|
return this.lastForwardError;
|
|
205
307
|
}
|
|
@@ -213,25 +315,86 @@ export class AndroidTransport {
|
|
|
213
315
|
return;
|
|
214
316
|
try {
|
|
215
317
|
const userId = this.detectDebugBuildUser();
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
startCapture(ADB);
|
|
318
|
+
// Same taller ceiling as the async twin: the broadcast waits for ordered delivery and a
|
|
319
|
+
// cold debug build can hold completion past 5 seconds.
|
|
320
|
+
execFileSync(ADB, enableBroadcastArgs(userId, timeout), {
|
|
321
|
+
timeout: 15000,
|
|
322
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
323
|
+
});
|
|
324
|
+
this.recordActivateSuccess(timeout);
|
|
224
325
|
}
|
|
225
326
|
catch (e) {
|
|
226
|
-
|
|
227
|
-
// path can report it instead of claiming success with a failing ping.
|
|
228
|
-
// radarEnabled drops too: a prior success no longer proves the device
|
|
229
|
-
// side is armed, and leaving it true would make every later activate()
|
|
230
|
-
// early-return and never re-send the broadcast.
|
|
231
|
-
this.radarEnabled = false;
|
|
232
|
-
this.lastActivateError = e.message;
|
|
327
|
+
this.recordActivateFailure(e);
|
|
233
328
|
}
|
|
234
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* activate, off the event loop. Same skip guard, the same broadcast argv, and the same
|
|
332
|
+
* state recording as the sync twin, but the `ps` user detection and the broadcast run
|
|
333
|
+
* through execFileAsync so a caller serving HTTP (the dashboard's boot-arm chain) keeps
|
|
334
|
+
* answering while adb runs.
|
|
335
|
+
*/
|
|
336
|
+
async activateAsync(timeoutMinutes) {
|
|
337
|
+
if (!ADB) {
|
|
338
|
+
this.lastActivateError = "adb binary not found";
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const timeout = timeoutMinutes || this.currentTimeoutMinutes;
|
|
342
|
+
if (this.radarEnabled && timeout === this.currentTimeoutMinutes)
|
|
343
|
+
return;
|
|
344
|
+
try {
|
|
345
|
+
const userId = await this.detectDebugBuildUserAsync();
|
|
346
|
+
// The broadcast waits for ordered delivery, and a cold debug build has been observed to
|
|
347
|
+
// hold completion past 5 seconds; a killed broadcast reads as a failed arm, so this one
|
|
348
|
+
// call gets a taller ceiling than the quick adb queries.
|
|
349
|
+
await execFileAsync(ADB, enableBroadcastArgs(userId, timeout), { timeout: 15000 });
|
|
350
|
+
this.recordActivateSuccess(timeout);
|
|
351
|
+
}
|
|
352
|
+
catch (e) {
|
|
353
|
+
this.recordActivateFailure(e);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Whether the device-side Radar server's abstract socket exists right now, read from the
|
|
358
|
+
* device's own socket table so the answer does not travel through the adb forward. Through a
|
|
359
|
+
* forward every failure collapses to a connection reset, because the HOST adb server accepts
|
|
360
|
+
* the TCP connection unconditionally and only then discovers the device side is absent, so a
|
|
361
|
+
* ping cannot tell "no session exists" from "a session exists and the probe failed". This read
|
|
362
|
+
* can. "unknown" (adb missing, or the table could not be read) leaves the caller its safe branch.
|
|
363
|
+
*/
|
|
364
|
+
async radarSocketPresentAsync() {
|
|
365
|
+
if (!ADB)
|
|
366
|
+
return "unknown";
|
|
367
|
+
try {
|
|
368
|
+
const { stdout } = await execFileAsync(ADB, ["shell", "cat", "/proc/net/unix"], {
|
|
369
|
+
timeout: 5000,
|
|
370
|
+
});
|
|
371
|
+
// The @ pins the match to the abstract-namespace entry the forward targets (the table
|
|
372
|
+
// renders abstract sockets as @name), not any path merely containing the name.
|
|
373
|
+
return stdout.includes(`@${SOCKET_NAME}`) ? "present" : "absent";
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
return "unknown";
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
// A broadcast landed: record the session state and start streaming logcat to a
|
|
380
|
+
// session-scoped file so reads are not subject to the device ring buffer rotating
|
|
381
|
+
// under load. Shared by activate and activateAsync so the two cannot drift.
|
|
382
|
+
recordActivateSuccess(timeout) {
|
|
383
|
+
this.currentTimeoutMinutes = timeout;
|
|
384
|
+
this.radarEnabled = true;
|
|
385
|
+
this.lastActivateError = null;
|
|
386
|
+
registerCleanup();
|
|
387
|
+
if (ADB)
|
|
388
|
+
startCapture(ADB);
|
|
389
|
+
}
|
|
390
|
+
// Broadcast failed, device may not be connected. Recorded so the enable path can report
|
|
391
|
+
// it instead of claiming success with a failing ping. radarEnabled drops too: a prior
|
|
392
|
+
// success no longer proves the device side is armed, and leaving it true would make every
|
|
393
|
+
// later activate() early-return and never re-send the broadcast.
|
|
394
|
+
recordActivateFailure(e) {
|
|
395
|
+
this.radarEnabled = false;
|
|
396
|
+
this.lastActivateError = e.message;
|
|
397
|
+
}
|
|
235
398
|
getLastActivateError() {
|
|
236
399
|
return this.lastActivateError;
|
|
237
400
|
}
|
|
@@ -274,12 +437,13 @@ export class AndroidTransport {
|
|
|
274
437
|
throw new Error("adb not found on PATH or common SDK locations");
|
|
275
438
|
const tmpFile = path.join(os.tmpdir(), `slack-radar-screenshot-${Date.now()}.png`);
|
|
276
439
|
const devicePath = "/data/local/tmp/slack-radar-screenshot.png";
|
|
277
|
-
|
|
278
|
-
execSync(`${ADB}
|
|
279
|
-
execSync(`${ADB}
|
|
440
|
+
const stdio = ["ignore", "ignore", "pipe"];
|
|
441
|
+
execSync(`${ADB} shell screencap -p ${devicePath}`, { timeout: 10000, stdio });
|
|
442
|
+
execSync(`${ADB} pull ${devicePath} "${tmpFile}"`, { timeout: 10000, stdio });
|
|
443
|
+
execSync(`${ADB} shell rm ${devicePath}`, { timeout: 5000, stdio });
|
|
280
444
|
if (!fullRes) {
|
|
281
445
|
try {
|
|
282
|
-
execSync(`sips -Z 720 "${tmpFile}" 2>/dev/null`, { timeout: 5000 });
|
|
446
|
+
execSync(`sips -Z 720 "${tmpFile}" 2>/dev/null`, { timeout: 5000, stdio });
|
|
283
447
|
}
|
|
284
448
|
catch {
|
|
285
449
|
// Resize failed; keep full res
|
|
@@ -293,15 +457,16 @@ export class AndroidTransport {
|
|
|
293
457
|
const duration = Math.min(Math.max(durationS, 1), 30);
|
|
294
458
|
const tmpFile = path.join(os.tmpdir(), `slack-radar-recording-${Date.now()}.mp4`);
|
|
295
459
|
const devicePath = "/data/local/tmp/slack-radar-recording.mp4";
|
|
296
|
-
|
|
297
|
-
execSync(`${ADB}
|
|
298
|
-
execSync(`${ADB}
|
|
460
|
+
const recordStdio = ["ignore", "ignore", "pipe"];
|
|
461
|
+
execSync(`${ADB} shell screenrecord --time-limit ${duration} --size 720x1280 ${devicePath}`, { timeout: (duration + 5) * 1000, stdio: recordStdio });
|
|
462
|
+
execSync(`${ADB} pull ${devicePath} "${tmpFile}"`, { timeout: 10000, stdio: recordStdio });
|
|
463
|
+
execSync(`${ADB} shell rm ${devicePath}`, { timeout: 5000, stdio: recordStdio });
|
|
299
464
|
return tmpFile;
|
|
300
465
|
}
|
|
301
466
|
getAppState() {
|
|
302
467
|
if (!ADB)
|
|
303
468
|
throw new Error("adb not found on PATH or common SDK locations");
|
|
304
|
-
return execSync(`${ADB} shell "dumpsys activity activities | grep -A 5 'com.Slack.internal'"`, { timeout: 10000, encoding: "utf-8" }).trim();
|
|
469
|
+
return execSync(`${ADB} shell "dumpsys activity activities | grep -A 5 'com.Slack.internal'"`, { timeout: 10000, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
305
470
|
}
|
|
306
471
|
getRecentLogs(tag, limit, grep) {
|
|
307
472
|
if (!ADB)
|
|
@@ -339,6 +504,7 @@ export class AndroidTransport {
|
|
|
339
504
|
timeout: 10000,
|
|
340
505
|
encoding: "utf-8",
|
|
341
506
|
maxBuffer: 64 * 1024 * 1024,
|
|
507
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
342
508
|
});
|
|
343
509
|
const lines = output.split("\n").filter(Boolean);
|
|
344
510
|
return {
|
|
@@ -346,33 +512,27 @@ export class AndroidTransport {
|
|
|
346
512
|
source: "logcat_ring",
|
|
347
513
|
};
|
|
348
514
|
}
|
|
515
|
+
// Shares psDebugBuildProbe's `ps` call rather than repeating the pipeline: two copies
|
|
516
|
+
// of the same command drifted independently once already.
|
|
349
517
|
listProfiles() {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
label: id === "0" ? "Personal (user 0)" : `Secondary (user ${id}, e.g. a work profile)`,
|
|
366
|
-
});
|
|
367
|
-
}
|
|
518
|
+
const fallback = [{ userId: "0", label: "Personal (user 0)" }];
|
|
519
|
+
const ps = this.psDebugBuildLine();
|
|
520
|
+
if (!ps)
|
|
521
|
+
return fallback;
|
|
522
|
+
const seen = new Set();
|
|
523
|
+
const profiles = [];
|
|
524
|
+
for (const line of ps.split("\n")) {
|
|
525
|
+
const match = line.match(/u(\d+)_/);
|
|
526
|
+
if (match && !seen.has(match[1])) {
|
|
527
|
+
seen.add(match[1]);
|
|
528
|
+
const id = match[1];
|
|
529
|
+
profiles.push({
|
|
530
|
+
userId: id,
|
|
531
|
+
label: id === "0" ? "Personal (user 0)" : `Secondary (user ${id}, e.g. a work profile)`,
|
|
532
|
+
});
|
|
368
533
|
}
|
|
369
|
-
return profiles.length > 0
|
|
370
|
-
? profiles
|
|
371
|
-
: [{ userId: "0", label: "Personal (user 0)" }];
|
|
372
|
-
}
|
|
373
|
-
catch {
|
|
374
|
-
return [{ userId: "0", label: "Personal (user 0)" }];
|
|
375
534
|
}
|
|
535
|
+
return profiles.length > 0 ? profiles : fallback;
|
|
376
536
|
}
|
|
377
537
|
activateForUser(userId, timeoutMinutes) {
|
|
378
538
|
if (!ADB)
|
|
@@ -382,7 +542,12 @@ export class AndroidTransport {
|
|
|
382
542
|
// scoped to a profile we could not detect, and appending this profile's
|
|
383
543
|
// run to it would mix profiles in one file. Rotating is the safe default.
|
|
384
544
|
const profileChanged = this.detectedUserId !== userId;
|
|
385
|
-
|
|
545
|
+
// The same argv and the same tall ceiling as activate's twins: the broadcast waits for
|
|
546
|
+
// ordered delivery and a cold debug build can hold completion past 5 seconds.
|
|
547
|
+
execFileSync(ADB, enableBroadcastArgs(userId, timeout), {
|
|
548
|
+
timeout: 15000,
|
|
549
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
550
|
+
});
|
|
386
551
|
this.detectedUserId = userId;
|
|
387
552
|
this.currentTimeoutMinutes = timeout;
|
|
388
553
|
this.radarEnabled = true;
|
|
@@ -408,6 +573,7 @@ export class AndroidTransport {
|
|
|
408
573
|
const raw = execSync(`${ADB} devices -l`, {
|
|
409
574
|
timeout: 5000,
|
|
410
575
|
encoding: "utf-8",
|
|
576
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
411
577
|
});
|
|
412
578
|
// Empty output parses to [] — a valid "no devices attached" success, distinct
|
|
413
579
|
// from the null below which means the listing itself failed.
|
|
@@ -422,6 +588,24 @@ export class AndroidTransport {
|
|
|
422
588
|
return null;
|
|
423
589
|
}
|
|
424
590
|
}
|
|
591
|
+
// listDevices, off the event loop. Same parse, the same selected-device reconciliation,
|
|
592
|
+
// and the same [] / null distinction as the sync twin.
|
|
593
|
+
async listDevicesAsync() {
|
|
594
|
+
if (!ADB)
|
|
595
|
+
return [];
|
|
596
|
+
try {
|
|
597
|
+
const { stdout } = await execFileAsync(ADB, ["devices", "-l"], {
|
|
598
|
+
timeout: 5000,
|
|
599
|
+
encoding: "utf-8",
|
|
600
|
+
});
|
|
601
|
+
const devices = parseAdbDevicesOutput(stdout);
|
|
602
|
+
this.reconcileSelectedDevice(devices);
|
|
603
|
+
return devices;
|
|
604
|
+
}
|
|
605
|
+
catch {
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
425
609
|
// Drop the recorded selection if its serial is no longer in the attached list, so
|
|
426
610
|
// getSelectedDevice() (read by /health and the dashboard header) never claims a
|
|
427
611
|
// device that has disconnected. Deliberately does NOT touch process.env.ANDROID_SERIAL
|
|
@@ -436,7 +620,18 @@ export class AndroidTransport {
|
|
|
436
620
|
this.selectedDevice = null;
|
|
437
621
|
}
|
|
438
622
|
selectDevice(serial) {
|
|
439
|
-
|
|
623
|
+
return this.pickFromListing(this.listDevices(), serial);
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* selectDevice, off the event loop. The listing runs through execFileAsync; the pick
|
|
627
|
+
* itself is the exact code the sync twin runs (pickFromListing), so the two cannot
|
|
628
|
+
* disagree about which device wins.
|
|
629
|
+
*/
|
|
630
|
+
async selectDeviceAsync(serial) {
|
|
631
|
+
return this.pickFromListing(await this.listDevicesAsync(), serial);
|
|
632
|
+
}
|
|
633
|
+
// Everything selectDevice decides once a listing (or its failure, null) is in hand.
|
|
634
|
+
pickFromListing(devices, serial) {
|
|
440
635
|
if (devices === null) {
|
|
441
636
|
return {
|
|
442
637
|
ok: false,
|
|
@@ -459,7 +654,7 @@ export class AndroidTransport {
|
|
|
459
654
|
const match = attached.find((d) => d.serial === serial);
|
|
460
655
|
if (match) {
|
|
461
656
|
this.pin(match.serial, match.label);
|
|
462
|
-
return { ok: true, serial: match.serial, switched: switchedTo(match.serial) };
|
|
657
|
+
return { ok: true, serial: match.serial, switched: switchedTo(match.serial), devices };
|
|
463
658
|
}
|
|
464
659
|
const list = attached.length > 0 ? listAttached() : "none";
|
|
465
660
|
return {
|
|
@@ -475,7 +670,7 @@ export class AndroidTransport {
|
|
|
475
670
|
const stillAttached = attached.find((d) => d.serial === pinned);
|
|
476
671
|
if (stillAttached) {
|
|
477
672
|
this.pin(stillAttached.serial, stillAttached.label);
|
|
478
|
-
return { ok: true, serial: stillAttached.serial, switched: switchedTo(stillAttached.serial) };
|
|
673
|
+
return { ok: true, serial: stillAttached.serial, switched: switchedTo(stillAttached.serial), devices };
|
|
479
674
|
}
|
|
480
675
|
// The pinned device detached. Drop the stale env pin AND the recorded selection so
|
|
481
676
|
// /health does not keep claiming a device that is gone.
|
|
@@ -485,7 +680,7 @@ export class AndroidTransport {
|
|
|
485
680
|
// (c) Exactly one attached device: auto-pin it.
|
|
486
681
|
if (attached.length === 1) {
|
|
487
682
|
this.pin(attached[0].serial, attached[0].label);
|
|
488
|
-
return { ok: true, serial: attached[0].serial, switched: switchedTo(attached[0].serial) };
|
|
683
|
+
return { ok: true, serial: attached[0].serial, switched: switchedTo(attached[0].serial), devices };
|
|
489
684
|
}
|
|
490
685
|
// (d) Zero devices (or a fake adb that does not answer `devices -l`): no-op. The
|
|
491
686
|
// existing ensureForward failure path reports no-device cleanly, and returning
|
|
@@ -493,7 +688,7 @@ export class AndroidTransport {
|
|
|
493
688
|
if (attached.length === 0) {
|
|
494
689
|
// Nothing attached: clear any recorded selection so /health reports no device.
|
|
495
690
|
this.selectedDevice = null;
|
|
496
|
-
return { ok: true, serial: null };
|
|
691
|
+
return { ok: true, serial: null, devices };
|
|
497
692
|
}
|
|
498
693
|
// (e) 2+ attached devices with no explicit choice: ambiguous. The transport error stays a
|
|
499
694
|
// neutral, surface-agnostic core; each caller (MCP, web) appends its own retry guidance.
|
|
@@ -513,26 +708,105 @@ export class AndroidTransport {
|
|
|
513
708
|
process.env.ANDROID_SERIAL = serial;
|
|
514
709
|
this.selectedDevice = { serial, label };
|
|
515
710
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
711
|
+
/**
|
|
712
|
+
* Fresh `ps` probe for debug-build Slack processes. Reports what it could establish,
|
|
713
|
+
* not a yes/no: "stopped" only when the device answered and matched nothing, "unknown"
|
|
714
|
+
* when it could not answer at all.
|
|
715
|
+
*
|
|
716
|
+
* Two details carry that distinction, and both are load-bearing.
|
|
717
|
+
*
|
|
718
|
+
* The trailing `|| true` keeps a grep no-match from looking like a failure. Without it,
|
|
719
|
+
* grep's exit 1 arrives as an execSync throw, identical to a detached device, an
|
|
720
|
+
* unauthorized device, a missing adb and a timeout, so every one of them would read as
|
|
721
|
+
* "not running".
|
|
722
|
+
*
|
|
723
|
+
* Running `ps` through a command substitution and exiting 9 when it fails keeps the
|
|
724
|
+
* opposite mistake from happening. Piping `ps` straight into grep discards ps's own exit
|
|
725
|
+
* status, so a device whose `ps` rejects `-o USER,NAME` produced empty output and exit 0
|
|
726
|
+
* and read as a CONFIRMED stop, which is precisely the failure class this return type
|
|
727
|
+
* exists to rule out. Exit 9 throws instead, landing on "unknown".
|
|
728
|
+
*
|
|
729
|
+
* @param timeoutMs Ceiling for the adb round trip. Callers diagnosing an already-known
|
|
730
|
+
* failure should pass something short; see DIAGNOSTIC_PS_TIMEOUT_MS in web/server.ts.
|
|
731
|
+
*/
|
|
732
|
+
psDebugBuildProbe(timeoutMs = PS_PROBE_TIMEOUT_MS) {
|
|
519
733
|
if (!ADB)
|
|
520
|
-
return "
|
|
734
|
+
return { state: "unknown", lines: "" };
|
|
521
735
|
try {
|
|
522
|
-
const ps = execSync(`${ADB} shell
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
this.detectedUserId = parseUserFromPsOutput(ps);
|
|
531
|
-
return this.detectedUserId;
|
|
736
|
+
const ps = execSync(`${ADB} shell '${PS_DEBUG_BUILD_SCRIPT}'`, {
|
|
737
|
+
timeout: timeoutMs,
|
|
738
|
+
encoding: "utf-8",
|
|
739
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
740
|
+
}).trim();
|
|
741
|
+
return ps.length > 0
|
|
742
|
+
? { state: "running", lines: ps }
|
|
743
|
+
: { state: "stopped", lines: "" };
|
|
532
744
|
}
|
|
533
745
|
catch {
|
|
534
|
-
|
|
535
|
-
|
|
746
|
+
return { state: "unknown", lines: "" };
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
// psDebugBuildProbe, off the event loop. Same script, same tri-state classification.
|
|
750
|
+
async psDebugBuildProbeAsync(timeoutMs = PS_PROBE_TIMEOUT_MS) {
|
|
751
|
+
if (!ADB)
|
|
752
|
+
return { state: "unknown", lines: "" };
|
|
753
|
+
try {
|
|
754
|
+
const { stdout } = await execFileAsync(ADB, ["shell", PS_DEBUG_BUILD_SCRIPT], {
|
|
755
|
+
timeout: timeoutMs,
|
|
756
|
+
encoding: "utf-8",
|
|
757
|
+
});
|
|
758
|
+
const ps = stdout.trim();
|
|
759
|
+
return ps.length > 0
|
|
760
|
+
? { state: "running", lines: ps }
|
|
761
|
+
: { state: "stopped", lines: "" };
|
|
762
|
+
}
|
|
763
|
+
catch {
|
|
764
|
+
return { state: "unknown", lines: "" };
|
|
536
765
|
}
|
|
537
766
|
}
|
|
767
|
+
/** Raw `ps` lines for running debug-build Slack processes, or null if there are none. */
|
|
768
|
+
psDebugBuildLine() {
|
|
769
|
+
const probe = this.psDebugBuildProbe();
|
|
770
|
+
return probe.state === "running" ? probe.lines : null;
|
|
771
|
+
}
|
|
772
|
+
detectDebugBuildUser() {
|
|
773
|
+
if (this.detectedUserId !== null)
|
|
774
|
+
return this.detectedUserId;
|
|
775
|
+
return this.cacheUserFromPs(this.psDebugBuildLine());
|
|
776
|
+
}
|
|
777
|
+
// detectDebugBuildUser, off the event loop. Same cache, same fallback rules.
|
|
778
|
+
async detectDebugBuildUserAsync() {
|
|
779
|
+
if (this.detectedUserId !== null)
|
|
780
|
+
return this.detectedUserId;
|
|
781
|
+
const probe = await this.psDebugBuildProbeAsync();
|
|
782
|
+
return this.cacheUserFromPs(probe.state === "running" ? probe.lines : null);
|
|
783
|
+
}
|
|
784
|
+
// Cache only when a debug-build process was actually seen. Caching the "0"
|
|
785
|
+
// fallback would pin every later enable to user 0 for the process lifetime,
|
|
786
|
+
// so a work-profile build (user 10) launched after a failed detect would
|
|
787
|
+
// never receive the enable broadcast.
|
|
788
|
+
cacheUserFromPs(ps) {
|
|
789
|
+
if (!ps)
|
|
790
|
+
return "0";
|
|
791
|
+
const match = ps.match(/u(\d+)_/);
|
|
792
|
+
if (!match)
|
|
793
|
+
return "0";
|
|
794
|
+
this.detectedUserId = parseUserFromPsOutput(ps);
|
|
795
|
+
return this.detectedUserId;
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
* State of the debug-build Slack process, checked fresh via `ps` (independent of
|
|
799
|
+
* detectDebugBuildUser's cache, so a process that died after the cache was set still
|
|
800
|
+
* reads as stopped). Lets the enable flow tell a confirmed stop apart from a probe
|
|
801
|
+
* that could not reach the device, which is the difference between naming the fix and
|
|
802
|
+
* guessing at it.
|
|
803
|
+
*
|
|
804
|
+
* @param timeoutMs Optional ceiling for the adb round trip, defaulting to
|
|
805
|
+
* PS_PROBE_TIMEOUT_MS. A caller that is only explaining a failure it has already
|
|
806
|
+
* detected should pass something shorter, so the explanation cannot cost more than
|
|
807
|
+
* the operation it explains.
|
|
808
|
+
*/
|
|
809
|
+
processState(timeoutMs) {
|
|
810
|
+
return this.psDebugBuildProbe(timeoutMs).state;
|
|
811
|
+
}
|
|
538
812
|
}
|
|
@@ -13,8 +13,24 @@ export declare const RADAR_VERSION: string;
|
|
|
13
13
|
* exists for tests only: stream lifecycle tests stand up a local server on an
|
|
14
14
|
* ephemeral port so they cannot collide with a real forwarded device on 8099.
|
|
15
15
|
* Set before module load, like SLACK_RADAR_WEB_PORT.
|
|
16
|
+
*
|
|
17
|
+
* `let`, not `const`: the webapp auto-detect path (mcp/platform-detect.ts, wired from
|
|
18
|
+
* createTransport()) needs to repoint every existing RADAR_HOST:RADAR_PORT caller
|
|
19
|
+
* (radarRequest, android.ts's adb forward target) at the webapp dev server's port
|
|
20
|
+
* AFTER this module has already loaded and android callers have already captured the
|
|
21
|
+
* initial value via import. setRadarPort() is the only sanctioned way to change it; the
|
|
22
|
+
* initial-value expression below must stay exactly as it is today so android's
|
|
23
|
+
* zero-config default (8099) never changes.
|
|
16
24
|
*/
|
|
17
|
-
export declare
|
|
25
|
+
export declare let RADAR_PORT: number;
|
|
26
|
+
/**
|
|
27
|
+
* Override RADAR_PORT after module load. Called exactly once, only by the webapp
|
|
28
|
+
* auto-detect/explicit-platform path in mcp/index.ts's createTransport() -- never on the
|
|
29
|
+
* android path, so android's RADAR_PORT resolution stays byte-for-byte identical to
|
|
30
|
+
* today. ES module `let` exports are live bindings, so every existing importer (stream.ts,
|
|
31
|
+
* android.ts, index.ts's radarRequest) sees the new value immediately, with no re-import.
|
|
32
|
+
*/
|
|
33
|
+
export declare function setRadarPort(port: number): void;
|
|
18
34
|
/** Host address for the forwarded connection. */
|
|
19
35
|
export declare const RADAR_HOST = "127.0.0.1";
|
|
20
36
|
/** Max events to retain in each local SSE buffer. */
|
|
@@ -39,6 +55,14 @@ export declare const MAX_LOG_SESSION_BUFFER = 20000;
|
|
|
39
55
|
* `SLACK_RADAR_WEB_PORT` env override.
|
|
40
56
|
*/
|
|
41
57
|
export declare const WEB_PORT_DEFAULT = 8100;
|
|
58
|
+
/**
|
|
59
|
+
* Default port the webapp Radar debug server listens on, matching
|
|
60
|
+
* webpack/util/default-dev-port.ts in the webapp repo. Hardcoded rather than imported: this
|
|
61
|
+
* package does not depend on the webapp repo. Separate constant from RADAR_PORT/WEB_PORT_DEFAULT
|
|
62
|
+
* -- it names the webapp target's OWN default, which platform-detect.ts probes before anything
|
|
63
|
+
* commits to redirecting RADAR_PORT at it.
|
|
64
|
+
*/
|
|
65
|
+
export declare const WEBAPP_DEFAULT_PORT = 1989;
|
|
42
66
|
/**
|
|
43
67
|
* The Slack debug build whose sandbox the host can `run-as` into. Both the
|
|
44
68
|
* on-device SQLite browser (db.ts) and the app log-buffer reader (debug-log.ts)
|