dsh-trajectory-traceview-plus 0.11.4 → 0.11.5

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/src/40-view.js CHANGED
@@ -262,6 +262,32 @@
262
262
  function TraceMapView({ useSession, useTrajectory, t, viewRequest, completeViewRequest, loadOlder, sessionKey }) {
263
263
  const scrollerRef = (0, react.useRef)(null);
264
264
  const minimapRef = (0, react.useRef)(null);
265
+ // Per-station rendered elements, so an unchanged card keeps its element identity and
266
+ // React skips the subtree entirely. Cleared when the session changes (ids are only
267
+ // unique within a session) — see `stationSig`'s comment.
268
+
269
+ const cardElCache = (0, react.useRef)(new Map());
270
+ const tickElCache = (0, react.useRef)(new Map());
271
+ // The caches live as long as the view, but their contents are only valid for one
272
+ // language and one session: station ids are per-session, and a cached element holds
273
+ // already-translated text. Clearing on change keeps `t` and `sessionKey` out of the
274
+ // per-element signature (they would invalidate every card anyway).
275
+ const sigCacheRef = (0, react.useRef)({ arr: null, sigs: [] });
276
+ const cacheScopeRef = (0, react.useRef)({ t: null, sessionKey: null });
277
+ // A cached element keeps the closures it was built with. Those closures would go
278
+ // stale the moment the view re-renders (arrow keys read the current selection, seek
279
+ // reads the current window) — so the cards call THROUGH this ref, which is refreshed
280
+ // on every render. Element identity stays cacheable, behaviour stays current.
281
+ const handlersRef = (0, react.useRef)({ seek: null, keyDown: null });
282
+
283
+
284
+ if (cacheScopeRef.current.t !== t || cacheScopeRef.current.sessionKey !== sessionKey) {
285
+ cacheScopeRef.current = { t, sessionKey };
286
+ cardElCache.current.clear();
287
+ tickElCache.current.clear();
288
+ sigCacheRef.current.arr = null;
289
+ sigCacheRef.current.sigs = [];
290
+ }
265
291
  const searchRef = (0, react.useRef)(null);
266
292
  const initDone = (0, react.useRef)(false);
267
293
  // Which session this panel is showing. The host reuses one component instance
@@ -983,6 +1009,17 @@
983
1009
  seek(i); ev.preventDefault(); ev.stopPropagation();
984
1010
  }
985
1011
  };
1012
+ // Refreshed after commit; a cached element reads it at click/key time. A layout
1013
+ // effect rather than a render-time assignment, because a render React throws away
1014
+ // would otherwise leave the abandoned pass's handler behind.
1015
+ //
1016
+ // DECLARED HERE, above `if (N === 0)` and its early return: a hook below that
1017
+ // branch is skipped on the empty/loading render, and React aborts the whole panel
1018
+ // with "Rendered fewer hooks than expected".
1019
+ (0, react.useLayoutEffect)(() => {
1020
+ handlersRef.current.seek = seek;
1021
+ handlersRef.current.keyDown = stationOnKeyDown;
1022
+ });
986
1023
  const onKeyDown = (ev) => {
987
1024
  const k = ev.key;
988
1025
  // Browser and OS shortcuts must win. The panel root is focusable
@@ -1295,6 +1332,77 @@
1295
1332
  });
1296
1333
  }
1297
1334
 
