File size: 1,772 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { getBackgroundWorkSnapshot, isBackgroundWorkLane } from "./background-work.js";
// Bounded diagnostics composition over the command queue's lane totals.
// Static lanes get full snapshots; per-session (dynamic) lanes collapse into
// one aggregate so a saturation snapshot can never become an unbounded payload.
import {
  type CommandLaneSnapshot,
  getCommandLaneSnapshot,
  listCommandLaneTotals,
} from "./command-queue.js";
import { CommandLane, STATIC_COMMAND_LANES } from "./lanes.js";

type DynamicCommandLaneSummary = {
  laneCount: number;
  activeCount: number;
  queuedCount: number;
  queuedLaneCount: number;
};

const STATIC_COMMAND_LANE_SET: ReadonlySet<string> = new Set(STATIC_COMMAND_LANES);

export function getCommandLaneDiagnostics(): {
  lanes: CommandLaneSnapshot[];
  dynamic: DynamicCommandLaneSummary | null;
} {
  const lanes = [...STATIC_COMMAND_LANES]
    .toSorted()
    .map((lane) =>
      lane === CommandLane.Background ? getBackgroundWorkSnapshot() : getCommandLaneSnapshot(lane),
    )
    // Disabled lanes disappear only once their outstanding work has cleared.
    .filter((lane) => lane.maxConcurrent > 0 || lane.activeCount > 0 || lane.queuedCount > 0);
  const dynamic: DynamicCommandLaneSummary = {
    laneCount: 0,
    activeCount: 0,
    queuedCount: 0,
    queuedLaneCount: 0,
  };
  for (const totals of listCommandLaneTotals()) {
    if (STATIC_COMMAND_LANE_SET.has(totals.lane) || isBackgroundWorkLane(totals.lane)) {
      continue;
    }
    dynamic.laneCount += 1;
    dynamic.activeCount += totals.activeCount;
    dynamic.queuedCount += totals.queuedCount;
    if (totals.queuedCount > 0) {
      dynamic.queuedLaneCount += 1;
    }
  }
  return { lanes, dynamic: dynamic.laneCount > 0 ? dynamic : null };
}