@slack/radar-mcp 1.19.0 → 1.21.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 +48 -8
- package/dist/mcp/api-paths.d.ts +11 -0
- package/dist/mcp/api-paths.js +67 -16
- package/dist/mcp/index.js +24 -7
- package/dist/mcp/network-detail.d.ts +5 -0
- package/dist/mcp/network-detail.js +86 -0
- package/dist/mcp/radar-response.d.ts +7 -0
- package/dist/mcp/radar-response.js +15 -0
- package/dist/mcp/session-backfill.d.ts +9 -3
- package/dist/mcp/session-backfill.js +77 -17
- package/dist/mcp/session-detail.js +54 -2
- package/dist/mcp/session-query.js +53 -11
- package/dist/mcp/sources-gate.js +4 -0
- package/dist/mcp/switchboard-tools.d.ts +9 -0
- package/dist/mcp/switchboard-tools.js +40 -0
- package/dist/mcp/tools.js +69 -18
- package/dist/shared/constants.d.ts +14 -3
- package/dist/shared/constants.js +14 -3
- package/dist/shared/event-spool.js +3 -2
- package/dist/shared/form-body.d.ts +18 -0
- package/dist/shared/form-body.js +251 -0
- package/dist/shared/stream.d.ts +1 -1
- package/dist/shared/switchboard.d.ts +9 -0
- package/dist/shared/switchboard.js +54 -0
- package/dist/shared/wire.d.ts +32 -0
- package/dist/web/install-app.d.ts +64 -1
- package/dist/web/install-app.js +174 -24
- package/dist/web/macapp/RadarApp.swift +1287 -71
- package/dist/web/public/index.html +216 -11
- package/dist/web/public/next/app.js +22 -12
- package/dist/web/public/next/db.js +180 -18
- package/dist/web/public/next/feed.js +571 -53
- package/dist/web/public/next/filters.js +160 -34
- package/dist/web/public/next/form-body.js +252 -0
- package/dist/web/public/next/inspector.js +103 -12
- package/dist/web/public/next/registry.js +33 -3
- package/dist/web/public/next/store.js +577 -53
- package/dist/web/public/next/voice-model.js +532 -0
- package/dist/web/public/next/voice.js +397 -0
- package/dist/web/server.d.ts +110 -6
- package/dist/web/server.js +436 -40
- package/dist/web/spec.d.ts +13 -6
- package/dist/web/spec.js +89 -6
- package/package.json +3 -3
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import { CACHE_TTL_MS } from "./registry.js";
|
|
2
|
+
// Re-exported so a model test can assert the clamp without importing the registry module itself.
|
|
3
|
+
export { CACHE_TTL_MS };
|
|
4
|
+
// The handshake phases in the order the device runs them. A phase record is drawn in this order,
|
|
5
|
+
// not in arrival order, so a slow late phase still sits at the right end of the waterfall.
|
|
6
|
+
export const PHASE_ORDER = ["url_lookup", "prepare", "audio_claim", "mic_open", "socket", "session_ready"];
|
|
7
|
+
// A phase at or past this reads as slow: the session.ready wait is the one that times out at 10 s.
|
|
8
|
+
export const SLOW_PHASE_MS = 5000;
|
|
9
|
+
// Frame rate window, shared by uplink and downlink. Ten seconds smooths the 200 ms chunk cadence
|
|
10
|
+
// (5/s) without hiding a stall.
|
|
11
|
+
export const RATE_WINDOW_MS = 10_000;
|
|
12
|
+
// A young session divides by the time it has actually been alive, not the full window, floored
|
|
13
|
+
// so a brand-new session does not divide by (near) zero.
|
|
14
|
+
export const RATE_MIN_DIVISOR_MS = 1000;
|
|
15
|
+
// The whole-session zoom never draws a span shorter than this, or a session that just began
|
|
16
|
+
// squeezes its first records into a sliver.
|
|
17
|
+
export const MIN_SPAN_MS = 10_000;
|
|
18
|
+
// p95 arrival gap past this is the choppy playback the re-arm counter is warning about.
|
|
19
|
+
export const GAP_P95_BAD_MS = 300;
|
|
20
|
+
// Human-readable names for phase codes that read oddly raw. A name not in this map prints as-is.
|
|
21
|
+
export const PHASE_LABELS = {
|
|
22
|
+
url_lookup: "url lookup",
|
|
23
|
+
audio_claim: "audio claim",
|
|
24
|
+
mic_open: "mic open",
|
|
25
|
+
session_ready: "session.ready",
|
|
26
|
+
};
|
|
27
|
+
export function phaseLabel(name) {
|
|
28
|
+
return Object.hasOwn(PHASE_LABELS, name) ? PHASE_LABELS[name] : name;
|
|
29
|
+
}
|
|
30
|
+
export const ZOOMS = [
|
|
31
|
+
{ key: "60s", label: "60 s", spanMs: 60_000, title: "Last 60 seconds" },
|
|
32
|
+
{ key: "5min", label: "5 min", spanMs: 300_000, title: "Last 5 minutes" },
|
|
33
|
+
{ key: "whole", label: "whole session", spanMs: null, title: "Whole session" },
|
|
34
|
+
];
|
|
35
|
+
const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : null);
|
|
36
|
+
const str = (v) => (typeof v === "string" ? v : null);
|
|
37
|
+
const isRecord = (r) => r !== null && typeof r === "object";
|
|
38
|
+
export function recordTime(r) {
|
|
39
|
+
return num(r.timestamp);
|
|
40
|
+
}
|
|
41
|
+
// The newest timestamp among a set of records, or null when there are none. Rows are ascending
|
|
42
|
+
// (sessionRows), so it is the last row's, not a scan: this runs several times per build.
|
|
43
|
+
function newestTs(rows) {
|
|
44
|
+
return rows.length ? recordTime(rows[rows.length - 1]) : null;
|
|
45
|
+
}
|
|
46
|
+
// The session to draw: the id on the newest `session begin` record, the larger id breaking a
|
|
47
|
+
// timestamp tie (a restarted process's id can be smaller than the session it replaces). An
|
|
48
|
+
// `event: "end"` record never votes, on either pass. When no begin record is present at all,
|
|
49
|
+
// falls back to the newest non-end record's id, so a session missing its begin (evicted, or an
|
|
50
|
+
// older build that never sent one) can still be picked up. Records without a usable session_id
|
|
51
|
+
// or timestamp cannot vote, so a lone odd record yields null.
|
|
52
|
+
export function newestSessionId(rows) {
|
|
53
|
+
let bestBegin = null;
|
|
54
|
+
let bestAny = null;
|
|
55
|
+
for (const r of rows) {
|
|
56
|
+
if (!isRecord(r))
|
|
57
|
+
continue;
|
|
58
|
+
const id = num(r.session_id);
|
|
59
|
+
const t = recordTime(r);
|
|
60
|
+
if (id === null || t === null)
|
|
61
|
+
continue;
|
|
62
|
+
const isBegin = r.kind === "session" && r.event === "begin";
|
|
63
|
+
const isEnd = r.kind === "session" && r.event === "end";
|
|
64
|
+
if (isBegin && (!bestBegin || t > bestBegin.t || (t === bestBegin.t && id > bestBegin.id)))
|
|
65
|
+
bestBegin = { id, t };
|
|
66
|
+
if (!isEnd && (!bestAny || t > bestAny.t || (t === bestAny.t && id > bestAny.id)))
|
|
67
|
+
bestAny = { id, t };
|
|
68
|
+
}
|
|
69
|
+
const best = bestBegin ?? bestAny;
|
|
70
|
+
return best ? best.id : null;
|
|
71
|
+
}
|
|
72
|
+
// The records of one session, oldest first, from its LAST `session begin` record onward (a
|
|
73
|
+
// session_id restarts with the app process, so an old process's rows under the same id must not
|
|
74
|
+
// bleed into the new one). All of the id's rows when no begin record is present. Only object
|
|
75
|
+
// rows with a numeric timestamp take part; anything else is dropped rather than sorted somewhere
|
|
76
|
+
// arbitrary.
|
|
77
|
+
export function sessionRows(rows, sessionId) {
|
|
78
|
+
const all = rows
|
|
79
|
+
.filter((r) => isRecord(r) && num(r.session_id) === sessionId && recordTime(r) !== null)
|
|
80
|
+
.sort((a, b) => recordTime(a) - recordTime(b) || (num(a.id) ?? 0) - (num(b.id) ?? 0));
|
|
81
|
+
let lastBeginAt = null;
|
|
82
|
+
for (const r of all) {
|
|
83
|
+
if (r.kind === "session" && r.event === "begin")
|
|
84
|
+
lastBeginAt = recordTime(r);
|
|
85
|
+
}
|
|
86
|
+
return lastBeginAt === null ? all : all.filter((r) => recordTime(r) >= lastBeginAt);
|
|
87
|
+
}
|
|
88
|
+
// The last `session end` record's timestamp, or null when the session has not ended.
|
|
89
|
+
export function sessionEndAt(rows) {
|
|
90
|
+
let at = null;
|
|
91
|
+
for (const r of rows) {
|
|
92
|
+
if (r.kind === "session" && r.event === "end")
|
|
93
|
+
at = recordTime(r);
|
|
94
|
+
}
|
|
95
|
+
return at;
|
|
96
|
+
}
|
|
97
|
+
// The latest state record of one state_kind, e.g. the current connection state.
|
|
98
|
+
export function latestState(rows, stateKind) {
|
|
99
|
+
let out = null;
|
|
100
|
+
for (const r of rows) {
|
|
101
|
+
if (r.kind !== "state" || r.state_kind !== stateKind)
|
|
102
|
+
continue;
|
|
103
|
+
const to = str(r.to);
|
|
104
|
+
if (to === null)
|
|
105
|
+
continue;
|
|
106
|
+
out = { to, cause: str(r.cause), at: recordTime(r) };
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
// Spans where a state_kind sat on `onValue`: opened by a state record whose `to` is onValue,
|
|
111
|
+
// closed by the next state record of that kind that moves off it, or by `now` if nothing has.
|
|
112
|
+
// Two opens in a row keep the first (the second carries no new information).
|
|
113
|
+
export function segments(rows, stateKind, onValue, now) {
|
|
114
|
+
const out = [];
|
|
115
|
+
let openAt = null;
|
|
116
|
+
for (const r of rows) {
|
|
117
|
+
if (r.kind !== "state" || r.state_kind !== stateKind)
|
|
118
|
+
continue;
|
|
119
|
+
const t = recordTime(r);
|
|
120
|
+
const on = r.to === onValue;
|
|
121
|
+
if (on && openAt === null)
|
|
122
|
+
openAt = t;
|
|
123
|
+
else if (!on && openAt !== null) {
|
|
124
|
+
out.push({ from: openAt, to: Math.max(t, openAt), open: false });
|
|
125
|
+
openAt = null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (openAt !== null)
|
|
129
|
+
out.push({ from: openAt, to: Math.max(now, openAt), open: true });
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
// A tts.stop marker's own label needs to know what happened after it: whether the model actually
|
|
133
|
+
// went quiet before the next stop was sent, and how long that took. Scans forward from the stop's
|
|
134
|
+
// own position rather than the whole array, so an earlier stop never borrows a later stop's quiet.
|
|
135
|
+
// `wasSpeaking` comes from the forward walk markers() already makes and its one-time seed (see
|
|
136
|
+
// lastSpeakingTo there) rather than its own backward scan: a scan re-run from every stop marker is
|
|
137
|
+
// quadratic on a session with plenty of stops and no speaking record early on, since it would walk
|
|
138
|
+
// all the way back to index 0 each time.
|
|
139
|
+
function stopFollowUpLabel(rows, fromIdx, stopAt, wasSpeaking, ended) {
|
|
140
|
+
if (!wasSpeaking)
|
|
141
|
+
return "tts.stop sent · model already quiet";
|
|
142
|
+
for (let j = fromIdx + 1; j < rows.length; j++) {
|
|
143
|
+
const r = rows[j];
|
|
144
|
+
// A later stop arrived before any quiet or re-entry followed this one: this stop's own fate
|
|
145
|
+
// is still "still speaking", whatever happens after the next stop, so it says when that next
|
|
146
|
+
// stop actually landed rather than just repeating the same bare "still speaking" as the
|
|
147
|
+
// no-later-stop case below.
|
|
148
|
+
if (r.kind === "frame" && r.direction === "outgoing" && r.frame_type === "tts.stop")
|
|
149
|
+
return `tts.stop sent · still speaking, next stop sent ${recordTime(r) - stopAt} ms later`;
|
|
150
|
+
if (r.kind === "state" && r.state_kind === "speaking") {
|
|
151
|
+
if (r.to === "quiet")
|
|
152
|
+
return `tts.stop sent · quiet after ${recordTime(r) - stopAt} ms`;
|
|
153
|
+
// A speaking-to-speaking re-entry: the model started over without ever honouring this stop,
|
|
154
|
+
// so crediting it with whatever quiet comes after the re-entry would be attributing someone
|
|
155
|
+
// else's quiet to it.
|
|
156
|
+
if (r.to === "speaking")
|
|
157
|
+
return "tts.stop sent · model started a new turn";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Nothing at all followed this stop: a live session may still go quiet later, so it reads as
|
|
161
|
+
// "no quiet yet" rather than the same "still speaking" an ended session gets, where there is no
|
|
162
|
+
// "later" left for the quiet to arrive in.
|
|
163
|
+
return ended ? "tts.stop sent · still speaking at session end" : "tts.stop sent · no quiet yet";
|
|
164
|
+
}
|
|
165
|
+
// A player event's gap plus, when the wire sent one, how much was still queued: "gap 410 ms, 0 B
|
|
166
|
+
// queued" reads a starving player very differently from one that re-armed with plenty on hand.
|
|
167
|
+
// `gapWord` is "gap " for a re-arm, "read blocked " for a stall (the stall label reads "stall,
|
|
168
|
+
// read blocked 410 ms", not "stall, gap 410 ms" -- nothing "re-armed", the read just sat there).
|
|
169
|
+
// No dedicated byte-formatting helper lives in this pure module (the one byte formatter in the
|
|
170
|
+
// codebase, prefs.ts's formatSize, is a private, unexported function this file could not import
|
|
171
|
+
// even if it wanted to), so a plain `${n} B` it is. Empty when the gap itself is unusable:
|
|
172
|
+
// nothing to attach a queue size to.
|
|
173
|
+
function gapQueueSuffix(gapWord, gapMs, bytesQueued) {
|
|
174
|
+
if (gapMs === null)
|
|
175
|
+
return "";
|
|
176
|
+
const queued = bytesQueued === null ? "" : `, ${bytesQueued} B queued`;
|
|
177
|
+
return `, ${gapWord}${gapMs} ms${queued}`;
|
|
178
|
+
}
|
|
179
|
+
// The moments worth a tick on the markers row. One record can be at most one marker. With a
|
|
180
|
+
// `window`, only records inside [from, to] become markers, and the walk starts at the window's
|
|
181
|
+
// first record (rows are ascending, see sessionRows) rather than at the session's: the panel
|
|
182
|
+
// keeps a few markers out of a session that can hold tens of thousands of rows.
|
|
183
|
+
export function markers(rows, window) {
|
|
184
|
+
const out = [];
|
|
185
|
+
// Whether the session has already ended, decided once up front rather than per stop marker: a
|
|
186
|
+
// stop with nothing following it reads "no quiet yet" while the session is still live, "still
|
|
187
|
+
// speaking" once it is not (there is no later left for the quiet to land in).
|
|
188
|
+
const ended = window ? window.ended : rows.some((r) => r.kind === "session" && r.event === "end");
|
|
189
|
+
let start = 0;
|
|
190
|
+
if (window) {
|
|
191
|
+
start = rows.length;
|
|
192
|
+
while (start > 0) {
|
|
193
|
+
const t = recordTime(rows[start - 1]);
|
|
194
|
+
if (t !== null && t < window.from)
|
|
195
|
+
break;
|
|
196
|
+
start--;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// The most recent speaking state's `to`, updated as the single walk below reaches each record:
|
|
200
|
+
// a stop marker reads this instead of scanning backward for it, which is what keeps this whole
|
|
201
|
+
// function one walk over the rows rather than one walk per stop marker. A windowed walk seeds
|
|
202
|
+
// it from the last speaking record before the window, so a stop just inside the edge still
|
|
203
|
+
// knows whether the model was speaking; that seed stops at the first speaking record it meets,
|
|
204
|
+
// and at worst reaches the session's begin (rows start there, see sessionRows).
|
|
205
|
+
let lastSpeakingTo = null;
|
|
206
|
+
for (let i = start - 1; i >= 0 && lastSpeakingTo === null; i--) {
|
|
207
|
+
const r = rows[i];
|
|
208
|
+
if (r.kind === "state" && r.state_kind === "speaking")
|
|
209
|
+
lastSpeakingTo = str(r.to);
|
|
210
|
+
}
|
|
211
|
+
for (let i = start; i < rows.length; i++) {
|
|
212
|
+
const r = rows[i];
|
|
213
|
+
const t = recordTime(r);
|
|
214
|
+
if (t === null)
|
|
215
|
+
continue;
|
|
216
|
+
if (window && t > window.to)
|
|
217
|
+
break;
|
|
218
|
+
if (r.kind === "state" && r.state_kind === "speaking") {
|
|
219
|
+
const to = str(r.to);
|
|
220
|
+
if (to !== null)
|
|
221
|
+
lastSpeakingTo = to;
|
|
222
|
+
}
|
|
223
|
+
if (r.kind === "player" && r.event === "rearm") {
|
|
224
|
+
const gap = num(r.gap_ms);
|
|
225
|
+
out.push({ kind: "rearm", t, label: `playback re-arm${gapQueueSuffix("gap ", gap, num(r.bytes_queued))}`, record: r });
|
|
226
|
+
}
|
|
227
|
+
else if (r.kind === "player" && r.event === "read_stall") {
|
|
228
|
+
const gap = num(r.gap_ms);
|
|
229
|
+
out.push({ kind: "stall", t, label: `player stall${gapQueueSuffix("read blocked ", gap, num(r.bytes_queued))}`, record: r });
|
|
230
|
+
}
|
|
231
|
+
else if (r.kind === "frame" && r.direction === "outgoing" && r.frame_type === "tts.stop") {
|
|
232
|
+
// No speaking record at all before this stop, or any state other than a confirmed "quiet",
|
|
233
|
+
// defaults to "yes, it was speaking" (the long-standing behaviour): an unknown or odd prior
|
|
234
|
+
// state is not the same claim as a confirmed quiet.
|
|
235
|
+
const wasSpeaking = lastSpeakingTo !== "quiet";
|
|
236
|
+
out.push({ kind: "stop", t, label: stopFollowUpLabel(rows, i, t, wasSpeaking, ended), record: r });
|
|
237
|
+
}
|
|
238
|
+
else if (r.kind === "state" && r.state_kind === "mic" && r.to === "Paused") {
|
|
239
|
+
const cause = str(r.cause);
|
|
240
|
+
out.push({ kind: "focus", t, label: cause ? `mic Paused (${cause})` : "mic Paused", record: r });
|
|
241
|
+
}
|
|
242
|
+
else if (r.kind === "state" && r.state_kind === "connection" && r.to === "Failed") {
|
|
243
|
+
const cause = str(r.cause);
|
|
244
|
+
out.push({ kind: "error", t, label: cause ? `connection Failed (${cause})` : "connection Failed", record: r });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
// `sessionBeginAt`, when given, lets a session younger than the window divide by its own age
|
|
250
|
+
// (floored at 1 s) instead of the full window, so a 2 s old session with 10 frames reads 5.0/s
|
|
251
|
+
// rather than 1.0/s. Shared by uplinkRate and downlinkRate, which differ only in which frame
|
|
252
|
+
// direction they count.
|
|
253
|
+
function frameRate(rows, direction, now, windowMs, sessionBeginAt) {
|
|
254
|
+
const effNow = Math.max(now, newestTs(rows) ?? now);
|
|
255
|
+
const from = effNow - windowMs;
|
|
256
|
+
// Rows are ascending (sessionRows), so the window is a suffix: walk back from the end and stop
|
|
257
|
+
// at its edge, rather than testing every row of the session for a 10 s window.
|
|
258
|
+
const dirFrames = [];
|
|
259
|
+
for (let i = rows.length - 1; i >= 0; i--) {
|
|
260
|
+
const r = rows[i];
|
|
261
|
+
const t = recordTime(r);
|
|
262
|
+
if (t === null)
|
|
263
|
+
continue;
|
|
264
|
+
if (t <= from)
|
|
265
|
+
break;
|
|
266
|
+
if (t <= effNow && r.kind === "frame" && r.direction === direction)
|
|
267
|
+
dirFrames.push(r);
|
|
268
|
+
}
|
|
269
|
+
// Fall back to every frame in that direction only when NONE of them carries a frame_type at all
|
|
270
|
+
// (a hand-built fixture that never bothered tagging; every device build tags its audio frames);
|
|
271
|
+
// a frame_type present but not "audio" (a control frame like "config") is real information, so
|
|
272
|
+
// a window like that counts audio frames only, zero included, rather than quietly guessing. An
|
|
273
|
+
// empty window has no untagged frame to fall back FOR, so it reads as the audio rule too (0
|
|
274
|
+
// audio frames), not the fallback -- `.every()` on an empty array is vacuously true and would
|
|
275
|
+
// otherwise claim the fallback rule for a window that never saw a frame of any kind.
|
|
276
|
+
const allUntagged = dirFrames.length > 0 && dirFrames.every((r) => r.frame_type == null);
|
|
277
|
+
const audio = dirFrames.filter((r) => r.frame_type === "audio");
|
|
278
|
+
const picked = allUntagged ? dirFrames : audio;
|
|
279
|
+
const age = sessionBeginAt !== null ? effNow - sessionBeginAt : windowMs;
|
|
280
|
+
const divisorMs = Math.max(RATE_MIN_DIVISOR_MS, Math.min(windowMs, age));
|
|
281
|
+
const secs = divisorMs / 1000;
|
|
282
|
+
return { perSec: secs > 0 ? picked.length / secs : 0, rule: allUntagged ? `all ${direction}` : "audio", count: picked.length };
|
|
283
|
+
}
|
|
284
|
+
export function uplinkRate(rows, now, windowMs = RATE_WINDOW_MS, sessionBeginAt = null) {
|
|
285
|
+
return frameRate(rows, "outgoing", now, windowMs, sessionBeginAt);
|
|
286
|
+
}
|
|
287
|
+
export function downlinkRate(rows, now, windowMs = RATE_WINDOW_MS, sessionBeginAt = null) {
|
|
288
|
+
return frameRate(rows, "incoming", now, windowMs, sessionBeginAt);
|
|
289
|
+
}
|
|
290
|
+
// A device build in a bad state can send the same handshake dozens of odd, one-off phase names;
|
|
291
|
+
// past this many unknown-order entries the waterfall reads as noise, not a chart, so the rest
|
|
292
|
+
// collapse into one "+N more" entry rather than drawing all of them.
|
|
293
|
+
const MAX_EXTRA_PHASES = 8;
|
|
294
|
+
// One entry per phase name in handshake order, the latest record of a name winning; names the
|
|
295
|
+
// order does not know go after, in arrival order, capped at MAX_EXTRA_PHASES with a trailing
|
|
296
|
+
// "+N more" entry. A phase without a numeric duration is skipped.
|
|
297
|
+
export function phases(rows) {
|
|
298
|
+
const byName = new Map();
|
|
299
|
+
for (const r of rows) {
|
|
300
|
+
if (r.kind !== "phase")
|
|
301
|
+
continue;
|
|
302
|
+
const name = str(r.phase);
|
|
303
|
+
const ms = num(r.duration_ms);
|
|
304
|
+
if (name === null || ms === null)
|
|
305
|
+
continue;
|
|
306
|
+
byName.set(name, ms);
|
|
307
|
+
}
|
|
308
|
+
const known = PHASE_ORDER.filter((n) => byName.has(n));
|
|
309
|
+
const extra = [...byName.keys()].filter((n) => !PHASE_ORDER.includes(n));
|
|
310
|
+
const shownExtra = extra.slice(0, MAX_EXTRA_PHASES);
|
|
311
|
+
const overflow = extra.length - shownExtra.length;
|
|
312
|
+
const list = [...known, ...shownExtra].map((name) => {
|
|
313
|
+
const ms = byName.get(name);
|
|
314
|
+
return { name, ms, slow: ms >= SLOW_PHASE_MS };
|
|
315
|
+
});
|
|
316
|
+
if (overflow > 0)
|
|
317
|
+
list.push({ name: `+${overflow} more`, ms: 0, slow: false, overflow: true });
|
|
318
|
+
return { list, totalMs: list.reduce((a, p) => a + p.ms, 0) };
|
|
319
|
+
}
|
|
320
|
+
// Nearest-rank percentile: the value at ceil(p * n), 1-based, of the sorted sample. 0 for an
|
|
321
|
+
// empty sample, so a card can print 0 rather than a dash.
|
|
322
|
+
export function percentile(values, p) {
|
|
323
|
+
if (!values.length)
|
|
324
|
+
return 0;
|
|
325
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
326
|
+
const rank = Math.min(sorted.length, Math.max(1, Math.ceil(p * sorted.length)));
|
|
327
|
+
return sorted[rank - 1];
|
|
328
|
+
}
|
|
329
|
+
// The four stat cards, over the records inside [from, to].
|
|
330
|
+
// Rows are ascending (sessionRows), so this walks back from the end and stops at `from`: one
|
|
331
|
+
// pass over the window, not several over the session.
|
|
332
|
+
export function windowStats(rows, from, to) {
|
|
333
|
+
let rearms = 0;
|
|
334
|
+
let rearmsStarved = 0;
|
|
335
|
+
const gaps = [];
|
|
336
|
+
let jitterTrim = 0;
|
|
337
|
+
let preReadyDrop = 0;
|
|
338
|
+
let framesIn = 0;
|
|
339
|
+
let framesOut = 0;
|
|
340
|
+
for (let i = rows.length - 1; i >= 0; i--) {
|
|
341
|
+
const r = rows[i];
|
|
342
|
+
const t = recordTime(r);
|
|
343
|
+
if (t === null)
|
|
344
|
+
continue;
|
|
345
|
+
if (t < from)
|
|
346
|
+
break;
|
|
347
|
+
if (t > to)
|
|
348
|
+
continue;
|
|
349
|
+
if (r.kind === "player") {
|
|
350
|
+
if (r.event === "rearm") {
|
|
351
|
+
rearms++;
|
|
352
|
+
if (num(r.bytes_queued) === 0)
|
|
353
|
+
rearmsStarved++;
|
|
354
|
+
const g = num(r.gap_ms);
|
|
355
|
+
if (g !== null)
|
|
356
|
+
gaps.push(g);
|
|
357
|
+
}
|
|
358
|
+
else if (r.event === "jitter_trim")
|
|
359
|
+
jitterTrim++;
|
|
360
|
+
else if (r.event === "pre_ready_drop")
|
|
361
|
+
preReadyDrop++;
|
|
362
|
+
}
|
|
363
|
+
else if (r.kind === "frame") {
|
|
364
|
+
if (r.direction === "incoming")
|
|
365
|
+
framesIn++;
|
|
366
|
+
else if (r.direction === "outgoing")
|
|
367
|
+
framesOut++;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
rearms,
|
|
372
|
+
rearmsStarved,
|
|
373
|
+
gapP50: percentile(gaps, 0.5),
|
|
374
|
+
gapP95: percentile(gaps, 0.95),
|
|
375
|
+
gapSamples: gaps.length,
|
|
376
|
+
dropped: jitterTrim + preReadyDrop,
|
|
377
|
+
jitterTrim,
|
|
378
|
+
preReadyDrop,
|
|
379
|
+
framesIn,
|
|
380
|
+
framesOut,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
// The whole-session span: from the session's begin record (or its earliest record when the begin
|
|
384
|
+
// was evicted or never seen) to `to` (never earlier than `now`, nor than the newest record here,
|
|
385
|
+
// which guards a device clock running ahead of the dashboard's). `from` never runs earlier than
|
|
386
|
+
// the store's cache TTL behind `to` (a span stretching into records the cache has already evicted
|
|
387
|
+
// reads as "quiet" rather than "gone"), and the span is never shorter than MIN_SPAN_MS. `clamped`
|
|
388
|
+
// is true exactly when the retention clamp is what pushed `from` later than the session's own
|
|
389
|
+
// natural start (`start`) -- the MIN_SPAN_MS widening moves `from` earlier instead, never later,
|
|
390
|
+
// so it never sets this on its own.
|
|
391
|
+
export function sessionSpan(rows, now) {
|
|
392
|
+
const to = Math.max(now, newestTs(rows) ?? now);
|
|
393
|
+
const begin = rows.find((r) => r.kind === "session" && r.event === "begin");
|
|
394
|
+
const first = begin ?? rows[0];
|
|
395
|
+
const start = first ? recordTime(first) : now;
|
|
396
|
+
const oldestAllowed = to - CACHE_TTL_MS;
|
|
397
|
+
const from = Math.max(Math.min(start, to - MIN_SPAN_MS), oldestAllowed);
|
|
398
|
+
return { from, to, clamped: from > start };
|
|
399
|
+
}
|
|
400
|
+
export function windowFor(rows, zoom, now) {
|
|
401
|
+
const z = ZOOMS.find((x) => x.key === zoom) ?? ZOOMS[0];
|
|
402
|
+
if (z.spanMs === null)
|
|
403
|
+
return sessionSpan(rows, now);
|
|
404
|
+
const to = Math.max(now, newestTs(rows) ?? now);
|
|
405
|
+
return { from: to - z.spanMs, to };
|
|
406
|
+
}
|
|
407
|
+
// Axis tick spacing: the smallest step from a short list of tidy values that keeps the axis to
|
|
408
|
+
// at most eight ticks, so 60 s reads in tens and an hour-long session in ten-minute steps.
|
|
409
|
+
const TICK_STEPS_MS = [10_000, 30_000, 60_000, 120_000, 300_000, 600_000, 1_800_000, 3_600_000];
|
|
410
|
+
export function tickStepMs(spanMs) {
|
|
411
|
+
for (const s of TICK_STEPS_MS)
|
|
412
|
+
if (spanMs / s <= 8)
|
|
413
|
+
return s;
|
|
414
|
+
return TICK_STEPS_MS[TICK_STEPS_MS.length - 1];
|
|
415
|
+
}
|
|
416
|
+
// Everything the panel draws for the newest session at `now`, or null when no record can name a
|
|
417
|
+
// session (no switchboard record at all, or only ones missing session_id / timestamp).
|
|
418
|
+
export function buildVoiceModel(rows, zoom, now) {
|
|
419
|
+
const sessionId = newestSessionId(rows);
|
|
420
|
+
if (sessionId === null)
|
|
421
|
+
return null;
|
|
422
|
+
const rs = sessionRows(rows, sessionId);
|
|
423
|
+
const beginRec = rs.find((r) => r.kind === "session" && r.event === "begin");
|
|
424
|
+
const hasBegin = beginRec !== undefined;
|
|
425
|
+
const endedAt = sessionEndAt(rs);
|
|
426
|
+
// Once a session has ended, nothing after that timestamp is part of it: a stray post-end
|
|
427
|
+
// record (a malformed or reordered one) must not stretch the window or re-open a segment.
|
|
428
|
+
const modelRows = endedAt !== null ? rs.filter((r) => recordTime(r) <= endedAt) : rs;
|
|
429
|
+
const closeAt = endedAt !== null ? endedAt : now;
|
|
430
|
+
const window = windowFor(modelRows, zoom, closeAt);
|
|
431
|
+
const inWindow = (s) => s.to >= window.from && s.from <= window.to;
|
|
432
|
+
const ph = phases(modelRows);
|
|
433
|
+
const hasReady = ph.list.some((p) => p.name === "session_ready");
|
|
434
|
+
const socketIdx = ph.list.findIndex((p) => p.name === "socket");
|
|
435
|
+
const socketRec = [...modelRows].filter((r) => r.kind === "phase" && r.phase === "socket").pop();
|
|
436
|
+
// The latest connection state; the pill below starts from it.
|
|
437
|
+
const latestConn = latestState(modelRows, "connection");
|
|
438
|
+
// The last `to: "Failed"` connection record, not just the latest connection state: a trailing
|
|
439
|
+
// Disconnected (or any other state) after the real Failed(Timeout) must not hide it.
|
|
440
|
+
let lastFailed = null;
|
|
441
|
+
for (const r of modelRows) {
|
|
442
|
+
if (r.kind === "state" && r.state_kind === "connection" && str(r.to) === "Failed") {
|
|
443
|
+
lastFailed = { to: "Failed", cause: str(r.cause), at: recordTime(r) };
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const failedAt = lastFailed ? lastFailed.at : null;
|
|
447
|
+
// A connection that ever reached Connected is done handshaking, whatever its phase records
|
|
448
|
+
// say: a synthetic entry here would be reporting a timeout on a handshake that plainly finished.
|
|
449
|
+
const hasConnected = modelRows.some((r) => r.kind === "state" && r.state_kind === "connection" && r.to === "Connected");
|
|
450
|
+
const causeIsTimeout = failedAt !== null && lastFailed?.cause === "Timeout";
|
|
451
|
+
let phaseTimedOut = false;
|
|
452
|
+
// Only an actual Timeout earns a synthetic phase: a hang-up or a plain disconnect mid-handshake
|
|
453
|
+
// (UserHangup, Server, or just an `end` record with no Failed at all) isn't the device waiting
|
|
454
|
+
// out the clock, so it gets no synthetic entry and no "timed out" reading, whatever socketRec says.
|
|
455
|
+
if (!hasReady && !hasConnected && causeIsTimeout) {
|
|
456
|
+
if (!socketRec) {
|
|
457
|
+
// The handshake never even reached the socket phase, so there is nothing to measure the
|
|
458
|
+
// wait from but whatever phase record landed last (or the session's own begin, absent that).
|
|
459
|
+
const phaseRecords = modelRows.filter((r) => r.kind === "phase" && str(r.phase) !== null && num(r.duration_ms) !== null);
|
|
460
|
+
const lastPhaseAt = phaseRecords.length
|
|
461
|
+
? phaseRecords.reduce((mx, r) => Math.max(mx, recordTime(r)), -Infinity)
|
|
462
|
+
: beginRec
|
|
463
|
+
? recordTime(beginRec)
|
|
464
|
+
: failedAt;
|
|
465
|
+
const entry = { name: "socket", ms: Math.max(0, failedAt - lastPhaseAt), slow: true, timedOut: true };
|
|
466
|
+
// The handshake never got as far as session_ready either, so that phase never reached: a
|
|
467
|
+
// real entry, `ms: 0` and `neverReached: true`, rather than a fake elapsed wait -- the panel
|
|
468
|
+
// draws no bar for it (there is nothing to size a bar from) but still names it in the inline
|
|
469
|
+
// phase text below.
|
|
470
|
+
const neverReachedReady = { name: "session_ready", ms: 0, slow: false, neverReached: true };
|
|
471
|
+
// Same placement rule as the session.ready branch below: right after the last known
|
|
472
|
+
// handshake-order phase that actually landed, not tacked onto the very end of the list.
|
|
473
|
+
const knownCount = ph.list.filter((p) => PHASE_ORDER.includes(p.name)).length;
|
|
474
|
+
ph.list.splice(knownCount, 0, entry, neverReachedReady);
|
|
475
|
+
ph.totalMs += entry.ms;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
const from = recordTime(socketRec);
|
|
479
|
+
// causeIsTimeout already guarantees failedAt is set, so this is always the Failed timestamp.
|
|
480
|
+
const to = failedAt;
|
|
481
|
+
const entry = { name: "session_ready", ms: Math.max(0, to - from), slow: true, timedOut: true };
|
|
482
|
+
ph.list.splice(socketIdx === -1 ? ph.list.length : socketIdx + 1, 0, entry);
|
|
483
|
+
ph.totalMs += entry.ms;
|
|
484
|
+
}
|
|
485
|
+
phaseTimedOut = true;
|
|
486
|
+
}
|
|
487
|
+
// Reported on the total line's "never" wording: from the session's own begin when there is one
|
|
488
|
+
// (the honest wall-clock wait), else the phase durations' sum is the best guess left.
|
|
489
|
+
const phaseTimedOutMs = phaseTimedOut ? (beginRec ? failedAt - recordTime(beginRec) : ph.totalMs) : null;
|
|
490
|
+
const phaseTimedOutFromBegin = phaseTimedOut && beginRec !== undefined;
|
|
491
|
+
// "start to ready", the non-timed-out case: the wall clock from begin to the session_ready
|
|
492
|
+
// phase record's own timestamp, when both exist, rather than the phase durations' sum -- two
|
|
493
|
+
// phases can overlap or leave a gap the durations alone never show. Falls back to the sum when
|
|
494
|
+
// either is missing.
|
|
495
|
+
const readyRec = modelRows.filter((r) => r.kind === "phase" && str(r.phase) === "session_ready" && num(r.duration_ms) !== null).pop();
|
|
496
|
+
const phaseWallIsSum = !(readyRec && beginRec);
|
|
497
|
+
const phaseWallMs = phaseWallIsSum ? ph.totalMs : recordTime(readyRec) - recordTime(beginRec);
|
|
498
|
+
let connection = latestConn;
|
|
499
|
+
if (connection === null && hasBegin && endedAt === null) {
|
|
500
|
+
connection = { to: "Connecting", cause: null, at: recordTime(beginRec) };
|
|
501
|
+
}
|
|
502
|
+
const beginAt = beginRec ? recordTime(beginRec) : null;
|
|
503
|
+
const whole = sessionSpan(modelRows, closeAt);
|
|
504
|
+
return {
|
|
505
|
+
sessionId,
|
|
506
|
+
connection,
|
|
507
|
+
mic: latestState(modelRows, "mic"),
|
|
508
|
+
speaking: latestState(modelRows, "speaking"),
|
|
509
|
+
uplink: uplinkRate(modelRows, closeAt, RATE_WINDOW_MS, beginAt),
|
|
510
|
+
downlink: downlinkRate(modelRows, closeAt, RATE_WINDOW_MS, beginAt),
|
|
511
|
+
sessionAgeMs: beginAt !== null ? closeAt - beginAt : null,
|
|
512
|
+
phases: ph.list,
|
|
513
|
+
phaseTotalMs: ph.totalMs,
|
|
514
|
+
phaseTimedOut,
|
|
515
|
+
phaseWallMs,
|
|
516
|
+
phaseWallIsSum,
|
|
517
|
+
phaseTimedOutMs,
|
|
518
|
+
phaseTimedOutFromBegin,
|
|
519
|
+
window,
|
|
520
|
+
model: segments(modelRows, "speaking", "speaking", closeAt).filter(inWindow),
|
|
521
|
+
you: segments(modelRows, "mic", "Capturing", closeAt).filter(inWindow),
|
|
522
|
+
youMuted: segments(modelRows, "mic", "Muted", closeAt).filter(inWindow),
|
|
523
|
+
markers: markers(modelRows, { from: window.from, to: window.to, ended: endedAt !== null }),
|
|
524
|
+
stats: windowStats(modelRows, window.from, window.to),
|
|
525
|
+
hasBegin,
|
|
526
|
+
// The retention clamp is a property of the whole-session span itself (sessionSpan derives it
|
|
527
|
+
// as `from > start`), gated on hasBegin: a session with no surviving begin record has nothing
|
|
528
|
+
// real to have been clamped away from, whatever its earliest surviving record's own age is.
|
|
529
|
+
wholeSpanClamped: hasBegin && whole.clamped,
|
|
530
|
+
ended: endedAt !== null,
|
|
531
|
+
};
|
|
532
|
+
}
|