@slack/radar-mcp 1.10.0 → 1.12.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.
@@ -32,6 +32,7 @@ export const MODES = [
32
32
  { key: "trace", label: "Traces", spec: { title: "Traces", summary: "Performance trace spans, click to see timing details and tags.", source: "trace", viz: ["rate", "graph", "feed", "inspector"], series: [{ metric: "ratePerSec", label: "spans/s", unit: "/s" }, { metric: "avg", field: "duration_ms", label: "avg ms", unit: "ms" }] } },
33
33
  { key: "log", label: "Logs", spec: { title: "Device Logs", summary: "Live Slack app logcat, color-coded by level. Scroll the full session buffer, select a range, copy raw lines.", source: "log", viz: ["feed", "inspector"], match: { package: "com.Slack.internal.debug" } } },
34
34
  { key: "db", label: "Database", spec: { title: "Device DB", summary: "Browse the on-device SQLite databases (read-only snapshot).", source: "db" } },
35
+ { key: "prefs", label: "Preferences", spec: { title: "Device Preferences", summary: "Browse the on-device SharedPreferences (including feature-flag overrides), read-only.", source: "prefs" } },
35
36
  ];
36
37
  // A preset is identified by what it DOES, not how it is labelled. canonicalSpecKey reduces a
37
38
  // spec to its behavioral identity so a spec that is semantically a preset still matches it
