Repository navigation
Expand file tree
/
Copy pathego-ledger.js
More file actions
1340 lines (1250 loc) · 65.8 KB
/
Copy pathego-ledger.js
File metadata and controls
1340 lines (1250 loc) · 65.8 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================================
// XERJ.ai — SECOND BRAIN · EGO LEDGER primitives
//
// The graph view is a LEDGER, not a hairball. A global force-directed
// graph was rejected on purpose: it collapses past ~200 nodes, its
// layout is non-deterministic (fatal for a memory you revisit — the
// same brain must always draw the same picture), and it optimises for
// looking at over navigating. The ledger is an ego view:
//
// LEFT column = what links INTO the focus note, grouped by kind
// CENTRE = the focus card (one note, its text, its provenance)
// RIGHT column = what the focus note links OUT to, grouped by kind
//
// Graph gestalt comes from 1px orthogonal traces drawn from the focus
// card to each GROUP HEADER (never to every row — zero occlusion, zero
// simulation). Two-steps-away appears as a "+N MORE" count on each
// neighbour row — a promise of the next click, not 27 more drawn
// nodes. The signature interaction is the BELIEF-TIME strip: every
// link's lifetime on one time axis with a draggable as-of caret.
//
// THE MAP (ux/brain-map.js) sits ABOVE this ledger and does not repeal
// the rationale: it draws a BOUNDED number of structure-derived groups
// with a deterministic seeded layout (no live simulation), and every
// terminal click lands back HERE — the ledger stays the leaf view and
// the only evidence surface. This file also renders the mount point,
// the shared scrubber panel, and the statistics row.
//
// LANGUAGE RULE: this surface speaks user words — "link", "believed
// since", "retired", "what taught this". Schema names (src/dst/
// valid_at/edge_id) appear ONLY inside the evidence paper-trail, which
// is the developer-facing raw view, and even there labelled in words.
//
// Honesty rules (§0 invariant 8 of the second-brain contract): every
// cap this view applies is labelled in place ("TOP 12 OF 41"), the
// honesty panel always renders — zeros prove absence — and the
// lexical-embedder disclosure rides the timeline panel.
//
// Every export is a pure HTML-string renderer except
// `drawLedgerTraces`, which measures the live DOM (trace geometry
// depends on final text layout, so it must run post-paint).
// ============================================================
import { esc, fmt, num, Num } from './text.js';
import { Spark, Series, Dist } from './charts.js';
import { vocabFor } from './sb-vocab.js';
/** The display vocabulary for a render's data bag — note-like corpora keep
* the "notes / believed" persona, everything else gets the neutral
* "records / active" frame (issue #1100). Unknown ⇒ neutral. */
const vocOf = (data) => vocabFor(!!(data && data.sb && data.sb.noteLike));
// Rows shown per link-kind group before the in-band "TOP n OF m" cap.
export const LEDGER_GROUP_CAP = 12;
// Belief-strip form switch: up to this many links the strip draws one
// lifetime row per link; beyond it, individual 1px rows become an
// unreadable hatch, so the strip switches to a belief-count curve over
// the same time axis (same caret, same drag). Both forms count every
// fetched link — the switch changes the drawing, never the truth.
export const SCRUB_LIFETIME_MAX = 24;
// ----- user-word vocabulary ----------------------------------------
/** Detector-flavoured edge types → words a first-run user knows.
* Unknown types (hand-asserted vocabularies) are prettified raw. */
const TYPE_WORDS = {
wikilink: 'WIKI-LINKS',
mdlink: 'MARKDOWN LINKS',
href: 'HTML LINKS',
pathcite: 'CITES FILE',
cratecite: 'CITES CRATE',
sequence: 'READING ORDER',
same_dir: 'SAME FOLDER',
shared_term: 'SHARED WORDING',
manual: 'HAND-ASSERTED',
};
export const typeName = (t) =>
TYPE_WORDS[t] || String(t || 'links').replace(/[_-]+/g, ' ').toUpperCase();
/** Detector tags ("wikilink@1") → the same user vocabulary as the link
* kinds, with the version kept — versioning IS part of the promise
* (same folder in, same links out, bump on any behavior change). The
* raw tag stays available on hover and in the evidence paper-trail. */
const DETECTOR_WORDS = {
wikilink: 'WIKI-LINKS',
mdlink: 'MARKDOWN LINKS',
href: 'HTML LINKS',
pathcite: 'CITES FILE',
cratecite: 'CITES CRATE',
sequence: 'READING ORDER',
samedir: 'FOLDER NEIGHBORS',
sharedterm: 'SHARED WORDING',
manual: 'HAND-ASSERTED',
};
export const detectorName = (tag) => {
const [base, v] = String(tag || '').split('@');
const w = DETECTOR_WORDS[base];
if (!w) return String(tag || 'unknown').toUpperCase();
return v ? `${w} · v${v}` : w;
};
// ----- evidence honesty classes -------------------------------------
//
// Not every link's evidence is a quote, and saying so is load-bearing:
// AUTHORED links (wikilink/mdlink/href) carry the exact text the
// author wrote — a real quote, quoted.
// STRUCTURAL links (samedir/sequence/sharedterm) carry a detector-
// generated rationale — real evidence of WHY, but never text from
// the note, so it must never wear quotation marks. sharedterm's
// rationale NAMES the shared words, which is the whole reason an
// inferred link is inspectable at all.
// Links with no evidence at all (asserted by hand or agent without a
// quote) say so — absence is shown, not papered over.
// pathcite/cratecite quote the author's actual line (a real citation the
// author typed, resolved structurally) — authored, quote-rendered.
const AUTHORED_DETECTORS = new Set(['wikilink', 'mdlink', 'href', 'pathcite', 'cratecite']);
const STRUCTURAL_DETECTORS = new Set(['samedir', 'sequence', 'sharedterm']);
const detectorBase = (tag) => String(tag || '').split('@')[0];
/** 'quote' | 'rationale' | 'none' — how this link's evidence renders. */
export const evidenceKind = (e) => {
const q = e && e.evidence && e.evidence.quote;
if (!q) return 'none';
return STRUCTURAL_DETECTORS.has(detectorBase(e.detector)) ? 'rationale' : 'quote';
};
/**
* Live-link counts split by what kind of teaching stands behind them,
* from `overview.detectors`. AUTHORED = a person wrote that link;
* STRUCTURAL = folder position / sort order (a prior, not a citation);
* anything else (hand- or agent-asserted vocabularies) is counted
* apart, never folded in. This split is what keeps a wikilink-free
* corpus honest: "0 authored · 214 structural", not a proud 214.
*/
export function authoredSplit(detectors) {
const s = { authored: 0, structural: 0, asserted: 0 };
for (const d of detectors || []) {
const b = detectorBase(d.detector);
if (AUTHORED_DETECTORS.has(b)) s.authored += d.live || 0;
else if (STRUCTURAL_DETECTORS.has(b)) s.structural += d.live || 0;
else s.asserted += d.live || 0;
}
return s;
}
/** The split as one lowercase hint phrase. */
export const splitLabel = (s) =>
`${num(s.authored)} authored · ${num(s.structural)} structural${s.asserted > 0 ? ` · ${num(s.asserted)} asserted` : ''}`;
/** Ids are opaque doc ids — autoindex ones are long hashes. Never make
* a human read 32 hex chars as a name. */
const looksHashy = (id) => /^[0-9a-f]{16,}$/i.test(String(id || ''));
export const displayId = (id) => {
const s = String(id || '');
return looksHashy(s) ? `${s.slice(0, 10)}…` : s;
};
/** Display name for a node id: hydrated title first (ego nodes, then
* the console's own name cache), else the id (shortened if hashy). */
const nodeName = (id, ctx) => {
const fromEgo = ctx && ctx.nodes && ctx.nodes[id] && ctx.nodes[id].title;
if (fromEgo) return fromEgo;
const fromCache = ctx && ctx.names && ctx.names[id] && ctx.names[id].title;
if (fromCache) return fromCache;
return displayId(id);
};
// ----- time ---------------------------------------------------------
/** Epoch-ms → 'YYYY-MM-DD HH:MM' (UTC — belief time is server time). */
export const fmtTs = (ms) => {
if (ms == null || !Number.isFinite(Number(ms))) return '—';
return new Date(Number(ms)).toISOString().slice(0, 16).replace('T', ' ');
};
/** Epoch-ms → 'YYYY-MM-DD' (UTC). Row-width-friendly. */
export const fmtDay = (ms) => {
if (ms == null || !Number.isFinite(Number(ms))) return '—';
return new Date(Number(ms)).toISOString().slice(0, 10);
};
/** A link's lifetime as words. The tags on the row (RETIRED / NOT
* YET) carry the state at the caret; this carries the facts. */
const fmtWindow = (validAt, invalidAt) =>
invalidAt == null
? `since ${fmtDay(validAt)}`
: `${fmtDay(validAt)} → ${fmtDay(invalidAt)}`;
// ----- bi-temporal cut (client-side preview only) -------------------
//
// The SERVER owns the as-of cut (`ego?as_of=`). These helpers exist so
// the scrubber can preview visibility over already-fetched links while
// the caret is mid-drag; the release commits an honest re-query.
export const edgeStateAt = (edge, asOfMs) => {
const t = asOfMs == null ? Date.now() : asOfMs;
if (Number(edge.valid_at) > t) return 'future';
if (edge.invalid_at != null && Number(edge.invalid_at) <= t) return 'expired';
return 'live';
};
// ----- neighbours + grouping ---------------------------------------
/** The node on the far end of an edge, given the expansion direction. */
const farEnd = (edge) => (edge.direction === 'in' ? edge.src : edge.dst);
/**
* Hop-1 edges of one side, grouped by type. Order is deterministic:
* groups by count desc then type asc; rows keep the server's contract
* order (hop asc, weight desc, edge_id asc) untouched — the same data
* always draws the same picture.
*/
export function groupEdges(edges, side) {
const byType = new Map();
for (const e of edges || []) {
if (e.hop !== 1) continue;
if ((side === 'out') !== (e.direction === 'out')) continue;
if (!byType.has(e.type)) byType.set(e.type, []);
byType.get(e.type).push(e);
}
const groups = [...byType.entries()].map(([type, list]) => ({ type, list }));
groups.sort((a, b) => b.list.length - a.list.length || (a.type < b.type ? -1 : a.type > b.type ? 1 : 0));
return groups;
}
/**
* Per-neighbour hop-2 degree: how many two-steps-away links touch this
* node, counting only links live at the caret. When the server clipped
* the result set the count is a floor, rendered '≥n' — never a fake
* exact.
*/
export function hop2Counts(edges, asOfMs) {
const counts = new Map();
for (const e of edges || []) {
if (e.hop !== 2) continue;
if (edgeStateAt(e, asOfMs) !== 'live') continue;
counts.set(e.src, (counts.get(e.src) || 0) + 1);
counts.set(e.dst, (counts.get(e.dst) || 0) + 1);
}
return counts;
}
// ----- evidence -----------------------------------------------------
/** Detector-aware evidence line. A quote is quoted; a structural
* rationale is prefixed WHY — and never dressed as text from the
* note; no evidence says so in as many words. */
const evidenceLine = (e) => {
const kind = evidenceKind(e);
if (kind === 'quote') return `<p class="sb-evquote">“${esc(e.evidence.quote)}”</p>`;
if (kind === 'rationale') return `<p class="sb-evwhy">WHY — ${esc(e.evidence.quote)}</p>`;
return `<p class="sb-evquote sb-evnone">NO EVIDENCE RECORDED — ASSERTED, NOT DETECTED</p>`;
};
/** The evidence that taught this link, plus its full paper trail.
* This block is the ONLY place raw ids surface — labelled. `pinned`
* renders it open in-flow (the pin set lives in module state on the
* controller side, so pins survive every re-render). */
const EvidenceBlock = (e, pinned, voc = vocabFor(false)) => {
const ev = e.evidence || {};
const src = ev.source
? `<span>from ${esc(ev.source)}${ev.offset != null && ev.offset > 0 ? ` · byte ${esc(ev.offset)}` : ''}</span>`
: '';
const conf = e.confidence ?? 1;
const invalid = e.invalid_at != null
? ` · ${voc.until} ${esc(fmtTs(e.invalid_at))}` : '';
const expired = e.expired_at != null
? ` · retirement recorded ${esc(fmtTs(e.expired_at))}` : '';
return `
<div class="sb-evbody" data-sb-evbody${pinned ? '' : ' hidden'}>
${evidenceLine(e)}
<div class="sb-evmeta mono faint">
${src}
<span>taught by ${esc(detectorName(e.detector).toLowerCase())} (${esc(e.detector || 'unknown detector')}) · ${esc(fmt(conf * 100, { decimals: 0 }))}% confident · strength ${esc(fmt(e.weight ?? 1, { decimals: 2 }))}</span>
<span>${voc.since} ${esc(fmtTs(e.valid_at))}${invalid}${expired}</span>
<span>link id ${esc(e.edge_id)} · this is the handle the API takes</span>
</div>
</div>`;
};
/**
* Floating hover card: the evidence line + one taught-by sentence. No
* raw ids here — the full paper trail is one click (the pin) away.
* Rendered into a fixed-position card by the interaction controller.
*/
export const EvidencePreview = (e) => {
const conf = e.confidence ?? 1;
return `
${evidenceLine(e)}
<div class="sb-evmeta mono faint">
<span>taught by ${esc(detectorName(e.detector).toLowerCase())} · ${esc(fmt(conf * 100, { decimals: 0 }))}% confident · click to pin the paper trail</span>
</div>`;
};
// ----- rows + groups ------------------------------------------------
/** The row chip naming what stands behind the link: QUOTE (authored
* text), WHY (structural rationale), or NO EVIDENCE. */
const evidenceChip = (e) => {
const kind = evidenceKind(e);
if (kind === 'quote') return '<span class="sb-eq">QUOTE</span>';
if (kind === 'rationale') return '<span class="sb-eq sb-eq-why">WHY</span>';
return '<span class="sb-eq sb-eq-none">NO EVIDENCE</span>';
};
const edgeRow = (e, side, { counts, asOf, clipped, nodes, names, open, voc = vocabFor(false) }) => {
const id = farEnd(e);
const state = edgeStateAt(e, asOf);
const n = counts.get(id) || 0;
const pinned = !!(open && open.has(e.edge_id));
const hop2 = n > 0
? `+${clipped ? '≥' : ''}${num(n)} MORE`
: '';
return `
<div class="sb-erow${state === 'expired' ? ' sb-expired' : ''}${state === 'future' ? ' sb-off' : ''}"
data-sb-edge="${esc(e.edge_id)}" data-va="${esc(e.valid_at)}"${e.invalid_at != null ? ` data-vi="${esc(e.invalid_at)}"` : ''}>
<div class="sb-erow-main">
<button type="button" class="sb-ename" data-sb-focus="${esc(id)}"
title="Open the ledger for ${esc(id)}">${esc(nodeName(id, { nodes, names }))}</button>
<button type="button" class="sb-emeta" data-sb-evidence="${esc(e.edge_id)}"
aria-expanded="${pinned}"
title="Show what taught this link — click to pin the full paper trail">
<span class="sb-ewin">${esc(fmtWindow(e.valid_at, e.invalid_at))}</span>
${evidenceChip(e)}
${hop2 ? `<span class="sb-ehop2" title="${esc(num(n))} more link${n === 1 ? '' : 's'} one step beyond — click the name to walk there">${esc(hop2)}</span>` : ''}
<span class="sb-tag sb-tag-retired">RETIRED</span>
<span class="sb-tag sb-tag-notyet">NOT YET</span>
</button>
</div>
${EvidenceBlock(e, pinned, voc)}
</div>`;
};
const groupBlock = (g, side, ctx) => {
const total = g.list.length;
const shown = Math.min(total, LEDGER_GROUP_CAP);
const countLabel = shown < total
? `TOP ${num(shown)} OF ${num(total)}`
: `${num(total)}`;
// Collapse is a client-side fold — header + count stay visible, the
// honesty counters never change, nothing is refetched.
const key = `${side}:${g.type}`;
const collapsed = !!(ctx.collapsed && ctx.collapsed.has(key));
return `
<div class="sb-group">
<button type="button" class="sb-grouphead" data-sb-grouphead="${side}"
data-sb-collapse="${esc(key)}" aria-expanded="${!collapsed}"
title="${collapsed ? 'Show' : 'Fold away'} these links (nothing is refetched)">
<span class="sb-ghtype"><span class="sb-ghcaret" data-sb-ghcaret>${collapsed ? '▸' : '▾'}</span>${esc(typeName(g.type))}</span>
<span class="sb-ghcount">${esc(countLabel)}</span>
</button>
<div class="sb-grouprows" data-sb-grouprows${collapsed ? ' hidden' : ''}>
${g.list.slice(0, LEDGER_GROUP_CAP).map((e) => edgeRow(e, side, ctx)).join('')}
</div>
</div>`;
};
// ----- focus card ---------------------------------------------------
const FocusCard = ({ ego, nodes, names, brain, asOf }) => {
const id = ego.node;
const hop1 = (ego.edges || []).filter((e) => e.hop === 1);
// Degrees speak the page's tense — BELIEVED AT THIS MOMENT — so they
// count only links live at the caret; retired ones are counted apart
// (they are drawn struck-through in the columns either way).
const live = hop1.filter((e) => edgeStateAt(e, asOf) === 'live');
const nRetired = hop1.filter((e) => edgeStateAt(e, asOf) === 'expired').length;
const nIn = live.filter((e) => e.direction === 'in').length;
const nOut = live.length - nIn;
const kinds = new Set(live.map((e) => e.type)).size;
const earliest = hop1.length
? Math.min(...hop1.map((e) => Number(e.valid_at)))
: null;
const meta = nodes && nodes[id];
const preview = meta && meta.preview
? `<p class="sb-ftext">${esc(meta.preview)}</p>`
: '';
// The real file behind the note, when the engine (or the console's
// name cache) knows it — a truthful label, never derived from the id.
const path = (meta && typeof meta.path === 'string' && meta.path)
|| (names && names[id] && names[id].path) || null;
const dangling = !meta && ego.not_shown
&& Array.isArray(ego.not_shown.dangling_ids)
&& ego.not_shown.dangling_ids.includes(id);
return `
<div class="sb-focuscard" data-sb-focuscard>
<div class="key">FOCUS</div>
<div class="sb-ftitle">${esc(nodeName(id, { nodes, names }))}</div>
${preview}
<div class="sb-fdeg mono">
<span>← <span class="sb-fnum">${num(nIn)}</span> IN</span>
<span><span class="sb-fnum">${num(nOut)}</span> OUT →</span>
<span><span class="sb-fnum">${num(kinds)}</span> KIND${kinds === 1 ? '' : 'S'}</span>
${nRetired > 0 ? `<span class="sb-fret">+${num(nRetired)} RETIRED</span>` : ''}
</div>
<div class="sb-fprov mono faint">
${esc(meta && meta.index ? meta.index : `brain ${brain}`)}${path ? ` · ${esc(path)}` : ''} · id ${esc(id)}${dangling ? ' · NO DOCUMENT BEHIND THIS ID' : ''}${earliest != null ? ` · in this brain since ${esc(fmtDay(earliest))}` : ''}
</div>
</div>`;
};
// ----- controls line ------------------------------------------------
const EgoControls = ({ brain, brains, focus, asOf, trail, names, nodes }) => {
const others = (brains || []).filter((b) => b !== brain);
const brainSwitch = others.length
? `<span class="sb-cswitch">${others.map((b) =>
`<button type="button" class="sb-clink" data-sb-brain="${esc(b)}" title="Switch to brain ${esc(b)}">${esc(b)}</button>`).join('<span class="faint"> · </span>')}</span>`
: '';
const crumbs = (trail || []).filter((t) => t !== focus).slice(-3);
const crumbName = (t) => {
const n = nodeName(t, { nodes, names });
return n.length > 18 ? `${n.slice(0, 17)}…` : n;
};
const trailHtml = crumbs.length
? `<span class="sb-ctrail">${crumbs.map((t) =>
`<button type="button" class="sb-clink" data-sb-focus="${esc(t)}" title="Back to ${esc(t)}">${esc(crumbName(t))}</button>`).join('<span class="faint"> → </span>')}<span class="faint"> → </span></span>`
: '';
return `
<div class="sb-controls mono">
<span><span class="key">BRAIN</span> <span class="sb-cval">${esc(brain || '—')}</span> ${brainSwitch}</span>
<span><span class="key">FOCUS</span> ${trailHtml}<span class="sb-cval">${esc(focus ? crumbName(focus) : '—')}</span></span>
<span><span class="key">VIEWING</span> <span class="sb-cval${asOf != null ? ' accent' : ''}">${asOf != null ? esc(fmtTs(asOf)) + ' UTC' : 'NOW'}</span>${asOf != null ? ` <button type="button" class="sb-clink" data-sb-now>BACK TO NOW</button>` : ''}</span>
<span class="sb-cfind"><span class="key">FIND</span><input class="sb-findinput" type="text"
data-sb-find spellcheck="false" autocomplete="off"
placeholder="a note, by the words in it"
aria-label="Find a note in this brain — Enter opens the first match"></span>
<span class="sb-cerr" data-sb-error></span>
</div>
<div class="sb-findresults" data-sb-findresults hidden></div>`;
};
/**
* FIND results: a temporary strip of note links under the controls.
* Every hit is the same refocus affordance as any other note name; an
* empty answer teaches what the brain is (and is not).
*/
export const FindResults = ({ hits, names }) => {
if (!hits || !hits.length) {
return `<span class="sb-sidenote mono faint">NO NOTE MATCHES — THE BRAIN ONLY KNOWS WHAT THE FOLDER TAUGHT IT</span>`;
}
return hits.map((h, i) =>
`<button type="button" class="sb-clink sb-findrow${i === 0 ? ' sb-findfirst' : ''}"
data-sb-focus="${esc(h.id)}" title="Open the ledger for ${esc(h.id)}">${esc(h.title || nodeName(h.id, { names }))}</button>`)
.join('<span class="faint"> · </span>');
};
// ----- belief-time scrubber ----------------------------------------
/** Caret-label edge alignment: centred by default, left-anchored near
* the left edge, right-anchored near the right edge — so the label
* never clips off the strip at any width. Shared with the drag
* preview in data/second-brain-api.js. */
export const caretAlignClass = (pct) =>
pct < 12 ? 'sb-cl-left' : pct > 88 ? 'sb-cl-right' : '';
/**
* Full-width belief-time strip with a draggable as-of caret. Two forms
* from one time axis, picked by how many links there are:
*
* ≤ SCRUB_LIFETIME_MAX one row per link, drawn from the moment the
* brain started believing it to the moment it
* stopped (or to now); retired rows go dashed.
* beyond a 1px step curve counting how many links were
* believed at every moment (individual 1px rows
* beyond ~24 are an unreadable hatch).
*
* Dragging previews client-side; releasing re-queries the server with
* `as_of` (the caret is a query parameter, not a client filter). The
* interval set is pinned at NOW with retired links included, so
* scrubbing never removes data from the strip — only states change.
*/
export const BeliefScrubber = ({ timeline, asOf, shift, voc = vocabFor(false) }) => {
const edges = (timeline && timeline.edges) || [];
if (!edges.length) {
return `<div class="sb-scrub" data-sb-scrub><div class="panel-empty mono faint">NOTHING TO REPLAY YET · EVERY LINK WILL DRAW ITS LIFETIME HERE</div></div>`;
}
// The fetch that feeds the strip is capped (ego limit); when the
// server clipped, every count here is a floor — say so with '≥'.
const clipped = !!(timeline && timeline.not_shown && timeline.not_shown.edges_clipped > 0);
const floor = clipped ? '≥' : '';
const now = Date.now();
const d0raw = Math.min(...edges.map((e) => Number(e.valid_at)));
const span = Math.max(now - d0raw, 60_000);
const d0 = d0raw - span * 0.04; // 4% air before the first assertion
const d1 = now;
const pctOf = (ms) => Math.max(0, Math.min(100, ((ms - d0) / (d1 - d0)) * 100));
const caretMs = asOf == null ? d1 : asOf;
const caretPct = pctOf(caretMs);
const labelPct = Math.max(1, Math.min(99, caretPct));
const liveAt = (t) => edges.reduce((a, e) => a + (edgeStateAt(e, t) === 'live' ? 1 : 0), 0);
const liveAtCaret = liveAt(asOf);
// Every belief event on the strip (assertions AND retirements),
// sorted, deduped: the caret snaps to these on release and the arrow
// keys step between them — moments where nothing changed are not
// interesting stops.
const eventTimes = [...new Set(
edges.flatMap((e) => (e.invalid_at != null
? [Number(e.valid_at), Number(e.invalid_at)]
: [Number(e.valid_at)])),
)].sort((a, b) => a - b);
const eventsAttr = ` data-sb-events="${esc(eventTimes.join(','))}"`;
const lifetimes = edges.length <= SCRUB_LIFETIME_MAX;
let stripInner = '';
let h = 0;
let stepsAttr = '';
let explain = '';
if (lifetimes) {
// Deterministic row order: strength desc, believed-since asc, id asc.
const rows = [...edges].sort((a, b) =>
(b.weight - a.weight)
|| (Number(a.valid_at) - Number(b.valid_at))
|| (a.edge_id < b.edge_id ? -1 : 1)
);
const rowH = rows.length <= 6 ? 10 : rows.length <= 12 ? 8 : 6;
h = Math.max(44, rows.length * rowH + 8);
const lines = rows.map((e, i) => {
const y = 6 + i * rowH;
const x1 = pctOf(Number(e.valid_at));
let x2 = e.invalid_at != null ? pctOf(Number(e.invalid_at)) : 100;
// A belief held briefly must not vanish into a zero-width line.
if (x2 - x1 < 0.3) x2 = Math.min(100, x1 + 0.3);
const state = edgeStateAt(e, asOf);
const tick = e.invalid_at != null
? `<line class="sb-ltick" x1="${x2}%" x2="${x2}%" y1="${y - 3}" y2="${y + 3}" stroke="currentColor" stroke-width="1"/>`
: '';
return `<line class="sb-lseg sb-l-${state}" data-va="${esc(e.valid_at)}"${e.invalid_at != null ? ` data-vi="${esc(e.invalid_at)}"` : ''} x1="${x1}%" x2="${x2}%" y1="${y}" y2="${y}" stroke="currentColor" stroke-width="1"/>${tick}`;
}).join('');
stripInner = `<svg class="chart sb-striplines" width="100%" height="${h}" aria-hidden="true">${lines}</svg>`;
explain = `EACH LINE IS ONE LINK'S LIFETIME · RETIRED LINKS KEEP THEIR ROW, DASHED`;
} else {
// Belief-count step curve. Events: +1 when a link starts being
// believed, −1 when it retires; the curve is the running count.
const ev = [];
for (const e of edges) {
ev.push([Number(e.valid_at), 1]);
if (e.invalid_at != null) ev.push([Number(e.invalid_at), -1]);
}
ev.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
const steps = []; // [t, countFromT]
let c = 0;
for (const [t, d] of ev) {
c += d;
if (steps.length && steps[steps.length - 1][0] === t) steps[steps.length - 1][1] = c;
e
10BC8
lse steps.push([t, c]);
}
const maxC = Math.max(...steps.map((s) => s[1]), 1);
h = 88;
const X = 1000; // viewBox units; preserveAspectRatio=none stretches
const xOf = (ms) => (pctOf(ms) / 100) * X;
const yOf = (n) => 4 + (1 - n / maxC) * (h - 10);
let pts = `0,${yOf(0).toFixed(1)}`;
let prevY = yOf(0);
for (const [t, n] of steps) {
const x = xOf(t).toFixed(1);
const y = yOf(n);
pts += ` ${x},${prevY.toFixed(1)} ${x},${y.toFixed(1)}`;
prevY = y;
}
pts += ` ${X},${prevY.toFixed(1)}`;
stepsAttr = ` data-sb-steps="${esc(steps.map(([t, n]) => `${t}:${n}`).join(','))}"`;
// Assertion ticks: one 1px mute tick per moment a link started
// being believed (deduped at tick resolution) — the snap targets,
// made visible on the curve form where individual rows are gone.
const seenX = new Set();
let ticks = '';
for (const e of edges) {
const x = xOf(Number(e.valid_at));
const xq = Math.round(x);
if (seenX.has(xq)) continue;
seenX.add(xq);
ticks += `<line class="sb-atick" x1="${x.toFixed(1)}" x2="${x.toFixed(1)}" y1="${h - 6}" y2="${h - 1}" stroke="currentColor" stroke-width="1"/>`;
}
stripInner = `<svg class="chart sb-striplines" width="100%" height="${h}" viewBox="0 0 ${X} ${h}" preserveAspectRatio="none" aria-hidden="true">
${ticks}
<polyline points="${pts}" fill="none" stroke="currentColor" stroke-width="1"/>
</svg>
<span class="sb-curvemax mono faint">peak ${esc(floor)}${num(maxC)}</span>`;
explain = `THE LINE COUNTS LIVE LINKS OVER TIME — TOO MANY LINKS (${floor}${num(edges.length)}) TO DRAW ONE PER ROW`;
}
// What the last time-travel changed, stated in the legend: counts of
// appearances and retirements between the two carets — always named
// separately, never netted, '≥' floors when the fetch was clipped.
// "THIS MOMENT" must name the caret's end of the crossed interval:
// dragging right the caret sits at t1, dragging left it sits at t0 —
// the other endpoint is named by its date, never misattributed.
const shiftWhen = shift
? (Math.abs(caretMs - shift.t1) <= Math.abs(caretMs - shift.t0)
? `BETWEEN ${esc(fmtDay(shift.t0))} AND THIS MOMENT`
: `BETWEEN THIS MOMENT AND ${esc(fmtDay(shift.t1))}`)
: '';
const shiftHtml = shift
? `<span class="sb-changeout" data-sb-changeout>↳ ${shift.appeared === 0 && shift.retired === 0
? 'NOTHING CHANGED'
: `${esc(floor)}${num(shift.appeared)} APPEARED · ${esc(floor)}${num(shift.retired)} RETIRED`} ${shiftWhen}</span>`
: '';
return `
<div class="sb-scrub" data-sb-scrub data-d0="${d0}" data-d1="${d1}"${stepsAttr}${eventsAttr}>
<div class="key">${esc(voc.timeAxis)} · DRAG ANYWHERE ON THE STRIP — ${esc(voc.replayLine)}</div>
<div class="sb-strip" data-sb-strip style="height:${h}px;">
${stripInner}
<div class="sb-caret" data-sb-caret style="left:${caretPct}%;" role="slider" tabindex="0"
aria-label="Belief time — what did this brain believe, when"
aria-valuemin="${d0}" aria-valuemax="${d1}" aria-valuenow="${caretMs}"
aria-valuetext="${asOf == null ? 'now' : esc(fmtTs(caretMs)) + ' UTC'}"></div>
<div class="sb-caret-label mono ${caretAlignClass(labelPct)}" data-sb-caret-label style="left:${labelPct}%;">${asOf == null ? 'NOW' : esc(fmtTs(caretMs))}</div>
</div>
<div class="sb-scrub-legend mono faint">
<span>${esc(fmtDay(d0raw))}</span>
<span class="mid"><span data-sb-livecount>${esc(floor)}${num(liveAtCaret)} OF ${esc(floor)}${num(edges.length)}</span> ${esc(voc.atMoment)}<span class="sb-scrub-explain"> · ${explain}</span>${shiftHtml}</span>
<span>${asOf != null ? `<button type="button" class="text-btn" data-sb-now>BACK TO NOW</button>` : `TODAY · ${esc(fmtDay(now))}`}</span>
</div>
</div>`;
};
// ----- the ledger ---------------------------------------------------
const sideCol = (groups, side, ctx, caption, emptyLabel) => {
const inner = groups.length
? groups.map((g) => groupBlock(g, side, ctx)).join('')
: `<div class="sb-sidenote mono faint">${esc(emptyLabel)}</div>`;
return `<div class="sb-col sb-col-${side}">
<div class="sb-sidecap key">${esc(caption)}</div>
${inner}
</div>`;
};
/**
* The three-column ego ledger + rails. The brain/focus/FIND controls
* and the belief-time scrubber used to render inside this panel; they
* are now panels of their own (`controls` at the top of the page, and
* `scrub` between the map and the statistics row) so ONE caret drives
* both the map and the ledger — same module state, same commit path.
*/
export const EgoLedger = ({ data }) => {
const sb = data.sb || {};
const ego = data.ego;
const asOf = sb.asOf ?? null;
const names = sb.names || {};
const voc = vocOf(data);
if (!ego) {
return `
<div class="panel-empty mono faint">${sb.focus ? voc.readingFocus + esc(String(sb.focus).toUpperCase()) + '…' : `NO ${voc.Item} IN FOCUS · CLICK ANY ${voc.item} ON THIS PAGE TO OPEN ITS LEDGER`}</div>`;
}
const nodes = ego.nodes || {};
const clipped = !!(ego.not_shown && ego.not_shown.edges_clipped > 0);
const counts = hop2Counts(ego.edges, asOf);
const ctx = {
nodes, names, counts, asOf, clipped,
open: sb.openEvidence, // pinned paper trails — survive re-renders
collapsed: sb.collapsed, // folded link-kind groups
voc, // corpus-aware wording (#1100)
};
const inGroups = groupEdges(ego.edges, 'in');
const outGroups = groupEdges(ego.edges, 'out');
const hop1 = (ego.edges || []).filter((e) => e.hop === 1);
const body = hop1.length
? `
<div class="sb-ledger" data-sb-ledger>
${sideCol(inGroups, 'in', ctx, '← WHAT LINKS IN', 'NOTHING LINKS IN AT THIS MOMENT')}
<div class="sb-rail" data-sb-rail="in" aria-hidden="true"></div>
${FocusCard({ ego, nodes, names, brain: data.brain, asOf })}
<div class="sb-rail" data-sb-rail="out" aria-hidden="true"></div>
${sideCol(outGroups, 'out', ctx, 'WHERE IT LINKS OUT →', 'NO OUTBOUND LINKS AT THIS MOMENT')}
</div>`
: `
<div class="sb-ledger" data-sb-ledger>
<div class="sb-col sb-col-in"></div>
<div class="sb-rail" data-sb-rail="in" aria-hidden="true"></div>
${FocusCard({ ego, nodes, names, brain: data.brain, asOf })}
<div class="sb-rail" data-sb-rail="out" aria-hidden="true"></div>
<div class="sb-col sb-col-out"></div>
</div>
<div class="panel-empty mono faint">NO LINKS TOUCH THIS NOTE AT THIS MOMENT · DRAG BELIEF TIME BACK TO NOW, OR CLICK ANOTHER NOTE BELOW</div>`;
return body;
};
// ----- honesty panel ------------------------------------------------
const hrow = (k, v, emph, rawName) => `
<div class="sb-hrow"${rawName ? ` title="API field: ${esc(rawName)}"` : ''}>
<span class="sb-hk">${esc(k)}</span>
<span class="sb-hv${emph && v !== '0' ? ' accent' : ''}">${esc(v)}</span>
</div>`;
/**
* Definition list over every `not_shown` counter of both endpoints
* plus the view's own caps — in user words (the raw API field name
* rides on hover). ALWAYS renders, zeros included — this panel exists
* to prove absence, not just to report presence.
*/
export const HonestyList = ({ data }) => {
const ns = (data.ego && data.ego.not_shown) || {};
const on = (data.overview && data.overview.not_shown) || {};
const voc = vocOf(data);
const z = (v) => num(v || 0);
const dangling = Array.isArray(ns.dangling_ids) && ns.dangling_ids.length
? `<div class="sb-hids mono faint">no document behind: ${ns.dangling_ids.slice(0, 8).map(esc).join(', ')}${ns.dangling_ids.length > 8 ? ` +${ns.dangling_ids.length - 8} more` : ''}</div>`
: '';
// The belief strip draws EVERY link it fetched (rows or count-curve —
// the form switches, the truth does not); what it can hide is what
// the fetch itself clipped server-side.
const tlns = (data.sb && data.sb.timeline && data.sb.timeline.not_shown) || {};
return `
<div class="sb-honesty mono">
<div class="sb-hgroup key">THE LEDGER READ</div>
${hrow('links beyond the fetch cap (not drawn)', z(ns.edges_clipped), true, 'edges_clipped')}
${hrow(`starting ${voc.items} the walk had to drop`, z(ns.frontier_clipped), true, 'frontier_clipped')}
${hrow('links hidden by the belief-time cut', z(ns.expired_excluded), false, 'expired_excluded')}
${hrow('links hidden by a kind filter', z(ns.type_filtered), false, 'type_filtered')}
${hrow('storage segments this read had to skip', z(ns.segments_without_columns), true, 'segments_without_columns')}
${hrow('unflushed writes checked (cost, not loss)', z(ns.memtable_docs_scanned), false, 'memtable_docs_scanned')}
${hrow(`links pointing at ${voc.items} with no document`, z(ns.dangling_nodes), true, 'dangling_nodes')}
${dangling}
<div class="sb-hgroup key">THE COUNTS ABOVE</div>
${hrow('link kinds beyond the top list', z(on.types_not_listed), true, 'types_not_listed')}
${hrow('detectors beyond the top list', z(on.detectors_not_listed), true, 'detectors_not_listed')}
${hrow(`most-citing ${voc.items} beyond the top list`, z(on.hubs_out_not_listed), false, 'hubs_out_not_listed')}
${hrow(`most-cited ${voc.items} beyond the top list`, z(on.hubs_in_not_listed), false, 'hubs_in_not_listed')}
<div class="sb-hgroup key">CAPS THIS PAGE APPLIES</div>
${hrow('ledger rows per link kind (capped groups say so)', `≤ ${num(LEDGER_GROUP_CAP)}`, false)}
${hrow('belief-strip lifetimes beyond its fetch cap', z(tlns.edges_clipped), true, 'edges_clipped')}
</div>`;
};
// ----- hubs (centers of gravity) ------------------------------------
const hubList = (items, notListed, focus, ctx) => {
if (!items || !items.length) return `<div class="sb-sidenote mono faint">NOTHING RANKS YET</div>`;
const max = Math.max(...items.map((i) => i.live_edges), 1);
const rows = items.map((i) => {
const frac = Math.max(0, Math.min(1, i.live_edges / max));
return `
<button type="button" class="sb-hubrow${i.id === focus ? ' sb-hub-active' : ''}" data-sb-focus="${esc(i.id)}"
title="Open the ledger for ${esc(i.id)}">
<span class="sb-hubname">${esc(nodeName(i.id, ctx))}</span>
<span class="sb-hubn">${esc(num(i.live_edges))}</span>
<svg class="chart sb-hubbar" height="6" viewBox="0 0 100 6" preserveAspectRatio="none" aria-hidden="true">
<line x1="0" y1="5" x2="${(frac * 100).toFixed(1)}" y2="5" stroke="currentColor" stroke-width="1"/>
</svg>
</button>`;
}).join('');
const tail = notListed > 0
? `<div class="hint">+ ${num(notListed)} more beyond this list</div>` : '';
return rows + tail;
};
export const HubsPanel = ({ data }) => {
const o = data.overview || {};
const voc = vocOf(data);
const hubs = o.hubs || { out: [], in: [] };
const ns = o.not_shown || {};
const sb = data.sb || {};
const focus = sb.focus || null;
const ctx = { nodes: (data.ego && data.ego.nodes) || {}, names: sb.names || {} };
return `
<div class="sb-hubs">
<div>
<div class="key">MOST CITED · LINKS POINTING AT A ${voc.Item}</div>
${hubList(hubs.in, ns.hubs_in_not_listed, focus, ctx)}
</div>
<div>
<div class="key">CITES THE MOST · LINKS A ${voc.Item} SENDS OUT</div>
${hubList(hubs.out, ns.hubs_out_not_listed, focus, ctx)}
</div>
</div>
<div class="hint" style="margin-top:var(--sp-1);">click any ${voc.item} to open its ledger above</div>`;
};
// ----- small panels -------------------------------------------------
/** 2-series comparison legend chip: a short 3px line in the series
* color + the number IN INK — color is identity, text is the value. */
const legChip = (cls, label) =>
`<span class="sb-leg"><svg class="sb-legline ${cls}" width="14" height="4" viewBox="0 0 14 4" aria-hidden="true"><line x1="0" y1="2" x2="14" y2="2" stroke="currentColor" stroke-width="3"/></svg>${label}</span>`;
/**
* BELIEVED NOW — headline number + the believed-vs-retired composition
* bar. The ONE two-series comparison on this page: accent (believed)
* against --z-cmp (retired), 2px surface gap, both directly labelled.
*/
const BelievedPanel = ({ data }) => {
const o = data.overview || {};
const voc = vocOf(data);
const ed = o.edges || { total: 0, live: 0, invalidated: 0 };
const m = (data.metrics && data.metrics.edgesLive) || { formatted: num(ed.live), hint: '' };
if (!ed.total) {
return Num({ value: '0', unit: 'links', hint: 'nothing asserted yet — see below', emphasis: true });
}
const W = 200;
const both = ed.live > 0 && ed.invalidated > 0;
let liveW = Math.round((ed.live / ed.total) * W);
// When both segments exist, neither may vanish (a 0.5% retired share
// must still be visibly present) and neither may go negative-width.
if (both) liveW = Math.max(4, Math.min(W - 4, liveW));
const gap = both ? 1 : 0;
const bar = `
<svg class="chart sb-split" width="100%" height="8" viewBox="0 0 ${W} 8" preserveAspectRatio="none" aria-hidden="true">
${ed.live > 0 ? `<line class="sb-seg-live" x1="0" y1="4" x2="${liveW - gap}" y2="4" stroke="currentColor" stroke-width="3"/>` : ''}
${ed.invalidated > 0 ? `<line class="sb-seg-retired" x1="${liveW + gap}" y1="4" x2="${W}" y2="4" stroke="currentColor" stroke-width="3"/>` : ''}
</svg>`;
return `
${Num({ value: m.formatted, unit: 'links', hint: m.hint, emphasis: true })}
<div class="sb-splitwrap">
${bar}
<div class="sb-splitleg mono">
${legChip('sb-seg-live', `${num(ed.live)} ${voc.live}`)}
${legChip('sb-seg-retired', `${num(ed.invalidated)} RETIRED`)}
</div>
</div>`;
};
/** EVER ASSERTED — total + the growth shape (cumulative spark). */
const AssertedPanel = ({ data }) => {
const o = data.overview || {};
const ed = o.edges || { total: 0 };
const m = (data.metrics && data.metrics.edgesTotal) || { formatted: num(ed.total || 0), hint: '' };
const buckets = o.created_over_time || [];
let run = 0;
const cumulative = buckets.map((b) => { run += (b.count || 0); return run; });
const spark = cumulative.length >= 2 ? Spark(cumulative, { w: 150, h: 30 }) : '';
return Num({ value: m.formatted, unit: 'links', spark, hint: m.hint, emphasis: false });
};
/** RETIRED — count + share of everything ever asserted + the three
* most recently retired links, each with the evidence it carried.
* The churn rows are truth-now (most recent retirements on record),
* regardless of where the belief caret sits. */
const RetiredPanel = ({ data }) => {
const o = data.overview || {};
const sb = data.sb || {};
const ed = o.edges || { total: 0, invalidated: 0 };
const m = (data.metrics && data.metrics.invalidated) || { formatted: num(ed.invalidated || 0), hint: '' };
const share = ed.total > 0 ? `${((100 * ed.invalidated) / ed.total).toFixed(ed.invalidated ? 1 : 0)}% of all links` : '';
const names = sb.names || {};
const recent = Array.isArray(sb.recentRetired) ? sb.recentRetired : [];
const evSnippet = (e) => {
const kind = evidenceKind(e);
if (kind === 'quote') return `“${e.evidence.quote}”`;
if (kind === 'rationale') return `WHY — ${e.evidence.quote}`;
return 'no evidence recorded';
};
const rows = recent.map((e) => `
<button type="button" class="sb-retrow" data-sb-focus="${esc(e.src)}"
title="Open the ledger for ${esc(e.src)}">
<span class="sb-retpair">${esc(nodeName(e.src, { names }))} → ${esc(nodeName(e.dst, { names }))}</span>
<span class="sb-retwhen">retired ${esc(fmtDay(e.invalid_at))}</span>
<span class="sb-retev">${esc(evSnippet(e))}</span>
</button>`).join('');
return `
${Num({ value: m.formatted, unit: 'links', hint: m.hint, emphasis: false })}
${share ? `<div class="hint" style="margin-top:6px;">${esc(share)} · never deleted, only retired</div>` : ''}
${rows ? `<div class="sb-retrows">${rows}</div>` : ''}`;
};
/** WHAT TAUGHT THIS BRAIN — the detectors as identity + magnitude
* rows (name · count · 1px bar), not a bare count. */
const TaughtByPanel = ({ data }) => {
const o = data.overview || {};
const dets = o.detectors || [];
if (!dets.length) return `<div class="panel-empty mono faint">NO DETECTOR HAS RUN YET</div>`;
const shown = dets.slice(0, 4);
const max = Math.max(...shown.map((d) => d.live), 1);
const rows = shown.map((d) => `
<div class="sb-det">
<span class="sb-detname" title="deterministic detector ${esc(d.detector)} — same folder in, same links out">${esc(detectorName(d.detector))}</span>
<span class="sb-detn">${esc(num(d.live))}</span>
<svg class="chart sb-hubbar" height="6" viewBox="0 0 100 6" preserveAspectRatio="none" aria-hidden="true">
<line x1="0" y1="5" x2="${((d.live / max) * 100).toFixed(1)}" y2="5" stroke="currentColor" stroke-width="1"/>
</svg>
</div>`).join('');
const notListed = (o.not_shown && o.not_shown.detectors_not_listed) || 0;
const tail = (dets.length > 4 || notListed > 0)
? `<div class="hint">+ ${num(Math.max(0, dets.length - 4) + notListed)} more</div>` : '';
// Authored vs structural, always stated: a corpus with no written
// links must read "0 authored · N structural", never a proud N.
const split = authoredSplit(dets);
return `<div class="sb-dets">${rows}${tail}</div>
<div class="hint" style="margin-top:6px;">${esc(splitLabel(split))}</div>
<div class="hint">deterministic · versioned · no LLM</div>`;
};
const TypeDistPanel = ({ data }) => {
const o = data.overview || {};
const types = o.types || [];
if (!types.length) return `<div class="panel-empty mono faint">NO LINKS AT THIS MOMENT</div>`;
const tail = o.not_shown && o.not_shown.types_not_listed > 0
? `<div class="hint">+ ${num(o.not_shown.types_not_listed)} kinds beyond this list</div>` : '';
// The same authored/structural honesty as the detectors tile: how
// much of this distribution is written citation vs folder position.
const split = authoredSplit(o.detectors);
const splitNote = `<div class="hint" style="margin-top:6px;">${esc(splitLabel(split))} — authored links are written citations; structural ones are folder position and sort order</div>`;
return Dist({ segments: types.map((t) => ({ label: typeName(t.type), value: t.live })) }) + tail + splitNote;
};
const TimelinePanel = ({ data }) => {
const o = data.overview || {};
const series = (data.series && data.series.created) || [];
const ts = (data.series && data.series.createdT) || [];
// §0 invariant 8: the default embedder is lexical feature-hashing; any
// surface that could imply neural semantics must say otherwise.
const embedderNote = o.embedder === 'lexical-feature-hash'
? `<div class="hint" style="margin-top:var(--sp-1);">recall is lexical (feature hashing) — not neural</div>`
: (o.embedder ? `<div class="hint" style="margin-top:var(--sp-1);">embedder · ${esc(o.embedder)}</div>` : '');
if (series.length < 2) {
const === 1
? `<div class="mono">${esc(num(series[0]))} link${series[0] === 1 ? '' : 's'} · all asserted ${esc(fmtDay(ts[0]))}</div>` : '';
return `${one || '<div class="panel-empty mono faint">EACH DAY’S NEW LINKS WILL DRAW HERE</div>'}${embedderNote}`;
}
return Series(series, {
h: 120,
labels: [fmtDay(ts[0]), fmtDay(ts[ts.length - 1])],
unit: 'links',
}) + embedderNote;
};
// ----- empty / error / disconnected states -------------------------
/**
* The dashboard reads the LIVE graph endpoints only — no demo numbers.
* Anything else on this page would be a fake brain.
*/
const LiveOnlyNote = () => `
<div class="sb-empty">
<div class="mono">THIS PAGE READS THE LIVE ENGINE · IT NEVER INVENTS A BRAIN</div>
<div class="mono faint" style="margin-top:var(--sp-1);">start xerj, point the console at it (SETTINGS → BACKEND · XERJ), reload</div>
</div>`;
/** Overview failed on a real brain: say so — never render zeros as truth. */
const BrainErrorNote = ({ brain, reason }) => `
<div class="sb-empty">
<div class="sb-ftitle">COULD NOT READ THIS BRAIN</div>
<p class="sb-ftext">The engine answered, but the graph overview for
<span class="mono">${esc(brain || '—')}</span> failed. Nothing on this page is a guess —
so nothing is shown.</p>
<div class="mono faint" style="margin:var(--sp-1) 0;">${esc(String(reason || 'unknown error').slice(0, 140))}</div>
<button type="button" class="text-btn" data-sb-retry>RETRY</button>
</div>`;
/** A 401/403 from the console graph read path is a REFUSAL, not an
* outage: no session, or a role that may not read brains (issue #936).
* Say that, and point at the views that do work. */
export const isRefusal = (error) => /HTTP 40[13]\b/.test(String(error || ''));
/** A brand-new brain: the ONE command that fills it, copyable. */
export const EmptyBrainNote = ({ brain, connected, error }) => {
if (!connected && isRefusal(error)) {
return `
<div class="sb-empty">
<div class="sb-ftitle">THE GRAPH API REFUSED THIS CONSOLE SESSION</div>
<div class="mono faint" style="margin:var(--sp-1) 0;">${esc(String(error).slice(0, 120))}</div>
<p class="sb-ftext">The brain views read the graph through the console session, and this
session was refused — there is none, or its role may not read brains (owner and admin
may; issue #936). Nothing here is a guess, so nothing is shown. What was indexed is on
CORPUS, and any record opens in the READER — those go through the session and work.</p>
<a class="text-btn" href="#/corpus">OPEN CORPUS</a>
</div>`;
}
if (!connected) {
return `
<div class="sb-empty">
<div class="sb-ftitle">ENGINE UNREACHABLE</div>
${error ? `<div class="mono faint" style="margin:var(--sp-1) 0;">${esc(String(error).slice(0, 120))}</div>` : ''}
<p class="sb-ftext">This page reads the live graph API only — it never shows demo data.
Start xerj, then reload:</p>
<div class="sb-cmdrow"><pre class="sb-cmd mono" data-sb-cmd>xerj</pre><button type="button" class="text-btn" data-sb-copy>COPY</button></div>
</div>`;
}
return `
<div class="sb-empty">
<div class="sb-ftitle">THIS BRAIN IS EMPTY — ONE COMMAND FILLS IT</div>
<p class="sb-ftext">Point it at a folder of notes, docs or exports. Every wiki-link,
relative link, reading sequence and folder neighborhood becomes a link that
remembers the exact quote that taught it — then this page turns into a ledger
of what your documents link to, replayable at any moment in time.</p>
<div class="sb-cmdrow"><pre class="sb-cmd mono" data-sb-cmd>xerj brain ~/your-folder</pre><button type="button" class="text-btn" data-sb-copy>COPY</button></div>
<p class="sb-ftext">The folder's name becomes the brain's name here. Detection is
deterministic — the same folder always produces the same brain, and re-running
only picks up what changed. Agents can assert links of their own through the
graph API once there is something to build on.</p>
<div class="mono faint">no upload · runs local · nothing is ever hard-deleted</div>
</div>`;
};
// ----- composition panels: controls / map / scrub -------------------
/**
* The header strip — brain switcher, trail crumbs, FIND — hoisted out
* of the ledger so it sits at the top of the page and survives no
* matter which panel below is empty or broken.
*/