1335
+ /**
1336
+ * Element cache for the station list and the minimap.
1337
+ *
1338
+ * Why: the list renders EVERY station on every state change. Measured on a
1339
+ * 900-station window, one keystroke in the search box cost 1.3–2.2 s of main
1340
+ * thread — typing a query was unusable. The tree fold is NOT the cost (that is
1341
+ * memoised and does not re-run on a keystroke); the cost is rebuilding ~900 card
1342
+ * subtrees and ~900 minimap buttons to flip `data-seek-dim` on the handful of
1343
+ * stations that actually matched.
1344
+ *
1345
+ * Rebuilding an element and re-rendering it are different costs, and React bails
1346
+ * out of the second when the ELEMENT IS THE SAME OBJECT (referential equality).
1347
+ * So: cache the element per station and return the previous one when nothing the
1348
+ * card reads has changed. Compared by VALUE — each fold allocates fresh station
1349
+ * objects, so nothing else here can be compared by identity (§13.72).
1350
+ *
1351
+ * The signature covers exactly what the card body reads: the station fields
1352
+ * `renderStationCard` + the axis zone touch, its position, its neighbour's edge
1353
+ * (the idle band), and the four per-station booleans the parent computes.
1354
+ */
1355
+ // FNV-1a over the fields the card reads, accumulated WITHOUT building a
1356
+ // concatenated string. The first version joined every field into one string —
1357
+ // harmlessly fast on the fixture, but a real 1000-node window carries ~1.1 MB of
1358
+ // tool output, and joining that per station per render made a keystroke cost
1359
+ // 1.17 s again. A rolling hash over each field is linear either way, but it
1360
+ // allocates nothing (§13.73).
1361
+
1362
+ const stationSig = (s) => {
1363
+ if (s == null) return "";
1364
+ let h = 2166136261;
1365
+ const mix = (v) => {
1366
+ if (v === null || v === void 0) { h = (h ^ 31) * 16777619 >>> 0; return; }
1367
+ if (typeof v === "number") {
1368
+ h = (h ^ (v | 0)) * 16777619 >>> 0;
1369
+ h = (h ^ (Math.floor(v * 1000) | 0)) * 16777619 >>> 0;
1370
+ return;
1371
+ }
1372
+ if (typeof v === "boolean") { h = (h ^ (v ? 1 : 2)) * 16777619 >>> 0; return; }
1373
+ const str = String(v);
1374
+ h = (h ^ str.length) * 16777619 >>> 0;
1375
+ for (let i = 0; i < str.length; i += 1) h = ((h ^ str.charCodeAt(i)) * 16777619) >>> 0;
1376
+ };
1377
+ mix(s.kind); mix(s.id); mix(s.label); mix(s.who); mix(s.text);
1378
+ mix(s.displayText); mix(s.summary); mix(s.message); mix(s.name);
1379
+ mix(s.args); mix(s.payload); mix(s.eventType); mix(s.replace); mix(s.retry);
1380
+ mix(s.isError); mix(s.hasError); mix(s.live); mix(s.synthetic); mix(s.tag);
1381
+ mix(s.time); mix(s.startMs); mix(s.endMs); mix(s.seq); mix(s.toolMs); mix(s.maxToolMs);
1382
+ // The duration bar divides by a GLOBAL denominator (`stats.maxToolMs ||
1383
+ // stats.toolMs`), not by this station's own maximum. Mixing only the station
1384
+ // fields meant every cached bar kept its old width when a DIFFERENT station
1385
+ // became the slowest one — a stale proportion bar, caught by review.
1386
+ mix(stats.maxToolMs); mix(stats.toolMs);
1387
+ mix(s.outcomeText); mix(s.outcomeKind);
1388
+ if (Array.isArray(s.meta)) { mix(s.meta.length); for (const m of s.meta) mix(m); }
1389
+ else mix(s.meta);
1390
+ // EVERY cell field the card renders, not just the shape. The first version
1391
+ // hashed only kind/isError/running — so a settled step whose assistant cell text
1392
+ // changed kept the OLD text on the card (verified: ALPHA-ONE stayed on screen
1393
+ // while the detail panel showed ALPHA-TWO). `resultText` is deliberately absent:
1394
+ // the card does not render it and it is by far the largest field (§13.75).
1395
+ if (Array.isArray(s.cells)) {
1396
+ mix(s.cells.length);
1397
+ for (const c of s.cells) {
1398
+ mix(c.kind); mix(c.id);
1399
+ mix(c.text); mix(c.thinking);
1400
+ mix(c.name); mix(c.args); mix(c.errorName);
1401
+ mix(c.durationMs); mix(c.isError === true); mix(c.running === true);
1402
+ }
1403
+ } else mix(-1);
1404
+ return h.toString(36);
1405
+ };
1298
1406
  const renderStationCard = (s, cardKey) => {
1299
1407
  if (s.kind === "user") {
1300
1408
  return jsxs("div", {
@@ -1459,6 +1567,9 @@
1459
1567
  const minimapTicks = jsx("div", {
1460
1568
  className: "dstv_minimap",
1461
1569
  ref: minimapRef,
1570
+ // Same reason as the listbox: search dimming is one attribute on the strip,
1571
+ // not one per tick, so a keystroke does not rebuild every tick element.
1572
+ "data-searching": searching ? "true" : undefined,
1462
1573
  children: stations.map((s, i) => {
1463
1574
  const reached = i < R;
1464
1575
  let k = s.kind;
@@ -1466,13 +1577,34 @@
1466
1577
  if (s.kind === "maxTokens") k = "error";
1467
1578
  const done = reached && (s.kind !== "step" || !s.live);
1468
1579
  const matched = searching && matchSet.has(i);
1469
- return jsx("button", {
1580
+ // Same element-identity trick as the station cards: an unchanged tick keeps
1581
+ // its element and React skips it (§13.72).
1582
+ const tickSig = s.kind + "\u0002" + String(i) + "\u0002" + (reached ? "1" : "0")
1583
+ + (done ? "1" : "0") + (matched ? "1" : "0") + (s.live === true ? "1" : "0")
1584
+ + (k === "tool" || k === "step" ? String(s.cells.length) : "")
1585
+ // The highlight and the "N / total" part of the aria-label change with the
1586
+ // selection and with the window length. Missing either leaves the old
1587
+ // highlight cached — caught by the minimap assertion, not by reasoning.
1588
+ //
1589
+ // The tab stop is the SAME expression as `tabIndex` below, including its
1590
+ // overview half (`revealed === null && i === 0`). Hashing only the active
1591
+ // half left tick 0 with `tabIndex: 0` after any selection, so the strip had
1592
+ // TWO tab stops (measured: [0, 5] after selecting station 5).
1593
+ + "\u0002" + ((revealed !== null && i === R - 1) || (revealed === null && i === 0) ? "1" : "0")
1594
+ // `data-k` reads the DERIVED kind, which is "tool" for a step that contains
1595
+ // one — hash the value actually used rather than re-deriving it here.
1596
+ + "\u0002" + k
1597
+ + "\u0002" + minimapTickLabel(s) + "\u0002" + fmtClock(s.time)
1598
+ + "\u0002" + String(stations.length);
1599
+ const tickHit = tickElCache.current.get(i);
1600
+ if (tickHit !== undefined && tickHit.sig === tickSig && tickHit.t === t) return tickHit.el;
1601
+ const tickEl = jsx("button", {
1470
1602
  type: "button",
1471
1603
  className: "dstv_miniTick",
1472
1604
  "data-tick-idx": String(i),
1473
1605
  "data-k": k,
1474
1606
  "data-done": done ? "true" : undefined,
1475
- "data-dim": reached ? (searching && !matched ? "true" : undefined) : "true",
1607
+ "data-dim": reached ? undefined : "true",
1476
1608
  "data-active": revealed !== null && i === R - 1 ? "true" : undefined,
1477
1609
  "data-live": s.kind === "step" && s.live === true ? "true" : undefined,
1478
1610
  "data-match": matched ? "true" : undefined,
@@ -1490,11 +1622,40 @@
1490
1622
  // all. Fall back to the kind and always expose aria-label.
1491
1623
  title: `${minimapTickLabel(s)}${fmtClock(s.time) !== "" ? ` · ${fmtClock(s.time)}` : ""}`,
1492
1624
  "aria-label": `${t("detail.position")} ${String(i + 1)} / ${String(stations.length)} · ${minimapTickLabel(s)}${fmtClock(s.time) !== "" ? ` · ${fmtClock(s.time)}` : ""}`,
1493
- onClick: (ev) => { ev.stopPropagation(); seek(i); }
1625
+ onClick: (ev) => { ev.stopPropagation(); handlersRef.current.seek(i); }
1494
1626
  }, `mini${i}`);
1627
+ tickElCache.current.set(i, { sig: tickSig, t, el: tickEl });
1628
+ return tickEl;
1495
1629
  })
1496
1630
  }, "rootMini");
1497
1631
 
1632
+ // One signature per station, computed once per TREE rather than once per render.
1633
+ // The per-render version re-hashed every cell body on every keystroke and replay
1634
+ // tick: a real 1.11 MB window cost 105 ms per rebuild instead of 30 ms. `stations`
1635
+ // identity only changes when the fold produces a new tree, so keying on it is
1636
+ // exactly "recompute when the content could have changed".
1637
+ //
1638
+ // A ref rather than useMemo on purpose: this sits BELOW the `if (N === 0)` early
1639
+ // return, and a hook here would be skipped on the empty/loading render (§13.75).
1640
+ const sigPack = sigCacheRef.current;
1641
+ if (sigPack.arr !== stations) {
1642
+ sigPack.arr = stations;
1643
+ sigPack.sigs = stations.map(stationSig);
1644
+ // Drop what the new window no longer contains. A live station is replaced by its
1645
+ // durable twin (new id) on every fold, and paging in older history shifts every
1646
+ // index, so without this the element maps only ever grow — each entry pinning a
1647
+ // whole card subtree and its station closure (§13.75).
1648
+ if (cardElCache.current.size > stations.length || tickElCache.current.size > stations.length) {
1649
+ const live = new Set(stations.map((st) => st.id));
1650
+ for (const key of [...cardElCache.current.keys()]) {
1651
+ if (!live.has(key)) cardElCache.current.delete(key);
1652
+ }
1653
+ for (const key of [...tickElCache.current.keys()]) {
1654
+ if (!(key >= 0 && key < stations.length)) tickElCache.current.delete(key);
1655
+ }
1656
+ }
1657
+ }
1658
+ const sigOf = (i) => (i < 0 || i >= sigPack.sigs.length ? "" : sigPack.sigs[i]);
1498
1659
  const body = jsxs("div", {
1499
1660
  children: [
1500
1661
  jsx("div", {
@@ -1505,10 +1666,17 @@
1505
1666
  role: "listbox",
1506
1667
  "aria-label": t("view.label"),
1507
1668
  "aria-orientation": "horizontal",
1669
+ // Dimming non-matches while searching is a CSS job, not a per-card one.
1670
+ // Setting it per station meant the FIRST keystroke invalidated all 900
1671
+ // cards (2.4 s at 900 stations); one attribute on the list lets the
1672
+ // stylesheet do it and leaves every card's element identity intact.
1673
+ "data-searching": searching ? "true" : undefined,
1508
1674
  children: [
1509
1675
  revealed !== null && playheadLeft !== null ? jsx("span", { className: "dstv_playhead", "aria-hidden": "true", style: { left: `${playheadLeft}px` }, children: jsx("i", {}) }, "ph") : null,
1510
1676
  stations.map((s, i) => {
1511
1677
  const reached = i < R;
1678
+ const isActive = revealed !== null && i === R - 1;
1679
+ const matched = searching && matchSet.has(i);
1512
1680
  // G4-T3: idle gap vs the previous station's end (falls back to its
1513
1681
  // start / time). Rendered as an inline band so a long pause between
1514
1682
  // two steps is visible without opening either one.
@@ -1551,14 +1719,27 @@
1551
1719
  else if (s.kind === "unknown") dotCls += " unknown";
1552
1720
  else if (s.kind === "step" && s.cells.some((c) => c.kind === "tool")) dotCls += " tool";
1553
1721
  if (s.kind === "step" && s.live === true) dotCls += " live";
1554
- const matched = searching && matchSet.has(i);
1555
1722
  const clock = fmtClock(s.time);
1556
- const isActive = revealed !== null && i === R - 1;
1557
- return jsxs("div", {
1723
+ // Everything below is a pure function of (station, neighbour, position,
1724
+ // N, found-in-search). If none of those moved, hand React the element
1725
+ // it already rendered and it skips the whole subtree.
1726
+ const sig = [
1727
+ sigOf(i), sigOf(i - 1), String(i), String(N),
1728
+ reached ? "1" : "0", isActive ? "1" : "0",
1729
+ matched ? "1" : "0", clockAxis ? "1" : "0",
1730
+ // The overview state is its own dependency: station 0 is the roving
1731
+ // tab stop in the overview and is not once a selection exists, even
1732
+ // though neither `reached` nor `isActive` changes for it. Caught by
1733
+ // the roving-tab-stop assertion, not by reasoning (§13.72).
1734
+ revealed === null ? "1" : "0"
1735
+ ].join("\u0002");
1736
+ const hit = cardElCache.current.get(s.id);
1737
+ if (hit !== undefined && hit.sig === sig) return hit.el;
1738
+ const el = jsxs("div", {
1558
1739
  className: "dstv_station",
1559
1740
  "data-station-idx": String(i),
1560
1741
  "data-dim": reached ? undefined : "true",
1561
- "data-seek-dim": searching && !matched && reached ? "true" : undefined,
1742
+
1562
1743
  "data-match": matched ? "true" : undefined,
1563
1744
  "data-active": isActive ? "true" : undefined,
1564
1745
  role: "option",
@@ -1571,8 +1752,8 @@
1571
1752
  "aria-selected": revealed === null ? "false" : (isActive ? "true" : "false"),
1572
1753
  "aria-label": stationAriaLabel(s, i + 1, N, t),
1573
1754
  tabIndex: isActive || (revealed === null && i === 0) ? 0 : -1,
1574
- onKeyDown: (ev) => stationOnKeyDown(ev, i),
1575
- onClick: () => seek(i),
1755
+ onKeyDown: (ev) => handlersRef.current.keyDown(ev, i),
1756
+ onClick: () => handlersRef.current.seek(i),
1576
1757
  children: [
1577
1758
  ...gapPrefix,
1578
1759
  jsxs("div", {
@@ -1602,6 +1783,8 @@
1602
1783
  }, `card${s.id}`)
1603
1784
  ]
1604
1785
  }, s.id);
1786
+ cardElCache.current.set(s.id, { sig, el });
1787
+ return el;
1605
1788
  })
1606
1789
  ]
1607
1790
  })