@@ -73,7 +74,23 @@ class Store {
73
74
  spec = { blank: true, viz: [] };
74
75
  conn = { state: "waiting", msg: "connecting…" };
75
76
  profileLabel = "detecting…";
77
+ serverVersion = "";
76
78
  profiles = [];
79
+ // Attached devices + the currently pinned one, for the header device picker. Mirrors how
80
+ // profiles/profileLabel are stored: plain fields the header reads via useStoreValue, refreshed
81
+ // off the same poll loop and emitted on a named event.
82
+ devices = [];
83
+ selectedDevice = null;
84
+ // True when the last /_control/devices poll reported adb_error: `adb devices` itself failed
85
+ // (adb not on PATH, server down), which is distinct from "zero devices attached". The header
86
+ // chip renders this differently so the user knows to check adb, not plug in a phone.
87
+ devicesError = false;
88
+ // Last device-select failure text (an /enable 409 or error), or null when the pick succeeded.
89
+ // Distinct from devicesError (adb itself failed) and from conn: a device-select failure used to
90
+ // ride conn via setConn("waiting", …), but any healthy pollHealth flips conn back to connected
91
+ // within 2s and the message vanished before the user read it. This field is owned by
92
+ // selectDevice, survives the health poll, and clears only on a successful pick.
93
+ deviceError = null;
77
94
  cache = [];
78
95
  cacheKeys = new Set();
79
96
  seen = {};
@@ -86,6 +103,11 @@ class Store {
86
103
  liveMatches = 0;
87
104
  graph = null;
88
105
  feedVersion = 0;
106
+ // Bumped on every observed serial change (first pin included), emitted as "db-resync". The DB
107
+ // tab keys its remount off this so it drops stale picked-db/table/results and re-fetches /dbs
108
+ // when the bound device changes — including the null->serial FIRST pin, where the tab loaded
109
+ // unpinned and its /dbs failed with an ambiguous run-as before any device was pinned.
110
+ dbSerialVersion = 0;
89
111
  customActive = false;
90
112
  lastCustomSpec = null;
91
113
  detailCache = new Map();
@@ -107,6 +129,11 @@ class Store {
107
129
  // connected but the dead EventSource is never re-opened and the device ring is never pulled,
108
130
  // so the tab shows nothing until a manual refresh.
109
131
  _dropped = false;
132
+ // The device_serial last seen from /health. Lets pollHealth detect a device SWITCH under a
133
+ // live dashboard (a re-enable that repinned ANDROID_SERIAL to different hardware) so it can
134
+ // drop the old device's cached feed once, on the switch edge. null = not yet observed; the
135
+ // first non-null serial just records it and does NOT clear (nothing stale to drop yet).
136
+ _lastSerial = null;
110
137
  _rafPending = false;
111
138
  _intervals = [];
112
139
  // ---- shared detail cache (bounded FIFO) ----
@@ -188,7 +215,12 @@ class Store {
188
215
  this.spec = { blank: true, viz: [] };
189
216
  this.conn = { state: "waiting", msg: "connecting…" };
190
217
  this.profileLabel = "detecting…";
218
+ this.serverVersion = "";
191
219
  this.profiles = [];
220
+ this.devices = [];
221
+ this.selectedDevice = null;
222
+ this.devicesError = false;
223
+ this.deviceError = null;
192
224
  this.cache = [];
193
225
  this.cacheKeys = new Set();
194
226
  this.seen = {};
@@ -199,6 +231,7 @@ class Store {
199
231
  this.liveMatches = 0;
200
232
  this.graph = null;
201
233
  this.feedVersion = 0;
234
+ this.dbSerialVersion = 0;
202
235
  this.customActive = false;
203
236
  this.lastCustomSpec = null;
204
237
  this.detailCache = new Map();
@@ -212,8 +245,38 @@ class Store {
212
245
  this._lastBoundLabel = null;
213
246
  this._backfillToken = 0;
214
247
  this._dropped = false;
248
+ this._lastSerial = null;
215
249
  this._rafPending = false;
216
250
  }
251
+ // Build the graph aggregate structure for the current spec: one zero-filled series per
252
+ // spec.series entry, or null when the spec has no graph. Shared by applySpec (fresh spec) and
253
+ // clearDeviceCaches (device switch) so the two paths cannot drift.
254
+ buildGraph() {
255
+ if (this.spec.blank || this.spec.source === "db" || this.spec.source === "prefs" || !Array.isArray(this.spec.viz) || !this.spec.viz.includes("graph") || !Array.isArray(this.spec.series)) {
256
+ return null;
257
+ }
258
+ return { N: GRAPH_N, series: this.spec.series.map((s) => ({ def: s, buckets: new Array(GRAPH_N).fill(0), counts: new Array(GRAPH_N).fill(0) })) };
259
+ }
260
+ // Drop every device-scoped cache when the dashboard switches to different hardware. The old
261
+ // device's records, dedup keys, learned field values, and detail bodies belong to the wrong
262
+ // phone. The live aggregates (counter, rate, graph) reset too: backfill re-ACCUMULATES into
263
+ // them rather than rebuilding them, so leaving them would layer the new device's counts on
264
+ // top of the old device's totals. The graph is REBUILT zero-filled (not nulled): nothing else
265
+ // reconstructs it after a switch, so nulling it would blank the Network/Trace charts until the
266
+ // next tab change. Timers and spec are left running.
267
+ clearDeviceCaches() {
268
+ this.cache = [];
269
+ this.cacheKeys = new Set();
270
+ this.seen = {};
271
+ this.detailCache = new Map();
272
+ this._capSeen = new Set();
273
+ this._capQueue = [];
274
+ this.total = 0;
275
+ this.liveMatches = 0;
276
+ this.times = [];
277
+ this.perSec = new Array(PERSEC_N).fill(0);
278
+ this.graph = this.buildGraph();
279
+ }
217
280
  // ---- start everything ----
218
281
  start() {
219
282
  this.bootDefault();
@@ -237,6 +300,14 @@ class Store {
237
300
  this.closeStream();
238
301
  }
239
302
  setConn(state, msg) {
303
+ // Unpinned multi-device is never "connected": frames can ride a stale forward from a prior
304
+ // pin and flip the dot green while the per-device surfaces (DB run-as) are broken by the
305
+ // ambiguity. Override any connected claim to a waiting "pick one" until a device is pinned.
306
+ // Centralized here so every caller (pollHealth, onFrame) is covered by the one derived getter.
307
+ if (state === "connected" && this.needsDevicePick) {
308
+ state = "waiting";
309
+ msg = "multiple devices attached, pick one";
310
+ }
240
311
  if (this.conn.state === state && this.conn.msg === (msg || ""))
241
312
  return;
242
313
  this.conn = { state, msg: msg || "" };
@@ -244,7 +315,17 @@ class Store {
244
315
  }
245
316
  async pollSpec() {
246
317
  try {
247
- const spec = await (await fetch("/spec")).json();
318
+ const res = await fetch("/spec");
319
+ // The server already stamps its version on /spec (the adopt-path check);
320
+ // reuse it here so the header can show which Radar version is serving.
321
+ // A missing header clears the badge rather than pinning a stale value
322
+ // (an older server rebound to the port would not send it).
323
+ const ver = res.headers.get("X-Radar-Version") ?? "";
324
+ if (ver !== this.serverVersion) {
325
+ this.serverVersion = ver;
326
+ this.emit("version", ver);
327
+ }
328
+ const spec = await res.json();
248
329
  const key = JSON.stringify(spec);
249
330
  if (key === this._specKey)
250
331
  return;
@@ -298,15 +379,12 @@ class Store {
298
379
  this.liveMatches = 0;
299
380
  this.times = [];
300
381
  this.perSec = new Array(PERSEC_N).fill(0);
301
- this.graph = null;
302
382
  this._capQueue.length = 0;
303
- if (!this.spec.blank && this.spec.source !== "db" && Array.isArray(this.spec.viz) && this.spec.viz.includes("graph") && Array.isArray(this.spec.series)) {
304
- this.graph = { N: GRAPH_N, series: this.spec.series.map((s) => ({ def: s, buckets: new Array(GRAPH_N).fill(0), counts: new Array(GRAPH_N).fill(0) })) };
305
- }
383
+ this.graph = this.buildGraph();
306
384
  this.emit("spec", this.spec);
307
- // DB browser + blank: fully quiet the streaming engine so it does not compete with the DB
308
- // pull. Close the SSE stream AND stop the per-second tick. The 100k cache is left intact.
309
- if (this.spec.blank || this.spec.source === "db") {
385
+ // DB/Prefs browser + blank: fully quiet the streaming engine so it does not compete with
386
+ // point-in-time operations. Close the SSE stream AND stop the per-second tick. The 100k cache is left intact.
387
+ if (this.spec.blank || this.spec.source === "db" || this.spec.source === "prefs") {
310
388
  this.closeStream();
311
389
  if (this._tick) {
312
390
  clearInterval(this._tick);
@@ -584,6 +662,17 @@ class Store {
584
662
  get matchedRetained() {
585
663
  return this.viewFrames().length;
586
664
  }
665
+ // 2+ READY devices attached but none pinned yet: a real, renderable state, not "connected".
666
+ // The header shows a "pick a device…" dropdown and the conn dot says so, instead of riding a
667
+ // stale forward and claiming live while the per-device surfaces (DB run-as) are broken by
668
+ // ambiguity. Ready-state only, matching the picker's option list and the server's ambiguity
669
+ // rule: an offline or unauthorized entry cannot be picked, so it must not put the dot in a
670
+ // pick-one state the dropdown cannot resolve. A pinned session (selectedDevice set) is never
671
+ // in this state, even with 2 devices attached.
672
+ get needsDevicePick() {
673
+ return (this.devices.filter((d) => d.state === "device").length >= 2 &&
674
+ this.selectedDevice === null);
675
+ }
587
676
  matches(ev) {
588
677
  const m = this.spec.match;
589
678
  if (!m)
@@ -606,6 +695,33 @@ class Store {
606
695
  const op = h.op || ">";
607
696
  return op === ">" ? v > h.value : op === ">=" ? v >= h.value : op === "<" ? v < h.value : op === "<=" ? v <= h.value : v === h.value;
608
697
  }
698
+ // Device-SWITCH edge detection, shared by pollHealth (quiet case) and fetchDevices (busy
699
+ // observer case). A changed non-null serial means a re-enable repinned to different hardware:
700
+ // drop the old device's cached feed + resync the stream ONCE. Skip the first observation
701
+ // (_lastSerial null): there is nothing stale to clear yet, just record the baseline. A null
702
+ // serial (not reported this poll) is ignored, never resets the baseline.
703
+ checkSerialEdge(serial) {
704
+ if (serial == null || serial === this._lastSerial)
705
+ return;
706
+ // Feed clear + stream resync only on a REAL switch (serial -> other serial): a null->serial
707
+ // FIRST pin has no stale feed to drop, it just records the baseline.
708
+ if (this._lastSerial !== null) {
709
+ this.clearDeviceCaches();
710
+ this.resyncStream();
711
+ }
712
+ this._lastSerial = serial;
713
+ // The DB tab, unlike the feed, DOES carry stale state across a first pin: it loaded unpinned
714
+ // and its /dbs failed with an ambiguous run-as. So the db-resync fires on ANY serial change,
715
+ // including null->serial, so the tab drops that failed state and re-fetches once pinned.
716
+ this.bumpDbSerial();
717
+ }
718
+ // Signal the DB tab to drop its picked-db/table/results and reload /dbs. The DB module cannot
719
+ // import the store (it is a point-in-time view with no SSE engine), so app.ts subscribes and
720
+ // remounts DbBrowser off this counter. Fires on every bound-device change, first pin included.
721
+ bumpDbSerial() {
722
+ this.dbSerialVersion++;
723
+ this.emit("db-resync", this.dbSerialVersion);
724
+ }
609
725
  // ---- health + profile ----
610
726
  async pollHealth() {
611
727
  if (Date.now() - this._lastEventTs < 6000) {
@@ -616,6 +732,12 @@ class Store {
616
732
  }
617
733
  try {
618
734
  const h = await (await fetch("/health")).json();
735
+ // Detect a device SWITCH under the live dashboard off /health's pinned serial. This edge
736
+ // only covers the QUIET case: the fresh-frame early return above skips this fetch while the
737
+ // stream is busy, so a switch made from another tab (whose stream stays busy here) is caught
738
+ // instead by fetchDevices, which polls /_control/devices every 5s regardless of stream
739
+ // activity. Between the two, both busy and quiet observer tabs converge within 5s.
740
+ this.checkSerialEdge(h.device_serial ?? null);
619
741
  if (h.device) {
620
742
  this._healthMisses = 0;
621
743
  this.recovered();
@@ -644,6 +766,7 @@ class Store {
644
766
  this.resyncStream();
645
767
  }
646
768
  async pollProfile() {
769
+ this.fetchDevices();
647
770
  try {
648
771
  const [profilesR, healthR] = await Promise.all([
649
772
  fetch("/_control/profiles").then((r) => r.json()).catch(() => null),
@@ -667,6 +790,66 @@ class Store {
667
790
  /* leave the last label */
668
791
  }
669
792
  }
793
+ // Pull the attached-device list + the pinned device for the header picker. Fired off the
794
+ // profile poll loop (same cadence as profiles) and again whenever an /enable 409 lands, so the
795
+ // dropdown reflects what is actually attached when the user is asked to disambiguate.
796
+ async fetchDevices() {
797
+ try {
798
+ const r = await (await fetch("/_control/devices")).json();
799
+ this.devices = r?.devices || [];
800
+ this.selectedDevice = r?.selected || null;
801
+ this.devicesError = !!r?.adb_error;
802
+ // Observer-tab switch coverage: a busy stream keeps pollHealth on its fresh-frame fast path,
803
+ // so its serial edge never fires. This poll gets the pinned serial regardless of stream
804
+ // activity, so drive the same switch detection off it (no extra request).
805
+ this.checkSerialEdge(this.selectedDevice?.serial ?? null);
806
+ this.emit("devices", this.devices);
807
+ }
808
+ catch {
809
+ /* transient; next poll retries */
810
+ }
811
+ }
812
+ // Pin a specific device for the session. POSTs the same /enable the reconnect button uses,
813
+ // with the chosen serial; on success it runs the same post-enable refresh as reconnect() so
814
+ // the UI does not wait for the next poll. On a REAL switch (target serial differs from the one
815
+ // last seen) it clears the old device's caches and resyncs HERE, deterministically, rather than
816
+ // leaning on pollHealth's serial edge: that edge is starved by the <6s fresh-frame fast path
817
+ // while the new stream is already delivering frames, so it may never fire. Same-serial reselect
818
+ // skips the clear (nothing stale). A 409/error is surfaced on deviceError (the device chip shows
819
+ // it) rather than via conn, which a healthy health poll would overwrite within 2s; the device
820
+ // list is refreshed so the dropdown is current and the user can pick a valid device.
821
+ async selectDevice(serial) {
822
+ try {
823
+ const r = await fetch("/enable?device_serial=" + encodeURIComponent(serial), { method: "POST" });
824
+ if (!r.ok && r.status === 409) {
825
+ const body = await r.json().catch(() => null);
826
+ // Surface the failure on the device chip itself, not via conn: a healthy pollHealth flips
827
+ // conn back to connected within 2s and would erase a setConn("waiting", …) message before
828
+ // the user reads it. deviceError is owned here and survives the health poll.
829
+ this.deviceError = (body && body.error) ? String(body.error) : "device select failed";
830
+ this.emit("devices", this.devices);
831
+ this.fetchDevices();
832
+ return;
833
+ }
834
+ // A successful pick clears any prior select failure.
835
+ this.deviceError = null;
836
+ this.emit("devices", this.devices);
837
+ if (serial !== this._lastSerial) {
838
+ this.clearDeviceCaches();
839
+ this._lastSerial = serial;
840
+ // Resync the DB tab too: a first pick (null->serial) leaves it holding the failed
841
+ // ambiguous /dbs; a real switch leaves it on the old device's databases.
842
+ this.bumpDbSerial();
843
+ }
844
+ }
845
+ catch {
846
+ /* ignore; the refresh below re-syncs state */
847
+ }
848
+ this.pollHealth();
849
+ this.pollProfile();
850
+ this.pollSpec();
851
+ this.resyncStream();
852
+ }
670
853
  // Re-open the SSE stream and backfill the device ring for the ACTIVE streaming source, so the
671
854
  // current tab repopulates from what the device already holds (not just new live events).
672
855
  // Needed because the EventSource does not survive a device drop and pollSpec only re-applies
@@ -682,14 +865,22 @@ class Store {
682
865
  resyncStream() {
683
866
  this._dropped = false;
684
867
  const src = this.spec.source;
685
- if (this.spec.blank || !src || src === "db")
868
+ if (this.spec.blank || !src || src === "db" || src === "prefs")
686
869
  return;
687
870
  this.openStream();
688
871
  this.backfill();
689
872
  }
690
873
  async reconnect() {
691
874
  try {
692
- await fetch("/enable", { method: "POST" });
875
+ const r = await fetch("/enable", { method: "POST" });
876
+ // 409 = ambiguous device (multiple attached, none picked). Surface the server's
877
+ // reason through the existing connection-status line so the user sees why enable
878
+ // did not connect, instead of a silent stall on "reconnecting…".
879
+ if (!r.ok && r.status === 409) {
880
+ const body = await r.json().catch(() => null);
881
+ if (body && body.error)
882
+ this.setConn("waiting", String(body.error));
883
+ }
693
884
  }
694
885
  catch {
695
886
  /* ignore */